## Coverage Report

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

next uncovered line (L), next uncovered region (R), next uncovered branch (B)

    /libfido2/src/touch.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 <openssl/sha.h>
    9
    #include "fido.h"
    10
    11
    int
    12
    fido_dev_get_touch_begin(fido_dev_t *dev)
    13
    11.5k
    {
    14
    11.5k
            fido_blob_t      f;
    15
    11.5k
            cbor_item_t     *argv[9];
    16
    11.5k
            const char      *clientdata = FIDO_DUMMY_CLIENTDATA;
    17
    11.5k
            const uint8_t    user_id = FIDO_DUMMY_USER_ID;
    18
    11.5k
            unsigned char    cdh[SHA256_DIGEST_LENGTH];
    19
    11.5k
            fido_rp_t        rp;
    20
    11.5k
            fido_user_t      user;
    21
    11.5k
            int              ms = dev->timeout_ms;
    22
    11.5k
            int              r = FIDO_ERR_INTERNAL;
    23
    24
    11.5k
            memset(&f, 0, sizeof(f));
    25
    11.5k
            memset(argv, 0, sizeof(argv));
    26
    11.5k
            memset(cdh, 0, sizeof(cdh));
    27
    11.5k
            memset(&rp, 0, sizeof(rp));
    28
    11.5k
            memset(&user, 0, sizeof(user));
    29
    30
    11.5k
            if (fido_dev_is_fido2(dev) == false)
    31
    10.6k
                    return (u2f_get_touch_begin(dev, &ms));
    32
    33
    886
            if (SHA256((const void *)clientdata, strlen(clientdata), cdh) != cdh) {
    34
    5
                    fido_log_debug("%s: sha256", __func__);
    35
    5
                    return (FIDO_ERR_INTERNAL);
    36
    5
            }
    37
    38
    881
            if ((rp.id = strdup(FIDO_DUMMY_RP_ID)) == NULL ||
    39
    881
                (user.name = strdup(FIDO_DUMMY_USER_NAME)) == NULL) {
    40
    24
                    fido_log_debug("%s: strdup", __func__);
    41
    24
                    goto fail;
    42
    24
            }
    43
    44
    857
            if (fido_blob_set(&user.id, &user_id, sizeof(user_id)) < 0) {
    45
    8
                    fido_log_debug("%s: fido_blob_set", __func__);
    46
    8
                    goto fail;
    47
    8
            }
    48
    49
    849
            if ((argv[0] = cbor_build_bytestring(cdh, sizeof(cdh))) == NULL ||
    50
    849
                (argv[1] = cbor_encode_rp_entity(&rp)) == NULL ||
    51
    849
                (argv[2] = cbor_encode_user_entity(&user)) == NULL ||
    52
    849
                (argv[3] = cbor_encode_pubkey_param(COSE_ES256)) == NULL) {
    53
    188
                    fido_log_debug("%s: cbor encode", __func__);
    54
    188
                    goto fail;
    55
    188
            }
    56
    57
    661
            if (fido_dev_supports_pin(dev)) {
    58
    570
                    if ((argv[7] = cbor_new_definite_bytestring()) == NULL ||
    59
    570
                        (argv[8] = cbor_encode_pin_opt(dev)) == NULL) {
    60
    30
                            fido_log_debug("%s: cbor encode", __func__);
    61
    30
                            goto fail;
    62
    30
                    }
    63
    570
            }
    64
    65
    631
            if (cbor_build_frame(CTAP_CBOR_MAKECRED, argv, nitems(argv), &f) < 0 ||
    66
    631
                fido_tx(dev, CTAP_CMD_CBOR, f.ptr, f.len, &ms) < 0) {
    67
    113
                    fido_log_debug("%s: fido_tx", __func__);
    68
    113
                    r = FIDO_ERR_TX;
    69
    113
                    goto fail;
    70
    113
            }
    71
    72
    518
            r = FIDO_OK;
    73
    881
    fail:
    74
    881
            cbor_vector_free(argv, nitems(argv));
    75
    881
            free(f.ptr);
    76
    881
            free(rp.id);
    77
    881
            free(user.name);
    78
    881
            free(user.id.ptr);
    79
    80
    881
            return (r);
    81
    518
    }
    82
    83
    int
    84
    fido_dev_get_touch_status(fido_dev_t *dev, int *touched, int ms)
    85
    11.5k
    {
    86
    11.5k
            int r;
    87
    88
    11.5k
            *touched = 0;
    89
    90
    11.5k
            if (fido_dev_is_fido2(dev) == false)
    91
    10.6k
                    return (u2f_get_touch_status(dev, touched, &ms));
    92
    93
    886
            switch ((r = fido_rx_cbor_status(dev, &ms))) {
    94
    5
            case FIDO_ERR_PIN_AUTH_INVALID:
    95
    9
            case FIDO_ERR_PIN_INVALID:
    96
    13
            case FIDO_ERR_PIN_NOT_SET:
    97
    402
            case FIDO_ERR_SUCCESS:
    98
    402
                    *touched = 1;
    99
    402
                    break;
    100
    274
            case FIDO_ERR_RX:
    101
                    /* ignore */
    102
    274
                    break;
    103
    210
            default:
    104
    210
                    fido_log_debug("%s: fido_rx_cbor_status", __func__);
    105
    210
                    return (r);
    106
    886
            }
    107
    108
    676
            return (FIDO_OK);
    109
    886
    }