Coverage Report

Created: 2022-04-27 14:33

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