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 parse_authkey(const cbor_item_t *key, const cbor_item_t *val, void *arg) 12 { 13 es256_pk_t *authkey = arg; 14 15 if (cbor_isa_uint(key) == false || 16 cbor_int_get_width(key) != CBOR_INT_8 || 17 cbor_get_uint8(key) != 1) { 18 fido_log_debug("%s: cbor type", __func__); 19 return (0); /* ignore */ 20 } 21 22 return (es256_pk_decode(val, authkey)); 23 } 24 25 static int 26 fido_dev_authkey_tx(fido_dev_t *dev, int *ms) 27 { 28 fido_blob_t f; 29 cbor_item_t *argv[2]; 30 int r; 31 32 fido_log_debug("%s: dev=%p", __func__, (void *)dev); 33 34 memset(&f, 0, sizeof(f)); 35 memset(argv, 0, sizeof(argv)); 36 37 /* add command parameters */ 38 if ((argv[0] = cbor_encode_pin_opt(dev)) == NULL || 39 (argv[1] = cbor_build_uint8(2)) == NULL) { 40 fido_log_debug("%s: cbor_build", __func__); 41 r = FIDO_ERR_INTERNAL; 42 goto fail; 43 } 44 45 /* frame and transmit */ 46 if (cbor_build_frame(CTAP_CBOR_CLIENT_PIN, argv, nitems(argv), 47 &f) < 0 || fido_tx(dev, CTAP_CMD_CBOR, f.ptr, f.len, ms) < 0) { 48 fido_log_debug("%s: fido_tx", __func__); 49 r = FIDO_ERR_TX; 50 goto fail; 51 } 52 53 r = FIDO_OK; 54 fail: 55 cbor_vector_free(argv, nitems(argv)); 56 free(f.ptr); 57 58 return (r); 59 } 60 61 static int 62 fido_dev_authkey_rx(fido_dev_t *dev, es256_pk_t *authkey, int *ms) 63 { 64 unsigned char *msg; 65 int msglen; 66 int r; 67 68 fido_log_debug("%s: dev=%p, authkey=%p, ms=%d", __func__, (void *)dev, 69 (void *)authkey, *ms); 70 71 memset(authkey, 0, sizeof(*authkey)); 72 73 if ((msg = malloc(FIDO_MAXMSG)) == NULL) { 74 r = FIDO_ERR_INTERNAL; 75 goto out; 76 } 77 78 if ((msglen = fido_rx(dev, CTAP_CMD_CBOR, msg, FIDO_MAXMSG, ms)) < 0) { 79 fido_log_debug("%s: fido_rx", __func__); 80 r = FIDO_ERR_RX; 81 goto out; 82 } 83 84 r = cbor_parse_reply(msg, (size_t)msglen, authkey, parse_authkey); 85 out: 86 freezero(msg, FIDO_MAXMSG); 87 88 return (r); 89 } 90 91 static int 92 fido_dev_authkey_wait(fido_dev_t *dev, es256_pk_t *authkey, int *ms) 93 { 94 int r; 95 96 if ((r = fido_dev_authkey_tx(dev, ms)) != FIDO_OK || 97 (r = fido_dev_authkey_rx(dev, authkey, ms)) != FIDO_OK) 98 return (r); 99 100 return (FIDO_OK); 101 } 102 103 int 104 fido_dev_authkey(fido_dev_t *dev, es256_pk_t *authkey, int *ms) 105 { 106 return (fido_dev_authkey_wait(dev, authkey, ms)); 107 } 108