Coverage Report

Created: 2026-09-12 06:55

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