Debian Codegraph @

src/pin.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 "fido.h"
10 #include "fido/es256.h"
11
12 int
13 fido_sha256(fido_blob_t *digest, const u_char *data, size_t data_len)
14 {
15 if ((digest->ptr = calloc(1, SHA256_DIGEST_LENGTH)) == NULL)
16 return (-1);
17
18 digest->len = SHA256_DIGEST_LENGTH;
19
20 if (SHA256(data, data_len, digest->ptr) != digest->ptr) {
21 fido_blob_reset(digest);
22 return (-1);
23 }
24
25 return (0);
26 }
27
28 static int
29 pin_sha256_enc(const fido_dev_t *dev, const fido_blob_t *shared,
30 const fido_blob_t *pin, fido_blob_t **out)
31 {
32 fido_blob_t *ph = NULL;
33 int r;
34
35 if ((*out = fido_blob_new()) == NULL ||
36 (ph = fido_blob_new()) == NULL) {
37 r = FIDO_ERR_INTERNAL;
38 goto fail;
39 }
40
41 if (fido_sha256(ph, pin->ptr, pin->len) < 0 || ph->len < 16) {
42 fido_log_debug("%s: SHA256", __func__);
43 r = FIDO_ERR_INTERNAL;
44 goto fail;
45 }
46
47 ph->len = 16; /* first 16 bytes */
48
49 if (aes256_cbc_enc(dev, shared, ph, *out) < 0) {
50 fido_log_debug("%s: aes256_cbc_enc", __func__);
51 r = FIDO_ERR_INTERNAL;
52 goto fail;
53 }
54
55 r = FIDO_OK;
56 fail:
57 fido_blob_free(&ph);
58
59 return (r);
60 }
61
62 static int
63 pad64(const char *pin, fido_blob_t **ppin)
64 {
65 size_t pin_len;
66 size_t ppin_len;
67
68 pin_len = strlen(pin);
69 if (pin_len < 4 || pin_len > 63) {
70 fido_log_debug("%s: invalid pin length", __func__);
71 return (FIDO_ERR_PIN_POLICY_VIOLATION);
72 }
73
74 if ((*ppin = fido_blob_new()) == NULL)
75 return (FIDO_ERR_INTERNAL);
76
77 ppin_len = (pin_len + 63U) & ~63U;
78 if (ppin_len < pin_len ||
79 ((*ppin)->ptr = calloc(1, ppin_len)) == NULL) {
80 fido_blob_free(ppin);
81 return (FIDO_ERR_INTERNAL);
82 }
83
84 memcpy((*ppin)->ptr, pin, pin_len);
85 (*ppin)->len = ppin_len;
86
87 return (FIDO_OK);
88 }
89
90 static int
91 pin_pad64_enc(const fido_dev_t *dev, const fido_blob_t *shared,
92 const char *pin, fido_blob_t **out)
93 {
94 fido_blob_t *ppin = NULL;
95 int r;
96
97 if ((r = pad64(pin, &ppin)) != FIDO_OK) {
98 fido_log_debug("%s: pad64", __func__);
99 goto fail;
100 }
101
102 if ((*out = fido_blob_new()) == NULL) {
103 r = FIDO_ERR_INTERNAL;
104 goto fail;
105 }
106
107 if (aes256_cbc_enc(dev, shared, ppin, *out) < 0) {
108 fido_log_debug("%s: aes256_cbc_enc", __func__);
109 r = FIDO_ERR_INTERNAL;
110 goto fail;
111 }
112
113 r = FIDO_OK;
114 fail:
115 fido_blob_free(&ppin);
116
117 return (r);
118 }
119
120 static int
121 ctap20_uv_token_tx(fido_dev_t *dev, const char *pin, const fido_blob_t *ecdh,
122 const es256_pk_t *pk, int *ms)
123 {
124 fido_blob_t f;
125 fido_blob_t *p = NULL;
126 fido_blob_t *phe = NULL;
127 cbor_item_t *argv[6];
128 int r;
129
130 memset(&f, 0, sizeof(f));
131 memset(argv, 0, sizeof(argv));
132
133 if (pin == NULL) {
134 fido_log_debug("%s: NULL pin", __func__);
135 r = FIDO_ERR_PIN_REQUIRED;
136 goto fail;
137 }
138
139 if ((p = fido_blob_new()) == NULL || fido_blob_set(p,
140 (const unsigned char *)pin, strlen(pin)) < 0) {
141 fido_log_debug("%s: fido_blob_set", __func__);
142 r = FIDO_ERR_INVALID_ARGUMENT;
143 goto fail;
144 }
145
146 if ((r = pin_sha256_enc(dev, ecdh, p, &phe)) != FIDO_OK) {
147 fido_log_debug("%s: pin_sha256_enc", __func__);
148 goto fail;
149 }
150
151 if ((argv[0] = cbor_encode_pin_opt(dev)) == NULL ||
152 (argv[1] = cbor_build_uint8(5)) == NULL ||
153 (argv[2] = es256_pk_encode(pk, 1)) == NULL ||
154 (argv[5] = fido_blob_encode(phe)) == NULL) {
155 fido_log_debug("%s: cbor encode", __func__);
156 r = FIDO_ERR_INTERNAL;
157 goto fail;
158 }
159
160 if (cbor_build_frame(CTAP_CBOR_CLIENT_PIN, argv, nitems(argv),
161 &f) < 0 || fido_tx(dev, CTAP_CMD_CBOR, f.ptr, f.len, ms) < 0) {
162 fido_log_debug("%s: fido_tx", __func__);
163 r = FIDO_ERR_TX;
164 goto fail;
165 }
166
167 r = FIDO_OK;
168 fail:
169 cbor_vector_free(argv, nitems(argv));
170 fido_blob_free(&p);
171 fido_blob_free(&phe);
172 free(f.ptr);
173
174 return (r);
175 }
176
177 static int
178 ctap21_uv_token_tx(fido_dev_t *dev, const char *pin, const fido_blob_t *ecdh,
179 const es256_pk_t *pk, unsigned int perm, const char *rpid, int *ms)
180 {
181 fido_blob_t f;
182 fido_blob_t *p = NULL;
183 fido_blob_t *phe = NULL;
184 cbor_item_t *argv[10];
185 uint8_t subcmd;
186 int r;
187
188 memset(&f, 0, sizeof(f));
189 memset(argv, 0, sizeof(argv));
190
191 if (pin != NULL) {
192 if ((p = fido_blob_new()) == NULL || fido_blob_set(p,
193 (const unsigned char *)pin, strlen(pin)) < 0) {
194 fido_log_debug("%s: fido_blob_set", __func__);
195 r = FIDO_ERR_INVALID_ARGUMENT;
196 goto fail;
197 }
198 if ((r = pin_sha256_enc(dev, ecdh, p, &phe)) != FIDO_OK) {
199 fido_log_debug("%s: pin_sha256_enc", __func__);
200 goto fail;
201 }
202 subcmd = 9; /* getPinUvAuthTokenUsingPinWithPermissions */
203 } else {
204 if (fido_dev_has_uv(dev) == false) {
205 fido_log_debug("%s: fido_dev_has_uv", __func__);
206 r = FIDO_ERR_PIN_REQUIRED;
207 goto fail;
208 }
209 subcmd = 6; /* getPinUvAuthTokenUsingUvWithPermissions */
210 }
211
212 if ((argv[0] = cbor_encode_pin_opt(dev)) == NULL ||
213 (argv[1] = cbor_build_uint8(subcmd)) == NULL ||
214 (argv[2] = es256_pk_encode(pk, 1)) == NULL ||
215 (phe != NULL && (argv[5] = fido_blob_encode(phe)) == NULL) ||
216 (argv[8] = cbor_build_uint(perm)) == NULL ||
217 (rpid != NULL && (argv[9] = cbor_build_string(rpid)) == NULL)) {
218 fido_log_debug("%s: cbor encode", __func__);
219 r = FIDO_ERR_INTERNAL;
220 goto fail;
221 }
222
223 if (cbor_build_frame(CTAP_CBOR_CLIENT_PIN, argv, nitems(argv),
224 &f) < 0 || fido_tx(dev, CTAP_CMD_CBOR, f.ptr, f.len, ms) < 0) {
225 fido_log_debug("%s: fido_tx", __func__);
226 r = FIDO_ERR_TX;
227 goto fail;
228 }
229
230 r = FIDO_OK;
231 fail:
232 cbor_vector_free(argv, nitems(argv));
233 fido_blob_free(&p);
234 fido_blob_free(&phe);
235 free(f.ptr);
236
237 return (r);
238 }
239
240 static int
241 parse_uv_token(const cbor_item_t *key, const cbor_item_t *val, void *arg)
242 {
243 fido_blob_t *token = arg;
244
245 if (cbor_isa_uint(key) == false ||
246 cbor_int_get_width(key) != CBOR_INT_8 ||
247 cbor_get_uint8(key) != 2) {
248 fido_log_debug("%s: cbor type", __func__);
249 return (0); /* ignore */
250 }
251
252 return (fido_blob_decode(val, token));
253 }
254
255 static int
256 uv_token_rx(fido_dev_t *dev, const fido_blob_t *ecdh, fido_blob_t *token,
257 int *ms)
258 {
259 fido_blob_t *aes_token = NULL;
260 unsigned char *msg = NULL;
261 int msglen;
262 int r;
263
264 if ((aes_token = fido_blob_new()) == NULL) {
265 r = FIDO_ERR_INTERNAL;
266 goto fail;
267 }
268
269 if ((msg = malloc(FIDO_MAXMSG)) == NULL) {
270 r = FIDO_ERR_INTERNAL;
271 goto fail;
272 }
273
274 if ((msglen = fido_rx(dev, CTAP_CMD_CBOR, msg, FIDO_MAXMSG, ms)) < 0) {
275 fido_log_debug("%s: fido_rx", __func__);
276 r = FIDO_ERR_RX;
277 goto fail;
278 }
279
280 if ((r = cbor_parse_reply(msg, (size_t)msglen, aes_token,
281 parse_uv_token)) != FIDO_OK) {
282 fido_log_debug("%s: parse_uv_token", __func__);
283 goto fail;
284 }
285
286 if (aes256_cbc_dec(dev, ecdh, aes_token, token) < 0) {
287 fido_log_debug("%s: aes256_cbc_dec", __func__);
288 r = FIDO_ERR_RX;
289 goto fail;
290 }
291
292 r = FIDO_OK;
293 fail:
294 fido_blob_free(&aes_token);
295 freezero(msg, FIDO_MAXMSG);
296
297 return (r);
298 }
299
300 static int
301 uv_token_wait(fido_dev_t *dev, unsigned int perm, const char *pin,
302 const fido_blob_t *ecdh, const es256_pk_t *pk, const char *rpid,
303 fido_blob_t *token, int *ms)
304 {
305 int r;
306
307 if (ecdh == NULL || pk == NULL)
308 return (FIDO_ERR_INVALID_ARGUMENT);
309 if (fido_dev_supports_permissions(dev))
310 r = ctap21_uv_token_tx(dev, pin, ecdh, pk, perm, rpid, ms);
311 else
312 r = ctap20_uv_token_tx(dev, pin, ecdh, pk, ms);
313 if (r != FIDO_OK)
314 return (r);
315
316 return (uv_token_rx(dev, ecdh, token, ms));
317 }
318
319 static uint8_t
320 cmd_to_perm(uint8_t cmd)
321 {
322 switch (cmd) {
323 case CTAP_CBOR_ASSERT:
324 return (FIDO_PUAT_GETASSERT);
325 case CTAP_CBOR_BIO_ENROLL_PRE:
326 case CTAP_CBOR_BIO_ENROLL:
327 return (FIDO_PUAT_BIOENROLL);
328 case CTAP_CBOR_CONFIG:
329 return (FIDO_PUAT_CONFIG);
330 case CTAP_CBOR_MAKECRED:
331 return (FIDO_PUAT_MAKECRED);
332 case CTAP_CBOR_CRED_MGMT_PRE:
333 case CTAP_CBOR_CRED_MGMT:
334 return (FIDO_PUAT_CREDMAN);
335 case CTAP_CBOR_LARGEBLOB:
336 return (FIDO_PUAT_LARGEBLOB);
337 default:
338 fido_log_debug("%s: cmd 0x%02x", __func__, cmd);
339 return (0);
340 }
341 }
342
343 int
344 fido_dev_get_uv_token(fido_dev_t *dev, uint8_t cmd, const char *pin,
345 const fido_blob_t *ecdh, const es256_pk_t *pk, const char *rpid,
346 fido_blob_t *token, int *ms)
347 {
348 uint8_t perm = cmd_to_perm(cmd);
349
350 if (perm == 0)
351 return (FIDO_ERR_INTERNAL);
352
353 return (uv_token_wait(dev, perm, pin, ecdh, pk, rpid, token, ms));
354 }
355
356 int
357 fido_dev_get_puat(fido_dev_t *dev, unsigned int perm, const char *rpid,
358 const char *pin)
359 {
360 fido_blob_t *ecdh = NULL;
361 es256_pk_t *pk = NULL;
362 int ms = dev->timeout_ms;
363 int r;
364
365 fido_blob_reset(&dev->puat);
366
367 if (!fido_dev_is_fido2(dev) || fido_dev_is_winhello(dev)) {
368 r = FIDO_ERR_INVALID_ARGUMENT;
369 goto fail;
370 }
371
372 if ((r = fido_do_ecdh(dev, &pk, &ecdh, &ms)) != FIDO_OK) {
373 fido_log_debug("%s: fido_do_ecdh", __func__);
374 goto fail;
375 }
376
377 r = uv_token_wait(dev, perm, pin, ecdh, pk, rpid, &dev->puat, &ms);
378 fail:
379 es256_pk_free(&pk);
380 fido_blob_free(&ecdh);
381
382 return (r);
383 }
384
385 static int
386 fido_dev_change_pin_tx(fido_dev_t *dev, const char *pin, const char *oldpin,
387 int *ms)
388 {
389 fido_blob_t f;
390 fido_blob_t *ppine = NULL;
391 fido_blob_t *ecdh = NULL;
392 fido_blob_t *opin = NULL;
393 fido_blob_t *opinhe = NULL;
394 cbor_item_t *argv[6];
395 es256_pk_t *pk = NULL;
396 int r;
397
398 memset(&f, 0, sizeof(f));
399 memset(argv, 0, sizeof(argv));
400
401 if ((opin = fido_blob_new()) == NULL || fido_blob_set(opin,
402 (const unsigned char *)oldpin, strlen(oldpin)) < 0) {
403 fido_log_debug("%s: fido_blob_set", __func__);
404 r = FIDO_ERR_INVALID_ARGUMENT;
405 goto fail;
406 }
407
408 if ((r = fido_do_ecdh(dev, &pk, &ecdh, ms)) != FIDO_OK) {
409 fido_log_debug("%s: fido_do_ecdh", __func__);
410 goto fail;
411 }
412
413 /* pad and encrypt new pin */
414 if ((r = pin_pad64_enc(dev, ecdh, pin, &ppine)) != FIDO_OK) {
415 fido_log_debug("%s: pin_pad64_enc", __func__);
416 goto fail;
417 }
418
419 /* hash and encrypt old pin */
420 if ((r = pin_sha256_enc(dev, ecdh, opin, &opinhe)) != FIDO_OK) {
421 fido_log_debug("%s: pin_sha256_enc", __func__);
422 goto fail;
423 }
424
425 if ((argv[0] = cbor_encode_pin_opt(dev)) == NULL ||
426 (argv[1] = cbor_build_uint8(4)) == NULL ||
427 (argv[2] = es256_pk_encode(pk, 1)) == NULL ||
428 (argv[3] = cbor_encode_change_pin_auth(dev, ecdh, ppine, opinhe)) == NULL ||
429 (argv[4] = fido_blob_encode(ppine)) == NULL ||
430 (argv[5] = fido_blob_encode(opinhe)) == NULL) {
431 fido_log_debug("%s: cbor encode", __func__);
432 r = FIDO_ERR_INTERNAL;
433 goto fail;
434 }
435
436 if (cbor_build_frame(CTAP_CBOR_CLIENT_PIN, argv, nitems(argv),
437 &f) < 0 || fido_tx(dev, CTAP_CMD_CBOR, f.ptr, f.len, ms) < 0) {
438 fido_log_debug("%s: fido_tx", __func__);
439 r = FIDO_ERR_TX;
440 goto fail;
441 }
442
443 r = FIDO_OK;
444 fail:
445 cbor_vector_free(argv, nitems(argv));
446 es256_pk_free(&pk);
447 fido_blob_free(&ppine);
448 fido_blob_free(&ecdh);
449 fido_blob_free(&opin);
450 fido_blob_free(&opinhe);
451 free(f.ptr);
452
453 return (r);
454
455 }
456
457 static int
458 fido_dev_set_pin_tx(fido_dev_t *dev, const char *pin, int *ms)
459 {
460 fido_blob_t f;
461 fido_blob_t *ppine = NULL;
462 fido_blob_t *ecdh = NULL;
463 cbor_item_t *argv[5];
464 es256_pk_t *pk = NULL;
465 int r;
466
467 memset(&f, 0, sizeof(f));
468 memset(argv, 0, sizeof(argv));
469
470 if ((r = fido_do_ecdh(dev, &pk, &ecdh, ms)) != FIDO_OK) {
471 fido_log_debug("%s: fido_do_ecdh", __func__);
472 goto fail;
473 }
474
475 if ((r = pin_pad64_enc(dev, ecdh, pin, &ppine)) != FIDO_OK) {
476 fido_log_debug("%s: pin_pad64_enc", __func__);
477 goto fail;
478 }
479
480 if ((argv[0] = cbor_encode_pin_opt(dev)) == NULL ||
481 (argv[1] = cbor_build_uint8(3)) == NULL ||
482 (argv[2] = es256_pk_encode(pk, 1)) == NULL ||
483 (argv[3] = cbor_encode_pin_auth(dev, ecdh, ppine)) == NULL ||
484 (argv[4] = fido_blob_encode(ppine)) == NULL) {
485 fido_log_debug("%s: cbor encode", __func__);
486 r = FIDO_ERR_INTERNAL;
487 goto fail;
488 }
489
490 if (cbor_build_frame(CTAP_CBOR_CLIENT_PIN, argv, nitems(argv),
491 &f) < 0 || fido_tx(dev, CTAP_CMD_CBOR, f.ptr, f.len, ms) < 0) {
492 fido_log_debug("%s: fido_tx", __func__);
493 r = FIDO_ERR_TX;
494 goto fail;
495 }
496
497 r = FIDO_OK;
498 fail:
499 cbor_vector_free(argv, nitems(argv));
500 es256_pk_free(&pk);
501 fido_blob_free(&ppine);
502 fido_blob_free(&ecdh);
503 free(f.ptr);
504
505 return (r);
506 }
507
508 static int
509 fido_dev_set_pin_wait(fido_dev_t *dev, const char *pin, const char *oldpin,
510 int *ms)
511 {
512 int r;
513
514 if (oldpin != NULL) {
515 if ((r = fido_dev_change_pin_tx(dev, pin, oldpin,
516 ms)) != FIDO_OK) {
517 fido_log_debug("%s: fido_dev_change_pin_tx", __func__);
518 return (r);
519 }
520 } else {
521 if ((r = fido_dev_set_pin_tx(dev, pin, ms)) != FIDO_OK) {
522 fido_log_debug("%s: fido_dev_set_pin_tx", __func__);
523 return (r);
524 }
525 }
526
527 if ((r = fido_rx_cbor_status(dev, ms)) != FIDO_OK) {
528 fido_log_debug("%s: fido_rx_cbor_status", __func__);
529 return (r);
530 }
531
532 if (dev->flags & FIDO_DEV_PIN_UNSET) {
533 dev->flags &= ~FIDO_DEV_PIN_UNSET;
534 dev->flags |= FIDO_DEV_PIN_SET;
535 }
536
537 return (FIDO_OK);
538 }
539
540 int
541 fido_dev_set_pin(fido_dev_t *dev, const char *pin, const char *oldpin)
542 {
543 int ms = dev->timeout_ms;
544
545 return (fido_dev_set_pin_wait(dev, pin, oldpin, &ms));
546 }
547
548 static int
549 parse_retry_count(const uint8_t keyval, const cbor_item_t *key,
550 const cbor_item_t *val, void *arg)
551 {
552 int *retries = arg;
553 uint64_t n;
554
555 if (cbor_isa_uint(key) == false ||
556 cbor_int_get_width(key) != CBOR_INT_8 ||
557 cbor_get_uint8(key) != keyval) {
558 fido_log_debug("%s: cbor type", __func__);
559 return (0); /* ignore */
560 }
561
562 if (cbor_decode_uint64(val, &n) < 0 || n > INT_MAX) {
563 fido_log_debug("%s: cbor_decode_uint64", __func__);
564 return (-1);
565 }
566
567 *retries = (int)n;
568
569 return (0);
570 }
571
572 static int
573 parse_pin_retry_count(const cbor_item_t *key, const cbor_item_t *val, void *arg)
574 {
575 return (parse_retry_count(3, key, val, arg));
576 }
577
578 static int
579 parse_uv_retry_count(const cbor_item_t *key, const cbor_item_t *val, void *arg)
580 {
581 return (parse_retry_count(5, key, val, arg));
582 }
583
584 static int
585 fido_dev_get_retry_count_tx(fido_dev_t *dev, uint8_t subcmd, int *ms)
586 {
587 fido_blob_t f;
588 cbor_item_t *argv[2];
589 int r;
590
591 memset(&f, 0, sizeof(f));
592 memset(argv, 0, sizeof(argv));
593
594 if ((argv[0] = cbor_build_uint8(1)) == NULL ||
595 (argv[1] = cbor_build_uint8(subcmd)) == NULL) {
596 r = FIDO_ERR_INTERNAL;
597 goto fail;
598 }
599
600 if (cbor_build_frame(CTAP_CBOR_CLIENT_PIN, argv, nitems(argv),
601 &f) < 0 || fido_tx(dev, CTAP_CMD_CBOR, f.ptr, f.len, ms) < 0) {
602 fido_log_debug("%s: fido_tx", __func__);
603 r = FIDO_ERR_TX;
604 goto fail;
605 }
606
607 r = FIDO_OK;
608 fail:
609 cbor_vector_free(argv, nitems(argv));
610 free(f.ptr);
611
612 return (r);
613 }
614
615 static int
616 fido_dev_get_pin_retry_count_rx(fido_dev_t *dev, int *retries, int *ms)
617 {
618 unsigned char *msg;
619 int msglen;
620 int r;
621
622 *retries = 0;
623
624 if ((msg = malloc(FIDO_MAXMSG)) == NULL) {
625 r = FIDO_ERR_INTERNAL;
626 goto fail;
627 }
628
629 if ((msglen = fido_rx(dev, CTAP_CMD_CBOR, msg, FIDO_MAXMSG, ms)) < 0) {
630 fido_log_debug("%s: fido_rx", __func__);
631 r = FIDO_ERR_RX;
632 goto fail;
633 }
634
635 if ((r = cbor_parse_reply(msg, (size_t)msglen, retries,
636 parse_pin_retry_count)) != FIDO_OK) {
637 fido_log_debug("%s: parse_pin_retry_count", __func__);
638 goto fail;
639 }
640
641 r = FIDO_OK;
642 fail:
643 freezero(msg, FIDO_MAXMSG);
644
645 return (r);
646 }
647
648 static int
649 fido_dev_get_pin_retry_count_wait(fido_dev_t *dev, int *retries, int *ms)
650 {
651 int r;
652
653 if ((r = fido_dev_get_retry_count_tx(dev, 1, ms)) != FIDO_OK ||
654 (r = fido_dev_get_pin_retry_count_rx(dev, retries, ms)) != FIDO_OK)
655 return (r);
656
657 return (FIDO_OK);
658 }
659
660 int
661 fido_dev_get_retry_count(fido_dev_t *dev, int *retries)
662 {
663 int ms = dev->timeout_ms;
664
665 return (fido_dev_get_pin_retry_count_wait(dev, retries, &ms));
666 }
667
668 static int
669 fido_dev_get_uv_retry_count_rx(fido_dev_t *dev, int *retries, int *ms)
670 {
671 unsigned char *msg;
672 int msglen;
673 int r;
674
675 *retries = 0;
676
677 if ((msg = malloc(FIDO_MAXMSG)) == NULL) {
678 r = FIDO_ERR_INTERNAL;
679 goto fail;
680 }
681
682 if ((msglen = fido_rx(dev, CTAP_CMD_CBOR, msg, FIDO_MAXMSG, ms)) < 0) {
683 fido_log_debug("%s: fido_rx", __func__);
684 r = FIDO_ERR_RX;
685 goto fail;
686 }
687
688 if ((r = cbor_parse_reply(msg, (size_t)msglen, retries,
689 parse_uv_retry_count)) != FIDO_OK) {
690 fido_log_debug("%s: parse_uv_retry_count", __func__);
691 goto fail;
692 }
693
694 r = FIDO_OK;
695 fail:
696 freezero(msg, FIDO_MAXMSG);
697
698 return (r);
699 }
700
701 static int
702 fido_dev_get_uv_retry_count_wait(fido_dev_t *dev, int *retries, int *ms)
703 {
704 int r;
705
706 if ((r = fido_dev_get_retry_count_tx(dev, 7, ms)) != FIDO_OK ||
707 (r = fido_dev_get_uv_retry_count_rx(dev, retries, ms)) != FIDO_OK)
708 return (r);
709
710 return (FIDO_OK);
711 }
712
713 int
714 fido_dev_get_uv_retry_count(fido_dev_t *dev, int *retries)
715 {
716 int ms = dev->timeout_ms;
717
718 return (fido_dev_get_uv_retry_count_wait(dev, retries, &ms));
719 }
720
721 int
722 cbor_add_uv_params(fido_dev_t *dev, uint8_t cmd, const fido_blob_t *hmac_data,
723 const es256_pk_t *pk, const fido_blob_t *ecdh, const char *pin,
724 const char *rpid, cbor_item_t **auth, cbor_item_t **opt, int *ms)
725 {
726 fido_blob_t token_store;
727 const fido_blob_t *token;
728 int r;
729
730 memset(&token_store, 0, sizeof(token_store));
731
732 if ((token = fido_dev_puat_blob(dev)) == NULL) {
733 if ((r = fido_dev_get_uv_token(dev, cmd, pin, ecdh, pk, rpid,
734 &token_store, ms)) != FIDO_OK) {
735 fido_log_debug("%s: fido_dev_get_uv_token", __func__);
736 return r;
737 }
738 token = &token_store;
739 }
740
741 if ((*auth = cbor_encode_pin_auth(dev, token, hmac_data)) == NULL ||
742 (*opt = cbor_encode_pin_opt(dev)) == NULL) {
743 fido_log_debug("%s: cbor encode", __func__);
744 r = FIDO_ERR_INTERNAL;
745 goto fail;
746 }
747
748 r = FIDO_OK;
749 fail:
750 fido_blob_reset(&token_store);
751 return (r);
752 }