Debian Codegraph @

fuzz/fuzz_cred.c

raw
1 /*
2 * Copyright (c) 2019-2026 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 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8 #undef NDEBUG
9
10 #include <assert.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include "mutator_aux.h"
17 #include "wiredata_fido2.h"
18 #include "wiredata_u2f.h"
19 #include "dummy.h"
20
21 #include "../openbsd-compat/openbsd-compat.h"
22
23 enum {
24 OPT_FORCE_U2F = 1,
25 OPT_NFC = 2,
26 OPT_PUAT = 4,
27
28 OPT_EDGE,
29 OPT_MASK = (((OPT_EDGE - 1) << 1) - 1),
30 };
31
32 /* Parameter set defining a FIDO2 make credential operation. */
33 struct param {
34 char pin[MAXSTR];
35 char rp_id[MAXSTR];
36 char rp_name[MAXSTR];
37 char user_icon[MAXSTR];
38 char user_name[MAXSTR];
39 char user_nick[MAXSTR];
40 int ext;
41 int seed;
42 struct blob cdh;
43 struct blob excl_cred;
44 struct blob user_id;
45 struct blob wire_data;
46 uint8_t excl_count;
47 uint8_t rk;
48 uint8_t type;
49 uint8_t opt;
50 uint8_t uv;
51 };
52
53 /*
54 * Collection of HID reports from an authenticator issued with a FIDO2
55 * make credential using the example parameters above.
56 */
57 static const uint8_t dummy_wire_data_fido[] = {
58 WIREDATA_CTAP_INIT,
59 WIREDATA_CTAP_CBOR_INFO,
60 WIREDATA_CTAP_CBOR_AUTHKEY,
61 WIREDATA_CTAP_CBOR_PINTOKEN,
62 WIREDATA_CTAP_KEEPALIVE,
63 WIREDATA_CTAP_KEEPALIVE,
64 WIREDATA_CTAP_KEEPALIVE,
65 WIREDATA_CTAP_CBOR_CRED,
66 };
67
68 /*
69 * Collection of HID reports from an authenticator issued with a U2F
70 * registration using the example parameters above.
71 */
72 static const uint8_t dummy_wire_data_u2f[] = {
73 WIREDATA_CTAP_INIT,
74 WIREDATA_CTAP_U2F_6985,
75 WIREDATA_CTAP_U2F_6985,
76 WIREDATA_CTAP_U2F_6985,
77 WIREDATA_CTAP_U2F_6985,
78 WIREDATA_CTAP_U2F_6985,
79 WIREDATA_CTAP_U2F_REGISTER,
80 };
81
82 struct param *
83 unpack(const uint8_t *ptr, size_t len)
84 {
85 cbor_item_t *item = NULL, **v;
86 struct cbor_load_result cbor;
87 struct param *p;
88 int ok = -1;
89
90 if ((p = calloc(1, sizeof(*p))) == NULL ||
91 (item = cbor_load(ptr, len, &cbor)) == NULL ||
92 cbor.read != len ||
93 cbor_isa_array(item) == false ||
94 cbor_array_is_definite(item) == false ||
95 cbor_array_size(item) != 17 ||
96 (v = cbor_array_handle(item)) == NULL)
97 goto fail;
98
99 if (unpack_byte(v[0], &p->rk) < 0 ||
100 unpack_byte(v[1], &p->type) < 0 ||
101 unpack_byte(v[2], &p->opt) < 0 ||
102 unpack_byte(v[3], &p->uv) < 0 ||
103 unpack_byte(v[4], &p->excl_count) < 0 ||
104 unpack_int(v[5], &p->ext) < 0 ||
105 unpack_int(v[6], &p->seed) < 0 ||
106 unpack_string(v[7], p->pin) < 0 ||
107 unpack_string(v[8], p->rp_id) < 0 ||
108 unpack_string(v[9], p->rp_name) < 0 ||
109 unpack_string(v[10], p->user_icon) < 0 ||
110 unpack_string(v[11], p->user_name) < 0 ||
111 unpack_string(v[12], p->user_nick) < 0 ||
112 unpack_blob(v[13], &p->cdh) < 0 ||
113 unpack_blob(v[14], &p->user_id) < 0 ||
114 unpack_blob(v[15], &p->wire_data) < 0 ||
115 unpack_blob(v[16], &p->excl_cred) < 0)
116 goto fail;
117
118 ok = 0;
119 fail:
120 if (ok < 0) {
121 free(p);
122 p = NULL;
123 }
124
125 if (item)
126 cbor_decref(&item);
127
128 return p;
129 }
130
131 size_t
132 pack(uint8_t *ptr, size_t len, const struct param *p)
133 {
134 cbor_item_t *argv[17], *array = NULL;
135 size_t cbor_alloc_len, cbor_len = 0;
136 unsigned char *cbor = NULL;
137
138 memset(argv, 0, sizeof(argv));
139
140 if ((array = cbor_new_definite_array(17)) == NULL ||
141 (argv[0] = pack_byte(p->rk)) == NULL ||
142 (argv[1] = pack_byte(p->type)) == NULL ||
143 (argv[2] = pack_byte(p->opt)) == NULL ||
144 (argv[3] = pack_byte(p->uv)) == NULL ||
145 (argv[4] = pack_byte(p->excl_count)) == NULL ||
146 (argv[5] = pack_int(p->ext)) == NULL ||
147 (argv[6] = pack_int(p->seed)) == NULL ||
148 (argv[7] = pack_string(p->pin)) == NULL ||
149 (argv[8] = pack_string(p->rp_id)) == NULL ||
150 (argv[9] = pack_string(p->rp_name)) == NULL ||
151 (argv[10] = pack_string(p->user_icon)) == NULL ||
152 (argv[11] = pack_string(p->user_name)) == NULL ||
153 (argv[12] = pack_string(p->user_nick)) == NULL ||
154 (argv[13] = pack_blob(&p->cdh)) == NULL ||
155 (argv[14] = pack_blob(&p->user_id)) == NULL ||
156 (argv[15] = pack_blob(&p->wire_data)) == NULL ||
157 (argv[16] = pack_blob(&p->excl_cred)) == NULL)
158 goto fail;
159
160 for (size_t i = 0; i < 17; i++)
161 if (cbor_array_push(array, argv[i]) == false)
162 goto fail;
163
164 if ((cbor_len = cbor_serialize_alloc(array, &cbor,
165 &cbor_alloc_len)) == 0 || cbor_len > len) {
166 cbor_len = 0;
167 goto fail;
168 }
169
170 memcpy(ptr, cbor, cbor_len);
171 fail:
172 for (size_t i = 0; i < 17; i++)
173 if (argv[i])
174 cbor_decref(&argv[i]);
175
176 if (array)
177 cbor_decref(&array);
178
179 free(cbor);
180
181 return cbor_len;
182 }
183
184 size_t
185 pack_dummy(uint8_t *ptr, size_t len)
186 {
187 struct param dummy;
188 uint8_t blob[MAXCORPUS];
189 size_t blob_len;
190
191 memset(&dummy, 0, sizeof(dummy));
192
193 dummy.type = 1;
194 dummy.ext = FIDO_EXT_HMAC_SECRET;
195
196 strlcpy(dummy.pin, dummy_pin, sizeof(dummy.pin));
197 strlcpy(dummy.rp_id, dummy_rp_id, sizeof(dummy.rp_id));
198 strlcpy(dummy.rp_name, dummy_rp_name, sizeof(dummy.rp_name));
199 strlcpy(dummy.user_icon, dummy_user_icon, sizeof(dummy.user_icon));
200 strlcpy(dummy.user_name, dummy_user_name, sizeof(dummy.user_name));
201 strlcpy(dummy.user_nick, dummy_user_nick, sizeof(dummy.user_nick));
202
203 dummy.cdh.len = sizeof(dummy_cdh);
204 dummy.user_id.len = sizeof(dummy_user_id);
205 dummy.wire_data.len = sizeof(dummy_wire_data_fido);
206
207 memcpy(&dummy.cdh.body, &dummy_cdh, dummy.cdh.len);
208 memcpy(&dummy.user_id.body, &dummy_user_id, dummy.user_id.len);
209 memcpy(&dummy.wire_data.body, &dummy_wire_data_fido,
210 dummy.wire_data.len);
211
212 assert((blob_len = pack(blob, sizeof(blob), &dummy)) != 0);
213
214 if (blob_len > len) {
215 memcpy(ptr, blob, len);
216 return len;
217 }
218
219 memcpy(ptr, blob, blob_len);
220
221 return blob_len;
222 }
223
224 static void
225 make_cred(fido_cred_t *cred, uint8_t opt, int type, const struct blob *cdh,
226 const char *rp_id, const char *rp_name, const struct blob *user_id,
227 const char *user_name, const char *user_nick, const char *user_icon,
228 int ext, uint8_t rk, uint8_t uv, const char *pin, uint8_t excl_count,
229 const struct blob *excl_cred)
230 {
231 fido_dev_t *dev;
232
233 if ((dev = open_dev(opt & OPT_NFC)) == NULL)
234 return;
235 if (opt & OPT_FORCE_U2F)
236 fido_dev_force_u2f(dev);
237
238 for (uint8_t i = 0; i < excl_count; i++)
239 fido_cred_exclude(cred, excl_cred->body, excl_cred->len);
240
241 fido_cred_set_type(cred, type);
242 fido_cred_set_clientdata_hash(cred, cdh->body, cdh->len);
243 fido_cred_set_rp(cred, rp_id, rp_name);
244 fido_cred_set_user(cred, user_id->body, user_id->len, user_name,
245 user_nick, user_icon);
246
247 if (ext & FIDO_EXT_HMAC_SECRET)
248 fido_cred_set_extensions(cred, FIDO_EXT_HMAC_SECRET);
249 if (ext & FIDO_EXT_CRED_BLOB)
250 fido_cred_set_blob(cred, user_id->body, user_id->len);
251 if (ext & FIDO_EXT_LARGEBLOB_KEY)
252 fido_cred_set_extensions(cred, FIDO_EXT_LARGEBLOB_KEY);
253 if (ext & FIDO_EXT_MINPINLEN)
254 fido_cred_set_pin_minlen(cred, strlen(pin));
255 if (ext & FIDO_EXT_HMAC_SECRET_MC)
256 fido_cred_set_extensions(cred, FIDO_EXT_HMAC_SECRET_MC);
257 if (ext & FIDO_EXT_PAYMENT)
258 fido_cred_set_extensions(cred, FIDO_EXT_PAYMENT);
259
260 if (rk & 1)
261 fido_cred_set_rk(cred, FIDO_OPT_TRUE);
262 if (uv & 1)
263 fido_cred_set_uv(cred, FIDO_OPT_TRUE);
264 if (user_id->len)
265 fido_cred_set_prot(cred, user_id->body[0] & 0x03);
266 if (excl_cred->len)
267 fido_cred_set_entattest(cred, excl_cred->body[0] & 0x03);
268
269 /* repeat memory operations to trigger reallocation paths */
270 fido_cred_set_type(cred, type);
271 fido_cred_set_clientdata_hash(cred, cdh->body, cdh->len);
272 fido_cred_set_rp(cred, rp_id, rp_name);
273 fido_cred_set_user(cred, user_id->body, user_id->len, user_name,
274 user_nick, user_icon);
275 /* XXX reuse cred as hmac salt */
276 fido_cred_set_hmac_salt(cred, excl_cred->body, excl_cred->len);
277
278 if (opt & OPT_FORCE_U2F || strlen(pin) == 0)
279 pin = NULL;
280
281 if (opt & OPT_PUAT) {
282 /* the actual PUAT permission is not relevant for libfido2 */
283 fido_dev_get_puat(dev, FIDO_PUAT_MAKECRED, rp_id, pin);
284 }
285
286 fido_dev_make_cred(dev, cred, pin);
287
288 fido_dev_cancel(dev);
289 fido_dev_close(dev);
290 fido_dev_free(&dev);
291 }
292
293 static void
294 verify_cred(int type, const unsigned char *cdh_ptr, size_t cdh_len,
295 const char *rp_id, const char *rp_name, const unsigned char *authdata_ptr,
296 size_t authdata_len, const unsigned char *authdata_raw_ptr,
297 size_t authdata_raw_len, int ext, uint8_t rk, uint8_t uv,
298 const unsigned char *x5c_ptr, size_t x5c_len, const unsigned char *sig_ptr,
299 size_t sig_len, const unsigned char *attstmt_ptr, size_t attstmt_len,
300 const char *fmt, int prot, size_t minpinlen)
301 {
302 fido_cred_t *cred;
303 uint8_t flags;
304 uint32_t sigcount;
305 int r;
306 bool ea;
307
308 if ((cred = fido_cred_new()) == NULL)
309 return;
310
311 fido_cred_set_type(cred, type);
312 fido_cred_set_clientdata_hash(cred, cdh_ptr, cdh_len);
313 fido_cred_set_rp(cred, rp_id, rp_name);
314 consume(authdata_ptr, authdata_len);
315 consume(authdata_raw_ptr, authdata_raw_len);
316 consume(x5c_ptr, x5c_len);
317 consume(sig_ptr, sig_len);
318 consume(attstmt_ptr, attstmt_len);
319 if (fido_cred_set_authdata(cred, authdata_ptr, authdata_len) != FIDO_OK)
320 fido_cred_set_authdata_raw(cred, authdata_raw_ptr,
321 authdata_raw_len);
322 fido_cred_set_extensions(cred, ext);
323 if (fido_cred_set_attstmt(cred, attstmt_ptr, attstmt_len) != FIDO_OK) {
324 fido_cred_set_x509(cred, x5c_ptr, x5c_len);
325 fido_cred_set_sig(cred, sig_ptr, sig_len);
326 }
327 fido_cred_set_prot(cred, prot);
328 fido_cred_set_pin_minlen(cred, minpinlen);
329
330 if (rk & 1)
331 fido_cred_set_rk(cred, FIDO_OPT_TRUE);
332 if (uv & 1)
333 fido_cred_set_uv(cred, FIDO_OPT_TRUE);
334 if (fmt)
335 fido_cred_set_fmt(cred, fmt);
336
337 /* XXX +1 on purpose */
338 for (size_t i = 0; i < fido_cred_x5c_list_count(cred) + 1; i++)
339 consume(fido_cred_x5c_list_ptr(cred, i),
340 fido_cred_x5c_list_len(cred, i));
341
342 /* repeat memory operations to trigger reallocation paths */
343 if (fido_cred_set_authdata(cred, authdata_ptr, authdata_len) != FIDO_OK)
344 fido_cred_set_authdata_raw(cred, authdata_raw_ptr,
345 authdata_raw_len);
346 if (fido_cred_set_attstmt(cred, attstmt_ptr, attstmt_len) != FIDO_OK) {
347 fido_cred_set_x509(cred, x5c_ptr, x5c_len);
348 fido_cred_set_sig(cred, sig_ptr, sig_len);
349 }
350 fido_cred_set_x509(cred, x5c_ptr, x5c_len);
351 fido_cred_set_sig(cred, sig_ptr, sig_len);
352
353 r = fido_cred_verify(cred);
354 consume(&r, sizeof(r));
355 r = fido_cred_verify_self(cred);
356 consume(&r, sizeof(r));
357
358 consume(fido_cred_pubkey_ptr(cred), fido_cred_pubkey_len(cred));
359 consume(fido_cred_id_ptr(cred), fido_cred_id_len(cred));
360 consume(fido_cred_aaguid_ptr(cred), fido_cred_aaguid_len(cred));
361 consume(fido_cred_user_id_ptr(cred), fido_cred_user_id_len(cred));
362 consume_str(fido_cred_user_name(cred));
363 consume_str(fido_cred_display_name(cred));
364 consume(fido_cred_largeblob_key_ptr(cred),
365 fido_cred_largeblob_key_len(cred));
366 consume(fido_cred_hmac_secret_ptr(cred),
367 fido_cred_hmac_secret_len(cred));
368
369 flags = fido_cred_flags(cred);
370 consume(&flags, sizeof(flags));
371 sigcount = fido_cred_sigcount(cred);
372 consume(&sigcount, sizeof(sigcount));
373 type = fido_cred_type(cred);
374 consume(&type, sizeof(type));
375 minpinlen = fido_cred_pin_minlen(cred);
376 consume(&minpinlen, sizeof(minpinlen));
377
378 ea = fido_cred_entattest(cred);
379 consume(&ea, sizeof(ea));
380
381 fido_cred_free(&cred);
382 }
383
384 static void
385 test_cred(const struct param *p)
386 {
387 fido_cred_t *cred = NULL;
388 int cose_alg = 0;
389
390 if ((cred = fido_cred_new()) == NULL)
391 return;
392
393 switch (p->type & 3) {
394 case 0:
395 cose_alg = COSE_ES256;
396 break;
397 case 1:
398 cose_alg = COSE_RS256;
399 break;
400 case 2:
401 cose_alg = COSE_ES384;
402 break;
403 default:
404 cose_alg = COSE_EDDSA;
405 break;
406 }
407
408 set_wire_data(p->wire_data.body, p->wire_data.len);
409
410 make_cred(cred, p->opt, cose_alg, &p->cdh, p->rp_id, p->rp_name,
411 &p->user_id, p->user_name, p->user_nick, p->user_icon, p->ext,
412 p->rk, p->uv, p->pin, p->excl_count, &p->excl_cred);
413
414 verify_cred(cose_alg,
415 fido_cred_clientdata_hash_ptr(cred),
416 fido_cred_clientdata_hash_len(cred), fido_cred_rp_id(cred),
417 fido_cred_rp_name(cred), fido_cred_authdata_ptr(cred),
418 fido_cred_authdata_len(cred), fido_cred_authdata_raw_ptr(cred),
419 fido_cred_authdata_raw_len(cred), p->ext, p->rk, p->uv,
420 fido_cred_x5c_ptr(cred), fido_cred_x5c_len(cred),
421 fido_cred_sig_ptr(cred), fido_cred_sig_len(cred),
422 fido_cred_attstmt_ptr(cred), fido_cred_attstmt_len(cred),
423 fido_cred_fmt(cred), fido_cred_prot(cred),
424 fido_cred_pin_minlen(cred));
425
426 fido_cred_free(&cred);
427 }
428
429 static void
430 test_touch(const struct param *p)
431 {
432 fido_dev_t *dev;
433 int r;
434 int touched;
435
436 set_wire_data(p->wire_data.body, p->wire_data.len);
437
438 if ((dev = open_dev(p->opt & OPT_NFC)) == NULL)
439 return;
440 if (p->opt & OPT_FORCE_U2F)
441 fido_dev_force_u2f(dev);
442
443 r = fido_dev_get_touch_begin(dev);
444 consume_str(fido_strerr(r));
445 r = fido_dev_get_touch_status(dev, &touched, -1);
446 consume_str(fido_strerr(r));
447 consume(&touched, sizeof(touched));
448
449 fido_dev_cancel(dev);
450 fido_dev_close(dev);
451 fido_dev_free(&dev);
452 }
453
454 static void
455 test_misc(const struct param *p)
456 {
457 fido_cred_t *cred = NULL;
458
459 if ((cred = fido_cred_new()) == NULL)
460 return;
461
462 /* reuse user id as credential id */
463 fido_cred_set_id(cred, p->user_id.body, p->user_id.len);
464 consume(fido_cred_id_ptr(cred), fido_cred_id_len(cred));
465 fido_cred_free(&cred);
466 }
467
468 void
469 test(const struct param *p)
470 {
471 prng_init((unsigned int)p->seed);
472 fuzz_clock_reset();
473 fido_init(FIDO_DEBUG);
474 fido_set_log_handler(consume_str);
475
476 test_cred(p);
477 test_touch(p);
478 test_misc(p);
479 }
480
481 void
482 mutate(struct param *p, unsigned int seed, unsigned int flags) NO_MSAN
483 {
484 if (flags & MUTATE_SEED)
485 p->seed = (int)seed;
486
487 if (flags & MUTATE_PARAM) {
488 mutate_byte(&p->rk);
489 mutate_byte(&p->type);
490 mutate_byte(&p->opt);
491 mutate_byte(&p->uv);
492 mutate_byte(&p->excl_count);
493 mutate_int(&p->ext);
494 mutate_blob(&p->cdh);
495 mutate_blob(&p->user_id);
496 mutate_blob(&p->excl_cred);
497 mutate_string(p->pin);
498 mutate_string(p->user_icon);
499 mutate_string(p->user_name);
500 mutate_string(p->user_nick);
501 mutate_string(p->rp_id);
502 mutate_string(p->rp_name);
503
504 p->opt &= OPT_MASK;
505 }
506
507 if (flags & MUTATE_WIREDATA) {
508 if (p->opt & OPT_FORCE_U2F) {
509 p->wire_data.len = sizeof(dummy_wire_data_u2f);
510 memcpy(&p->wire_data.body, &dummy_wire_data_u2f,
511 p->wire_data.len);
512 } else {
513 p->wire_data.len = sizeof(dummy_wire_data_fido);
514 memcpy(&p->wire_data.body, &dummy_wire_data_fido,
515 p->wire_data.len);
516 }
517 mutate_blob(&p->wire_data);
518 }
519 }