Use _NO_INST in some build.info files
[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 #ifndef OPENSSL_NO_EC
34     ASN1_ITEM_ref(ECPARAMETERS),
35     ASN1_ITEM_ref(ECPKPARAMETERS),
36 #endif
37     ASN1_ITEM_ref(GENERAL_NAME),
38     ASN1_ITEM_ref(GENERAL_SUBTREE),
39     ASN1_ITEM_ref(NAME_CONSTRAINTS),
40     ASN1_ITEM_ref(OCSP_BASICRESP),
41     ASN1_ITEM_ref(OCSP_RESPONSE),
42     ASN1_ITEM_ref(PKCS12),
43     ASN1_ITEM_ref(PKCS12_AUTHSAFES),
44     ASN1_ITEM_ref(PKCS12_SAFEBAGS),
45     ASN1_ITEM_ref(PKCS7),
46     ASN1_ITEM_ref(PKCS7_ATTR_SIGN),
47     ASN1_ITEM_ref(PKCS7_ATTR_VERIFY),
48     ASN1_ITEM_ref(PKCS7_DIGEST),
49     ASN1_ITEM_ref(PKCS7_ENC_CONTENT),
50     ASN1_ITEM_ref(PKCS7_ENCRYPT),
51     ASN1_ITEM_ref(PKCS7_ENVELOPE),
52     ASN1_ITEM_ref(PKCS7_RECIP_INFO),
53     ASN1_ITEM_ref(PKCS7_SIGN_ENVELOPE),
54     ASN1_ITEM_ref(PKCS7_SIGNED),
55     ASN1_ITEM_ref(PKCS7_SIGNER_INFO),
56     ASN1_ITEM_ref(POLICY_CONSTRAINTS),
57     ASN1_ITEM_ref(POLICY_MAPPINGS),
58     ASN1_ITEM_ref(SXNET),
59     /*ASN1_ITEM_ref(TS_RESP),  want to do this, but type is hidden, however d2i exists... */
60     ASN1_ITEM_ref(X509),
61     ASN1_ITEM_ref(X509_CRL),
62     NULL
63 };
64
65 int FuzzerInitialize(int *argc, char ***argv) {
66     return 1;
67 }
68
69 int FuzzerTestOneInput(const uint8_t *buf, size_t len) {
70     int n;
71
72     ASN1_PCTX *pctx = ASN1_PCTX_new();
73
74     ASN1_PCTX_set_flags(pctx, ASN1_PCTX_FLAGS_SHOW_ABSENT |
75         ASN1_PCTX_FLAGS_SHOW_SEQUENCE | ASN1_PCTX_FLAGS_SHOW_SSOF |
76         ASN1_PCTX_FLAGS_SHOW_TYPE | ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME);
77     ASN1_PCTX_set_str_flags(pctx, ASN1_STRFLGS_UTF8_CONVERT |
78         ASN1_STRFLGS_SHOW_TYPE | ASN1_STRFLGS_DUMP_ALL);
79
80     for (n = 0; item_type[n] != NULL; ++n) {
81         const uint8_t *b = buf;
82         unsigned char *der = NULL;
83         const ASN1_ITEM *i = ASN1_ITEM_ptr(item_type[n]);
84         ASN1_VALUE *o = ASN1_item_d2i(NULL, &b, len, i);
85
86         if (o != NULL) {
87             BIO *bio = BIO_new(BIO_s_null());
88             ASN1_item_print(bio, o, 4, i, pctx);
89             BIO_free(bio);
90
91             ASN1_item_i2d(o, &der, i);
92             OPENSSL_free(der);
93
94             ASN1_item_free(o, i);
95         }
96     }
97
98     ASN1_PCTX_free(pctx);
99
100     return 0;
101 }