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