Coverage Report

Created: 2026-04-01 06:39

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