summaryrefslogtreecommitdiffstats
path: root/src/info.c
blob: cd30828d7ce91650807cd2a84cf64b8af046e026 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/*
 * Copyright (c) 2018-2022 Yubico AB. All rights reserved.
 * Use of this source code is governed by a BSD-style
 * license that can be found in the LICENSE file.
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "fido.h"

static int
decode_string(const cbor_item_t *item, void *arg)
{
	fido_str_array_t	*a = arg;
	const size_t		 i = a->len;

	/* keep ptr[x] and len consistent */
	if (cbor_string_copy(item, &a->ptr[i]) < 0) {
		fido_log_debug("%s: cbor_string_copy", __func__);
		return (-1);
	}

	a->len++;

	return (0);
}

static int
decode_string_array(const cbor_item_t *item, fido_str_array_t *v)
{
	v->ptr = NULL;
	v->len = 0;

	if (cbor_isa_array(item) == false ||
	    cbor_array_is_definite(item) == false) {
		fido_log_debug("%s: cbor type", __func__);
		return (-1);
	}

	v->ptr = calloc(cbor_array_size(item), sizeof(char *));
	if (v->ptr == NULL)
		return (-1);

	if (cbor_array_iter(item, v, decode_string) < 0) {
		fido_log_debug("%s: decode_string", __func__);
		return (-1);
	}

	return (0);
}

static int
decode_aaguid(const cbor_item_t *item, unsigned char *aaguid, size_t aaguid_len)
{
	if (cbor_isa_bytestring(item) == false ||
	    cbor_bytestring_is_definite(item) == false ||
	    cbor_bytestring_length(item) != aaguid_len) {
		fido_log_debug("%s: cbor type", __func__);
		return (-1);
	}

	memcpy(aaguid, cbor_bytestring_handle(item), aaguid_len);

	return (0);
}

static int
decode_option(const cbor_item_t *key, const cbor_item_t *val, void *arg)
{
	fido_opt_array_t	*o = arg;
	const size_t		 i = o->len;

	if (cbor_decode_bool(val, NULL) < 0) {
		fido_log_debug("%s: cbor_decode_bool", __func__);
		return (0); /* ignore */
	}

	if (cbor_string_copy(key, &o->name[i]) < 0) {
		fido_log_debug("%s: cbor_string_copy", __func__);
		return (0); /* ignore */
	}

	/* keep name/value and len consistent */
	o->value[i] = cbor_ctrl_value(val) == CBOR_CTRL_TRUE;
	o->len++;

	return (0);
}

static int
decode_options(const cbor_item_t *item, fido_opt_array_t *o)
{
	o->name = NULL;
	o->value = NULL;
	o->len = 0;

	if (cbor_isa_map(item) == false ||
	    cbor_map_is_definite(item) == false) {
		fido_log_debug("%s: cbor type", __func__);
		return (-1);
	}

	o->name = calloc(cbor_map_size(item), sizeof(char *));
	o->value = calloc(cbor_map_size(item), sizeof(bool));
	if (o->name == NULL || o->value == NULL)
		return (-1);

	return (cbor_map_iter(item, o, decode_option));
}

static int
decode_protocol(const cbor_item_t *item, void *arg)
{
	fido_byte_array_t	*p = arg;
	const size_t		 i = p->len;

	if (cbor_isa_uint(item) == false ||
	    cbor_int_get_width(item) != CBOR_INT_8) {
		fido_log_debug("%s: cbor type", __func__);
		return (-1);
	}

	/* keep ptr[x] and len consistent */
	p->ptr[i] = cbor_get_uint8(item);
	p->len++;

	return (0);
}

static int
decode_protocols(const cbor_item_t *item, fido_byte_array_t *p)
{
	p->ptr = NULL;
	p->len = 0;

	if (cbor_isa_array(item) == false ||
	    cbor_array_is_definite(item) == false) {
		fido_log_debug("%s: cbor type", __func__);
		return (-1);
	}

	p->ptr = calloc(cbor_array_size(item), sizeof(uint8_t));
	if (p->ptr == NULL)
		return (-1);

	if (cbor_array_iter(item, p, decode_protocol) < 0) {
		fido_log_debug("%s: decode_protocol", __func__);
		return (-1);
	}

	return (0);
}

static int
decode_algorithm_entry(const cbor_item_t *key, const cbor_item_t *val,
    void *arg)
{
	fido_algo_t *alg = arg;
	char *name = NULL;
	int ok = -1;

	if (cbor_string_copy(key, &name) < 0) {
		fido_log_debug("%s: cbor type", __func__);
		ok = 0; /* ignore */
		goto out;
	}

	if (!strcmp(name, "alg")) {
		if (cbor_isa_negint(val) == false ||
		    cbor_get_int(val) > INT_MAX || alg->cose != 0) {
			fido_log_debug("%s: alg", __func__);
			goto out;
		}
		alg->cose = -(int)cbor_get_int(val) - 1;
	} else if (!strcmp(name, "type")) {
		if (cbor_string_copy(val, &alg->type) < 0) {
			fido_log_debug("%s: type", __func__);
			goto out;
		}
	}

	ok = 0;
out:
	free(name);

	return (ok);
}

static int
decode_algorithm(const cbor_item_t *item, void *arg)
{
	fido_algo_array_t *aa = arg;
	const size_t i = aa->len;

	if (cbor_isa_map(item) == false ||
	    cbor_map_is_definite(item) == false) {
		fido_log_debug("%s: cbor type", __func__);
		return (-1);
	}

	memset(&aa->ptr[i], 0, sizeof(aa->ptr[i]));

	if (cbor_map_iter(item, &aa->ptr[i], decode_algorithm_entry) < 0) {
		fido_log_debug("%s: decode_algorithm_entry", __func__);
		fido_algo_free(&aa->ptr[i]);
		return (-1);
	}

	/* keep ptr[x] and len consistent */
	aa->len++;

	return (0);
}

static int
decode_algorithms(const cbor_item_t *item, fido_algo_array_t *aa)
{
	aa->ptr = NULL;
	aa->len = 0;

	if (cbor_isa_array(item) == false ||
	    cbor_array_is_definite(item) == false) {
		fido_log_debug("%s: cbor type", __func__);
		return (-1);
	}

	aa->ptr = calloc(cbor_array_size(item), sizeof(fido_algo_t));
	if (aa->ptr == NULL)
		return (-1);

	if (cbor_array_iter(item, aa, decode_algorithm) < 0) {
		fido_log_debug("%s: decode_algorithm", __func__);
		return (-1);
	}

	return (0);
}

static int
decode_cert(const cbor_item_t *key, const cbor_item_t *val, void *arg)
{
	fido_cert_array_t	*c = arg;
	const size_t		 i = c->len;

	if (cbor_is_int(val) == false) {
		fido_log_debug("%s: cbor_is_int", __func__);
		return (0); /* ignore */
	}

	if (cbor_string_copy(key, &c->name[i]) < 0) {
		fido_log_debug("%s: cbor_string_copy", __func__);
		return (0); /* ignore */
	}

	/* keep name/value and len consistent */
	c->value[i] = cbor_get_int(val);
	c->len++;

	return (0);
}

static int
decode_certs(const cbor_item_t *item, fido_cert_array_t *c)
{
	c->name = NULL;
	c->value = NULL;
	c->len = 0;

	if (cbor_isa_map(item) == false ||
	    cbor_map_is_definite(item) == false) {
		fido_log_debug("%s: cbor type", __func__);
		return (-1);
	}

	c->name = calloc(cbor_map_size(item), sizeof(char *));
	c->value = calloc(cbor_map_size(item), sizeof(uint64_t));
	if (c->name == NULL || c->value == NULL)
		return (-1);

	return (cbor_map_iter(item, c, decode_cert));
}

static int
parse_reply_element(const cbor_item_t *key, const cbor_item_t *val, void *arg)
{
	fido_cbor_info_t *ci = arg;
	uint64_t x;

	if (cbor_isa_uint(key) == false ||
	    cbor_int_get_width(key) != CBOR_INT_8) {
		fido_log_debug("%s: cbor type", __func__);
		return (0); /* ignore */
	}

	switch (cbor_get_uint8(key)) {
	case 1: /* versions */
		return (decode_string_array(val, &ci->versions));
	case 2: /* extensions */
		return (decode_string_array(val, &ci->extensions));
	case 3: /* aaguid */
		return (decode_aaguid(val, ci->aaguid, sizeof(ci->aaguid)));
	case 4: /* options */
		return (decode_options(val, &ci->options));
	case 5: /* maxMsgSize */
		return (cbor_decode_uint64(val, &ci->maxmsgsiz));
	case 6: /* pinProtocols */
		return (decode_protocols(val, &ci->protocols));
	case 7: /* maxCredentialCountInList */
		return (cbor_decode_uint64(val, &ci->maxcredcntlst));
	case 8: /* maxCredentialIdLength */
		return (cbor_decode_uint64(val, &ci->maxcredidlen));
	case 9: /* transports */
		return (decode_string_array(val, &ci->transports));
	case 10: /* algorithms */
		return (decode_algorithms(val, &ci->algorithms));
	case 11: /* maxSerializedLargeBlobArray */
		return (cbor_decode_uint64(val, &ci->maxlargeblob));
	case 12: /* forcePINChange */
		return (cbor_decode_bool(val, &ci->new_pin_reqd));
	case 13: /* minPINLength */
		return (cbor_decode_uint64(val, &ci->minpinlen));
	case 14: /* fwVersion */
		return (cbor_decode_uint64(val, &ci->fwversion));
	case 15: /* maxCredBlobLen */
		return (cbor_decode_uint64(val, &ci->maxcredbloblen));
	case 16: /* maxRPIDsForSetMinPINLength */
		return (cbor_decode_uint64(val, &ci->maxrpid_minlen));
	case 17: /* preferredPlatformUvAttempts */
		return (cbor_decode_uint64(val, &ci->uv_attempts));
	case 18: /* uvModality */
		return (cbor_decode_uint64(val, &ci->uv_modality));
	case 19: /* certifications */
		return (decode_certs(val, &ci->certs));
	case 20: /* remainingDiscoverableCredentials */
		if (cbor_decode_uint64(val, &x) < 0 || x > INT64_MAX) {
			fido_log_debug("%s: cbor_decode_uint64", __func__);
			return (-1);
		}
		ci->rk_remaining = (int64_t)x;
		return (0);
	default: /* ignore */
		fido_log_debug("%s: cbor type: 0x%02x", __func__, cbor_get_uint8(key));
		return (0);
	}
}

static int
fido_dev_get_cbor_info_tx(fido_dev_t *dev, int *ms)
{
	const unsigned char cbor[] = { CTAP_CBOR_GETINFO };

	fido_log_debug("%s: dev=%p", __func__, (void *)dev);

	if (fido_tx(dev, CTAP_CMD_CBOR, cbor, sizeof(cbor), ms) < 0) {
		fido_log_debug("%s: fido_tx", __func__);
		return (FIDO_ERR_TX);
	}

	return (FIDO_OK);
}

static int
fido_dev_get_cbor_info_rx(fido_dev_t *dev, fido_cbor_info_t *ci, int *ms)
{
	unsigned char	*msg;
	int		 msglen;
	int		 r;

	fido_log_debug("%s: dev=%p, ci=%p, ms=%d", __func__, (void *)dev,
	    (void *)ci, *ms);

	fido_cbor_info_reset(ci);

	if ((msg = malloc(FIDO_MAXMSG)) == NULL) {
		r = FIDO_ERR_INTERNAL;
		goto out;
	}

	if ((msglen = fido_rx(dev, CTAP_CMD_CBOR, msg, FIDO_MAXMSG, ms)) < 0) {
		fido_log_debug("%s: fido_rx", __func__);
		r = FIDO_ERR_RX;
		goto out;
	}

	r = cbor_parse_reply(msg, (size_t)msglen, ci, parse_reply_element);
out:
	freezero(msg, FIDO_MAXMSG);

	return (r);
}

int
fido_dev_get_cbor_info_wait(fido_dev_t *dev, fido_cbor_info_t *ci, int *ms)
{
	int r;

#ifdef USE_WINHELLO
	if (dev->flags & FIDO_DEV_WINHELLO)
		return (fido_winhello_get_cbor_info(dev, ci));
#endif
	if ((r = fido_dev_get_cbor_info_tx(dev, ms)) != FIDO_OK ||
	    (r = fido_dev_get_cbor_info_rx(dev, ci, ms)) != FIDO_OK)
		return (r);

	return (FIDO_OK);
}

int
fido_dev_get_cbor_info(fido_dev_t *dev, fido_cbor_info_t *ci)
{
	int ms = dev->timeout_ms;

	return (fido_dev_get_cbor_info_wait(dev, ci, &ms));
}

/*
 * get/set functions for fido_cbor_info_t; always at the end of the file
 */

fido_cbor_info_t *
fido_cbor_info_new(void)
{
	fido_cbor_info_t *ci;

	if ((ci = calloc(1, sizeof(fido_cbor_info_t))) == NULL)
		return (NULL);

	fido_cbor_info_reset(ci);

	return (ci);
}

void
fido_cbor_info_reset(fido_cbor_info_t *ci)
{
	fido_str_array_free(&ci->versions);
	fido_str_array_free(&ci->extensions);
	fido_str_array_free(&ci->transports);
	fido_opt_array_free(&ci->options);
	fido_byte_array_free(&ci->protocols);
	fido_algo_array_free(&ci->algorithms);
	fido_cert_array_free(&ci->certs);
	ci->rk_remaining = -1;
}

void
fido_cbor_info_free(fido_cbor_info_t **ci_p)
{
	fido_cbor_info_t *ci;

	if (ci_p == NULL || (ci = *ci_p) ==  NULL)
		return;
	fido_cbor_info_reset(ci);
	free(ci);
	*ci_p = NULL;
}

char **
fido_cbor_info_versions_ptr(const fido_cbor_info_t *ci)
{
	return (ci->versions.ptr);
}

size_t
fido_cbor_info_versions_len(const fido_cbor_info_t *ci)
{
	return (ci->versions.len);
}

char **
fido_cbor_info_extensions_ptr(const fido_cbor_info_t *ci)
{
	return (ci->extensions.ptr);
}

size_t
fido_cbor_info_extensions_len(const fido_cbor_info_t *ci)
{
	return (ci->extensions.len);
}

char **
fido_cbor_info_transports_ptr(const fido_cbor_info_t *ci)
{
	return (ci->transports.ptr);
}

size_t
fido_cbor_info_transports_len(const fido_cbor_info_t *ci)
{
	return (ci->transports.len);
}

const unsigned char *
fido_cbor_info_aaguid_ptr(const fido_cbor_info_t *ci)
{
	return (ci->aaguid);
}

size_t
fido_cbor_info_aaguid_len(const fido_cbor_info_t *ci)
{
	return (sizeof(ci->aaguid));
}

char **
fido_cbor_info_options_name_ptr(const fido_cbor_info_t *ci)
{
	return (ci->options.name);
}

const bool *
fido_cbor_info_options_value_ptr(const fido_cbor_info_t *ci)
{
	return (ci->options.value);
}

size_t
fido_cbor_info_options_len(const fido_cbor_info_t *ci)
{
	return (ci->options.len);
}

uint64_t
fido_cbor_info_maxcredbloblen(const fido_cbor_info_t *ci)
{
	return (ci->maxcredbloblen);
}

uint64_t
fido_cbor_info_maxmsgsiz(const fido_cbor_info_t *ci)
{
	return (ci->maxmsgsiz);
}

uint64_t
fido_cbor_info_maxcredcntlst(const fido_cbor_info_t *ci)
{
	return (ci->maxcredcntlst);
}

uint64_t
fido_cbor_info_maxcredidlen(const fido_cbor_info_t *ci)
{
	return (ci->maxcredidlen);
}

uint64_t
fido_cbor_info_maxlargeblob(const fido_cbor_info_t *ci)
{
	return (ci->maxlargeblob);
}

uint64_t
fido_cbor_info_fwversion(const fido_cbor_info_t *ci)
{
	return (ci->fwversion);
}

uint64_t
fido_cbor_info_minpinlen(const fido_cbor_info_t *ci)
{
	return (ci->minpinlen);
}

uint64_t
fido_cbor_info_maxrpid_minpinlen(const fido_cbor_info_t *ci)
{
	return (ci->maxrpid_minlen);
}

uint64_t
fido_cbor_info_uv_attempts(const fido_cbor_info_t *ci)
{
	return (ci->uv_attempts);
}

uint64_t
fido_cbor_info_uv_modality(const fido_cbor_info_t *ci)
{
	return (ci->uv_modality);
}

int64_t
fido_cbor_info_rk_remaining(const fido_cbor_info_t *ci)
{
	return (ci->rk_remaining);
}

const uint8_t *
fido_cbor_info_protocols_ptr(const fido_cbor_info_t *ci)
{
	return (ci->protocols.ptr);
}

size_t
fido_cbor_info_protocols_len(const fido_cbor_info_t *ci)
{
	return (ci->protocols.len);
}

size_t
fido_cbor_info_algorithm_count(const fido_cbor_info_t *ci)
{
	return (ci->algorithms.len);
}

const char *
fido_cbor_info_algorithm_type(const fido_cbor_info_t *ci, size_t idx)
{
	if (idx >= ci->algorithms.len)
		return (NULL);

	return (ci->algorithms.ptr[idx].type);
}

int
fido_cbor_info_algorithm_cose(const fido_cbor_info_t *ci, size_t idx)
{
	if (idx >= ci->algorithms.len)
		return (0);

	return (ci->algorithms.ptr[idx].cose);
}

bool
fido_cbor_info_new_pin_required(const fido_cbor_info_t *ci)
{
	return (ci->new_pin_reqd);
}

char **
fido_cbor_info_certs_name_ptr(const fido_cbor_info_t *ci)
{
	return (ci->certs.name);
}

const uint64_t *
fido_cbor_info_certs_value_ptr(const fido_cbor_info_t *ci)
{
	return (ci->certs.value);
}

size_t
fido_cbor_info_certs_len(const fido_cbor_info_t *ci)
{
	return (ci->certs.len);
}