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