Debian Codegraph @

src/cryptenroll/cryptenroll.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/mman.h>
4
5 #include "sd-device.h"
6
7 #include "blockdev-list.h"
8 #include "blockdev-util.h"
9 #include "build.h"
10 #include "cryptenroll.h"
11 #include "cryptenroll-fido2.h"
12 #include "cryptenroll-list.h"
13 #include "cryptenroll-password.h"
14 #include "cryptenroll-pkcs11.h"
15 #include "cryptenroll-recovery.h"
16 #include "cryptenroll-tpm2.h"
17 #include "cryptenroll-wipe.h"
18 #include "cryptsetup-util.h"
19 #include "extract-word.h"
20 #include "fileio.h"
21 #include "format-table.h"
22 #include "libfido2-util.h"
23 #include "log.h"
24 #include "main-func.h"
25 #include "options.h"
26 #include "pager.h"
27 #include "parse-argument.h"
28 #include "parse-util.h"
29 #include "pkcs11-util.h"
30 #include "pretty-print.h"
31 #include "process-util.h"
32 #include "string-table.h"
33 #include "string-util.h"
34 #include "tpm2-pcr.h"
35 #include "tpm2-util.h"
36
37 static EnrollType arg_enroll_type = _ENROLL_TYPE_INVALID;
38 static char *arg_unlock_keyfile = NULL;
39 static UnlockType arg_unlock_type = UNLOCK_PASSWORD;
40 static char *arg_unlock_fido2_device = NULL;
41 static char *arg_unlock_tpm2_device = NULL;
42 static char *arg_pkcs11_token_uri = NULL;
43 static char *arg_fido2_device = NULL;
44 static char *arg_fido2_salt_file = NULL;
45 static bool arg_fido2_parameters_in_header = true;
46 static char *arg_tpm2_device = NULL;
47 static uint32_t arg_tpm2_seal_key_handle = 0;
48 static char *arg_tpm2_device_key = NULL;
49 static Tpm2PCRValue *arg_tpm2_hash_pcr_values = NULL;
50 static size_t arg_tpm2_n_hash_pcr_values = 0;
51 static bool arg_tpm2_pin = false;
52 static char *arg_tpm2_public_key = NULL;
53 static bool arg_tpm2_load_public_key = true;
54 static uint32_t arg_tpm2_public_key_pcr_mask = 0;
55 static char *arg_tpm2_signature = NULL;
56 static char *arg_tpm2_pcrlock = NULL;
57 static char *arg_node = NULL;
58 static PagerFlags arg_pager_flags = 0;
59 static int *arg_wipe_slots = NULL;
60 static size_t arg_n_wipe_slots = 0;
61 static WipeScope arg_wipe_slots_scope = WIPE_EXPLICIT;
62 static unsigned arg_wipe_slots_mask = 0; /* Bitmask of (1U << EnrollType), for wiping all slots of specific types */
63 static Fido2EnrollFlags arg_fido2_lock_with = FIDO2ENROLL_PIN | FIDO2ENROLL_UP;
64 #if HAVE_LIBFIDO2
65 static int arg_fido2_cred_alg = COSE_ES256;
66 #else
67 static int arg_fido2_cred_alg = 0;
68 #endif
69
70 assert_cc(sizeof(arg_wipe_slots_mask) * 8 >= _ENROLL_TYPE_MAX);
71
72 STATIC_DESTRUCTOR_REGISTER(arg_unlock_keyfile, freep);
73 STATIC_DESTRUCTOR_REGISTER(arg_unlock_fido2_device, freep);
74 STATIC_DESTRUCTOR_REGISTER(arg_unlock_tpm2_device, freep);
75 STATIC_DESTRUCTOR_REGISTER(arg_pkcs11_token_uri, freep);
76 STATIC_DESTRUCTOR_REGISTER(arg_fido2_device, freep);
77 STATIC_DESTRUCTOR_REGISTER(arg_fido2_salt_file, freep);
78 STATIC_DESTRUCTOR_REGISTER(arg_tpm2_device, freep);
79 STATIC_DESTRUCTOR_REGISTER(arg_tpm2_device_key, freep);
80 STATIC_DESTRUCTOR_REGISTER(arg_tpm2_hash_pcr_values, freep);
81 STATIC_DESTRUCTOR_REGISTER(arg_tpm2_public_key, freep);
82 STATIC_DESTRUCTOR_REGISTER(arg_tpm2_signature, freep);
83 STATIC_DESTRUCTOR_REGISTER(arg_tpm2_pcrlock, freep);
84 STATIC_DESTRUCTOR_REGISTER(arg_node, freep);
85 STATIC_DESTRUCTOR_REGISTER(arg_wipe_slots, freep);
86
87 static bool wipe_requested(void) {
88 return arg_n_wipe_slots > 0 ||
89 arg_wipe_slots_scope != WIPE_EXPLICIT ||
90 arg_wipe_slots_mask != 0;
91 }
92
93 static const char* const enroll_type_table[_ENROLL_TYPE_MAX] = {
94 [ENROLL_PASSWORD] = "password",
95 [ENROLL_RECOVERY] = "recovery",
96 [ENROLL_PKCS11] = "pkcs11",
97 [ENROLL_FIDO2] = "fido2",
98 [ENROLL_TPM2] = "tpm2",
99 };
100
101 DEFINE_STRING_TABLE_LOOKUP(enroll_type, EnrollType);
102
103 static const char *const luks2_token_type_table[_ENROLL_TYPE_MAX] = {
104 /* ENROLL_PASSWORD has no entry here, as slots of this type do not have a token in the LUKS2 header */
105 [ENROLL_RECOVERY] = "systemd-recovery",
106 [ENROLL_PKCS11] = "systemd-pkcs11",
107 [ENROLL_FIDO2] = "systemd-fido2",
108 [ENROLL_TPM2] = "systemd-tpm2",
109 };
110
111 DEFINE_STRING_TABLE_LOOKUP(luks2_token_type, EnrollType);
112
113 static int determine_default_node(void) {
114 int r;
115
116 /* If no device is specified we'll default to the backing device of /var/.
117 *
118 * Why /var/ and not just / you ask?
119 *
120 * On most systems /var/ is going to be on the root fs, hence the outcome is usually the same.
121 *
122 * However, on systems where / and /var/ are separate it makes more sense to default to /var/ because
123 * that's where the persistent and variable data is placed (i.e. where LUKS should be used) while /
124 * doesn't really have to be variable and could as well be immutable or ephemeral. Hence /var/ should
125 * be a better default.
126 *
127 * Or to say this differently: it makes sense to support well systems with /var/ being on /. It also
128 * makes sense to support well systems with them being separate, and /var/ being variable and
129 * persistent. But any other kind of system appears much less interesting to support, and in that
130 * case people should just specify the device name explicitly. */
131
132 dev_t devno;
133 r = get_block_device("/var", &devno);
134 if (r < 0)
135 return log_error_errno(r, "Failed to determine block device backing /var/: %m");
136 if (r == 0)
137 return log_error_errno(SYNTHETIC_ERRNO(ENXIO),
138 "File system /var/ is on not backed by a (single) whole block device.");
139
140 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
141 r = sd_device_new_from_devnum(&dev, 'b', devno);
142 if (r < 0)
143 return log_error_errno(r, "Unable to access backing block device for /var/: %m");
144
145 const char *dm_uuid;
146 r = sd_device_get_property_value(dev, "DM_UUID", &dm_uuid);
147 if (r == -ENOENT)
148 return log_error_errno(r, "Backing block device of /var/ is not a DM device: %m");
149 if (r < 0)
150 return log_error_errno(r, "Unable to query DM_UUID udev property of backing block device for /var/: %m");
151
152 if (!startswith(dm_uuid, "CRYPT-LUKS2-"))
153 return log_error_errno(SYNTHETIC_ERRNO(ENXIO), "Block device backing /var/ is not a LUKS2 device.");
154
155 _cleanup_(sd_device_unrefp) sd_device *origin = NULL;
156 r = block_device_get_originating(dev, &origin, /* recursive= */ false);
157 if (r < 0)
158 return log_error_errno(r, "Failed to get originating device of LUKS2 device backing /var/: %m");
159
160 const char *dp;
161 r = sd_device_get_devname(origin, &dp);
162 if (r < 0)
163 return log_error_errno(r, "Failed to get device path for LUKS2 device backing /var/: %m");
164
165 r = free_and_strdup_warn(&arg_node, dp);
166 if (r < 0)
167 return r;
168
169 log_info("No device specified, defaulting to '%s'.", arg_node);
170 return 0;
171 }
172
173 static int parse_wipe_slot(const char *arg) {
174 int r;
175
176 assert(arg);
177
178 if (isempty(arg)) {
179 arg_wipe_slots_mask = 0;
180 arg_wipe_slots_scope = WIPE_EXPLICIT;
181 return 0;
182 }
183
184 for (const char *p = arg;;) {
185 _cleanup_free_ char *slot = NULL;
186
187 r = extract_first_word(&p, &slot, ",", EXTRACT_DONT_COALESCE_SEPARATORS);
188 if (r == 0)
189 return 0;
190 if (r < 0)
191 return log_error_errno(r, "Failed to parse slot list: %s", arg);
192
193 if (streq(slot, "all"))
194 arg_wipe_slots_scope = WIPE_ALL;
195 else if (streq(slot, "empty")) {
196 if (arg_wipe_slots_scope != WIPE_ALL) /* if "all" was specified before, that wins */
197 arg_wipe_slots_scope = WIPE_EMPTY_PASSPHRASE;
198 } else if (streq(slot, "password"))
199 arg_wipe_slots_mask |= 1U << ENROLL_PASSWORD;
200 else if (streq(slot, "recovery"))
201 arg_wipe_slots_mask |= 1U << ENROLL_RECOVERY;
202 else if (streq(slot, "pkcs11"))
203 arg_wipe_slots_mask |= 1U << ENROLL_PKCS11;
204 else if (streq(slot, "fido2"))
205 arg_wipe_slots_mask |= 1U << ENROLL_FIDO2;
206 else if (streq(slot, "tpm2"))
207 arg_wipe_slots_mask |= 1U << ENROLL_TPM2;
208 else {
209 unsigned n;
210
211 r = safe_atou(slot, &n);
212 if (r < 0)
213 return log_error_errno(r, "Failed to parse slot index: %s", slot);
214 if (n > INT_MAX)
215 return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Slot index out of range: %u", n);
216
217 if (!GREEDY_REALLOC(arg_wipe_slots, arg_n_wipe_slots + 1))
218 return log_oom();
219
220 arg_wipe_slots[arg_n_wipe_slots++] = (int) n;
221 }
222 }
223 }
224
225 static int help(void) {
226 _cleanup_free_ char *link = NULL;
227 int r;
228
229 pager_open(arg_pager_flags);
230
231 r = terminal_urlify_man("systemd-cryptenroll", "1", &link);
232 if (r < 0)
233 return log_oom();
234
235 static const char* const groups[] = {
236 NULL,
237 "Unlocking",
238 "Simple Enrollment",
239 "PKCS#11 Enrollment",
240 "FIDO2 Enrollment",
241 "TPM2 Enrollment",
242 };
243
244 Table *tables[ELEMENTSOF(groups)] = {};
245 CLEANUP_ELEMENTS(tables, table_unref_array_clear);
246
247 for (size_t i = 0; i < ELEMENTSOF(groups); i++) {
248 r = option_parser_get_help_table_group(groups[i], &tables[i]);
249 if (r < 0)
250 return r;
251 }
252
253 (void) table_sync_column_widths(0, tables[0], tables[1], tables[2], tables[3], tables[4], tables[5]);
254
255 printf("%s [OPTIONS...] [BLOCK-DEVICE]\n\n"
256 "%sEnroll a security token or authentication credential to a LUKS volume.%s\n",
257 program_invocation_short_name,
258 ansi_highlight(),
259 ansi_normal());
260
261 for (size_t i = 0; i < ELEMENTSOF(groups); i++) {
262 printf("\n%s%s:%s\n", ansi_underline(), groups[i] ?: "Options", ansi_normal());
263
264 r = table_print_or_warn(tables[i]);
265 if (r < 0)
266 return r;
267 }
268
269 printf("\nSee the %s for details.\n", link);
270 return 0;
271 }
272
273 static int parse_argv(int argc, char *argv[]) {
274 bool auto_public_key_pcr_mask = true, auto_pcrlock = true;
275
276 assert(argc >= 0);
277 assert(argv);
278
279 OptionParser opts = { argc, argv };
280 int r;
281
282 FOREACH_OPTION_OR_RETURN(c, &opts)
283 switch (c) {
284
285 OPTION_COMMON_HELP:
286 return help();
287
288 OPTION_COMMON_VERSION:
289 return version();
290
291 OPTION_COMMON_NO_PAGER:
292 arg_pager_flags |= PAGER_DISABLE;
293 break;
294
295 OPTION_LONG("list-devices", NULL,
296 "List candidate block devices to operate on"):
297 return blockdev_list(BLOCKDEV_LIST_SHOW_SYMLINKS|BLOCKDEV_LIST_REQUIRE_LUKS,
298 /* ret_devices= */ NULL,
299 /* ret_n_devices= */ NULL);
300
301 OPTION_LONG("wipe-slot", "SLOT1,SLOT2,…",
302 "Wipe specified slots"):
303 r = parse_wipe_slot(opts.arg);
304 if (r < 0)
305 return r;
306 break;
307
308 OPTION_GROUP("Unlocking"): {}
309
310 OPTION_LONG("unlock-key-file", "PATH",
311 "Use a file to unlock the volume"):
312 if (arg_unlock_type != UNLOCK_PASSWORD)
313 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
314 "Multiple unlock methods specified at once, refusing.");
315
316 r = parse_path_argument(opts.arg, /* suppress_root= */ true, &arg_unlock_keyfile);
317 if (r < 0)
318 return r;
319
320 arg_unlock_type = UNLOCK_KEYFILE;
321 break;
322
323 OPTION_LONG("unlock-fido2-device", "PATH",
324 "Use a FIDO2 device to unlock the volume"): {
325 _cleanup_free_ char *device = NULL;
326
327 if (arg_unlock_type != UNLOCK_PASSWORD)
328 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
329 "Multiple unlock methods specified at once, refusing.");
330
331 assert(!arg_unlock_fido2_device);
332
333 if (!streq(opts.arg, "auto")) {
334 device = strdup(opts.arg);
335 if (!device)
336 return log_oom();
337 }
338
339 arg_unlock_type = UNLOCK_FIDO2;
340 arg_unlock_fido2_device = TAKE_PTR(device);
341 break;
342 }
343
344 OPTION_LONG("unlock-tpm2-device", "PATH",
345 "Use a TPM2 device to unlock the volume"): {
346 _cleanup_free_ char *device = NULL;
347
348 if (arg_unlock_type != UNLOCK_PASSWORD)
349 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
350 "Multiple unlock methods specified at once, refusing.");
351
352 assert(!arg_unlock_tpm2_device);
353
354 if (!streq(opts.arg, "auto")) {
355 device = strdup(opts.arg);
356 if (!device)
357 return log_oom();
358 }
359
360 arg_unlock_type = UNLOCK_TPM2;
361 arg_unlock_tpm2_device = TAKE_PTR(device);
362 break;
363 }
364
365 OPTION_GROUP("Simple Enrollment"): {}
366
367 OPTION_LONG("password", NULL,
368 "Enroll a user-supplied password"):
369 if (arg_enroll_type >= 0)
370 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
371 "Multiple operations specified at once, refusing.");
372
373 arg_enroll_type = ENROLL_PASSWORD;
374 break;
375
376 OPTION_LONG("recovery-key", NULL,
377 "Enroll a recovery key"):
378 if (arg_enroll_type >= 0)
379 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
380 "Multiple operations specified at once, refusing.");
381
382 arg_enroll_type = ENROLL_RECOVERY;
383 break;
384
385 OPTION_GROUP("PKCS#11 Enrollment"): {}
386
387 OPTION_LONG("pkcs11-token-uri", "URI|auto|list",
388 "Enroll a PKCS#11 security token or list them"): {
389 _cleanup_free_ char *uri = NULL;
390
391 if (streq(opts.arg, "list"))
392 return pkcs11_list_tokens();
393
394 if (arg_enroll_type >= 0 || arg_pkcs11_token_uri)
395 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
396 "Multiple operations specified at once, refusing.");
397
398 if (streq(opts.arg, "auto")) {
399 r = pkcs11_find_token_auto(&uri);
400 if (r < 0)
401 return r;
402 } else {
403 if (!pkcs11_uri_valid(opts.arg))
404 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Not a valid PKCS#11 URI: %s", opts.arg);
405
406 uri = strdup(opts.arg);
407 if (!uri)
408 return log_oom();
409 }
410
411 arg_enroll_type = ENROLL_PKCS11;
412 arg_pkcs11_token_uri = TAKE_PTR(uri);
413 break;
414 }
415
416 OPTION_GROUP("FIDO2 Enrollment"): {}
417
418 OPTION_LONG("fido2-device", "PATH|auto|list",
419 "Enroll a FIDO2-HMAC security token or list them"): {
420 _cleanup_free_ char *device = NULL;
421
422 if (streq(opts.arg, "list"))
423 return fido2_list_devices();
424
425 if (arg_enroll_type >= 0 || arg_fido2_device)
426 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
427 "Multiple operations specified at once, refusing.");
428
429 if (!streq(opts.arg, "auto")) {
430 device = strdup(opts.arg);
431 if (!device)
432 return log_oom();
433 }
434
435 arg_enroll_type = ENROLL_FIDO2;
436 arg_fido2_device = TAKE_PTR(device);
437 break;
438 }
439
440 OPTION_LONG("fido2-salt-file", "PATH",
441 "Use salt from a file instead of generating one"):
442 r = parse_path_argument(opts.arg, /* suppress_root= */ true, &arg_fido2_salt_file);
443 if (r < 0)
444 return r;
445 break;
446
447 OPTION_LONG("fido2-parameters-in-header", "BOOL",
448 "Whether to store FIDO2 parameters in the LUKS2 header"):
449 r = parse_boolean_argument("--fido2-parameters-in-header=", opts.arg, &arg_fido2_parameters_in_header);
450 if (r < 0)
451 return r;
452 break;
453
454 OPTION_LONG("fido2-credential-algorithm", "STRING",
455 "Specify COSE algorithm for FIDO2 credential"):
456 r = parse_fido2_algorithm(opts.arg, &arg_fido2_cred_alg);
457 if (r < 0)
458 return log_error_errno(r, "Failed to parse COSE algorithm: %s", opts.arg);
459 break;
460
461 OPTION_LONG("fido2-with-client-pin", "BOOL",
462 "Whether to require entering a PIN to unlock the volume"):
463 r = parse_boolean_argument("--fido2-with-client-pin=", opts.arg, NULL);
464 if (r < 0)
465 return r;
466 SET_FLAG(arg_fido2_lock_with, FIDO2ENROLL_PIN, r);
467 break;
468
469 OPTION_LONG("fido2-with-user-presence", "BOOL",
470 "Whether to require user presence to unlock the volume"):
471 r = parse_boolean_argument("--fido2-with-user-presence=", opts.arg, NULL);
472 if (r < 0)
473 return r;
474 SET_FLAG(arg_fido2_lock_with, FIDO2ENROLL_UP, r);
475 break;
476
477 OPTION_LONG("fido2-with-user-verification", "BOOL",
478 "Whether to require user verification to unlock the volume"):
479 r = parse_boolean_argument("--fido2-with-user-verification=", opts.arg, NULL);
480 if (r < 0)
481 return r;
482 SET_FLAG(arg_fido2_lock_with, FIDO2ENROLL_UV, r);
483 break;
484
485 OPTION_GROUP("TPM2 Enrollment"): {}
486
487 OPTION_LONG("tpm2-device", "PATH|auto|list",
488 "Enroll a TPM2 device or list them"): {
489 _cleanup_free_ char *device = NULL;
490
491 if (streq(opts.arg, "list"))
492 return tpm2_list_devices(/* legend= */ true, /* quiet= */ false);
493
494 if (arg_enroll_type >= 0 || arg_tpm2_device)
495 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
496 "Multiple operations specified at once, refusing.");
497
498 if (!streq(opts.arg, "auto")) {
499 device = strdup(opts.arg);
500 if (!device)
501 return log_oom();
502 }
503
504 arg_enroll_type = ENROLL_TPM2;
505 arg_tpm2_device = TAKE_PTR(device);
506 break;
507 }
508
509 OPTION_LONG("tpm2-device-key", "PATH",
510 "Enroll a TPM2 device using its public key"):
511 if (arg_enroll_type >= 0 || arg_tpm2_device_key)
512 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
513 "Multiple operations specified at once, refusing.");
514
515 r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_tpm2_device_key);
516 if (r < 0)
517 return r;
518
519 arg_enroll_type = ENROLL_TPM2;
520 break;
521
522 OPTION_LONG("tpm2-seal-key-handle", "HANDLE",
523 "Specify handle of key to use for sealing"):
524 r = safe_atou32_full(opts.arg, 16, &arg_tpm2_seal_key_handle);
525 if (r < 0)
526 return log_error_errno(r, "Could not parse TPM2 seal key handle index '%s': %m", opts.arg);
527 break;
528
529 OPTION_LONG("tpm2-pcrs", "PCR1+PCR2+PCR3+…",
530 "Specify TPM2 PCRs to seal against"):
531 r = tpm2_parse_pcr_argument_append(opts.arg, &arg_tpm2_hash_pcr_values, &arg_tpm2_n_hash_pcr_values);
532 if (r < 0)
533 return r;
534 break;
535
536 OPTION_LONG("tpm2-public-key", "PATH",
537 "Enroll signed TPM2 PCR policy against PEM public key"):
538 /* an empty argument disables loading a public key */
539 if (isempty(opts.arg)) {
540 arg_tpm2_load_public_key = false;
541 arg_tpm2_public_key = mfree(arg_tpm2_public_key);
542 break;
543 }
544
545 r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_tpm2_public_key);
546 if (r < 0)
547 return r;
548 arg_tpm2_load_public_key = true;
549 break;
550
551 OPTION_LONG("tpm2-public-key-pcrs", "PCR1+PCR2+PCR3+…",
552 "Enroll signed TPM2 PCR policy for specified TPM2 PCRs"):
553 auto_public_key_pcr_mask = false;
554 r = tpm2_parse_pcr_argument_to_mask(opts.arg, &arg_tpm2_public_key_pcr_mask);
555 if (r < 0)
556 return r;
557 break;
558
559 OPTION_LONG("tpm2-signature", "PATH",
560 "Validate public key enrollment works with JSON signature file"):
561 r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_tpm2_signature);
562 if (r < 0)
563 return r;
564 break;
565
566 OPTION_LONG("tpm2-pcrlock", "PATH",
567 "Specify pcrlock policy to lock against"):
568 r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_tpm2_pcrlock);
569 if (r < 0)
570 return r;
571 auto_pcrlock = false;
572 break;
573
574 OPTION_LONG("tpm2-with-pin", "BOOL",
575 "Whether to require entering a PIN to unlock the volume"):
576 r = parse_boolean_argument("--tpm2-with-pin=", opts.arg, &arg_tpm2_pin);
577 if (r < 0)
578 return r;
579 break;
580 }
581
582 if (option_parser_get_n_args(&opts) > 1)
583 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Too many arguments, refusing.");
584
585 const char *arg = option_parser_get_arg(&opts, 0);
586 if (arg)
587 r = parse_path_argument(arg, false, &arg_node);
588 else if (!wipe_requested())
589 r = determine_default_node();
590 else
591 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
592 "Wiping requested and no block device node specified, refusing.");
593 if (r < 0)
594 return r;
595
596 if (arg_enroll_type == ENROLL_FIDO2) {
597 if (arg_unlock_type == UNLOCK_FIDO2 && !(arg_fido2_device && arg_unlock_fido2_device))
598 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
599 "When both enrolling and unlocking with FIDO2 tokens, automatic discovery is unsupported. "
600 "Please specify device paths for enrolling and unlocking respectively.");
601
602 if (!arg_fido2_parameters_in_header && !arg_fido2_salt_file)
603 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
604 "FIDO2 parameters' storage in the LUKS2 header was disabled, but no salt file provided, refusing.");
605
606 if (!arg_fido2_device) {
607 r = fido2_find_device_auto(&arg_fido2_device);
608 if (r < 0)
609 return r;
610 }
611 }
612
613 if (arg_enroll_type == ENROLL_TPM2) {
614 if (auto_pcrlock) {
615 assert(!arg_tpm2_pcrlock);
616
617 r = tpm2_pcrlock_search_file(NULL, NULL, &arg_tpm2_pcrlock);
618 if (r < 0) {
619 if (r != -ENOENT)
620 log_warning_errno(r, "Search for pcrlock.json failed, assuming it does not exist: %m");
621 } else
622 log_info("Automatically using pcrlock policy '%s'.", arg_tpm2_pcrlock);
623 }
624
625 if (auto_public_key_pcr_mask) {
626 assert(arg_tpm2_public_key_pcr_mask == 0);
627 arg_tpm2_public_key_pcr_mask = INDEX_TO_MASK(uint32_t, TPM2_PCR_KERNEL_BOOT);
628 }
629
630 if (arg_tpm2_n_hash_pcr_values == 0 &&
631 !arg_tpm2_pin &&
632 arg_tpm2_public_key_pcr_mask == 0 &&
633 !arg_tpm2_pcrlock)
634 log_notice("Notice: enrolling TPM2 with an empty policy, i.e. without any state or access restrictions.\n"
635 "Use --tpm2-public-key=, --tpm2-pcrlock=, --tpm2-with-pin= or --tpm2-pcrs= to enable one or more restrictions.");
636 }
637
638 return 1;
639 }
640
641 static int check_for_homed(struct crypt_device *cd) {
642 int r;
643
644 assert_se(cd);
645
646 /* Politely refuse operating on homed volumes. The enrolled tokens for the user record and the LUKS2
647 * volume should not get out of sync. */
648
649 for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token++) {
650 r = cryptsetup_get_token_as_json(cd, token, "systemd-homed", NULL);
651 if (IN_SET(r, -ENOENT, -EINVAL, -EMEDIUMTYPE))
652 continue;
653 if (r < 0)
654 return log_error_errno(r, "Failed to read JSON token data off disk: %m");
655
656 return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN),
657 "LUKS2 volume is managed by systemd-homed, please use homectl to enroll tokens.");
658 }
659
660 return 0;
661 }
662
663 static int load_volume_key_keyfile(
664 struct crypt_device *cd,
665 void *ret_vk,
666 size_t *ret_vks) {
667
668 _cleanup_(erase_and_freep) char *password = NULL;
669 size_t password_len;
670 int r;
671
672 assert_se(cd);
673 assert_se(ret_vk);
674 assert_se(ret_vks);
675
676 r = read_full_file_full(
677 AT_FDCWD,
678 arg_unlock_keyfile,
679 UINT64_MAX,
680 SIZE_MAX,
681 READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET,
682 NULL,
683 &password,
684 &password_len);
685 if (r < 0)
686 return log_error_errno(r, "Reading keyfile %s failed: %m", arg_unlock_keyfile);
687
688 r = sym_crypt_volume_key_get(
689 cd,
690 CRYPT_ANY_SLOT,
691 ret_vk,
692 ret_vks,
693 password,
694 password_len);
695 if (r < 0)
696 return log_error_errno(r, "Unlocking via keyfile failed: %m");
697
698 return r;
699 }
700
701 static int prepare_luks(
702 struct crypt_device **ret_cd,
703 struct iovec *ret_volume_key) {
704
705 _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
706 int r;
707
708 assert(ret_cd);
709
710 r = sym_crypt_init(&cd, arg_node);
711 if (r < 0)
712 return log_error_errno(r, "Failed to allocate libcryptsetup context: %m");
713
714 cryptsetup_enable_logging(cd);
715
716 r = sym_crypt_load(cd, CRYPT_LUKS2, NULL);
717 if (r < 0)
718 return log_error_errno(r, "Failed to load LUKS2 superblock of %s: %m", arg_node);
719
720 r = check_for_homed(cd);
721 if (r < 0)
722 return r;
723
724 if (!ret_volume_key) {
725 *ret_cd = TAKE_PTR(cd);
726 return 0;
727 }
728
729 r = sym_crypt_get_volume_key_size(cd);
730 if (r <= 0)
731 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine LUKS volume key size");
732
733 _cleanup_(iovec_done_erase) struct iovec vk = {};
734
735 vk.iov_base = malloc(r);
736 if (!vk.iov_base)
737 return log_oom();
738
739 vk.iov_len = (size_t) r;
740
741 switch (arg_unlock_type) {
742
743 case UNLOCK_PASSWORD:
744 r = load_volume_key_password(cd, arg_node, vk.iov_base, &vk.iov_len);
745 break;
746
747 case UNLOCK_KEYFILE:
748 r = load_volume_key_keyfile(cd, vk.iov_base, &vk.iov_len);
749 break;
750
751 case UNLOCK_FIDO2:
752 r = load_volume_key_fido2(cd, arg_node, arg_unlock_fido2_device, vk.iov_base, &vk.iov_len);
753 break;
754
755 case UNLOCK_TPM2:
756 r = load_volume_key_tpm2(cd, arg_node, arg_unlock_tpm2_device, vk.iov_base, &vk.iov_len);
757 break;
758
759 default:
760 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown LUKS unlock method");
761 }
762
763 if (r < 0)
764 return r;
765
766 *ret_cd = TAKE_PTR(cd);
767 *ret_volume_key = TAKE_STRUCT(vk);
768
769 return 0;
770 }
771
772 static int run(int argc, char *argv[]) {
773 _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
774 _cleanup_(iovec_done_erase) struct iovec vk = {};
775 int slot, slot_to_wipe, r;
776
777 log_setup();
778
779 r = parse_argv(argc, argv);
780 if (r <= 0)
781 return r;
782
783 r = DLOPEN_CRYPTSETUP(LOG_ERR, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED);
784 if (r < 0)
785 return r;
786
787 /* A delicious drop of snake oil */
788 (void) safe_mlockall(MCL_CURRENT|MCL_FUTURE|MCL_ONFAULT);
789
790 if (arg_enroll_type < 0)
791 r = prepare_luks(&cd, /* ret_volume_key= */ NULL); /* No need to unlock device if we don't need the volume key because we don't need to enroll anything */
792 else
793 r = prepare_luks(&cd, &vk);
794 if (r < 0)
795 return r;
796
797 switch (arg_enroll_type) {
798
799 case ENROLL_PASSWORD:
800 slot = enroll_password(cd, &vk);
801 break;
802
803 case ENROLL_RECOVERY:
804 slot = enroll_recovery(cd, &vk);
805 break;
806
807 case ENROLL_PKCS11:
808 slot = enroll_pkcs11(cd, &vk, arg_pkcs11_token_uri);
809 break;
810
811 case ENROLL_FIDO2:
812 slot = enroll_fido2(cd, &vk, arg_fido2_device, arg_fido2_lock_with, arg_fido2_cred_alg, arg_fido2_salt_file, arg_fido2_parameters_in_header);
813 break;
814
815 case ENROLL_TPM2:
816 slot = enroll_tpm2(cd, &vk, arg_tpm2_device, arg_tpm2_seal_key_handle, arg_tpm2_device_key, arg_tpm2_hash_pcr_values, arg_tpm2_n_hash_pcr_values, arg_tpm2_public_key, arg_tpm2_load_public_key, arg_tpm2_public_key_pcr_mask, arg_tpm2_signature, arg_tpm2_pin, arg_tpm2_pcrlock, &slot_to_wipe);
817
818 if (slot >= 0 && slot_to_wipe >= 0) {
819 assert(slot != slot_to_wipe);
820
821 /* Updating PIN on an existing enrollment */
822 r = wipe_slots(
823 cd,
824 &slot_to_wipe,
825 /* n_explicit_slots= */ 1,
826 WIPE_EXPLICIT,
827 /* by_mask= */ 0,
828 /* except_slot= */ -1);
829 if (r < 0)
830 return r;
831 }
832 break;
833 case _ENROLL_TYPE_INVALID:
834 /* List enrolled slots if we are called without anything to enroll or wipe */
835 if (!wipe_requested())
836 return list_enrolled(cd);
837
838 /* Only slot wiping selected */
839 return wipe_slots(cd, arg_wipe_slots, arg_n_wipe_slots, arg_wipe_slots_scope, arg_wipe_slots_mask, -1);
840
841 default:
842 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Operation not implemented yet.");
843 }
844 if (slot < 0)
845 return slot;
846
847 /* After we completed enrolling, remove user selected slots */
848 r = wipe_slots(cd, arg_wipe_slots, arg_n_wipe_slots, arg_wipe_slots_scope, arg_wipe_slots_mask, slot);
849 if (r < 0)
850 return r;
851
852 return 0;
853 }
854
855 DEFINE_MAIN_FUNCTION(run);