Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2018-2021 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 | | */ |
6 | | |
7 | | #include "fido.h" |
8 | | |
9 | | static int |
10 | | decode_string(const cbor_item_t *item, void *arg) |
11 | 50.3k | { |
12 | 50.3k | fido_str_array_t *a = arg; |
13 | 50.3k | const size_t i = a->len; |
14 | | |
15 | | /* keep ptr[x] and len consistent */ |
16 | 50.3k | if (cbor_string_copy(item, &a->ptr[i]) < 0) { |
17 | 92 | fido_log_debug("%s: cbor_string_copy", __func__); |
18 | 92 | return (-1); |
19 | 92 | } |
20 | | |
21 | 50.2k | a->len++; |
22 | | |
23 | 50.2k | return (0); |
24 | 50.2k | } |
25 | | |
26 | | static int |
27 | | decode_string_array(const cbor_item_t *item, fido_str_array_t *v) |
28 | 20.2k | { |
29 | 20.2k | v->ptr = NULL; |
30 | 20.2k | v->len = 0; |
31 | | |
32 | 20.2k | if (cbor_isa_array(item) == false || |
33 | 20.2k | cbor_array_is_definite(item) == false) { |
34 | 62 | fido_log_debug("%s: cbor type", __func__); |
35 | 62 | return (-1); |
36 | 62 | } |
37 | | |
38 | 20.2k | v->ptr = calloc(cbor_array_size(item), sizeof(char *)); |
39 | 20.2k | if (v->ptr == NULL) |
40 | 20.2k | return (-1); |
41 | | |
42 | 20.1k | if (cbor_array_iter(item, v, decode_string) < 0) { |
43 | 100 | fido_log_debug("%s: decode_string", __func__); |
44 | 100 | return (-1); |
45 | 100 | } |
46 | | |
47 | 20.0k | return (0); |
48 | 20.0k | } |
49 | | |
50 | | static int |
51 | | decode_aaguid(const cbor_item_t *item, unsigned char *aaguid, size_t aaguid_len) |
52 | 9.33k | { |
53 | 9.33k | if (cbor_isa_bytestring(item) == false || |
54 | 9.33k | cbor_bytestring_is_definite(item) == false || |
55 | 9.33k | cbor_bytestring_length(item) != aaguid_len) { |
56 | 103 | fido_log_debug("%s: cbor type", __func__); |
57 | 103 | return (-1); |
58 | 103 | } |
59 | | |
60 | 9.23k | memcpy(aaguid, cbor_bytestring_handle(item), aaguid_len); |
61 | | |
62 | 9.23k | return (0); |
63 | 9.23k | } |
64 | | |
65 | | static int |
66 | | decode_option(const cbor_item_t *key, const cbor_item_t *val, void *arg) |
67 | 50.8k | { |
68 | 50.8k | fido_opt_array_t *o = arg; |
69 | 50.8k | const size_t i = o->len; |
70 | | |
71 | 50.8k | if (cbor_isa_float_ctrl(val) == false || |
72 | 50.8k | cbor_float_get_width(val) != CBOR_FLOAT_0 || |
73 | 50.8k | cbor_is_bool(val) == false) { |
74 | 3.21k | fido_log_debug("%s: cbor type", __func__); |
75 | 3.21k | return (0); /* ignore */ |
76 | 3.21k | } |
77 | | |
78 | 47.5k | if (cbor_string_copy(key, &o->name[i]) < 0) { |
79 | 249 | fido_log_debug("%s: cbor_string_copy", __func__); |
80 | 249 | return (0); /* ignore */ |
81 | 249 | } |
82 | | |
83 | | /* keep name/value and len consistent */ |
84 | 47.3k | o->value[i] = cbor_ctrl_value(val) == CBOR_CTRL_TRUE; |
85 | 47.3k | o->len++; |
86 | | |
87 | 47.3k | return (0); |
88 | 47.3k | } |
89 | | |
90 | | static int |
91 | | decode_options(const cbor_item_t *item, fido_opt_array_t *o) |
92 | 9.04k | { |
93 | 9.04k | o->name = NULL; |
94 | 9.04k | o->value = NULL; |
95 | 9.04k | o->len = 0; |
96 | | |
97 | 9.04k | if (cbor_isa_map(item) == false || |
98 | 9.04k | cbor_map_is_definite(item) == false) { |
99 | 40 | fido_log_debug("%s: cbor type", __func__); |
100 | 40 | return (-1); |
101 | 40 | } |
102 | | |
103 | 9.00k | o->name = calloc(cbor_map_size(item), sizeof(char *)); |
104 | 9.00k | o->value = calloc(cbor_map_size(item), sizeof(bool)); |
105 | 9.00k | if (o->name == NULL || o->value == NULL) |
106 | 9.00k | return (-1); |
107 | | |
108 | 8.97k | return (cbor_map_iter(item, o, decode_option)); |
109 | 8.97k | } |
110 | | |
111 | | static int |
112 | | decode_protocol(const cbor_item_t *item, void *arg) |
113 | 11.7k | { |
114 | 11.7k | fido_byte_array_t *p = arg; |
115 | 11.7k | const size_t i = p->len; |
116 | | |
117 | 11.7k | if (cbor_isa_uint(item) == false || |
118 | 11.7k | cbor_int_get_width(item) != CBOR_INT_8) { |
119 | 77 | fido_log_debug("%s: cbor type", __func__); |
120 | 77 | return (-1); |
121 | 77 | } |
122 | | |
123 | | /* keep ptr[x] and len consistent */ |
124 | 11.6k | p->ptr[i] = cbor_get_uint8(item); |
125 | 11.6k | p->len++; |
126 | | |
127 | 11.6k | return (0); |
128 | 11.6k | } |
129 | | |
130 | | static int |
131 | | decode_protocols(const cbor_item_t *item, fido_byte_array_t *p) |
132 | 8.91k | { |
133 | 8.91k | p->ptr = NULL; |
134 | 8.91k | p->len = 0; |
135 | | |
136 | 8.91k | if (cbor_isa_array(item) == false || |
137 | 8.91k | cbor_array_is_definite(item) == false) { |
138 | 44 | fido_log_debug("%s: cbor type", __func__); |
139 | 44 | return (-1); |
140 | 44 | } |
141 | | |
142 | 8.86k | p->ptr = calloc(cbor_array_size(item), sizeof(uint8_t)); |
143 | 8.86k | if (p->ptr == NULL) |
144 | 8.86k | return (-1); |
145 | | |
146 | 8.85k | if (cbor_array_iter(item, p, decode_protocol) < 0) { |
147 | 84 | fido_log_debug("%s: decode_protocol", __func__); |
148 | 84 | return (-1); |
149 | 84 | } |
150 | | |
151 | 8.77k | return (0); |
152 | 8.77k | } |
153 | | |
154 | | static int |
155 | | decode_algorithm_entry(const cbor_item_t *key, const cbor_item_t *val, |
156 | | void *arg) |
157 | 25.4k | { |
158 | 25.4k | fido_algo_t *alg = arg; |
159 | 25.4k | char *name = NULL; |
160 | 25.4k | int ok = -1; |
161 | | |
162 | 25.4k | if (cbor_string_copy(key, &name) < 0) { |
163 | 256 | fido_log_debug("%s: cbor type", __func__); |
164 | 256 | ok = 0; /* ignore */ |
165 | 256 | goto out; |
166 | 256 | } |
167 | | |
168 | 25.2k | if (!strcmp(name, "alg")) { |
169 | 10.9k | if (cbor_isa_negint(val) == false || |
170 | 10.9k | cbor_get_int(val) > INT_MAX || alg->cose != 0) { |
171 | 402 | fido_log_debug("%s: alg", __func__); |
172 | 402 | goto out; |
173 | 402 | } |
174 | 10.5k | alg->cose = -(int)cbor_get_int(val) - 1; |
175 | 14.2k | } else if (!strcmp(name, "type")) { |
176 | 9.39k | if (cbor_string_copy(val, &alg->type) < 0) { |
177 | 39 | fido_log_debug("%s: type", __func__); |
178 | 39 | goto out; |
179 | 39 | } |
180 | 24.7k | } |
181 | | |
182 | 24.7k | ok = 0; |
183 | 25.4k | out: |
184 | 25.4k | free(name); |
185 | | |
186 | 25.4k | return (ok); |
187 | 24.7k | } |
188 | | |
189 | | static int |
190 | | decode_algorithm(const cbor_item_t *item, void *arg) |
191 | 13.6k | { |
192 | 13.6k | fido_algo_array_t *aa = arg; |
193 | 13.6k | const size_t i = aa->len; |
194 | | |
195 | 13.6k | if (cbor_isa_map(item) == false || |
196 | 13.6k | cbor_map_is_definite(item) == false) { |
197 | 131 | fido_log_debug("%s: cbor type", __func__); |
198 | 131 | return (-1); |
199 | 131 | } |
200 | | |
201 | 13.5k | memset(&aa->ptr[i], 0, sizeof(aa->ptr[i])); |
202 | | |
203 | 13.5k | if (cbor_map_iter(item, &aa->ptr[i], decode_algorithm_entry) < 0) { |
204 | 723 | fido_log_debug("%s: decode_algorithm_entry", __func__); |
205 | 723 | fido_algo_free(&aa->ptr[i]); |
206 | 723 | return (-1); |
207 | 723 | } |
208 | | |
209 | | /* keep ptr[x] and len consistent */ |
210 | 12.8k | aa->len++; |
211 | | |
212 | 12.8k | return (0); |
213 | 12.8k | } |
214 | | |
215 | | static int |
216 | | decode_algorithms(const cbor_item_t *item, fido_algo_array_t *aa) |
217 | 6.94k | { |
218 | 6.94k | aa->ptr = NULL; |
219 | 6.94k | aa->len = 0; |
220 | | |
221 | 6.94k | if (cbor_isa_array(item) == false || |
222 | 6.94k | cbor_array_is_definite(item) == false) { |
223 | 44 | fido_log_debug("%s: cbor type", __func__); |
224 | 44 | return (-1); |
225 | 44 | } |
226 | | |
227 | 6.90k | aa->ptr = calloc(cbor_array_size(item), sizeof(fido_algo_t)); |
228 | 6.90k | if (aa->ptr == NULL) |
229 | 6.90k | return (-1); |
230 | | |
231 | 6.88k | if (cbor_array_iter(item, aa, decode_algorithm) < 0) { |
232 | 863 | fido_log_debug("%s: decode_algorithm", __func__); |
233 | 863 | return (-1); |
234 | 863 | } |
235 | | |
236 | 6.02k | return (0); |
237 | 6.02k | } |
238 | | |
239 | | static int |
240 | | parse_reply_element(const cbor_item_t *key, const cbor_item_t *val, void *arg) |
241 | 90.7k | { |
242 | 90.7k | fido_cbor_info_t *ci = arg; |
243 | | |
244 | 90.7k | if (cbor_isa_uint(key) == false || |
245 | 90.7k | cbor_int_get_width(key) != CBOR_INT_8) { |
246 | 3.65k | fido_log_debug("%s: cbor type", __func__); |
247 | 3.65k | return (0); /* ignore */ |
248 | 3.65k | } |
249 | | |
250 | 87.0k | switch (cbor_get_uint8(key)) { |
251 | 9.43k | case 1: /* versions */ |
252 | 9.43k | return (decode_string_array(val, &ci->versions)); |
253 | 10.1k | case 2: /* extensions */ |
254 | 10.1k | return (decode_string_array(val, &ci->extensions)); |
255 | 9.33k | case 3: /* aaguid */ |
256 | 9.33k | return (decode_aaguid(val, ci->aaguid, sizeof(ci->aaguid))); |
257 | 9.04k | case 4: /* options */ |
258 | 9.04k | return (decode_options(val, &ci->options)); |
259 | 9.20k | case 5: /* maxMsgSize */ |
260 | 9.20k | return (cbor_decode_uint64(val, &ci->maxmsgsiz)); |
261 | 8.91k | case 6: /* pinProtocols */ |
262 | 8.91k | return (decode_protocols(val, &ci->protocols)); |
263 | 8.51k | case 7: /* maxCredentialCountInList */ |
264 | 8.51k | return (cbor_decode_uint64(val, &ci->maxcredcntlst)); |
265 | 8.43k | case 8: /* maxCredentialIdLength */ |
266 | 8.43k | return (cbor_decode_uint64(val, &ci->maxcredidlen)); |
267 | 732 | case 9: /* transports */ |
268 | 732 | return (decode_string_array(val, &ci->transports)); |
269 | 6.94k | case 10: /* algorithms */ |
270 | 6.94k | return (decode_algorithms(val, &ci->algorithms)); |
271 | 598 | case 14: /* fwVersion */ |
272 | 598 | return (cbor_decode_uint64(val, &ci->fwversion)); |
273 | 560 | case 15: /* maxCredBlobLen */ |
274 | 560 | return (cbor_decode_uint64(val, &ci->maxcredbloblen)); |
275 | 5.25k | default: /* ignore */ |
276 | 5.25k | fido_log_debug("%s: cbor type", __func__); |
277 | 5.25k | return (0); |
278 | 87.0k | } |
279 | 87.0k | } |
280 | | |
281 | | static int |
282 | | fido_dev_get_cbor_info_tx(fido_dev_t *dev, int *ms) |
283 | 22.3k | { |
284 | 22.3k | const unsigned char cbor[] = { CTAP_CBOR_GETINFO }; |
285 | | |
286 | 22.3k | fido_log_debug("%s: dev=%p", __func__, (void *)dev); |
287 | | |
288 | 22.3k | if (fido_tx(dev, CTAP_CMD_CBOR, cbor, sizeof(cbor), ms) < 0) { |
289 | 291 | fido_log_debug("%s: fido_tx", __func__); |
290 | 291 | return (FIDO_ERR_TX); |
291 | 291 | } |
292 | | |
293 | 22.1k | return (FIDO_OK); |
294 | 22.1k | } |
295 | | |
296 | | static int |
297 | | fido_dev_get_cbor_info_rx(fido_dev_t *dev, fido_cbor_info_t *ci, int *ms) |
298 | 22.1k | { |
299 | 22.1k | unsigned char reply[FIDO_MAXMSG]; |
300 | 22.1k | int reply_len; |
301 | | |
302 | 22.1k | fido_log_debug("%s: dev=%p, ci=%p, ms=%d", __func__, (void *)dev, |
303 | 22.1k | (void *)ci, *ms); |
304 | | |
305 | 22.1k | fido_cbor_info_reset(ci); |
306 | | |
307 | 22.1k | if ((reply_len = fido_rx(dev, CTAP_CMD_CBOR, &reply, sizeof(reply), |
308 | 22.1k | ms)) < 0) { |
309 | 5.34k | fido_log_debug("%s: fido_rx", __func__); |
310 | 5.34k | return (FIDO_ERR_RX); |
311 | 5.34k | } |
312 | | |
313 | 16.7k | return (cbor_parse_reply(reply, (size_t)reply_len, ci, |
314 | 16.7k | parse_reply_element)); |
315 | 16.7k | } |
316 | | |
317 | | int |
318 | | fido_dev_get_cbor_info_wait(fido_dev_t *dev, fido_cbor_info_t *ci, int *ms) |
319 | 22.3k | { |
320 | 22.3k | int r; |
321 | | |
322 | | #ifdef USE_WINHELLO |
323 | | if (dev->flags & FIDO_DEV_WINHELLO) |
324 | | return (fido_winhello_get_cbor_info(dev, ci)); |
325 | | #endif |
326 | 22.3k | if ((r = fido_dev_get_cbor_info_tx(dev, ms)) != FIDO_OK || |
327 | 22.3k | (r = fido_dev_get_cbor_info_rx(dev, ci, ms)) != FIDO_OK) |
328 | 22.3k | return (r); |
329 | | |
330 | 8.98k | return (FIDO_OK); |
331 | 8.98k | } |
332 | | |
333 | | int |
334 | | fido_dev_get_cbor_info(fido_dev_t *dev, fido_cbor_info_t *ci) |
335 | 303 | { |
336 | 303 | int ms = dev->timeout_ms; |
337 | | |
338 | 303 | return (fido_dev_get_cbor_info_wait(dev, ci, &ms)); |
339 | 303 | } |
340 | | |
341 | | /* |
342 | | * get/set functions for fido_cbor_info_t; always at the end of the file |
343 | | */ |
344 | | |
345 | | fido_cbor_info_t * |
346 | | fido_cbor_info_new(void) |
347 | 22.4k | { |
348 | 22.4k | return (calloc(1, sizeof(fido_cbor_info_t))); |
349 | 22.4k | } |
350 | | |
351 | | void |
352 | | fido_cbor_info_reset(fido_cbor_info_t *ci) |
353 | 44.4k | { |
354 | 44.4k | fido_str_array_free(&ci->versions); |
355 | 44.4k | fido_str_array_free(&ci->extensions); |
356 | 44.4k | fido_str_array_free(&ci->transports); |
357 | 44.4k | fido_opt_array_free(&ci->options); |
358 | 44.4k | fido_byte_array_free(&ci->protocols); |
359 | 44.4k | fido_algo_array_free(&ci->algorithms); |
360 | 44.4k | } |
361 | | |
362 | | void |
363 | | fido_cbor_info_free(fido_cbor_info_t **ci_p) |
364 | 70.9k | { |
365 | 70.9k | fido_cbor_info_t *ci; |
366 | | |
367 | 70.9k | if (ci_p == NULL || (ci = *ci_p) == NULL) |
368 | 70.9k | return; |
369 | 22.3k | fido_cbor_info_reset(ci); |
370 | 22.3k | free(ci); |
371 | 22.3k | *ci_p = NULL; |
372 | 22.3k | } |
373 | | |
374 | | char ** |
375 | | fido_cbor_info_versions_ptr(const fido_cbor_info_t *ci) |
376 | 310 | { |
377 | 310 | return (ci->versions.ptr); |
378 | 310 | } |
379 | | |
380 | | size_t |
381 | | fido_cbor_info_versions_len(const fido_cbor_info_t *ci) |
382 | 613 | { |
383 | 613 | return (ci->versions.len); |
384 | 613 | } |
385 | | |
386 | | char ** |
387 | | fido_cbor_info_extensions_ptr(const fido_cbor_info_t *ci) |
388 | 9.08k | { |
389 | 9.08k | return (ci->extensions.ptr); |
390 | 9.08k | } |
391 | | |
392 | | size_t |
393 | | fido_cbor_info_extensions_len(const fido_cbor_info_t *ci) |
394 | 9.38k | { |
395 | 9.38k | return (ci->extensions.len); |
396 | 9.38k | } |
397 | | |
398 | | char ** |
399 | | fido_cbor_info_transports_ptr(const fido_cbor_info_t *ci) |
400 | 46 | { |
401 | 46 | return (ci->transports.ptr); |
402 | 46 | } |
403 | | |
404 | | size_t |
405 | | fido_cbor_info_transports_len(const fido_cbor_info_t *ci) |
406 | 349 | { |
407 | 349 | return (ci->transports.len); |
408 | 349 | } |
409 | | |
410 | | const unsigned char * |
411 | | fido_cbor_info_aaguid_ptr(const fido_cbor_info_t *ci) |
412 | 303 | { |
413 | 303 | return (ci->aaguid); |
414 | 303 | } |
415 | | |
416 | | size_t |
417 | | fido_cbor_info_aaguid_len(const fido_cbor_info_t *ci) |
418 | 303 | { |
419 | 303 | return (sizeof(ci->aaguid)); |
420 | 303 | } |
421 | | |
422 | | char ** |
423 | | fido_cbor_info_options_name_ptr(const fido_cbor_info_t *ci) |
424 | 9.09k | { |
425 | 9.09k | return (ci->options.name); |
426 | 9.09k | } |
427 | | |
428 | | const bool * |
429 | | fido_cbor_info_options_value_ptr(const fido_cbor_info_t *ci) |
430 | 9.09k | { |
431 | 9.09k | return (ci->options.value); |
432 | 9.09k | } |
433 | | |
434 | | size_t |
435 | | fido_cbor_info_options_len(const fido_cbor_info_t *ci) |
436 | 9.39k | { |
437 | 9.39k | return (ci->options.len); |
438 | 9.39k | } |
439 | | |
440 | | uint64_t |
441 | | fido_cbor_info_maxcredbloblen(const fido_cbor_info_t *ci) |
442 | 303 | { |
443 | 303 | return (ci->maxcredbloblen); |
444 | 303 | } |
445 | | |
446 | | uint64_t |
447 | | fido_cbor_info_maxmsgsiz(const fido_cbor_info_t *ci) |
448 | 9.27k | { |
449 | 9.27k | return (ci->maxmsgsiz); |
450 | 9.27k | } |
451 | | |
452 | | uint64_t |
453 | | fido_cbor_info_maxcredcntlst(const fido_cbor_info_t *ci) |
454 | 303 | { |
455 | 303 | return (ci->maxcredcntlst); |
456 | 303 | } |
457 | | |
458 | | uint64_t |
459 | | fido_cbor_info_maxcredidlen(const fido_cbor_info_t *ci) |
460 | 303 | { |
461 | 303 | return (ci->maxcredidlen); |
462 | 303 | } |
463 | | |
464 | | uint64_t |
465 | | fido_cbor_info_fwversion(const fido_cbor_info_t *ci) |
466 | 303 | { |
467 | 303 | return (ci->fwversion); |
468 | 303 | } |
469 | | |
470 | | const uint8_t * |
471 | | fido_cbor_info_protocols_ptr(const fido_cbor_info_t *ci) |
472 | 9.27k | { |
473 | 9.27k | return (ci->protocols.ptr); |
474 | 9.27k | } |
475 | | |
476 | | size_t |
477 | | fido_cbor_info_protocols_len(const fido_cbor_info_t *ci) |
478 | 9.27k | { |
479 | 9.27k | return (ci->protocols.len); |
480 | 9.27k | } |
481 | | |
482 | | size_t |
483 | | fido_cbor_info_algorithm_count(const fido_cbor_info_t *ci) |
484 | 647 | { |
485 | 647 | return (ci->algorithms.len); |
486 | 647 | } |
487 | | |
488 | | const char * |
489 | | fido_cbor_info_algorithm_type(const fido_cbor_info_t *ci, size_t idx) |
490 | 344 | { |
491 | 344 | if (idx >= ci->algorithms.len) |
492 | 303 | return (NULL); |
493 | | |
494 | 41 | return (ci->algorithms.ptr[idx].type); |
495 | 41 | } |
496 | | |
497 | | int |
498 | | fido_cbor_info_algorithm_cose(const fido_cbor_info_t *ci, size_t idx) |
499 | 344 | { |
500 | 344 | if (idx >= ci->algorithms.len) |
501 | 303 | return (0); |
502 | | |
503 | 41 | return (ci->algorithms.ptr[idx].cose); |
504 | 41 | } |