Coverage Report

Created: 2025-12-31 06:58

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.3k
{
47
39.3k
    if (srv_ctx == NULL)
48
0
        return;
49
50
39.3k
    OSSL_CMP_CTX_free(srv_ctx->ctx);
51
39.3k
    OPENSSL_free(srv_ctx);
52
39.3k
}
53
54
OSSL_CMP_SRV_CTX *OSSL_CMP_SRV_CTX_new(OSSL_LIB_CTX *libctx, const char *propq)
55
39.3k
{
56
39.3k
    OSSL_CMP_SRV_CTX *ctx = OPENSSL_zalloc(sizeof(OSSL_CMP_SRV_CTX));
57
58
39.3k
    if (ctx == NULL)
59
0
        goto err;
60
61
39.3k
    if ((ctx->ctx = OSSL_CMP_CTX_new(libctx, propq)) == NULL)
62
0
        goto err;
63
39.3k
    ctx->certReqId = OSSL_CMP_CERTREQID_INVALID;
64
39.3k
    ctx->polling = 0;
65
66
    /* all other elements are initialized to 0 or NULL, respectively */
67
39.3k
    return ctx;
68
0
err:
69
0
    OSSL_CMP_SRV_CTX_free(ctx);
70
0
    return NULL;
71
39.3k
}
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.3k
{
81
39.3k
    if (srv_ctx == NULL) {
82
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
83
0
        return 0;
84
0
    }
85
39.3k
    srv_ctx->custom_ctx = custom_ctx;
86
39.3k
    srv_ctx->process_cert_request = process_cert_request;
87
39.3k
    srv_ctx->process_rr = process_rr;
88
39.3k
    srv_ctx->process_genm = process_genm;
89
39.3k
    srv_ctx->process_error = process_error;
90
39.3k
    srv_ctx->process_certConf = process_certConf;
91
39.3k
    srv_ctx->process_pollReq = process_pollReq;
92
39.3k
    return 1;
93
39.3k
}
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
32.2k
{
99
32.2k
    if (srv_ctx == NULL) {
100
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
101
0
        return 0;
102
0
    }
103
32.2k
    srv_ctx->delayed_delivery = delay;
104
32.2k
    srv_ctx->clean_transaction = clean;
105
32.2k
    return 1;
106
32.2k
}
107
108
OSSL_CMP_CTX *OSSL_CMP_SRV_CTX_get0_cmp_ctx(const OSSL_CMP_SRV_CTX *srv_ctx)
109
39.3k
{
110
39.3k
    if (srv_ctx == NULL) {
111
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
112
0
        return NULL;
113
0
    }
114
39.3k
    return srv_ctx->ctx;
115
39.3k
}
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.6k
{
172
10.6k
    int ret;
173
10.6k
    unsigned long err;
174
10.6k
    int status = OSSL_CMP_PKISTATUS_waiting,
175
10.6k
        fail_info = 0, errorCode = 0;
176
10.6k
    const char *txt = NULL, *details = NULL;
177
10.6k
    OSSL_CMP_PKISI *si;
178
10.6k
    OSSL_CMP_MSG *msg;
179
180
10.6k
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL
181
10.6k
            && srv_ctx->delayed_delivery != NULL))
182
0
        return NULL;
183
184
10.6k
    ret = srv_ctx->delayed_delivery(srv_ctx, req);
185
10.6k
    if (ret == 0)
186
10.6k
        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.67k
{
216
2.67k
    OSSL_CMP_MSG *msg = NULL;
217
2.67k
    OSSL_CMP_PKISI *si = NULL;
218
2.67k
    X509 *certOut = NULL;
219
2.67k
    STACK_OF(X509) *chainOut = NULL, *caPubs = NULL;
220
2.67k
    const OSSL_CRMF_MSG *crm = NULL;
221
2.67k
    const X509_REQ *p10cr = NULL;
222
2.67k
    int bodytype;
223
2.67k
    int certReqId;
224
225
2.67k
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
226
0
        return NULL;
227
228
2.67k
    switch (OSSL_CMP_MSG_get_bodytype(req)) {
229
1.51k
    case OSSL_CMP_PKIBODY_P10CR:
230
1.96k
    case OSSL_CMP_PKIBODY_CR:
231
1.96k
        bodytype = OSSL_CMP_PKIBODY_CP;
232
1.96k
        break;
233
669
    case OSSL_CMP_PKIBODY_IR:
234
669
        bodytype = OSSL_CMP_PKIBODY_IP;
235
669
        break;
236
42
    case OSSL_CMP_PKIBODY_KUR:
237
42
        bodytype = OSSL_CMP_PKIBODY_KUP;
238
42
        break;
239
0
    default:
240
0
        ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
241
0
        return NULL;
242
2.67k
    }
243
244
2.67k
    if (OSSL_CMP_MSG_get_bodytype(req) == OSSL_CMP_PKIBODY_P10CR) {
245
1.51k
        certReqId = OSSL_CMP_CERTREQID_NONE; /* p10cr does not include an Id */
246
1.51k
        p10cr = req->body->value.p10cr;
247
1.51k
    } else {
248
1.16k
        OSSL_CRMF_MSGS *reqs = req->body->value.ir; /* same for cr and kur */
249
250
1.16k
        if (sk_OSSL_CRMF_MSG_num(reqs) != 1) {
251
17
            ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED);
252
17
            return NULL;
253
17
        }
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
168
            ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID);
261
168
            return NULL;
262
168
        }
263
1.14k
    }
264
2.49k
    srv_ctx->certReqId = certReqId;
265
266
2.49k
    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.49k
    } else {
274
2.49k
        OSSL_CMP_PKIHEADER *hdr = OSSL_CMP_MSG_get0_header(req);
275
276
2.49k
        si = srv_ctx->process_cert_request(srv_ctx, req, certReqId, crm, p10cr,
277
2.49k
            &certOut, &chainOut, &caPubs);
278
2.49k
        if (si == NULL)
279
2.49k
            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.49k
err:
300
2.49k
    OSSL_CMP_PKISI_free(si);
301
2.49k
    X509_free(certOut);
302
2.49k
    OSSL_STACK_OF_X509_free(chainOut);
303
2.49k
    OSSL_STACK_OF_X509_free(caPubs);
304
2.49k
    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
151
{
310
151
    OSSL_CMP_MSG *msg = NULL;
311
151
    OSSL_CMP_REVDETAILS *details;
312
151
    OSSL_CRMF_CERTID *certId = NULL;
313
151
    OSSL_CRMF_CERTTEMPLATE *tmpl;
314
151
    const X509_NAME *issuer;
315
151
    const ASN1_INTEGER *serial;
316
151
    OSSL_CMP_PKISI *si;
317
318
151
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
319
0
        return NULL;
320
321
151
    if (sk_OSSL_CMP_REVDETAILS_num(req->body->value.rr) != 1) {
322
78
        ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED);
323
78
        return NULL;
324
78
    }
325
73
    details = sk_OSSL_CMP_REVDETAILS_value(req->body->value.rr, 0);
326
73
    if (details == NULL) {
327
0
        ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
328
0
        return NULL;
329
0
    }
330
331
73
    tmpl = details->certDetails;
332
73
    issuer = OSSL_CRMF_CERTTEMPLATE_get0_issuer(tmpl);
333
73
    serial = OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(tmpl);
334
73
    if (issuer != NULL && serial != NULL
335
50
        && (certId = OSSL_CRMF_CERTID_gen(issuer, serial)) == NULL)
336
0
        return NULL;
337
73
    if ((si = srv_ctx->process_rr(srv_ctx, req, issuer, serial)) == NULL)
338
73
        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
73
err:
346
73
    OSSL_CRMF_CERTID_free(certId);
347
73
    OSSL_CMP_PKISI_free(si);
348
73
    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
896
{
375
896
    OSSL_CMP_ERRORMSGCONTENT *errorContent;
376
896
    OSSL_CMP_MSG *msg;
377
378
896
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
379
0
        return NULL;
380
896
    errorContent = req->body->value.error;
381
896
    srv_ctx->process_error(srv_ctx, req, errorContent->pKIStatusInfo,
382
896
        errorContent->errorCode, errorContent->errorDetails);
383
384
896
    if ((msg = ossl_cmp_pkiconf_new(srv_ctx->ctx)) == NULL)
385
896
        ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF);
386
896
    return msg;
387
896
}
388
389
static OSSL_CMP_MSG *process_certConf(OSSL_CMP_SRV_CTX *srv_ctx,
390
    const OSSL_CMP_MSG *req)
391
251
{
392
251
    OSSL_CMP_CTX *ctx;
393
251
    OSSL_CMP_CERTCONFIRMCONTENT *ccc;
394
251
    int num;
395
251
    OSSL_CMP_MSG *msg = NULL;
396
251
    OSSL_CMP_CERTSTATUS *status = NULL;
397
398
251
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
399
0
        return NULL;
400
401
251
    ctx = srv_ctx->ctx;
402
251
    ccc = req->body->value.certConf;
403
251
    num = sk_OSSL_CMP_CERTSTATUS_num(ccc);
404
405
251
    if (OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM) == 1
406
251
        || ctx->status != OSSL_CMP_PKISTATUS_trans) {
407
251
        ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_UNEXPECTED_CERTCONF);
408
251
        return NULL;
409
251
    }
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.6k
{
451
10.6k
    OSSL_CMP_MSG *rsp = NULL;
452
453
10.6k
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL
454
10.6k
            && req->body != NULL))
455
0
        return NULL;
456
457
10.6k
    switch (OSSL_CMP_MSG_get_bodytype(req)) {
458
1.46k
    case OSSL_CMP_PKIBODY_IR:
459
2.62k
    case OSSL_CMP_PKIBODY_CR:
460
6.03k
    case OSSL_CMP_PKIBODY_P10CR:
461
6.32k
    case OSSL_CMP_PKIBODY_KUR:
462
6.32k
        if (srv_ctx->process_cert_request == NULL)
463
6.32k
            ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
464
6.32k
        else
465
6.32k
            rsp = process_cert_request(srv_ctx, req);
466
6.32k
        break;
467
129
    case OSSL_CMP_PKIBODY_RR:
468
129
        if (srv_ctx->process_rr == NULL)
469
129
            ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
470
129
        else
471
129
            rsp = process_rr(srv_ctx, req);
472
129
        break;
473
354
    case OSSL_CMP_PKIBODY_GENM:
474
354
        if (srv_ctx->process_genm == NULL)
475
354
            ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
476
354
        else
477
354
            rsp = process_genm(srv_ctx, req);
478
354
        break;
479
772
    case OSSL_CMP_PKIBODY_ERROR:
480
772
        if (srv_ctx->process_error == NULL)
481
772
            ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
482
772
        else
483
772
            rsp = process_error(srv_ctx, req);
484
772
        break;
485
232
    case OSSL_CMP_PKIBODY_CERTCONF:
486
232
        if (srv_ctx->process_certConf == NULL)
487
232
            ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
488
232
        else
489
232
            rsp = process_certConf(srv_ctx, req);
490
232
        break;
491
492
0
    case OSSL_CMP_PKIBODY_POLLREQ:
493
0
        ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
494
0
        break;
495
2.87k
    default:
496
2.87k
        ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
497
2.87k
        break;
498
10.6k
    }
499
500
10.6k
    return rsp;
501
10.6k
}
502
503
static OSSL_CMP_MSG *process_pollReq(OSSL_CMP_SRV_CTX *srv_ctx,
504
    const OSSL_CMP_MSG *req)
505
122
{
506
122
    OSSL_CMP_POLLREQCONTENT *prc;
507
122
    OSSL_CMP_POLLREQ *pr;
508
122
    int certReqId;
509
122
    OSSL_CMP_MSG *orig_req;
510
122
    int64_t check_after = 0;
511
122
    OSSL_CMP_MSG *msg = NULL;
512
513
122
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
514
0
        return NULL;
515
516
122
    if (!srv_ctx->polling) {
517
122
        ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
518
122
        return NULL;
519
122
    }
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.6k
{
555
13.6k
    if (!ossl_assert(ctx != NULL && req != NULL))
556
0
        return -1;
557
558
13.6k
    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.6k
    if (OSSL_CMP_MSG_get_bodytype(req) == OSSL_CMP_PKIBODY_ERROR
564
896
        && 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.6k
    return 0;
569
13.6k
}
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
32.2k
{
577
32.2k
    OSSL_CMP_CTX *ctx;
578
32.2k
    ASN1_OCTET_STRING *backup_secret;
579
32.2k
    OSSL_CMP_PKIHEADER *hdr;
580
32.2k
    int req_type, rsp_type;
581
32.2k
    int req_verified = 0;
582
32.2k
    OSSL_CMP_MSG *rsp = NULL;
583
584
32.2k
    if (srv_ctx == NULL || srv_ctx->ctx == NULL
585
32.2k
        || req == NULL || req->body == NULL
586
32.2k
        || (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
32.2k
    ctx = srv_ctx->ctx;
591
32.2k
    backup_secret = ctx->secretValue;
592
32.2k
    req_type = OSSL_CMP_MSG_get_bodytype(req);
593
32.2k
    ossl_cmp_log1(DEBUG, ctx,
594
32.2k
        "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
32.2k
    if (hdr->sender->type != GEN_DIRNAME) {
601
21.4k
        ERR_raise(ERR_LIB_CMP, CMP_R_SENDER_GENERALNAME_TYPE_NOT_SUPPORTED);
602
21.4k
        goto err;
603
21.4k
    }
604
10.8k
    if (!OSSL_CMP_CTX_set1_recipient(ctx, hdr->sender->d.directoryName))
605
4
        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.46k
    case OSSL_CMP_PKIBODY_IR:
615
2.62k
    case OSSL_CMP_PKIBODY_CR:
616
6.03k
    case OSSL_CMP_PKIBODY_P10CR:
617
6.32k
    case OSSL_CMP_PKIBODY_KUR:
618
6.44k
    case OSSL_CMP_PKIBODY_RR:
619
6.80k
    case OSSL_CMP_PKIBODY_GENM:
620
7.57k
    case OSSL_CMP_PKIBODY_ERROR:
621
7.57k
        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.57k
        if (!OSSL_CMP_CTX_set1_transactionID(ctx, NULL)
632
7.57k
            || !OSSL_CMP_CTX_set1_senderNonce(ctx, NULL))
633
0
            goto err;
634
635
7.57k
        if (srv_ctx->clean_transaction != NULL
636
7.57k
            && !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.57k
        break;
642
7.57k
    default:
643
        /* transactionID should be already initialized */
644
3.23k
        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.23k
        }
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
122
        if (srv_ctx->process_pollReq == NULL)
662
122
            ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
663
122
        else
664
122
            rsp = process_pollReq(srv_ctx, req);
665
10.6k
    } else {
666
10.6k
        if (srv_ctx->delayed_delivery != NULL
667
10.6k
            && (rsp = delayed_delivery(srv_ctx, req)) != NULL) {
668
0
            goto err;
669
0
        }
670
10.6k
        rsp = process_non_polling_request(srv_ctx, req);
671
10.6k
    }
672
673
32.2k
err:
674
32.2k
    if (rsp == NULL) {
675
        /* on error, try to respond with CMP error message to client */
676
32.2k
        const char *data = NULL, *reason = NULL;
677
32.2k
        int flags = 0;
678
32.2k
        unsigned long err = ERR_peek_error_data(&data, &flags);
679
32.2k
        int fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_badRequest;
680
        /* fail_info is not very specific */
681
32.2k
        OSSL_CMP_PKISI *si = NULL;
682
683
32.2k
        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
21.4k
            if (ctx->transactionID == NULL)
691
21.4k
                (void)OSSL_CMP_CTX_set1_transactionID(ctx, hdr->transactionID);
692
21.4k
            (void)ossl_cmp_ctx_set1_recipNonce(ctx, hdr->senderNonce);
693
21.4k
        }
694
695
32.2k
        if ((flags & ERR_TXT_STRING) == 0 || *data == '\0')
696
29.5k
            data = NULL;
697
32.2k
        reason = ERR_reason_error_string(err);
698
32.2k
        if ((si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection,
699
32.2k
                 fail_info, reason))
700
32.2k
            != NULL) {
701
32.2k
            rsp = ossl_cmp_error_new(srv_ctx->ctx, si, err,
702
32.2k
                data, srv_ctx->sendUnprotectedErrors);
703
32.2k
            OSSL_CMP_PKISI_free(si);
704
32.2k
        }
705
32.2k
    }
706
32.2k
    OSSL_CMP_CTX_print_errors(ctx);
707
32.2k
    ctx->secretValue = backup_secret;
708
709
32.2k
    rsp_type = rsp != NULL ? OSSL_CMP_MSG_get_bodytype(rsp) : OSSL_CMP_PKIBODY_ERROR;
710
32.2k
    if (rsp != NULL)
711
0
        ossl_cmp_log1(DEBUG, ctx,
712
32.2k
            "sending %s", ossl_cmp_bodytype_to_string(rsp_type));
713
32.2k
    else
714
32.2k
        ossl_cmp_log(ERR, ctx, "cannot send proper CMP response");
715
716
    /* determine whether to keep the transaction open or not */
717
32.2k
    ctx->status = OSSL_CMP_PKISTATUS_trans;
718
32.2k
    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
32.2k
    case OSSL_CMP_PKIBODY_ERROR:
727
32.2k
        if (rsp != NULL && ossl_cmp_is_error_with_waiting(rsp))
728
0
            break;
729
        /* fall through */
730
731
32.2k
    case OSSL_CMP_PKIBODY_RP:
732
32.2k
    case OSSL_CMP_PKIBODY_PKICONF:
733
32.2k
    case OSSL_CMP_PKIBODY_GENP:
734
        /* Other terminating response message types are not supported */
735
32.2k
        srv_ctx->certReqId = OSSL_CMP_CERTREQID_INVALID;
736
        /* Prepare for next transaction, ignoring any errors here: */
737
32.2k
        if (srv_ctx->clean_transaction != NULL)
738
32.2k
            (void)srv_ctx->clean_transaction(srv_ctx, ctx->transactionID);
739
32.2k
        (void)OSSL_CMP_CTX_set1_transactionID(ctx, NULL);
740
32.2k
        (void)OSSL_CMP_CTX_set1_senderNonce(ctx, NULL);
741
32.2k
        ctx->status = OSSL_CMP_PKISTATUS_unspecified; /* transaction closed */
742
743
32.2k
    default: /* not closing transaction in other cases */
744
32.2k
        break;
745
32.2k
    }
746
32.2k
    return rsp;
747
32.2k
}
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
}