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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
/*
* 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.
*/
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_SYS_RANDOM_H
#include <sys/random.h>
#endif
#include <fcntl.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "fido.h"
#if defined(_WIN32)
#include <windows.h>
#include <winternl.h>
#include <winerror.h>
#include <stdio.h>
#include <bcrypt.h>
#include <sal.h>
static int
obtain_nonce(uint64_t *nonce)
{
NTSTATUS status;
status = BCryptGenRandom(NULL, (unsigned char *)nonce, sizeof(*nonce),
BCRYPT_USE_SYSTEM_PREFERRED_RNG);
if (!NT_SUCCESS(status))
return (-1);
return (0);
}
#elif defined(HAVE_ARC4RANDOM_BUF)
static int
obtain_nonce(uint64_t *nonce)
{
arc4random_buf(nonce, sizeof(*nonce));
return (0);
}
#elif defined(HAVE_GETRANDOM)
static int
obtain_nonce(uint64_t *nonce)
{
if (getrandom(nonce, sizeof(*nonce), 0) < 0)
return (-1);
return (0);
}
#elif defined(HAVE_DEV_URANDOM)
static int
obtain_nonce(uint64_t *nonce)
{
int fd = -1;
int ok = -1;
ssize_t r;
if ((fd = open(FIDO_RANDOM_DEV, O_RDONLY)) < 0)
goto fail;
if ((r = read(fd, nonce, sizeof(*nonce))) < 0 ||
(size_t)r != sizeof(*nonce))
goto fail;
ok = 0;
fail:
if (fd != -1)
close(fd);
return (ok);
}
#else
#error "please provide an implementation of obtain_nonce() for your platform"
#endif /* _WIN32 */
#ifndef TLS
#define TLS
#endif
typedef struct dev_manifest_func_node {
dev_manifest_func_t manifest_func;
struct dev_manifest_func_node *next;
} dev_manifest_func_node_t;
static TLS dev_manifest_func_node_t *manifest_funcs = NULL;
static void
find_manifest_func_node(dev_manifest_func_t f, dev_manifest_func_node_t **curr,
dev_manifest_func_node_t **prev)
{
*prev = NULL;
*curr = manifest_funcs;
while (*curr != NULL && (*curr)->manifest_func != f) {
*prev = *curr;
*curr = (*curr)->next;
}
}
static int
fido_dev_open_tx(fido_dev_t *dev, const char *path)
{
const uint8_t cmd = CTAP_CMD_INIT;
if (dev->io_handle != NULL) {
fido_log_debug("%s: handle=%p", __func__, dev->io_handle);
return (FIDO_ERR_INVALID_ARGUMENT);
}
if (dev->io.open == NULL || dev->io.close == NULL) {
fido_log_debug("%s: NULL open/close", __func__);
return (FIDO_ERR_INVALID_ARGUMENT);
}
if (obtain_nonce(&dev->nonce) < 0) {
fido_log_debug("%s: obtain_nonce", __func__);
return (FIDO_ERR_INTERNAL);
}
if ((dev->io_handle = dev->io.open(path)) == NULL) {
fido_log_debug("%s: dev->io.open", __func__);
return (FIDO_ERR_INTERNAL);
}
if (fido_tx(dev, cmd, &dev->nonce, sizeof(dev->nonce)) < 0) {
fido_log_debug("%s: fido_tx", __func__);
dev->io.close(dev->io_handle);
dev->io_handle = NULL;
return (FIDO_ERR_TX);
}
return (FIDO_OK);
}
static int
fido_dev_open_rx(fido_dev_t *dev, int ms)
{
fido_cbor_info_t *info = NULL;
int reply_len;
int r;
if ((reply_len = fido_rx(dev, CTAP_CMD_INIT, &dev->attr,
sizeof(dev->attr), ms)) < 0) {
fido_log_debug("%s: fido_rx", __func__);
r = FIDO_ERR_RX;
goto fail;
}
#ifdef FIDO_FUZZ
dev->attr.nonce = dev->nonce;
#endif
if ((size_t)reply_len != sizeof(dev->attr) ||
dev->attr.nonce != dev->nonce) {
fido_log_debug("%s: invalid nonce", __func__);
r = FIDO_ERR_RX;
goto fail;
}
dev->cid = dev->attr.cid;
if (fido_dev_is_fido2(dev)) {
if ((info = fido_cbor_info_new()) == NULL) {
fido_log_debug("%s: fido_cbor_info_new", __func__);
r = FIDO_ERR_INTERNAL;
goto fail;
}
if (fido_dev_get_cbor_info_wait(dev, info, ms) != FIDO_OK) {
fido_log_debug("%s: falling back to u2f", __func__);
fido_dev_force_u2f(dev);
}
}
if (fido_dev_is_fido2(dev) && info != NULL) {
fido_log_debug("%s: FIDO_MAXMSG=%d, maxmsgsiz=%lu", __func__,
FIDO_MAXMSG, (unsigned long)fido_cbor_info_maxmsgsiz(info));
}
r = FIDO_OK;
fail:
fido_cbor_info_free(&info);
if (r != FIDO_OK) {
dev->io.close(dev->io_handle);
dev->io_handle = NULL;
}
return (r);
}
static int
fido_dev_open_wait(fido_dev_t *dev, const char *path, int ms)
{
int r;
if ((r = fido_dev_open_tx(dev, path)) != FIDO_OK ||
(r = fido_dev_open_rx(dev, ms)) != FIDO_OK)
return (r);
return (FIDO_OK);
}
int
fido_dev_register_manifest_func(const dev_manifest_func_t f)
{
dev_manifest_func_node_t *prev, *curr, *n;
find_manifest_func_node(f, &curr, &prev);
if (curr != NULL)
return (FIDO_OK);
if ((n = calloc(1, sizeof(*n))) == NULL) {
fido_log_debug("%s: calloc", __func__);
return (FIDO_ERR_INTERNAL);
}
n->manifest_func = f;
n->next = manifest_funcs;
manifest_funcs = n;
return (FIDO_OK);
}
void
fido_dev_unregister_manifest_func(const dev_manifest_func_t f)
{
dev_manifest_func_node_t *prev, *curr;
find_manifest_func_node(f, &curr, &prev);
if (curr == NULL)
return;
if (prev != NULL)
prev->next = curr->next;
else
manifest_funcs = curr->next;
free(curr);
}
int
fido_dev_info_manifest(fido_dev_info_t *devlist, size_t ilen, size_t *olen)
{
dev_manifest_func_node_t *curr = NULL;
dev_manifest_func_t m_func;
size_t curr_olen;
int r;
*olen = 0;
if (fido_dev_register_manifest_func(fido_hid_manifest) != FIDO_OK)
return (FIDO_ERR_INTERNAL);
for (curr = manifest_funcs; curr != NULL; curr = curr->next) {
curr_olen = 0;
m_func = curr->manifest_func;
r = m_func(devlist + *olen, ilen - *olen, &curr_olen);
if (r != FIDO_OK)
return (r);
*olen += curr_olen;
if (*olen == ilen)
break;
}
return (FIDO_OK);
}
int
fido_dev_open_with_info(fido_dev_t *dev)
{
if (dev->path == NULL)
return (FIDO_ERR_INVALID_ARGUMENT);
return (fido_dev_open_wait(dev, dev->path, -1));
}
int
fido_dev_open(fido_dev_t *dev, const char *path)
{
return (fido_dev_open_wait(dev, path, -1));
}
int
fido_dev_close(fido_dev_t *dev)
{
if (dev->io_handle == NULL || dev->io.close == NULL)
return (FIDO_ERR_INVALID_ARGUMENT);
dev->io.close(dev->io_handle);
dev->io_handle = NULL;
return (FIDO_OK);
}
int
fido_dev_cancel(fido_dev_t *dev)
{
if (fido_tx(dev, CTAP_CMD_CANCEL, NULL, 0) < 0)
return (FIDO_ERR_TX);
return (FIDO_OK);
}
int
fido_dev_set_io_functions(fido_dev_t *dev, const fido_dev_io_t *io)
{
if (dev->io_handle != NULL) {
fido_log_debug("%s: non-NULL handle", __func__);
return (FIDO_ERR_INVALID_ARGUMENT);
}
if (io == NULL || io->open == NULL || io->close == NULL ||
io->read == NULL || io->write == NULL) {
fido_log_debug("%s: NULL function", __func__);
return (FIDO_ERR_INVALID_ARGUMENT);
}
dev->io = *io;
return (FIDO_OK);
}
int
fido_dev_set_transport_functions(fido_dev_t *dev, const fido_dev_transport_t *t)
{
if (dev->io_handle != NULL) {
fido_log_debug("%s: non-NULL handle", __func__);
return (FIDO_ERR_INVALID_ARGUMENT);
}
dev->transport = *t;
return (FIDO_OK);
}
void
fido_init(int flags)
{
if (flags & FIDO_DEBUG || getenv("FIDO_DEBUG") != NULL)
fido_log_init();
}
fido_dev_t *
fido_dev_new(void)
{
fido_dev_t *dev;
if ((dev = calloc(1, sizeof(*dev))) == NULL)
return (NULL);
dev->cid = CTAP_CID_BROADCAST;
dev->io = (fido_dev_io_t) {
&fido_hid_open,
&fido_hid_close,
&fido_hid_read,
&fido_hid_write,
};
return (dev);
}
fido_dev_t *
fido_dev_new_with_info(const fido_dev_info_t *di)
{
fido_dev_t *dev;
if ((dev = calloc(1, sizeof(*dev))) == NULL)
return (NULL);
dev->cid = CTAP_CID_BROADCAST;
if (di->io.open == NULL || di->io.close == NULL ||
di->io.read == NULL || di->io.write == NULL) {
fido_log_debug("%s: NULL function", __func__);
fido_dev_free(&dev);
return (NULL);
}
dev->io = di->io;
dev->transport = di->transport;
if ((dev->path = strdup(di->path)) == NULL) {
fido_log_debug("%s: strdup", __func__);
fido_dev_free(&dev);
return (NULL);
}
return (dev);
}
void
fido_dev_free(fido_dev_t **dev_p)
{
fido_dev_t *dev;
if (dev_p == NULL || (dev = *dev_p) == NULL)
return;
free(dev->path);
free(dev);
*dev_p = NULL;
}
uint8_t
fido_dev_protocol(const fido_dev_t *dev)
{
return (dev->attr.protocol);
}
uint8_t
fido_dev_major(const fido_dev_t *dev)
{
return (dev->attr.major);
}
uint8_t
fido_dev_minor(const fido_dev_t *dev)
{
return (dev->attr.minor);
}
uint8_t
fido_dev_build(const fido_dev_t *dev)
{
return (dev->attr.build);
}
uint8_t
fido_dev_flags(const fido_dev_t *dev)
{
return (dev->attr.flags);
}
bool
fido_dev_is_fido2(const fido_dev_t *dev)
{
return (dev->attr.flags & FIDO_CAP_CBOR);
}
void
fido_dev_force_u2f(fido_dev_t *dev)
{
dev->attr.flags &= ~FIDO_CAP_CBOR;
}
void
fido_dev_force_fido2(fido_dev_t *dev)
{
dev->attr.flags |= FIDO_CAP_CBOR;
}
|