2
2
#include < stddef.h>
3
3
#include < string.h>
4
4
5
+ #include " fuzzer/FuzzedDataProvider.h"
6
+ #include < algorithm>
7
+
5
8
#include < openssl/ec.h>
6
9
#include < openssl/x509.h>
7
10
@@ -85,34 +88,16 @@ static EVP_PKEY *generate_keypair_openssl() {
85
88
return pkey;
86
89
}
87
90
88
- typedef struct {
89
- CK_ULONG attribute_count;
90
- CK_OBJECT_HANDLE obj_handle;
91
- uint8_t derived_ecdh_key_count;
92
- } test_case_t ;
93
-
94
91
void populate_attribute_template (CK_ATTRIBUTE_PTR *attribute_array,
95
92
CK_ULONG attribute_count,
96
- uint8_t **fuzzer_data,
97
- size_t *fuzzer_data_size) {
93
+ FuzzedDataProvider *fdp) {
98
94
CK_ATTRIBUTE_PTR new_array = new CK_ATTRIBUTE[attribute_count];
99
95
memset (new_array, 0 , sizeof (CK_ATTRIBUTE) * attribute_count);
100
96
101
97
for (int i = 0 ; i < attribute_count; i++) {
102
- unsigned long ulValueLen = 0 ;
103
-
104
- if (*fuzzer_data_size > 0 ) {
105
- ulValueLen = (*fuzzer_data)[0 ];
106
- *fuzzer_data += 1 ;
107
- *fuzzer_data_size -= 1 ;
108
- }
109
-
110
- if (*fuzzer_data_size >= sizeof (unsigned long )) {
111
- memcpy (&new_array[i].type , *fuzzer_data, sizeof (unsigned long ));
112
- *fuzzer_data += sizeof (unsigned long );
113
- *fuzzer_data_size -= sizeof (unsigned long );
114
- }
98
+ uint8_t ulValueLen = fdp->ConsumeIntegral <uint8_t >();
115
99
100
+ new_array[i].type = fdp->ConsumeIntegral <CK_ATTRIBUTE_TYPE>();
116
101
new_array[i].pValue = new uint8_t [ulValueLen]; // TODO populate pValue from
117
102
// fuzzer generated data?
118
103
new_array[i].ulValueLen = ulValueLen;
@@ -122,33 +107,22 @@ void populate_attribute_template(CK_ATTRIBUTE_PTR *attribute_array,
122
107
}
123
108
124
109
void populate_derived_ecdh_key_template (CK_ATTRIBUTE_PTR *attribute_array,
125
- uint8_t **fuzzer_data,
126
- size_t *fuzzer_data_size) {
127
-
110
+ FuzzedDataProvider *fdp) {
128
111
CK_ATTRIBUTE_PTR new_array = new CK_ATTRIBUTE[ECDH_ATTRIBUTE_COUNT];
129
112
memset (new_array, 0 , sizeof (CK_ATTRIBUTE) * ECDH_ATTRIBUTE_COUNT);
130
113
131
- uint8_t value_len = 0 ;
132
- uint8_t label_len = 0 ;
133
- if (*fuzzer_data_size > 0 ) {
134
- value_len = (*fuzzer_data)[0 ];
135
- *fuzzer_data += 1 ;
136
- *fuzzer_data_size -= 1 ;
137
- }
138
- if (*fuzzer_data_size > 0 ) {
139
- label_len = (*fuzzer_data)[0 ];
140
- *fuzzer_data += 1 ;
141
- *fuzzer_data_size -= 1 ;
142
- }
114
+ uint8_t value_len = fdp->ConsumeIntegral <uint8_t >();
115
+ std::vector<uint8_t > value = fdp->ConsumeBytes <uint8_t >(value_len);
143
116
144
117
new_array[0 ].type = CKA_VALUE_LEN;
145
118
new_array[0 ].ulValueLen = value_len;
146
119
new_array[0 ].pValue = new uint8_t [value_len];
147
- if (*fuzzer_data_size < value_len) {
148
- value_len = *fuzzer_data_size;
149
- }
150
- memcpy (new_array[0 ].pValue , *fuzzer_data, value_len);
151
- *fuzzer_data_size -= value_len;
120
+
121
+ memset (new_array[0 ].pValue , 0 , value_len);
122
+ memcpy (new_array[0 ].pValue , &value[0 ],
123
+ std::min (value.size (), (size_t ) value_len));
124
+
125
+ uint8_t label_len = fdp->ConsumeIntegral <uint8_t >();
152
126
153
127
new_array[1 ].type = CKA_LABEL;
154
128
new_array[1 ].ulValueLen = label_len;
@@ -201,16 +175,25 @@ void free_attribute_template(CK_ATTRIBUTE_PTR attribute_array,
201
175
}
202
176
203
177
extern " C" int LLVMFuzzerTestOneInput (uint8_t *data, size_t size) {
178
+ typedef struct {
179
+ CK_ULONG attribute_count;
180
+ CK_OBJECT_HANDLE obj_handle;
181
+ uint8_t derived_ecdh_key_count;
182
+ } test_case_t ;
183
+
204
184
static bool p11_initialized = init_p11 ();
205
185
206
186
if (size < sizeof (test_case_t )) {
207
187
return 0 ;
208
188
}
209
189
190
+ FuzzedDataProvider *fdp = new FuzzedDataProvider (data, size);
191
+
210
192
test_case_t test_case;
211
- memcpy (&test_case, data, sizeof (test_case_t ));
212
- data += sizeof (test_case_t );
213
- size -= sizeof (test_case_t );
193
+ memset (&test_case, 0 , sizeof (test_case_t ));
194
+ test_case.attribute_count = fdp->ConsumeIntegral <CK_ULONG>();
195
+ test_case.obj_handle = fdp->ConsumeIntegral <CK_OBJECT_HANDLE>();
196
+ test_case.derived_ecdh_key_count = fdp->ConsumeIntegral <uint8_t >();
214
197
215
198
/* limit the number of request attributes to 10
216
199
* this is an artificial limitation to make fuzzer iterations faster
@@ -220,14 +203,14 @@ extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {
220
203
}
221
204
222
205
CK_ATTRIBUTE_PTR attribute_array;
223
- populate_attribute_template (&attribute_array, test_case.attribute_count ,
224
- &data, &size);
225
206
CK_ATTRIBUTE_PTR ecdh_attribute_array;
226
- populate_derived_ecdh_key_template (&ecdh_attribute_array, &data, &size);
207
+ populate_attribute_template (&attribute_array, test_case.attribute_count , fdp);
208
+ populate_derived_ecdh_key_template (&ecdh_attribute_array, fdp);
227
209
228
210
// the rest of the data is used for responses sent back by the backend
229
- backend_data = data;
230
- backend_data_len = size;
211
+ std::vector<uint8_t > backend_vector = fdp->ConsumeRemainingBytes <uint8_t >();
212
+ backend_data = &backend_vector[0 ];
213
+ backend_data_len = backend_vector.size ();
231
214
232
215
init_session ();
233
216
@@ -245,6 +228,8 @@ extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {
245
228
free_attribute_template (attribute_array, test_case.attribute_count );
246
229
free_attribute_template (ecdh_attribute_array, ECDH_ATTRIBUTE_COUNT);
247
230
231
+ delete fdp;
232
+
248
233
fflush (stdout);
249
234
return 0 ;
250
235
}
0 commit comments