Coverage Report

Created: 2025-12-31 06:58

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