Coverage Report

Created: 2025-10-04 18:33

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