Coverage Report

Created: 2022-04-27 14:33

/libfido2/src/nfc.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2020-2022 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 <stdio.h>
8
#include <string.h>
9
10
#include "fido.h"
11
#include "fido/param.h"
12
#include "iso7816.h"
13
14
2.80k
#define TX_CHUNK_SIZE   240
15
16
static const uint8_t aid[] = { 0xa0, 0x00, 0x00, 0x06, 0x47, 0x2f, 0x00, 0x01 };
17
static const uint8_t v_u2f[] = { 'U', '2', 'F', '_', 'V', '2' };
18
static const uint8_t v_fido[] = { 'F', 'I', 'D', 'O', '_', '2', '_', '0' };
19
20
static int
21
tx_short_apdu(fido_dev_t *d, const iso7816_header_t *h, const uint8_t *payload,
22
    uint8_t payload_len, uint8_t cla_flags)
23
2.62k
{
24
2.62k
        uint8_t apdu[5 + UINT8_MAX + 1];
25
2.62k
        uint8_t sw[2];
26
2.62k
        size_t apdu_len;
27
2.62k
        int ok = -1;
28
29
2.62k
        memset(&apdu, 0, sizeof(apdu));
30
2.62k
        apdu[0] = h->cla | cla_flags;
31
2.62k
        apdu[1] = h->ins;
32
2.62k
        apdu[2] = h->p1;
33
2.62k
        apdu[3] = h->p2;
34
2.62k
        apdu[4] = payload_len;
35
2.62k
        memcpy(&apdu[5], payload, payload_len);
36
2.62k
        apdu_len = (size_t)(5 + payload_len + 1);
37
38
2.62k
        if (d->io.write(d->io_handle, apdu, apdu_len) < 0) {
39
25
                fido_log_debug("%s: write", __func__);
40
25
                goto fail;
41
25
        }
42
43
2.60k
        if (cla_flags & 0x10) {
44
156
                if (d->io.read(d->io_handle, sw, sizeof(sw), -1) != 2) {
45
62
                        fido_log_debug("%s: read", __func__);
46
62
                        goto fail;
47
62
                }
48
94
                if ((sw[0] << 8 | sw[1]) != SW_NO_ERROR) {
49
84
                        fido_log_debug("%s: unexpected sw", __func__);
50
84
                        goto fail;
51
84
                }
52
94
        }
53
54
2.45k
        ok = 0;
55
2.62k
fail:
56
2.62k
        explicit_bzero(apdu, sizeof(apdu));
57
58
2.62k
        return ok;
59
2.45k
}
60
61
static int
62
nfc_do_tx(fido_dev_t *d, const uint8_t *apdu_ptr, size_t apdu_len)
63
2.87k
{
64
2.87k
        iso7816_header_t h;
65
66
2.87k
        if (fido_buf_read(&apdu_ptr, &apdu_len, &h, sizeof(h)) < 0) {
67
253
                fido_log_debug("%s: header", __func__);
68
253
                return -1;
69
253
        }
70
2.62k
        if (apdu_len < 2) {
71
5
                fido_log_debug("%s: apdu_len %zu", __func__, apdu_len);
72
5
                return -1;
73
5
        }
74
75
2.61k
        apdu_len -= 2; /* trim le1 le2 */
76
77
2.62k
        while (apdu_len > TX_CHUNK_SIZE) {
78
156
                if (tx_short_apdu(d, &h, apdu_ptr, TX_CHUNK_SIZE, 0x10) < 0) {
79
146
                        fido_log_debug("%s: chain", __func__);
80
146
                        return -1;
81
146
                }
82
10
                apdu_ptr += TX_CHUNK_SIZE;
83
10
                apdu_len -= TX_CHUNK_SIZE;
84
10
        }
85
86
2.47k
        if (tx_short_apdu(d, &h, apdu_ptr, (uint8_t)apdu_len, 0) < 0) {
87
25
                fido_log_debug("%s: tx_short_apdu", __func__);
88
25
                return -1;
89
25
        }
90
91
2.44k
        return 0;
92
2.47k
}
93
94
int
95
fido_nfc_tx(fido_dev_t *d, uint8_t cmd, const unsigned char *buf, size_t count)
96
3.36k
{
97
3.36k
        iso7816_apdu_t *apdu = NULL;
98
3.36k
        const uint8_t *ptr;
99
3.36k
        size_t len;
100
3.36k
        int ok = -1;
101
102
3.36k
        switch (cmd) {
103
1.21k
        case CTAP_CMD_INIT: /* select */
104
1.21k
                if ((apdu = iso7816_new(0, 0xa4, 0x04, sizeof(aid))) == NULL ||
105
1.21k
                    iso7816_add(apdu, aid, sizeof(aid)) < 0) {
106
14
                        fido_log_debug("%s: iso7816", __func__);
107
14
                        goto fail;
108
14
                }
109
1.20k
                break;
110
1.20k
        case CTAP_CMD_CBOR: /* wrap cbor */
111
957
                if (count > UINT16_MAX || (apdu = iso7816_new(0x80, 0x10, 0x00,
112
954
                    (uint16_t)count)) == NULL ||
113
957
                    iso7816_add(apdu, buf, count) < 0) {
114
17
                        fido_log_debug("%s: iso7816", __func__);
115
17
                        goto fail;
116
17
                }
117
940
                break;
118
940
        case CTAP_CMD_MSG: /* already an apdu */
119
731
                break;
120
455
        default:
121
455
                fido_log_debug("%s: cmd=%02x", __func__, cmd);
122
455
                goto fail;
123
3.36k
        }
124
125
2.87k
        if (apdu != NULL) {
126
2.14k
                ptr = iso7816_ptr(apdu);
127
2.14k
                len = iso7816_len(apdu);
128
2.14k
        } else {
129
731
                ptr = buf;
130
731
                len = count;
131
731
        }
132
133
2.87k
        if (nfc_do_tx(d, ptr, len) < 0) {
134
429
                fido_log_debug("%s: nfc_do_tx", __func__);
135
429
                goto fail;
136
429
        }
137
138
2.44k
        ok = 0;
139
3.36k
fail:
140
3.36k
        iso7816_free(&apdu);
141
142
3.36k
        return ok;
143
2.44k
}
144
145
static int
146
rx_init(fido_dev_t *d, unsigned char *buf, size_t count, int ms)
147
1.18k
{
148
1.18k
        fido_ctap_info_t *attr = (fido_ctap_info_t *)buf;
149
1.18k
        uint8_t f[64];
150
1.18k
        int n;
151
152
1.18k
        if (count != sizeof(*attr)) {
153
246
                fido_log_debug("%s: count=%zu", __func__, count);
154
246
                return -1;
155
246
        }
156
157
936
        memset(attr, 0, sizeof(*attr));
158
159
936
        if ((n = d->io.read(d->io_handle, f, sizeof(f), ms)) < 2 ||
160
936
            (f[n - 2] << 8 | f[n - 1]) != SW_NO_ERROR) {
161
323
                fido_log_debug("%s: read", __func__);
162
323
                return -1;
163
323
        }
164
165
613
        n -= 2;
166
167
613
        if (n == sizeof(v_u2f) && memcmp(f, v_u2f, sizeof(v_u2f)) == 0)
168
1
                attr->flags = FIDO_CAP_CBOR;
169
612
        else if (n == sizeof(v_fido) && memcmp(f, v_fido, sizeof(v_fido)) == 0)
170
1
                attr->flags = FIDO_CAP_CBOR | FIDO_CAP_NMSG;
171
611
        else {
172
611
                fido_log_debug("%s: unknown version string", __func__);
173
611
#ifdef FIDO_FUZZ
174
611
                attr->flags = FIDO_CAP_CBOR | FIDO_CAP_NMSG;
175
#else
176
                return -1;
177
#endif
178
611
        }
179
180
613
        memcpy(&attr->nonce, &d->nonce, sizeof(attr->nonce)); /* XXX */
181
182
613
        return (int)count;
183
936
}
184
185
static int
186
tx_get_response(fido_dev_t *d, uint8_t count)
187
96
{
188
96
        uint8_t apdu[5];
189
190
96
        memset(apdu, 0, sizeof(apdu));
191
96
        apdu[1] = 0xc0; /* GET_RESPONSE */
192
96
        apdu[4] = count;
193
194
96
        if (d->io.write(d->io_handle, apdu, sizeof(apdu)) < 0) {
195
10
                fido_log_debug("%s: write", __func__);
196
10
                return -1;
197
10
        }
198
199
86
        return 0;
200
96
}
201
202
static int
203
rx_apdu(fido_dev_t *d, uint8_t sw[2], unsigned char **buf, size_t *count, int *ms)
204
1.65k
{
205
1.65k
        uint8_t f[256 + 2];
206
1.65k
        struct timespec ts;
207
1.65k
        int n, ok = -1;
208
209
1.65k
        if (fido_time_now(&ts) != 0)
210
23
                goto fail;
211
212
1.63k
        if ((n = d->io.read(d->io_handle, f, sizeof(f), *ms)) < 2) {
213
1.10k
                fido_log_debug("%s: read", __func__);
214
1.10k
                goto fail;
215
1.10k
        }
216
217
527
        if (fido_time_delta(&ts, ms) != 0)
218
15
                goto fail;
219
220
512
        if (fido_buf_write(buf, count, f, (size_t)(n - 2)) < 0) {
221
0
                fido_log_debug("%s: fido_buf_write", __func__);
222
0
                goto fail;
223
0
        }
224
225
512
        memcpy(sw, f + n - 2, 2);
226
227
512
        ok = 0;
228
1.65k
fail:
229
1.65k
        explicit_bzero(f, sizeof(f));
230
231
1.65k
        return ok;
232
512
}
233
234
static int
235
rx_msg(fido_dev_t *d, unsigned char *buf, size_t count, int ms)
236
1.57k
{
237
1.57k
        uint8_t sw[2];
238
1.57k
        const size_t bufsiz = count;
239
240
1.57k
        if (rx_apdu(d, sw, &buf, &count, &ms) < 0) {
241
1.08k
                fido_log_debug("%s: preamble", __func__);
242
1.08k
                return -1;
243
1.08k
        }
244
245
512
        while (sw[0] == SW1_MORE_DATA)
246
96
                if (tx_get_response(d, sw[1]) < 0 ||
247
96
                    rx_apdu(d, sw, &buf, &count, &ms) < 0) {
248
75
                        fido_log_debug("%s: chain", __func__);
249
75
                        return -1;
250
75
                }
251
252
416
        if (fido_buf_write(&buf, &count, sw, sizeof(sw)) < 0) {
253
0
                fido_log_debug("%s: sw", __func__);
254
0
                return -1;
255
0
        }
256
257
416
        if (bufsiz - count > INT_MAX) {
258
0
                fido_log_debug("%s: bufsiz", __func__);
259
0
                return -1;
260
0
        }
261
262
416
        return (int)(bufsiz - count);
263
416
}
264
265
static int
266
rx_cbor(fido_dev_t *d, unsigned char *buf, size_t count, int ms)
267
870
{
268
870
        int r;
269
270
870
        if ((r = rx_msg(d, buf, count, ms)) < 2)
271
577
                return -1;
272
273
293
        return r - 2;
274
870
}
275
276
int
277
fido_nfc_rx(fido_dev_t *d, uint8_t cmd, unsigned char *buf, size_t count, int ms)
278
3.16k
{
279
3.16k
        switch (cmd) {
280
1.18k
        case CTAP_CMD_INIT:
281
1.18k
                return rx_init(d, buf, count, ms);
282
870
        case CTAP_CMD_CBOR:
283
870
                return rx_cbor(d, buf, count, ms);
284
701
        case CTAP_CMD_MSG:
285
701
                return rx_msg(d, buf, count, ms);
286
414
        default:
287
414
                fido_log_debug("%s: cmd=%02x", __func__, cmd);
288
414
                return -1;
289
3.16k
        }
290
3.16k
}
291
292
#ifdef USE_NFC
293
bool
294
fido_is_nfc(const char *path)
295
69.4k
{
296
69.4k
        return strncmp(path, FIDO_NFC_PREFIX, strlen(FIDO_NFC_PREFIX)) == 0;
297
69.4k
}
298
299
int
300
fido_dev_set_nfc(fido_dev_t *d)
301
0
{
302
0
        if (d->io_handle != NULL) {
303
0
                fido_log_debug("%s: device open", __func__);
304
0
                return -1;
305
0
        }
306
0
        d->io_own = true;
307
0
        d->io = (fido_dev_io_t) {
308
0
                fido_nfc_open,
309
0
                fido_nfc_close,
310
0
                fido_nfc_read,
311
0
                fido_nfc_write,
312
0
        };
313
0
        d->transport = (fido_dev_transport_t) {
314
0
                fido_nfc_rx,
315
0
                fido_nfc_tx,
316
0
        };
317
318
0
        return 0;
319
0
}
320
#endif /* USE_NFC */