Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/cmp/cmp_server.c
Line
Count
Source
1
/*
2
 * Copyright 2007-2023 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
/* general CMP server functions */
13
14
#include <openssl/asn1t.h>
15
16
#include "cmp_local.h"
17
18
/* explicit #includes not strictly needed since implied by the above: */
19
#include <openssl/cmp.h>
20
#include <openssl/err.h>
21
22
/* the context for the generic CMP server */
23
struct ossl_cmp_srv_ctx_st {
24
    void *custom_ctx; /* pointer to application-specific server context */
25
    OSSL_CMP_CTX *ctx; /* Client CMP context, reusing transactionID etc. */
26
    int certReqId; /* id of last ir/cr/kur, OSSL_CMP_CERTREQID_NONE for p10cr */
27
28
    OSSL_CMP_SRV_cert_request_cb_t process_cert_request;
29
    OSSL_CMP_SRV_rr_cb_t process_rr;
30
    OSSL_CMP_SRV_genm_cb_t process_genm;
31
    OSSL_CMP_SRV_error_cb_t process_error;
32
    OSSL_CMP_SRV_certConf_cb_t process_certConf;
33
    OSSL_CMP_SRV_pollReq_cb_t process_pollReq;
34
35
    int sendUnprotectedErrors; /* Send error and rejection msgs unprotected */
36
    int acceptUnprotected; /* Accept requests with no/invalid prot. */
37
    int acceptRAVerified; /* Accept ir/cr/kur with POPO RAVerified */
38
    int grantImplicitConfirm; /* Grant implicit confirmation if requested */
39
40
}; /* OSSL_CMP_SRV_CTX */
41
42
void OSSL_CMP_SRV_CTX_free(OSSL_CMP_SRV_CTX *srv_ctx)
43
39.3k
{
44
39.3k
    if (srv_ctx == NULL)
45
0
        return;
46
47
39.3k
    OSSL_CMP_CTX_free(srv_ctx->ctx);
48
39.3k
    OPENSSL_free(srv_ctx);
49
39.3k
}
50
51
OSSL_CMP_SRV_CTX *OSSL_CMP_SRV_CTX_new(OSSL_LIB_CTX *libctx, const char *propq)
52
39.3k
{
53
39.3k
    OSSL_CMP_SRV_CTX *ctx = OPENSSL_zalloc(sizeof(OSSL_CMP_SRV_CTX));
54
55
39.3k
    if (ctx == NULL)
56
0
        goto err;
57
58
39.3k
    if ((ctx->ctx = OSSL_CMP_CTX_new(libctx, propq)) == NULL)
59
0
        goto err;
60
39.3k
    ctx->certReqId = OSSL_CMP_CERTREQID_INVALID;
61
62
    /* all other elements are initialized to 0 or NULL, respectively */
63
39.3k
    return ctx;
64
0
err:
65
0
    OSSL_CMP_SRV_CTX_free(ctx);
66
0
    return NULL;
67
39.3k
}
68
69
int OSSL_CMP_SRV_CTX_init(OSSL_CMP_SRV_CTX *srv_ctx, void *custom_ctx,
70
    OSSL_CMP_SRV_cert_request_cb_t process_cert_request,
71
    OSSL_CMP_SRV_rr_cb_t process_rr,
72
    OSSL_CMP_SRV_genm_cb_t process_genm,
73
    OSSL_CMP_SRV_error_cb_t process_error,
74
    OSSL_CMP_SRV_certConf_cb_t process_certConf,
75
    OSSL_CMP_SRV_pollReq_cb_t process_pollReq)
76
39.3k
{
77
39.3k
    if (srv_ctx == NULL) {
78
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
79
0
        return 0;
80
0
    }
81
39.3k
    srv_ctx->custom_ctx = custom_ctx;
82
39.3k
    srv_ctx->process_cert_request = process_cert_request;
83
39.3k
    srv_ctx->process_rr = process_rr;
84
39.3k
    srv_ctx->process_genm = process_genm;
85
39.3k
    srv_ctx->process_error = process_error;
86
39.3k
    srv_ctx->process_certConf = process_certConf;
87
39.3k
    srv_ctx->process_pollReq = process_pollReq;
88
39.3k
    return 1;
89
39.3k
}
90
91
OSSL_CMP_CTX *OSSL_CMP_SRV_CTX_get0_cmp_ctx(const OSSL_CMP_SRV_CTX *srv_ctx)
92
39.3k
{
93
39.3k
    if (srv_ctx == NULL) {
94
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
95
0
        return NULL;
96
0
    }
97
39.3k
    return srv_ctx->ctx;
98
39.3k
}
99
100
void *OSSL_CMP_SRV_CTX_get0_custom_ctx(const OSSL_CMP_SRV_CTX *srv_ctx)
101
0
{
102
0
    if (srv_ctx == NULL) {
103
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
104
0
        return NULL;
105
0
    }
106
0
    return srv_ctx->custom_ctx;
107
0
}
108
109
int OSSL_CMP_SRV_CTX_set_send_unprotected_errors(OSSL_CMP_SRV_CTX *srv_ctx,
110
    int val)
111
0
{
112
0
    if (srv_ctx == NULL) {
113
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
114
0
        return 0;
115
0
    }
116
0
    srv_ctx->sendUnprotectedErrors = val != 0;
117
0
    return 1;
118
0
}
119
120
int OSSL_CMP_SRV_CTX_set_accept_unprotected(OSSL_CMP_SRV_CTX *srv_ctx, int val)
121
0
{
122
0
    if (srv_ctx == NULL) {
123
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
124
0
        return 0;
125
0
    }
126
0
    srv_ctx->acceptUnprotected = val != 0;
127
0
    return 1;
128
0
}
129
130
int OSSL_CMP_SRV_CTX_set_accept_raverified(OSSL_CMP_SRV_CTX *srv_ctx, int val)
131
0
{
132
0
    if (srv_ctx == NULL) {
133
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
134
0
        return 0;
135
0
    }
136
0
    srv_ctx->acceptRAVerified = val != 0;
137
0
    return 1;
138
0
}
139
140
int OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(OSSL_CMP_SRV_CTX *srv_ctx,
141
    int val)
142
0
{
143
0
    if (srv_ctx == NULL) {
144
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
145
0
        return 0;
146
0
    }
147
0
    srv_ctx->grantImplicitConfirm = val != 0;
148
0
    return 1;
149
0
}
150
151
/*
152
 * Processes an ir/cr/p10cr/kur and returns a certification response.
153
 * Only handles the first certification request contained in req
154
 * returns an ip/cp/kup on success and NULL on error
155
 */
156
static OSSL_CMP_MSG *process_cert_request(OSSL_CMP_SRV_CTX *srv_ctx,
157
    const OSSL_CMP_MSG *req)
158
1.27k
{
159
1.27k
    OSSL_CMP_MSG *msg = NULL;
160
1.27k
    OSSL_CMP_PKISI *si = NULL;
161
1.27k
    X509 *certOut = NULL;
162
1.27k
    STACK_OF(X509) *chainOut = NULL, *caPubs = NULL;
163
1.27k
    const OSSL_CRMF_MSG *crm = NULL;
164
1.27k
    const X509_REQ *p10cr = NULL;
165
1.27k
    int bodytype;
166
1.27k
    int certReqId;
167
168
1.27k
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
169
0
        return NULL;
170
171
1.27k
    switch (OSSL_CMP_MSG_get_bodytype(req)) {
172
623
    case OSSL_CMP_PKIBODY_P10CR:
173
815
    case OSSL_CMP_PKIBODY_CR:
174
815
        bodytype = OSSL_CMP_PKIBODY_CP;
175
815
        break;
176
283
    case OSSL_CMP_PKIBODY_IR:
177
283
        bodytype = OSSL_CMP_PKIBODY_IP;
178
283
        break;
179
179
    case OSSL_CMP_PKIBODY_KUR:
180
179
        bodytype = OSSL_CMP_PKIBODY_KUP;
181
179
        break;
182
0
    default:
183
0
        ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
184
0
        return NULL;
185
1.27k
    }
186
187
1.27k
    if (OSSL_CMP_MSG_get_bodytype(req) == OSSL_CMP_PKIBODY_P10CR) {
188
623
        certReqId = OSSL_CMP_CERTREQID_NONE; /* p10cr does not include an Id */
189
623
        p10cr = req->body->value.p10cr;
190
654
    } else {
191
654
        OSSL_CRMF_MSGS *reqs = req->body->value.ir; /* same for cr and kur */
192
193
654
        if (sk_OSSL_CRMF_MSG_num(reqs) != 1) {
194
186
            ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED);
195
186
            return NULL;
196
186
        }
197
198
468
        if ((crm = sk_OSSL_CRMF_MSG_value(reqs, OSSL_CMP_CERTREQID)) == NULL) {
199
0
            ERR_raise(ERR_LIB_CMP, CMP_R_CERTREQMSG_NOT_FOUND);
200
0
            return NULL;
201
0
        }
202
468
        certReqId = OSSL_CRMF_MSG_get_certReqId(crm);
203
468
        if (certReqId != OSSL_CMP_CERTREQID) {
204
131
            ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID);
205
131
            return 0;
206
131
        }
207
468
    }
208
960
    srv_ctx->certReqId = certReqId;
209
210
960
    if (!ossl_cmp_verify_popo(srv_ctx->ctx, req, srv_ctx->acceptRAVerified)) {
211
        /* Proof of possession could not be verified */
212
0
        si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection,
213
0
            1 << OSSL_CMP_PKIFAILUREINFO_badPOP,
214
0
            ERR_reason_error_string(ERR_peek_error()));
215
0
        if (si == NULL)
216
0
            return NULL;
217
960
    } else {
218
960
        OSSL_CMP_PKIHEADER *hdr = OSSL_CMP_MSG_get0_header(req);
219
220
960
        si = srv_ctx->process_cert_request(srv_ctx, req, certReqId, crm, p10cr,
221
960
            &certOut, &chainOut, &caPubs);
222
960
        if (si == NULL)
223
960
            goto err;
224
        /* set OSSL_CMP_OPT_IMPLICIT_CONFIRM if and only if transaction ends */
225
0
        if (!OSSL_CMP_CTX_set_option(srv_ctx->ctx,
226
0
                OSSL_CMP_OPT_IMPLICIT_CONFIRM,
227
0
                ossl_cmp_hdr_has_implicitConfirm(hdr)
228
0
                    && srv_ctx->grantImplicitConfirm
229
                    /* do not set if polling starts: */
230
0
                    && certOut != NULL))
231
0
            goto err;
232
0
    }
233
234
0
    msg = ossl_cmp_certrep_new(srv_ctx->ctx, bodytype, certReqId, si,
235
0
        certOut, NULL /* enc */, chainOut, caPubs,
236
0
        srv_ctx->sendUnprotectedErrors);
237
0
    if (msg == NULL)
238
0
        ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTREP);
239
240
960
err:
241
960
    OSSL_CMP_PKISI_free(si);
242
960
    X509_free(certOut);
243
960
    sk_X509_pop_free(chainOut, X509_free);
244
960
    sk_X509_pop_free(caPubs, X509_free);
245
960
    return msg;
246
0
}
247
248
static OSSL_CMP_MSG *process_rr(OSSL_CMP_SRV_CTX *srv_ctx,
249
    const OSSL_CMP_MSG *req)
250
151
{
251
151
    OSSL_CMP_MSG *msg = NULL;
252
151
    OSSL_CMP_REVDETAILS *details;
253
151
    OSSL_CRMF_CERTID *certId = NULL;
254
151
    OSSL_CRMF_CERTTEMPLATE *tmpl;
255
151
    const X509_NAME *issuer;
256
151
    const ASN1_INTEGER *serial;
257
151
    OSSL_CMP_PKISI *si;
258
259
151
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
260
0
        return NULL;
261
262
151
    if (sk_OSSL_CMP_REVDETAILS_num(req->body->value.rr) != 1) {
263
78
        ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED);
264
78
        return NULL;
265
78
    }
266
267
73
    if ((details = sk_OSSL_CMP_REVDETAILS_value(req->body->value.rr,
268
73
             OSSL_CMP_REVREQSID))
269
73
        == NULL) {
270
0
        ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
271
0
        return NULL;
272
0
    }
273
274
73
    tmpl = details->certDetails;
275
73
    issuer = OSSL_CRMF_CERTTEMPLATE_get0_issuer(tmpl);
276
73
    serial = OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(tmpl);
277
73
    if (issuer != NULL && serial != NULL
278
50
        && (certId = OSSL_CRMF_CERTID_gen(issuer, serial)) == NULL)
279
0
        return NULL;
280
73
    if ((si = srv_ctx->process_rr(srv_ctx, req, issuer, serial)) == NULL)
281
73
        goto err;
282
283
0
    if ((msg = ossl_cmp_rp_new(srv_ctx->ctx, si, certId,
284
0
             srv_ctx->sendUnprotectedErrors))
285
0
        == NULL)
286
0
        ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_RR);
287
288
73
err:
289
73
    OSSL_CRMF_CERTID_free(certId);
290
73
    OSSL_CMP_PKISI_free(si);
291
73
    return msg;
292
0
}
293
294
/*
295
 * Processes genm and creates a genp message mirroring the contents of the
296
 * incoming message
297
 */
298
static OSSL_CMP_MSG *process_genm(OSSL_CMP_SRV_CTX *srv_ctx,
299
    const OSSL_CMP_MSG *req)
300
396
{
301
396
    OSSL_CMP_GENMSGCONTENT *itavs;
302
396
    OSSL_CMP_MSG *msg;
303
304
396
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
305
0
        return NULL;
306
307
396
    if (!srv_ctx->process_genm(srv_ctx, req, req->body->value.genm, &itavs))
308
396
        return NULL;
309
310
0
    msg = ossl_cmp_genp_new(srv_ctx->ctx, itavs);
311
0
    sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
312
0
    return msg;
313
396
}
314
315
static OSSL_CMP_MSG *process_error(OSSL_CMP_SRV_CTX *srv_ctx,
316
    const OSSL_CMP_MSG *req)
317
896
{
318
896
    OSSL_CMP_ERRORMSGCONTENT *errorContent;
319
896
    OSSL_CMP_MSG *msg;
320
321
896
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
322
0
        return NULL;
323
896
    errorContent = req->body->value.error;
324
896
    srv_ctx->process_error(srv_ctx, req, errorContent->pKIStatusInfo,
325
896
        errorContent->errorCode, errorContent->errorDetails);
326
327
896
    if ((msg = ossl_cmp_pkiconf_new(srv_ctx->ctx)) == NULL)
328
896
        ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF);
329
896
    return msg;
330
896
}
331
332
static OSSL_CMP_MSG *process_certConf(OSSL_CMP_SRV_CTX *srv_ctx,
333
    const OSSL_CMP_MSG *req)
334
251
{
335
251
    OSSL_CMP_CTX *ctx;
336
251
    OSSL_CMP_CERTCONFIRMCONTENT *ccc;
337
251
    int num;
338
251
    OSSL_CMP_MSG *msg = NULL;
339
251
    OSSL_CMP_CERTSTATUS *status = NULL;
340
341
251
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
342
0
        return NULL;
343
344
251
    ctx = srv_ctx->ctx;
345
251
    ccc = req->body->value.certConf;
346
251
    num = sk_OSSL_CMP_CERTSTATUS_num(ccc);
347
348
251
    if (OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM) == 1
349
251
        || ctx->status != OSSL_CMP_PKISTATUS_trans) {
350
251
        ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_UNEXPECTED_CERTCONF);
351
251
        return NULL;
352
251
    }
353
354
0
    if (num == 0) {
355
0
        ossl_cmp_err(ctx, "certificate rejected by client");
356
0
    } else {
357
0
        if (num > 1)
358
0
            ossl_cmp_warn(ctx, "All CertStatus but the first will be ignored");
359
0
        status = sk_OSSL_CMP_CERTSTATUS_value(ccc, OSSL_CMP_CERTREQID);
360
0
    }
361
362
0
    if (status != NULL) {
363
0
        int certReqId = ossl_cmp_asn1_get_int(status->certReqId);
364
0
        ASN1_OCTET_STRING *certHash = status->certHash;
365
0
        OSSL_CMP_PKISI *si = status->statusInfo;
366
367
0
        if (certReqId != srv_ctx->certReqId) {
368
0
            ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID);
369
0
            return NULL;
370
0
        }
371
0
        if (!srv_ctx->process_certConf(srv_ctx, req, certReqId, certHash, si))
372
0
            return NULL; /* reason code may be: CMP_R_CERTHASH_UNMATCHED */
373
374
0
        if (si != NULL
375
0
            && ossl_cmp_pkisi_get_status(si) != OSSL_CMP_PKISTATUS_accepted) {
376
0
            int pki_status = ossl_cmp_pkisi_get_status(si);
377
0
            const char *str = ossl_cmp_PKIStatus_to_string(pki_status);
378
379
0
            ossl_cmp_log2(INFO, ctx, "certificate rejected by client %s %s",
380
0
                str == NULL ? "without" : "with",
381
0
                str == NULL ? "PKIStatus" : str);
382
0
        }
383
0
    }
384
385
0
    if ((msg = ossl_cmp_pkiconf_new(ctx)) == NULL)
386
0
        ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF);
387
0
    return msg;
388
0
}
389
390
static OSSL_CMP_MSG *process_pollReq(OSSL_CMP_SRV_CTX *srv_ctx,
391
    const OSSL_CMP_MSG *req)
392
162
{
393
162
    OSSL_CMP_POLLREQCONTENT *prc;
394
162
    OSSL_CMP_POLLREQ *pr;
395
162
    int certReqId;
396
162
    OSSL_CMP_MSG *certReq;
397
162
    int64_t check_after = 0;
398
162
    OSSL_CMP_MSG *msg = NULL;
399
400
162
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
401
0
        return NULL;
402
403
162
    prc = req->body->value.pollReq;
404
162
    if (sk_OSSL_CMP_POLLREQ_num(prc) != 1) {
405
11
        ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED);
406
11
        return NULL;
407
11
    }
408
409
151
    pr = sk_OSSL_CMP_POLLREQ_value(prc, OSSL_CMP_CERTREQID);
410
151
    certReqId = ossl_cmp_asn1_get_int(pr->certReqId);
411
151
    if (certReqId != srv_ctx->certReqId) {
412
91
        ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID);
413
91
        return NULL;
414
91
    }
415
60
    if (!srv_ctx->process_pollReq(srv_ctx, req, certReqId,
416
60
            &certReq, &check_after))
417
60
        return NULL;
418
419
0
    if (certReq != NULL) {
420
0
        msg = process_cert_request(srv_ctx, certReq);
421
0
        OSSL_CMP_MSG_free(certReq);
422
0
    } else {
423
0
        if ((msg = ossl_cmp_pollRep_new(srv_ctx->ctx, certReqId,
424
0
                 check_after))
425
0
            == NULL)
426
0
            ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREP);
427
0
    }
428
0
    return msg;
429
60
}
430
431
/*
432
 * Determine whether missing/invalid protection of request message is allowed.
433
 * Return 1 on acceptance, 0 on rejection, or -1 on (internal) error.
434
 */
435
static int unprotected_exception(const OSSL_CMP_CTX *ctx,
436
    const OSSL_CMP_MSG *req,
437
    int invalid_protection,
438
    int accept_unprotected_requests)
439
13.6k
{
440
13.6k
    if (!ossl_assert(ctx != NULL && req != NULL))
441
0
        return -1;
442
443
13.6k
    if (accept_unprotected_requests) {
444
0
        ossl_cmp_log1(WARN, ctx, "ignoring %s protection of request message",
445
0
            invalid_protection ? "invalid" : "missing");
446
0
        return 1;
447
0
    }
448
13.6k
    if (OSSL_CMP_MSG_get_bodytype(req) == OSSL_CMP_PKIBODY_ERROR
449
896
        && OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS) == 1) {
450
0
        ossl_cmp_warn(ctx, "ignoring missing protection of error message");
451
0
        return 1;
452
0
    }
453
13.6k
    return 0;
454
13.6k
}
455
456
/*
457
 * returns created message and NULL on internal error
458
 */
459
OSSL_CMP_MSG *OSSL_CMP_SRV_process_request(OSSL_CMP_SRV_CTX *srv_ctx,
460
    const OSSL_CMP_MSG *req)
461
7.07k
{
462
7.07k
    OSSL_CMP_CTX *ctx;
463
7.07k
    ASN1_OCTET_STRING *backup_secret;
464
7.07k
    OSSL_CMP_PKIHEADER *hdr;
465
7.07k
    int req_type, rsp_type;
466
7.07k
    int req_verified = 0;
467
7.07k
    OSSL_CMP_MSG *rsp = NULL;
468
469
7.07k
    if (srv_ctx == NULL || srv_ctx->ctx == NULL
470
7.07k
        || req == NULL || req->body == NULL
471
7.07k
        || (hdr = OSSL_CMP_MSG_get0_header(req)) == NULL) {
472
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
473
0
        return 0;
474
0
    }
475
7.07k
    ctx = srv_ctx->ctx;
476
7.07k
    backup_secret = ctx->secretValue;
477
7.07k
    req_type = OSSL_CMP_MSG_get_bodytype(req);
478
7.07k
    ossl_cmp_log1(DEBUG, ctx,
479
7.07k
        "received %s", ossl_cmp_bodytype_to_string(req_type));
480
481
    /*
482
     * Some things need to be done already before validating the message in
483
     * order to be able to send an error message as far as needed and possible.
484
     */
485
7.07k
    if (hdr->sender->type != GEN_DIRNAME) {
486
4.19k
        ERR_raise(ERR_LIB_CMP, CMP_R_SENDER_GENERALNAME_TYPE_NOT_SUPPORTED);
487
4.19k
        goto err;
488
4.19k
    }
489
2.88k
    if (!OSSL_CMP_CTX_set1_recipient(ctx, hdr->sender->d.directoryName))
490
2
        goto err;
491
492
2.88k
    switch (req_type) {
493
283
    case OSSL_CMP_PKIBODY_IR:
494
475
    case OSSL_CMP_PKIBODY_CR:
495
1.09k
    case OSSL_CMP_PKIBODY_P10CR:
496
1.27k
    case OSSL_CMP_PKIBODY_KUR:
497
1.29k
    case OSSL_CMP_PKIBODY_RR:
498
1.34k
    case OSSL_CMP_PKIBODY_GENM:
499
1.46k
    case OSSL_CMP_PKIBODY_ERROR:
500
1.46k
        if (ctx->transactionID != NULL) {
501
0
            char *tid;
502
503
0
            tid = OPENSSL_buf2hexstr(ctx->transactionID->data,
504
0
                ctx->transactionID->length);
505
0
            if (tid != NULL)
506
0
                ossl_cmp_log1(WARN, ctx,
507
0
                    "Assuming that last transaction with ID=%s got aborted",
508
0
                    tid);
509
0
            OPENSSL_free(tid);
510
0
        }
511
        /* start of a new transaction, reset transactionID and senderNonce */
512
1.46k
        if (!OSSL_CMP_CTX_set1_transactionID(ctx, NULL)
513
1.46k
            || !OSSL_CMP_CTX_set1_senderNonce(ctx, NULL))
514
0
            goto err;
515
1.46k
        break;
516
1.46k
    default:
517
        /* transactionID should be already initialized */
518
1.42k
        if (ctx->transactionID == NULL) {
519
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
520
            ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
521
            goto err;
522
#endif
523
1.42k
        }
524
2.88k
    }
525
526
2.88k
    req_verified = ossl_cmp_msg_check_update(ctx, req, unprotected_exception,
527
2.88k
        srv_ctx->acceptUnprotected);
528
2.88k
    if (ctx->secretValue != NULL && ctx->pkey != NULL
529
0
        && ossl_cmp_hdr_get_protection_nid(hdr) != NID_id_PasswordBasedMAC)
530
0
        ctx->secretValue = NULL; /* use MSG_SIG_ALG when protecting rsp */
531
2.88k
    if (!req_verified)
532
0
        goto err;
533
534
2.88k
    switch (req_type) {
535
283
    case OSSL_CMP_PKIBODY_IR:
536
475
    case OSSL_CMP_PKIBODY_CR:
537
1.09k
    case OSSL_CMP_PKIBODY_P10CR:
538
1.27k
    case OSSL_CMP_PKIBODY_KUR:
539
1.27k
        if (srv_ctx->process_cert_request == NULL)
540
1.27k
            ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
541
1.27k
        else
542
1.27k
            rsp = process_cert_request(srv_ctx, req);
543
1.27k
        break;
544
22
    case OSSL_CMP_PKIBODY_RR:
545
22
        if (srv_ctx->process_rr == NULL)
546
22
            ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
547
22
        else
548
22
            rsp = process_rr(srv_ctx, req);
549
22
        break;
550
42
    case OSSL_CMP_PKIBODY_GENM:
551
42
        if (srv_ctx->process_genm == NULL)
552
42
            ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
553
42
        else
554
42
            rsp = process_genm(srv_ctx, req);
555
42
        break;
556
124
    case OSSL_CMP_PKIBODY_ERROR:
557
124
        if (srv_ctx->process_error == NULL)
558
124
            ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
559
124
        else
560
124
            rsp = process_error(srv_ctx, req);
561
124
        break;
562
19
    case OSSL_CMP_PKIBODY_CERTCONF:
563
19
        if (srv_ctx->process_certConf == NULL)
564
19
            ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
565
19
        else
566
19
            rsp = process_certConf(srv_ctx, req);
567
19
        break;
568
162
    case OSSL_CMP_PKIBODY_POLLREQ:
569
162
        if (srv_ctx->process_pollReq == NULL)
570
162
            ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
571
162
        else
572
162
            rsp = process_pollReq(srv_ctx, req);
573
162
        break;
574
1.24k
    default:
575
1.24k
        ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
576
1.24k
        break;
577
2.88k
    }
578
579
7.07k
err:
580
7.07k
    if (rsp == NULL) {
581
        /* on error, try to respond with CMP error message to client */
582
7.07k
        const char *data = NULL, *reason = NULL;
583
7.07k
        int flags = 0;
584
7.07k
        unsigned long err = ERR_peek_error_data(&data, &flags);
585
7.07k
        int fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_badRequest;
586
7.07k
        OSSL_CMP_PKISI *si = NULL;
587
588
7.07k
        if (!req_verified) {
589
            /*
590
             * Above ossl_cmp_msg_check_update() was not successfully executed,
591
             * which normally would set ctx->transactionID and ctx->recipNonce.
592
             * So anyway try to provide the right transactionID and recipNonce,
593
             * while ignoring any (extra) error in next two function calls.
594
             */
595
4.19k
            if (ctx->transactionID == NULL)
596
4.19k
                (void)OSSL_CMP_CTX_set1_transactionID(ctx, hdr->transactionID);
597
4.19k
            (void)ossl_cmp_ctx_set1_recipNonce(ctx, hdr->senderNonce);
598
4.19k
        }
599
600
7.07k
        if ((flags & ERR_TXT_STRING) == 0 || *data == '\0')
601
6.71k
            data = NULL;
602
7.07k
        reason = ERR_reason_error_string(err);
603
7.07k
        if ((si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection,
604
7.07k
                 fail_info, reason))
605
7.07k
            != NULL) {
606
7.07k
            rsp = ossl_cmp_error_new(srv_ctx->ctx, si, err,
607
7.07k
                data, srv_ctx->sendUnprotectedErrors);
608
7.07k
            OSSL_CMP_PKISI_free(si);
609
7.07k
        }
610
7.07k
    }
611
7.07k
    OSSL_CMP_CTX_print_errors(ctx);
612
7.07k
    ctx->secretValue = backup_secret;
613
614
7.07k
    rsp_type = rsp != NULL ? OSSL_CMP_MSG_get_bodytype(rsp) : OSSL_CMP_PKIBODY_ERROR;
615
7.07k
    if (rsp != NULL)
616
0
        ossl_cmp_log1(DEBUG, ctx,
617
7.07k
            "sending %s", ossl_cmp_bodytype_to_string(rsp_type));
618
7.07k
    else
619
7.07k
        ossl_cmp_log(ERR, ctx, "cannot send proper CMP response");
620
621
    /* determine whether to keep the transaction open or not */
622
7.07k
    ctx->status = OSSL_CMP_PKISTATUS_trans;
623
7.07k
    switch (rsp_type) {
624
0
    case OSSL_CMP_PKIBODY_IP:
625
0
    case OSSL_CMP_PKIBODY_CP:
626
0
    case OSSL_CMP_PKIBODY_KUP:
627
0
        if (OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM) == 0)
628
0
            break;
629
        /* fall through */
630
631
0
    case OSSL_CMP_PKIBODY_RP:
632
0
    case OSSL_CMP_PKIBODY_PKICONF:
633
0
    case OSSL_CMP_PKIBODY_GENP:
634
7.07k
    case OSSL_CMP_PKIBODY_ERROR:
635
7.07k
        (void)OSSL_CMP_CTX_set1_transactionID(ctx, NULL);
636
7.07k
        (void)OSSL_CMP_CTX_set1_senderNonce(ctx, NULL);
637
7.07k
        ctx->status = OSSL_CMP_PKISTATUS_unspecified; /* transaction closed */
638
639
7.07k
    default: /* not closing transaction in other cases */
640
7.07k
        break;
641
7.07k
    }
642
7.07k
    return rsp;
643
7.07k
}
644
645
/*
646
 * Server interface that may substitute OSSL_CMP_MSG_http_perform at the client.
647
 * The OSSL_CMP_SRV_CTX must be set as client_ctx->transfer_cb_arg.
648
 * returns received message on success, else NULL and pushes an element on the
649
 * error stack.
650
 */
651
OSSL_CMP_MSG *OSSL_CMP_CTX_server_perform(OSSL_CMP_CTX *client_ctx,
652
    const OSSL_CMP_MSG *req)
653
0
{
654
0
    OSSL_CMP_SRV_CTX *srv_ctx = NULL;
655
656
0
    if (client_ctx == NULL || req == NULL) {
657
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
658
0
        return NULL;
659
0
    }
660
661
0
    if ((srv_ctx = OSSL_CMP_CTX_get_transfer_cb_arg(client_ctx)) == NULL) {
662
0
        ERR_raise(ERR_LIB_CMP, CMP_R_TRANSFER_ERROR);
663
0
        return NULL;
664
0
    }
665
666
0
    return OSSL_CMP_SRV_process_request(srv_ctx, req);
667
0
}