4d22eebd63272e620ec0b7d83b20f82671dce8d8
[openssl.git] / fuzz / asn1.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL licenses, (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/ec.h>
23 #include <openssl/ocsp.h>
24 #include <openssl/pkcs12.h>
25 #include <openssl/ts.h>
26 #include <openssl/x509v3.h>
27 #include "fuzzer.h"
28
29 static ASN1_ITEM_EXP *item_type[] = {
30     ASN1_ITEM_ref(ASN1_SEQUENCE),
31     ASN1_ITEM_ref(AUTHORITY_INFO_ACCESS),
32     ASN1_ITEM_ref(BIGNUM),
33     ASN1_ITEM_ref(ECPARAMETERS),
34     ASN1_ITEM_ref(ECPKPARAMETERS),
35     ASN1_ITEM_ref(GENERAL_NAME),
36     ASN1_ITEM_ref(GENERAL_SUBTREE),
37     ASN1_ITEM_ref(NAME_CONSTRAINTS),
38     ASN1_ITEM_ref(OCSP_BASICRESP),
39     ASN1_ITEM_ref(OCSP_RESPONSE),
40     ASN1_ITEM_ref(PKCS12),
41     ASN1_ITEM_ref(PKCS12_AUTHSAFES),
42     ASN1_ITEM_ref(PKCS12_SAFEBAGS),
43     ASN1_ITEM_ref(PKCS7),
44     ASN1_ITEM_ref(PKCS7_ATTR_SIGN),
45     ASN1_ITEM_ref(PKCS7_ATTR_VERIFY),
46     ASN1_ITEM_ref(PKCS7_DIGEST),
47     ASN1_ITEM_ref(PKCS7_ENC_CONTENT),
48     ASN1_ITEM_ref(PKCS7_ENCRYPT),
49     ASN1_ITEM_ref(PKCS7_ENVELOPE),
50     ASN1_ITEM_ref(PKCS7_RECIP_INFO),
51     ASN1_ITEM_ref(PKCS7_SIGN_ENVELOPE),
52     ASN1_ITEM_ref(PKCS7_SIGNED),
53     ASN1_ITEM_ref(PKCS7_SIGNER_INFO),
54     ASN1_ITEM_ref(POLICY_CONSTRAINTS),
55     ASN1_ITEM_ref(POLICY_MAPPINGS),
56     ASN1_ITEM_ref(SXNET),
57     /*ASN1_ITEM_ref(TS_RESP),  want to do this, but type is hidden, however d2i exists... */
58     ASN1_ITEM_ref(X509),
59     ASN1_ITEM_ref(X509_CRL),
60     NULL
61 };
62
63 int FuzzerInitialize(int *argc, char ***argv) {
64     return 1;
65 }
66
67 int FuzzerTestOneInput(const uint8_t *buf, size_t len) {
68     int n;
69
70     for (n = 0; item_type[n] != NULL; ++n) {
71         const uint8_t *b = buf;
72         const ASN1_ITEM *i = ASN1_ITEM_ptr(item_type[n]);
73         ASN1_VALUE *o = ASN1_item_d2i(NULL, &b, len, i);
74         ASN1_item_free(o, i);
75     }
76     return 0;
77 }