Debian Codegraph @

src/shared/libfido2-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-dlopen.h"
4
5 #include "libfido2-util.h"
6 #include "log.h"
7
8 #if HAVE_LIBFIDO2
9 #include "alloc-util.h"
10 #include "ansi-color.h"
11 #include "ask-password-api.h"
12 #include "dlfcn-util.h"
13 #include "format-table.h"
14 #include "glyph-util.h"
15 #include "iovec-util.h"
16 #include "locale-util.h"
17 #include "plymouth-util.h"
18 #include "string-util.h"
19 #include "strv.h"
20 #include "unistd.h"
21
22 /* Added in version 1.5.0 */
23 #ifndef FIDO_ERR_UV_BLOCKED
24 #define FIDO_ERR_UV_BLOCKED 0x3c
25 #endif
26
27 DLSYM_PROTOTYPE(fido_assert_allow_cred) = NULL;
28 DLSYM_PROTOTYPE(fido_assert_free) = NULL;
29 DLSYM_PROTOTYPE(fido_assert_hmac_secret_len) = NULL;
30 DLSYM_PROTOTYPE(fido_assert_hmac_secret_ptr) = NULL;
31 DLSYM_PROTOTYPE(fido_assert_new) = NULL;
32 DLSYM_PROTOTYPE(fido_assert_set_clientdata_hash) = NULL;
33 DLSYM_PROTOTYPE(fido_assert_set_extensions) = NULL;
34 DLSYM_PROTOTYPE(fido_assert_set_hmac_salt) = NULL;
35 DLSYM_PROTOTYPE(fido_assert_set_rp) = NULL;
36 DLSYM_PROTOTYPE(fido_assert_set_up) = NULL;
37 DLSYM_PROTOTYPE(fido_assert_set_uv) = NULL;
38 DLSYM_PROTOTYPE(fido_cbor_info_extensions_len) = NULL;
39 DLSYM_PROTOTYPE(fido_cbor_info_extensions_ptr) = NULL;
40 DLSYM_PROTOTYPE(fido_cbor_info_free) = NULL;
41 DLSYM_PROTOTYPE(fido_cbor_info_new) = NULL;
42 DLSYM_PROTOTYPE(fido_cbor_info_options_len) = NULL;
43 DLSYM_PROTOTYPE(fido_cbor_info_options_name_ptr) = NULL;
44 DLSYM_PROTOTYPE(fido_cbor_info_options_value_ptr) = NULL;
45 DLSYM_PROTOTYPE(fido_cred_free) = NULL;
46 DLSYM_PROTOTYPE(fido_cred_id_len) = NULL;
47 DLSYM_PROTOTYPE(fido_cred_id_ptr) = NULL;
48 DLSYM_PROTOTYPE(fido_cred_new) = NULL;
49 DLSYM_PROTOTYPE(fido_cred_set_clientdata_hash) = NULL;
50 DLSYM_PROTOTYPE(fido_cred_set_extensions) = NULL;
51 DLSYM_PROTOTYPE(fido_cred_set_prot) = NULL;
52 DLSYM_PROTOTYPE(fido_cred_set_rk) = NULL;
53 DLSYM_PROTOTYPE(fido_cred_set_rp) = NULL;
54 DLSYM_PROTOTYPE(fido_cred_set_type) = NULL;
55 DLSYM_PROTOTYPE(fido_cred_set_user) = NULL;
56 DLSYM_PROTOTYPE(fido_cred_set_uv) = NULL;
57 DLSYM_PROTOTYPE(fido_dev_close) = NULL;
58 DLSYM_PROTOTYPE(fido_dev_free) = NULL;
59 DLSYM_PROTOTYPE(fido_dev_get_assert) = NULL;
60 DLSYM_PROTOTYPE(fido_dev_get_cbor_info) = NULL;
61 DLSYM_PROTOTYPE(fido_dev_info_free) = NULL;
62 DLSYM_PROTOTYPE(fido_dev_info_manifest) = NULL;
63 DLSYM_PROTOTYPE(fido_dev_info_manufacturer_string) = NULL;
64 DLSYM_PROTOTYPE(fido_dev_info_new) = NULL;
65 DLSYM_PROTOTYPE(fido_dev_info_path) = NULL;
66 DLSYM_PROTOTYPE(fido_dev_info_product_string) = NULL;
67 DLSYM_PROTOTYPE(fido_dev_info_ptr) = NULL;
68 DLSYM_PROTOTYPE(fido_dev_is_fido2) = NULL;
69 DLSYM_PROTOTYPE(fido_dev_make_cred) = NULL;
70 DLSYM_PROTOTYPE(fido_dev_new) = NULL;
71 DLSYM_PROTOTYPE(fido_dev_open) = NULL;
72 DLSYM_PROTOTYPE(fido_init) = NULL;
73 DLSYM_PROTOTYPE(fido_set_log_handler) = NULL;
74 DLSYM_PROTOTYPE(fido_strerr) = NULL;
75
76 static void fido_log_propagate_handler(const char *s) {
77 log_debug("libfido2: %s", strempty(s));
78 }
79
80 #endif
81
82 int dlopen_libfido2(int log_level) {
83 #if HAVE_LIBFIDO2
84 static void *libfido2_dl = NULL;
85 int r;
86
87 SD_ELF_NOTE_DLOPEN(
88 "fido2",
89 "Support fido2 for encryption and authentication",
90 SD_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED,
91 "libfido2.so.1");
92
93 r = dlopen_many_sym_or_warn(
94 &libfido2_dl, "libfido2.so.1", log_level,
95 DLSYM_ARG(fido_assert_allow_cred),
96 DLSYM_ARG(fido_assert_free),
97 DLSYM_ARG(fido_assert_hmac_secret_len),
98 DLSYM_ARG(fido_assert_hmac_secret_ptr),
99 DLSYM_ARG(fido_assert_new),
100 DLSYM_ARG(fido_assert_set_clientdata_hash),
101 DLSYM_ARG(fido_assert_set_extensions),
102 DLSYM_ARG(fido_assert_set_hmac_salt),
103 DLSYM_ARG(fido_assert_set_rp),
104 DLSYM_ARG(fido_assert_set_up),
105 DLSYM_ARG(fido_assert_set_uv),
106 DLSYM_ARG(fido_cbor_info_extensions_len),
107 DLSYM_ARG(fido_cbor_info_extensions_ptr),
108 DLSYM_ARG(fido_cbor_info_free),
109 DLSYM_ARG(fido_cbor_info_new),
110 DLSYM_ARG(fido_cbor_info_options_len),
111 DLSYM_ARG(fido_cbor_info_options_name_ptr),
112 DLSYM_ARG(fido_cbor_info_options_value_ptr),
113 DLSYM_ARG(fido_cred_free),
114 DLSYM_ARG(fido_cred_id_len),
115 DLSYM_ARG(fido_cred_id_ptr),
116 DLSYM_ARG(fido_cred_new),
117 DLSYM_ARG(fido_cred_set_clientdata_hash),
118 DLSYM_ARG(fido_cred_set_extensions),
119 DLSYM_ARG(fido_cred_set_prot),
120 DLSYM_ARG(fido_cred_set_rk),
121 DLSYM_ARG(fido_cred_set_rp),
122 DLSYM_ARG(fido_cred_set_type),
123 DLSYM_ARG(fido_cred_set_user),
124 DLSYM_ARG(fido_cred_set_uv),
125 DLSYM_ARG(fido_dev_close),
126 DLSYM_ARG(fido_dev_free),
127 DLSYM_ARG(fido_dev_get_assert),
128 DLSYM_ARG(fido_dev_get_cbor_info),
129 DLSYM_ARG(fido_dev_info_free),
130 DLSYM_ARG(fido_dev_info_manifest),
131 DLSYM_ARG(fido_dev_info_manufacturer_string),
132 DLSYM_ARG(fido_dev_info_new),
133 DLSYM_ARG(fido_dev_info_path),
134 DLSYM_ARG(fido_dev_info_product_string),
135 DLSYM_ARG(fido_dev_info_ptr),
136 DLSYM_ARG(fido_dev_is_fido2),
137 DLSYM_ARG(fido_dev_make_cred),
138 DLSYM_ARG(fido_dev_new),
139 DLSYM_ARG(fido_dev_open),
140 DLSYM_ARG(fido_init),
141 DLSYM_ARG(fido_set_log_handler),
142 DLSYM_ARG(fido_strerr));
143 if (r < 0)
144 return r;
145
146 sym_fido_init(FIDO_DEBUG);
147 sym_fido_set_log_handler(fido_log_propagate_handler);
148
149 return 0;
150 #else
151 return log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP),
152 "libfido2 support is not compiled in.");
153 #endif
154 }
155
156 #if HAVE_LIBFIDO2
157
158 static int verify_features(
159 fido_dev_t *d,
160 const char *path,
161 int log_level, /* the log level to use when device is not FIDO2 with hmac-secret */
162 bool *ret_has_rk,
163 bool *ret_has_client_pin,
164 bool *ret_has_up,
165 bool *ret_has_uv,
166 bool *ret_has_always_uv) {
167
168 _cleanup_(fido_cbor_info_free_wrapper) fido_cbor_info_t *di = NULL;
169 bool found_extension = false;
170 char **e, **o;
171 const bool *b;
172 bool has_rk = false, has_client_pin = false, has_up = true, has_uv = false, has_always_uv = false; /* Defaults are per table in 5.4 in FIDO2 spec */
173 size_t n;
174 int r;
175
176 assert(d);
177 assert(path);
178
179 if (!sym_fido_dev_is_fido2(d))
180 return log_full_errno(log_level, SYNTHETIC_ERRNO(ENODEV),
181 "Specified device %s is not a FIDO2 device.", path);
182
183 di = sym_fido_cbor_info_new();
184 if (!di)
185 return log_oom();
186
187 r = sym_fido_dev_get_cbor_info(d, di);
188 if (r != FIDO_OK)
189 return log_error_errno(SYNTHETIC_ERRNO(EIO),
190 "Failed to get CBOR device info for %s: %s", path, sym_fido_strerr(r));
191
192 e = sym_fido_cbor_info_extensions_ptr(di);
193 n = sym_fido_cbor_info_extensions_len(di);
194 for (size_t i = 0; i < n; i++) {
195 log_debug("FIDO2 device implements extension: %s", e[i]);
196 if (streq(e[i], "hmac-secret"))
197 found_extension = true;
198 }
199
200 o = sym_fido_cbor_info_options_name_ptr(di);
201 b = sym_fido_cbor_info_options_value_ptr(di);
202 n = sym_fido_cbor_info_options_len(di);
203 for (size_t i = 0; i < n; i++) {
204 log_debug("FIDO2 device implements option %s: %s", o[i], yes_no(b[i]));
205 if (streq(o[i], "rk"))
206 has_rk = b[i];
207 if (streq(o[i], "clientPin"))
208 has_client_pin = b[i];
209 if (streq(o[i], "up"))
210 has_up = b[i];
211 if (streq(o[i], "uv"))
212 has_uv = b[i];
213 if (streq(o[i], "alwaysUv"))
214 has_always_uv = b[i];
215 }
216
217 if (!found_extension)
218 return log_full_errno(log_level,
219 SYNTHETIC_ERRNO(ENODEV),
220 "Specified device %s is a FIDO2 device, but does not support the required HMAC-SECRET extension.", path);
221
222 log_debug("Has rk ('Resident Key') support: %s\n"
223 "Has clientPin support: %s\n"
224 "Has up ('User Presence') support: %s\n"
225 "Has uv ('User Verification') support: %s\n"
226 "Has alwaysUv ('User Verification' required): %s\n",
227 yes_no(has_rk),
228 yes_no(has_client_pin),
229 yes_no(has_up),
230 yes_no(has_uv),
231 yes_no(has_always_uv));
232
233 if (ret_has_rk)
234 *ret_has_rk = has_rk;
235 if (ret_has_client_pin)
236 *ret_has_client_pin = has_client_pin;
237 if (ret_has_up)
238 *ret_has_up = has_up;
239 if (ret_has_uv)
240 *ret_has_uv = has_uv;
241 if (ret_has_always_uv)
242 *ret_has_always_uv = has_always_uv;
243
244 return 0;
245 }
246
247 static int fido2_assert_set_basic_properties(
248 fido_assert_t *a,
249 const char *rp_id,
250 const void *cid,
251 size_t cid_size) {
252 int r;
253
254 assert(a);
255 assert(rp_id);
256 assert(cid);
257 assert(cid_size > 0);
258
259 r = sym_fido_assert_set_rp(a, rp_id);
260 if (r != FIDO_OK)
261 return log_error_errno(SYNTHETIC_ERRNO(EIO),
262 "Failed to set FIDO2 assertion ID: %s", sym_fido_strerr(r));
263
264 r = sym_fido_assert_set_clientdata_hash(a, (const unsigned char[32]) {}, 32);
265 if (r != FIDO_OK)
266 return log_error_errno(SYNTHETIC_ERRNO(EIO),
267 "Failed to set FIDO2 assertion client data hash: %s", sym_fido_strerr(r));
268
269 r = sym_fido_assert_allow_cred(a, cid, cid_size);
270 if (r != FIDO_OK)
271 return log_error_errno(SYNTHETIC_ERRNO(EIO),
272 "Failed to add FIDO2 assertion credential ID: %s", sym_fido_strerr(r));
273
274 return 0;
275 }
276
277 static int fido2_common_assert_error_handle(int r) {
278 switch (r) {
279 case FIDO_OK:
280 return 0;
281 case FIDO_ERR_NO_CREDENTIALS:
282 return log_error_errno(SYNTHETIC_ERRNO(EBADSLT),
283 "Wrong security token; needed credentials not present on token.");
284 case FIDO_ERR_PIN_REQUIRED:
285 return log_error_errno(SYNTHETIC_ERRNO(ENOANO),
286 "Security token requires PIN.");
287 case FIDO_ERR_PIN_AUTH_BLOCKED:
288 return log_error_errno(SYNTHETIC_ERRNO(EOWNERDEAD),
289 "PIN of security token is blocked, please remove/reinsert token.");
290 case FIDO_ERR_UV_BLOCKED:
291 return log_error_errno(SYNTHETIC_ERRNO(EOWNERDEAD),
292 "Verification of security token is blocked, please remove/reinsert token.");
293 case FIDO_ERR_PIN_INVALID:
294 return log_error_errno(SYNTHETIC_ERRNO(ENOLCK),
295 "PIN of security token incorrect.");
296 case FIDO_ERR_UP_REQUIRED:
297 return log_error_errno(SYNTHETIC_ERRNO(EMEDIUMTYPE),
298 "User presence required.");
299 case FIDO_ERR_ACTION_TIMEOUT:
300 return log_error_errno(SYNTHETIC_ERRNO(ENOSTR),
301 "Token action timeout. (User didn't interact with token quickly enough.)");
302 default:
303 return log_error_errno(SYNTHETIC_ERRNO(EIO),
304 "Failed to ask token for assertion: %s", sym_fido_strerr(r));
305 }
306 }
307
308 static int fido2_is_cred_in_specific_token(
309 const char *path,
310 const char *rp_id,
311 const void *cid,
312 size_t cid_size,
313 Fido2EnrollFlags flags) {
314
315 assert(path);
316 assert(rp_id);
317 assert(cid);
318 assert(cid_size);
319
320 _cleanup_(fido_dev_free_wrapper) fido_dev_t *d = NULL;
321 _cleanup_(fido_assert_free_wrapper) fido_assert_t *a = NULL;
322 bool has_up = false, has_uv = false;
323 int r;
324
325 d = sym_fido_dev_new();
326 if (!d)
327 return log_oom();
328
329 r = sym_fido_dev_open(d, path);
330 if (r != FIDO_OK)
331 return log_error_errno(SYNTHETIC_ERRNO(EIO),
332 "Failed to open FIDO2 device %s: %s", path, sym_fido_strerr(r));
333
334 r = verify_features(d, path, LOG_ERR, /* ret_has_rk= */ NULL, /* ret_has_client_pin= */ NULL, &has_up, &has_uv, /* ret_has_always_uv= */ NULL);
335 if (r == -ENODEV) { /* Not a FIDO2 device or lacking HMAC-SECRET extension */
336 log_debug_errno(r, "%s is not a FIDO2 device, or it lacks the hmac-secret extension", path);
337 return false;
338 }
339 if (r < 0)
340 return r;
341
342 a = sym_fido_assert_new();
343 if (!a)
344 return log_oom();
345
346 r = fido2_assert_set_basic_properties(a, rp_id, cid, cid_size);
347 if (r < 0)
348 return r;
349
350 /* FIDO2 devices may not support pre-flight requests with UV, at least not
351 * without user interaction [1]. As a result, let's just return true
352 * here and go ahead with trying the unlock directly.
353 * Reference:
354 * 1: https://fidoalliance.org/specs/fido-v2.1-ps-20210615/fido-client-to-authenticator-protocol-v2.1-ps-20210615.html#sctn-getAssert-authnr-alg
355 * See section 7.4 */
356 if (has_uv && FLAGS_SET(flags, FIDO2ENROLL_UV)) {
357 log_debug("Pre-flight requests with UV are unsupported, device: %s", path);
358 return true;
359 }
360
361 /* According to CTAP 2.1 specification, to do pre-flight we need to set up option to false
362 * with optionally pinUvAuthParam in assertion[1]. But for authenticator that doesn't support
363 * user presence, once up option is present, the authenticator may return CTAP2_ERR_UNSUPPORTED_OPTION[2].
364 * So we simply omit the option in that case.
365 * Reference:
366 * 1: https://fidoalliance.org/specs/fido-v2.1-ps-20210615/fido-client-to-authenticator-protocol-v2.1-ps-20210615.html#pre-flight
367 * 2: https://fidoalliance.org/specs/fido-v2.0-ps-20190130/fido-client-to-authenticator-protocol-v2.0-ps-20190130.html#authenticatorGetAssertion (in step 5)
368 */
369 if (has_up)
370 r = sym_fido_assert_set_up(a, FIDO_OPT_FALSE);
371 else
372 r = sym_fido_assert_set_up(a, FIDO_OPT_OMIT);
373 if (r != FIDO_OK)
374 return log_error_errno(SYNTHETIC_ERRNO(EIO),
375 "Failed to set assertion user presence: %s", sym_fido_strerr(r));
376
377 r = sym_fido_dev_get_assert(d, a, NULL);
378
379 switch (r) {
380 case FIDO_OK:
381 return true;
382 case FIDO_ERR_NO_CREDENTIALS:
383 return false;
384 default:
385 return fido2_common_assert_error_handle(r);
386 }
387 }
388
389 static void plymouth_start_interaction(const char *text, bool *ret_displayed) {
390 assert(ret_displayed);
391
392 if (plymouth_send_msg(text, /* pause_spinner= */ true) < 0)
393 return;
394
395 *ret_displayed = true;
396 }
397
398 static void plymouth_end_interaction(bool *displayed) {
399 assert(displayed);
400
401 if (!*displayed)
402 return;
403
404 /* In theory 'm' should hide a message, but it doesn't work (long standing issue).
405 * As a workaround, sending a single NUL byte hides the previous messages. */
406 (void) plymouth_send_msg("", /* pause_spinner= */ false);
407 }
408
409 static int fido2_use_hmac_hash_specific_token(
410 const char *path,
411 const char *rp_id,
412 const void *salt,
413 size_t salt_size,
414 const void *cid,
415 size_t cid_size,
416 char **pins,
417 Fido2EnrollFlags required, /* client pin/user presence required */
418 void **ret_hmac,
419 size_t *ret_hmac_size) {
420
421 _cleanup_(plymouth_end_interaction) bool plymouth_displayed = false;
422 _cleanup_(fido_assert_free_wrapper) fido_assert_t *a = NULL;
423 _cleanup_(fido_dev_free_wrapper) fido_dev_t *d = NULL;
424 _cleanup_(erase_and_freep) void *hmac_copy = NULL;
425 bool has_up, has_client_pin, has_uv;
426 size_t hmac_size;
427 const void *hmac;
428 int r;
429
430 assert(path);
431 assert(rp_id);
432 assert(salt);
433 assert(cid);
434 assert(ret_hmac);
435 assert(ret_hmac_size);
436
437 d = sym_fido_dev_new();
438 if (!d)
439 return log_oom();
440
441 r = sym_fido_dev_open(d, path);
442 if (r != FIDO_OK)
443 return log_error_errno(SYNTHETIC_ERRNO(EIO),
444 "Failed to open FIDO2 device %s: %s", path, sym_fido_strerr(r));
445
446 r = verify_features(d, path, LOG_ERR, /* ret_has_rk= */ NULL, &has_client_pin, &has_up, &has_uv, /* ret_has_always_uv= */ NULL);
447 if (r < 0)
448 return r;
449
450 if (!has_client_pin && FLAGS_SET(required, FIDO2ENROLL_PIN))
451 return log_error_errno(SYNTHETIC_ERRNO(EHWPOISON),
452 "PIN required to unlock, but FIDO2 device %s does not support it.",
453 path);
454
455 if (!has_up && FLAGS_SET(required, FIDO2ENROLL_UP))
456 return log_error_errno(SYNTHETIC_ERRNO(EHWPOISON),
457 "User presence test required to unlock, but FIDO2 device %s does not support it.",
458 path);
459
460 if (!has_uv && FLAGS_SET(required, FIDO2ENROLL_UV))
461 return log_error_errno(SYNTHETIC_ERRNO(EHWPOISON),
462 "User verification required to unlock, but FIDO2 device %s does not support it.",
463 path);
464
465 a = sym_fido_assert_new();
466 if (!a)
467 return log_oom();
468
469 r = sym_fido_assert_set_extensions(a, FIDO_EXT_HMAC_SECRET);
470 if (r != FIDO_OK)
471 return log_error_errno(SYNTHETIC_ERRNO(EIO),
472 "Failed to enable HMAC-SECRET extension on FIDO2 assertion: %s", sym_fido_strerr(r));
473
474 r = sym_fido_assert_set_hmac_salt(a, salt, salt_size);
475 if (r != FIDO_OK)
476 return log_error_errno(SYNTHETIC_ERRNO(EIO),
477 "Failed to set salt on FIDO2 assertion: %s", sym_fido_strerr(r));
478
479 r = fido2_assert_set_basic_properties(a, rp_id, cid, cid_size);
480 if (r < 0)
481 return r;
482
483 log_info("Asking FIDO2 token for authentication.");
484
485 if (has_up) {
486 r = sym_fido_assert_set_up(a, FLAGS_SET(required, FIDO2ENROLL_UP) ? FIDO_OPT_TRUE : FIDO_OPT_FALSE);
487 if (r != FIDO_OK)
488 return log_error_errno(SYNTHETIC_ERRNO(EIO),
489 "Failed to %s FIDO2 user presence test: %s",
490 enable_disable(FLAGS_SET(required, FIDO2ENROLL_UP)),
491 sym_fido_strerr(r));
492
493 if (FLAGS_SET(required, FIDO2ENROLL_UP)) {
494 log_notice("%s%sPlease confirm presence on security token to unlock.",
495 emoji_enabled() ? glyph(GLYPH_TOUCH) : "",
496 emoji_enabled() ? " " : "");
497 plymouth_start_interaction(_("Please confirm presence on security token to unlock."), &plymouth_displayed);
498 }
499 }
500
501 if (has_uv && !FLAGS_SET(required, FIDO2ENROLL_UV_OMIT)) {
502 r = sym_fido_assert_set_uv(a, FLAGS_SET(required, FIDO2ENROLL_UV) ? FIDO_OPT_TRUE : FIDO_OPT_FALSE);
503 if (r != FIDO_OK)
504 return log_error_errno(SYNTHETIC_ERRNO(EIO),
505 "Failed to %s FIDO2 user verification: %s",
506 enable_disable(FLAGS_SET(required, FIDO2ENROLL_UV)),
507 sym_fido_strerr(r));
508
509 if (FLAGS_SET(required, FIDO2ENROLL_UV)) {
510 log_notice("%s%sPlease verify user on security token to unlock.",
511 emoji_enabled() ? glyph(GLYPH_TOUCH) : "",
512 emoji_enabled() ? " " : "");
513 plymouth_start_interaction(_("Please verify user on security token to unlock."), &plymouth_displayed);
514 }
515 }
516
517 for (;;) {
518 bool retry_with_up = false, retry_with_pin = false;
519
520 if (FLAGS_SET(required, FIDO2ENROLL_PIN)) {
521 /* OK, we need a pin, try with all pins in turn */
522 if (strv_isempty(pins))
523 r = FIDO_ERR_PIN_REQUIRED;
524 else
525 STRV_FOREACH(i, pins) {
526 r = sym_fido_dev_get_assert(d, a, *i);
527 if (r != FIDO_ERR_PIN_INVALID)
528 break;
529 }
530
531 } else
532 r = sym_fido_dev_get_assert(d, a, NULL);
533
534 /* In some conditions, where a PIN or UP is required we might accept that. Let's check the
535 * conditions and if so try immediately again. */
536
537 switch (r) {
538
539 case FIDO_ERR_UP_REQUIRED:
540 /* So the token asked for "up". Try to turn it on, for compat with systemd 248 and try again. */
541
542 if (!has_up)
543 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
544 "Token asks for user presence test but doesn't advertise 'up' feature.");
545
546 if (FLAGS_SET(required, FIDO2ENROLL_UP))
547 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
548 "Token asks for user presence test but was already enabled.");
549
550 if (FLAGS_SET(required, FIDO2ENROLL_UP_IF_NEEDED)) {
551 log_notice("%s%sPlease confirm presence on security to unlock.",
552 emoji_enabled() ? glyph(GLYPH_TOUCH) : "",
553 emoji_enabled() ? " " : "");
554 plymouth_start_interaction(_("Please confirm presence on security token to unlock."), &plymouth_displayed);
555 retry_with_up = true;
556 }
557
558 break;
559
560 case FIDO_ERR_UNSUPPORTED_OPTION:
561 /* AuthenTrend ATKey.Pro returns this instead of FIDO_ERR_UP_REQUIRED, let's handle
562 * it gracefully (also see below.) */
563
564 if (has_up && (required & (FIDO2ENROLL_UP|FIDO2ENROLL_UP_IF_NEEDED)) == FIDO2ENROLL_UP_IF_NEEDED) {
565 log_notice("%s%sGot unsupported option error when user presence test is turned off. Trying with user presence test turned on.",
566 emoji_enabled() ? glyph(GLYPH_TOUCH) : "",
567 emoji_enabled() ? " " : "");
568 retry_with_up = true;
569 }
570
571 break;
572
573 case FIDO_ERR_PIN_REQUIRED:
574 /* A pin was requested. Maybe supply one, if we are configured to do so on request */
575
576 if (!has_client_pin)
577 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
578 "Token asks for PIN but doesn't advertise 'clientPin' feature.");
579
580 if (FLAGS_SET(required, FIDO2ENROLL_PIN) && !strv_isempty(pins))
581 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
582 "Token asks for PIN but one was already supplied.");
583
584 if ((required & (FIDO2ENROLL_PIN|FIDO2ENROLL_PIN_IF_NEEDED)) == FIDO2ENROLL_PIN_IF_NEEDED) {
585 /* If a PIN so far wasn't specified but is requested by the device, and
586 * FIDO2ENROLL_PIN_IF_NEEDED is set, then provide it */
587 log_debug("Retrying to create credential with PIN.");
588 retry_with_pin = true;
589 }
590
591 break;
592
593 default:
594 ;
595 }
596
597 if (!retry_with_up && !retry_with_pin)
598 break;
599
600 if (retry_with_up) {
601 r = sym_fido_assert_set_up(a, FIDO_OPT_TRUE);
602 if (r != FIDO_OK)
603 return log_error_errno(SYNTHETIC_ERRNO(EIO),
604 "Failed to enable FIDO2 user presence test: %s", sym_fido_strerr(r));
605
606 required |= FIDO2ENROLL_UP;
607 }
608
609 if (retry_with_pin)
610 required |= FIDO2ENROLL_PIN;
611 }
612
613 r = fido2_common_assert_error_handle(r);
614 if (r < 0)
615 return r;
616
617 hmac = sym_fido_assert_hmac_secret_ptr(a, 0);
618 if (!hmac)
619 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve HMAC secret.");
620
621 hmac_size = sym_fido_assert_hmac_secret_len(a, 0);
622
623 hmac_copy = memdup(hmac, hmac_size);
624 if (!hmac_copy)
625 return log_oom();
626
627 *ret_hmac = TAKE_PTR(hmac_copy);
628 *ret_hmac_size = hmac_size;
629 return 0;
630 }
631
632 /* COSE_ECDH_ES256 is not usable with fido_cred_set_type() thus it's not listed here. */
633 static const char *fido2_algorithm_to_string(int alg) {
634 switch (alg) {
635 case COSE_ES256:
636 return "es256";
637 case COSE_RS256:
638 return "rs256";
639 case COSE_EDDSA:
640 return "eddsa";
641 default:
642 return NULL;
643 }
644 }
645
646 int fido2_use_hmac_hash(
647 const char *device,
648 const char *rp_id,
649 const void *salt,
650 size_t salt_size,
651 const void *cid,
652 size_t cid_size,
653 char **pins,
654 Fido2EnrollFlags required, /* client pin/user presence required */
655 void **ret_hmac,
656 size_t *ret_hmac_size) {
657
658 size_t allocated = 64, found = 0;
659 fido_dev_info_t *di = NULL;
660 int r;
661
662 r = dlopen_libfido2(LOG_ERR);
663 if (r < 0)
664 return r;
665
666 if (device) {
667 r = fido2_is_cred_in_specific_token(device, rp_id, cid, cid_size, required);
668 if (r == 0)
669 /* The caller is expected to attempt other key slots in this case,
670 * therefore, do not spam the console with error logs here. */
671 return log_debug_errno(SYNTHETIC_ERRNO(EBADSLT),
672 "The credential is not in the token %s.", device);
673 if (r < 0)
674 return log_error_errno(r, "Token returned error during pre-flight: %m");
675
676 return fido2_use_hmac_hash_specific_token(device, rp_id, salt, salt_size, cid, cid_size, pins, required, ret_hmac, ret_hmac_size);
677 }
678
679 di = sym_fido_dev_info_new(allocated);
680 if (!di)
681 return log_oom();
682
683 r = sym_fido_dev_info_manifest(di, allocated, &found);
684 if (r == FIDO_ERR_INTERNAL) {
685 /* The library returns FIDO_ERR_INTERNAL when no devices are found. I wish it wouldn't. */
686 r = log_debug_errno(SYNTHETIC_ERRNO(EAGAIN), "Got FIDO_ERR_INTERNAL, assuming no devices.");
687 goto finish;
688 }
689 if (r != FIDO_OK) {
690 r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to enumerate FIDO2 devices: %s", sym_fido_strerr(r));
691 goto finish;
692 }
693
694 for (size_t i = 0; i < found; i++) {
695 const fido_dev_info_t *entry;
696 const char *path;
697
698 entry = sym_fido_dev_info_ptr(di, i);
699 if (!entry) {
700 r = log_error_errno(SYNTHETIC_ERRNO(EIO),
701 "Failed to get device information for FIDO device %zu.", i);
702 goto finish;
703 }
704
705 path = sym_fido_dev_info_path(entry);
706 if (!path) {
707 r = log_error_errno(SYNTHETIC_ERRNO(EIO),
708 "Failed to query FIDO device path.");
709 goto finish;
710 }
711
712 r = fido2_is_cred_in_specific_token(path, rp_id, cid, cid_size, required);
713 if (r < 0) {
714 log_error_errno(r, "Token returned error during pre-flight: %m");
715 goto finish;
716 }
717 if (r == 0) {
718 log_debug("The credential is not in the token %s, skipping.", path);
719 continue;
720 }
721
722 r = fido2_use_hmac_hash_specific_token(path, rp_id, salt, salt_size, cid, cid_size, pins, required, ret_hmac, ret_hmac_size);
723 if (!IN_SET(r,
724 -EBADSLT, /* device doesn't understand our credential hash */
725 -ENODEV /* device is not a FIDO2 device with HMAC-SECRET */))
726 goto finish;
727 }
728
729 r = -EAGAIN;
730
731 finish:
732 sym_fido_dev_info_free(&di, allocated);
733 return r;
734 }
735
736 int fido2_generate_hmac_hash(
737 const char *device,
738 const char *rp_id,
739 const char *rp_name,
740 const void *user_id, size_t user_id_len,
741 const char *user_name,
742 const char *user_display_name,
743 const char *user_icon,
744 const char *askpw_icon,
745 const char *askpw_credential,
746 Fido2EnrollFlags lock_with,
747 int cred_alg,
748 const struct iovec *salt,
749 void **ret_cid, size_t *ret_cid_size,
750 void **ret_secret, size_t *ret_secret_size,
751 char **ret_usedpin,
752 Fido2EnrollFlags *ret_locked_with) {
753
754 _cleanup_(erase_and_freep) void *secret_copy = NULL;
755 _cleanup_(fido_assert_free_wrapper) fido_assert_t *a = NULL;
756 _cleanup_(fido_cred_free_wrapper) fido_cred_t *c = NULL;
757 _cleanup_(fido_dev_free_wrapper) fido_dev_t *d = NULL;
758 _cleanup_(erase_and_freep) char *used_pin = NULL;
759 bool has_rk, has_client_pin, has_up, has_uv, has_always_uv;
760 _cleanup_free_ char *cid_copy = NULL;
761 size_t cid_size, secret_size;
762 const void *cid, *secret;
763 int r;
764
765 assert(device);
766 assert(ret_cid);
767 assert(ret_cid_size);
768 assert(ret_secret);
769 assert(ret_secret_size);
770
771 /* Construction is like this: we read or generate a salt of 32 bytes. We then ask the FIDO2 device to
772 * HMAC-SHA256 it for us with its internal key. The result is the key used by LUKS and account
773 * authentication. LUKS and UNIX password auth all do their own salting before hashing, so that FIDO2
774 * device never sees the volume key.
775 *
776 * S = HMAC-SHA256(I, D)
777 *
778 * with: S → LUKS/account authentication key (never stored)
779 * I → internal key on FIDO2 device (stored in the FIDO2 device)
780 * D → salt (stored in the privileged part of the JSON record or read from a file/socket)
781 *
782 */
783
784 assert(device);
785 assert((lock_with & ~(FIDO2ENROLL_PIN|FIDO2ENROLL_UP|FIDO2ENROLL_UV)) == 0);
786 assert(iovec_is_set(salt));
787
788 r = dlopen_libfido2(LOG_ERR);
789 if (r < 0)
790 return r;
791
792 d = sym_fido_dev_new();
793 if (!d)
794 return log_oom();
795
796 r = sym_fido_dev_open(d, device);
797 if (r != FIDO_OK)
798 return log_error_errno(SYNTHETIC_ERRNO(EIO),
799 "Failed to open FIDO2 device %s: %s", device, sym_fido_strerr(r));
800
801 r = verify_features(d, device, LOG_ERR, &has_rk, &has_client_pin, &has_up, &has_uv, &has_always_uv);
802 if (r < 0)
803 return r;
804
805 /* While enrolling degrade gracefully if the requested feature set isn't available, but let the user know */
806 if (!has_client_pin && FLAGS_SET(lock_with, FIDO2ENROLL_PIN)) {
807 log_notice("Requested to lock with PIN, but FIDO2 device %s does not support it, disabling.", device);
808 lock_with &= ~FIDO2ENROLL_PIN;
809 }
810
811 if (!has_up && FLAGS_SET(lock_with, FIDO2ENROLL_UP)) {
812 log_notice("Locking with user presence test requested, but FIDO2 device %s does not support it, disabling.", device);
813 lock_with &= ~FIDO2ENROLL_UP;
814 }
815
816 if (!has_uv && FLAGS_SET(lock_with, FIDO2ENROLL_UV)) {
817 log_notice("Locking with user verification test requested, but FIDO2 device %s does not support it, disabling.", device);
818 lock_with &= ~FIDO2ENROLL_UV;
819 }
820
821 if (has_always_uv && !(FLAGS_SET(lock_with, FIDO2ENROLL_PIN) || FLAGS_SET(lock_with, FIDO2ENROLL_UV))) {
822 if (has_uv) {
823 log_notice("FIDO2 device %s enforces 'always user verification', forcing user verification.", device);
824 lock_with |= FIDO2ENROLL_UV;
825 } else if (has_client_pin) {
826 log_notice("FIDO2 device %s enforces 'always user verification', but doesn't support user verification, forcing PIN.", device);
827 lock_with |= FIDO2ENROLL_PIN;
828 } else
829 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
830 "FIDO2 device %s enforces 'always user verification', but doesn't support user verification or PIN, cannot proceed.",
831 device);
832 }
833
834 c = sym_fido_cred_new();
835 if (!c)
836 return log_oom();
837
838 int extensions = FIDO_EXT_HMAC_SECRET;
839 if (FLAGS_SET(lock_with, FIDO2ENROLL_UV)) {
840 /* Attempt to use the "cred protect" extension, requiring user verification (UV) for this
841 * credential. If the authenticator doesn't support the extension, it will be ignored. */
842 extensions |= FIDO_EXT_CRED_PROTECT;
843
844 r = sym_fido_cred_set_prot(c, FIDO_CRED_PROT_UV_REQUIRED);
845 if (r != FIDO_OK)
846 log_warning("Failed to set protection level on FIDO2 credential, ignoring: %s", sym_fido_strerr(r));
847 }
848
849 r = sym_fido_cred_set_extensions(c, extensions);
850 if (r != FIDO_OK)
851 return log_error_errno(SYNTHETIC_ERRNO(EIO),
852 "Failed to enable extensions on FIDO2 credential: %s", sym_fido_strerr(r));
853
854 r = sym_fido_cred_set_rp(c, rp_id, rp_name);
855 if (r != FIDO_OK)
856 return log_error_errno(SYNTHETIC_ERRNO(EIO),
857 "Failed to set FIDO2 credential relying party ID/name: %s", sym_fido_strerr(r));
858
859 r = sym_fido_cred_set_type(c, cred_alg);
860 if (r != FIDO_OK)
861 return log_error_errno(SYNTHETIC_ERRNO(EIO),
862 "Failed to set FIDO2 credential type to %s: %s", fido2_algorithm_to_string(cred_alg), sym_fido_strerr(r));
863
864 r = sym_fido_cred_set_user(
865 c,
866 user_id, user_id_len,
867 user_name,
868 user_display_name,
869 user_icon);
870 if (r != FIDO_OK)
871 return log_error_errno(SYNTHETIC_ERRNO(EIO),
872 "Failed to set FIDO2 credential user data: %s", sym_fido_strerr(r));
873
874 r = sym_fido_cred_set_clientdata_hash(c, (const unsigned char[32]) {}, 32);
875 if (r != FIDO_OK)
876 return log_error_errno(SYNTHETIC_ERRNO(EIO),
877 "Failed to set FIDO2 client data hash: %s", sym_fido_strerr(r));
878
879 if (has_rk) {
880 r = sym_fido_cred_set_rk(c, FIDO_OPT_FALSE);
881 if (r != FIDO_OK)
882 return log_error_errno(SYNTHETIC_ERRNO(EIO),
883 "Failed to turn off FIDO2 resident key option of credential: %s", sym_fido_strerr(r));
884 }
885
886 if (has_uv) {
887 r = sym_fido_cred_set_uv(c, FIDO_OPT_FALSE);
888 if (r != FIDO_OK)
889 return log_error_errno(SYNTHETIC_ERRNO(EIO),
890 "Failed to turn off FIDO2 user verification option of credential: %s", sym_fido_strerr(r));
891 }
892
893 /* As per specification "up" is assumed to be implicit when making credentials, hence we don't
894 * explicitly enable/disable it here */
895
896 log_info("Initializing FIDO2 credential on security token.");
897
898 if (has_uv || has_up)
899 log_notice("%s%s(Hint: This might require confirmation of user presence on security token.)",
900 emoji_enabled() ? glyph(GLYPH_TOUCH) : "",
901 emoji_enabled() ? " " : "");
902
903 /* If we are using the user PIN, then we must pass that PIN to the get_assertion call below, or
904 * the authenticator will use the non-user-verification HMAC secret (which differs from the one when
905 * the PIN is passed).
906 *
907 * Rather than potentially trying and failing to create the credential, just collect the PIN first
908 * and then pass it to both the make_credential and the get_assertion operations. */
909 if (FLAGS_SET(lock_with, FIDO2ENROLL_PIN))
910 r = FIDO_ERR_PIN_REQUIRED;
911 else
912 r = sym_fido_dev_make_cred(d, c, NULL);
913
914 if (r == FIDO_ERR_PIN_REQUIRED) {
915
916 if (!has_client_pin)
917 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
918 "Token asks for PIN but doesn't advertise 'clientPin' feature.");
919
920 AskPasswordFlags askpw_flags = ASK_PASSWORD_ACCEPT_CACHED;
921
922 for (;;) {
923 _cleanup_strv_free_erase_ char **pin = NULL;
924 AskPasswordRequest req = {
925 .tty_fd = -EBADF,
926 .message = _("Please enter security token PIN:"),
927 .icon = askpw_icon,
928 .keyring = "fido2-pin",
929 .credential = askpw_credential,
930 .until = USEC_INFINITY,
931 .hup_fd = -EBADF,
932 };
933
934 r = ask_password_auto(&req, askpw_flags, &pin);
935 if (r < 0)
936 return log_error_errno(r, "Failed to acquire user PIN: %m");
937
938 askpw_flags &= ~ASK_PASSWORD_ACCEPT_CACHED;
939
940 r = FIDO_ERR_PIN_INVALID;
941 STRV_FOREACH(i, pin) {
942 if (isempty(*i)) {
943 log_notice("PIN may not be empty.");
944 continue;
945 }
946
947 r = sym_fido_dev_make_cred(d, c, *i);
948 if (r == FIDO_OK) {
949 used_pin = strdup(*i);
950 if (!used_pin)
951 return log_oom();
952 break;
953 }
954 if (r != FIDO_ERR_PIN_INVALID)
955 break;
956 }
957
958 if (r != FIDO_ERR_PIN_INVALID)
959 break;
960
961 log_notice("PIN incorrect, please try again.");
962 }
963 }
964 if (r == FIDO_ERR_PIN_AUTH_BLOCKED)
965 return log_notice_errno(SYNTHETIC_ERRNO(EPERM),
966 "Token PIN is currently blocked, please remove and reinsert token.");
967 if (r == FIDO_ERR_UV_BLOCKED)
968 return log_notice_errno(SYNTHETIC_ERRNO(EPERM),
969 "Token verification is currently blocked, please remove and reinsert token.");
970 if (r == FIDO_ERR_ACTION_TIMEOUT)
971 return log_error_errno(SYNTHETIC_ERRNO(ENOSTR),
972 "Token action timeout. (User didn't interact with token quickly enough.)");
973 if (r == FIDO_ERR_UNSUPPORTED_ALGORITHM)
974 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
975 "Token doesn't support credential algorithm %s.", fido2_algorithm_to_string(cred_alg));
976 if (r != FIDO_OK)
977 return log_error_errno(SYNTHETIC_ERRNO(EIO),
978 "Failed to generate FIDO2 credential: %s", sym_fido_strerr(r));
979
980 cid = sym_fido_cred_id_ptr(c);
981 if (!cid)
982 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to get FIDO2 credential ID.");
983
984 cid_size = sym_fido_cred_id_len(c);
985
986 a = sym_fido_assert_new();
987 if (!a)
988 return log_oom();
989
990 r = sym_fido_assert_set_extensions(a, FIDO_EXT_HMAC_SECRET);
991 if (r != FIDO_OK)
992 return log_error_errno(SYNTHETIC_ERRNO(EIO),
993 "Failed to enable HMAC-SECRET extension on FIDO2 assertion: %s", sym_fido_strerr(r));
994
995 r = sym_fido_assert_set_hmac_salt(a, salt->iov_base, salt->iov_len);
996 if (r != FIDO_OK)
997 return log_error_errno(SYNTHETIC_ERRNO(EIO),
998 "Failed to set salt on FIDO2 assertion: %s", sym_fido_strerr(r));
999
1000 r = fido2_assert_set_basic_properties(a, rp_id, cid, cid_size);
1001 if (r < 0)
1002 return r;
1003
1004 log_info("Generating secret key on FIDO2 security token.");
1005
1006 if (has_up) {
1007 r = sym_fido_assert_set_up(a, FLAGS_SET(lock_with, FIDO2ENROLL_UP) ? FIDO_OPT_TRUE : FIDO_OPT_FALSE);
1008 if (r != FIDO_OK)
1009 return log_error_errno(SYNTHETIC_ERRNO(EIO),
1010 "Failed to %s FIDO2 user presence test: %s",
1011 enable_disable(FLAGS_SET(lock_with, FIDO2ENROLL_UP)),
1012 sym_fido_strerr(r));
1013
1014 if (FLAGS_SET(lock_with, FIDO2ENROLL_UP))
1015 log_notice("%s%sIn order to allow secret key generation, please confirm presence on security token.",
1016 emoji_enabled() ? glyph(GLYPH_TOUCH) : "",
1017 emoji_enabled() ? " " : "");
1018 }
1019
1020 if (has_uv) {
1021 r = sym_fido_assert_set_uv(a, FLAGS_SET(lock_with, FIDO2ENROLL_UV) ? FIDO_OPT_TRUE : FIDO_OPT_FALSE);
1022 if (r != FIDO_OK)
1023 return log_error_errno(SYNTHETIC_ERRNO(EIO),
1024 "Failed to %s FIDO user verification: %s",
1025 enable_disable(FLAGS_SET(lock_with, FIDO2ENROLL_UV)),
1026 sym_fido_strerr(r));
1027
1028 if (FLAGS_SET(lock_with, FIDO2ENROLL_UV))
1029 log_notice("%s%sIn order to allow secret key generation, please verify user on security token.",
1030 emoji_enabled() ? glyph(GLYPH_TOUCH) : "",
1031 emoji_enabled() ? " " : "");
1032 }
1033
1034 for (;;) {
1035 bool retry_with_up = false, retry_with_pin = false;
1036
1037 r = sym_fido_dev_get_assert(d, a, FLAGS_SET(lock_with, FIDO2ENROLL_PIN) ? used_pin : NULL);
1038
1039 switch (r) {
1040
1041 case FIDO_ERR_UP_REQUIRED:
1042 /* If the token asks for "up" when we turn off, then this might be a feature that
1043 * isn't optional. Let's enable it */
1044
1045 if (!has_up)
1046 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1047 "Token asks for user presence test but doesn't advertise 'up' feature.");
1048
1049 if (FLAGS_SET(lock_with, FIDO2ENROLL_UP))
1050 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1051 "Token asks for user presence test but was already enabled.");
1052
1053 log_notice("%s%sLocking without user presence test requested, but FIDO2 device %s requires it, enabling.",
1054 emoji_enabled() ? glyph(GLYPH_TOUCH) : "",
1055 emoji_enabled() ? " " : "",
1056 device);
1057
1058 retry_with_up = true;
1059 break;
1060
1061 case FIDO_ERR_UNSUPPORTED_OPTION:
1062 /* AuthenTrend ATKey.Pro says it supports "up", but if we disable it it will fail
1063 * with FIDO_ERR_UNSUPPORTED_OPTION, probably because it isn't actually
1064 * optional. Let's see if turning it on works. This is very similar to the
1065 * FIDO_ERR_UP_REQUIRED case, but since the error is so vague we implement it
1066 * slightly more defensively. */
1067
1068 if (has_up && !FLAGS_SET(lock_with, FIDO2ENROLL_UP)) {
1069 log_notice("%s%sGot unsupported option error when user presence test is turned off. Trying with user presence test turned on.",
1070 emoji_enabled() ? glyph(GLYPH_TOUCH) : "",
1071 emoji_enabled() ? " " : "");
1072 retry_with_up = true;
1073 }
1074
1075 break;
1076
1077 case FIDO_ERR_PIN_REQUIRED:
1078 if (!has_client_pin)
1079 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1080 "Token asks for client PIN check but doesn't advertise 'clientPin' feature.");
1081
1082 if (FLAGS_SET(lock_with, FIDO2ENROLL_PIN))
1083 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1084 "Token asks for user client PIN check but was already enabled.");
1085
1086 log_debug("Token requires PIN for assertion, enabling.");
1087 retry_with_pin = true;
1088 break;
1089
1090 default:
1091 ;
1092 }
1093
1094 if (!retry_with_up && !retry_with_pin)
1095 break;
1096
1097 if (retry_with_up) {
1098 r = sym_fido_assert_set_up(a, FIDO_OPT_TRUE);
1099 if (r != FIDO_OK)
1100 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to enable FIDO2 user presence test: %s", sym_fido_strerr(r));
1101
1102 lock_with |= FIDO2ENROLL_UP;
1103 }
1104
1105 if (retry_with_pin)
1106 lock_with |= FIDO2ENROLL_PIN;
1107 }
1108
1109 if (r == FIDO_ERR_ACTION_TIMEOUT)
1110 return log_error_errno(SYNTHETIC_ERRNO(ENOSTR),
1111 "Token action timeout. (User didn't interact with token quickly enough.)");
1112 if (r != FIDO_OK)
1113 return log_error_errno(SYNTHETIC_ERRNO(EIO),
1114 "Failed to ask token for assertion: %s", sym_fido_strerr(r));
1115
1116 secret = sym_fido_assert_hmac_secret_ptr(a, 0);
1117 if (!secret)
1118 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve HMAC secret.");
1119
1120 secret_size = sym_fido_assert_hmac_secret_len(a, 0);
1121
1122 secret_copy = memdup(secret, secret_size);
1123 if (!secret_copy)
1124 return log_oom();
1125
1126 cid_copy = memdup(cid, cid_size);
1127 if (!cid_copy)
1128 return log_oom();
1129
1130 *ret_cid = TAKE_PTR(cid_copy);
1131 *ret_cid_size = cid_size;
1132 *ret_secret = TAKE_PTR(secret_copy);
1133 *ret_secret_size = secret_size;
1134
1135 if (ret_usedpin)
1136 *ret_usedpin = TAKE_PTR(used_pin);
1137
1138 if (ret_locked_with)
1139 *ret_locked_with = lock_with;
1140
1141 return 0;
1142 }
1143 #endif
1144
1145 #if HAVE_LIBFIDO2
1146 static int check_device_is_fido2_with_hmac_secret(
1147 const char *path,
1148 bool *ret_has_rk,
1149 bool *ret_has_client_pin,
1150 bool *ret_has_up,
1151 bool *ret_has_uv,
1152 bool *ret_has_always_uv) {
1153
1154 _cleanup_(fido_dev_free_wrapper) fido_dev_t *d = NULL;
1155 int r;
1156
1157 d = sym_fido_dev_new();
1158 if (!d)
1159 return log_oom();
1160
1161 r = sym_fido_dev_open(d, path);
1162 if (r != FIDO_OK)
1163 return log_error_errno(SYNTHETIC_ERRNO(EIO),
1164 "Failed to open FIDO2 device %s: %s", path, sym_fido_strerr(r));
1165
1166 r = verify_features(d, path, LOG_DEBUG, ret_has_rk, ret_has_client_pin, ret_has_up, ret_has_uv, ret_has_always_uv);
1167 if (r == -ENODEV) { /* Not a FIDO2 device, or not implementing 'hmac-secret' */
1168 if (ret_has_rk)
1169 *ret_has_rk = false;
1170 if (ret_has_client_pin)
1171 *ret_has_client_pin = false;
1172 if (ret_has_up)
1173 *ret_has_up = false;
1174 if (ret_has_uv)
1175 *ret_has_uv = false;
1176 if (ret_has_always_uv)
1177 *ret_has_always_uv = false;
1178 return false;
1179 }
1180 if (r < 0)
1181 return r;
1182
1183 return true;
1184 }
1185 #endif
1186
1187 int fido2_list_devices(void) {
1188 #if HAVE_LIBFIDO2
1189 _cleanup_(table_unrefp) Table *t = NULL;
1190
1191 size_t allocated = 64, found = 0;
1192 fido_dev_info_t *di = NULL;
1193 int r;
1194
1195 r = dlopen_libfido2(LOG_ERR);
1196 if (r < 0)
1197 return r;
1198
1199 di = sym_fido_dev_info_new(allocated);
1200 if (!di)
1201 return log_oom();
1202
1203 r = sym_fido_dev_info_manifest(di, allocated, &found);
1204 if (r == FIDO_ERR_INTERNAL || (r == FIDO_OK && found == 0)) {
1205 /* The library returns FIDO_ERR_INTERNAL when no devices are found. I wish it wouldn't. */
1206 log_info("No FIDO2 devices found.");
1207 r = 0;
1208 goto finish;
1209 }
1210 if (r != FIDO_OK) {
1211 r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to enumerate FIDO2 devices: %s", sym_fido_strerr(r));
1212 goto finish;
1213 }
1214
1215 t = table_new("path", "manufacturer", "product", "compatible", "rk", "clientpin", "up", "uv", "alwaysuv");
1216 if (!t) {
1217 r = log_oom();
1218 goto finish;
1219 }
1220
1221 for (size_t i = 0; i < found; i++) {
1222 const fido_dev_info_t *entry;
1223 bool has_rk, has_client_pin, has_up, has_uv, has_always_uv;
1224
1225 entry = sym_fido_dev_info_ptr(di, i);
1226 if (!entry) {
1227 r = log_error_errno(SYNTHETIC_ERRNO(EIO),
1228 "Failed to get device information for FIDO device %zu.", i);
1229 goto finish;
1230 }
1231
1232 r = check_device_is_fido2_with_hmac_secret(sym_fido_dev_info_path(entry), &has_rk, &has_client_pin, &has_up, &has_uv, &has_always_uv);
1233 if (r < 0)
1234 goto finish;
1235 bool compatible = r > 0;
1236
1237 r = table_add_many(
1238 t,
1239 TABLE_PATH, sym_fido_dev_info_path(entry),
1240 TABLE_STRING, sym_fido_dev_info_manufacturer_string(entry),
1241 TABLE_STRING, sym_fido_dev_info_product_string(entry),
1242 TABLE_BOOLEAN_CHECKMARK, compatible,
1243 TABLE_BOOLEAN_CHECKMARK, has_rk,
1244 TABLE_BOOLEAN_CHECKMARK, has_client_pin,
1245 TABLE_BOOLEAN_CHECKMARK, has_up,
1246 TABLE_BOOLEAN_CHECKMARK, has_uv,
1247 TABLE_BOOLEAN_CHECKMARK, has_always_uv);
1248 if (r < 0) {
1249 table_log_add_error(r);
1250 goto finish;
1251 }
1252 }
1253
1254 r = table_print_or_warn(t);
1255 if (r < 0)
1256 goto finish;
1257
1258 if (!table_isempty(t))
1259 printf("\n"
1260 "%1$sLegend: RK %2$s Resident key%3$s\n"
1261 "%1$s CLIENTPIN %2$s PIN request%3$s\n"
1262 "%1$s UP %2$s User presence%3$s\n"
1263 "%1$s UV %2$s User verification%3$s\n"
1264 "%1$s AlwaysUV %2$s User verification Required%3$s\n",
1265 ansi_grey(),
1266 glyph(GLYPH_ARROW_RIGHT),
1267 ansi_normal());
1268
1269 r = 0;
1270
1271 finish:
1272 sym_fido_dev_info_free(&di, allocated);
1273 return r;
1274 #else
1275 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
1276 "FIDO2 tokens not supported on this build.");
1277 #endif
1278 }
1279
1280 int fido2_find_device_auto(char **ret) {
1281 #if HAVE_LIBFIDO2
1282 _cleanup_free_ char *copy = NULL;
1283 size_t di_size = 64, found = 0;
1284 const fido_dev_info_t *entry;
1285 fido_dev_info_t *di = NULL;
1286 const char *path;
1287 int r;
1288
1289 r = dlopen_libfido2(LOG_ERR);
1290 if (r < 0)
1291 return r;
1292
1293 di = sym_fido_dev_info_new(di_size);
1294 if (!di)
1295 return log_oom();
1296
1297 r = sym_fido_dev_info_manifest(di, di_size, &found);
1298 if (r == FIDO_ERR_INTERNAL || (r == FIDO_OK && found == 0)) {
1299 /* The library returns FIDO_ERR_INTERNAL when no devices are found. I wish it wouldn't. */
1300 r = log_error_errno(SYNTHETIC_ERRNO(ENODEV), "No FIDO devices found.");
1301 goto finish;
1302 }
1303 if (r != FIDO_OK) {
1304 r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to enumerate FIDO devices: %s", sym_fido_strerr(r));
1305 goto finish;
1306 }
1307 if (found > 1) {
1308 r = log_error_errno(SYNTHETIC_ERRNO(ENOTUNIQ), "More than one FIDO device found.");
1309 goto finish;
1310 }
1311
1312 entry = sym_fido_dev_info_ptr(di, 0);
1313 if (!entry) {
1314 r = log_error_errno(SYNTHETIC_ERRNO(EIO),
1315 "Failed to get device information for FIDO device 0.");
1316 goto finish;
1317 }
1318
1319 r = check_device_is_fido2_with_hmac_secret(
1320 sym_fido_dev_info_path(entry),
1321 /* ret_has_rk= */ NULL,
1322 /* ret_has_client_pin= */ NULL,
1323 /* ret_has_up= */ NULL,
1324 /* ret_has_uv= */ NULL,
1325 /* ret_has_always_uv= */ NULL);
1326 if (r < 0)
1327 goto finish;
1328 if (!r) {
1329 r = log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "FIDO device discovered does not implement FIDO2 with 'hmac-secret' extension.");
1330 goto finish;
1331 }
1332
1333 path = sym_fido_dev_info_path(entry);
1334 if (!path) {
1335 r = log_error_errno(SYNTHETIC_ERRNO(EIO),
1336 "Failed to query FIDO device path.");
1337 goto finish;
1338 }
1339
1340 copy = strdup(path);
1341 if (!copy) {
1342 r = log_oom();
1343 goto finish;
1344 }
1345
1346 *ret = TAKE_PTR(copy);
1347 r = 0;
1348
1349 finish:
1350 sym_fido_dev_info_free(&di, di_size);
1351 return r;
1352 #else
1353 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
1354 "FIDO2 tokens not supported on this build.");
1355 #endif
1356 }
1357
1358 int fido2_have_device(const char *device) {
1359 #if HAVE_LIBFIDO2
1360 size_t allocated = 64, found = 0;
1361 fido_dev_info_t *di = NULL;
1362 int r;
1363
1364 /* Return == 0 if not devices are found, > 0 if at least one is found */
1365
1366 r = dlopen_libfido2(LOG_ERR);
1367 if (r < 0)
1368 return r;
1369
1370 if (device) {
1371 if (access(device, F_OK) < 0) {
1372 if (errno == ENOENT)
1373 return 0;
1374
1375 return log_error_errno(errno, "Failed to determine whether device '%s' exists: %m", device);
1376 }
1377
1378 return 1;
1379 }
1380
1381 di = sym_fido_dev_info_new(allocated);
1382 if (!di)
1383 return log_oom();
1384
1385 r = sym_fido_dev_info_manifest(di, allocated, &found);
1386 if (r == FIDO_ERR_INTERNAL) {
1387 /* The library returns FIDO_ERR_INTERNAL when no devices are found. I wish it wouldn't. */
1388 r = 0;
1389 goto finish;
1390 }
1391 if (r != FIDO_OK) {
1392 r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to enumerate FIDO2 devices: %s", sym_fido_strerr(r));
1393 goto finish;
1394 }
1395
1396 r = found;
1397
1398 finish:
1399 sym_fido_dev_info_free(&di, allocated);
1400 return r;
1401 #else
1402 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
1403 "FIDO2 tokens not supported on this build.");
1404 #endif
1405 }
1406
1407 #if HAVE_LIBFIDO2
1408 int parse_fido2_algorithm(const char *s, int *ret) {
1409 int a;
1410
1411 assert(s);
1412
1413 if (streq(s, "es256"))
1414 a = COSE_ES256;
1415 else if (streq(s, "rs256"))
1416 a = COSE_RS256;
1417 else if (streq(s, "eddsa"))
1418 a = COSE_EDDSA;
1419 else
1420 return -EINVAL;
1421
1422 if (ret)
1423 *ret = a;
1424 return 0;
1425 }
1426 #endif