Coverage Report

Created: 2025-11-16 06:40

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