Coverage Report

Created: 2025-08-28 07:07

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