VirtualBox

source: vbox/trunk/src/libs/openssl-1.1.1l/fuzz/asn1.c@ 91772

Last change on this file since 91772 was 91772, checked in by vboxsync, 3 years ago

openssl-1.1.1l: Applied and adjusted our OpenSSL changes to 1.1.1l. bugref:10126

File size: 10.7 KB
Line 
1/*
2 * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL licenses, (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.openssl.org/source/license.html
8 * or in the file LICENSE in the source distribution.
9 */
10
11/*
12 * Fuzz ASN.1 parsing for various data structures. Specify which on the
13 * command line:
14 *
15 * asn1 <data structure>
16 */
17
18#include <stdio.h>
19#include <string.h>
20#include <openssl/asn1.h>
21#include <openssl/asn1t.h>
22#include <openssl/dh.h>
23#include <openssl/dsa.h>
24#include <openssl/ec.h>
25#include <openssl/ocsp.h>
26#include <openssl/pkcs12.h>
27#include <openssl/rsa.h>
28#include <openssl/ts.h>
29#include <openssl/x509v3.h>
30#include <openssl/cms.h>
31#include <openssl/err.h>
32#include <openssl/rand.h>
33#include <openssl/bio.h>
34#include <openssl/evp.h>
35#include <openssl/ssl.h>
36#include "fuzzer.h"
37
38#include "rand.inc"
39
40static ASN1_ITEM_EXP *item_type[] = {
41 ASN1_ITEM_ref(ACCESS_DESCRIPTION),
42#ifndef OPENSSL_NO_RFC3779
43 ASN1_ITEM_ref(ASIdentifierChoice),
44 ASN1_ITEM_ref(ASIdentifiers),
45 ASN1_ITEM_ref(ASIdOrRange),
46#endif
47 ASN1_ITEM_ref(ASN1_ANY),
48 ASN1_ITEM_ref(ASN1_BIT_STRING),
49 ASN1_ITEM_ref(ASN1_BMPSTRING),
50 ASN1_ITEM_ref(ASN1_BOOLEAN),
51 ASN1_ITEM_ref(ASN1_ENUMERATED),
52 ASN1_ITEM_ref(ASN1_FBOOLEAN),
53 ASN1_ITEM_ref(ASN1_GENERALIZEDTIME),
54 ASN1_ITEM_ref(ASN1_GENERALSTRING),
55 ASN1_ITEM_ref(ASN1_IA5STRING),
56 ASN1_ITEM_ref(ASN1_INTEGER),
57 ASN1_ITEM_ref(ASN1_NULL),
58 ASN1_ITEM_ref(ASN1_OBJECT),
59 ASN1_ITEM_ref(ASN1_OCTET_STRING),
60 ASN1_ITEM_ref(ASN1_OCTET_STRING_NDEF),
61 ASN1_ITEM_ref(ASN1_PRINTABLE),
62 ASN1_ITEM_ref(ASN1_PRINTABLESTRING),
63 ASN1_ITEM_ref(ASN1_SEQUENCE),
64 ASN1_ITEM_ref(ASN1_SEQUENCE_ANY),
65 ASN1_ITEM_ref(ASN1_SET_ANY),
66 ASN1_ITEM_ref(ASN1_T61STRING),
67 ASN1_ITEM_ref(ASN1_TBOOLEAN),
68 ASN1_ITEM_ref(ASN1_TIME),
69 ASN1_ITEM_ref(ASN1_UNIVERSALSTRING),
70 ASN1_ITEM_ref(ASN1_UTCTIME),
71 ASN1_ITEM_ref(ASN1_UTF8STRING),
72 ASN1_ITEM_ref(ASN1_VISIBLESTRING),
73#ifndef OPENSSL_NO_RFC3779
74 ASN1_ITEM_ref(ASRange),
75#endif
76 ASN1_ITEM_ref(AUTHORITY_INFO_ACCESS),
77 ASN1_ITEM_ref(AUTHORITY_KEYID),
78 ASN1_ITEM_ref(BASIC_CONSTRAINTS),
79 ASN1_ITEM_ref(BIGNUM),
80 ASN1_ITEM_ref(CBIGNUM),
81 ASN1_ITEM_ref(CERTIFICATEPOLICIES),
82#ifndef OPENSSL_NO_CMS
83 ASN1_ITEM_ref(CMS_ContentInfo),
84 ASN1_ITEM_ref(CMS_ReceiptRequest),
85 ASN1_ITEM_ref(CRL_DIST_POINTS),
86#endif
87#ifndef OPENSSL_NO_DH
88 ASN1_ITEM_ref(DHparams),
89#endif
90 ASN1_ITEM_ref(DIRECTORYSTRING),
91 ASN1_ITEM_ref(DISPLAYTEXT),
92 ASN1_ITEM_ref(DIST_POINT),
93 ASN1_ITEM_ref(DIST_POINT_NAME),
94#ifndef OPENSSL_NO_EC
95 ASN1_ITEM_ref(ECPARAMETERS),
96 ASN1_ITEM_ref(ECPKPARAMETERS),
97#endif
98 ASN1_ITEM_ref(EDIPARTYNAME),
99 ASN1_ITEM_ref(EXTENDED_KEY_USAGE),
100 ASN1_ITEM_ref(GENERAL_NAME),
101 ASN1_ITEM_ref(GENERAL_NAMES),
102 ASN1_ITEM_ref(GENERAL_SUBTREE),
103#ifndef OPENSSL_NO_RFC3779
104 ASN1_ITEM_ref(IPAddressChoice),
105 ASN1_ITEM_ref(IPAddressFamily),
106 ASN1_ITEM_ref(IPAddressOrRange),
107 ASN1_ITEM_ref(IPAddressRange),
108#endif
109 ASN1_ITEM_ref(ISSUING_DIST_POINT),
110#if OPENSSL_API_COMPAT < 0x10200000L
111 ASN1_ITEM_ref(LONG),
112#endif
113 ASN1_ITEM_ref(NAME_CONSTRAINTS),
114 ASN1_ITEM_ref(NETSCAPE_CERT_SEQUENCE),
115 ASN1_ITEM_ref(NETSCAPE_SPKAC),
116 ASN1_ITEM_ref(NETSCAPE_SPKI),
117 ASN1_ITEM_ref(NOTICEREF),
118#ifndef OPENSSL_NO_OCSP
119 ASN1_ITEM_ref(OCSP_BASICRESP),
120 ASN1_ITEM_ref(OCSP_CERTID),
121 ASN1_ITEM_ref(OCSP_CERTSTATUS),
122 ASN1_ITEM_ref(OCSP_CRLID),
123 ASN1_ITEM_ref(OCSP_ONEREQ),
124 ASN1_ITEM_ref(OCSP_REQINFO),
125 ASN1_ITEM_ref(OCSP_REQUEST),
126 ASN1_ITEM_ref(OCSP_RESPBYTES),
127 ASN1_ITEM_ref(OCSP_RESPDATA),
128 ASN1_ITEM_ref(OCSP_RESPID),
129 ASN1_ITEM_ref(OCSP_RESPONSE),
130 ASN1_ITEM_ref(OCSP_REVOKEDINFO),
131 ASN1_ITEM_ref(OCSP_SERVICELOC),
132 ASN1_ITEM_ref(OCSP_SIGNATURE),
133 ASN1_ITEM_ref(OCSP_SINGLERESP),
134#endif
135 ASN1_ITEM_ref(OTHERNAME),
136 ASN1_ITEM_ref(PBE2PARAM),
137 ASN1_ITEM_ref(PBEPARAM),
138 ASN1_ITEM_ref(PBKDF2PARAM),
139 ASN1_ITEM_ref(PKCS12),
140 ASN1_ITEM_ref(PKCS12_AUTHSAFES),
141 ASN1_ITEM_ref(PKCS12_BAGS),
142 ASN1_ITEM_ref(PKCS12_MAC_DATA),
143 ASN1_ITEM_ref(PKCS12_SAFEBAG),
144 ASN1_ITEM_ref(PKCS12_SAFEBAGS),
145 ASN1_ITEM_ref(PKCS7),
146 ASN1_ITEM_ref(PKCS7_ATTR_SIGN),
147 ASN1_ITEM_ref(PKCS7_ATTR_VERIFY),
148 ASN1_ITEM_ref(PKCS7_DIGEST),
149 ASN1_ITEM_ref(PKCS7_ENC_CONTENT),
150 ASN1_ITEM_ref(PKCS7_ENCRYPT),
151 ASN1_ITEM_ref(PKCS7_ENVELOPE),
152 ASN1_ITEM_ref(PKCS7_ISSUER_AND_SERIAL),
153 ASN1_ITEM_ref(PKCS7_RECIP_INFO),
154 ASN1_ITEM_ref(PKCS7_SIGNED),
155 ASN1_ITEM_ref(PKCS7_SIGN_ENVELOPE),
156 ASN1_ITEM_ref(PKCS7_SIGNER_INFO),
157 ASN1_ITEM_ref(PKCS8_PRIV_KEY_INFO),
158 ASN1_ITEM_ref(PKEY_USAGE_PERIOD),
159 ASN1_ITEM_ref(POLICY_CONSTRAINTS),
160 ASN1_ITEM_ref(POLICYINFO),
161 ASN1_ITEM_ref(POLICY_MAPPING),
162 ASN1_ITEM_ref(POLICY_MAPPINGS),
163 ASN1_ITEM_ref(POLICYQUALINFO),
164 ASN1_ITEM_ref(PROXY_CERT_INFO_EXTENSION),
165 ASN1_ITEM_ref(PROXY_POLICY),
166 ASN1_ITEM_ref(RSA_OAEP_PARAMS),
167 ASN1_ITEM_ref(RSAPrivateKey),
168 ASN1_ITEM_ref(RSA_PSS_PARAMS),
169 ASN1_ITEM_ref(RSAPublicKey),
170 ASN1_ITEM_ref(SXNET),
171 ASN1_ITEM_ref(SXNETID),
172 ASN1_ITEM_ref(USERNOTICE),
173 ASN1_ITEM_ref(X509),
174 ASN1_ITEM_ref(X509_ALGOR),
175 ASN1_ITEM_ref(X509_ALGORS),
176 ASN1_ITEM_ref(X509_ATTRIBUTE),
177 ASN1_ITEM_ref(X509_CERT_AUX),
178 ASN1_ITEM_ref(X509_CINF),
179 ASN1_ITEM_ref(X509_CRL),
180 ASN1_ITEM_ref(X509_CRL_INFO),
181 ASN1_ITEM_ref(X509_EXTENSION),
182 ASN1_ITEM_ref(X509_EXTENSIONS),
183 ASN1_ITEM_ref(X509_NAME),
184 ASN1_ITEM_ref(X509_NAME_ENTRY),
185 ASN1_ITEM_ref(X509_PUBKEY),
186 ASN1_ITEM_ref(X509_REQ),
187 ASN1_ITEM_ref(X509_REQ_INFO),
188 ASN1_ITEM_ref(X509_REVOKED),
189 ASN1_ITEM_ref(X509_SIG),
190 ASN1_ITEM_ref(X509_VAL),
191#if OPENSSL_API_COMPAT < 0x10200000L
192 ASN1_ITEM_ref(ZLONG),
193#endif
194 ASN1_ITEM_ref(INT32),
195 ASN1_ITEM_ref(ZINT32),
196 ASN1_ITEM_ref(UINT32),
197 ASN1_ITEM_ref(ZUINT32),
198 ASN1_ITEM_ref(INT64),
199 ASN1_ITEM_ref(ZINT64),
200 ASN1_ITEM_ref(UINT64),
201 ASN1_ITEM_ref(ZUINT64),
202 NULL
203};
204
205static ASN1_PCTX *pctx;
206
207#define DO_TEST(TYPE, D2I, I2D, PRINT) { \
208 const unsigned char *p = buf; \
209 unsigned char *der = NULL; \
210 TYPE *type = D2I(NULL, &p, len); \
211 \
212 if (type != NULL) { \
213 int len2; \
214 BIO *bio = BIO_new(BIO_s_null()); \
215 \
216 PRINT(bio, type); \
217 BIO_free(bio); \
218 len2 = I2D(type, &der); \
219 if (len2 != 0) {} \
220 OPENSSL_free(der); \
221 TYPE ## _free(type); \
222 } \
223}
224
225#define DO_TEST_PRINT_OFFSET(TYPE, D2I, I2D, PRINT) { \
226 const unsigned char *p = buf; \
227 unsigned char *der = NULL; \
228 TYPE *type = D2I(NULL, &p, len); \
229 \
230 if (type != NULL) { \
231 BIO *bio = BIO_new(BIO_s_null()); \
232 \
233 PRINT(bio, type, 0); \
234 BIO_free(bio); \
235 I2D(type, &der); \
236 OPENSSL_free(der); \
237 TYPE ## _free(type); \
238 } \
239}
240
241#define DO_TEST_PRINT_PCTX(TYPE, D2I, I2D, PRINT) { \
242 const unsigned char *p = buf; \
243 unsigned char *der = NULL; \
244 TYPE *type = D2I(NULL, &p, len); \
245 \
246 if (type != NULL) { \
247 BIO *bio = BIO_new(BIO_s_null()); \
248 \
249 PRINT(bio, type, 0, pctx); \
250 BIO_free(bio); \
251 I2D(type, &der); \
252 OPENSSL_free(der); \
253 TYPE ## _free(type); \
254 } \
255}
256
257
258#define DO_TEST_NO_PRINT(TYPE, D2I, I2D) { \
259 const unsigned char *p = buf; \
260 unsigned char *der = NULL; \
261 TYPE *type = D2I(NULL, &p, len); \
262 \
263 if (type != NULL) { \
264 BIO *bio = BIO_new(BIO_s_null()); \
265 \
266 BIO_free(bio); \
267 I2D(type, &der); \
268 OPENSSL_free(der); \
269 TYPE ## _free(type); \
270 } \
271}
272
273
274int FuzzerInitialize(int *argc, char ***argv)
275{
276 pctx = ASN1_PCTX_new();
277 ASN1_PCTX_set_flags(pctx, ASN1_PCTX_FLAGS_SHOW_ABSENT |
278 ASN1_PCTX_FLAGS_SHOW_SEQUENCE | ASN1_PCTX_FLAGS_SHOW_SSOF |
279 ASN1_PCTX_FLAGS_SHOW_TYPE | ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME);
280 ASN1_PCTX_set_str_flags(pctx, ASN1_STRFLGS_UTF8_CONVERT |
281 ASN1_STRFLGS_SHOW_TYPE | ASN1_STRFLGS_DUMP_ALL);
282
283 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
284 OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
285 ERR_get_state();
286 CRYPTO_free_ex_index(0, -1);
287 FuzzerSetRand();
288
289 return 1;
290}
291
292int FuzzerTestOneInput(const uint8_t *buf, size_t len)
293{
294 int n;
295
296
297 for (n = 0; item_type[n] != NULL; ++n) {
298 const uint8_t *b = buf;
299 unsigned char *der = NULL;
300 const ASN1_ITEM *i = ASN1_ITEM_ptr(item_type[n]);
301 ASN1_VALUE *o = ASN1_item_d2i(NULL, &b, len, i);
302
303 if (o != NULL) {
304 BIO *bio = BIO_new(BIO_s_null());
305
306 ASN1_item_print(bio, o, 4, i, pctx);
307 BIO_free(bio);
308 ASN1_item_i2d(o, &der, i);
309 OPENSSL_free(der);
310 ASN1_item_free(o, i);
311 }
312 }
313
314#ifndef OPENSSL_NO_TS
315 DO_TEST(TS_REQ, d2i_TS_REQ, i2d_TS_REQ, TS_REQ_print_bio);
316 DO_TEST(TS_MSG_IMPRINT, d2i_TS_MSG_IMPRINT, i2d_TS_MSG_IMPRINT, TS_MSG_IMPRINT_print_bio);
317 DO_TEST(TS_RESP, d2i_TS_RESP, i2d_TS_RESP, TS_RESP_print_bio);
318 DO_TEST(TS_STATUS_INFO, d2i_TS_STATUS_INFO, i2d_TS_STATUS_INFO, TS_STATUS_INFO_print_bio);
319 DO_TEST(TS_TST_INFO, d2i_TS_TST_INFO, i2d_TS_TST_INFO, TS_TST_INFO_print_bio);
320 DO_TEST_NO_PRINT(TS_ACCURACY, d2i_TS_ACCURACY, i2d_TS_ACCURACY);
321 DO_TEST_NO_PRINT(ESS_ISSUER_SERIAL, d2i_ESS_ISSUER_SERIAL, i2d_ESS_ISSUER_SERIAL);
322 DO_TEST_NO_PRINT(ESS_CERT_ID, d2i_ESS_CERT_ID, i2d_ESS_CERT_ID);
323 DO_TEST_NO_PRINT(ESS_SIGNING_CERT, d2i_ESS_SIGNING_CERT, i2d_ESS_SIGNING_CERT);
324#endif
325#ifndef OPENSSL_NO_DH
326 DO_TEST(DH, d2i_DHparams, i2d_DHparams, DHparams_print);
327 DO_TEST(DH, d2i_DHxparams, i2d_DHxparams, DHparams_print);
328#endif
329#ifndef OPENSSL_NO_DSA
330 DO_TEST_NO_PRINT(DSA_SIG, d2i_DSA_SIG, i2d_DSA_SIG);
331 DO_TEST_PRINT_OFFSET(DSA, d2i_DSAPrivateKey, i2d_DSAPrivateKey, DSA_print);
332 DO_TEST_PRINT_OFFSET(DSA, d2i_DSAPublicKey, i2d_DSAPublicKey, DSA_print);
333 DO_TEST(DSA, d2i_DSAparams, i2d_DSAparams, DSAparams_print);
334#endif
335 DO_TEST_PRINT_OFFSET(RSA, d2i_RSAPublicKey, i2d_RSAPublicKey, RSA_print);
336#ifndef OPENSSL_NO_EC
337 DO_TEST_PRINT_OFFSET(EC_GROUP, d2i_ECPKParameters, i2d_ECPKParameters, ECPKParameters_print);
338 DO_TEST_PRINT_OFFSET(EC_KEY, d2i_ECPrivateKey, i2d_ECPrivateKey, EC_KEY_print);
339 DO_TEST(EC_KEY, d2i_ECParameters, i2d_ECParameters, ECParameters_print);
340 DO_TEST_NO_PRINT(ECDSA_SIG, d2i_ECDSA_SIG, i2d_ECDSA_SIG);
341#endif
342 DO_TEST_PRINT_PCTX(EVP_PKEY, d2i_AutoPrivateKey, i2d_PrivateKey, EVP_PKEY_print_private);
343 DO_TEST(SSL_SESSION, d2i_SSL_SESSION, i2d_SSL_SESSION, SSL_SESSION_print);
344
345 ERR_clear_error();
346
347 return 0;
348}
349
350void FuzzerCleanup(void)
351{
352 ASN1_PCTX_free(pctx);
353}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette