Make x509 and asn1 fuzzer reproducible
[openssl.git] / fuzz / asn1.c
1 /*
2  * Copyright 2016 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/ec.h>
24 #include <openssl/ocsp.h>
25 #include <openssl/pkcs12.h>
26 #include <openssl/rsa.h>
27 #include <openssl/ts.h>
28 #include <openssl/x509v3.h>
29 #include <openssl/cms.h>
30 #include <openssl/err.h>
31 #include <openssl/rand.h>
32 #include "fuzzer.h"
33
34 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
35 extern int rand_predictable;
36 #endif
37 #define ENTROPY_NEEDED 32
38
39 static ASN1_ITEM_EXP *item_type[] = {
40     ASN1_ITEM_ref(ACCESS_DESCRIPTION),
41 #ifndef OPENSSL_NO_RFC3779
42     ASN1_ITEM_ref(ASIdentifierChoice),
43     ASN1_ITEM_ref(ASIdentifiers),
44     ASN1_ITEM_ref(ASIdOrRange),
45 #endif
46     ASN1_ITEM_ref(ASN1_ANY),
47     ASN1_ITEM_ref(ASN1_BIT_STRING),
48     ASN1_ITEM_ref(ASN1_BMPSTRING),
49     ASN1_ITEM_ref(ASN1_BOOLEAN),
50     ASN1_ITEM_ref(ASN1_ENUMERATED),
51     ASN1_ITEM_ref(ASN1_FBOOLEAN),
52     ASN1_ITEM_ref(ASN1_GENERALIZEDTIME),
53     ASN1_ITEM_ref(ASN1_GENERALSTRING),
54     ASN1_ITEM_ref(ASN1_IA5STRING),
55     ASN1_ITEM_ref(ASN1_INTEGER),
56     ASN1_ITEM_ref(ASN1_NULL),
57     ASN1_ITEM_ref(ASN1_OBJECT),
58     ASN1_ITEM_ref(ASN1_OCTET_STRING),
59     ASN1_ITEM_ref(ASN1_OCTET_STRING_NDEF),
60     ASN1_ITEM_ref(ASN1_PRINTABLE),
61     ASN1_ITEM_ref(ASN1_PRINTABLESTRING),
62     ASN1_ITEM_ref(ASN1_SEQUENCE),
63     ASN1_ITEM_ref(ASN1_SEQUENCE_ANY),
64     ASN1_ITEM_ref(ASN1_SET_ANY),
65     ASN1_ITEM_ref(ASN1_T61STRING),
66     ASN1_ITEM_ref(ASN1_TBOOLEAN),
67     ASN1_ITEM_ref(ASN1_TIME),
68     ASN1_ITEM_ref(ASN1_UNIVERSALSTRING),
69     ASN1_ITEM_ref(ASN1_UTCTIME),
70     ASN1_ITEM_ref(ASN1_UTF8STRING),
71     ASN1_ITEM_ref(ASN1_VISIBLESTRING),
72 #ifndef OPENSSL_NO_RFC3779
73     ASN1_ITEM_ref(ASRange),
74 #endif
75     ASN1_ITEM_ref(AUTHORITY_INFO_ACCESS),
76     ASN1_ITEM_ref(AUTHORITY_KEYID),
77     ASN1_ITEM_ref(BASIC_CONSTRAINTS),
78     ASN1_ITEM_ref(BIGNUM),
79     ASN1_ITEM_ref(CBIGNUM),
80     ASN1_ITEM_ref(CERTIFICATEPOLICIES),
81 #ifndef OPENSSL_NO_CMS
82     ASN1_ITEM_ref(CMS_ContentInfo),
83     ASN1_ITEM_ref(CMS_ReceiptRequest),
84     ASN1_ITEM_ref(CRL_DIST_POINTS),
85 #endif
86 #ifndef OPENSSL_NO_DH
87     ASN1_ITEM_ref(DHparams),
88 #endif
89     ASN1_ITEM_ref(DIRECTORYSTRING),
90     ASN1_ITEM_ref(DISPLAYTEXT),
91     ASN1_ITEM_ref(DIST_POINT),
92     ASN1_ITEM_ref(DIST_POINT_NAME),
93 #ifndef OPENSSL_NO_EC
94     ASN1_ITEM_ref(ECPARAMETERS),
95     ASN1_ITEM_ref(ECPKPARAMETERS),
96 #endif
97     ASN1_ITEM_ref(EDIPARTYNAME),
98     ASN1_ITEM_ref(EXTENDED_KEY_USAGE),
99     ASN1_ITEM_ref(GENERAL_NAME),
100     ASN1_ITEM_ref(GENERAL_NAMES),
101     ASN1_ITEM_ref(GENERAL_SUBTREE),
102 #ifndef OPENSSL_NO_RFC3779
103     ASN1_ITEM_ref(IPAddressChoice),
104     ASN1_ITEM_ref(IPAddressFamily),
105     ASN1_ITEM_ref(IPAddressOrRange),
106     ASN1_ITEM_ref(IPAddressRange),
107 #endif
108     ASN1_ITEM_ref(ISSUING_DIST_POINT),
109 #if OPENSSL_API_COMPAT < 0x10200000L
110     ASN1_ITEM_ref(LONG),
111 #endif
112     ASN1_ITEM_ref(NAME_CONSTRAINTS),
113     ASN1_ITEM_ref(NETSCAPE_CERT_SEQUENCE),
114     ASN1_ITEM_ref(NETSCAPE_SPKAC),
115     ASN1_ITEM_ref(NETSCAPE_SPKI),
116     ASN1_ITEM_ref(NOTICEREF),
117 #ifndef OPENSSL_NO_OCSP
118     ASN1_ITEM_ref(OCSP_BASICRESP),
119     ASN1_ITEM_ref(OCSP_CERTID),
120     ASN1_ITEM_ref(OCSP_CERTSTATUS),
121     ASN1_ITEM_ref(OCSP_CRLID),
122     ASN1_ITEM_ref(OCSP_ONEREQ),
123     ASN1_ITEM_ref(OCSP_REQINFO),
124     ASN1_ITEM_ref(OCSP_REQUEST),
125     ASN1_ITEM_ref(OCSP_RESPBYTES),
126     ASN1_ITEM_ref(OCSP_RESPDATA),
127     ASN1_ITEM_ref(OCSP_RESPID),
128     ASN1_ITEM_ref(OCSP_RESPONSE),
129     ASN1_ITEM_ref(OCSP_REVOKEDINFO),
130     ASN1_ITEM_ref(OCSP_SERVICELOC),
131     ASN1_ITEM_ref(OCSP_SIGNATURE),
132     ASN1_ITEM_ref(OCSP_SINGLERESP),
133 #endif
134     ASN1_ITEM_ref(OTHERNAME),
135     ASN1_ITEM_ref(PBE2PARAM),
136     ASN1_ITEM_ref(PBEPARAM),
137     ASN1_ITEM_ref(PBKDF2PARAM),
138     ASN1_ITEM_ref(PKCS12),
139     ASN1_ITEM_ref(PKCS12_AUTHSAFES),
140     ASN1_ITEM_ref(PKCS12_BAGS),
141     ASN1_ITEM_ref(PKCS12_MAC_DATA),
142     ASN1_ITEM_ref(PKCS12_SAFEBAG),
143     ASN1_ITEM_ref(PKCS12_SAFEBAGS),
144     ASN1_ITEM_ref(PKCS7),
145     ASN1_ITEM_ref(PKCS7_ATTR_SIGN),
146     ASN1_ITEM_ref(PKCS7_ATTR_VERIFY),
147     ASN1_ITEM_ref(PKCS7_DIGEST),
148     ASN1_ITEM_ref(PKCS7_ENC_CONTENT),
149     ASN1_ITEM_ref(PKCS7_ENCRYPT),
150     ASN1_ITEM_ref(PKCS7_ENVELOPE),
151     ASN1_ITEM_ref(PKCS7_ISSUER_AND_SERIAL),
152     ASN1_ITEM_ref(PKCS7_RECIP_INFO),
153     ASN1_ITEM_ref(PKCS7_SIGNED),
154     ASN1_ITEM_ref(PKCS7_SIGN_ENVELOPE),
155     ASN1_ITEM_ref(PKCS7_SIGNER_INFO),
156     ASN1_ITEM_ref(PKCS8_PRIV_KEY_INFO),
157     ASN1_ITEM_ref(PKEY_USAGE_PERIOD),
158     ASN1_ITEM_ref(POLICY_CONSTRAINTS),
159     ASN1_ITEM_ref(POLICYINFO),
160     ASN1_ITEM_ref(POLICY_MAPPING),
161     ASN1_ITEM_ref(POLICY_MAPPINGS),
162     ASN1_ITEM_ref(POLICYQUALINFO),
163     ASN1_ITEM_ref(PROXY_CERT_INFO_EXTENSION),
164     ASN1_ITEM_ref(PROXY_POLICY),
165     ASN1_ITEM_ref(RSA_OAEP_PARAMS),
166     ASN1_ITEM_ref(RSAPrivateKey),
167     ASN1_ITEM_ref(RSA_PSS_PARAMS),
168     ASN1_ITEM_ref(RSAPublicKey),
169     ASN1_ITEM_ref(SXNET),
170     ASN1_ITEM_ref(SXNETID),
171     /*ASN1_ITEM_ref(TS_RESP),  want to do this, but type is hidden, however d2i exists... */
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
205 static ASN1_PCTX *pctx;
206
207 int FuzzerInitialize(int *argc, char ***argv)
208 {
209     pctx = ASN1_PCTX_new();
210     ASN1_PCTX_set_flags(pctx, ASN1_PCTX_FLAGS_SHOW_ABSENT |
211         ASN1_PCTX_FLAGS_SHOW_SEQUENCE | ASN1_PCTX_FLAGS_SHOW_SSOF |
212         ASN1_PCTX_FLAGS_SHOW_TYPE | ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME);
213     ASN1_PCTX_set_str_flags(pctx, ASN1_STRFLGS_UTF8_CONVERT |
214         ASN1_STRFLGS_SHOW_TYPE | ASN1_STRFLGS_DUMP_ALL);
215
216     OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
217     ERR_get_state();
218     CRYPTO_free_ex_index(0, -1);
219     RAND_add("", 1, ENTROPY_NEEDED);
220     RAND_status();
221
222 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
223     rand_predictable = 1;
224 #endif
225
226     return 1;
227 }
228
229 int FuzzerTestOneInput(const uint8_t *buf, size_t len)
230 {
231     int n;
232
233
234     for (n = 0; item_type[n] != NULL; ++n) {
235         const uint8_t *b = buf;
236         unsigned char *der = NULL;
237         const ASN1_ITEM *i = ASN1_ITEM_ptr(item_type[n]);
238         ASN1_VALUE *o = ASN1_item_d2i(NULL, &b, len, i);
239
240         if (o != NULL) {
241             BIO *bio = BIO_new(BIO_s_null());
242             ASN1_item_print(bio, o, 4, i, pctx);
243             BIO_free(bio);
244
245             ASN1_item_i2d(o, &der, i);
246             OPENSSL_free(der);
247
248             ASN1_item_free(o, i);
249         }
250     }
251
252     ERR_clear_error();
253
254     return 0;
255 }
256
257 void FuzzerCleanup(void)
258 {
259     ASN1_PCTX_free(pctx);
260 }