Debian Codegraph @

src/u2f.c

raw
1 /*
2 * Copyright (c) 2018-2026 Yubico AB. All rights reserved.
3 * Use of this source code is governed by a BSD-style
4 * license that can be found in the LICENSE file.
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8 #include <openssl/sha.h>
9 #include <openssl/x509.h>
10
11 #ifdef HAVE_UNISTD_H
12 #include <unistd.h>
13 #endif
14 #include <errno.h>
15
16 #include "fido.h"
17 #include "fido/es256.h"
18 #include "fallthrough.h"
19
20 #define U2F_PACE_MS (100)
21 #define U2F_IO_MS (20)
22
23 #if defined(_MSC_VER)
24 static int
25 usleep(unsigned int usec)
26 {
27 Sleep(usec / 1000);
28
29 return (0);
30 }
31 #endif
32
33 static int
34 delay_ms(unsigned int ms, int *ms_remain)
35 {
36 if (*ms_remain > -1 && (unsigned int)*ms_remain < ms)
37 ms = (unsigned int)*ms_remain;
38
39 if (ms > UINT_MAX / 1000) {
40 fido_log_debug("%s: ms=%u", __func__, ms);
41 return (-1);
42 }
43
44 if (usleep(ms * 1000) < 0) {
45 fido_log_error(errno, "%s: usleep", __func__);
46 return (-1);
47 }
48
49 if (*ms_remain > -1)
50 *ms_remain -= (int)ms;
51
52 return (0);
53 }
54
55 static int
56 sig_get(fido_blob_t *sig, const unsigned char **buf, size_t *len)
57 {
58 sig->len = *len; /* consume the whole buffer */
59 if ((sig->ptr = calloc(1, sig->len)) == NULL ||
60 fido_buf_read(buf, len, sig->ptr, sig->len) < 0) {
61 fido_log_debug("%s: fido_buf_read", __func__);
62 fido_blob_reset(sig);
63 return (-1);
64 }
65
66 return (0);
67 }
68
69 static int
70 x5c_get(fido_blob_t *x5c, const unsigned char **buf, size_t *len)
71 {
72 X509 *cert = NULL;
73 int ok = -1;
74
75 if (*len > LONG_MAX) {
76 fido_log_debug("%s: invalid len %zu", __func__, *len);
77 goto fail;
78 }
79
80 /* find out the certificate's length */
81 const unsigned char *end = *buf;
82 if ((cert = d2i_X509(NULL, &end, (long)*len)) == NULL || end <= *buf ||
83 (x5c->len = (size_t)(end - *buf)) >= *len) {
84 fido_log_debug("%s: d2i_X509", __func__);
85 goto fail;
86 }
87
88 /* read accordingly */
89 if ((x5c->ptr = calloc(1, x5c->len)) == NULL ||
90 fido_buf_read(buf, len, x5c->ptr, x5c->len) < 0) {
91 fido_log_debug("%s: fido_buf_read", __func__);
92 goto fail;
93 }
94
95 ok = 0;
96 fail:
97 if (cert != NULL)
98 X509_free(cert);
99
100 if (ok < 0)
101 fido_blob_reset(x5c);
102
103 return (ok);
104 }
105
106 static int
107 authdata_fake(const char *rp_id, uint8_t flags, uint32_t sigcount,
108 fido_blob_t *fake_cbor_ad)
109 {
110 fido_authdata_t ad;
111 cbor_item_t *item = NULL;
112 size_t alloc_len;
113
114 memset(&ad, 0, sizeof(ad));
115
116 if (SHA256((const void *)rp_id, strlen(rp_id),
117 ad.rp_id_hash) != ad.rp_id_hash) {
118 fido_log_debug("%s: sha256", __func__);
119 return (-1);
120 }
121
122 ad.flags = flags; /* XXX translate? */
123 ad.sigcount = sigcount;
124
125 if ((item = cbor_build_bytestring((const unsigned char *)&ad,
126 sizeof(ad))) == NULL) {
127 fido_log_debug("%s: cbor_build_bytestring", __func__);
128 return (-1);
129 }
130
131 if (fake_cbor_ad->ptr != NULL ||
132 (fake_cbor_ad->len = cbor_serialize_alloc(item, &fake_cbor_ad->ptr,
133 &alloc_len)) == 0) {
134 fido_log_debug("%s: cbor_serialize_alloc", __func__);
135 cbor_decref(&item);
136 return (-1);
137 }
138
139 cbor_decref(&item);
140
141 return (0);
142 }
143
144 static int
145 check_io_timeout(int ms)
146 {
147 if (ms < 0 || ms >= U2F_IO_MS)
148 return FIDO_OK;
149
150 return FIDO_ERR_TIMEOUT;
151 }
152
153 /* TODO: use u2f_get_touch_begin & u2f_get_touch_status instead */
154 static int
155 send_dummy_register(fido_dev_t *dev, int *ms)
156 {
157 iso7816_apdu_t *apdu = NULL;
158 unsigned char *reply = NULL;
159 unsigned char challenge[SHA256_DIGEST_LENGTH];
160 unsigned char application[SHA256_DIGEST_LENGTH];
161 int r;
162
163 /* dummy challenge & application */
164 memset(&challenge, 0xff, sizeof(challenge));
165 memset(&application, 0xff, sizeof(application));
166
167 if ((apdu = iso7816_new(0, U2F_CMD_REGISTER, 0, 2 *
168 SHA256_DIGEST_LENGTH)) == NULL ||
169 iso7816_add(apdu, &challenge, sizeof(challenge)) < 0 ||
170 iso7816_add(apdu, &application, sizeof(application)) < 0) {
171 fido_log_debug("%s: iso7816", __func__);
172 r = FIDO_ERR_INTERNAL;
173 goto fail;
174 }
175
176 if ((reply = malloc(FIDO_MAXMSG)) == NULL) {
177 fido_log_debug("%s: malloc", __func__);
178 r = FIDO_ERR_INTERNAL;
179 goto fail;
180 }
181
182 do {
183 if ((r = check_io_timeout(*ms)) != FIDO_OK) {
184 fido_log_debug("%s: check_io_timeout", __func__);
185 goto fail;
186 }
187 if (fido_tx(dev, CTAP_CMD_MSG, iso7816_ptr(apdu),
188 iso7816_len(apdu), ms) < 0) {
189 fido_log_debug("%s: fido_tx", __func__);
190 r = FIDO_ERR_TX;
191 goto fail;
192 }
193 if (fido_rx(dev, CTAP_CMD_MSG, reply, FIDO_MAXMSG, ms) < 2) {
194 fido_log_debug("%s: fido_rx", __func__);
195 r = FIDO_ERR_RX;
196 goto fail;
197 }
198 if (delay_ms(U2F_PACE_MS, ms) != 0) {
199 fido_log_debug("%s: delay_ms", __func__);
200 r = FIDO_ERR_RX;
201 goto fail;
202 }
203 } while (((reply[0] << 8) | reply[1]) == SW_CONDITIONS_NOT_SATISFIED);
204
205 r = FIDO_OK;
206 fail:
207 iso7816_free(&apdu);
208 freezero(reply, FIDO_MAXMSG);
209
210 return (r);
211 }
212
213 static int
214 key_lookup(fido_dev_t *dev, const char *rp_id, const fido_blob_t *key_id,
215 int *found, int *ms)
216 {
217 iso7816_apdu_t *apdu = NULL;
218 unsigned char *reply = NULL;
219 unsigned char challenge[SHA256_DIGEST_LENGTH];
220 unsigned char rp_id_hash[SHA256_DIGEST_LENGTH];
221 uint8_t key_id_len;
222 int r;
223
224 if (key_id->len > UINT8_MAX || rp_id == NULL) {
225 fido_log_debug("%s: key_id->len=%zu, rp_id=%p", __func__,
226 key_id->len, (const void *)rp_id);
227 r = FIDO_ERR_INVALID_ARGUMENT;
228 goto fail;
229 }
230
231 memset(&challenge, 0xff, sizeof(challenge));
232 memset(&rp_id_hash, 0, sizeof(rp_id_hash));
233
234 if (SHA256((const void *)rp_id, strlen(rp_id),
235 rp_id_hash) != rp_id_hash) {
236 fido_log_debug("%s: sha256", __func__);
237 r = FIDO_ERR_INTERNAL;
238 goto fail;
239 }
240
241 key_id_len = (uint8_t)key_id->len;
242
243 if ((apdu = iso7816_new(0, U2F_CMD_AUTH, U2F_AUTH_CHECK, (uint16_t)(2 *
244 SHA256_DIGEST_LENGTH + sizeof(key_id_len) + key_id_len))) == NULL ||
245 iso7816_add(apdu, &challenge, sizeof(challenge)) < 0 ||
246 iso7816_add(apdu, &rp_id_hash, sizeof(rp_id_hash)) < 0 ||
247 iso7816_add(apdu, &key_id_len, sizeof(key_id_len)) < 0 ||
248 iso7816_add(apdu, key_id->ptr, key_id_len) < 0) {
249 fido_log_debug("%s: iso7816", __func__);
250 r = FIDO_ERR_INTERNAL;
251 goto fail;
252 }
253
254 if ((reply = malloc(FIDO_MAXMSG)) == NULL) {
255 fido_log_debug("%s: malloc", __func__);
256 r = FIDO_ERR_INTERNAL;
257 goto fail;
258 }
259
260 if ((r = check_io_timeout(*ms)) != FIDO_OK) {
261 fido_log_debug("%s: check_io_timeout", __func__);
262 goto fail;
263 }
264 if (fido_tx(dev, CTAP_CMD_MSG, iso7816_ptr(apdu),
265 iso7816_len(apdu), ms) < 0) {
266 fido_log_debug("%s: fido_tx", __func__);
267 r = FIDO_ERR_TX;
268 goto fail;
269 }
270 if (fido_rx(dev, CTAP_CMD_MSG, reply, FIDO_MAXMSG, ms) != 2) {
271 fido_log_debug("%s: fido_rx", __func__);
272 r = FIDO_ERR_RX;
273 goto fail;
274 }
275
276 switch ((reply[0] << 8) | reply[1]) {
277 case SW_CONDITIONS_NOT_SATISFIED:
278 *found = 1; /* key exists */
279 break;
280 case SW_WRONG_DATA:
281 case SW_WRONG_LENGTH:
282 *found = 0; /* key does not exist */
283 break;
284 default:
285 /* unexpected sw */
286 r = FIDO_ERR_INTERNAL;
287 goto fail;
288 }
289
290 r = FIDO_OK;
291 fail:
292 iso7816_free(&apdu);
293 freezero(reply, FIDO_MAXMSG);
294
295 return (r);
296 }
297
298 static int
299 parse_auth_reply(fido_blob_t *sig, fido_blob_t *ad, const char *rp_id,
300 const unsigned char *reply, size_t len)
301 {
302 uint8_t flags;
303 uint32_t sigcount;
304
305 if (len < 2 || ((reply[len - 2] << 8) | reply[len - 1]) != SW_NO_ERROR) {
306 fido_log_debug("%s: unexpected sw", __func__);
307 return (FIDO_ERR_RX);
308 }
309
310 len -= 2;
311
312 if (fido_buf_read(&reply, &len, &flags, sizeof(flags)) < 0 ||
313 fido_buf_read(&reply, &len, &sigcount, sizeof(sigcount)) < 0) {
314 fido_log_debug("%s: fido_buf_read", __func__);
315 return (FIDO_ERR_RX);
316 }
317
318 if (sig_get(sig, &reply, &len) < 0) {
319 fido_log_debug("%s: sig_get", __func__);
320 return (FIDO_ERR_RX);
321 }
322
323 if (authdata_fake(rp_id, flags, sigcount, ad) < 0) {
324 fido_log_debug("%s; authdata_fake", __func__);
325 return (FIDO_ERR_RX);
326 }
327
328 return (FIDO_OK);
329 }
330
331 static int
332 do_auth(fido_dev_t *dev, const fido_blob_t *cdh, const char *rp_id,
333 const fido_blob_t *key_id, fido_blob_t *sig, fido_blob_t *ad, int *ms)
334 {
335 iso7816_apdu_t *apdu = NULL;
336 unsigned char *reply = NULL;
337 unsigned char rp_id_hash[SHA256_DIGEST_LENGTH];
338 int reply_len;
339 uint8_t key_id_len;
340 int r;
341
342 #ifdef FIDO_FUZZ
343 *ms = 0; /* XXX */
344 #endif
345
346 if (cdh->len != SHA256_DIGEST_LENGTH || key_id->len > UINT8_MAX ||
347 rp_id == NULL) {
348 r = FIDO_ERR_INVALID_ARGUMENT;
349 goto fail;
350 }
351
352 memset(&rp_id_hash, 0, sizeof(rp_id_hash));
353
354 if (SHA256((const void *)rp_id, strlen(rp_id),
355 rp_id_hash) != rp_id_hash) {
356 fido_log_debug("%s: sha256", __func__);
357 r = FIDO_ERR_INTERNAL;
358 goto fail;
359 }
360
361 key_id_len = (uint8_t)key_id->len;
362
363 if ((apdu = iso7816_new(0, U2F_CMD_AUTH, U2F_AUTH_SIGN, (uint16_t)(2 *
364 SHA256_DIGEST_LENGTH + sizeof(key_id_len) + key_id_len))) == NULL ||
365 iso7816_add(apdu, cdh->ptr, cdh->len) < 0 ||
366 iso7816_add(apdu, &rp_id_hash, sizeof(rp_id_hash)) < 0 ||
367 iso7816_add(apdu, &key_id_len, sizeof(key_id_len)) < 0 ||
368 iso7816_add(apdu, key_id->ptr, key_id_len) < 0) {
369 fido_log_debug("%s: iso7816", __func__);
370 r = FIDO_ERR_INTERNAL;
371 goto fail;
372 }
373
374 if ((reply = malloc(FIDO_MAXMSG)) == NULL) {
375 fido_log_debug("%s: malloc", __func__);
376 r = FIDO_ERR_INTERNAL;
377 goto fail;
378 }
379
380 do {
381 if ((r = check_io_timeout(*ms)) != FIDO_OK) {
382 fido_log_debug("%s: check_io_timeout", __func__);
383 goto fail;
384 }
385 if (fido_tx(dev, CTAP_CMD_MSG, iso7816_ptr(apdu),
386 iso7816_len(apdu), ms) < 0) {
387 fido_log_debug("%s: fido_tx", __func__);
388 r = FIDO_ERR_TX;
389 goto fail;
390 }
391 if ((reply_len = fido_rx(dev, CTAP_CMD_MSG, reply,
392 FIDO_MAXMSG, ms)) < 2) {
393 fido_log_debug("%s: fido_rx", __func__);
394 r = FIDO_ERR_RX;
395 goto fail;
396 }
397 if (delay_ms(U2F_PACE_MS, ms) != 0) {
398 fido_log_debug("%s: delay_ms", __func__);
399 r = FIDO_ERR_RX;
400 goto fail;
401 }
402 } while (((reply[0] << 8) | reply[1]) == SW_CONDITIONS_NOT_SATISFIED);
403
404 if ((r = parse_auth_reply(sig, ad, rp_id, reply,
405 (size_t)reply_len)) != FIDO_OK) {
406 fido_log_debug("%s: parse_auth_reply", __func__);
407 goto fail;
408 }
409
410 fail:
411 iso7816_free(&apdu);
412 freezero(reply, FIDO_MAXMSG);
413
414 return (r);
415 }
416
417 static int
418 cbor_blob_from_ec_point(const uint8_t *ec_point, size_t ec_point_len,
419 fido_blob_t *cbor_blob)
420 {
421 es256_pk_t *pk = NULL;
422 cbor_item_t *pk_cbor = NULL;
423 size_t alloc_len;
424 int ok = -1;
425
426 /* only handle uncompressed points */
427 if (ec_point_len != 65 || ec_point[0] != 0x04) {
428 fido_log_debug("%s: unexpected format", __func__);
429 goto fail;
430 }
431
432 if ((pk = es256_pk_new()) == NULL ||
433 es256_pk_set_x(pk, &ec_point[1]) < 0 ||
434 es256_pk_set_y(pk, &ec_point[33]) < 0) {
435 fido_log_debug("%s: es256_pk_set", __func__);
436 goto fail;
437 }
438
439 if ((pk_cbor = es256_pk_encode(pk, 0)) == NULL) {
440 fido_log_debug("%s: es256_pk_encode", __func__);
441 goto fail;
442 }
443
444 if ((cbor_blob->len = cbor_serialize_alloc(pk_cbor, &cbor_blob->ptr,
445 &alloc_len)) != 77) {
446 fido_log_debug("%s: cbor_serialize_alloc", __func__);
447 goto fail;
448 }
449
450 ok = 0;
451 fail:
452 es256_pk_free(&pk);
453
454 if (pk_cbor)
455 cbor_decref(&pk_cbor);
456
457 return (ok);
458 }
459
460 static int
461 encode_cred_attstmt(int cose_alg, const fido_blob_t *x5c,
462 const fido_blob_t *sig, fido_blob_t *out)
463 {
464 cbor_item_t *item = NULL;
465 cbor_item_t *x5c_cbor = NULL;
466 const uint8_t alg_cbor = (uint8_t)(-cose_alg - 1);
467 struct cbor_pair kv[3];
468 size_t alloc_len;
469 int ok = -1;
470
471 memset(&kv, 0, sizeof(kv));
472 memset(out, 0, sizeof(*out));
473
474 if ((item = cbor_new_definite_map(3)) == NULL) {
475 fido_log_debug("%s: cbor_new_definite_map", __func__);
476 goto fail;
477 }
478
479 if ((kv[0].key = cbor_build_string("alg")) == NULL ||
480 (kv[0].value = cbor_build_negint8(alg_cbor)) == NULL ||
481 !cbor_map_add(item, kv[0])) {
482 fido_log_debug("%s: alg", __func__);
483 goto fail;
484 }
485
486 if ((kv[1].key = cbor_build_string("sig")) == NULL ||
487 (kv[1].value = fido_blob_encode(sig)) == NULL ||
488 !cbor_map_add(item, kv[1])) {
489 fido_log_debug("%s: sig", __func__);
490 goto fail;
491 }
492
493 if ((kv[2].key = cbor_build_string("x5c")) == NULL ||
494 (kv[2].value = cbor_new_definite_array(1)) == NULL ||
495 (x5c_cbor = fido_blob_encode(x5c)) == NULL ||
496 !cbor_array_push(kv[2].value, x5c_cbor) ||
497 !cbor_map_add(item, kv[2])) {
498 fido_log_debug("%s: x5c", __func__);
499 goto fail;
500 }
501
502 if ((out->len = cbor_serialize_alloc(item, &out->ptr,
503 &alloc_len)) == 0) {
504 fido_log_debug("%s: cbor_serialize_alloc", __func__);
505 goto fail;
506 }
507
508 ok = 0;
509 fail:
510 if (item != NULL)
511 cbor_decref(&item);
512 if (x5c_cbor != NULL)
513 cbor_decref(&x5c_cbor);
514
515 for (size_t i = 0; i < nitems(kv); i++) {
516 if (kv[i].key)
517 cbor_decref(&kv[i].key);
518 if (kv[i].value)
519 cbor_decref(&kv[i].value);
520 }
521
522 return (ok);
523 }
524
525 static int
526 encode_cred_authdata(const char *rp_id, const uint8_t *kh, uint8_t kh_len,
527 const uint8_t *pubkey, size_t pubkey_len, fido_blob_t *out)
528 {
529 fido_authdata_t authdata;
530 fido_attcred_raw_t attcred_raw;
531 fido_blob_t pk_blob;
532 fido_blob_t authdata_blob;
533 cbor_item_t *authdata_cbor = NULL;
534 unsigned char *ptr;
535 size_t len;
536 size_t alloc_len;
537 int ok = -1;
538
539 memset(&pk_blob, 0, sizeof(pk_blob));
540 memset(&authdata, 0, sizeof(authdata));
541 memset(&authdata_blob, 0, sizeof(authdata_blob));
542 memset(out, 0, sizeof(*out));
543
544 if (rp_id == NULL) {
545 fido_log_debug("%s: NULL rp_id", __func__);
546 goto fail;
547 }
548
549 if (cbor_blob_from_ec_point(pubkey, pubkey_len, &pk_blob) < 0) {
550 fido_log_debug("%s: cbor_blob_from_ec_point", __func__);
551 goto fail;
552 }
553
554 if (SHA256((const void *)rp_id, strlen(rp_id),
555 authdata.rp_id_hash) != authdata.rp_id_hash) {
556 fido_log_debug("%s: sha256", __func__);
557 goto fail;
558 }
559
560 authdata.flags = (CTAP_AUTHDATA_ATT_CRED | CTAP_AUTHDATA_USER_PRESENT);
561 authdata.sigcount = 0;
562
563 memset(&attcred_raw.aaguid, 0, sizeof(attcred_raw.aaguid));
564 attcred_raw.id_len = htobe16(kh_len);
565
566 len = authdata_blob.len = sizeof(authdata) + sizeof(attcred_raw) +
567 kh_len + pk_blob.len;
568 ptr = authdata_blob.ptr = calloc(1, authdata_blob.len);
569
570 fido_log_debug("%s: ptr=%p, len=%zu", __func__, (void *)ptr, len);
571
572 if (authdata_blob.ptr == NULL)
573 goto fail;
574
575 if (fido_buf_write(&ptr, &len, &authdata, sizeof(authdata)) < 0 ||
576 fido_buf_write(&ptr, &len, &attcred_raw, sizeof(attcred_raw)) < 0 ||
577 fido_buf_write(&ptr, &len, kh, kh_len) < 0 ||
578 fido_buf_write(&ptr, &len, pk_blob.ptr, pk_blob.len) < 0) {
579 fido_log_debug("%s: fido_buf_write", __func__);
580 goto fail;
581 }
582
583 if ((authdata_cbor = fido_blob_encode(&authdata_blob)) == NULL) {
584 fido_log_debug("%s: fido_blob_encode", __func__);
585 goto fail;
586 }
587
588 if ((out->len = cbor_serialize_alloc(authdata_cbor, &out->ptr,
589 &alloc_len)) == 0) {
590 fido_log_debug("%s: cbor_serialize_alloc", __func__);
591 goto fail;
592 }
593
594 ok = 0;
595 fail:
596 if (authdata_cbor)
597 cbor_decref(&authdata_cbor);
598
599 fido_blob_reset(&pk_blob);
600 fido_blob_reset(&authdata_blob);
601
602 return (ok);
603 }
604
605 static int
606 parse_register_reply(fido_cred_t *cred, const unsigned char *reply, size_t len)
607 {
608 fido_blob_t x5c;
609 fido_blob_t sig;
610 fido_blob_t ad;
611 fido_blob_t stmt;
612 uint8_t dummy;
613 uint8_t pubkey[65];
614 uint8_t kh_len = 0;
615 uint8_t *kh = NULL;
616 int r;
617
618 memset(&x5c, 0, sizeof(x5c));
619 memset(&sig, 0, sizeof(sig));
620 memset(&ad, 0, sizeof(ad));
621 memset(&stmt, 0, sizeof(stmt));
622 r = FIDO_ERR_RX;
623
624 /* status word */
625 if (len < 2 || ((reply[len - 2] << 8) | reply[len - 1]) != SW_NO_ERROR) {
626 fido_log_debug("%s: unexpected sw", __func__);
627 goto fail;
628 }
629
630 len -= 2;
631
632 /* reserved byte */
633 if (fido_buf_read(&reply, &len, &dummy, sizeof(dummy)) < 0 ||
634 dummy != 0x05) {
635 fido_log_debug("%s: reserved byte", __func__);
636 goto fail;
637 }
638
639 /* pubkey + key handle */
640 if (fido_buf_read(&reply, &len, &pubkey, sizeof(pubkey)) < 0 ||
641 fido_buf_read(&reply, &len, &kh_len, sizeof(kh_len)) < 0 ||
642 (kh = calloc(1, kh_len)) == NULL ||
643 fido_buf_read(&reply, &len, kh, kh_len) < 0) {
644 fido_log_debug("%s: fido_buf_read", __func__);
645 goto fail;
646 }
647
648 /* x5c + sig */
649 if (x5c_get(&x5c, &reply, &len) < 0 ||
650 sig_get(&sig, &reply, &len) < 0) {
651 fido_log_debug("%s: x5c || sig", __func__);
652 goto fail;
653 }
654
655 /* attstmt */
656 if (encode_cred_attstmt(COSE_ES256, &x5c, &sig, &stmt) < 0) {
657 fido_log_debug("%s: encode_cred_attstmt", __func__);
658 goto fail;
659 }
660
661 /* authdata */
662 if (encode_cred_authdata(cred->rp.id, kh, kh_len, pubkey,
663 sizeof(pubkey), &ad) < 0) {
664 fido_log_debug("%s: encode_cred_authdata", __func__);
665 goto fail;
666 }
667
668 if (fido_cred_set_fmt(cred, "fido-u2f") != FIDO_OK ||
669 fido_cred_set_authdata(cred, ad.ptr, ad.len) != FIDO_OK ||
670 fido_cred_set_attstmt(cred, stmt.ptr, stmt.len) != FIDO_OK) {
671 fido_log_debug("%s: fido_cred_set", __func__);
672 r = FIDO_ERR_INTERNAL;
673 goto fail;
674 }
675
676 r = FIDO_OK;
677 fail:
678 freezero(kh, kh_len);
679 fido_blob_reset(&x5c);
680 fido_blob_reset(&sig);
681 fido_blob_reset(&ad);
682 fido_blob_reset(&stmt);
683
684 return (r);
685 }
686
687 int
688 u2f_register(fido_dev_t *dev, fido_cred_t *cred, int *ms)
689 {
690 iso7816_apdu_t *apdu = NULL;
691 unsigned char rp_id_hash[SHA256_DIGEST_LENGTH];
692 unsigned char *reply = NULL;
693 int reply_len;
694 int found;
695 int r;
696
697 if (cred->rk == FIDO_OPT_TRUE || cred->uv == FIDO_OPT_TRUE) {
698 fido_log_debug("%s: rk=%d, uv=%d", __func__, cred->rk,
699 cred->uv);
700 return (FIDO_ERR_UNSUPPORTED_OPTION);
701 }
702
703 if (cred->type != COSE_ES256 || cred->cdh.ptr == NULL ||
704 cred->rp.id == NULL || cred->cdh.len != SHA256_DIGEST_LENGTH) {
705 fido_log_debug("%s: type=%d, cdh=(%p,%zu)" , __func__,
706 cred->type, (void *)cred->cdh.ptr, cred->cdh.len);
707 return (FIDO_ERR_INVALID_ARGUMENT);
708 }
709
710 for (size_t i = 0; i < cred->excl.len; i++) {
711 if ((r = key_lookup(dev, cred->rp.id, &cred->excl.ptr[i],
712 &found, ms)) != FIDO_OK) {
713 fido_log_debug("%s: key_lookup", __func__);
714 return (r);
715 }
716 if (found) {
717 if ((r = send_dummy_register(dev, ms)) != FIDO_OK) {
718 fido_log_debug("%s: send_dummy_register",
719 __func__);
720 return (r);
721 }
722 return (FIDO_ERR_CREDENTIAL_EXCLUDED);
723 }
724 }
725
726 memset(&rp_id_hash, 0, sizeof(rp_id_hash));
727
728 if (SHA256((const void *)cred->rp.id, strlen(cred->rp.id),
729 rp_id_hash) != rp_id_hash) {
730 fido_log_debug("%s: sha256", __func__);
731 return (FIDO_ERR_INTERNAL);
732 }
733
734 if ((apdu = iso7816_new(0, U2F_CMD_REGISTER, 0, 2 *
735 SHA256_DIGEST_LENGTH)) == NULL ||
736 iso7816_add(apdu, cred->cdh.ptr, cred->cdh.len) < 0 ||
737 iso7816_add(apdu, rp_id_hash, sizeof(rp_id_hash)) < 0) {
738 fido_log_debug("%s: iso7816", __func__);
739 r = FIDO_ERR_INTERNAL;
740 goto fail;
741 }
742
743 if ((reply = malloc(FIDO_MAXMSG)) == NULL) {
744 fido_log_debug("%s: malloc", __func__);
745 r = FIDO_ERR_INTERNAL;
746 goto fail;
747 }
748
749 do {
750 if ((r = check_io_timeout(*ms)) != FIDO_OK) {
751 fido_log_debug("%s: check_io_timeout", __func__);
752 goto fail;
753 }
754 if (fido_tx(dev, CTAP_CMD_MSG, iso7816_ptr(apdu),
755 iso7816_len(apdu), ms) < 0) {
756 fido_log_debug("%s: fido_tx", __func__);
757 r = FIDO_ERR_TX;
758 goto fail;
759 }
760 if ((reply_len = fido_rx(dev, CTAP_CMD_MSG, reply,
761 FIDO_MAXMSG, ms)) < 2) {
762 fido_log_debug("%s: fido_rx", __func__);
763 r = FIDO_ERR_RX;
764 goto fail;
765 }
766 if (delay_ms(U2F_PACE_MS, ms) != 0) {
767 fido_log_debug("%s: delay_ms", __func__);
768 r = FIDO_ERR_RX;
769 goto fail;
770 }
771 } while (((reply[0] << 8) | reply[1]) == SW_CONDITIONS_NOT_SATISFIED);
772
773 if ((r = parse_register_reply(cred, reply,
774 (size_t)reply_len)) != FIDO_OK) {
775 fido_log_debug("%s: parse_register_reply", __func__);
776 goto fail;
777 }
778 fail:
779 iso7816_free(&apdu);
780 freezero(reply, FIDO_MAXMSG);
781
782 return (r);
783 }
784
785 static int
786 u2f_authenticate_single(fido_dev_t *dev, const fido_blob_t *key_id,
787 fido_assert_t *fa, size_t idx, int *ms)
788 {
789 fido_blob_t sig;
790 fido_blob_t ad;
791 int found;
792 int r;
793
794 memset(&sig, 0, sizeof(sig));
795 memset(&ad, 0, sizeof(ad));
796
797 if ((r = key_lookup(dev, fa->rp_id, key_id, &found, ms)) != FIDO_OK) {
798 fido_log_debug("%s: key_lookup", __func__);
799 goto fail;
800 }
801
802 if (!found) {
803 fido_log_debug("%s: not found", __func__);
804 r = FIDO_ERR_CREDENTIAL_EXCLUDED;
805 goto fail;
806 }
807
808 if (fido_blob_set(&fa->stmt[idx].id, key_id->ptr, key_id->len) < 0) {
809 fido_log_debug("%s: fido_blob_set", __func__);
810 r = FIDO_ERR_INTERNAL;
811 goto fail;
812 }
813
814 if (fa->up == FIDO_OPT_FALSE) {
815 fido_log_debug("%s: checking for key existence only", __func__);
816 r = FIDO_ERR_USER_PRESENCE_REQUIRED;
817 goto fail;
818 }
819
820 if ((r = do_auth(dev, &fa->cdh, fa->rp_id, key_id, &sig, &ad,
821 ms)) != FIDO_OK) {
822 fido_log_debug("%s: do_auth", __func__);
823 goto fail;
824 }
825
826 if (fido_assert_set_authdata(fa, idx, ad.ptr, ad.len) != FIDO_OK ||
827 fido_assert_set_sig(fa, idx, sig.ptr, sig.len) != FIDO_OK) {
828 fido_log_debug("%s: fido_assert_set", __func__);
829 r = FIDO_ERR_INTERNAL;
830 goto fail;
831 }
832
833 r = FIDO_OK;
834 fail:
835 fido_blob_reset(&sig);
836 fido_blob_reset(&ad);
837
838 return (r);
839 }
840
841 int
842 u2f_authenticate(fido_dev_t *dev, fido_assert_t *fa, int *ms)
843 {
844 size_t nfound = 0;
845 size_t nauth_ok = 0;
846 int r;
847
848 if (fa->uv == FIDO_OPT_TRUE || fa->allow_list.ptr == NULL) {
849 fido_log_debug("%s: uv=%d, allow_list=%p", __func__, fa->uv,
850 (void *)fa->allow_list.ptr);
851 return (FIDO_ERR_UNSUPPORTED_OPTION);
852 }
853
854 if ((r = fido_assert_set_count(fa, fa->allow_list.len)) != FIDO_OK) {
855 fido_log_debug("%s: fido_assert_set_count", __func__);
856 return (r);
857 }
858
859 for (size_t i = 0; i < fa->allow_list.len; i++) {
860 switch ((r = u2f_authenticate_single(dev,
861 &fa->allow_list.ptr[i], fa, nfound, ms))) {
862 case FIDO_OK:
863 nauth_ok++;
864 FALLTHROUGH
865 case FIDO_ERR_USER_PRESENCE_REQUIRED:
866 nfound++;
867 break;
868 default:
869 if (r != FIDO_ERR_CREDENTIAL_EXCLUDED) {
870 fido_log_debug("%s: u2f_authenticate_single",
871 __func__);
872 return (r);
873 }
874 /* ignore credentials that don't exist */
875 }
876 }
877
878 fa->stmt_len = nfound;
879
880 if (nfound == 0)
881 return (FIDO_ERR_NO_CREDENTIALS);
882 if (nauth_ok == 0)
883 return (FIDO_ERR_USER_PRESENCE_REQUIRED);
884
885 return (FIDO_OK);
886 }
887
888 int
889 u2f_get_touch_begin(fido_dev_t *dev, int *ms)
890 {
891 iso7816_apdu_t *apdu = NULL;
892 const char *clientdata = FIDO_DUMMY_CLIENTDATA;
893 const char *rp_id = FIDO_DUMMY_RP_ID;
894 unsigned char *reply = NULL;
895 unsigned char clientdata_hash[SHA256_DIGEST_LENGTH];
896 unsigned char rp_id_hash[SHA256_DIGEST_LENGTH];
897 int r;
898
899 memset(&clientdata_hash, 0, sizeof(clientdata_hash));
900 memset(&rp_id_hash, 0, sizeof(rp_id_hash));
901
902 if (SHA256((const void *)clientdata, strlen(clientdata),
903 clientdata_hash) != clientdata_hash || SHA256((const void *)rp_id,
904 strlen(rp_id), rp_id_hash) != rp_id_hash) {
905 fido_log_debug("%s: sha256", __func__);
906 return (FIDO_ERR_INTERNAL);
907 }
908
909 if ((apdu = iso7816_new(0, U2F_CMD_REGISTER, 0, 2 *
910 SHA256_DIGEST_LENGTH)) == NULL ||
911 iso7816_add(apdu, clientdata_hash, sizeof(clientdata_hash)) < 0 ||
912 iso7816_add(apdu, rp_id_hash, sizeof(rp_id_hash)) < 0) {
913 fido_log_debug("%s: iso7816", __func__);
914 r = FIDO_ERR_INTERNAL;
915 goto fail;
916 }
917
918 if ((reply = malloc(FIDO_MAXMSG)) == NULL) {
919 fido_log_debug("%s: malloc", __func__);
920 r = FIDO_ERR_INTERNAL;
921 goto fail;
922 }
923
924 if (dev->attr.flags & FIDO_CAP_WINK) {
925 fido_tx(dev, CTAP_CMD_WINK, NULL, 0, ms);
926 fido_rx(dev, CTAP_CMD_WINK, reply, FIDO_MAXMSG, ms);
927 }
928
929 if (fido_tx(dev, CTAP_CMD_MSG, iso7816_ptr(apdu),
930 iso7816_len(apdu), ms) < 0) {
931 fido_log_debug("%s: fido_tx", __func__);
932 r = FIDO_ERR_TX;
933 goto fail;
934 }
935
936 r = FIDO_OK;
937 fail:
938 iso7816_free(&apdu);
939 freezero(reply, FIDO_MAXMSG);
940
941 return (r);
942 }
943
944 int
945 u2f_get_touch_status(fido_dev_t *dev, int *touched, int *ms)
946 {
947 unsigned char *reply;
948 int reply_len;
949 int r;
950
951 if ((reply = malloc(FIDO_MAXMSG)) == NULL) {
952 fido_log_debug("%s: malloc", __func__);
953 r = FIDO_ERR_INTERNAL;
954 goto out;
955 }
956
957 if ((reply_len = fido_rx(dev, CTAP_CMD_MSG, reply, FIDO_MAXMSG,
958 ms)) < 2) {
959 fido_log_debug("%s: fido_rx", __func__);
960 r = FIDO_OK; /* ignore */
961 goto out;
962 }
963
964 switch ((reply[reply_len - 2] << 8) | reply[reply_len - 1]) {
965 case SW_CONDITIONS_NOT_SATISFIED:
966 if ((r = u2f_get_touch_begin(dev, ms)) != FIDO_OK) {
967 fido_log_debug("%s: u2f_get_touch_begin", __func__);
968 goto out;
969 }
970 *touched = 0;
971 break;
972 case SW_NO_ERROR:
973 *touched = 1;
974 break;
975 default:
976 fido_log_debug("%s: unexpected sw", __func__);
977 r = FIDO_ERR_RX;
978 goto out;
979 }
980
981 r = FIDO_OK;
982 out:
983 freezero(reply, FIDO_MAXMSG);
984
985 return (r);
986 }