Debian Codegraph @

src/cbor.c

raw
1 /*
2 * Copyright (c) 2018-2025 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/hmac.h>
9 #include <openssl/sha.h>
10 #include "fido.h"
11
12 static int
13 check_key_type(cbor_item_t *item)
14 {
15 if (item->type == CBOR_TYPE_UINT || item->type == CBOR_TYPE_NEGINT ||
16 item->type == CBOR_TYPE_STRING)
17 return (0);
18
19 fido_log_debug("%s: invalid type: %d", __func__, item->type);
20
21 return (-1);
22 }
23
24 /*
25 * Validate CTAP2 canonical CBOR encoding rules for maps.
26 */
27 static int
28 ctap_check_cbor(cbor_item_t *prev, cbor_item_t *curr)
29 {
30 size_t curr_len;
31 size_t prev_len;
32
33 if (check_key_type(prev) < 0 || check_key_type(curr) < 0)
34 return (-1);
35
36 if (prev->type != curr->type) {
37 if (prev->type < curr->type)
38 return (0);
39 fido_log_debug("%s: unsorted types", __func__);
40 return (-1);
41 }
42
43 if (curr->type == CBOR_TYPE_UINT || curr->type == CBOR_TYPE_NEGINT) {
44 if (cbor_int_get_width(curr) >= cbor_int_get_width(prev) &&
45 cbor_get_int(curr) > cbor_get_int(prev))
46 return (0);
47 } else {
48 curr_len = cbor_string_length(curr);
49 prev_len = cbor_string_length(prev);
50
51 if (curr_len > prev_len || (curr_len == prev_len &&
52 memcmp(cbor_string_handle(prev), cbor_string_handle(curr),
53 curr_len) < 0))
54 return (0);
55 }
56
57 fido_log_debug("%s: invalid cbor", __func__);
58
59 return (-1);
60 }
61
62 int
63 cbor_map_iter(const cbor_item_t *item, void *arg, int(*f)(const cbor_item_t *,
64 const cbor_item_t *, void *))
65 {
66 struct cbor_pair *v;
67 size_t n;
68
69 if ((v = cbor_map_handle(item)) == NULL) {
70 fido_log_debug("%s: cbor_map_handle", __func__);
71 return (-1);
72 }
73
74 n = cbor_map_size(item);
75
76 for (size_t i = 0; i < n; i++) {
77 if (v[i].key == NULL || v[i].value == NULL) {
78 fido_log_debug("%s: key=%p, value=%p for i=%zu",
79 __func__, (void *)v[i].key, (void *)v[i].value, i);
80 return (-1);
81 }
82 if (i && ctap_check_cbor(v[i - 1].key, v[i].key) < 0) {
83 fido_log_debug("%s: ctap_check_cbor", __func__);
84 return (-1);
85 }
86 if (f(v[i].key, v[i].value, arg) < 0) {
87 fido_log_debug("%s: iterator < 0 on i=%zu", __func__,
88 i);
89 return (-1);
90 }
91 }
92
93 return (0);
94 }
95
96 int
97 cbor_array_iter(const cbor_item_t *item, void *arg, int(*f)(const cbor_item_t *,
98 void *))
99 {
100 cbor_item_t **v;
101 size_t n;
102
103 if ((v = cbor_array_handle(item)) == NULL) {
104 fido_log_debug("%s: cbor_array_handle", __func__);
105 return (-1);
106 }
107
108 n = cbor_array_size(item);
109
110 for (size_t i = 0; i < n; i++)
111 if (v[i] == NULL || f(v[i], arg) < 0) {
112 fido_log_debug("%s: iterator < 0 on i=%zu,%p",
113 __func__, i, (void *)v[i]);
114 return (-1);
115 }
116
117 return (0);
118 }
119
120 int
121 cbor_parse_reply(const unsigned char *blob, size_t blob_len, void *arg,
122 int(*parser)(const cbor_item_t *, const cbor_item_t *, void *))
123 {
124 cbor_item_t *item = NULL;
125 struct cbor_load_result cbor;
126 int r;
127
128 if (blob_len < 1) {
129 fido_log_debug("%s: blob_len=%zu", __func__, blob_len);
130 r = FIDO_ERR_RX;
131 goto fail;
132 }
133
134 if (blob[0] != FIDO_OK) {
135 fido_log_debug("%s: blob[0]=0x%02x", __func__, blob[0]);
136 r = blob[0];
137 goto fail;
138 }
139
140 if ((item = cbor_load(blob + 1, blob_len - 1, &cbor)) == NULL) {
141 fido_log_debug("%s: cbor_load", __func__);
142 r = FIDO_ERR_RX_NOT_CBOR;
143 goto fail;
144 }
145
146 if (cbor_isa_map(item) == false ||
147 cbor_map_is_definite(item) == false) {
148 fido_log_debug("%s: cbor type", __func__);
149 r = FIDO_ERR_RX_INVALID_CBOR;
150 goto fail;
151 }
152
153 if (cbor_map_iter(item, arg, parser) < 0) {
154 fido_log_debug("%s: cbor_map_iter", __func__);
155 r = FIDO_ERR_RX_INVALID_CBOR;
156 goto fail;
157 }
158
159 r = FIDO_OK;
160 fail:
161 if (item != NULL)
162 cbor_decref(&item);
163
164 return (r);
165 }
166
167 void
168 cbor_vector_free(cbor_item_t **item, size_t len)
169 {
170 for (size_t i = 0; i < len; i++)
171 if (item[i] != NULL)
172 cbor_decref(&item[i]);
173 }
174
175 int
176 cbor_bytestring_copy(const cbor_item_t *item, unsigned char **buf, size_t *len)
177 {
178 if (*buf != NULL || *len != 0) {
179 fido_log_debug("%s: dup", __func__);
180 return (-1);
181 }
182
183 if (cbor_isa_bytestring(item) == false ||
184 cbor_bytestring_is_definite(item) == false) {
185 fido_log_debug("%s: cbor type", __func__);
186 return (-1);
187 }
188
189 *len = cbor_bytestring_length(item);
190 if ((*buf = malloc(*len)) == NULL) {
191 *len = 0;
192 return (-1);
193 }
194
195 memcpy(*buf, cbor_bytestring_handle(item), *len);
196
197 return (0);
198 }
199
200 int
201 cbor_string_copy(const cbor_item_t *item, char **str)
202 {
203 size_t len;
204
205 if (*str != NULL) {
206 fido_log_debug("%s: dup", __func__);
207 return (-1);
208 }
209
210 if (cbor_isa_string(item) == false ||
211 cbor_string_is_definite(item) == false) {
212 fido_log_debug("%s: cbor type", __func__);
213 return (-1);
214 }
215
216 if ((len = cbor_string_length(item)) == SIZE_MAX ||
217 (*str = malloc(len + 1)) == NULL)
218 return (-1);
219
220 memcpy(*str, cbor_string_handle(item), len);
221 (*str)[len] = '\0';
222
223 return (0);
224 }
225
226 int
227 cbor_add_bytestring(cbor_item_t *item, const char *key,
228 const unsigned char *value, size_t value_len)
229 {
230 struct cbor_pair pair;
231 int ok = -1;
232
233 memset(&pair, 0, sizeof(pair));
234
235 if ((pair.key = cbor_build_string(key)) == NULL ||
236 (pair.value = cbor_build_bytestring(value, value_len)) == NULL) {
237 fido_log_debug("%s: cbor_build", __func__);
238 goto fail;
239 }
240
241 if (!cbor_map_add(item, pair)) {
242 fido_log_debug("%s: cbor_map_add", __func__);
243 goto fail;
244 }
245
246 ok = 0;
247 fail:
248 if (pair.key)
249 cbor_decref(&pair.key);
250 if (pair.value)
251 cbor_decref(&pair.value);
252
253 return (ok);
254 }
255
256 int
257 cbor_add_string(cbor_item_t *item, const char *key, const char *value)
258 {
259 struct cbor_pair pair;
260 int ok = -1;
261
262 memset(&pair, 0, sizeof(pair));
263
264 if ((pair.key = cbor_build_string(key)) == NULL ||
265 (pair.value = cbor_build_string(value)) == NULL) {
266 fido_log_debug("%s: cbor_build", __func__);
267 goto fail;
268 }
269
270 if (!cbor_map_add(item, pair)) {
271 fido_log_debug("%s: cbor_map_add", __func__);
272 goto fail;
273 }
274
275 ok = 0;
276 fail:
277 if (pair.key)
278 cbor_decref(&pair.key);
279 if (pair.value)
280 cbor_decref(&pair.value);
281
282 return (ok);
283 }
284
285 int
286 cbor_add_bool(cbor_item_t *item, const char *key, fido_opt_t value)
287 {
288 struct cbor_pair pair;
289 int ok = -1;
290
291 memset(&pair, 0, sizeof(pair));
292
293 if ((pair.key = cbor_build_string(key)) == NULL ||
294 (pair.value = cbor_build_bool(value == FIDO_OPT_TRUE)) == NULL) {
295 fido_log_debug("%s: cbor_build", __func__);
296 goto fail;
297 }
298
299 if (!cbor_map_add(item, pair)) {
300 fido_log_debug("%s: cbor_map_add", __func__);
301 goto fail;
302 }
303
304 ok = 0;
305 fail:
306 if (pair.key)
307 cbor_decref(&pair.key);
308 if (pair.value)
309 cbor_decref(&pair.value);
310
311 return (ok);
312 }
313
314 static int
315 cbor_add_uint8(cbor_item_t *item, const char *key, uint8_t value)
316 {
317 struct cbor_pair pair;
318 int ok = -1;
319
320 memset(&pair, 0, sizeof(pair));
321
322 if ((pair.key = cbor_build_string(key)) == NULL ||
323 (pair.value = cbor_build_uint8(value)) == NULL) {
324 fido_log_debug("%s: cbor_build", __func__);
325 goto fail;
326 }
327
328 if (!cbor_map_add(item, pair)) {
329 fido_log_debug("%s: cbor_map_add", __func__);
330 goto fail;
331 }
332
333 ok = 0;
334 fail:
335 if (pair.key)
336 cbor_decref(&pair.key);
337 if (pair.value)
338 cbor_decref(&pair.value);
339
340 return (ok);
341 }
342
343 static int
344 cbor_add_arg(cbor_item_t *item, uint8_t n, cbor_item_t *arg)
345 {
346 struct cbor_pair pair;
347 int ok = -1;
348
349 memset(&pair, 0, sizeof(pair));
350
351 if (arg == NULL)
352 return (0); /* empty argument */
353
354 if ((pair.key = cbor_build_uint8(n)) == NULL) {
355 fido_log_debug("%s: cbor_build", __func__);
356 goto fail;
357 }
358
359 pair.value = arg;
360
361 if (!cbor_map_add(item, pair)) {
362 fido_log_debug("%s: cbor_map_add", __func__);
363 goto fail;
364 }
365
366 ok = 0;
367 fail:
368 if (pair.key)
369 cbor_decref(&pair.key);
370
371 return (ok);
372 }
373
374 cbor_item_t *
375 cbor_flatten_vector(cbor_item_t *argv[], size_t argc)
376 {
377 cbor_item_t *map;
378 uint8_t i;
379
380 if (argc > UINT8_MAX - 1)
381 return (NULL);
382
383 if ((map = cbor_new_definite_map(argc)) == NULL)
384 return (NULL);
385
386 for (i = 0; i < argc; i++)
387 if (cbor_add_arg(map, (uint8_t)(i + 1), argv[i]) < 0)
388 break;
389
390 if (i != argc) {
391 cbor_decref(&map);
392 map = NULL;
393 }
394
395 return (map);
396 }
397
398 int
399 cbor_build_frame(uint8_t cmd, cbor_item_t *argv[], size_t argc, fido_blob_t *f)
400 {
401 cbor_item_t *flat = NULL;
402 unsigned char *cbor = NULL;
403 size_t cbor_len;
404 size_t cbor_alloc_len;
405 int ok = -1;
406
407 if ((flat = cbor_flatten_vector(argv, argc)) == NULL)
408 goto fail;
409
410 cbor_len = cbor_serialize_alloc(flat, &cbor, &cbor_alloc_len);
411 if (cbor_len == 0 || cbor_len == SIZE_MAX) {
412 fido_log_debug("%s: cbor_len=%zu", __func__, cbor_len);
413 goto fail;
414 }
415
416 if ((f->ptr = malloc(cbor_len + 1)) == NULL)
417 goto fail;
418
419 f->len = cbor_len + 1;
420 f->ptr[0] = cmd;
421 memcpy(f->ptr + 1, cbor, f->len - 1);
422
423 ok = 0;
424 fail:
425 if (flat != NULL)
426 cbor_decref(&flat);
427
428 free(cbor);
429
430 return (ok);
431 }
432
433 cbor_item_t *
434 cbor_encode_rp_entity(const fido_rp_t *rp)
435 {
436 cbor_item_t *item = NULL;
437
438 if ((item = cbor_new_definite_map(2)) == NULL)
439 return (NULL);
440
441 if ((rp->id && cbor_add_string(item, "id", rp->id) < 0) ||
442 (rp->name && cbor_add_string(item, "name", rp->name) < 0)) {
443 cbor_decref(&item);
444 return (NULL);
445 }
446
447 return (item);
448 }
449
450 cbor_item_t *
451 cbor_encode_user_entity(const fido_user_t *user)
452 {
453 cbor_item_t *item = NULL;
454 const fido_blob_t *id = &user->id;
455 const char *display = user->display_name;
456
457 if ((item = cbor_new_definite_map(4)) == NULL)
458 return (NULL);
459
460 if ((id->ptr && cbor_add_bytestring(item, "id", id->ptr, id->len) < 0) ||
461 (user->icon && cbor_add_string(item, "icon", user->icon) < 0) ||
462 (user->name && cbor_add_string(item, "name", user->name) < 0) ||
463 (display && cbor_add_string(item, "displayName", display) < 0)) {
464 cbor_decref(&item);
465 return (NULL);
466 }
467
468 return (item);
469 }
470
471 cbor_item_t *
472 cbor_encode_pubkey_param(int cose_alg)
473 {
474 cbor_item_t *item = NULL;
475 cbor_item_t *body = NULL;
476 struct cbor_pair alg;
477 int ok = -1;
478
479 memset(&alg, 0, sizeof(alg));
480
481 if ((item = cbor_new_definite_array(1)) == NULL ||
482 (body = cbor_new_definite_map(2)) == NULL ||
483 cose_alg > -1 || cose_alg < INT16_MIN)
484 goto fail;
485
486 alg.key = cbor_build_string("alg");
487
488 if (-cose_alg - 1 > UINT8_MAX)
489 alg.value = cbor_build_negint16((uint16_t)(-cose_alg - 1));
490 else
491 alg.value = cbor_build_negint8((uint8_t)(-cose_alg - 1));
492
493 if (alg.key == NULL || alg.value == NULL) {
494 fido_log_debug("%s: cbor_build", __func__);
495 goto fail;
496 }
497
498 if (cbor_map_add(body, alg) == false ||
499 cbor_add_string(body, "type", "public-key") < 0 ||
500 cbor_array_push(item, body) == false)
501 goto fail;
502
503 ok = 0;
504 fail:
505 if (ok < 0) {
506 if (item != NULL) {
507 cbor_decref(&item);
508 item = NULL;
509 }
510 }
511
512 if (body != NULL)
513 cbor_decref(&body);
514 if (alg.key != NULL)
515 cbor_decref(&alg.key);
516 if (alg.value != NULL)
517 cbor_decref(&alg.value);
518
519 return (item);
520 }
521
522 cbor_item_t *
523 cbor_encode_pubkey(const fido_blob_t *pubkey)
524 {
525 cbor_item_t *cbor_key = NULL;
526
527 if ((cbor_key = cbor_new_definite_map(2)) == NULL ||
528 cbor_add_bytestring(cbor_key, "id", pubkey->ptr, pubkey->len) < 0 ||
529 cbor_add_string(cbor_key, "type", "public-key") < 0) {
530 if (cbor_key)
531 cbor_decref(&cbor_key);
532 return (NULL);
533 }
534
535 return (cbor_key);
536 }
537
538 cbor_item_t *
539 cbor_encode_pubkey_list(const fido_blob_array_t *list)
540 {
541 cbor_item_t *array = NULL;
542 cbor_item_t *key = NULL;
543
544 if ((array = cbor_new_definite_array(list->len)) == NULL)
545 goto fail;
546
547 for (size_t i = 0; i < list->len; i++) {
548 if ((key = cbor_encode_pubkey(&list->ptr[i])) == NULL ||
549 cbor_array_push(array, key) == false)
550 goto fail;
551 cbor_decref(&key);
552 }
553
554 return (array);
555 fail:
556 if (key != NULL)
557 cbor_decref(&key);
558 if (array != NULL)
559 cbor_decref(&array);
560
561 return (NULL);
562 }
563
564 cbor_item_t *
565 cbor_encode_str_array(const fido_str_array_t *a)
566 {
567 cbor_item_t *array = NULL;
568 cbor_item_t *entry = NULL;
569
570 if ((array = cbor_new_definite_array(a->len)) == NULL)
571 goto fail;
572
573 for (size_t i = 0; i < a->len; i++) {
574 if ((entry = cbor_build_string(a->ptr[i])) == NULL ||
575 cbor_array_push(array, entry) == false)
576 goto fail;
577 cbor_decref(&entry);
578 }
579
580 return (array);
581 fail:
582 if (entry != NULL)
583 cbor_decref(&entry);
584 if (array != NULL)
585 cbor_decref(&array);
586
587 return (NULL);
588 }
589
590 static int
591 cbor_encode_largeblob_key_ext(cbor_item_t *map)
592 {
593 if (map == NULL ||
594 cbor_add_bool(map, "largeBlobKey", FIDO_OPT_TRUE) < 0)
595 return (-1);
596
597 return (0);
598 }
599
600 static int
601 cbor_encode_hmac_secret_param(const char *key, const fido_dev_t *dev,
602 cbor_item_t *item, const fido_blob_t *ecdh, const es256_pk_t *pk,
603 const fido_blob_t *salt)
604 {
605 cbor_item_t *param = NULL;
606 cbor_item_t *argv[4];
607 struct cbor_pair pair;
608 fido_blob_t *enc = NULL;
609 uint8_t prot;
610 int r;
611
612 memset(argv, 0, sizeof(argv));
613 memset(&pair, 0, sizeof(pair));
614
615 if (item == NULL || ecdh == NULL || pk == NULL || salt->ptr == NULL) {
616 fido_log_debug("%s: ecdh=%p, pk=%p, salt->ptr=%p", __func__,
617 (const void *)ecdh, (const void *)pk,
618 (const void *)salt->ptr);
619 r = FIDO_ERR_INTERNAL;
620 goto fail;
621 }
622
623 if (salt->len != 32 && salt->len != 64) {
624 fido_log_debug("%s: salt->len=%zu", __func__, salt->len);
625 r = FIDO_ERR_INTERNAL;
626 goto fail;
627 }
628
629 if ((enc = fido_blob_new()) == NULL ||
630 aes256_cbc_enc(dev, ecdh, salt, enc) < 0) {
631 fido_log_debug("%s: aes256_cbc_enc", __func__);
632 r = FIDO_ERR_INTERNAL;
633 goto fail;
634 }
635
636 if ((prot = fido_dev_get_pin_protocol(dev)) == 0) {
637 fido_log_debug("%s: fido_dev_get_pin_protocol", __func__);
638 r = FIDO_ERR_INTERNAL;
639 goto fail;
640 }
641
642 /* XXX not pin, but salt */
643 if ((argv[0] = es256_pk_encode(pk, 1)) == NULL ||
644 (argv[1] = fido_blob_encode(enc)) == NULL ||
645 (argv[2] = cbor_encode_pin_auth(dev, ecdh, enc)) == NULL ||
646 (prot != 1 && (argv[3] = cbor_build_uint8(prot)) == NULL)) {
647 fido_log_debug("%s: cbor encode", __func__);
648 r = FIDO_ERR_INTERNAL;
649 goto fail;
650 }
651
652 if ((param = cbor_flatten_vector(argv, nitems(argv))) == NULL) {
653 fido_log_debug("%s: cbor_flatten_vector", __func__);
654 r = FIDO_ERR_INTERNAL;
655 goto fail;
656 }
657
658 if ((pair.key = cbor_build_string(key)) == NULL) {
659 fido_log_debug("%s: cbor_build", __func__);
660 r = FIDO_ERR_INTERNAL;
661 goto fail;
662 }
663
664 pair.value = param;
665
666 if (!cbor_map_add(item, pair)) {
667 fido_log_debug("%s: cbor_map_add", __func__);
668 r = FIDO_ERR_INTERNAL;
669 goto fail;
670 }
671
672 r = FIDO_OK;
673
674 fail:
675 cbor_vector_free(argv, nitems(argv));
676
677 if (param != NULL)
678 cbor_decref(&param);
679 if (pair.key != NULL)
680 cbor_decref(&pair.key);
681
682 fido_blob_free(&enc);
683
684 return (r);
685 }
686
687 cbor_item_t *
688 cbor_encode_cred_ext(const fido_dev_t *dev, const fido_cred_extin_t *ext,
689 const fido_blob_t *ecdh, const es256_pk_t *pk)
690 {
691 cbor_item_t *item = NULL;
692 size_t size = 0;
693
694 if (ext->attr.mask & FIDO_EXT_CRED_BLOB)
695 size++;
696 if (ext->attr.mask & FIDO_EXT_HMAC_SECRET)
697 size++;
698 if (ext->attr.mask & FIDO_EXT_CRED_PROTECT)
699 size++;
700 if (ext->attr.mask & FIDO_EXT_LARGEBLOB_KEY)
701 size++;
702 if (ext->attr.mask & FIDO_EXT_MINPINLEN)
703 size++;
704 if (ext->attr.mask & FIDO_EXT_HMAC_SECRET_MC)
705 size++;
706 if (ext->attr.mask & FIDO_EXT_PAYMENT)
707 size++;
708
709 if (size == 0 || (item = cbor_new_definite_map(size)) == NULL)
710 return (NULL);
711
712 if (ext->attr.mask & FIDO_EXT_CRED_BLOB) {
713 if (cbor_add_bytestring(item, "credBlob", ext->blob.ptr,
714 ext->blob.len) < 0) {
715 cbor_decref(&item);
716 return (NULL);
717 }
718 }
719 if (ext->attr.mask & FIDO_EXT_CRED_PROTECT) {
720 if (ext->attr.prot < 0 || ext->attr.prot > UINT8_MAX ||
721 cbor_add_uint8(item, "credProtect",
722 (uint8_t)ext->attr.prot) < 0) {
723 cbor_decref(&item);
724 return (NULL);
725 }
726 }
727 if (ext->attr.mask & FIDO_EXT_HMAC_SECRET) {
728 if (cbor_add_bool(item, "hmac-secret", FIDO_OPT_TRUE) < 0) {
729 cbor_decref(&item);
730 return (NULL);
731 }
732 }
733 if (ext->attr.mask & FIDO_EXT_LARGEBLOB_KEY) {
734 if (cbor_encode_largeblob_key_ext(item) < 0) {
735 cbor_decref(&item);
736 return (NULL);
737 }
738 }
739 if (ext->attr.mask & FIDO_EXT_MINPINLEN) {
740 if (cbor_add_bool(item, "minPinLength", FIDO_OPT_TRUE) < 0) {
741 cbor_decref(&item);
742 return (NULL);
743 }
744 }
745 if (ext->attr.mask & FIDO_EXT_HMAC_SECRET_MC) {
746 if (cbor_encode_hmac_secret_param("hmac-secret-mc", dev, item,
747 ecdh, pk, &ext->hmac_salt) < 0) {
748 cbor_decref(&item);
749 return (NULL);
750 }
751 }
752 if (ext->attr.mask & FIDO_EXT_PAYMENT) {
753 if (cbor_add_bool(item, "thirdPartyPayment",
754 FIDO_OPT_TRUE) < 0) {
755 cbor_decref(&item);
756 return (NULL);
757 }
758 }
759
760 return (item);
761 }
762
763 cbor_item_t *
764 cbor_encode_cred_opt(fido_opt_t rk, fido_opt_t uv)
765 {
766 cbor_item_t *item = NULL;
767
768 if ((item = cbor_new_definite_map(2)) == NULL)
769 return (NULL);
770 if ((rk != FIDO_OPT_OMIT && cbor_add_bool(item, "rk", rk) < 0) ||
771 (uv != FIDO_OPT_OMIT && cbor_add_bool(item, "uv", uv) < 0)) {
772 cbor_decref(&item);
773 return (NULL);
774 }
775
776 return (item);
777 }
778
779 cbor_item_t *
780 cbor_encode_assert_opt(fido_opt_t up, fido_opt_t uv)
781 {
782 cbor_item_t *item = NULL;
783
784 if ((item = cbor_new_definite_map(2)) == NULL)
785 return (NULL);
786 if ((up != FIDO_OPT_OMIT && cbor_add_bool(item, "up", up) < 0) ||
787 (uv != FIDO_OPT_OMIT && cbor_add_bool(item, "uv", uv) < 0)) {
788 cbor_decref(&item);
789 return (NULL);
790 }
791
792 return (item);
793 }
794
795 cbor_item_t *
796 cbor_encode_pin_auth(const fido_dev_t *dev, const fido_blob_t *secret,
797 const fido_blob_t *data)
798 {
799 const EVP_MD *md = NULL;
800 unsigned char dgst[SHA256_DIGEST_LENGTH];
801 unsigned int dgst_len;
802 size_t outlen;
803 uint8_t prot;
804 fido_blob_t key;
805
806 key.ptr = secret->ptr;
807 key.len = secret->len;
808
809 if ((prot = fido_dev_get_pin_protocol(dev)) == 0) {
810 fido_log_debug("%s: fido_dev_get_pin_protocol", __func__);
811 return (NULL);
812 }
813
814 /* select hmac portion of the shared secret */
815 if (prot == CTAP_PIN_PROTOCOL2 && key.len > 32)
816 key.len = 32;
817
818 if ((md = EVP_sha256()) == NULL || HMAC(md, key.ptr,
819 (int)key.len, data->ptr, data->len, dgst,
820 &dgst_len) == NULL || dgst_len != SHA256_DIGEST_LENGTH)
821 return (NULL);
822
823 outlen = (prot == CTAP_PIN_PROTOCOL1) ? 16 : dgst_len;
824
825 return (cbor_build_bytestring(dgst, outlen));
826 }
827
828 cbor_item_t *
829 cbor_encode_pin_opt(const fido_dev_t *dev)
830 {
831 uint8_t prot;
832
833 if ((prot = fido_dev_get_pin_protocol(dev)) == 0) {
834 fido_log_debug("%s: fido_dev_get_pin_protocol", __func__);
835 return (NULL);
836 }
837
838 return (cbor_build_uint8(prot));
839 }
840
841 cbor_item_t *
842 cbor_encode_change_pin_auth(const fido_dev_t *dev, const fido_blob_t *secret,
843 const fido_blob_t *new_pin_enc, const fido_blob_t *pin_hash_enc)
844 {
845 unsigned char dgst[SHA256_DIGEST_LENGTH];
846 unsigned int dgst_len;
847 cbor_item_t *item = NULL;
848 const EVP_MD *md = NULL;
849 HMAC_CTX *ctx = NULL;
850 fido_blob_t key;
851 uint8_t prot;
852 size_t outlen;
853
854 key.ptr = secret->ptr;
855 key.len = secret->len;
856
857 if ((prot = fido_dev_get_pin_protocol(dev)) == 0) {
858 fido_log_debug("%s: fido_dev_get_pin_protocol", __func__);
859 goto fail;
860 }
861
862 if (prot == CTAP_PIN_PROTOCOL2 && key.len > 32)
863 key.len = 32;
864
865 if ((ctx = HMAC_CTX_new()) == NULL ||
866 (md = EVP_sha256()) == NULL ||
867 HMAC_Init_ex(ctx, key.ptr, (int)key.len, md, NULL) == 0 ||
868 HMAC_Update(ctx, new_pin_enc->ptr, new_pin_enc->len) == 0 ||
869 HMAC_Update(ctx, pin_hash_enc->ptr, pin_hash_enc->len) == 0 ||
870 HMAC_Final(ctx, dgst, &dgst_len) == 0 ||
871 dgst_len != SHA256_DIGEST_LENGTH) {
872 fido_log_debug("%s: HMAC", __func__);
873 goto fail;
874 }
875
876 outlen = (prot == CTAP_PIN_PROTOCOL1) ? 16 : dgst_len;
877
878 if ((item = cbor_build_bytestring(dgst, outlen)) == NULL) {
879 fido_log_debug("%s: cbor_build_bytestring", __func__);
880 goto fail;
881 }
882
883 fail:
884 HMAC_CTX_free(ctx);
885
886 return (item);
887 }
888
889 cbor_item_t *
890 cbor_encode_assert_ext(fido_dev_t *dev, const fido_assert_ext_t *ext,
891 const fido_blob_t *ecdh, const es256_pk_t *pk)
892 {
893 cbor_item_t *item = NULL;
894 size_t size = 0;
895
896 if (ext->mask & FIDO_EXT_CRED_BLOB)
897 size++;
898 if (ext->mask & FIDO_EXT_HMAC_SECRET)
899 size++;
900 if (ext->mask & FIDO_EXT_LARGEBLOB_KEY)
901 size++;
902 if (ext->mask & FIDO_EXT_PAYMENT)
903 size++;
904 if (size == 0 || (item = cbor_new_definite_map(size)) == NULL)
905 return (NULL);
906
907 if (ext->mask & FIDO_EXT_CRED_BLOB) {
908 if (cbor_add_bool(item, "credBlob", FIDO_OPT_TRUE) < 0) {
909 cbor_decref(&item);
910 return (NULL);
911 }
912 }
913 if (ext->mask & FIDO_EXT_HMAC_SECRET) {
914 if (cbor_encode_hmac_secret_param("hmac-secret", dev, item,
915 ecdh, pk, &ext->hmac_salt) < 0) {
916 cbor_decref(&item);
917 return (NULL);
918 }
919 }
920 if (ext->mask & FIDO_EXT_LARGEBLOB_KEY) {
921 if (cbor_encode_largeblob_key_ext(item) < 0) {
922 cbor_decref(&item);
923 return (NULL);
924 }
925 }
926 if (ext->mask & FIDO_EXT_PAYMENT) {
927 if (cbor_add_bool(item, "thirdPartyPayment", FIDO_OPT_TRUE) < 0) {
928 cbor_decref(&item);
929 return (NULL);
930 }
931 }
932
933 return (item);
934 }
935
936 int
937 cbor_decode_fmt(const cbor_item_t *item, char **fmt)
938 {
939 char *type = NULL;
940
941 if (cbor_string_copy(item, &type) < 0) {
942 fido_log_debug("%s: cbor_string_copy", __func__);
943 return (-1);
944 }
945
946 if (strcmp(type, "packed") && strcmp(type, "fido-u2f") &&
947 strcmp(type, "none") && strcmp(type, "tpm")) {
948 fido_log_debug("%s: type=%s", __func__, type);
949 free(type);
950 return (-1);
951 }
952
953 *fmt = type;
954
955 return (0);
956 }
957
958 struct cose_key {
959 int kty;
960 int alg;
961 int crv;
962 };
963
964 static int
965 find_cose_alg(const cbor_item_t *key, const cbor_item_t *val, void *arg)
966 {
967 struct cose_key *cose_key = arg;
968
969 if (cbor_isa_uint(key) == true &&
970 cbor_int_get_width(key) == CBOR_INT_8) {
971 switch (cbor_get_uint8(key)) {
972 case 1:
973 if (cbor_isa_uint(val) == false ||
974 cbor_get_int(val) > INT_MAX || cose_key->kty != 0) {
975 fido_log_debug("%s: kty", __func__);
976 return (-1);
977 }
978
979 cose_key->kty = (int)cbor_get_int(val);
980
981 break;
982 case 3:
983 if (cbor_isa_negint(val) == false ||
984 cbor_get_int(val) > INT_MAX || cose_key->alg != 0) {
985 fido_log_debug("%s: alg", __func__);
986 return (-1);
987 }
988
989 cose_key->alg = -(int)cbor_get_int(val) - 1;
990
991 break;
992 }
993 } else if (cbor_isa_negint(key) == true &&
994 cbor_int_get_width(key) == CBOR_INT_8) {
995 if (cbor_get_uint8(key) == 0) {
996 /* get crv if not rsa, otherwise ignore */
997 if (cbor_isa_uint(val) == true &&
998 cbor_get_int(val) <= INT_MAX &&
999 cose_key->crv == 0)
1000 cose_key->crv = (int)cbor_get_int(val);
1001 }
1002 }
1003
1004 return (0);
1005 }
1006
1007 static int
1008 get_cose_alg(const cbor_item_t *item, int *cose_alg)
1009 {
1010 struct cose_key cose_key;
1011
1012 memset(&cose_key, 0, sizeof(cose_key));
1013
1014 *cose_alg = 0;
1015
1016 if (cbor_isa_map(item) == false ||
1017 cbor_map_is_definite(item) == false ||
1018 cbor_map_iter(item, &cose_key, find_cose_alg) < 0) {
1019 fido_log_debug("%s: cbor type", __func__);
1020 return (-1);
1021 }
1022
1023 switch (cose_key.alg) {
1024 case COSE_ES256:
1025 if (cose_key.kty != COSE_KTY_EC2 ||
1026 cose_key.crv != COSE_P256) {
1027 fido_log_debug("%s: invalid kty/crv", __func__);
1028 return (-1);
1029 }
1030 break;
1031 case COSE_ES384:
1032 if (cose_key.kty != COSE_KTY_EC2 ||
1033 cose_key.crv != COSE_P384) {
1034 fido_log_debug("%s: invalid kty/crv", __func__);
1035 return (-1);
1036 }
1037 break;
1038 case COSE_EDDSA:
1039 if (cose_key.kty != COSE_KTY_OKP ||
1040 cose_key.crv != COSE_ED25519) {
1041 fido_log_debug("%s: invalid kty/crv", __func__);
1042 return (-1);
1043 }
1044 break;
1045 case COSE_RS256:
1046 if (cose_key.kty != COSE_KTY_RSA) {
1047 fido_log_debug("%s: invalid kty/crv", __func__);
1048 return (-1);
1049 }
1050 break;
1051 default:
1052 fido_log_debug("%s: unknown alg %d", __func__, cose_key.alg);
1053
1054 return (-1);
1055 }
1056
1057 *cose_alg = cose_key.alg;
1058
1059 return (0);
1060 }
1061
1062 int
1063 cbor_decode_pubkey(const cbor_item_t *item, int *type, void *key)
1064 {
1065 if (get_cose_alg(item, type) < 0) {
1066 fido_log_debug("%s: get_cose_alg", __func__);
1067 return (-1);
1068 }
1069
1070 switch (*type) {
1071 case COSE_ES256:
1072 if (es256_pk_decode(item, key) < 0) {
1073 fido_log_debug("%s: es256_pk_decode", __func__);
1074 return (-1);
1075 }
1076 break;
1077 case COSE_ES384:
1078 if (es384_pk_decode(item, key) < 0) {
1079 fido_log_debug("%s: es384_pk_decode", __func__);
1080 return (-1);
1081 }
1082 break;
1083 case COSE_RS256:
1084 if (rs256_pk_decode(item, key) < 0) {
1085 fido_log_debug("%s: rs256_pk_decode", __func__);
1086 return (-1);
1087 }
1088 break;
1089 case COSE_EDDSA:
1090 if (eddsa_pk_decode(item, key) < 0) {
1091 fido_log_debug("%s: eddsa_pk_decode", __func__);
1092 return (-1);
1093 }
1094 break;
1095 default:
1096 fido_log_debug("%s: invalid cose_alg %d", __func__, *type);
1097 return (-1);
1098 }
1099
1100 return (0);
1101 }
1102
1103 static int
1104 decode_attcred(const unsigned char **buf, size_t *len, int cose_alg,
1105 fido_attcred_t *attcred)
1106 {
1107 cbor_item_t *item = NULL;
1108 struct cbor_load_result cbor;
1109 uint16_t id_len;
1110 int ok = -1;
1111
1112 fido_log_xxd(*buf, *len, "%s", __func__);
1113
1114 if (fido_buf_read(buf, len, &attcred->aaguid,
1115 sizeof(attcred->aaguid)) < 0) {
1116 fido_log_debug("%s: fido_buf_read aaguid", __func__);
1117 return (-1);
1118 }
1119
1120 if (fido_buf_read(buf, len, &id_len, sizeof(id_len)) < 0) {
1121 fido_log_debug("%s: fido_buf_read id_len", __func__);
1122 return (-1);
1123 }
1124
1125 attcred->id.len = (size_t)be16toh(id_len);
1126 if ((attcred->id.ptr = malloc(attcred->id.len)) == NULL)
1127 return (-1);
1128
1129 fido_log_debug("%s: attcred->id.len=%zu", __func__, attcred->id.len);
1130
1131 if (fido_buf_read(buf, len, attcred->id.ptr, attcred->id.len) < 0) {
1132 fido_log_debug("%s: fido_buf_read id", __func__);
1133 return (-1);
1134 }
1135
1136 if ((item = cbor_load(*buf, *len, &cbor)) == NULL) {
1137 fido_log_debug("%s: cbor_load", __func__);
1138 goto fail;
1139 }
1140
1141 if (cbor_decode_pubkey(item, &attcred->type, &attcred->pubkey) < 0) {
1142 fido_log_debug("%s: cbor_decode_pubkey", __func__);
1143 goto fail;
1144 }
1145
1146 if (attcred->type != cose_alg) {
1147 fido_log_debug("%s: cose_alg mismatch (%d != %d)", __func__,
1148 attcred->type, cose_alg);
1149 goto fail;
1150 }
1151
1152 *buf += cbor.read;
1153 *len -= cbor.read;
1154
1155 ok = 0;
1156 fail:
1157 if (item != NULL)
1158 cbor_decref(&item);
1159
1160 return (ok);
1161 }
1162
1163 static int
1164 decode_attobj(const cbor_item_t *key, const cbor_item_t *val, void *arg)
1165 {
1166 fido_cred_t *cred = arg;
1167 char *name = NULL;
1168 int ok = -1;
1169
1170 if (cbor_string_copy(key, &name) < 0) {
1171 fido_log_debug("%s: cbor type", __func__);
1172 ok = 0; /* ignore */
1173 goto fail;
1174 }
1175
1176 if (!strcmp(name, "fmt")) {
1177 if (cbor_decode_fmt(val, &cred->fmt) < 0) {
1178 fido_log_debug("%s: cbor_decode_fmt", __func__);
1179 goto fail;
1180 }
1181 } else if (!strcmp(name, "attStmt")) {
1182 if (cbor_decode_attstmt(val, &cred->attstmt) < 0) {
1183 fido_log_debug("%s: cbor_decode_attstmt", __func__);
1184 goto fail;
1185 }
1186 } else if (!strcmp(name, "authData")) {
1187 if (fido_blob_decode(val, &cred->authdata_raw) < 0) {
1188 fido_log_debug("%s: fido_blob_decode", __func__);
1189 goto fail;
1190 }
1191 if (cbor_decode_cred_authdata(val, cred->type,
1192 &cred->authdata_cbor, &cred->authdata, &cred->attcred,
1193 &cred->authdata_ext) < 0) {
1194 fido_log_debug("%s: cbor_decode_cred_authdata",
1195 __func__);
1196 goto fail;
1197 }
1198 }
1199
1200 ok = 0;
1201 fail:
1202 free(name);
1203
1204 return (ok);
1205 }
1206
1207 /* XXX introduce fido_attobj_t? */
1208 int
1209 cbor_decode_attobj(const cbor_item_t *item, fido_cred_t *cred)
1210 {
1211 if (cbor_isa_map(item) == false ||
1212 cbor_map_is_definite(item) == false ||
1213 cbor_map_iter(item, cred, decode_attobj) < 0) {
1214 fido_log_debug("%s: cbor type", __func__);
1215 return (-1);
1216 }
1217
1218 return (0);
1219 }
1220
1221 static int
1222 decode_cred_extension(const cbor_item_t *key, const cbor_item_t *val, void *arg)
1223 {
1224 fido_cred_extout_t *ext = arg;
1225 char *type = NULL;
1226 int ok = -1;
1227
1228 if (cbor_string_copy(key, &type) < 0) {
1229 fido_log_debug("%s: cbor type", __func__);
1230 ok = 0; /* ignore */
1231 goto out;
1232 }
1233
1234 if (strcmp(type, "hmac-secret") == 0) {
1235 if (cbor_decode_bool(val, NULL) < 0) {
1236 fido_log_debug("%s: cbor_decode_bool", __func__);
1237 goto out;
1238 }
1239 if (cbor_ctrl_value(val) == CBOR_CTRL_TRUE)
1240 ext->attr.mask |= FIDO_EXT_HMAC_SECRET;
1241 } else if (strcmp(type, "hmac-secret-mc") == 0) {
1242 if (fido_blob_decode(val, &ext->hmac_secret_enc) < 0) {
1243 fido_log_debug("%s: fido_blob_decode", __func__);
1244 goto out;
1245 }
1246 ext->attr.mask |= FIDO_EXT_HMAC_SECRET_MC;
1247 } else if (strcmp(type, "credProtect") == 0) {
1248 if (cbor_isa_uint(val) == false ||
1249 cbor_int_get_width(val) != CBOR_INT_8) {
1250 fido_log_debug("%s: cbor type", __func__);
1251 goto out;
1252 }
1253 ext->attr.mask |= FIDO_EXT_CRED_PROTECT;
1254 ext->attr.prot = cbor_get_uint8(val);
1255 } else if (strcmp(type, "credBlob") == 0) {
1256 if (cbor_decode_bool(val, NULL) < 0) {
1257 fido_log_debug("%s: cbor_decode_bool", __func__);
1258 goto out;
1259 }
1260 if (cbor_ctrl_value(val) == CBOR_CTRL_TRUE)
1261 ext->attr.mask |= FIDO_EXT_CRED_BLOB;
1262 } else if (strcmp(type, "minPinLength") == 0) {
1263 if (cbor_isa_uint(val) == false ||
1264 cbor_int_get_width(val) != CBOR_INT_8) {
1265 fido_log_debug("%s: cbor type", __func__);
1266 goto out;
1267 }
1268 ext->attr.mask |= FIDO_EXT_MINPINLEN;
1269 ext->attr.minpinlen = cbor_get_uint8(val);
1270 }
1271
1272 ok = 0;
1273 out:
1274 free(type);
1275
1276 return (ok);
1277 }
1278
1279 static int
1280 decode_cred_extensions(const unsigned char **buf, size_t *len,
1281 fido_cred_extout_t *authdata_ext)
1282 {
1283 cbor_item_t *item = NULL;
1284 struct cbor_load_result cbor;
1285 int ok = -1;
1286
1287 memset(authdata_ext, 0, sizeof(*authdata_ext));
1288
1289 fido_log_xxd(*buf, *len, "%s", __func__);
1290
1291 if ((item = cbor_load(*buf, *len, &cbor)) == NULL) {
1292 fido_log_debug("%s: cbor_load", __func__);
1293 goto fail;
1294 }
1295
1296 if (cbor_isa_map(item) == false ||
1297 cbor_map_is_definite(item) == false ||
1298 cbor_map_iter(item, authdata_ext, decode_cred_extension) < 0) {
1299 fido_log_debug("%s: cbor type", __func__);
1300 goto fail;
1301 }
1302
1303 *buf += cbor.read;
1304 *len -= cbor.read;
1305
1306 ok = 0;
1307 fail:
1308 if (item != NULL)
1309 cbor_decref(&item);
1310
1311 return (ok);
1312 }
1313
1314 static int
1315 decode_assert_extension(const cbor_item_t *key, const cbor_item_t *val,
1316 void *arg)
1317 {
1318 fido_assert_extattr_t *authdata_ext = arg;
1319 char *type = NULL;
1320 int ok = -1;
1321
1322 if (cbor_string_copy(key, &type) < 0) {
1323 fido_log_debug("%s: cbor type", __func__);
1324 ok = 0; /* ignore */
1325 goto out;
1326 }
1327
1328 if (strcmp(type, "hmac-secret") == 0) {
1329 if (fido_blob_decode(val, &authdata_ext->hmac_secret_enc) < 0) {
1330 fido_log_debug("%s: fido_blob_decode", __func__);
1331 goto out;
1332 }
1333 authdata_ext->mask |= FIDO_EXT_HMAC_SECRET;
1334 } else if (strcmp(type, "credBlob") == 0) {
1335 if (fido_blob_decode(val, &authdata_ext->blob) < 0) {
1336 fido_log_debug("%s: fido_blob_decode", __func__);
1337 goto out;
1338 }
1339 authdata_ext->mask |= FIDO_EXT_CRED_BLOB;
1340 } else if (strcmp(type, "thirdPartyPayment") == 0) {
1341 if (cbor_decode_bool(val, NULL) < 0) {
1342 fido_log_debug("%s: cbor_decode_bool", __func__);
1343 goto out;
1344 }
1345 if (cbor_ctrl_value(val) == CBOR_CTRL_TRUE)
1346 authdata_ext->mask |= FIDO_EXT_PAYMENT;
1347 }
1348
1349 ok = 0;
1350 out:
1351 free(type);
1352
1353 return (ok);
1354 }
1355
1356 static int
1357 decode_assert_extensions(const unsigned char **buf, size_t *len,
1358 fido_assert_extattr_t *authdata_ext)
1359 {
1360 cbor_item_t *item = NULL;
1361 struct cbor_load_result cbor;
1362 int ok = -1;
1363
1364 fido_log_xxd(*buf, *len, "%s", __func__);
1365
1366 if ((item = cbor_load(*buf, *len, &cbor)) == NULL) {
1367 fido_log_debug("%s: cbor_load", __func__);
1368 goto fail;
1369 }
1370
1371 if (cbor_isa_map(item) == false ||
1372 cbor_map_is_definite(item) == false ||
1373 cbor_map_iter(item, authdata_ext, decode_assert_extension) < 0) {
1374 fido_log_debug("%s: cbor type", __func__);
1375 goto fail;
1376 }
1377
1378 *buf += cbor.read;
1379 *len -= cbor.read;
1380
1381 ok = 0;
1382 fail:
1383 if (item != NULL)
1384 cbor_decref(&item);
1385
1386 return (ok);
1387 }
1388
1389 int
1390 cbor_decode_cred_authdata(const cbor_item_t *item, int cose_alg,
1391 fido_blob_t *authdata_cbor, fido_authdata_t *authdata,
1392 fido_attcred_t *attcred, fido_cred_extout_t *authdata_ext)
1393 {
1394 const unsigned char *buf = NULL;
1395 size_t len;
1396 size_t alloc_len;
1397
1398 if (cbor_isa_bytestring(item) == false ||
1399 cbor_bytestring_is_definite(item) == false) {
1400 fido_log_debug("%s: cbor type", __func__);
1401 return (-1);
1402 }
1403
1404 if (authdata_cbor->ptr != NULL ||
1405 (authdata_cbor->len = cbor_serialize_alloc(item,
1406 &authdata_cbor->ptr, &alloc_len)) == 0) {
1407 fido_log_debug("%s: cbor_serialize_alloc", __func__);
1408 return (-1);
1409 }
1410
1411 buf = cbor_bytestring_handle(item);
1412 len = cbor_bytestring_length(item);
1413 fido_log_xxd(buf, len, "%s", __func__);
1414
1415 if (fido_buf_read(&buf, &len, authdata, sizeof(*authdata)) < 0) {
1416 fido_log_debug("%s: fido_buf_read", __func__);
1417 return (-1);
1418 }
1419
1420 authdata->sigcount = be32toh(authdata->sigcount);
1421
1422 if (attcred != NULL) {
1423 if ((authdata->flags & CTAP_AUTHDATA_ATT_CRED) == 0 ||
1424 decode_attcred(&buf, &len, cose_alg, attcred) < 0)
1425 return (-1);
1426 }
1427
1428 if (authdata_ext != NULL) {
1429 if ((authdata->flags & CTAP_AUTHDATA_EXT_DATA) != 0 &&
1430 decode_cred_extensions(&buf, &len, authdata_ext) < 0)
1431 return (-1);
1432 }
1433
1434 /* XXX we should probably ensure that len == 0 at this point */
1435
1436 return (FIDO_OK);
1437 }
1438
1439 int
1440 cbor_decode_assert_authdata(const cbor_item_t *item, fido_blob_t *authdata_cbor,
1441 fido_authdata_t *authdata, fido_assert_extattr_t *authdata_ext)
1442 {
1443 const unsigned char *buf = NULL;
1444 size_t len;
1445 size_t alloc_len;
1446
1447 if (cbor_isa_bytestring(item) == false ||
1448 cbor_bytestring_is_definite(item) == false) {
1449 fido_log_debug("%s: cbor type", __func__);
1450 return (-1);
1451 }
1452
1453 if (authdata_cbor->ptr != NULL ||
1454 (authdata_cbor->len = cbor_serialize_alloc(item,
1455 &authdata_cbor->ptr, &alloc_len)) == 0) {
1456 fido_log_debug("%s: cbor_serialize_alloc", __func__);
1457 return (-1);
1458 }
1459
1460 buf = cbor_bytestring_handle(item);
1461 len = cbor_bytestring_length(item);
1462
1463 fido_log_debug("%s: buf=%p, len=%zu", __func__, (const void *)buf, len);
1464
1465 if (fido_buf_read(&buf, &len, authdata, sizeof(*authdata)) < 0) {
1466 fido_log_debug("%s: fido_buf_read", __func__);
1467 return (-1);
1468 }
1469
1470 authdata->sigcount = be32toh(authdata->sigcount);
1471
1472 if ((authdata->flags & CTAP_AUTHDATA_EXT_DATA) != 0) {
1473 if (decode_assert_extensions(&buf, &len, authdata_ext) < 0) {
1474 fido_log_debug("%s: decode_assert_extensions",
1475 __func__);
1476 return (-1);
1477 }
1478 }
1479
1480 /* XXX we should probably ensure that len == 0 at this point */
1481
1482 return (FIDO_OK);
1483 }
1484
1485 static int
1486 decode_x5c(const cbor_item_t *item, void *arg)
1487 {
1488 fido_blob_array_t *x5c = arg;
1489 fido_blob_t *list_ptr = NULL;
1490 fido_blob_t x5c_blob;
1491
1492 memset(&x5c_blob, 0, sizeof(x5c_blob));
1493
1494 if (fido_blob_decode(item, &x5c_blob) < 0) {
1495 fido_log_debug("%s: fido_blob_decode", __func__);
1496 return (-1);
1497 }
1498
1499 if (x5c->len == SIZE_MAX) {
1500 fido_blob_reset(&x5c_blob);
1501 return (-1);
1502 }
1503
1504 if ((list_ptr = recallocarray(x5c->ptr, x5c->len,
1505 x5c->len + 1, sizeof(x5c_blob))) == NULL) {
1506 fido_blob_reset(&x5c_blob);
1507 return (-1);
1508 }
1509
1510 list_ptr[x5c->len++] = x5c_blob;
1511 x5c->ptr = list_ptr;
1512
1513 return (0);
1514 }
1515
1516 static int
1517 decode_x5c_array(const cbor_item_t *item, fido_blob_array_t *arr)
1518 {
1519 if (arr->len) {
1520 fido_log_debug("%s: dup", __func__);
1521 return (-1);
1522 }
1523 if (cbor_isa_array(item) == false ||
1524 cbor_array_is_definite(item) == false) {
1525 fido_log_debug("%s: cbor", __func__);
1526 return (-1);
1527 }
1528 return (cbor_array_iter(item, arr, decode_x5c));
1529 }
1530
1531 static int
1532 decode_attstmt_entry(const cbor_item_t *key, const cbor_item_t *val, void *arg)
1533 {
1534 fido_attstmt_t *attstmt = arg;
1535 char *name = NULL;
1536 int ok = -1;
1537
1538 if (cbor_string_copy(key, &name) < 0) {
1539 fido_log_debug("%s: cbor type", __func__);
1540 ok = 0; /* ignore */
1541 goto out;
1542 }
1543
1544 if (!strcmp(name, "alg")) {
1545 if (cbor_isa_negint(val) == false ||
1546 cbor_get_int(val) > UINT16_MAX) {
1547 fido_log_debug("%s: alg", __func__);
1548 goto out;
1549 }
1550 attstmt->alg = -(int)cbor_get_int(val) - 1;
1551 if (attstmt->alg != COSE_ES256 && attstmt->alg != COSE_ES384 &&
1552 attstmt->alg != COSE_RS256 && attstmt->alg != COSE_EDDSA &&
1553 attstmt->alg != COSE_RS1) {
1554 fido_log_debug("%s: unsupported attstmt->alg=%d",
1555 __func__, attstmt->alg);
1556 goto out;
1557 }
1558 } else if (!strcmp(name, "sig")) {
1559 if (fido_blob_decode(val, &attstmt->sig) < 0) {
1560 fido_log_debug("%s: sig", __func__);
1561 goto out;
1562 }
1563 } else if (!strcmp(name, "x5c")) {
1564 if (decode_x5c_array(val, &attstmt->x5c)) {
1565 fido_log_debug("%s: x5c", __func__);
1566 goto out;
1567 }
1568 } else if (!strcmp(name, "certInfo")) {
1569 if (fido_blob_decode(val, &attstmt->certinfo) < 0) {
1570 fido_log_debug("%s: certinfo", __func__);
1571 goto out;
1572 }
1573 } else if (!strcmp(name, "pubArea")) {
1574 if (fido_blob_decode(val, &attstmt->pubarea) < 0) {
1575 fido_log_debug("%s: pubarea", __func__);
1576 goto out;
1577 }
1578 }
1579
1580 ok = 0;
1581 out:
1582 free(name);
1583
1584 return (ok);
1585 }
1586
1587 int
1588 cbor_decode_attstmt(const cbor_item_t *item, fido_attstmt_t *attstmt)
1589 {
1590 size_t alloc_len;
1591
1592 if (cbor_isa_map(item) == false ||
1593 cbor_map_is_definite(item) == false ||
1594 cbor_map_iter(item, attstmt, decode_attstmt_entry) < 0) {
1595 fido_log_debug("%s: cbor type", __func__);
1596 return (-1);
1597 }
1598
1599 if (attstmt->cbor.ptr != NULL ||
1600 (attstmt->cbor.len = cbor_serialize_alloc(item,
1601 &attstmt->cbor.ptr, &alloc_len)) == 0) {
1602 fido_log_debug("%s: cbor_serialize_alloc", __func__);
1603 return (-1);
1604 }
1605
1606 return (0);
1607 }
1608
1609 int
1610 cbor_decode_uint64(const cbor_item_t *item, uint64_t *n)
1611 {
1612 if (cbor_isa_uint(item) == false) {
1613 fido_log_debug("%s: cbor type", __func__);
1614 return (-1);
1615 }
1616
1617 *n = cbor_get_int(item);
1618
1619 return (0);
1620 }
1621
1622 static int
1623 decode_cred_id_entry(const cbor_item_t *key, const cbor_item_t *val, void *arg)
1624 {
1625 fido_blob_t *id = arg;
1626 char *name = NULL;
1627 int ok = -1;
1628
1629 if (cbor_string_copy(key, &name) < 0) {
1630 fido_log_debug("%s: cbor type", __func__);
1631 ok = 0; /* ignore */
1632 goto out;
1633 }
1634
1635 if (!strcmp(name, "id"))
1636 if (fido_blob_decode(val, id) < 0) {
1637 fido_log_debug("%s: cbor_bytestring_copy", __func__);
1638 goto out;
1639 }
1640
1641 ok = 0;
1642 out:
1643 free(name);
1644
1645 return (ok);
1646 }
1647
1648 int
1649 cbor_decode_cred_id(const cbor_item_t *item, fido_blob_t *id)
1650 {
1651 if (cbor_isa_map(item) == false ||
1652 cbor_map_is_definite(item) == false ||
1653 cbor_map_iter(item, id, decode_cred_id_entry) < 0) {
1654 fido_log_debug("%s: cbor type", __func__);
1655 return (-1);
1656 }
1657
1658 return (0);
1659 }
1660
1661 static int
1662 decode_user_entry(const cbor_item_t *key, const cbor_item_t *val, void *arg)
1663 {
1664 fido_user_t *user = arg;
1665 char *name = NULL;
1666 int ok = -1;
1667
1668 if (cbor_string_copy(key, &name) < 0) {
1669 fido_log_debug("%s: cbor type", __func__);
1670 ok = 0; /* ignore */
1671 goto out;
1672 }
1673
1674 if (!strcmp(name, "icon")) {
1675 if (cbor_string_copy(val, &user->icon) < 0) {
1676 fido_log_debug("%s: icon", __func__);
1677 goto out;
1678 }
1679 } else if (!strcmp(name, "name")) {
1680 if (cbor_string_copy(val, &user->name) < 0) {
1681 fido_log_debug("%s: name", __func__);
1682 goto out;
1683 }
1684 } else if (!strcmp(name, "displayName")) {
1685 if (cbor_string_copy(val, &user->display_name) < 0) {
1686 fido_log_debug("%s: display_name", __func__);
1687 goto out;
1688 }
1689 } else if (!strcmp(name, "id")) {
1690 if (fido_blob_decode(val, &user->id) < 0) {
1691 fido_log_debug("%s: id", __func__);
1692 goto out;
1693 }
1694 }
1695
1696 ok = 0;
1697 out:
1698 free(name);
1699
1700 return (ok);
1701 }
1702
1703 int
1704 cbor_decode_user(const cbor_item_t *item, fido_user_t *user)
1705 {
1706 if (cbor_isa_map(item) == false ||
1707 cbor_map_is_definite(item) == false ||
1708 cbor_map_iter(item, user, decode_user_entry) < 0) {
1709 fido_log_debug("%s: cbor type", __func__);
1710 return (-1);
1711 }
1712
1713 return (0);
1714 }
1715
1716 static int
1717 decode_rp_entity_entry(const cbor_item_t *key, const cbor_item_t *val,
1718 void *arg)
1719 {
1720 fido_rp_t *rp = arg;
1721 char *name = NULL;
1722 int ok = -1;
1723
1724 if (cbor_string_copy(key, &name) < 0) {
1725 fido_log_debug("%s: cbor type", __func__);
1726 ok = 0; /* ignore */
1727 goto out;
1728 }
1729
1730 if (!strcmp(name, "id")) {
1731 if (cbor_string_copy(val, &rp->id) < 0) {
1732 fido_log_debug("%s: id", __func__);
1733 goto out;
1734 }
1735 } else if (!strcmp(name, "name")) {
1736 if (cbor_string_copy(val, &rp->name) < 0) {
1737 fido_log_debug("%s: name", __func__);
1738 goto out;
1739 }
1740 }
1741
1742 ok = 0;
1743 out:
1744 free(name);
1745
1746 return (ok);
1747 }
1748
1749 int
1750 cbor_decode_rp_entity(const cbor_item_t *item, fido_rp_t *rp)
1751 {
1752 if (cbor_isa_map(item) == false ||
1753 cbor_map_is_definite(item) == false ||
1754 cbor_map_iter(item, rp, decode_rp_entity_entry) < 0) {
1755 fido_log_debug("%s: cbor type", __func__);
1756 return (-1);
1757 }
1758
1759 return (0);
1760 }
1761
1762 int
1763 cbor_decode_bool(const cbor_item_t *item, bool *v)
1764 {
1765 if (cbor_isa_float_ctrl(item) == false ||
1766 cbor_float_get_width(item) != CBOR_FLOAT_0 ||
1767 cbor_is_bool(item) == false) {
1768 fido_log_debug("%s: cbor type", __func__);
1769 return (-1);
1770 }
1771
1772 if (v != NULL)
1773 *v = cbor_ctrl_value(item) == CBOR_CTRL_TRUE;
1774
1775 return (0);
1776 }
1777
1778 cbor_item_t *
1779 cbor_build_uint(const uint64_t value)
1780 {
1781 if (value <= UINT8_MAX)
1782 return cbor_build_uint8((uint8_t)value);
1783 else if (value <= UINT16_MAX)
1784 return cbor_build_uint16((uint16_t)value);
1785 else if (value <= UINT32_MAX)
1786 return cbor_build_uint32((uint32_t)value);
1787
1788 return cbor_build_uint64(value);
1789 }
1790
1791 int
1792 cbor_array_append(cbor_item_t **array, cbor_item_t *item)
1793 {
1794 cbor_item_t **v, *ret;
1795 size_t n;
1796
1797 if ((v = cbor_array_handle(*array)) == NULL ||
1798 (n = cbor_array_size(*array)) == SIZE_MAX ||
1799 (ret = cbor_new_definite_array(n + 1)) == NULL)
1800 return -1;
1801 for (size_t i = 0; i < n; i++) {
1802 if (cbor_array_push(ret, v[i]) == 0) {
1803 cbor_decref(&ret);
1804 return -1;
1805 }
1806 }
1807 if (cbor_array_push(ret, item) == 0) {
1808 cbor_decref(&ret);
1809 return -1;
1810 }
1811 cbor_decref(array);
1812 *array = ret;
1813
1814 return 0;
1815 }
1816
1817 int
1818 cbor_array_drop(cbor_item_t **array, size_t idx)
1819 {
1820 cbor_item_t **v, *ret;
1821 size_t n;
1822
1823 if ((v = cbor_array_handle(*array)) == NULL ||
1824 (n = cbor_array_size(*array)) == 0 || idx >= n ||
1825 (ret = cbor_new_definite_array(n - 1)) == NULL)
1826 return -1;
1827 for (size_t i = 0; i < n; i++) {
1828 if (i != idx && cbor_array_push(ret, v[i]) == 0) {
1829 cbor_decref(&ret);
1830 return -1;
1831 }
1832 }
1833 cbor_decref(array);
1834 *array = ret;
1835
1836 return 0;
1837 }