Coverage Report

Created: 2025-12-04 06:33

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