Coverage Report

Created: 2025-08-28 07:07

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