-
Notifications
You must be signed in to change notification settings - Fork 56
/
main.c
480 lines (459 loc) · 16.7 KB
/
main.c
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
* Copyright (c) 2011 Alex Hornung <alex@alexhornung.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include "tcplay.h"
#ifndef SIGINFO
#define SIGINFO SIGUSR1
#endif
#define FLAG_LONG_FDE 0xff01
#define FLAG_LONG_USE_BACKUP 0xff02
#define FLAG_LONG_MOD 0xff04
#define FLAG_LONG_MOD_KF 0xff08
#define FLAG_LONG_MOD_PRF 0xff10
#define FLAG_LONG_MOD_NONE 0xff20
#define FLAG_LONG_MOD_TO_FILE 0xff40
#define FLAG_LONG_USE_HDR_FILE 0xfe01
#define FLAG_LONG_USE_HHDR_FILE 0xfe02
#define FLAG_LONG_NO_RETRIES 0xfabc
static
void
sig_handler(int sig)
{
if ((sig == SIGUSR1 || sig == SIGINFO) && (summary_fn != NULL))
summary_fn();
}
static
void
usage(void)
{
fprintf(stderr,
"usage: tcplay -c -d device [-g] [-z] [-w] [-a pbkdf_hash] [-b cipher]\n"
" [-f keyfile_hidden] [-k keyfile] [-x pbkdf_hash] [-y cipher]\n"
" tcplay -i -d device [-e] [-p] [-f keyfile_hidden] [-k keyfile]\n"
" [-s system_device] [--fde] [--use-backup]\n"
" [--use-hdr-file=hdr_file] [--use-hidden-hdr-file=hdr_file]\n"
" tcplay -m mapping -d device [-e] [-p] [-f keyfile_hidden] [-k keyfile]\n"
" [-s system_device] [--fde] [--use-backup] [--allow-trim]\n"
" [--use-hdr-file=hdr_file] [--use-hidden-hdr-file=hdr_file]\n"
" tcplay --modify -d device [-k keyfile] [--new-keyfile=keyfile]\n"
" [--new-pbkdf-prf=pbkdf_hash] [-s system_device] [--fde]\n"
" [--use-backup] [--save-hdr-to-file=hdr_file] [-w]\n"
" [--use-hdr-file=hdr_file] [--use-hidden-hdr-file=hdr_file]\n"
" tcplay --modify -d device [-k keyfile] --restore-from-backup-hdr [-w]\n"
" tcplay -j mapping\n"
" tcplay -u mapping\n"
" tcplay -h | -v\n"
"\n"
"Valid commands are:\n"
" -c, --create\n"
"\t Creates a new TC volume on the device specified by -d or --device.\n"
" -h, --help\n"
"\t Print help message and exit.\n"
" -i, --info\n"
"\t Gives information about the TC volume specified by -d or --device.\n"
" -j <mapping name>, --info-mapped=<mapping name>\n"
"\t Gives information about the mapped TC volume under the given mapping.\n"
" -m <mapping name>, --map=<mapping name>\n"
"\t Creates a dm-crypt mapping with the given name for the device\n"
"\t specified by -d or --device.\n"
" -u <mapping name>, --unmap=<mapping name>\n"
"\t Removes a dm-crypt mapping with the given name.\n"
" --modify\n"
"\t Changes the volume's passphrase, keyfile and optionally the hashing\n"
"\t function used for the PBKDF password derivation.\n"
" -v, --version\n"
"\t Print version message and exit.\n"
"\n"
"Valid options for --create are:\n"
" -a <pbkdf prf algorithm>, --pbkdf-prf=<pbkdf prf algorithm>\n"
"\t Specifies which hashing function to use for the PBKDF password\n"
"\t derivation when creating a new volume.\n"
"\t To see valid options, specify '-a help'.\n"
" -b <cipher>, --cipher=<cipher>\n"
"\t Specifies which cipher to use when creating a new TC volume.\n"
"\t To see valid options, specify '-b help'.\n"
" -g, --hidden\n"
"\t Specifies that the newly created volume will contain a hidden volume.\n"
" -x <pbkdf prf algorithm>, --pbkdf-prf=<pbkdf prf algorithm>\n"
"\t Specifies which hashing function to use for the PBKDF password\n"
"\t derivation when creating a new hidden volume. By default, the\n"
"\t same as for the outer volume will be used.\n"
"\t To see valid options, specify '-x help'.\n"
" -y <cipher>, --cipher=<cipher>\n"
"\t Specifies which cipher to use when creating a new hidden volume.\n"
"\t By default, the same as for the outer volume will be used.\n"
"\t To see valid options, specify '-y help'.\n"
" -z, --insecure-erase\n"
"\t Skips the erase of the disk. Possible security hazard.\n"
" -w, --weak-keys\n"
"\t Uses a weak source of entropy (urandom) for key material.\n"
"\t WARNING: This is a REALLY REALLY bad idea for anything but\n"
"\t testing.\n"
"\n"
"Valid options for --modify are:\n"
" --new-keyfile=<key file>\n"
"\t Specifies a key file to use for the password derivation, when\n"
"\t re-encrypting the header, can appear multiple times.\n"
" --new-pbkdf-prf=<pbkdf prf algorithm>\n"
"\t Specifies which hashing function to use for the PBKDF password\n"
"\t derivation when re-encrypting the header.\n"
"\t To see valid options, specify '-a help'.\n"
" -s <disk path>, --system-encryption=<disk path>\n"
"\t Specifies that the disk (e.g. /dev/da0) is using system encryption.\n"
" --fde\n"
"\t Specifies that the disk (e.g. /dev/da0) is using full disk encryption.\n"
" --use-backup\n"
"\t Uses the backup headers (at the end of the volume) instead of the\n"
"\t primary headers. Both normal and backup headers will be modified!\n"
"\t This is useful when your primary headers have been corrupted.\n"
" --use-hdr-file=<header file>\n"
"\t Use the header in the specified file instead of the main header on the\n"
"\t disk as source for the modify operation.\n"
" --use-hidden-hdr-file=<header file>\n"
"\t Use the header in the specified file instead of the hidden header on the\n"
"\t disk as source for the modify operation.\n"
" --restore-from-backup-hdr\n"
"\t Implies --use-backup, no new PBKDF hashing function, no new keyfiles\n"
"\t and no new passphrase.\n"
"\t In other words, this will simply restore both headers from the backup\n"
"\t header. This option cannot be used to restore from a backup header file.\n"
" -w, --weak-keys\n"
"\t Uses a weak source of entropy (urandom) for salt material. The\n"
"\t key material is not affected, as the master keys are kept intact.\n"
"\t WARNING: This is a bad idea for anything but testing.\n"
" --save-hdr-backup=<header file>\n"
"\t Saves the modified header in the specified file instead of updating\n"
"\t the header files on disk.\n"
"\n"
"Valid options for --info and --map are:\n"
" -e, --protect-hidden\n"
"\t Protect a hidden volume when mounting the outer volume.\n"
" -p, --prompt-passphrase\n"
"\t Immediately prompt for a passphrase even if a keyfile is supplied.\n"
" -s <disk path>, --system-encryption=<disk path>\n"
"\t Specifies that the disk (e.g. /dev/da0) is using system encryption.\n"
" -t, --allow-trim\n"
"\t Allow discards (TRIM command) on mapped volume.\n"
" --fde\n"
"\t Specifies that the disk (e.g. /dev/da0) is using full disk encryption.\n"
" --use-backup\n"
"\t Uses the backup headers (at the end of the volume) instead of the\n"
"\t primary headers.\n"
"\t This is useful when your primary headers have been corrupted.\n"
" --use-hdr-file=<header file>\n"
"\t Use the header in the specified file instead of the main header on the\n"
"\t disk.\n"
" --use-hidden-hdr-file=<header file>\n"
"\t Use the header in the specified file instead of the hidden header on the\n"
"\t disk.\n"
"\n"
"Valid options common to all commands are:\n"
" -d <device path>, --device=<device path>\n"
"\t Specifies the path to the volume to operate on (e.g. /dev/da0s1).\n"
" -f <key file>, --keyfile-hidden=<key file>\n"
"\t Specifies a key file to use for the hidden volume password derivation.\n"
"\t This option is only valid in combination with -e, --protect-hidden\n"
"\t or -g, --hidden.\n"
" -k <key file>, --keyfile=<key file>\n"
"\t Specifies a key file to use for the password derivation, can appear\n"
"\t multiple times.\n"
);
exit(EXIT_FAILURE);
}
static struct option longopts[] = {
{ "create", no_argument, NULL, 'c' },
{ "cipher", required_argument, NULL, 'b' },
{ "cipher-hidden", required_argument, NULL, 'y' },
{ "hidden", no_argument, NULL, 'g' },
{ "pbkdf-prf", required_argument, NULL, 'a' },
{ "pbkdf-prf-hidden", required_argument, NULL, 'x' },
{ "info", no_argument, NULL, 'i' },
{ "info-mapped", required_argument, NULL, 'j' },
{ "map", required_argument, NULL, 'm' },
{ "keyfile", required_argument, NULL, 'k' },
{ "keyfile-hidden", required_argument, NULL, 'f' },
{ "protect-hidden", no_argument, NULL, 'e' },
{ "device", required_argument, NULL, 'd' },
{ "prompt-passphrase", no_argument, NULL, 'p' },
{ "system-encryption", required_argument, NULL, 's' },
{ "allow-trim", no_argument, NULL, 't' },
{ "fde", no_argument, NULL, FLAG_LONG_FDE },
{ "use-backup", no_argument, NULL, FLAG_LONG_USE_BACKUP },
{ "use-hdr-file", required_argument, NULL, FLAG_LONG_USE_HDR_FILE },
{ "use-hidden-hdr-file",required_argument, NULL, FLAG_LONG_USE_HHDR_FILE },
{ "modify", no_argument, NULL, FLAG_LONG_MOD },
{ "new-keyfile", required_argument, NULL, FLAG_LONG_MOD_KF },
{ "new-pbkdf-prf", required_argument, NULL, FLAG_LONG_MOD_PRF },
{ "restore-from-backup-hdr", no_argument, NULL, FLAG_LONG_MOD_NONE },
{ "save-hdr-backup", required_argument, NULL, FLAG_LONG_MOD_TO_FILE },
{ "unmap", required_argument, NULL, 'u' },
{ "version", no_argument, NULL, 'v' },
{ "weak-keys", no_argument, NULL, 'w' },
{ "insecure-erase", no_argument, NULL, 'z' },
{ "help", no_argument, NULL, 'h' },
{ "no-retries", no_argument, NULL, FLAG_LONG_NO_RETRIES },
{ NULL, 0, NULL, 0 },
};
#define _set_str_opt(opt) \
do { \
if ((opts->opt = strdup_safe_mem(optarg)) == NULL) { \
fprintf(stderr, "Could not allocate safe mem.\n"); \
exit(EXIT_FAILURE); \
} \
} while(0)
int
main(int argc, char *argv[])
{
struct tcplay_opts *opts;
int ch, error;
int info_vol = 0, map_vol = 0,
unmap_vol = 0, info_map = 0,
create_vol = 0, modify_vol = 0;
if ((error = tc_play_init()) != 0) {
fprintf(stderr, "Initialization failed, exiting.");
exit(EXIT_FAILURE);
}
atexit(check_and_purge_safe_mem);
signal(SIGUSR1, sig_handler);
signal(SIGINFO, sig_handler);
if ((opts = opts_init()) == NULL) {
fprintf(stderr, "Initialization failed (opts), exiting.");
exit(EXIT_FAILURE);
}
opts->interactive = 1;
while ((ch = getopt_long(argc, argv, "a:b:cd:ef:ghij:k:m:ps:tu:vwx:y:z",
longopts, NULL)) != -1) {
switch(ch) {
case 'a':
if (opts->prf_algo != NULL)
usage();
if ((opts->prf_algo = check_prf_algo(optarg, 0, 0)) == NULL) {
if (strcmp(optarg, "help") == 0)
exit(EXIT_SUCCESS);
else
usage();
/* NOT REACHED */
}
break;
case 'b':
if (opts->cipher_chain != NULL)
usage();
if ((opts->cipher_chain = check_cipher_chain(optarg, 0)) == NULL) {
if (strcmp(optarg, "help") == 0)
exit(EXIT_SUCCESS);
else
usage();
/* NOT REACHED */
}
break;
case 'c':
create_vol = 1;
break;
case 'd':
_set_str_opt(dev);
break;
case 'e':
opts->protect_hidden = 1;
break;
case 'f':
if ((error = opts_add_keyfile_hidden(opts, optarg)) != 0) {
fprintf(stderr, "Could not add keyfile: %s\n", optarg);
exit(EXIT_FAILURE);
}
break;
case 'g':
opts->hidden = 1;
break;
case 'i':
info_vol = 1;
break;
case 'j':
info_map = 1;
_set_str_opt(map_name);
break;
case 'k':
if ((error = opts_add_keyfile(opts, optarg)) != 0) {
fprintf(stderr, "Could not add keyfile: %s\n", optarg);
exit(EXIT_FAILURE);
}
break;
case 'm':
map_vol = 1;
_set_str_opt(map_name);
break;
case 'p':
opts->prompt_passphrase = 1;
break;
case 's':
opts->flags |= TC_FLAG_SYS;
_set_str_opt(sys_dev);
break;
case 't':
opts->flags |= TC_FLAG_ALLOW_TRIM;
break;
case 'u':
unmap_vol = 1;
_set_str_opt(map_name);
break;
case 'v':
printf("tcplay v%d.%d\n", MAJ_VER, MIN_VER);
exit(EXIT_SUCCESS);
/* NOT REACHED */
case 'w':
fprintf(stderr, "WARNING: Using urandom as source of "
"entropy for key material is a really bad idea.\n");
opts->weak_keys_and_salt = 1;
break;
case 'x':
if (opts->h_prf_algo != NULL)
usage();
if ((opts->h_prf_algo = check_prf_algo(optarg, 0, 0)) == NULL) {
if (strcmp(optarg, "help") == 0)
exit(EXIT_SUCCESS);
else
usage();
/* NOT REACHED */
}
break;
case 'y':
if (opts->h_cipher_chain != NULL)
usage();
if ((opts->h_cipher_chain = check_cipher_chain(optarg, 0)) == NULL) {
if (strcmp(optarg, "help") == 0)
exit(EXIT_SUCCESS);
else
usage();
/* NOT REACHED */
}
break;
case 'z':
opts->secure_erase = 0;
break;
case FLAG_LONG_FDE:
opts->flags |= TC_FLAG_FDE;
break;
case FLAG_LONG_USE_BACKUP:
opts->flags |= TC_FLAG_BACKUP;
break;
case FLAG_LONG_USE_HDR_FILE:
opts->flags |= TC_FLAG_HDR_FROM_FILE;
_set_str_opt(hdr_file_in);
break;
case FLAG_LONG_USE_HHDR_FILE:
opts->flags |= TC_FLAG_H_HDR_FROM_FILE;
_set_str_opt(h_hdr_file_in);
break;
case FLAG_LONG_MOD:
modify_vol = 1;
break;
case FLAG_LONG_MOD_KF:
if ((error = opts_add_keyfile_new(opts, optarg)) != 0) {
fprintf(stderr, "Could not add keyfile: %s\n", optarg);
exit(EXIT_FAILURE);
}
break;
case FLAG_LONG_MOD_PRF:
if (opts->new_prf_algo != NULL)
usage();
if ((opts->new_prf_algo = check_prf_algo(optarg, 0, 0)) == NULL) {
if (strcmp(optarg, "help") == 0)
exit(EXIT_SUCCESS);
else
usage();
/* NOT REACHED */
}
break;
case FLAG_LONG_MOD_NONE:
opts->new_prf_algo = NULL;
opts->flags |= TC_FLAG_ONLY_RESTORE;
opts->flags |= TC_FLAG_BACKUP;
break;
case FLAG_LONG_MOD_TO_FILE:
opts->flags |= TC_FLAG_SAVE_TO_FILE;
_set_str_opt(hdr_file_out);
break;
case FLAG_LONG_NO_RETRIES:
opts->retries = 1;
break;
case 'h':
case '?':
default:
usage();
/* NOT REACHED */
}
}
argc -= optind;
argv += optind;
/* Check arguments */
if (!(((map_vol || info_vol || create_vol || modify_vol) && opts->dev != NULL) ||
((unmap_vol || info_map) && opts->map_name != NULL)) ||
(TC_FLAG_SET(opts->flags, SYS) && TC_FLAG_SET(opts->flags, FDE)) ||
(map_vol + info_vol + create_vol + unmap_vol + info_map + modify_vol > 1) ||
(opts->hidden && !create_vol) ||
(TC_FLAG_SET(opts->flags, SYS) && (opts->sys_dev == NULL)) ||
(TC_FLAG_SET(opts->flags, ONLY_RESTORE) && (opts->n_newkeyfiles > 0 || opts->new_prf_algo != NULL)) ||
(TC_FLAG_SET(opts->flags, BACKUP) && (opts->sys_dev != NULL || TC_FLAG_SET(opts->flags, FDE))) ||
(map_vol && (opts->map_name == NULL)) ||
(unmap_vol && (opts->map_name == NULL)) ||
(!modify_vol && opts->n_newkeyfiles > 0) ||
(!modify_vol && opts->new_prf_algo != NULL) ||
(!modify_vol && TC_FLAG_SET(opts->flags, ONLY_RESTORE)) ||
(!modify_vol && TC_FLAG_SET(opts->flags, SAVE_TO_FILE)) ||
(!(opts->protect_hidden || create_vol) && opts->n_hkeyfiles > 0)) {
usage();
/* NOT REACHED */
}
/* Create a new volume */
if (create_vol) {
error = create_volume(opts);
if (error) {
tc_log(1, "could not create new volume on %s\n", opts->dev);
}
} else if (info_map) {
error = info_mapped_volume(opts);
} else if (info_vol) {
error = info_volume(opts);
} else if (map_vol) {
error = map_volume(opts);
} else if (unmap_vol) {
error = dm_teardown(opts->map_name, NULL);
} else if (modify_vol) {
error = modify_volume(opts);
}
return error;
}
#undef _set_str_opt