Coverage Report

Created: 2023-09-23 17:42

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