Path: blob/main/contrib/libfido2/fuzz/fuzz_attobj.c
213651 views
/*1* Copyright (c) 2024 Yubico AB. All rights reserved.2* Use of this source code is governed by a BSD-style3* license that can be found in the LICENSE file.4* SPDX-License-Identifier: BSD-2-Clause5*/67#include <assert.h>8#include <stdint.h>9#include <stdio.h>10#include <stdlib.h>11#include <string.h>1213#include "mutator_aux.h"14#include "wiredata_fido2.h"15#include "wiredata_u2f.h"16#include "dummy.h"1718#include "../openbsd-compat/openbsd-compat.h"1920struct param {21int seed;22char rp_id[MAXSTR];23struct blob cdh;24struct blob attobj;25uint8_t type;26};2728static const uint8_t dummy_attestation_object[] = {290xa3, 0x63, 0x66, 0x6d, 0x74, 0x66, 0x70, 0x61,300x63, 0x6b, 0x65, 0x64, 0x67, 0x61, 0x74, 0x74,310x53, 0x74, 0x6d, 0x74, 0xa3, 0x63, 0x61, 0x6c,320x67, 0x26, 0x63, 0x73, 0x69, 0x67, 0x58, 0x46,330x30, 0x44, 0x02, 0x20, 0x54, 0x92, 0x28, 0x3b,340x83, 0x33, 0x47, 0x56, 0x68, 0x79, 0xb2, 0x0c,350x84, 0x80, 0xcc, 0x67, 0x27, 0x8b, 0xfa, 0x48,360x43, 0x0d, 0x3c, 0xb4, 0x02, 0x36, 0x87, 0x97,370x3e, 0xdf, 0x2f, 0x65, 0x02, 0x20, 0x1b, 0x56,380x17, 0x06, 0xe2, 0x26, 0x0f, 0x6a, 0xe9, 0xa9,390x70, 0x99, 0x62, 0xeb, 0x3a, 0x04, 0x1a, 0xc4,400xa7, 0x03, 0x28, 0x56, 0x7c, 0xed, 0x47, 0x08,410x68, 0x73, 0x6a, 0xb6, 0x89, 0x0d, 0x63, 0x78,420x35, 0x63, 0x81, 0x59, 0x02, 0xe6, 0x30, 0x82,430x02, 0xe2, 0x30, 0x81, 0xcb, 0x02, 0x01, 0x01,440x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,450xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30,460x1d, 0x31, 0x1b, 0x30, 0x19, 0x06, 0x03, 0x55,470x04, 0x03, 0x13, 0x12, 0x59, 0x75, 0x62, 0x69,480x63, 0x6f, 0x20, 0x55, 0x32, 0x46, 0x20, 0x54,490x65, 0x73, 0x74, 0x20, 0x43, 0x41, 0x30, 0x1e,500x17, 0x0d, 0x31, 0x34, 0x30, 0x35, 0x31, 0x35,510x31, 0x32, 0x35, 0x38, 0x35, 0x34, 0x5a, 0x17,520x0d, 0x31, 0x34, 0x30, 0x36, 0x31, 0x34, 0x31,530x32, 0x35, 0x38, 0x35, 0x34, 0x5a, 0x30, 0x1d,540x31, 0x1b, 0x30, 0x19, 0x06, 0x03, 0x55, 0x04,550x03, 0x13, 0x12, 0x59, 0x75, 0x62, 0x69, 0x63,560x6f, 0x20, 0x55, 0x32, 0x46, 0x20, 0x54, 0x65,570x73, 0x74, 0x20, 0x45, 0x45, 0x30, 0x59, 0x30,580x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d,590x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce,600x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04,610xdb, 0x0a, 0xdb, 0xf5, 0x21, 0xc7, 0x5c, 0xce,620x63, 0xdc, 0xa6, 0xe1, 0xe8, 0x25, 0x06, 0x0d,630x94, 0xe6, 0x27, 0x54, 0x19, 0x4f, 0x9d, 0x24,640xaf, 0x26, 0x1a, 0xbe, 0xad, 0x99, 0x44, 0x1f,650x95, 0xa3, 0x71, 0x91, 0x0a, 0x3a, 0x20, 0xe7,660x3e, 0x91, 0x5e, 0x13, 0xe8, 0xbe, 0x38, 0x05,670x7a, 0xd5, 0x7a, 0xa3, 0x7e, 0x76, 0x90, 0x8f,680xaf, 0xe2, 0x8a, 0x94, 0xb6, 0x30, 0xeb, 0x9d,690x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,700xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03,710x82, 0x02, 0x01, 0x00, 0x95, 0x40, 0x6b, 0x50,720x61, 0x7d, 0xad, 0x84, 0xa3, 0xb4, 0xeb, 0x88,730x0f, 0xe3, 0x30, 0x0f, 0x2d, 0xa2, 0x0a, 0x00,740xd9, 0x25, 0x04, 0xee, 0x72, 0xfa, 0x67, 0xdf,750x58, 0x51, 0x0f, 0x0b, 0x47, 0x02, 0x9c, 0x3e,760x41, 0x29, 0x4a, 0x93, 0xac, 0x29, 0x85, 0x89,770x2d, 0xa4, 0x7a, 0x81, 0x32, 0x28, 0x57, 0x71,780x01, 0xef, 0xa8, 0x42, 0x88, 0x16, 0x96, 0x37,790x91, 0xd5, 0xdf, 0xe0, 0x8f, 0xc9, 0x3c, 0x8d,800xb0, 0xcd, 0x89, 0x70, 0x82, 0xec, 0x79, 0xd3,810xc6, 0x78, 0x73, 0x29, 0x32, 0xe5, 0xab, 0x6c,820xbd, 0x56, 0x9f, 0xd5, 0x45, 0x91, 0xce, 0xc1,830xdd, 0x8d, 0x64, 0xdc, 0xe9, 0x9c, 0x1f, 0x5e,840x3c, 0xd2, 0xaf, 0x51, 0xa5, 0x82, 0x18, 0xaf,850xe0, 0x37, 0xe7, 0x32, 0x9e, 0x76, 0x05, 0x77,860x02, 0x7b, 0xe6, 0x24, 0xa0, 0x31, 0x56, 0x1b,870xfd, 0x19, 0xc5, 0x71, 0xd3, 0xf0, 0x9e, 0xc0,880x73, 0x05, 0x4e, 0xbc, 0x85, 0xb8, 0x53, 0x9e,890xef, 0xc5, 0xbc, 0x9c, 0x56, 0xa3, 0xba, 0xd9,900x27, 0x6a, 0xbb, 0xa9, 0x7a, 0x40, 0xd7, 0x47,910x8b, 0x55, 0x72, 0x6b, 0xe3, 0xfe, 0x28, 0x49,920x71, 0x24, 0xf4, 0x8f, 0xf4, 0x20, 0x81, 0xea,930x38, 0xff, 0x7c, 0x0a, 0x4f, 0xdf, 0x02, 0x82,940x39, 0x81, 0x82, 0x3b, 0xca, 0x09, 0xdd, 0xca,950xaa, 0x0f, 0x27, 0xf5, 0xa4, 0x83, 0x55, 0x6c,960x9a, 0x39, 0x9b, 0x15, 0x3a, 0x16, 0x63, 0xdc,970x5b, 0xf9, 0xac, 0x5b, 0xbc, 0xf7, 0x9f, 0xbe,980x0f, 0x8a, 0xa2, 0x3c, 0x31, 0x13, 0xa3, 0x32,990x48, 0xca, 0x58, 0x87, 0xf8, 0x7b, 0xa0, 0xa1,1000x0a, 0x6a, 0x60, 0x96, 0x93, 0x5f, 0x5d, 0x26,1010x9e, 0x63, 0x1d, 0x09, 0xae, 0x9a, 0x41, 0xe5,1020xbd, 0x08, 0x47, 0xfe, 0xe5, 0x09, 0x9b, 0x20,1030xfd, 0x12, 0xe2, 0xe6, 0x40, 0x7f, 0xba, 0x4a,1040x61, 0x33, 0x66, 0x0d, 0x0e, 0x73, 0xdb, 0xb0,1050xd5, 0xa2, 0x9a, 0x9a, 0x17, 0x0d, 0x34, 0x30,1060x85, 0x6a, 0x42, 0x46, 0x9e, 0xff, 0x34, 0x8f,1070x5f, 0x87, 0x6c, 0x35, 0xe7, 0xa8, 0x4d, 0x35,1080xeb, 0xc1, 0x41, 0xaa, 0x8a, 0xd2, 0xda, 0x19,1090xaa, 0x79, 0xa2, 0x5f, 0x35, 0x2c, 0xa0, 0xfd,1100x25, 0xd3, 0xf7, 0x9d, 0x25, 0x18, 0x2d, 0xfa,1110xb4, 0xbc, 0xbb, 0x07, 0x34, 0x3c, 0x8d, 0x81,1120xbd, 0xf4, 0xe9, 0x37, 0xdb, 0x39, 0xe9, 0xd1,1130x45, 0x5b, 0x20, 0x41, 0x2f, 0x2d, 0x27, 0x22,1140xdc, 0x92, 0x74, 0x8a, 0x92, 0xd5, 0x83, 0xfd,1150x09, 0xfb, 0x13, 0x9b, 0xe3, 0x39, 0x7a, 0x6b,1160x5c, 0xfa, 0xe6, 0x76, 0x9e, 0xe0, 0xe4, 0xe3,1170xef, 0xad, 0xbc, 0xfd, 0x42, 0x45, 0x9a, 0xd4,1180x94, 0xd1, 0x7e, 0x8d, 0xa7, 0xd8, 0x05, 0xd5,1190xd3, 0x62, 0xcf, 0x15, 0xcf, 0x94, 0x7d, 0x1f,1200x5b, 0x58, 0x20, 0x44, 0x20, 0x90, 0x71, 0xbe,1210x66, 0xe9, 0x9a, 0xab, 0x74, 0x32, 0x70, 0x53,1220x1d, 0x69, 0xed, 0x87, 0x66, 0xf4, 0x09, 0x4f,1230xca, 0x25, 0x30, 0xc2, 0x63, 0x79, 0x00, 0x3c,1240xb1, 0x9b, 0x39, 0x3f, 0x00, 0xe0, 0xa8, 0x88,1250xef, 0x7a, 0x51, 0x5b, 0xe7, 0xbd, 0x49, 0x64,1260xda, 0x41, 0x7b, 0x24, 0xc3, 0x71, 0x22, 0xfd,1270xd1, 0xd1, 0x20, 0xb3, 0x3f, 0x97, 0xd3, 0x97,1280xb2, 0xaa, 0x18, 0x1c, 0x9e, 0x03, 0x77, 0x7b,1290x5b, 0x7e, 0xf9, 0xa3, 0xa0, 0xd6, 0x20, 0x81,1300x2c, 0x38, 0x8f, 0x9d, 0x25, 0xde, 0xe9, 0xc8,1310xf5, 0xdd, 0x6a, 0x47, 0x9c, 0x65, 0x04, 0x5a,1320x56, 0xe6, 0xc2, 0xeb, 0xf2, 0x02, 0x97, 0xe1,1330xb9, 0xd8, 0xe1, 0x24, 0x76, 0x9f, 0x23, 0x62,1340x39, 0x03, 0x4b, 0xc8, 0xf7, 0x34, 0x07, 0x49,1350xd6, 0xe7, 0x4d, 0x9a, 0x68, 0x61, 0x75, 0x74,1360x68, 0x44, 0x61, 0x74, 0x61, 0x58, 0xc4, 0x49,1370x96, 0x0d, 0xe5, 0x88, 0x0e, 0x8c, 0x68, 0x74,1380x34, 0x17, 0x0f, 0x64, 0x76, 0x60, 0x5b, 0x8f,1390xe4, 0xae, 0xb9, 0xa2, 0x86, 0x32, 0xc7, 0x99,1400x5c, 0xf3, 0xba, 0x83, 0x1d, 0x97, 0x63, 0x41,1410x00, 0x00, 0x00, 0x00, 0xf8, 0xa0, 0x11, 0xf3,1420x8c, 0x0a, 0x4d, 0x15, 0x80, 0x06, 0x17, 0x11,1430x1f, 0x9e, 0xdc, 0x7d, 0x00, 0x40, 0x53, 0xfb,1440xdf, 0xaa, 0xce, 0x63, 0xde, 0xc5, 0xfe, 0x47,1450xe6, 0x52, 0xeb, 0xf3, 0x5d, 0x53, 0xa8, 0xbf,1460x9d, 0xd6, 0x09, 0x6b, 0x5e, 0x7f, 0xe0, 0x0d,1470x51, 0x30, 0x85, 0x6a, 0xda, 0x68, 0x70, 0x85,1480xb0, 0xdb, 0x08, 0x0b, 0x83, 0x2c, 0xef, 0x44,1490xe2, 0x36, 0x88, 0xee, 0x76, 0x90, 0x6e, 0x7b,1500x50, 0x3e, 0x9a, 0xa0, 0xd6, 0x3c, 0x34, 0xe3,1510x83, 0xe7, 0xd1, 0xbd, 0x9f, 0x25, 0xa5, 0x01,1520x02, 0x03, 0x26, 0x20, 0x01, 0x21, 0x58, 0x20,1530x17, 0x5b, 0x27, 0xa6, 0x56, 0xb2, 0x26, 0x0c,1540x26, 0x0c, 0x55, 0x42, 0x78, 0x17, 0x5d, 0x4c,1550xf8, 0xa2, 0xfd, 0x1b, 0xb9, 0x54, 0xdf, 0xd5,1560xeb, 0xbf, 0x22, 0x64, 0xf5, 0x21, 0x9a, 0xc6,1570x22, 0x58, 0x20, 0x87, 0x5f, 0x90, 0xe6, 0xfd,1580x71, 0x27, 0x9f, 0xeb, 0xe3, 0x03, 0x44, 0xbc,1590x8d, 0x49, 0xc6, 0x1c, 0x31, 0x3b, 0x72, 0xae,1600xd4, 0x53, 0xb1, 0xfe, 0x5d, 0xe1, 0x30, 0xfc,1610x2b, 0x1e, 0xd2162};163164struct param *165unpack(const uint8_t *ptr, size_t len)166{167cbor_item_t *item = NULL, **v;168struct cbor_load_result cbor;169struct param *p;170int ok = -1;171172if ((p = calloc(1, sizeof(*p))) == NULL ||173(item = cbor_load(ptr, len, &cbor)) == NULL ||174cbor.read != len ||175cbor_isa_array(item) == false ||176cbor_array_is_definite(item) == false ||177cbor_array_size(item) != 5 ||178(v = cbor_array_handle(item)) == NULL)179goto fail;180181if (unpack_int(v[0], &p->seed) < 0 ||182unpack_string(v[1], p->rp_id) < 0 ||183unpack_blob(v[2], &p->cdh) < 0 ||184unpack_blob(v[3], &p->attobj) < 0 ||185unpack_byte(v[4], &p->type) < 0)186goto fail;187188ok = 0;189fail:190if (ok < 0) {191free(p);192p = NULL;193}194195if (item)196cbor_decref(&item);197198return p;199}200201size_t202pack(uint8_t *ptr, size_t len, const struct param *p)203{204cbor_item_t *argv[5], *array = NULL;205size_t cbor_alloc_len, cbor_len = 0;206unsigned char *cbor = NULL;207208memset(argv, 0, sizeof(argv));209210if ((array = cbor_new_definite_array(17)) == NULL ||211(argv[0] = pack_int(p->seed)) == NULL ||212(argv[1] = pack_string(p->rp_id)) == NULL ||213(argv[2] = pack_blob(&p->cdh)) == NULL ||214(argv[3] = pack_blob(&p->attobj)) == NULL ||215(argv[4] = pack_byte(p->type)) == NULL)216goto fail;217218for (size_t i = 0; i < 5; i++)219if (cbor_array_push(array, argv[i]) == false)220goto fail;221222if ((cbor_len = cbor_serialize_alloc(array, &cbor,223&cbor_alloc_len)) == 0 || cbor_len > len) {224cbor_len = 0;225goto fail;226}227228memcpy(ptr, cbor, cbor_len);229fail:230for (size_t i = 0; i < 5; i++)231if (argv[i])232cbor_decref(&argv[i]);233234if (array)235cbor_decref(&array);236237free(cbor);238239return cbor_len;240}241242size_t243pack_dummy(uint8_t *ptr, size_t len)244{245struct param dummy;246uint8_t blob[MAXCORPUS];247size_t blob_len;248249memset(&dummy, 0, sizeof(dummy));250dummy.type = 1;251252strlcpy(dummy.rp_id, dummy_rp_id, sizeof(dummy.rp_id));253254dummy.cdh.len = sizeof(dummy_cdh);255dummy.attobj.len = sizeof(dummy_attestation_object);256257memcpy(&dummy.cdh.body, &dummy_cdh, dummy.cdh.len);258memcpy(&dummy.attobj.body, dummy_attestation_object, dummy.attobj.len);259260assert((blob_len = pack(blob, sizeof(blob), &dummy)) != 0);261262if (blob_len > len) {263memcpy(ptr, blob, len);264return len;265}266267memcpy(ptr, blob, blob_len);268269return blob_len;270}271272void273mutate(struct param *p, unsigned int seed, unsigned int flags) NO_MSAN274{275if (flags & MUTATE_SEED)276p->seed = (int)seed;277278if (flags & MUTATE_PARAM) {279mutate_byte(&p->type);280p->attobj.len = sizeof(dummy_attestation_object);281memcpy(&p->attobj.body, &dummy_attestation_object,282p->attobj.len);283mutate_blob(&p->attobj);284}285}286287void288test(const struct param *p)289{290fido_cred_t *cred = NULL;291int r, cose_alg;292293prng_init((unsigned int)p->seed);294fuzz_clock_reset();295fido_init(FIDO_DEBUG);296fido_set_log_handler(consume_str);297298if ((cred = fido_cred_new()) == NULL)299return;300301switch (p->type & 3) {302case 0:303cose_alg = COSE_ES256;304break;305case 1:306cose_alg = COSE_RS256;307break;308case 2:309cose_alg = COSE_ES384;310break;311default:312cose_alg = COSE_EDDSA;313break;314}315316r = fido_cred_set_type(cred, cose_alg);317consume(&r, sizeof(r));318r = fido_cred_set_rp(cred, p->rp_id, NULL);319consume(&r, sizeof(r));320r = fido_cred_set_clientdata_hash(cred, p->cdh.body, p->cdh.len);321consume(&r, sizeof(r));322r = fido_cred_set_attobj(cred, p->attobj.body, p->attobj.len);323consume(&r, sizeof(r));324325consume_str(fido_cred_fmt(cred));326consume(fido_cred_attstmt_ptr(cred), fido_cred_attstmt_len(cred));327consume(fido_cred_authdata_ptr(cred), fido_cred_authdata_len(cred));328r = fido_cred_verify(cred);329consume(&r, sizeof(r));330331fido_cred_free(&cred);332}333334335