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