Coverage Report

Created: 2025-12-04 06:33

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