Coverage Report

Created: 2021-10-21 13:35

/libfido2/src/u2f.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2018-2021 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 <openssl/sha.h>
8
#include <openssl/x509.h>
9
10
#ifdef HAVE_UNISTD_H
11
#include <unistd.h>
12
#endif
13
#include <errno.h>
14
15
#include "fido.h"
16
#include "fido/es256.h"
17
18
927
#define U2F_PACE_MS (100)
19
20
#if defined(_MSC_VER)
21
static int
22
usleep(unsigned int usec)
23
{
24
        Sleep(usec / 1000);
25
26
        return (0);
27
}
28
#endif
29
30
static int
31
delay_ms(unsigned int ms, int *ms_remain)
32
927
{
33
927
        if (*ms_remain > -1 && (unsigned int)*ms_remain < ms)
34
628
                ms = (unsigned int)*ms_remain;
35
36
927
        if (ms > UINT_MAX / 1000) {
37
0
                fido_log_debug("%s: ms=%u", __func__, ms);
38
0
                return (-1);
39
0
        }
40
41
927
        if (usleep(ms * 1000) < 0) {
42
3
                fido_log_error(errno, "%s: usleep", __func__);
43
3
                return (-1);
44
3
        }
45
46
924
        if (*ms_remain > -1)
47
924
                *ms_remain -= (int)ms;
48
49
924
        return (0);
50
924
}
51
52
static int
53
sig_get(fido_blob_t *sig, const unsigned char **buf, size_t *len)
54
214
{
55
214
        sig->len = *len; /* consume the whole buffer */
56
214
        if ((sig->ptr = calloc(1, sig->len)) == NULL ||
57
214
            fido_buf_read(buf, len, sig->ptr, sig->len) < 0) {
58
2
                fido_log_debug("%s: fido_buf_read", __func__);
59
2
                fido_blob_reset(sig);
60
2
                return (-1);
61
2
        }
62
63
212
        return (0);
64
212
}
65
66
static int
67
x5c_get(fido_blob_t *x5c, const unsigned char **buf, size_t *len)
68
120
{
69
120
        X509    *cert = NULL;
70
120
        int      ok = -1;
71
72
120
        if (*len > LONG_MAX) {
73
0
                fido_log_debug("%s: invalid len %zu", __func__, *len);
74
0
                goto fail;
75
0
        }
76
77
        /* find out the certificate's length */
78
120
        const unsigned char *end = *buf;
79
120
        if ((cert = d2i_X509(NULL, &end, (long)*len)) == NULL || end <= *buf ||
80
120
            (x5c->len = (size_t)(end - *buf)) >= *len) {
81
24
                fido_log_debug("%s: d2i_X509", __func__);
82
24
                goto fail;
83
24
        }
84
85
        /* read accordingly */
86
96
        if ((x5c->ptr = calloc(1, x5c->len)) == NULL ||
87
96
            fido_buf_read(buf, len, x5c->ptr, x5c->len) < 0) {
88
1
                fido_log_debug("%s: fido_buf_read", __func__);
89
1
                goto fail;
90
1
        }
91
92
95
        ok = 0;
93
120
fail:
94
120
        if (cert != NULL)
95
120
                X509_free(cert);
96
97
120
        if (ok < 0)
98
25
                fido_blob_reset(x5c);
99
100
120
        return (ok);
101
95
}
102
103
static int
104
authdata_fake(const char *rp_id, uint8_t flags, uint32_t sigcount,
105
    fido_blob_t *fake_cbor_ad)
106
118
{
107
118
        fido_authdata_t  ad;
108
118
        cbor_item_t     *item = NULL;
109
118
        size_t           alloc_len;
110
111
118
        memset(&ad, 0, sizeof(ad));
112
113
118
        if (SHA256((const void *)rp_id, strlen(rp_id),
114
118
            ad.rp_id_hash) != ad.rp_id_hash) {
115
1
                fido_log_debug("%s: sha256", __func__);
116
1
                return (-1);
117
1
        }
118
119
117
        ad.flags = flags; /* XXX translate? */
120
117
        ad.sigcount = sigcount;
121
122
117
        if ((item = cbor_build_bytestring((const unsigned char *)&ad,
123
117
            sizeof(ad))) == NULL) {
124
1
                fido_log_debug("%s: cbor_build_bytestring", __func__);
125
1
                return (-1);
126
1
        }
127
128
116
        if (fake_cbor_ad->ptr != NULL ||
129
116
            (fake_cbor_ad->len = cbor_serialize_alloc(item, &fake_cbor_ad->ptr,
130
116
            &alloc_len)) == 0) {
131
1
                fido_log_debug("%s: cbor_serialize_alloc", __func__);
132
1
                cbor_decref(&item);
133
1
                return (-1);
134
1
        }
135
136
115
        cbor_decref(&item);
137
138
115
        return (0);
139
115
}
140
141
/* TODO: use u2f_get_touch_begin & u2f_get_touch_status instead */
142
static int
143
send_dummy_register(fido_dev_t *dev, int *ms)
144
19
{
145
19
        iso7816_apdu_t  *apdu = NULL;
146
19
        unsigned char    challenge[SHA256_DIGEST_LENGTH];
147
19
        unsigned char    application[SHA256_DIGEST_LENGTH];
148
19
        unsigned char    reply[FIDO_MAXMSG];
149
19
        int              r;
150
151
        /* dummy challenge & application */
152
19
        memset(&challenge, 0xff, sizeof(challenge));
153
19
        memset(&application, 0xff, sizeof(application));
154
155
19
        if ((apdu = iso7816_new(0, U2F_CMD_REGISTER, 0, 2 *
156
19
            SHA256_DIGEST_LENGTH)) == NULL ||
157
19
            iso7816_add(apdu, &challenge, sizeof(challenge)) < 0 ||
158
19
            iso7816_add(apdu, &application, sizeof(application)) < 0) {
159
1
                fido_log_debug("%s: iso7816", __func__);
160
1
                r = FIDO_ERR_INTERNAL;
161
1
                goto fail;
162
1
        }
163
164
59
        do {
165
59
                if (fido_tx(dev, CTAP_CMD_MSG, iso7816_ptr(apdu),
166
59
                    iso7816_len(apdu), ms) < 0) {
167
1
                        fido_log_debug("%s: fido_tx", __func__);
168
1
                        r = FIDO_ERR_TX;
169
1
                        goto fail;
170
1
                }
171
58
                if (fido_rx(dev, CTAP_CMD_MSG, &reply, sizeof(reply), ms) < 2) {
172
8
                        fido_log_debug("%s: fido_rx", __func__);
173
8
                        r = FIDO_ERR_RX;
174
8
                        goto fail;
175
8
                }
176
50
                if (delay_ms(U2F_PACE_MS, ms) != 0) {
177
1
                        fido_log_debug("%s: delay_ms", __func__);
178
1
                        r = FIDO_ERR_RX;
179
1
                        goto fail;
180
1
                }
181
49
        } while (((reply[0] << 8) | reply[1]) == SW_CONDITIONS_NOT_SATISFIED);
182
183
18
        r = FIDO_OK;
184
19
fail:
185
19
        iso7816_free(&apdu);
186
187
19
        return (r);
188
8
}
189
190
static int
191
key_lookup(fido_dev_t *dev, const char *rp_id, const fido_blob_t *key_id,
192
    int *found, int *ms)
193
876
{
194
876
        iso7816_apdu_t  *apdu = NULL;
195
876
        unsigned char    challenge[SHA256_DIGEST_LENGTH];
196
876
        unsigned char    rp_id_hash[SHA256_DIGEST_LENGTH];
197
876
        unsigned char    reply[FIDO_MAXMSG];
198
876
        uint8_t          key_id_len;
199
876
        int              r;
200
201
876
        if (key_id->len > UINT8_MAX || rp_id == NULL) {
202
10
                fido_log_debug("%s: key_id->len=%zu, rp_id=%p", __func__,
203
10
                    key_id->len, (const void *)rp_id);
204
10
                r = FIDO_ERR_INVALID_ARGUMENT;
205
10
                goto fail;
206
10
        }
207
208
866
        memset(&challenge, 0xff, sizeof(challenge));
209
866
        memset(&rp_id_hash, 0, sizeof(rp_id_hash));
210
211
866
        if (SHA256((const void *)rp_id, strlen(rp_id),
212
866
            rp_id_hash) != rp_id_hash) {
213
2
                fido_log_debug("%s: sha256", __func__);
214
2
                r = FIDO_ERR_INTERNAL;
215
2
                goto fail;
216
2
        }
217
218
864
        key_id_len = (uint8_t)key_id->len;
219
220
864
        if ((apdu = iso7816_new(0, U2F_CMD_AUTH, U2F_AUTH_CHECK, (uint16_t)(2 *
221
864
            SHA256_DIGEST_LENGTH + sizeof(key_id_len) + key_id_len))) == NULL ||
222
864
            iso7816_add(apdu, &challenge, sizeof(challenge)) < 0 ||
223
864
            iso7816_add(apdu, &rp_id_hash, sizeof(rp_id_hash)) < 0 ||
224
864
            iso7816_add(apdu, &key_id_len, sizeof(key_id_len)) < 0 ||
225
864
            iso7816_add(apdu, key_id->ptr, key_id_len) < 0) {
226
4
                fido_log_debug("%s: iso7816", __func__);
227
4
                r = FIDO_ERR_INTERNAL;
228
4
                goto fail;
229
4
        }
230
231
860
        if (fido_tx(dev, CTAP_CMD_MSG, iso7816_ptr(apdu),
232
860
            iso7816_len(apdu), ms) < 0) {
233
84
                fido_log_debug("%s: fido_tx", __func__);
234
84
                r = FIDO_ERR_TX;
235
84
                goto fail;
236
84
        }
237
776
        if (fido_rx(dev, CTAP_CMD_MSG, &reply, sizeof(reply), ms) != 2) {
238
393
                fido_log_debug("%s: fido_rx", __func__);
239
393
                r = FIDO_ERR_RX;
240
393
                goto fail;
241
393
        }
242
243
383
        switch ((reply[0] << 8) | reply[1]) {
244
265
        case SW_CONDITIONS_NOT_SATISFIED:
245
265
                *found = 1; /* key exists */
246
265
                break;
247
11
        case SW_WRONG_DATA:
248
11
                *found = 0; /* key does not exist */
249
11
                break;
250
107
        default:
251
                /* unexpected sw */
252
107
                r = FIDO_ERR_INTERNAL;
253
107
                goto fail;
254
276
        }
255
256
276
        r = FIDO_OK;
257
876
fail:
258
876
        iso7816_free(&apdu);
259
260
876
        return (r);
261
276
}
262
263
static int
264
parse_auth_reply(fido_blob_t *sig, fido_blob_t *ad, const char *rp_id,
265
    const unsigned char *reply, size_t len)
266
147
{
267
147
        uint8_t         flags;
268
147
        uint32_t        sigcount;
269
270
147
        if (len < 2 || ((reply[len - 2] << 8) | reply[len - 1]) != SW_NO_ERROR) {
271
27
                fido_log_debug("%s: unexpected sw", __func__);
272
27
                return (FIDO_ERR_RX);
273
27
        }
274
275
120
        len -= 2;
276
277
120
        if (fido_buf_read(&reply, &len, &flags, sizeof(flags)) < 0 ||
278
120
            fido_buf_read(&reply, &len, &sigcount, sizeof(sigcount)) < 0) {
279
1
                fido_log_debug("%s: fido_buf_read", __func__);
280
1
                return (FIDO_ERR_RX);
281
1
        }
282
283
119
        if (sig_get(sig, &reply, &len) < 0) {
284
1
                fido_log_debug("%s: sig_get", __func__);
285
1
                return (FIDO_ERR_RX);
286
1
        }
287
288
118
        if (authdata_fake(rp_id, flags, sigcount, ad) < 0) {
289
3
                fido_log_debug("%s; authdata_fake", __func__);
290
3
                return (FIDO_ERR_RX);
291
3
        }
292
293
115
        return (FIDO_OK);
294
115
}
295
296
static int
297
do_auth(fido_dev_t *dev, const fido_blob_t *cdh, const char *rp_id,
298
    const fido_blob_t *key_id, fido_blob_t *sig, fido_blob_t *ad, int *ms)
299
174
{
300
174
        iso7816_apdu_t  *apdu = NULL;
301
174
        unsigned char    rp_id_hash[SHA256_DIGEST_LENGTH];
302
174
        unsigned char    reply[FIDO_MAXMSG];
303
174
        int              reply_len;
304
174
        uint8_t          key_id_len;
305
174
        int              r;
306
307
174
#ifdef FIDO_FUZZ
308
174
        *ms = 0; /* XXX */
309
174
#endif
310
311
174
        if (cdh->len != SHA256_DIGEST_LENGTH || key_id->len > UINT8_MAX ||
312
174
            rp_id == NULL) {
313
12
                r = FIDO_ERR_INVALID_ARGUMENT;
314
12
                goto fail;
315
12
        }
316
317
162
        memset(&rp_id_hash, 0, sizeof(rp_id_hash));
318
319
162
        if (SHA256((const void *)rp_id, strlen(rp_id),
320
162
            rp_id_hash) != rp_id_hash) {
321
1
                fido_log_debug("%s: sha256", __func__);
322
1
                r = FIDO_ERR_INTERNAL;
323
1
                goto fail;
324
1
        }
325
326
161
        key_id_len = (uint8_t)key_id->len;
327
328
161
        if ((apdu = iso7816_new(0, U2F_CMD_AUTH, U2F_AUTH_SIGN, (uint16_t)(2 *
329
161
            SHA256_DIGEST_LENGTH + sizeof(key_id_len) + key_id_len))) == NULL ||
330
161
            iso7816_add(apdu, cdh->ptr, cdh->len) < 0 ||
331
161
            iso7816_add(apdu, &rp_id_hash, sizeof(rp_id_hash)) < 0 ||
332
161
            iso7816_add(apdu, &key_id_len, sizeof(key_id_len)) < 0 ||
333
161
            iso7816_add(apdu, key_id->ptr, key_id_len) < 0) {
334
1
                fido_log_debug("%s: iso7816", __func__);
335
1
                r = FIDO_ERR_INTERNAL;
336
1
                goto fail;
337
1
        }
338
339
327
        do {
340
327
                if (fido_tx(dev, CTAP_CMD_MSG, iso7816_ptr(apdu),
341
327
                    iso7816_len(apdu), ms) < 0) {
342
2
                        fido_log_debug("%s: fido_tx", __func__);
343
2
                        r = FIDO_ERR_TX;
344
2
                        goto fail;
345
2
                }
346
325
                if ((reply_len = fido_rx(dev, CTAP_CMD_MSG, &reply,
347
325
                    sizeof(reply), ms)) < 2) {
348
10
                        fido_log_debug("%s: fido_rx", __func__);
349
10
                        r = FIDO_ERR_RX;
350
10
                        goto fail;
351
10
                }
352
315
                if (delay_ms(U2F_PACE_MS, ms) != 0) {
353
1
                        fido_log_debug("%s: delay_ms", __func__);
354
1
                        r = FIDO_ERR_RX;
355
1
                        goto fail;
356
1
                }
357
314
        } while (((reply[0] << 8) | reply[1]) == SW_CONDITIONS_NOT_SATISFIED);
358
359
160
        if ((r = parse_auth_reply(sig, ad, rp_id, reply,
360
147
            (size_t)reply_len)) != FIDO_OK) {
361
32
                fido_log_debug("%s: parse_auth_reply", __func__);
362
32
                goto fail;
363
32
        }
364
365
174
fail:
366
174
        iso7816_free(&apdu);
367
368
174
        return (r);
369
147
}
370
371
static int
372
cbor_blob_from_ec_point(const uint8_t *ec_point, size_t ec_point_len,
373
    fido_blob_t *cbor_blob)
374
81
{
375
81
        es256_pk_t      *pk = NULL;
376
81
        cbor_item_t     *pk_cbor = NULL;
377
81
        size_t           alloc_len;
378
81
        int              ok = -1;
379
380
        /* only handle uncompressed points */
381
81
        if (ec_point_len != 65 || ec_point[0] != 0x04) {
382
8
                fido_log_debug("%s: unexpected format", __func__);
383
8
                goto fail;
384
8
        }
385
386
73
        if ((pk = es256_pk_new()) == NULL ||
387
73
            es256_pk_set_x(pk, &ec_point[1]) < 0 ||
388
73
            es256_pk_set_y(pk, &ec_point[33]) < 0) {
389
1
                fido_log_debug("%s: es256_pk_set", __func__);
390
1
                goto fail;
391
1
        }
392
393
72
        if ((pk_cbor = es256_pk_encode(pk, 0)) == NULL) {
394
2
                fido_log_debug("%s: es256_pk_encode", __func__);
395
2
                goto fail;
396
2
        }
397
398
70
        if ((cbor_blob->len = cbor_serialize_alloc(pk_cbor, &cbor_blob->ptr,
399
70
            &alloc_len)) != 77) {
400
1
                fido_log_debug("%s: cbor_serialize_alloc", __func__);
401
1
                goto fail;
402
1
        }
403
404
69
        ok = 0;
405
81
fail:
406
81
        es256_pk_free(&pk);
407
408
81
        if (pk_cbor)
409
70
                cbor_decref(&pk_cbor);
410
411
81
        return (ok);
412
69
}
413
414
static int
415
encode_cred_attstmt(int cose_alg, const fido_blob_t *x5c,
416
    const fido_blob_t *sig, fido_blob_t *out)
417
94
{
418
94
        cbor_item_t             *item = NULL;
419
94
        cbor_item_t             *x5c_cbor = NULL;
420
94
        const uint8_t            alg_cbor = (uint8_t)(-cose_alg - 1);
421
94
        struct cbor_pair         kv[3];
422
94
        size_t                   alloc_len;
423
94
        int                      ok = -1;
424
425
94
        memset(&kv, 0, sizeof(kv));
426
94
        memset(out, 0, sizeof(*out));
427
428
94
        if ((item = cbor_new_definite_map(3)) == NULL) {
429
1
                fido_log_debug("%s: cbor_new_definite_map", __func__);
430
1
                goto fail;
431
1
        }
432
433
93
        if ((kv[0].key = cbor_build_string("alg")) == NULL ||
434
93
            (kv[0].value = cbor_build_negint8(alg_cbor)) == NULL ||
435
93
            !cbor_map_add(item, kv[0])) {
436
3
                fido_log_debug("%s: alg", __func__);
437
3
                goto fail;
438
3
        }
439
440
90
        if ((kv[1].key = cbor_build_string("sig")) == NULL ||
441
90
            (kv[1].value = fido_blob_encode(sig)) == NULL ||
442
90
            !cbor_map_add(item, kv[1])) {
443
3
                fido_log_debug("%s: sig", __func__);
444
3
                goto fail;
445
3
        }
446
447
87
        if ((kv[2].key = cbor_build_string("x5c")) == NULL ||
448
87
            (kv[2].value = cbor_new_definite_array(1)) == NULL ||
449
87
            (x5c_cbor = fido_blob_encode(x5c)) == NULL ||
450
87
            !cbor_array_push(kv[2].value, x5c_cbor) ||
451
87
            !cbor_map_add(item, kv[2])) {
452
5
                fido_log_debug("%s: x5c", __func__);
453
5
                goto fail;
454
5
        }
455
456
82
        if ((out->len = cbor_serialize_alloc(item, &out->ptr,
457
82
            &alloc_len)) == 0) {
458
1
                fido_log_debug("%s: cbor_serialize_alloc", __func__);
459
1
                goto fail;
460
1
        }
461
462
81
        ok = 0;
463
94
fail:
464
94
        if (item != NULL)
465
94
                cbor_decref(&item);
466
94
        if (x5c_cbor != NULL)
467
94
                cbor_decref(&x5c_cbor);
468
469
376
        for (size_t i = 0; i < nitems(kv); i++) {
470
282
                if (kv[i].key)
471
267
                        cbor_decref(&kv[i].key);
472
282
                if (kv[i].value)
473
264
                        cbor_decref(&kv[i].value);
474
282
        }
475
476
94
        return (ok);
477
81
}
478
479
static int
480
encode_cred_authdata(const char *rp_id, const uint8_t *kh, uint8_t kh_len,
481
    const uint8_t *pubkey, size_t pubkey_len, fido_blob_t *out)
482
81
{
483
81
        fido_authdata_t          authdata;
484
81
        fido_attcred_raw_t       attcred_raw;
485
81
        fido_blob_t              pk_blob;
486
81
        fido_blob_t              authdata_blob;
487
81
        cbor_item_t             *authdata_cbor = NULL;
488
81
        unsigned char           *ptr;
489
81
        size_t                   len;
490
81
        size_t                   alloc_len;
491
81
        int                      ok = -1;
492
493
81
        memset(&pk_blob, 0, sizeof(pk_blob));
494
81
        memset(&authdata, 0, sizeof(authdata));
495
81
        memset(&authdata_blob, 0, sizeof(authdata_blob));
496
81
        memset(out, 0, sizeof(*out));
497
498
81
        if (rp_id == NULL) {
499
0
                fido_log_debug("%s: NULL rp_id", __func__);
500
0
                goto fail;
501
0
        }
502
503
81
        if (cbor_blob_from_ec_point(pubkey, pubkey_len, &pk_blob) < 0) {
504
12
                fido_log_debug("%s: cbor_blob_from_ec_point", __func__);
505
12
                goto fail;
506
12
        }
507
508
69
        if (SHA256((const void *)rp_id, strlen(rp_id),
509
69
            authdata.rp_id_hash) != authdata.rp_id_hash) {
510
1
                fido_log_debug("%s: sha256", __func__);
511
1
                goto fail;
512
1
        }
513
514
68
        authdata.flags = (CTAP_AUTHDATA_ATT_CRED | CTAP_AUTHDATA_USER_PRESENT);
515
68
        authdata.sigcount = 0;
516
517
68
        memset(&attcred_raw.aaguid, 0, sizeof(attcred_raw.aaguid));
518
68
        attcred_raw.id_len = htobe16(kh_len);
519
520
68
        len = authdata_blob.len = sizeof(authdata) + sizeof(attcred_raw) +
521
68
            kh_len + pk_blob.len;
522
68
        ptr = authdata_blob.ptr = calloc(1, authdata_blob.len);
523
524
68
        fido_log_debug("%s: ptr=%p, len=%zu", __func__, (void *)ptr, len);
525
526
68
        if (authdata_blob.ptr == NULL)
527
68
                goto fail;
528
529
67
        if (fido_buf_write(&ptr, &len, &authdata, sizeof(authdata)) < 0 ||
530
67
            fido_buf_write(&ptr, &len, &attcred_raw, sizeof(attcred_raw)) < 0 ||
531
67
            fido_buf_write(&ptr, &len, kh, kh_len) < 0 ||
532
67
            fido_buf_write(&ptr, &len, pk_blob.ptr, pk_blob.len) < 0) {
533
0
                fido_log_debug("%s: fido_buf_write", __func__);
534
0
                goto fail;
535
0
        }
536
537
67
        if ((authdata_cbor = fido_blob_encode(&authdata_blob)) == NULL) {
538
1
                fido_log_debug("%s: fido_blob_encode", __func__);
539
1
                goto fail;
540
1
        }
541
542
66
        if ((out->len = cbor_serialize_alloc(authdata_cbor, &out->ptr,
543
66
            &alloc_len)) == 0) {
544
1
                fido_log_debug("%s: cbor_serialize_alloc", __func__);
545
1
                goto fail;
546
1
        }
547
548
65
        ok = 0;
549
81
fail:
550
81
        if (authdata_cbor)
551
66
                cbor_decref(&authdata_cbor);
552
553
81
        fido_blob_reset(&pk_blob);
554
81
        fido_blob_reset(&authdata_blob);
555
556
81
        return (ok);
557
65
}
558
559
static int
560
parse_register_reply(fido_cred_t *cred, const unsigned char *reply, size_t len)
561
166
{
562
166
        fido_blob_t      x5c;
563
166
        fido_blob_t      sig;
564
166
        fido_blob_t      ad;
565
166
        fido_blob_t      stmt;
566
166
        uint8_t          dummy;
567
166
        uint8_t          pubkey[65];
568
166
        uint8_t          kh_len = 0;
569
166
        uint8_t         *kh = NULL;
570
166
        int              r;
571
572
166
        memset(&x5c, 0, sizeof(x5c));
573
166
        memset(&sig, 0, sizeof(sig));
574
166
        memset(&ad, 0, sizeof(ad));
575
166
        memset(&stmt, 0, sizeof(stmt));
576
166
        r = FIDO_ERR_RX;
577
578
        /* status word */
579
166
        if (len < 2 || ((reply[len - 2] << 8) | reply[len - 1]) != SW_NO_ERROR) {
580
36
                fido_log_debug("%s: unexpected sw", __func__);
581
36
                goto fail;
582
36
        }
583
584
130
        len -= 2;
585
586
        /* reserved byte */
587
130
        if (fido_buf_read(&reply, &len, &dummy, sizeof(dummy)) < 0 ||
588
130
            dummy != 0x05) {
589
9
                fido_log_debug("%s: reserved byte", __func__);
590
9
                goto fail;
591
9
        }
592
593
        /* pubkey + key handle */
594
121
        if (fido_buf_read(&reply, &len, &pubkey, sizeof(pubkey)) < 0 ||
595
121
            fido_buf_read(&reply, &len, &kh_len, sizeof(kh_len)) < 0 ||
596
121
            (kh = calloc(1, kh_len)) == NULL ||
597
121
            fido_buf_read(&reply, &len, kh, kh_len) < 0) {
598
1
                fido_log_debug("%s: fido_buf_read", __func__);
599
1
                goto fail;
600
1
        }
601
602
        /* x5c + sig */
603
120
        if (x5c_get(&x5c, &reply, &len) < 0 ||
604
120
            sig_get(&sig, &reply, &len) < 0) {
605
26
                fido_log_debug("%s: x5c || sig", __func__);
606
26
                goto fail;
607
26
        }
608
609
        /* attstmt */
610
94
        if (encode_cred_attstmt(COSE_ES256, &x5c, &sig, &stmt) < 0) {
611
13
                fido_log_debug("%s: encode_cred_attstmt", __func__);
612
13
                goto fail;
613
13
        }
614
615
        /* authdata */
616
81
        if (encode_cred_authdata(cred->rp.id, kh, kh_len, pubkey,
617
81
            sizeof(pubkey), &ad) < 0) {
618
16
                fido_log_debug("%s: encode_cred_authdata", __func__);
619
16
                goto fail;
620
16
        }
621
622
65
        if (fido_cred_set_fmt(cred, "fido-u2f") != FIDO_OK ||
623
65
            fido_cred_set_authdata(cred, ad.ptr, ad.len) != FIDO_OK ||
624
65
            fido_cred_set_attstmt(cred, stmt.ptr, stmt.len) != FIDO_OK) {
625
10
                fido_log_debug("%s: fido_cred_set", __func__);
626
10
                r = FIDO_ERR_INTERNAL;
627
10
                goto fail;
628
10
        }
629
630
55
        r = FIDO_OK;
631
166
fail:
632
166
        freezero(kh, kh_len);
633
166
        fido_blob_reset(&x5c);
634
166
        fido_blob_reset(&sig);
635
166
        fido_blob_reset(&ad);
636
166
        fido_blob_reset(&stmt);
637
638
166
        return (r);
639
55
}
640
641
int
642
u2f_register(fido_dev_t *dev, fido_cred_t *cred, int *ms)
643
410
{
644
410
        iso7816_apdu_t  *apdu = NULL;
645
410
        unsigned char    rp_id_hash[SHA256_DIGEST_LENGTH];
646
410
        unsigned char    reply[FIDO_MAXMSG];
647
410
        int              reply_len;
648
410
        int              found;
649
410
        int              r;
650
651
410
        if (cred->rk == FIDO_OPT_TRUE || cred->uv == FIDO_OPT_TRUE) {
652
15
                fido_log_debug("%s: rk=%d, uv=%d", __func__, cred->rk,
653
15
                    cred->uv);
654
15
                return (FIDO_ERR_UNSUPPORTED_OPTION);
655
15
        }
656
657
395
        if (cred->type != COSE_ES256 || cred->cdh.ptr == NULL ||
658
395
            cred->rp.id == NULL || cred->cdh.len != SHA256_DIGEST_LENGTH) {
659
40
                fido_log_debug("%s: type=%d, cdh=(%p,%zu)" , __func__,
660
40
                    cred->type, (void *)cred->cdh.ptr, cred->cdh.len);
661
40
                return (FIDO_ERR_INVALID_ARGUMENT);
662
40
        }
663
664
364
        for (size_t i = 0; i < cred->excl.len; i++) {
665
172
                if ((r = key_lookup(dev, cred->rp.id, &cred->excl.ptr[i],
666
172
                    &found, ms)) != FIDO_OK) {
667
144
                        fido_log_debug("%s: key_lookup", __func__);
668
144
                        return (r);
669
144
                }
670
28
                if (found) {
671
19
                        if ((r = send_dummy_register(dev, ms)) != FIDO_OK) {
672
11
                                fido_log_debug("%s: send_dummy_register",
673
11
                                    __func__);
674
11
                                return (r);
675
11
                        }
676
8
                        return (FIDO_ERR_CREDENTIAL_EXCLUDED);
677
8
                }
678
28
        }
679
680
355
        memset(&rp_id_hash, 0, sizeof(rp_id_hash));
681
682
192
        if (SHA256((const void *)cred->rp.id, strlen(cred->rp.id),
683
192
            rp_id_hash) != rp_id_hash) {
684
1
                fido_log_debug("%s: sha256", __func__);
685
1
                return (FIDO_ERR_INTERNAL);
686
1
        }
687
688
191
        if ((apdu = iso7816_new(0, U2F_CMD_REGISTER, 0, 2 *
689
191
            SHA256_DIGEST_LENGTH)) == NULL ||
690
191
            iso7816_add(apdu, cred->cdh.ptr, cred->cdh.len) < 0 ||
691
191
            iso7816_add(apdu, rp_id_hash, sizeof(rp_id_hash)) < 0) {
692
1
                fido_log_debug("%s: iso7816", __func__);
693
1
                r = FIDO_ERR_INTERNAL;
694
1
                goto fail;
695
1
        }
696
697
585
        do {
698
585
                if (fido_tx(dev, CTAP_CMD_MSG, iso7816_ptr(apdu),
699
585
                    iso7816_len(apdu), ms) < 0) {
700
6
                        fido_log_debug("%s: fido_tx", __func__);
701
6
                        r = FIDO_ERR_TX;
702
6
                        goto fail;
703
6
                }
704
579
                if ((reply_len = fido_rx(dev, CTAP_CMD_MSG, &reply,
705
579
                    sizeof(reply), ms)) < 2) {
706
17
                        fido_log_debug("%s: fido_rx", __func__);
707
17
                        r = FIDO_ERR_RX;
708
17
                        goto fail;
709
17
                }
710
562
                if (delay_ms(U2F_PACE_MS, ms) != 0) {
711
1
                        fido_log_debug("%s: delay_ms", __func__);
712
1
                        r = FIDO_ERR_RX;
713
1
                        goto fail;
714
1
                }
715
561
        } while (((reply[0] << 8) | reply[1]) == SW_CONDITIONS_NOT_SATISFIED);
716
717
190
        if ((r = parse_register_reply(cred, reply,
718
166
            (size_t)reply_len)) != FIDO_OK) {
719
111
                fido_log_debug("%s: parse_register_reply", __func__);
720
111
                goto fail;
721
111
        }
722
191
fail:
723
191
        iso7816_free(&apdu);
724
725
191
        return (r);
726
166
}
727
728
static int
729
u2f_authenticate_single(fido_dev_t *dev, const fido_blob_t *key_id,
730
    fido_assert_t *fa, size_t idx, int *ms)
731
704
{
732
704
        fido_blob_t     sig;
733
704
        fido_blob_t     ad;
734
704
        int             found;
735
704
        int             r;
736
737
704
        memset(&sig, 0, sizeof(sig));
738
704
        memset(&ad, 0, sizeof(ad));
739
740
704
        if ((r = key_lookup(dev, fa->rp_id, key_id, &found, ms)) != FIDO_OK) {
741
456
                fido_log_debug("%s: key_lookup", __func__);
742
456
                goto fail;
743
456
        }
744
745
248
        if (!found) {
746
2
                fido_log_debug("%s: not found", __func__);
747
2
                r = FIDO_ERR_CREDENTIAL_EXCLUDED;
748
2
                goto fail;
749
2
        }
750
751
246
        if (fido_blob_set(&fa->stmt[idx].id, key_id->ptr, key_id->len) < 0) {
752
1
                fido_log_debug("%s: fido_blob_set", __func__);
753
1
                r = FIDO_ERR_INTERNAL;
754
1
                goto fail;
755
1
        }
756
757
245
        if (fa->up == FIDO_OPT_FALSE) {
758
71
                fido_log_debug("%s: checking for key existence only", __func__);
759
71
                r = FIDO_ERR_USER_PRESENCE_REQUIRED;
760
71
                goto fail;
761
71
        }
762
763
174
        if ((r = do_auth(dev, &fa->cdh, fa->rp_id, key_id, &sig, &ad,
764
174
            ms)) != FIDO_OK) {
765
59
                fido_log_debug("%s: do_auth", __func__);
766
59
                goto fail;
767
59
        }
768
769
115
        if (fido_assert_set_authdata(fa, idx, ad.ptr, ad.len) != FIDO_OK ||
770
115
            fido_assert_set_sig(fa, idx, sig.ptr, sig.len) != FIDO_OK) {
771
2
                fido_log_debug("%s: fido_assert_set", __func__);
772
2
                r = FIDO_ERR_INTERNAL;
773
2
                goto fail;
774
2
        }
775
776
113
        r = FIDO_OK;
777
704
fail:
778
704
        fido_blob_reset(&sig);
779
704
        fido_blob_reset(&ad);
780
781
704
        return (r);
782
113
}
783
784
int
785
u2f_authenticate(fido_dev_t *dev, fido_assert_t *fa, int *ms)
786
580
{
787
580
        size_t  nfound = 0;
788
580
        size_t  nauth_ok = 0;
789
580
        int     r;
790
791
580
        if (fa->uv == FIDO_OPT_TRUE || fa->allow_list.ptr == NULL) {
792
54
                fido_log_debug("%s: uv=%d, allow_list=%p", __func__, fa->uv,
793
54
                    (void *)fa->allow_list.ptr);
794
54
                return (FIDO_ERR_UNSUPPORTED_OPTION);
795
54
        }
796
797
526
        if ((r = fido_assert_set_count(fa, fa->allow_list.len)) != FIDO_OK) {
798
1
                fido_log_debug("%s: fido_assert_set_count", __func__);
799
1
                return (r);
800
1
        }
801
802
711
        for (size_t i = 0; i < fa->allow_list.len; i++) {
803
704
                switch ((r = u2f_authenticate_single(dev,
804
704
                    &fa->allow_list.ptr[i], fa, nfound, ms))) {
805
113
                case FIDO_OK:
806
113
                        nauth_ok++;
807
                        /* FALLTHROUGH */
808
184
                case FIDO_ERR_USER_PRESENCE_REQUIRED:
809
184
                        nfound++;
810
184
                        break;
811
520
                default:
812
520
                        if (r != FIDO_ERR_CREDENTIAL_EXCLUDED) {
813
518
                                fido_log_debug("%s: u2f_authenticate_single",
814
518
                                    __func__);
815
518
                                return (r);
816
518
                        }
817
                        /* ignore credentials that don't exist */
818
704
                }
819
704
        }
820
821
525
        fa->stmt_len = nfound;
822
823
7
        if (nfound == 0)
824
1
                return (FIDO_ERR_NO_CREDENTIALS);
825
6
        if (nauth_ok == 0)
826
3
                return (FIDO_ERR_USER_PRESENCE_REQUIRED);
827
828
3
        return (FIDO_OK);
829
3
}
830
831
int
832
u2f_get_touch_begin(fido_dev_t *dev, int *ms)
833
2.07k
{
834
2.07k
        iso7816_apdu_t  *apdu = NULL;
835
2.07k
        const char      *clientdata = FIDO_DUMMY_CLIENTDATA;
836
2.07k
        const char      *rp_id = FIDO_DUMMY_RP_ID;
837
2.07k
        unsigned char    clientdata_hash[SHA256_DIGEST_LENGTH];
838
2.07k
        unsigned char    rp_id_hash[SHA256_DIGEST_LENGTH];
839
2.07k
        unsigned char    reply[FIDO_MAXMSG];
840
2.07k
        int              r;
841
842
2.07k
        memset(&clientdata_hash, 0, sizeof(clientdata_hash));
843
2.07k
        memset(&rp_id_hash, 0, sizeof(rp_id_hash));
844
845
2.07k
        if (SHA256((const void *)clientdata, strlen(clientdata),
846
2.07k
            clientdata_hash) != clientdata_hash || SHA256((const void *)rp_id,
847
2.06k
            strlen(rp_id), rp_id_hash) != rp_id_hash) {
848
26
                fido_log_debug("%s: sha256", __func__);
849
26
                return (FIDO_ERR_INTERNAL);
850
26
        }
851
852
2.05k
        if ((apdu = iso7816_new(0, U2F_CMD_REGISTER, 0, 2 *
853
2.05k
            SHA256_DIGEST_LENGTH)) == NULL ||
854
2.05k
            iso7816_add(apdu, clientdata_hash, sizeof(clientdata_hash)) < 0 ||
855
2.05k
            iso7816_add(apdu, rp_id_hash, sizeof(rp_id_hash)) < 0) {
856
11
                fido_log_debug("%s: iso7816", __func__);
857
11
                r = FIDO_ERR_INTERNAL;
858
11
                goto fail;
859
11
        }
860
861
2.04k
        if (dev->attr.flags & FIDO_CAP_WINK) {
862
1.30k
                fido_tx(dev, CTAP_CMD_WINK, NULL, 0, ms);
863
1.30k
                fido_rx(dev, CTAP_CMD_WINK, &reply, sizeof(reply), ms);
864
1.30k
        }
865
866
2.04k
        if (fido_tx(dev, CTAP_CMD_MSG, iso7816_ptr(apdu),
867
2.04k
            iso7816_len(apdu), ms) < 0) {
868
124
                fido_log_debug("%s: fido_tx", __func__);
869
124
                r = FIDO_ERR_TX;
870
124
                goto fail;
871
124
        }
872
873
1.91k
        r = FIDO_OK;
874
2.05k
fail:
875
2.05k
        iso7816_free(&apdu);
876
877
2.05k
        return (r);
878
1.91k
}
879
880
int
881
u2f_get_touch_status(fido_dev_t *dev, int *touched, int *ms)
882
2.06k
{
883
2.06k
        unsigned char   reply[FIDO_MAXMSG];
884
2.06k
        int             reply_len;
885
2.06k
        int             r;
886
887
2.06k
        if ((reply_len = fido_rx(dev, CTAP_CMD_MSG, &reply, sizeof(reply),
888
2.06k
            ms)) < 2) {
889
1.87k
                fido_log_debug("%s: fido_rx", __func__);
890
1.87k
                return (FIDO_OK); /* ignore */
891
1.87k
        }
892
893
184
        switch ((reply[reply_len - 2] << 8) | reply[reply_len - 1]) {
894
15
        case SW_CONDITIONS_NOT_SATISFIED:
895
15
                if ((r = u2f_get_touch_begin(dev, ms)) != FIDO_OK) {
896
4
                        fido_log_debug("%s: u2f_get_touch_begin", __func__);
897
4
                        return (r);
898
4
                }
899
11
                *touched = 0;
900
11
                break;
901
11
        case SW_NO_ERROR:
902
4
                *touched = 1;
903
4
                break;
904
165
        default:
905
165
                fido_log_debug("%s: unexpected sw", __func__);
906
165
                return (FIDO_ERR_RX);
907
15
        }
908
909
15
        return (FIDO_OK);
910
15
}