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