Coverage Report

Created: 2026-04-08 06:32

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