Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/fuzz/cmp.c
Line
Count
Source
1
/*
2
 * Copyright 2007-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
/*
11
 * Test CMP DER parsing.
12
 */
13
14
#include <openssl/bio.h>
15
#include <openssl/cmp.h>
16
#include "../crypto/cmp/cmp_local.h"
17
#include <openssl/err.h>
18
#include "fuzzer.h"
19
20
int FuzzerInitialize(int *argc, char ***argv)
21
254
{
22
254
    FuzzerSetRand();
23
254
    OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
24
254
    ERR_clear_error();
25
254
    CRYPTO_free_ex_index(0, -1);
26
254
    return 1;
27
254
}
28
29
static int num_responses;
30
31
static OSSL_CMP_MSG *transfer_cb(OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *req)
32
0
{
33
0
    if (num_responses++ > 2)
34
0
        return NULL; /* prevent loops due to repeated pollRep */
35
0
    return OSSL_CMP_MSG_dup((OSSL_CMP_MSG *)
36
0
            OSSL_CMP_CTX_get_transfer_cb_arg(ctx));
37
0
}
38
39
static int print_noop(const char *func, const char *file, int line,
40
    OSSL_CMP_severity level, const char *msg)
41
372k
{
42
372k
    return 1;
43
372k
}
44
45
static int allow_unprotected(const OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *rep,
46
    int invalid_protection, int expected_type)
47
34.8k
{
48
34.8k
    return 1;
49
34.8k
}
50
51
static void cmp_client_process_response(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg)
52
37.7k
{
53
37.7k
    X509_NAME *name = X509_NAME_new();
54
37.7k
    ASN1_INTEGER *serial = ASN1_INTEGER_new();
55
56
37.7k
    ctx->unprotectedSend = 1; /* satisfy ossl_cmp_msg_protect() */
57
37.7k
    ctx->disableConfirm = 1; /* check just one response message */
58
37.7k
    ctx->popoMethod = OSSL_CRMF_POPO_NONE; /* satisfy ossl_cmp_certReq_new() */
59
37.7k
    ctx->oldCert = X509_new(); /* satisfy crm_new() and ossl_cmp_rr_new() */
60
37.7k
    if (!OSSL_CMP_CTX_set1_secretValue(ctx, (unsigned char *)"",
61
37.7k
            0) /* prevent too unspecific error */
62
37.7k
        || ctx->oldCert == NULL
63
37.7k
        || name == NULL || !X509_set_issuer_name(ctx->oldCert, name)
64
37.7k
        || serial == NULL || !X509_set_serialNumber(ctx->oldCert, serial))
65
0
        goto err;
66
67
37.7k
    (void)OSSL_CMP_CTX_set_transfer_cb(ctx, transfer_cb);
68
37.7k
    (void)OSSL_CMP_CTX_set_transfer_cb_arg(ctx, msg);
69
37.7k
    (void)OSSL_CMP_CTX_set_log_cb(ctx, print_noop);
70
37.7k
    num_responses = 0;
71
37.7k
    switch (msg->body != NULL ? msg->body->type : -1) {
72
1.16k
    case OSSL_CMP_PKIBODY_IP:
73
1.16k
        (void)OSSL_CMP_exec_IR_ses(ctx);
74
1.16k
        break;
75
88
    case OSSL_CMP_PKIBODY_CP:
76
88
        (void)OSSL_CMP_exec_CR_ses(ctx);
77
88
        (void)OSSL_CMP_exec_P10CR_ses(ctx);
78
88
        break;
79
54
    case OSSL_CMP_PKIBODY_KUP:
80
54
        (void)OSSL_CMP_exec_KUR_ses(ctx);
81
54
        break;
82
224
    case OSSL_CMP_PKIBODY_POLLREP:
83
224
        ctx->status = OSSL_CMP_PKISTATUS_waiting;
84
224
        (void)OSSL_CMP_try_certreq(ctx, OSSL_CMP_PKIBODY_CR, NULL, NULL);
85
224
        break;
86
372
    case OSSL_CMP_PKIBODY_RP:
87
372
        (void)OSSL_CMP_exec_RR_ses(ctx);
88
372
        break;
89
1.00k
    case OSSL_CMP_PKIBODY_GENP:
90
1.00k
        sk_OSSL_CMP_ITAV_pop_free(OSSL_CMP_exec_GENM_ses(ctx),
91
1.00k
            OSSL_CMP_ITAV_free);
92
1.00k
        break;
93
34.8k
    default:
94
34.8k
        (void)ossl_cmp_msg_check_update(ctx, msg, allow_unprotected, 0);
95
34.8k
        break;
96
37.7k
    }
97
37.7k
err:
98
37.7k
    X509_NAME_free(name);
99
37.7k
    ASN1_INTEGER_free(serial);
100
37.7k
}
101
102
static OSSL_CMP_PKISI *process_cert_request(OSSL_CMP_SRV_CTX *srv_ctx,
103
    const OSSL_CMP_MSG *cert_req,
104
    int certReqId,
105
    const OSSL_CRMF_MSG *crm,
106
    const X509_REQ *p10cr,
107
    X509 **certOut,
108
    STACK_OF(X509) **chainOut,
109
    STACK_OF(X509) **caPubs)
110
6.57k
{
111
6.57k
    ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
112
6.57k
    return NULL;
113
6.57k
}
114
115
static OSSL_CMP_PKISI *process_rr(OSSL_CMP_SRV_CTX *srv_ctx,
116
    const OSSL_CMP_MSG *rr,
117
    const X509_NAME *issuer,
118
    const ASN1_INTEGER *serial)
119
52
{
120
52
    ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
121
52
    return NULL;
122
52
}
123
124
static int process_genm(OSSL_CMP_SRV_CTX *srv_ctx,
125
    const OSSL_CMP_MSG *genm,
126
    const STACK_OF(OSSL_CMP_ITAV) *in,
127
    STACK_OF(OSSL_CMP_ITAV) **out)
128
299
{
129
299
    ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
130
299
    return 0;
131
299
}
132
133
static void process_error(OSSL_CMP_SRV_CTX *srv_ctx, const OSSL_CMP_MSG *error,
134
    const OSSL_CMP_PKISI *statusInfo,
135
    const ASN1_INTEGER *errorCode,
136
    const OSSL_CMP_PKIFREETEXT *errorDetails)
137
819
{
138
819
    ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
139
819
}
140
141
static int process_certConf(OSSL_CMP_SRV_CTX *srv_ctx,
142
    const OSSL_CMP_MSG *certConf, int certReqId,
143
    const ASN1_OCTET_STRING *certHash,
144
    const OSSL_CMP_PKISI *si)
145
0
{
146
0
    ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
147
0
    return 0;
148
0
}
149
150
static int process_pollReq(OSSL_CMP_SRV_CTX *srv_ctx,
151
    const OSSL_CMP_MSG *pollReq, int certReqId,
152
    OSSL_CMP_MSG **certReq, int64_t *check_after)
153
0
{
154
0
    ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
155
0
    return 0;
156
0
}
157
158
static int clean_transaction(ossl_unused OSSL_CMP_SRV_CTX *srv_ctx,
159
    ossl_unused const ASN1_OCTET_STRING *id)
160
49.3k
{
161
49.3k
    return 1;
162
49.3k
}
163
164
static int delayed_delivery(ossl_unused OSSL_CMP_SRV_CTX *srv_ctx,
165
    ossl_unused const OSSL_CMP_MSG *req)
166
12.3k
{
167
12.3k
    return 0;
168
12.3k
}
169
170
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
171
17.8k
{
172
17.8k
    OSSL_CMP_MSG *msg;
173
17.8k
    BIO *in;
174
175
17.8k
    if (len == 0 || len > INT_MAX)
176
0
        return 0;
177
178
17.8k
    in = BIO_new(BIO_s_mem());
179
17.8k
    if (in == NULL) {
180
0
        ERR_clear_error();
181
0
        return 0;
182
0
    }
183
17.8k
    if ((size_t)BIO_write(in, buf, (int)len) != len) {
184
0
        BIO_free(in);
185
0
        ERR_clear_error();
186
0
        return 0;
187
0
    }
188
189
17.8k
    msg = d2i_OSSL_CMP_MSG_bio(in, NULL);
190
17.8k
    if (msg != NULL) {
191
11.8k
        BIO *out = BIO_new(BIO_s_null());
192
11.8k
        OSSL_CMP_SRV_CTX *srv_ctx = OSSL_CMP_SRV_CTX_new(NULL, NULL);
193
11.8k
        OSSL_CMP_CTX *client_ctx = OSSL_CMP_CTX_new(NULL, NULL);
194
195
11.8k
        if (out == NULL) {
196
0
            OSSL_CMP_CTX_free(client_ctx);
197
0
            OSSL_CMP_SRV_CTX_free(srv_ctx);
198
0
            OSSL_CMP_MSG_free(msg);
199
0
            BIO_free(in);
200
0
            ERR_clear_error();
201
0
            return 0;
202
0
        }
203
204
11.8k
        i2d_OSSL_CMP_MSG_bio(out, msg);
205
11.8k
        ASN1_item_print(out, (ASN1_VALUE *)msg, 4,
206
11.8k
            ASN1_ITEM_rptr(OSSL_CMP_MSG), NULL);
207
11.8k
        BIO_free(out);
208
209
11.8k
        if (client_ctx != NULL)
210
11.8k
            cmp_client_process_response(client_ctx, msg);
211
11.8k
        if (srv_ctx != NULL
212
11.8k
            && OSSL_CMP_CTX_set_log_cb(OSSL_CMP_SRV_CTX_get0_cmp_ctx(srv_ctx),
213
11.8k
                print_noop)
214
11.8k
            && OSSL_CMP_SRV_CTX_init(srv_ctx, NULL, process_cert_request,
215
11.8k
                process_rr, process_genm, process_error,
216
11.8k
                process_certConf, process_pollReq)
217
11.8k
            && OSSL_CMP_SRV_CTX_init_trans(srv_ctx, delayed_delivery,
218
11.8k
                clean_transaction))
219
11.8k
            OSSL_CMP_MSG_free(OSSL_CMP_SRV_process_request(srv_ctx, msg));
220
221
11.8k
        OSSL_CMP_CTX_free(client_ctx);
222
11.8k
        OSSL_CMP_SRV_CTX_free(srv_ctx);
223
11.8k
        OSSL_CMP_MSG_free(msg);
224
11.8k
    }
225
226
17.8k
    BIO_free(in);
227
17.8k
    ERR_clear_error();
228
229
17.8k
    return 0;
230
17.8k
}
231
232
void FuzzerCleanup(void)
233
0
{
234
0
    FuzzerClearRand();
235
0
}