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