xref: /freebsd/contrib/libfido2/fuzz/fuzz_pcsc.c (revision 2ccfa855b2fc331819953e3de1b1c15ce5b95a7e)
1*2ccfa855SEd Maste /*
2*2ccfa855SEd Maste  * Copyright (c) 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 #define _FIDO_INTERNAL
9*2ccfa855SEd Maste 
10*2ccfa855SEd Maste #include <assert.h>
11*2ccfa855SEd Maste #include <stdint.h>
12*2ccfa855SEd Maste #include <stdlib.h>
13*2ccfa855SEd Maste #include <string.h>
14*2ccfa855SEd Maste #include <stdio.h>
15*2ccfa855SEd Maste #include <winscard.h>
16*2ccfa855SEd Maste 
17*2ccfa855SEd Maste #include "mutator_aux.h"
18*2ccfa855SEd Maste #include "wiredata_fido2.h"
19*2ccfa855SEd Maste #include "dummy.h"
20*2ccfa855SEd Maste 
21*2ccfa855SEd Maste #include "../src/extern.h"
22*2ccfa855SEd Maste 
23*2ccfa855SEd Maste struct param {
24*2ccfa855SEd Maste 	int seed;
25*2ccfa855SEd Maste 	char path[MAXSTR];
26*2ccfa855SEd Maste 	struct blob pcsc_list;
27*2ccfa855SEd Maste 	struct blob tx_apdu;
28*2ccfa855SEd Maste 	struct blob wiredata_init;
29*2ccfa855SEd Maste 	struct blob wiredata_msg;
30*2ccfa855SEd Maste };
31*2ccfa855SEd Maste 
32*2ccfa855SEd Maste static const uint8_t dummy_tx_apdu[] = { WIREDATA_CTAP_EXTENDED_APDU };
33*2ccfa855SEd Maste static const uint8_t dummy_wiredata_init[] = { WIREDATA_CTAP_NFC_INIT };
34*2ccfa855SEd Maste static const uint8_t dummy_wiredata_msg[] = { WIREDATA_CTAP_NFC_MSG };
35*2ccfa855SEd Maste 
36*2ccfa855SEd Maste struct param *
unpack(const uint8_t * ptr,size_t len)37*2ccfa855SEd Maste unpack(const uint8_t *ptr, size_t len)
38*2ccfa855SEd Maste {
39*2ccfa855SEd Maste 	cbor_item_t *item = NULL, **v;
40*2ccfa855SEd Maste 	struct cbor_load_result cbor;
41*2ccfa855SEd Maste 	struct param *p;
42*2ccfa855SEd Maste 	int ok = -1;
43*2ccfa855SEd Maste 
44*2ccfa855SEd Maste 	if ((p = calloc(1, sizeof(*p))) == NULL ||
45*2ccfa855SEd Maste 	    (item = cbor_load(ptr, len, &cbor)) == NULL ||
46*2ccfa855SEd Maste 	    cbor.read != len ||
47*2ccfa855SEd Maste 	    cbor_isa_array(item) == false ||
48*2ccfa855SEd Maste 	    cbor_array_is_definite(item) == false ||
49*2ccfa855SEd Maste 	    cbor_array_size(item) != 6 ||
50*2ccfa855SEd Maste 	    (v = cbor_array_handle(item)) == NULL)
51*2ccfa855SEd Maste 		goto fail;
52*2ccfa855SEd Maste 
53*2ccfa855SEd Maste 	if (unpack_int(v[0], &p->seed) < 0 ||
54*2ccfa855SEd Maste 	    unpack_string(v[1], p->path) < 0 ||
55*2ccfa855SEd Maste 	    unpack_blob(v[2], &p->pcsc_list) < 0 ||
56*2ccfa855SEd Maste 	    unpack_blob(v[3], &p->tx_apdu) < 0 ||
57*2ccfa855SEd Maste 	    unpack_blob(v[4], &p->wiredata_init) < 0 ||
58*2ccfa855SEd Maste 	    unpack_blob(v[5], &p->wiredata_msg) < 0)
59*2ccfa855SEd Maste 		goto fail;
60*2ccfa855SEd Maste 
61*2ccfa855SEd Maste 	ok = 0;
62*2ccfa855SEd Maste fail:
63*2ccfa855SEd Maste 	if (ok < 0) {
64*2ccfa855SEd Maste 		free(p);
65*2ccfa855SEd Maste 		p = NULL;
66*2ccfa855SEd Maste 	}
67*2ccfa855SEd Maste 
68*2ccfa855SEd Maste 	if (item)
69*2ccfa855SEd Maste 		cbor_decref(&item);
70*2ccfa855SEd Maste 
71*2ccfa855SEd Maste 	return p;
72*2ccfa855SEd Maste }
73*2ccfa855SEd Maste 
74*2ccfa855SEd Maste size_t
pack(uint8_t * ptr,size_t len,const struct param * p)75*2ccfa855SEd Maste pack(uint8_t *ptr, size_t len, const struct param *p)
76*2ccfa855SEd Maste {
77*2ccfa855SEd Maste 	cbor_item_t *argv[6], *array = NULL;
78*2ccfa855SEd Maste 	size_t cbor_alloc_len, cbor_len = 0;
79*2ccfa855SEd Maste 	unsigned char *cbor = NULL;
80*2ccfa855SEd Maste 
81*2ccfa855SEd Maste 	memset(argv, 0, sizeof(argv));
82*2ccfa855SEd Maste 
83*2ccfa855SEd Maste 	if ((array = cbor_new_definite_array(6)) == NULL ||
84*2ccfa855SEd Maste 	    (argv[0] = pack_int(p->seed)) == NULL ||
85*2ccfa855SEd Maste 	    (argv[1] = pack_string(p->path)) == NULL ||
86*2ccfa855SEd Maste 	    (argv[2] = pack_blob(&p->pcsc_list)) == NULL ||
87*2ccfa855SEd Maste 	    (argv[3] = pack_blob(&p->tx_apdu)) == NULL ||
88*2ccfa855SEd Maste 	    (argv[4] = pack_blob(&p->wiredata_init)) == NULL ||
89*2ccfa855SEd Maste 	    (argv[5] = pack_blob(&p->wiredata_msg)) == NULL)
90*2ccfa855SEd Maste 		goto fail;
91*2ccfa855SEd Maste 
92*2ccfa855SEd Maste 	for (size_t i = 0; i < 6; i++)
93*2ccfa855SEd Maste 		if (cbor_array_push(array, argv[i]) == false)
94*2ccfa855SEd Maste 			goto fail;
95*2ccfa855SEd Maste 
96*2ccfa855SEd Maste 	if ((cbor_len = cbor_serialize_alloc(array, &cbor,
97*2ccfa855SEd Maste 	    &cbor_alloc_len)) == 0 || cbor_len > len) {
98*2ccfa855SEd Maste 		cbor_len = 0;
99*2ccfa855SEd Maste 		goto fail;
100*2ccfa855SEd Maste 	}
101*2ccfa855SEd Maste 
102*2ccfa855SEd Maste 	memcpy(ptr, cbor, cbor_len);
103*2ccfa855SEd Maste fail:
104*2ccfa855SEd Maste 	for (size_t i = 0; i < 6; i++)
105*2ccfa855SEd Maste 		if (argv[i])
106*2ccfa855SEd Maste 			cbor_decref(&argv[i]);
107*2ccfa855SEd Maste 
108*2ccfa855SEd Maste 	if (array)
109*2ccfa855SEd Maste 		cbor_decref(&array);
110*2ccfa855SEd Maste 
111*2ccfa855SEd Maste 	free(cbor);
112*2ccfa855SEd Maste 
113*2ccfa855SEd Maste 	return cbor_len;
114*2ccfa855SEd Maste }
115*2ccfa855SEd Maste 
116*2ccfa855SEd Maste size_t
pack_dummy(uint8_t * ptr,size_t len)117*2ccfa855SEd Maste pack_dummy(uint8_t *ptr, size_t len)
118*2ccfa855SEd Maste {
119*2ccfa855SEd Maste 	struct param dummy;
120*2ccfa855SEd Maste 	uint8_t	blob[MAXCORPUS];
121*2ccfa855SEd Maste 	size_t blob_len;
122*2ccfa855SEd Maste 
123*2ccfa855SEd Maste 	memset(&dummy, 0, sizeof(dummy));
124*2ccfa855SEd Maste 
125*2ccfa855SEd Maste 	strlcpy(dummy.path, dummy_pcsc_path, sizeof(dummy.path));
126*2ccfa855SEd Maste 
127*2ccfa855SEd Maste 	dummy.pcsc_list.len = sizeof(dummy_pcsc_list);
128*2ccfa855SEd Maste 	memcpy(&dummy.pcsc_list.body, &dummy_pcsc_list, dummy.pcsc_list.len);
129*2ccfa855SEd Maste 
130*2ccfa855SEd Maste 	dummy.tx_apdu.len = sizeof(dummy_tx_apdu);
131*2ccfa855SEd Maste 	memcpy(&dummy.tx_apdu.body, &dummy_tx_apdu, dummy.tx_apdu.len);
132*2ccfa855SEd Maste 
133*2ccfa855SEd Maste 	dummy.wiredata_init.len = sizeof(dummy_wiredata_init);
134*2ccfa855SEd Maste 	memcpy(&dummy.wiredata_init.body, &dummy_wiredata_init,
135*2ccfa855SEd Maste 	    dummy.wiredata_init.len);
136*2ccfa855SEd Maste 
137*2ccfa855SEd Maste 	dummy.wiredata_msg.len = sizeof(dummy_wiredata_msg);
138*2ccfa855SEd Maste 	memcpy(&dummy.wiredata_msg.body, &dummy_wiredata_msg,
139*2ccfa855SEd Maste 	    dummy.wiredata_msg.len);
140*2ccfa855SEd Maste 
141*2ccfa855SEd Maste 	assert((blob_len = pack(blob, sizeof(blob), &dummy)) != 0);
142*2ccfa855SEd Maste 
143*2ccfa855SEd Maste 	if (blob_len > len) {
144*2ccfa855SEd Maste 		memcpy(ptr, blob, len);
145*2ccfa855SEd Maste 		return len;
146*2ccfa855SEd Maste 	}
147*2ccfa855SEd Maste 
148*2ccfa855SEd Maste 	memcpy(ptr, blob, blob_len);
149*2ccfa855SEd Maste 
150*2ccfa855SEd Maste 	return blob_len;
151*2ccfa855SEd Maste }
152*2ccfa855SEd Maste 
153*2ccfa855SEd Maste static void
test_manifest(void)154*2ccfa855SEd Maste test_manifest(void)
155*2ccfa855SEd Maste {
156*2ccfa855SEd Maste 	size_t ndevs, nfound;
157*2ccfa855SEd Maste 	fido_dev_info_t *devlist = NULL;
158*2ccfa855SEd Maste 	int16_t vendor_id, product_id;
159*2ccfa855SEd Maste 	int r;
160*2ccfa855SEd Maste 
161*2ccfa855SEd Maste 	r = fido_pcsc_manifest(NULL, 0, &nfound);
162*2ccfa855SEd Maste 	assert(r == FIDO_OK && nfound == 0);
163*2ccfa855SEd Maste 	r = fido_pcsc_manifest(NULL, 1, &nfound);
164*2ccfa855SEd Maste 	assert(r == FIDO_ERR_INVALID_ARGUMENT);
165*2ccfa855SEd Maste 
166*2ccfa855SEd Maste 	ndevs = uniform_random(64);
167*2ccfa855SEd Maste 	if ((devlist = fido_dev_info_new(ndevs)) == NULL ||
168*2ccfa855SEd Maste 	    fido_pcsc_manifest(devlist, ndevs, &nfound) != FIDO_OK)
169*2ccfa855SEd Maste 		goto out;
170*2ccfa855SEd Maste 
171*2ccfa855SEd Maste 	for (size_t i = 0; i < nfound; i++) {
172*2ccfa855SEd Maste 		const fido_dev_info_t *di = fido_dev_info_ptr(devlist, i);
173*2ccfa855SEd Maste 		consume_str(fido_dev_info_path(di));
174*2ccfa855SEd Maste 		consume_str(fido_dev_info_manufacturer_string(di));
175*2ccfa855SEd Maste 		consume_str(fido_dev_info_product_string(di));
176*2ccfa855SEd Maste 		vendor_id = fido_dev_info_vendor(di);
177*2ccfa855SEd Maste 		product_id = fido_dev_info_product(di);
178*2ccfa855SEd Maste 		consume(&vendor_id, sizeof(vendor_id));
179*2ccfa855SEd Maste 		consume(&product_id, sizeof(product_id));
180*2ccfa855SEd Maste 	}
181*2ccfa855SEd Maste 
182*2ccfa855SEd Maste out:
183*2ccfa855SEd Maste 	fido_dev_info_free(&devlist, ndevs);
184*2ccfa855SEd Maste }
185*2ccfa855SEd Maste 
186*2ccfa855SEd Maste static void
test_tx(const char * path,const struct blob * apdu,uint8_t cmd,u_char * rx_buf,size_t rx_len)187*2ccfa855SEd Maste test_tx(const char *path, const struct blob *apdu, uint8_t cmd, u_char *rx_buf,
188*2ccfa855SEd Maste     size_t rx_len)
189*2ccfa855SEd Maste {
190*2ccfa855SEd Maste 	fido_dev_t dev;
191*2ccfa855SEd Maste 	const u_char *tx_ptr = NULL;
192*2ccfa855SEd Maste 	size_t tx_len = 0;
193*2ccfa855SEd Maste 	int n;
194*2ccfa855SEd Maste 
195*2ccfa855SEd Maste 	memset(&dev, 0, sizeof(dev));
196*2ccfa855SEd Maste 
197*2ccfa855SEd Maste 	if (fido_dev_set_pcsc(&dev) < 0)
198*2ccfa855SEd Maste 		return;
199*2ccfa855SEd Maste 	if ((dev.io_handle = fido_pcsc_open(path)) == NULL)
200*2ccfa855SEd Maste 		return;
201*2ccfa855SEd Maste 
202*2ccfa855SEd Maste 	if (apdu) {
203*2ccfa855SEd Maste 		tx_ptr = apdu->body;
204*2ccfa855SEd Maste 		tx_len = apdu->len;
205*2ccfa855SEd Maste 	}
206*2ccfa855SEd Maste 
207*2ccfa855SEd Maste 	fido_pcsc_tx(&dev, cmd, tx_ptr, tx_len);
208*2ccfa855SEd Maste 
209*2ccfa855SEd Maste 	if ((n = fido_pcsc_rx(&dev, cmd, rx_buf, rx_len, -1)) >= 0)
210*2ccfa855SEd Maste 		consume(rx_buf, n);
211*2ccfa855SEd Maste 
212*2ccfa855SEd Maste 	fido_pcsc_close(dev.io_handle);
213*2ccfa855SEd Maste }
214*2ccfa855SEd Maste 
215*2ccfa855SEd Maste static void
test_misc(void)216*2ccfa855SEd Maste test_misc(void)
217*2ccfa855SEd Maste {
218*2ccfa855SEd Maste 	assert(fido_pcsc_open(NULL) == NULL);
219*2ccfa855SEd Maste 	assert(fido_pcsc_write(NULL, NULL, INT_MAX + 1LL) == -1);
220*2ccfa855SEd Maste }
221*2ccfa855SEd Maste 
222*2ccfa855SEd Maste void
test(const struct param * p)223*2ccfa855SEd Maste test(const struct param *p)
224*2ccfa855SEd Maste {
225*2ccfa855SEd Maste 	u_char buf[512];
226*2ccfa855SEd Maste 
227*2ccfa855SEd Maste 	prng_init((unsigned int)p->seed);
228*2ccfa855SEd Maste 	fuzz_clock_reset();
229*2ccfa855SEd Maste 	fido_init(FIDO_DEBUG);
230*2ccfa855SEd Maste 	fido_set_log_handler(consume_str);
231*2ccfa855SEd Maste 
232*2ccfa855SEd Maste 	set_pcsc_parameters(&p->pcsc_list);
233*2ccfa855SEd Maste 	set_pcsc_io_functions(nfc_read, nfc_write, consume);
234*2ccfa855SEd Maste 
235*2ccfa855SEd Maste 	set_wire_data(p->wiredata_init.body, p->wiredata_init.len);
236*2ccfa855SEd Maste 	test_manifest();
237*2ccfa855SEd Maste 
238*2ccfa855SEd Maste 	test_misc();
239*2ccfa855SEd Maste 
240*2ccfa855SEd Maste 	set_wire_data(p->wiredata_init.body, p->wiredata_init.len);
241*2ccfa855SEd Maste 	test_tx(p->path, NULL, CTAP_CMD_INIT, buf, uniform_random(20));
242*2ccfa855SEd Maste 
243*2ccfa855SEd Maste 	set_wire_data(p->wiredata_msg.body, p->wiredata_msg.len);
244*2ccfa855SEd Maste 	test_tx(p->path, &p->tx_apdu, CTAP_CMD_MSG, buf, sizeof(buf));
245*2ccfa855SEd Maste 
246*2ccfa855SEd Maste 	set_wire_data(p->wiredata_msg.body, p->wiredata_msg.len);
247*2ccfa855SEd Maste 	test_tx(p->path, &p->tx_apdu, CTAP_CMD_CBOR, buf, sizeof(buf));
248*2ccfa855SEd Maste 
249*2ccfa855SEd Maste 	set_wire_data(p->wiredata_msg.body, p->wiredata_msg.len);
250*2ccfa855SEd Maste 	test_tx(p->path, &p->tx_apdu, CTAP_CMD_LOCK, buf, sizeof(buf));
251*2ccfa855SEd Maste }
252*2ccfa855SEd Maste 
253*2ccfa855SEd Maste void
mutate(struct param * p,unsigned int seed,unsigned int flags)254*2ccfa855SEd Maste mutate(struct param *p, unsigned int seed, unsigned int flags) NO_MSAN
255*2ccfa855SEd Maste {
256*2ccfa855SEd Maste 	if (flags & MUTATE_SEED)
257*2ccfa855SEd Maste 		p->seed = (int)seed;
258*2ccfa855SEd Maste 
259*2ccfa855SEd Maste 	if (flags & MUTATE_PARAM) {
260*2ccfa855SEd Maste 		mutate_string(p->path);
261*2ccfa855SEd Maste 		mutate_blob(&p->pcsc_list);
262*2ccfa855SEd Maste 		mutate_blob(&p->tx_apdu);
263*2ccfa855SEd Maste 	}
264*2ccfa855SEd Maste 
265*2ccfa855SEd Maste 	if (flags & MUTATE_WIREDATA) {
266*2ccfa855SEd Maste 		mutate_blob(&p->wiredata_init);
267*2ccfa855SEd Maste 		mutate_blob(&p->wiredata_msg);
268*2ccfa855SEd Maste 	}
269*2ccfa855SEd Maste }
270