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