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