Coverage Report

Created: 2023-09-25 06:45

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