Coverage Report

Created: 2025-10-04 18:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/libfido2/src/hid.c
Line
Count
Source
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
 * SPDX-License-Identifier: BSD-2-Clause
6
 */
7
8
#include "fido.h"
9
10
static int
11
get_key_len(uint8_t tag, uint8_t *key, size_t *key_len)
12
20.5M
{
13
20.5M
        *key = tag & 0xfc;
14
20.5M
        if ((*key & 0xf0) == 0xf0) {
15
199k
                fido_log_debug("%s: *key=0x%02x", __func__, *key);
16
199k
                return (-1);
17
199k
        }
18
19
20.3M
        *key_len = tag & 0x3;
20
20.3M
        if (*key_len == 3) {
21
381k
                *key_len = 4;
22
381k
        }
23
24
20.3M
        return (0);
25
20.5M
}
26
27
static int
28
get_key_val(const void *body, size_t key_len, uint32_t *val)
29
19.3M
{
30
19.3M
        const uint8_t *ptr = body;
31
32
19.3M
        switch (key_len) {
33
10.7M
        case 0:
34
10.7M
                *val = 0;
35
10.7M
                break;
36
5.74M
        case 1:
37
5.74M
                *val = ptr[0];
38
5.74M
                break;
39
2.72M
        case 2:
40
2.72M
                *val = (uint32_t)((ptr[1] << 8) | ptr[0]);
41
2.72M
                break;
42
86.7k
        default:
43
86.7k
                fido_log_debug("%s: key_len=%zu", __func__, key_len);
44
86.7k
                return (-1);
45
19.3M
        }
46
47
19.2M
        return (0);
48
19.3M
}
49
50
int
51
fido_hid_get_usage(const uint8_t *report_ptr, size_t report_len,
52
    uint32_t *usage_page)
53
3.02M
{
54
3.02M
        const uint8_t   *ptr = report_ptr;
55
3.02M
        size_t           len = report_len;
56
57
22.2M
        while (len > 0) {
58
20.4M
                const uint8_t tag = ptr[0];
59
20.4M
                ptr++;
60
20.4M
                len--;
61
62
20.4M
                uint8_t  key;
63
20.4M
                size_t   key_len;
64
20.4M
                uint32_t key_val;
65
66
20.4M
                if (get_key_len(tag, &key, &key_len) < 0 || key_len > len ||
67
20.4M
                    get_key_val(ptr, key_len, &key_val) < 0) {
68
1.28M
                        return (-1);
69
1.28M
                }
70
71
19.1M
                if (key == 0x4) {
72
1.23M
                        *usage_page = key_val;
73
1.23M
                }
74
75
19.1M
                ptr += key_len;
76
19.1M
                len -= key_len;
77
19.1M
        }
78
79
1.73M
        return (0);
80
3.02M
}
81
82
int
83
fido_hid_get_report_len(const uint8_t *report_ptr, size_t report_len,
84
    size_t *report_in_len, size_t *report_out_len)
85
6.87k
{
86
6.87k
        const uint8_t   *ptr = report_ptr;
87
6.87k
        size_t           len = report_len;
88
6.87k
        uint32_t         report_size = 0;
89
90
40.6k
        while (len > 0) {
91
36.9k
                const uint8_t tag = ptr[0];
92
36.9k
                ptr++;
93
36.9k
                len--;
94
95
36.9k
                uint8_t  key;
96
36.9k
                size_t   key_len;
97
36.9k
                uint32_t key_val;
98
99
36.9k
                if (get_key_len(tag, &key, &key_len) < 0 || key_len > len ||
100
36.9k
                    get_key_val(ptr, key_len, &key_val) < 0) {
101
3.20k
                        return (-1);
102
3.20k
                }
103
104
33.7k
                if (key == 0x94) {
105
1.48k
                        report_size = key_val;
106
32.2k
                } else if (key == 0x80) {
107
1.74k
                        *report_in_len = (size_t)report_size;
108
30.5k
                } else if (key == 0x90) {
109
1.09k
                        *report_out_len = (size_t)report_size;
110
1.09k
                }
111
112
33.7k
                ptr += key_len;
113
33.7k
                len -= key_len;
114
33.7k
        }
115
116
3.67k
        return (0);
117
6.87k
}
118
119
fido_dev_info_t *
120
fido_dev_info_new(size_t n)
121
21.3k
{
122
21.3k
        return (calloc(n, sizeof(fido_dev_info_t)));
123
21.3k
}
124
125
static void
126
fido_dev_info_reset(fido_dev_info_t *di)
127
515k
{
128
515k
        free(di->path);
129
515k
        free(di->manufacturer);
130
515k
        free(di->product);
131
515k
        memset(di, 0, sizeof(*di));
132
515k
}
133
134
void
135
fido_dev_info_free(fido_dev_info_t **devlist_p, size_t n)
136
21.3k
{
137
21.3k
        fido_dev_info_t *devlist;
138
139
21.3k
        if (devlist_p == NULL || (devlist = *devlist_p) == NULL)
140
70
                return;
141
142
527k
        for (size_t i = 0; i < n; i++)
143
506k
                fido_dev_info_reset(&devlist[i]);
144
145
21.2k
        free(devlist);
146
147
21.2k
        *devlist_p = NULL;
148
21.2k
}
149
150
const fido_dev_info_t *
151
fido_dev_info_ptr(const fido_dev_info_t *devlist, size_t i)
152
10.3k
{
153
10.3k
        return (&devlist[i]);
154
10.3k
}
155
156
int
157
fido_dev_info_set(fido_dev_info_t *devlist, size_t i,
158
    const char *path, const char *manufacturer, const char *product,
159
    const fido_dev_io_t *io, const fido_dev_transport_t *transport)
160
9.59k
{
161
9.59k
        char *path_copy = NULL, *manu_copy = NULL, *prod_copy = NULL;
162
9.59k
        int r;
163
164
9.59k
        if (path == NULL || manufacturer == NULL || product == NULL ||
165
9.59k
            io == NULL) {
166
0
                r = FIDO_ERR_INVALID_ARGUMENT;
167
0
                goto out;
168
0
        }
169
170
9.59k
        if ((path_copy = strdup(path)) == NULL ||
171
9.59k
            (manu_copy = strdup(manufacturer)) == NULL ||
172
9.59k
            (prod_copy = strdup(product)) == NULL) {
173
178
                r = FIDO_ERR_INTERNAL;
174
178
                goto out;
175
178
        }
176
177
9.41k
        fido_dev_info_reset(&devlist[i]);
178
9.41k
        devlist[i].path = path_copy;
179
9.41k
        devlist[i].manufacturer = manu_copy;
180
9.41k
        devlist[i].product = prod_copy;
181
9.41k
        devlist[i].io = *io;
182
9.41k
        if (transport)
183
9.41k
                devlist[i].transport = *transport;
184
9.41k
        r = FIDO_OK;
185
9.59k
out:
186
9.59k
        if (r != FIDO_OK) {
187
178
                free(prod_copy);
188
178
                free(manu_copy);
189
178
                free(path_copy);
190
178
        }
191
9.59k
        return (r);
192
9.41k
}
193
194
const char *
195
fido_dev_info_path(const fido_dev_info_t *di)
196
19.9k
{
197
19.9k
        return (di->path);
198
19.9k
}
199
200
int16_t
201
fido_dev_info_vendor(const fido_dev_info_t *di)
202
10.3k
{
203
10.3k
        return (di->vendor_id);
204
10.3k
}
205
206
int16_t
207
fido_dev_info_product(const fido_dev_info_t *di)
208
10.3k
{
209
10.3k
        return (di->product_id);
210
10.3k
}
211
212
const char *
213
fido_dev_info_manufacturer_string(const fido_dev_info_t *di)
214
19.9k
{
215
19.9k
        return (di->manufacturer);
216
19.9k
}
217
218
const char *
219
fido_dev_info_product_string(const fido_dev_info_t *di)
220
19.9k
{
221
19.9k
        return (di->product);
222
19.9k
}