Coverage Report

Created: 2025-12-04 06:33

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
42.7k
{
47
42.7k
    if (srv_ctx == NULL)
48
0
        return;
49
50
42.7k
    OSSL_CMP_CTX_free(srv_ctx->ctx);
51
42.7k
    OPENSSL_free(srv_ctx);
52
42.7k
}
53
54
OSSL_CMP_SRV_CTX *OSSL_CMP_SRV_CTX_new(OSSL_LIB_CTX *libctx, const char *propq)
55
42.7k
{
56
42.7k
    OSSL_CMP_SRV_CTX *ctx = OPENSSL_zalloc(sizeof(OSSL_CMP_SRV_CTX));
57
58
42.7k
    if (ctx == NULL)
59
0
        goto err;
60
61
42.7k
    if ((ctx->ctx = OSSL_CMP_CTX_new(libctx, propq)) == NULL)
62
0
        goto err;
63
42.7k
    ctx->certReqId = OSSL_CMP_CERTREQID_INVALID;
64
42.7k
    ctx->polling = 0;
65
66
    /* all other elements are initialized to 0 or NULL, respectively */
67
42.7k
    return ctx;
68
0
 err:
69
0
    OSSL_CMP_SRV_CTX_free(ctx);
70
0
    return NULL;
71
42.7k
}
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
42.7k
{
81
42.7k
    if (srv_ctx == NULL) {
82
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
83
0
        return 0;
84
0
    }
85
42.7k
    srv_ctx->custom_ctx = custom_ctx;
86
42.7k
    srv_ctx->process_cert_request = process_cert_request;
87
42.7k
    srv_ctx->process_rr = process_rr;
88
42.7k
    srv_ctx->process_genm = process_genm;
89
42.7k
    srv_ctx->process_error = process_error;
90
42.7k
    srv_ctx->process_certConf = process_certConf;
91
42.7k
    srv_ctx->process_pollReq = process_pollReq;
92
42.7k
    return 1;
93
42.7k
}
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
28.9k
{
99
28.9k
    if (srv_ctx == NULL) {
100
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
101
0
        return 0;
102
0
    }
103
28.9k
    srv_ctx->delayed_delivery = delay;
104
28.9k
    srv_ctx->clean_transaction = clean;
105
28.9k
    return 1;
106
28.9k
}
107
108
OSSL_CMP_CTX *OSSL_CMP_SRV_CTX_get0_cmp_ctx(const OSSL_CMP_SRV_CTX *srv_ctx)
109
42.7k
{
110
42.7k
    if (srv_ctx == NULL) {
111
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
112
0
        return NULL;
113
0
    }
114
42.7k
    return srv_ctx->ctx;
115
42.7k
}
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
9.85k
{
172
9.85k
    int ret;
173
9.85k
    unsigned long err;
174
9.85k
    int status = OSSL_CMP_PKISTATUS_waiting,
175
9.85k
        fail_info = 0, errorCode = 0;
176
9.85k
    const char *txt = NULL, *details = NULL;
177
9.85k
    OSSL_CMP_PKISI *si;
178
9.85k
    OSSL_CMP_MSG *msg;
179
180
9.85k
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL
181
9.85k
                     && srv_ctx->delayed_delivery != NULL))
182
0
        return NULL;
183
184
9.85k
    ret = srv_ctx->delayed_delivery(srv_ctx, req);
185
9.85k
    if (ret == 0)
186
9.85k
        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.51k
{
216
2.51k
    OSSL_CMP_MSG *msg = NULL;
217
2.51k
    OSSL_CMP_PKISI *si = NULL;
218
2.51k
    X509 *certOut = NULL;
219
2.51k
    STACK_OF(X509) *chainOut = NULL, *caPubs = NULL;
220
2.51k
    const OSSL_CRMF_MSG *crm = NULL;
221
2.51k
    const X509_REQ *p10cr = NULL;
222
2.51k
    int bodytype;
223
2.51k
    int certReqId;
224
225
2.51k
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
226
0
        return NULL;
227
228
2.51k
    switch (OSSL_CMP_MSG_get_bodytype(req)) {
229
1.48k
    case OSSL_CMP_PKIBODY_P10CR:
230
1.85k
    case OSSL_CMP_PKIBODY_CR:
231
1.85k
        bodytype = OSSL_CMP_PKIBODY_CP;
232
1.85k
        break;
233
613
    case OSSL_CMP_PKIBODY_IR:
234
613
        bodytype = OSSL_CMP_PKIBODY_IP;
235
613
        break;
236
44
    case OSSL_CMP_PKIBODY_KUR:
237
44
        bodytype = OSSL_CMP_PKIBODY_KUP;
238
44
        break;
239
0
    default:
240
0
        ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
241
0
        return NULL;
242
2.51k
    }
243
244
2.51k
    if (OSSL_CMP_MSG_get_bodytype(req) == OSSL_CMP_PKIBODY_P10CR) {
245
1.48k
        certReqId = OSSL_CMP_CERTREQID_NONE; /* p10cr does not include an Id */
246
1.48k
        p10cr = req->body->value.p10cr;
247
1.48k
    } else {
248
1.02k
        OSSL_CRMF_MSGS *reqs = req->body->value.ir; /* same for cr and kur */
249
250
1.02k
        if (sk_OSSL_CRMF_MSG_num(reqs) != 1) {
251
15
            ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED);
252
15
            return NULL;
253
15
        }
254
1.01k
        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.01k
        certReqId = OSSL_CRMF_MSG_get_certReqId(crm);
259
1.01k
        if (certReqId != OSSL_CMP_CERTREQID) { /* so far, only possible value */
260
154
            ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID);
261
154
            return NULL;
262
154
        }
263
1.01k
    }
264
2.34k
    srv_ctx->certReqId = certReqId;
265
266
2.34k
    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.34k
    } else {
274
2.34k
        OSSL_CMP_PKIHEADER *hdr = OSSL_CMP_MSG_get0_header(req);
275
276
2.34k
        si = srv_ctx->process_cert_request(srv_ctx, req, certReqId, crm, p10cr,
277
2.34k
                                           &certOut, &chainOut, &caPubs);
278
2.34k
        if (si == NULL)
279
2.34k
            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.34k
 err:
300
2.34k
    OSSL_CMP_PKISI_free(si);
301
2.34k
    X509_free(certOut);
302
2.34k
    OSSL_STACK_OF_X509_free(chainOut);
303
2.34k
    OSSL_STACK_OF_X509_free(caPubs);
304
2.34k
    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
172
{
310
172
    OSSL_CMP_MSG *msg = NULL;
311
172
    OSSL_CMP_REVDETAILS *details;
312
172
    OSSL_CRMF_CERTID *certId = NULL;
313
172
    OSSL_CRMF_CERTTEMPLATE *tmpl;
314
172
    const X509_NAME *issuer;
315
172
    const ASN1_INTEGER *serial;
316
172
    OSSL_CMP_PKISI *si;
317
318
172
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
319
0
        return NULL;
320
321
172
    if (sk_OSSL_CMP_REVDETAILS_num(req->body->value.rr) != 1) {
322
131
        ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED);
323
131
        return NULL;
324
131
    }
325
41
    details = sk_OSSL_CMP_REVDETAILS_value(req->body->value.rr, 0);
326
41
    if (details == NULL) {
327
0
        ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
328
0
        return NULL;
329
0
    }
330
331
41
    tmpl = details->certDetails;
332
41
    issuer = OSSL_CRMF_CERTTEMPLATE_get0_issuer(tmpl);
333
41
    serial = OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(tmpl);
334
41
    if (issuer != NULL && serial != NULL
335
19
            && (certId = OSSL_CRMF_CERTID_gen(issuer, serial)) == NULL)
336
0
        return NULL;
337
41
    if ((si = srv_ctx->process_rr(srv_ctx, req, issuer, serial)) == NULL)
338
41
        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
41
 err:
345
41
    OSSL_CRMF_CERTID_free(certId);
346
41
    OSSL_CMP_PKISI_free(si);
347
41
    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
409
{
357
409
    OSSL_CMP_GENMSGCONTENT *itavs;
358
409
    OSSL_CMP_MSG *msg;
359
360
409
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
361
0
        return NULL;
362
363
409
    if (!srv_ctx->process_genm(srv_ctx, req, req->body->value.genm, &itavs))
364
409
        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
409
}
370
371
static OSSL_CMP_MSG *process_error(OSSL_CMP_SRV_CTX *srv_ctx,
372
                                   const OSSL_CMP_MSG *req)
373
988
{
374
988
    OSSL_CMP_ERRORMSGCONTENT *errorContent;
375
988
    OSSL_CMP_MSG *msg;
376
377
988
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
378
0
        return NULL;
379
988
    errorContent = req->body->value.error;
380
988
    srv_ctx->process_error(srv_ctx, req, errorContent->pKIStatusInfo,
381
988
                           errorContent->errorCode, errorContent->errorDetails);
382
383
988
    if ((msg = ossl_cmp_pkiconf_new(srv_ctx->ctx)) == NULL)
384
988
        ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF);
385
988
    return msg;
386
988
}
387
388
static OSSL_CMP_MSG *process_certConf(OSSL_CMP_SRV_CTX *srv_ctx,
389
                                      const OSSL_CMP_MSG *req)
390
254
{
391
254
    OSSL_CMP_CTX *ctx;
392
254
    OSSL_CMP_CERTCONFIRMCONTENT *ccc;
393
254
    int num;
394
254
    OSSL_CMP_MSG *msg = NULL;
395
254
    OSSL_CMP_CERTSTATUS *status = NULL;
396
397
254
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
398
0
        return NULL;
399
400
254
    ctx = srv_ctx->ctx;
401
254
    ccc = req->body->value.certConf;
402
254
    num = sk_OSSL_CMP_CERTSTATUS_num(ccc);
403
404
254
    if (OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM) == 1
405
254
            || ctx->status != OSSL_CMP_PKISTATUS_trans) {
406
254
        ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_UNEXPECTED_CERTCONF);
407
254
        return NULL;
408
254
    }
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
9.85k
{
450
9.85k
    OSSL_CMP_MSG *rsp = NULL;
451
452
9.85k
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL
453
9.85k
                     && req->body != NULL))
454
0
        return NULL;
455
456
9.85k
    switch (OSSL_CMP_MSG_get_bodytype(req)) {
457
1.39k
    case OSSL_CMP_PKIBODY_IR:
458
2.33k
    case OSSL_CMP_PKIBODY_CR:
459
5.64k
    case OSSL_CMP_PKIBODY_P10CR:
460
5.93k
    case OSSL_CMP_PKIBODY_KUR:
461
5.93k
        if (srv_ctx->process_cert_request == NULL)
462
5.93k
            ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
463
5.93k
        else
464
5.93k
            rsp = process_cert_request(srv_ctx, req);
465
5.93k
        break;
466
83
    case OSSL_CMP_PKIBODY_RR:
467
83
        if (srv_ctx->process_rr == NULL)
468
83
            ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
469
83
        else
470
83
            rsp = process_rr(srv_ctx, req);
471
83
        break;
472
303
    case OSSL_CMP_PKIBODY_GENM:
473
303
        if (srv_ctx->process_genm == NULL)
474
303
            ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
475
303
        else
476
303
            rsp = process_genm(srv_ctx, req);
477
303
        break;
478
792
    case OSSL_CMP_PKIBODY_ERROR:
479
792
        if (srv_ctx->process_error == NULL)
480
792
            ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
481
792
        else
482
792
            rsp = process_error(srv_ctx, req);
483
792
        break;
484
175
    case OSSL_CMP_PKIBODY_CERTCONF:
485
175
        if (srv_ctx->process_certConf == NULL)
486
175
            ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
487
175
        else
488
175
            rsp = process_certConf(srv_ctx, req);
489
175
        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.56k
    default:
495
2.56k
        ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
496
2.56k
        break;
497
9.85k
    }
498
499
9.85k
    return rsp;
500
9.85k
}
501
502
static OSSL_CMP_MSG *process_pollReq(OSSL_CMP_SRV_CTX *srv_ctx,
503
                                     const OSSL_CMP_MSG *req)
504
135
{
505
135
    OSSL_CMP_POLLREQCONTENT *prc;
506
135
    OSSL_CMP_POLLREQ *pr;
507
135
    int certReqId;
508
135
    OSSL_CMP_MSG *orig_req;
509
135
    int64_t check_after = 0;
510
135
    OSSL_CMP_MSG *msg = NULL;
511
512
135
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
513
0
        return NULL;
514
515
135
    if (!srv_ctx->polling) {
516
135
        ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
517
135
        return NULL;
518
135
    }
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
15.3k
{
553
15.3k
    if (!ossl_assert(ctx != NULL && req != NULL))
554
0
        return -1;
555
556
15.3k
    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
15.3k
    if (OSSL_CMP_MSG_get_bodytype(req) == OSSL_CMP_PKIBODY_ERROR
562
988
        && 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
15.3k
    return 0;
567
15.3k
}
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
28.9k
{
575
28.9k
    OSSL_CMP_CTX *ctx;
576
28.9k
    ASN1_OCTET_STRING *backup_secret;
577
28.9k
    OSSL_CMP_PKIHEADER *hdr;
578
28.9k
    int req_type, rsp_type;
579
28.9k
    int req_verified = 0;
580
28.9k
    OSSL_CMP_MSG *rsp = NULL;
581
582
28.9k
    if (srv_ctx == NULL || srv_ctx->ctx == NULL
583
28.9k
            || req == NULL || req->body == NULL
584
28.9k
            || (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
28.9k
    ctx = srv_ctx->ctx;
589
28.9k
    backup_secret = ctx->secretValue;
590
28.9k
    req_type = OSSL_CMP_MSG_get_bodytype(req);
591
28.9k
    ossl_cmp_log1(DEBUG, ctx,
592
28.9k
                  "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
28.9k
    if (hdr->sender->type != GEN_DIRNAME) {
599
18.9k
        ERR_raise(ERR_LIB_CMP, CMP_R_SENDER_GENERALNAME_TYPE_NOT_SUPPORTED);
600
18.9k
        goto err;
601
18.9k
    }
602
9.99k
    if (!OSSL_CMP_CTX_set1_recipient(ctx, hdr->sender->d.directoryName))
603
4
        goto err;
604
605
9.98k
    if (srv_ctx->polling && req_type != OSSL_CMP_PKIBODY_POLLREQ
606
0
            && 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
9.98k
    switch (req_type) {
612
1.39k
    case OSSL_CMP_PKIBODY_IR:
613
2.33k
    case OSSL_CMP_PKIBODY_CR:
614
5.64k
    case OSSL_CMP_PKIBODY_P10CR:
615
5.93k
    case OSSL_CMP_PKIBODY_KUR:
616
6.01k
    case OSSL_CMP_PKIBODY_RR:
617
6.32k
    case OSSL_CMP_PKIBODY_GENM:
618
7.11k
    case OSSL_CMP_PKIBODY_ERROR:
619
7.11k
        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
7.11k
        if (!OSSL_CMP_CTX_set1_transactionID(ctx, NULL)
630
7.11k
                || !OSSL_CMP_CTX_set1_senderNonce(ctx, NULL))
631
0
            goto err;
632
633
7.11k
        if (srv_ctx->clean_transaction != NULL
634
7.11k
                && !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
7.11k
        break;
640
7.11k
    default:
641
        /* transactionID should be already initialized */
642
2.87k
        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.87k
        }
648
9.98k
    }
649
650
9.98k
    req_verified = ossl_cmp_msg_check_update(ctx, req, unprotected_exception,
651
9.98k
                                             srv_ctx->acceptUnprotected);
652
9.98k
    if (ctx->secretValue != NULL && ctx->pkey != NULL
653
0
            && ossl_cmp_hdr_get_protection_nid(hdr) != NID_id_PasswordBasedMAC)
654
0
        ctx->secretValue = NULL; /* use MSG_SIG_ALG when protecting rsp */
655
9.98k
    if (!req_verified)
656
0
        goto err;
657
658
9.98k
    if (req_type == OSSL_CMP_PKIBODY_POLLREQ) {
659
135
        if (srv_ctx->process_pollReq == NULL)
660
135
            ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
661
135
        else
662
135
            rsp = process_pollReq(srv_ctx, req);
663
9.85k
    } else {
664
9.85k
        if (srv_ctx->delayed_delivery != NULL
665
9.85k
            && (rsp = delayed_delivery(srv_ctx, req)) != NULL) {
666
0
            goto err;
667
0
        }
668
9.85k
        rsp = process_non_polling_request(srv_ctx, req);
669
9.85k
    }
670
671
28.9k
 err:
672
28.9k
    if (rsp == NULL) {
673
        /* on error, try to respond with CMP error message to client */
674
28.9k
        const char *data = NULL, *reason = NULL;
675
28.9k
        int flags = 0;
676
28.9k
        unsigned long err = ERR_peek_error_data(&data, &flags);
677
28.9k
        int fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_badRequest;
678
        /* fail_info is not very specific */
679
28.9k
        OSSL_CMP_PKISI *si = NULL;
680
681
28.9k
        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
18.9k
            if (ctx->transactionID == NULL)
689
18.9k
                (void)OSSL_CMP_CTX_set1_transactionID(ctx, hdr->transactionID);
690
18.9k
            (void)ossl_cmp_ctx_set1_recipNonce(ctx, hdr->senderNonce);
691
18.9k
        }
692
693
28.9k
        if ((flags & ERR_TXT_STRING) == 0 || *data == '\0')
694
26.6k
            data = NULL;
695
28.9k
        reason = ERR_reason_error_string(err);
696
28.9k
        if ((si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection,
697
28.9k
                                          fail_info, reason)) != NULL) {
698
28.9k
            rsp = ossl_cmp_error_new(srv_ctx->ctx, si, err,
699
28.9k
                                     data, srv_ctx->sendUnprotectedErrors);
700
28.9k
            OSSL_CMP_PKISI_free(si);
701
28.9k
        }
702
28.9k
    }
703
28.9k
    OSSL_CMP_CTX_print_errors(ctx);
704
28.9k
    ctx->secretValue = backup_secret;
705
706
28.9k
    rsp_type =
707
28.9k
        rsp != NULL ? OSSL_CMP_MSG_get_bodytype(rsp) : OSSL_CMP_PKIBODY_ERROR;
708
28.9k
    if (rsp != NULL)
709
0
        ossl_cmp_log1(DEBUG, ctx,
710
28.9k
                      "sending %s", ossl_cmp_bodytype_to_string(rsp_type));
711
28.9k
    else
712
28.9k
        ossl_cmp_log(ERR, ctx, "cannot send proper CMP response");
713
714
    /* determine whether to keep the transaction open or not */
715
28.9k
    ctx->status = OSSL_CMP_PKISTATUS_trans;
716
28.9k
    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
28.9k
    case OSSL_CMP_PKIBODY_ERROR:
725
28.9k
        if (rsp != NULL && ossl_cmp_is_error_with_waiting(rsp))
726
0
            break;
727
        /* fall through */
728
729
28.9k
    case OSSL_CMP_PKIBODY_RP:
730
28.9k
    case OSSL_CMP_PKIBODY_PKICONF:
731
28.9k
    case OSSL_CMP_PKIBODY_GENP:
732
        /* Other terminating response message types are not supported */
733
28.9k
        srv_ctx->certReqId = OSSL_CMP_CERTREQID_INVALID;
734
        /* Prepare for next transaction, ignoring any errors here: */
735
28.9k
        if (srv_ctx->clean_transaction != NULL)
736
28.9k
            (void)srv_ctx->clean_transaction(srv_ctx, ctx->transactionID);
737
28.9k
        (void)OSSL_CMP_CTX_set1_transactionID(ctx, NULL);
738
28.9k
        (void)OSSL_CMP_CTX_set1_senderNonce(ctx, NULL);
739
28.9k
        ctx->status = OSSL_CMP_PKISTATUS_unspecified; /* transaction closed */
740
741
28.9k
    default: /* not closing transaction in other cases */
742
28.9k
        break;
743
28.9k
    }
744
28.9k
    return rsp;
745
28.9k
}
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
}