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