2 * Copyright 2007-2020 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 #include "cmp_local.h"
13 #include "internal/cryptlib.h"
15 /* explicit #includes not strictly needed since implied by the above: */
16 #include <openssl/bio.h>
17 #include <openssl/cmp.h>
18 #include <openssl/err.h>
19 #include <openssl/evp.h>
20 #include <openssl/x509v3.h>
22 #include "openssl/cmp_util.h"
24 DEFINE_STACK_OF(ASN1_UTF8STRING)
26 DEFINE_STACK_OF(X509_CRL)
27 DEFINE_STACK_OF(OSSL_CMP_CERTRESPONSE)
28 DEFINE_STACK_OF(OSSL_CMP_PKISI)
29 DEFINE_STACK_OF(OSSL_CRMF_CERTID)
31 #define IS_CREP(t) ((t) == OSSL_CMP_PKIBODY_IP || (t) == OSSL_CMP_PKIBODY_CP \
32 || (t) == OSSL_CMP_PKIBODY_KUP)
35 * Evaluate whether there's an exception (violating the standard) configured for
36 * handling negative responses without protection or with invalid protection.
37 * Returns 1 on acceptance, 0 on rejection, or -1 on (internal) error.
39 static int unprotected_exception(const OSSL_CMP_CTX *ctx,
40 const OSSL_CMP_MSG *rep,
41 int invalid_protection,
42 int expected_type /* ignored here */)
44 int rcvd_type = ossl_cmp_msg_get_bodytype(rep /* may be NULL */);
45 const char *msg_type = NULL;
47 if (!ossl_assert(ctx != NULL && rep != NULL))
50 if (!ctx->unprotectedErrors)
54 case OSSL_CMP_PKIBODY_ERROR:
55 msg_type = "error response";
57 case OSSL_CMP_PKIBODY_RP:
60 ossl_cmp_revrepcontent_get_pkisi(rep->body->value.rp,
65 if (ossl_cmp_pkisi_get_status(si) == OSSL_CMP_PKISTATUS_rejection)
66 msg_type = "revocation response message with rejection status";
69 case OSSL_CMP_PKIBODY_PKICONF:
70 msg_type = "PKI Confirmation message";
73 if (IS_CREP(rcvd_type)) {
74 OSSL_CMP_CERTREPMESSAGE *crepmsg = rep->body->value.ip;
75 OSSL_CMP_CERTRESPONSE *crep =
76 ossl_cmp_certrepmessage_get0_certresponse(crepmsg,
79 if (sk_OSSL_CMP_CERTRESPONSE_num(crepmsg->response) > 1)
81 /* TODO: handle potentially multiple CertResponses in CertRepMsg */
84 if (ossl_cmp_pkisi_get_status(crep->status)
85 == OSSL_CMP_PKISTATUS_rejection)
86 msg_type = "CertRepMessage with rejection status";
91 ossl_cmp_log2(WARN, ctx, "ignoring %s protection of %s",
92 invalid_protection ? "invalid" : "missing", msg_type);
97 /* Save error info from PKIStatusInfo field of a certresponse into ctx */
98 static int save_statusInfo(OSSL_CMP_CTX *ctx, OSSL_CMP_PKISI *si)
101 OSSL_CMP_PKIFREETEXT *ss;
103 if (!ossl_assert(ctx != NULL && si != NULL))
106 if ((ctx->status = ossl_cmp_pkisi_get_status(si)) < 0)
109 ctx->failInfoCode = 0;
110 if (si->failInfo != NULL) {
111 for (i = 0; i <= OSSL_CMP_PKIFAILUREINFO_MAX; i++) {
112 if (ASN1_BIT_STRING_get_bit(si->failInfo, i))
113 ctx->failInfoCode |= (1 << i);
117 if (!ossl_cmp_ctx_set0_statusString(ctx, sk_ASN1_UTF8STRING_new_null())
118 || (ctx->statusString == NULL))
121 ss = si->statusString; /* may be NULL */
122 for (i = 0; i < sk_ASN1_UTF8STRING_num(ss); i++) {
123 ASN1_UTF8STRING *str = sk_ASN1_UTF8STRING_value(ss, i);
125 if (!sk_ASN1_UTF8STRING_push(ctx->statusString, ASN1_STRING_dup(str)))
132 * Perform the generic aspects of sending a request and receiving a response.
133 * Returns 1 on success and provides the received PKIMESSAGE in *rep.
134 * Returns 0 on error.
135 * Regardless of success, caller is responsible for freeing *rep (unless NULL).
137 static int send_receive_check(OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *req,
138 OSSL_CMP_MSG **rep, int expected_type)
140 const char *req_type_str =
141 ossl_cmp_bodytype_to_string(ossl_cmp_msg_get_bodytype(req));
142 const char *expected_type_str = ossl_cmp_bodytype_to_string(expected_type);
145 time_t now = time(NULL);
147 OSSL_CMP_transfer_cb_t transfer_cb = ctx->transfer_cb;
149 if (transfer_cb == NULL)
150 transfer_cb = OSSL_CMP_MSG_http_perform;
153 msg_timeout = ctx->msg_timeout; /* backup original value */
154 if ((IS_CREP(expected_type) || expected_type == OSSL_CMP_PKIBODY_POLLREP)
155 && ctx->total_timeout > 0 /* timeout is not infinite */) {
156 if (now >= ctx->end_time) {
157 CMPerr(0, CMP_R_TOTAL_TIMEOUT);
160 if (!ossl_assert(ctx->end_time - time(NULL) < INT_MAX)) {
161 /* cannot really happen due to the assignment in do_certreq_seq() */
162 CMPerr(0, CMP_R_INVALID_ARGS);
165 time_left = (int)(ctx->end_time - now);
166 if (ctx->msg_timeout == 0 || time_left < ctx->msg_timeout)
167 ctx->msg_timeout = time_left;
170 /* should print error queue since transfer_cb may call ERR_clear_error() */
171 OSSL_CMP_CTX_print_errors(ctx);
173 ossl_cmp_log1(INFO, ctx, "sending %s", req_type_str);
175 *rep = (*transfer_cb)(ctx, req);
176 ctx->msg_timeout = msg_timeout; /* restore original value */
179 CMPerr(0, CMP_R_TRANSFER_ERROR); /* or receiving response */
180 ERR_add_error_data(2, "request sent: ", req_type_str);
181 ERR_add_error_data(2, ", expected response: ", expected_type_str);
185 bt = ossl_cmp_msg_get_bodytype(*rep);
187 * The body type in the 'bt' variable is not yet verified.
188 * Still we use this preliminary value already for a progress report because
189 * the following msg verification may also produce log entries and may fail.
191 ossl_cmp_log1(INFO, ctx, "received %s", ossl_cmp_bodytype_to_string(bt));
193 /* copy received extraCerts to ctx->extraCertsIn so they can be retrieved */
194 if (bt != OSSL_CMP_PKIBODY_POLLREP && bt != OSSL_CMP_PKIBODY_PKICONF
195 && !ossl_cmp_ctx_set1_extraCertsIn(ctx, (*rep)->extraCerts))
198 if (!ossl_cmp_msg_check_update(ctx, *rep, unprotected_exception,
202 if (bt == expected_type
203 /* as an answer to polling, there could be IP/CP/KUP: */
204 || (IS_CREP(bt) && expected_type == OSSL_CMP_PKIBODY_POLLREP))
207 /* received message type is not one of the expected ones (e.g., error) */
208 CMPerr(0, bt == OSSL_CMP_PKIBODY_ERROR ? CMP_R_RECEIVED_ERROR :
209 CMP_R_UNEXPECTED_PKIBODY); /* in next line for mkerr.pl */
211 if (bt != OSSL_CMP_PKIBODY_ERROR) {
212 ERR_add_error_data(3, "message type is '",
213 ossl_cmp_bodytype_to_string(bt), "'");
215 OSSL_CMP_ERRORMSGCONTENT *emc = (*rep)->body->value.error;
216 OSSL_CMP_PKISI *si = emc->pKIStatusInfo;
217 char buf[OSSL_CMP_PKISI_BUFLEN];
219 if (save_statusInfo(ctx, si)
220 && OSSL_CMP_CTX_snprint_PKIStatus(ctx, buf,
221 sizeof(buf)) != NULL)
222 ERR_add_error_data(1, buf);
223 if (emc->errorCode != NULL
224 && BIO_snprintf(buf, sizeof(buf), "; errorCode: %ld",
225 ASN1_INTEGER_get(emc->errorCode)) > 0)
226 ERR_add_error_data(1, buf);
227 if (emc->errorDetails != NULL) {
228 char *text = sk_ASN1_UTF8STRING2text(emc->errorDetails, ", ",
229 OSSL_CMP_PKISI_BUFLEN - 1);
232 ERR_add_error_data(2, "; errorDetails: ", text);
235 if (ctx->status != OSSL_CMP_PKISTATUS_rejection) {
236 CMPerr(0, CMP_R_UNEXPECTED_PKISTATUS);
237 if (ctx->status == OSSL_CMP_PKISTATUS_waiting)
238 ctx->status = OSSL_CMP_PKISTATUS_rejection;
245 * When a 'waiting' PKIStatus has been received, this function is used to
246 * poll, which should yield a pollRep or finally a CertRepMessage in ip/cp/kup.
247 * On receiving a pollRep, which includes a checkAfter value, it return this
248 * value if sleep == 0, else it sleeps as long as indicated and retries.
250 * A transaction timeout is enabled if ctx->total_timeout is > 0.
251 * In this case polling will continue until the timeout is reached and then
252 * polling is done a last time even if this is before the "checkAfter" time.
254 * Returns -1 on receiving pollRep if sleep == 0, setting the checkAfter value.
255 * Returns 1 on success and provides the received PKIMESSAGE in *rep.
256 * In this case the caller is responsible for freeing *rep.
257 * Returns 0 on error (which includes the case that timeout has been reached).
259 static int poll_for_response(OSSL_CMP_CTX *ctx, int sleep, int rid,
260 OSSL_CMP_MSG **rep, int *checkAfter)
262 OSSL_CMP_MSG *preq = NULL;
263 OSSL_CMP_MSG *prep = NULL;
266 "received 'waiting' PKIStatus, starting to poll for response");
269 /* TODO: handle potentially multiple poll requests per message */
270 if ((preq = ossl_cmp_pollReq_new(ctx, rid)) == NULL)
273 if (!send_receive_check(ctx, preq, &prep, OSSL_CMP_PKIBODY_POLLREP))
276 /* handle potential pollRep */
277 if (ossl_cmp_msg_get_bodytype(prep) == OSSL_CMP_PKIBODY_POLLREP) {
278 OSSL_CMP_POLLREPCONTENT *prc = prep->body->value.pollRep;
279 OSSL_CMP_POLLREP *pollRep = NULL;
281 char str[OSSL_CMP_PKISI_BUFLEN];
284 /* TODO: handle potentially multiple elements in pollRep */
285 if (sk_OSSL_CMP_POLLREP_num(prc) > 1) {
286 CMPerr(0, CMP_R_MULTIPLE_RESPONSES_NOT_SUPPORTED);
289 pollRep = ossl_cmp_pollrepcontent_get0_pollrep(prc, rid);
293 if (!ASN1_INTEGER_get_int64(&check_after, pollRep->checkAfter)) {
294 CMPerr(0, CMP_R_BAD_CHECKAFTER_IN_POLLREP);
297 if (check_after < 0 || (uint64_t)check_after
298 > (sleep ? ULONG_MAX / 1000 : INT_MAX)) {
299 CMPerr(0, CMP_R_CHECKAFTER_OUT_OF_RANGE);
300 if (BIO_snprintf(str, OSSL_CMP_PKISI_BUFLEN, "value = %jd",
302 ERR_add_error_data(1, str);
305 if (ctx->total_timeout > 0) { /* timeout is not infinite */
306 const int exp = 5; /* expected max time per msg round trip */
307 int64_t time_left = (int64_t)(ctx->end_time - exp - time(NULL));
309 if (time_left <= 0) {
310 CMPerr(0, CMP_R_TOTAL_TIMEOUT);
313 if (time_left < check_after)
314 check_after = time_left;
315 /* poll one last time just when timeout was reached */
318 if (pollRep->reason == NULL
319 || (len = BIO_snprintf(str, OSSL_CMP_PKISI_BUFLEN,
320 " with reason = '")) < 0) {
323 char *text = sk_ASN1_UTF8STRING2text(pollRep->reason, ", ",
324 sizeof(str) - len - 2);
327 || BIO_snprintf(str + len, sizeof(str) - len,
332 ossl_cmp_log2(INFO, ctx,
333 "received polling response%s; checkAfter = %ld seconds",
336 OSSL_CMP_MSG_free(preq);
338 OSSL_CMP_MSG_free(prep);
341 ossl_sleep((unsigned long)(1000 * check_after));
343 if (checkAfter != NULL)
344 *checkAfter = (int)check_after;
345 return -1; /* exits the loop */
348 ossl_cmp_info(ctx, "received ip/cp/kup after polling");
349 /* any other body type has been rejected by send_receive_check() */
356 OSSL_CMP_MSG_free(preq);
361 OSSL_CMP_MSG_free(preq);
362 OSSL_CMP_MSG_free(prep);
366 /* Send certConf for IR, CR or KUR sequences and check response */
367 int ossl_cmp_exchange_certConf(OSSL_CMP_CTX *ctx, int fail_info,
370 OSSL_CMP_MSG *certConf;
371 OSSL_CMP_MSG *PKIconf = NULL;
374 /* OSSL_CMP_certConf_new() also checks if all necessary options are set */
375 if ((certConf = ossl_cmp_certConf_new(ctx, fail_info, txt)) == NULL)
378 res = send_receive_check(ctx, certConf, &PKIconf, OSSL_CMP_PKIBODY_PKICONF);
381 OSSL_CMP_MSG_free(certConf);
382 OSSL_CMP_MSG_free(PKIconf);
386 /* Send given error and check response */
387 int ossl_cmp_exchange_error(OSSL_CMP_CTX *ctx, int status, int fail_info,
388 const char *txt, int errorCode, const char *details)
390 OSSL_CMP_MSG *error = NULL;
391 OSSL_CMP_PKISI *si = NULL;
392 OSSL_CMP_MSG *PKIconf = NULL;
395 if ((si = OSSL_CMP_STATUSINFO_new(status, fail_info, txt)) == NULL)
397 /* ossl_cmp_error_new() also checks if all necessary options are set */
398 if ((error = ossl_cmp_error_new(ctx, si, errorCode, details, 0)) == NULL)
401 res = send_receive_check(ctx, error, &PKIconf, OSSL_CMP_PKIBODY_PKICONF);
404 OSSL_CMP_MSG_free(error);
405 OSSL_CMP_PKISI_free(si);
406 OSSL_CMP_MSG_free(PKIconf);
411 * Retrieve a copy of the certificate, if any, from the given CertResponse.
412 * Take into account PKIStatusInfo of CertResponse in ctx, report it on error.
413 * Returns NULL if not found or on error.
415 static X509 *get1_cert_status(OSSL_CMP_CTX *ctx, int bodytype,
416 OSSL_CMP_CERTRESPONSE *crep)
418 char buf[OSSL_CMP_PKISI_BUFLEN];
422 if (!ossl_assert(ctx != NULL && crep != NULL))
425 privkey = OSSL_CMP_CTX_get0_newPkey(ctx, 1);
426 switch (ossl_cmp_pkisi_get_status(crep->status)) {
427 case OSSL_CMP_PKISTATUS_waiting:
429 "received \"waiting\" status for cert when actually aiming to extract cert");
430 CMPerr(0, CMP_R_ENCOUNTERED_WAITING);
432 case OSSL_CMP_PKISTATUS_grantedWithMods:
433 ossl_cmp_warn(ctx, "received \"grantedWithMods\" for certificate");
435 case OSSL_CMP_PKISTATUS_accepted:
437 /* get all information in case of a rejection before going to error */
438 case OSSL_CMP_PKISTATUS_rejection:
439 ossl_cmp_err(ctx, "received \"rejection\" status rather than cert");
440 CMPerr(0, CMP_R_REQUEST_REJECTED_BY_SERVER);
442 case OSSL_CMP_PKISTATUS_revocationWarning:
444 "received \"revocationWarning\" - a revocation of the cert is imminent");
446 case OSSL_CMP_PKISTATUS_revocationNotification:
448 "received \"revocationNotification\" - a revocation of the cert has occurred");
450 case OSSL_CMP_PKISTATUS_keyUpdateWarning:
451 if (bodytype != OSSL_CMP_PKIBODY_KUR) {
452 CMPerr(0, CMP_R_ENCOUNTERED_KEYUPDATEWARNING);
457 ossl_cmp_log1(ERROR, ctx,
458 "received unsupported PKIStatus %d for certificate",
460 CMPerr(0, CMP_R_UNKNOWN_PKISTATUS);
463 crt = ossl_cmp_certresponse_get1_cert(crep, ctx, privkey);
464 if (crt == NULL) /* according to PKIStatus, we can expect a cert */
465 CMPerr(0, CMP_R_CERTIFICATE_NOT_FOUND);
470 if (OSSL_CMP_CTX_snprint_PKIStatus(ctx, buf, sizeof(buf)) != NULL)
471 ERR_add_error_data(1, buf);
476 * Callback fn validating that the new certificate can be verified, using
477 * ctx->certConf_cb_arg, which has been initialized using opt_out_trusted, and
478 * ctx->untrusted, which at this point already contains msg->extraCerts.
479 * Returns 0 on acceptance, else a bit field reflecting PKIFailureInfo.
480 * Quoting from RFC 4210 section 5.1. Overall PKI Message:
481 * The extraCerts field can contain certificates that may be useful to
482 * the recipient. For example, this can be used by a CA or RA to
483 * present an end entity with certificates that it needs to verify its
484 * own new certificate (if, for example, the CA that issued the end
485 * entity's certificate is not a root CA for the end entity). Note that
486 * this field does not necessarily contain a certification path; the
487 * recipient may have to sort, select from, or otherwise process the
488 * extra certificates in order to use them.
489 * Note: While often handy, there is no hard requirement by CMP that
490 * an EE must be able to validate the certificates it gets enrolled.
492 int OSSL_CMP_certConf_cb(OSSL_CMP_CTX *ctx, X509 *cert, int fail_info,
495 X509_STORE *out_trusted = OSSL_CMP_CTX_get_certConf_cb_arg(ctx);
496 STACK_OF(X509) *chain = NULL;
497 (void)text; /* make (artificial) use of var to prevent compiler warning */
499 if (fail_info != 0) /* accept any error flagged by CMP core library */
502 ossl_cmp_debug(ctx, "trying to build chain for newly enrolled cert");
503 chain = ossl_cmp_build_cert_chain(ctx->libctx, ctx->propq,
504 out_trusted /* may be NULL */,
505 ctx->untrusted, cert);
506 if (sk_X509_num(chain) > 0)
507 X509_free(sk_X509_shift(chain)); /* remove leaf (EE) cert */
508 if (out_trusted != NULL) {
510 ossl_cmp_err(ctx, "failed building chain for newly enrolled cert");
511 fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_incorrectData;
514 "succeeded building proper chain for newly enrolled cert");
516 } else if (chain == NULL) {
517 ossl_cmp_warn(ctx, "could not build approximate chain for newly enrolled cert, resorting to received extraCerts");
518 chain = OSSL_CMP_CTX_get1_extraCertsIn(ctx);
521 "success building approximate chain for newly enrolled cert");
523 (void)ossl_cmp_ctx_set1_newChain(ctx, chain);
524 sk_X509_pop_free(chain, X509_free);
530 * Perform the generic handling of certificate responses for IR/CR/KUR/P10CR.
531 * Returns -1 on receiving pollRep if sleep == 0, setting the checkAfter value.
532 * Returns 1 on success and provides the received PKIMESSAGE in *resp.
533 * Returns 0 on error (which includes the case that timeout has been reached).
534 * Regardless of success, caller is responsible for freeing *resp (unless NULL).
536 static int cert_response(OSSL_CMP_CTX *ctx, int sleep, int rid,
537 OSSL_CMP_MSG **resp, int *checkAfter,
538 int req_type, int expected_type)
540 EVP_PKEY *rkey = OSSL_CMP_CTX_get0_newPkey(ctx /* may be NULL */, 0);
541 int fail_info = 0; /* no failure */
542 const char *txt = NULL;
543 OSSL_CMP_CERTREPMESSAGE *crepmsg;
544 OSSL_CMP_CERTRESPONSE *crep;
545 OSSL_CMP_certConf_cb_t cb;
550 if (!ossl_assert(ctx != NULL))
554 crepmsg = (*resp)->body->value.ip; /* same for cp and kup */
555 if (sk_OSSL_CMP_CERTRESPONSE_num(crepmsg->response) > 1) {
556 CMPerr(0, CMP_R_MULTIPLE_RESPONSES_NOT_SUPPORTED);
559 /* TODO: handle potentially multiple CertResponses in CertRepMsg */
560 crep = ossl_cmp_certrepmessage_get0_certresponse(crepmsg, rid);
563 if (!save_statusInfo(ctx, crep->status))
566 /* for OSSL_CMP_PKIBODY_P10CR learn CertReqId from response */
567 rid = ossl_cmp_asn1_get_int(crep->certReqId);
569 CMPerr(0, CMP_R_BAD_REQUEST_ID);
574 if (ossl_cmp_pkisi_get_status(crep->status) == OSSL_CMP_PKISTATUS_waiting) {
575 OSSL_CMP_MSG_free(*resp);
577 if ((ret = poll_for_response(ctx, sleep, rid, resp, checkAfter)) != 0) {
578 if (ret == -1) /* at this point implies sleep == 0 */
579 return ret; /* waiting */
580 goto retry; /* got ip/cp/kup, which may still indicate 'waiting' */
582 CMPerr(0, CMP_R_POLLING_FAILED);
587 cert = get1_cert_status(ctx, (*resp)->body->type, crep);
589 ERR_add_error_data(1, "; cannot extract certificate from response");
592 if (!ossl_cmp_ctx_set0_newCert(ctx, cert))
596 * if the CMP server returned certificates in the caPubs field, copy them
597 * to the context so that they can be retrieved if necessary
599 if (crepmsg->caPubs != NULL
600 && !ossl_cmp_ctx_set1_caPubs(ctx, crepmsg->caPubs))
603 subj = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0);
605 /* X509_check_private_key() also works if rkey is just public key */
606 && !(X509_check_private_key(ctx->newCert, rkey))) {
607 fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_incorrectData;
608 txt = "public key in new certificate does not match our enrollment key";
610 * not calling (void)ossl_cmp_exchange_error(ctx,
611 * OSSL_CMP_PKISTATUS_rejection, fail_info, txt)
612 * not throwing CMP_R_CERTIFICATE_NOT_ACCEPTED with txt
614 * since we better leave this for the certConf_cb to decide
619 * Execute the certification checking callback function,
620 * which can determine whether to accept a newly enrolled certificate.
621 * It may overrule the pre-decision reflected in 'fail_info' and '*txt'.
623 cb = ctx->certConf_cb != NULL ? ctx->certConf_cb : OSSL_CMP_certConf_cb;
624 if ((fail_info = cb(ctx, ctx->newCert, fail_info, &txt)) != 0
626 txt = "CMP client did not accept it";
627 if (fail_info != 0) /* immediately log error before any certConf exchange */
628 ossl_cmp_log1(ERROR, ctx,
629 "rejecting newly enrolled cert with subject: %s", subj);
632 * TODO: better move certConf exchange to do_certreq_seq() such that
633 * also more low-level errors with CertReqMessages get reported to server
635 if (!ctx->disableConfirm
636 && !ossl_cmp_hdr_has_implicitConfirm((*resp)->header)) {
637 if (!ossl_cmp_exchange_certConf(ctx, fail_info, txt))
641 /* not throwing failure earlier as transfer_cb may call ERR_clear_error() */
642 if (fail_info != 0) {
643 CMPerr(0, CMP_R_CERTIFICATE_NOT_ACCEPTED);
644 ERR_add_error_data(2, "rejecting newly enrolled cert with subject: ",
647 ERR_add_error_txt("; ", txt);
654 int OSSL_CMP_try_certreq(OSSL_CMP_CTX *ctx, int req_type,
655 const OSSL_CRMF_MSG *crm, int *checkAfter)
657 OSSL_CMP_MSG *req = NULL;
658 OSSL_CMP_MSG *rep = NULL;
659 int is_p10 = req_type == OSSL_CMP_PKIBODY_P10CR;
660 int rid = is_p10 ? -1 : OSSL_CMP_CERTREQID;
661 int rep_type = is_p10 ? OSSL_CMP_PKIBODY_CP : req_type + 1;
665 CMPerr(0, CMP_R_NULL_ARGUMENT);
669 if (ctx->status != OSSL_CMP_PKISTATUS_waiting) { /* not polling already */
671 if (!ossl_cmp_ctx_set0_newCert(ctx, NULL))
674 if (ctx->total_timeout > 0) /* else ctx->end_time is not used */
675 ctx->end_time = time(NULL) + ctx->total_timeout;
677 req = ossl_cmp_certreq_new(ctx, req_type, crm);
678 if (req == NULL) /* also checks if all necessary options are set */
681 if (!send_receive_check(ctx, req, &rep, rep_type))
685 return ossl_cmp_exchange_error(ctx, OSSL_CMP_PKISTATUS_rejection,
686 0 /* TODO better fail_info value? */,
687 "polling aborted", 0 /* errorCode */,
689 res = poll_for_response(ctx, 0 /* no sleep */, rid, &rep, checkAfter);
690 if (res <= 0) /* waiting or error */
693 res = cert_response(ctx, 0 /* no sleep */, rid, &rep, checkAfter,
697 OSSL_CMP_MSG_free(req);
698 OSSL_CMP_MSG_free(rep);
703 * Do the full sequence CR/IR/KUR/P10CR, CP/IP/KUP/CP,
704 * certConf, PKIconf, and polling if required.
705 * Will sleep as long as indicated by the server (according to checkAfter).
706 * All enrollment options need to be present in the context.
707 * TODO: another function to request two certificates at once should be created.
708 * Returns pointer to received certificate, or NULL if none was received.
710 X509 *OSSL_CMP_exec_certreq(OSSL_CMP_CTX *ctx, int req_type,
711 const OSSL_CRMF_MSG *crm)
714 OSSL_CMP_MSG *req = NULL;
715 OSSL_CMP_MSG *rep = NULL;
716 int is_p10 = req_type == OSSL_CMP_PKIBODY_P10CR;
717 int rid = is_p10 ? -1 : OSSL_CMP_CERTREQID;
718 int rep_type = is_p10 ? OSSL_CMP_PKIBODY_CP : req_type + 1;
722 CMPerr(0, CMP_R_NULL_ARGUMENT);
725 if (is_p10 && crm != NULL) {
726 CMPerr(0, CMP_R_INVALID_ARGS);
731 if (!ossl_cmp_ctx_set0_newCert(ctx, NULL))
734 if (ctx->total_timeout > 0) /* else ctx->end_time is not used */
735 ctx->end_time = time(NULL) + ctx->total_timeout;
737 /* OSSL_CMP_certreq_new() also checks if all necessary options are set */
738 if ((req = ossl_cmp_certreq_new(ctx, req_type, crm)) == NULL)
741 if (!send_receive_check(ctx, req, &rep, rep_type))
744 if (cert_response(ctx, 1 /* sleep */, rid, &rep, NULL, req_type, rep_type)
748 result = ctx->newCert;
750 OSSL_CMP_MSG_free(req);
751 OSSL_CMP_MSG_free(rep);
755 X509 *OSSL_CMP_exec_RR_ses(OSSL_CMP_CTX *ctx)
757 OSSL_CMP_MSG *rr = NULL;
758 OSSL_CMP_MSG *rp = NULL;
759 const int num_RevDetails = 1;
760 const int rsid = OSSL_CMP_REVREQSID;
761 OSSL_CMP_REVREPCONTENT *rrep = NULL;
762 OSSL_CMP_PKISI *si = NULL;
763 char buf[OSSL_CMP_PKISI_BUFLEN];
767 CMPerr(0, CMP_R_INVALID_ARGS);
770 if (ctx->oldCert == NULL) {
771 CMPerr(0, CMP_R_MISSING_REFERENCE_CERT);
776 /* OSSL_CMP_rr_new() also checks if all necessary options are set */
777 if ((rr = ossl_cmp_rr_new(ctx)) == NULL)
780 if (!send_receive_check(ctx, rr, &rp, OSSL_CMP_PKIBODY_RP))
783 rrep = rp->body->value.rp;
784 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
785 if (sk_OSSL_CMP_PKISI_num(rrep->status) != num_RevDetails) {
786 CMPerr(0, CMP_R_WRONG_RP_COMPONENT_COUNT);
790 if (sk_OSSL_CMP_PKISI_num(rrep->status) < 1) {
791 CMPerr(0, CMP_R_WRONG_RP_COMPONENT_COUNT);
796 /* evaluate PKIStatus field */
797 si = ossl_cmp_revrepcontent_get_pkisi(rrep, rsid);
798 if (!save_statusInfo(ctx, si))
800 switch (ossl_cmp_pkisi_get_status(si)) {
801 case OSSL_CMP_PKISTATUS_accepted:
802 ossl_cmp_info(ctx, "revocation accepted (PKIStatus=accepted)");
803 result = ctx->oldCert;
805 case OSSL_CMP_PKISTATUS_grantedWithMods:
806 ossl_cmp_info(ctx, "revocation accepted (PKIStatus=grantedWithMods)");
807 result = ctx->oldCert;
809 case OSSL_CMP_PKISTATUS_rejection:
810 CMPerr(0, CMP_R_REQUEST_REJECTED_BY_SERVER);
812 case OSSL_CMP_PKISTATUS_revocationWarning:
813 ossl_cmp_info(ctx, "revocation accepted (PKIStatus=revocationWarning)");
814 result = ctx->oldCert;
816 case OSSL_CMP_PKISTATUS_revocationNotification:
817 /* interpretation as warning or error depends on CA */
819 "revocation accepted (PKIStatus=revocationNotification)");
820 result = ctx->oldCert;
822 case OSSL_CMP_PKISTATUS_waiting:
823 case OSSL_CMP_PKISTATUS_keyUpdateWarning:
824 CMPerr(0, CMP_R_UNEXPECTED_PKISTATUS);
827 CMPerr(0, CMP_R_UNKNOWN_PKISTATUS);
831 /* check any present CertId in optional revCerts field */
832 if (rrep->revCerts != NULL) {
833 OSSL_CRMF_CERTID *cid;
834 OSSL_CRMF_CERTTEMPLATE *tmpl =
835 sk_OSSL_CMP_REVDETAILS_value(rr->body->value.rr, rsid)->certDetails;
836 const X509_NAME *issuer = OSSL_CRMF_CERTTEMPLATE_get0_issuer(tmpl);
837 ASN1_INTEGER *serial = OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(tmpl);
839 if (sk_OSSL_CRMF_CERTID_num(rrep->revCerts) != num_RevDetails) {
840 CMPerr(0, CMP_R_WRONG_RP_COMPONENT_COUNT);
844 if ((cid = ossl_cmp_revrepcontent_get_CertId(rrep, rsid)) == NULL) {
848 if (X509_NAME_cmp(issuer, OSSL_CRMF_CERTID_get0_issuer(cid)) != 0) {
849 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
850 CMPerr(0, CMP_R_WRONG_CERTID_IN_RP);
855 if (ASN1_INTEGER_cmp(serial,
856 OSSL_CRMF_CERTID_get0_serialNumber(cid)) != 0) {
857 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
858 CMPerr(0, CMP_R_WRONG_SERIAL_IN_RP);
865 /* check number of any optionally present crls */
866 if (rrep->crls != NULL && sk_X509_CRL_num(rrep->crls) != num_RevDetails) {
867 CMPerr(0, CMP_R_WRONG_RP_COMPONENT_COUNT);
874 && OSSL_CMP_CTX_snprint_PKIStatus(ctx, buf, sizeof(buf)) != NULL)
875 ERR_add_error_data(1, buf);
878 OSSL_CMP_MSG_free(rr);
879 OSSL_CMP_MSG_free(rp);
883 STACK_OF(OSSL_CMP_ITAV) *OSSL_CMP_exec_GENM_ses(OSSL_CMP_CTX *ctx)
886 OSSL_CMP_MSG *genp = NULL;
887 STACK_OF(OSSL_CMP_ITAV) *rcvd_itavs = NULL;
890 CMPerr(0, CMP_R_INVALID_ARGS);
894 if ((genm = ossl_cmp_genm_new(ctx)) == NULL)
897 if (!send_receive_check(ctx, genm, &genp, OSSL_CMP_PKIBODY_GENP))
900 /* received stack of itavs not to be freed with the genp */
901 rcvd_itavs = genp->body->value.genp;
902 genp->body->value.genp = NULL;
905 OSSL_CMP_MSG_free(genm);
906 OSSL_CMP_MSG_free(genp);
908 return rcvd_itavs; /* recv_itavs == NULL indicates an error */