xref: /freebsd/crypto/openssl/fuzz/asn1parse.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License");
5*e0c4386eSCy Schubert  * you may not use this file except in compliance with the License.
6*e0c4386eSCy Schubert  * You may obtain a copy of the License at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  * or in the file LICENSE in the source distribution.
9*e0c4386eSCy Schubert  */
10*e0c4386eSCy Schubert 
11*e0c4386eSCy Schubert /*
12*e0c4386eSCy Schubert  * Fuzz the parser used for dumping ASN.1 using "openssl asn1parse".
13*e0c4386eSCy Schubert  */
14*e0c4386eSCy Schubert 
15*e0c4386eSCy Schubert #include <stdio.h>
16*e0c4386eSCy Schubert #include <openssl/asn1.h>
17*e0c4386eSCy Schubert #include <openssl/x509.h>
18*e0c4386eSCy Schubert #include <openssl/x509v3.h>
19*e0c4386eSCy Schubert #include <openssl/err.h>
20*e0c4386eSCy Schubert #include "fuzzer.h"
21*e0c4386eSCy Schubert 
22*e0c4386eSCy Schubert static BIO *bio_out;
23*e0c4386eSCy Schubert 
FuzzerInitialize(int * argc,char *** argv)24*e0c4386eSCy Schubert int FuzzerInitialize(int *argc, char ***argv)
25*e0c4386eSCy Schubert {
26*e0c4386eSCy Schubert     bio_out = BIO_new(BIO_s_null()); /* output will be ignored */
27*e0c4386eSCy Schubert     if (bio_out == NULL)
28*e0c4386eSCy Schubert         return 0;
29*e0c4386eSCy Schubert     OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
30*e0c4386eSCy Schubert     ERR_clear_error();
31*e0c4386eSCy Schubert     CRYPTO_free_ex_index(0, -1);
32*e0c4386eSCy Schubert     return 1;
33*e0c4386eSCy Schubert }
34*e0c4386eSCy Schubert 
FuzzerTestOneInput(const uint8_t * buf,size_t len)35*e0c4386eSCy Schubert int FuzzerTestOneInput(const uint8_t *buf, size_t len)
36*e0c4386eSCy Schubert {
37*e0c4386eSCy Schubert     (void)ASN1_parse_dump(bio_out, buf, len, 0, 0);
38*e0c4386eSCy Schubert     ERR_clear_error();
39*e0c4386eSCy Schubert     return 0;
40*e0c4386eSCy Schubert }
41*e0c4386eSCy Schubert 
FuzzerCleanup(void)42*e0c4386eSCy Schubert void FuzzerCleanup(void)
43*e0c4386eSCy Schubert {
44*e0c4386eSCy Schubert     BIO_free(bio_out);
45*e0c4386eSCy Schubert }
46