a91f67b2647ba0aae03b423a9750826474f9b8ac
[openssl.git] / crypto / cmp / cmp_server.c
1 /*
2  * Copyright 2007-2019 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright Nokia 2007-2019
4  * Copyright Siemens AG 2015-2019
5  *
6  * Licensed under the Apache License 2.0 (the "License").  You may not use
7  * this file except in compliance with the License.  You can obtain a copy
8  * in the file LICENSE in the source distribution or at
9  * https://www.openssl.org/source/license.html
10  */
11
12 /* general CMP server functions */
13
14 #include <openssl/asn1t.h>
15
16 #include "cmp_local.h"
17
18 /* explicit #includes not strictly needed since implied by the above: */
19 #include <openssl/cmp.h>
20 #include <openssl/err.h>
21
22 /* the context for the generic CMP server */
23 struct ossl_cmp_srv_ctx_st
24 {
25     OSSL_CMP_CTX *ctx; /* Client CMP context, partly reused for srv */
26     void *custom_ctx;  /* pointer to specific server context */
27
28     OSSL_CMP_SRV_cert_request_cb_t process_cert_request;
29     OSSL_CMP_SRV_rr_cb_t process_rr;
30     OSSL_CMP_SRV_genm_cb_t process_genm;
31     OSSL_CMP_SRV_error_cb_t process_error;
32     OSSL_CMP_SRV_certConf_cb_t process_certConf;
33     OSSL_CMP_SRV_pollReq_cb_t process_pollReq;
34
35     int sendUnprotectedErrors; /* Send error and rejection msgs unprotected */
36     int acceptUnprotected;     /* Accept requests with no/invalid prot. */
37     int acceptRAVerified;      /* Accept ir/cr/kur with POPO RAVerified */
38     int grantImplicitConfirm;  /* Grant implicit confirmation if requested */
39
40 }; /* OSSL_CMP_SRV_CTX */
41
42 void OSSL_CMP_SRV_CTX_free(OSSL_CMP_SRV_CTX *srv_ctx)
43 {
44     if (srv_ctx == NULL)
45         return;
46
47     OSSL_CMP_CTX_free(srv_ctx->ctx);
48     OPENSSL_free(srv_ctx);
49 }
50
51 OSSL_CMP_SRV_CTX *OSSL_CMP_SRV_CTX_new(void)
52 {
53     OSSL_CMP_SRV_CTX *ctx = OPENSSL_zalloc(sizeof(OSSL_CMP_SRV_CTX));
54
55     if (ctx == NULL)
56         goto err;
57
58     if ((ctx->ctx = OSSL_CMP_CTX_new()) == NULL)
59         goto err;
60
61     /* all other elements are initialized to 0 or NULL, respectively */
62     return ctx;
63  err:
64     OSSL_CMP_SRV_CTX_free(ctx);
65     return NULL;
66 }
67
68 int OSSL_CMP_SRV_CTX_init(OSSL_CMP_SRV_CTX *srv_ctx, void *custom_ctx,
69                           OSSL_CMP_SRV_cert_request_cb_t process_cert_request,
70                           OSSL_CMP_SRV_rr_cb_t process_rr,
71                           OSSL_CMP_SRV_genm_cb_t process_genm,
72                           OSSL_CMP_SRV_error_cb_t process_error,
73                           OSSL_CMP_SRV_certConf_cb_t process_certConf,
74                           OSSL_CMP_SRV_pollReq_cb_t process_pollReq)
75 {
76     if (srv_ctx == NULL) {
77         CMPerr(0, CMP_R_NULL_ARGUMENT);
78         return 0;
79     }
80     srv_ctx->custom_ctx = custom_ctx;
81     srv_ctx->process_cert_request = process_cert_request;
82     srv_ctx->process_rr = process_rr;
83     srv_ctx->process_genm = process_genm;
84     srv_ctx->process_error = process_error;
85     srv_ctx->process_certConf = process_certConf;
86     srv_ctx->process_pollReq = process_pollReq;
87     return 1;
88 }
89
90 OSSL_CMP_CTX *OSSL_CMP_SRV_CTX_get0_cmp_ctx(const OSSL_CMP_SRV_CTX *srv_ctx)
91 {
92     if (srv_ctx == NULL) {
93         CMPerr(0, CMP_R_NULL_ARGUMENT);
94         return NULL;
95     }
96     return srv_ctx->ctx;
97 }
98
99 void *OSSL_CMP_SRV_CTX_get0_custom_ctx(const OSSL_CMP_SRV_CTX *srv_ctx)
100 {
101     if (srv_ctx == NULL) {
102         CMPerr(0, CMP_R_NULL_ARGUMENT);
103         return NULL;
104     }
105     return srv_ctx->custom_ctx;
106 }
107
108 int OSSL_CMP_SRV_CTX_set_send_unprotected_errors(OSSL_CMP_SRV_CTX *srv_ctx,
109                                                  int val)
110 {
111     if (srv_ctx == NULL) {
112         CMPerr(0, CMP_R_NULL_ARGUMENT);
113         return 0;
114     }
115     srv_ctx->sendUnprotectedErrors = val != 0;
116     return 1;
117 }
118
119 int OSSL_CMP_SRV_CTX_set_accept_unprotected(OSSL_CMP_SRV_CTX *srv_ctx, int val)
120 {
121     if (srv_ctx == NULL) {
122         CMPerr(0, CMP_R_NULL_ARGUMENT);
123         return 0;
124     }
125     srv_ctx->acceptUnprotected = val != 0;
126     return 1;
127 }
128
129 int OSSL_CMP_SRV_CTX_set_accept_raverified(OSSL_CMP_SRV_CTX *srv_ctx, int val)
130 {
131     if (srv_ctx == NULL) {
132         CMPerr(0, CMP_R_NULL_ARGUMENT);
133         return 0;
134     }
135     srv_ctx->acceptRAVerified = val != 0;
136     return 1;
137 }
138
139 int OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(OSSL_CMP_SRV_CTX *srv_ctx,
140                                                 int val)
141 {
142     if (srv_ctx == NULL) {
143         CMPerr(0, CMP_R_NULL_ARGUMENT);
144         return 0;
145     }
146     srv_ctx->grantImplicitConfirm = val != 0;
147     return 1;
148 }
149
150 /*
151  * Processes an ir/cr/p10cr/kur and returns a certification response.
152  * Only handles the first certification request contained in req
153  * returns an ip/cp/kup on success and NULL on error
154  */
155 static OSSL_CMP_MSG *process_cert_request(OSSL_CMP_SRV_CTX *srv_ctx,
156                                           const OSSL_CMP_MSG *req)
157 {
158     OSSL_CMP_MSG *msg = NULL;
159     OSSL_CMP_PKISI *si = NULL;
160     X509 *certOut = NULL;
161     STACK_OF(X509) *chainOut = NULL, *caPubs = NULL;
162     const OSSL_CRMF_MSG *crm = NULL;
163     const X509_REQ *p10cr = NULL;
164     int bodytype;
165     int certReqId;
166
167     if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
168         return NULL;
169
170     switch (ossl_cmp_msg_get_bodytype(req)) {
171     case OSSL_CMP_PKIBODY_P10CR:
172     case OSSL_CMP_PKIBODY_CR:
173         bodytype = OSSL_CMP_PKIBODY_CP;
174         break;
175     case OSSL_CMP_PKIBODY_IR:
176         bodytype = OSSL_CMP_PKIBODY_IP;
177         break;
178     case OSSL_CMP_PKIBODY_KUR:
179         bodytype = OSSL_CMP_PKIBODY_KUP;
180         break;
181     default:
182         CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
183         return NULL;
184     }
185
186     if (ossl_cmp_msg_get_bodytype(req) == OSSL_CMP_PKIBODY_P10CR) {
187         certReqId = OSSL_CMP_CERTREQID;
188         p10cr = req->body->value.p10cr;
189     } else {
190         OSSL_CRMF_MSGS *reqs = req->body->value.ir; /* same for cr and kur */
191
192         if (sk_OSSL_CRMF_MSG_num(reqs) != 1) { /* TODO: handle case > 1 */
193             CMPerr(0, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED);
194             return NULL;
195         }
196
197         if ((crm = sk_OSSL_CRMF_MSG_value(reqs, OSSL_CMP_CERTREQID)) == NULL) {
198             CMPerr(0, CMP_R_CERTREQMSG_NOT_FOUND);
199             return NULL;
200         }
201         certReqId = OSSL_CRMF_MSG_get_certReqId(crm);
202     }
203
204     if (!ossl_cmp_verify_popo(req, srv_ctx->acceptRAVerified)) {
205         /* Proof of possession could not be verified */
206         si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection,
207                                      1 << OSSL_CMP_PKIFAILUREINFO_badPOP,
208                                      ERR_reason_error_string(ERR_peek_error()));
209         if (si == NULL)
210             return NULL;
211     } else {
212         OSSL_CMP_PKIHEADER *hdr = OSSL_CMP_MSG_get0_header(req);
213
214         si = srv_ctx->process_cert_request(srv_ctx, req, certReqId, crm, p10cr,
215                                            &certOut, &chainOut, &caPubs);
216         if (si == NULL)
217             goto err;
218         /* set OSSL_CMP_OPT_IMPLICITCONFIRM if and only if transaction ends */
219         if (!OSSL_CMP_CTX_set_option(srv_ctx->ctx, OSSL_CMP_OPT_IMPLICITCONFIRM,
220                                      ossl_cmp_hdr_has_implicitConfirm(hdr)
221                                          && srv_ctx->grantImplicitConfirm
222                                          /* do not set if polling starts: */
223                                          && certOut != NULL))
224             goto err;
225     }
226
227     msg = ossl_cmp_certRep_new(srv_ctx->ctx, bodytype, certReqId, si,
228                                certOut, chainOut, caPubs, 0 /* encrypted */,
229                                srv_ctx->sendUnprotectedErrors);
230     /*
231      * TODO when implemented in ossl_cmp_certrep_new():
232      * in case OSSL_CRMF_POPO_KEYENC, set encrypted
233      */
234     if (msg == NULL)
235         CMPerr(0, CMP_R_ERROR_CREATING_CERTREP);
236
237  err:
238     OSSL_CMP_PKISI_free(si);
239     X509_free(certOut);
240     sk_X509_pop_free(chainOut, X509_free);
241     sk_X509_pop_free(caPubs, X509_free);
242     return msg;
243 }
244
245 static OSSL_CMP_MSG *process_rr(OSSL_CMP_SRV_CTX *srv_ctx,
246                                 const OSSL_CMP_MSG *req)
247 {
248     OSSL_CMP_MSG *msg = NULL;
249     OSSL_CMP_REVDETAILS *details;
250     OSSL_CRMF_CERTID *certId;
251     OSSL_CRMF_CERTTEMPLATE *tmpl;
252     X509_NAME *issuer;
253     ASN1_INTEGER *serial;
254     OSSL_CMP_PKISI *si;
255
256     if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
257         return NULL;
258
259     if (sk_OSSL_CMP_REVDETAILS_num(req->body->value.rr) != 1) {
260         /* TODO: handle multiple elements if multiple requests have been sent */
261         CMPerr(0, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED);
262         return NULL;
263     }
264
265     if ((details = sk_OSSL_CMP_REVDETAILS_value(req->body->value.rr,
266                                                 OSSL_CMP_REVREQSID)) == NULL) {
267         CMPerr(0, CMP_R_ERROR_PROCESSING_MSG);
268         return NULL;
269     }
270
271     tmpl = details->certDetails;
272     issuer = OSSL_CRMF_CERTTEMPLATE_get0_issuer(tmpl);
273     serial = OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(tmpl);
274     /* here issuer and serial may safely be NULL */
275     if ((certId = OSSL_CRMF_CERTID_gen(issuer, serial)) == NULL)
276         return NULL;
277     if ((si = srv_ctx->process_rr(srv_ctx, req, issuer, serial)) == NULL)
278         goto err;
279
280     if ((msg = ossl_cmp_rp_new(srv_ctx->ctx, si, certId,
281                                srv_ctx->sendUnprotectedErrors)) == NULL)
282         CMPerr(0, CMP_R_ERROR_CREATING_RR);
283
284  err:
285     OSSL_CRMF_CERTID_free(certId);
286     OSSL_CMP_PKISI_free(si);
287     return msg;
288 }
289
290 /*
291  * Processes genm and creates a genp message mirroring the contents of the
292  * incoming message
293  */
294 static OSSL_CMP_MSG *process_genm(OSSL_CMP_SRV_CTX *srv_ctx,
295                                   const OSSL_CMP_MSG *req)
296 {
297     OSSL_CMP_GENMSGCONTENT *itavs;
298     OSSL_CMP_MSG *msg;
299
300     if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
301         return NULL;
302
303     if (!srv_ctx->process_genm(srv_ctx, req, req->body->value.genm, &itavs))
304         return NULL;
305
306     msg = ossl_cmp_genp_new(srv_ctx->ctx, itavs);
307     sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
308     return msg;
309 }
310
311 static OSSL_CMP_MSG *process_error(OSSL_CMP_SRV_CTX *srv_ctx,
312                                    const OSSL_CMP_MSG *req)
313 {
314     OSSL_CMP_ERRORMSGCONTENT *errorContent;
315     OSSL_CMP_MSG *msg;
316
317     if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
318         return NULL;
319     errorContent = req->body->value.error;
320     srv_ctx->process_error(srv_ctx, req, errorContent->pKIStatusInfo,
321                            errorContent->errorCode, errorContent->errorDetails);
322
323     if ((msg = ossl_cmp_pkiconf_new(srv_ctx->ctx)) == NULL)
324         CMPerr(0, CMP_R_ERROR_CREATING_PKICONF);
325     return msg;
326 }
327
328 static OSSL_CMP_MSG *process_certConf(OSSL_CMP_SRV_CTX *srv_ctx,
329                                       const OSSL_CMP_MSG *req)
330 {
331     OSSL_CMP_CTX *ctx;
332     OSSL_CMP_CERTCONFIRMCONTENT *ccc;
333     int num;
334     OSSL_CMP_MSG *msg = NULL;
335     OSSL_CMP_CERTSTATUS *status = NULL;
336
337     if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
338         return NULL;
339
340     ctx = srv_ctx->ctx;
341     ccc = req->body->value.certConf;
342     num = sk_OSSL_CMP_CERTSTATUS_num(ccc);
343
344     if (OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_IMPLICITCONFIRM) == 1) {
345         CMPerr(0, CMP_R_ERROR_UNEXPECTED_CERTCONF);
346         return NULL;
347     }
348
349     if (num == 0) {
350         ossl_cmp_err(ctx, "certificate rejected by client");
351     } else {
352         if (num > 1)
353             ossl_cmp_warn(ctx, "All CertStatus but the first will be ignored");
354         status = sk_OSSL_CMP_CERTSTATUS_value(ccc, OSSL_CMP_CERTREQID);
355     }
356
357     if (status != NULL) {
358         int certReqId = ossl_cmp_asn1_get_int(status->certReqId);
359         ASN1_OCTET_STRING *certHash = status->certHash;
360         OSSL_CMP_PKISI *si = status->statusInfo;
361
362         if (!srv_ctx->process_certConf(srv_ctx, req, certReqId, certHash, si))
363             return NULL; /* reason code may be: CMP_R_CERTHASH_UNMATCHED */
364
365         if (si != NULL && ossl_cmp_pkisi_get_status(si)
366             != OSSL_CMP_PKISTATUS_accepted) {
367             int pki_status = ossl_cmp_pkisi_get_status(si);
368             const char *str = ossl_cmp_PKIStatus_to_string(pki_status);
369
370             ossl_cmp_log2(INFO, ctx, "certificate rejected by client %s %s",
371                           str == NULL ? "without" : "with",
372                           str == NULL ? "PKIStatus" : str);
373         }
374     }
375
376     if ((msg = ossl_cmp_pkiconf_new(ctx)) == NULL)
377         CMPerr(0, CMP_R_ERROR_CREATING_PKICONF);
378     return msg;
379 }
380
381 static OSSL_CMP_MSG *process_pollReq(OSSL_CMP_SRV_CTX *srv_ctx,
382                                      const OSSL_CMP_MSG *req)
383 {
384     OSSL_CMP_POLLREQCONTENT *prc;
385     OSSL_CMP_POLLREQ *pr;
386     int certReqId;
387     OSSL_CMP_MSG *certReq;
388     int64_t check_after = 0;
389     OSSL_CMP_MSG *msg = NULL;
390
391     if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
392         return NULL;
393
394     prc = req->body->value.pollReq;
395     if (sk_OSSL_CMP_POLLREQ_num(prc) != 1) { /* TODO: handle case > 1 */
396         CMPerr(0, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED);
397         return NULL;
398     }
399
400     pr = sk_OSSL_CMP_POLLREQ_value(prc, 0);
401     certReqId = ossl_cmp_asn1_get_int(pr->certReqId);
402     if (!srv_ctx->process_pollReq(srv_ctx, req, certReqId,
403                                   &certReq, &check_after))
404         return NULL;
405
406     if (certReq != NULL) {
407         msg = process_cert_request(srv_ctx, certReq);
408         OSSL_CMP_MSG_free(certReq);
409     } else {
410         if ((msg = ossl_cmp_pollRep_new(srv_ctx->ctx, certReqId,
411                                         check_after)) == NULL)
412             CMPerr(0, CMP_R_ERROR_CREATING_POLLREP);
413     }
414     return msg;
415 }
416
417 /*
418  * Determines whether missing protection is allowed
419  */
420 static int unprotected_exception(const OSSL_CMP_CTX *ctx,
421                                  const OSSL_CMP_MSG *req,
422                                  int invalid_protection,
423                                  int accept_unprotected_requests)
424 {
425     if (accept_unprotected_requests) {
426         ossl_cmp_log1(WARN, ctx, "ignoring %s protection of request message",
427                       invalid_protection ? "invalid" : "missing");
428         return 1;
429     }
430     if (ossl_cmp_msg_get_bodytype(req) == OSSL_CMP_PKIBODY_ERROR
431         && OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS) == 1) {
432         ossl_cmp_warn(ctx, "ignoring missing protection of error message");
433         return 1;
434     }
435     return 0;
436 }
437
438 /*
439  * returns created message and NULL on internal error
440  */
441 OSSL_CMP_MSG *OSSL_CMP_SRV_process_request(OSSL_CMP_SRV_CTX *srv_ctx,
442                                            const OSSL_CMP_MSG *req)
443 {
444     OSSL_CMP_CTX *ctx;
445     OSSL_CMP_PKIHEADER *hdr;
446     int req_type, rsp_type;
447     OSSL_CMP_MSG *rsp = NULL;
448
449     if (srv_ctx == NULL || srv_ctx->ctx == NULL
450             || req == NULL || req->body == NULL
451             || (hdr = OSSL_CMP_MSG_get0_header(req)) == NULL) {
452         CMPerr(0, CMP_R_NULL_ARGUMENT);
453         return 0;
454     }
455     ctx = srv_ctx->ctx;
456
457     if (hdr->sender->type != GEN_DIRNAME) {
458         CMPerr(0, CMP_R_SENDER_GENERALNAME_TYPE_NOT_SUPPORTED);
459         goto err;
460     }
461     if (!OSSL_CMP_CTX_set1_recipient(ctx, hdr->sender->d.directoryName))
462         goto err;
463
464     req_type = ossl_cmp_msg_get_bodytype(req);
465     switch (req_type) {
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     case OSSL_CMP_PKIBODY_RR:
471     case OSSL_CMP_PKIBODY_GENM:
472     case OSSL_CMP_PKIBODY_ERROR:
473         if (ctx->transactionID != NULL) {
474             char *tid;
475
476             tid = OPENSSL_buf2hexstr(ctx->transactionID->data,
477                                      ctx->transactionID->length);
478             ossl_cmp_log1(WARN, ctx,
479                           "Assuming that last transaction with ID=%s got aborted",
480                           tid);
481             OPENSSL_free(tid);
482         }
483         /* start of a new transaction, set transactionID and senderNonce */
484         if (!OSSL_CMP_CTX_set1_transactionID(ctx, hdr->transactionID)
485                 || !ossl_cmp_ctx_set1_recipNonce(ctx, hdr->senderNonce))
486             goto err;
487         break;
488     default:
489         /* transactionID should be already initialized */
490         if (ctx->transactionID == NULL) {
491             CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
492             /* ignore any (extra) error in next two function calls: */
493             (void)OSSL_CMP_CTX_set1_transactionID(ctx, hdr->transactionID);
494             (void)ossl_cmp_ctx_set1_recipNonce(ctx, hdr->senderNonce);
495             goto err;
496         }
497     }
498
499     if (ossl_cmp_msg_check_received(ctx, req, unprotected_exception,
500                                     srv_ctx->acceptUnprotected) < 0)
501         goto err;
502
503     switch (req_type) {
504     case OSSL_CMP_PKIBODY_IR:
505     case OSSL_CMP_PKIBODY_CR:
506     case OSSL_CMP_PKIBODY_P10CR:
507     case OSSL_CMP_PKIBODY_KUR:
508         if (srv_ctx->process_cert_request == NULL)
509             CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
510         else
511             rsp = process_cert_request(srv_ctx, req);
512         break;
513     case OSSL_CMP_PKIBODY_RR:
514         if (srv_ctx->process_rr == NULL)
515             CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
516         else
517             rsp = process_rr(srv_ctx, req);
518         break;
519     case OSSL_CMP_PKIBODY_GENM:
520         if (srv_ctx->process_genm == NULL)
521             CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
522         else
523             rsp = process_genm(srv_ctx, req);
524         break;
525     case OSSL_CMP_PKIBODY_ERROR:
526         if (srv_ctx->process_error == NULL)
527             CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
528         else
529             rsp = process_error(srv_ctx, req);
530         break;
531     case OSSL_CMP_PKIBODY_CERTCONF:
532         if (srv_ctx->process_certConf == NULL)
533             CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
534         else
535             rsp = process_certConf(srv_ctx, req);
536         break;
537     case OSSL_CMP_PKIBODY_POLLREQ:
538         if (srv_ctx->process_pollReq == NULL)
539             CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
540         else
541             rsp = process_pollReq(srv_ctx, req);
542         break;
543     default:
544         /* TODO possibly support further request message types */
545         CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
546     }
547
548  err:
549     if (rsp == NULL) {
550         /* on error, try to respond with CMP error message to client */
551         const char *data = NULL;
552         int flags = 0;
553         unsigned long err = ERR_peek_error_data(&data, &flags);
554         int fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_badRequest;
555         /* TODO fail_info could be more specific */
556         OSSL_CMP_PKISI *si = NULL;
557
558         if ((si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection,
559                                           fail_info, NULL)) == NULL)
560             return 0;
561         if (err != 0 && (flags & ERR_TXT_STRING) != 0)
562             data = ERR_reason_error_string(err);
563         rsp = ossl_cmp_error_new(srv_ctx->ctx, si,
564                                  err != 0 ? ERR_GET_REASON(err) : -1,
565                                  data, srv_ctx->sendUnprotectedErrors);
566         OSSL_CMP_PKISI_free(si);
567     }
568
569     /* possibly close the transaction */
570     rsp_type =
571         rsp != NULL ? ossl_cmp_msg_get_bodytype(rsp) : OSSL_CMP_PKIBODY_ERROR;
572     switch (rsp_type) {
573     case OSSL_CMP_PKIBODY_IP:
574     case OSSL_CMP_PKIBODY_CP:
575     case OSSL_CMP_PKIBODY_KUP:
576     case OSSL_CMP_PKIBODY_RP:
577         if (OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_IMPLICITCONFIRM) == 0)
578             break;
579         /* fall through */
580
581     case OSSL_CMP_PKIBODY_PKICONF:
582     case OSSL_CMP_PKIBODY_GENP:
583     case OSSL_CMP_PKIBODY_ERROR:
584         /* TODO possibly support further terminating response message types */
585         (void)OSSL_CMP_CTX_set1_transactionID(ctx, NULL); /* ignore any error */
586
587     default: /* not closing transaction in other cases */
588         break;
589     }
590     return rsp;
591 }
592
593 /*
594  * Server interface that may substitute OSSL_CMP_MSG_http_perform at the client.
595  * The OSSL_CMP_SRV_CTX must be set as client_ctx->transfer_cb_arg.
596  * returns received message on success, else NULL and pushes an element on the
597  * error stack.
598  */
599 OSSL_CMP_MSG * OSSL_CMP_CTX_server_perform(OSSL_CMP_CTX *client_ctx,
600                                            const OSSL_CMP_MSG *req)
601 {
602     OSSL_CMP_SRV_CTX *srv_ctx = NULL;
603
604     if (client_ctx == NULL || req == NULL) {
605         CMPerr(0, CMP_R_NULL_ARGUMENT);
606         return 0;
607     }
608
609     if ((srv_ctx = OSSL_CMP_CTX_get_transfer_cb_arg(client_ctx)) == NULL) {
610         CMPerr(0, CMP_R_ERROR_TRANSFERRING_OUT);
611         return 0;
612     }
613
614     return OSSL_CMP_SRV_process_request(srv_ctx, req);
615 }