## Coverage Report

#### Created: 2025-03-01 02:43

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