-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathu2f_util.cc
More file actions
498 lines (393 loc) · 12.2 KB
/
u2f_util.cc
File metadata and controls
498 lines (393 loc) · 12.2 KB
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// Copyright 2014 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include <stdlib.h>
#include <string.h>
#ifdef __OS_WIN
#include <winsock2.h> // ntohl, htonl
#else
#include <arpa/inet.h> // ntohl, htonl
#endif
#include <string>
#include "u2f_util.h"
// This is a "library"; do not abort.
#define AbortOrNot() \
std::cerr << "returning false" << std::endl; \
return false
#ifdef __OS_WIN
#define strdup _strdup
#endif
#ifdef __OS_DARWIN
// Implement something compatible w/ linux clock_gettime()
#include <mach/mach_time.h>
#define CLOCK_MONOTONIC 0
static
void clock_gettime(int which, struct timespec* ts) {
static mach_timebase_info_data_t __clock_gettime_inf;
uint64_t now, nano;
now = mach_absolute_time();
if (0 == __clock_gettime_inf.denom)
mach_timebase_info(&__clock_gettime_inf);
nano = now * __clock_gettime_inf.numer / __clock_gettime_inf.denom;
ts->tv_sec = nano * 1e-9;
ts->tv_nsec = nano - (ts->tv_sec * 1e9);
}
#endif // __OS_MAC
std::string b2a(const void* ptr, size_t size) {
const uint8_t* p = reinterpret_cast<const uint8_t*>(ptr);
std::string result;
for (size_t i = 0; i < 2 * size; ++i) {
int nib = p[i / 2];
if ((i & 1) == 0) nib >>= 4;
nib &= 15;
result.push_back("0123456789ABCDEF"[nib]);
}
return result;
}
std::string b2a(const std::string& s) {
return b2a(s.data(), s.size());
}
std::string a2b(const std::string& s) {
std::string result;
int v;
for (size_t i = 0; i < s.size(); ++i) {
if ((i & 1) == 1) v <<= 4; else v = 0;
char d = s[i];
if (d >= '0' && d <= '9') v += (d - '0');
else if (d >= 'A' && d <= 'F') v += (d - 'A' + 10);
else if (d >= 'a' && d <= 'f') v += (d - 'a' + 10);
if ((i & 1) == 1) result.push_back(v & 255);
}
return result;
}
float U2Fob_deltaTime(uint64_t* state) {
uint64_t now, delta;
#ifdef __OS_WIN
now = (uint64_t) GetTickCount64() * 1000000;
#else
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
now = (uint64_t) (ts.tv_sec * 1e9 + ts.tv_nsec);
#endif
delta = *state ? now - *state : 0;
*state = now;
return (float) (delta / 1.0e9);
}
struct U2Fob* U2Fob_create() {
struct U2Fob* f = NULL;
if (hid_init() == 0) {
f = (struct U2Fob*)malloc(sizeof(struct U2Fob));
memset(f, 0, sizeof(struct U2Fob));
f->cid = -1;
}
return f;
}
void U2Fob_destroy(struct U2Fob* device) {
if (device) {
U2Fob_close(device);
if (device->path) {
free(device->path);
device->path = NULL;
}
free(device);
}
hid_exit();
}
uint32_t U2Fob_getCid(struct U2Fob* device) {
return device->cid;
}
int U2Fob_open(struct U2Fob* device, const char* path) {
U2Fob_close(device);
if (device->path) {
free(device->path);
device->path = NULL;
}
device->path = strdup(path);
device->dev = hid_open_path(device->path);
return device->dev != NULL ? -ERR_NONE : -ERR_OTHER;
}
void U2Fob_close(struct U2Fob* device) {
if (device->dev) {
hid_close(device->dev);
device->dev = NULL;
}
}
int U2Fob_reopen(struct U2Fob* device) {
U2Fob_close(device);
device->dev = hid_open_path(device->path);
return device->dev != NULL ? -ERR_NONE : -ERR_OTHER;
}
void U2Fob_setLog(struct U2Fob* device, FILE* fd, int level) {
device->logfp = fd;
device->loglevel = level;
device->logtime = 0;
U2Fob_deltaTime(&device->logtime);
}
static
void U2Fob_logFrame(struct U2Fob* device,
const char* tag, const U2FHID_FRAME* f) {
if (device->logfp) {
fprintf(device->logfp, "t+%.3f", U2Fob_deltaTime(&device->logtime));
fprintf(device->logfp, "%s %08x:%02x", tag, f->cid, f->type);
if (f->type & TYPE_INIT) {
int len = f->init.bcnth * 256 + f->init.bcntl;
fprintf(device->logfp, "[%d]:", len);
for (size_t i = 0; i < sizeof(f->init.data); ++i)
fprintf(device->logfp, "%02X", f->init.data[i]);
} else {
fprintf(device->logfp, ":");
for (size_t i = 0; i < sizeof(f->cont.data); ++i)
fprintf(device->logfp, "%02X", f->cont.data[i]);
}
fprintf(device->logfp, "\n");
}
}
int U2Fob_sendHidFrame(struct U2Fob* device, U2FHID_FRAME* f) {
uint8_t d[sizeof(U2FHID_FRAME) + 1];
int res;
d[0] = 0; // un-numbered report
f->cid = htonl(f->cid); // cid is in network order on the wire
memcpy(d + 1, f, sizeof(U2FHID_FRAME));
f->cid = ntohl(f->cid);
if (!device->dev) return -ERR_OTHER;
res = hid_write(device->dev, d, sizeof(d));
if (res == sizeof(d)) {
U2Fob_logFrame(device, ">", f);
return 0;
}
return -ERR_OTHER;
}
int U2Fob_receiveHidFrame(struct U2Fob* device, U2FHID_FRAME* r, float to) {
if (to <= 0.0)
return -ERR_MSG_TIMEOUT;
if (!device->dev) return -ERR_OTHER;
memset((int8_t*)r, 0xEE, sizeof(U2FHID_FRAME));
int res = hid_read_timeout(device->dev,
(uint8_t*) r, sizeof(U2FHID_FRAME),
(int) (to * 1000));
if (res == sizeof(U2FHID_FRAME)) {
r->cid = ntohl(r->cid);
U2Fob_logFrame(device, "<", r);
return 0;
}
if (res == -1)
return -ERR_OTHER;
if (device->logfp) {
fprintf(device->logfp, "t+%.3f", U2Fob_deltaTime(&device->logtime));
fprintf(device->logfp, "< (timeout)\n");
}
return -ERR_MSG_TIMEOUT;
}
int U2Fob_init(struct U2Fob* device) {
int res;
U2FHID_FRAME challenge;
for (size_t i = 0; i < sizeof(device->nonce); ++i) {
device->nonce[i] ^= (rand() >> 3);
}
challenge.cid = device->cid;
challenge.init.cmd = U2FHID_INIT | TYPE_INIT;
challenge.init.bcnth = 0;
challenge.init.bcntl = INIT_NONCE_SIZE;
memcpy(challenge.init.data, device->nonce, INIT_NONCE_SIZE);
res = U2Fob_sendHidFrame(device, &challenge);
if (res != 0) return res;
for (;;) {
U2FHID_FRAME response;
res = U2Fob_receiveHidFrame(device, &response, 2.0);
if (res == -ERR_MSG_TIMEOUT) return res;
if (res == -ERR_OTHER) return res;
if (response.cid != challenge.cid) continue;
if (response.init.cmd != challenge.init.cmd) continue;
if (MSG_LEN(response) != sizeof(U2FHID_INIT_RESP)) continue;
if (memcmp(response.init.data, challenge.init.data, INIT_NONCE_SIZE))
continue;
device->cid =
(response.init.data[8] << 24) |
(response.init.data[9] << 16) |
(response.init.data[10] << 8) |
(response.init.data[11] << 0);
break;
}
return 0;
}
int U2Fob_send(struct U2Fob* device, uint8_t cmd,
const void* data, size_t size) {
U2FHID_FRAME frame;
int res;
size_t frameLen;
uint8_t seq = 0;
uint8_t* pData = (uint8_t*) data;
frame.cid = device->cid;
frame.init.cmd = TYPE_INIT | cmd;
frame.init.bcnth = (size >> 8) & 255;
frame.init.bcntl = (size & 255);
frameLen = min(size, sizeof(frame.init.data));
memset(frame.init.data, 0xEE, sizeof(frame.init.data));
memcpy(frame.init.data, pData, frameLen);
do {
res = U2Fob_sendHidFrame(device, &frame);
if (res != 0) return res;
size -= frameLen;
pData += frameLen;
frame.cont.seq = seq++;
frameLen = min(size, sizeof(frame.cont.data));
memset(frame.cont.data, 0xEE, sizeof(frame.cont.data));
memcpy(frame.cont.data, pData, frameLen);
} while (size);
return 0;
}
int U2Fob_recv(struct U2Fob* device, uint8_t* cmd,
void* data, size_t max,
float timeout) {
U2FHID_FRAME frame;
int res, result;
size_t totalLen, frameLen;
uint8_t seq = 0;
uint8_t* pData = (uint8_t*) data;
uint64_t timeTracker = 0;
U2Fob_deltaTime(&timeTracker);
do {
res = U2Fob_receiveHidFrame(device, &frame, timeout);
if (res != 0) return res;
timeout -= U2Fob_deltaTime(&timeTracker);
} while (frame.cid != device->cid || FRAME_TYPE(frame) != TYPE_INIT);
if (frame.init.cmd == U2FHID_ERROR) return -frame.init.data[0];
*cmd = frame.init.cmd;
totalLen = min(max, MSG_LEN(frame));
frameLen = min(sizeof(frame.init.data), totalLen);
result = totalLen;
memcpy(pData, frame.init.data, frameLen);
totalLen -= frameLen;
pData += frameLen;
while (totalLen) {
res = U2Fob_receiveHidFrame(device, &frame, timeout);
if (res != 0) return res;
timeout -= U2Fob_deltaTime(&timeTracker);
if (frame.cid != device->cid) continue;
if (FRAME_TYPE(frame) != TYPE_CONT) return -ERR_INVALID_SEQ;
if (FRAME_SEQ(frame) != seq++) return -ERR_INVALID_SEQ;
frameLen = min(sizeof(frame.cont.data), totalLen);
memcpy(pData, frame.cont.data, frameLen);
totalLen -= frameLen;
pData += frameLen;
}
return result;
}
int U2Fob_apdu(struct U2Fob* device,
uint8_t CLA, uint8_t INS, uint8_t P1, uint8_t P2,
const std::string& out,
std::string* in) {
size_t off = 0;
if (CLA == 0)
off = 5;
else
off = 3;
uint8_t buf[4096];
size_t bufSize = out.size() + off + 2 + 2;
uint8_t cmd = U2FHID_MSG;
// Construct outgoing message.
memset(buf, 0xEE, sizeof(buf));
buf[0] = CLA;
buf[1] = INS;
buf[2] = P1;
buf[3] = P2;
if (CLA == 0) {
buf[4] = 0; // extended length
buf[5] = (out.size() >> 8) & 255;
buf[6] = (out.size() & 255);
} else {
buf[4] = out.size() & 0xff;
}
memcpy(buf + off + 2, out.data(), out.size());
buf[off + 2 + out.size() + 0] = 0;
buf[off + 2 + out.size() + 1] = 0;
int res = U2Fob_send(device, cmd, buf, bufSize);
if (res != 0) return res;
memset(buf, 0xEE, sizeof(buf));
res = U2Fob_recv(device, &cmd, buf, sizeof(buf), 5.0);
if (res < 0) return res;
if (cmd != U2FHID_MSG) return -ERR_OTHER;
uint16_t sw12;
if (res < 2) return -ERR_OTHER;
sw12 = (buf[res - 2] << 8) | buf[res - 1];
res -= 2;
in->assign(reinterpret_cast<char*>(buf), res);
return sw12;
}
bool getCertificate(const U2F_REGISTER_RESP& rsp,
std::string* cert) {
size_t hkLen = rsp.keyHandleLen;
CHECK_GE(hkLen, 64);
CHECK_LT(hkLen, sizeof(rsp.keyHandleCertSig));
size_t certOff = hkLen;
size_t certLen = sizeof(rsp.keyHandleCertSig) - certOff;
const uint8_t* p = &rsp.keyHandleCertSig[certOff];
CHECK_GE(certLen, 4);
CHECK_EQ(p[0], 0x30);
CHECK_GE(p[1], 0x81);
CHECK_LE(p[1], 0x82);
size_t seqLen;
size_t headerLen;
if (p[1] == 0x81) {
seqLen = p[2];
headerLen = 3;
} else if (p[1] == 0x82) {
seqLen = p[2] * 256 + p[3];
headerLen = 4;
} else {
// FAIL
AbortOrNot();
}
CHECK_LE(seqLen, certLen - headerLen);
cert->assign(reinterpret_cast<const char*>(p), seqLen + headerLen);
return true;
}
bool getSignature(const U2F_REGISTER_RESP& rsp,
std::string* sig) {
std::string cert;
CHECK_NE(false, getCertificate(rsp, &cert));
size_t sigOff = rsp.keyHandleLen + cert.size();
CHECK_LE(sigOff, sizeof(rsp.keyHandleCertSig));
size_t sigLen = sizeof(rsp.keyHandleCertSig) - sigOff;
const uint8_t* p = &rsp.keyHandleCertSig[sigOff];
CHECK_GE(sigLen, 2);
CHECK_EQ(p[0], 0x30);
size_t seqLen = p[1];
CHECK_LE(seqLen, sigLen - 2);
sig->assign(reinterpret_cast<const char*>(p), seqLen + 2);
return true;
}
bool getSubjectPublicKey(const std::string& cert,
std::string* pk) {
CHECK_GE(cert.size(), P256_POINT_SIZE);
// Explicitly search for asn1 lead-in sequence of p256-ecdsa public key.
const char asn1[] = "3059301306072A8648CE3D020106082A8648CE3D030107034200";
std::string pkStart(a2b(asn1));
size_t off = cert.find(pkStart);
CHECK_NE(off, std::string::npos);
off += pkStart.size();
CHECK_LE(off, cert.size() - P256_POINT_SIZE);
pk->assign(cert, off, P256_POINT_SIZE);
return true;
}
bool getCertSignature(const std::string& cert,
std::string* sig) {
// Explicitly search asn1 lead-in sequence of p256-ecdsa signature.
const char asn1[] = "300A06082A8648CE3D04030203";
std::string sigStart(a2b(asn1));
size_t off = cert.find(sigStart);
CHECK_NE(off, std::string::npos);
off += sigStart.size();
CHECK_LE(off, cert.size() - 8);
size_t bitStringLen = cert[off] & 255;
CHECK_EQ(bitStringLen, cert.size() - off - 1);
CHECK_EQ(cert[off + 1], 0);
sig->assign(cert, off + 2, cert.size() - off - 2);
return true;
}
bool verifyCertificate(const std::string& pk,
const std::string& cert) {
CHECK_EQ(true, false); // not yet implemented
}