Coverage Report

Created: 2026-04-01 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/crypto/cmp/cmp_client.c
Line
Count
Source
1
/*
2
 * Copyright 2007-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright Nokia 2007-2019
4
 * Copyright Siemens AG 2015-2019
5
 *
6
 * Licensed under the Apache License 2.0 (the "License").  You may not use
7
 * this file except in compliance with the License.  You can obtain a copy
8
 * in the file LICENSE in the source distribution or at
9
 * https://www.openssl.org/source/license.html
10
 */
11
12
#include "cmp_local.h"
13
14
0
#define IS_CREP(t) ((t) == OSSL_CMP_PKIBODY_IP || (t) == OSSL_CMP_PKIBODY_CP \
15
0
    || (t) == OSSL_CMP_PKIBODY_KUP)
16
17
/*-
18
 * Evaluate whether there's an exception (violating the standard) configured for
19
 * handling negative responses without protection or with invalid protection.
20
 * Returns 1 on acceptance, 0 on rejection, or -1 on (internal) error.
21
 */
22
static int unprotected_exception(const OSSL_CMP_CTX *ctx,
23
    const OSSL_CMP_MSG *rep,
24
    int invalid_protection,
25
    ossl_unused int expected_type)
26
0
{
27
0
    int rcvd_type = OSSL_CMP_MSG_get_bodytype(rep /* may be NULL */);
28
0
    const char *msg_type = NULL;
29
30
0
    if (!ossl_assert(ctx != NULL && rep != NULL))
31
0
        return -1;
32
33
0
    if (!ctx->unprotectedErrors)
34
0
        return 0;
35
36
0
    switch (rcvd_type) {
37
0
    case OSSL_CMP_PKIBODY_ERROR:
38
0
        msg_type = "error response";
39
0
        break;
40
0
    case OSSL_CMP_PKIBODY_RP: {
41
0
        OSSL_CMP_PKISI *si = ossl_cmp_revrepcontent_get_pkisi(rep->body->value.rp,
42
0
            OSSL_CMP_REVREQSID);
43
44
0
        if (si == NULL)
45
0
            return -1;
46
0
        if (ossl_cmp_pkisi_get_status(si) == OSSL_CMP_PKISTATUS_rejection)
47
0
            msg_type = "revocation response message with rejection status";
48
0
        break;
49
0
    }
50
0
    case OSSL_CMP_PKIBODY_PKICONF:
51
0
        msg_type = "PKI Confirmation message";
52
0
        break;
53
0
    default:
54
0
        if (IS_CREP(rcvd_type)) {
55
0
            int any_rid = OSSL_CMP_CERTREQID_NONE;
56
0
            OSSL_CMP_CERTREPMESSAGE *crepmsg = rep->body->value.ip;
57
0
            OSSL_CMP_CERTRESPONSE *crep = ossl_cmp_certrepmessage_get0_certresponse(crepmsg, any_rid);
58
59
0
            if (sk_OSSL_CMP_CERTRESPONSE_num(crepmsg->response) > 1)
60
0
                return -1;
61
0
            if (crep == NULL)
62
0
                return -1;
63
0
            if (ossl_cmp_pkisi_get_status(crep->status)
64
0
                == OSSL_CMP_PKISTATUS_rejection)
65
0
                msg_type = "CertRepMessage with rejection status";
66
0
        }
67
0
    }
68
0
    if (msg_type == NULL)
69
0
        return 0;
70
0
    ossl_cmp_log2(WARN, ctx, "ignoring %s protection of %s",
71
0
        invalid_protection ? "invalid" : "missing", msg_type);
72
0
    return 1;
73
0
}
74
75
/* Save error info from PKIStatusInfo field of a certresponse into ctx */
76
static int save_statusInfo(OSSL_CMP_CTX *ctx, OSSL_CMP_PKISI *si)
77
0
{
78
0
    int i;
79
0
    OSSL_CMP_PKIFREETEXT *ss;
80
81
0
    if (!ossl_assert(ctx != NULL && si != NULL))
82
0
        return 0;
83
84
0
    ctx->status = ossl_cmp_pkisi_get_status(si);
85
0
    if (ctx->status < OSSL_CMP_PKISTATUS_accepted)
86
0
        return 0;
87
88
0
    ctx->failInfoCode = ossl_cmp_pkisi_get_pkifailureinfo(si);
89
90
0
    if (!ossl_cmp_ctx_set0_statusString(ctx, sk_ASN1_UTF8STRING_new_null())
91
0
        || (ctx->statusString == NULL))
92
0
        return 0;
93
94
0
    ss = si->statusString; /* may be NULL */
95
0
    for (i = 0; i < sk_ASN1_UTF8STRING_num(ss); i++) {
96
0
        ASN1_UTF8STRING *str = sk_ASN1_UTF8STRING_value(ss, i);
97
0
        ASN1_UTF8STRING *dup = ASN1_STRING_dup(str);
98
99
0
        if (dup == NULL || !sk_ASN1_UTF8STRING_push(ctx->statusString, dup)) {
100
0
            ASN1_UTF8STRING_free(dup);
101
0
            return 0;
102
0
        }
103
0
    }
104
0
    return 1;
105
0
}
106
107
static int is_crep_with_waiting(const OSSL_CMP_MSG *resp, int rid)
108
0
{
109
0
    OSSL_CMP_CERTREPMESSAGE *crepmsg;
110
0
    OSSL_CMP_CERTRESPONSE *crep;
111
0
    int bt = OSSL_CMP_MSG_get_bodytype(resp);
112
113
0
    if (!IS_CREP(bt))
114
0
        return 0;
115
116
0
    crepmsg = resp->body->value.ip; /* same for cp and kup */
117
0
    crep = ossl_cmp_certrepmessage_get0_certresponse(crepmsg, rid);
118
119
0
    return (crep != NULL
120
0
        && ossl_cmp_pkisi_get_status(crep->status)
121
0
            == OSSL_CMP_PKISTATUS_waiting);
122
0
}
123
124
/*-
125
 * Perform the generic aspects of sending a request and receiving a response.
126
 * Returns 1 on success and provides the received PKIMESSAGE in *rep.
127
 * Returns 0 on error.
128
 * Regardless of success, caller is responsible for freeing *rep (unless NULL).
129
 */
130
static int send_receive_check(OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *req,
131
    OSSL_CMP_MSG **rep, int expected_type)
132
0
{
133
0
    int begin_transaction = expected_type != OSSL_CMP_PKIBODY_POLLREP
134
0
        && expected_type != OSSL_CMP_PKIBODY_PKICONF;
135
0
    const char *req_type_str = ossl_cmp_bodytype_to_string(OSSL_CMP_MSG_get_bodytype(req));
136
0
    const char *expected_type_str = ossl_cmp_bodytype_to_string(expected_type);
137
0
    int bak_msg_timeout = ctx->msg_timeout;
138
0
    int bt;
139
0
    time_t now = time(NULL);
140
0
    int time_left;
141
0
    OSSL_CMP_transfer_cb_t transfer_cb = ctx->transfer_cb;
142
143
0
    ctx->status = OSSL_CMP_PKISTATUS_trans;
144
0
#ifndef OPENSSL_NO_HTTP
145
0
    if (transfer_cb == NULL)
146
0
        transfer_cb = OSSL_CMP_MSG_http_perform;
147
0
#endif
148
0
    *rep = NULL;
149
150
0
    if (ctx->total_timeout != 0 /* not waiting indefinitely */) {
151
0
        if (begin_transaction)
152
0
            ctx->end_time = now + ctx->total_timeout;
153
0
        if (now >= ctx->end_time) {
154
0
            ERR_raise(ERR_LIB_CMP, CMP_R_TOTAL_TIMEOUT);
155
0
            return 0;
156
0
        }
157
0
        if (!ossl_assert(ctx->end_time - now < INT_MAX)) {
158
            /* actually cannot happen due to assignment in initial_certreq() */
159
0
            ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
160
0
            return 0;
161
0
        }
162
0
        time_left = (int)(ctx->end_time - now);
163
0
        if (ctx->msg_timeout == 0 || time_left < ctx->msg_timeout)
164
0
            ctx->msg_timeout = time_left;
165
0
    }
166
167
    /* should print error queue since transfer_cb may call ERR_clear_error() */
168
0
    OSSL_CMP_CTX_print_errors(ctx);
169
170
0
    if (ctx->server != NULL || ctx->transfer_cb != NULL)
171
0
        ossl_cmp_log1(INFO, ctx, "sending %s", req_type_str);
172
173
0
    *rep = (*transfer_cb)(ctx, req);
174
0
    ctx->msg_timeout = bak_msg_timeout;
175
176
0
    if (*rep == NULL) {
177
0
        ERR_raise_data(ERR_LIB_CMP,
178
0
            ctx->total_timeout != 0 && time(NULL) >= ctx->end_time ? CMP_R_TOTAL_TIMEOUT : CMP_R_TRANSFER_ERROR,
179
0
            "request sent: %s, expected response: %s",
180
0
            req_type_str, expected_type_str);
181
0
        return 0;
182
0
    }
183
184
0
    ctx->status = OSSL_CMP_PKISTATUS_checking_response;
185
0
    bt = OSSL_CMP_MSG_get_bodytype(*rep);
186
    /*
187
     * The body type in the 'bt' variable is not yet verified.
188
     * Still we use this preliminary value already for a progress report because
189
     * the following msg verification may also produce log entries and may fail.
190
     */
191
0
    ossl_cmp_log2(INFO, ctx, "received %s%s", ossl_cmp_bodytype_to_string(bt),
192
0
        ossl_cmp_is_error_with_waiting(*rep) ? " (waiting)" : "");
193
194
    /* copy received extraCerts to ctx->extraCertsIn so they can be retrieved */
195
0
    if (bt != OSSL_CMP_PKIBODY_POLLREP && bt != OSSL_CMP_PKIBODY_PKICONF
196
0
        && !ossl_cmp_ctx_set1_extraCertsIn(ctx, (*rep)->extraCerts))
197
0
        return 0;
198
199
0
    if (!ossl_cmp_msg_check_update(ctx, *rep, unprotected_exception,
200
0
            expected_type))
201
0
        return 0;
202
203
    /*
204
     * rep can have the expected response type, which during polling is pollRep.
205
     * When polling, also any other non-error response (the final response)
206
     * is fine here. When not yet polling, delayed delivery may be initiated
207
     * by the server returning an error message with 'waiting' status (or a
208
     * response message of expected type ip/cp/kup with 'waiting' status).
209
     */
210
0
    if (bt == expected_type
211
0
        || (expected_type == OSSL_CMP_PKIBODY_POLLREP
212
0
                ? bt != OSSL_CMP_PKIBODY_ERROR
213
0
                : ossl_cmp_is_error_with_waiting(*rep)))
214
0
        return 1;
215
216
    /* received message type is not one of the expected ones (e.g., error) */
217
0
    ERR_raise(ERR_LIB_CMP, bt == OSSL_CMP_PKIBODY_ERROR ? CMP_R_RECEIVED_ERROR : CMP_R_UNEXPECTED_PKIBODY); /* in next line for mkerr.pl */
218
219
0
    if (bt != OSSL_CMP_PKIBODY_ERROR) {
220
0
        ERR_add_error_data(3, "message type is '",
221
0
            ossl_cmp_bodytype_to_string(bt), "'");
222
0
    } else {
223
0
        OSSL_CMP_ERRORMSGCONTENT *emc = (*rep)->body->value.error;
224
0
        OSSL_CMP_PKISI *si = emc->pKIStatusInfo;
225
0
        char buf[OSSL_CMP_PKISI_BUFLEN];
226
227
0
        if (save_statusInfo(ctx, si)
228
0
            && OSSL_CMP_CTX_snprint_PKIStatus(ctx, buf,
229
0
                   sizeof(buf))
230
0
                != NULL)
231
0
            ERR_add_error_data(1, buf);
232
0
        if (emc->errorCode != NULL
233
0
            && BIO_snprintf(buf, sizeof(buf), "; errorCode: %08lX",
234
0
                   ASN1_INTEGER_get(emc->errorCode))
235
0
                > 0)
236
0
            ERR_add_error_data(1, buf);
237
0
        if (emc->errorDetails != NULL) {
238
0
            char *text = ossl_sk_ASN1_UTF8STRING2text(emc->errorDetails, ", ",
239
0
                OSSL_CMP_PKISI_BUFLEN - 1);
240
241
0
            if (text != NULL && *text != '\0')
242
0
                ERR_add_error_data(2, "; errorDetails: ", text);
243
0
            OPENSSL_free(text);
244
0
        }
245
0
        if (ctx->status != OSSL_CMP_PKISTATUS_rejection) {
246
0
            ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKISTATUS);
247
0
            if (ctx->status == OSSL_CMP_PKISTATUS_waiting)
248
0
                ctx->status = OSSL_CMP_PKISTATUS_rejection;
249
0
        }
250
0
    }
251
0
    return 0;
252
0
}
253
254
/*-
255
 * When a 'waiting' PKIStatus has been received, this function is used to
256
 * poll, which should yield a pollRep or the final response.
257
 * On receiving a pollRep, which includes a checkAfter value, it return this
258
 * value if sleep == 0, else it sleeps as long as indicated and retries.
259
 *
260
 * A transaction timeout is enabled if ctx->total_timeout is != 0.
261
 * In this case polling will continue until the timeout is reached and then
262
 * polling is done a last time even if this is before the "checkAfter" time.
263
 *
264
 * Returns -1 on receiving pollRep if sleep == 0, setting the checkAfter value.
265
 * Returns 1 on success and provides the received PKIMESSAGE in *rep.
266
 *           In this case the caller is responsible for freeing *rep.
267
 * Returns 0 on error (which includes the cases that timeout has been reached
268
 *           or a response with 'waiting' status has been received).
269
 */
270
static int poll_for_response(OSSL_CMP_CTX *ctx, int sleep, int rid,
271
    OSSL_CMP_MSG **rep, int *checkAfter)
272
186
{
273
186
    OSSL_CMP_MSG *preq = NULL;
274
186
    OSSL_CMP_MSG *prep = NULL;
275
276
186
    ossl_cmp_info(ctx,
277
186
        "received 'waiting' PKIStatus, starting to poll for response");
278
186
    *rep = NULL;
279
186
    for (;;) {
280
186
        int bak = ctx->status;
281
282
186
        ctx->status = OSSL_CMP_PKISTATUS_request;
283
186
        if ((preq = ossl_cmp_pollReq_new(ctx, rid)) == NULL)
284
186
            goto err;
285
286
0
        if (!send_receive_check(ctx, preq, &prep, OSSL_CMP_PKIBODY_POLLREP))
287
0
            goto err;
288
0
        ctx->status = bak;
289
290
        /* handle potential pollRep */
291
0
        if (OSSL_CMP_MSG_get_bodytype(prep) == OSSL_CMP_PKIBODY_POLLREP) {
292
0
            OSSL_CMP_POLLREPCONTENT *prc = prep->body->value.pollRep;
293
0
            OSSL_CMP_POLLREP *pollRep = NULL;
294
0
            int64_t check_after;
295
0
            char str[OSSL_CMP_PKISI_BUFLEN];
296
0
            int len;
297
298
0
            if (sk_OSSL_CMP_POLLREP_num(prc) > 1) {
299
0
                ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_RESPONSES_NOT_SUPPORTED);
300
0
                goto err;
301
0
            }
302
0
            pollRep = ossl_cmp_pollrepcontent_get0_pollrep(prc, rid);
303
0
            if (pollRep == NULL)
304
0
                goto err;
305
306
0
            if (!ASN1_INTEGER_get_int64(&check_after, pollRep->checkAfter)) {
307
0
                ERR_raise(ERR_LIB_CMP, CMP_R_BAD_CHECKAFTER_IN_POLLREP);
308
0
                goto err;
309
0
            }
310
0
            if (check_after < 0 || (uint64_t)check_after > (sleep ? ULONG_MAX / 1000 : INT_MAX)) {
311
0
                ERR_raise(ERR_LIB_CMP, CMP_R_CHECKAFTER_OUT_OF_RANGE);
312
0
                if (BIO_snprintf(str, OSSL_CMP_PKISI_BUFLEN, "value = %jd",
313
0
                        check_after)
314
0
                    >= 0)
315
0
                    ERR_add_error_data(1, str);
316
0
                goto err;
317
0
            }
318
319
0
            if (pollRep->reason == NULL
320
0
                || (len = BIO_snprintf(str, OSSL_CMP_PKISI_BUFLEN,
321
0
                        " with reason = '"))
322
0
                    < 0) {
323
0
                *str = '\0';
324
0
            } else {
325
0
                char *text = ossl_sk_ASN1_UTF8STRING2text(pollRep->reason, ", ",
326
0
                    sizeof(str) - len - 2);
327
328
0
                if (text == NULL
329
0
                    || BIO_snprintf(str + len, sizeof(str) - len,
330
0
                           "%s'", text)
331
0
                        < 0)
332
0
                    *str = '\0';
333
0
                OPENSSL_free(text);
334
0
            }
335
0
            ossl_cmp_log2(INFO, ctx,
336
0
                "received polling response%s; checkAfter = %ld seconds",
337
0
                str, check_after);
338
339
0
            if (ctx->total_timeout != 0) { /* timeout is not infinite */
340
0
                const int exp = OSSL_CMP_EXPECTED_RESP_TIME;
341
0
                int64_t time_left = (int64_t)(ctx->end_time - exp - time(NULL));
342
343
0
                if (time_left <= 0) {
344
0
                    ctx->status = OSSL_CMP_PKISTATUS_trans;
345
0
                    ERR_raise(ERR_LIB_CMP, CMP_R_TOTAL_TIMEOUT);
346
0
                    goto err;
347
0
                }
348
0
                if (time_left < check_after)
349
0
                    check_after = time_left;
350
                /* poll one last time just when timeout was reached */
351
0
            }
352
353
0
            OSSL_CMP_MSG_free(preq);
354
0
            preq = NULL;
355
0
            OSSL_CMP_MSG_free(prep);
356
0
            prep = NULL;
357
0
            if (sleep) {
358
0
                OSSL_sleep((unsigned long)(1000 * check_after));
359
0
            } else {
360
0
                if (checkAfter != NULL)
361
0
                    *checkAfter = (int)check_after;
362
0
                return -1; /* exits the loop */
363
0
            }
364
0
        } else if (is_crep_with_waiting(prep, rid)
365
0
            || ossl_cmp_is_error_with_waiting(prep)) {
366
            /* received status must not be 'waiting' */
367
0
            (void)ossl_cmp_exchange_error(ctx, OSSL_CMP_PKISTATUS_rejection,
368
0
                OSSL_CMP_CTX_FAILINFO_badRequest,
369
0
                "polling already started",
370
0
                0 /* errorCode */, NULL);
371
0
            ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKISTATUS);
372
0
            goto err;
373
0
        } else {
374
0
            ossl_cmp_info(ctx, "received final response after polling");
375
0
            if (!ossl_cmp_ctx_set1_first_senderNonce(ctx, NULL))
376
0
                goto err;
377
0
            break;
378
0
        }
379
0
    }
380
0
    if (prep == NULL)
381
0
        goto err;
382
383
0
    OSSL_CMP_MSG_free(preq);
384
0
    *rep = prep;
385
386
0
    return 1;
387
186
err:
388
186
    (void)ossl_cmp_ctx_set1_first_senderNonce(ctx, NULL);
389
186
    OSSL_CMP_MSG_free(preq);
390
186
    OSSL_CMP_MSG_free(prep);
391
186
    return 0;
392
0
}
393
394
static int save_senderNonce_if_waiting(OSSL_CMP_CTX *ctx,
395
    const OSSL_CMP_MSG *rep, int rid)
396
0
{
397
    /*
398
     * Lightweight CMP Profile section 4.4 states: the senderNonce of the
399
     * preceding request message because this value will be needed for checking
400
     * the recipNonce of the final response to be received after polling.
401
     */
402
0
    if ((is_crep_with_waiting(rep, rid)
403
0
            || ossl_cmp_is_error_with_waiting(rep))
404
0
        && !ossl_cmp_ctx_set1_first_senderNonce(ctx, ctx->senderNonce))
405
0
        return 0;
406
407
0
    return 1;
408
0
}
409
410
/*
411
 * Send request and get response possibly with polling initiated by error msg.
412
 * Polling for ip/cp/kup/ with 'waiting' status is handled by cert_response().
413
 */
414
static int send_receive_also_delayed(OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *req,
415
    OSSL_CMP_MSG **rep, int expected_type)
416
0
{
417
418
0
    if (!send_receive_check(ctx, req, rep, expected_type))
419
0
        return 0;
420
421
0
    if (ossl_cmp_is_error_with_waiting(*rep)) {
422
0
        if (!save_senderNonce_if_waiting(ctx, *rep, OSSL_CMP_CERTREQID_NONE))
423
0
            return 0;
424
        /* not modifying ctx->status during certConf and error exchanges */
425
0
        if (expected_type != OSSL_CMP_PKIBODY_PKICONF
426
0
            && !save_statusInfo(ctx, (*rep)->body->value.error->pKIStatusInfo))
427
0
            return 0;
428
429
0
        OSSL_CMP_MSG_free(*rep);
430
0
        *rep = NULL;
431
432
0
        if (poll_for_response(ctx, 1 /* can sleep */, OSSL_CMP_CERTREQID_NONE,
433
0
                rep, NULL /* checkAfter */)
434
0
            <= 0) {
435
0
            ERR_raise(ERR_LIB_CMP, CMP_R_POLLING_FAILED);
436
0
            return 0;
437
0
        }
438
0
    }
439
0
    if (OSSL_CMP_MSG_get_bodytype(*rep) != expected_type) {
440
0
        ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
441
0
        return 0;
442
0
    }
443
444
0
    return 1;
445
0
}
446
/*
447
 * Send certConf for IR, CR or KUR sequences and check response,
448
 * not modifying ctx->status during the certConf exchange
449
 */
450
int ossl_cmp_exchange_certConf(OSSL_CMP_CTX *ctx, int certReqId,
451
    int fail_info, const char *txt)
452
0
{
453
0
    OSSL_CMP_MSG *certConf;
454
0
    OSSL_CMP_MSG *PKIconf = NULL;
455
0
    int res = 0;
456
0
    int bak = ctx->status;
457
458
0
    ctx->status = OSSL_CMP_PKISTATUS_request;
459
    /* OSSL_CMP_certConf_new() also checks if all necessary options are set */
460
0
    certConf = ossl_cmp_certConf_new(ctx, certReqId, fail_info, txt);
461
0
    if (certConf == NULL)
462
0
        goto err;
463
464
0
    res = send_receive_also_delayed(ctx, certConf, &PKIconf,
465
0
        OSSL_CMP_PKIBODY_PKICONF);
466
467
0
    if (res)
468
0
        ctx->status = bak;
469
470
0
err:
471
0
    OSSL_CMP_MSG_free(certConf);
472
0
    OSSL_CMP_MSG_free(PKIconf);
473
0
    return res;
474
0
}
475
476
/* Send given error and check response */
477
int ossl_cmp_exchange_error(OSSL_CMP_CTX *ctx, int status, int fail_info,
478
    const char *txt, int errorCode, const char *details)
479
0
{
480
0
    OSSL_CMP_MSG *error = NULL;
481
0
    OSSL_CMP_PKISI *si = NULL;
482
0
    OSSL_CMP_MSG *PKIconf = NULL;
483
0
    int res = 0;
484
485
0
    ctx->status = OSSL_CMP_PKISTATUS_request;
486
    /* not overwriting ctx->status on error exchange */
487
0
    if ((si = OSSL_CMP_STATUSINFO_new(status, fail_info, txt)) == NULL)
488
0
        goto err;
489
    /* ossl_cmp_error_new() also checks if all necessary options are set */
490
0
    if ((error = ossl_cmp_error_new(ctx, si, errorCode, details, 0)) == NULL)
491
0
        goto err;
492
493
0
    res = send_receive_also_delayed(ctx, error,
494
0
        &PKIconf, OSSL_CMP_PKIBODY_PKICONF);
495
0
    ctx->status = OSSL_CMP_PKISTATUS_rejected_by_client;
496
497
0
err:
498
0
    OSSL_CMP_MSG_free(error);
499
0
    OSSL_CMP_PKISI_free(si);
500
0
    OSSL_CMP_MSG_free(PKIconf);
501
0
    return res;
502
0
}
503
504
/*-
505
 * Retrieve a copy of the certificate, if any, from the given CertResponse.
506
 * Take into account PKIStatusInfo of CertResponse in ctx, report it on error.
507
 * Returns NULL if not found or on error.
508
 */
509
static X509 *get1_cert_status(OSSL_CMP_CTX *ctx, int bodytype,
510
    OSSL_CMP_CERTRESPONSE *crep)
511
0
{
512
0
    char buf[OSSL_CMP_PKISI_BUFLEN];
513
0
    X509 *crt = NULL;
514
515
0
    if (!ossl_assert(ctx != NULL && crep != NULL))
516
0
        return NULL;
517
518
0
    switch (ossl_cmp_pkisi_get_status(crep->status)) {
519
0
    case OSSL_CMP_PKISTATUS_waiting:
520
0
        ossl_cmp_err(ctx,
521
0
            "received \"waiting\" status for cert when actually aiming to extract cert");
522
0
        ERR_raise(ERR_LIB_CMP, CMP_R_ENCOUNTERED_WAITING);
523
0
        goto err;
524
0
    case OSSL_CMP_PKISTATUS_grantedWithMods:
525
0
        ossl_cmp_warn(ctx, "received \"grantedWithMods\" for certificate");
526
0
        break;
527
0
    case OSSL_CMP_PKISTATUS_accepted:
528
0
        break;
529
        /* get all information in case of a rejection before going to error */
530
0
    case OSSL_CMP_PKISTATUS_rejection:
531
0
        ossl_cmp_err(ctx, "received \"rejection\" status rather than cert");
532
0
        ERR_raise(ERR_LIB_CMP, CMP_R_REQUEST_REJECTED_BY_SERVER);
533
0
        goto err;
534
0
    case OSSL_CMP_PKISTATUS_revocationWarning:
535
0
        ossl_cmp_warn(ctx,
536
0
            "received \"revocationWarning\" - a revocation of the cert is imminent");
537
0
        break;
538
0
    case OSSL_CMP_PKISTATUS_revocationNotification:
539
0
        ossl_cmp_warn(ctx,
540
0
            "received \"revocationNotification\" - a revocation of the cert has occurred");
541
0
        break;
542
0
    case OSSL_CMP_PKISTATUS_keyUpdateWarning:
543
0
        if (bodytype != OSSL_CMP_PKIBODY_KUR) {
544
0
            ERR_raise(ERR_LIB_CMP, CMP_R_ENCOUNTERED_KEYUPDATEWARNING);
545
0
            goto err;
546
0
        }
547
0
        break;
548
0
    default:
549
0
        ossl_cmp_log1(ERROR, ctx,
550
0
            "received unsupported PKIStatus %d for certificate",
551
0
            ctx->status);
552
0
        ERR_raise(ERR_LIB_CMP, CMP_R_UNKNOWN_PKISTATUS);
553
0
        goto err;
554
0
    }
555
0
    crt = ossl_cmp_certresponse_get1_cert(ctx, crep);
556
0
    if (crt == NULL) /* according to PKIStatus, we can expect a cert */
557
0
        ERR_raise(ERR_LIB_CMP, CMP_R_CERTIFICATE_NOT_FOUND);
558
559
0
    return crt;
560
561
0
err:
562
0
    if (OSSL_CMP_CTX_snprint_PKIStatus(ctx, buf, sizeof(buf)) != NULL)
563
0
        ERR_add_error_data(1, buf);
564
0
    return NULL;
565
0
}
566
567
/*-
568
 * Callback fn validating that the new certificate can be verified, using
569
 * ctx->certConf_cb_arg, which has been initialized using opt_out_trusted, and
570
 * ctx->untrusted, which at this point already contains msg->extraCerts.
571
 * Returns 0 on acceptance, else a bit field reflecting PKIFailureInfo.
572
 * Quoting from RFC 9810 section 5.1. Overall PKI Message:
573
 *     The extraCerts field can contain certificates that may be useful to
574
 *     the recipient.  For example, this can be used by a CA or RA to
575
 *     present an end entity with certificates that it needs to verify its
576
 *     own new certificate (for example, if the CA that issued the end
577
 *     entity's certificate is not a root CA for the end entity).  Note that
578
 *     this field does not necessarily contain a certification path; the
579
 *     recipient may have to sort, select from, or otherwise process the
580
 *     extra certificates in order to use them.
581
 * Note: While often handy, there is no hard requirement by CMP that
582
 * an EE must be able to validate the certificates it gets enrolled.
583
 */
584
int OSSL_CMP_certConf_cb(OSSL_CMP_CTX *ctx, X509 *cert, int fail_info,
585
    const char **text)
586
0
{
587
0
    X509_STORE *out_trusted = OSSL_CMP_CTX_get_certConf_cb_arg(ctx);
588
0
    STACK_OF(X509) *chain = NULL;
589
590
0
    (void)text; /* make (artificial) use of var to prevent compiler warning */
591
592
0
    if (fail_info != 0) /* accept any error flagged by CMP core library */
593
0
        return fail_info;
594
595
0
    if (out_trusted == NULL) {
596
0
        ossl_cmp_debug(ctx, "trying to build chain for newly enrolled cert");
597
0
        chain = X509_build_chain(cert, ctx->untrusted, out_trusted,
598
0
            0, ctx->libctx, ctx->propq);
599
0
    } else {
600
0
        X509_STORE_CTX *csc = X509_STORE_CTX_new_ex(ctx->libctx, ctx->propq);
601
602
0
        ossl_cmp_debug(ctx, "validating newly enrolled cert");
603
0
        if (csc == NULL)
604
0
            goto err;
605
0
        if (!X509_STORE_CTX_init(csc, out_trusted, cert, ctx->untrusted))
606
0
            goto err;
607
        /* disable any cert status/revocation checking etc. */
608
0
        X509_VERIFY_PARAM_clear_flags(X509_STORE_CTX_get0_param(csc),
609
0
            ~(X509_V_FLAG_USE_CHECK_TIME
610
0
                | X509_V_FLAG_NO_CHECK_TIME
611
0
                | X509_V_FLAG_PARTIAL_CHAIN
612
0
                | X509_V_FLAG_POLICY_CHECK));
613
0
        if (X509_verify_cert(csc) <= 0)
614
0
            goto err;
615
616
0
        if (!ossl_x509_add_certs_new(&chain, X509_STORE_CTX_get0_chain(csc),
617
0
                X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP
618
0
                    | X509_ADD_FLAG_NO_SS)) {
619
0
            sk_X509_free(chain);
620
0
            chain = NULL;
621
0
        }
622
0
    err:
623
0
        X509_STORE_CTX_free(csc);
624
0
    }
625
626
0
    if (sk_X509_num(chain) > 0)
627
0
        X509_free(sk_X509_shift(chain)); /* remove leaf (EE) cert */
628
0
    if (out_trusted != NULL) {
629
0
        if (chain == NULL) {
630
0
            ossl_cmp_err(ctx, "failed to validate newly enrolled cert");
631
0
            fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_incorrectData;
632
0
        } else {
633
0
            ossl_cmp_debug(ctx,
634
0
                "success validating newly enrolled cert");
635
0
        }
636
0
    } else if (chain == NULL) {
637
0
        ossl_cmp_warn(ctx, "could not build approximate chain for newly enrolled cert, resorting to received extraCerts");
638
0
        chain = OSSL_CMP_CTX_get1_extraCertsIn(ctx);
639
0
    } else {
640
0
        ossl_cmp_debug(ctx,
641
0
            "success building approximate chain for newly enrolled cert");
642
0
    }
643
0
    (void)ossl_cmp_ctx_set1_newChain(ctx, chain);
644
0
    OSSL_STACK_OF_X509_free(chain);
645
646
0
    return fail_info;
647
0
}
648
649
/*-
650
 * Perform the generic handling of certificate responses for IR/CR/KUR/P10CR.
651
 * |rid| must be OSSL_CMP_CERTREQID_NONE if not available, namely for p10cr
652
 * Returns -1 on receiving pollRep if sleep == 0, setting the checkAfter value.
653
 * Returns 1 on success and provides the received PKIMESSAGE in *resp.
654
 * Returns 0 on error (which includes the case that timeout has been reached).
655
 * Regardless of success, caller is responsible for freeing *resp (unless NULL).
656
 */
657
static int cert_response(OSSL_CMP_CTX *ctx, int sleep, int rid,
658
    OSSL_CMP_MSG **resp, int *checkAfter,
659
    ossl_unused int req_type,
660
    ossl_unused int expected_type)
661
0
{
662
0
    EVP_PKEY *rkey = NULL;
663
0
    int fail_info = 0; /* no failure */
664
0
    const char *txt = NULL;
665
0
    OSSL_CMP_CERTREPMESSAGE *crepmsg = NULL;
666
0
    OSSL_CMP_CERTRESPONSE *crep = NULL;
667
0
    OSSL_CMP_certConf_cb_t cb;
668
0
    X509 *cert;
669
0
    char *subj = NULL;
670
0
    int ret = 1;
671
0
    int rcvd_type;
672
0
    OSSL_CMP_PKISI *si;
673
674
0
    if (!ossl_assert(ctx != NULL))
675
0
        return 0;
676
677
0
retry:
678
0
    rcvd_type = OSSL_CMP_MSG_get_bodytype(*resp);
679
0
    if (IS_CREP(rcvd_type)) {
680
0
        crepmsg = (*resp)->body->value.ip; /* same for cp and kup */
681
0
        if (sk_OSSL_CMP_CERTRESPONSE_num(crepmsg->response) > 1) {
682
0
            ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_RESPONSES_NOT_SUPPORTED);
683
0
            return 0;
684
0
        }
685
0
        crep = ossl_cmp_certrepmessage_get0_certresponse(crepmsg, rid);
686
0
        if (crep == NULL)
687
0
            return 0;
688
0
        si = crep->status;
689
690
0
        if (rid == OSSL_CMP_CERTREQID_NONE) {
691
            /* for OSSL_CMP_PKIBODY_P10CR learn CertReqId from response */
692
0
            rid = ossl_cmp_asn1_get_int(crep->certReqId);
693
0
            if (rid < OSSL_CMP_CERTREQID_NONE) {
694
0
                ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID);
695
0
                return 0;
696
0
            }
697
0
        }
698
0
    } else if (rcvd_type == OSSL_CMP_PKIBODY_ERROR) {
699
0
        si = (*resp)->body->value.error->pKIStatusInfo;
700
0
    } else {
701
0
        ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
702
0
        return 0;
703
0
    }
704
705
0
    if (!save_statusInfo(ctx, si))
706
0
        return 0;
707
708
0
    if (ossl_cmp_pkisi_get_status(si) == OSSL_CMP_PKISTATUS_waiting) {
709
        /*
710
         * Here we allow both and error message with waiting indication
711
         * as well as a certificate response with waiting indication, where
712
         * its flavor (ip, cp, or kup) may not strictly match ir/cr/p10cr/kur.
713
         */
714
0
        OSSL_CMP_MSG_free(*resp);
715
0
        *resp = NULL;
716
0
        if ((ret = poll_for_response(ctx, sleep, rid, resp, checkAfter)) != 0) {
717
0
            if (ret == -1) /* at this point implies sleep == 0 */
718
0
                return ret; /* waiting */
719
0
            goto retry; /* got some response other than pollRep */
720
0
        } else {
721
0
            ERR_raise(ERR_LIB_CMP, CMP_R_POLLING_FAILED);
722
0
            return 0;
723
0
        }
724
0
    }
725
726
    /* at this point, we have received ip/cp/kup/error without waiting */
727
0
    if (rcvd_type == OSSL_CMP_PKIBODY_ERROR) {
728
0
        ERR_raise(ERR_LIB_CMP, CMP_R_RECEIVED_ERROR);
729
0
        return 0;
730
0
    }
731
    /* here we are strict on the flavor of ip/cp/kup: must match request */
732
0
    if (rcvd_type != expected_type) {
733
0
        ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
734
0
        return 0;
735
0
    }
736
737
0
    cert = get1_cert_status(ctx, (*resp)->body->type, crep);
738
0
    if (cert == NULL) {
739
0
        ERR_add_error_data(1, "; cannot extract certificate from response");
740
0
        return 0;
741
0
    }
742
0
    if (!ossl_cmp_ctx_set0_newCert(ctx, cert)) {
743
0
        X509_free(cert);
744
0
        return 0;
745
0
    }
746
747
    /*
748
     * if the CMP server returned certificates in the caPubs field, copy them
749
     * to the context so that they can be retrieved if necessary
750
     */
751
0
    if (crepmsg != NULL && crepmsg->caPubs != NULL
752
0
        && !ossl_cmp_ctx_set1_caPubs(ctx, crepmsg->caPubs))
753
0
        return 0;
754
755
0
    subj = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0);
756
0
    rkey = ossl_cmp_ctx_get0_newPubkey(ctx);
757
0
    if (rkey != NULL
758
        /* X509_check_private_key() also works if rkey is just public key */
759
0
        && !(X509_check_private_key(ctx->newCert, rkey))) {
760
0
        fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_incorrectData;
761
0
        txt = "public key in new certificate does not match our enrollment key";
762
        /*-
763
         * not calling (void)ossl_cmp_exchange_error(ctx,
764
         *                   OSSL_CMP_PKISTATUS_rejection, fail_info, txt)
765
         * not throwing CMP_R_CERTIFICATE_NOT_ACCEPTED with txt
766
         * not returning 0
767
         * since we better leave this for the certConf_cb to decide
768
         */
769
0
    }
770
771
    /*
772
     * Execute the certification checking callback function,
773
     * which can determine whether to accept a newly enrolled certificate.
774
     * It may overrule the pre-decision reflected in 'fail_info' and '*txt'.
775
     */
776
0
    cb = ctx->certConf_cb != NULL ? ctx->certConf_cb : OSSL_CMP_certConf_cb;
777
0
    if ((fail_info = cb(ctx, ctx->newCert, fail_info, &txt)) != 0
778
0
        && txt == NULL)
779
0
        txt = "CMP client did not accept it";
780
0
    if (fail_info != 0) /* immediately log error before any certConf exchange */
781
0
        ossl_cmp_log1(ERROR, ctx,
782
0
            "rejecting newly enrolled cert with subject: %s", subj);
783
    /*
784
     * certConf exchange should better be moved to do_certreq_seq() such that
785
     * also more low-level errors with CertReqMessages get reported to server
786
     */
787
0
    if (!ctx->disableConfirm
788
0
        && !ossl_cmp_hdr_has_implicitConfirm((*resp)->header)) {
789
0
        if (!ossl_cmp_exchange_certConf(ctx, rid, fail_info, txt))
790
0
            ret = 0;
791
0
    }
792
793
    /* not throwing failure earlier as transfer_cb may call ERR_clear_error() */
794
0
    if (fail_info != 0) {
795
0
        ERR_raise_data(ERR_LIB_CMP, CMP_R_CERTIFICATE_NOT_ACCEPTED,
796
0
            "rejecting newly enrolled cert with subject: %s; %s",
797
0
            subj, txt);
798
0
        ctx->status = OSSL_CMP_PKISTATUS_rejected_by_client;
799
0
        ret = 0;
800
0
    }
801
0
    OPENSSL_free(subj);
802
0
    return ret;
803
0
}
804
805
static int initial_certreq(OSSL_CMP_CTX *ctx,
806
    int req_type, const OSSL_CRMF_MSG *crm,
807
    OSSL_CMP_MSG **p_rep, int rep_type)
808
1.38k
{
809
1.38k
    OSSL_CMP_MSG *req;
810
1.38k
    int res;
811
812
1.38k
    ctx->status = OSSL_CMP_PKISTATUS_request;
813
1.38k
    if (!ossl_cmp_ctx_set0_newCert(ctx, NULL))
814
0
        return 0;
815
816
    /* also checks if all necessary options are set */
817
1.38k
    if ((req = ossl_cmp_certreq_new(ctx, req_type, crm)) == NULL)
818
1.38k
        return 0;
819
820
0
    res = send_receive_check(ctx, req, p_rep, rep_type);
821
0
    OSSL_CMP_MSG_free(req);
822
0
    return res;
823
1.38k
}
824
825
int OSSL_CMP_try_certreq(OSSL_CMP_CTX *ctx, int req_type,
826
    const OSSL_CRMF_MSG *crm, int *checkAfter)
827
186
{
828
186
    OSSL_CMP_MSG *rep = NULL;
829
186
    int is_p10 = req_type == OSSL_CMP_PKIBODY_P10CR;
830
186
    int rid = is_p10 ? OSSL_CMP_CERTREQID_NONE : OSSL_CMP_CERTREQID;
831
186
    int rep_type = is_p10 ? OSSL_CMP_PKIBODY_CP : req_type + 1;
832
186
    int res = 0;
833
834
186
    if (ctx == NULL) {
835
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
836
0
        return 0;
837
0
    }
838
839
186
    if (ctx->status != OSSL_CMP_PKISTATUS_waiting) { /* not polling already */
840
0
        if (!initial_certreq(ctx, req_type, crm, &rep, rep_type))
841
0
            goto err;
842
843
0
        if (!save_senderNonce_if_waiting(ctx, rep, rid))
844
0
            goto err;
845
186
    } else {
846
186
        if (req_type < 0)
847
0
            return ossl_cmp_exchange_error(ctx, OSSL_CMP_PKISTATUS_rejection,
848
0
                0, "polling aborted",
849
0
                0 /* errorCode */, "by application");
850
186
        res = poll_for_response(ctx, 0 /* no sleep */, rid, &rep, checkAfter);
851
186
        if (res <= 0) /* waiting or error */
852
186
            return res;
853
186
    }
854
0
    res = cert_response(ctx, 0 /* no sleep */, rid, &rep, checkAfter,
855
0
        req_type, rep_type);
856
857
0
err:
858
0
    OSSL_CMP_MSG_free(rep);
859
0
    return res;
860
0
}
861
862
/*-
863
 * Do the full sequence CR/IR/KUR/P10CR, CP/IP/KUP/CP,
864
 * certConf, PKIconf, and polling if required.
865
 * Will sleep as long as indicated by the server (according to checkAfter).
866
 * All enrollment options need to be present in the context.
867
 * Returns pointer to received certificate, or NULL if none was received.
868
 */
869
X509 *OSSL_CMP_exec_certreq(OSSL_CMP_CTX *ctx, int req_type,
870
    const OSSL_CRMF_MSG *crm)
871
1.19k
{
872
1.19k
    OSSL_CMP_MSG *rep = NULL;
873
1.19k
    int is_p10 = req_type == OSSL_CMP_PKIBODY_P10CR;
874
1.19k
    int rid = is_p10 ? OSSL_CMP_CERTREQID_NONE : OSSL_CMP_CERTREQID;
875
1.19k
    int rep_type = is_p10 ? OSSL_CMP_PKIBODY_CP : req_type + 1;
876
1.19k
    X509 *result = NULL;
877
878
1.19k
    if (ctx == NULL) {
879
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
880
0
        return NULL;
881
0
    }
882
883
1.19k
    if (!initial_certreq(ctx, req_type, crm, &rep, rep_type))
884
1.19k
        goto err;
885
886
0
    if (!save_senderNonce_if_waiting(ctx, rep, rid))
887
0
        goto err;
888
889
0
    if (cert_response(ctx, 1 /* sleep */, rid, &rep, NULL, req_type, rep_type)
890
0
        <= 0)
891
0
        goto err;
892
893
0
    result = ctx->newCert;
894
1.19k
err:
895
1.19k
    OSSL_CMP_MSG_free(rep);
896
1.19k
    return result;
897
0
}
898
899
int OSSL_CMP_exec_RR_ses(OSSL_CMP_CTX *ctx)
900
155
{
901
155
    OSSL_CMP_MSG *rr = NULL;
902
155
    OSSL_CMP_MSG *rp = NULL;
903
155
    const int num_RevDetails = 1;
904
155
    const int rsid = OSSL_CMP_REVREQSID;
905
155
    OSSL_CMP_REVREPCONTENT *rrep = NULL;
906
155
    OSSL_CMP_PKISI *si = NULL;
907
155
    char buf[OSSL_CMP_PKISI_BUFLEN];
908
155
    int ret = 0;
909
910
155
    if (ctx == NULL) {
911
0
        ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
912
0
        return 0;
913
0
    }
914
155
    ctx->status = OSSL_CMP_PKISTATUS_request;
915
155
    if (ctx->oldCert == NULL && ctx->p10CSR == NULL
916
0
        && (ctx->serialNumber == NULL || ctx->issuer == NULL)) {
917
0
        ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_REFERENCE_CERT);
918
0
        return 0;
919
0
    }
920
921
    /* OSSL_CMP_rr_new() also checks if all necessary options are set */
922
155
    if ((rr = ossl_cmp_rr_new(ctx)) == NULL)
923
155
        goto end;
924
925
0
    if (!send_receive_also_delayed(ctx, rr, &rp, OSSL_CMP_PKIBODY_RP))
926
0
        goto end;
927
928
0
    rrep = rp->body->value.rp;
929
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
930
    if (sk_OSSL_CMP_PKISI_num(rrep->status) != num_RevDetails) {
931
        ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_RP_COMPONENT_COUNT);
932
        goto end;
933
    }
934
#else
935
0
    if (sk_OSSL_CMP_PKISI_num(rrep->status) < 1) {
936
0
        ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_RP_COMPONENT_COUNT);
937
0
        goto end;
938
0
    }
939
0
#endif
940
941
    /* evaluate PKIStatus field */
942
0
    si = ossl_cmp_revrepcontent_get_pkisi(rrep, rsid);
943
0
    if (!save_statusInfo(ctx, si))
944
0
        goto err;
945
0
    switch (ossl_cmp_pkisi_get_status(si)) {
946
0
    case OSSL_CMP_PKISTATUS_accepted:
947
0
        ossl_cmp_info(ctx, "revocation accepted (PKIStatus=accepted)");
948
0
        ret = 1;
949
0
        break;
950
0
    case OSSL_CMP_PKISTATUS_grantedWithMods:
951
0
        ossl_cmp_info(ctx, "revocation accepted (PKIStatus=grantedWithMods)");
952
0
        ret = 1;
953
0
        break;
954
0
    case OSSL_CMP_PKISTATUS_rejection:
955
0
        ERR_raise(ERR_LIB_CMP, CMP_R_REQUEST_REJECTED_BY_SERVER);
956
0
        goto err;
957
0
    case OSSL_CMP_PKISTATUS_revocationWarning:
958
0
        ossl_cmp_info(ctx, "revocation accepted (PKIStatus=revocationWarning)");
959
0
        ret = 1;
960
0
        break;
961
0
    case OSSL_CMP_PKISTATUS_revocationNotification:
962
        /* interpretation as warning or error depends on CA */
963
0
        ossl_cmp_warn(ctx,
964
0
            "revocation accepted (PKIStatus=revocationNotification)");
965
0
        ret = 1;
966
0
        break;
967
0
    case OSSL_CMP_PKISTATUS_waiting:
968
0
    case OSSL_CMP_PKISTATUS_keyUpdateWarning:
969
0
        ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKISTATUS);
970
0
        goto err;
971
0
    default:
972
0
        ERR_raise(ERR_LIB_CMP, CMP_R_UNKNOWN_PKISTATUS);
973
0
        goto err;
974
0
    }
975
976
    /* check any present CertId in optional revCerts field */
977
0
    if (sk_OSSL_CRMF_CERTID_num(rrep->revCerts) >= 1) {
978
0
        OSSL_CRMF_CERTID *cid;
979
0
        OSSL_CRMF_CERTTEMPLATE *tmpl = sk_OSSL_CMP_REVDETAILS_value(rr->body->value.rr, rsid)->certDetails;
980
0
        const X509_NAME *issuer = OSSL_CRMF_CERTTEMPLATE_get0_issuer(tmpl);
981
0
        const ASN1_INTEGER *serial = OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(tmpl);
982
983
0
        if (sk_OSSL_CRMF_CERTID_num(rrep->revCerts) != num_RevDetails) {
984
0
            ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_RP_COMPONENT_COUNT);
985
0
            ret = 0;
986
0
            goto err;
987
0
        }
988
0
        if ((cid = ossl_cmp_revrepcontent_get_CertId(rrep, rsid)) == NULL) {
989
0
            ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_CERTID);
990
0
            ret = 0;
991
0
            goto err;
992
0
        }
993
0
        if (X509_NAME_cmp(issuer, OSSL_CRMF_CERTID_get0_issuer(cid)) != 0) {
994
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
995
            ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_CERTID_IN_RP);
996
            ret = 0;
997
            goto err;
998
#endif
999
0
        }
1000
0
        if (ASN1_INTEGER_cmp(serial,
1001
0
                OSSL_CRMF_CERTID_get0_serialNumber(cid))
1002
0
            != 0) {
1003
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1004
            ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_SERIAL_IN_RP);
1005
            ret = 0;
1006
            goto err;
1007
#endif
1008
0
        }
1009
0
    }
1010
1011
    /* check number of any optionally present crls */
1012
0
    if (rrep->crls != NULL && sk_X509_CRL_num(rrep->crls) != num_RevDetails) {
1013
0
        ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_RP_COMPONENT_COUNT);
1014
0
        ret = 0;
1015
0
        goto err;
1016
0
    }
1017
1018
0
err:
1019
0
    if (ret == 0
1020
0
        && OSSL_CMP_CTX_snprint_PKIStatus(ctx, buf, sizeof(buf)) != NULL)
1021
0
        ERR_add_error_data(1, buf);
1022
1023
155
end:
1024
155
    OSSL_CMP_MSG_free(rr);
1025
155
    OSSL_CMP_MSG_free(rp);
1026
155
    return ret;
1027
0
}
1028
1029
STACK_OF(OSSL_CMP_ITAV) *OSSL_CMP_exec_GENM_ses(OSSL_CMP_CTX *ctx)
1030
1.03k
{
1031
1.03k
    OSSL_CMP_MSG *genm;
1032
1.03k
    OSSL_CMP_MSG *genp = NULL;
1033
1.03k
    STACK_OF(OSSL_CMP_ITAV) *itavs = NULL;
1034
1035
1.03k
    if (ctx == NULL) {
1036
0
        ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
1037
0
        return NULL;
1038
0
    }
1039
1.03k
    ctx->status = OSSL_CMP_PKISTATUS_request;
1040
1041
1.03k
    if ((genm = ossl_cmp_genm_new(ctx)) == NULL)
1042
1.03k
        goto err;
1043
1044
0
    if (!send_receive_also_delayed(ctx, genm, &genp, OSSL_CMP_PKIBODY_GENP))
1045
0
        goto err;
1046
0
    ctx->status = OSSL_CMP_PKISTATUS_accepted;
1047
1048
0
    itavs = genp->body->value.genp;
1049
0
    if (itavs == NULL)
1050
0
        itavs = sk_OSSL_CMP_ITAV_new_null();
1051
    /* received stack of itavs not to be freed with the genp */
1052
0
    genp->body->value.genp = NULL;
1053
1054
1.03k
err:
1055
1.03k
    OSSL_CMP_MSG_free(genm);
1056
1.03k
    OSSL_CMP_MSG_free(genp);
1057
1058
1.03k
    return itavs; /* NULL indicates error case */
1059
0
}