Debian Codegraph @

src/bio.c

raw
1 /*
2 * Copyright (c) 2019-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 "fido.h"
9 #include "fido/bio.h"
10 #include "fido/es256.h"
11
12 #define CMD_ENROLL_BEGIN 0x01
13 #define CMD_ENROLL_NEXT 0x02
14 #define CMD_ENROLL_CANCEL 0x03
15 #define CMD_ENUM 0x04
16 #define CMD_SET_NAME 0x05
17 #define CMD_ENROLL_REMOVE 0x06
18 #define CMD_GET_INFO 0x07
19
20 static int
21 bio_prepare_hmac(uint8_t cmd, cbor_item_t **argv, size_t argc,
22 cbor_item_t **param, fido_blob_t *hmac_data)
23 {
24 const uint8_t prefix[2] = { 0x01 /* modality */, cmd };
25 int ok = -1;
26 size_t cbor_alloc_len;
27 size_t cbor_len;
28 unsigned char *cbor = NULL;
29
30 if (argv == NULL || param == NULL)
31 return (fido_blob_set(hmac_data, prefix, sizeof(prefix)));
32
33 if ((*param = cbor_flatten_vector(argv, argc)) == NULL) {
34 fido_log_debug("%s: cbor_flatten_vector", __func__);
35 goto fail;
36 }
37
38 if ((cbor_len = cbor_serialize_alloc(*param, &cbor,
39 &cbor_alloc_len)) == 0 || cbor_len > SIZE_MAX - sizeof(prefix)) {
40 fido_log_debug("%s: cbor_serialize_alloc", __func__);
41 goto fail;
42 }
43
44 if ((hmac_data->ptr = malloc(cbor_len + sizeof(prefix))) == NULL) {
45 fido_log_debug("%s: malloc", __func__);
46 goto fail;
47 }
48
49 memcpy(hmac_data->ptr, prefix, sizeof(prefix));
50 memcpy(hmac_data->ptr + sizeof(prefix), cbor, cbor_len);
51 hmac_data->len = cbor_len + sizeof(prefix);
52
53 ok = 0;
54 fail:
55 free(cbor);
56
57 return (ok);
58 }
59
60 static uint8_t
61 bio_get_cmd(const fido_dev_t *dev)
62 {
63 if (dev->flags & (FIDO_DEV_BIO_SET|FIDO_DEV_BIO_UNSET))
64 return (CTAP_CBOR_BIO_ENROLL);
65
66 return (CTAP_CBOR_BIO_ENROLL_PRE);
67 }
68
69 static int
70 bio_get_uv_token(fido_dev_t *dev, const char *pin, uint8_t cmd,
71 fido_blob_t *token, int *ms)
72 {
73 es256_pk_t *pk = NULL;
74 fido_blob_t *ecdh = NULL;
75 int r;
76
77 if ((r = fido_do_ecdh(dev, &pk, &ecdh, ms)) != FIDO_OK) {
78 fido_log_debug("%s: fido_do_ecdh", __func__);
79 return r;
80 }
81
82 r = fido_dev_get_uv_token(dev, cmd, pin, ecdh, pk, NULL, token, ms);
83 if (r != FIDO_OK)
84 fido_log_debug("%s: fido_dev_get_uv_token", __func__);
85
86 es256_pk_free(&pk);
87 fido_blob_free(&ecdh);
88
89 return r;
90 }
91
92 static int
93 bio_tx(fido_dev_t *dev, uint8_t subcmd, cbor_item_t **sub_argv, size_t sub_argc,
94 const char *pin, const fido_blob_t *token, int *ms)
95 {
96 cbor_item_t *argv[5];
97 fido_blob_t token_store;
98 fido_blob_t f;
99 fido_blob_t hmac;
100 const uint8_t cmd = bio_get_cmd(dev);
101 int r = FIDO_ERR_INTERNAL;
102
103 memset(&f, 0, sizeof(f));
104 memset(&hmac, 0, sizeof(hmac));
105 memset(&argv, 0, sizeof(argv));
106 memset(&token_store, 0, sizeof(token_store));
107
108 /* modality, subCommand */
109 if ((argv[0] = cbor_build_uint8(1)) == NULL ||
110 (argv[1] = cbor_build_uint8(subcmd)) == NULL) {
111 fido_log_debug("%s: cbor encode", __func__);
112 goto fail;
113 }
114
115 if (pin && !token) {
116 if ((r = bio_get_uv_token(dev, pin, cmd, &token_store,
117 ms)) != FIDO_OK)
118 goto fail;
119
120 token = &token_store;
121 }
122
123 if (token) {
124 if (bio_prepare_hmac(subcmd, sub_argv, sub_argc, &argv[2],
125 &hmac) < 0) {
126 fido_log_debug("%s: bio_prepare_hmac", __func__);
127 goto fail;
128 }
129
130 if ((argv[3] = cbor_encode_pin_opt(dev)) == NULL ||
131 (argv[4] = cbor_encode_pin_auth(dev, token, &hmac)) == NULL) {
132 fido_log_debug("%s: encode pin", __func__);
133 goto fail;
134 }
135 }
136
137 /* framing and transmission */
138 if (cbor_build_frame(cmd, argv, nitems(argv), &f) < 0 ||
139 fido_tx(dev, CTAP_CMD_CBOR, f.ptr, f.len, ms) < 0) {
140 fido_log_debug("%s: fido_tx", __func__);
141 r = FIDO_ERR_TX;
142 goto fail;
143 }
144
145 r = FIDO_OK;
146 fail:
147 cbor_vector_free(argv, nitems(argv));
148 fido_blob_reset(&token_store);
149 free(f.ptr);
150 free(hmac.ptr);
151
152 return (r);
153 }
154
155 static void
156 bio_reset_template(fido_bio_template_t *t)
157 {
158 free(t->name);
159 t->name = NULL;
160 fido_blob_reset(&t->id);
161 }
162
163 static void
164 bio_reset_template_array(fido_bio_template_array_t *ta)
165 {
166 for (size_t i = 0; i < ta->n_alloc; i++)
167 bio_reset_template(&ta->ptr[i]);
168
169 free(ta->ptr);
170 ta->ptr = NULL;
171 memset(ta, 0, sizeof(*ta));
172 }
173
174 static int
175 decode_template(const cbor_item_t *key, const cbor_item_t *val, void *arg)
176 {
177 fido_bio_template_t *t = arg;
178
179 if (cbor_isa_uint(key) == false ||
180 cbor_int_get_width(key) != CBOR_INT_8) {
181 fido_log_debug("%s: cbor type", __func__);
182 return (0); /* ignore */
183 }
184
185 switch (cbor_get_uint8(key)) {
186 case 1: /* id */
187 return (fido_blob_decode(val, &t->id));
188 case 2: /* name */
189 return (cbor_string_copy(val, &t->name));
190 }
191
192 return (0); /* ignore */
193 }
194
195 static int
196 decode_template_array(const cbor_item_t *item, void *arg)
197 {
198 fido_bio_template_array_t *ta = arg;
199
200 if (cbor_isa_map(item) == false ||
201 cbor_map_is_definite(item) == false) {
202 fido_log_debug("%s: cbor type", __func__);
203 return (-1);
204 }
205
206 if (ta->n_rx >= ta->n_alloc) {
207 fido_log_debug("%s: n_rx >= n_alloc", __func__);
208 return (-1);
209 }
210
211 if (cbor_map_iter(item, &ta->ptr[ta->n_rx], decode_template) < 0) {
212 fido_log_debug("%s: decode_template", __func__);
213 return (-1);
214 }
215
216 ta->n_rx++;
217
218 return (0);
219 }
220
221 static int
222 bio_parse_template_array(const cbor_item_t *key, const cbor_item_t *val,
223 void *arg)
224 {
225 fido_bio_template_array_t *ta = arg;
226
227 if (cbor_isa_uint(key) == false ||
228 cbor_int_get_width(key) != CBOR_INT_8 ||
229 cbor_get_uint8(key) != 7) {
230 fido_log_debug("%s: cbor type", __func__);
231 return (0); /* ignore */
232 }
233
234 if (cbor_isa_array(val) == false ||
235 cbor_array_is_definite(val) == false) {
236 fido_log_debug("%s: cbor type", __func__);
237 return (-1);
238 }
239
240 if (ta->ptr != NULL || ta->n_alloc != 0 || ta->n_rx != 0) {
241 fido_log_debug("%s: ptr != NULL || n_alloc != 0 || n_rx != 0",
242 __func__);
243 return (-1);
244 }
245
246 if ((ta->ptr = calloc(cbor_array_size(val), sizeof(*ta->ptr))) == NULL)
247 return (-1);
248
249 ta->n_alloc = cbor_array_size(val);
250
251 if (cbor_array_iter(val, ta, decode_template_array) < 0) {
252 fido_log_debug("%s: decode_template_array", __func__);
253 return (-1);
254 }
255
256 return (0);
257 }
258
259 static int
260 bio_rx_template_array(fido_dev_t *dev, fido_bio_template_array_t *ta, int *ms)
261 {
262 unsigned char *msg;
263 int msglen;
264 int r;
265
266 bio_reset_template_array(ta);
267
268 if ((msg = malloc(FIDO_MAXMSG)) == NULL) {
269 r = FIDO_ERR_INTERNAL;
270 goto out;
271 }
272
273 if ((msglen = fido_rx(dev, CTAP_CMD_CBOR, msg, FIDO_MAXMSG, ms)) < 0) {
274 fido_log_debug("%s: fido_rx", __func__);
275 r = FIDO_ERR_RX;
276 goto out;
277 }
278
279 if ((r = cbor_parse_reply(msg, (size_t)msglen, ta,
280 bio_parse_template_array)) != FIDO_OK) {
281 fido_log_debug("%s: bio_parse_template_array" , __func__);
282 goto out;
283 }
284
285 r = FIDO_OK;
286 out:
287 freezero(msg, FIDO_MAXMSG);
288
289 return (r);
290 }
291
292 static int
293 bio_get_template_array_wait(fido_dev_t *dev, fido_bio_template_array_t *ta,
294 const char *pin, int *ms)
295 {
296 int r;
297
298 if ((r = bio_tx(dev, CMD_ENUM, NULL, 0, pin, fido_dev_puat_blob(dev),
299 ms)) != FIDO_OK || (r = bio_rx_template_array(dev, ta, ms)) != FIDO_OK)
300 return (r);
301
302 return (FIDO_OK);
303 }
304
305 int
306 fido_bio_dev_get_template_array(fido_dev_t *dev, fido_bio_template_array_t *ta,
307 const char *pin)
308 {
309 int ms = dev->timeout_ms;
310
311 if (pin == NULL && fido_dev_puat_blob(dev) == NULL)
312 return (FIDO_ERR_INVALID_ARGUMENT);
313
314 return (bio_get_template_array_wait(dev, ta, pin, &ms));
315 }
316
317 static int
318 bio_set_template_name_wait(fido_dev_t *dev, const fido_bio_template_t *t,
319 const char *pin, int *ms)
320 {
321 cbor_item_t *argv[2];
322 int r = FIDO_ERR_INTERNAL;
323
324 memset(&argv, 0, sizeof(argv));
325
326 if ((argv[0] = fido_blob_encode(&t->id)) == NULL ||
327 (argv[1] = cbor_build_string(t->name)) == NULL) {
328 fido_log_debug("%s: cbor encode", __func__);
329 goto fail;
330 }
331
332 if ((r = bio_tx(dev, CMD_SET_NAME, argv, 2, pin, fido_dev_puat_blob(dev),
333 ms)) != FIDO_OK ||
334 (r = fido_rx_cbor_status(dev, ms)) != FIDO_OK) {
335 fido_log_debug("%s: tx/rx", __func__);
336 goto fail;
337 }
338
339 r = FIDO_OK;
340 fail:
341 cbor_vector_free(argv, nitems(argv));
342
343 return (r);
344 }
345
346 int
347 fido_bio_dev_set_template_name(fido_dev_t *dev, const fido_bio_template_t *t,
348 const char *pin)
349 {
350 int ms = dev->timeout_ms;
351
352 if ((pin == NULL && fido_dev_puat_blob(dev) == NULL) || t->name == NULL)
353 return (FIDO_ERR_INVALID_ARGUMENT);
354
355 return (bio_set_template_name_wait(dev, t, pin, &ms));
356 }
357
358 static void
359 bio_reset_enroll(fido_bio_enroll_t *e)
360 {
361 e->remaining_samples = 0;
362 e->last_status = 0;
363 fido_blob_reset(&e->token);
364 }
365
366 static int
367 bio_parse_enroll_status(const cbor_item_t *key, const cbor_item_t *val,
368 void *arg)
369 {
370 fido_bio_enroll_t *e = arg;
371 uint64_t x;
372
373 if (cbor_isa_uint(key) == false ||
374 cbor_int_get_width(key) != CBOR_INT_8) {
375 fido_log_debug("%s: cbor type", __func__);
376 return (0); /* ignore */
377 }
378
379 switch (cbor_get_uint8(key)) {
380 case 5:
381 if (cbor_decode_uint64(val, &x) < 0 || x > UINT8_MAX) {
382 fido_log_debug("%s: cbor_decode_uint64", __func__);
383 return (-1);
384 }
385 e->last_status = (uint8_t)x;
386 break;
387 case 6:
388 if (cbor_decode_uint64(val, &x) < 0 || x > UINT8_MAX) {
389 fido_log_debug("%s: cbor_decode_uint64", __func__);
390 return (-1);
391 }
392 e->remaining_samples = (uint8_t)x;
393 break;
394 default:
395 return (0); /* ignore */
396 }
397
398 return (0);
399 }
400
401 static int
402 bio_parse_template_id(const cbor_item_t *key, const cbor_item_t *val,
403 void *arg)
404 {
405 fido_blob_t *id = arg;
406
407 if (cbor_isa_uint(key) == false ||
408 cbor_int_get_width(key) != CBOR_INT_8 ||
409 cbor_get_uint8(key) != 4) {
410 fido_log_debug("%s: cbor type", __func__);
411 return (0); /* ignore */
412 }
413
414 return (fido_blob_decode(val, id));
415 }
416
417 static int
418 bio_rx_enroll_begin(fido_dev_t *dev, fido_bio_template_t *t,
419 fido_bio_enroll_t *e, int *ms)
420 {
421 unsigned char *msg;
422 int msglen;
423 int r;
424
425 bio_reset_template(t);
426
427 e->remaining_samples = 0;
428 e->last_status = 0;
429
430 if ((msg = malloc(FIDO_MAXMSG)) == NULL) {
431 r = FIDO_ERR_INTERNAL;
432 goto out;
433 }
434
435 if ((msglen = fido_rx(dev, CTAP_CMD_CBOR, msg, FIDO_MAXMSG, ms)) < 0) {
436 fido_log_debug("%s: fido_rx", __func__);
437 r = FIDO_ERR_RX;
438 goto out;
439 }
440
441 if ((r = cbor_parse_reply(msg, (size_t)msglen, e,
442 bio_parse_enroll_status)) != FIDO_OK) {
443 fido_log_debug("%s: bio_parse_enroll_status", __func__);
444 goto out;
445 }
446
447 if ((r = cbor_parse_reply(msg, (size_t)msglen, &t->id,
448 bio_parse_template_id)) != FIDO_OK) {
449 fido_log_debug("%s: bio_parse_template_id", __func__);
450 goto out;
451 }
452
453 r = FIDO_OK;
454 out:
455 freezero(msg, FIDO_MAXMSG);
456
457 return (r);
458 }
459
460 static int
461 bio_enroll_begin_wait(fido_dev_t *dev, fido_bio_template_t *t,
462 fido_bio_enroll_t *e, uint32_t timo_ms, int *ms)
463 {
464 cbor_item_t *argv[3];
465 const uint8_t cmd = CMD_ENROLL_BEGIN;
466 int r = FIDO_ERR_INTERNAL;
467
468 memset(&argv, 0, sizeof(argv));
469
470 if ((argv[2] = cbor_build_uint(timo_ms)) == NULL) {
471 fido_log_debug("%s: cbor encode", __func__);
472 goto fail;
473 }
474
475 if ((r = bio_tx(dev, cmd, argv, 3, NULL, &e->token, ms)) != FIDO_OK ||
476 (r = bio_rx_enroll_begin(dev, t, e, ms)) != FIDO_OK) {
477 fido_log_debug("%s: tx/rx", __func__);
478 goto fail;
479 }
480
481 r = FIDO_OK;
482 fail:
483 cbor_vector_free(argv, nitems(argv));
484
485 return (r);
486 }
487
488 int
489 fido_bio_dev_enroll_begin(fido_dev_t *dev, fido_bio_template_t *t,
490 fido_bio_enroll_t *e, uint32_t timo_ms, const char *pin)
491 {
492 const fido_blob_t *token;
493 int ms = dev->timeout_ms;
494 int r;
495
496 token = fido_dev_puat_blob(dev);
497
498 if ((pin == NULL && token == NULL) || !fido_blob_is_empty(&e->token))
499 return (FIDO_ERR_INVALID_ARGUMENT);
500
501 if (token)
502 r = fido_blob_set(&e->token, token->ptr, token->len);
503 else
504 r = bio_get_uv_token(dev, pin, CTAP_CBOR_BIO_ENROLL_PRE, &e->token, &ms);
505
506 if (r != FIDO_OK)
507 return (r);
508
509 return (bio_enroll_begin_wait(dev, t, e, timo_ms, &ms));
510 }
511
512 static int
513 bio_rx_enroll_continue(fido_dev_t *dev, fido_bio_enroll_t *e, int *ms)
514 {
515 unsigned char *msg;
516 int msglen;
517 int r;
518
519 e->remaining_samples = 0;
520 e->last_status = 0;
521
522 if ((msg = malloc(FIDO_MAXMSG)) == NULL) {
523 r = FIDO_ERR_INTERNAL;
524 goto out;
525 }
526
527 if ((msglen = fido_rx(dev, CTAP_CMD_CBOR, msg, FIDO_MAXMSG, ms)) < 0) {
528 fido_log_debug("%s: fido_rx", __func__);
529 r = FIDO_ERR_RX;
530 goto out;
531 }
532
533 if ((r = cbor_parse_reply(msg, (size_t)msglen, e,
534 bio_parse_enroll_status)) != FIDO_OK) {
535 fido_log_debug("%s: bio_parse_enroll_status", __func__);
536 goto out;
537 }
538
539 r = FIDO_OK;
540 out:
541 freezero(msg, FIDO_MAXMSG);
542
543 return (r);
544 }
545
546 static int
547 bio_enroll_continue_wait(fido_dev_t *dev, const fido_bio_template_t *t,
548 fido_bio_enroll_t *e, uint32_t timo_ms, int *ms)
549 {
550 cbor_item_t *argv[3];
551 const uint8_t cmd = CMD_ENROLL_NEXT;
552 int r = FIDO_ERR_INTERNAL;
553
554 memset(&argv, 0, sizeof(argv));
555
556 if ((argv[0] = fido_blob_encode(&t->id)) == NULL ||
557 (argv[2] = cbor_build_uint(timo_ms)) == NULL) {
558 fido_log_debug("%s: cbor encode", __func__);
559 goto fail;
560 }
561
562 if ((r = bio_tx(dev, cmd, argv, 3, NULL, &e->token, ms)) != FIDO_OK ||
563 (r = bio_rx_enroll_continue(dev, e, ms)) != FIDO_OK) {
564 fido_log_debug("%s: tx/rx", __func__);
565 goto fail;
566 }
567
568 r = FIDO_OK;
569 fail:
570 cbor_vector_free(argv, nitems(argv));
571
572 return (r);
573 }
574
575 int
576 fido_bio_dev_enroll_continue(fido_dev_t *dev, const fido_bio_template_t *t,
577 fido_bio_enroll_t *e, uint32_t timo_ms)
578 {
579 int ms = dev->timeout_ms;
580
581 if (fido_blob_is_empty(&e->token))
582 return (FIDO_ERR_INVALID_ARGUMENT);
583
584 return (bio_enroll_continue_wait(dev, t, e, timo_ms, &ms));
585 }
586
587 static int
588 bio_enroll_cancel_wait(fido_dev_t *dev, int *ms)
589 {
590 const uint8_t cmd = CMD_ENROLL_CANCEL;
591 int r;
592
593 if ((r = bio_tx(dev, cmd, NULL, 0, NULL, NULL, ms)) != FIDO_OK ||
594 (r = fido_rx_cbor_status(dev, ms)) != FIDO_OK) {
595 fido_log_debug("%s: tx/rx", __func__);
596 return (r);
597 }
598
599 return (FIDO_OK);
600 }
601
602 int
603 fido_bio_dev_enroll_cancel(fido_dev_t *dev)
604 {
605 int ms = dev->timeout_ms;
606
607 return (bio_enroll_cancel_wait(dev, &ms));
608 }
609
610 static int
611 bio_enroll_remove_wait(fido_dev_t *dev, const fido_bio_template_t *t,
612 const char *pin, int *ms)
613 {
614 cbor_item_t *argv[1];
615 const uint8_t cmd = CMD_ENROLL_REMOVE;
616 int r = FIDO_ERR_INTERNAL;
617
618 memset(&argv, 0, sizeof(argv));
619
620 if ((argv[0] = fido_blob_encode(&t->id)) == NULL) {
621 fido_log_debug("%s: cbor encode", __func__);
622 goto fail;
623 }
624
625 if ((r = bio_tx(dev, cmd, argv, 1, pin, fido_dev_puat_blob(dev),
626 ms)) != FIDO_OK ||
627 (r = fido_rx_cbor_status(dev, ms)) != FIDO_OK) {
628 fido_log_debug("%s: tx/rx", __func__);
629 goto fail;
630 }
631
632 r = FIDO_OK;
633 fail:
634 cbor_vector_free(argv, nitems(argv));
635
636 return (r);
637 }
638
639 int
640 fido_bio_dev_enroll_remove(fido_dev_t *dev, const fido_bio_template_t *t,
641 const char *pin)
642 {
643 int ms = dev->timeout_ms;
644
645 return (bio_enroll_remove_wait(dev, t, pin, &ms));
646 }
647
648 static void
649 bio_reset_info(fido_bio_info_t *i)
650 {
651 i->type = 0;
652 i->max_samples = 0;
653 }
654
655 static int
656 bio_parse_info(const cbor_item_t *key, const cbor_item_t *val, void *arg)
657 {
658 fido_bio_info_t *i = arg;
659 uint64_t x;
660
661 if (cbor_isa_uint(key) == false ||
662 cbor_int_get_width(key) != CBOR_INT_8) {
663 fido_log_debug("%s: cbor type", __func__);
664 return (0); /* ignore */
665 }
666
667 switch (cbor_get_uint8(key)) {
668 case 2:
669 if (cbor_decode_uint64(val, &x) < 0 || x > UINT8_MAX) {
670 fido_log_debug("%s: cbor_decode_uint64", __func__);
671 return (-1);
672 }
673 i->type = (uint8_t)x;
674 break;
675 case 3:
676 if (cbor_decode_uint64(val, &x) < 0 || x > UINT8_MAX) {
677 fido_log_debug("%s: cbor_decode_uint64", __func__);
678 return (-1);
679 }
680 i->max_samples = (uint8_t)x;
681 break;
682 default:
683 return (0); /* ignore */
684 }
685
686 return (0);
687 }
688
689 static int
690 bio_rx_info(fido_dev_t *dev, fido_bio_info_t *i, int *ms)
691 {
692 unsigned char *msg;
693 int msglen;
694 int r;
695
696 bio_reset_info(i);
697
698 if ((msg = malloc(FIDO_MAXMSG)) == NULL) {
699 r = FIDO_ERR_INTERNAL;
700 goto out;
701 }
702
703 if ((msglen = fido_rx(dev, CTAP_CMD_CBOR, msg, FIDO_MAXMSG, ms)) < 0) {
704 fido_log_debug("%s: fido_rx", __func__);
705 r = FIDO_ERR_RX;
706 goto out;
707 }
708
709 if ((r = cbor_parse_reply(msg, (size_t)msglen, i,
710 bio_parse_info)) != FIDO_OK) {
711 fido_log_debug("%s: bio_parse_info" , __func__);
712 goto out;
713 }
714
715 r = FIDO_OK;
716 out:
717 freezero(msg, FIDO_MAXMSG);
718
719 return (r);
720 }
721
722 static int
723 bio_get_info_wait(fido_dev_t *dev, fido_bio_info_t *i, int *ms)
724 {
725 int r;
726
727 if ((r = bio_tx(dev, CMD_GET_INFO, NULL, 0, NULL, NULL,
728 ms)) != FIDO_OK ||
729 (r = bio_rx_info(dev, i, ms)) != FIDO_OK) {
730 fido_log_debug("%s: tx/rx", __func__);
731 return (r);
732 }
733
734 return (FIDO_OK);
735 }
736
737 int
738 fido_bio_dev_get_info(fido_dev_t *dev, fido_bio_info_t *i)
739 {
740 int ms = dev->timeout_ms;
741
742 return (bio_get_info_wait(dev, i, &ms));
743 }
744
745 const char *
746 fido_bio_template_name(const fido_bio_template_t *t)
747 {
748 return (t->name);
749 }
750
751 const unsigned char *
752 fido_bio_template_id_ptr(const fido_bio_template_t *t)
753 {
754 return (t->id.ptr);
755 }
756
757 size_t
758 fido_bio_template_id_len(const fido_bio_template_t *t)
759 {
760 return (t->id.len);
761 }
762
763 size_t
764 fido_bio_template_array_count(const fido_bio_template_array_t *ta)
765 {
766 return (ta->n_rx);
767 }
768
769 fido_bio_template_array_t *
770 fido_bio_template_array_new(void)
771 {
772 return (calloc(1, sizeof(fido_bio_template_array_t)));
773 }
774
775 fido_bio_template_t *
776 fido_bio_template_new(void)
777 {
778 return (calloc(1, sizeof(fido_bio_template_t)));
779 }
780
781 void
782 fido_bio_template_array_free(fido_bio_template_array_t **tap)
783 {
784 fido_bio_template_array_t *ta;
785
786 if (tap == NULL || (ta = *tap) == NULL)
787 return;
788
789 bio_reset_template_array(ta);
790 free(ta);
791 *tap = NULL;
792 }
793
794 void
795 fido_bio_template_free(fido_bio_template_t **tp)
796 {
797 fido_bio_template_t *t;
798
799 if (tp == NULL || (t = *tp) == NULL)
800 return;
801
802 bio_reset_template(t);
803 free(t);
804 *tp = NULL;
805 }
806
807 int
808 fido_bio_template_set_name(fido_bio_template_t *t, const char *name)
809 {
810 free(t->name);
811 t->name = NULL;
812
813 if (name && (t->name = strdup(name)) == NULL)
814 return (FIDO_ERR_INTERNAL);
815
816 return (FIDO_OK);
817 }
818
819 int
820 fido_bio_template_set_id(fido_bio_template_t *t, const unsigned char *ptr,
821 size_t len)
822 {
823 fido_blob_reset(&t->id);
824
825 if (ptr && fido_blob_set(&t->id, ptr, len) < 0)
826 return (FIDO_ERR_INTERNAL);
827
828 return (FIDO_OK);
829 }
830
831 const fido_bio_template_t *
832 fido_bio_template(const fido_bio_template_array_t *ta, size_t idx)
833 {
834 if (idx >= ta->n_alloc)
835 return (NULL);
836
837 return (&ta->ptr[idx]);
838 }
839
840 fido_bio_enroll_t *
841 fido_bio_enroll_new(void)
842 {
843 return (calloc(1, sizeof(fido_bio_enroll_t)));
844 }
845
846 fido_bio_info_t *
847 fido_bio_info_new(void)
848 {
849 return (calloc(1, sizeof(fido_bio_info_t)));
850 }
851
852 uint8_t
853 fido_bio_info_type(const fido_bio_info_t *i)
854 {
855 return (i->type);
856 }
857
858 uint8_t
859 fido_bio_info_max_samples(const fido_bio_info_t *i)
860 {
861 return (i->max_samples);
862 }
863
864 void
865 fido_bio_enroll_free(fido_bio_enroll_t **ep)
866 {
867 fido_bio_enroll_t *e;
868
869 if (ep == NULL || (e = *ep) == NULL)
870 return;
871
872 bio_reset_enroll(e);
873
874 free(e);
875 *ep = NULL;
876 }
877
878 void
879 fido_bio_info_free(fido_bio_info_t **ip)
880 {
881 fido_bio_info_t *i;
882
883 if (ip == NULL || (i = *ip) == NULL)
884 return;
885
886 free(i);
887 *ip = NULL;
888 }
889
890 uint8_t
891 fido_bio_enroll_remaining_samples(const fido_bio_enroll_t *e)
892 {
893 return (e->remaining_samples);
894 }
895
896 uint8_t
897 fido_bio_enroll_last_status(const fido_bio_enroll_t *e)
898 {
899 return (e->last_status);
900 }