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