1*98a5e356Schristos /*
2*98a5e356Schristos * Copyright (c) 2020 Yubico AB. All rights reserved.
3*98a5e356Schristos * Use of this source code is governed by a BSD-style
4*98a5e356Schristos * license that can be found in the LICENSE file.
5*98a5e356Schristos */
6*98a5e356Schristos
7*98a5e356Schristos #include <assert.h>
8*98a5e356Schristos #include <stdint.h>
9*98a5e356Schristos #include <stdio.h>
10*98a5e356Schristos #include <stdlib.h>
11*98a5e356Schristos #include <string.h>
12*98a5e356Schristos
13*98a5e356Schristos #include "mutator_aux.h"
14*98a5e356Schristos #include "wiredata_fido2.h"
15*98a5e356Schristos #include "dummy.h"
16*98a5e356Schristos
17*98a5e356Schristos #include "../openbsd-compat/openbsd-compat.h"
18*98a5e356Schristos
19*98a5e356Schristos /* Parameter set defining a FIDO2 "large blob" operation. */
20*98a5e356Schristos struct param {
21*98a5e356Schristos char pin[MAXSTR];
22*98a5e356Schristos int seed;
23*98a5e356Schristos struct blob key;
24*98a5e356Schristos struct blob get_wiredata;
25*98a5e356Schristos struct blob set_wiredata;
26*98a5e356Schristos };
27*98a5e356Schristos
28*98a5e356Schristos /*
29*98a5e356Schristos * Collection of HID reports from an authenticator issued with a FIDO2
30*98a5e356Schristos * 'authenticatorLargeBlobs' 'get' command.
31*98a5e356Schristos */
32*98a5e356Schristos static const uint8_t dummy_get_wiredata[] = {
33*98a5e356Schristos WIREDATA_CTAP_INIT,
34*98a5e356Schristos WIREDATA_CTAP_CBOR_INFO,
35*98a5e356Schristos WIREDATA_CTAP_CBOR_LARGEBLOB_GET_ARRAY
36*98a5e356Schristos };
37*98a5e356Schristos
38*98a5e356Schristos /*
39*98a5e356Schristos * Collection of HID reports from an authenticator issued with a FIDO2
40*98a5e356Schristos * 'authenticatorLargeBlobs' 'set' command.
41*98a5e356Schristos */
42*98a5e356Schristos static const uint8_t dummy_set_wiredata[] = {
43*98a5e356Schristos WIREDATA_CTAP_INIT,
44*98a5e356Schristos WIREDATA_CTAP_CBOR_INFO,
45*98a5e356Schristos WIREDATA_CTAP_CBOR_LARGEBLOB_GET_ARRAY,
46*98a5e356Schristos WIREDATA_CTAP_CBOR_AUTHKEY,
47*98a5e356Schristos WIREDATA_CTAP_CBOR_PINTOKEN,
48*98a5e356Schristos WIREDATA_CTAP_CBOR_STATUS
49*98a5e356Schristos };
50*98a5e356Schristos
51*98a5e356Schristos /*
52*98a5e356Schristos * XXX this needs to match the encrypted blob embedded in
53*98a5e356Schristos * WIREDATA_CTAP_CBOR_LARGEBLOB_GET_ARRAY.
54*98a5e356Schristos */
55*98a5e356Schristos static const uint8_t dummy_key[] = {
56*98a5e356Schristos 0xa9, 0x1b, 0xc4, 0xdd, 0xfc, 0x9a, 0x93, 0x79,
57*98a5e356Schristos 0x75, 0xba, 0xf7, 0x7f, 0x4d, 0x57, 0xfc, 0xa6,
58*98a5e356Schristos 0xe1, 0xf8, 0x06, 0x43, 0x23, 0x99, 0x51, 0x32,
59*98a5e356Schristos 0xce, 0x6e, 0x19, 0x84, 0x50, 0x13, 0x2d, 0x7b
60*98a5e356Schristos };
61*98a5e356Schristos
62*98a5e356Schristos struct param *
unpack(const uint8_t * ptr,size_t len)63*98a5e356Schristos unpack(const uint8_t *ptr, size_t len)
64*98a5e356Schristos {
65*98a5e356Schristos cbor_item_t *item = NULL, **v;
66*98a5e356Schristos struct cbor_load_result cbor;
67*98a5e356Schristos struct param *p;
68*98a5e356Schristos int ok = -1;
69*98a5e356Schristos
70*98a5e356Schristos if ((p = calloc(1, sizeof(*p))) == NULL ||
71*98a5e356Schristos (item = cbor_load(ptr, len, &cbor)) == NULL ||
72*98a5e356Schristos cbor.read != len ||
73*98a5e356Schristos cbor_isa_array(item) == false ||
74*98a5e356Schristos cbor_array_is_definite(item) == false ||
75*98a5e356Schristos cbor_array_size(item) != 5 ||
76*98a5e356Schristos (v = cbor_array_handle(item)) == NULL)
77*98a5e356Schristos goto fail;
78*98a5e356Schristos
79*98a5e356Schristos if (unpack_int(v[0], &p->seed) < 0 ||
80*98a5e356Schristos unpack_string(v[1], p->pin) < 0 ||
81*98a5e356Schristos unpack_blob(v[2], &p->key) < 0 ||
82*98a5e356Schristos unpack_blob(v[3], &p->get_wiredata) < 0 ||
83*98a5e356Schristos unpack_blob(v[4], &p->set_wiredata) < 0)
84*98a5e356Schristos goto fail;
85*98a5e356Schristos
86*98a5e356Schristos ok = 0;
87*98a5e356Schristos fail:
88*98a5e356Schristos if (ok < 0) {
89*98a5e356Schristos free(p);
90*98a5e356Schristos p = NULL;
91*98a5e356Schristos }
92*98a5e356Schristos
93*98a5e356Schristos if (item)
94*98a5e356Schristos cbor_decref(&item);
95*98a5e356Schristos
96*98a5e356Schristos return p;
97*98a5e356Schristos }
98*98a5e356Schristos
99*98a5e356Schristos size_t
pack(uint8_t * ptr,size_t len,const struct param * p)100*98a5e356Schristos pack(uint8_t *ptr, size_t len, const struct param *p)
101*98a5e356Schristos {
102*98a5e356Schristos cbor_item_t *argv[5], *array = NULL;
103*98a5e356Schristos size_t cbor_alloc_len, cbor_len = 0;
104*98a5e356Schristos unsigned char *cbor = NULL;
105*98a5e356Schristos
106*98a5e356Schristos memset(argv, 0, sizeof(argv));
107*98a5e356Schristos
108*98a5e356Schristos if ((array = cbor_new_definite_array(5)) == NULL ||
109*98a5e356Schristos (argv[0] = pack_int(p->seed)) == NULL ||
110*98a5e356Schristos (argv[1] = pack_string(p->pin)) == NULL ||
111*98a5e356Schristos (argv[2] = pack_blob(&p->key)) == NULL ||
112*98a5e356Schristos (argv[3] = pack_blob(&p->get_wiredata)) == NULL ||
113*98a5e356Schristos (argv[4] = pack_blob(&p->set_wiredata)) == NULL)
114*98a5e356Schristos goto fail;
115*98a5e356Schristos
116*98a5e356Schristos for (size_t i = 0; i < 5; i++)
117*98a5e356Schristos if (cbor_array_push(array, argv[i]) == false)
118*98a5e356Schristos goto fail;
119*98a5e356Schristos
120*98a5e356Schristos if ((cbor_len = cbor_serialize_alloc(array, &cbor,
121*98a5e356Schristos &cbor_alloc_len)) > len) {
122*98a5e356Schristos cbor_len = 0;
123*98a5e356Schristos goto fail;
124*98a5e356Schristos }
125*98a5e356Schristos
126*98a5e356Schristos memcpy(ptr, cbor, cbor_len);
127*98a5e356Schristos fail:
128*98a5e356Schristos for (size_t i = 0; i < 5; i++)
129*98a5e356Schristos if (argv[i])
130*98a5e356Schristos cbor_decref(&argv[i]);
131*98a5e356Schristos
132*98a5e356Schristos if (array)
133*98a5e356Schristos cbor_decref(&array);
134*98a5e356Schristos
135*98a5e356Schristos free(cbor);
136*98a5e356Schristos
137*98a5e356Schristos return cbor_len;
138*98a5e356Schristos }
139*98a5e356Schristos
140*98a5e356Schristos size_t
pack_dummy(uint8_t * ptr,size_t len)141*98a5e356Schristos pack_dummy(uint8_t *ptr, size_t len)
142*98a5e356Schristos {
143*98a5e356Schristos struct param dummy;
144*98a5e356Schristos uint8_t blob[4096];
145*98a5e356Schristos size_t blob_len;
146*98a5e356Schristos
147*98a5e356Schristos memset(&dummy, 0, sizeof(dummy));
148*98a5e356Schristos
149*98a5e356Schristos strlcpy(dummy.pin, dummy_pin, sizeof(dummy.pin));
150*98a5e356Schristos
151*98a5e356Schristos dummy.get_wiredata.len = sizeof(dummy_get_wiredata);
152*98a5e356Schristos dummy.set_wiredata.len = sizeof(dummy_set_wiredata);
153*98a5e356Schristos dummy.key.len = sizeof(dummy_key);
154*98a5e356Schristos
155*98a5e356Schristos memcpy(&dummy.get_wiredata.body, &dummy_get_wiredata,
156*98a5e356Schristos dummy.get_wiredata.len);
157*98a5e356Schristos memcpy(&dummy.set_wiredata.body, &dummy_set_wiredata,
158*98a5e356Schristos dummy.set_wiredata.len);
159*98a5e356Schristos memcpy(&dummy.key.body, &dummy_key, dummy.key.len);
160*98a5e356Schristos
161*98a5e356Schristos assert((blob_len = pack(blob, sizeof(blob), &dummy)) != 0);
162*98a5e356Schristos
163*98a5e356Schristos if (blob_len > len) {
164*98a5e356Schristos memcpy(ptr, blob, len);
165*98a5e356Schristos return len;
166*98a5e356Schristos }
167*98a5e356Schristos
168*98a5e356Schristos memcpy(ptr, blob, blob_len);
169*98a5e356Schristos
170*98a5e356Schristos return blob_len;
171*98a5e356Schristos }
172*98a5e356Schristos
173*98a5e356Schristos static fido_dev_t *
prepare_dev(void)174*98a5e356Schristos prepare_dev(void)
175*98a5e356Schristos {
176*98a5e356Schristos fido_dev_t *dev;
177*98a5e356Schristos
178*98a5e356Schristos if ((dev = open_dev(0)) == NULL)
179*98a5e356Schristos return NULL;
180*98a5e356Schristos
181*98a5e356Schristos return dev;
182*98a5e356Schristos }
183*98a5e356Schristos
184*98a5e356Schristos static void
get_blob(const struct param * p,int array)185*98a5e356Schristos get_blob(const struct param *p, int array)
186*98a5e356Schristos {
187*98a5e356Schristos fido_dev_t *dev;
188*98a5e356Schristos u_char *ptr = NULL;
189*98a5e356Schristos size_t len = 0;
190*98a5e356Schristos
191*98a5e356Schristos set_wire_data(p->get_wiredata.body, p->get_wiredata.len);
192*98a5e356Schristos
193*98a5e356Schristos if ((dev = prepare_dev()) == NULL)
194*98a5e356Schristos return;
195*98a5e356Schristos
196*98a5e356Schristos if (array)
197*98a5e356Schristos fido_dev_largeblob_get_array(dev, &ptr, &len);
198*98a5e356Schristos else
199*98a5e356Schristos fido_dev_largeblob_get(dev, p->key.body, p->key.len, &ptr, &len);
200*98a5e356Schristos consume(ptr, len);
201*98a5e356Schristos free(ptr);
202*98a5e356Schristos
203*98a5e356Schristos fido_dev_close(dev);
204*98a5e356Schristos fido_dev_free(&dev);
205*98a5e356Schristos }
206*98a5e356Schristos
207*98a5e356Schristos
208*98a5e356Schristos static void
set_blob(const struct param * p,int op)209*98a5e356Schristos set_blob(const struct param *p, int op)
210*98a5e356Schristos {
211*98a5e356Schristos fido_dev_t *dev;
212*98a5e356Schristos const char *pin;
213*98a5e356Schristos
214*98a5e356Schristos set_wire_data(p->set_wiredata.body, p->set_wiredata.len);
215*98a5e356Schristos
216*98a5e356Schristos if ((dev = prepare_dev()) == NULL)
217*98a5e356Schristos return;
218*98a5e356Schristos pin = p->pin;
219*98a5e356Schristos if (strlen(pin) == 0)
220*98a5e356Schristos pin = NULL;
221*98a5e356Schristos
222*98a5e356Schristos switch (op) {
223*98a5e356Schristos case 0:
224*98a5e356Schristos fido_dev_largeblob_remove(dev, p->key.body, p->key.len, pin);
225*98a5e356Schristos break;
226*98a5e356Schristos case 1:
227*98a5e356Schristos /* XXX reuse p->get_wiredata as the blob to be set */
228*98a5e356Schristos fido_dev_largeblob_set(dev, p->key.body, p->key.len,
229*98a5e356Schristos p->get_wiredata.body, p->get_wiredata.len, pin);
230*98a5e356Schristos break;
231*98a5e356Schristos case 2:
232*98a5e356Schristos /* XXX reuse p->get_wiredata as the body of the cbor array */
233*98a5e356Schristos fido_dev_largeblob_set_array(dev, p->get_wiredata.body,
234*98a5e356Schristos p->get_wiredata.len, pin);
235*98a5e356Schristos }
236*98a5e356Schristos
237*98a5e356Schristos fido_dev_close(dev);
238*98a5e356Schristos fido_dev_free(&dev);
239*98a5e356Schristos }
240*98a5e356Schristos
241*98a5e356Schristos void
test(const struct param * p)242*98a5e356Schristos test(const struct param *p)
243*98a5e356Schristos {
244*98a5e356Schristos prng_init((unsigned int)p->seed);
245*98a5e356Schristos fido_init(FIDO_DEBUG);
246*98a5e356Schristos fido_set_log_handler(consume_str);
247*98a5e356Schristos
248*98a5e356Schristos get_blob(p, 0);
249*98a5e356Schristos get_blob(p, 1);
250*98a5e356Schristos set_blob(p, 0);
251*98a5e356Schristos set_blob(p, 1);
252*98a5e356Schristos set_blob(p, 2);
253*98a5e356Schristos }
254*98a5e356Schristos
255*98a5e356Schristos void
mutate(struct param * p,unsigned int seed,unsigned int flags)256*98a5e356Schristos mutate(struct param *p, unsigned int seed, unsigned int flags) NO_MSAN
257*98a5e356Schristos {
258*98a5e356Schristos if (flags & MUTATE_SEED)
259*98a5e356Schristos p->seed = (int)seed;
260*98a5e356Schristos
261*98a5e356Schristos if (flags & MUTATE_PARAM) {
262*98a5e356Schristos mutate_blob(&p->key);
263*98a5e356Schristos mutate_string(p->pin);
264*98a5e356Schristos }
265*98a5e356Schristos
266*98a5e356Schristos if (flags & MUTATE_WIREDATA) {
267*98a5e356Schristos mutate_blob(&p->get_wiredata);
268*98a5e356Schristos mutate_blob(&p->set_wiredata);
269*98a5e356Schristos }
270*98a5e356Schristos }
271