Coverage Report

Created: 2023-09-23 17:42

/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
1.66M
{
13
1.66M
        *key = tag & 0xfc;
14
1.66M
        if ((*key & 0xf0) == 0xf0) {
15
24.5k
                fido_log_debug("%s: *key=0x%02x", __func__, *key);
16
24.5k
                return (-1);
17
24.5k
        }
18
19
1.63M
        *key_len = tag & 0x3;
20
1.63M
        if (*key_len == 3) {
21
75.2k
                *key_len = 4;
22
75.2k
        }
23
24
1.63M
        return (0);
25
1.66M
}
26
27
static int
28
get_key_val(const void *body, size_t key_len, uint32_t *val)
29
1.41M
{
30
1.41M
        const uint8_t *ptr = body;
31
32
1.41M
        switch (key_len) {
33
936k
        case 0:
34
936k
                *val = 0;
35
936k
                break;
36
106k
        case 1:
37
106k
                *val = ptr[0];
38
106k
                break;
39
355k
        case 2:
40
355k
                *val = (uint32_t)((ptr[1] << 8) | ptr[0]);
41
355k
                break;
42
11.9k
        default:
43
11.9k
                fido_log_debug("%s: key_len=%zu", __func__, key_len);
44
11.9k
                return (-1);
45
1.41M
        }
46
47
1.39M
        return (0);
48
1.41M
}
49
50
int
51
fido_hid_get_usage(const uint8_t *report_ptr, size_t report_len,
52
    uint32_t *usage_page)
53
483k
{
54
483k
        const uint8_t   *ptr = report_ptr;
55
483k
        size_t           len = report_len;
56
57
1.87M
        while (len > 0) {
58
1.66M
                const uint8_t tag = ptr[0];
59
1.66M
                ptr++;
60
1.66M
                len--;
61
62
1.66M
                uint8_t  key;
63
1.66M
                size_t   key_len;
64
1.66M
                uint32_t key_val;
65
66
1.66M
                if (get_key_len(tag, &key, &key_len) < 0 || key_len > len ||
67
1.66M
                    get_key_val(ptr, key_len, &key_val) < 0) {
68
265k
                        return (-1);
69
265k
                }
70
71
1.39M
                if (key == 0x4) {
72
76.9k
                        *usage_page = key_val;
73
76.9k
                }
74
75
1.39M
                ptr += key_len;
76
1.39M
                len -= key_len;
77
1.39M
        }
78
79
218k
        return (0);
80
483k
}
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
1.21k
{
86
1.21k
        const uint8_t   *ptr = report_ptr;
87
1.21k
        size_t           len = report_len;
88
1.21k
        uint32_t         report_size = 0;
89
90
3.54k
        while (len > 0) {
91
3.03k
                const uint8_t tag = ptr[0];
92
3.03k
                ptr++;
93
3.03k
                len--;
94
95
3.03k
                uint8_t  key;
96
3.03k
                size_t   key_len;
97
3.03k
                uint32_t key_val;
98
99
3.03k
                if (get_key_len(tag, &key, &key_len) < 0 || key_len > len ||
100
3.03k
                    get_key_val(ptr, key_len, &key_val) < 0) {
101
707
                        return (-1);
102
707
                }
103
104
2.32k
                if (key == 0x94) {
105
203
                        report_size = key_val;
106
2.12k
                } else if (key == 0x80) {
107
215
                        *report_in_len = (size_t)report_size;
108
1.90k
                } else if (key == 0x90) {
109
232
                        *report_out_len = (size_t)report_size;
110
232
                }
111
112
2.32k
                ptr += key_len;
113
2.32k
                len -= key_len;
114
2.32k
        }
115
116
506
        return (0);
117
1.21k
}
118
119
fido_dev_info_t *
120
fido_dev_info_new(size_t n)
121
3.25k
{
122
3.25k
        return (calloc(n, sizeof(fido_dev_info_t)));
123
3.25k
}
124
125
static void
126
fido_dev_info_reset(fido_dev_info_t *di)
127
68.2k
{
128
68.2k
        free(di->path);
129
68.2k
        free(di->manufacturer);
130
68.2k
        free(di->product);
131
68.2k
        memset(di, 0, sizeof(*di));
132
68.2k
}
133
134
void
135
fido_dev_info_free(fido_dev_info_t **devlist_p, size_t n)
136
3.25k
{
137
3.25k
        fido_dev_info_t *devlist;
138
139
3.25k
        if (devlist_p == NULL || (devlist = *devlist_p) == NULL)
140
15
                return;
141
142
70.0k
        for (size_t i = 0; i < n; i++)
143
66.8k
                fido_dev_info_reset(&devlist[i]);
144
145
3.24k
        free(devlist);
146
147
3.24k
        *devlist_p = NULL;
148
3.24k
}
149
150
const fido_dev_info_t *
151
fido_dev_info_ptr(const fido_dev_info_t *devlist, size_t i)
152
1.52k
{
153
1.52k
        return (&devlist[i]);
154
1.52k
}
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
1.42k
{
161
1.42k
        char *path_copy = NULL, *manu_copy = NULL, *prod_copy = NULL;
162
1.42k
        int r;
163
164
1.42k
        if (path == NULL || manufacturer == NULL || product == NULL ||
165
1.42k
            io == NULL) {
166
0
                r = FIDO_ERR_INVALID_ARGUMENT;
167
0
                goto out;
168
0
        }
169
170
1.42k
        if ((path_copy = strdup(path)) == NULL ||
171
1.42k
            (manu_copy = strdup(manufacturer)) == NULL ||
172
1.42k
            (prod_copy = strdup(product)) == NULL) {
173
37
                r = FIDO_ERR_INTERNAL;
174
37
                goto out;
175
37
        }
176
177
1.39k
        fido_dev_info_reset(&devlist[i]);
178
1.39k
        devlist[i].path = path_copy;
179
1.39k
        devlist[i].manufacturer = manu_copy;
180
1.39k
        devlist[i].product = prod_copy;
181
1.39k
        devlist[i].io = *io;
182
1.39k
        if (transport)
183
1.39k
                devlist[i].transport = *transport;
184
1.39k
        r = FIDO_OK;
185
1.42k
out:
186
1.42k
        if (r != FIDO_OK) {
187
37
                free(prod_copy);
188
37
                free(manu_copy);
189
37
                free(path_copy);
190
37
        }
191
1.42k
        return (r);
192
1.39k
}
193
194
const char *
195
fido_dev_info_path(const fido_dev_info_t *di)
196
2.95k
{
197
2.95k
        return (di->path);
198
2.95k
}
199
200
int16_t
201
fido_dev_info_vendor(const fido_dev_info_t *di)
202
1.52k
{
203
1.52k
        return (di->vendor_id);
204
1.52k
}
205
206
int16_t
207
fido_dev_info_product(const fido_dev_info_t *di)
208
1.52k
{
209
1.52k
        return (di->product_id);
210
1.52k
}
211
212
const char *
213
fido_dev_info_manufacturer_string(const fido_dev_info_t *di)
214
2.95k
{
215
2.95k
        return (di->manufacturer);
216
2.95k
}
217
218
const char *
219
fido_dev_info_product_string(const fido_dev_info_t *di)
220
2.95k
{
221
2.95k
        return (di->product);
222
2.95k
}