| 1 |
/* |
| 2 |
* Copyright (c) 2018-2026 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 |
{ |
| 13 |
fido_str_array_t *a = arg; |
| 14 |
const size_t i = a->len; |
| 15 |
|
| 16 |
/* keep ptr[x] and len consistent */ |
| 17 |
if (cbor_string_copy(item, &a->ptr[i]) < 0) { |
| 18 |
fido_log_debug("%s: cbor_string_copy", __func__); |
| 19 |
return (-1); |
| 20 |
} |
| 21 |
|
| 22 |
a->len++; |
| 23 |
|
| 24 |
return (0); |
| 25 |
} |
| 26 |
|
| 27 |
static int |
| 28 |
decode_string_array(const cbor_item_t *item, fido_str_array_t *v) |
| 29 |
{ |
| 30 |
v->ptr = NULL; |
| 31 |
v->len = 0; |
| 32 |
|
| 33 |
if (cbor_isa_array(item) == false || |
| 34 |
cbor_array_is_definite(item) == false) { |
| 35 |
fido_log_debug("%s: cbor type", __func__); |
| 36 |
return (-1); |
| 37 |
} |
| 38 |
|
| 39 |
v->ptr = calloc(cbor_array_size(item), sizeof(char *)); |
| 40 |
if (v->ptr == NULL) |
| 41 |
return (-1); |
| 42 |
|
| 43 |
if (cbor_array_iter(item, v, decode_string) < 0) { |
| 44 |
fido_log_debug("%s: decode_string", __func__); |
| 45 |
return (-1); |
| 46 |
} |
| 47 |
|
| 48 |
return (0); |
| 49 |
} |
| 50 |
|
| 51 |
static int |
| 52 |
decode_aaguid(const cbor_item_t *item, unsigned char *aaguid, size_t aaguid_len) |
| 53 |
{ |
| 54 |
if (cbor_isa_bytestring(item) == false || |
| 55 |
cbor_bytestring_is_definite(item) == false || |
| 56 |
cbor_bytestring_length(item) != aaguid_len) { |
| 57 |
fido_log_debug("%s: cbor type", __func__); |
| 58 |
return (-1); |
| 59 |
} |
| 60 |
|
| 61 |
memcpy(aaguid, cbor_bytestring_handle(item), aaguid_len); |
| 62 |
|
| 63 |
return (0); |
| 64 |
} |
| 65 |
|
| 66 |
static int |
| 67 |
decode_option(const cbor_item_t *key, const cbor_item_t *val, void *arg) |
| 68 |
{ |
| 69 |
fido_opt_array_t *o = arg; |
| 70 |
const size_t i = o->len; |
| 71 |
|
| 72 |
if (cbor_decode_bool(val, NULL) < 0) { |
| 73 |
fido_log_debug("%s: cbor_decode_bool", __func__); |
| 74 |
return (0); /* ignore */ |
| 75 |
} |
| 76 |
|
| 77 |
if (cbor_string_copy(key, &o->name[i]) < 0) { |
| 78 |
fido_log_debug("%s: cbor_string_copy", __func__); |
| 79 |
return (0); /* ignore */ |
| 80 |
} |
| 81 |
|
| 82 |
/* keep name/value and len consistent */ |
| 83 |
o->value[i] = cbor_ctrl_value(val) == CBOR_CTRL_TRUE; |
| 84 |
o->len++; |
| 85 |
|
| 86 |
return (0); |
| 87 |
} |
| 88 |
|
| 89 |
static int |
| 90 |
decode_options(const cbor_item_t *item, fido_opt_array_t *o) |
| 91 |
{ |
| 92 |
o->name = NULL; |
| 93 |
o->value = NULL; |
| 94 |
o->len = 0; |
| 95 |
|
| 96 |
if (cbor_isa_map(item) == false || |
| 97 |
cbor_map_is_definite(item) == false) { |
| 98 |
fido_log_debug("%s: cbor type", __func__); |
| 99 |
return (-1); |
| 100 |
} |
| 101 |
|
| 102 |
o->name = calloc(cbor_map_size(item), sizeof(char *)); |
| 103 |
o->value = calloc(cbor_map_size(item), sizeof(bool)); |
| 104 |
if (o->name == NULL || o->value == NULL) |
| 105 |
return (-1); |
| 106 |
|
| 107 |
return (cbor_map_iter(item, o, decode_option)); |
| 108 |
} |
| 109 |
|
| 110 |
static int |
| 111 |
decode_byte_array_item(const cbor_item_t *item, void *arg) |
| 112 |
{ |
| 113 |
fido_byte_array_t *p = arg; |
| 114 |
const size_t i = p->len; |
| 115 |
|
| 116 |
if (cbor_isa_uint(item) == false || |
| 117 |
cbor_int_get_width(item) != CBOR_INT_8) { |
| 118 |
fido_log_debug("%s: cbor type", __func__); |
| 119 |
return (-1); |
| 120 |
} |
| 121 |
|
| 122 |
/* keep ptr[x] and len consistent */ |
| 123 |
p->ptr[i] = cbor_get_uint8(item); |
| 124 |
p->len++; |
| 125 |
|
| 126 |
return (0); |
| 127 |
} |
| 128 |
|
| 129 |
static int |
| 130 |
decode_byte_array(const cbor_item_t *item, fido_byte_array_t *p) |
| 131 |
{ |
| 132 |
p->ptr = NULL; |
| 133 |
p->len = 0; |
| 134 |
|
| 135 |
if (cbor_isa_array(item) == false || |
| 136 |
cbor_array_is_definite(item) == false) { |
| 137 |
fido_log_debug("%s: cbor type", __func__); |
| 138 |
return (-1); |
| 139 |
} |
| 140 |
|
| 141 |
p->ptr = calloc(cbor_array_size(item), sizeof(uint8_t)); |
| 142 |
if (p->ptr == NULL) |
| 143 |
return (-1); |
| 144 |
|
| 145 |
if (cbor_array_iter(item, p, decode_byte_array_item) < 0) { |
| 146 |
fido_log_debug("%s: decode_byte_array_item", __func__); |
| 147 |
return (-1); |
| 148 |
} |
| 149 |
|
| 150 |
return (0); |
| 151 |
} |
| 152 |
|
| 153 |
static int |
| 154 |
decode_algorithm_entry(const cbor_item_t *key, const cbor_item_t *val, |
| 155 |
void *arg) |
| 156 |
{ |
| 157 |
fido_algo_t *alg = arg; |
| 158 |
char *name = NULL; |
| 159 |
int ok = -1; |
| 160 |
|
| 161 |
if (cbor_string_copy(key, &name) < 0) { |
| 162 |
fido_log_debug("%s: cbor type", __func__); |
| 163 |
ok = 0; /* ignore */ |
| 164 |
goto out; |
| 165 |
} |
| 166 |
|
| 167 |
if (!strcmp(name, "alg")) { |
| 168 |
if (cbor_isa_negint(val) == false || |
| 169 |
cbor_get_int(val) > INT_MAX || alg->cose != 0) { |
| 170 |
fido_log_debug("%s: alg", __func__); |
| 171 |
goto out; |
| 172 |
} |
| 173 |
alg->cose = -(int)cbor_get_int(val) - 1; |
| 174 |
} else if (!strcmp(name, "type")) { |
| 175 |
if (cbor_string_copy(val, &alg->type) < 0) { |
| 176 |
fido_log_debug("%s: type", __func__); |
| 177 |
goto out; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
ok = 0; |
| 182 |
out: |
| 183 |
free(name); |
| 184 |
|
| 185 |
return (ok); |
| 186 |
} |
| 187 |
|
| 188 |
static int |
| 189 |
decode_algorithm(const cbor_item_t *item, void *arg) |
| 190 |
{ |
| 191 |
fido_algo_array_t *aa = arg; |
| 192 |
const size_t i = aa->len; |
| 193 |
|
| 194 |
if (cbor_isa_map(item) == false || |
| 195 |
cbor_map_is_definite(item) == false) { |
| 196 |
fido_log_debug("%s: cbor type", __func__); |
| 197 |
return (-1); |
| 198 |
} |
| 199 |
|
| 200 |
memset(&aa->ptr[i], 0, sizeof(aa->ptr[i])); |
| 201 |
|
| 202 |
if (cbor_map_iter(item, &aa->ptr[i], decode_algorithm_entry) < 0) { |
| 203 |
fido_log_debug("%s: decode_algorithm_entry", __func__); |
| 204 |
fido_algo_free(&aa->ptr[i]); |
| 205 |
return (-1); |
| 206 |
} |
| 207 |
|
| 208 |
/* keep ptr[x] and len consistent */ |
| 209 |
aa->len++; |
| 210 |
|
| 211 |
return (0); |
| 212 |
} |
| 213 |
|
| 214 |
static int |
| 215 |
decode_algorithms(const cbor_item_t *item, fido_algo_array_t *aa) |
| 216 |
{ |
| 217 |
aa->ptr = NULL; |
| 218 |
aa->len = 0; |
| 219 |
|
| 220 |
if (cbor_isa_array(item) == false || |
| 221 |
cbor_array_is_definite(item) == false) { |
| 222 |
fido_log_debug("%s: cbor type", __func__); |
| 223 |
return (-1); |
| 224 |
} |
| 225 |
|
| 226 |
aa->ptr = calloc(cbor_array_size(item), sizeof(fido_algo_t)); |
| 227 |
if (aa->ptr == NULL) |
| 228 |
return (-1); |
| 229 |
|
| 230 |
if (cbor_array_iter(item, aa, decode_algorithm) < 0) { |
| 231 |
fido_log_debug("%s: decode_algorithm", __func__); |
| 232 |
return (-1); |
| 233 |
} |
| 234 |
|
| 235 |
return (0); |
| 236 |
} |
| 237 |
|
| 238 |
static int |
| 239 |
decode_cert(const cbor_item_t *key, const cbor_item_t *val, void *arg) |
| 240 |
{ |
| 241 |
fido_cert_array_t *c = arg; |
| 242 |
const size_t i = c->len; |
| 243 |
|
| 244 |
if (cbor_is_int(val) == false) { |
| 245 |
fido_log_debug("%s: cbor_is_int", __func__); |
| 246 |
return (0); /* ignore */ |
| 247 |
} |
| 248 |
|
| 249 |
if (cbor_string_copy(key, &c->name[i]) < 0) { |
| 250 |
fido_log_debug("%s: cbor_string_copy", __func__); |
| 251 |
return (0); /* ignore */ |
| 252 |
} |
| 253 |
|
| 254 |
/* keep name/value and len consistent */ |
| 255 |
c->value[i] = cbor_get_int(val); |
| 256 |
c->len++; |
| 257 |
|
| 258 |
return (0); |
| 259 |
} |
| 260 |
|
| 261 |
static int |
| 262 |
decode_certs(const cbor_item_t *item, fido_cert_array_t *c) |
| 263 |
{ |
| 264 |
c->name = NULL; |
| 265 |
c->value = NULL; |
| 266 |
c->len = 0; |
| 267 |
|
| 268 |
if (cbor_isa_map(item) == false || |
| 269 |
cbor_map_is_definite(item) == false) { |
| 270 |
fido_log_debug("%s: cbor type", __func__); |
| 271 |
return (-1); |
| 272 |
} |
| 273 |
|
| 274 |
c->name = calloc(cbor_map_size(item), sizeof(char *)); |
| 275 |
c->value = calloc(cbor_map_size(item), sizeof(uint64_t)); |
| 276 |
if (c->name == NULL || c->value == NULL) |
| 277 |
return (-1); |
| 278 |
|
| 279 |
return (cbor_map_iter(item, c, decode_cert)); |
| 280 |
} |
| 281 |
|
| 282 |
static int |
| 283 |
parse_reply_element(const cbor_item_t *key, const cbor_item_t *val, void *arg) |
| 284 |
{ |
| 285 |
fido_cbor_info_t *ci = arg; |
| 286 |
uint64_t x; |
| 287 |
bool b; |
| 288 |
|
| 289 |
if (cbor_isa_uint(key) == false || |
| 290 |
cbor_int_get_width(key) != CBOR_INT_8) { |
| 291 |
fido_log_debug("%s: cbor type", __func__); |
| 292 |
return (0); /* ignore */ |
| 293 |
} |
| 294 |
|
| 295 |
switch (cbor_get_uint8(key)) { |
| 296 |
case 1: /* versions */ |
| 297 |
return (decode_string_array(val, &ci->versions)); |
| 298 |
case 2: /* extensions */ |
| 299 |
return (decode_string_array(val, &ci->extensions)); |
| 300 |
case 3: /* aaguid */ |
| 301 |
return (decode_aaguid(val, ci->aaguid, sizeof(ci->aaguid))); |
| 302 |
case 4: /* options */ |
| 303 |
return (decode_options(val, &ci->options)); |
| 304 |
case 5: /* maxMsgSize */ |
| 305 |
return (cbor_decode_uint64(val, &ci->maxmsgsiz)); |
| 306 |
case 6: /* pinProtocols */ |
| 307 |
return (decode_byte_array(val, &ci->protocols)); |
| 308 |
case 7: /* maxCredentialCountInList */ |
| 309 |
return (cbor_decode_uint64(val, &ci->maxcredcntlst)); |
| 310 |
case 8: /* maxCredentialIdLength */ |
| 311 |
return (cbor_decode_uint64(val, &ci->maxcredidlen)); |
| 312 |
case 9: /* transports */ |
| 313 |
return (decode_string_array(val, &ci->transports)); |
| 314 |
case 10: /* algorithms */ |
| 315 |
return (decode_algorithms(val, &ci->algorithms)); |
| 316 |
case 11: /* maxSerializedLargeBlobArray */ |
| 317 |
return (cbor_decode_uint64(val, &ci->maxlargeblob)); |
| 318 |
case 12: /* forcePINChange */ |
| 319 |
return (cbor_decode_bool(val, &ci->new_pin_reqd)); |
| 320 |
case 13: /* minPINLength */ |
| 321 |
return (cbor_decode_uint64(val, &ci->minpinlen)); |
| 322 |
case 14: /* fwVersion */ |
| 323 |
return (cbor_decode_uint64(val, &ci->fwversion)); |
| 324 |
case 15: /* maxCredBlobLen */ |
| 325 |
return (cbor_decode_uint64(val, &ci->maxcredbloblen)); |
| 326 |
case 16: /* maxRPIDsForSetMinPINLength */ |
| 327 |
return (cbor_decode_uint64(val, &ci->maxrpid_minlen)); |
| 328 |
case 17: /* preferredPlatformUvAttempts */ |
| 329 |
return (cbor_decode_uint64(val, &ci->uv_attempts)); |
| 330 |
case 18: /* uvModality */ |
| 331 |
return (cbor_decode_uint64(val, &ci->uv_modality)); |
| 332 |
case 19: /* certifications */ |
| 333 |
return (decode_certs(val, &ci->certs)); |
| 334 |
case 20: /* remainingDiscoverableCredentials */ |
| 335 |
if (cbor_decode_uint64(val, &x) < 0 || x > INT64_MAX) { |
| 336 |
fido_log_debug("%s: cbor_decode_uint64", __func__); |
| 337 |
return (-1); |
| 338 |
} |
| 339 |
ci->rk_remaining = (int64_t)x; |
| 340 |
return (0); |
| 341 |
case 22: /* attestationFormats */ |
| 342 |
return (decode_string_array(val, &ci->attfmts)); |
| 343 |
case 23: /* uvCountSinceLastPinEntry */ |
| 344 |
if (cbor_decode_uint64(val, &x) < 0 || x > INT64_MAX) { |
| 345 |
fido_log_debug("%s: cbor_decode_uint64", __func__); |
| 346 |
return (-1); |
| 347 |
} |
| 348 |
ci->uv_since_pin = (int64_t)x; |
| 349 |
return (0); |
| 350 |
case 24: /* longTouchForReset */ |
| 351 |
return (cbor_decode_bool(val, &ci->long_reset)); |
| 352 |
case 25: /* encIdentifier */ |
| 353 |
return (fido_blob_decode(val, &ci->encid)); |
| 354 |
case 26: /* transportsForReset */ |
| 355 |
return (decode_string_array(val, &ci->rsttransports)); |
| 356 |
case 27: /* pinComplexityPolicy */ |
| 357 |
if (cbor_decode_bool(val, &b) < 0) { |
| 358 |
fido_log_debug("%s: cbor_decode_bool", __func__); |
| 359 |
return (-1); |
| 360 |
} |
| 361 |
ci->pinpolicy = b; |
| 362 |
return (0); |
| 363 |
case 28: /* pinComplexityPolicyUrl */ |
| 364 |
return (fido_blob_decode(val, &ci->pinpolicyurl)); |
| 365 |
case 29: /* maxPINLength */ |
| 366 |
return (cbor_decode_uint64(val, &ci->maxpinlen)); |
| 367 |
case 30: /* encCredStoreState */ |
| 368 |
return (fido_blob_decode(val, &ci->encstate)); |
| 369 |
case 31: /* authenticatorConfigCommands */ |
| 370 |
return (decode_byte_array(val, &ci->cfgcmds)); |
| 371 |
default: /* ignore */ |
| 372 |
fido_log_debug("%s: cbor type: 0x%02x", __func__, cbor_get_uint8(key)); |
| 373 |
return (0); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
static int |
| 378 |
fido_dev_get_cbor_info_tx(fido_dev_t *dev, int *ms) |
| 379 |
{ |
| 380 |
const unsigned char cbor[] = { CTAP_CBOR_GETINFO }; |
| 381 |
|
| 382 |
fido_log_debug("%s: dev=%p", __func__, (void *)dev); |
| 383 |
|
| 384 |
if (fido_tx(dev, CTAP_CMD_CBOR, cbor, sizeof(cbor), ms) < 0) { |
| 385 |
fido_log_debug("%s: fido_tx", __func__); |
| 386 |
return (FIDO_ERR_TX); |
| 387 |
} |
| 388 |
|
| 389 |
return (FIDO_OK); |
| 390 |
} |
| 391 |
|
| 392 |
static int |
| 393 |
fido_dev_get_cbor_info_rx(fido_dev_t *dev, fido_cbor_info_t *ci, int *ms) |
| 394 |
{ |
| 395 |
unsigned char *msg; |
| 396 |
int msglen; |
| 397 |
int r; |
| 398 |
|
| 399 |
fido_log_debug("%s: dev=%p, ci=%p, ms=%d", __func__, (void *)dev, |
| 400 |
(void *)ci, *ms); |
| 401 |
|
| 402 |
fido_cbor_info_reset(ci); |
| 403 |
|
| 404 |
if ((msg = malloc(FIDO_MAXMSG)) == NULL) { |
| 405 |
r = FIDO_ERR_INTERNAL; |
| 406 |
goto out; |
| 407 |
} |
| 408 |
|
| 409 |
if ((msglen = fido_rx(dev, CTAP_CMD_CBOR, msg, FIDO_MAXMSG, ms)) < 0) { |
| 410 |
fido_log_debug("%s: fido_rx", __func__); |
| 411 |
r = FIDO_ERR_RX; |
| 412 |
goto out; |
| 413 |
} |
| 414 |
|
| 415 |
r = cbor_parse_reply(msg, (size_t)msglen, ci, parse_reply_element); |
| 416 |
out: |
| 417 |
freezero(msg, FIDO_MAXMSG); |
| 418 |
|
| 419 |
return (r); |
| 420 |
} |
| 421 |
|
| 422 |
int |
| 423 |
fido_dev_get_cbor_info_wait(fido_dev_t *dev, fido_cbor_info_t *ci, int *ms) |
| 424 |
{ |
| 425 |
int r; |
| 426 |
|
| 427 |
#ifdef USE_WINHELLO |
| 428 |
if (dev->flags & FIDO_DEV_WINHELLO) |
| 429 |
return (fido_winhello_get_cbor_info(dev, ci)); |
| 430 |
#endif |
| 431 |
if ((r = fido_dev_get_cbor_info_tx(dev, ms)) != FIDO_OK || |
| 432 |
(r = fido_dev_get_cbor_info_rx(dev, ci, ms)) != FIDO_OK) |
| 433 |
return (r); |
| 434 |
|
| 435 |
return (FIDO_OK); |
| 436 |
} |
| 437 |
|
| 438 |
int |
| 439 |
fido_dev_get_cbor_info(fido_dev_t *dev, fido_cbor_info_t *ci) |
| 440 |
{ |
| 441 |
int ms = dev->timeout_ms; |
| 442 |
|
| 443 |
return (fido_dev_get_cbor_info_wait(dev, ci, &ms)); |
| 444 |
} |
| 445 |
|
| 446 |
/* |
| 447 |
* get/set functions for fido_cbor_info_t; always at the end of the file |
| 448 |
*/ |
| 449 |
|
| 450 |
fido_cbor_info_t * |
| 451 |
fido_cbor_info_new(void) |
| 452 |
{ |
| 453 |
fido_cbor_info_t *ci; |
| 454 |
|
| 455 |
if ((ci = calloc(1, sizeof(fido_cbor_info_t))) == NULL) |
| 456 |
return (NULL); |
| 457 |
|
| 458 |
fido_cbor_info_reset(ci); |
| 459 |
|
| 460 |
return (ci); |
| 461 |
} |
| 462 |
|
| 463 |
void |
| 464 |
fido_cbor_info_reset(fido_cbor_info_t *ci) |
| 465 |
{ |
| 466 |
fido_str_array_free(&ci->versions); |
| 467 |
fido_str_array_free(&ci->extensions); |
| 468 |
fido_str_array_free(&ci->transports); |
| 469 |
fido_str_array_free(&ci->attfmts); |
| 470 |
fido_str_array_free(&ci->rsttransports); |
| 471 |
fido_opt_array_free(&ci->options); |
| 472 |
fido_byte_array_free(&ci->protocols); |
| 473 |
fido_byte_array_free(&ci->cfgcmds); |
| 474 |
fido_algo_array_free(&ci->algorithms); |
| 475 |
fido_cert_array_free(&ci->certs); |
| 476 |
fido_blob_reset(&ci->encid); |
| 477 |
fido_blob_reset(&ci->id); |
| 478 |
fido_blob_reset(&ci->pinpolicyurl); |
| 479 |
fido_blob_reset(&ci->encstate); |
| 480 |
fido_blob_reset(&ci->state); |
| 481 |
ci->rk_remaining = -1; |
| 482 |
ci->uv_since_pin = -1; |
| 483 |
ci->pinpolicy = -1; |
| 484 |
} |
| 485 |
|
| 486 |
void |
| 487 |
fido_cbor_info_free(fido_cbor_info_t **ci_p) |
| 488 |
{ |
| 489 |
fido_cbor_info_t *ci; |
| 490 |
|
| 491 |
if (ci_p == NULL || (ci = *ci_p) == NULL) |
| 492 |
return; |
| 493 |
fido_cbor_info_reset(ci); |
| 494 |
free(ci); |
| 495 |
*ci_p = NULL; |
| 496 |
} |
| 497 |
|
| 498 |
char ** |
| 499 |
fido_cbor_info_versions_ptr(const fido_cbor_info_t *ci) |
| 500 |
{ |
| 501 |
return (ci->versions.ptr); |
| 502 |
} |
| 503 |
|
| 504 |
size_t |
| 505 |
fido_cbor_info_versions_len(const fido_cbor_info_t *ci) |
| 506 |
{ |
| 507 |
return (ci->versions.len); |
| 508 |
} |
| 509 |
|
| 510 |
char ** |
| 511 |
fido_cbor_info_extensions_ptr(const fido_cbor_info_t *ci) |
| 512 |
{ |
| 513 |
return (ci->extensions.ptr); |
| 514 |
} |
| 515 |
|
| 516 |
size_t |
| 517 |
fido_cbor_info_extensions_len(const fido_cbor_info_t *ci) |
| 518 |
{ |
| 519 |
return (ci->extensions.len); |
| 520 |
} |
| 521 |
|
| 522 |
char ** |
| 523 |
fido_cbor_info_transports_ptr(const fido_cbor_info_t *ci) |
| 524 |
{ |
| 525 |
return (ci->transports.ptr); |
| 526 |
} |
| 527 |
|
| 528 |
size_t |
| 529 |
fido_cbor_info_transports_len(const fido_cbor_info_t *ci) |
| 530 |
{ |
| 531 |
return (ci->transports.len); |
| 532 |
} |
| 533 |
|
| 534 |
const unsigned char * |
| 535 |
fido_cbor_info_aaguid_ptr(const fido_cbor_info_t *ci) |
| 536 |
{ |
| 537 |
return (ci->aaguid); |
| 538 |
} |
| 539 |
|
| 540 |
size_t |
| 541 |
fido_cbor_info_aaguid_len(const fido_cbor_info_t *ci) |
| 542 |
{ |
| 543 |
return (sizeof(ci->aaguid)); |
| 544 |
} |
| 545 |
|
| 546 |
const unsigned char * |
| 547 |
fido_cbor_info_encid_ptr(const fido_cbor_info_t *ci) |
| 548 |
{ |
| 549 |
return (ci->encid.ptr); |
| 550 |
} |
| 551 |
|
| 552 |
size_t |
| 553 |
fido_cbor_info_encid_len(const fido_cbor_info_t *ci) |
| 554 |
{ |
| 555 |
return (ci->encid.len); |
| 556 |
} |
| 557 |
|
| 558 |
const unsigned char * |
| 559 |
fido_cbor_info_id_ptr(const fido_cbor_info_t *ci) |
| 560 |
{ |
| 561 |
return (ci->id.ptr); |
| 562 |
} |
| 563 |
|
| 564 |
size_t |
| 565 |
fido_cbor_info_id_len(const fido_cbor_info_t *ci) |
| 566 |
{ |
| 567 |
return (ci->id.len); |
| 568 |
} |
| 569 |
|
| 570 |
const unsigned char * |
| 571 |
fido_cbor_info_encstate_ptr(const fido_cbor_info_t *ci) |
| 572 |
{ |
| 573 |
return (ci->encstate.ptr); |
| 574 |
} |
| 575 |
|
| 576 |
size_t |
| 577 |
fido_cbor_info_encstate_len(const fido_cbor_info_t *ci) |
| 578 |
{ |
| 579 |
return (ci->encstate.len); |
| 580 |
} |
| 581 |
|
| 582 |
const unsigned char * |
| 583 |
fido_cbor_info_state_ptr(const fido_cbor_info_t *ci) |
| 584 |
{ |
| 585 |
return (ci->state.ptr); |
| 586 |
} |
| 587 |
|
| 588 |
size_t |
| 589 |
fido_cbor_info_state_len(const fido_cbor_info_t *ci) |
| 590 |
{ |
| 591 |
return (ci->state.len); |
| 592 |
} |
| 593 |
|
| 594 |
char ** |
| 595 |
fido_cbor_info_options_name_ptr(const fido_cbor_info_t *ci) |
| 596 |
{ |
| 597 |
return (ci->options.name); |
| 598 |
} |
| 599 |
|
| 600 |
const bool * |
| 601 |
fido_cbor_info_options_value_ptr(const fido_cbor_info_t *ci) |
| 602 |
{ |
| 603 |
return (ci->options.value); |
| 604 |
} |
| 605 |
|
| 606 |
size_t |
| 607 |
fido_cbor_info_options_len(const fido_cbor_info_t *ci) |
| 608 |
{ |
| 609 |
return (ci->options.len); |
| 610 |
} |
| 611 |
|
| 612 |
uint64_t |
| 613 |
fido_cbor_info_maxcredbloblen(const fido_cbor_info_t *ci) |
| 614 |
{ |
| 615 |
return (ci->maxcredbloblen); |
| 616 |
} |
| 617 |
|
| 618 |
uint64_t |
| 619 |
fido_cbor_info_maxmsgsiz(const fido_cbor_info_t *ci) |
| 620 |
{ |
| 621 |
return (ci->maxmsgsiz); |
| 622 |
} |
| 623 |
|
| 624 |
uint64_t |
| 625 |
fido_cbor_info_maxcredcntlst(const fido_cbor_info_t *ci) |
| 626 |
{ |
| 627 |
return (ci->maxcredcntlst); |
| 628 |
} |
| 629 |
|
| 630 |
uint64_t |
| 631 |
fido_cbor_info_maxcredidlen(const fido_cbor_info_t *ci) |
| 632 |
{ |
| 633 |
return (ci->maxcredidlen); |
| 634 |
} |
| 635 |
|
| 636 |
uint64_t |
| 637 |
fido_cbor_info_maxlargeblob(const fido_cbor_info_t *ci) |
| 638 |
{ |
| 639 |
return (ci->maxlargeblob); |
| 640 |
} |
| 641 |
|
| 642 |
uint64_t |
| 643 |
fido_cbor_info_fwversion(const fido_cbor_info_t *ci) |
| 644 |
{ |
| 645 |
return (ci->fwversion); |
| 646 |
} |
| 647 |
|
| 648 |
uint64_t |
| 649 |
fido_cbor_info_minpinlen(const fido_cbor_info_t *ci) |
| 650 |
{ |
| 651 |
return (ci->minpinlen); |
| 652 |
} |
| 653 |
|
| 654 |
uint64_t |
| 655 |
fido_cbor_info_maxrpid_minpinlen(const fido_cbor_info_t *ci) |
| 656 |
{ |
| 657 |
return (ci->maxrpid_minlen); |
| 658 |
} |
| 659 |
|
| 660 |
uint64_t |
| 661 |
fido_cbor_info_uv_attempts(const fido_cbor_info_t *ci) |
| 662 |
{ |
| 663 |
return (ci->uv_attempts); |
| 664 |
} |
| 665 |
|
| 666 |
int64_t |
| 667 |
fido_cbor_info_uv_count_since_pin(const fido_cbor_info_t *ci) |
| 668 |
{ |
| 669 |
return (ci->uv_since_pin); |
| 670 |
} |
| 671 |
|
| 672 |
uint64_t |
| 673 |
fido_cbor_info_uv_modality(const fido_cbor_info_t *ci) |
| 674 |
{ |
| 675 |
return (ci->uv_modality); |
| 676 |
} |
| 677 |
|
| 678 |
int64_t |
| 679 |
fido_cbor_info_rk_remaining(const fido_cbor_info_t *ci) |
| 680 |
{ |
| 681 |
return (ci->rk_remaining); |
| 682 |
} |
| 683 |
|
| 684 |
const uint8_t * |
| 685 |
fido_cbor_info_protocols_ptr(const fido_cbor_info_t *ci) |
| 686 |
{ |
| 687 |
return (ci->protocols.ptr); |
| 688 |
} |
| 689 |
|
| 690 |
size_t |
| 691 |
fido_cbor_info_protocols_len(const fido_cbor_info_t *ci) |
| 692 |
{ |
| 693 |
return (ci->protocols.len); |
| 694 |
} |
| 695 |
|
| 696 |
size_t |
| 697 |
fido_cbor_info_algorithm_count(const fido_cbor_info_t *ci) |
| 698 |
{ |
| 699 |
return (ci->algorithms.len); |
| 700 |
} |
| 701 |
|
| 702 |
const char * |
| 703 |
fido_cbor_info_algorithm_type(const fido_cbor_info_t *ci, size_t idx) |
| 704 |
{ |
| 705 |
if (idx >= ci->algorithms.len) |
| 706 |
return (NULL); |
| 707 |
|
| 708 |
return (ci->algorithms.ptr[idx].type); |
| 709 |
} |
| 710 |
|
| 711 |
int |
| 712 |
fido_cbor_info_algorithm_cose(const fido_cbor_info_t *ci, size_t idx) |
| 713 |
{ |
| 714 |
if (idx >= ci->algorithms.len) |
| 715 |
return (0); |
| 716 |
|
| 717 |
return (ci->algorithms.ptr[idx].cose); |
| 718 |
} |
| 719 |
|
| 720 |
bool |
| 721 |
fido_cbor_info_new_pin_required(const fido_cbor_info_t *ci) |
| 722 |
{ |
| 723 |
return (ci->new_pin_reqd); |
| 724 |
} |
| 725 |
|
| 726 |
bool |
| 727 |
fido_cbor_info_long_touch_reset(const fido_cbor_info_t *ci) |
| 728 |
{ |
| 729 |
return (ci->long_reset); |
| 730 |
} |
| 731 |
|
| 732 |
char ** |
| 733 |
fido_cbor_info_certs_name_ptr(const fido_cbor_info_t *ci) |
| 734 |
{ |
| 735 |
return (ci->certs.name); |
| 736 |
} |
| 737 |
|
| 738 |
const uint64_t * |
| 739 |
fido_cbor_info_certs_value_ptr(const fido_cbor_info_t *ci) |
| 740 |
{ |
| 741 |
return (ci->certs.value); |
| 742 |
} |
| 743 |
|
| 744 |
size_t |
| 745 |
fido_cbor_info_certs_len(const fido_cbor_info_t *ci) |
| 746 |
{ |
| 747 |
return (ci->certs.len); |
| 748 |
} |
| 749 |
|
| 750 |
char ** |
| 751 |
fido_cbor_info_attfmts_ptr(const fido_cbor_info_t *ci) |
| 752 |
{ |
| 753 |
return (ci->attfmts.ptr); |
| 754 |
} |
| 755 |
|
| 756 |
size_t |
| 757 |
fido_cbor_info_attfmts_len(const fido_cbor_info_t *ci) |
| 758 |
{ |
| 759 |
return (ci->attfmts.len); |
| 760 |
} |
| 761 |
|
| 762 |
char ** |
| 763 |
fido_cbor_info_reset_transports_ptr(const fido_cbor_info_t *ci) |
| 764 |
{ |
| 765 |
return (ci->rsttransports.ptr); |
| 766 |
} |
| 767 |
|
| 768 |
size_t |
| 769 |
fido_cbor_info_reset_transports_len(const fido_cbor_info_t *ci) |
| 770 |
{ |
| 771 |
return (ci->rsttransports.len); |
| 772 |
} |
| 773 |
|
| 774 |
int |
| 775 |
fido_cbor_info_pin_policy(const fido_cbor_info_t *ci) |
| 776 |
{ |
| 777 |
return (ci->pinpolicy); |
| 778 |
} |
| 779 |
|
| 780 |
const unsigned char* |
| 781 |
fido_cbor_info_pin_policy_url_ptr(const fido_cbor_info_t *ci) |
| 782 |
{ |
| 783 |
return (ci->pinpolicyurl.ptr); |
| 784 |
} |
| 785 |
|
| 786 |
size_t |
| 787 |
fido_cbor_info_pin_policy_url_len(const fido_cbor_info_t *ci) |
| 788 |
{ |
| 789 |
return (ci->pinpolicyurl.len); |
| 790 |
} |
| 791 |
|
| 792 |
uint64_t |
| 793 |
fido_cbor_info_maxpinlen(const fido_cbor_info_t *ci) |
| 794 |
{ |
| 795 |
return (ci->maxpinlen); |
| 796 |
} |
| 797 |
|
| 798 |
const uint8_t * |
| 799 |
fido_cbor_info_cfgcmds_ptr(const fido_cbor_info_t *ci) |
| 800 |
{ |
| 801 |
return (ci->cfgcmds.ptr); |
| 802 |
} |
| 803 |
|
| 804 |
size_t |
| 805 |
fido_cbor_info_cfgcmds_len(const fido_cbor_info_t *ci) |
| 806 |
{ |
| 807 |
return (ci->cfgcmds.len); |
| 808 |
} |
| 809 |
|
| 810 |
static int |
| 811 |
decrypt(const fido_blob_t *secret, const char *info, const fido_blob_t *in, |
| 812 |
fido_blob_t *out) |
| 813 |
{ |
| 814 |
fido_blob_t key; |
| 815 |
unsigned char keybuf[16]; |
| 816 |
int r = FIDO_ERR_INTERNAL; |
| 817 |
|
| 818 |
key.ptr = keybuf; |
| 819 |
key.len = sizeof(keybuf); |
| 820 |
|
| 821 |
fido_blob_reset(out); |
| 822 |
|
| 823 |
if (hkdf_sha256(key.ptr, key.len, info, secret) != 0) { |
| 824 |
fido_log_debug("%s: hkdf_sha256", __func__); |
| 825 |
goto fail; |
| 826 |
} |
| 827 |
if (aes128_cbc_dec(&key, in, out) != 0) { |
| 828 |
fido_log_debug("%s: aes128_cbc_dec", __func__); |
| 829 |
goto fail; |
| 830 |
} |
| 831 |
|
| 832 |
r = FIDO_OK; |
| 833 |
fail: |
| 834 |
explicit_bzero(key.ptr, key.len); |
| 835 |
|
| 836 |
return r; |
| 837 |
} |
| 838 |
|
| 839 |
int |
| 840 |
fido_cbor_info_decrypt(fido_cbor_info_t *ci, const unsigned char *ppuat, |
| 841 |
size_t ppuat_len) |
| 842 |
{ |
| 843 |
fido_blob_t secret; |
| 844 |
int r; |
| 845 |
|
| 846 |
memset(&secret, 0, sizeof(secret)); |
| 847 |
|
| 848 |
if (fido_blob_set(&secret, ppuat, ppuat_len) != 0) { |
| 849 |
fido_log_debug("%s: fido_blob_set", __func__); |
| 850 |
r = FIDO_ERR_INTERNAL; |
| 851 |
goto fail; |
| 852 |
} |
| 853 |
|
| 854 |
if (!fido_blob_is_empty(&ci->encid) && (r = decrypt(&secret, |
| 855 |
"encIdentifier", &ci->encid, &ci->id)) != FIDO_OK) { |
| 856 |
fido_log_debug("%s: encIdentifier", __func__); |
| 857 |
goto fail; |
| 858 |
} |
| 859 |
|
| 860 |
if (!fido_blob_is_empty(&ci->encstate) && (r = decrypt(&secret, |
| 861 |
"encCredStoreState", &ci->encstate, &ci->state)) != FIDO_OK) { |
| 862 |
fido_log_debug("%s: encIdentifier", __func__); |
| 863 |
goto fail; |
| 864 |
} |
| 865 |
|
| 866 |
r = FIDO_OK; |
| 867 |
fail: |
| 868 |
fido_blob_reset(&secret); |
| 869 |
|
| 870 |
return r; |
| 871 |
} |