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