2 * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved.
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.
11 #include <openssl/x509.h>
12 #include <openssl/ocsp.h>
13 #include <openssl/bio.h>
14 #include <openssl/err.h>
15 #include <openssl/rand.h>
18 int FuzzerInitialize(int *argc, char ***argv)
21 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS
22 | OPENSSL_INIT_ADD_ALL_CIPHERS | OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
24 CRYPTO_free_ex_index(0, -1);
28 static int cb(int ok, X509_STORE_CTX *ctx)
33 int FuzzerTestOneInput(const uint8_t *buf, size_t len)
35 const unsigned char *p = buf;
36 size_t orig_len = len;
37 unsigned char *der = NULL;
39 X509 *x509_1 = NULL, *x509_2 = NULL;
40 X509_STORE *store = NULL;
41 X509_VERIFY_PARAM *param = NULL;
42 X509_STORE_CTX *ctx = NULL;
44 STACK_OF(X509_CRL) *crls = NULL;
45 STACK_OF(X509) *certs = NULL;
46 OCSP_RESPONSE *resp = NULL;
47 OCSP_BASICRESP *bs = NULL;
48 OCSP_CERTID *id = NULL;
50 x509_1 = d2i_X509(NULL, &p, len);
54 bio = BIO_new(BIO_s_null());
58 /* This will load and print the public key as well as extensions */
59 X509_print(bio, x509_1);
62 X509_issuer_and_serial_hash(x509_1);
64 i2d_X509(x509_1, &der);
67 len = orig_len - (p - buf);
68 x509_2 = d2i_X509(NULL, &p, len);
72 len = orig_len - (p - buf);
73 crl = d2i_X509_CRL(NULL, &p, len);
77 len = orig_len - (p - buf);
78 resp = d2i_OCSP_RESPONSE(NULL, &p, len);
80 store = X509_STORE_new();
81 X509_STORE_add_cert(store, x509_2);
83 param = X509_VERIFY_PARAM_new();
84 X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_NO_CHECK_TIME);
85 X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_X509_STRICT);
86 X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_PARTIAL_CHAIN);
87 X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
89 X509_STORE_set1_param(store, param);
91 X509_STORE_set_verify_cb(store, cb);
93 ctx = X509_STORE_CTX_new();
97 X509_STORE_CTX_init(ctx, store, x509_1, NULL);
100 crls = sk_X509_CRL_new_null();
104 sk_X509_CRL_push(crls, crl);
105 X509_STORE_CTX_set0_crls(ctx, crls);
108 X509_verify_cert(ctx);
111 bs = OCSP_response_get1_basic(resp);
115 ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd;
117 certs = sk_X509_new_null();
121 sk_X509_push(certs, x509_1);
122 sk_X509_push(certs, x509_2);
124 OCSP_basic_verify(bs, certs, store, OCSP_PARTIAL_CHAIN);
126 id = OCSP_cert_to_id(NULL, x509_1, x509_2);
129 OCSP_resp_find_status(bs, id, &status, &reason, &revtime, &thisupd,
134 X509_STORE_CTX_free(ctx);
135 X509_VERIFY_PARAM_free(param);
136 X509_STORE_free(store);
140 OCSP_CERTID_free(id);
141 OCSP_BASICRESP_free(bs);
142 OCSP_RESPONSE_free(resp);
143 sk_X509_CRL_free(crls);
150 void FuzzerCleanup(void)