Coverage Report

Created: 2025-12-31 06:58

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