]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/cmp/cmp_server.c
Chunk 9 of CMP contribution to OpenSSL: CMP client and related tests
[thirdparty/openssl.git] / crypto / cmp / cmp_server.c
CommitLineData
62dcd2aa
DDO
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 */
23struct 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
42void 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
51OSSL_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
68int 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
90OSSL_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
99void *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
108int 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
119int 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
129int 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
139int 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 */
155static 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;
7e765f46
DDO
218 /* set OSSL_CMP_OPT_IMPLICIT_CONFIRM if and only if transaction ends */
219 if (!OSSL_CMP_CTX_set_option(srv_ctx->ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM,
62dcd2aa
DDO
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
245static 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;
8cc86b81 252 const X509_NAME *issuer;
62dcd2aa
DDO
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) {
7e765f46 267 CMPerr(0, CMP_R_ERROR_PROCESSING_MESSAGE);
62dcd2aa
DDO
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 */
294static 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
311static 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
328static 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
7e765f46 344 if (OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM) == 1) {
62dcd2aa
DDO
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
381static 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/*
7e765f46
DDO
418 * Determine whether missing/invalid protection of request message is allowed.
419 * Return 1 on acceptance, 0 on rejection, or -1 on (internal) error.
62dcd2aa
DDO
420 */
421static int unprotected_exception(const OSSL_CMP_CTX *ctx,
422 const OSSL_CMP_MSG *req,
423 int invalid_protection,
424 int accept_unprotected_requests)
425{
7e765f46
DDO
426 if (!ossl_assert(ctx != NULL && req != NULL))
427 return -1;
428
62dcd2aa
DDO
429 if (accept_unprotected_requests) {
430 ossl_cmp_log1(WARN, ctx, "ignoring %s protection of request message",
431 invalid_protection ? "invalid" : "missing");
432 return 1;
433 }
434 if (ossl_cmp_msg_get_bodytype(req) == OSSL_CMP_PKIBODY_ERROR
435 && OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS) == 1) {
436 ossl_cmp_warn(ctx, "ignoring missing protection of error message");
437 return 1;
438 }
439 return 0;
440}
441
442/*
443 * returns created message and NULL on internal error
444 */
445OSSL_CMP_MSG *OSSL_CMP_SRV_process_request(OSSL_CMP_SRV_CTX *srv_ctx,
446 const OSSL_CMP_MSG *req)
447{
448 OSSL_CMP_CTX *ctx;
449 OSSL_CMP_PKIHEADER *hdr;
450 int req_type, rsp_type;
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 CMPerr(0, CMP_R_NULL_ARGUMENT);
457 return 0;
458 }
459 ctx = srv_ctx->ctx;
460
461 if (hdr->sender->type != GEN_DIRNAME) {
462 CMPerr(0, CMP_R_SENDER_GENERALNAME_TYPE_NOT_SUPPORTED);
463 goto err;
464 }
465 if (!OSSL_CMP_CTX_set1_recipient(ctx, hdr->sender->d.directoryName))
466 goto err;
467
468 req_type = ossl_cmp_msg_get_bodytype(req);
469 switch (req_type) {
470 case OSSL_CMP_PKIBODY_IR:
471 case OSSL_CMP_PKIBODY_CR:
472 case OSSL_CMP_PKIBODY_P10CR:
473 case OSSL_CMP_PKIBODY_KUR:
474 case OSSL_CMP_PKIBODY_RR:
475 case OSSL_CMP_PKIBODY_GENM:
476 case OSSL_CMP_PKIBODY_ERROR:
477 if (ctx->transactionID != NULL) {
478 char *tid;
479
480 tid = OPENSSL_buf2hexstr(ctx->transactionID->data,
481 ctx->transactionID->length);
482 ossl_cmp_log1(WARN, ctx,
483 "Assuming that last transaction with ID=%s got aborted",
484 tid);
485 OPENSSL_free(tid);
486 }
487 /* start of a new transaction, set transactionID and senderNonce */
488 if (!OSSL_CMP_CTX_set1_transactionID(ctx, hdr->transactionID)
489 || !ossl_cmp_ctx_set1_recipNonce(ctx, hdr->senderNonce))
490 goto err;
491 break;
492 default:
493 /* transactionID should be already initialized */
494 if (ctx->transactionID == NULL) {
495 CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
496 /* ignore any (extra) error in next two function calls: */
497 (void)OSSL_CMP_CTX_set1_transactionID(ctx, hdr->transactionID);
498 (void)ossl_cmp_ctx_set1_recipNonce(ctx, hdr->senderNonce);
499 goto err;
500 }
501 }
502
503 if (ossl_cmp_msg_check_received(ctx, req, unprotected_exception,
504 srv_ctx->acceptUnprotected) < 0)
505 goto err;
506
507 switch (req_type) {
508 case OSSL_CMP_PKIBODY_IR:
509 case OSSL_CMP_PKIBODY_CR:
510 case OSSL_CMP_PKIBODY_P10CR:
511 case OSSL_CMP_PKIBODY_KUR:
512 if (srv_ctx->process_cert_request == NULL)
513 CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
514 else
515 rsp = process_cert_request(srv_ctx, req);
516 break;
517 case OSSL_CMP_PKIBODY_RR:
518 if (srv_ctx->process_rr == NULL)
519 CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
520 else
521 rsp = process_rr(srv_ctx, req);
522 break;
523 case OSSL_CMP_PKIBODY_GENM:
524 if (srv_ctx->process_genm == NULL)
525 CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
526 else
527 rsp = process_genm(srv_ctx, req);
528 break;
529 case OSSL_CMP_PKIBODY_ERROR:
530 if (srv_ctx->process_error == NULL)
531 CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
532 else
533 rsp = process_error(srv_ctx, req);
534 break;
535 case OSSL_CMP_PKIBODY_CERTCONF:
536 if (srv_ctx->process_certConf == NULL)
537 CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
538 else
539 rsp = process_certConf(srv_ctx, req);
540 break;
541 case OSSL_CMP_PKIBODY_POLLREQ:
542 if (srv_ctx->process_pollReq == NULL)
543 CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
544 else
545 rsp = process_pollReq(srv_ctx, req);
546 break;
547 default:
548 /* TODO possibly support further request message types */
549 CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
550 }
551
552 err:
553 if (rsp == NULL) {
554 /* on error, try to respond with CMP error message to client */
555 const char *data = NULL;
556 int flags = 0;
557 unsigned long err = ERR_peek_error_data(&data, &flags);
558 int fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_badRequest;
559 /* TODO fail_info could be more specific */
560 OSSL_CMP_PKISI *si = NULL;
561
562 if ((si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection,
563 fail_info, NULL)) == NULL)
564 return 0;
565 if (err != 0 && (flags & ERR_TXT_STRING) != 0)
566 data = ERR_reason_error_string(err);
567 rsp = ossl_cmp_error_new(srv_ctx->ctx, si,
568 err != 0 ? ERR_GET_REASON(err) : -1,
569 data, srv_ctx->sendUnprotectedErrors);
570 OSSL_CMP_PKISI_free(si);
571 }
572
573 /* possibly close the transaction */
574 rsp_type =
575 rsp != NULL ? ossl_cmp_msg_get_bodytype(rsp) : OSSL_CMP_PKIBODY_ERROR;
576 switch (rsp_type) {
577 case OSSL_CMP_PKIBODY_IP:
578 case OSSL_CMP_PKIBODY_CP:
579 case OSSL_CMP_PKIBODY_KUP:
580 case OSSL_CMP_PKIBODY_RP:
7e765f46 581 if (OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM) == 0)
62dcd2aa
DDO
582 break;
583 /* fall through */
584
585 case OSSL_CMP_PKIBODY_PKICONF:
586 case OSSL_CMP_PKIBODY_GENP:
587 case OSSL_CMP_PKIBODY_ERROR:
588 /* TODO possibly support further terminating response message types */
589 (void)OSSL_CMP_CTX_set1_transactionID(ctx, NULL); /* ignore any error */
590
591 default: /* not closing transaction in other cases */
592 break;
593 }
594 return rsp;
595}
596
597/*
598 * Server interface that may substitute OSSL_CMP_MSG_http_perform at the client.
599 * The OSSL_CMP_SRV_CTX must be set as client_ctx->transfer_cb_arg.
600 * returns received message on success, else NULL and pushes an element on the
601 * error stack.
602 */
603OSSL_CMP_MSG * OSSL_CMP_CTX_server_perform(OSSL_CMP_CTX *client_ctx,
604 const OSSL_CMP_MSG *req)
605{
606 OSSL_CMP_SRV_CTX *srv_ctx = NULL;
607
608 if (client_ctx == NULL || req == NULL) {
609 CMPerr(0, CMP_R_NULL_ARGUMENT);
610 return 0;
611 }
612
613 if ((srv_ctx = OSSL_CMP_CTX_get_transfer_cb_arg(client_ctx)) == NULL) {
7e765f46 614 CMPerr(0, CMP_R_TRANSFER_ERROR);
62dcd2aa
DDO
615 return 0;
616 }
617
618 return OSSL_CMP_SRV_process_request(srv_ctx, req);
619}