Coverage Report

Created: 2024-06-03 09:43

/libfido2/src/info.c
Line
Count
Source (jump to first uncovered line)
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 "fido.h"
9
10
static int
11
decode_string(const cbor_item_t *item, void *arg)
12
62.3k
{
13
62.3k
        fido_str_array_t        *a = arg;
14
62.3k
        const size_t             i = a->len;
15
16
        /* keep ptr[x] and len consistent */
17
62.3k
        if (cbor_string_copy(item, &a->ptr[i]) < 0) {
18
118
                fido_log_debug("%s: cbor_string_copy", __func__);
19
118
                return (-1);
20
118
        }
21
22
62.2k
        a->len++;
23
24
62.2k
        return (0);
25
62.3k
}
26
27
static int
28
decode_string_array(const cbor_item_t *item, fido_str_array_t *v)
29
26.1k
{
30
26.1k
        v->ptr = NULL;
31
26.1k
        v->len = 0;
32
33
26.1k
        if (cbor_isa_array(item) == false ||
34
26.1k
            cbor_array_is_definite(item) == false) {
35
77
                fido_log_debug("%s: cbor type", __func__);
36
77
                return (-1);
37
77
        }
38
39
26.0k
        v->ptr = calloc(cbor_array_size(item), sizeof(char *));
40
26.0k
        if (v->ptr == NULL)
41
18
                return (-1);
42
43
26.0k
        if (cbor_array_iter(item, v, decode_string) < 0) {
44
130
                fido_log_debug("%s: decode_string", __func__);
45
130
                return (-1);
46
130
        }
47
48
25.8k
        return (0);
49
26.0k
}
50
51
static int
52
decode_aaguid(const cbor_item_t *item, unsigned char *aaguid, size_t aaguid_len)
53
11.6k
{
54
11.6k
        if (cbor_isa_bytestring(item) == false ||
55
11.6k
            cbor_bytestring_is_definite(item) == false ||
56
11.6k
            cbor_bytestring_length(item) != aaguid_len) {
57
130
                fido_log_debug("%s: cbor type", __func__);
58
130
                return (-1);
59
130
        }
60
61
11.5k
        memcpy(aaguid, cbor_bytestring_handle(item), aaguid_len);
62
63
11.5k
        return (0);
64
11.6k
}
65
66
static int
67
decode_option(const cbor_item_t *key, const cbor_item_t *val, void *arg)
68
61.3k
{
69
61.3k
        fido_opt_array_t        *o = arg;
70
61.3k
        const size_t             i = o->len;
71
72
61.3k
        if (cbor_decode_bool(val, NULL) < 0) {
73
3.69k
                fido_log_debug("%s: cbor_decode_bool", __func__);
74
3.69k
                return (0); /* ignore */
75
3.69k
        }
76
77
57.6k
        if (cbor_string_copy(key, &o->name[i]) < 0) {
78
211
                fido_log_debug("%s: cbor_string_copy", __func__);
79
211
                return (0); /* ignore */
80
211
        }
81
82
        /* keep name/value and len consistent */
83
57.4k
        o->value[i] = cbor_ctrl_value(val) == CBOR_CTRL_TRUE;
84
57.4k
        o->len++;
85
86
57.4k
        return (0);
87
57.6k
}
88
89
static int
90
decode_options(const cbor_item_t *item, fido_opt_array_t *o)
91
11.3k
{
92
11.3k
        o->name = NULL;
93
11.3k
        o->value = NULL;
94
11.3k
        o->len = 0;
95
96
11.3k
        if (cbor_isa_map(item) == false ||
97
11.3k
            cbor_map_is_definite(item) == false) {
98
46
                fido_log_debug("%s: cbor type", __func__);
99
46
                return (-1);
100
46
        }
101
102
11.2k
        o->name = calloc(cbor_map_size(item), sizeof(char *));
103
11.2k
        o->value = calloc(cbor_map_size(item), sizeof(bool));
104
11.2k
        if (o->name == NULL || o->value == NULL)
105
37
                return (-1);
106
107
11.2k
        return (cbor_map_iter(item, o, decode_option));
108
11.2k
}
109
110
static int
111
decode_protocol(const cbor_item_t *item, void *arg)
112
14.4k
{
113
14.4k
        fido_byte_array_t       *p = arg;
114
14.4k
        const size_t             i = p->len;
115
116
14.4k
        if (cbor_isa_uint(item) == false ||
117
14.4k
            cbor_int_get_width(item) != CBOR_INT_8) {
118
54
                fido_log_debug("%s: cbor type", __func__);
119
54
                return (-1);
120
54
        }
121
122
        /* keep ptr[x] and len consistent */
123
14.3k
        p->ptr[i] = cbor_get_uint8(item);
124
14.3k
        p->len++;
125
126
14.3k
        return (0);
127
14.4k
}
128
129
static int
130
decode_protocols(const cbor_item_t *item, fido_byte_array_t *p)
131
11.3k
{
132
11.3k
        p->ptr = NULL;
133
11.3k
        p->len = 0;
134
135
11.3k
        if (cbor_isa_array(item) == false ||
136
11.3k
            cbor_array_is_definite(item) == false) {
137
51
                fido_log_debug("%s: cbor type", __func__);
138
51
                return (-1);
139
51
        }
140
141
11.2k
        p->ptr = calloc(cbor_array_size(item), sizeof(uint8_t));
142
11.2k
        if (p->ptr == NULL)
143
19
                return (-1);
144
145
11.2k
        if (cbor_array_iter(item, p, decode_protocol) < 0) {
146
62
                fido_log_debug("%s: decode_protocol", __func__);
147
62
                return (-1);
148
62
        }
149
150
11.2k
        return (0);
151
11.2k
}
152
153
static int
154
decode_algorithm_entry(const cbor_item_t *key, const cbor_item_t *val,
155
    void *arg)
156
33.6k
{
157
33.6k
        fido_algo_t *alg = arg;
158
33.6k
        char *name = NULL;
159
33.6k
        int ok = -1;
160
161
33.6k
        if (cbor_string_copy(key, &name) < 0) {
162
394
                fido_log_debug("%s: cbor type", __func__);
163
394
                ok = 0; /* ignore */
164
394
                goto out;
165
394
        }
166
167
33.2k
        if (!strcmp(name, "alg")) {
168
14.4k
                if (cbor_isa_negint(val) == false ||
169
14.4k
                    cbor_get_int(val) > INT_MAX || alg->cose != 0) {
170
420
                        fido_log_debug("%s: alg", __func__);
171
420
                        goto out;
172
420
                }
173
13.9k
                alg->cose = -(int)cbor_get_int(val) - 1;
174
18.8k
        } else if (!strcmp(name, "type")) {
175
12.6k
                if (cbor_string_copy(val, &alg->type) < 0) {
176
39
                        fido_log_debug("%s: type", __func__);
177
39
                        goto out;
178
39
                }
179
12.6k
        }
180
181
32.8k
        ok = 0;
182
33.6k
out:
183
33.6k
        free(name);
184
185
33.6k
        return (ok);
186
32.8k
}
187
188
static int
189
decode_algorithm(const cbor_item_t *item, void *arg)
190
18.0k
{
191
18.0k
        fido_algo_array_t *aa = arg;
192
18.0k
        const size_t i = aa->len;
193
194
18.0k
        if (cbor_isa_map(item) == false ||
195
18.0k
            cbor_map_is_definite(item) == false) {
196
104
                fido_log_debug("%s: cbor type", __func__);
197
104
                return (-1);
198
104
        }
199
200
17.9k
        memset(&aa->ptr[i], 0, sizeof(aa->ptr[i]));
201
202
17.9k
        if (cbor_map_iter(item, &aa->ptr[i], decode_algorithm_entry) < 0) {
203
696
                fido_log_debug("%s: decode_algorithm_entry", __func__);
204
696
                fido_algo_free(&aa->ptr[i]);
205
696
                return (-1);
206
696
        }
207
208
        /* keep ptr[x] and len consistent */
209
17.2k
        aa->len++;
210
211
17.2k
        return (0);
212
17.9k
}
213
214
static int
215
decode_algorithms(const cbor_item_t *item, fido_algo_array_t *aa)
216
9.22k
{
217
9.22k
        aa->ptr = NULL;
218
9.22k
        aa->len = 0;
219
220
9.22k
        if (cbor_isa_array(item) == false ||
221
9.22k
            cbor_array_is_definite(item) == false) {
222
47
                fido_log_debug("%s: cbor type", __func__);
223
47
                return (-1);
224
47
        }
225
226
9.17k
        aa->ptr = calloc(cbor_array_size(item), sizeof(fido_algo_t));
227
9.17k
        if (aa->ptr == NULL)
228
18
                return (-1);
229
230
9.16k
        if (cbor_array_iter(item, aa, decode_algorithm) < 0) {
231
805
                fido_log_debug("%s: decode_algorithm", __func__);
232
805
                return (-1);
233
805
        }
234
235
8.35k
        return (0);
236
9.16k
}
237
238
static int
239
decode_cert(const cbor_item_t *key, const cbor_item_t *val, void *arg)
240
1.76k
{
241
1.76k
        fido_cert_array_t       *c = arg;
242
1.76k
        const size_t             i = c->len;
243
244
1.76k
        if (cbor_is_int(val) == false) {
245
446
                fido_log_debug("%s: cbor_is_int", __func__);
246
446
                return (0); /* ignore */
247
446
        }
248
249
1.32k
        if (cbor_string_copy(key, &c->name[i]) < 0) {
250
255
                fido_log_debug("%s: cbor_string_copy", __func__);
251
255
                return (0); /* ignore */
252
255
        }
253
254
        /* keep name/value and len consistent */
255
1.06k
        c->value[i] = cbor_get_int(val);
256
1.06k
        c->len++;
257
258
1.06k
        return (0);
259
1.32k
}
260
261
static int
262
decode_certs(const cbor_item_t *item, fido_cert_array_t *c)
263
693
{
264
693
        c->name = NULL;
265
693
        c->value = NULL;
266
693
        c->len = 0;
267
268
693
        if (cbor_isa_map(item) == false ||
269
693
            cbor_map_is_definite(item) == false) {
270
51
                fido_log_debug("%s: cbor type", __func__);
271
51
                return (-1);
272
51
        }
273
274
642
        c->name = calloc(cbor_map_size(item), sizeof(char *));
275
642
        c->value = calloc(cbor_map_size(item), sizeof(uint64_t));
276
642
        if (c->name == NULL || c->value == NULL)
277
25
                return (-1);
278
279
617
        return (cbor_map_iter(item, c, decode_cert));
280
642
}
281
282
static int
283
parse_reply_element(const cbor_item_t *key, const cbor_item_t *val, void *arg)
284
116k
{
285
116k
        fido_cbor_info_t *ci = arg;
286
116k
        uint64_t x;
287
288
116k
        if (cbor_isa_uint(key) == false ||
289
116k
            cbor_int_get_width(key) != CBOR_INT_8) {
290
3.30k
                fido_log_debug("%s: cbor type", __func__);
291
3.30k
                return (0); /* ignore */
292
3.30k
        }
293
294
112k
        switch (cbor_get_uint8(key)) {
295
11.6k
        case 1: /* versions */
296
11.6k
                return (decode_string_array(val, &ci->versions));
297
13.1k
        case 2: /* extensions */
298
13.1k
                return (decode_string_array(val, &ci->extensions));
299
11.6k
        case 3: /* aaguid */
300
11.6k
                return (decode_aaguid(val, ci->aaguid, sizeof(ci->aaguid)));
301
11.3k
        case 4: /* options */
302
11.3k
                return (decode_options(val, &ci->options));
303
12.0k
        case 5: /* maxMsgSize */
304
12.0k
                return (cbor_decode_uint64(val, &ci->maxmsgsiz));
305
11.3k
        case 6: /* pinProtocols */
306
11.3k
                return (decode_protocols(val, &ci->protocols));
307
10.7k
        case 7: /* maxCredentialCountInList */
308
10.7k
                return (cbor_decode_uint64(val, &ci->maxcredcntlst));
309
10.2k
        case 8: /* maxCredentialIdLength */
310
10.2k
                return (cbor_decode_uint64(val, &ci->maxcredidlen));
311
1.32k
        case 9: /* transports */
312
1.32k
                return (decode_string_array(val, &ci->transports));
313
9.22k
        case 10: /* algorithms */
314
9.22k
                return (decode_algorithms(val, &ci->algorithms));
315
524
        case 11: /* maxSerializedLargeBlobArray */
316
524
                return (cbor_decode_uint64(val, &ci->maxlargeblob));
317
564
        case 12: /* forcePINChange */
318
564
                return (cbor_decode_bool(val, &ci->new_pin_reqd));
319
1.00k
        case 13: /* minPINLength */
320
1.00k
                return (cbor_decode_uint64(val, &ci->minpinlen));
321
1.17k
        case 14: /* fwVersion */
322
1.17k
                return (cbor_decode_uint64(val, &ci->fwversion));
323
616
        case 15: /* maxCredBlobLen */
324
616
                return (cbor_decode_uint64(val, &ci->maxcredbloblen));
325
530
        case 16: /* maxRPIDsForSetMinPINLength */
326
530
                return (cbor_decode_uint64(val, &ci->maxrpid_minlen));
327
579
        case 17: /* preferredPlatformUvAttempts */
328
579
                return (cbor_decode_uint64(val, &ci->uv_attempts));
329
526
        case 18: /* uvModality */
330
526
                return (cbor_decode_uint64(val, &ci->uv_modality));
331
693
        case 19: /* certifications */
332
693
                return (decode_certs(val, &ci->certs));
333
1.22k
        case 20: /* remainingDiscoverableCredentials */
334
1.22k
                if (cbor_decode_uint64(val, &x) < 0 || x > INT64_MAX) {
335
226
                        fido_log_debug("%s: cbor_decode_uint64", __func__);
336
226
                        return (-1);
337
226
                }
338
997
                ci->rk_remaining = (int64_t)x;
339
997
                return (0);
340
2.64k
        default: /* ignore */
341
2.64k
                fido_log_debug("%s: cbor type: 0x%02x", __func__, cbor_get_uint8(key));
342
2.64k
                return (0);
343
112k
        }
344
112k
}
345
346
static int
347
fido_dev_get_cbor_info_tx(fido_dev_t *dev, int *ms)
348
36.8k
{
349
36.8k
        const unsigned char cbor[] = { CTAP_CBOR_GETINFO };
350
351
36.8k
        fido_log_debug("%s: dev=%p", __func__, (void *)dev);
352
353
36.8k
        if (fido_tx(dev, CTAP_CMD_CBOR, cbor, sizeof(cbor), ms) < 0) {
354
378
                fido_log_debug("%s: fido_tx", __func__);
355
378
                return (FIDO_ERR_TX);
356
378
        }
357
358
36.4k
        return (FIDO_OK);
359
36.8k
}
360
361
static int
362
fido_dev_get_cbor_info_rx(fido_dev_t *dev, fido_cbor_info_t *ci, int *ms)
363
36.4k
{
364
36.4k
        unsigned char   *msg;
365
36.4k
        int              msglen;
366
36.4k
        int              r;
367
368
36.4k
        fido_log_debug("%s: dev=%p, ci=%p, ms=%d", __func__, (void *)dev,
369
36.4k
            (void *)ci, *ms);
370
371
36.4k
        fido_cbor_info_reset(ci);
372
373
36.4k
        if ((msg = malloc(FIDO_MAXMSG)) == NULL) {
374
104
                r = FIDO_ERR_INTERNAL;
375
104
                goto out;
376
104
        }
377
378
36.3k
        if ((msglen = fido_rx(dev, CTAP_CMD_CBOR, msg, FIDO_MAXMSG, ms)) < 0) {
379
9.04k
                fido_log_debug("%s: fido_rx", __func__);
380
9.04k
                r = FIDO_ERR_RX;
381
9.04k
                goto out;
382
9.04k
        }
383
384
27.3k
        r = cbor_parse_reply(msg, (size_t)msglen, ci, parse_reply_element);
385
36.4k
out:
386
36.4k
        freezero(msg, FIDO_MAXMSG);
387
388
36.4k
        return (r);
389
27.3k
}
390
391
int
392
fido_dev_get_cbor_info_wait(fido_dev_t *dev, fido_cbor_info_t *ci, int *ms)
393
36.8k
{
394
36.8k
        int r;
395
396
#ifdef USE_WINHELLO
397
        if (dev->flags & FIDO_DEV_WINHELLO)
398
                return (fido_winhello_get_cbor_info(dev, ci));
399
#endif
400
36.8k
        if ((r = fido_dev_get_cbor_info_tx(dev, ms)) != FIDO_OK ||
401
36.8k
            (r = fido_dev_get_cbor_info_rx(dev, ci, ms)) != FIDO_OK)
402
24.2k
                return (r);
403
404
12.6k
        return (FIDO_OK);
405
36.8k
}
406
407
int
408
fido_dev_get_cbor_info(fido_dev_t *dev, fido_cbor_info_t *ci)
409
315
{
410
315
        int ms = dev->timeout_ms;
411
412
315
        return (fido_dev_get_cbor_info_wait(dev, ci, &ms));
413
315
}
414
415
/*
416
 * get/set functions for fido_cbor_info_t; always at the end of the file
417
 */
418
419
fido_cbor_info_t *
420
fido_cbor_info_new(void)
421
36.9k
{
422
36.9k
        fido_cbor_info_t *ci;
423
424
36.9k
        if ((ci = calloc(1, sizeof(fido_cbor_info_t))) == NULL)
425
117
                return (NULL);
426
427
36.8k
        fido_cbor_info_reset(ci);
428
429
36.8k
        return (ci);
430
36.9k
}
431
432
void
433
fido_cbor_info_reset(fido_cbor_info_t *ci)
434
110k
{
435
110k
        fido_str_array_free(&ci->versions);
436
110k
        fido_str_array_free(&ci->extensions);
437
110k
        fido_str_array_free(&ci->transports);
438
110k
        fido_opt_array_free(&ci->options);
439
110k
        fido_byte_array_free(&ci->protocols);
440
110k
        fido_algo_array_free(&ci->algorithms);
441
110k
        fido_cert_array_free(&ci->certs);
442
110k
        ci->rk_remaining = -1;
443
110k
}
444
445
void
446
fido_cbor_info_free(fido_cbor_info_t **ci_p)
447
113k
{
448
113k
        fido_cbor_info_t *ci;
449
450
113k
        if (ci_p == NULL || (ci = *ci_p) ==  NULL)
451
77.1k
                return;
452
36.8k
        fido_cbor_info_reset(ci);
453
36.8k
        free(ci);
454
36.8k
        *ci_p = NULL;
455
36.8k
}
456
457
char **
458
fido_cbor_info_versions_ptr(const fido_cbor_info_t *ci)
459
263
{
460
263
        return (ci->versions.ptr);
461
263
}
462
463
size_t
464
fido_cbor_info_versions_len(const fido_cbor_info_t *ci)
465
578
{
466
578
        return (ci->versions.len);
467
578
}
468
469
char **
470
fido_cbor_info_extensions_ptr(const fido_cbor_info_t *ci)
471
12.7k
{
472
12.7k
        return (ci->extensions.ptr);
473
12.7k
}
474
475
size_t
476
fido_cbor_info_extensions_len(const fido_cbor_info_t *ci)
477
13.0k
{
478
13.0k
        return (ci->extensions.len);
479
13.0k
}
480
481
char **
482
fido_cbor_info_transports_ptr(const fido_cbor_info_t *ci)
483
64
{
484
64
        return (ci->transports.ptr);
485
64
}
486
487
size_t
488
fido_cbor_info_transports_len(const fido_cbor_info_t *ci)
489
379
{
490
379
        return (ci->transports.len);
491
379
}
492
493
const unsigned char *
494
fido_cbor_info_aaguid_ptr(const fido_cbor_info_t *ci)
495
315
{
496
315
        return (ci->aaguid);
497
315
}
498
499
size_t
500
fido_cbor_info_aaguid_len(const fido_cbor_info_t *ci)
501
315
{
502
315
        return (sizeof(ci->aaguid));
503
315
}
504
505
char **
506
fido_cbor_info_options_name_ptr(const fido_cbor_info_t *ci)
507
12.7k
{
508
12.7k
        return (ci->options.name);
509
12.7k
}
510
511
const bool *
512
fido_cbor_info_options_value_ptr(const fido_cbor_info_t *ci)
513
12.7k
{
514
12.7k
        return (ci->options.value);
515
12.7k
}
516
517
size_t
518
fido_cbor_info_options_len(const fido_cbor_info_t *ci)
519
13.0k
{
520
13.0k
        return (ci->options.len);
521
13.0k
}
522
523
uint64_t
524
fido_cbor_info_maxcredbloblen(const fido_cbor_info_t *ci)
525
315
{
526
315
        return (ci->maxcredbloblen);
527
315
}
528
529
uint64_t
530
fido_cbor_info_maxmsgsiz(const fido_cbor_info_t *ci)
531
12.8k
{
532
12.8k
        return (ci->maxmsgsiz);
533
12.8k
}
534
535
uint64_t
536
fido_cbor_info_maxcredcntlst(const fido_cbor_info_t *ci)
537
315
{
538
315
        return (ci->maxcredcntlst);
539
315
}
540
541
uint64_t
542
fido_cbor_info_maxcredidlen(const fido_cbor_info_t *ci)
543
315
{
544
315
        return (ci->maxcredidlen);
545
315
}
546
547
uint64_t
548
fido_cbor_info_maxlargeblob(const fido_cbor_info_t *ci)
549
315
{
550
315
        return (ci->maxlargeblob);
551
315
}
552
553
uint64_t
554
fido_cbor_info_fwversion(const fido_cbor_info_t *ci)
555
315
{
556
315
        return (ci->fwversion);
557
315
}
558
559
uint64_t
560
fido_cbor_info_minpinlen(const fido_cbor_info_t *ci)
561
315
{
562
315
        return (ci->minpinlen);
563
315
}
564
565
uint64_t
566
fido_cbor_info_maxrpid_minpinlen(const fido_cbor_info_t *ci)
567
315
{
568
315
        return (ci->maxrpid_minlen);
569
315
}
570
571
uint64_t
572
fido_cbor_info_uv_attempts(const fido_cbor_info_t *ci)
573
315
{
574
315
        return (ci->uv_attempts);
575
315
}
576
577
uint64_t
578
fido_cbor_info_uv_modality(const fido_cbor_info_t *ci)
579
315
{
580
315
        return (ci->uv_modality);
581
315
}
582
583
int64_t
584
fido_cbor_info_rk_remaining(const fido_cbor_info_t *ci)
585
315
{
586
315
        return (ci->rk_remaining);
587
315
}
588
589
const uint8_t *
590
fido_cbor_info_protocols_ptr(const fido_cbor_info_t *ci)
591
12.8k
{
592
12.8k
        return (ci->protocols.ptr);
593
12.8k
}
594
595
size_t
596
fido_cbor_info_protocols_len(const fido_cbor_info_t *ci)
597
12.8k
{
598
12.8k
        return (ci->protocols.len);
599
12.8k
}
600
601
size_t
602
fido_cbor_info_algorithm_count(const fido_cbor_info_t *ci)
603
682
{
604
682
        return (ci->algorithms.len);
605
682
}
606
607
const char *
608
fido_cbor_info_algorithm_type(const fido_cbor_info_t *ci, size_t idx)
609
367
{
610
367
        if (idx >= ci->algorithms.len)
611
315
                return (NULL);
612
613
52
        return (ci->algorithms.ptr[idx].type);
614
367
}
615
616
int
617
fido_cbor_info_algorithm_cose(const fido_cbor_info_t *ci, size_t idx)
618
367
{
619
367
        if (idx >= ci->algorithms.len)
620
315
                return (0);
621
622
52
        return (ci->algorithms.ptr[idx].cose);
623
367
}
624
625
bool
626
fido_cbor_info_new_pin_required(const fido_cbor_info_t *ci)
627
315
{
628
315
        return (ci->new_pin_reqd);
629
315
}
630
631
char **
632
fido_cbor_info_certs_name_ptr(const fido_cbor_info_t *ci)
633
23
{
634
23
        return (ci->certs.name);
635
23
}
636
637
const uint64_t *
638
fido_cbor_info_certs_value_ptr(const fido_cbor_info_t *ci)
639
23
{
640
23
        return (ci->certs.value);
641
23
}
642
643
size_t
644
fido_cbor_info_certs_len(const fido_cbor_info_t *ci)
645
338
{
646
338
        return (ci->certs.len);
647
338
}