2 * Copyright 2007-2019 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright Nokia 2007-2019
4 * Copyright Siemens AG 2015-2019
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
12 /* general CMP server functions */
14 #include <openssl/asn1t.h>
16 #include "cmp_local.h"
18 /* explicit #includes not strictly needed since implied by the above: */
19 #include <openssl/cmp.h>
20 #include <openssl/err.h>
22 /* the context for the generic CMP server */
23 struct ossl_cmp_srv_ctx_st
25 OSSL_CMP_CTX *ctx; /* Client CMP context, partly reused for srv */
26 void *custom_ctx; /* pointer to specific server context */
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;
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 */
40 }; /* OSSL_CMP_SRV_CTX */
42 void OSSL_CMP_SRV_CTX_free(OSSL_CMP_SRV_CTX *srv_ctx)
47 OSSL_CMP_CTX_free(srv_ctx->ctx);
48 OPENSSL_free(srv_ctx);
51 OSSL_CMP_SRV_CTX *OSSL_CMP_SRV_CTX_new(void)
53 OSSL_CMP_SRV_CTX *ctx = OPENSSL_zalloc(sizeof(OSSL_CMP_SRV_CTX));
58 if ((ctx->ctx = OSSL_CMP_CTX_new()) == NULL)
61 /* all other elements are initialized to 0 or NULL, respectively */
64 OSSL_CMP_SRV_CTX_free(ctx);
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)
76 if (srv_ctx == NULL) {
77 CMPerr(0, CMP_R_NULL_ARGUMENT);
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;
90 OSSL_CMP_CTX *OSSL_CMP_SRV_CTX_get0_cmp_ctx(const OSSL_CMP_SRV_CTX *srv_ctx)
92 if (srv_ctx == NULL) {
93 CMPerr(0, CMP_R_NULL_ARGUMENT);
99 void *OSSL_CMP_SRV_CTX_get0_custom_ctx(const OSSL_CMP_SRV_CTX *srv_ctx)
101 if (srv_ctx == NULL) {
102 CMPerr(0, CMP_R_NULL_ARGUMENT);
105 return srv_ctx->custom_ctx;
108 int OSSL_CMP_SRV_CTX_set_send_unprotected_errors(OSSL_CMP_SRV_CTX *srv_ctx,
111 if (srv_ctx == NULL) {
112 CMPerr(0, CMP_R_NULL_ARGUMENT);
115 srv_ctx->sendUnprotectedErrors = val != 0;
119 int OSSL_CMP_SRV_CTX_set_accept_unprotected(OSSL_CMP_SRV_CTX *srv_ctx, int val)
121 if (srv_ctx == NULL) {
122 CMPerr(0, CMP_R_NULL_ARGUMENT);
125 srv_ctx->acceptUnprotected = val != 0;
129 int OSSL_CMP_SRV_CTX_set_accept_raverified(OSSL_CMP_SRV_CTX *srv_ctx, int val)
131 if (srv_ctx == NULL) {
132 CMPerr(0, CMP_R_NULL_ARGUMENT);
135 srv_ctx->acceptRAVerified = val != 0;
139 int OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(OSSL_CMP_SRV_CTX *srv_ctx,
142 if (srv_ctx == NULL) {
143 CMPerr(0, CMP_R_NULL_ARGUMENT);
146 srv_ctx->grantImplicitConfirm = val != 0;
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
155 static OSSL_CMP_MSG *process_cert_request(OSSL_CMP_SRV_CTX *srv_ctx,
156 const OSSL_CMP_MSG *req)
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;
167 if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
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;
175 case OSSL_CMP_PKIBODY_IR:
176 bodytype = OSSL_CMP_PKIBODY_IP;
178 case OSSL_CMP_PKIBODY_KUR:
179 bodytype = OSSL_CMP_PKIBODY_KUP;
182 CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
186 if (ossl_cmp_msg_get_bodytype(req) == OSSL_CMP_PKIBODY_P10CR) {
187 certReqId = OSSL_CMP_CERTREQID;
188 p10cr = req->body->value.p10cr;
190 OSSL_CRMF_MSGS *reqs = req->body->value.ir; /* same for cr and kur */
192 if (sk_OSSL_CRMF_MSG_num(reqs) != 1) { /* TODO: handle case > 1 */
193 CMPerr(0, CMP_R_MULTIPLE_REQUESTS_NOT_SUPPORTED);
197 if ((crm = sk_OSSL_CRMF_MSG_value(reqs, OSSL_CMP_CERTREQID)) == NULL) {
198 CMPerr(0, CMP_R_CERTREQMSG_NOT_FOUND);
201 certReqId = OSSL_CRMF_MSG_get_certReqId(crm);
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()));
212 OSSL_CMP_PKIHEADER *hdr = OSSL_CMP_MSG_get0_header(req);
214 si = srv_ctx->process_cert_request(srv_ctx, req, certReqId, crm, p10cr,
215 &certOut, &chainOut, &caPubs);
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,
220 ossl_cmp_hdr_has_implicitConfirm(hdr)
221 && srv_ctx->grantImplicitConfirm
222 /* do not set if polling starts: */
227 msg = ossl_cmp_certRep_new(srv_ctx->ctx, bodytype, certReqId, si,
228 certOut, chainOut, caPubs, 0 /* encrypted */,
229 srv_ctx->sendUnprotectedErrors);
231 * TODO when implemented in ossl_cmp_certrep_new():
232 * in case OSSL_CRMF_POPO_KEYENC, set encrypted
235 CMPerr(0, CMP_R_ERROR_CREATING_CERTREP);
238 OSSL_CMP_PKISI_free(si);
240 sk_X509_pop_free(chainOut, X509_free);
241 sk_X509_pop_free(caPubs, X509_free);
245 static OSSL_CMP_MSG *process_rr(OSSL_CMP_SRV_CTX *srv_ctx,
246 const OSSL_CMP_MSG *req)
248 OSSL_CMP_MSG *msg = NULL;
249 OSSL_CMP_REVDETAILS *details;
250 OSSL_CRMF_CERTID *certId;
251 OSSL_CRMF_CERTTEMPLATE *tmpl;
252 const X509_NAME *issuer;
253 ASN1_INTEGER *serial;
256 if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
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);
265 if ((details = sk_OSSL_CMP_REVDETAILS_value(req->body->value.rr,
266 OSSL_CMP_REVREQSID)) == NULL) {
267 CMPerr(0, CMP_R_ERROR_PROCESSING_MESSAGE);
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)
277 if ((si = srv_ctx->process_rr(srv_ctx, req, issuer, serial)) == NULL)
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);
285 OSSL_CRMF_CERTID_free(certId);
286 OSSL_CMP_PKISI_free(si);
291 * Processes genm and creates a genp message mirroring the contents of the
294 static OSSL_CMP_MSG *process_genm(OSSL_CMP_SRV_CTX *srv_ctx,
295 const OSSL_CMP_MSG *req)
297 OSSL_CMP_GENMSGCONTENT *itavs;
300 if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
303 if (!srv_ctx->process_genm(srv_ctx, req, req->body->value.genm, &itavs))
306 msg = ossl_cmp_genp_new(srv_ctx->ctx, itavs);
307 sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
311 static OSSL_CMP_MSG *process_error(OSSL_CMP_SRV_CTX *srv_ctx,
312 const OSSL_CMP_MSG *req)
314 OSSL_CMP_ERRORMSGCONTENT *errorContent;
317 if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
319 errorContent = req->body->value.error;
320 srv_ctx->process_error(srv_ctx, req, errorContent->pKIStatusInfo,
321 errorContent->errorCode, errorContent->errorDetails);
323 if ((msg = ossl_cmp_pkiconf_new(srv_ctx->ctx)) == NULL)
324 CMPerr(0, CMP_R_ERROR_CREATING_PKICONF);
328 static OSSL_CMP_MSG *process_certConf(OSSL_CMP_SRV_CTX *srv_ctx,
329 const OSSL_CMP_MSG *req)
332 OSSL_CMP_CERTCONFIRMCONTENT *ccc;
334 OSSL_CMP_MSG *msg = NULL;
335 OSSL_CMP_CERTSTATUS *status = NULL;
337 if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
341 ccc = req->body->value.certConf;
342 num = sk_OSSL_CMP_CERTSTATUS_num(ccc);
344 if (OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM) == 1) {
345 CMPerr(0, CMP_R_ERROR_UNEXPECTED_CERTCONF);
350 ossl_cmp_err(ctx, "certificate rejected by client");
353 ossl_cmp_warn(ctx, "All CertStatus but the first will be ignored");
354 status = sk_OSSL_CMP_CERTSTATUS_value(ccc, OSSL_CMP_CERTREQID);
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;
362 if (!srv_ctx->process_certConf(srv_ctx, req, certReqId, certHash, si))
363 return NULL; /* reason code may be: CMP_R_CERTHASH_UNMATCHED */
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);
370 ossl_cmp_log2(INFO, ctx, "certificate rejected by client %s %s",
371 str == NULL ? "without" : "with",
372 str == NULL ? "PKIStatus" : str);
376 if ((msg = ossl_cmp_pkiconf_new(ctx)) == NULL)
377 CMPerr(0, CMP_R_ERROR_CREATING_PKICONF);
381 static OSSL_CMP_MSG *process_pollReq(OSSL_CMP_SRV_CTX *srv_ctx,
382 const OSSL_CMP_MSG *req)
384 OSSL_CMP_POLLREQCONTENT *prc;
385 OSSL_CMP_POLLREQ *pr;
387 OSSL_CMP_MSG *certReq;
388 int64_t check_after = 0;
389 OSSL_CMP_MSG *msg = NULL;
391 if (!ossl_assert(srv_ctx != NULL && srv_ctx->ctx != NULL && req != NULL))
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);
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))
406 if (certReq != NULL) {
407 msg = process_cert_request(srv_ctx, certReq);
408 OSSL_CMP_MSG_free(certReq);
410 if ((msg = ossl_cmp_pollRep_new(srv_ctx->ctx, certReqId,
411 check_after)) == NULL)
412 CMPerr(0, CMP_R_ERROR_CREATING_POLLREP);
418 * Determine whether missing/invalid protection of request message is allowed.
419 * Return 1 on acceptance, 0 on rejection, or -1 on (internal) error.
421 static int unprotected_exception(const OSSL_CMP_CTX *ctx,
422 const OSSL_CMP_MSG *req,
423 int invalid_protection,
424 int accept_unprotected_requests)
426 if (!ossl_assert(ctx != NULL && req != NULL))
429 if (accept_unprotected_requests) {
430 ossl_cmp_log1(WARN, ctx, "ignoring %s protection of request message",
431 invalid_protection ? "invalid" : "missing");
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");
443 * returns created message and NULL on internal error
445 OSSL_CMP_MSG *OSSL_CMP_SRV_process_request(OSSL_CMP_SRV_CTX *srv_ctx,
446 const OSSL_CMP_MSG *req)
449 OSSL_CMP_PKIHEADER *hdr;
450 int req_type, rsp_type;
451 OSSL_CMP_MSG *rsp = NULL;
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);
461 if (hdr->sender->type != GEN_DIRNAME) {
462 CMPerr(0, CMP_R_SENDER_GENERALNAME_TYPE_NOT_SUPPORTED);
465 if (!OSSL_CMP_CTX_set1_recipient(ctx, hdr->sender->d.directoryName))
468 req_type = ossl_cmp_msg_get_bodytype(req);
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) {
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",
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))
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);
503 if (ossl_cmp_msg_check_received(ctx, req, unprotected_exception,
504 srv_ctx->acceptUnprotected) < 0)
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);
515 rsp = process_cert_request(srv_ctx, req);
517 case OSSL_CMP_PKIBODY_RR:
518 if (srv_ctx->process_rr == NULL)
519 CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
521 rsp = process_rr(srv_ctx, req);
523 case OSSL_CMP_PKIBODY_GENM:
524 if (srv_ctx->process_genm == NULL)
525 CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
527 rsp = process_genm(srv_ctx, req);
529 case OSSL_CMP_PKIBODY_ERROR:
530 if (srv_ctx->process_error == NULL)
531 CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
533 rsp = process_error(srv_ctx, req);
535 case OSSL_CMP_PKIBODY_CERTCONF:
536 if (srv_ctx->process_certConf == NULL)
537 CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
539 rsp = process_certConf(srv_ctx, req);
541 case OSSL_CMP_PKIBODY_POLLREQ:
542 if (srv_ctx->process_pollReq == NULL)
543 CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
545 rsp = process_pollReq(srv_ctx, req);
548 /* TODO possibly support further request message types */
549 CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
554 /* on error, try to respond with CMP error message to client */
555 const char *data = NULL;
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;
562 if ((si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection,
563 fail_info, NULL)) == NULL)
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);
573 /* possibly close the transaction */
575 rsp != NULL ? ossl_cmp_msg_get_bodytype(rsp) : OSSL_CMP_PKIBODY_ERROR;
577 case OSSL_CMP_PKIBODY_IP:
578 case OSSL_CMP_PKIBODY_CP:
579 case OSSL_CMP_PKIBODY_KUP:
580 case OSSL_CMP_PKIBODY_RP:
581 if (OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM) == 0)
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 */
591 default: /* not closing transaction in other cases */
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
603 OSSL_CMP_MSG * OSSL_CMP_CTX_server_perform(OSSL_CMP_CTX *client_ctx,
604 const OSSL_CMP_MSG *req)
606 OSSL_CMP_SRV_CTX *srv_ctx = NULL;
608 if (client_ctx == NULL || req == NULL) {
609 CMPerr(0, CMP_R_NULL_ARGUMENT);
613 if ((srv_ctx = OSSL_CMP_CTX_get_transfer_cb_arg(client_ctx)) == NULL) {
614 CMPerr(0, CMP_R_TRANSFER_ERROR);
618 return OSSL_CMP_SRV_process_request(srv_ctx, req);