Add fuzzing!
[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 const ASN1_ITEM *item_type;
30
31 int LLVMFuzzerInitialize(int *argc, char ***argv) {
32     const char *cmd;
33     OPENSSL_assert(*argc > 1);
34
35     cmd = (*argv)[1];
36     (*argv)[1] = (*argv)[0];
37     ++*argv;
38     --*argc;
39
40     // TODO: make this work like d2i_test.c does, once its decided what the
41     // common scheme is!
42 #define Y(t)  if (!strcmp(cmd, #t)) item_type = ASN1_ITEM_rptr(t)
43 #define X(t)  else Y(t)
44
45     Y(ASN1_SEQUENCE);
46     X(AUTHORITY_INFO_ACCESS);
47     X(BIGNUM);
48     X(ECPARAMETERS);
49     X(ECPKPARAMETERS);
50     X(GENERAL_NAME);
51     X(GENERAL_SUBTREE);
52     X(NAME_CONSTRAINTS);
53     X(OCSP_BASICRESP);
54     X(OCSP_RESPONSE);
55     X(PKCS12);
56     X(PKCS12_AUTHSAFES);
57     X(PKCS12_SAFEBAGS);
58     X(PKCS7);
59     X(PKCS7_ATTR_SIGN);
60     X(PKCS7_ATTR_VERIFY);
61     X(PKCS7_DIGEST);
62     X(PKCS7_ENC_CONTENT);
63     X(PKCS7_ENCRYPT);
64     X(PKCS7_ENVELOPE);
65     X(PKCS7_RECIP_INFO);
66     X(PKCS7_SIGN_ENVELOPE);
67     X(PKCS7_SIGNED);
68     X(PKCS7_SIGNER_INFO);
69     X(POLICY_CONSTRAINTS);
70     X(POLICY_MAPPINGS);
71     X(SXNET);
72     //X(TS_RESP);  want to do this, but type is hidden, however d2i exists...
73     X(X509);
74     X(X509_CRL);
75     else
76         OPENSSL_assert(!"Bad type");
77
78     return 0;
79 }
80
81 int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) {
82     const uint8_t *b = buf;
83     ASN1_VALUE *o = ASN1_item_d2i(NULL, &b, len, item_type);
84     ASN1_item_free(o, item_type);
85     return 0;
86 }