Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/fuzz/x509.c
Line
Count
Source
1
/*
2
 * Copyright 2016-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 *
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.
9
 */
10
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>
16
#include "fuzzer.h"
17
18
int FuzzerInitialize(int *argc, char ***argv)
19
254
{
20
254
    FuzzerSetRand();
21
254
    OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS
22
254
            | OPENSSL_INIT_ADD_ALL_CIPHERS | OPENSSL_INIT_ADD_ALL_DIGESTS,
23
254
        NULL);
24
254
    ERR_clear_error();
25
254
    CRYPTO_free_ex_index(0, -1);
26
254
    return 1;
27
254
}
28
29
static int cb(int ok, X509_STORE_CTX *ctx)
30
38.2k
{
31
38.2k
    return 1;
32
38.2k
}
33
34
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
35
20.3k
{
36
20.3k
    const unsigned char *p = buf;
37
20.3k
    size_t orig_len = len;
38
20.3k
    unsigned char *der = NULL;
39
20.3k
    BIO *bio = NULL;
40
20.3k
    X509 *x509_1 = NULL, *x509_2 = NULL;
41
20.3k
    X509_STORE *store = NULL;
42
20.3k
    X509_VERIFY_PARAM *param = NULL;
43
20.3k
    X509_STORE_CTX *ctx = NULL;
44
20.3k
    X509_CRL *crl = NULL;
45
20.3k
    STACK_OF(X509_CRL) *crls = NULL;
46
20.3k
    STACK_OF(X509) *certs = NULL;
47
20.3k
    OCSP_RESPONSE *resp = NULL;
48
20.3k
    OCSP_BASICRESP *bs = NULL;
49
20.3k
    OCSP_CERTID *id = NULL;
50
51
20.3k
    x509_1 = d2i_X509(NULL, &p, (long)len);
52
20.3k
    if (x509_1 == NULL)
53
8.96k
        goto err;
54
55
11.3k
    bio = BIO_new(BIO_s_null());
56
11.3k
    if (bio == NULL)
57
0
        goto err;
58
59
    /* This will load and print the public key as well as extensions */
60
11.3k
    X509_print(bio, x509_1);
61
11.3k
    BIO_free(bio);
62
63
11.3k
    X509_issuer_and_serial_hash(x509_1);
64
65
11.3k
    i2d_X509(x509_1, &der);
66
11.3k
    OPENSSL_free(der);
67
68
11.3k
    len = orig_len - (p - buf);
69
11.3k
    x509_2 = d2i_X509(NULL, &p, (long)len);
70
11.3k
    if (x509_2 == NULL)
71
9.23k
        goto err;
72
73
2.16k
    len = orig_len - (p - buf);
74
2.16k
    crl = d2i_X509_CRL(NULL, &p, (long)len);
75
2.16k
    if (crl == NULL)
76
55
        goto err;
77
78
2.10k
    len = orig_len - (p - buf);
79
2.10k
    resp = d2i_OCSP_RESPONSE(NULL, &p, (long)len);
80
81
2.10k
    store = X509_STORE_new();
82
2.10k
    if (store == NULL)
83
0
        goto err;
84
2.10k
    X509_STORE_add_cert(store, x509_2);
85
86
2.10k
    param = X509_VERIFY_PARAM_new();
87
2.10k
    if (param == NULL)
88
0
        goto err;
89
2.10k
    X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_NO_CHECK_TIME);
90
2.10k
    X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_X509_STRICT);
91
2.10k
    X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_PARTIAL_CHAIN);
92
2.10k
    X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
93
94
2.10k
    X509_STORE_set1_param(store, param);
95
96
2.10k
    X509_STORE_set_verify_cb(store, cb);
97
98
2.10k
    ctx = X509_STORE_CTX_new();
99
2.10k
    if (ctx == NULL)
100
0
        goto err;
101
102
2.10k
    if (!X509_STORE_CTX_init(ctx, store, x509_1, NULL))
103
0
        goto err;
104
105
2.10k
    if (crl != NULL) {
106
2.10k
        crls = sk_X509_CRL_new_null();
107
2.10k
        if (crls == NULL
108
2.10k
            || !sk_X509_CRL_push(crls, crl))
109
0
            goto err;
110
111
2.10k
        X509_STORE_CTX_set0_crls(ctx, crls);
112
2.10k
    }
113
114
2.10k
    X509_verify_cert(ctx);
115
116
2.10k
    if (resp != NULL)
117
248
        bs = OCSP_response_get1_basic(resp);
118
119
2.10k
    if (bs != NULL) {
120
170
        int status, reason;
121
170
        ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd;
122
123
170
        certs = sk_X509_new_null();
124
170
        if (certs == NULL
125
170
            || !sk_X509_push(certs, x509_1)
126
170
            || !sk_X509_push(certs, x509_2))
127
0
            goto err;
128
129
170
        OCSP_basic_verify(bs, certs, store, OCSP_PARTIAL_CHAIN);
130
131
170
        id = OCSP_cert_to_id(NULL, x509_1, x509_2);
132
170
        if (id == NULL)
133
0
            goto err;
134
170
        OCSP_resp_find_status(bs, id, &status, &reason, &revtime, &thisupd,
135
170
            &nextupd);
136
170
    }
137
138
20.3k
err:
139
20.3k
    X509_STORE_CTX_free(ctx);
140
20.3k
    X509_VERIFY_PARAM_free(param);
141
20.3k
    X509_STORE_free(store);
142
20.3k
    X509_free(x509_1);
143
20.3k
    X509_free(x509_2);
144
20.3k
    X509_CRL_free(crl);
145
20.3k
    OCSP_CERTID_free(id);
146
20.3k
    OCSP_BASICRESP_free(bs);
147
20.3k
    OCSP_RESPONSE_free(resp);
148
20.3k
    sk_X509_CRL_free(crls);
149
20.3k
    sk_X509_free(certs);
150
151
20.3k
    ERR_clear_error();
152
20.3k
    return 0;
153
2.10k
}
154
155
void FuzzerCleanup(void)
156
0
{
157
0
    FuzzerClearRand();
158
0
}