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