xref: /freebsd/crypto/openssl/fuzz/asn1parse.c (revision ce15215befa1022d9a20ec15bdb0739dbe55f10f)
1 /*
2  * Copyright 2016-2021 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 the parser used for dumping ASN.1 using "openssl asn1parse".
13  */
14 
15 #include <stdio.h>
16 #include <openssl/asn1.h>
17 #include <openssl/x509.h>
18 #include <openssl/x509v3.h>
19 #include <openssl/err.h>
20 #include "fuzzer.h"
21 
22 static BIO *bio_out;
23 
24 int FuzzerInitialize(int *argc, char ***argv)
25 {
26     bio_out = BIO_new(BIO_s_null()); /* output will be ignored */
27     if (bio_out == NULL)
28         return 0;
29     OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
30     ERR_clear_error();
31     CRYPTO_free_ex_index(0, -1);
32     return 1;
33 }
34 
35 int FuzzerTestOneInput(const uint8_t *buf, size_t len)
36 {
37     (void)ASN1_parse_dump(bio_out, buf, len, 0, 0);
38     ERR_clear_error();
39     return 0;
40 }
41 
42 void FuzzerCleanup(void)
43 {
44     BIO_free(bio_out);
45 }
46