Coverage Report

Created: 2024-07-27 06:39

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