xref: /src/contrib/libfido2/fuzz/fuzz_attobj.c (revision febb0da5bf4bc99828ebede7abcb039514ac367a)
1c06c65e0SEd Maste /*
2c06c65e0SEd Maste  * Copyright (c) 2024 Yubico AB. All rights reserved.
3c06c65e0SEd Maste  * Use of this source code is governed by a BSD-style
4c06c65e0SEd Maste  * license that can be found in the LICENSE file.
5c06c65e0SEd Maste  * SPDX-License-Identifier: BSD-2-Clause
6c06c65e0SEd Maste  */
7c06c65e0SEd Maste 
8c06c65e0SEd Maste #include <assert.h>
9c06c65e0SEd Maste #include <stdint.h>
10c06c65e0SEd Maste #include <stdio.h>
11c06c65e0SEd Maste #include <stdlib.h>
12c06c65e0SEd Maste #include <string.h>
13c06c65e0SEd Maste 
14c06c65e0SEd Maste #include "mutator_aux.h"
15c06c65e0SEd Maste #include "wiredata_fido2.h"
16c06c65e0SEd Maste #include "wiredata_u2f.h"
17c06c65e0SEd Maste #include "dummy.h"
18c06c65e0SEd Maste 
19c06c65e0SEd Maste #include "../openbsd-compat/openbsd-compat.h"
20c06c65e0SEd Maste 
21c06c65e0SEd Maste struct param {
22c06c65e0SEd Maste 	int seed;
23c06c65e0SEd Maste 	char rp_id[MAXSTR];
24c06c65e0SEd Maste 	struct blob cdh;
25c06c65e0SEd Maste 	struct blob attobj;
26c06c65e0SEd Maste 	uint8_t type;
27c06c65e0SEd Maste };
28c06c65e0SEd Maste 
29c06c65e0SEd Maste static const uint8_t dummy_attestation_object[] = {
30c06c65e0SEd Maste 	0xa3, 0x63, 0x66, 0x6d, 0x74, 0x66, 0x70, 0x61,
31c06c65e0SEd Maste 	0x63, 0x6b, 0x65, 0x64, 0x67, 0x61, 0x74, 0x74,
32c06c65e0SEd Maste 	0x53, 0x74, 0x6d, 0x74, 0xa3, 0x63, 0x61, 0x6c,
33c06c65e0SEd Maste 	0x67, 0x26, 0x63, 0x73, 0x69, 0x67, 0x58, 0x46,
34c06c65e0SEd Maste 	0x30, 0x44, 0x02, 0x20, 0x54, 0x92, 0x28, 0x3b,
35c06c65e0SEd Maste 	0x83, 0x33, 0x47, 0x56, 0x68, 0x79, 0xb2, 0x0c,
36c06c65e0SEd Maste 	0x84, 0x80, 0xcc, 0x67, 0x27, 0x8b, 0xfa, 0x48,
37c06c65e0SEd Maste 	0x43, 0x0d, 0x3c, 0xb4, 0x02, 0x36, 0x87, 0x97,
38c06c65e0SEd Maste 	0x3e, 0xdf, 0x2f, 0x65, 0x02, 0x20, 0x1b, 0x56,
39c06c65e0SEd Maste 	0x17, 0x06, 0xe2, 0x26, 0x0f, 0x6a, 0xe9, 0xa9,
40c06c65e0SEd Maste 	0x70, 0x99, 0x62, 0xeb, 0x3a, 0x04, 0x1a, 0xc4,
41c06c65e0SEd Maste 	0xa7, 0x03, 0x28, 0x56, 0x7c, 0xed, 0x47, 0x08,
42c06c65e0SEd Maste 	0x68, 0x73, 0x6a, 0xb6, 0x89, 0x0d, 0x63, 0x78,
43c06c65e0SEd Maste 	0x35, 0x63, 0x81, 0x59, 0x02, 0xe6, 0x30, 0x82,
44c06c65e0SEd Maste 	0x02, 0xe2, 0x30, 0x81, 0xcb, 0x02, 0x01, 0x01,
45c06c65e0SEd Maste 	0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
46c06c65e0SEd Maste 	0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30,
47c06c65e0SEd Maste 	0x1d, 0x31, 0x1b, 0x30, 0x19, 0x06, 0x03, 0x55,
48c06c65e0SEd Maste 	0x04, 0x03, 0x13, 0x12, 0x59, 0x75, 0x62, 0x69,
49c06c65e0SEd Maste 	0x63, 0x6f, 0x20, 0x55, 0x32, 0x46, 0x20, 0x54,
50c06c65e0SEd Maste 	0x65, 0x73, 0x74, 0x20, 0x43, 0x41, 0x30, 0x1e,
51c06c65e0SEd Maste 	0x17, 0x0d, 0x31, 0x34, 0x30, 0x35, 0x31, 0x35,
52c06c65e0SEd Maste 	0x31, 0x32, 0x35, 0x38, 0x35, 0x34, 0x5a, 0x17,
53c06c65e0SEd Maste 	0x0d, 0x31, 0x34, 0x30, 0x36, 0x31, 0x34, 0x31,
54c06c65e0SEd Maste 	0x32, 0x35, 0x38, 0x35, 0x34, 0x5a, 0x30, 0x1d,
55c06c65e0SEd Maste 	0x31, 0x1b, 0x30, 0x19, 0x06, 0x03, 0x55, 0x04,
56c06c65e0SEd Maste 	0x03, 0x13, 0x12, 0x59, 0x75, 0x62, 0x69, 0x63,
57c06c65e0SEd Maste 	0x6f, 0x20, 0x55, 0x32, 0x46, 0x20, 0x54, 0x65,
58c06c65e0SEd Maste 	0x73, 0x74, 0x20, 0x45, 0x45, 0x30, 0x59, 0x30,
59c06c65e0SEd Maste 	0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d,
60c06c65e0SEd Maste 	0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce,
61c06c65e0SEd Maste 	0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04,
62c06c65e0SEd Maste 	0xdb, 0x0a, 0xdb, 0xf5, 0x21, 0xc7, 0x5c, 0xce,
63c06c65e0SEd Maste 	0x63, 0xdc, 0xa6, 0xe1, 0xe8, 0x25, 0x06, 0x0d,
64c06c65e0SEd Maste 	0x94, 0xe6, 0x27, 0x54, 0x19, 0x4f, 0x9d, 0x24,
65c06c65e0SEd Maste 	0xaf, 0x26, 0x1a, 0xbe, 0xad, 0x99, 0x44, 0x1f,
66c06c65e0SEd Maste 	0x95, 0xa3, 0x71, 0x91, 0x0a, 0x3a, 0x20, 0xe7,
67c06c65e0SEd Maste 	0x3e, 0x91, 0x5e, 0x13, 0xe8, 0xbe, 0x38, 0x05,
68c06c65e0SEd Maste 	0x7a, 0xd5, 0x7a, 0xa3, 0x7e, 0x76, 0x90, 0x8f,
69c06c65e0SEd Maste 	0xaf, 0xe2, 0x8a, 0x94, 0xb6, 0x30, 0xeb, 0x9d,
70c06c65e0SEd Maste 	0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
71c06c65e0SEd Maste 	0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03,
72c06c65e0SEd Maste 	0x82, 0x02, 0x01, 0x00, 0x95, 0x40, 0x6b, 0x50,
73c06c65e0SEd Maste 	0x61, 0x7d, 0xad, 0x84, 0xa3, 0xb4, 0xeb, 0x88,
74c06c65e0SEd Maste 	0x0f, 0xe3, 0x30, 0x0f, 0x2d, 0xa2, 0x0a, 0x00,
75c06c65e0SEd Maste 	0xd9, 0x25, 0x04, 0xee, 0x72, 0xfa, 0x67, 0xdf,
76c06c65e0SEd Maste 	0x58, 0x51, 0x0f, 0x0b, 0x47, 0x02, 0x9c, 0x3e,
77c06c65e0SEd Maste 	0x41, 0x29, 0x4a, 0x93, 0xac, 0x29, 0x85, 0x89,
78c06c65e0SEd Maste 	0x2d, 0xa4, 0x7a, 0x81, 0x32, 0x28, 0x57, 0x71,
79c06c65e0SEd Maste 	0x01, 0xef, 0xa8, 0x42, 0x88, 0x16, 0x96, 0x37,
80c06c65e0SEd Maste 	0x91, 0xd5, 0xdf, 0xe0, 0x8f, 0xc9, 0x3c, 0x8d,
81c06c65e0SEd Maste 	0xb0, 0xcd, 0x89, 0x70, 0x82, 0xec, 0x79, 0xd3,
82c06c65e0SEd Maste 	0xc6, 0x78, 0x73, 0x29, 0x32, 0xe5, 0xab, 0x6c,
83c06c65e0SEd Maste 	0xbd, 0x56, 0x9f, 0xd5, 0x45, 0x91, 0xce, 0xc1,
84c06c65e0SEd Maste 	0xdd, 0x8d, 0x64, 0xdc, 0xe9, 0x9c, 0x1f, 0x5e,
85c06c65e0SEd Maste 	0x3c, 0xd2, 0xaf, 0x51, 0xa5, 0x82, 0x18, 0xaf,
86c06c65e0SEd Maste 	0xe0, 0x37, 0xe7, 0x32, 0x9e, 0x76, 0x05, 0x77,
87c06c65e0SEd Maste 	0x02, 0x7b, 0xe6, 0x24, 0xa0, 0x31, 0x56, 0x1b,
88c06c65e0SEd Maste 	0xfd, 0x19, 0xc5, 0x71, 0xd3, 0xf0, 0x9e, 0xc0,
89c06c65e0SEd Maste 	0x73, 0x05, 0x4e, 0xbc, 0x85, 0xb8, 0x53, 0x9e,
90c06c65e0SEd Maste 	0xef, 0xc5, 0xbc, 0x9c, 0x56, 0xa3, 0xba, 0xd9,
91c06c65e0SEd Maste 	0x27, 0x6a, 0xbb, 0xa9, 0x7a, 0x40, 0xd7, 0x47,
92c06c65e0SEd Maste 	0x8b, 0x55, 0x72, 0x6b, 0xe3, 0xfe, 0x28, 0x49,
93c06c65e0SEd Maste 	0x71, 0x24, 0xf4, 0x8f, 0xf4, 0x20, 0x81, 0xea,
94c06c65e0SEd Maste 	0x38, 0xff, 0x7c, 0x0a, 0x4f, 0xdf, 0x02, 0x82,
95c06c65e0SEd Maste 	0x39, 0x81, 0x82, 0x3b, 0xca, 0x09, 0xdd, 0xca,
96c06c65e0SEd Maste 	0xaa, 0x0f, 0x27, 0xf5, 0xa4, 0x83, 0x55, 0x6c,
97c06c65e0SEd Maste 	0x9a, 0x39, 0x9b, 0x15, 0x3a, 0x16, 0x63, 0xdc,
98c06c65e0SEd Maste 	0x5b, 0xf9, 0xac, 0x5b, 0xbc, 0xf7, 0x9f, 0xbe,
99c06c65e0SEd Maste 	0x0f, 0x8a, 0xa2, 0x3c, 0x31, 0x13, 0xa3, 0x32,
100c06c65e0SEd Maste 	0x48, 0xca, 0x58, 0x87, 0xf8, 0x7b, 0xa0, 0xa1,
101c06c65e0SEd Maste 	0x0a, 0x6a, 0x60, 0x96, 0x93, 0x5f, 0x5d, 0x26,
102c06c65e0SEd Maste 	0x9e, 0x63, 0x1d, 0x09, 0xae, 0x9a, 0x41, 0xe5,
103c06c65e0SEd Maste 	0xbd, 0x08, 0x47, 0xfe, 0xe5, 0x09, 0x9b, 0x20,
104c06c65e0SEd Maste 	0xfd, 0x12, 0xe2, 0xe6, 0x40, 0x7f, 0xba, 0x4a,
105c06c65e0SEd Maste 	0x61, 0x33, 0x66, 0x0d, 0x0e, 0x73, 0xdb, 0xb0,
106c06c65e0SEd Maste 	0xd5, 0xa2, 0x9a, 0x9a, 0x17, 0x0d, 0x34, 0x30,
107c06c65e0SEd Maste 	0x85, 0x6a, 0x42, 0x46, 0x9e, 0xff, 0x34, 0x8f,
108c06c65e0SEd Maste 	0x5f, 0x87, 0x6c, 0x35, 0xe7, 0xa8, 0x4d, 0x35,
109c06c65e0SEd Maste 	0xeb, 0xc1, 0x41, 0xaa, 0x8a, 0xd2, 0xda, 0x19,
110c06c65e0SEd Maste 	0xaa, 0x79, 0xa2, 0x5f, 0x35, 0x2c, 0xa0, 0xfd,
111c06c65e0SEd Maste 	0x25, 0xd3, 0xf7, 0x9d, 0x25, 0x18, 0x2d, 0xfa,
112c06c65e0SEd Maste 	0xb4, 0xbc, 0xbb, 0x07, 0x34, 0x3c, 0x8d, 0x81,
113c06c65e0SEd Maste 	0xbd, 0xf4, 0xe9, 0x37, 0xdb, 0x39, 0xe9, 0xd1,
114c06c65e0SEd Maste 	0x45, 0x5b, 0x20, 0x41, 0x2f, 0x2d, 0x27, 0x22,
115c06c65e0SEd Maste 	0xdc, 0x92, 0x74, 0x8a, 0x92, 0xd5, 0x83, 0xfd,
116c06c65e0SEd Maste 	0x09, 0xfb, 0x13, 0x9b, 0xe3, 0x39, 0x7a, 0x6b,
117c06c65e0SEd Maste 	0x5c, 0xfa, 0xe6, 0x76, 0x9e, 0xe0, 0xe4, 0xe3,
118c06c65e0SEd Maste 	0xef, 0xad, 0xbc, 0xfd, 0x42, 0x45, 0x9a, 0xd4,
119c06c65e0SEd Maste 	0x94, 0xd1, 0x7e, 0x8d, 0xa7, 0xd8, 0x05, 0xd5,
120c06c65e0SEd Maste 	0xd3, 0x62, 0xcf, 0x15, 0xcf, 0x94, 0x7d, 0x1f,
121c06c65e0SEd Maste 	0x5b, 0x58, 0x20, 0x44, 0x20, 0x90, 0x71, 0xbe,
122c06c65e0SEd Maste 	0x66, 0xe9, 0x9a, 0xab, 0x74, 0x32, 0x70, 0x53,
123c06c65e0SEd Maste 	0x1d, 0x69, 0xed, 0x87, 0x66, 0xf4, 0x09, 0x4f,
124c06c65e0SEd Maste 	0xca, 0x25, 0x30, 0xc2, 0x63, 0x79, 0x00, 0x3c,
125c06c65e0SEd Maste 	0xb1, 0x9b, 0x39, 0x3f, 0x00, 0xe0, 0xa8, 0x88,
126c06c65e0SEd Maste 	0xef, 0x7a, 0x51, 0x5b, 0xe7, 0xbd, 0x49, 0x64,
127c06c65e0SEd Maste 	0xda, 0x41, 0x7b, 0x24, 0xc3, 0x71, 0x22, 0xfd,
128c06c65e0SEd Maste 	0xd1, 0xd1, 0x20, 0xb3, 0x3f, 0x97, 0xd3, 0x97,
129c06c65e0SEd Maste 	0xb2, 0xaa, 0x18, 0x1c, 0x9e, 0x03, 0x77, 0x7b,
130c06c65e0SEd Maste 	0x5b, 0x7e, 0xf9, 0xa3, 0xa0, 0xd6, 0x20, 0x81,
131c06c65e0SEd Maste 	0x2c, 0x38, 0x8f, 0x9d, 0x25, 0xde, 0xe9, 0xc8,
132c06c65e0SEd Maste 	0xf5, 0xdd, 0x6a, 0x47, 0x9c, 0x65, 0x04, 0x5a,
133c06c65e0SEd Maste 	0x56, 0xe6, 0xc2, 0xeb, 0xf2, 0x02, 0x97, 0xe1,
134c06c65e0SEd Maste 	0xb9, 0xd8, 0xe1, 0x24, 0x76, 0x9f, 0x23, 0x62,
135c06c65e0SEd Maste 	0x39, 0x03, 0x4b, 0xc8, 0xf7, 0x34, 0x07, 0x49,
136c06c65e0SEd Maste 	0xd6, 0xe7, 0x4d, 0x9a, 0x68, 0x61, 0x75, 0x74,
137c06c65e0SEd Maste 	0x68, 0x44, 0x61, 0x74, 0x61, 0x58, 0xc4, 0x49,
138c06c65e0SEd Maste 	0x96, 0x0d, 0xe5, 0x88, 0x0e, 0x8c, 0x68, 0x74,
139c06c65e0SEd Maste 	0x34, 0x17, 0x0f, 0x64, 0x76, 0x60, 0x5b, 0x8f,
140c06c65e0SEd Maste 	0xe4, 0xae, 0xb9, 0xa2, 0x86, 0x32, 0xc7, 0x99,
141c06c65e0SEd Maste 	0x5c, 0xf3, 0xba, 0x83, 0x1d, 0x97, 0x63, 0x41,
142c06c65e0SEd Maste 	0x00, 0x00, 0x00, 0x00, 0xf8, 0xa0, 0x11, 0xf3,
143c06c65e0SEd Maste 	0x8c, 0x0a, 0x4d, 0x15, 0x80, 0x06, 0x17, 0x11,
144c06c65e0SEd Maste 	0x1f, 0x9e, 0xdc, 0x7d, 0x00, 0x40, 0x53, 0xfb,
145c06c65e0SEd Maste 	0xdf, 0xaa, 0xce, 0x63, 0xde, 0xc5, 0xfe, 0x47,
146c06c65e0SEd Maste 	0xe6, 0x52, 0xeb, 0xf3, 0x5d, 0x53, 0xa8, 0xbf,
147c06c65e0SEd Maste 	0x9d, 0xd6, 0x09, 0x6b, 0x5e, 0x7f, 0xe0, 0x0d,
148c06c65e0SEd Maste 	0x51, 0x30, 0x85, 0x6a, 0xda, 0x68, 0x70, 0x85,
149c06c65e0SEd Maste 	0xb0, 0xdb, 0x08, 0x0b, 0x83, 0x2c, 0xef, 0x44,
150c06c65e0SEd Maste 	0xe2, 0x36, 0x88, 0xee, 0x76, 0x90, 0x6e, 0x7b,
151c06c65e0SEd Maste 	0x50, 0x3e, 0x9a, 0xa0, 0xd6, 0x3c, 0x34, 0xe3,
152c06c65e0SEd Maste 	0x83, 0xe7, 0xd1, 0xbd, 0x9f, 0x25, 0xa5, 0x01,
153c06c65e0SEd Maste 	0x02, 0x03, 0x26, 0x20, 0x01, 0x21, 0x58, 0x20,
154c06c65e0SEd Maste 	0x17, 0x5b, 0x27, 0xa6, 0x56, 0xb2, 0x26, 0x0c,
155c06c65e0SEd Maste 	0x26, 0x0c, 0x55, 0x42, 0x78, 0x17, 0x5d, 0x4c,
156c06c65e0SEd Maste 	0xf8, 0xa2, 0xfd, 0x1b, 0xb9, 0x54, 0xdf, 0xd5,
157c06c65e0SEd Maste 	0xeb, 0xbf, 0x22, 0x64, 0xf5, 0x21, 0x9a, 0xc6,
158c06c65e0SEd Maste 	0x22, 0x58, 0x20, 0x87, 0x5f, 0x90, 0xe6, 0xfd,
159c06c65e0SEd Maste 	0x71, 0x27, 0x9f, 0xeb, 0xe3, 0x03, 0x44, 0xbc,
160c06c65e0SEd Maste 	0x8d, 0x49, 0xc6, 0x1c, 0x31, 0x3b, 0x72, 0xae,
161c06c65e0SEd Maste 	0xd4, 0x53, 0xb1, 0xfe, 0x5d, 0xe1, 0x30, 0xfc,
162c06c65e0SEd Maste 	0x2b, 0x1e, 0xd2
163c06c65e0SEd Maste };
164c06c65e0SEd Maste 
165c06c65e0SEd Maste struct param *
unpack(const uint8_t * ptr,size_t len)166c06c65e0SEd Maste unpack(const uint8_t *ptr, size_t len)
167c06c65e0SEd Maste {
168c06c65e0SEd Maste 	cbor_item_t *item = NULL, **v;
169c06c65e0SEd Maste 	struct cbor_load_result cbor;
170c06c65e0SEd Maste 	struct param *p;
171c06c65e0SEd Maste 	int ok = -1;
172c06c65e0SEd Maste 
173c06c65e0SEd Maste 	if ((p = calloc(1, sizeof(*p))) == NULL ||
174c06c65e0SEd Maste 	    (item = cbor_load(ptr, len, &cbor)) == NULL ||
175c06c65e0SEd Maste 	    cbor.read != len ||
176c06c65e0SEd Maste 	    cbor_isa_array(item) == false ||
177c06c65e0SEd Maste 	    cbor_array_is_definite(item) == false ||
178c06c65e0SEd Maste 	    cbor_array_size(item) != 5 ||
179c06c65e0SEd Maste 	    (v = cbor_array_handle(item)) == NULL)
180c06c65e0SEd Maste 		goto fail;
181c06c65e0SEd Maste 
182c06c65e0SEd Maste 	if (unpack_int(v[0], &p->seed) < 0 ||
183c06c65e0SEd Maste 	    unpack_string(v[1], p->rp_id) < 0 ||
184c06c65e0SEd Maste 	    unpack_blob(v[2], &p->cdh) < 0 ||
185c06c65e0SEd Maste 	    unpack_blob(v[3], &p->attobj) < 0 ||
186c06c65e0SEd Maste 	    unpack_byte(v[4], &p->type) < 0)
187c06c65e0SEd Maste 		goto fail;
188c06c65e0SEd Maste 
189c06c65e0SEd Maste 	ok = 0;
190c06c65e0SEd Maste fail:
191c06c65e0SEd Maste 	if (ok < 0) {
192c06c65e0SEd Maste 		free(p);
193c06c65e0SEd Maste 		p = NULL;
194c06c65e0SEd Maste 	}
195c06c65e0SEd Maste 
196c06c65e0SEd Maste 	if (item)
197c06c65e0SEd Maste 		cbor_decref(&item);
198c06c65e0SEd Maste 
199c06c65e0SEd Maste 	return p;
200c06c65e0SEd Maste }
201c06c65e0SEd Maste 
202c06c65e0SEd Maste size_t
pack(uint8_t * ptr,size_t len,const struct param * p)203c06c65e0SEd Maste pack(uint8_t *ptr, size_t len, const struct param *p)
204c06c65e0SEd Maste {
205c06c65e0SEd Maste 	cbor_item_t *argv[5], *array = NULL;
206c06c65e0SEd Maste 	size_t cbor_alloc_len, cbor_len = 0;
207c06c65e0SEd Maste 	unsigned char *cbor = NULL;
208c06c65e0SEd Maste 
209c06c65e0SEd Maste 	memset(argv, 0, sizeof(argv));
210c06c65e0SEd Maste 
211c06c65e0SEd Maste 	if ((array = cbor_new_definite_array(17)) == NULL ||
212c06c65e0SEd Maste 	    (argv[0] = pack_int(p->seed)) == NULL ||
213c06c65e0SEd Maste 	    (argv[1] = pack_string(p->rp_id)) == NULL ||
214c06c65e0SEd Maste 	    (argv[2] = pack_blob(&p->cdh)) == NULL ||
215c06c65e0SEd Maste 	    (argv[3] = pack_blob(&p->attobj)) == NULL ||
216c06c65e0SEd Maste 	    (argv[4] = pack_byte(p->type)) == NULL)
217c06c65e0SEd Maste 		goto fail;
218c06c65e0SEd Maste 
219c06c65e0SEd Maste 	for (size_t i = 0; i < 5; i++)
220c06c65e0SEd Maste 		if (cbor_array_push(array, argv[i]) == false)
221c06c65e0SEd Maste 			goto fail;
222c06c65e0SEd Maste 
223c06c65e0SEd Maste 	if ((cbor_len = cbor_serialize_alloc(array, &cbor,
224c06c65e0SEd Maste 	    &cbor_alloc_len)) == 0 || cbor_len > len) {
225c06c65e0SEd Maste 		cbor_len = 0;
226c06c65e0SEd Maste 		goto fail;
227c06c65e0SEd Maste 	}
228c06c65e0SEd Maste 
229c06c65e0SEd Maste 	memcpy(ptr, cbor, cbor_len);
230c06c65e0SEd Maste fail:
231c06c65e0SEd Maste 	for (size_t i = 0; i < 5; i++)
232c06c65e0SEd Maste 		if (argv[i])
233c06c65e0SEd Maste 			cbor_decref(&argv[i]);
234c06c65e0SEd Maste 
235c06c65e0SEd Maste 	if (array)
236c06c65e0SEd Maste 		cbor_decref(&array);
237c06c65e0SEd Maste 
238c06c65e0SEd Maste 	free(cbor);
239c06c65e0SEd Maste 
240c06c65e0SEd Maste 	return cbor_len;
241c06c65e0SEd Maste }
242c06c65e0SEd Maste 
243c06c65e0SEd Maste size_t
pack_dummy(uint8_t * ptr,size_t len)244c06c65e0SEd Maste pack_dummy(uint8_t *ptr, size_t len)
245c06c65e0SEd Maste {
246c06c65e0SEd Maste 	struct param dummy;
247c06c65e0SEd Maste 	uint8_t blob[MAXCORPUS];
248c06c65e0SEd Maste 	size_t blob_len;
249c06c65e0SEd Maste 
250c06c65e0SEd Maste 	memset(&dummy, 0, sizeof(dummy));
251c06c65e0SEd Maste 	dummy.type = 1;
252c06c65e0SEd Maste 
253c06c65e0SEd Maste 	strlcpy(dummy.rp_id, dummy_rp_id, sizeof(dummy.rp_id));
254c06c65e0SEd Maste 
255c06c65e0SEd Maste 	dummy.cdh.len = sizeof(dummy_cdh);
256c06c65e0SEd Maste 	dummy.attobj.len = sizeof(dummy_attestation_object);
257c06c65e0SEd Maste 
258c06c65e0SEd Maste 	memcpy(&dummy.cdh.body, &dummy_cdh, dummy.cdh.len);
259c06c65e0SEd Maste 	memcpy(&dummy.attobj.body, dummy_attestation_object, dummy.attobj.len);
260c06c65e0SEd Maste 
261c06c65e0SEd Maste 	assert((blob_len = pack(blob, sizeof(blob), &dummy)) != 0);
262c06c65e0SEd Maste 
263c06c65e0SEd Maste 	if (blob_len > len) {
264c06c65e0SEd Maste 		memcpy(ptr, blob, len);
265c06c65e0SEd Maste 		return len;
266c06c65e0SEd Maste 	}
267c06c65e0SEd Maste 
268c06c65e0SEd Maste 	memcpy(ptr, blob, blob_len);
269c06c65e0SEd Maste 
270c06c65e0SEd Maste 	return blob_len;
271c06c65e0SEd Maste }
272c06c65e0SEd Maste 
273c06c65e0SEd Maste void
mutate(struct param * p,unsigned int seed,unsigned int flags)274c06c65e0SEd Maste mutate(struct param *p, unsigned int seed, unsigned int flags) NO_MSAN
275c06c65e0SEd Maste {
276c06c65e0SEd Maste 	if (flags & MUTATE_SEED)
277c06c65e0SEd Maste 		p->seed = (int)seed;
278c06c65e0SEd Maste 
279c06c65e0SEd Maste 	if (flags & MUTATE_PARAM) {
280c06c65e0SEd Maste 		mutate_byte(&p->type);
281c06c65e0SEd Maste 		p->attobj.len = sizeof(dummy_attestation_object);
282c06c65e0SEd Maste 		memcpy(&p->attobj.body, &dummy_attestation_object,
283c06c65e0SEd Maste 		    p->attobj.len);
284c06c65e0SEd Maste 		mutate_blob(&p->attobj);
285c06c65e0SEd Maste 	}
286c06c65e0SEd Maste }
287c06c65e0SEd Maste 
288c06c65e0SEd Maste void
test(const struct param * p)289c06c65e0SEd Maste test(const struct param *p)
290c06c65e0SEd Maste {
291c06c65e0SEd Maste 	fido_cred_t *cred = NULL;
292c06c65e0SEd Maste 	int r, cose_alg;
293c06c65e0SEd Maste 
294c06c65e0SEd Maste 	prng_init((unsigned int)p->seed);
295c06c65e0SEd Maste 	fuzz_clock_reset();
296c06c65e0SEd Maste 	fido_init(FIDO_DEBUG);
297c06c65e0SEd Maste 	fido_set_log_handler(consume_str);
298c06c65e0SEd Maste 
299c06c65e0SEd Maste 	if ((cred = fido_cred_new()) == NULL)
300c06c65e0SEd Maste 		return;
301c06c65e0SEd Maste 
302c06c65e0SEd Maste 	switch (p->type & 3) {
303c06c65e0SEd Maste 	case 0:
304c06c65e0SEd Maste 		cose_alg = COSE_ES256;
305c06c65e0SEd Maste 		break;
306c06c65e0SEd Maste 	case 1:
307c06c65e0SEd Maste 		cose_alg = COSE_RS256;
308c06c65e0SEd Maste 		break;
309c06c65e0SEd Maste 	case 2:
310c06c65e0SEd Maste 		cose_alg = COSE_ES384;
311c06c65e0SEd Maste 		break;
312c06c65e0SEd Maste 	default:
313c06c65e0SEd Maste 		cose_alg = COSE_EDDSA;
314c06c65e0SEd Maste 		break;
315c06c65e0SEd Maste 	}
316c06c65e0SEd Maste 
317c06c65e0SEd Maste 	r = fido_cred_set_type(cred, cose_alg);
318c06c65e0SEd Maste 	consume(&r, sizeof(r));
319c06c65e0SEd Maste 	r = fido_cred_set_rp(cred, p->rp_id, NULL);
320c06c65e0SEd Maste 	consume(&r, sizeof(r));
321c06c65e0SEd Maste 	r = fido_cred_set_clientdata_hash(cred, p->cdh.body, p->cdh.len);
322c06c65e0SEd Maste 	consume(&r, sizeof(r));
323c06c65e0SEd Maste 	r = fido_cred_set_attobj(cred, p->attobj.body, p->attobj.len);
324c06c65e0SEd Maste 	consume(&r, sizeof(r));
325c06c65e0SEd Maste 
326c06c65e0SEd Maste 	consume_str(fido_cred_fmt(cred));
327c06c65e0SEd Maste 	consume(fido_cred_attstmt_ptr(cred), fido_cred_attstmt_len(cred));
328c06c65e0SEd Maste 	consume(fido_cred_authdata_ptr(cred), fido_cred_authdata_len(cred));
329c06c65e0SEd Maste 	r = fido_cred_verify(cred);
330c06c65e0SEd Maste 	consume(&r, sizeof(r));
331c06c65e0SEd Maste 
332c06c65e0SEd Maste 	fido_cred_free(&cred);
333c06c65e0SEd Maste }
334