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