]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
e23a0ce8 | 2 | |
7f4e0805 | 3 | #include <errno.h> |
07630cea | 4 | #include <mntent.h> |
07630cea | 5 | #include <sys/mman.h> |
ca78ad1d ZJS |
6 | #include <sys/stat.h> |
7 | #include <sys/types.h> | |
8 | #include <unistd.h> | |
e23a0ce8 | 9 | |
4f5dd394 | 10 | #include "sd-device.h" |
94c0c85e | 11 | #include "sd-messages.h" |
4f5dd394 | 12 | |
b5efdb8a | 13 | #include "alloc-util.h" |
4f5dd394 | 14 | #include "ask-password-api.h" |
2bc5c425 | 15 | #include "cryptsetup-fido2.h" |
1e2f3230 | 16 | #include "cryptsetup-keyfile.h" |
08669709 | 17 | #include "cryptsetup-pkcs11.h" |
18843ecc | 18 | #include "cryptsetup-tpm2.h" |
7407f689 | 19 | #include "cryptsetup-util.h" |
4f5dd394 | 20 | #include "device-util.h" |
7be4b236 | 21 | #include "efi-api.h" |
6c51b49c | 22 | #include "efi-loader.h" |
64c590fb | 23 | #include "env-util.h" |
4f5dd394 | 24 | #include "escape.h" |
8cf3ca80 | 25 | #include "fileio.h" |
d3d49e76 | 26 | #include "fs-util.h" |
ed4ad488 | 27 | #include "fstab-util.h" |
08669709 | 28 | #include "hexdecoct.h" |
2bc5c425 | 29 | #include "libfido2-util.h" |
e23a0ce8 | 30 | #include "log.h" |
3a40f366 | 31 | #include "main-func.h" |
7407f689 | 32 | #include "memory-util.h" |
4349cd7c | 33 | #include "mount-util.h" |
d8b4d14d | 34 | #include "nulstr-util.h" |
6bedfcbb | 35 | #include "parse-util.h" |
9eb977db | 36 | #include "path-util.h" |
08669709 | 37 | #include "pkcs11-util.h" |
d8b4d14d | 38 | #include "pretty-print.h" |
542bb9be | 39 | #include "process-util.h" |
e2c2f868 | 40 | #include "random-util.h" |
b6c0bd11 | 41 | #include "string-table.h" |
21bc923a | 42 | #include "strv.h" |
e0e1f4f7 | 43 | #include "tpm2-pcr.h" |
18843ecc | 44 | #include "tpm2-util.h" |
7f4e0805 | 45 | |
b3b4ebab OK |
46 | /* internal helper */ |
47 | #define ANY_LUKS "LUKS" | |
a9fc6406 | 48 | /* as in src/cryptsetup.h */ |
63b98386 JJ |
49 | #define CRYPT_SECTOR_SIZE 512U |
50 | #define CRYPT_MAX_SECTOR_SIZE 4096U | |
b3b4ebab | 51 | |
b6c0bd11 AAF |
52 | typedef enum PassphraseType { |
53 | PASSPHRASE_NONE, | |
54 | PASSPHRASE_REGULAR = 1 << 0, | |
55 | PASSPHRASE_RECOVERY_KEY = 1 << 1, | |
56 | PASSPHRASE_BOTH = PASSPHRASE_REGULAR|PASSPHRASE_RECOVERY_KEY, | |
57 | _PASSPHRASE_TYPE_MAX, | |
58 | _PASSPHRASE_TYPE_INVALID = -1, | |
59 | } PassphraseType; | |
60 | ||
6cc27c29 | 61 | static const char *arg_type = NULL; /* ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2, CRYPT_TCRYPT, CRYPT_BITLK or CRYPT_PLAIN */ |
f75cac37 LP |
62 | static char *arg_cipher = NULL; |
63 | static unsigned arg_key_size = 0; | |
a9fc6406 | 64 | static unsigned arg_sector_size = CRYPT_SECTOR_SIZE; |
f75cac37 LP |
65 | static int arg_key_slot = CRYPT_ANY_SLOT; |
66 | static unsigned arg_keyfile_size = 0; | |
dc0a3555 | 67 | static uint64_t arg_keyfile_offset = 0; |
d3d49e76 | 68 | static bool arg_keyfile_erase = false; |
0ba6f85e | 69 | static bool arg_try_empty_password = false; |
f75cac37 | 70 | static char *arg_hash = NULL; |
7376e835 | 71 | static char *arg_header = NULL; |
f75cac37 LP |
72 | static unsigned arg_tries = 3; |
73 | static bool arg_readonly = false; | |
74 | static bool arg_verify = false; | |
2cbca51a | 75 | static AskPasswordFlags arg_ask_password_flags = 0; |
f75cac37 | 76 | static bool arg_discards = false; |
2c65512e YW |
77 | static bool arg_same_cpu_crypt = false; |
78 | static bool arg_submit_from_crypt_cpus = false; | |
227acf00 JU |
79 | static bool arg_no_read_workqueue = false; |
80 | static bool arg_no_write_workqueue = false; | |
f75cac37 LP |
81 | static bool arg_tcrypt_hidden = false; |
82 | static bool arg_tcrypt_system = false; | |
52028838 | 83 | static bool arg_tcrypt_veracrypt = false; |
70390240 | 84 | static uint32_t arg_tcrypt_veracrypt_pim = 0; |
f75cac37 | 85 | static char **arg_tcrypt_keyfiles = NULL; |
4eac2773 MP |
86 | static uint64_t arg_offset = 0; |
87 | static uint64_t arg_skip = 0; | |
0864d311 | 88 | static usec_t arg_timeout = USEC_INFINITY; |
08669709 | 89 | static char *arg_pkcs11_uri = NULL; |
b997d111 | 90 | static bool arg_pkcs11_uri_auto = false; |
2bc5c425 LP |
91 | static char *arg_fido2_device = NULL; |
92 | static bool arg_fido2_device_auto = false; | |
93 | static void *arg_fido2_cid = NULL; | |
94 | static size_t arg_fido2_cid_size = 0; | |
95 | static char *arg_fido2_rp_id = NULL; | |
d09df6b9 | 96 | static char *arg_tpm2_device = NULL; /* These and the following fields are about locking an encrypted volume to the local TPM */ |
18843ecc LP |
97 | static bool arg_tpm2_device_auto = false; |
98 | static uint32_t arg_tpm2_pcr_mask = UINT32_MAX; | |
dc63b2c9 | 99 | static char *arg_tpm2_signature = NULL; |
4005d41e | 100 | static bool arg_tpm2_pin = false; |
cd5f57bd | 101 | static bool arg_headless = false; |
5cbe70af | 102 | static usec_t arg_token_timeout_usec = 30*USEC_PER_SEC; |
94c0c85e LP |
103 | static unsigned arg_tpm2_measure_pcr = UINT_MAX; /* This and the following field is about measuring the unlocked volume key to the local TPM */ |
104 | static char **arg_tpm2_measure_banks = NULL; | |
7f4e0805 | 105 | |
3a40f366 YW |
106 | STATIC_DESTRUCTOR_REGISTER(arg_cipher, freep); |
107 | STATIC_DESTRUCTOR_REGISTER(arg_hash, freep); | |
108 | STATIC_DESTRUCTOR_REGISTER(arg_header, freep); | |
109 | STATIC_DESTRUCTOR_REGISTER(arg_tcrypt_keyfiles, strv_freep); | |
08669709 | 110 | STATIC_DESTRUCTOR_REGISTER(arg_pkcs11_uri, freep); |
2bc5c425 LP |
111 | STATIC_DESTRUCTOR_REGISTER(arg_fido2_device, freep); |
112 | STATIC_DESTRUCTOR_REGISTER(arg_fido2_cid, freep); | |
113 | STATIC_DESTRUCTOR_REGISTER(arg_fido2_rp_id, freep); | |
18843ecc | 114 | STATIC_DESTRUCTOR_REGISTER(arg_tpm2_device, freep); |
dc63b2c9 | 115 | STATIC_DESTRUCTOR_REGISTER(arg_tpm2_signature, freep); |
94c0c85e | 116 | STATIC_DESTRUCTOR_REGISTER(arg_tpm2_measure_banks, strv_freep); |
3a40f366 | 117 | |
b6c0bd11 AAF |
118 | static const char* const passphrase_type_table[_PASSPHRASE_TYPE_MAX] = { |
119 | [PASSPHRASE_REGULAR] = "passphrase", | |
120 | [PASSPHRASE_RECOVERY_KEY] = "recovery key", | |
121 | [PASSPHRASE_BOTH] = "passphrase or recovery key", | |
122 | }; | |
123 | ||
124 | const char* passphrase_type_to_string(PassphraseType t); | |
125 | PassphraseType passphrase_type_from_string(const char *s); | |
126 | ||
127 | DEFINE_STRING_TABLE_LOOKUP(passphrase_type, PassphraseType); | |
128 | ||
1fc76335 LP |
129 | /* Options Debian's crypttab knows we don't: |
130 | ||
1fc76335 LP |
131 | check= |
132 | checkargs= | |
8ced40c0 LP |
133 | noearly |
134 | loud | |
135 | quiet | |
1fc76335 | 136 | keyscript= |
8ced40c0 | 137 | initramfs |
1fc76335 LP |
138 | */ |
139 | ||
7f4e0805 | 140 | static int parse_one_option(const char *option) { |
fb4650aa ZJS |
141 | const char *val; |
142 | int r; | |
143 | ||
7f4e0805 LP |
144 | assert(option); |
145 | ||
146 | /* Handled outside of this tool */ | |
50d2eba2 | 147 | if (STR_IN_SET(option, "noauto", "auto", "nofail", "fail", "_netdev", "keyfile-timeout")) |
148 | return 0; | |
149 | ||
150 | if (startswith(option, "keyfile-timeout=")) | |
7f4e0805 LP |
151 | return 0; |
152 | ||
fb4650aa ZJS |
153 | if ((val = startswith(option, "cipher="))) { |
154 | r = free_and_strdup(&arg_cipher, val); | |
155 | if (r < 0) | |
4b93637f | 156 | return log_oom(); |
7f4e0805 | 157 | |
fb4650aa | 158 | } else if ((val = startswith(option, "size="))) { |
7f4e0805 | 159 | |
fb4650aa ZJS |
160 | r = safe_atou(val, &arg_key_size); |
161 | if (r < 0) { | |
b96cc40a | 162 | log_warning_errno(r, "Failed to parse %s, ignoring: %m", option); |
7f4e0805 LP |
163 | return 0; |
164 | } | |
165 | ||
6131a78b | 166 | if (arg_key_size % 8) { |
b96cc40a | 167 | log_warning("size= not a multiple of 8, ignoring."); |
6131a78b DH |
168 | return 0; |
169 | } | |
170 | ||
171 | arg_key_size /= 8; | |
172 | ||
a9fc6406 DJL |
173 | } else if ((val = startswith(option, "sector-size="))) { |
174 | ||
a9fc6406 DJL |
175 | r = safe_atou(val, &arg_sector_size); |
176 | if (r < 0) { | |
b96cc40a | 177 | log_warning_errno(r, "Failed to parse %s, ignoring: %m", option); |
a9fc6406 DJL |
178 | return 0; |
179 | } | |
180 | ||
181 | if (arg_sector_size % 2) { | |
b96cc40a | 182 | log_warning("sector-size= not a multiple of 2, ignoring."); |
a9fc6406 DJL |
183 | return 0; |
184 | } | |
185 | ||
b96cc40a LP |
186 | if (arg_sector_size < CRYPT_SECTOR_SIZE || arg_sector_size > CRYPT_MAX_SECTOR_SIZE) |
187 | log_warning("sector-size= is outside of %u and %u, ignoring.", CRYPT_SECTOR_SIZE, CRYPT_MAX_SECTOR_SIZE); | |
a9fc6406 | 188 | |
8ced40c0 LP |
189 | } else if ((val = startswith(option, "key-slot=")) || |
190 | (val = startswith(option, "keyslot="))) { | |
b4a11878 | 191 | |
b3b4ebab | 192 | arg_type = ANY_LUKS; |
fb4650aa | 193 | r = safe_atoi(val, &arg_key_slot); |
b96cc40a LP |
194 | if (r < 0) |
195 | log_warning_errno(r, "Failed to parse %s, ignoring: %m", option); | |
b4a11878 | 196 | |
fb4650aa | 197 | } else if ((val = startswith(option, "tcrypt-keyfile="))) { |
8cf3ca80 | 198 | |
f75cac37 | 199 | arg_type = CRYPT_TCRYPT; |
fb4650aa ZJS |
200 | if (path_is_absolute(val)) { |
201 | if (strv_extend(&arg_tcrypt_keyfiles, val) < 0) | |
4b93637f LP |
202 | return log_oom(); |
203 | } else | |
b96cc40a | 204 | log_warning("Key file path \"%s\" is not absolute, ignoring.", val); |
8cf3ca80 | 205 | |
fb4650aa | 206 | } else if ((val = startswith(option, "keyfile-size="))) { |
4271d823 | 207 | |
fb4650aa | 208 | r = safe_atou(val, &arg_keyfile_size); |
b96cc40a LP |
209 | if (r < 0) |
210 | log_warning_errno(r, "Failed to parse %s, ignoring: %m", option); | |
4271d823 | 211 | |
fb4650aa | 212 | } else if ((val = startswith(option, "keyfile-offset="))) { |
880a599e | 213 | |
d90874b4 | 214 | r = safe_atou64(val, &arg_keyfile_offset); |
b96cc40a LP |
215 | if (r < 0) |
216 | log_warning_errno(r, "Failed to parse %s, ignoring: %m", option); | |
880a599e | 217 | |
d3d49e76 LP |
218 | } else if ((val = startswith(option, "keyfile-erase="))) { |
219 | ||
220 | r = parse_boolean(val); | |
221 | if (r < 0) { | |
b96cc40a | 222 | log_warning_errno(r, "Failed to parse %s, ignoring: %m", option); |
d3d49e76 LP |
223 | return 0; |
224 | } | |
225 | ||
226 | arg_keyfile_erase = r; | |
227 | ||
228 | } else if (streq(option, "keyfile-erase")) | |
229 | arg_keyfile_erase = true; | |
230 | ||
231 | else if ((val = startswith(option, "hash="))) { | |
fb4650aa ZJS |
232 | r = free_and_strdup(&arg_hash, val); |
233 | if (r < 0) | |
4b93637f | 234 | return log_oom(); |
7f4e0805 | 235 | |
fb4650aa | 236 | } else if ((val = startswith(option, "header="))) { |
cdece7e3 | 237 | if (!arg_type || !STR_IN_SET(arg_type, ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2, CRYPT_TCRYPT)) |
70390240 | 238 | arg_type = ANY_LUKS; |
7376e835 | 239 | |
baaa35ad ZJS |
240 | if (!path_is_absolute(val)) |
241 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
242 | "Header path \"%s\" is not absolute, refusing.", val); | |
7376e835 | 243 | |
baaa35ad ZJS |
244 | if (arg_header) |
245 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
246 | "Duplicate header= option, refusing."); | |
7376e835 | 247 | |
fb4650aa | 248 | arg_header = strdup(val); |
7376e835 AC |
249 | if (!arg_header) |
250 | return log_oom(); | |
251 | ||
fb4650aa | 252 | } else if ((val = startswith(option, "tries="))) { |
7f4e0805 | 253 | |
fb4650aa | 254 | r = safe_atou(val, &arg_tries); |
b96cc40a LP |
255 | if (r < 0) |
256 | log_warning_errno(r, "Failed to parse %s, ignoring: %m", option); | |
7f4e0805 | 257 | |
f75cac37 LP |
258 | } else if (STR_IN_SET(option, "readonly", "read-only")) |
259 | arg_readonly = true; | |
7f4e0805 | 260 | else if (streq(option, "verify")) |
f75cac37 | 261 | arg_verify = true; |
2cbca51a SB |
262 | else if ((val = startswith(option, "password-echo="))) { |
263 | if (streq(val, "masked")) | |
264 | arg_ask_password_flags &= ~(ASK_PASSWORD_ECHO|ASK_PASSWORD_SILENT); | |
265 | else { | |
266 | r = parse_boolean(val); | |
267 | if (r < 0) { | |
268 | log_warning_errno(r, "Invalid password-echo= option \"%s\", ignoring.", val); | |
269 | return 0; | |
270 | } | |
271 | ||
272 | SET_FLAG(arg_ask_password_flags, ASK_PASSWORD_ECHO, r); | |
273 | SET_FLAG(arg_ask_password_flags, ASK_PASSWORD_SILENT, !r); | |
274 | } | |
275 | } else if (STR_IN_SET(option, "allow-discards", "discard")) | |
f75cac37 | 276 | arg_discards = true; |
2c65512e YW |
277 | else if (streq(option, "same-cpu-crypt")) |
278 | arg_same_cpu_crypt = true; | |
279 | else if (streq(option, "submit-from-crypt-cpus")) | |
280 | arg_submit_from_crypt_cpus = true; | |
227acf00 JU |
281 | else if (streq(option, "no-read-workqueue")) |
282 | arg_no_read_workqueue = true; | |
283 | else if (streq(option, "no-write-workqueue")) | |
284 | arg_no_write_workqueue = true; | |
260ab287 | 285 | else if (streq(option, "luks")) |
b3b4ebab | 286 | arg_type = ANY_LUKS; |
6cc27c29 MF |
287 | /* since cryptsetup 2.3.0 (Feb 2020) */ |
288 | #ifdef CRYPT_BITLK | |
289 | else if (streq(option, "bitlk")) | |
290 | arg_type = CRYPT_BITLK; | |
291 | #endif | |
8cf3ca80 | 292 | else if (streq(option, "tcrypt")) |
f75cac37 | 293 | arg_type = CRYPT_TCRYPT; |
8ced40c0 | 294 | else if (STR_IN_SET(option, "tcrypt-hidden", "tcrypthidden")) { |
f75cac37 LP |
295 | arg_type = CRYPT_TCRYPT; |
296 | arg_tcrypt_hidden = true; | |
8cf3ca80 | 297 | } else if (streq(option, "tcrypt-system")) { |
f75cac37 LP |
298 | arg_type = CRYPT_TCRYPT; |
299 | arg_tcrypt_system = true; | |
8ced40c0 | 300 | } else if (STR_IN_SET(option, "tcrypt-veracrypt", "veracrypt")) { |
52028838 GH |
301 | arg_type = CRYPT_TCRYPT; |
302 | arg_tcrypt_veracrypt = true; | |
70390240 KZ |
303 | } else if ((val = startswith(option, "veracrypt-pim="))) { |
304 | ||
305 | r = safe_atou32(val, &arg_tcrypt_veracrypt_pim); | |
306 | if (r < 0) { | |
307 | log_warning_errno(r, "Failed to parse %s, ignoring: %m", option); | |
308 | return 0; | |
309 | } | |
53ac130b LP |
310 | } else if (STR_IN_SET(option, "plain", "swap", "tmp") || |
311 | startswith(option, "tmp=")) | |
f75cac37 | 312 | arg_type = CRYPT_PLAIN; |
fb4650aa | 313 | else if ((val = startswith(option, "timeout="))) { |
7f4e0805 | 314 | |
0004f698 | 315 | r = parse_sec_fix_0(val, &arg_timeout); |
b96cc40a LP |
316 | if (r < 0) |
317 | log_warning_errno(r, "Failed to parse %s, ignoring: %m", option); | |
7f4e0805 | 318 | |
fb4650aa | 319 | } else if ((val = startswith(option, "offset="))) { |
4eac2773 | 320 | |
fb4650aa ZJS |
321 | r = safe_atou64(val, &arg_offset); |
322 | if (r < 0) | |
323 | return log_error_errno(r, "Failed to parse %s: %m", option); | |
4eac2773 | 324 | |
fb4650aa | 325 | } else if ((val = startswith(option, "skip="))) { |
4eac2773 | 326 | |
fb4650aa ZJS |
327 | r = safe_atou64(val, &arg_skip); |
328 | if (r < 0) | |
329 | return log_error_errno(r, "Failed to parse %s: %m", option); | |
4eac2773 | 330 | |
08669709 LP |
331 | } else if ((val = startswith(option, "pkcs11-uri="))) { |
332 | ||
b997d111 LP |
333 | if (streq(val, "auto")) { |
334 | arg_pkcs11_uri = mfree(arg_pkcs11_uri); | |
335 | arg_pkcs11_uri_auto = true; | |
336 | } else { | |
337 | if (!pkcs11_uri_valid(val)) | |
338 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "pkcs11-uri= parameter expects a PKCS#11 URI, refusing"); | |
08669709 | 339 | |
b997d111 LP |
340 | r = free_and_strdup(&arg_pkcs11_uri, val); |
341 | if (r < 0) | |
342 | return log_oom(); | |
343 | ||
344 | arg_pkcs11_uri_auto = false; | |
345 | } | |
08669709 | 346 | |
2bc5c425 LP |
347 | } else if ((val = startswith(option, "fido2-device="))) { |
348 | ||
349 | if (streq(val, "auto")) { | |
350 | arg_fido2_device = mfree(arg_fido2_device); | |
351 | arg_fido2_device_auto = true; | |
352 | } else { | |
353 | r = free_and_strdup(&arg_fido2_device, val); | |
354 | if (r < 0) | |
355 | return log_oom(); | |
356 | ||
357 | arg_fido2_device_auto = false; | |
358 | } | |
359 | ||
360 | } else if ((val = startswith(option, "fido2-cid="))) { | |
361 | ||
362 | if (streq(val, "auto")) | |
363 | arg_fido2_cid = mfree(arg_fido2_cid); | |
364 | else { | |
365 | _cleanup_free_ void *cid = NULL; | |
366 | size_t cid_size; | |
367 | ||
f5fbe71d | 368 | r = unbase64mem(val, SIZE_MAX, &cid, &cid_size); |
2bc5c425 LP |
369 | if (r < 0) |
370 | return log_error_errno(r, "Failed to decode FIDO2 CID data: %m"); | |
371 | ||
372 | free(arg_fido2_cid); | |
373 | arg_fido2_cid = TAKE_PTR(cid); | |
374 | arg_fido2_cid_size = cid_size; | |
375 | } | |
376 | ||
377 | /* Turn on FIDO2 as side-effect, if not turned on yet. */ | |
378 | if (!arg_fido2_device && !arg_fido2_device_auto) | |
379 | arg_fido2_device_auto = true; | |
380 | ||
381 | } else if ((val = startswith(option, "fido2-rp="))) { | |
382 | ||
383 | r = free_and_strdup(&arg_fido2_rp_id, val); | |
384 | if (r < 0) | |
385 | return log_oom(); | |
386 | ||
18843ecc LP |
387 | } else if ((val = startswith(option, "tpm2-device="))) { |
388 | ||
389 | if (streq(val, "auto")) { | |
390 | arg_tpm2_device = mfree(arg_tpm2_device); | |
391 | arg_tpm2_device_auto = true; | |
392 | } else { | |
393 | r = free_and_strdup(&arg_tpm2_device, val); | |
394 | if (r < 0) | |
395 | return log_oom(); | |
396 | ||
397 | arg_tpm2_device_auto = false; | |
398 | } | |
399 | ||
400 | } else if ((val = startswith(option, "tpm2-pcrs="))) { | |
401 | ||
07c04061 | 402 | r = tpm2_parse_pcr_argument_to_mask(val, &arg_tpm2_pcr_mask); |
222a951f LP |
403 | if (r < 0) |
404 | return r; | |
18843ecc | 405 | |
dc63b2c9 LP |
406 | } else if ((val = startswith(option, "tpm2-signature="))) { |
407 | ||
408 | if (!path_is_absolute(val)) | |
409 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
410 | "TPM2 signature path \"%s\" is not absolute, refusing.", val); | |
411 | ||
412 | r = free_and_strdup(&arg_tpm2_signature, val); | |
413 | if (r < 0) | |
414 | return log_oom(); | |
415 | ||
4005d41e GG |
416 | } else if ((val = startswith(option, "tpm2-pin="))) { |
417 | ||
418 | r = parse_boolean(val); | |
419 | if (r < 0) { | |
b96cc40a | 420 | log_warning_errno(r, "Failed to parse %s, ignoring: %m", option); |
4005d41e GG |
421 | return 0; |
422 | } | |
423 | ||
424 | arg_tpm2_pin = r; | |
425 | ||
94c0c85e LP |
426 | } else if ((val = startswith(option, "tpm2-measure-pcr="))) { |
427 | unsigned pcr; | |
428 | ||
429 | r = safe_atou(val, &pcr); | |
430 | if (r < 0) { | |
431 | r = parse_boolean(val); | |
432 | if (r < 0) { | |
433 | log_error_errno(r, "Failed to parse %s, ignoring: %m", option); | |
434 | return 0; | |
435 | } | |
436 | ||
2099cd62 | 437 | pcr = r ? TPM2_PCR_SYSTEM_IDENTITY : UINT_MAX; |
323eb480 | 438 | } else if (!TPM2_PCR_INDEX_VALID(pcr)) { |
b96cc40a | 439 | log_warning("Selected TPM index for measurement %u outside of allowed range 0…%u, ignoring.", pcr, TPM2_PCRS_MAX-1); |
94c0c85e LP |
440 | return 0; |
441 | } | |
442 | ||
443 | arg_tpm2_measure_pcr = pcr; | |
444 | ||
445 | } else if ((val = startswith(option, "tpm2-measure-bank="))) { | |
446 | ||
447 | #if HAVE_OPENSSL | |
448 | _cleanup_strv_free_ char **l = NULL; | |
449 | ||
450 | l = strv_split(optarg, ":"); | |
451 | if (!l) | |
452 | return log_oom(); | |
453 | ||
454 | STRV_FOREACH(i, l) { | |
455 | const EVP_MD *implementation; | |
456 | ||
457 | implementation = EVP_get_digestbyname(*i); | |
458 | if (!implementation) | |
459 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown bank '%s', refusing.", val); | |
460 | ||
461 | if (strv_extend(&arg_tpm2_measure_banks, EVP_MD_name(implementation)) < 0) | |
462 | return log_oom(); | |
463 | } | |
464 | #else | |
465 | log_error("Build lacks OpenSSL support, cannot measure to PCR banks, ignoring: %s", option); | |
466 | #endif | |
467 | ||
0ba6f85e LP |
468 | } else if ((val = startswith(option, "try-empty-password="))) { |
469 | ||
470 | r = parse_boolean(val); | |
471 | if (r < 0) { | |
b96cc40a | 472 | log_warning_errno(r, "Failed to parse %s, ignoring: %m", option); |
0ba6f85e LP |
473 | return 0; |
474 | } | |
475 | ||
476 | arg_try_empty_password = r; | |
477 | ||
478 | } else if (streq(option, "try-empty-password")) | |
479 | arg_try_empty_password = true; | |
cd5f57bd LB |
480 | else if ((val = startswith(option, "headless="))) { |
481 | ||
482 | r = parse_boolean(val); | |
483 | if (r < 0) { | |
b96cc40a | 484 | log_warning_errno(r, "Failed to parse %s, ignoring: %m", option); |
cd5f57bd LB |
485 | return 0; |
486 | } | |
487 | ||
488 | arg_headless = r; | |
489 | } else if (streq(option, "headless")) | |
490 | arg_headless = true; | |
0ba6f85e | 491 | |
5cbe70af LP |
492 | else if ((val = startswith(option, "token-timeout="))) { |
493 | ||
494 | r = parse_sec_fix_0(val, &arg_token_timeout_usec); | |
b96cc40a LP |
495 | if (r < 0) |
496 | log_warning_errno(r, "Failed to parse %s, ignoring: %m", option); | |
5cbe70af LP |
497 | |
498 | } else if (!streq(option, "x-initrd.attach")) | |
fb4650aa | 499 | log_warning("Encountered unknown /etc/crypttab option '%s', ignoring.", option); |
7f4e0805 LP |
500 | |
501 | return 0; | |
502 | } | |
503 | ||
504 | static int parse_options(const char *options) { | |
7f4e0805 LP |
505 | assert(options); |
506 | ||
dd2fff3a ZJS |
507 | for (;;) { |
508 | _cleanup_free_ char *word = NULL; | |
509 | int r; | |
510 | ||
7bb553bb | 511 | r = extract_first_word(&options, &word, ",", EXTRACT_DONT_COALESCE_SEPARATORS | EXTRACT_UNESCAPE_SEPARATORS); |
dd2fff3a | 512 | if (r < 0) |
be36bc1e | 513 | return log_error_errno(r, "Failed to parse options: %m"); |
dd2fff3a ZJS |
514 | if (r == 0) |
515 | break; | |
7f4e0805 | 516 | |
dd2fff3a | 517 | r = parse_one_option(word); |
7f4e0805 LP |
518 | if (r < 0) |
519 | return r; | |
520 | } | |
521 | ||
4eac2773 | 522 | /* sanity-check options */ |
9c5253ff LP |
523 | if (arg_type && !streq(arg_type, CRYPT_PLAIN)) { |
524 | if (arg_offset != 0) | |
4eac2773 | 525 | log_warning("offset= ignored with type %s", arg_type); |
9c5253ff | 526 | if (arg_skip != 0) |
4eac2773 MP |
527 | log_warning("skip= ignored with type %s", arg_type); |
528 | } | |
529 | ||
7f4e0805 LP |
530 | return 0; |
531 | } | |
532 | ||
1ca208fb | 533 | static char* disk_description(const char *path) { |
f75cac37 | 534 | static const char name_fields[] = |
74b1c371 LP |
535 | "DM_NAME\0" |
536 | "ID_MODEL_FROM_DATABASE\0" | |
f75cac37 | 537 | "ID_MODEL\0"; |
74b1c371 | 538 | |
4afd3348 | 539 | _cleanup_(sd_device_unrefp) sd_device *device = NULL; |
12e2b70f | 540 | const char *name; |
b1a2da0a | 541 | struct stat st; |
b1a2da0a LP |
542 | |
543 | assert(path); | |
544 | ||
545 | if (stat(path, &st) < 0) | |
546 | return NULL; | |
547 | ||
548 | if (!S_ISBLK(st.st_mode)) | |
549 | return NULL; | |
550 | ||
930aa88f | 551 | if (sd_device_new_from_stat_rdev(&device, &st) < 0) |
1ca208fb | 552 | return NULL; |
b1a2da0a | 553 | |
fadd34dd LP |
554 | if (sd_device_get_property_value(device, "ID_PART_ENTRY_NAME", &name) >= 0) { |
555 | _cleanup_free_ char *unescaped = NULL; | |
e437538f | 556 | ssize_t l; |
fadd34dd LP |
557 | |
558 | /* ID_PART_ENTRY_NAME uses \x style escaping, using libblkid's blkid_encode_string(). Let's | |
559 | * reverse this here to make the string more human friendly in case people embed spaces or | |
560 | * other weird stuff. */ | |
561 | ||
e437538f ZJS |
562 | l = cunescape(name, UNESCAPE_RELAX, &unescaped); |
563 | if (l < 0) { | |
564 | log_debug_errno(l, "Failed to unescape ID_PART_ENTRY_NAME, skipping device: %m"); | |
fadd34dd LP |
565 | return NULL; |
566 | } | |
567 | ||
568 | if (!isempty(unescaped) && !string_has_cc(unescaped, NULL)) | |
569 | return TAKE_PTR(unescaped); | |
570 | } | |
571 | ||
572 | /* These need no unescaping. */ | |
2c740afd YW |
573 | NULSTR_FOREACH(i, name_fields) |
574 | if (sd_device_get_property_value(device, i, &name) >= 0 && | |
575 | !isempty(name)) | |
1ca208fb | 576 | return strdup(name); |
b1a2da0a | 577 | |
1ca208fb | 578 | return NULL; |
b1a2da0a LP |
579 | } |
580 | ||
b61e476f | 581 | static char *disk_mount_point(const char *label) { |
e7d90b71 | 582 | _cleanup_free_ char *device = NULL; |
5862d652 | 583 | _cleanup_endmntent_ FILE *f = NULL; |
b61e476f LP |
584 | struct mntent *m; |
585 | ||
586 | /* Yeah, we don't support native systemd unit files here for now */ | |
587 | ||
090685b5 LP |
588 | device = strjoin("/dev/mapper/", label); |
589 | if (!device) | |
5862d652 | 590 | return NULL; |
b61e476f | 591 | |
ed4ad488 | 592 | f = setmntent(fstab_path(), "re"); |
e0295d26 | 593 | if (!f) |
5862d652 | 594 | return NULL; |
b61e476f LP |
595 | |
596 | while ((m = getmntent(f))) | |
5862d652 ZJS |
597 | if (path_equal(m->mnt_fsname, device)) |
598 | return strdup(m->mnt_dir); | |
b61e476f | 599 | |
5862d652 | 600 | return NULL; |
b61e476f LP |
601 | } |
602 | ||
08669709 LP |
603 | static char *friendly_disk_name(const char *src, const char *vol) { |
604 | _cleanup_free_ char *description = NULL, *mount_point = NULL; | |
605 | char *name_buffer = NULL; | |
606 | int r; | |
e7d90b71 | 607 | |
e51b9486 | 608 | assert(src); |
08669709 | 609 | assert(vol); |
e7d90b71 | 610 | |
e51b9486 HH |
611 | description = disk_description(src); |
612 | mount_point = disk_mount_point(vol); | |
613 | ||
08669709 | 614 | /* If the description string is simply the volume name, then let's not show this twice */ |
ece174c5 | 615 | if (description && streq(vol, description)) |
97b11eed | 616 | description = mfree(description); |
e51b9486 HH |
617 | |
618 | if (mount_point && description) | |
619 | r = asprintf(&name_buffer, "%s (%s) on %s", description, vol, mount_point); | |
620 | else if (mount_point) | |
621 | r = asprintf(&name_buffer, "%s on %s", vol, mount_point); | |
622 | else if (description) | |
623 | r = asprintf(&name_buffer, "%s (%s)", description, vol); | |
08669709 LP |
624 | else |
625 | return strdup(vol); | |
e51b9486 | 626 | if (r < 0) |
08669709 LP |
627 | return NULL; |
628 | ||
629 | return name_buffer; | |
630 | } | |
631 | ||
b6c0bd11 AAF |
632 | static PassphraseType check_registered_passwords(struct crypt_device *cd) { |
633 | _cleanup_free_ bool *slots = NULL; | |
634 | int slot_max; | |
635 | PassphraseType passphrase_type = PASSPHRASE_NONE; | |
636 | ||
637 | assert(cd); | |
638 | ||
639 | if (!streq_ptr(crypt_get_type(cd), CRYPT_LUKS2)) { | |
640 | log_debug("%s: not a LUKS2 device, only passphrases are supported", crypt_get_device_name(cd)); | |
641 | return PASSPHRASE_REGULAR; | |
642 | } | |
643 | ||
644 | /* Search all used slots */ | |
645 | assert_se((slot_max = crypt_keyslot_max(CRYPT_LUKS2)) > 0); | |
646 | slots = new(bool, slot_max); | |
647 | if (!slots) | |
648 | return log_oom(); | |
649 | ||
650 | for (int slot = 0; slot < slot_max; slot++) | |
651 | slots[slot] = IN_SET(crypt_keyslot_status(cd, slot), CRYPT_SLOT_ACTIVE, CRYPT_SLOT_ACTIVE_LAST); | |
652 | ||
653 | /* Iterate all LUKS2 tokens and keep track of all their slots */ | |
654 | for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token++) { | |
655 | _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; | |
656 | const char *type; | |
657 | JsonVariant *w, *z; | |
658 | int tk; | |
659 | ||
660 | tk = cryptsetup_get_token_as_json(cd, token, NULL, &v); | |
661 | if (IN_SET(tk, -ENOENT, -EINVAL)) | |
662 | continue; | |
663 | if (tk < 0) { | |
664 | log_warning_errno(tk, "Failed to read JSON token data, ignoring: %m"); | |
665 | continue; | |
666 | } | |
667 | ||
668 | w = json_variant_by_key(v, "type"); | |
669 | if (!w || !json_variant_is_string(w)) { | |
670 | log_warning("Token JSON data lacks type field, ignoring."); | |
671 | continue; | |
672 | } | |
673 | ||
674 | type = json_variant_string(w); | |
675 | if (STR_IN_SET(type, "systemd-recovery", "systemd-pkcs11", "systemd-fido2", "systemd-tpm2")) { | |
676 | ||
677 | /* At least exists one recovery key */ | |
678 | if (streq(type, "systemd-recovery")) | |
679 | passphrase_type |= PASSPHRASE_RECOVERY_KEY; | |
680 | ||
681 | w = json_variant_by_key(v, "keyslots"); | |
682 | if (!w || !json_variant_is_array(w)) { | |
683 | log_warning("Token JSON data lacks keyslots field, ignoring."); | |
684 | continue; | |
685 | } | |
686 | ||
687 | JSON_VARIANT_ARRAY_FOREACH(z, w) { | |
688 | unsigned u; | |
689 | int at; | |
690 | ||
691 | if (!json_variant_is_string(z)) { | |
692 | log_warning("Token JSON data's keyslot field is not an array of strings, ignoring."); | |
693 | continue; | |
694 | } | |
695 | ||
696 | at = safe_atou(json_variant_string(z), &u); | |
697 | if (at < 0) { | |
698 | log_warning_errno(at, "Token JSON data's keyslot field is not an integer formatted as string, ignoring."); | |
699 | continue; | |
700 | } | |
701 | ||
702 | if (u >= (unsigned) slot_max) { | |
703 | log_warning_errno(at, "Token JSON data's keyslot field exceeds the maximum value allowed, ignoring."); | |
704 | continue; | |
705 | } | |
706 | ||
707 | slots[u] = false; | |
708 | } | |
709 | } | |
710 | } | |
711 | ||
712 | /* Check if any of the slots is not referenced by systemd tokens */ | |
713 | for (int slot = 0; slot < slot_max; slot++) | |
714 | if (slots[slot]) { | |
715 | passphrase_type |= PASSPHRASE_REGULAR; | |
716 | break; | |
717 | } | |
718 | ||
719 | /* All the slots are referenced by systemd tokens, so if a recovery key is not enrolled, | |
720 | * we will not be able to enter a passphrase. */ | |
721 | return passphrase_type; | |
722 | } | |
723 | ||
08669709 LP |
724 | static int get_password( |
725 | const char *vol, | |
726 | const char *src, | |
727 | usec_t until, | |
728 | bool accept_cached, | |
b6c0bd11 | 729 | PassphraseType passphrase_type, |
08669709 LP |
730 | char ***ret) { |
731 | ||
732 | _cleanup_free_ char *friendly = NULL, *text = NULL, *disk_path = NULL; | |
733 | _cleanup_strv_free_erase_ char **passwords = NULL; | |
de010b0b | 734 | char *id; |
08669709 | 735 | int r = 0; |
2cbca51a | 736 | AskPasswordFlags flags = arg_ask_password_flags | ASK_PASSWORD_PUSH_CACHE; |
08669709 LP |
737 | |
738 | assert(vol); | |
739 | assert(src); | |
740 | assert(ret); | |
741 | ||
cd5f57bd LB |
742 | if (arg_headless) |
743 | return log_error_errno(SYNTHETIC_ERRNO(ENOPKG), "Password querying disabled via 'headless' option."); | |
744 | ||
08669709 LP |
745 | friendly = friendly_disk_name(src, vol); |
746 | if (!friendly) | |
e51b9486 HH |
747 | return log_oom(); |
748 | ||
b6c0bd11 | 749 | if (asprintf(&text, "Please enter %s for disk %s:", passphrase_type_to_string(passphrase_type), friendly) < 0) |
08669709 | 750 | return log_oom(); |
e51b9486 | 751 | |
08669709 LP |
752 | disk_path = cescape(src); |
753 | if (!disk_path) | |
e7d90b71 JJ |
754 | return log_oom(); |
755 | ||
ea7e7c1e | 756 | id = strjoina("cryptsetup:", disk_path); |
9fa1de96 | 757 | |
8806bb4b | 758 | r = ask_password_auto(text, "drive-harddisk", id, "cryptsetup", "cryptsetup.passphrase", until, |
1fa94a31 | 759 | flags | (accept_cached*ASK_PASSWORD_ACCEPT_CACHED), |
ab84f5b9 | 760 | &passwords); |
23bbb0de MS |
761 | if (r < 0) |
762 | return log_error_errno(r, "Failed to query password: %m"); | |
e7d90b71 | 763 | |
f75cac37 | 764 | if (arg_verify) { |
ab84f5b9 ZJS |
765 | _cleanup_strv_free_erase_ char **passwords2 = NULL; |
766 | ||
1602b008 | 767 | assert(strv_length(passwords) == 1); |
e7d90b71 | 768 | |
b6c0bd11 | 769 | if (asprintf(&text, "Please enter %s for disk %s (verification):", passphrase_type_to_string(passphrase_type), friendly) < 0) |
ab84f5b9 | 770 | return log_oom(); |
e7d90b71 | 771 | |
ea7e7c1e | 772 | id = strjoina("cryptsetup-verification:", disk_path); |
9fa1de96 | 773 | |
1fa94a31 | 774 | r = ask_password_auto(text, "drive-harddisk", id, "cryptsetup", "cryptsetup.passphrase", until, flags, &passwords2); |
ab84f5b9 ZJS |
775 | if (r < 0) |
776 | return log_error_errno(r, "Failed to query verification password: %m"); | |
e7d90b71 JJ |
777 | |
778 | assert(strv_length(passwords2) == 1); | |
779 | ||
8bc6ade7 LP |
780 | if (!streq(passwords[0], passwords2[0])) |
781 | return log_warning_errno(SYNTHETIC_ERRNO(EAGAIN), | |
782 | "Passwords did not match, retrying."); | |
e7d90b71 JJ |
783 | } |
784 | ||
1602b008 | 785 | strv_uniq(passwords); |
e7d90b71 | 786 | |
1602b008 | 787 | STRV_FOREACH(p, passwords) { |
e7d90b71 JJ |
788 | char *c; |
789 | ||
f75cac37 | 790 | if (strlen(*p)+1 >= arg_key_size) |
e7d90b71 JJ |
791 | continue; |
792 | ||
793 | /* Pad password if necessary */ | |
1602b008 | 794 | c = new(char, arg_key_size); |
ab84f5b9 ZJS |
795 | if (!c) |
796 | return log_oom(); | |
e7d90b71 | 797 | |
f75cac37 | 798 | strncpy(c, *p, arg_key_size); |
d3ad474f LP |
799 | erase_and_free(*p); |
800 | *p = TAKE_PTR(c); | |
e7d90b71 JJ |
801 | } |
802 | ||
ae2a15bc | 803 | *ret = TAKE_PTR(passwords); |
1602b008 | 804 | |
ab84f5b9 | 805 | return 0; |
e7d90b71 JJ |
806 | } |
807 | ||
94c0c85e LP |
808 | static int measure_volume_key( |
809 | struct crypt_device *cd, | |
810 | const char *name, | |
811 | const void *volume_key, | |
812 | size_t volume_key_size) { | |
813 | ||
814 | int r; | |
815 | ||
816 | assert(cd); | |
817 | assert(name); | |
818 | assert(volume_key); | |
819 | assert(volume_key_size > 0); | |
820 | ||
821 | if (arg_tpm2_measure_pcr == UINT_MAX) { | |
822 | log_debug("Not measuring volume key, deactivated."); | |
823 | return 0; | |
824 | } | |
825 | ||
2f6c52b9 | 826 | r = efi_stub_measured(LOG_WARNING); |
6c51b49c | 827 | if (r < 0) |
2f6c52b9 | 828 | return r; |
6c51b49c LP |
829 | if (r == 0) { |
830 | log_debug("Kernel stub did not measure kernel image into the expected PCR, skipping userspace measurement, too."); | |
831 | return 0; | |
832 | } | |
833 | ||
94c0c85e | 834 | #if HAVE_TPM2 |
1dc8f518 | 835 | _cleanup_(tpm2_context_unrefp) Tpm2Context *c = NULL; |
68d084ce | 836 | r = tpm2_context_new(arg_tpm2_device, &c); |
94c0c85e LP |
837 | if (r < 0) |
838 | return r; | |
839 | ||
840 | _cleanup_strv_free_ char **l = NULL; | |
841 | if (strv_isempty(arg_tpm2_measure_banks)) { | |
23e9ccc2 | 842 | r = tpm2_get_good_pcr_banks_strv(c, UINT32_C(1) << arg_tpm2_measure_pcr, &l); |
94c0c85e LP |
843 | if (r < 0) |
844 | return r; | |
845 | } | |
846 | ||
847 | _cleanup_free_ char *joined = strv_join(l ?: arg_tpm2_measure_banks, ", "); | |
848 | if (!joined) | |
849 | return log_oom(); | |
850 | ||
851 | /* Note: we don't directly measure the volume key, it might be a security problem to send an | |
852 | * unprotected direct hash of the secret volume key over the wire to the TPM. Hence let's instead | |
853 | * send a HMAC signature instead. */ | |
854 | ||
855 | _cleanup_free_ char *escaped = NULL; | |
856 | escaped = xescape(name, ":"); /* avoid ambiguity around ":" once we join things below */ | |
857 | if (!escaped) | |
858 | return log_oom(); | |
859 | ||
860 | _cleanup_free_ char *s = NULL; | |
861 | s = strjoin("cryptsetup:", escaped, ":", strempty(crypt_get_uuid(cd))); | |
862 | if (!s) | |
863 | return log_oom(); | |
864 | ||
cb19bdae | 865 | 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); |
94c0c85e LP |
866 | if (r < 0) |
867 | return r; | |
868 | ||
869 | log_struct(LOG_INFO, | |
870 | "MESSAGE_ID=" SD_MESSAGE_TPM_PCR_EXTEND_STR, | |
871 | LOG_MESSAGE("Successfully extended PCR index %u with '%s' and volume key (banks %s).", arg_tpm2_measure_pcr, s, joined), | |
872 | "MEASURING=%s", s, | |
873 | "PCR=%u", arg_tpm2_measure_pcr, | |
874 | "BANKS=%s", joined); | |
875 | ||
876 | return 0; | |
877 | #else | |
878 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "TPM2 support disabled, not measuring."); | |
879 | #endif | |
880 | } | |
881 | ||
882 | static int measured_crypt_activate_by_volume_key( | |
883 | struct crypt_device *cd, | |
884 | const char *name, | |
885 | const void *volume_key, | |
886 | size_t volume_key_size, | |
887 | uint32_t flags) { | |
888 | ||
889 | int r; | |
890 | ||
891 | assert(cd); | |
892 | assert(name); | |
893 | ||
894 | /* A wrapper around crypt_activate_by_volume_key() which also measures to a PCR if that's requested. */ | |
895 | ||
896 | r = crypt_activate_by_volume_key(cd, name, volume_key, volume_key_size, flags); | |
897 | if (r < 0) | |
898 | return r; | |
899 | ||
900 | if (volume_key_size == 0) { | |
901 | log_debug("Not measuring volume key, none specified."); | |
902 | return r; | |
903 | } | |
904 | ||
905 | (void) measure_volume_key(cd, name, volume_key, volume_key_size); /* OK if fails */ | |
906 | return r; | |
907 | } | |
908 | ||
909 | static int measured_crypt_activate_by_passphrase( | |
910 | struct crypt_device *cd, | |
911 | const char *name, | |
912 | int keyslot, | |
913 | const char *passphrase, | |
914 | size_t passphrase_size, | |
915 | uint32_t flags) { | |
916 | ||
917 | _cleanup_(erase_and_freep) void *vk = NULL; | |
918 | size_t vks; | |
919 | int r; | |
920 | ||
921 | assert(cd); | |
922 | ||
923 | /* A wrapper around crypt_activate_by_passphrase() which also measures to a PCR if that's | |
924 | * requested. Note that we need the volume key for the measurement, and | |
925 | * crypt_activate_by_passphrase() doesn't give us access to this. Hence, we operate indirectly, and | |
926 | * retrieve the volume key first, and then activate through that. */ | |
927 | ||
928 | if (arg_tpm2_measure_pcr == UINT_MAX) { | |
929 | log_debug("Not measuring volume key, deactivated."); | |
930 | goto shortcut; | |
931 | } | |
932 | ||
933 | r = crypt_get_volume_key_size(cd); | |
934 | if (r < 0) | |
935 | return r; | |
936 | if (r == 0) { | |
937 | log_debug("Not measuring volume key, none defined."); | |
938 | goto shortcut; | |
939 | } | |
940 | ||
941 | vk = malloc(vks = r); | |
942 | if (!vk) | |
943 | return -ENOMEM; | |
944 | ||
945 | r = crypt_volume_key_get(cd, keyslot, vk, &vks, passphrase, passphrase_size); | |
946 | if (r < 0) | |
947 | return r; | |
948 | ||
949 | return measured_crypt_activate_by_volume_key(cd, name, vk, vks, flags); | |
950 | ||
951 | shortcut: | |
952 | return crypt_activate_by_passphrase(cd, name, keyslot, passphrase, passphrase_size, flags); | |
953 | } | |
954 | ||
1602b008 LP |
955 | static int attach_tcrypt( |
956 | struct crypt_device *cd, | |
957 | const char *name, | |
958 | const char *key_file, | |
7407f689 LP |
959 | const void *key_data, |
960 | size_t key_data_size, | |
1602b008 LP |
961 | char **passwords, |
962 | uint32_t flags) { | |
963 | ||
8cf3ca80 | 964 | int r = 0; |
d3ad474f | 965 | _cleanup_(erase_and_freep) char *passphrase = NULL; |
8cf3ca80 JJ |
966 | struct crypt_params_tcrypt params = { |
967 | .flags = CRYPT_TCRYPT_LEGACY_MODES, | |
f75cac37 LP |
968 | .keyfiles = (const char **)arg_tcrypt_keyfiles, |
969 | .keyfiles_count = strv_length(arg_tcrypt_keyfiles) | |
8cf3ca80 JJ |
970 | }; |
971 | ||
972 | assert(cd); | |
973 | assert(name); | |
7407f689 | 974 | assert(key_file || key_data || !strv_isempty(passwords)); |
8cf3ca80 | 975 | |
18843ecc | 976 | if (arg_pkcs11_uri || arg_pkcs11_uri_auto || arg_fido2_device || arg_fido2_device_auto || arg_tpm2_device || arg_tpm2_device_auto) |
e514aa1e FS |
977 | /* Ask for a regular password */ |
978 | return log_error_errno(SYNTHETIC_ERRNO(EAGAIN), | |
18843ecc | 979 | "Sorry, but tcrypt devices are currently not supported in conjunction with pkcs11/fido2/tpm2 support."); |
08669709 | 980 | |
f75cac37 | 981 | if (arg_tcrypt_hidden) |
8cf3ca80 JJ |
982 | params.flags |= CRYPT_TCRYPT_HIDDEN_HEADER; |
983 | ||
f75cac37 | 984 | if (arg_tcrypt_system) |
8cf3ca80 JJ |
985 | params.flags |= CRYPT_TCRYPT_SYSTEM_HEADER; |
986 | ||
52028838 GH |
987 | if (arg_tcrypt_veracrypt) |
988 | params.flags |= CRYPT_TCRYPT_VERA_MODES; | |
6747d9a1 | 989 | |
70390240 KZ |
990 | if (arg_tcrypt_veracrypt && arg_tcrypt_veracrypt_pim != 0) |
991 | params.veracrypt_pim = arg_tcrypt_veracrypt_pim; | |
52028838 | 992 | |
7407f689 LP |
993 | if (key_data) { |
994 | params.passphrase = key_data; | |
995 | params.passphrase_size = key_data_size; | |
996 | } else { | |
997 | if (key_file) { | |
998 | r = read_one_line_file(key_file, &passphrase); | |
999 | if (r < 0) { | |
1000 | log_error_errno(r, "Failed to read password file '%s': %m", key_file); | |
1001 | return -EAGAIN; /* log with the actual error, but return EAGAIN */ | |
1002 | } | |
8cf3ca80 | 1003 | |
7407f689 LP |
1004 | params.passphrase = passphrase; |
1005 | } else | |
1006 | params.passphrase = passwords[0]; | |
1007 | ||
1008 | params.passphrase_size = strlen(params.passphrase); | |
1009 | } | |
8cf3ca80 JJ |
1010 | |
1011 | r = crypt_load(cd, CRYPT_TCRYPT, ¶ms); | |
1012 | if (r < 0) { | |
7407f689 | 1013 | if (r == -EPERM) { |
cb6c9283 | 1014 | if (key_data) |
7407f689 | 1015 | log_error_errno(r, "Failed to activate using discovered key. (Key not correct?)"); |
7407f689 | 1016 | |
cb6c9283 | 1017 | if (key_file) |
7407f689 | 1018 | log_error_errno(r, "Failed to activate using password file '%s'. (Key data not correct?)", key_file); |
cb6c9283 LP |
1019 | |
1020 | return -EAGAIN; /* log the actual error, but return EAGAIN */ | |
6f177c7d LP |
1021 | } |
1022 | ||
1023 | return log_error_errno(r, "Failed to load tcrypt superblock on device %s: %m", crypt_get_device_name(cd)); | |
8cf3ca80 JJ |
1024 | } |
1025 | ||
94c0c85e | 1026 | r = measured_crypt_activate_by_volume_key(cd, name, NULL, 0, flags); |
6f177c7d LP |
1027 | if (r < 0) |
1028 | return log_error_errno(r, "Failed to activate tcrypt device %s: %m", crypt_get_device_name(cd)); | |
1029 | ||
1030 | return 0; | |
8cf3ca80 JJ |
1031 | } |
1032 | ||
e2c2f868 LP |
1033 | static char *make_bindname(const char *volume) { |
1034 | char *s; | |
1035 | ||
1036 | if (asprintf(&s, "@%" PRIx64"/cryptsetup/%s", random_u64(), volume) < 0) | |
1037 | return NULL; | |
1038 | ||
1039 | return s; | |
1040 | } | |
1041 | ||
5cbe70af LP |
1042 | static int make_security_device_monitor( |
1043 | sd_event **ret_event, | |
1044 | sd_device_monitor **ret_monitor) { | |
8414cd48 | 1045 | _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL; |
5cbe70af | 1046 | _cleanup_(sd_event_unrefp) sd_event *event = NULL; |
8414cd48 LP |
1047 | int r; |
1048 | ||
5cbe70af LP |
1049 | assert(ret_event); |
1050 | assert(ret_monitor); | |
1051 | ||
1052 | /* Waits for a device with "security-device" tag to show up in udev */ | |
1053 | ||
1054 | r = sd_event_default(&event); | |
1055 | if (r < 0) | |
1056 | return log_error_errno(r, "Failed to allocate event loop: %m"); | |
1057 | ||
1058 | r = sd_event_add_time_relative(event, NULL, CLOCK_MONOTONIC, arg_token_timeout_usec, USEC_PER_SEC, NULL, INT_TO_PTR(-ETIMEDOUT)); | |
1059 | if (r < 0) | |
1060 | return log_error_errno(r, "Failed to install timeout event source: %m"); | |
8414cd48 LP |
1061 | |
1062 | r = sd_device_monitor_new(&monitor); | |
1063 | if (r < 0) | |
1064 | return log_error_errno(r, "Failed to allocate device monitor: %m"); | |
1065 | ||
17bf3c55 YW |
1066 | (void) sd_device_monitor_set_description(monitor, "security-device"); |
1067 | ||
8414cd48 LP |
1068 | r = sd_device_monitor_filter_add_match_tag(monitor, "security-device"); |
1069 | if (r < 0) | |
1070 | return log_error_errno(r, "Failed to configure device monitor: %m"); | |
1071 | ||
1072 | r = sd_device_monitor_attach_event(monitor, event); | |
1073 | if (r < 0) | |
1074 | return log_error_errno(r, "Failed to attach device monitor: %m"); | |
1075 | ||
1076 | r = sd_device_monitor_start(monitor, NULL, NULL); | |
1077 | if (r < 0) | |
1078 | return log_error_errno(r, "Failed to start device monitor: %m"); | |
1079 | ||
5cbe70af LP |
1080 | *ret_event = TAKE_PTR(event); |
1081 | *ret_monitor = TAKE_PTR(monitor); | |
8414cd48 LP |
1082 | return 0; |
1083 | } | |
1084 | ||
5cbe70af LP |
1085 | static int run_security_device_monitor( |
1086 | sd_event *event, | |
1087 | sd_device_monitor *monitor) { | |
1088 | bool processed = false; | |
1089 | int r; | |
1090 | ||
1091 | assert(event); | |
1092 | assert(monitor); | |
1093 | ||
1094 | /* Runs the event loop for the device monitor until either something happens, or the time-out is | |
1095 | * hit. */ | |
1096 | ||
1097 | for (;;) { | |
1098 | int x; | |
1099 | ||
1100 | r = sd_event_get_exit_code(event, &x); | |
1101 | if (r < 0) { | |
1102 | if (r != -ENODATA) | |
1103 | return log_error_errno(r, "Failed to query exit code from event loop: %m"); | |
1104 | ||
1105 | /* On ENODATA we aren't told to exit yet. */ | |
1106 | } else { | |
1107 | assert(x == -ETIMEDOUT); | |
1108 | return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN), | |
1109 | "Timed out waiting for security device, aborting security device based authentication attempt."); | |
1110 | } | |
1111 | ||
1112 | /* Wait for one event, and then eat all subsequent events until there are no further ones */ | |
1113 | r = sd_event_run(event, processed ? 0 : UINT64_MAX); | |
1114 | if (r < 0) | |
1115 | return log_error_errno(r, "Failed to run event loop: %m"); | |
1116 | if (r == 0) /* no events queued anymore */ | |
1117 | return 0; | |
1118 | ||
1119 | processed = true; | |
1120 | } | |
1121 | } | |
1122 | ||
351716e1 | 1123 | static bool libcryptsetup_plugins_support(void) { |
94c0c85e LP |
1124 | |
1125 | #if HAVE_TPM2 | |
1126 | /* Currently, there's no way for us to query the volume key when plugins are used. Hence don't use | |
1127 | * plugins, if measurement has been requested. */ | |
1128 | if (arg_tpm2_measure_pcr != UINT_MAX) | |
1129 | return false; | |
1130 | #endif | |
1131 | ||
351716e1 | 1132 | #if HAVE_LIBCRYPTSETUP_PLUGINS |
64c590fb LP |
1133 | int r; |
1134 | ||
1135 | /* Permit a way to disable libcryptsetup token module support, for debugging purposes. */ | |
1136 | r = getenv_bool("SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE"); | |
1137 | if (r < 0 && r != -ENXIO) | |
1138 | log_debug_errno(r, "Failed to parse $SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE env var: %m"); | |
1139 | if (r == 0) | |
1140 | return false; | |
1141 | ||
1142 | return crypt_token_external_path(); | |
351716e1 OK |
1143 | #else |
1144 | return false; | |
1145 | #endif | |
1146 | } | |
1147 | ||
1148 | #if HAVE_LIBCRYPTSETUP_PLUGINS | |
1149 | static int acquire_pins_from_env_variable(char ***ret_pins) { | |
e99ca147 | 1150 | _cleanup_(erase_and_freep) char *envpin = NULL; |
351716e1 | 1151 | _cleanup_strv_free_erase_ char **pins = NULL; |
e99ca147 | 1152 | int r; |
351716e1 OK |
1153 | |
1154 | assert(ret_pins); | |
1155 | ||
e99ca147 LP |
1156 | r = getenv_steal_erase("PIN", &envpin); |
1157 | if (r < 0) | |
1158 | return log_error_errno(r, "Failed to acquire PIN from environment: %m"); | |
1159 | if (r > 0) { | |
1160 | pins = strv_new(envpin); | |
351716e1 OK |
1161 | if (!pins) |
1162 | return log_oom(); | |
351716e1 OK |
1163 | } |
1164 | ||
1165 | *ret_pins = TAKE_PTR(pins); | |
1166 | ||
1167 | return 0; | |
1168 | } | |
1169 | #endif | |
1170 | ||
d1411499 | 1171 | static int crypt_activate_by_token_pin_ask_password( |
351716e1 OK |
1172 | struct crypt_device *cd, |
1173 | const char *name, | |
d1411499 | 1174 | const char *type, |
351716e1 OK |
1175 | usec_t until, |
1176 | bool headless, | |
1177 | void *usrptr, | |
d1411499 JW |
1178 | uint32_t activation_flags, |
1179 | const char *message, | |
1180 | const char *key_name, | |
1181 | const char *credential_name) { | |
351716e1 | 1182 | |
351716e1 | 1183 | #if HAVE_LIBCRYPTSETUP_PLUGINS |
351716e1 | 1184 | AskPasswordFlags flags = ASK_PASSWORD_PUSH_CACHE | ASK_PASSWORD_ACCEPT_CACHED; |
a2236110 | 1185 | _cleanup_strv_free_erase_ char **pins = NULL; |
a2236110 | 1186 | int r; |
351716e1 | 1187 | |
d1411499 | 1188 | r = crypt_activate_by_token_pin(cd, name, type, CRYPT_ANY_TOKEN, NULL, 0, usrptr, activation_flags); |
351716e1 OK |
1189 | if (r > 0) /* returns unlocked keyslot id on success */ |
1190 | r = 0; | |
1191 | if (r != -ENOANO) /* needs pin or pin is wrong */ | |
1192 | return r; | |
1193 | ||
1194 | r = acquire_pins_from_env_variable(&pins); | |
1195 | if (r < 0) | |
1196 | return r; | |
1197 | ||
1198 | STRV_FOREACH(p, pins) { | |
d1411499 | 1199 | r = crypt_activate_by_token_pin(cd, name, type, CRYPT_ANY_TOKEN, *p, strlen(*p), usrptr, activation_flags); |
351716e1 OK |
1200 | if (r > 0) /* returns unlocked keyslot id on success */ |
1201 | r = 0; | |
1202 | if (r != -ENOANO) /* needs pin or pin is wrong */ | |
1203 | return r; | |
1204 | } | |
1205 | ||
1206 | if (headless) | |
1207 | return log_error_errno(SYNTHETIC_ERRNO(ENOPKG), "PIN querying disabled via 'headless' option. Use the '$PIN' environment variable."); | |
1208 | ||
351716e1 OK |
1209 | for (;;) { |
1210 | pins = strv_free_erase(pins); | |
d1411499 | 1211 | r = ask_password_auto(message, "drive-harddisk", NULL, key_name, credential_name, until, flags, &pins); |
351716e1 OK |
1212 | if (r < 0) |
1213 | return r; | |
1214 | ||
1215 | STRV_FOREACH(p, pins) { | |
d1411499 | 1216 | r = crypt_activate_by_token_pin(cd, name, type, CRYPT_ANY_TOKEN, *p, strlen(*p), usrptr, activation_flags); |
351716e1 OK |
1217 | if (r > 0) /* returns unlocked keyslot id on success */ |
1218 | r = 0; | |
1219 | if (r != -ENOANO) /* needs pin or pin is wrong */ | |
1220 | return r; | |
1221 | } | |
92828080 LP |
1222 | |
1223 | flags &= ~ASK_PASSWORD_ACCEPT_CACHED; | |
351716e1 | 1224 | } |
351716e1 | 1225 | return r; |
a2236110 LP |
1226 | #else |
1227 | return -EOPNOTSUPP; | |
1228 | #endif | |
351716e1 OK |
1229 | } |
1230 | ||
d1411499 JW |
1231 | static int attach_luks2_by_fido2_via_plugin( |
1232 | struct crypt_device *cd, | |
1233 | const char *name, | |
1234 | usec_t until, | |
1235 | bool headless, | |
1236 | void *usrptr, | |
1237 | uint32_t activation_flags) { | |
1238 | ||
1239 | return crypt_activate_by_token_pin_ask_password( | |
1240 | cd, | |
1241 | name, | |
1242 | "systemd-fido2", | |
1243 | until, | |
1244 | headless, | |
1245 | usrptr, | |
1246 | activation_flags, | |
1247 | "Please enter security token PIN:", | |
1248 | "fido2-pin", | |
1249 | "cryptsetup.fido2-pin"); | |
1250 | } | |
1251 | ||
2bc5c425 LP |
1252 | static int attach_luks_or_plain_or_bitlk_by_fido2( |
1253 | struct crypt_device *cd, | |
1254 | const char *name, | |
1255 | const char *key_file, | |
1256 | const void *key_data, | |
1257 | size_t key_data_size, | |
1258 | usec_t until, | |
1259 | uint32_t flags, | |
1260 | bool pass_volume_key) { | |
1261 | ||
1262 | _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL; | |
1263 | _cleanup_(erase_and_freep) void *decrypted_key = NULL; | |
1264 | _cleanup_(sd_event_unrefp) sd_event *event = NULL; | |
e6319a10 PC |
1265 | size_t decrypted_key_size, cid_size = 0; |
1266 | _cleanup_free_ char *friendly = NULL; | |
2bc5c425 | 1267 | int keyslot = arg_key_slot, r; |
351716e1 OK |
1268 | const char *rp_id = NULL; |
1269 | const void *cid = NULL; | |
cde2f860 | 1270 | Fido2EnrollFlags required; |
351716e1 | 1271 | bool use_libcryptsetup_plugin = libcryptsetup_plugins_support(); |
2bc5c425 LP |
1272 | |
1273 | assert(cd); | |
1274 | assert(name); | |
1275 | assert(arg_fido2_device || arg_fido2_device_auto); | |
1276 | ||
1277 | if (arg_fido2_cid) { | |
1278 | if (!key_file && !key_data) | |
8205c151 ZJS |
1279 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
1280 | "FIDO2 mode with manual parameters selected, but no keyfile specified, refusing."); | |
2bc5c425 LP |
1281 | |
1282 | rp_id = arg_fido2_rp_id; | |
1283 | cid = arg_fido2_cid; | |
1284 | cid_size = arg_fido2_cid_size; | |
8205c151 | 1285 | |
3cc00ba6 LP |
1286 | /* For now and for compatibility, if the user explicitly configured FIDO2 support and we do |
1287 | * not read FIDO2 metadata off the LUKS2 header, default to the systemd 248 logic, where we | |
1288 | * use PIN + UP when needed, and do not configure UV at all. Eventually, we should make this | |
1289 | * explicitly configurable. */ | |
1290 | required = FIDO2ENROLL_PIN_IF_NEEDED | FIDO2ENROLL_UP_IF_NEEDED | FIDO2ENROLL_UV_OMIT; | |
2bc5c425 LP |
1291 | } |
1292 | ||
1293 | friendly = friendly_disk_name(crypt_get_device_name(cd), name); | |
1294 | if (!friendly) | |
1295 | return log_oom(); | |
1296 | ||
1297 | for (;;) { | |
351716e1 | 1298 | if (use_libcryptsetup_plugin && !arg_fido2_cid) { |
dcbc38c9 | 1299 | r = attach_luks2_by_fido2_via_plugin(cd, name, until, arg_headless, arg_fido2_device, flags); |
351716e1 OK |
1300 | if (IN_SET(r, -ENOTUNIQ, -ENXIO, -ENOENT)) |
1301 | return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN), | |
1302 | "Automatic FIDO2 metadata discovery was not possible because missing or not unique, falling back to traditional unlocking."); | |
1303 | ||
1304 | } else { | |
e6319a10 PC |
1305 | if (cid) |
1306 | r = acquire_fido2_key( | |
1307 | name, | |
1308 | friendly, | |
1309 | arg_fido2_device, | |
1310 | rp_id, | |
1311 | cid, cid_size, | |
1312 | key_file, arg_keyfile_size, arg_keyfile_offset, | |
1313 | key_data, key_data_size, | |
1314 | until, | |
1315 | arg_headless, | |
1316 | required, | |
1317 | &decrypted_key, &decrypted_key_size, | |
1318 | arg_ask_password_flags); | |
1319 | else | |
1320 | r = acquire_fido2_key_auto( | |
1321 | cd, | |
1322 | name, | |
1323 | friendly, | |
1324 | arg_fido2_device, | |
e6319a10 PC |
1325 | until, |
1326 | arg_headless, | |
1327 | &decrypted_key, &decrypted_key_size, | |
1328 | arg_ask_password_flags); | |
351716e1 OK |
1329 | if (r >= 0) |
1330 | break; | |
1331 | } | |
1332 | ||
2bc5c425 LP |
1333 | if (r != -EAGAIN) /* EAGAIN means: token not found */ |
1334 | return r; | |
1335 | ||
1336 | if (!monitor) { | |
1337 | /* We didn't find the token. In this case, watch for it via udev. Let's | |
1338 | * create an event loop and monitor first. */ | |
1339 | ||
1340 | assert(!event); | |
1341 | ||
5cbe70af | 1342 | r = make_security_device_monitor(&event, &monitor); |
2bc5c425 LP |
1343 | if (r < 0) |
1344 | return r; | |
1345 | ||
1346 | log_notice("Security token not present for unlocking volume %s, please plug it in.", friendly); | |
1347 | ||
1348 | /* Let's immediately rescan in case the token appeared in the time we needed | |
1349 | * to create and configure the monitor */ | |
1350 | continue; | |
1351 | } | |
1352 | ||
5cbe70af LP |
1353 | r = run_security_device_monitor(event, monitor); |
1354 | if (r < 0) | |
1355 | return r; | |
2bc5c425 LP |
1356 | |
1357 | log_debug("Got one or more potentially relevant udev events, rescanning FIDO2..."); | |
1358 | } | |
1359 | ||
1360 | if (pass_volume_key) | |
94c0c85e | 1361 | r = measured_crypt_activate_by_volume_key(cd, name, decrypted_key, decrypted_key_size, flags); |
2bc5c425 LP |
1362 | else { |
1363 | _cleanup_(erase_and_freep) char *base64_encoded = NULL; | |
5e476b85 | 1364 | ssize_t base64_encoded_size; |
2bc5c425 LP |
1365 | |
1366 | /* Before using this key as passphrase we base64 encode it, for compat with homed */ | |
1367 | ||
5e476b85 LP |
1368 | base64_encoded_size = base64mem(decrypted_key, decrypted_key_size, &base64_encoded); |
1369 | if (base64_encoded_size < 0) | |
2bc5c425 LP |
1370 | return log_oom(); |
1371 | ||
94c0c85e | 1372 | r = measured_crypt_activate_by_passphrase(cd, name, keyslot, base64_encoded, base64_encoded_size, flags); |
2bc5c425 LP |
1373 | } |
1374 | if (r == -EPERM) { | |
1375 | log_error_errno(r, "Failed to activate with FIDO2 decrypted key. (Key incorrect?)"); | |
1376 | return -EAGAIN; /* log actual error, but return EAGAIN */ | |
1377 | } | |
1378 | if (r < 0) | |
1379 | return log_error_errno(r, "Failed to activate with FIDO2 acquired key: %m"); | |
1380 | ||
1381 | return 0; | |
1382 | } | |
1383 | ||
dcbc38c9 | 1384 | static int attach_luks2_by_pkcs11_via_plugin( |
8186022c OK |
1385 | struct crypt_device *cd, |
1386 | const char *name, | |
1387 | const char *friendly_name, | |
1388 | usec_t until, | |
1389 | bool headless, | |
1390 | uint32_t flags) { | |
1391 | ||
8186022c | 1392 | #if HAVE_LIBCRYPTSETUP_PLUGINS |
a2236110 LP |
1393 | int r; |
1394 | ||
1395 | if (!streq_ptr(crypt_get_type(cd), CRYPT_LUKS2)) | |
8186022c OK |
1396 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Automatic PKCS#11 metadata requires LUKS2 device."); |
1397 | ||
1398 | systemd_pkcs11_plugin_params params = { | |
1399 | .friendly_name = friendly_name, | |
1400 | .until = until, | |
1401 | .headless = headless | |
1402 | }; | |
1403 | ||
1404 | r = crypt_activate_by_token_pin(cd, name, "systemd-pkcs11", CRYPT_ANY_TOKEN, NULL, 0, ¶ms, flags); | |
1405 | if (r > 0) /* returns unlocked keyslot id on success */ | |
1406 | r = 0; | |
a2236110 | 1407 | |
8186022c | 1408 | return r; |
a2236110 LP |
1409 | #else |
1410 | return -EOPNOTSUPP; | |
1411 | #endif | |
8186022c OK |
1412 | } |
1413 | ||
b8c80b56 LP |
1414 | static int attach_luks_or_plain_or_bitlk_by_pkcs11( |
1415 | struct crypt_device *cd, | |
1416 | const char *name, | |
1417 | const char *key_file, | |
1418 | const void *key_data, | |
1419 | size_t key_data_size, | |
1420 | usec_t until, | |
1421 | uint32_t flags, | |
1422 | bool pass_volume_key) { | |
1423 | ||
1424 | _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL; | |
1425 | _cleanup_free_ char *friendly = NULL, *discovered_uri = NULL; | |
1426 | size_t decrypted_key_size = 0, discovered_key_size = 0; | |
1427 | _cleanup_(erase_and_freep) void *decrypted_key = NULL; | |
1428 | _cleanup_(sd_event_unrefp) sd_event *event = NULL; | |
1429 | _cleanup_free_ void *discovered_key = NULL; | |
1430 | int keyslot = arg_key_slot, r; | |
8186022c OK |
1431 | const char *uri = NULL; |
1432 | bool use_libcryptsetup_plugin = libcryptsetup_plugins_support(); | |
b8c80b56 LP |
1433 | |
1434 | assert(cd); | |
1435 | assert(name); | |
1436 | assert(arg_pkcs11_uri || arg_pkcs11_uri_auto); | |
1437 | ||
1438 | if (arg_pkcs11_uri_auto) { | |
8186022c OK |
1439 | if (!use_libcryptsetup_plugin) { |
1440 | r = find_pkcs11_auto_data(cd, &discovered_uri, &discovered_key, &discovered_key_size, &keyslot); | |
1441 | if (IN_SET(r, -ENOTUNIQ, -ENXIO)) | |
1442 | return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN), | |
1443 | "Automatic PKCS#11 metadata discovery was not possible because missing or not unique, falling back to traditional unlocking."); | |
1444 | if (r < 0) | |
1445 | return r; | |
b8c80b56 | 1446 | |
8186022c OK |
1447 | uri = discovered_uri; |
1448 | key_data = discovered_key; | |
1449 | key_data_size = discovered_key_size; | |
1450 | } | |
b8c80b56 LP |
1451 | } else { |
1452 | uri = arg_pkcs11_uri; | |
1453 | ||
1454 | if (!key_file && !key_data) | |
1455 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "PKCS#11 mode selected but no key file specified, refusing."); | |
1456 | } | |
1457 | ||
1458 | friendly = friendly_disk_name(crypt_get_device_name(cd), name); | |
1459 | if (!friendly) | |
1460 | return log_oom(); | |
1461 | ||
1462 | for (;;) { | |
8186022c | 1463 | if (use_libcryptsetup_plugin && arg_pkcs11_uri_auto) |
dcbc38c9 | 1464 | r = attach_luks2_by_pkcs11_via_plugin(cd, name, friendly, until, arg_headless, flags); |
8186022c OK |
1465 | else { |
1466 | r = decrypt_pkcs11_key( | |
1467 | name, | |
1468 | friendly, | |
1469 | uri, | |
1470 | key_file, arg_keyfile_size, arg_keyfile_offset, | |
1471 | key_data, key_data_size, | |
1472 | until, | |
1473 | arg_headless, | |
1474 | &decrypted_key, &decrypted_key_size); | |
1475 | if (r >= 0) | |
1476 | break; | |
1477 | } | |
1478 | ||
b8c80b56 LP |
1479 | if (r != -EAGAIN) /* EAGAIN means: token not found */ |
1480 | return r; | |
1481 | ||
1482 | if (!monitor) { | |
1483 | /* We didn't find the token. In this case, watch for it via udev. Let's | |
1484 | * create an event loop and monitor first. */ | |
1485 | ||
1486 | assert(!event); | |
1487 | ||
5cbe70af | 1488 | r = make_security_device_monitor(&event, &monitor); |
b8c80b56 LP |
1489 | if (r < 0) |
1490 | return r; | |
1491 | ||
1492 | log_notice("Security token %s not present for unlocking volume %s, please plug it in.", | |
1493 | uri, friendly); | |
1494 | ||
1495 | /* Let's immediately rescan in case the token appeared in the time we needed | |
1496 | * to create and configure the monitor */ | |
1497 | continue; | |
1498 | } | |
1499 | ||
5cbe70af LP |
1500 | r = run_security_device_monitor(event, monitor); |
1501 | if (r < 0) | |
1502 | return r; | |
b8c80b56 LP |
1503 | |
1504 | log_debug("Got one or more potentially relevant udev events, rescanning PKCS#11..."); | |
1505 | } | |
8186022c | 1506 | assert(decrypted_key); |
b8c80b56 LP |
1507 | |
1508 | if (pass_volume_key) | |
94c0c85e | 1509 | r = measured_crypt_activate_by_volume_key(cd, name, decrypted_key, decrypted_key_size, flags); |
b8c80b56 LP |
1510 | else { |
1511 | _cleanup_(erase_and_freep) char *base64_encoded = NULL; | |
5e476b85 | 1512 | ssize_t base64_encoded_size; |
b8c80b56 LP |
1513 | |
1514 | /* Before using this key as passphrase we base64 encode it. Why? For compatibility | |
1515 | * with homed's PKCS#11 hookup: there we want to use the key we acquired through | |
1516 | * PKCS#11 for other authentication/decryption mechanisms too, and some of them do | |
ad337e55 | 1517 | * not take arbitrary binary blobs, but require NUL-terminated strings — most |
b8c80b56 LP |
1518 | * importantly UNIX password hashes. Hence, for compatibility we want to use a string |
1519 | * without embedded NUL here too, and that's easiest to generate from a binary blob | |
1520 | * via base64 encoding. */ | |
1521 | ||
5e476b85 LP |
1522 | base64_encoded_size = base64mem(decrypted_key, decrypted_key_size, &base64_encoded); |
1523 | if (base64_encoded_size < 0) | |
b8c80b56 LP |
1524 | return log_oom(); |
1525 | ||
94c0c85e | 1526 | r = measured_crypt_activate_by_passphrase(cd, name, keyslot, base64_encoded, base64_encoded_size, flags); |
b8c80b56 LP |
1527 | } |
1528 | if (r == -EPERM) { | |
1529 | log_error_errno(r, "Failed to activate with PKCS#11 decrypted key. (Key incorrect?)"); | |
1530 | return -EAGAIN; /* log actual error, but return EAGAIN */ | |
1531 | } | |
1532 | if (r < 0) | |
1533 | return log_error_errno(r, "Failed to activate with PKCS#11 acquired key: %m"); | |
1534 | ||
1535 | return 0; | |
1536 | } | |
1537 | ||
5cbe70af LP |
1538 | static int make_tpm2_device_monitor( |
1539 | sd_event **ret_event, | |
1540 | sd_device_monitor **ret_monitor) { | |
1541 | ||
18843ecc | 1542 | _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL; |
5cbe70af | 1543 | _cleanup_(sd_event_unrefp) sd_event *event = NULL; |
18843ecc LP |
1544 | int r; |
1545 | ||
5cbe70af LP |
1546 | assert(ret_event); |
1547 | assert(ret_monitor); | |
1548 | ||
1549 | r = sd_event_default(&event); | |
1550 | if (r < 0) | |
1551 | return log_error_errno(r, "Failed to allocate event loop: %m"); | |
1552 | ||
1553 | r = sd_event_add_time_relative(event, NULL, CLOCK_MONOTONIC, arg_token_timeout_usec, USEC_PER_SEC, NULL, INT_TO_PTR(-ETIMEDOUT)); | |
1554 | if (r < 0) | |
1555 | return log_error_errno(r, "Failed to install timeout event source: %m"); | |
18843ecc LP |
1556 | |
1557 | r = sd_device_monitor_new(&monitor); | |
1558 | if (r < 0) | |
1559 | return log_error_errno(r, "Failed to allocate device monitor: %m"); | |
1560 | ||
17bf3c55 YW |
1561 | (void) sd_device_monitor_set_description(monitor, "tpmrm"); |
1562 | ||
18843ecc LP |
1563 | r = sd_device_monitor_filter_add_match_subsystem_devtype(monitor, "tpmrm", NULL); |
1564 | if (r < 0) | |
1565 | return log_error_errno(r, "Failed to configure device monitor: %m"); | |
1566 | ||
1567 | r = sd_device_monitor_attach_event(monitor, event); | |
1568 | if (r < 0) | |
1569 | return log_error_errno(r, "Failed to attach device monitor: %m"); | |
1570 | ||
1571 | r = sd_device_monitor_start(monitor, NULL, NULL); | |
1572 | if (r < 0) | |
1573 | return log_error_errno(r, "Failed to start device monitor: %m"); | |
1574 | ||
5cbe70af LP |
1575 | *ret_event = TAKE_PTR(event); |
1576 | *ret_monitor = TAKE_PTR(monitor); | |
18843ecc LP |
1577 | return 0; |
1578 | } | |
1579 | ||
dcbc38c9 | 1580 | static int attach_luks2_by_tpm2_via_plugin( |
d1ae38d8 OK |
1581 | struct crypt_device *cd, |
1582 | const char *name, | |
35ba2b4f JW |
1583 | usec_t until, |
1584 | bool headless, | |
d1ae38d8 OK |
1585 | uint32_t flags) { |
1586 | ||
1587 | #if HAVE_LIBCRYPTSETUP_PLUGINS | |
d1ae38d8 OK |
1588 | systemd_tpm2_plugin_params params = { |
1589 | .search_pcr_mask = arg_tpm2_pcr_mask, | |
75a9681e LP |
1590 | .device = arg_tpm2_device, |
1591 | .signature_path = arg_tpm2_signature, | |
d1ae38d8 OK |
1592 | }; |
1593 | ||
d1b2e043 | 1594 | if (!libcryptsetup_plugins_support()) |
d1ae38d8 OK |
1595 | return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
1596 | "Libcryptsetup has external plugins support disabled."); | |
1597 | ||
35ba2b4f JW |
1598 | return crypt_activate_by_token_pin_ask_password( |
1599 | cd, | |
1600 | name, | |
1601 | "systemd-tpm2", | |
1602 | until, | |
1603 | headless, | |
1604 | ¶ms, | |
1605 | flags, | |
1606 | "Please enter TPM2 PIN:", | |
1607 | "tpm2-pin", | |
1608 | "cryptsetup.tpm2-pin"); | |
d1ae38d8 OK |
1609 | #else |
1610 | return -EOPNOTSUPP; | |
1611 | #endif | |
1612 | } | |
1613 | ||
18843ecc LP |
1614 | static int attach_luks_or_plain_or_bitlk_by_tpm2( |
1615 | struct crypt_device *cd, | |
1616 | const char *name, | |
1617 | const char *key_file, | |
1618 | const void *key_data, | |
1619 | size_t key_data_size, | |
1620 | usec_t until, | |
1621 | uint32_t flags, | |
1622 | bool pass_volume_key) { | |
1623 | ||
1624 | _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL; | |
1625 | _cleanup_(erase_and_freep) void *decrypted_key = NULL; | |
1626 | _cleanup_(sd_event_unrefp) sd_event *event = NULL; | |
1627 | _cleanup_free_ char *friendly = NULL; | |
1628 | int keyslot = arg_key_slot, r; | |
67e9c83b | 1629 | size_t decrypted_key_size; |
18843ecc LP |
1630 | |
1631 | assert(cd); | |
1632 | assert(name); | |
1633 | assert(arg_tpm2_device || arg_tpm2_device_auto); | |
1634 | ||
1635 | friendly = friendly_disk_name(crypt_get_device_name(cd), name); | |
1636 | if (!friendly) | |
1637 | return log_oom(); | |
1638 | ||
1639 | for (;;) { | |
18843ecc LP |
1640 | if (key_file || key_data) { |
1641 | /* If key data is specified, use that */ | |
1642 | ||
1643 | r = acquire_tpm2_key( | |
1644 | name, | |
1645 | arg_tpm2_device, | |
1646 | arg_tpm2_pcr_mask == UINT32_MAX ? TPM2_PCR_MASK_DEFAULT : arg_tpm2_pcr_mask, | |
07697bfe | 1647 | UINT16_MAX, |
dc63b2c9 LP |
1648 | /* pubkey= */ NULL, /* pubkey_size= */ 0, |
1649 | /* pubkey_pcr_mask= */ 0, | |
1650 | /* signature_path= */ NULL, | |
1651 | /* primary_alg= */ 0, | |
18843ecc LP |
1652 | key_file, arg_keyfile_size, arg_keyfile_offset, |
1653 | key_data, key_data_size, | |
dc63b2c9 | 1654 | /* policy_hash= */ NULL, /* policy_hash_size= */ 0, /* we don't know the policy hash */ |
aae6eb96 | 1655 | /* salt= */ NULL, /* salt_size= */ 0, |
acbb504e | 1656 | /* srk_buf= */ NULL, /* srk_buf_size= */ 0, |
3fab44b2 | 1657 | arg_tpm2_pin ? TPM2_FLAGS_USE_PIN : 0, |
bea344a1 GG |
1658 | until, |
1659 | arg_headless, | |
1660 | arg_ask_password_flags, | |
18843ecc LP |
1661 | &decrypted_key, &decrypted_key_size); |
1662 | if (r >= 0) | |
1663 | break; | |
bea344a1 GG |
1664 | if (IN_SET(r, -EACCES, -ENOLCK)) |
1665 | return log_error_errno(SYNTHETIC_ERRNO(EAGAIN), "TPM2 PIN unlock failed, falling back to traditional unlocking."); | |
cad10544 LP |
1666 | if (ERRNO_IS_NOT_SUPPORTED(r)) /* TPM2 support not compiled in? */ |
1667 | return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN), "TPM2 support not available, falling back to traditional unlocking."); | |
49be0383 AAF |
1668 | /* EAGAIN means: no tpm2 chip found */ |
1669 | if (r != -EAGAIN) { | |
1670 | log_notice_errno(r, "TPM2 operation failed, falling back to traditional unlocking: %m"); | |
1671 | return -EAGAIN; /* Mangle error code: let's make any form of TPM2 failure non-fatal. */ | |
1672 | } | |
18843ecc | 1673 | } else { |
35ba2b4f JW |
1674 | r = attach_luks2_by_tpm2_via_plugin(cd, name, until, arg_headless, flags); |
1675 | if (r >= 0) | |
1676 | return 0; | |
d1ae38d8 OK |
1677 | /* EAGAIN means: no tpm2 chip found |
1678 | * EOPNOTSUPP means: no libcryptsetup plugins support */ | |
1679 | if (r == -ENXIO) | |
2e4aae98 LP |
1680 | return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN), |
1681 | "No TPM2 metadata matching the current system state found in LUKS2 header, falling back to traditional unlocking."); | |
d1ae38d8 OK |
1682 | if (r == -ENOENT) |
1683 | return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN), | |
1684 | "No TPM2 metadata enrolled in LUKS2 header or TPM2 support not available, falling back to traditional unlocking."); | |
49be0383 AAF |
1685 | if (!IN_SET(r, -EOPNOTSUPP, -EAGAIN)) { |
1686 | log_notice_errno(r, "TPM2 operation failed, falling back to traditional unlocking: %m"); | |
1687 | return -EAGAIN; /* Mangle error code: let's make any form of TPM2 failure non-fatal. */ | |
1688 | } | |
d1ae38d8 OK |
1689 | } |
1690 | ||
dcbc38c9 | 1691 | if (r == -EOPNOTSUPP) { /* Plugin not available, let's process TPM2 stuff right here instead */ |
18843ecc LP |
1692 | _cleanup_free_ void *blob = NULL, *policy_hash = NULL; |
1693 | size_t blob_size, policy_hash_size; | |
1694 | bool found_some = false; | |
1695 | int token = 0; /* first token to look at */ | |
1696 | ||
1697 | /* If no key data is specified, look for it in the header. In order to support | |
1698 | * software upgrades we'll iterate through all suitable tokens, maybe one of them | |
1699 | * works. */ | |
1700 | ||
1701 | for (;;) { | |
acbb504e WR |
1702 | _cleanup_free_ void *pubkey = NULL, *salt = NULL, *srk_buf = NULL; |
1703 | size_t pubkey_size = 0, salt_size = 0, srk_buf_size = 0; | |
dc63b2c9 | 1704 | uint32_t hash_pcr_mask, pubkey_pcr_mask; |
2b92a672 | 1705 | uint16_t pcr_bank, primary_alg; |
bea344a1 | 1706 | TPM2Flags tpm2_flags; |
18843ecc LP |
1707 | |
1708 | r = find_tpm2_auto_data( | |
1709 | cd, | |
1710 | arg_tpm2_pcr_mask, /* if != UINT32_MAX we'll only look for tokens with this PCR mask */ | |
1711 | token, /* search for the token with this index, or any later index than this */ | |
dc63b2c9 | 1712 | &hash_pcr_mask, |
07697bfe | 1713 | &pcr_bank, |
dc63b2c9 LP |
1714 | &pubkey, &pubkey_size, |
1715 | &pubkey_pcr_mask, | |
2b92a672 | 1716 | &primary_alg, |
18843ecc LP |
1717 | &blob, &blob_size, |
1718 | &policy_hash, &policy_hash_size, | |
aae6eb96 | 1719 | &salt, &salt_size, |
acbb504e | 1720 | &srk_buf, &srk_buf_size, |
fdf6c27c | 1721 | &tpm2_flags, |
18843ecc | 1722 | &keyslot, |
fdf6c27c | 1723 | &token); |
c26f7dd9 | 1724 | if (r == -ENXIO) |
7802194a | 1725 | /* No further TPM2 tokens found in the LUKS2 header. */ |
2e4aae98 LP |
1726 | return log_full_errno(found_some ? LOG_NOTICE : LOG_DEBUG, |
1727 | SYNTHETIC_ERRNO(EAGAIN), | |
1728 | found_some | |
1729 | ? "No TPM2 metadata matching the current system state found in LUKS2 header, falling back to traditional unlocking." | |
1730 | : "No TPM2 metadata enrolled in LUKS2 header, falling back to traditional unlocking."); | |
bb44fd07 ZJS |
1731 | if (ERRNO_IS_NEG_NOT_SUPPORTED(r)) |
1732 | /* TPM2 support not compiled in? */ | |
1733 | return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN), | |
1734 | "TPM2 support not available, falling back to traditional unlocking."); | |
1735 | if (r < 0) | |
18843ecc LP |
1736 | return r; |
1737 | ||
1738 | found_some = true; | |
1739 | ||
1740 | r = acquire_tpm2_key( | |
1741 | name, | |
1742 | arg_tpm2_device, | |
dc63b2c9 | 1743 | hash_pcr_mask, |
07697bfe | 1744 | pcr_bank, |
dc63b2c9 LP |
1745 | pubkey, pubkey_size, |
1746 | pubkey_pcr_mask, | |
1747 | arg_tpm2_signature, | |
2b92a672 | 1748 | primary_alg, |
dc63b2c9 | 1749 | /* key_file= */ NULL, /* key_file_size= */ 0, /* key_file_offset= */ 0, /* no key file */ |
18843ecc LP |
1750 | blob, blob_size, |
1751 | policy_hash, policy_hash_size, | |
aae6eb96 | 1752 | salt, salt_size, |
acbb504e | 1753 | srk_buf, srk_buf_size, |
bea344a1 GG |
1754 | tpm2_flags, |
1755 | until, | |
1756 | arg_headless, | |
1757 | arg_ask_password_flags, | |
18843ecc | 1758 | &decrypted_key, &decrypted_key_size); |
bea344a1 | 1759 | if (IN_SET(r, -EACCES, -ENOLCK)) |
2e4aae98 | 1760 | return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN), "TPM2 PIN unlock failed, falling back to traditional unlocking."); |
18843ecc LP |
1761 | if (r != -EPERM) |
1762 | break; | |
1763 | ||
1764 | token++; /* try a different token next time */ | |
1765 | } | |
1766 | ||
1767 | if (r >= 0) | |
1768 | break; | |
49be0383 AAF |
1769 | /* EAGAIN means: no tpm2 chip found */ |
1770 | if (r != -EAGAIN) { | |
1771 | log_notice_errno(r, "TPM2 operation failed, falling back to traditional unlocking: %m"); | |
1772 | return -EAGAIN; /* Mangle error code: let's make any form of TPM2 failure non-fatal. */ | |
1773 | } | |
18843ecc LP |
1774 | } |
1775 | ||
1776 | if (!monitor) { | |
1777 | /* We didn't find the TPM2 device. In this case, watch for it via udev. Let's create | |
1778 | * an event loop and monitor first. */ | |
1779 | ||
1780 | assert(!event); | |
1781 | ||
71b25dea LP |
1782 | if (is_efi_boot() && !efi_has_tpm2()) |
1783 | return log_notice_errno(SYNTHETIC_ERRNO(EAGAIN), | |
88a65c93 | 1784 | "No TPM2 hardware discovered and EFI firmware does not see it either, falling back to traditional unlocking."); |
71b25dea | 1785 | |
5cbe70af | 1786 | r = make_tpm2_device_monitor(&event, &monitor); |
18843ecc LP |
1787 | if (r < 0) |
1788 | return r; | |
1789 | ||
1790 | log_info("TPM2 device not present for unlocking %s, waiting for it to become available.", friendly); | |
1791 | ||
1792 | /* Let's immediately rescan in case the device appeared in the time we needed | |
1793 | * to create and configure the monitor */ | |
1794 | continue; | |
1795 | } | |
1796 | ||
5cbe70af LP |
1797 | r = run_security_device_monitor(event, monitor); |
1798 | if (r < 0) | |
1799 | return r; | |
18843ecc LP |
1800 | |
1801 | log_debug("Got one or more potentially relevant udev events, rescanning for TPM2..."); | |
1802 | } | |
26fbc5aa | 1803 | assert(decrypted_key); |
18843ecc LP |
1804 | |
1805 | if (pass_volume_key) | |
94c0c85e | 1806 | r = measured_crypt_activate_by_volume_key(cd, name, decrypted_key, decrypted_key_size, flags); |
18843ecc LP |
1807 | else { |
1808 | _cleanup_(erase_and_freep) char *base64_encoded = NULL; | |
5e476b85 | 1809 | ssize_t base64_encoded_size; |
18843ecc LP |
1810 | |
1811 | /* Before using this key as passphrase we base64 encode it, for compat with homed */ | |
1812 | ||
5e476b85 LP |
1813 | base64_encoded_size = base64mem(decrypted_key, decrypted_key_size, &base64_encoded); |
1814 | if (base64_encoded_size < 0) | |
18843ecc LP |
1815 | return log_oom(); |
1816 | ||
94c0c85e | 1817 | r = measured_crypt_activate_by_passphrase(cd, name, keyslot, base64_encoded, base64_encoded_size, flags); |
18843ecc LP |
1818 | } |
1819 | if (r == -EPERM) { | |
1820 | log_error_errno(r, "Failed to activate with TPM2 decrypted key. (Key incorrect?)"); | |
1821 | return -EAGAIN; /* log actual error, but return EAGAIN */ | |
1822 | } | |
1823 | if (r < 0) | |
1824 | return log_error_errno(r, "Failed to activate with TPM2 acquired key: %m"); | |
1825 | ||
1826 | return 0; | |
1827 | } | |
1828 | ||
b8c80b56 LP |
1829 | static int attach_luks_or_plain_or_bitlk_by_key_data( |
1830 | struct crypt_device *cd, | |
1831 | const char *name, | |
1832 | const void *key_data, | |
1833 | size_t key_data_size, | |
1834 | uint32_t flags, | |
1835 | bool pass_volume_key) { | |
1836 | ||
1837 | int r; | |
1838 | ||
1839 | assert(cd); | |
1840 | assert(name); | |
1841 | assert(key_data); | |
1842 | ||
1843 | if (pass_volume_key) | |
94c0c85e | 1844 | r = measured_crypt_activate_by_volume_key(cd, name, key_data, key_data_size, flags); |
b8c80b56 | 1845 | else |
94c0c85e | 1846 | r = measured_crypt_activate_by_passphrase(cd, name, arg_key_slot, key_data, key_data_size, flags); |
b8c80b56 LP |
1847 | if (r == -EPERM) { |
1848 | log_error_errno(r, "Failed to activate. (Key incorrect?)"); | |
1849 | return -EAGAIN; /* Log actual error, but return EAGAIN */ | |
1850 | } | |
1851 | if (r < 0) | |
1852 | return log_error_errno(r, "Failed to activate: %m"); | |
1853 | ||
1854 | return 0; | |
1855 | } | |
1856 | ||
1857 | static int attach_luks_or_plain_or_bitlk_by_key_file( | |
1858 | struct crypt_device *cd, | |
1859 | const char *name, | |
1860 | const char *key_file, | |
1861 | uint32_t flags, | |
1862 | bool pass_volume_key) { | |
1863 | ||
1864 | _cleanup_(erase_and_freep) char *kfdata = NULL; | |
1865 | _cleanup_free_ char *bindname = NULL; | |
1866 | size_t kfsize; | |
1867 | int r; | |
1868 | ||
1869 | assert(cd); | |
1870 | assert(name); | |
1871 | assert(key_file); | |
1872 | ||
1873 | /* If we read the key via AF_UNIX, make this client recognizable */ | |
1874 | bindname = make_bindname(name); | |
1875 | if (!bindname) | |
1876 | return log_oom(); | |
1877 | ||
1878 | r = read_full_file_full( | |
1879 | AT_FDCWD, key_file, | |
1880 | arg_keyfile_offset == 0 ? UINT64_MAX : arg_keyfile_offset, | |
1881 | arg_keyfile_size == 0 ? SIZE_MAX : arg_keyfile_size, | |
1882 | READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET, | |
1883 | bindname, | |
1884 | &kfdata, &kfsize); | |
871f35af LP |
1885 | if (r == -E2BIG) { |
1886 | log_error_errno(r, "Failed to activate, key file '%s' too large.", key_file); | |
1887 | return -EAGAIN; | |
1888 | } | |
b8c80b56 LP |
1889 | if (r == -ENOENT) { |
1890 | log_error_errno(r, "Failed to activate, key file '%s' missing.", key_file); | |
1891 | return -EAGAIN; /* Log actual error, but return EAGAIN */ | |
1892 | } | |
abc50cf6 LP |
1893 | if (r < 0) |
1894 | return log_error_errno(r, "Failed to read key file '%s': %m", key_file); | |
b8c80b56 LP |
1895 | |
1896 | if (pass_volume_key) | |
94c0c85e | 1897 | r = measured_crypt_activate_by_volume_key(cd, name, kfdata, kfsize, flags); |
b8c80b56 | 1898 | else |
94c0c85e | 1899 | r = measured_crypt_activate_by_passphrase(cd, name, arg_key_slot, kfdata, kfsize, flags); |
b8c80b56 LP |
1900 | if (r == -EPERM) { |
1901 | log_error_errno(r, "Failed to activate with key file '%s'. (Key data incorrect?)", key_file); | |
1902 | return -EAGAIN; /* Log actual error, but return EAGAIN */ | |
1903 | } | |
1904 | if (r < 0) | |
1905 | return log_error_errno(r, "Failed to activate with key file '%s': %m", key_file); | |
1906 | ||
1907 | return 0; | |
1908 | } | |
1909 | ||
1910 | static int attach_luks_or_plain_or_bitlk_by_passphrase( | |
1911 | struct crypt_device *cd, | |
1912 | const char *name, | |
1913 | char **passwords, | |
1914 | uint32_t flags, | |
1915 | bool pass_volume_key) { | |
1916 | ||
b8c80b56 LP |
1917 | int r; |
1918 | ||
1919 | assert(cd); | |
1920 | assert(name); | |
1921 | ||
1922 | r = -EINVAL; | |
1923 | STRV_FOREACH(p, passwords) { | |
1924 | if (pass_volume_key) | |
94c0c85e | 1925 | r = measured_crypt_activate_by_volume_key(cd, name, *p, arg_key_size, flags); |
b8c80b56 | 1926 | else |
94c0c85e | 1927 | r = measured_crypt_activate_by_passphrase(cd, name, arg_key_slot, *p, strlen(*p), flags); |
b8c80b56 LP |
1928 | if (r >= 0) |
1929 | break; | |
1930 | } | |
1931 | if (r == -EPERM) { | |
1932 | log_error_errno(r, "Failed to activate with specified passphrase. (Passphrase incorrect?)"); | |
1933 | return -EAGAIN; /* log actual error, but return EAGAIN */ | |
1934 | } | |
1935 | if (r < 0) | |
1936 | return log_error_errno(r, "Failed to activate with specified passphrase: %m"); | |
1937 | ||
1938 | return 0; | |
1939 | } | |
1940 | ||
6cc27c29 | 1941 | static int attach_luks_or_plain_or_bitlk( |
9c5253ff LP |
1942 | struct crypt_device *cd, |
1943 | const char *name, | |
1944 | const char *key_file, | |
7407f689 LP |
1945 | const void *key_data, |
1946 | size_t key_data_size, | |
9c5253ff | 1947 | char **passwords, |
08669709 LP |
1948 | uint32_t flags, |
1949 | usec_t until) { | |
9c5253ff | 1950 | |
10fb4e35 | 1951 | bool pass_volume_key = false; |
b8c80b56 | 1952 | int r; |
10fb4e35 JJ |
1953 | |
1954 | assert(cd); | |
1955 | assert(name); | |
10fb4e35 | 1956 | |
2e4beb87 | 1957 | if ((!arg_type && !crypt_get_type(cd)) || streq_ptr(arg_type, CRYPT_PLAIN)) { |
4eac2773 MP |
1958 | struct crypt_params_plain params = { |
1959 | .offset = arg_offset, | |
1960 | .skip = arg_skip, | |
a9fc6406 | 1961 | .sector_size = arg_sector_size, |
4eac2773 | 1962 | }; |
10fb4e35 JJ |
1963 | const char *cipher, *cipher_mode; |
1964 | _cleanup_free_ char *truncated_cipher = NULL; | |
1965 | ||
b8c80b56 | 1966 | if (streq_ptr(arg_hash, "plain")) |
10fb4e35 | 1967 | /* plain isn't a real hash type. it just means "use no hash" */ |
b8c80b56 LP |
1968 | params.hash = NULL; |
1969 | else if (arg_hash) | |
1970 | params.hash = arg_hash; | |
1971 | else if (!key_file) | |
1972 | /* for CRYPT_PLAIN, the behaviour of cryptsetup package is to not hash when a key | |
1973 | * file is provided */ | |
10fb4e35 JJ |
1974 | params.hash = "ripemd160"; |
1975 | ||
f75cac37 | 1976 | if (arg_cipher) { |
10fb4e35 JJ |
1977 | size_t l; |
1978 | ||
f75cac37 LP |
1979 | l = strcspn(arg_cipher, "-"); |
1980 | truncated_cipher = strndup(arg_cipher, l); | |
10fb4e35 JJ |
1981 | if (!truncated_cipher) |
1982 | return log_oom(); | |
1983 | ||
1984 | cipher = truncated_cipher; | |
f75cac37 | 1985 | cipher_mode = arg_cipher[l] ? arg_cipher+l+1 : "plain"; |
10fb4e35 JJ |
1986 | } else { |
1987 | cipher = "aes"; | |
1988 | cipher_mode = "cbc-essiv:sha256"; | |
1989 | } | |
1990 | ||
aed68083 | 1991 | /* for CRYPT_PLAIN limit reads from keyfile to key length, and ignore keyfile-size */ |
6131a78b | 1992 | arg_keyfile_size = arg_key_size; |
10fb4e35 | 1993 | |
30747265 | 1994 | /* In contrast to what the name crypt_format() might suggest this doesn't actually format |
aed68083 | 1995 | * anything, it just configures encryption parameters when used for plain mode. */ |
1602b008 | 1996 | r = crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, arg_keyfile_size, ¶ms); |
2e4beb87 MB |
1997 | if (r < 0) |
1998 | return log_error_errno(r, "Loading of cryptographic parameters failed: %m"); | |
10fb4e35 JJ |
1999 | |
2000 | /* hash == NULL implies the user passed "plain" */ | |
b8c80b56 | 2001 | pass_volume_key = !params.hash; |
10fb4e35 | 2002 | } |
10fb4e35 JJ |
2003 | |
2004 | log_info("Set cipher %s, mode %s, key size %i bits for device %s.", | |
2005 | crypt_get_cipher(cd), | |
2006 | crypt_get_cipher_mode(cd), | |
2007 | crypt_get_volume_key_size(cd)*8, | |
2008 | crypt_get_device_name(cd)); | |
2009 | ||
18843ecc LP |
2010 | if (arg_tpm2_device || arg_tpm2_device_auto) |
2011 | return attach_luks_or_plain_or_bitlk_by_tpm2(cd, name, key_file, key_data, key_data_size, until, flags, pass_volume_key); | |
2bc5c425 LP |
2012 | if (arg_fido2_device || arg_fido2_device_auto) |
2013 | return attach_luks_or_plain_or_bitlk_by_fido2(cd, name, key_file, key_data, key_data_size, until, flags, pass_volume_key); | |
b8c80b56 LP |
2014 | if (arg_pkcs11_uri || arg_pkcs11_uri_auto) |
2015 | return attach_luks_or_plain_or_bitlk_by_pkcs11(cd, name, key_file, key_data, key_data_size, until, flags, pass_volume_key); | |
2016 | if (key_data) | |
2017 | return attach_luks_or_plain_or_bitlk_by_key_data(cd, name, key_data, key_data_size, flags, pass_volume_key); | |
2018 | if (key_file) | |
2019 | return attach_luks_or_plain_or_bitlk_by_key_file(cd, name, key_file, flags, pass_volume_key); | |
10fb4e35 | 2020 | |
b8c80b56 | 2021 | return attach_luks_or_plain_or_bitlk_by_passphrase(cd, name, passwords, flags, pass_volume_key); |
10fb4e35 JJ |
2022 | } |
2023 | ||
dd5e696d | 2024 | static int help(void) { |
37ec0fdd LP |
2025 | _cleanup_free_ char *link = NULL; |
2026 | int r; | |
2027 | ||
2028 | r = terminal_urlify_man("systemd-cryptsetup@.service", "8", &link); | |
2029 | if (r < 0) | |
2030 | return log_oom(); | |
dd5e696d | 2031 | |
b7de9651 | 2032 | printf("%s attach VOLUME SOURCEDEVICE [KEY-FILE] [OPTIONS]\n" |
dd5e696d | 2033 | "%s detach VOLUME\n\n" |
37ec0fdd | 2034 | "Attaches or detaches an encrypted block device.\n" |
bc556335 DDM |
2035 | "\nSee the %s for details.\n", |
2036 | program_invocation_short_name, | |
2037 | program_invocation_short_name, | |
2038 | link); | |
dd5e696d LP |
2039 | |
2040 | return 0; | |
2041 | } | |
2042 | ||
d5d1ae15 LP |
2043 | static uint32_t determine_flags(void) { |
2044 | uint32_t flags = 0; | |
2045 | ||
2046 | if (arg_readonly) | |
2047 | flags |= CRYPT_ACTIVATE_READONLY; | |
2048 | ||
2049 | if (arg_discards) | |
2050 | flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS; | |
2051 | ||
2052 | if (arg_same_cpu_crypt) | |
2053 | flags |= CRYPT_ACTIVATE_SAME_CPU_CRYPT; | |
2054 | ||
2055 | if (arg_submit_from_crypt_cpus) | |
2056 | flags |= CRYPT_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS; | |
2057 | ||
227acf00 JU |
2058 | if (arg_no_read_workqueue) |
2059 | flags |= CRYPT_ACTIVATE_NO_READ_WORKQUEUE; | |
2060 | ||
2061 | if (arg_no_write_workqueue) | |
2062 | flags |= CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE; | |
2063 | ||
408c81f6 MS |
2064 | #ifdef CRYPT_ACTIVATE_SERIALIZE_MEMORY_HARD_PBKDF |
2065 | /* Try to decrease the risk of OOM event if memory hard key derivation function is in use */ | |
2066 | /* https://gitlab.com/cryptsetup/cryptsetup/issues/446/ */ | |
2067 | flags |= CRYPT_ACTIVATE_SERIALIZE_MEMORY_HARD_PBKDF; | |
2068 | #endif | |
2069 | ||
d5d1ae15 LP |
2070 | return flags; |
2071 | } | |
2072 | ||
d3d49e76 LP |
2073 | static void remove_and_erasep(const char **p) { |
2074 | int r; | |
2075 | ||
2076 | if (!*p) | |
2077 | return; | |
2078 | ||
2079 | r = unlinkat_deallocate(AT_FDCWD, *p, UNLINK_ERASE); | |
2080 | if (r < 0 && r != -ENOENT) | |
2081 | log_warning_errno(r, "Unable to erase key file '%s', ignoring: %m", *p); | |
2082 | } | |
2083 | ||
3a40f366 | 2084 | static int run(int argc, char *argv[]) { |
294bd454 | 2085 | _cleanup_(crypt_freep) struct crypt_device *cd = NULL; |
da2268f9 | 2086 | const char *verb; |
3a40f366 | 2087 | int r; |
e23a0ce8 | 2088 | |
542bb9be | 2089 | if (argv_looks_like_help(argc, argv)) |
3a40f366 | 2090 | return help(); |
dd5e696d | 2091 | |
d7a0f1f4 FS |
2092 | if (argc < 3) |
2093 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
2094 | "This program requires at least two arguments."); | |
e23a0ce8 | 2095 | |
d2acb93d | 2096 | log_setup(); |
e23a0ce8 | 2097 | |
2f678640 | 2098 | cryptsetup_enable_logging(NULL); |
568a8404 | 2099 | |
4c12626c LP |
2100 | umask(0022); |
2101 | ||
da2268f9 LP |
2102 | verb = argv[1]; |
2103 | ||
2104 | if (streq(verb, "attach")) { | |
d7ac0952 | 2105 | _unused_ _cleanup_(remove_and_erasep) const char *destroy_key_file = NULL; |
7407f689 | 2106 | _cleanup_(erase_and_freep) void *key_data = NULL; |
da2268f9 LP |
2107 | const char *volume, *source, *key_file, *options; |
2108 | crypt_status_info status; | |
7407f689 | 2109 | size_t key_data_size = 0; |
da2268f9 LP |
2110 | uint32_t flags = 0; |
2111 | unsigned tries; | |
2112 | usec_t until; | |
b6c0bd11 | 2113 | PassphraseType passphrase_type = PASSPHRASE_NONE; |
7f4e0805 | 2114 | |
b7de9651 | 2115 | /* Arguments: systemd-cryptsetup attach VOLUME SOURCE-DEVICE [KEY-FILE] [OPTIONS] */ |
b61e476f | 2116 | |
0ffff81a LP |
2117 | if (argc < 4) |
2118 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "attach requires at least two arguments."); | |
7f4e0805 | 2119 | |
da2268f9 LP |
2120 | volume = argv[2]; |
2121 | source = argv[3]; | |
2fabbad8 LP |
2122 | key_file = mangle_none(argc >= 5 ? argv[4] : NULL); |
2123 | options = mangle_none(argc >= 6 ? argv[5] : NULL); | |
7407f689 | 2124 | |
da2268f9 LP |
2125 | if (!filename_is_valid(volume)) |
2126 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume name '%s' is not valid.", volume); | |
2127 | ||
2128 | if (key_file && !path_is_absolute(key_file)) { | |
2129 | log_warning("Password file path '%s' is not absolute. Ignoring.", key_file); | |
2130 | key_file = NULL; | |
7f4e0805 LP |
2131 | } |
2132 | ||
da2268f9 LP |
2133 | if (options) { |
2134 | r = parse_options(options); | |
3a40f366 YW |
2135 | if (r < 0) |
2136 | return r; | |
74b1c371 | 2137 | } |
e23a0ce8 | 2138 | |
7bb553bb | 2139 | log_debug("%s %s ← %s type=%s cipher=%s", __func__, |
da2268f9 | 2140 | volume, source, strempty(arg_type), strempty(arg_cipher)); |
7bb553bb | 2141 | |
b853f6e9 | 2142 | /* A delicious drop of snake oil */ |
9c5253ff | 2143 | (void) mlockall(MCL_FUTURE); |
b853f6e9 | 2144 | |
7407f689 | 2145 | if (!key_file) { |
e060ed32 | 2146 | _cleanup_free_ char *bindname = NULL; |
7407f689 LP |
2147 | const char *fn; |
2148 | ||
da2268f9 | 2149 | bindname = make_bindname(volume); |
e060ed32 LP |
2150 | if (!bindname) |
2151 | return log_oom(); | |
2152 | ||
7407f689 LP |
2153 | /* If a key file is not explicitly specified, search for a key in a well defined |
2154 | * search path, and load it. */ | |
2155 | ||
da2268f9 | 2156 | fn = strjoina(volume, ".key"); |
e060ed32 LP |
2157 | r = find_key_file( |
2158 | fn, | |
2159 | STRV_MAKE("/etc/cryptsetup-keys.d", "/run/cryptsetup-keys.d"), | |
2160 | bindname, | |
2161 | &key_data, &key_data_size); | |
7407f689 LP |
2162 | if (r < 0) |
2163 | return r; | |
2164 | if (r > 0) | |
da2268f9 | 2165 | log_debug("Automatically discovered key for volume '%s'.", volume); |
7407f689 | 2166 | } else if (arg_keyfile_erase) |
d3d49e76 LP |
2167 | destroy_key_file = key_file; /* let's get this baby erased when we leave */ |
2168 | ||
7376e835 | 2169 | if (arg_header) { |
70390240 KZ |
2170 | if (streq_ptr(arg_type, CRYPT_TCRYPT)){ |
2171 | log_debug("tcrypt header: %s", arg_header); | |
2172 | r = crypt_init_data_device(&cd, arg_header, source); | |
2173 | } else { | |
2174 | log_debug("LUKS header: %s", arg_header); | |
2175 | r = crypt_init(&cd, arg_header); | |
2176 | } | |
7376e835 | 2177 | } else |
da2268f9 | 2178 | r = crypt_init(&cd, source); |
3a40f366 YW |
2179 | if (r < 0) |
2180 | return log_error_errno(r, "crypt_init() failed: %m"); | |
e23a0ce8 | 2181 | |
efc3b12f | 2182 | cryptsetup_enable_logging(cd); |
7f4e0805 | 2183 | |
da2268f9 | 2184 | status = crypt_status(cd, volume); |
3742095b | 2185 | if (IN_SET(status, CRYPT_ACTIVE, CRYPT_BUSY)) { |
da2268f9 | 2186 | log_info("Volume %s already active.", volume); |
3a40f366 | 2187 | return 0; |
7f4e0805 LP |
2188 | } |
2189 | ||
d5d1ae15 | 2190 | flags = determine_flags(); |
2c65512e | 2191 | |
496db330 YW |
2192 | until = usec_add(now(CLOCK_MONOTONIC), arg_timeout); |
2193 | if (until == USEC_INFINITY) | |
7dcda352 | 2194 | until = 0; |
260ab287 | 2195 | |
714c5869 LP |
2196 | if (arg_key_size == 0) |
2197 | arg_key_size = 256U / 8U; | |
e2d480b9 | 2198 | |
10fb4e35 JJ |
2199 | if (key_file) { |
2200 | struct stat st; | |
e2d480b9 | 2201 | |
10fb4e35 JJ |
2202 | /* Ideally we'd do this on the open fd, but since this is just a |
2203 | * warning it's OK to do this in two steps. */ | |
3f4d56a0 MP |
2204 | if (stat(key_file, &st) >= 0 && S_ISREG(st.st_mode) && (st.st_mode & 0005)) |
2205 | log_warning("Key file %s is world-readable. This is not a good idea!", key_file); | |
e2d480b9 LP |
2206 | } |
2207 | ||
6930d069 LP |
2208 | if (!arg_type || STR_IN_SET(arg_type, ANY_LUKS, CRYPT_LUKS1, CRYPT_LUKS2)) { |
2209 | r = crypt_load(cd, !arg_type || streq(arg_type, ANY_LUKS) ? CRYPT_LUKS : arg_type, NULL); | |
ea9a9d49 MB |
2210 | if (r < 0) |
2211 | return log_error_errno(r, "Failed to load LUKS superblock on device %s: %m", crypt_get_device_name(cd)); | |
2212 | ||
2213 | if (arg_header) { | |
da2268f9 | 2214 | r = crypt_set_data_device(cd, source); |
ea9a9d49 | 2215 | if (r < 0) |
da2268f9 | 2216 | return log_error_errno(r, "Failed to set LUKS data device %s: %m", source); |
ea9a9d49 | 2217 | } |
d90874b4 | 2218 | |
894bb3ca | 2219 | /* Tokens are available in LUKS2 only, but it is ok to call (and fail) with LUKS1. */ |
a0789e5f | 2220 | if (!key_file && !key_data && getenv_bool("SYSTEMD_CRYPTSETUP_USE_TOKEN_MODULE") != 0) { |
ee6c66ac JW |
2221 | r = crypt_activate_by_token_pin_ask_password( |
2222 | cd, | |
2223 | volume, | |
2224 | NULL, | |
2225 | until, | |
2226 | arg_headless, | |
2227 | NULL, | |
2228 | flags, | |
2229 | "Please enter LUKS2 token PIN:", | |
2230 | "luks2-pin", | |
2231 | "cryptsetup.luks2-pin"); | |
894bb3ca | 2232 | if (r >= 0) { |
da2268f9 | 2233 | log_debug("Volume %s activated with LUKS token id %i.", volume, r); |
894bb3ca MB |
2234 | return 0; |
2235 | } | |
2236 | ||
2237 | log_debug_errno(r, "Token activation unsuccessful for device %s: %m", crypt_get_device_name(cd)); | |
2238 | } | |
ea9a9d49 MB |
2239 | } |
2240 | ||
6cc27c29 MF |
2241 | /* since cryptsetup 2.3.0 (Feb 2020) */ |
2242 | #ifdef CRYPT_BITLK | |
5af39ac8 | 2243 | if (streq_ptr(arg_type, CRYPT_BITLK)) { |
6cc27c29 MF |
2244 | r = crypt_load(cd, CRYPT_BITLK, NULL); |
2245 | if (r < 0) | |
2246 | return log_error_errno(r, "Failed to load Bitlocker superblock on device %s: %m", crypt_get_device_name(cd)); | |
2247 | } | |
2248 | #endif | |
2249 | ||
f75cac37 | 2250 | for (tries = 0; arg_tries == 0 || tries < arg_tries; tries++) { |
ab84f5b9 | 2251 | _cleanup_strv_free_erase_ char **passwords = NULL; |
260ab287 | 2252 | |
0ba6f85e LP |
2253 | /* When we were able to acquire multiple keys, let's always process them in this order: |
2254 | * | |
18843ecc | 2255 | * 1. A key acquired via PKCS#11 or FIDO2 token, or TPM2 chip |
0ba6f85e LP |
2256 | * 2. The discovered key: i.e. key_data + key_data_size |
2257 | * 3. The configured key: i.e. key_file + arg_keyfile_offset + arg_keyfile_size | |
2258 | * 4. The empty password, in case arg_try_empty_password is set | |
2259 | * 5. We enquire the user for a password | |
2260 | */ | |
2261 | ||
18843ecc | 2262 | 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) { |
0ba6f85e LP |
2263 | |
2264 | if (arg_try_empty_password) { | |
2265 | /* Hmm, let's try an empty password now, but only once */ | |
2266 | arg_try_empty_password = false; | |
2267 | ||
2268 | key_data = strdup(""); | |
2269 | if (!key_data) | |
2270 | return log_oom(); | |
2271 | ||
2272 | key_data_size = 0; | |
2273 | } else { | |
b6c0bd11 | 2274 | /* Ask the user for a passphrase or recovery key only as last resort, if we have |
0ba6f85e | 2275 | * nothing else to check for */ |
b6c0bd11 AAF |
2276 | if (passphrase_type == PASSPHRASE_NONE) { |
2277 | passphrase_type = check_registered_passwords(cd); | |
2278 | if (passphrase_type == PASSPHRASE_NONE) | |
2279 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No passphrase or recovery key registered."); | |
2280 | } | |
0ba6f85e | 2281 | |
b6c0bd11 | 2282 | r = get_password(volume, source, until, tries == 0 && !arg_verify, passphrase_type, &passwords); |
0ba6f85e LP |
2283 | if (r == -EAGAIN) |
2284 | continue; | |
2285 | if (r < 0) | |
2286 | return r; | |
2287 | } | |
260ab287 LP |
2288 | } |
2289 | ||
f75cac37 | 2290 | if (streq_ptr(arg_type, CRYPT_TCRYPT)) |
da2268f9 | 2291 | r = attach_tcrypt(cd, volume, key_file, key_data, key_data_size, passwords, flags); |
8cf3ca80 | 2292 | else |
da2268f9 | 2293 | r = attach_luks_or_plain_or_bitlk(cd, volume, key_file, key_data, key_data_size, passwords, flags, until); |
5f4bfe56 | 2294 | if (r >= 0) |
260ab287 | 2295 | break; |
6f177c7d LP |
2296 | if (r != -EAGAIN) |
2297 | return r; | |
260ab287 | 2298 | |
2424fb7e LP |
2299 | /* Key not correct? Let's try again! */ |
2300 | ||
6f177c7d | 2301 | key_file = NULL; |
7407f689 LP |
2302 | key_data = erase_and_free(key_data); |
2303 | key_data_size = 0; | |
2424fb7e | 2304 | arg_pkcs11_uri = mfree(arg_pkcs11_uri); |
b997d111 | 2305 | arg_pkcs11_uri_auto = false; |
2bc5c425 LP |
2306 | arg_fido2_device = mfree(arg_fido2_device); |
2307 | arg_fido2_device_auto = false; | |
18843ecc LP |
2308 | arg_tpm2_device = mfree(arg_tpm2_device); |
2309 | arg_tpm2_device_auto = false; | |
7f4e0805 LP |
2310 | } |
2311 | ||
0ffff81a LP |
2312 | if (arg_tries != 0 && tries >= arg_tries) |
2313 | return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Too many attempts to activate; giving up."); | |
7f4e0805 | 2314 | |
da2268f9 LP |
2315 | } else if (streq(verb, "detach")) { |
2316 | const char *volume; | |
2317 | ||
2318 | volume = argv[2]; | |
7f4e0805 | 2319 | |
da2268f9 LP |
2320 | if (!filename_is_valid(volume)) |
2321 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Volume name '%s' is not valid.", volume); | |
7407f689 | 2322 | |
da2268f9 | 2323 | r = crypt_init_by_name(&cd, volume); |
5f4bfe56 | 2324 | if (r == -ENODEV) { |
da2268f9 | 2325 | log_info("Volume %s already inactive.", volume); |
3a40f366 | 2326 | return 0; |
7f4e0805 | 2327 | } |
3a40f366 | 2328 | if (r < 0) |
ffe0da29 | 2329 | return log_error_errno(r, "crypt_init_by_name() for volume '%s' failed: %m", volume); |
7f4e0805 | 2330 | |
efc3b12f | 2331 | cryptsetup_enable_logging(cd); |
7f4e0805 | 2332 | |
da2268f9 | 2333 | r = crypt_deactivate(cd, volume); |
3a40f366 | 2334 | if (r < 0) |
ffe0da29 | 2335 | return log_error_errno(r, "Failed to deactivate '%s': %m", volume); |
e23a0ce8 | 2336 | |
0ffff81a | 2337 | } else |
da2268f9 | 2338 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown verb %s.", verb); |
e23a0ce8 | 2339 | |
3a40f366 | 2340 | return 0; |
e23a0ce8 | 2341 | } |
3a40f366 YW |
2342 | |
2343 | DEFINE_MAIN_FUNCTION(run); |