x509.c revision 1.1.1.4.6.2 1 1.1 christos /*
2 1.1.1.4.6.2 sborrill * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved.
3 1.1 christos *
4 1.1.1.4.6.1 martin * Licensed under the Apache License 2.0 (the "License");
5 1.1 christos * you may not use this file except in compliance with the License.
6 1.1 christos * You may obtain a copy of the License at
7 1.1 christos * https://www.openssl.org/source/license.html
8 1.1 christos * or in the file LICENSE in the source distribution.
9 1.1 christos */
10 1.1 christos
11 1.1 christos #include <openssl/x509.h>
12 1.1.1.4.6.2 sborrill #include <openssl/ocsp.h>
13 1.1 christos #include <openssl/bio.h>
14 1.1.1.2 christos #include <openssl/err.h>
15 1.1.1.2 christos #include <openssl/rand.h>
16 1.1 christos #include "fuzzer.h"
17 1.1 christos
18 1.1.1.2 christos int FuzzerInitialize(int *argc, char ***argv)
19 1.1.1.2 christos {
20 1.1.1.4.6.1 martin FuzzerSetRand();
21 1.1.1.4.6.2 sborrill OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS
22 1.1.1.4.6.2 sborrill | OPENSSL_INIT_ADD_ALL_CIPHERS | OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
23 1.1.1.4.6.1 martin ERR_clear_error();
24 1.1.1.2 christos CRYPTO_free_ex_index(0, -1);
25 1.1 christos return 1;
26 1.1 christos }
27 1.1 christos
28 1.1.1.4.6.2 sborrill static int cb(int ok, X509_STORE_CTX *ctx)
29 1.1.1.4.6.2 sborrill {
30 1.1.1.4.6.2 sborrill return 1;
31 1.1.1.4.6.2 sborrill }
32 1.1.1.4.6.2 sborrill
33 1.1.1.2 christos int FuzzerTestOneInput(const uint8_t *buf, size_t len)
34 1.1.1.2 christos {
35 1.1 christos const unsigned char *p = buf;
36 1.1.1.4.6.2 sborrill size_t orig_len = len;
37 1.1 christos unsigned char *der = NULL;
38 1.1.1.4.6.2 sborrill BIO *bio = NULL;
39 1.1.1.4.6.2 sborrill X509 *x509_1 = NULL, *x509_2 = NULL;
40 1.1.1.4.6.2 sborrill X509_STORE *store = NULL;
41 1.1.1.4.6.2 sborrill X509_VERIFY_PARAM *param = NULL;
42 1.1.1.4.6.2 sborrill X509_STORE_CTX *ctx = NULL;
43 1.1.1.4.6.2 sborrill X509_CRL *crl = NULL;
44 1.1.1.4.6.2 sborrill STACK_OF(X509_CRL) *crls = NULL;
45 1.1.1.4.6.2 sborrill STACK_OF(X509) *certs = NULL;
46 1.1.1.4.6.2 sborrill OCSP_RESPONSE *resp = NULL;
47 1.1.1.4.6.2 sborrill OCSP_BASICRESP *bs = NULL;
48 1.1.1.4.6.2 sborrill OCSP_CERTID *id = NULL;
49 1.1.1.4.6.2 sborrill
50 1.1.1.4.6.2 sborrill x509_1 = d2i_X509(NULL, &p, len);
51 1.1.1.4.6.2 sborrill if (x509_1 == NULL)
52 1.1.1.4.6.2 sborrill goto err;
53 1.1.1.4.6.2 sborrill
54 1.1.1.4.6.2 sborrill bio = BIO_new(BIO_s_null());
55 1.1.1.4.6.2 sborrill if (bio == NULL)
56 1.1.1.4.6.2 sborrill goto err;
57 1.1.1.4.6.2 sborrill
58 1.1.1.4.6.2 sborrill /* This will load and print the public key as well as extensions */
59 1.1.1.4.6.2 sborrill X509_print(bio, x509_1);
60 1.1.1.4.6.2 sborrill BIO_free(bio);
61 1.1.1.4.6.2 sborrill
62 1.1.1.4.6.2 sborrill X509_issuer_and_serial_hash(x509_1);
63 1.1.1.4.6.2 sborrill
64 1.1.1.4.6.2 sborrill i2d_X509(x509_1, &der);
65 1.1.1.4.6.2 sborrill OPENSSL_free(der);
66 1.1.1.4.6.2 sborrill
67 1.1.1.4.6.2 sborrill len = orig_len - (p - buf);
68 1.1.1.4.6.2 sborrill x509_2 = d2i_X509(NULL, &p, len);
69 1.1.1.4.6.2 sborrill if (x509_2 == NULL)
70 1.1.1.4.6.2 sborrill goto err;
71 1.1.1.4.6.2 sborrill
72 1.1.1.4.6.2 sborrill len = orig_len - (p - buf);
73 1.1.1.4.6.2 sborrill crl = d2i_X509_CRL(NULL, &p, len);
74 1.1.1.4.6.2 sborrill if (crl == NULL)
75 1.1.1.4.6.2 sborrill goto err;
76 1.1.1.4.6.2 sborrill
77 1.1.1.4.6.2 sborrill len = orig_len - (p - buf);
78 1.1.1.4.6.2 sborrill resp = d2i_OCSP_RESPONSE(NULL, &p, len);
79 1.1.1.4.6.2 sborrill
80 1.1.1.4.6.2 sborrill store = X509_STORE_new();
81 1.1.1.4.6.2 sborrill X509_STORE_add_cert(store, x509_2);
82 1.1.1.4.6.2 sborrill
83 1.1.1.4.6.2 sborrill param = X509_VERIFY_PARAM_new();
84 1.1.1.4.6.2 sborrill X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_NO_CHECK_TIME);
85 1.1.1.4.6.2 sborrill X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_X509_STRICT);
86 1.1.1.4.6.2 sborrill X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_PARTIAL_CHAIN);
87 1.1.1.4.6.2 sborrill X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
88 1.1.1.4.6.2 sborrill
89 1.1.1.4.6.2 sborrill X509_STORE_set1_param(store, param);
90 1.1.1.4.6.2 sborrill
91 1.1.1.4.6.2 sborrill X509_STORE_set_verify_cb(store, cb);
92 1.1.1.4.6.2 sborrill
93 1.1.1.4.6.2 sborrill ctx = X509_STORE_CTX_new();
94 1.1.1.4.6.2 sborrill if (ctx == NULL)
95 1.1.1.4.6.2 sborrill goto err;
96 1.1.1.4.6.2 sborrill
97 1.1.1.4.6.2 sborrill X509_STORE_CTX_init(ctx, store, x509_1, NULL);
98 1.1.1.4.6.2 sborrill
99 1.1.1.4.6.2 sborrill if (crl != NULL) {
100 1.1.1.4.6.2 sborrill crls = sk_X509_CRL_new_null();
101 1.1.1.4.6.2 sborrill if (crls == NULL)
102 1.1.1.4.6.2 sborrill goto err;
103 1.1.1.4.6.2 sborrill
104 1.1.1.4.6.2 sborrill sk_X509_CRL_push(crls, crl);
105 1.1.1.4.6.2 sborrill X509_STORE_CTX_set0_crls(ctx, crls);
106 1.1.1.4.6.2 sborrill }
107 1.1 christos
108 1.1.1.4.6.2 sborrill X509_verify_cert(ctx);
109 1.1 christos
110 1.1.1.4.6.2 sborrill if (resp != NULL)
111 1.1.1.4.6.2 sborrill bs = OCSP_response_get1_basic(resp);
112 1.1.1.3 christos
113 1.1.1.4.6.2 sborrill if (bs != NULL) {
114 1.1.1.4.6.2 sborrill int status, reason;
115 1.1.1.4.6.2 sborrill ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd;
116 1.1 christos
117 1.1.1.4.6.2 sborrill certs = sk_X509_new_null();
118 1.1.1.4.6.2 sborrill if (certs == NULL)
119 1.1.1.4.6.2 sborrill goto err;
120 1.1.1.4.6.2 sborrill
121 1.1.1.4.6.2 sborrill sk_X509_push(certs, x509_1);
122 1.1.1.4.6.2 sborrill sk_X509_push(certs, x509_2);
123 1.1.1.4.6.2 sborrill
124 1.1.1.4.6.2 sborrill OCSP_basic_verify(bs, certs, store, OCSP_PARTIAL_CHAIN);
125 1.1.1.4.6.2 sborrill
126 1.1.1.4.6.2 sborrill id = OCSP_cert_to_id(NULL, x509_1, x509_2);
127 1.1.1.4.6.2 sborrill if (id == NULL)
128 1.1.1.4.6.2 sborrill goto err;
129 1.1.1.4.6.2 sborrill OCSP_resp_find_status(bs, id, &status, &reason, &revtime, &thisupd,
130 1.1.1.4.6.2 sborrill &nextupd);
131 1.1 christos }
132 1.1.1.4.6.2 sborrill
133 1.1.1.4.6.2 sborrill err:
134 1.1.1.4.6.2 sborrill X509_STORE_CTX_free(ctx);
135 1.1.1.4.6.2 sborrill X509_VERIFY_PARAM_free(param);
136 1.1.1.4.6.2 sborrill X509_STORE_free(store);
137 1.1.1.4.6.2 sborrill X509_free(x509_1);
138 1.1.1.4.6.2 sborrill X509_free(x509_2);
139 1.1.1.4.6.2 sborrill X509_CRL_free(crl);
140 1.1.1.4.6.2 sborrill OCSP_CERTID_free(id);
141 1.1.1.4.6.2 sborrill OCSP_BASICRESP_free(bs);
142 1.1.1.4.6.2 sborrill OCSP_RESPONSE_free(resp);
143 1.1.1.4.6.2 sborrill sk_X509_CRL_free(crls);
144 1.1.1.4.6.2 sborrill sk_X509_free(certs);
145 1.1.1.4.6.2 sborrill
146 1.1.1.2 christos ERR_clear_error();
147 1.1 christos return 0;
148 1.1 christos }
149 1.1.1.2 christos
150 1.1.1.2 christos void FuzzerCleanup(void)
151 1.1.1.2 christos {
152 1.1.1.4.6.1 martin FuzzerClearRand();
153 1.1.1.2 christos }
154