Debian Codegraph @

src/shared/cryptsetup-fido2.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-json.h"
4
5 #include "alloc-util.h"
6 #include "ask-password-api.h"
7 #include "cryptsetup-fido2.h"
8 #include "cryptsetup-util.h"
9 #include "env-util.h"
10 #include "fido2-util.h"
11 #include "iovec-util.h"
12 #include "libfido2-util.h"
13 #include "log.h"
14
15 #include "strv.h"
16
17 int acquire_fido2_key(
18 const char *volume_name,
19 const char *friendly_name,
20 const char *device,
21 const char *rp_id,
22 const void *cid,
23 size_t cid_size,
24 const char *key_file,
25 size_t key_file_size,
26 uint64_t key_file_offset,
27 const struct iovec *key_data,
28 usec_t until,
29 Fido2EnrollFlags required,
30 const char *askpw_credential,
31 AskPasswordFlags askpw_flags,
32 void **ret_decrypted_key,
33 size_t *ret_decrypted_key_size) {
34
35 #if HAVE_LIBCRYPTSETUP && HAVE_LIBFIDO2
36 _cleanup_(erase_and_freep) char *envpw = NULL;
37 _cleanup_strv_free_erase_ char **pins = NULL;
38 _cleanup_(iovec_done_erase) struct iovec loaded_salt = {};
39 bool device_exists = false;
40 struct iovec salt;
41 int r;
42
43 if ((required & (FIDO2ENROLL_PIN | FIDO2ENROLL_UP | FIDO2ENROLL_UV)) && FLAGS_SET(askpw_flags, ASK_PASSWORD_HEADLESS))
44 return log_error_errno(SYNTHETIC_ERRNO(ENOPKG),
45 "Local verification is required to unlock this volume, but the 'headless' parameter was set.");
46
47 assert(cid);
48 assert(key_file || iovec_is_set(key_data));
49
50 if (iovec_is_set(key_data))
51 salt = *key_data;
52 else {
53 if (key_file_size > 0)
54 log_debug("Ignoring 'keyfile-size=' option for a FIDO2 salt file.");
55
56 r = fido2_read_salt_file(
57 key_file, key_file_offset,
58 /* client= */ "cryptsetup",
59 /* node= */ volume_name,
60 &loaded_salt);
61 if (r < 0)
62 return r;
63
64 salt = loaded_salt;
65 }
66
67 r = getenv_steal_erase("PIN", &envpw);
68 if (r < 0)
69 return log_error_errno(r, "Failed to acquire password from environment: %m");
70 if (r > 0) {
71 pins = strv_new(envpw);
72 if (!pins)
73 return log_oom();
74 }
75
76 for (;;) {
77 if (!device_exists) {
78 /* Before we inquire for the PIN we'll need, if we never talked to the device, check
79 * if the device actually is plugged in. Otherwise we'll ask for the PIN already when
80 * the device is not plugged in, which is confusing. */
81
82 r = fido2_have_device(device);
83 if (r < 0)
84 return r;
85 if (r == 0) /* no device found, return EAGAIN so that caller will wait/watch udev */
86 return -EAGAIN;
87
88 device_exists = true; /* now we know for sure, a device exists, no need to ask again */
89 }
90
91 /* Always make an attempt before asking for PIN.
92 * fido2_use_hmac_hash() will perform a pre-flight check for whether the credential for
93 * can be found on one of the connected devices. This way, we can avoid prompting the user
94 * for a PIN when we are sure that no device can be used. */
95 r = fido2_use_hmac_hash(
96 device,
97 rp_id ?: "io.systemd.cryptsetup",
98 salt.iov_base, salt.iov_len,
99 cid, cid_size,
100 pins,
101 required,
102 ret_decrypted_key,
103 ret_decrypted_key_size);
104 if (!IN_SET(r,
105 -ENOANO, /* needs pin */
106 -ENOLCK)) /* pin incorrect */
107 return r;
108
109 device_exists = true; /* that a PIN is needed/wasn't correct means that we managed to
110 * talk to a device */
111
112 if (FLAGS_SET(askpw_flags, ASK_PASSWORD_HEADLESS))
113 return log_error_errno(SYNTHETIC_ERRNO(ENOPKG), "PIN querying disabled via 'headless' option. Use the '$PIN' environment variable.");
114
115 AskPasswordRequest req = {
116 .tty_fd = -EBADF,
117 .message = "Please enter security token PIN:",
118 .icon = "drive-harddisk",
119 .keyring = "fido2-pin",
120 .credential = "cryptsetup.fido2-pin",
121 .until = until,
122 .hup_fd = -EBADF,
123 };
124
125 pins = strv_free_erase(pins);
126 r = ask_password_auto(&req, askpw_flags, &pins);
127 if (r < 0)
128 return log_error_errno(r, "Failed to ask for user password: %m");
129
130 askpw_flags &= ~ASK_PASSWORD_ACCEPT_CACHED;
131 }
132 #else
133 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "FIDO2 token support not available.");
134 #endif
135 }
136
137 int acquire_fido2_key_auto(
138 struct crypt_device *cd,
139 const char *name,
140 const char *friendly_name,
141 const char *fido2_device,
142 usec_t until,
143 const char *askpw_credential,
144 AskPasswordFlags askpw_flags,
145 void **ret_decrypted_key,
146 size_t *ret_decrypted_key_size) {
147
148 #if HAVE_LIBCRYPTSETUP && HAVE_LIBFIDO2
149 _cleanup_free_ void *cid = NULL;
150 size_t cid_size = 0;
151 int r, ret = -ENOENT;
152 Fido2EnrollFlags required = 0;
153
154 assert(cd);
155 assert(name);
156 assert(ret_decrypted_key);
157 assert(ret_decrypted_key_size);
158
159 /* Loads FIDO2 metadata from LUKS2 JSON token headers. */
160
161 for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token++) {
162 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
163 sd_json_variant *w;
164 _cleanup_free_ void *salt = NULL;
165 _cleanup_free_ char *rp = NULL;
166 size_t salt_size = 0;
167 int ks;
168
169 r = cryptsetup_get_token_as_json(cd, token, "systemd-fido2", &v);
170 if (IN_SET(r, -ENOENT, -EINVAL, -EMEDIUMTYPE))
171 continue;
172 if (r < 0)
173 return log_error_errno(r, "Failed to read JSON token data off disk: %m");
174
175 ks = cryptsetup_get_keyslot_from_token(v);
176 if (ks < 0) {
177 /* Handle parsing errors of the keyslots field gracefully, since it's not 'owned' by
178 * us, but by the LUKS2 spec */
179 log_warning_errno(ks, "Failed to extract keyslot index from FIDO2 JSON data token %i, skipping: %m", token);
180 continue;
181 }
182
183 w = sd_json_variant_by_key(v, "fido2-credential");
184 if (!w)
185 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
186 "FIDO2 token data lacks 'fido2-credential' field.");
187
188 r = sd_json_variant_unbase64(w, &cid, &cid_size);
189 if (r < 0)
190 return log_error_errno(r, "Invalid base64 data in 'fido2-credential' field: %m");
191
192 w = sd_json_variant_by_key(v, "fido2-salt");
193 if (!w)
194 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
195 "FIDO2 token data lacks 'fido2-salt' field.");
196
197 assert(!salt);
198 assert(salt_size == 0);
199 r = sd_json_variant_unbase64(w, &salt, &salt_size);
200 if (r < 0)
201 return log_error_errno(r, "Failed to decode base64 encoded salt: %m");
202
203 w = sd_json_variant_by_key(v, "fido2-rp");
204 if (w) {
205 /* The "rp" field is optional. */
206
207 if (!sd_json_variant_is_string(w))
208 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
209 "FIDO2 token data's 'fido2-rp' field is not a string.");
210
211 assert(!rp);
212 rp = strdup(sd_json_variant_string(w));
213 if (!rp)
214 return log_oom();
215 }
216
217 w = sd_json_variant_by_key(v, "fido2-clientPin-required");
218 if (w) {
219 /* The "fido2-clientPin-required" field is optional. */
220
221 if (!sd_json_variant_is_boolean(w))
222 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
223 "FIDO2 token data's 'fido2-clientPin-required' field is not a boolean.");
224
225 SET_FLAG(required, FIDO2ENROLL_PIN, sd_json_variant_boolean(w));
226 } else
227 required |= FIDO2ENROLL_PIN_IF_NEEDED; /* compat with 248, where the field was unset */
228
229 w = sd_json_variant_by_key(v, "fido2-up-required");
230 if (w) {
231 /* The "fido2-up-required" field is optional. */
232
233 if (!sd_json_variant_is_boolean(w))
234 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
235 "FIDO2 token data's 'fido2-up-required' field is not a boolean.");
236
237 SET_FLAG(required, FIDO2ENROLL_UP, sd_json_variant_boolean(w));
238 } else
239 required |= FIDO2ENROLL_UP_IF_NEEDED; /* compat with 248 */
240
241 w = sd_json_variant_by_key(v, "fido2-uv-required");
242 if (w) {
243 /* The "fido2-uv-required" field is optional. */
244
245 if (!sd_json_variant_is_boolean(w))
246 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
247 "FIDO2 token data's 'fido2-uv-required' field is not a boolean.");
248
249 SET_FLAG(required, FIDO2ENROLL_UV, sd_json_variant_boolean(w));
250 } else
251 required |= FIDO2ENROLL_UV_OMIT; /* compat with 248 */
252
253 ret = acquire_fido2_key(
254 name,
255 friendly_name,
256 fido2_device,
257 rp,
258 cid, cid_size,
259 /* key_file= */ NULL, /* salt is read from LUKS header instead of key_file */
260 /* key_file_size= */ 0,
261 /* key_file_offset= */ 0,
262 &IOVEC_MAKE(salt, salt_size),
263 until,
264 required,
265 "cryptsetup.fido2-pin",
266 askpw_flags,
267 ret_decrypted_key,
268 ret_decrypted_key_size);
269 if (ret == 0)
270 break;
271 }
272
273 if (!cid)
274 return log_error_errno(SYNTHETIC_ERRNO(ENXIO),
275 "No valid FIDO2 token data found.");
276
277 if (ret == -EAGAIN) /* fido2 device does not exist, or UV is blocked; caller will prompt for retry */
278 return log_debug_errno(ret, "FIDO2 token does not exist, or UV is blocked.");
279 if (ret < 0)
280 return log_error_errno(ret, "Failed to unlock LUKS volume with FIDO2 token: %m");
281
282 log_info("Unlocked volume via automatically discovered security FIDO2 token.");
283 return ret;
284 #else
285 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "FIDO2 token support not available.");
286 #endif
287 }