1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
/*
* Example of LUKS2 token storing third party metadata (EXAMPLE)
*
* Copyright (C) 2016-2019 Milan Broz <gmazyland@gmail.com>
*
* Use:
* - generate LUKS device
* - store passphrase used in previous step remotely (single line w/o \n\r)
* - add new token using this example
* - activate device with passphrase recovered remotely using the example
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <json-c/json.h>
#include <libssh/libssh.h>
#include <libssh/sftp.h>
#include "libcryptsetup.h"
#define TOKEN_NUM 0
#define TOKEN_TYPE "sshkeytest"
#define PASSWORD_LENGTH 8192
static json_object *get_token_jobj(struct crypt_device *cd, int token)
{
const char *json_slot;
if (crypt_token_json_get(cd, token, &json_slot))
return NULL;
return json_tokener_parse(json_slot);
}
static int read_remote_passphrase(struct crypt_device *cd, const char *host,
const char *user, const char *path,
char *password, size_t password_size)
{
ssh_session ssh = NULL;
sftp_session sftp = NULL;
sftp_file file = NULL;
ssh_key pkey = NULL;
int r, port = 22;
ssh = ssh_new();
if (!ssh)
return -EINVAL;
ssh_options_set(ssh, SSH_OPTIONS_HOST, host);
ssh_options_set(ssh, SSH_OPTIONS_USER, user);
ssh_options_set(ssh, SSH_OPTIONS_PORT, &port);
r = ssh_connect(ssh);
if (r != SSH_OK) {
crypt_log(cd, CRYPT_LOG_ERROR, "Connection failed: ");
goto out;
}
r = ssh_is_server_known(ssh);
if (r != SSH_SERVER_KNOWN_OK) {
crypt_log(cd, CRYPT_LOG_ERROR, "Server not known: ");
r = SSH_AUTH_ERROR;
goto out;
}
r = ssh_pki_import_privkey_file("/home/user/.ssh/id_rsa", NULL, NULL, NULL, &pkey);
if (r != SSH_OK) {
crypt_log(cd, CRYPT_LOG_ERROR, "error\n");
r = SSH_AUTH_ERROR;
goto out;
}
r = ssh_userauth_publickey(ssh, user, pkey);
/* or r = ssh_userauth_publickey_auto(ssh, user, NULL); */
if (r != SSH_AUTH_SUCCESS) {
crypt_log(cd, CRYPT_LOG_ERROR, "Public key authentication error: ");
goto out;
}
sftp = sftp_new(ssh);
if (!sftp) {
crypt_log(cd, CRYPT_LOG_ERROR, "Cannot create sftp session: ");
r = SSH_FX_FAILURE;
goto out;
}
r = sftp_init(sftp);
if (r != SSH_OK) {
crypt_log(cd, CRYPT_LOG_ERROR, "Cannot init sftp session: ");
goto out;
}
file = sftp_open(sftp, path, O_RDONLY, 0);
if (!file) {
crypt_log(cd, CRYPT_LOG_ERROR, "Cannot create sftp session: ");
r = SSH_FX_FAILURE;
goto out;
}
r = sftp_read(file, password, password_size);
if (r < 1 || (size_t)r >= password_size) {
crypt_log(cd, CRYPT_LOG_ERROR, "Cannot read remote password: ");
r = SSH_FX_FAILURE;
goto out;
}
r = SSH_OK;
out:
if (r != SSH_OK) {
crypt_log(cd, CRYPT_LOG_ERROR, ssh_get_error(ssh));
crypt_log(cd, CRYPT_LOG_ERROR, "\n");
}
if (pkey)
ssh_key_free(pkey);
if (file)
sftp_close(file);
if (sftp)
sftp_free(sftp);
ssh_disconnect(ssh);
ssh_free(ssh);
return r == SSH_OK ? 0 : -EINVAL;
}
static int token_add(const char *device, const char *server,
const char *user, const char *path)
{
struct crypt_device *cd = NULL;
json_object *jobj = NULL, *jobj_keyslots;
int r;
r = crypt_init(&cd, device);
if (r < 0)
return EXIT_FAILURE;
r = crypt_load(cd, CRYPT_LUKS2, NULL);
if (r < 0) {
crypt_free(cd);
return EXIT_FAILURE;
}
jobj = json_object_new_object();
/* 'type' is mandatory field */
json_object_object_add(jobj, "type", json_object_new_string(TOKEN_TYPE));
/* 'keyslots' is mandatory field (may be empty) */
jobj_keyslots = json_object_new_array();
json_object_array_add(jobj_keyslots, json_object_new_string("0"));
json_object_array_add(jobj_keyslots, json_object_new_string("1"));
json_object_object_add(jobj, "keyslots", jobj_keyslots);
/* third party values */
json_object_object_add(jobj, "ssh_server", json_object_new_string(server));
json_object_object_add(jobj, "ssh_user", json_object_new_string(user));
json_object_object_add(jobj, "ssh_path", json_object_new_string(path));
r = crypt_token_json_set(cd, TOKEN_NUM, json_object_to_json_string_ext(jobj, JSON_C_TO_STRING_PLAIN));
crypt_free(cd);
json_object_put(jobj);
return EXIT_SUCCESS;
}
static int download_remote_password(struct crypt_device *cd, char *password, size_t password_len)
{
json_object *jobj_server, *jobj_user, *jobj_path, *jobj_keyslot;
/* get token json object representation as string */
jobj_keyslot = get_token_jobj(cd, TOKEN_NUM);
if (!jobj_keyslot)
return -EINVAL;
/* extract third party metadata necessary to extract passphrase remotely */
json_object_object_get_ex(jobj_keyslot, "ssh_server", &jobj_server);
json_object_object_get_ex(jobj_keyslot, "ssh_user", &jobj_user);
json_object_object_get_ex(jobj_keyslot, "ssh_path", &jobj_path);
return read_remote_passphrase(cd, json_object_get_string(jobj_server),
json_object_get_string(jobj_user),
json_object_get_string(jobj_path),
password, password_len);
}
static int open_by_remote_password(const char *device, const char *name)
{
char password[PASSWORD_LENGTH+1];
struct crypt_device *cd = NULL;
int r;
r = crypt_init(&cd, device);
if (r < 0)
return EXIT_FAILURE;
r = crypt_load(cd, CRYPT_LUKS2, NULL);
if (r < 0) {
crypt_free(cd);
return EXIT_FAILURE;
}
/* custom routines to acquire password */
r = download_remote_password(cd, password, sizeof(password));
if (r < 0) {
crypt_free(cd);
return EXIT_FAILURE;
}
password[PASSWORD_LENGTH] = '\0';
/* open first genuine LUKS2 keyslot available provided the password matches */
/* for the sake of simplicity password is a string */
r = crypt_activate_by_passphrase(cd, name, CRYPT_ANY_SLOT, password, strlen(password), 0);
crypt_free(cd);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
static void keyslot_help(void)
{
printf("Use parameters:\n add device server user path\n"
" open device name\n");
exit(1);
}
int main(int argc, char *argv[])
{
crypt_set_debug_level(CRYPT_LOG_DEBUG);
/* Adding slot to device */
if (argc == 6 && !strcmp("add", argv[1]))
return token_add(argv[2], argv[3], argv[4], argv[5]);
/* Password check without activation */
if (argc == 3 && !strcmp("open", argv[1]))
return open_by_remote_password(argv[2], NULL);
/* Password check with activation (requires root) */
if (argc == 4 && !strcmp("open", argv[1]))
return open_by_remote_password(argv[2], argv[3]);
keyslot_help();
return 1;
}
|