Coverage Report

Created: 2025-12-04 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/crypto/cmp/cmp_server.c
Line
Count
Source
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
    OSSL_CMP_CTX *ctx; /* CMP client context reused for transactionID etc. */
26
    void *custom_ctx;  /* application-specific server context */
27
    int certReqId;     /* of ir/cr/kur, OSSL_CMP_CERTREQID_NONE for p10cr */
28
    int polling;       /* current transaction is in polling mode */
29
30
    OSSL_CMP_SRV_cert_request_cb_t process_cert_request;
31
    OSSL_CMP_SRV_rr_cb_t process_rr;
32
    OSSL_CMP_SRV_genm_cb_t process_genm;
33
    OSSL_CMP_SRV_error_cb_t process_error;
34
    OSSL_CMP_SRV_certConf_cb_t process_certConf;
35
    OSSL_CMP_SRV_pollReq_cb_t process_pollReq;
36
    OSSL_CMP_SRV_delayed_delivery_cb_t delayed_delivery;
37
    OSSL_CMP_SRV_clean_transaction_cb_t clean_transaction;
38
39
    int sendUnprotectedErrors; /* Send error and rejection msgs unprotected */
40
    int acceptUnprotected;     /* Accept requests with no/invalid prot. */
41
    int acceptRAVerified;      /* Accept ir/cr/kur with POPO RAVerified */
42
    int grantImplicitConfirm;  /* Grant implicit confirmation if requested */
43
44
}; /* OSSL_CMP_SRV_CTX */
45
46
void OSSL_CMP_SRV_CTX_free(OSSL_CMP_SRV_CTX *srv_ctx)
47
42.7k
{
48
42.7k
    if (srv_ctx == NULL)
49
0
        return;
50
51
42.7k
    OSSL_CMP_CTX_free(srv_ctx->ctx);
52
42.7k
    OPENSSL_free(srv_ctx);
53
42.7k
}
54
55
OSSL_CMP_SRV_CTX *OSSL_CMP_SRV_CTX_new(OSSL_LIB_CTX *libctx, const char *propq)
56
42.7k
{
57
42.7k
    OSSL_CMP_SRV_CTX *ctx = OPENSSL_zalloc(sizeof(OSSL_CMP_SRV_CTX));
58
59
42.7k
    if (ctx == NULL)
60
0
        goto err;
61
62
42.7k
    if ((ctx->ctx = OSSL_CMP_CTX_new(libctx, propq)) == NULL)
63
0
        goto err;
64
42.7k
    ctx->certReqId = OSSL_CMP_CERTREQID_INVALID;
65
42.7k
    ctx->polling = 0;
66
67
    /* all other elements are initialized to 0 or NULL, respectively */
68
42.7k
    return ctx;
69
0
 err:
70
0
    OSSL_CMP_SRV_CTX_free(ctx);
71
0
    return NULL;
72
42.7k
}
73
74
int OSSL_CMP_SRV_CTX_init(OSSL_CMP_SRV_CTX *srv_ctx, void *custom_ctx,
75
                          OSSL_CMP_SRV_cert_request_cb_t process_cert_request,
76
                          OSSL_CMP_SRV_rr_cb_t process_rr,
77
                          OSSL_CMP_SRV_genm_cb_t process_genm,
78
                          OSSL_CMP_SRV_error_cb_t process_error,
79
                          OSSL_CMP_SRV_certConf_cb_t process_certConf,
80
                          OSSL_CMP_SRV_pollReq_cb_t process_pollReq)
81
42.7k
{
82
42.7k
    if (srv_ctx == NULL) {
83
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
84
0
        return 0;
85
0
    }
86
42.7k
    srv_ctx->custom_ctx = custom_ctx;
87
42.7k
    srv_ctx->process_cert_request = process_cert_request;
88
42.7k
    srv_ctx->process_rr = process_rr;
89
42.7k
    srv_ctx->process_genm = process_genm;
90
42.7k
    srv_ctx->process_error = process_error;
91
42.7k
    srv_ctx->process_certConf = process_certConf;
92
42.7k
    srv_ctx->process_pollReq = process_pollReq;
93
42.7k
    return 1;
94
42.7k
}
95
96
int OSSL_CMP_SRV_CTX_init_trans(OSSL_CMP_SRV_CTX *srv_ctx,
97
                                OSSL_CMP_SRV_delayed_delivery_cb_t delay,
98
                                OSSL_CMP_SRV_clean_transaction_cb_t clean)
99
28.9k
{
100
28.9k
    if (srv_ctx == NULL) {
101
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
102
0
        return 0;
103
0
    }
104
28.9k
    srv_ctx->delayed_delivery = delay;
105
28.9k
    srv_ctx->clean_transaction = clean;
106
28.9k
    return 1;
107
28.9k
}
108
109
OSSL_CMP_CTX *OSSL_CMP_SRV_CTX_get0_cmp_ctx(const OSSL_CMP_SRV_CTX *srv_ctx)
110
42.7k
{
111
42.7k
    if (srv_ctx == NULL) {
112
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
113
0
        return NULL;
114
0
    }
115
42.7k
    return srv_ctx->ctx;
116
42.7k
}
117
118
void *OSSL_CMP_SRV_CTX_get0_custom_ctx(const OSSL_CMP_SRV_CTX *srv_ctx)
119
0
{
120
0
    if (srv_ctx == NULL) {
121
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
122
0
        return NULL;
123
0
    }
124
0
    return srv_ctx->custom_ctx;
125
0
}
126
127
int OSSL_CMP_SRV_CTX_set_send_unprotected_errors(OSSL_CMP_SRV_CTX *srv_ctx,
128
                                                 int val)
129
0
{
130
0
    if (srv_ctx == NULL) {
131
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
132
0
        return 0;
133
0
    }
134
0
    srv_ctx->sendUnprotectedErrors = val != 0;
135
0
    return 1;
136
0
}
137
138
int OSSL_CMP_SRV_CTX_set_accept_unprotected(OSSL_CMP_SRV_CTX *srv_ctx, int val)
139
0
{
140
0
    if (srv_ctx == NULL) {
141
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
142
0
        return 0;
143
0
    }
144
0
    srv_ctx->acceptUnprotected = val != 0;
145
0
    return 1;
146
0
}
147
148
int OSSL_CMP_SRV_CTX_set_accept_raverified(OSSL_CMP_SRV_CTX *srv_ctx, int val)
149
0
{
150
0
    if (srv_ctx == NULL) {
151
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
152
0
        return 0;
153
0
    }
154
0
    srv_ctx->acceptRAVerified = val != 0;
155
0
    return 1;
156
0
}
157
158
int OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(OSSL_CMP_SRV_CTX *srv_ctx,
159
                                                int val)
160
0
{
161
0
    if (srv_ctx == NULL) {
162
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
163
0
        return 0;
164
0
    }
165
0
    srv_ctx->grantImplicitConfirm = val != 0;
166
0
    return 1;
167
0
}
168
169
/* return error msg with waiting status if polling is initiated, else NULL */
170
static OSSL_CMP_MSG *delayed_delivery(OSSL_CMP_SRV_CTX *srv_ctx,
171
                                      const OSSL_CMP_MSG *req)
172
9.85k
{
173
9.85k
    int ret;
174
9.85k
    unsigned long err;
175
9.85k
    int status = OSSL_CMP_PKISTATUS_waiting,
176
9.85k
        fail_info = 0, errorCode = 0;
177
9.85k
    const char *txt = NULL, *details = NULL;
178
9.85k
    OSSL_CMP_PKISI *si;
179
9.85k
    OSSL_CMP_MSG *msg;
180
181
9.85k
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL
182
9.85k
                     && srv_ctx->delayed_delivery != NULL))
183
0
        return NULL;
184
185
9.85k
    ret = srv_ctx->delayed_delivery(srv_ctx, req);
186
9.85k
    if (ret == 0)
187
9.85k
        return NULL;
188
0
    if (ret == 1) {
189
0
        srv_ctx->polling = 1;
190
0
    } else {
191
0
        status = OSSL_CMP_PKISTATUS_rejection;
192
0
        fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_systemFailure;
193
0
        txt = "server application error";
194
0
        err = ERR_peek_error();
195
0
        errorCode = ERR_GET_REASON(err);
196
0
        details = ERR_reason_error_string(err);
197
0
    }
198
199
0
    si = OSSL_CMP_STATUSINFO_new(status, fail_info, txt);
200
0
    if (si == NULL)
201
0
        return NULL;
202
203
0
    msg = ossl_cmp_error_new(srv_ctx->ctx, si, errorCode, details,
204
0
                             srv_ctx->sendUnprotectedErrors);
205
0
    OSSL_CMP_PKISI_free(si);
206
0
    return msg;
207
0
}
208
209
/*
210
 * Processes an ir/cr/p10cr/kur and returns a certification response.
211
 * Only handles the first certification request contained in req
212
 * returns an ip/cp/kup on success and NULL on error
213
 */
214
static OSSL_CMP_MSG *process_cert_request(OSSL_CMP_SRV_CTX *srv_ctx,
215
                                          const OSSL_CMP_MSG *req)
216
2.51k
{
217
2.51k
    OSSL_CMP_MSG *msg = NULL;
218
2.51k
    OSSL_CMP_PKISI *si = NULL;
219
2.51k
    X509 *certOut = NULL;
220
2.51k
    STACK_OF(X509) *chainOut = NULL, *caPubs = NULL;
221
2.51k
    const OSSL_CRMF_MSG *crm = NULL;
222
2.51k
    const X509_REQ *p10cr = NULL;
223
2.51k
    int bodytype;
224
2.51k
    int certReqId;
225
226
2.51k
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
227
0
        return NULL;
228
229
2.51k
    switch (OSSL_CMP_MSG_get_bodytype(req)) {
230
1.48k
    case OSSL_CMP_PKIBODY_P10CR:
231
1.85k
    case OSSL_CMP_PKIBODY_CR:
232
1.85k
        bodytype = OSSL_CMP_PKIBODY_CP;
233
1.85k
        break;
234
613
    case OSSL_CMP_PKIBODY_IR:
235
613
        bodytype = OSSL_CMP_PKIBODY_IP;
236
613
        break;
237
44
    case OSSL_CMP_PKIBODY_KUR:
238
44
        bodytype = OSSL_CMP_PKIBODY_KUP;
239
44
        break;
240
0
    default:
241
0
        ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
242
0
        return NULL;
243
2.51k
    }
244
245
2.51k
    if (OSSL_CMP_MSG_get_bodytype(req) == OSSL_CMP_PKIBODY_P10CR) {
246
1.48k
        certReqId = OSSL_CMP_CERTREQID_NONE; /* p10cr does not include an Id */
247
1.48k
        p10cr = req->body->value.p10cr;
248
1.48k
    } else {
249
1.02k
        OSSL_CRMF_MSGS *reqs = req->body->value.ir; /* same for cr and kur */
250
251
1.02k
        if (sk_OSSL_CRMF_MSG_num(reqs) != 1) {
252
15
            ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED);
253
15
            return NULL;
254
15
        }
255
1.01k
        if ((crm = sk_OSSL_CRMF_MSG_value(reqs, 0)) == NULL) {
256
0
            ERR_raise(ERR_LIB_CMP, CMP_R_CERTREQMSG_NOT_FOUND);
257
0
            return NULL;
258
0
        }
259
1.01k
        certReqId = OSSL_CRMF_MSG_get_certReqId(crm);
260
1.01k
        if (certReqId != OSSL_CMP_CERTREQID) { /* so far, only possible value */
261
154
            ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID);
262
154
            return NULL;
263
154
        }
264
1.01k
    }
265
2.34k
    srv_ctx->certReqId = certReqId;
266
267
2.34k
    if (!ossl_cmp_verify_popo(srv_ctx->ctx, req, srv_ctx->acceptRAVerified)) {
268
        /* Proof of possession could not be verified */
269
0
        si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection,
270
0
                                     1 << OSSL_CMP_PKIFAILUREINFO_badPOP,
271
0
                                     ERR_reason_error_string(ERR_peek_error()));
272
0
        if (si == NULL)
273
0
            return NULL;
274
2.34k
    } else {
275
2.34k
        OSSL_CMP_PKIHEADER *hdr = OSSL_CMP_MSG_get0_header(req);
276
277
2.34k
        si = srv_ctx->process_cert_request(srv_ctx, req, certReqId, crm, p10cr,
278
2.34k
                                           &certOut, &chainOut, &caPubs);
279
2.34k
        if (si == NULL)
280
2.34k
            goto err;
281
0
        if (ossl_cmp_pkisi_get_status(si) == OSSL_CMP_PKISTATUS_waiting)
282
0
            srv_ctx->polling = 1;
283
        /* set OSSL_CMP_OPT_IMPLICIT_CONFIRM if and only if transaction ends */
284
0
        if (!OSSL_CMP_CTX_set_option(srv_ctx->ctx,
285
0
                                     OSSL_CMP_OPT_IMPLICIT_CONFIRM,
286
0
                                     ossl_cmp_hdr_has_implicitConfirm(hdr)
287
0
                                         && srv_ctx->grantImplicitConfirm
288
                                         /* do not set if polling starts: */
289
0
                                         && certOut != NULL))
290
0
            goto err;
291
0
    }
292
293
0
    msg = ossl_cmp_certrep_new(srv_ctx->ctx, bodytype, certReqId, si,
294
0
                               certOut, NULL /* enc */, chainOut, caPubs,
295
0
                               srv_ctx->sendUnprotectedErrors);
296
    /* When supporting OSSL_CRMF_POPO_KEYENC, "enc" will need to be set */
297
0
    if (msg == NULL)
298
0
        ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTREP);
299
300
2.34k
 err:
301
2.34k
    OSSL_CMP_PKISI_free(si);
302
2.34k
    X509_free(certOut);
303
2.34k
    OSSL_STACK_OF_X509_free(chainOut);
304
2.34k
    OSSL_STACK_OF_X509_free(caPubs);
305
2.34k
    return msg;
306
0
}
307
308
static OSSL_CMP_MSG *process_rr(OSSL_CMP_SRV_CTX *srv_ctx,
309
                                const OSSL_CMP_MSG *req)
310
172
{
311
172
    OSSL_CMP_MSG *msg = NULL;
312
172
    OSSL_CMP_REVDETAILS *details;
313
172
    OSSL_CRMF_CERTID *certId = NULL;
314
172
    OSSL_CRMF_CERTTEMPLATE *tmpl;
315
172
    const X509_NAME *issuer;
316
172
    const ASN1_INTEGER *serial;
317
172
    OSSL_CMP_PKISI *si;
318
319
172
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
320
0
        return NULL;
321
322
172
    if (sk_OSSL_CMP_REVDETAILS_num(req->body->value.rr) != 1) {
323
131
        ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED);
324
131
        return NULL;
325
131
    }
326
41
    details = sk_OSSL_CMP_REVDETAILS_value(req->body->value.rr, 0);
327
41
    if (details == NULL) {
328
0
        ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
329
0
        return NULL;
330
0
    }
331
332
41
    tmpl = details->certDetails;
333
41
    issuer = OSSL_CRMF_CERTTEMPLATE_get0_issuer(tmpl);
334
41
    serial = OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(tmpl);
335
41
    if (issuer != NULL && serial != NULL
336
19
            && (certId = OSSL_CRMF_CERTID_gen(issuer, serial)) == NULL)
337
0
        return NULL;
338
41
    if ((si = srv_ctx->process_rr(srv_ctx, req, issuer, serial)) == NULL)
339
41
        goto err;
340
341
0
    if ((msg = ossl_cmp_rp_new(srv_ctx->ctx, si, certId,
342
0
                               srv_ctx->sendUnprotectedErrors)) == NULL)
343
0
        ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_RR);
344
345
41
 err:
346
41
    OSSL_CRMF_CERTID_free(certId);
347
41
    OSSL_CMP_PKISI_free(si);
348
41
    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
409
{
358
409
    OSSL_CMP_GENMSGCONTENT *itavs;
359
409
    OSSL_CMP_MSG *msg;
360
361
409
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
362
0
        return NULL;
363
364
409
    if (!srv_ctx->process_genm(srv_ctx, req, req->body->value.genm, &itavs))
365
409
        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
409
}
371
372
static OSSL_CMP_MSG *process_error(OSSL_CMP_SRV_CTX *srv_ctx,
373
                                   const OSSL_CMP_MSG *req)
374
988
{
375
988
    OSSL_CMP_ERRORMSGCONTENT *errorContent;
376
988
    OSSL_CMP_MSG *msg;
377
378
988
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
379
0
        return NULL;
380
988
    errorContent = req->body->value.error;
381
988
    srv_ctx->process_error(srv_ctx, req, errorContent->pKIStatusInfo,
382
988
                           errorContent->errorCode, errorContent->errorDetails);
383
384
988
    if ((msg = ossl_cmp_pkiconf_new(srv_ctx->ctx)) == NULL)
385
988
        ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF);
386
988
    return msg;
387
988
}
388
389
static OSSL_CMP_MSG *process_certConf(OSSL_CMP_SRV_CTX *srv_ctx,
390
                                      const OSSL_CMP_MSG *req)
391
254
{
392
254
    OSSL_CMP_CTX *ctx;
393
254
    OSSL_CMP_CERTCONFIRMCONTENT *ccc;
394
254
    int num;
395
254
    OSSL_CMP_MSG *msg = NULL;
396
254
    OSSL_CMP_CERTSTATUS *status = NULL;
397
398
254
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
399
0
        return NULL;
400
401
254
    ctx = srv_ctx->ctx;
402
254
    ccc = req->body->value.certConf;
403
254
    num = sk_OSSL_CMP_CERTSTATUS_num(ccc);
404
405
254
    if (OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM) == 1
406
254
            || ctx->status != OSSL_CMP_PKISTATUS_trans) {
407
254
        ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_UNEXPECTED_CERTCONF);
408
254
        return NULL;
409
254
    }
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
9.85k
{
451
9.85k
    OSSL_CMP_MSG *rsp = NULL;
452
453
9.85k
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL
454
9.85k
                     && req->body != NULL))
455
0
        return NULL;
456
457
9.85k
    switch (OSSL_CMP_MSG_get_bodytype(req)) {
458
1.39k
    case OSSL_CMP_PKIBODY_IR:
459
2.33k
    case OSSL_CMP_PKIBODY_CR:
460
5.64k
    case OSSL_CMP_PKIBODY_P10CR:
461
5.93k
    case OSSL_CMP_PKIBODY_KUR:
462
5.93k
        if (srv_ctx->process_cert_request == NULL)
463
5.93k
            ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
464
5.93k
        else
465
5.93k
            rsp = process_cert_request(srv_ctx, req);
466
5.93k
        break;
467
83
    case OSSL_CMP_PKIBODY_RR:
468
83
        if (srv_ctx->process_rr == NULL)
469
83
            ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
470
83
        else
471
83
            rsp = process_rr(srv_ctx, req);
472
83
        break;
473
303
    case OSSL_CMP_PKIBODY_GENM:
474
303
        if (srv_ctx->process_genm == NULL)
475
303
            ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
476
303
        else
477
303
            rsp = process_genm(srv_ctx, req);
478
303
        break;
479
792
    case OSSL_CMP_PKIBODY_ERROR:
480
792
        if (srv_ctx->process_error == NULL)
481
792
            ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
482
792
        else
483
792
            rsp = process_error(srv_ctx, req);
484
792
        break;
485
175
    case OSSL_CMP_PKIBODY_CERTCONF:
486
175
        if (srv_ctx->process_certConf == NULL)
487
175
            ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
488
175
        else
489
175
            rsp = process_certConf(srv_ctx, req);
490
175
        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.56k
    default:
496
2.56k
        ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
497
2.56k
        break;
498
9.85k
    }
499
500
9.85k
    return rsp;
501
9.85k
}
502
503
static OSSL_CMP_MSG *process_pollReq(OSSL_CMP_SRV_CTX *srv_ctx,
504
                                     const OSSL_CMP_MSG *req)
505
135
{
506
135
    OSSL_CMP_POLLREQCONTENT *prc;
507
135
    OSSL_CMP_POLLREQ *pr;
508
135
    int certReqId;
509
135
    OSSL_CMP_MSG *orig_req;
510
135
    int64_t check_after = 0;
511
135
    OSSL_CMP_MSG *msg = NULL;
512
513
135
    if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
514
0
        return NULL;
515
516
135
    if (!srv_ctx->polling) {
517
135
        ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
518
135
        return NULL;
519
135
    }
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)) == NULL)
540
0
            ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREP);
541
0
    }
542
0
    return msg;
543
0
}
544
545
/*
546
 * Determine whether missing/invalid protection of request message is allowed.
547
 * Return 1 on acceptance, 0 on rejection, or -1 on (internal) error.
548
 */
549
static int unprotected_exception(const OSSL_CMP_CTX *ctx,
550
                                 const OSSL_CMP_MSG *req,
551
                                 int invalid_protection,
552
                                 int accept_unprotected_requests)
553
15.3k
{
554
15.3k
    if (!ossl_assert(ctx != NULL && req != NULL))
555
0
        return -1;
556
557
15.3k
    if (accept_unprotected_requests) {
558
0
        ossl_cmp_log1(WARN, ctx, "ignoring %s protection of request message",
559
0
                      invalid_protection ? "invalid" : "missing");
560
0
        return 1;
561
0
    }
562
15.3k
    if (OSSL_CMP_MSG_get_bodytype(req) == OSSL_CMP_PKIBODY_ERROR
563
988
        && OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS) == 1) {
564
0
        ossl_cmp_warn(ctx, "ignoring missing protection of error message");
565
0
        return 1;
566
0
    }
567
15.3k
    return 0;
568
15.3k
}
569
570
/*
571
 * returns created message and NULL on internal error
572
 */
573
OSSL_CMP_MSG *OSSL_CMP_SRV_process_request(OSSL_CMP_SRV_CTX *srv_ctx,
574
                                           const OSSL_CMP_MSG *req)
575
28.9k
{
576
28.9k
    OSSL_CMP_CTX *ctx;
577
28.9k
    ASN1_OCTET_STRING *backup_secret;
578
28.9k
    OSSL_CMP_PKIHEADER *hdr;
579
28.9k
    int req_type, rsp_type;
580
28.9k
    int req_verified = 0;
581
28.9k
    OSSL_CMP_MSG *rsp = NULL;
582
583
28.9k
    if (srv_ctx == NULL || srv_ctx->ctx == NULL
584
28.9k
            || req == NULL || req->body == NULL
585
28.9k
            || (hdr = OSSL_CMP_MSG_get0_header(req)) == NULL) {
586
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
587
0
        return 0;
588
0
    }
589
28.9k
    ctx = srv_ctx->ctx;
590
28.9k
    backup_secret = ctx->secretValue;
591
28.9k
    req_type = OSSL_CMP_MSG_get_bodytype(req);
592
28.9k
    ossl_cmp_log1(DEBUG, ctx,
593
28.9k
                  "received %s", ossl_cmp_bodytype_to_string(req_type));
594
595
    /*
596
     * Some things need to be done already before validating the message in
597
     * order to be able to send an error message as far as needed and possible.
598
     */
599
28.9k
    if (hdr->sender->type != GEN_DIRNAME) {
600
18.9k
        ERR_raise(ERR_LIB_CMP, CMP_R_SENDER_GENERALNAME_TYPE_NOT_SUPPORTED);
601
18.9k
        goto err;
602
18.9k
    }
603
9.99k
    if (!OSSL_CMP_CTX_set1_recipient(ctx, hdr->sender->d.directoryName))
604
4
        goto err;
605
606
9.98k
    if (srv_ctx->polling && req_type != OSSL_CMP_PKIBODY_POLLREQ
607
0
            && req_type != OSSL_CMP_PKIBODY_ERROR) {
608
0
        ERR_raise(ERR_LIB_CMP, CMP_R_EXPECTED_POLLREQ);
609
0
        goto err;
610
0
    }
611
612
9.98k
    switch (req_type) {
613
1.39k
    case OSSL_CMP_PKIBODY_IR:
614
2.33k
    case OSSL_CMP_PKIBODY_CR:
615
5.64k
    case OSSL_CMP_PKIBODY_P10CR:
616
5.93k
    case OSSL_CMP_PKIBODY_KUR:
617
6.01k
    case OSSL_CMP_PKIBODY_RR:
618
6.32k
    case OSSL_CMP_PKIBODY_GENM:
619
7.11k
    case OSSL_CMP_PKIBODY_ERROR:
620
7.11k
        if (ctx->transactionID != NULL) {
621
0
            char *tid = i2s_ASN1_OCTET_STRING(NULL, ctx->transactionID);
622
623
0
            if (tid != NULL)
624
0
                ossl_cmp_log1(WARN, ctx,
625
0
                              "Assuming that last transaction with ID=%s got aborted",
626
0
                              tid);
627
0
            OPENSSL_free(tid);
628
0
        }
629
        /* start of a new transaction, reset transactionID and senderNonce */
630
7.11k
        if (!OSSL_CMP_CTX_set1_transactionID(ctx, NULL)
631
7.11k
                || !OSSL_CMP_CTX_set1_senderNonce(ctx, NULL))
632
0
            goto err;
633
634
7.11k
        if (srv_ctx->clean_transaction != NULL
635
7.11k
                && !srv_ctx->clean_transaction(srv_ctx, NULL)) {
636
0
            ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
637
0
            goto err;
638
0
        }
639
640
7.11k
        break;
641
7.11k
    default:
642
        /* transactionID should be already initialized */
643
2.87k
        if (ctx->transactionID == NULL) {
644
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
645
            ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
646
            goto err;
647
#endif
648
2.87k
        }
649
9.98k
    }
650
651
9.98k
    req_verified = ossl_cmp_msg_check_update(ctx, req, unprotected_exception,
652
9.98k
                                             srv_ctx->acceptUnprotected);
653
9.98k
    if (ctx->secretValue != NULL && ctx->pkey != NULL
654
0
            && ossl_cmp_hdr_get_protection_nid(hdr) != NID_id_PasswordBasedMAC)
655
0
        ctx->secretValue = NULL; /* use MSG_SIG_ALG when protecting rsp */
656
9.98k
    if (!req_verified)
657
0
        goto err;
658
659
9.98k
    if (req_type == OSSL_CMP_PKIBODY_POLLREQ) {
660
135
        if (srv_ctx->process_pollReq == NULL)
661
135
            ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PKIBODY);
662
135
        else
663
135
            rsp = process_pollReq(srv_ctx, req);
664
9.85k
    } else {
665
9.85k
        if (srv_ctx->delayed_delivery != NULL
666
9.85k
            && (rsp = delayed_delivery(srv_ctx, req)) != NULL) {
667
0
            goto err;
668
0
        }
669
9.85k
        rsp = process_non_polling_request(srv_ctx, req);
670
9.85k
    }
671
672
28.9k
 err:
673
28.9k
    if (rsp == NULL) {
674
        /* on error, try to respond with CMP error message to client */
675
28.9k
        const char *data = NULL, *reason = NULL;
676
28.9k
        int flags = 0;
677
28.9k
        unsigned long err = ERR_peek_error_data(&data, &flags);
678
28.9k
        int fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_badRequest;
679
        /* fail_info is not very specific */
680
28.9k
        OSSL_CMP_PKISI *si = NULL;
681
682
28.9k
        if (!req_verified) {
683
            /*
684
             * Above ossl_cmp_msg_check_update() was not successfully executed,
685
             * which normally would set ctx->transactionID and ctx->recipNonce.
686
             * So anyway try to provide the right transactionID and recipNonce,
687
             * while ignoring any (extra) error in next two function calls.
688
             */
689
18.9k
            if (ctx->transactionID == NULL)
690
18.9k
                (void)OSSL_CMP_CTX_set1_transactionID(ctx, hdr->transactionID);
691
18.9k
            (void)ossl_cmp_ctx_set1_recipNonce(ctx, hdr->senderNonce);
692
18.9k
        }
693
694
28.9k
        if ((flags & ERR_TXT_STRING) == 0 || *data == '\0')
695
26.6k
            data = NULL;
696
28.9k
        reason = ERR_reason_error_string(err);
697
28.9k
        if ((si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection,
698
28.9k
                                          fail_info, reason)) != NULL) {
699
28.9k
            rsp = ossl_cmp_error_new(srv_ctx->ctx, si, err,
700
28.9k
                                     data, srv_ctx->sendUnprotectedErrors);
701
28.9k
            OSSL_CMP_PKISI_free(si);
702
28.9k
        }
703
28.9k
    }
704
28.9k
    OSSL_CMP_CTX_print_errors(ctx);
705
28.9k
    ctx->secretValue = backup_secret;
706
707
28.9k
    rsp_type =
708
28.9k
        rsp != NULL ? OSSL_CMP_MSG_get_bodytype(rsp) : OSSL_CMP_PKIBODY_ERROR;
709
28.9k
    if (rsp != NULL)
710
0
        ossl_cmp_log1(DEBUG, ctx,
711
28.9k
                      "sending %s", ossl_cmp_bodytype_to_string(rsp_type));
712
28.9k
    else
713
28.9k
        ossl_cmp_log(ERR, ctx, "cannot send proper CMP response");
714
715
    /* determine whether to keep the transaction open or not */
716
28.9k
    ctx->status = OSSL_CMP_PKISTATUS_trans;
717
28.9k
    switch (rsp_type) {
718
0
    case OSSL_CMP_PKIBODY_IP:
719
0
    case OSSL_CMP_PKIBODY_CP:
720
0
    case OSSL_CMP_PKIBODY_KUP:
721
0
        if (OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM) == 0)
722
0
            break;
723
        /* fall through */
724
725
28.9k
    case OSSL_CMP_PKIBODY_ERROR:
726
28.9k
        if (rsp != NULL && ossl_cmp_is_error_with_waiting(rsp))
727
0
            break;
728
        /* fall through */
729
730
28.9k
    case OSSL_CMP_PKIBODY_RP:
731
28.9k
    case OSSL_CMP_PKIBODY_PKICONF:
732
28.9k
    case OSSL_CMP_PKIBODY_GENP:
733
        /* Other terminating response message types are not supported */
734
28.9k
        srv_ctx->certReqId = OSSL_CMP_CERTREQID_INVALID;
735
        /* Prepare for next transaction, ignoring any errors here: */
736
28.9k
        if (srv_ctx->clean_transaction != NULL)
737
28.9k
            (void)srv_ctx->clean_transaction(srv_ctx, ctx->transactionID);
738
28.9k
        (void)OSSL_CMP_CTX_set1_transactionID(ctx, NULL);
739
28.9k
        (void)OSSL_CMP_CTX_set1_senderNonce(ctx, NULL);
740
28.9k
        ctx->status = OSSL_CMP_PKISTATUS_unspecified; /* transaction closed */
741
742
28.9k
    default: /* not closing transaction in other cases */
743
28.9k
        break;
744
28.9k
    }
745
28.9k
    return rsp;
746
28.9k
}
747
748
/*
749
 * Server interface that may substitute OSSL_CMP_MSG_http_perform at the client.
750
 * The OSSL_CMP_SRV_CTX must be set as client_ctx->transfer_cb_arg.
751
 * returns received message on success, else NULL and pushes an element on the
752
 * error stack.
753
 */
754
OSSL_CMP_MSG *OSSL_CMP_CTX_server_perform(OSSL_CMP_CTX *client_ctx,
755
                                          const OSSL_CMP_MSG *req)
756
0
{
757
0
    OSSL_CMP_SRV_CTX *srv_ctx = NULL;
758
759
0
    if (client_ctx == NULL || req == NULL) {
760
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
761
0
        return NULL;
762
0
    }
763
764
0
    if ((srv_ctx = OSSL_CMP_CTX_get_transfer_cb_arg(client_ctx)) == NULL) {
765
0
        ERR_raise(ERR_LIB_CMP, CMP_R_TRANSFER_ERROR);
766
0
        return NULL;
767
0
    }
768
769
0
    return OSSL_CMP_SRV_process_request(srv_ctx, req);
770
0
}