Coverage Report

Created: 2024-02-05 19:20

/libfido2/src/hid.c
Line
Count
Source (jump to first uncovered line)
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
97.3M
{
13
97.3M
        *key = tag & 0xfc;
14
97.3M
        if ((*key & 0xf0) == 0xf0) {
15
253k
                fido_log_debug("%s: *key=0x%02x", __func__, *key);
16
253k
                return (-1);
17
253k
        }
18
19
97.0M
        *key_len = tag & 0x3;
20
97.0M
        if (*key_len == 3) {
21
480k
                *key_len = 4;
22
480k
        }
23
24
97.0M
        return (0);
25
97.3M
}
26
27
static int
28
get_key_val(const void *body, size_t key_len, uint32_t *val)
29
95.3M
{
30
95.3M
        const uint8_t *ptr = body;
31
32
95.3M
        switch (key_len) {
33
54.0M
        case 0:
34
54.0M
                *val = 0;
35
54.0M
                break;
36
10.6M
        case 1:
37
10.6M
                *val = ptr[0];
38
10.6M
                break;
39
30.4M
        case 2:
40
30.4M
                *val = (uint32_t)((ptr[1] << 8) | ptr[0]);
41
30.4M
                break;
42
210k
        default:
43
210k
                fido_log_debug("%s: key_len=%zu", __func__, key_len);
44
210k
                return (-1);
45
95.3M
        }
46
47
95.1M
        return (0);
48
95.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.83M
{
54
3.83M
        const uint8_t   *ptr = report_ptr;
55
3.83M
        size_t           len = report_len;
56
57
98.8M
        while (len > 0) {
58
97.1M
                const uint8_t tag = ptr[0];
59
97.1M
                ptr++;
60
97.1M
                len--;
61
62
97.1M
                uint8_t  key;
63
97.1M
                size_t   key_len;
64
97.1M
                uint32_t key_val;
65
66
97.1M
                if (get_key_len(tag, &key, &key_len) < 0 || key_len > len ||
67
97.1M
                    get_key_val(ptr, key_len, &key_val) < 0) {
68
2.16M
                        return (-1);
69
2.16M
                }
70
71
95.0M
                if (key == 0x4) {
72
1.62M
                        *usage_page = key_val;
73
1.62M
                }
74
75
95.0M
                ptr += key_len;
76
95.0M
                len -= key_len;
77
95.0M
        }
78
79
1.66M
        return (0);
80
3.83M
}
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
9.21k
{
86
9.21k
        const uint8_t   *ptr = report_ptr;
87
9.21k
        size_t           len = report_len;
88
9.21k
        uint32_t         report_size = 0;
89
90
118k
        while (len > 0) {
91
114k
                const uint8_t tag = ptr[0];
92
114k
                ptr++;
93
114k
                len--;
94
95
114k
                uint8_t  key;
96
114k
                size_t   key_len;
97
114k
                uint32_t key_val;
98
99
114k
                if (get_key_len(tag, &key, &key_len) < 0 || key_len > len ||
100
114k
                    get_key_val(ptr, key_len, &key_val) < 0) {
101
5.03k
                        return (-1);
102
5.03k
                }
103
104
109k
                if (key == 0x94) {
105
10.4k
                        report_size = key_val;
106
98.6k
                } else if (key == 0x80) {
107
1.68k
                        *report_in_len = (size_t)report_size;
108
97.0k
                } else if (key == 0x90) {
109
1.93k
                        *report_out_len = (size_t)report_size;
110
1.93k
                }
111
112
109k
                ptr += key_len;
113
109k
                len -= key_len;
114
109k
        }
115
116
4.17k
        return (0);
117
9.21k
}
118
119
fido_dev_info_t *
120
fido_dev_info_new(size_t n)
121
44.9k
{
122
44.9k
        return (calloc(n, sizeof(fido_dev_info_t)));
123
44.9k
}
124
125
static void
126
fido_dev_info_reset(fido_dev_info_t *di)
127
1.00M
{
128
1.00M
        free(di->path);
129
1.00M
        free(di->manufacturer);
130
1.00M
        free(di->product);
131
1.00M
        memset(di, 0, sizeof(*di));
132
1.00M
}
133
134
void
135
fido_dev_info_free(fido_dev_info_t **devlist_p, size_t n)
136
44.9k
{
137
44.9k
        fido_dev_info_t *devlist;
138
139
44.9k
        if (devlist_p == NULL || (devlist = *devlist_p) == NULL)
140
24
                return;
141
142
1.03M
        for (size_t i = 0; i < n; i++)
143
990k
                fido_dev_info_reset(&devlist[i]);
144
145
44.9k
        free(devlist);
146
147
44.9k
        *devlist_p = NULL;
148
44.9k
}
149
150
const fido_dev_info_t *
151
fido_dev_info_ptr(const fido_dev_info_t *devlist, size_t i)
152
15.9k
{
153
15.9k
        return (&devlist[i]);
154
15.9k
}
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
12.3k
{
161
12.3k
        char *path_copy = NULL, *manu_copy = NULL, *prod_copy = NULL;
162
12.3k
        int r;
163
164
12.3k
        if (path == NULL || manufacturer == NULL || product == NULL ||
165
12.3k
            io == NULL) {
166
0
                r = FIDO_ERR_INVALID_ARGUMENT;
167
0
                goto out;
168
0
        }
169
170
12.3k
        if ((path_copy = strdup(path)) == NULL ||
171
12.3k
            (manu_copy = strdup(manufacturer)) == NULL ||
172
12.3k
            (prod_copy = strdup(product)) == NULL) {
173
282
                r = FIDO_ERR_INTERNAL;
174
282
                goto out;
175
282
        }
176
177
12.0k
        fido_dev_info_reset(&devlist[i]);
178
12.0k
        devlist[i].path = path_copy;
179
12.0k
        devlist[i].manufacturer = manu_copy;
180
12.0k
        devlist[i].product = prod_copy;
181
12.0k
        devlist[i].io = *io;
182
12.0k
        if (transport)
183
12.0k
                devlist[i].transport = *transport;
184
12.0k
        r = FIDO_OK;
185
12.3k
out:
186
12.3k
        if (r != FIDO_OK) {
187
282
                free(prod_copy);
188
282
                free(manu_copy);
189
282
                free(path_copy);
190
282
        }
191
12.3k
        return (r);
192
12.0k
}
193
194
const char *
195
fido_dev_info_path(const fido_dev_info_t *di)
196
28.3k
{
197
28.3k
        return (di->path);
198
28.3k
}
199
200
int16_t
201
fido_dev_info_vendor(const fido_dev_info_t *di)
202
15.9k
{
203
15.9k
        return (di->vendor_id);
204
15.9k
}
205
206
int16_t
207
fido_dev_info_product(const fido_dev_info_t *di)
208
15.9k
{
209
15.9k
        return (di->product_id);
210
15.9k
}
211
212
const char *
213
fido_dev_info_manufacturer_string(const fido_dev_info_t *di)
214
28.3k
{
215
28.3k
        return (di->manufacturer);
216
28.3k
}
217
218
const char *
219
fido_dev_info_product_string(const fido_dev_info_t *di)
220
28.3k
{
221
28.3k
        return (di->product);
222
28.3k
}