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