This repository was archived by the owner on Oct 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathu2fmisc.c
More file actions
414 lines (354 loc) · 9.11 KB
/
Copy pathu2fmisc.c
File metadata and controls
414 lines (354 loc) · 9.11 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
/*
Copyright (C) 2013-2015 Yubico AB
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include "internal.h"
#include <json.h>
#include "sha256.h"
#define RESPHEAD_SIZE 7
#define HID_TIMEOUT 2
#define HID_MAX_TIMEOUT 4096
#ifdef HAVE_JSON_OBJECT_OBJECT_GET_EX
#define u2fh_json_object_object_get(obj, key, value) json_object_object_get_ex(obj, key, &value)
#else
typedef int json_bool;
#define u2fh_json_object_object_get(obj, key, value) (value = json_object_object_get(obj, key)) == NULL ? (json_bool)FALSE : (json_bool)TRUE
#endif
static void
dumpHex (unsigned char *data, int offs, int len)
{
int i;
for (i = offs; i < len; i++)
{
fprintf (stderr, "%02x", data[i] & 0xFF);
}
fprintf (stderr, "\n");
}
int
prepare_browserdata (const char *challenge, const char *origin,
const char *typstr, char *out, size_t * outlen)
{
int rc = U2FH_JSON_ERROR;
struct json_object *jo = NULL;
struct json_object *chal = NULL, *orig = NULL, *typ = NULL;
const char *buf;
size_t len;
chal = json_object_new_string (challenge);
if (chal == NULL)
goto done;
orig = json_object_new_string (origin);
if (orig == NULL)
goto done;
typ = json_object_new_string (typstr);
if (typ == NULL)
goto done;
jo = json_object_new_object ();
if (jo == NULL)
goto done;
json_object_object_add (jo, "challenge", json_object_get (chal));
json_object_object_add (jo, "origin", json_object_get (orig));
json_object_object_add (jo, "typ", json_object_get (typ));
if (debug)
{
fprintf (stderr, "client data: %s\n", json_object_to_json_string (jo));
}
buf = json_object_to_json_string (jo);
len = strlen (buf);
if (len >= *outlen)
{
rc = U2FH_MEMORY_ERROR;
goto done;
}
strcpy (out, buf);
*outlen = len;
rc = U2FH_OK;
done:
json_object_put (jo);
json_object_put (chal);
json_object_put (orig);
json_object_put (typ);
return rc;
}
int
prepare_origin (const char *jsonstr, unsigned char *p)
{
const char *app_id;
struct json_object *jo;
struct json_object *k;
jo = json_tokener_parse (jsonstr);
if (jo == NULL)
return U2FH_MEMORY_ERROR;
if (debug)
fprintf (stderr, "JSON: %s\n", json_object_to_json_string (jo));
if (u2fh_json_object_object_get (jo, "appId", k) == FALSE)
return U2FH_JSON_ERROR;
app_id = json_object_get_string (k);
if (app_id == NULL)
return U2FH_JSON_ERROR;
if (debug)
fprintf (stderr, "JSON app_id %s\n", app_id);
sha256_buffer (app_id ? app_id : "", app_id ? strlen (app_id) : 0, p);
json_object_put (jo);
return U2FH_OK;
}
/**
* u2fh_sendrecv:
* @devs: device handle, from u2fh_devs_init().
* @index: index of device
* @cmd: command to run
* @send: buffer of data to send
* @sendlen: length of data to send
* @recv: buffer of data to receive
* @recvlen: length of data to receive
*
* Send a command with data to the device at @index.
*
* Returns: %U2FH_OK on success, another #u2fh_rc error code
* otherwise.
*/
u2fh_rc
u2fh_sendrecv (u2fh_devs * devs, unsigned index, uint8_t cmd,
const unsigned char *send, uint16_t sendlen,
unsigned char *recv, size_t * recvlen)
{
int datasent = 0;
int sequence = 0;
struct u2fdevice *dev = get_device (devs, index);
if (!dev)
{
return U2FH_NO_U2F_DEVICE;
}
while (sendlen > datasent)
{
U2FHID_FRAME frame = { 0 };
{
int len = sendlen - datasent;
unsigned int maxlen;
unsigned char *data;
frame.cid = dev->cid;
if (datasent == 0)
{
frame.init.cmd = cmd;
frame.init.bcnth = (sendlen >> 8) & 0xff;
frame.init.bcntl = sendlen & 0xff;
data = frame.init.data;
maxlen = sizeof (frame.init.data);
}
else
{
frame.cont.seq = sequence++;
data = frame.cont.data;
maxlen = sizeof (frame.cont.data);
}
if (len > maxlen)
{
len = maxlen;
}
memcpy (data, send + datasent, len);
datasent += len;
}
{
unsigned char data[sizeof (U2FHID_FRAME) + 1];
int len;
/* FIXME: add report as first byte, is report 0 correct? */
data[0] = 0;
memcpy (data + 1, &frame, sizeof (U2FHID_FRAME));
if (debug)
{
fprintf (stderr, "USB send: ");
dumpHex (data, 0, sizeof (U2FHID_FRAME));
}
len = hid_write (dev->devh, data, sizeof (U2FHID_FRAME) + 1);
if (debug)
fprintf (stderr, "USB write returned %d\n", len);
if (len < 0)
return U2FH_TRANSPORT_ERROR;
if (sizeof (U2FHID_FRAME) + 1 != len)
return U2FH_TRANSPORT_ERROR;
}
}
{
U2FHID_FRAME frame;
unsigned char data[HID_RPT_SIZE];
int len = HID_RPT_SIZE;
unsigned int maxlen = *recvlen;
int recvddata = 0;
unsigned short datalen;
int timeout = HID_TIMEOUT;
int rc = 0;
do
{
timeout = HID_TIMEOUT;
rc = 0;
while (rc == 0)
{
if (debug)
{
fprintf (stderr, "now trying with timeout %d\n", timeout);
}
rc = hid_read_timeout (dev->devh, data, len, timeout);
timeout *= 2;
if (timeout > HID_MAX_TIMEOUT)
{
rc = -2;
break;
}
}
if (debug)
{
fprintf (stderr, "USB read rc read %d\n", len);
if (rc > 0)
{
fprintf (stderr, "USB recv: ");
dumpHex (data, 0, rc);
}
}
if (rc < 0)
{
return U2FH_TRANSPORT_ERROR;
}
memcpy (&frame, data, HID_RPT_SIZE);
}
while (frame.cid == dev->cid && frame.init.cmd == CTAPHID_KEEPALIVE);
if (frame.cid != dev->cid || frame.init.cmd != cmd)
{
return U2FH_TRANSPORT_ERROR;
}
datalen = frame.init.bcnth << 8 | frame.init.bcntl;
if (datalen + datalen % HID_RPT_SIZE > maxlen)
{
return U2FH_TRANSPORT_ERROR;
}
memcpy (recv, frame.init.data, sizeof (frame.init.data));
recvddata = sizeof (frame.init.data);
sequence = 0;
while (datalen > recvddata)
{
timeout = HID_TIMEOUT;
rc = 0;
while (rc == 0)
{
if (debug)
{
fprintf (stderr, "now trying with timeout %d\n", timeout);
}
rc = hid_read_timeout (dev->devh, data, len, timeout);
timeout *= 2;
if (timeout > HID_MAX_TIMEOUT)
{
rc = -2;
break;
}
}
if (debug)
{
fprintf (stderr, "USB read rc read %d\n", len);
if (rc > 0)
{
fprintf (stderr, "USB recv: ");
dumpHex (data, 0, rc);
}
}
if (rc < 0)
{
return U2FH_TRANSPORT_ERROR;
}
memcpy (&frame, data, HID_RPT_SIZE);
if (frame.cid != dev->cid || frame.cont.seq != sequence++)
{
fprintf (stderr, "bar: %d %d %d %d\n", frame.cid, dev->cid,
frame.cont.seq, sequence);
return U2FH_TRANSPORT_ERROR;
}
if (recvddata + sizeof (frame.cont.data) > maxlen)
{
return U2FH_TRANSPORT_ERROR;
}
memcpy (recv + recvddata, frame.cont.data, sizeof (frame.cont.data));
recvddata += sizeof (frame.cont.data);
}
*recvlen = datalen;
}
return U2FH_OK;
}
u2fh_rc
send_apdu (u2fh_devs * devs, int index, int cmd, const unsigned char *d,
size_t dlen, int p1, unsigned char *out, size_t * outlen)
{
unsigned char data[2048] = { 0 };
int rc;
if (dlen > MAXDATASIZE)
return U2FH_MEMORY_ERROR;
data[1] = cmd;
data[2] = p1;
data[5] = (dlen >> 8) & 0xff;
data[6] = dlen & 0xff;
memcpy (data + RESPHEAD_SIZE, d, dlen);
memset (data + RESPHEAD_SIZE + dlen, 0, 2);
rc =
u2fh_sendrecv (devs, index, U2FHID_MSG, data, RESPHEAD_SIZE + dlen + 2,
out, outlen);
if (rc != U2FH_OK)
{
if (debug)
fprintf (stderr, "USB rc %d\n", rc);
return rc;
}
if (*outlen < 2)
{
if (debug)
fprintf (stderr, "USB read too short\n");
return U2FH_TRANSPORT_ERROR;
}
if (*outlen > MAXDATASIZE)
{
if (debug)
fprintf (stderr, "USB too large response?\n");
return U2FH_MEMORY_ERROR;
}
/* FIXME: Improve APDU error handling? */
if (debug)
{
fprintf (stderr, "USB data (len %zu): ", *outlen);
dumpHex (out, 0, *outlen);
}
return U2FH_OK;
}
int
get_fixed_json_data (const char *jsonstr, const char *key, char *p,
size_t * len)
{
struct json_object *jo;
struct json_object *k;
const char *urlb64;
jo = json_tokener_parse (jsonstr);
if (jo == NULL)
return U2FH_JSON_ERROR;
if (debug)
fprintf (stderr, "JSON: %s\n", json_object_to_json_string (jo));
if (u2fh_json_object_object_get (jo, key, k) == FALSE)
return U2FH_JSON_ERROR;
urlb64 = json_object_get_string (k);
if (urlb64 == NULL)
return U2FH_JSON_ERROR;
if (debug)
fprintf (stderr, "JSON %s URL-B64: %s\n", key, urlb64);
if (strlen (urlb64) >= *len)
{
return U2FH_JSON_ERROR;
}
*len = strlen (urlb64);
strcpy (p, urlb64);
json_object_put (jo);
return U2FH_OK;
}