Coverage Report

Created: 2025-08-28 07:07

/src/openssl35/crypto/cmp/cmp_server.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2007-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright Nokia 2007-2019
4
 * Copyright Siemens AG 2015-2019
5
 *
6
 * Licensed under the Apache License 2.0 (the "License").  You may not use
7
 * this file except in compliance with the License.  You can obtain a copy
8
 * in the file LICENSE in the source distribution or at
9
 * https://www.openssl.org/source/license.html
10
 */
11
12
/* 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
    OSSL_CMP_CTX *ctx; /* CMP client context reused for transactionID etc. */
25
    void *custom_ctx;  /* application-specific server context */
26
    int certReqId;     /* of ir/cr/kur, OSSL_CMP_CERTREQID_NONE for p10cr */
27
    int polling;       /* current transaction is in polling mode */
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
    OSSL_CMP_SRV_delayed_delivery_cb_t delayed_delivery;
36
    OSSL_CMP_SRV_clean_transaction_cb_t clean_transaction;
37
38
    int sendUnprotectedErrors; /* Send error and rejection msgs unprotected */
39
    int acceptUnprotected;     /* Accept requests with no/invalid prot. */
40
    int acceptRAVerified;      /* Accept ir/cr/kur with POPO RAVerified */
41
    int grantImplicitConfirm;  /* Grant implicit confirmation if requested */
42
43
}; /* OSSL_CMP_SRV_CTX */
44
45
void OSSL_CMP_SRV_CTX_free(OSSL_CMP_SRV_CTX *srv_ctx)
46
37.8k
{
47
37.8k
    if (srv_ctx == NULL)
48
0
        return;
49
50
37.8k
    OSSL_CMP_CTX_free(srv_ctx->ctx);
51
37.8k
    OPENSSL_free(srv_ctx);
52
37.8k
}
53
54
OSSL_CMP_SRV_CTX *OSSL_CMP_SRV_CTX_new(OSSL_LIB_CTX *libctx, const char *propq)
55
37.8k
{
56
37.8k
    OSSL_CMP_SRV_CTX *ctx = OPENSSL_zalloc(sizeof(OSSL_CMP_SRV_CTX));
57
58
37.8k
    if (ctx == NULL)
59
0
        goto err;
60
61
37.8k
    if ((ctx->ctx = OSSL_CMP_CTX_new(libctx, propq)) == NULL)
62
0
        goto err;
63
37.8k
    ctx->certReqId = OSSL_CMP_CERTREQID_INVALID;
64
37.8k
    ctx->polling = 0;
65
66
    /* all other elements are initialized to 0 or NULL, respectively */
67
37.8k
    return ctx;
68
0
 err:
69
0
    OSSL_CMP_SRV_CTX_free(ctx);
70
0
    return NULL;
71
37.8k
}
72
73
int OSSL_CMP_SRV_CTX_init(OSSL_CMP_SRV_CTX *srv_ctx, void *custom_ctx,
74
                          OSSL_CMP_SRV_cert_request_cb_t process_cert_request,
75
                          OSSL_CMP_SRV_rr_cb_t process_rr,
76
                          OSSL_CMP_SRV_genm_cb_t process_genm,
77
                          OSSL_CMP_SRV_error_cb_t process_error,
78
                          OSSL_CMP_SRV_certConf_cb_t process_certConf,
79
                          OSSL_CMP_SRV_pollReq_cb_t process_pollReq)
80
37.8k
{
81
37.8k
    if (srv_ctx == NULL) {
82
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
83
0
        return 0;
84
0
    }
85
37.8k
    srv_ctx->custom_ctx = custom_ctx;
86
37.8k
    srv_ctx->process_cert_request = process_cert_request;
87
37.8k
    srv_ctx->process_rr = process_rr;
88
37.8k
    srv_ctx->process_genm = process_genm;
89
37.8k
    srv_ctx->process_error = process_error;
90
37.8k
    srv_ctx->process_certConf = process_certConf;
91
37.8k
    srv_ctx->process_pollReq = process_pollReq;
92
37.8k
    return 1;
93
37.8k
}
94
95
int OSSL_CMP_SRV_CTX_init_trans(OSSL_CMP_SRV_CTX *srv_ctx,
96
                                OSSL_CMP_SRV_delayed_delivery_cb_t delay,
97
                                OSSL_CMP_SRV_clean_transaction_cb_t clean)
98
24.5k
{
99
24.5k
    if (srv_ctx == NULL) {
100
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
101
0
        return 0;
102
0
    }
103
24.5k
    srv_ctx->delayed_delivery = delay;
104
24.5k
    srv_ctx->clean_transaction = clean;
105
24.5k
    return 1;
106
24.5k
}
107
108
OSSL_CMP_CTX *OSSL_CMP_SRV_CTX_get0_cmp_ctx(const OSSL_CMP_SRV_CTX *srv_ctx)
109
37.8k
{
110
37.8k
    if (srv_ctx == NULL) {
111
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
112
0
        return NULL;
113
0
    }
114
37.8k
    return srv_ctx->ctx;
115
37.8k
}
116
117
void *OSSL_CMP_SRV_CTX_get0_custom_ctx(const OSSL_CMP_SRV_CTX *srv_ctx)
118
0
{
119
0
    if (srv_ctx == NULL) {
120
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
121
0
        return NULL;
122
0
    }
123
0
    return srv_ctx->custom_ctx;
124
0
}
125
126
int OSSL_CMP_SRV_CTX_set_send_unprotected_errors(OSSL_CMP_SRV_CTX *srv_ctx,
127
                                                 int val)
128
0
{
129
0
    if (srv_ctx == NULL) {
130
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
131
0
        return 0;
132
0
    }
133
0
    srv_ctx->sendUnprotectedErrors = val != 0;
134
0
    return 1;
135
0
}
136
137
int OSSL_CMP_SRV_CTX_set_accept_unprotected(OSSL_CMP_SRV_CTX *srv_ctx, int val)
138
0
{
139
0
    if (srv_ctx == NULL) {
140
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
141
0
        return 0;
142
0
    }
143
0
    srv_ctx->acceptUnprotected = val != 0;
144
0
    return 1;
145
0
}
146
147
int OSSL_CMP_SRV_CTX_set_accept_raverified(OSSL_CMP_SRV_CTX *srv_ctx, int val)
148
0
{
149
0
    if (srv_ctx == NULL) {
150
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
151
0
        return 0;
152
0
    }
153
0
    srv_ctx->acceptRAVerified = val != 0;
154
0
    return 1;
155
0
}
156
157
int OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(OSSL_CMP_SRV_CTX *srv_ctx,
158
                                                int val)
159
0
{
160
0
    if (srv_ctx == NULL) {
161
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
162
0
        return 0;
163
0
    }
164
0
    srv_ctx->grantImplicitConfirm = val != 0;
165
0
    return 1;
166
0
}
167
168
/* return error msg with waiting status if polling is initiated, else NULL */
169
static OSSL_CMP_MSG *delayed_delivery(OSSL_CMP_SRV_CTX *srv_ctx,
170
                                      const OSSL_CMP_MSG *req)
171
7.86k
{
172
7.86k
    int ret;
173
7.86k
    unsigned long err;
174
7.86k
    int status = OSSL_CMP_PKISTATUS_waiting,
175
7.86k
        fail_info = 0, errorCode = 0;
176
7.86k
    const char *txt = NULL, *details = NULL;
177
7.86k
    OSSL_CMP_PKISI *si;
178
7.86k
    OSSL_CMP_MSG *msg;
179
180
7.86k
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL
181
7.86k
                     && srv_ctx->delayed_delivery != NULL))
182
0
        return NULL;
183
184
7.86k
    ret = srv_ctx->delayed_delivery(srv_ctx, req);
185
7.86k
    if (ret == 0)
186
7.86k
        return NULL;
187
0
    if (ret == 1) {
188
0
        srv_ctx->polling = 1;
189
0
    } else {
190
0
        status = OSSL_CMP_PKISTATUS_rejection;
191
0
        fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_systemFailure;
192
0
        txt = "server application error";
193
0
        err = ERR_peek_error();
194
0
        errorCode = ERR_GET_REASON(err);
195
0
        details = ERR_reason_error_string(err);
196
0
    }
197
198
0
    si = OSSL_CMP_STATUSINFO_new(status, fail_info, txt);
199
0
    if (si == NULL)
200
0
        return NULL;
201
202
0
    msg = ossl_cmp_error_new(srv_ctx->ctx, si, errorCode, details,
203
0
                             srv_ctx->sendUnprotectedErrors);
204
0
    OSSL_CMP_PKISI_free(si);
205
0
    return msg;
206
0
}
207
208
/*
209
 * Processes an ir/cr/p10cr/kur and returns a certification response.
210
 * Only handles the first certification request contained in req
211
 * returns an ip/cp/kup on success and NULL on error
212
 */
213
static OSSL_CMP_MSG *process_cert_request(OSSL_CMP_SRV_CTX *srv_ctx,
214
                                          const OSSL_CMP_MSG *req)
215
2.05k
{
216
2.05k
    OSSL_CMP_MSG *msg = NULL;
217
2.05k
    OSSL_CMP_PKISI *si = NULL;
218
2.05k
    X509 *certOut = NULL;
219
2.05k
    EVP_PKEY *keyOut = NULL;
220
2.05k
    STACK_OF(X509) *chainOut = NULL, *caPubs = NULL;
221
2.05k
    const OSSL_CRMF_MSG *crm = NULL;
222
2.05k
    const X509_REQ *p10cr = NULL;
223
2.05k
    int bodytype;
224
2.05k
    int certReqId, central_keygen;
225
226
2.05k
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
227
0
        return NULL;
228
229
2.05k
    switch (OSSL_CMP_MSG_get_bodytype(req)) {
230
1.32k
    case OSSL_CMP_PKIBODY_P10CR:
231
1.47k
    case OSSL_CMP_PKIBODY_CR:
232
1.47k
        bodytype = OSSL_CMP_PKIBODY_CP;
233
1.47k
        break;
234
387
    case OSSL_CMP_PKIBODY_IR:
235
387
        bodytype = OSSL_CMP_PKIBODY_IP;
236
387
        break;
237
189
    case OSSL_CMP_PKIBODY_KUR:
238
189
        bodytype = OSSL_CMP_PKIBODY_KUP;
239
189
        break;
240
0
    default:
241
0
        ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
242
0
        return NULL;
243
2.05k
    }
244
245
2.05k
    if (OSSL_CMP_MSG_get_bodytype(req) == OSSL_CMP_PKIBODY_P10CR) {
246
1.32k
        certReqId = OSSL_CMP_CERTREQID_NONE; /* p10cr does not include an Id */
247
1.32k
        p10cr = req->body->value.p10cr;
248
1.32k
    } else {
249
734
        OSSL_CRMF_MSGS *reqs = req->body->value.ir; /* same for cr and kur */
250
251
734
        if (sk_OSSL_CRMF_MSG_num(reqs) != 1) {
252
25
            ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED);
253
25
            return NULL;
254
25
        }
255
709
        if ((crm = sk_OSSL_CRMF_MSG_value(reqs, 0)) == NULL) {
256
0
            ERR_raise(ERR_LIB_CMP, CMP_R_CERTREQMSG_NOT_FOUND);
257
0
            return NULL;
258
0
        }
259
709
        certReqId = OSSL_CRMF_MSG_get_certReqId(crm);
260
709
        if (certReqId != OSSL_CMP_CERTREQID) { /* so far, only possible value */
261
93
            ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID);
262
93
            return NULL;
263
93
        }
264
709
    }
265
1.93k
    srv_ctx->certReqId = certReqId;
266
267
1.93k
    central_keygen = OSSL_CRMF_MSG_centralkeygen_requested(crm, p10cr);
268
1.93k
    if (central_keygen < 0)
269
3
        return NULL;
270
1.93k
    if (central_keygen == 0
271
1.93k
        && !ossl_cmp_verify_popo(srv_ctx->ctx, req, srv_ctx->acceptRAVerified)) {
272
        /* Proof of possession could not be verified */
273
0
        si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection,
274
0
                                     1 << OSSL_CMP_PKIFAILUREINFO_badPOP,
275
0
                                     ERR_reason_error_string(ERR_peek_error()));
276
0
        if (si == NULL)
277
0
            return NULL;
278
1.93k
    } else {
279
1.93k
        OSSL_CMP_PKIHEADER *hdr = OSSL_CMP_MSG_get0_header(req);
280
281
1.93k
        si = srv_ctx->process_cert_request(srv_ctx, req, certReqId, crm, p10cr,
282
1.93k
                                           &certOut, &chainOut, &caPubs);
283
1.93k
        if (si == NULL)
284
1.93k
            goto err;
285
0
        if (ossl_cmp_pkisi_get_status(si) == OSSL_CMP_PKISTATUS_waiting)
286
0
            srv_ctx->polling = 1;
287
        /* set OSSL_CMP_OPT_IMPLICIT_CONFIRM if and only if transaction ends */
288
0
        if (!OSSL_CMP_CTX_set_option(srv_ctx->ctx,
289
0
                                     OSSL_CMP_OPT_IMPLICIT_CONFIRM,
290
0
                                     ossl_cmp_hdr_has_implicitConfirm(hdr)
291
0
                                         && srv_ctx->grantImplicitConfirm
292
                                         /* do not set if polling starts: */
293
0
                                         && certOut != NULL))
294
0
            goto err;
295
0
        if (central_keygen == 1
296
0
            && srv_ctx->ctx->newPkey_priv && srv_ctx->ctx->newPkey != NULL)
297
0
            keyOut = srv_ctx->ctx->newPkey;
298
0
    }
299
300
0
    msg = ossl_cmp_certrep_new(srv_ctx->ctx, bodytype, certReqId, si,
301
0
                               certOut, keyOut, NULL /* enc */, chainOut, caPubs,
302
0
                               srv_ctx->sendUnprotectedErrors);
303
    /* When supporting OSSL_CRMF_POPO_KEYENC, "enc" will need to be set */
304
0
    if (msg == NULL)
305
0
        ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTREP);
306
307
1.93k
 err:
308
1.93k
    OSSL_CMP_PKISI_free(si);
309
1.93k
    X509_free(certOut);
310
1.93k
    OSSL_CMP_CTX_set0_newPkey(srv_ctx->ctx, 0, NULL);
311
1.93k
    OSSL_STACK_OF_X509_free(chainOut);
312
1.93k
    OSSL_STACK_OF_X509_free(caPubs);
313
1.93k
    return msg;
314
0
}
315
316
static OSSL_CMP_MSG *process_rr(OSSL_CMP_SRV_CTX *srv_ctx,
317
                                const OSSL_CMP_MSG *req)
318
162
{
319
162
    OSSL_CMP_MSG *msg = NULL;
320
162
    OSSL_CMP_REVDETAILS *details;
321
162
    OSSL_CRMF_CERTID *certId = NULL;
322
162
    OSSL_CRMF_CERTTEMPLATE *tmpl;
323
162
    const X509_NAME *issuer;
324
162
    const ASN1_INTEGER *serial;
325
162
    OSSL_CMP_PKISI *si;
326
327
162
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
328
0
        return NULL;
329
330
162
    if (sk_OSSL_CMP_REVDETAILS_num(req->body->value.rr) != 1) {
331
118
        ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED);
332
118
        return NULL;
333
118
    }
334
44
    details = sk_OSSL_CMP_REVDETAILS_value(req->body->value.rr, 0);
335
44
    if (details == NULL) {
336
0
        ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
337
0
        return NULL;
338
0
    }
339
340
44
    tmpl = details->certDetails;
341
44
    issuer = OSSL_CRMF_CERTTEMPLATE_get0_issuer(tmpl);
342
44
    serial = OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(tmpl);
343
44
    if (issuer != NULL && serial != NULL
344
44
            && (certId = OSSL_CRMF_CERTID_gen(issuer, serial)) == NULL)
345
0
        return NULL;
346
44
    if ((si = srv_ctx->process_rr(srv_ctx, req, issuer, serial)) == NULL)
347
44
        goto err;
348
349
0
    if ((msg = ossl_cmp_rp_new(srv_ctx->ctx, si, certId,
350
0
                               srv_ctx->sendUnprotectedErrors)) == NULL)
351
0
        ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_RR);
352
353
44
 err:
354
44
    OSSL_CRMF_CERTID_free(certId);
355
44
    OSSL_CMP_PKISI_free(si);
356
44
    return msg;
357
0
}
358
359
/*
360
 * Processes genm and creates a genp message mirroring the contents of the
361
 * incoming message
362
 */
363
static OSSL_CMP_MSG *process_genm(OSSL_CMP_SRV_CTX *srv_ctx,
364
                                  const OSSL_CMP_MSG *req)
365
376
{
366
376
    OSSL_CMP_GENMSGCONTENT *itavs;
367
376
    OSSL_CMP_MSG *msg;
368
369
376
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
370
0
        return NULL;
371
372
376
    if (!srv_ctx->process_genm(srv_ctx, req, req->body->value.genm, &itavs))
373
376
        return NULL;
374
375
0
    msg = ossl_cmp_genp_new(srv_ctx->ctx, itavs);
376
0
    sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
377
0
    return msg;
378
376
}
379
380
static OSSL_CMP_MSG *process_error(OSSL_CMP_SRV_CTX *srv_ctx,
381
                                   const OSSL_CMP_MSG *req)
382
768
{
383
768
    OSSL_CMP_ERRORMSGCONTENT *errorContent;
384
768
    OSSL_CMP_MSG *msg;
385
386
768
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
387
0
        return NULL;
388
768
    errorContent = req->body->value.error;
389
768
    srv_ctx->process_error(srv_ctx, req, errorContent->pKIStatusInfo,
390
768
                           errorContent->errorCode, errorContent->errorDetails);
391
392
768
    if ((msg = ossl_cmp_pkiconf_new(srv_ctx->ctx)) == NULL)
393
768
        ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF);
394
768
    return msg;
395
768
}
396
397
static OSSL_CMP_MSG *process_certConf(OSSL_CMP_SRV_CTX *srv_ctx,
398
                                      const OSSL_CMP_MSG *req)
399
289
{
400
289
    OSSL_CMP_CTX *ctx;
401
289
    OSSL_CMP_CERTCONFIRMCONTENT *ccc;
402
289
    int num;
403
289
    OSSL_CMP_MSG *msg = NULL;
404
289
    OSSL_CMP_CERTSTATUS *status = NULL;
405
406
289
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
407
0
        return NULL;
408
409
289
    ctx = srv_ctx->ctx;
410
289
    ccc = req->body->value.certConf;
411
289
    num = sk_OSSL_CMP_CERTSTATUS_num(ccc);
412
413
289
    if (OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM) == 1
414
289
            || ctx->status != OSSL_CMP_PKISTATUS_trans) {
415
289
        ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_UNEXPECTED_CERTCONF);
416
289
        return NULL;
417
289
    }
418
419
0
    if (num == 0) {
420
0
        ossl_cmp_err(ctx, "certificate rejected by client");
421
0
    } else {
422
0
        if (num > 1)
423
0
            ossl_cmp_warn(ctx, "All CertStatus but the first will be ignored");
424
0
        status = sk_OSSL_CMP_CERTSTATUS_value(ccc, 0);
425
0
    }
426
427
0
    if (status != NULL) {
428
0
        int certReqId = ossl_cmp_asn1_get_int(status->certReqId);
429
0
        ASN1_OCTET_STRING *certHash = status->certHash;
430
0
        OSSL_CMP_PKISI *si = status->statusInfo;
431
432
0
        if (certReqId != srv_ctx->certReqId) {
433
0
            ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID);
434
0
            return NULL;
435
0
        }
436
0
        if (!srv_ctx->process_certConf(srv_ctx, req, certReqId, certHash, si))
437
0
            return NULL; /* reason code may be: CMP_R_CERTHASH_UNMATCHED */
438
439
0
        if (si != NULL
440
0
            && ossl_cmp_pkisi_get_status(si) != OSSL_CMP_PKISTATUS_accepted) {
441
0
            int pki_status = ossl_cmp_pkisi_get_status(si);
442
0
            const char *str = ossl_cmp_PKIStatus_to_string(pki_status);
443
444
0
            ossl_cmp_log2(INFO, ctx, "certificate rejected by client %s %s",
445
0
                          str == NULL ? "without" : "with",
446
0
                          str == NULL ? "PKIStatus" : str);
447
0
        }
448
0
    }
449
450
0
    if ((msg = ossl_cmp_pkiconf_new(ctx)) == NULL)
451
0
        ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF);
452
0
    return msg;
453
0
}
454
455
/* pollReq is handled separately, to avoid recursive call */
456
static OSSL_CMP_MSG *process_non_polling_request(OSSL_CMP_SRV_CTX *srv_ctx,
457
                                                 const OSSL_CMP_MSG *req)
458
7.86k
{
459
7.86k
    OSSL_CMP_MSG *rsp = NULL;
460
461
7.86k
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL
462
7.86k
                     && req->body != NULL))
463
0
        return NULL;
464
465
7.86k
    switch (OSSL_CMP_MSG_get_bodytype(req)) {
466
909
    case OSSL_CMP_PKIBODY_IR:
467
1.33k
    case OSSL_CMP_PKIBODY_CR:
468
4.33k
    case OSSL_CMP_PKIBODY_P10CR:
469
4.54k
    case OSSL_CMP_PKIBODY_KUR:
470
4.54k
        if (srv_ctx->process_cert_request == NULL)
471
4.54k
            ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
472
4.54k
        else
473
4.54k
            rsp = process_cert_request(srv_ctx, req);
474
4.54k
        break;
475
71
    case OSSL_CMP_PKIBODY_RR:
476
71
        if (srv_ctx->process_rr == NULL)
477
71
            ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
478
71
        else
479
71
            rsp = process_rr(srv_ctx, req);
480
71
        break;
481
273
    case OSSL_CMP_PKIBODY_GENM:
482
273
        if (srv_ctx->process_genm == NULL)
483
273
            ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
484
273
        else
485
273
            rsp = process_genm(srv_ctx, req);
486
273
        break;
487
581
    case OSSL_CMP_PKIBODY_ERROR:
488
581
        if (srv_ctx->process_error == NULL)
489
581
            ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
490
581
        else
491
581
            rsp = process_error(srv_ctx, req);
492
581
        break;
493
189
    case OSSL_CMP_PKIBODY_CERTCONF:
494
189
        if (srv_ctx->process_certConf == NULL)
495
189
            ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
496
189
        else
497
189
            rsp = process_certConf(srv_ctx, req);
498
189
        break;
499
500
0
    case OSSL_CMP_PKIBODY_POLLREQ:
501
0
        ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
502
0
        break;
503
2.20k
    default:
504
2.20k
        ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
505
2.20k
        break;
506
7.86k
    }
507
508
7.86k
    return rsp;
509
7.86k
}
510
511
static OSSL_CMP_MSG *process_pollReq(OSSL_CMP_SRV_CTX *srv_ctx,
512
                                     const OSSL_CMP_MSG *req)
513
116
{
514
116
    OSSL_CMP_POLLREQCONTENT *prc;
515
116
    OSSL_CMP_POLLREQ *pr;
516
116
    int certReqId;
517
116
    OSSL_CMP_MSG *orig_req;
518
116
    int64_t check_after = 0;
519
116
    OSSL_CMP_MSG *msg = NULL;
520
521
116
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
522
0
        return NULL;
523
524
116
    if (!srv_ctx->polling) {
525
116
        ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
526
116
        return NULL;
527
116
    }
528
529
0
    prc = req->body->value.pollReq;
530
0
    if (sk_OSSL_CMP_POLLREQ_num(prc) != 1) {
531
0
        ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED);
532
0
        return NULL;
533
0
    }
534
535
0
    pr = sk_OSSL_CMP_POLLREQ_value(prc, 0);
536
0
    certReqId = ossl_cmp_asn1_get_int(pr->certReqId);
537
0
    if (!srv_ctx->process_pollReq(srv_ctx, req, certReqId,
538
0
                                  &orig_req, &check_after))
539
0
        return NULL;
540
541
0
    if (orig_req != NULL) {
542
0
        srv_ctx->polling = 0;
543
0
        msg = process_non_polling_request(srv_ctx, orig_req);
544
0
        OSSL_CMP_MSG_free(orig_req);
545
0
    } else {
546
0
        if ((msg = ossl_cmp_pollRep_new(srv_ctx->ctx, certReqId,
547
0
                                        check_after)) == NULL)
548
0
            ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREP);
549
0
    }
550
0
    return msg;
551
0
}
552
553
/*
554
 * Determine whether missing/invalid protection of request message is allowed.
555
 * Return 1 on acceptance, 0 on rejection, or -1 on (internal) error.
556
 */
557
static int unprotected_exception(const OSSL_CMP_CTX *ctx,
558
                                 const OSSL_CMP_MSG *req,
559
                                 int invalid_protection,
560
                                 int accept_unprotected_requests)
561
13.1k
{
562
13.1k
    if (!ossl_assert(ctx != NULL && req != NULL))
563
0
        return -1;
564
565
13.1k
    if (accept_unprotected_requests) {
566
0
        ossl_cmp_log1(WARN, ctx, "ignoring %s protection of request message",
567
0
                      invalid_protection ? "invalid" : "missing");
568
0
        return 1;
569
0
    }
570
13.1k
    if (OSSL_CMP_MSG_get_bodytype(req) == OSSL_CMP_PKIBODY_ERROR
571
13.1k
        && OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS) == 1) {
572
0
        ossl_cmp_warn(ctx, "ignoring missing protection of error message");
573
0
        return 1;
574
0
    }
575
13.1k
    return 0;
576
13.1k
}
577
578
/*
579
 * returns created message and NULL on internal error
580
 */
581
OSSL_CMP_MSG *OSSL_CMP_SRV_process_request(OSSL_CMP_SRV_CTX *srv_ctx,
582
                                           const OSSL_CMP_MSG *req)
583
24.5k
{
584
24.5k
    OSSL_CMP_CTX *ctx;
585
24.5k
    ASN1_OCTET_STRING *backup_secret;
586
24.5k
    OSSL_CMP_PKIHEADER *hdr;
587
24.5k
    int req_type, rsp_type;
588
24.5k
    int req_verified = 0;
589
24.5k
    OSSL_CMP_MSG *rsp = NULL;
590
591
24.5k
    if (srv_ctx == NULL || srv_ctx->ctx == NULL
592
24.5k
            || req == NULL || req->body == NULL
593
24.5k
            || (hdr = OSSL_CMP_MSG_get0_header(req)) == NULL) {
594
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
595
0
        return 0;
596
0
    }
597
24.5k
    ctx = srv_ctx->ctx;
598
24.5k
    backup_secret = ctx->secretValue;
599
24.5k
    req_type = OSSL_CMP_MSG_get_bodytype(req);
600
24.5k
    ossl_cmp_log1(DEBUG, ctx,
601
24.5k
                  "received %s", ossl_cmp_bodytype_to_string(req_type));
602
603
    /*
604
     * Some things need to be done already before validating the message in
605
     * order to be able to send an error message as far as needed and possible.
606
     */
607
24.5k
    if (hdr->sender->type != GEN_DIRNAME) {
608
16.5k
        ERR_raise(ERR_LIB_CMP, CMP_R_SENDER_GENERALNAME_TYPE_NOT_SUPPORTED);
609
16.5k
        goto err;
610
16.5k
    }
611
7.98k
    if (!OSSL_CMP_CTX_set1_recipient(ctx, hdr->sender->d.directoryName))
612
4
        goto err;
613
614
7.98k
    if (srv_ctx->polling && req_type != OSSL_CMP_PKIBODY_POLLREQ
615
7.98k
            && req_type != OSSL_CMP_PKIBODY_ERROR) {
616
0
        ERR_raise(ERR_LIB_CMP, CMP_R_EXPECTED_POLLREQ);
617
0
        goto err;
618
0
    }
619
620
7.98k
    switch (req_type) {
621
909
    case OSSL_CMP_PKIBODY_IR:
622
1.33k
    case OSSL_CMP_PKIBODY_CR:
623
4.33k
    case OSSL_CMP_PKIBODY_P10CR:
624
4.54k
    case OSSL_CMP_PKIBODY_KUR:
625
4.61k
    case OSSL_CMP_PKIBODY_RR:
626
4.89k
    case OSSL_CMP_PKIBODY_GENM:
627
5.47k
    case OSSL_CMP_PKIBODY_ERROR:
628
5.47k
        if (ctx->transactionID != NULL) {
629
0
            char *tid = i2s_ASN1_OCTET_STRING(NULL, ctx->transactionID);
630
631
0
            if (tid != NULL)
632
0
                ossl_cmp_log1(WARN, ctx,
633
0
                              "Assuming that last transaction with ID=%s got aborted",
634
0
                              tid);
635
0
            OPENSSL_free(tid);
636
0
        }
637
        /* start of a new transaction, reset transactionID and senderNonce */
638
5.47k
        if (!OSSL_CMP_CTX_set1_transactionID(ctx, NULL)
639
5.47k
                || !OSSL_CMP_CTX_set1_senderNonce(ctx, NULL))
640
0
            goto err;
641
642
5.47k
        if (srv_ctx->clean_transaction != NULL
643
5.47k
                && !srv_ctx->clean_transaction(srv_ctx, NULL)) {
644
0
            ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
645
0
            goto err;
646
0
        }
647
648
5.47k
        break;
649
5.47k
    default:
650
        /* transactionID should be already initialized */
651
2.50k
        if (ctx->transactionID == NULL) {
652
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
653
            ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
654
            goto err;
655
#endif
656
2.50k
        }
657
7.98k
    }
658
659
7.98k
    req_verified = ossl_cmp_msg_check_update(ctx, req, unprotected_exception,
660
7.98k
                                             srv_ctx->acceptUnprotected);
661
7.98k
    if (ctx->secretValue != NULL && ctx->pkey != NULL
662
7.98k
            && ossl_cmp_hdr_get_protection_nid(hdr) != NID_id_PasswordBasedMAC)
663
0
        ctx->secretValue = NULL; /* use MSG_SIG_ALG when protecting rsp */
664
7.98k
    if (!req_verified)
665
0
        goto err;
666
667
7.98k
    if (req_type == OSSL_CMP_PKIBODY_POLLREQ) {
668
116
        if (srv_ctx->process_pollReq == NULL)
669
116
            ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
670
116
        else
671
116
            rsp = process_pollReq(srv_ctx, req);
672
7.86k
    } else {
673
7.86k
        if (srv_ctx->delayed_delivery != NULL
674
7.86k
            && (rsp = delayed_delivery(srv_ctx, req)) != NULL) {
675
0
            goto err;
676
0
        }
677
7.86k
        rsp = process_non_polling_request(srv_ctx, req);
678
7.86k
    }
679
680
24.5k
 err:
681
24.5k
    if (rsp == NULL) {
682
        /* on error, try to respond with CMP error message to client */
683
24.5k
        const char *data = NULL, *reason = NULL;
684
24.5k
        int flags = 0;
685
24.5k
        unsigned long err = ERR_peek_error_data(&data, &flags);
686
24.5k
        int fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_badRequest;
687
        /* fail_info is not very specific */
688
24.5k
        OSSL_CMP_PKISI *si = NULL;
689
690
24.5k
        if (!req_verified) {
691
            /*
692
             * Above ossl_cmp_msg_check_update() was not successfully executed,
693
             * which normally would set ctx->transactionID and ctx->recipNonce.
694
             * So anyway try to provide the right transactionID and recipNonce,
695
             * while ignoring any (extra) error in next two function calls.
696
             */
697
16.5k
            if (ctx->transactionID == NULL)
698
16.5k
                (void)OSSL_CMP_CTX_set1_transactionID(ctx, hdr->transactionID);
699
16.5k
            (void)ossl_cmp_ctx_set1_recipNonce(ctx, hdr->senderNonce);
700
16.5k
        }
701
702
24.5k
        if ((flags & ERR_TXT_STRING) == 0 || *data == '\0')
703
22.8k
            data = NULL;
704
24.5k
        reason = ERR_reason_error_string(err);
705
24.5k
        if ((si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection,
706
24.5k
                                          fail_info, reason)) != NULL) {
707
24.5k
            rsp = ossl_cmp_error_new(srv_ctx->ctx, si, err,
708
24.5k
                                     data, srv_ctx->sendUnprotectedErrors);
709
24.5k
            OSSL_CMP_PKISI_free(si);
710
24.5k
        }
711
24.5k
    }
712
24.5k
    OSSL_CMP_CTX_print_errors(ctx);
713
24.5k
    ctx->secretValue = backup_secret;
714
715
24.5k
    rsp_type =
716
24.5k
        rsp != NULL ? OSSL_CMP_MSG_get_bodytype(rsp) : OSSL_CMP_PKIBODY_ERROR;
717
24.5k
    if (rsp != NULL)
718
0
        ossl_cmp_log1(DEBUG, ctx,
719
24.5k
                      "sending %s", ossl_cmp_bodytype_to_string(rsp_type));
720
24.5k
    else
721
24.5k
        ossl_cmp_log(ERR, ctx, "cannot send proper CMP response");
722
723
    /* determine whether to keep the transaction open or not */
724
24.5k
    ctx->status = OSSL_CMP_PKISTATUS_trans;
725
24.5k
    switch (rsp_type) {
726
0
    case OSSL_CMP_PKIBODY_IP:
727
0
    case OSSL_CMP_PKIBODY_CP:
728
0
    case OSSL_CMP_PKIBODY_KUP:
729
0
        if (OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM) == 0)
730
0
            break;
731
        /* fall through */
732
733
24.5k
    case OSSL_CMP_PKIBODY_ERROR:
734
24.5k
        if (rsp != NULL && ossl_cmp_is_error_with_waiting(rsp))
735
0
            break;
736
        /* fall through */
737
738
24.5k
    case OSSL_CMP_PKIBODY_RP:
739
24.5k
    case OSSL_CMP_PKIBODY_PKICONF:
740
24.5k
    case OSSL_CMP_PKIBODY_GENP:
741
        /* Other terminating response message types are not supported */
742
24.5k
        srv_ctx->certReqId = OSSL_CMP_CERTREQID_INVALID;
743
        /* Prepare for next transaction, ignoring any errors here: */
744
24.5k
        if (srv_ctx->clean_transaction != NULL)
745
24.5k
            (void)srv_ctx->clean_transaction(srv_ctx, ctx->transactionID);
746
24.5k
        (void)OSSL_CMP_CTX_set1_transactionID(ctx, NULL);
747
24.5k
        (void)OSSL_CMP_CTX_set1_senderNonce(ctx, NULL);
748
24.5k
        ctx->status = OSSL_CMP_PKISTATUS_unspecified; /* transaction closed */
749
750
24.5k
    default: /* not closing transaction in other cases */
751
24.5k
        break;
752
24.5k
    }
753
24.5k
    return rsp;
754
24.5k
}
755
756
/*
757
 * Server interface that may substitute OSSL_CMP_MSG_http_perform at the client.
758
 * The OSSL_CMP_SRV_CTX must be set as client_ctx->transfer_cb_arg.
759
 * returns received message on success, else NULL and pushes an element on the
760
 * error stack.
761
 */
762
OSSL_CMP_MSG *OSSL_CMP_CTX_server_perform(OSSL_CMP_CTX *client_ctx,
763
                                          const OSSL_CMP_MSG *req)
764
0
{
765
0
    OSSL_CMP_SRV_CTX *srv_ctx = NULL;
766
767
0
    if (client_ctx == NULL || req == NULL) {
768
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
769
0
        return NULL;
770
0
    }
771
772
0
    if ((srv_ctx = OSSL_CMP_CTX_get_transfer_cb_arg(client_ctx)) == NULL) {
773
0
        ERR_raise(ERR_LIB_CMP, CMP_R_TRANSFER_ERROR);
774
0
        return NULL;
775
0
    }
776
777
0
    return OSSL_CMP_SRV_process_request(srv_ctx, req);
778
0
}