05e8cb19552aa873bd5447dbfbbad33ca202bb1b
[openssl.git] / crypto / cmp / cmp_server.c
1 /*
2  * Copyright 2007-2021 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(OSSL_LIB_CTX *libctx, const char *propq)
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(libctx, propq)) == 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         ERR_raise(ERR_LIB_CMP, 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         ERR_raise(ERR_LIB_CMP, 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         ERR_raise(ERR_LIB_CMP, 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         ERR_raise(ERR_LIB_CMP, 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         ERR_raise(ERR_LIB_CMP, 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         ERR_raise(ERR_LIB_CMP, 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         ERR_raise(ERR_LIB_CMP, 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         ERR_raise(ERR_LIB_CMP, 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) {
193             ERR_raise(ERR_LIB_CMP, 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             ERR_raise(ERR_LIB_CMP, 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(srv_ctx->ctx, 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_IMPLICIT_CONFIRM if and only if transaction ends */
219         if (!OSSL_CMP_CTX_set_option(srv_ctx->ctx,
220                                      OSSL_CMP_OPT_IMPLICIT_CONFIRM,
221                                      ossl_cmp_hdr_has_implicitConfirm(hdr)
222                                          && srv_ctx->grantImplicitConfirm
223                                          /* do not set if polling starts: */
224                                          && certOut != NULL))
225             goto err;
226     }
227
228     msg = ossl_cmp_certrep_new(srv_ctx->ctx, bodytype, certReqId, si,
229                                certOut, NULL /* enc */, chainOut, caPubs,
230                                srv_ctx->sendUnprotectedErrors);
231     /* When supporting OSSL_CRMF_POPO_KEYENC, "enc" will need to be set */
232     if (msg == NULL)
233         ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_CERTREP);
234
235  err:
236     OSSL_CMP_PKISI_free(si);
237     X509_free(certOut);
238     OSSL_STACK_OF_X509_free(chainOut);
239     OSSL_STACK_OF_X509_free(caPubs);
240     return msg;
241 }
242
243 static OSSL_CMP_MSG *process_rr(OSSL_CMP_SRV_CTX *srv_ctx,
244                                 const OSSL_CMP_MSG *req)
245 {
246     OSSL_CMP_MSG *msg = NULL;
247     OSSL_CMP_REVDETAILS *details;
248     OSSL_CRMF_CERTID *certId = NULL;
249     OSSL_CRMF_CERTTEMPLATE *tmpl;
250     const X509_NAME *issuer;
251     const ASN1_INTEGER *serial;
252     OSSL_CMP_PKISI *si;
253
254     if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
255         return NULL;
256
257     if (sk_OSSL_CMP_REVDETAILS_num(req->body->value.rr) != 1) {
258         ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED);
259         return NULL;
260     }
261
262     if ((details = sk_OSSL_CMP_REVDETAILS_value(req->body->value.rr,
263                                                 OSSL_CMP_REVREQSID)) == NULL) {
264         ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
265         return NULL;
266     }
267
268     tmpl = details->certDetails;
269     issuer = OSSL_CRMF_CERTTEMPLATE_get0_issuer(tmpl);
270     serial = OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(tmpl);
271     if (issuer != NULL && serial != NULL
272             && (certId = OSSL_CRMF_CERTID_gen(issuer, serial)) == NULL)
273         return NULL;
274     if ((si = srv_ctx->process_rr(srv_ctx, req, issuer, serial)) == NULL)
275         goto err;
276
277     if ((msg = ossl_cmp_rp_new(srv_ctx->ctx, si, certId,
278                                srv_ctx->sendUnprotectedErrors)) == NULL)
279         ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_RR);
280
281  err:
282     OSSL_CRMF_CERTID_free(certId);
283     OSSL_CMP_PKISI_free(si);
284     return msg;
285 }
286
287 /*
288  * Processes genm and creates a genp message mirroring the contents of the
289  * incoming message
290  */
291 static OSSL_CMP_MSG *process_genm(OSSL_CMP_SRV_CTX *srv_ctx,
292                                   const OSSL_CMP_MSG *req)
293 {
294     OSSL_CMP_GENMSGCONTENT *itavs;
295     OSSL_CMP_MSG *msg;
296
297     if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
298         return NULL;
299
300     if (!srv_ctx->process_genm(srv_ctx, req, req->body->value.genm, &itavs))
301         return NULL;
302
303     msg = ossl_cmp_genp_new(srv_ctx->ctx, itavs);
304     sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
305     return msg;
306 }
307
308 static OSSL_CMP_MSG *process_error(OSSL_CMP_SRV_CTX *srv_ctx,
309                                    const OSSL_CMP_MSG *req)
310 {
311     OSSL_CMP_ERRORMSGCONTENT *errorContent;
312     OSSL_CMP_MSG *msg;
313
314     if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
315         return NULL;
316     errorContent = req->body->value.error;
317     srv_ctx->process_error(srv_ctx, req, errorContent->pKIStatusInfo,
318                            errorContent->errorCode, errorContent->errorDetails);
319
320     if ((msg = ossl_cmp_pkiconf_new(srv_ctx->ctx)) == NULL)
321         ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF);
322     return msg;
323 }
324
325 static OSSL_CMP_MSG *process_certConf(OSSL_CMP_SRV_CTX *srv_ctx,
326                                       const OSSL_CMP_MSG *req)
327 {
328     OSSL_CMP_CTX *ctx;
329     OSSL_CMP_CERTCONFIRMCONTENT *ccc;
330     int num;
331     OSSL_CMP_MSG *msg = NULL;
332     OSSL_CMP_CERTSTATUS *status = NULL;
333
334     if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
335         return NULL;
336
337     ctx = srv_ctx->ctx;
338     ccc = req->body->value.certConf;
339     num = sk_OSSL_CMP_CERTSTATUS_num(ccc);
340
341     if (OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM) == 1
342             || ctx->status != OSSL_CMP_PKISTATUS_trans) {
343         ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_UNEXPECTED_CERTCONF);
344         return NULL;
345     }
346
347     if (num == 0) {
348         ossl_cmp_err(ctx, "certificate rejected by client");
349     } else {
350         if (num > 1)
351             ossl_cmp_warn(ctx, "All CertStatus but the first will be ignored");
352         status = sk_OSSL_CMP_CERTSTATUS_value(ccc, OSSL_CMP_CERTREQID);
353     }
354
355     if (status != NULL) {
356         int certReqId = ossl_cmp_asn1_get_int(status->certReqId);
357         ASN1_OCTET_STRING *certHash = status->certHash;
358         OSSL_CMP_PKISI *si = status->statusInfo;
359
360         if (!srv_ctx->process_certConf(srv_ctx, req, certReqId, certHash, si))
361             return NULL; /* reason code may be: CMP_R_CERTHASH_UNMATCHED */
362
363         if (si != NULL
364             && ossl_cmp_pkisi_get_status(si) != OSSL_CMP_PKISTATUS_accepted) {
365             int pki_status = ossl_cmp_pkisi_get_status(si);
366             const char *str = ossl_cmp_PKIStatus_to_string(pki_status);
367
368             ossl_cmp_log2(INFO, ctx, "certificate rejected by client %s %s",
369                           str == NULL ? "without" : "with",
370                           str == NULL ? "PKIStatus" : str);
371         }
372     }
373
374     if ((msg = ossl_cmp_pkiconf_new(ctx)) == NULL)
375         ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_PKICONF);
376     return msg;
377 }
378
379 static OSSL_CMP_MSG *process_pollReq(OSSL_CMP_SRV_CTX *srv_ctx,
380                                      const OSSL_CMP_MSG *req)
381 {
382     OSSL_CMP_POLLREQCONTENT *prc;
383     OSSL_CMP_POLLREQ *pr;
384     int certReqId;
385     OSSL_CMP_MSG *certReq;
386     int64_t check_after = 0;
387     OSSL_CMP_MSG *msg = NULL;
388
389     if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
390         return NULL;
391
392     prc = req->body->value.pollReq;
393     if (sk_OSSL_CMP_POLLREQ_num(prc) != 1) {
394         ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED);
395         return NULL;
396     }
397
398     pr = sk_OSSL_CMP_POLLREQ_value(prc, 0);
399     certReqId = ossl_cmp_asn1_get_int(pr->certReqId);
400     if (!srv_ctx->process_pollReq(srv_ctx, req, certReqId,
401                                   &certReq, &check_after))
402         return NULL;
403
404     if (certReq != NULL) {
405         msg = process_cert_request(srv_ctx, certReq);
406         OSSL_CMP_MSG_free(certReq);
407     } else {
408         if ((msg = ossl_cmp_pollRep_new(srv_ctx->ctx, certReqId,
409                                         check_after)) == NULL)
410             ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CREATING_POLLREP);
411     }
412     return msg;
413 }
414
415 /*
416  * Determine whether missing/invalid protection of request message is allowed.
417  * Return 1 on acceptance, 0 on rejection, or -1 on (internal) error.
418  */
419 static int unprotected_exception(const OSSL_CMP_CTX *ctx,
420                                  const OSSL_CMP_MSG *req,
421                                  int invalid_protection,
422                                  int accept_unprotected_requests)
423 {
424     if (!ossl_assert(ctx != NULL && req != NULL))
425         return -1;
426
427     if (accept_unprotected_requests) {
428         ossl_cmp_log1(WARN, ctx, "ignoring %s protection of request message",
429                       invalid_protection ? "invalid" : "missing");
430         return 1;
431     }
432     if (OSSL_CMP_MSG_get_bodytype(req) == OSSL_CMP_PKIBODY_ERROR
433         && OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS) == 1) {
434         ossl_cmp_warn(ctx, "ignoring missing protection of error message");
435         return 1;
436     }
437     return 0;
438 }
439
440 /*
441  * returns created message and NULL on internal error
442  */
443 OSSL_CMP_MSG *OSSL_CMP_SRV_process_request(OSSL_CMP_SRV_CTX *srv_ctx,
444                                            const OSSL_CMP_MSG *req)
445 {
446     OSSL_CMP_CTX *ctx;
447     ASN1_OCTET_STRING *backup_secret;
448     OSSL_CMP_PKIHEADER *hdr;
449     int req_type, rsp_type;
450     int req_verified = 0;
451     OSSL_CMP_MSG *rsp = NULL;
452
453     if (srv_ctx == NULL || srv_ctx->ctx == NULL
454             || req == NULL || req->body == NULL
455             || (hdr = OSSL_CMP_MSG_get0_header(req)) == NULL) {
456         ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
457         return 0;
458     }
459     ctx = srv_ctx->ctx;
460     backup_secret = ctx->secretValue;
461     req_type = OSSL_CMP_MSG_get_bodytype(req);
462     ossl_cmp_log1(DEBUG, ctx,
463                   "received %s", ossl_cmp_bodytype_to_string(req_type));
464
465     /*
466      * Some things need to be done already before validating the message in
467      * order to be able to send an error message as far as needed and possible.
468      */
469     if (hdr->sender->type != GEN_DIRNAME) {
470         ERR_raise(ERR_LIB_CMP, CMP_R_SENDER_GENERALNAME_TYPE_NOT_SUPPORTED);
471         goto err;
472     }
473     if (!OSSL_CMP_CTX_set1_recipient(ctx, hdr->sender->d.directoryName))
474         goto err;
475
476     switch (req_type) {
477     case OSSL_CMP_PKIBODY_IR:
478     case OSSL_CMP_PKIBODY_CR:
479     case OSSL_CMP_PKIBODY_P10CR:
480     case OSSL_CMP_PKIBODY_KUR:
481     case OSSL_CMP_PKIBODY_RR:
482     case OSSL_CMP_PKIBODY_GENM:
483     case OSSL_CMP_PKIBODY_ERROR:
484         if (ctx->transactionID != NULL) {
485             char *tid = i2s_ASN1_OCTET_STRING(NULL, ctx->transactionID);
486
487             if (tid != NULL)
488                 ossl_cmp_log1(WARN, ctx,
489                               "Assuming that last transaction with ID=%s got aborted",
490                               tid);
491             OPENSSL_free(tid);
492         }
493         /* start of a new transaction, reset transactionID and senderNonce */
494         if (!OSSL_CMP_CTX_set1_transactionID(ctx, NULL)
495                 || !OSSL_CMP_CTX_set1_senderNonce(ctx, NULL))
496             goto err;
497         break;
498     default:
499         /* transactionID should be already initialized */
500         if (ctx->transactionID == NULL) {
501 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
502             ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
503             goto err;
504 #endif
505         }
506     }
507
508     req_verified = ossl_cmp_msg_check_update(ctx, req, unprotected_exception,
509                                              srv_ctx->acceptUnprotected);
510     if (ctx->secretValue != NULL && ctx->pkey != NULL
511             && ossl_cmp_hdr_get_protection_nid(hdr) != NID_id_PasswordBasedMAC)
512         ctx->secretValue = NULL; /* use MSG_SIG_ALG when protecting rsp */
513     if (!req_verified)
514         goto err;
515
516     switch (req_type) {
517     case OSSL_CMP_PKIBODY_IR:
518     case OSSL_CMP_PKIBODY_CR:
519     case OSSL_CMP_PKIBODY_P10CR:
520     case OSSL_CMP_PKIBODY_KUR:
521         if (srv_ctx->process_cert_request == NULL)
522             ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
523         else
524             rsp = process_cert_request(srv_ctx, req);
525         break;
526     case OSSL_CMP_PKIBODY_RR:
527         if (srv_ctx->process_rr == NULL)
528             ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
529         else
530             rsp = process_rr(srv_ctx, req);
531         break;
532     case OSSL_CMP_PKIBODY_GENM:
533         if (srv_ctx->process_genm == NULL)
534             ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
535         else
536             rsp = process_genm(srv_ctx, req);
537         break;
538     case OSSL_CMP_PKIBODY_ERROR:
539         if (srv_ctx->process_error == NULL)
540             ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
541         else
542             rsp = process_error(srv_ctx, req);
543         break;
544     case OSSL_CMP_PKIBODY_CERTCONF:
545         if (srv_ctx->process_certConf == NULL)
546             ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
547         else
548             rsp = process_certConf(srv_ctx, req);
549         break;
550     case OSSL_CMP_PKIBODY_POLLREQ:
551         if (srv_ctx->process_pollReq == NULL)
552             ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
553         else
554             rsp = process_pollReq(srv_ctx, req);
555         break;
556     default:
557         /* Other request message types are not supported */
558         ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
559         break;
560     }
561
562  err:
563     if (rsp == NULL) {
564         /* on error, try to respond with CMP error message to client */
565         const char *data = NULL, *reason = NULL;
566         int flags = 0;
567         unsigned long err = ERR_peek_error_data(&data, &flags);
568         int fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_badRequest;
569         /* fail_info is not very specific */
570         OSSL_CMP_PKISI *si = NULL;
571
572         if (!req_verified) {
573             /*
574              * Above ossl_cmp_msg_check_update() was not successfully executed,
575              * which normally would set ctx->transactionID and ctx->recipNonce.
576              * So anyway try to provide the right transactionID and recipNonce,
577              * while ignoring any (extra) error in next two function calls.
578              */
579             if (ctx->transactionID == NULL)
580                 (void)OSSL_CMP_CTX_set1_transactionID(ctx, hdr->transactionID);
581             (void)ossl_cmp_ctx_set1_recipNonce(ctx, hdr->senderNonce);
582         }
583
584         if ((flags & ERR_TXT_STRING) == 0 || *data == '\0')
585             data = NULL;
586         reason = ERR_reason_error_string(err);
587         if ((si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection,
588                                           fail_info, reason)) != NULL) {
589             rsp = ossl_cmp_error_new(srv_ctx->ctx, si, err,
590                                      data, srv_ctx->sendUnprotectedErrors);
591             OSSL_CMP_PKISI_free(si);
592         }
593     }
594     OSSL_CMP_CTX_print_errors(ctx);
595     ctx->secretValue = backup_secret;
596
597     rsp_type =
598         rsp != NULL ? OSSL_CMP_MSG_get_bodytype(rsp) : OSSL_CMP_PKIBODY_ERROR;
599     if (rsp != NULL)
600         ossl_cmp_log1(DEBUG, ctx,
601                       "sending %s", ossl_cmp_bodytype_to_string(rsp_type));
602     else
603         ossl_cmp_log(ERR, ctx, "cannot send proper CMP response");
604
605     /* determine whether to keep the transaction open or not */
606     ctx->status = OSSL_CMP_PKISTATUS_trans;
607     switch (rsp_type) {
608     case OSSL_CMP_PKIBODY_IP:
609     case OSSL_CMP_PKIBODY_CP:
610     case OSSL_CMP_PKIBODY_KUP:
611         if (OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM) == 0)
612             break;
613         /* fall through */
614
615     case OSSL_CMP_PKIBODY_RP:
616     case OSSL_CMP_PKIBODY_PKICONF:
617     case OSSL_CMP_PKIBODY_GENP:
618     case OSSL_CMP_PKIBODY_ERROR:
619         /* Other terminating response message types are not supported */
620         /* Prepare for next transaction, ignoring any errors here: */
621         (void)OSSL_CMP_CTX_set1_transactionID(ctx, NULL);
622         (void)OSSL_CMP_CTX_set1_senderNonce(ctx, NULL);
623         ctx->status = OSSL_CMP_PKISTATUS_unspecified; /* transaction closed */
624
625     default: /* not closing transaction in other cases */
626         break;
627     }
628     return rsp;
629 }
630
631 /*
632  * Server interface that may substitute OSSL_CMP_MSG_http_perform at the client.
633  * The OSSL_CMP_SRV_CTX must be set as client_ctx->transfer_cb_arg.
634  * returns received message on success, else NULL and pushes an element on the
635  * error stack.
636  */
637 OSSL_CMP_MSG *OSSL_CMP_CTX_server_perform(OSSL_CMP_CTX *client_ctx,
638                                           const OSSL_CMP_MSG *req)
639 {
640     OSSL_CMP_SRV_CTX *srv_ctx = NULL;
641
642     if (client_ctx == NULL || req == NULL) {
643         ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
644         return NULL;
645     }
646
647     if ((srv_ctx = OSSL_CMP_CTX_get_transfer_cb_arg(client_ctx)) == NULL) {
648         ERR_raise(ERR_LIB_CMP, CMP_R_TRANSFER_ERROR);
649         return NULL;
650     }
651
652     return OSSL_CMP_SRV_process_request(srv_ctx, req);
653 }