1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
11 #include "sd-device.h"
12 #include "sd-messages.h"
14 #include "alloc-util.h"
15 #include "ask-password-api.h"
17 #include "cryptsetup-fido2.h"
18 #include "cryptsetup-keyfile.h"
19 #include "cryptsetup-pkcs11.h"
20 #include "cryptsetup-tpm2.h"
21 #include "cryptsetup-util.h"
22 #include "device-util.h"
24 #include "efi-loader.h"
29 #include "fstab-util.h"
30 #include "hexdecoct.h"
31 #include "libfido2-util.h"
33 #include "main-func.h"
34 #include "memory-util.h"
35 #include "mount-util.h"
36 #include "nulstr-util.h"
37 #include "parse-util.h"
38 #include "path-util.h"
39 #include "pkcs11-util.h"
40 #include "pretty-print.h"
41 #include "process-util.h"
42 #include "random-util.h"
43 #include "string-table.h"
46 #include "tpm2-util.h"
49 #define ANY_LUKS "LUKS"
50 /* as in src/cryptsetup.h */
51 #define CRYPT_SECTOR_SIZE 512U
52 #define CRYPT_MAX_SECTOR_SIZE 4096U
54 typedef enum PassphraseType
{
56 PASSPHRASE_REGULAR
= 1 << 0,
57 PASSPHRASE_RECOVERY_KEY
= 1 << 1,
58 PASSPHRASE_BOTH
= PASSPHRASE_REGULAR
|PASSPHRASE_RECOVERY_KEY
,
60 _PASSPHRASE_TYPE_INVALID
= -1,
63 static const char *arg_type
= NULL
; /* ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2, CRYPT_TCRYPT, CRYPT_BITLK or CRYPT_PLAIN */
64 static char *arg_cipher
= NULL
;
65 static unsigned arg_key_size
= 0;
66 static unsigned arg_sector_size
= CRYPT_SECTOR_SIZE
;
67 static int arg_key_slot
= CRYPT_ANY_SLOT
;
68 static unsigned arg_keyfile_size
= 0;
69 static uint64_t arg_keyfile_offset
= 0;
70 static bool arg_keyfile_erase
= false;
71 static bool arg_try_empty_password
= false;
72 static char *arg_hash
= NULL
;
73 static char *arg_header
= NULL
;
74 static unsigned arg_tries
= 3;
75 static bool arg_readonly
= false;
76 static bool arg_verify
= false;
77 static AskPasswordFlags arg_ask_password_flags
= 0;
78 static bool arg_discards
= false;
79 static bool arg_same_cpu_crypt
= false;
80 static bool arg_submit_from_crypt_cpus
= false;
81 static bool arg_no_read_workqueue
= false;
82 static bool arg_no_write_workqueue
= false;
83 static bool arg_tcrypt_hidden
= false;
84 static bool arg_tcrypt_system
= false;
85 static bool arg_tcrypt_veracrypt
= false;
86 static uint32_t arg_tcrypt_veracrypt_pim
= 0;
87 static char **arg_tcrypt_keyfiles
= NULL
;
88 static uint64_t arg_offset
= 0;
89 static uint64_t arg_skip
= 0;
90 static usec_t arg_timeout
= USEC_INFINITY
;
91 static char *arg_pkcs11_uri
= NULL
;
92 static bool arg_pkcs11_uri_auto
= false;
93 static char *arg_fido2_device
= NULL
;
94 static bool arg_fido2_device_auto
= false;
95 static void *arg_fido2_cid
= NULL
;
96 static size_t arg_fido2_cid_size
= 0;
97 static char *arg_fido2_rp_id
= NULL
;
98 static char *arg_tpm2_device
= NULL
; /* These and the following fields are about locking an encrypted volume to the local TPM */
99 static bool arg_tpm2_device_auto
= false;
100 static uint32_t arg_tpm2_pcr_mask
= UINT32_MAX
;
101 static char *arg_tpm2_signature
= NULL
;
102 static bool arg_tpm2_pin
= false;
103 static char *arg_tpm2_pcrlock
= NULL
;
104 static bool arg_headless
= false;
105 static usec_t arg_token_timeout_usec
= 30*USEC_PER_SEC
;
106 static unsigned arg_tpm2_measure_pcr
= UINT_MAX
; /* This and the following field is about measuring the unlocked volume key to the local TPM */
107 static char **arg_tpm2_measure_banks
= NULL
;
108 static char *arg_link_keyring
= NULL
;
109 static char *arg_link_key_type
= NULL
;
110 static char *arg_link_key_description
= NULL
;
112 STATIC_DESTRUCTOR_REGISTER(arg_cipher
, freep
);
113 STATIC_DESTRUCTOR_REGISTER(arg_hash
, freep
);
114 STATIC_DESTRUCTOR_REGISTER(arg_header
, freep
);
115 STATIC_DESTRUCTOR_REGISTER(arg_tcrypt_keyfiles
, strv_freep
);
116 STATIC_DESTRUCTOR_REGISTER(arg_pkcs11_uri
, freep
);
117 STATIC_DESTRUCTOR_REGISTER(arg_fido2_device
, freep
);
118 STATIC_DESTRUCTOR_REGISTER(arg_fido2_cid
, freep
);
119 STATIC_DESTRUCTOR_REGISTER(arg_fido2_rp_id
, freep
);
120 STATIC_DESTRUCTOR_REGISTER(arg_tpm2_device
, freep
);
121 STATIC_DESTRUCTOR_REGISTER(arg_tpm2_signature
, freep
);
122 STATIC_DESTRUCTOR_REGISTER(arg_tpm2_measure_banks
, strv_freep
);
123 STATIC_DESTRUCTOR_REGISTER(arg_tpm2_pcrlock
, freep
);
124 STATIC_DESTRUCTOR_REGISTER(arg_link_keyring
, freep
);
125 STATIC_DESTRUCTOR_REGISTER(arg_link_key_type
, freep
);
126 STATIC_DESTRUCTOR_REGISTER(arg_link_key_description
, freep
);
128 static const char* const passphrase_type_table
[_PASSPHRASE_TYPE_MAX
] = {
129 [PASSPHRASE_REGULAR
] = "passphrase",
130 [PASSPHRASE_RECOVERY_KEY
] = "recovery key",
131 [PASSPHRASE_BOTH
] = "passphrase or recovery key",
134 const char* passphrase_type_to_string(PassphraseType t
);
135 PassphraseType
passphrase_type_from_string(const char *s
);
137 DEFINE_STRING_TABLE_LOOKUP(passphrase_type
, PassphraseType
);
139 /* Options Debian's crypttab knows we don't:
149 static int parse_one_option(const char *option
) {
155 /* Handled outside of this tool */
156 if (STR_IN_SET(option
, "noauto", "auto", "nofail", "fail", "_netdev", "keyfile-timeout"))
159 if (startswith(option
, "keyfile-timeout="))
162 if ((val
= startswith(option
, "cipher="))) {
163 r
= free_and_strdup(&arg_cipher
, val
);
167 } else if ((val
= startswith(option
, "size="))) {
169 r
= safe_atou(val
, &arg_key_size
);
171 log_warning_errno(r
, "Failed to parse %s, ignoring: %m", option
);
175 if (arg_key_size
% 8) {
176 log_warning("size= not a multiple of 8, ignoring.");
182 } else if ((val
= startswith(option
, "sector-size="))) {
184 r
= safe_atou(val
, &arg_sector_size
);
186 log_warning_errno(r
, "Failed to parse %s, ignoring: %m", option
);
190 if (arg_sector_size
% 2) {
191 log_warning("sector-size= not a multiple of 2, ignoring.");
195 if (arg_sector_size
< CRYPT_SECTOR_SIZE
|| arg_sector_size
> CRYPT_MAX_SECTOR_SIZE
)
196 log_warning("sector-size= is outside of %u and %u, ignoring.", CRYPT_SECTOR_SIZE
, CRYPT_MAX_SECTOR_SIZE
);
198 } else if ((val
= startswith(option
, "key-slot=")) ||
199 (val
= startswith(option
, "keyslot="))) {
202 r
= safe_atoi(val
, &arg_key_slot
);
204 log_warning_errno(r
, "Failed to parse %s, ignoring: %m", option
);
206 } else if ((val
= startswith(option
, "tcrypt-keyfile="))) {
208 arg_type
= CRYPT_TCRYPT
;
209 if (path_is_absolute(val
)) {
210 if (strv_extend(&arg_tcrypt_keyfiles
, val
) < 0)
213 log_warning("Key file path \"%s\" is not absolute, ignoring.", val
);
215 } else if ((val
= startswith(option
, "keyfile-size="))) {
217 r
= safe_atou(val
, &arg_keyfile_size
);
219 log_warning_errno(r
, "Failed to parse %s, ignoring: %m", option
);
221 } else if ((val
= startswith(option
, "keyfile-offset="))) {
223 r
= safe_atou64(val
, &arg_keyfile_offset
);
225 log_warning_errno(r
, "Failed to parse %s, ignoring: %m", option
);
227 } else if ((val
= startswith(option
, "keyfile-erase="))) {
229 r
= parse_boolean(val
);
231 log_warning_errno(r
, "Failed to parse %s, ignoring: %m", option
);
235 arg_keyfile_erase
= r
;
237 } else if (streq(option
, "keyfile-erase"))
238 arg_keyfile_erase
= true;
240 else if ((val
= startswith(option
, "hash="))) {
241 r
= free_and_strdup(&arg_hash
, val
);
245 } else if ((val
= startswith(option
, "header="))) {
246 if (!arg_type
|| !STR_IN_SET(arg_type
, ANY_LUKS
, CRYPT_LUKS1
, CRYPT_LUKS2
, CRYPT_TCRYPT
))
249 if (!path_is_absolute(val
))
250 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
251 "Header path \"%s\" is not absolute, refusing.", val
);
254 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
255 "Duplicate header= option, refusing.");
257 arg_header
= strdup(val
);
261 } else if ((val
= startswith(option
, "tries="))) {
263 r
= safe_atou(val
, &arg_tries
);
265 log_warning_errno(r
, "Failed to parse %s, ignoring: %m", option
);
267 } else if (STR_IN_SET(option
, "readonly", "read-only"))
269 else if (streq(option
, "verify"))
271 else if ((val
= startswith(option
, "password-echo="))) {
272 if (streq(val
, "masked"))
273 arg_ask_password_flags
&= ~(ASK_PASSWORD_ECHO
|ASK_PASSWORD_SILENT
);
275 r
= parse_boolean(val
);
277 log_warning_errno(r
, "Invalid password-echo= option \"%s\", ignoring.", val
);
281 SET_FLAG(arg_ask_password_flags
, ASK_PASSWORD_ECHO
, r
);
282 SET_FLAG(arg_ask_password_flags
, ASK_PASSWORD_SILENT
, !r
);
284 } else if (STR_IN_SET(option
, "allow-discards", "discard"))
286 else if (streq(option
, "same-cpu-crypt"))
287 arg_same_cpu_crypt
= true;
288 else if (streq(option
, "submit-from-crypt-cpus"))
289 arg_submit_from_crypt_cpus
= true;
290 else if (streq(option
, "no-read-workqueue"))
291 arg_no_read_workqueue
= true;
292 else if (streq(option
, "no-write-workqueue"))
293 arg_no_write_workqueue
= true;
294 else if (streq(option
, "luks"))
296 /* since cryptsetup 2.3.0 (Feb 2020) */
298 else if (streq(option
, "bitlk"))
299 arg_type
= CRYPT_BITLK
;
301 else if (streq(option
, "tcrypt"))
302 arg_type
= CRYPT_TCRYPT
;
303 else if (STR_IN_SET(option
, "tcrypt-hidden", "tcrypthidden")) {
304 arg_type
= CRYPT_TCRYPT
;
305 arg_tcrypt_hidden
= true;
306 } else if (streq(option
, "tcrypt-system")) {
307 arg_type
= CRYPT_TCRYPT
;
308 arg_tcrypt_system
= true;
309 } else if (STR_IN_SET(option
, "tcrypt-veracrypt", "veracrypt")) {
310 arg_type
= CRYPT_TCRYPT
;
311 arg_tcrypt_veracrypt
= true;
312 } else if ((val
= startswith(option
, "veracrypt-pim="))) {
314 r
= safe_atou32(val
, &arg_tcrypt_veracrypt_pim
);
316 log_warning_errno(r
, "Failed to parse %s, ignoring: %m", option
);
319 } else if (STR_IN_SET(option
, "plain", "swap", "tmp") ||
320 startswith(option
, "tmp="))
321 arg_type
= CRYPT_PLAIN
;
322 else if ((val
= startswith(option
, "timeout="))) {
324 r
= parse_sec_fix_0(val
, &arg_timeout
);
326 log_warning_errno(r
, "Failed to parse %s, ignoring: %m", option
);
328 } else if ((val
= startswith(option
, "offset="))) {
330 r
= safe_atou64(val
, &arg_offset
);
332 return log_error_errno(r
, "Failed to parse %s: %m", option
);
334 } else if ((val
= startswith(option
, "skip="))) {
336 r
= safe_atou64(val
, &arg_skip
);
338 return log_error_errno(r
, "Failed to parse %s: %m", option
);
340 } else if ((val
= startswith(option
, "pkcs11-uri="))) {
342 if (streq(val
, "auto")) {
343 arg_pkcs11_uri
= mfree(arg_pkcs11_uri
);
344 arg_pkcs11_uri_auto
= true;
346 if (!pkcs11_uri_valid(val
))
347 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "pkcs11-uri= parameter expects a PKCS#11 URI, refusing");
349 r
= free_and_strdup(&arg_pkcs11_uri
, val
);
353 arg_pkcs11_uri_auto
= false;
356 } else if ((val
= startswith(option
, "fido2-device="))) {
358 if (streq(val
, "auto")) {
359 arg_fido2_device
= mfree(arg_fido2_device
);
360 arg_fido2_device_auto
= true;
362 r
= free_and_strdup(&arg_fido2_device
, val
);
366 arg_fido2_device_auto
= false;
369 } else if ((val
= startswith(option
, "fido2-cid="))) {
371 if (streq(val
, "auto"))
372 arg_fido2_cid
= mfree(arg_fido2_cid
);
374 _cleanup_free_
void *cid
= NULL
;
377 r
= unbase64mem(val
, &cid
, &cid_size
);
379 return log_error_errno(r
, "Failed to decode FIDO2 CID data: %m");
382 arg_fido2_cid
= TAKE_PTR(cid
);
383 arg_fido2_cid_size
= cid_size
;
386 /* Turn on FIDO2 as side-effect, if not turned on yet. */
387 if (!arg_fido2_device
&& !arg_fido2_device_auto
)
388 arg_fido2_device_auto
= true;
390 } else if ((val
= startswith(option
, "fido2-rp="))) {
392 r
= free_and_strdup(&arg_fido2_rp_id
, val
);
396 } else if ((val
= startswith(option
, "tpm2-device="))) {
398 if (streq(val
, "auto")) {
399 arg_tpm2_device
= mfree(arg_tpm2_device
);
400 arg_tpm2_device_auto
= true;
402 r
= free_and_strdup(&arg_tpm2_device
, val
);
406 arg_tpm2_device_auto
= false;
409 } else if ((val
= startswith(option
, "tpm2-pcrs="))) {
411 r
= tpm2_parse_pcr_argument_to_mask(val
, &arg_tpm2_pcr_mask
);
415 } else if ((val
= startswith(option
, "tpm2-signature="))) {
417 if (!path_is_absolute(val
))
418 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
419 "TPM2 signature path \"%s\" is not absolute, refusing.", val
);
421 r
= free_and_strdup(&arg_tpm2_signature
, val
);
425 } else if ((val
= startswith(option
, "tpm2-pin="))) {
427 r
= parse_boolean(val
);
429 log_warning_errno(r
, "Failed to parse %s, ignoring: %m", option
);
435 } else if ((val
= startswith(option
, "tpm2-pcrlock="))) {
437 if (!path_is_absolute(val
))
438 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
439 "TPM2 pcrlock policy path \"%s\" is not absolute, refusing.", val
);
441 r
= free_and_strdup(&arg_tpm2_pcrlock
, val
);
445 } else if ((val
= startswith(option
, "tpm2-measure-pcr="))) {
448 r
= safe_atou(val
, &pcr
);
450 r
= parse_boolean(val
);
452 log_error_errno(r
, "Failed to parse %s, ignoring: %m", option
);
456 pcr
= r
? TPM2_PCR_SYSTEM_IDENTITY
: UINT_MAX
;
457 } else if (!TPM2_PCR_INDEX_VALID(pcr
)) {
458 log_warning("Selected TPM index for measurement %u outside of allowed range 0…%u, ignoring.", pcr
, TPM2_PCRS_MAX
-1);
462 arg_tpm2_measure_pcr
= pcr
;
464 } else if ((val
= startswith(option
, "tpm2-measure-bank="))) {
467 _cleanup_strv_free_
char **l
= NULL
;
469 l
= strv_split(optarg
, ":");
474 const EVP_MD
*implementation
;
476 implementation
= EVP_get_digestbyname(*i
);
478 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown bank '%s', refusing.", val
);
480 if (strv_extend(&arg_tpm2_measure_banks
, EVP_MD_name(implementation
)) < 0)
484 log_error("Build lacks OpenSSL support, cannot measure to PCR banks, ignoring: %s", option
);
487 } else if ((val
= startswith(option
, "try-empty-password="))) {
489 r
= parse_boolean(val
);
491 log_warning_errno(r
, "Failed to parse %s, ignoring: %m", option
);
495 arg_try_empty_password
= r
;
497 } else if (streq(option
, "try-empty-password"))
498 arg_try_empty_password
= true;
499 else if ((val
= startswith(option
, "headless="))) {
501 r
= parse_boolean(val
);
503 log_warning_errno(r
, "Failed to parse %s, ignoring: %m", option
);
508 } else if (streq(option
, "headless"))
511 else if ((val
= startswith(option
, "token-timeout="))) {
513 r
= parse_sec_fix_0(val
, &arg_token_timeout_usec
);
515 log_warning_errno(r
, "Failed to parse %s, ignoring: %m", option
);
517 } else if ((val
= startswith(option
, "link-volume-key="))) {
518 #ifdef HAVE_CRYPT_SET_KEYRING_TO_LINK
520 _cleanup_free_
char *keyring
= NULL
, *key_type
= NULL
, *key_description
= NULL
;
522 /* Stick with cryptsetup --link-vk-to-keyring format
523 * <keyring_description>::%<key_type>:<key_description>,
524 * where %<key_type> is optional and defaults to 'user'.
526 if (!(sep
= strstr(val
, "::")))
527 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to parse link-volume-key= option value: %m");
529 /* cryptsetup (cli) supports <keyring_description> passed in various formats:
530 * - well-known keyrings prefixed with '@' (@u user, @s session, etc)
531 * - text descriptions prefixed with "%:" or "%keyring:".
532 * - text description with no prefix.
533 * - numeric keyring id (ignored in current patch set). */
534 if (*val
== '@' || *val
== '%')
535 keyring
= strndup(val
, sep
- val
);
537 /* add type prefix if missing (crypt_set_keyring_to_link() expects it) */
538 keyring
= strnappend("%:", val
, sep
- val
);
544 /* %<key_type> is optional (and defaults to 'user') */
546 /* must be separated by colon */
547 if (!(c
= strchr(sep
, ':')))
548 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to parse link-volume-key= option value: %m");
550 key_type
= strndup(sep
+ 1, c
- sep
- 1);
557 key_description
= strdup(sep
);
558 if (!key_description
)
561 free_and_replace(arg_link_keyring
, keyring
);
562 free_and_replace(arg_link_key_type
, key_type
);
563 free_and_replace(arg_link_key_description
, key_description
);
565 log_error("Build lacks libcryptsetup support for linking volume keys in user specified kernel keyrings upon device activation, ignoring: %s", option
);
567 } else if (!streq(option
, "x-initrd.attach"))
568 log_warning("Encountered unknown /etc/crypttab option '%s', ignoring.", option
);
573 static int parse_crypt_config(const char *options
) {
577 _cleanup_free_
char *word
= NULL
;
580 r
= extract_first_word(&options
, &word
, ",", EXTRACT_DONT_COALESCE_SEPARATORS
| EXTRACT_UNESCAPE_SEPARATORS
);
582 return log_error_errno(r
, "Failed to parse options: %m");
586 r
= parse_one_option(word
);
591 /* sanity-check options */
592 if (arg_type
&& !streq(arg_type
, CRYPT_PLAIN
)) {
594 log_warning("offset= ignored with type %s", arg_type
);
596 log_warning("skip= ignored with type %s", arg_type
);
602 static char* disk_description(const char *path
) {
603 static const char name_fields
[] =
605 "ID_MODEL_FROM_DATABASE\0"
608 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
614 if (stat(path
, &st
) < 0)
617 if (!S_ISBLK(st
.st_mode
))
620 if (sd_device_new_from_stat_rdev(&device
, &st
) < 0)
623 if (sd_device_get_property_value(device
, "ID_PART_ENTRY_NAME", &name
) >= 0) {
624 _cleanup_free_
char *unescaped
= NULL
;
627 /* ID_PART_ENTRY_NAME uses \x style escaping, using libblkid's blkid_encode_string(). Let's
628 * reverse this here to make the string more human friendly in case people embed spaces or
629 * other weird stuff. */
631 l
= cunescape(name
, UNESCAPE_RELAX
, &unescaped
);
633 log_debug_errno(l
, "Failed to unescape ID_PART_ENTRY_NAME, skipping device: %m");
637 if (!isempty(unescaped
) && !string_has_cc(unescaped
, NULL
))
638 return TAKE_PTR(unescaped
);
641 /* These need no unescaping. */
642 NULSTR_FOREACH(i
, name_fields
)
643 if (sd_device_get_property_value(device
, i
, &name
) >= 0 &&
650 static char *disk_mount_point(const char *label
) {
651 _cleanup_free_
char *device
= NULL
;
652 _cleanup_endmntent_
FILE *f
= NULL
;
655 /* Yeah, we don't support native systemd unit files here for now */
657 device
= strjoin("/dev/mapper/", label
);
661 f
= setmntent(fstab_path(), "re");
665 while ((m
= getmntent(f
)))
666 if (path_equal(m
->mnt_fsname
, device
))
667 return strdup(m
->mnt_dir
);
672 static char *friendly_disk_name(const char *src
, const char *vol
) {
673 _cleanup_free_
char *description
= NULL
, *mount_point
= NULL
;
674 char *name_buffer
= NULL
;
680 description
= disk_description(src
);
681 mount_point
= disk_mount_point(vol
);
683 /* If the description string is simply the volume name, then let's not show this twice */
684 if (description
&& streq(vol
, description
))
685 description
= mfree(description
);
687 if (mount_point
&& description
)
688 r
= asprintf(&name_buffer
, "%s (%s) on %s", description
, vol
, mount_point
);
689 else if (mount_point
)
690 r
= asprintf(&name_buffer
, "%s on %s", vol
, mount_point
);
691 else if (description
)
692 r
= asprintf(&name_buffer
, "%s (%s)", description
, vol
);
701 static PassphraseType
check_registered_passwords(struct crypt_device
*cd
) {
702 _cleanup_free_
bool *slots
= NULL
;
704 PassphraseType passphrase_type
= PASSPHRASE_NONE
;
708 if (!streq_ptr(crypt_get_type(cd
), CRYPT_LUKS2
)) {
709 log_debug("%s: not a LUKS2 device, only passphrases are supported", crypt_get_device_name(cd
));
710 return PASSPHRASE_REGULAR
;
713 /* Search all used slots */
714 assert_se((slot_max
= crypt_keyslot_max(CRYPT_LUKS2
)) > 0);
715 slots
= new(bool, slot_max
);
719 for (int slot
= 0; slot
< slot_max
; slot
++)
720 slots
[slot
] = IN_SET(crypt_keyslot_status(cd
, slot
), CRYPT_SLOT_ACTIVE
, CRYPT_SLOT_ACTIVE_LAST
);
722 /* Iterate all LUKS2 tokens and keep track of all their slots */
723 for (int token
= 0; token
< sym_crypt_token_max(CRYPT_LUKS2
); token
++) {
724 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
729 tk
= cryptsetup_get_token_as_json(cd
, token
, NULL
, &v
);
730 if (IN_SET(tk
, -ENOENT
, -EINVAL
))
733 log_warning_errno(tk
, "Failed to read JSON token data, ignoring: %m");
737 w
= json_variant_by_key(v
, "type");
738 if (!w
|| !json_variant_is_string(w
)) {
739 log_warning("Token JSON data lacks type field, ignoring.");
743 type
= json_variant_string(w
);
744 if (STR_IN_SET(type
, "systemd-recovery", "systemd-pkcs11", "systemd-fido2", "systemd-tpm2")) {
746 /* At least exists one recovery key */
747 if (streq(type
, "systemd-recovery"))
748 passphrase_type
|= PASSPHRASE_RECOVERY_KEY
;
750 w
= json_variant_by_key(v
, "keyslots");
751 if (!w
|| !json_variant_is_array(w
)) {
752 log_warning("Token JSON data lacks keyslots field, ignoring.");
756 JSON_VARIANT_ARRAY_FOREACH(z
, w
) {
760 if (!json_variant_is_string(z
)) {
761 log_warning("Token JSON data's keyslot field is not an array of strings, ignoring.");
765 at
= safe_atou(json_variant_string(z
), &u
);
767 log_warning_errno(at
, "Token JSON data's keyslot field is not an integer formatted as string, ignoring.");
771 if (u
>= (unsigned) slot_max
) {
772 log_warning_errno(at
, "Token JSON data's keyslot field exceeds the maximum value allowed, ignoring.");
781 /* Check if any of the slots is not referenced by systemd tokens */
782 for (int slot
= 0; slot
< slot_max
; slot
++)
784 passphrase_type
|= PASSPHRASE_REGULAR
;
788 /* All the slots are referenced by systemd tokens, so if a recovery key is not enrolled,
789 * we will not be able to enter a passphrase. */
790 return passphrase_type
;
793 static int get_password(
798 PassphraseType passphrase_type
,
801 _cleanup_free_
char *friendly
= NULL
, *text
= NULL
, *disk_path
= NULL
, *id
= NULL
;
802 _cleanup_strv_free_erase_
char **passwords
= NULL
;
803 AskPasswordFlags flags
= arg_ask_password_flags
| ASK_PASSWORD_PUSH_CACHE
;
811 return log_error_errno(SYNTHETIC_ERRNO(ENOPKG
), "Password querying disabled via 'headless' option.");
813 friendly
= friendly_disk_name(src
, vol
);
817 if (asprintf(&text
, "Please enter %s for disk %s:", passphrase_type_to_string(passphrase_type
), friendly
) < 0)
820 disk_path
= cescape(src
);
824 id
= strjoin("cryptsetup:", disk_path
);
828 AskPasswordRequest req
= {
830 .icon
= "drive-harddisk",
832 .keyring
= "cryptsetup",
833 .credential
= "cryptsetup.passphrase",
836 r
= ask_password_auto(
839 flags
| (accept_cached
*ASK_PASSWORD_ACCEPT_CACHED
),
842 return log_error_errno(r
, "Failed to query password: %m");
845 _cleanup_strv_free_erase_
char **passwords2
= NULL
;
847 assert(strv_length(passwords
) == 1);
850 if (asprintf(&text
, "Please enter %s for disk %s (verification):", passphrase_type_to_string(passphrase_type
), friendly
) < 0)
854 id
= strjoin("cryptsetup-verification:", disk_path
);
861 r
= ask_password_auto(&req
, until
, flags
, &passwords2
);
863 return log_error_errno(r
, "Failed to query verification password: %m");
865 assert(strv_length(passwords2
) == 1);
867 if (!streq(passwords
[0], passwords2
[0]))
868 return log_warning_errno(SYNTHETIC_ERRNO(EAGAIN
),
869 "Passwords did not match, retrying.");
872 strv_uniq(passwords
);
874 STRV_FOREACH(p
, passwords
) {
877 if (strlen(*p
)+1 >= arg_key_size
)
880 /* Pad password if necessary */
881 c
= new(char, arg_key_size
);
885 strncpy(c
, *p
, arg_key_size
);
890 *ret
= TAKE_PTR(passwords
);
895 static int measure_volume_key(
896 struct crypt_device
*cd
,
898 const void *volume_key
,
899 size_t volume_key_size
) {
906 assert(volume_key_size
> 0);
908 if (arg_tpm2_measure_pcr
== UINT_MAX
) {
909 log_debug("Not measuring volume key, deactivated.");
913 r
= efi_measured_uki(LOG_WARNING
);
917 log_debug("Kernel stub did not measure kernel image into the expected PCR, skipping userspace measurement, too.");
922 _cleanup_(tpm2_context_unrefp
) Tpm2Context
*c
= NULL
;
923 r
= tpm2_context_new(arg_tpm2_device
, &c
);
925 return log_error_errno(r
, "Failed to create TPM2 context: %m");
927 _cleanup_strv_free_
char **l
= NULL
;
928 if (strv_isempty(arg_tpm2_measure_banks
)) {
929 r
= tpm2_get_good_pcr_banks_strv(c
, UINT32_C(1) << arg_tpm2_measure_pcr
, &l
);
931 return log_error_errno(r
, "Could not verify pcr banks: %m");
934 _cleanup_free_
char *joined
= strv_join(l
?: arg_tpm2_measure_banks
, ", ");
938 /* Note: we don't directly measure the volume key, it might be a security problem to send an
939 * unprotected direct hash of the secret volume key over the wire to the TPM. Hence let's instead
940 * send a HMAC signature instead. */
942 _cleanup_free_
char *escaped
= NULL
;
943 escaped
= xescape(name
, ":"); /* avoid ambiguity around ":" once we join things below */
947 _cleanup_free_
char *s
= NULL
;
948 s
= strjoin("cryptsetup:", escaped
, ":", strempty(crypt_get_uuid(cd
)));
952 r
= tpm2_extend_bytes(c
, l
?: arg_tpm2_measure_banks
, arg_tpm2_measure_pcr
, s
, SIZE_MAX
, volume_key
, volume_key_size
, TPM2_EVENT_VOLUME_KEY
, s
);
954 return log_error_errno(r
, "Could not extend PCR: %m");
957 "MESSAGE_ID=" SD_MESSAGE_TPM_PCR_EXTEND_STR
,
958 LOG_MESSAGE("Successfully extended PCR index %u with '%s' and volume key (banks %s).", arg_tpm2_measure_pcr
, s
, joined
),
960 "PCR=%u", arg_tpm2_measure_pcr
,
965 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
), "TPM2 support disabled, not measuring.");
969 static int measured_crypt_activate_by_volume_key(
970 struct crypt_device
*cd
,
972 const void *volume_key
,
973 size_t volume_key_size
,
981 /* A wrapper around crypt_activate_by_volume_key() which also measures to a PCR if that's requested. */
983 r
= crypt_activate_by_volume_key(cd
, name
, volume_key
, volume_key_size
, flags
);
987 if (volume_key_size
== 0) {
988 log_debug("Not measuring volume key, none specified.");
992 (void) measure_volume_key(cd
, name
, volume_key
, volume_key_size
); /* OK if fails */
996 static int measured_crypt_activate_by_passphrase(
997 struct crypt_device
*cd
,
1000 const char *passphrase
,
1001 size_t passphrase_size
,
1004 _cleanup_(erase_and_freep
) void *vk
= NULL
;
1010 /* A wrapper around crypt_activate_by_passphrase() which also measures to a PCR if that's
1011 * requested. Note that we need the volume key for the measurement, and
1012 * crypt_activate_by_passphrase() doesn't give us access to this. Hence, we operate indirectly, and
1013 * retrieve the volume key first, and then activate through that. */
1015 if (arg_tpm2_measure_pcr
== UINT_MAX
) {
1016 log_debug("Not measuring volume key, deactivated.");
1020 r
= crypt_get_volume_key_size(cd
);
1024 log_debug("Not measuring volume key, none defined.");
1028 vk
= malloc(vks
= r
);
1032 r
= crypt_volume_key_get(cd
, keyslot
, vk
, &vks
, passphrase
, passphrase_size
);
1036 return measured_crypt_activate_by_volume_key(cd
, name
, vk
, vks
, flags
);
1039 return crypt_activate_by_passphrase(cd
, name
, keyslot
, passphrase
, passphrase_size
, flags
);
1042 static int attach_tcrypt(
1043 struct crypt_device
*cd
,
1045 const char *key_file
,
1046 const void *key_data
,
1047 size_t key_data_size
,
1052 _cleanup_(erase_and_freep
) char *passphrase
= NULL
;
1053 struct crypt_params_tcrypt params
= {
1054 .flags
= CRYPT_TCRYPT_LEGACY_MODES
,
1055 .keyfiles
= (const char **)arg_tcrypt_keyfiles
,
1056 .keyfiles_count
= strv_length(arg_tcrypt_keyfiles
)
1061 assert(key_file
|| key_data
|| !strv_isempty(passwords
));
1063 if (arg_pkcs11_uri
|| arg_pkcs11_uri_auto
|| arg_fido2_device
|| arg_fido2_device_auto
|| arg_tpm2_device
|| arg_tpm2_device_auto
)
1064 /* Ask for a regular password */
1065 return log_error_errno(SYNTHETIC_ERRNO(EAGAIN
),
1066 "Sorry, but tcrypt devices are currently not supported in conjunction with pkcs11/fido2/tpm2 support.");
1068 if (arg_tcrypt_hidden
)
1069 params
.flags
|= CRYPT_TCRYPT_HIDDEN_HEADER
;
1071 if (arg_tcrypt_system
)
1072 params
.flags
|= CRYPT_TCRYPT_SYSTEM_HEADER
;
1074 if (arg_tcrypt_veracrypt
)
1075 params
.flags
|= CRYPT_TCRYPT_VERA_MODES
;
1077 if (arg_tcrypt_veracrypt
&& arg_tcrypt_veracrypt_pim
!= 0)
1078 params
.veracrypt_pim
= arg_tcrypt_veracrypt_pim
;
1081 params
.passphrase
= key_data
;
1082 params
.passphrase_size
= key_data_size
;
1083 r
= crypt_load(cd
, CRYPT_TCRYPT
, ¶ms
);
1084 } else if (key_file
) {
1085 r
= read_one_line_file(key_file
, &passphrase
);
1087 log_error_errno(r
, "Failed to read password file '%s': %m", key_file
);
1088 return -EAGAIN
; /* log with the actual error, but return EAGAIN */
1090 params
.passphrase
= passphrase
;
1091 params
.passphrase_size
= strlen(passphrase
);
1092 r
= crypt_load(cd
, CRYPT_TCRYPT
, ¶ms
);
1095 STRV_FOREACH(p
, passwords
){
1096 params
.passphrase
= *p
;
1097 params
.passphrase_size
= strlen(*p
);
1098 r
= crypt_load(cd
, CRYPT_TCRYPT
, ¶ms
);
1107 log_error_errno(r
, "Failed to activate using discovered key. (Key not correct?)");
1109 log_error_errno(r
, "Failed to activate using password file '%s'. (Key data not correct?)", key_file
);
1111 log_error_errno(r
, "Failed to activate using supplied passwords.");
1116 return log_error_errno(r
, "Failed to load tcrypt superblock on device %s: %m", crypt_get_device_name(cd
));
1119 r
= measured_crypt_activate_by_volume_key(cd
, name
, NULL
, 0, flags
);
1121 return log_error_errno(r
, "Failed to activate tcrypt device %s: %m", crypt_get_device_name(cd
));
1126 static char *make_bindname(const char *volume
) {
1129 if (asprintf(&s
, "@%" PRIx64
"/cryptsetup/%s", random_u64(), volume
) < 0)
1135 static int make_security_device_monitor(
1136 sd_event
**ret_event
,
1137 sd_device_monitor
**ret_monitor
) {
1138 _cleanup_(sd_device_monitor_unrefp
) sd_device_monitor
*monitor
= NULL
;
1139 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1143 assert(ret_monitor
);
1145 /* Waits for a device with "security-device" tag to show up in udev */
1147 r
= sd_event_default(&event
);
1149 return log_error_errno(r
, "Failed to allocate event loop: %m");
1151 r
= sd_event_add_time_relative(event
, NULL
, CLOCK_MONOTONIC
, arg_token_timeout_usec
, USEC_PER_SEC
, NULL
, INT_TO_PTR(-ETIMEDOUT
));
1153 return log_error_errno(r
, "Failed to install timeout event source: %m");
1155 r
= sd_device_monitor_new(&monitor
);
1157 return log_error_errno(r
, "Failed to allocate device monitor: %m");
1159 (void) sd_device_monitor_set_description(monitor
, "security-device");
1161 r
= sd_device_monitor_filter_add_match_tag(monitor
, "security-device");
1163 return log_error_errno(r
, "Failed to configure device monitor: %m");
1165 r
= sd_device_monitor_attach_event(monitor
, event
);
1167 return log_error_errno(r
, "Failed to attach device monitor: %m");
1169 r
= sd_device_monitor_start(monitor
, NULL
, NULL
);
1171 return log_error_errno(r
, "Failed to start device monitor: %m");
1173 *ret_event
= TAKE_PTR(event
);
1174 *ret_monitor
= TAKE_PTR(monitor
);
1178 static int run_security_device_monitor(
1180 sd_device_monitor
*monitor
) {
1181 bool processed
= false;
1187 /* Runs the event loop for the device monitor until either something happens, or the time-out is
1193 r
= sd_event_get_exit_code(event
, &x
);
1196 return log_error_errno(r
, "Failed to query exit code from event loop: %m");
1198 /* On ENODATA we aren't told to exit yet. */
1200 assert(x
== -ETIMEDOUT
);
1201 return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN
),
1202 "Timed out waiting for security device, aborting security device based authentication attempt.");
1205 /* Wait for one event, and then eat all subsequent events until there are no further ones */
1206 r
= sd_event_run(event
, processed
? 0 : UINT64_MAX
);
1208 return log_error_errno(r
, "Failed to run event loop: %m");
1209 if (r
== 0) /* no events queued anymore */
1216 static bool libcryptsetup_plugins_support(void) {
1219 /* Currently, there's no way for us to query the volume key when plugins are used. Hence don't use
1220 * plugins, if measurement has been requested. */
1221 if (arg_tpm2_measure_pcr
!= UINT_MAX
)
1225 #if HAVE_LIBCRYPTSETUP_PLUGINS
1228 /* Permit a way to disable libcryptsetup token module support, for debugging purposes. */
1229 r
= getenv_bool("SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE");
1230 if (r
< 0 && r
!= -ENXIO
)
1231 log_debug_errno(r
, "Failed to parse $SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE env var: %m");
1235 return crypt_token_external_path();
1241 #if HAVE_LIBCRYPTSETUP_PLUGINS
1242 static int acquire_pins_from_env_variable(char ***ret_pins
) {
1243 _cleanup_(erase_and_freep
) char *envpin
= NULL
;
1244 _cleanup_strv_free_erase_
char **pins
= NULL
;
1249 r
= getenv_steal_erase("PIN", &envpin
);
1251 return log_error_errno(r
, "Failed to acquire PIN from environment: %m");
1253 pins
= strv_new(envpin
);
1258 *ret_pins
= TAKE_PTR(pins
);
1264 static int crypt_activate_by_token_pin_ask_password(
1265 struct crypt_device
*cd
,
1271 uint32_t activation_flags
,
1272 const char *message
,
1273 const char *keyring
,
1274 const char *credential
) {
1276 #if HAVE_LIBCRYPTSETUP_PLUGINS
1277 AskPasswordFlags flags
= arg_ask_password_flags
| ASK_PASSWORD_PUSH_CACHE
| ASK_PASSWORD_ACCEPT_CACHED
;
1278 _cleanup_strv_free_erase_
char **pins
= NULL
;
1281 r
= crypt_activate_by_token_pin(cd
, name
, type
, CRYPT_ANY_TOKEN
, /* pin=*/ NULL
, /* pin_size= */ 0, userdata
, activation_flags
);
1282 if (r
> 0) /* returns unlocked keyslot id on success */
1284 if (r
!= -ENOANO
) /* needs pin or pin is wrong */
1287 r
= acquire_pins_from_env_variable(&pins
);
1291 STRV_FOREACH(p
, pins
) {
1292 r
= crypt_activate_by_token_pin(cd
, name
, type
, CRYPT_ANY_TOKEN
, *p
, strlen(*p
), userdata
, activation_flags
);
1293 if (r
> 0) /* returns unlocked keyslot id on success */
1295 if (r
!= -ENOANO
) /* needs pin or pin is wrong */
1300 return log_error_errno(SYNTHETIC_ERRNO(ENOPKG
), "PIN querying disabled via 'headless' option. Use the '$PIN' environment variable.");
1303 pins
= strv_free_erase(pins
);
1305 AskPasswordRequest req
= {
1307 .icon
= "drive-harddisk",
1309 .credential
= credential
,
1312 r
= ask_password_auto(&req
, until
, flags
, &pins
);
1316 STRV_FOREACH(p
, pins
) {
1317 r
= crypt_activate_by_token_pin(cd
, name
, type
, CRYPT_ANY_TOKEN
, *p
, strlen(*p
), userdata
, activation_flags
);
1318 if (r
> 0) /* returns unlocked keyslot id on success */
1320 if (r
!= -ENOANO
) /* needs pin or pin is wrong */
1324 flags
&= ~ASK_PASSWORD_ACCEPT_CACHED
;
1332 static int attach_luks2_by_fido2_via_plugin(
1333 struct crypt_device
*cd
,
1338 uint32_t activation_flags
) {
1340 return crypt_activate_by_token_pin_ask_password(
1348 "Please enter security token PIN:",
1350 "cryptsetup.fido2-pin");
1353 static int attach_luks_or_plain_or_bitlk_by_fido2(
1354 struct crypt_device
*cd
,
1356 const char *key_file
,
1357 const void *key_data
,
1358 size_t key_data_size
,
1361 bool pass_volume_key
) {
1363 _cleanup_(sd_device_monitor_unrefp
) sd_device_monitor
*monitor
= NULL
;
1364 _cleanup_(erase_and_freep
) void *decrypted_key
= NULL
;
1365 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1366 size_t decrypted_key_size
, cid_size
= 0;
1367 _cleanup_free_
char *friendly
= NULL
;
1368 int keyslot
= arg_key_slot
, r
;
1369 const char *rp_id
= NULL
;
1370 const void *cid
= NULL
;
1371 Fido2EnrollFlags required
;
1372 bool use_libcryptsetup_plugin
= libcryptsetup_plugins_support();
1376 assert(arg_fido2_device
|| arg_fido2_device_auto
);
1378 if (arg_fido2_cid
) {
1379 if (!key_file
&& !key_data
)
1380 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1381 "FIDO2 mode with manual parameters selected, but no keyfile specified, refusing.");
1383 rp_id
= arg_fido2_rp_id
;
1384 cid
= arg_fido2_cid
;
1385 cid_size
= arg_fido2_cid_size
;
1387 /* For now and for compatibility, if the user explicitly configured FIDO2 support and we do
1388 * not read FIDO2 metadata off the LUKS2 header, default to the systemd 248 logic, where we
1389 * use PIN + UP when needed, and do not configure UV at all. Eventually, we should make this
1390 * explicitly configurable. */
1391 required
= FIDO2ENROLL_PIN_IF_NEEDED
| FIDO2ENROLL_UP_IF_NEEDED
| FIDO2ENROLL_UV_OMIT
;
1394 friendly
= friendly_disk_name(crypt_get_device_name(cd
), name
);
1399 if (use_libcryptsetup_plugin
&& !arg_fido2_cid
) {
1400 r
= attach_luks2_by_fido2_via_plugin(cd
, name
, until
, arg_headless
, arg_fido2_device
, flags
);
1401 if (IN_SET(r
, -ENOTUNIQ
, -ENXIO
, -ENOENT
))
1402 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN
),
1403 "Automatic FIDO2 metadata discovery was not possible because missing or not unique, falling back to traditional unlocking.");
1407 r
= acquire_fido2_key(
1413 key_file
, arg_keyfile_size
, arg_keyfile_offset
,
1414 key_data
, key_data_size
,
1418 &decrypted_key
, &decrypted_key_size
,
1419 arg_ask_password_flags
);
1421 r
= acquire_fido2_key_auto(
1428 &decrypted_key
, &decrypted_key_size
,
1429 arg_ask_password_flags
);
1434 if (r
!= -EAGAIN
) /* EAGAIN means: token not found */
1438 /* We didn't find the token. In this case, watch for it via udev. Let's
1439 * create an event loop and monitor first. */
1443 r
= make_security_device_monitor(&event
, &monitor
);
1447 log_notice("Security token not present for unlocking volume %s, please plug it in.", friendly
);
1449 /* Let's immediately rescan in case the token appeared in the time we needed
1450 * to create and configure the monitor */
1454 r
= run_security_device_monitor(event
, monitor
);
1458 log_debug("Got one or more potentially relevant udev events, rescanning FIDO2...");
1461 if (pass_volume_key
)
1462 r
= measured_crypt_activate_by_volume_key(cd
, name
, decrypted_key
, decrypted_key_size
, flags
);
1464 _cleanup_(erase_and_freep
) char *base64_encoded
= NULL
;
1465 ssize_t base64_encoded_size
;
1467 /* Before using this key as passphrase we base64 encode it, for compat with homed */
1469 base64_encoded_size
= base64mem(decrypted_key
, decrypted_key_size
, &base64_encoded
);
1470 if (base64_encoded_size
< 0)
1473 r
= measured_crypt_activate_by_passphrase(cd
, name
, keyslot
, base64_encoded
, base64_encoded_size
, flags
);
1476 log_error_errno(r
, "Failed to activate with FIDO2 decrypted key. (Key incorrect?)");
1477 return -EAGAIN
; /* log actual error, but return EAGAIN */
1480 return log_error_errno(r
, "Failed to activate with FIDO2 acquired key: %m");
1485 static int attach_luks2_by_pkcs11_via_plugin(
1486 struct crypt_device
*cd
,
1488 const char *friendly_name
,
1493 #if HAVE_LIBCRYPTSETUP_PLUGINS
1496 if (!streq_ptr(crypt_get_type(cd
), CRYPT_LUKS2
))
1497 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Automatic PKCS#11 metadata requires LUKS2 device.");
1499 systemd_pkcs11_plugin_params params
= {
1500 .friendly_name
= friendly_name
,
1502 .headless
= headless
,
1503 .askpw_flags
= arg_ask_password_flags
,
1506 r
= crypt_activate_by_token_pin(cd
, name
, "systemd-pkcs11", CRYPT_ANY_TOKEN
, NULL
, 0, ¶ms
, flags
);
1507 if (r
> 0) /* returns unlocked keyslot id on success */
1516 static int attach_luks_or_plain_or_bitlk_by_pkcs11(
1517 struct crypt_device
*cd
,
1519 const char *key_file
,
1520 const void *key_data
,
1521 size_t key_data_size
,
1524 bool pass_volume_key
) {
1526 _cleanup_(sd_device_monitor_unrefp
) sd_device_monitor
*monitor
= NULL
;
1527 _cleanup_free_
char *friendly
= NULL
, *discovered_uri
= NULL
;
1528 size_t decrypted_key_size
= 0, discovered_key_size
= 0;
1529 _cleanup_(erase_and_freep
) void *decrypted_key
= NULL
;
1530 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1531 _cleanup_free_
void *discovered_key
= NULL
;
1532 int keyslot
= arg_key_slot
, r
;
1533 const char *uri
= NULL
;
1534 bool use_libcryptsetup_plugin
= libcryptsetup_plugins_support();
1538 assert(arg_pkcs11_uri
|| arg_pkcs11_uri_auto
);
1540 if (arg_pkcs11_uri_auto
) {
1541 if (!use_libcryptsetup_plugin
) {
1542 r
= find_pkcs11_auto_data(cd
, &discovered_uri
, &discovered_key
, &discovered_key_size
, &keyslot
);
1543 if (IN_SET(r
, -ENOTUNIQ
, -ENXIO
))
1544 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN
),
1545 "Automatic PKCS#11 metadata discovery was not possible because missing or not unique, falling back to traditional unlocking.");
1549 uri
= discovered_uri
;
1550 key_data
= discovered_key
;
1551 key_data_size
= discovered_key_size
;
1554 uri
= arg_pkcs11_uri
;
1556 if (!key_file
&& !key_data
)
1557 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "PKCS#11 mode selected but no key file specified, refusing.");
1560 friendly
= friendly_disk_name(crypt_get_device_name(cd
), name
);
1565 if (use_libcryptsetup_plugin
&& arg_pkcs11_uri_auto
)
1566 r
= attach_luks2_by_pkcs11_via_plugin(cd
, name
, friendly
, until
, arg_headless
, flags
);
1568 r
= decrypt_pkcs11_key(
1572 key_file
, arg_keyfile_size
, arg_keyfile_offset
,
1573 key_data
, key_data_size
,
1576 &decrypted_key
, &decrypted_key_size
);
1581 if (r
!= -EAGAIN
) /* EAGAIN means: token not found */
1585 /* We didn't find the token. In this case, watch for it via udev. Let's
1586 * create an event loop and monitor first. */
1590 r
= make_security_device_monitor(&event
, &monitor
);
1594 log_notice("Security token%s%s not present for unlocking volume %s, please plug it in.",
1595 uri
? " " : "", strempty(uri
), friendly
);
1597 /* Let's immediately rescan in case the token appeared in the time we needed
1598 * to create and configure the monitor */
1602 r
= run_security_device_monitor(event
, monitor
);
1606 log_debug("Got one or more potentially relevant udev events, rescanning PKCS#11...");
1608 assert(decrypted_key
);
1610 if (pass_volume_key
)
1611 r
= measured_crypt_activate_by_volume_key(cd
, name
, decrypted_key
, decrypted_key_size
, flags
);
1613 _cleanup_(erase_and_freep
) char *base64_encoded
= NULL
;
1614 ssize_t base64_encoded_size
;
1616 /* Before using this key as passphrase we base64 encode it. Why? For compatibility
1617 * with homed's PKCS#11 hookup: there we want to use the key we acquired through
1618 * PKCS#11 for other authentication/decryption mechanisms too, and some of them do
1619 * not take arbitrary binary blobs, but require NUL-terminated strings — most
1620 * importantly UNIX password hashes. Hence, for compatibility we want to use a string
1621 * without embedded NUL here too, and that's easiest to generate from a binary blob
1622 * via base64 encoding. */
1624 base64_encoded_size
= base64mem(decrypted_key
, decrypted_key_size
, &base64_encoded
);
1625 if (base64_encoded_size
< 0)
1628 r
= measured_crypt_activate_by_passphrase(cd
, name
, keyslot
, base64_encoded
, base64_encoded_size
, flags
);
1631 log_error_errno(r
, "Failed to activate with PKCS#11 decrypted key. (Key incorrect?)");
1632 return -EAGAIN
; /* log actual error, but return EAGAIN */
1635 return log_error_errno(r
, "Failed to activate with PKCS#11 acquired key: %m");
1640 static int make_tpm2_device_monitor(
1641 sd_event
**ret_event
,
1642 sd_device_monitor
**ret_monitor
) {
1644 _cleanup_(sd_device_monitor_unrefp
) sd_device_monitor
*monitor
= NULL
;
1645 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1649 assert(ret_monitor
);
1651 r
= sd_event_default(&event
);
1653 return log_error_errno(r
, "Failed to allocate event loop: %m");
1655 r
= sd_event_add_time_relative(event
, NULL
, CLOCK_MONOTONIC
, arg_token_timeout_usec
, USEC_PER_SEC
, NULL
, INT_TO_PTR(-ETIMEDOUT
));
1657 return log_error_errno(r
, "Failed to install timeout event source: %m");
1659 r
= sd_device_monitor_new(&monitor
);
1661 return log_error_errno(r
, "Failed to allocate device monitor: %m");
1663 (void) sd_device_monitor_set_description(monitor
, "tpmrm");
1665 r
= sd_device_monitor_filter_add_match_subsystem_devtype(monitor
, "tpmrm", NULL
);
1667 return log_error_errno(r
, "Failed to configure device monitor: %m");
1669 r
= sd_device_monitor_attach_event(monitor
, event
);
1671 return log_error_errno(r
, "Failed to attach device monitor: %m");
1673 r
= sd_device_monitor_start(monitor
, NULL
, NULL
);
1675 return log_error_errno(r
, "Failed to start device monitor: %m");
1677 *ret_event
= TAKE_PTR(event
);
1678 *ret_monitor
= TAKE_PTR(monitor
);
1682 static bool use_token_plugins(void) {
1685 /* Disable tokens if we shall measure, since we won't get access to the volume key then. */
1686 if (arg_tpm2_measure_pcr
!= UINT_MAX
)
1689 r
= getenv_bool("SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE");
1690 if (r
< 0 && r
!= -ENXIO
)
1691 log_debug_errno(r
, "Failed to parse $SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE value, ignoring: %m");
1696 static int attach_luks2_by_tpm2_via_plugin(
1697 struct crypt_device
*cd
,
1703 #if HAVE_LIBCRYPTSETUP_PLUGINS
1704 systemd_tpm2_plugin_params params
= {
1705 .search_pcr_mask
= arg_tpm2_pcr_mask
,
1706 .device
= arg_tpm2_device
,
1707 .signature_path
= arg_tpm2_signature
,
1708 .pcrlock_path
= arg_tpm2_pcrlock
,
1711 if (!libcryptsetup_plugins_support())
1712 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1713 "Libcryptsetup has external plugins support disabled.");
1715 return crypt_activate_by_token_pin_ask_password(
1723 "Please enter TPM2 PIN:",
1725 "cryptsetup.tpm2-pin");
1731 static int attach_luks_or_plain_or_bitlk_by_tpm2(
1732 struct crypt_device
*cd
,
1734 const char *key_file
,
1735 const struct iovec
*key_data
,
1738 bool pass_volume_key
) {
1740 _cleanup_(sd_device_monitor_unrefp
) sd_device_monitor
*monitor
= NULL
;
1741 _cleanup_(iovec_done_erase
) struct iovec decrypted_key
= {};
1742 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1743 _cleanup_free_
char *friendly
= NULL
;
1744 int keyslot
= arg_key_slot
, r
;
1748 assert(arg_tpm2_device
|| arg_tpm2_device_auto
);
1750 friendly
= friendly_disk_name(crypt_get_device_name(cd
), name
);
1755 if (key_file
|| iovec_is_set(key_data
)) {
1756 /* If key data is specified, use that */
1758 r
= acquire_tpm2_key(
1761 arg_tpm2_pcr_mask
== UINT32_MAX
? TPM2_PCR_MASK_DEFAULT
: arg_tpm2_pcr_mask
,
1764 /* pubkey_pcr_mask= */ 0,
1765 /* signature_path= */ NULL
,
1766 /* pcrlock_path= */ NULL
,
1767 /* primary_alg= */ 0,
1768 key_file
, arg_keyfile_size
, arg_keyfile_offset
,
1770 /* policy_hash= */ NULL
, /* we don't know the policy hash */
1773 /* pcrlock_nv= */ NULL
,
1774 arg_tpm2_pin
? TPM2_FLAGS_USE_PIN
: 0,
1777 arg_ask_password_flags
,
1781 if (IN_SET(r
, -EACCES
, -ENOLCK
))
1782 return log_error_errno(SYNTHETIC_ERRNO(EAGAIN
), "TPM2 PIN unlock failed, falling back to traditional unlocking.");
1783 if (ERRNO_IS_NOT_SUPPORTED(r
)) /* TPM2 support not compiled in? */
1784 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN
), "TPM2 support not available, falling back to traditional unlocking.");
1785 /* EAGAIN means: no tpm2 chip found */
1787 log_notice_errno(r
, "TPM2 operation failed, falling back to traditional unlocking: %m");
1788 return -EAGAIN
; /* Mangle error code: let's make any form of TPM2 failure non-fatal. */
1791 r
= attach_luks2_by_tpm2_via_plugin(cd
, name
, until
, arg_headless
, flags
);
1794 /* EAGAIN means: no tpm2 chip found
1795 * EOPNOTSUPP means: no libcryptsetup plugins support */
1797 return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN
),
1798 "No TPM2 metadata matching the current system state found in LUKS2 header, falling back to traditional unlocking.");
1800 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN
),
1801 "No TPM2 metadata enrolled in LUKS2 header or TPM2 support not available, falling back to traditional unlocking.");
1802 if (!IN_SET(r
, -EOPNOTSUPP
, -EAGAIN
)) {
1803 log_notice_errno(r
, "TPM2 operation failed, falling back to traditional unlocking: %m");
1804 return -EAGAIN
; /* Mangle error code: let's make any form of TPM2 failure non-fatal. */
1808 if (r
== -EOPNOTSUPP
) { /* Plugin not available, let's process TPM2 stuff right here instead */
1809 bool found_some
= false;
1810 int token
= 0; /* first token to look at */
1812 /* If no key data is specified, look for it in the header. In order to support
1813 * software upgrades we'll iterate through all suitable tokens, maybe one of them
1817 _cleanup_(iovec_done
) struct iovec pubkey
= {}, salt
= {}, srk
= {}, pcrlock_nv
= {};
1818 _cleanup_(iovec_done
) struct iovec blob
= {}, policy_hash
= {};
1819 uint32_t hash_pcr_mask
, pubkey_pcr_mask
;
1820 uint16_t pcr_bank
, primary_alg
;
1821 TPM2Flags tpm2_flags
;
1823 r
= find_tpm2_auto_data(
1825 arg_tpm2_pcr_mask
, /* if != UINT32_MAX we'll only look for tokens with this PCR mask */
1826 token
, /* search for the token with this index, or any later index than this */
1841 /* No further TPM2 tokens found in the LUKS2 header. */
1842 return log_full_errno(found_some
? LOG_NOTICE
: LOG_DEBUG
,
1843 SYNTHETIC_ERRNO(EAGAIN
),
1845 ? "No TPM2 metadata matching the current system state found in LUKS2 header, falling back to traditional unlocking."
1846 : "No TPM2 metadata enrolled in LUKS2 header, falling back to traditional unlocking.");
1847 if (ERRNO_IS_NEG_NOT_SUPPORTED(r
))
1848 /* TPM2 support not compiled in? */
1849 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN
),
1850 "TPM2 support not available, falling back to traditional unlocking.");
1856 r
= acquire_tpm2_key(
1866 /* key_file= */ NULL
, /* key_file_size= */ 0, /* key_file_offset= */ 0, /* no key file */
1875 arg_ask_password_flags
,
1877 if (IN_SET(r
, -EACCES
, -ENOLCK
))
1878 return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN
), "TPM2 PIN unlock failed, falling back to traditional unlocking.");
1882 token
++; /* try a different token next time */
1887 /* EAGAIN means: no tpm2 chip found */
1889 log_notice_errno(r
, "TPM2 operation failed, falling back to traditional unlocking: %m");
1890 return -EAGAIN
; /* Mangle error code: let's make any form of TPM2 failure non-fatal. */
1895 /* We didn't find the TPM2 device. In this case, watch for it via udev. Let's create
1896 * an event loop and monitor first. */
1900 if (is_efi_boot() && !efi_has_tpm2())
1901 return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN
),
1902 "No TPM2 hardware discovered and EFI firmware does not see it either, falling back to traditional unlocking.");
1904 r
= make_tpm2_device_monitor(&event
, &monitor
);
1908 log_info("TPM2 device not present for unlocking %s, waiting for it to become available.", friendly
);
1910 /* Let's immediately rescan in case the device appeared in the time we needed
1911 * to create and configure the monitor */
1915 r
= run_security_device_monitor(event
, monitor
);
1919 log_debug("Got one or more potentially relevant udev events, rescanning for TPM2...");
1922 if (pass_volume_key
)
1923 r
= measured_crypt_activate_by_volume_key(cd
, name
, decrypted_key
.iov_base
, decrypted_key
.iov_len
, flags
);
1925 _cleanup_(erase_and_freep
) char *base64_encoded
= NULL
;
1926 ssize_t base64_encoded_size
;
1928 /* Before using this key as passphrase we base64 encode it, for compat with homed */
1930 base64_encoded_size
= base64mem(decrypted_key
.iov_base
, decrypted_key
.iov_len
, &base64_encoded
);
1931 if (base64_encoded_size
< 0)
1934 r
= measured_crypt_activate_by_passphrase(cd
, name
, keyslot
, base64_encoded
, base64_encoded_size
, flags
);
1937 log_error_errno(r
, "Failed to activate with TPM2 decrypted key. (Key incorrect?)");
1938 return -EAGAIN
; /* log actual error, but return EAGAIN */
1941 return log_error_errno(r
, "Failed to activate with TPM2 acquired key: %m");
1946 static int attach_luks_or_plain_or_bitlk_by_key_data(
1947 struct crypt_device
*cd
,
1949 const void *key_data
,
1950 size_t key_data_size
,
1952 bool pass_volume_key
) {
1960 if (pass_volume_key
)
1961 r
= measured_crypt_activate_by_volume_key(cd
, name
, key_data
, key_data_size
, flags
);
1963 r
= measured_crypt_activate_by_passphrase(cd
, name
, arg_key_slot
, key_data
, key_data_size
, flags
);
1965 log_error_errno(r
, "Failed to activate. (Key incorrect?)");
1966 return -EAGAIN
; /* Log actual error, but return EAGAIN */
1969 return log_error_errno(r
, "Failed to activate: %m");
1974 static int attach_luks_or_plain_or_bitlk_by_key_file(
1975 struct crypt_device
*cd
,
1977 const char *key_file
,
1979 bool pass_volume_key
) {
1981 _cleanup_(erase_and_freep
) char *kfdata
= NULL
;
1982 _cleanup_free_
char *bindname
= NULL
;
1990 /* If we read the key via AF_UNIX, make this client recognizable */
1991 bindname
= make_bindname(name
);
1995 r
= read_full_file_full(
1997 arg_keyfile_offset
== 0 ? UINT64_MAX
: arg_keyfile_offset
,
1998 arg_keyfile_size
== 0 ? SIZE_MAX
: arg_keyfile_size
,
1999 READ_FULL_FILE_SECURE
|READ_FULL_FILE_WARN_WORLD_READABLE
|READ_FULL_FILE_CONNECT_SOCKET
,
2003 log_error_errno(r
, "Failed to activate, key file '%s' too large.", key_file
);
2007 log_error_errno(r
, "Failed to activate, key file '%s' missing.", key_file
);
2008 return -EAGAIN
; /* Log actual error, but return EAGAIN */
2011 return log_error_errno(r
, "Failed to read key file '%s': %m", key_file
);
2013 if (pass_volume_key
)
2014 r
= measured_crypt_activate_by_volume_key(cd
, name
, kfdata
, kfsize
, flags
);
2016 r
= measured_crypt_activate_by_passphrase(cd
, name
, arg_key_slot
, kfdata
, kfsize
, flags
);
2018 log_error_errno(r
, "Failed to activate with key file '%s'. (Key data incorrect?)", key_file
);
2019 return -EAGAIN
; /* Log actual error, but return EAGAIN */
2022 return log_error_errno(r
, "Failed to activate with key file '%s': %m", key_file
);
2027 static int attach_luks_or_plain_or_bitlk_by_passphrase(
2028 struct crypt_device
*cd
,
2032 bool pass_volume_key
) {
2040 STRV_FOREACH(p
, passwords
) {
2041 if (pass_volume_key
)
2042 r
= measured_crypt_activate_by_volume_key(cd
, name
, *p
, arg_key_size
, flags
);
2044 r
= measured_crypt_activate_by_passphrase(cd
, name
, arg_key_slot
, *p
, strlen(*p
), flags
);
2049 log_error_errno(r
, "Failed to activate with specified passphrase. (Passphrase incorrect?)");
2050 return -EAGAIN
; /* log actual error, but return EAGAIN */
2053 return log_error_errno(r
, "Failed to activate with specified passphrase: %m");
2058 static int attach_luks_or_plain_or_bitlk(
2059 struct crypt_device
*cd
,
2061 const char *key_file
,
2062 const void *key_data
,
2063 size_t key_data_size
,
2068 bool pass_volume_key
= false;
2074 if ((!arg_type
&& !crypt_get_type(cd
)) || streq_ptr(arg_type
, CRYPT_PLAIN
)) {
2075 struct crypt_params_plain params
= {
2076 .offset
= arg_offset
,
2078 .sector_size
= arg_sector_size
,
2080 const char *cipher
, *cipher_mode
;
2081 _cleanup_free_
char *truncated_cipher
= NULL
;
2083 if (streq_ptr(arg_hash
, "plain"))
2084 /* plain isn't a real hash type. it just means "use no hash" */
2087 params
.hash
= arg_hash
;
2089 /* for CRYPT_PLAIN, the behaviour of cryptsetup package is to not hash when a key
2090 * file is provided */
2091 params
.hash
= "ripemd160";
2096 l
= strcspn(arg_cipher
, "-");
2097 truncated_cipher
= strndup(arg_cipher
, l
);
2098 if (!truncated_cipher
)
2101 cipher
= truncated_cipher
;
2102 cipher_mode
= arg_cipher
[l
] ? arg_cipher
+l
+1 : "plain";
2105 cipher_mode
= "cbc-essiv:sha256";
2108 /* for CRYPT_PLAIN limit reads from keyfile to key length, and ignore keyfile-size */
2109 arg_keyfile_size
= arg_key_size
;
2111 /* In contrast to what the name crypt_format() might suggest this doesn't actually format
2112 * anything, it just configures encryption parameters when used for plain mode. */
2113 r
= crypt_format(cd
, CRYPT_PLAIN
, cipher
, cipher_mode
, NULL
, NULL
, arg_keyfile_size
, ¶ms
);
2115 return log_error_errno(r
, "Loading of cryptographic parameters failed: %m");
2117 /* hash == NULL implies the user passed "plain" */
2118 pass_volume_key
= !params
.hash
;
2121 log_info("Set cipher %s, mode %s, key size %i bits for device %s.",
2122 crypt_get_cipher(cd
),
2123 crypt_get_cipher_mode(cd
),
2124 crypt_get_volume_key_size(cd
)*8,
2125 crypt_get_device_name(cd
));
2127 if (arg_tpm2_device
|| arg_tpm2_device_auto
)
2128 return attach_luks_or_plain_or_bitlk_by_tpm2(cd
, name
, key_file
, &IOVEC_MAKE(key_data
, key_data_size
), until
, flags
, pass_volume_key
);
2129 if (arg_fido2_device
|| arg_fido2_device_auto
)
2130 return attach_luks_or_plain_or_bitlk_by_fido2(cd
, name
, key_file
, key_data
, key_data_size
, until
, flags
, pass_volume_key
);
2131 if (arg_pkcs11_uri
|| arg_pkcs11_uri_auto
)
2132 return attach_luks_or_plain_or_bitlk_by_pkcs11(cd
, name
, key_file
, key_data
, key_data_size
, until
, flags
, pass_volume_key
);
2134 return attach_luks_or_plain_or_bitlk_by_key_data(cd
, name
, key_data
, key_data_size
, flags
, pass_volume_key
);
2136 return attach_luks_or_plain_or_bitlk_by_key_file(cd
, name
, key_file
, flags
, pass_volume_key
);
2138 return attach_luks_or_plain_or_bitlk_by_passphrase(cd
, name
, passwords
, flags
, pass_volume_key
);
2141 static int help(void) {
2142 _cleanup_free_
char *link
= NULL
;
2145 r
= terminal_urlify_man("systemd-cryptsetup", "8", &link
);
2149 printf("%1$s attach VOLUME SOURCE-DEVICE [KEY-FILE] [CONFIG]\n"
2150 "%1$s detach VOLUME\n\n"
2151 "%2$sAttach or detach an encrypted block device.%3$s\n\n"
2152 " -h --help Show this help\n"
2153 " --version Show package version\n"
2154 "\nSee the %4$s for details.\n",
2155 program_invocation_short_name
,
2163 static int parse_argv(int argc
, char *argv
[]) {
2165 ARG_VERSION
= 0x100,
2168 static const struct option options
[] = {
2169 { "help", no_argument
, NULL
, 'h' },
2170 { "version", no_argument
, NULL
, ARG_VERSION
},
2179 if (argv_looks_like_help(argc
, argv
))
2182 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
2195 assert_not_reached();
2201 static uint32_t determine_flags(void) {
2205 flags
|= CRYPT_ACTIVATE_READONLY
;
2208 flags
|= CRYPT_ACTIVATE_ALLOW_DISCARDS
;
2210 if (arg_same_cpu_crypt
)
2211 flags
|= CRYPT_ACTIVATE_SAME_CPU_CRYPT
;
2213 if (arg_submit_from_crypt_cpus
)
2214 flags
|= CRYPT_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS
;
2216 if (arg_no_read_workqueue
)
2217 flags
|= CRYPT_ACTIVATE_NO_READ_WORKQUEUE
;
2219 if (arg_no_write_workqueue
)
2220 flags
|= CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE
;
2222 #ifdef CRYPT_ACTIVATE_SERIALIZE_MEMORY_HARD_PBKDF
2223 /* Try to decrease the risk of OOM event if memory hard key derivation function is in use */
2224 /* https://gitlab.com/cryptsetup/cryptsetup/issues/446/ */
2225 flags
|= CRYPT_ACTIVATE_SERIALIZE_MEMORY_HARD_PBKDF
;
2231 static void remove_and_erasep(const char **p
) {
2237 r
= unlinkat_deallocate(AT_FDCWD
, *p
, UNLINK_ERASE
);
2238 if (r
< 0 && r
!= -ENOENT
)
2239 log_warning_errno(r
, "Unable to erase key file '%s', ignoring: %m", *p
);
2242 static int run(int argc
, char *argv
[]) {
2243 _cleanup_(crypt_freep
) struct crypt_device
*cd
= NULL
;
2251 r
= parse_argv(argc
, argv
);
2255 cryptsetup_enable_logging(NULL
);
2257 if (argc
- optind
< 2)
2258 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2259 "This program requires at least two arguments.");
2260 verb
= ASSERT_PTR(argv
[optind
]);
2262 if (streq(verb
, "attach")) {
2263 _unused_
_cleanup_(remove_and_erasep
) const char *destroy_key_file
= NULL
;
2264 _cleanup_(erase_and_freep
) void *key_data
= NULL
;
2265 crypt_status_info status
;
2266 size_t key_data_size
= 0;
2270 PassphraseType passphrase_type
= PASSPHRASE_NONE
;
2272 /* Arguments: systemd-cryptsetup attach VOLUME SOURCE-DEVICE [KEY-FILE] [CONFIG] */
2274 if (argc
- optind
< 3)
2275 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "attach requires at least two arguments.");
2276 if (argc
- optind
>= 6)
2277 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "attach does not accept more than four arguments.");
2279 const char *volume
= ASSERT_PTR(argv
[optind
+ 1]),
2280 *source
= ASSERT_PTR(argv
[optind
+ 2]),
2281 *key_file
= argc
- optind
>= 4 ? mangle_none(argv
[optind
+ 3]) : NULL
,
2282 *config
= argc
- optind
>= 5 ? mangle_none(argv
[optind
+ 4]) : NULL
;
2284 if (!filename_is_valid(volume
))
2285 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Volume name '%s' is not valid.", volume
);
2287 if (key_file
&& !path_is_absolute(key_file
)) {
2288 log_warning("Password file path '%s' is not absolute. Ignoring.", key_file
);
2293 r
= parse_crypt_config(config
);
2298 log_debug("%s %s ← %s type=%s cipher=%s", __func__
,
2299 volume
, source
, strempty(arg_type
), strempty(arg_cipher
));
2301 /* A delicious drop of snake oil */
2302 (void) mlockall(MCL_FUTURE
);
2305 _cleanup_free_
char *bindname
= NULL
;
2308 bindname
= make_bindname(volume
);
2312 /* If a key file is not explicitly specified, search for a key in a well defined
2313 * search path, and load it. */
2315 fn
= strjoina(volume
, ".key");
2318 STRV_MAKE("/etc/cryptsetup-keys.d", "/run/cryptsetup-keys.d"),
2320 &key_data
, &key_data_size
);
2324 log_debug("Automatically discovered key for volume '%s'.", volume
);
2325 } else if (arg_keyfile_erase
)
2326 destroy_key_file
= key_file
; /* let's get this baby erased when we leave */
2329 if (streq_ptr(arg_type
, CRYPT_TCRYPT
)){
2330 log_debug("tcrypt header: %s", arg_header
);
2331 r
= crypt_init_data_device(&cd
, arg_header
, source
);
2333 log_debug("LUKS header: %s", arg_header
);
2334 r
= crypt_init(&cd
, arg_header
);
2337 r
= crypt_init(&cd
, source
);
2339 return log_error_errno(r
, "crypt_init() failed: %m");
2341 cryptsetup_enable_logging(cd
);
2343 status
= crypt_status(cd
, volume
);
2344 if (IN_SET(status
, CRYPT_ACTIVE
, CRYPT_BUSY
)) {
2345 log_info("Volume %s already active.", volume
);
2349 flags
= determine_flags();
2351 until
= usec_add(now(CLOCK_MONOTONIC
), arg_timeout
);
2352 if (until
== USEC_INFINITY
)
2355 if (arg_key_size
== 0)
2356 arg_key_size
= 256U / 8U;
2361 /* Ideally we'd do this on the open fd, but since this is just a
2362 * warning it's OK to do this in two steps. */
2363 if (stat(key_file
, &st
) >= 0 && S_ISREG(st
.st_mode
) && (st
.st_mode
& 0005))
2364 log_warning("Key file %s is world-readable. This is not a good idea!", key_file
);
2367 if (!arg_type
|| STR_IN_SET(arg_type
, ANY_LUKS
, CRYPT_LUKS1
, CRYPT_LUKS2
)) {
2368 r
= crypt_load(cd
, !arg_type
|| streq(arg_type
, ANY_LUKS
) ? CRYPT_LUKS
: arg_type
, NULL
);
2370 return log_error_errno(r
, "Failed to load LUKS superblock on device %s: %m", crypt_get_device_name(cd
));
2372 /* since cryptsetup 2.7.0 (Jan 2024) */
2373 #if HAVE_CRYPT_SET_KEYRING_TO_LINK
2374 if (arg_link_key_description
) {
2375 r
= crypt_set_keyring_to_link(cd
, arg_link_key_description
, NULL
, arg_link_key_type
, arg_link_keyring
);
2377 log_warning_errno(r
, "Failed to set keyring or key description to link volume key in, ignoring: %m");
2382 r
= crypt_set_data_device(cd
, source
);
2384 return log_error_errno(r
, "Failed to set LUKS data device %s: %m", source
);
2387 /* Tokens are available in LUKS2 only, but it is ok to call (and fail) with LUKS1. */
2388 if (!key_file
&& !key_data
&& use_token_plugins()) {
2389 r
= crypt_activate_by_token_pin_ask_password(
2395 /* userdata= */ NULL
,
2397 "Please enter LUKS2 token PIN:",
2399 "cryptsetup.luks2-pin");
2401 log_debug("Volume %s activated with LUKS token id %i.", volume
, r
);
2405 log_debug_errno(r
, "Token activation unsuccessful for device %s: %m", crypt_get_device_name(cd
));
2409 /* since cryptsetup 2.3.0 (Feb 2020) */
2411 if (streq_ptr(arg_type
, CRYPT_BITLK
)) {
2412 r
= crypt_load(cd
, CRYPT_BITLK
, NULL
);
2414 return log_error_errno(r
, "Failed to load Bitlocker superblock on device %s: %m", crypt_get_device_name(cd
));
2418 for (tries
= 0; arg_tries
== 0 || tries
< arg_tries
; tries
++) {
2419 _cleanup_strv_free_erase_
char **passwords
= NULL
;
2421 /* When we were able to acquire multiple keys, let's always process them in this order:
2423 * 1. A key acquired via PKCS#11 or FIDO2 token, or TPM2 chip
2424 * 2. The discovered key: i.e. key_data + key_data_size
2425 * 3. The configured key: i.e. key_file + arg_keyfile_offset + arg_keyfile_size
2426 * 4. The empty password, in case arg_try_empty_password is set
2427 * 5. We enquire the user for a password
2430 if (!key_file
&& !key_data
&& !arg_pkcs11_uri
&& !arg_pkcs11_uri_auto
&& !arg_fido2_device
&& !arg_fido2_device_auto
&& !arg_tpm2_device
&& !arg_tpm2_device_auto
) {
2432 if (arg_try_empty_password
) {
2433 /* Hmm, let's try an empty password now, but only once */
2434 arg_try_empty_password
= false;
2436 key_data
= strdup("");
2442 /* Ask the user for a passphrase or recovery key only as last resort, if we have
2443 * nothing else to check for */
2444 if (passphrase_type
== PASSPHRASE_NONE
) {
2445 passphrase_type
= check_registered_passwords(cd
);
2446 if (passphrase_type
== PASSPHRASE_NONE
)
2447 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "No passphrase or recovery key registered.");
2450 r
= get_password(volume
, source
, until
, tries
== 0 && !arg_verify
, passphrase_type
, &passwords
);
2458 if (streq_ptr(arg_type
, CRYPT_TCRYPT
))
2459 r
= attach_tcrypt(cd
, volume
, key_file
, key_data
, key_data_size
, passwords
, flags
);
2461 r
= attach_luks_or_plain_or_bitlk(cd
, volume
, key_file
, key_data
, key_data_size
, passwords
, flags
, until
);
2467 /* Key not correct? Let's try again! */
2470 key_data
= erase_and_free(key_data
);
2472 arg_pkcs11_uri
= mfree(arg_pkcs11_uri
);
2473 arg_pkcs11_uri_auto
= false;
2474 arg_fido2_device
= mfree(arg_fido2_device
);
2475 arg_fido2_device_auto
= false;
2476 arg_tpm2_device
= mfree(arg_tpm2_device
);
2477 arg_tpm2_device_auto
= false;
2480 if (arg_tries
!= 0 && tries
>= arg_tries
)
2481 return log_error_errno(SYNTHETIC_ERRNO(EPERM
), "Too many attempts to activate; giving up.");
2483 } else if (streq(verb
, "detach")) {
2484 const char *volume
= ASSERT_PTR(argv
[optind
+ 1]);
2486 if (argc
- optind
>= 3)
2487 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "attach does not accept more than one argument.");
2489 if (!filename_is_valid(volume
))
2490 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Volume name '%s' is not valid.", volume
);
2492 r
= crypt_init_by_name(&cd
, volume
);
2494 log_info("Volume %s already inactive.", volume
);
2498 return log_error_errno(r
, "crypt_init_by_name() for volume '%s' failed: %m", volume
);
2500 cryptsetup_enable_logging(cd
);
2502 r
= crypt_deactivate(cd
, volume
);
2504 return log_error_errno(r
, "Failed to deactivate '%s': %m", volume
);
2507 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown verb %s.", verb
);
2512 DEFINE_MAIN_FUNCTION(run
);