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