Coverage Report

Created: 2026-04-01 06:39

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