A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

900 lines
32 KiB

  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. #include "cmp_local.h"
  12. #include "internal/cryptlib.h"
  13. /* explicit #includes not strictly needed since implied by the above: */
  14. #include <openssl/bio.h>
  15. #include <openssl/cmp.h>
  16. #include <openssl/err.h>
  17. #include <openssl/evp.h>
  18. #include <openssl/x509v3.h>
  19. #include "openssl/cmp_util.h"
  20. #define IS_CREP(t) ((t) == OSSL_CMP_PKIBODY_IP || (t) == OSSL_CMP_PKIBODY_CP \
  21. || (t) == OSSL_CMP_PKIBODY_KUP)
  22. /*-
  23. * Evaluate whether there's an exception (violating the standard) configured for
  24. * handling negative responses without protection or with invalid protection.
  25. * Returns 1 on acceptance, 0 on rejection, or -1 on (internal) error.
  26. */
  27. static int unprotected_exception(const OSSL_CMP_CTX *ctx,
  28. const OSSL_CMP_MSG *rep,
  29. int invalid_protection,
  30. int expected_type /* ignored here */)
  31. {
  32. int rcvd_type = ossl_cmp_msg_get_bodytype(rep /* may be NULL */);
  33. const char *msg_type = NULL;
  34. if (!ossl_assert(ctx != NULL && rep != NULL))
  35. return -1;
  36. if (!ctx->unprotectedErrors)
  37. return 0;
  38. switch (rcvd_type) {
  39. case OSSL_CMP_PKIBODY_ERROR:
  40. msg_type = "error response";
  41. break;
  42. case OSSL_CMP_PKIBODY_RP:
  43. {
  44. OSSL_CMP_PKISI *si =
  45. ossl_cmp_revrepcontent_get_pkisi(rep->body->value.rp,
  46. OSSL_CMP_REVREQSID);
  47. if (si == NULL)
  48. return -1;
  49. if (ossl_cmp_pkisi_get_status(si) == OSSL_CMP_PKISTATUS_rejection)
  50. msg_type = "revocation response message with rejection status";
  51. break;
  52. }
  53. case OSSL_CMP_PKIBODY_PKICONF:
  54. msg_type = "PKI Confirmation message";
  55. break;
  56. default:
  57. if (IS_CREP(rcvd_type)) {
  58. OSSL_CMP_CERTREPMESSAGE *crepmsg = rep->body->value.ip;
  59. OSSL_CMP_CERTRESPONSE *crep =
  60. ossl_cmp_certrepmessage_get0_certresponse(crepmsg,
  61. -1 /* any rid */);
  62. if (sk_OSSL_CMP_CERTRESPONSE_num(crepmsg->response) > 1)
  63. return -1;
  64. /* TODO: handle potentially multiple CertResponses in CertRepMsg */
  65. if (crep == NULL)
  66. return -1;
  67. if (ossl_cmp_pkisi_get_status(crep->status)
  68. == OSSL_CMP_PKISTATUS_rejection)
  69. msg_type = "CertRepMessage with rejection status";
  70. }
  71. }
  72. if (msg_type == NULL)
  73. return 0;
  74. ossl_cmp_log2(WARN, ctx, "ignoring %s protection of %s",
  75. invalid_protection ? "invalid" : "missing", msg_type);
  76. return 1;
  77. }
  78. /* Save error info from PKIStatusInfo field of a certresponse into ctx */
  79. static int save_statusInfo(OSSL_CMP_CTX *ctx, OSSL_CMP_PKISI *si)
  80. {
  81. int i;
  82. OSSL_CMP_PKIFREETEXT *ss;
  83. if (!ossl_assert(ctx != NULL && si != NULL))
  84. return 0;
  85. if ((ctx->status = ossl_cmp_pkisi_get_status(si)) < 0)
  86. return 0;
  87. ctx->failInfoCode = 0;
  88. if (si->failInfo != NULL) {
  89. for (i = 0; i <= OSSL_CMP_PKIFAILUREINFO_MAX; i++) {
  90. if (ASN1_BIT_STRING_get_bit(si->failInfo, i))
  91. ctx->failInfoCode |= (1 << i);
  92. }
  93. }
  94. if (!ossl_cmp_ctx_set0_statusString(ctx, sk_ASN1_UTF8STRING_new_null())
  95. || (ctx->statusString == NULL))
  96. return 0;
  97. ss = si->statusString; /* may be NULL */
  98. for (i = 0; i < sk_ASN1_UTF8STRING_num(ss); i++) {
  99. ASN1_UTF8STRING *str = sk_ASN1_UTF8STRING_value(ss, i);
  100. if (!sk_ASN1_UTF8STRING_push(ctx->statusString, ASN1_STRING_dup(str)))
  101. return 0;
  102. }
  103. return 1;
  104. }
  105. /*-
  106. * Perform the generic aspects of sending a request and receiving a response.
  107. * Returns 1 on success and provides the received PKIMESSAGE in *rep.
  108. * Returns 0 on error.
  109. * Regardless of success, caller is responsible for freeing *rep (unless NULL).
  110. */
  111. static int send_receive_check(OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *req,
  112. OSSL_CMP_MSG **rep, int expected_type)
  113. {
  114. const char *req_type_str =
  115. ossl_cmp_bodytype_to_string(ossl_cmp_msg_get_bodytype(req));
  116. const char *expected_type_str = ossl_cmp_bodytype_to_string(expected_type);
  117. int msg_timeout;
  118. int bt;
  119. time_t now = time(NULL);
  120. int time_left;
  121. OSSL_CMP_transfer_cb_t transfer_cb = ctx->transfer_cb;
  122. if (transfer_cb == NULL)
  123. transfer_cb = OSSL_CMP_MSG_http_perform;
  124. *rep = NULL;
  125. msg_timeout = ctx->msg_timeout; /* backup original value */
  126. if ((IS_CREP(expected_type) || expected_type == OSSL_CMP_PKIBODY_POLLREP)
  127. && ctx->total_timeout > 0 /* timeout is not infinite */) {
  128. if (now >= ctx->end_time) {
  129. ERR_raise(ERR_LIB_CMP, CMP_R_TOTAL_TIMEOUT);
  130. return 0;
  131. }
  132. if (!ossl_assert(ctx->end_time - time(NULL) < INT_MAX)) {
  133. /* cannot really happen due to the assignment in do_certreq_seq() */
  134. ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
  135. return 0;
  136. }
  137. time_left = (int)(ctx->end_time - now);
  138. if (ctx->msg_timeout == 0 || time_left < ctx->msg_timeout)
  139. ctx->msg_timeout = time_left;
  140. }
  141. /* should print error queue since transfer_cb may call ERR_clear_error() */
  142. OSSL_CMP_CTX_print_errors(ctx);
  143. ossl_cmp_log1(INFO, ctx, "sending %s", req_type_str);
  144. *rep = (*transfer_cb)(ctx, req);
  145. ctx->msg_timeout = msg_timeout; /* restore original value */
  146. if (*rep == NULL) {
  147. ERR_raise_data(ERR_LIB_CMP, CMP_R_TRANSFER_ERROR, /* or receiving response */
  148. "request sent: %s, expected response: %s",
  149. req_type_str, expected_type_str);
  150. return 0;
  151. }
  152. bt = ossl_cmp_msg_get_bodytype(*rep);
  153. /*
  154. * The body type in the 'bt' variable is not yet verified.
  155. * Still we use this preliminary value already for a progress report because
  156. * the following msg verification may also produce log entries and may fail.
  157. */
  158. ossl_cmp_log1(INFO, ctx, "received %s", ossl_cmp_bodytype_to_string(bt));
  159. /* copy received extraCerts to ctx->extraCertsIn so they can be retrieved */
  160. if (bt != OSSL_CMP_PKIBODY_POLLREP && bt != OSSL_CMP_PKIBODY_PKICONF
  161. && !ossl_cmp_ctx_set1_extraCertsIn(ctx, (*rep)->extraCerts))
  162. return 0;
  163. if (!ossl_cmp_msg_check_update(ctx, *rep, unprotected_exception,
  164. expected_type))
  165. return 0;
  166. if (bt == expected_type
  167. /* as an answer to polling, there could be IP/CP/KUP: */
  168. || (IS_CREP(bt) && expected_type == OSSL_CMP_PKIBODY_POLLREP))
  169. return 1;
  170. /* received message type is not one of the expected ones (e.g., error) */
  171. ERR_raise(ERR_LIB_CMP, bt == OSSL_CMP_PKIBODY_ERROR ? CMP_R_RECEIVED_ERROR :
  172. CMP_R_UNEXPECTED_PKIBODY); /* in next line for mkerr.pl */
  173. if (bt != OSSL_CMP_PKIBODY_ERROR) {
  174. ERR_add_error_data(3, "message type is '",
  175. ossl_cmp_bodytype_to_string(bt), "'");
  176. } else {
  177. OSSL_CMP_ERRORMSGCONTENT *emc = (*rep)->body->value.error;
  178. OSSL_CMP_PKISI *si = emc->pKIStatusInfo;
  179. char buf[OSSL_CMP_PKISI_BUFLEN];
  180. if (save_statusInfo(ctx, si)
  181. && OSSL_CMP_CTX_snprint_PKIStatus(ctx, buf,
  182. sizeof(buf)) != NULL)
  183. ERR_add_error_data(1, buf);
  184. if (emc->errorCode != NULL
  185. && BIO_snprintf(buf, sizeof(buf), "; errorCode: %ld",
  186. ASN1_INTEGER_get(emc->errorCode)) > 0)
  187. ERR_add_error_data(1, buf);
  188. if (emc->errorDetails != NULL) {
  189. char *text = sk_ASN1_UTF8STRING2text(emc->errorDetails, ", ",
  190. OSSL_CMP_PKISI_BUFLEN - 1);
  191. if (text != NULL)
  192. ERR_add_error_data(2, "; errorDetails: ", text);
  193. OPENSSL_free(text);
  194. }
  195. if (ctx->status != OSSL_CMP_PKISTATUS_rejection) {
  196. ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKISTATUS);
  197. if (ctx->status == OSSL_CMP_PKISTATUS_waiting)
  198. ctx->status = OSSL_CMP_PKISTATUS_rejection;
  199. }
  200. }
  201. return 0;
  202. }
  203. /*-
  204. * When a 'waiting' PKIStatus has been received, this function is used to
  205. * poll, which should yield a pollRep or finally a CertRepMessage in ip/cp/kup.
  206. * On receiving a pollRep, which includes a checkAfter value, it return this
  207. * value if sleep == 0, else it sleeps as long as indicated and retries.
  208. *
  209. * A transaction timeout is enabled if ctx->total_timeout is > 0.
  210. * In this case polling will continue until the timeout is reached and then
  211. * polling is done a last time even if this is before the "checkAfter" time.
  212. *
  213. * Returns -1 on receiving pollRep if sleep == 0, setting the checkAfter value.
  214. * Returns 1 on success and provides the received PKIMESSAGE in *rep.
  215. * In this case the caller is responsible for freeing *rep.
  216. * Returns 0 on error (which includes the case that timeout has been reached).
  217. */
  218. static int poll_for_response(OSSL_CMP_CTX *ctx, int sleep, int rid,
  219. OSSL_CMP_MSG **rep, int *checkAfter)
  220. {
  221. OSSL_CMP_MSG *preq = NULL;
  222. OSSL_CMP_MSG *prep = NULL;
  223. ossl_cmp_info(ctx,
  224. "received 'waiting' PKIStatus, starting to poll for response");
  225. *rep = NULL;
  226. for (;;) {
  227. /* TODO: handle potentially multiple poll requests per message */
  228. if ((preq = ossl_cmp_pollReq_new(ctx, rid)) == NULL)
  229. goto err;
  230. if (!send_receive_check(ctx, preq, &prep, OSSL_CMP_PKIBODY_POLLREP))
  231. goto err;
  232. /* handle potential pollRep */
  233. if (ossl_cmp_msg_get_bodytype(prep) == OSSL_CMP_PKIBODY_POLLREP) {
  234. OSSL_CMP_POLLREPCONTENT *prc = prep->body->value.pollRep;
  235. OSSL_CMP_POLLREP *pollRep = NULL;
  236. int64_t check_after;
  237. char str[OSSL_CMP_PKISI_BUFLEN];
  238. int len;
  239. /* TODO: handle potentially multiple elements in pollRep */
  240. if (sk_OSSL_CMP_POLLREP_num(prc) > 1) {
  241. ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_RESPONSES_NOT_SUPPORTED);
  242. goto err;
  243. }
  244. pollRep = ossl_cmp_pollrepcontent_get0_pollrep(prc, rid);
  245. if (pollRep == NULL)
  246. goto err;
  247. if (!ASN1_INTEGER_get_int64(&check_after, pollRep->checkAfter)) {
  248. ERR_raise(ERR_LIB_CMP, CMP_R_BAD_CHECKAFTER_IN_POLLREP);
  249. goto err;
  250. }
  251. if (check_after < 0 || (uint64_t)check_after
  252. > (sleep ? ULONG_MAX / 1000 : INT_MAX)) {
  253. ERR_raise(ERR_LIB_CMP, CMP_R_CHECKAFTER_OUT_OF_RANGE);
  254. if (BIO_snprintf(str, OSSL_CMP_PKISI_BUFLEN, "value = %jd",
  255. check_after) >= 0)
  256. ERR_add_error_data(1, str);
  257. goto err;
  258. }
  259. if (ctx->total_timeout > 0) { /* timeout is not infinite */
  260. const int exp = 5; /* expected max time per msg round trip */
  261. int64_t time_left = (int64_t)(ctx->end_time - exp - time(NULL));
  262. if (time_left <= 0) {
  263. ERR_raise(ERR_LIB_CMP, CMP_R_TOTAL_TIMEOUT);
  264. goto err;
  265. }
  266. if (time_left < check_after)
  267. check_after = time_left;
  268. /* poll one last time just when timeout was reached */
  269. }
  270. if (pollRep->reason == NULL
  271. || (len = BIO_snprintf(str, OSSL_CMP_PKISI_BUFLEN,
  272. " with reason = '")) < 0) {
  273. *str = '\0';
  274. } else {
  275. char *text = sk_ASN1_UTF8STRING2text(pollRep->reason, ", ",
  276. sizeof(str) - len - 2);
  277. if (text == NULL
  278. || BIO_snprintf(str + len, sizeof(str) - len,
  279. "%s'", text) < 0)
  280. *str = '\0';
  281. OPENSSL_free(text);
  282. }
  283. ossl_cmp_log2(INFO, ctx,
  284. "received polling response%s; checkAfter = %ld seconds",
  285. str, check_after);
  286. OSSL_CMP_MSG_free(preq);
  287. preq = NULL;
  288. OSSL_CMP_MSG_free(prep);
  289. prep = NULL;
  290. if (sleep) {
  291. ossl_sleep((unsigned long)(1000 * check_after));
  292. } else {
  293. if (checkAfter != NULL)
  294. *checkAfter = (int)check_after;
  295. return -1; /* exits the loop */
  296. }
  297. } else {
  298. ossl_cmp_info(ctx, "received ip/cp/kup after polling");
  299. /* any other body type has been rejected by send_receive_check() */
  300. break;
  301. }
  302. }
  303. if (prep == NULL)
  304. goto err;
  305. OSSL_CMP_MSG_free(preq);
  306. *rep = prep;
  307. return 1;
  308. err:
  309. OSSL_CMP_MSG_free(preq);
  310. OSSL_CMP_MSG_free(prep);
  311. return 0;
  312. }
  313. /* Send certConf for IR, CR or KUR sequences and check response */
  314. int ossl_cmp_exchange_certConf(OSSL_CMP_CTX *ctx, int fail_info,
  315. const char *txt)
  316. {
  317. OSSL_CMP_MSG *certConf;
  318. OSSL_CMP_MSG *PKIconf = NULL;
  319. int res = 0;
  320. /* OSSL_CMP_certConf_new() also checks if all necessary options are set */
  321. if ((certConf = ossl_cmp_certConf_new(ctx, fail_info, txt)) == NULL)
  322. goto err;
  323. res = send_receive_check(ctx, certConf, &PKIconf, OSSL_CMP_PKIBODY_PKICONF);
  324. err:
  325. OSSL_CMP_MSG_free(certConf);
  326. OSSL_CMP_MSG_free(PKIconf);
  327. return res;
  328. }
  329. /* Send given error and check response */
  330. int ossl_cmp_exchange_error(OSSL_CMP_CTX *ctx, int status, int fail_info,
  331. const char *txt, int errorCode, const char *details)
  332. {
  333. OSSL_CMP_MSG *error = NULL;
  334. OSSL_CMP_PKISI *si = NULL;
  335. OSSL_CMP_MSG *PKIconf = NULL;
  336. int res = 0;
  337. if ((si = OSSL_CMP_STATUSINFO_new(status, fail_info, txt)) == NULL)
  338. goto err;
  339. /* ossl_cmp_error_new() also checks if all necessary options are set */
  340. if ((error = ossl_cmp_error_new(ctx, si, errorCode, details, 0)) == NULL)
  341. goto err;
  342. res = send_receive_check(ctx, error, &PKIconf, OSSL_CMP_PKIBODY_PKICONF);
  343. err:
  344. OSSL_CMP_MSG_free(error);
  345. OSSL_CMP_PKISI_free(si);
  346. OSSL_CMP_MSG_free(PKIconf);
  347. return res;
  348. }
  349. /*-
  350. * Retrieve a copy of the certificate, if any, from the given CertResponse.
  351. * Take into account PKIStatusInfo of CertResponse in ctx, report it on error.
  352. * Returns NULL if not found or on error.
  353. */
  354. static X509 *get1_cert_status(OSSL_CMP_CTX *ctx, int bodytype,
  355. OSSL_CMP_CERTRESPONSE *crep)
  356. {
  357. char buf[OSSL_CMP_PKISI_BUFLEN];
  358. X509 *crt = NULL;
  359. EVP_PKEY *privkey;
  360. if (!ossl_assert(ctx != NULL && crep != NULL))
  361. return NULL;
  362. privkey = OSSL_CMP_CTX_get0_newPkey(ctx, 1);
  363. switch (ossl_cmp_pkisi_get_status(crep->status)) {
  364. case OSSL_CMP_PKISTATUS_waiting:
  365. ossl_cmp_err(ctx,
  366. "received \"waiting\" status for cert when actually aiming to extract cert");
  367. ERR_raise(ERR_LIB_CMP, CMP_R_ENCOUNTERED_WAITING);
  368. goto err;
  369. case OSSL_CMP_PKISTATUS_grantedWithMods:
  370. ossl_cmp_warn(ctx, "received \"grantedWithMods\" for certificate");
  371. break;
  372. case OSSL_CMP_PKISTATUS_accepted:
  373. break;
  374. /* get all information in case of a rejection before going to error */
  375. case OSSL_CMP_PKISTATUS_rejection:
  376. ossl_cmp_err(ctx, "received \"rejection\" status rather than cert");
  377. ERR_raise(ERR_LIB_CMP, CMP_R_REQUEST_REJECTED_BY_SERVER);
  378. goto err;
  379. case OSSL_CMP_PKISTATUS_revocationWarning:
  380. ossl_cmp_warn(ctx,
  381. "received \"revocationWarning\" - a revocation of the cert is imminent");
  382. break;
  383. case OSSL_CMP_PKISTATUS_revocationNotification:
  384. ossl_cmp_warn(ctx,
  385. "received \"revocationNotification\" - a revocation of the cert has occurred");
  386. break;
  387. case OSSL_CMP_PKISTATUS_keyUpdateWarning:
  388. if (bodytype != OSSL_CMP_PKIBODY_KUR) {
  389. ERR_raise(ERR_LIB_CMP, CMP_R_ENCOUNTERED_KEYUPDATEWARNING);
  390. goto err;
  391. }
  392. break;
  393. default:
  394. ossl_cmp_log1(ERROR, ctx,
  395. "received unsupported PKIStatus %d for certificate",
  396. ctx->status);
  397. ERR_raise(ERR_LIB_CMP, CMP_R_UNKNOWN_PKISTATUS);
  398. goto err;
  399. }
  400. crt = ossl_cmp_certresponse_get1_cert(crep, ctx, privkey);
  401. if (crt == NULL) /* according to PKIStatus, we can expect a cert */
  402. ERR_raise(ERR_LIB_CMP, CMP_R_CERTIFICATE_NOT_FOUND);
  403. return crt;
  404. err:
  405. if (OSSL_CMP_CTX_snprint_PKIStatus(ctx, buf, sizeof(buf)) != NULL)
  406. ERR_add_error_data(1, buf);
  407. return NULL;
  408. }
  409. /*-
  410. * Callback fn validating that the new certificate can be verified, using
  411. * ctx->certConf_cb_arg, which has been initialized using opt_out_trusted, and
  412. * ctx->untrusted, which at this point already contains msg->extraCerts.
  413. * Returns 0 on acceptance, else a bit field reflecting PKIFailureInfo.
  414. * Quoting from RFC 4210 section 5.1. Overall PKI Message:
  415. * The extraCerts field can contain certificates that may be useful to
  416. * the recipient. For example, this can be used by a CA or RA to
  417. * present an end entity with certificates that it needs to verify its
  418. * own new certificate (if, for example, the CA that issued the end
  419. * entity's certificate is not a root CA for the end entity). Note that
  420. * this field does not necessarily contain a certification path; the
  421. * recipient may have to sort, select from, or otherwise process the
  422. * extra certificates in order to use them.
  423. * Note: While often handy, there is no hard requirement by CMP that
  424. * an EE must be able to validate the certificates it gets enrolled.
  425. */
  426. int OSSL_CMP_certConf_cb(OSSL_CMP_CTX *ctx, X509 *cert, int fail_info,
  427. const char **text)
  428. {
  429. X509_STORE *out_trusted = OSSL_CMP_CTX_get_certConf_cb_arg(ctx);
  430. STACK_OF(X509) *chain = NULL;
  431. (void)text; /* make (artificial) use of var to prevent compiler warning */
  432. if (fail_info != 0) /* accept any error flagged by CMP core library */
  433. return fail_info;
  434. ossl_cmp_debug(ctx, "trying to build chain for newly enrolled cert");
  435. chain = ossl_cmp_build_cert_chain(ctx->libctx, ctx->propq,
  436. out_trusted /* may be NULL */,
  437. ctx->untrusted, cert);
  438. if (sk_X509_num(chain) > 0)
  439. X509_free(sk_X509_shift(chain)); /* remove leaf (EE) cert */
  440. if (out_trusted != NULL) {
  441. if (chain == NULL) {
  442. ossl_cmp_err(ctx, "failed building chain for newly enrolled cert");
  443. fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_incorrectData;
  444. } else {
  445. ossl_cmp_debug(ctx,
  446. "succeeded building proper chain for newly enrolled cert");
  447. }
  448. } else if (chain == NULL) {
  449. ossl_cmp_warn(ctx, "could not build approximate chain for newly enrolled cert, resorting to received extraCerts");
  450. chain = OSSL_CMP_CTX_get1_extraCertsIn(ctx);
  451. } else {
  452. ossl_cmp_debug(ctx,
  453. "success building approximate chain for newly enrolled cert");
  454. }
  455. (void)ossl_cmp_ctx_set1_newChain(ctx, chain);
  456. sk_X509_pop_free(chain, X509_free);
  457. return fail_info;
  458. }
  459. /*-
  460. * Perform the generic handling of certificate responses for IR/CR/KUR/P10CR.
  461. * Returns -1 on receiving pollRep if sleep == 0, setting the checkAfter value.
  462. * Returns 1 on success and provides the received PKIMESSAGE in *resp.
  463. * Returns 0 on error (which includes the case that timeout has been reached).
  464. * Regardless of success, caller is responsible for freeing *resp (unless NULL).
  465. */
  466. static int cert_response(OSSL_CMP_CTX *ctx, int sleep, int rid,
  467. OSSL_CMP_MSG **resp, int *checkAfter,
  468. int req_type, int expected_type)
  469. {
  470. EVP_PKEY *rkey = OSSL_CMP_CTX_get0_newPkey(ctx /* may be NULL */, 0);
  471. int fail_info = 0; /* no failure */
  472. const char *txt = NULL;
  473. OSSL_CMP_CERTREPMESSAGE *crepmsg;
  474. OSSL_CMP_CERTRESPONSE *crep;
  475. OSSL_CMP_certConf_cb_t cb;
  476. X509 *cert;
  477. char *subj = NULL;
  478. int ret = 1;
  479. if (!ossl_assert(ctx != NULL))
  480. return 0;
  481. retry:
  482. crepmsg = (*resp)->body->value.ip; /* same for cp and kup */
  483. if (sk_OSSL_CMP_CERTRESPONSE_num(crepmsg->response) > 1) {
  484. ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_RESPONSES_NOT_SUPPORTED);
  485. return 0;
  486. }
  487. /* TODO: handle potentially multiple CertResponses in CertRepMsg */
  488. crep = ossl_cmp_certrepmessage_get0_certresponse(crepmsg, rid);
  489. if (crep == NULL)
  490. return 0;
  491. if (!save_statusInfo(ctx, crep->status))
  492. return 0;
  493. if (rid == -1) {
  494. /* for OSSL_CMP_PKIBODY_P10CR learn CertReqId from response */
  495. rid = ossl_cmp_asn1_get_int(crep->certReqId);
  496. if (rid == -1) {
  497. ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID);
  498. return 0;
  499. }
  500. }
  501. if (ossl_cmp_pkisi_get_status(crep->status) == OSSL_CMP_PKISTATUS_waiting) {
  502. OSSL_CMP_MSG_free(*resp);
  503. *resp = NULL;
  504. if ((ret = poll_for_response(ctx, sleep, rid, resp, checkAfter)) != 0) {
  505. if (ret == -1) /* at this point implies sleep == 0 */
  506. return ret; /* waiting */
  507. goto retry; /* got ip/cp/kup, which may still indicate 'waiting' */
  508. } else {
  509. ERR_raise(ERR_LIB_CMP, CMP_R_POLLING_FAILED);
  510. return 0;
  511. }
  512. }
  513. cert = get1_cert_status(ctx, (*resp)->body->type, crep);
  514. if (cert == NULL) {
  515. ERR_add_error_data(1, "; cannot extract certificate from response");
  516. return 0;
  517. }
  518. if (!ossl_cmp_ctx_set0_newCert(ctx, cert))
  519. return 0;
  520. /*
  521. * if the CMP server returned certificates in the caPubs field, copy them
  522. * to the context so that they can be retrieved if necessary
  523. */
  524. if (crepmsg->caPubs != NULL
  525. && !ossl_cmp_ctx_set1_caPubs(ctx, crepmsg->caPubs))
  526. return 0;
  527. subj = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0);
  528. if (rkey != NULL
  529. /* X509_check_private_key() also works if rkey is just public key */
  530. && !(X509_check_private_key(ctx->newCert, rkey))) {
  531. fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_incorrectData;
  532. txt = "public key in new certificate does not match our enrollment key";
  533. /*-
  534. * not calling (void)ossl_cmp_exchange_error(ctx,
  535. * OSSL_CMP_PKISTATUS_rejection, fail_info, txt)
  536. * not throwing CMP_R_CERTIFICATE_NOT_ACCEPTED with txt
  537. * not returning 0
  538. * since we better leave this for the certConf_cb to decide
  539. */
  540. }
  541. /*
  542. * Execute the certification checking callback function,
  543. * which can determine whether to accept a newly enrolled certificate.
  544. * It may overrule the pre-decision reflected in 'fail_info' and '*txt'.
  545. */
  546. cb = ctx->certConf_cb != NULL ? ctx->certConf_cb : OSSL_CMP_certConf_cb;
  547. if ((fail_info = cb(ctx, ctx->newCert, fail_info, &txt)) != 0
  548. && txt == NULL)
  549. txt = "CMP client did not accept it";
  550. if (fail_info != 0) /* immediately log error before any certConf exchange */
  551. ossl_cmp_log1(ERROR, ctx,
  552. "rejecting newly enrolled cert with subject: %s", subj);
  553. /*
  554. * TODO: better move certConf exchange to do_certreq_seq() such that
  555. * also more low-level errors with CertReqMessages get reported to server
  556. */
  557. if (!ctx->disableConfirm
  558. && !ossl_cmp_hdr_has_implicitConfirm((*resp)->header)) {
  559. if (!ossl_cmp_exchange_certConf(ctx, fail_info, txt))
  560. ret = 0;
  561. }
  562. /* not throwing failure earlier as transfer_cb may call ERR_clear_error() */
  563. if (fail_info != 0) {
  564. ERR_raise_data(ERR_LIB_CMP, CMP_R_CERTIFICATE_NOT_ACCEPTED,
  565. "rejecting newly enrolled cert with subject: %s; %s",
  566. subj, txt);
  567. ret = 0;
  568. }
  569. OPENSSL_free(subj);
  570. return ret;
  571. }
  572. int OSSL_CMP_try_certreq(OSSL_CMP_CTX *ctx, int req_type,
  573. const OSSL_CRMF_MSG *crm, int *checkAfter)
  574. {
  575. OSSL_CMP_MSG *req = NULL;
  576. OSSL_CMP_MSG *rep = NULL;
  577. int is_p10 = req_type == OSSL_CMP_PKIBODY_P10CR;
  578. int rid = is_p10 ? -1 : OSSL_CMP_CERTREQID;
  579. int rep_type = is_p10 ? OSSL_CMP_PKIBODY_CP : req_type + 1;
  580. int res = 0;
  581. if (ctx == NULL) {
  582. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  583. return 0;
  584. }
  585. if (ctx->status != OSSL_CMP_PKISTATUS_waiting) { /* not polling already */
  586. ctx->status = -1;
  587. if (!ossl_cmp_ctx_set0_newCert(ctx, NULL))
  588. return 0;
  589. if (ctx->total_timeout > 0) /* else ctx->end_time is not used */
  590. ctx->end_time = time(NULL) + ctx->total_timeout;
  591. req = ossl_cmp_certreq_new(ctx, req_type, crm);
  592. if (req == NULL) /* also checks if all necessary options are set */
  593. return 0;
  594. if (!send_receive_check(ctx, req, &rep, rep_type))
  595. goto err;
  596. } else {
  597. if (req_type < 0)
  598. return ossl_cmp_exchange_error(ctx, OSSL_CMP_PKISTATUS_rejection,
  599. 0 /* TODO better fail_info value? */,
  600. "polling aborted", 0 /* errorCode */,
  601. "by application");
  602. res = poll_for_response(ctx, 0 /* no sleep */, rid, &rep, checkAfter);
  603. if (res <= 0) /* waiting or error */
  604. return res;
  605. }
  606. res = cert_response(ctx, 0 /* no sleep */, rid, &rep, checkAfter,
  607. req_type, rep_type);
  608. err:
  609. OSSL_CMP_MSG_free(req);
  610. OSSL_CMP_MSG_free(rep);
  611. return res;
  612. }
  613. /*-
  614. * Do the full sequence CR/IR/KUR/P10CR, CP/IP/KUP/CP,
  615. * certConf, PKIconf, and polling if required.
  616. * Will sleep as long as indicated by the server (according to checkAfter).
  617. * All enrollment options need to be present in the context.
  618. * TODO: another function to request two certificates at once should be created.
  619. * Returns pointer to received certificate, or NULL if none was received.
  620. */
  621. X509 *OSSL_CMP_exec_certreq(OSSL_CMP_CTX *ctx, int req_type,
  622. const OSSL_CRMF_MSG *crm)
  623. {
  624. OSSL_CMP_MSG *req = NULL;
  625. OSSL_CMP_MSG *rep = NULL;
  626. int is_p10 = req_type == OSSL_CMP_PKIBODY_P10CR;
  627. int rid = is_p10 ? -1 : OSSL_CMP_CERTREQID;
  628. int rep_type = is_p10 ? OSSL_CMP_PKIBODY_CP : req_type + 1;
  629. X509 *result = NULL;
  630. if (ctx == NULL) {
  631. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  632. return NULL;
  633. }
  634. if (is_p10 && crm != NULL) {
  635. ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
  636. return NULL;
  637. }
  638. ctx->status = -1;
  639. if (!ossl_cmp_ctx_set0_newCert(ctx, NULL))
  640. return NULL;
  641. if (ctx->total_timeout > 0) /* else ctx->end_time is not used */
  642. ctx->end_time = time(NULL) + ctx->total_timeout;
  643. /* OSSL_CMP_certreq_new() also checks if all necessary options are set */
  644. if ((req = ossl_cmp_certreq_new(ctx, req_type, crm)) == NULL)
  645. goto err;
  646. if (!send_receive_check(ctx, req, &rep, rep_type))
  647. goto err;
  648. if (cert_response(ctx, 1 /* sleep */, rid, &rep, NULL, req_type, rep_type)
  649. <= 0)
  650. goto err;
  651. result = ctx->newCert;
  652. err:
  653. OSSL_CMP_MSG_free(req);
  654. OSSL_CMP_MSG_free(rep);
  655. return result;
  656. }
  657. int OSSL_CMP_exec_RR_ses(OSSL_CMP_CTX *ctx)
  658. {
  659. OSSL_CMP_MSG *rr = NULL;
  660. OSSL_CMP_MSG *rp = NULL;
  661. const int num_RevDetails = 1;
  662. const int rsid = OSSL_CMP_REVREQSID;
  663. OSSL_CMP_REVREPCONTENT *rrep = NULL;
  664. OSSL_CMP_PKISI *si = NULL;
  665. char buf[OSSL_CMP_PKISI_BUFLEN];
  666. int ret = 0;
  667. if (ctx == NULL) {
  668. ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
  669. return 0;
  670. }
  671. if (ctx->oldCert == NULL && ctx->p10CSR == NULL) {
  672. ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_REFERENCE_CERT);
  673. return 0;
  674. }
  675. ctx->status = -1;
  676. /* OSSL_CMP_rr_new() also checks if all necessary options are set */
  677. if ((rr = ossl_cmp_rr_new(ctx)) == NULL)
  678. goto end;
  679. if (!send_receive_check(ctx, rr, &rp, OSSL_CMP_PKIBODY_RP))
  680. goto end;
  681. rrep = rp->body->value.rp;
  682. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  683. if (sk_OSSL_CMP_PKISI_num(rrep->status) != num_RevDetails) {
  684. ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_RP_COMPONENT_COUNT);
  685. goto end;
  686. }
  687. #else
  688. if (sk_OSSL_CMP_PKISI_num(rrep->status) < 1) {
  689. ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_RP_COMPONENT_COUNT);
  690. goto end;
  691. }
  692. #endif
  693. /* evaluate PKIStatus field */
  694. si = ossl_cmp_revrepcontent_get_pkisi(rrep, rsid);
  695. if (!save_statusInfo(ctx, si))
  696. goto err;
  697. switch (ossl_cmp_pkisi_get_status(si)) {
  698. case OSSL_CMP_PKISTATUS_accepted:
  699. ossl_cmp_info(ctx, "revocation accepted (PKIStatus=accepted)");
  700. ret = 1;
  701. break;
  702. case OSSL_CMP_PKISTATUS_grantedWithMods:
  703. ossl_cmp_info(ctx, "revocation accepted (PKIStatus=grantedWithMods)");
  704. ret = 1;
  705. break;
  706. case OSSL_CMP_PKISTATUS_rejection:
  707. ERR_raise(ERR_LIB_CMP, CMP_R_REQUEST_REJECTED_BY_SERVER);
  708. goto err;
  709. case OSSL_CMP_PKISTATUS_revocationWarning:
  710. ossl_cmp_info(ctx, "revocation accepted (PKIStatus=revocationWarning)");
  711. ret = 1;
  712. break;
  713. case OSSL_CMP_PKISTATUS_revocationNotification:
  714. /* interpretation as warning or error depends on CA */
  715. ossl_cmp_warn(ctx,
  716. "revocation accepted (PKIStatus=revocationNotification)");
  717. ret = 1;
  718. break;
  719. case OSSL_CMP_PKISTATUS_waiting:
  720. case OSSL_CMP_PKISTATUS_keyUpdateWarning:
  721. ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKISTATUS);
  722. goto err;
  723. default:
  724. ERR_raise(ERR_LIB_CMP, CMP_R_UNKNOWN_PKISTATUS);
  725. goto err;
  726. }
  727. /* check any pretent CertId in optional revCerts field */
  728. if (sk_OSSL_CRMF_CERTID_num(rrep->revCerts) >= 1) {
  729. OSSL_CRMF_CERTID *cid;
  730. OSSL_CRMF_CERTTEMPLATE *tmpl =
  731. sk_OSSL_CMP_REVDETAILS_value(rr->body->value.rr, rsid)->certDetails;
  732. const X509_NAME *issuer = OSSL_CRMF_CERTTEMPLATE_get0_issuer(tmpl);
  733. ASN1_INTEGER *serial = OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(tmpl);
  734. if (sk_OSSL_CRMF_CERTID_num(rrep->revCerts) != num_RevDetails) {
  735. ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_RP_COMPONENT_COUNT);
  736. ret = 0;
  737. goto err;
  738. }
  739. if ((cid = ossl_cmp_revrepcontent_get_CertId(rrep, rsid)) == NULL) {
  740. ret = 0;
  741. goto err;
  742. }
  743. if (X509_NAME_cmp(issuer, OSSL_CRMF_CERTID_get0_issuer(cid)) != 0) {
  744. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  745. ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_CERTID_IN_RP);
  746. ret = 0;
  747. goto err;
  748. #endif
  749. }
  750. if (ASN1_INTEGER_cmp(serial,
  751. OSSL_CRMF_CERTID_get0_serialNumber(cid)) != 0) {
  752. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  753. ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_SERIAL_IN_RP);
  754. ret = 0;
  755. goto err;
  756. #endif
  757. }
  758. }
  759. /* check number of any optionally present crls */
  760. if (rrep->crls != NULL && sk_X509_CRL_num(rrep->crls) != num_RevDetails) {
  761. ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_RP_COMPONENT_COUNT);
  762. ret = 0;
  763. goto err;
  764. }
  765. err:
  766. if (ret == 0
  767. && OSSL_CMP_CTX_snprint_PKIStatus(ctx, buf, sizeof(buf)) != NULL)
  768. ERR_add_error_data(1, buf);
  769. end:
  770. OSSL_CMP_MSG_free(rr);
  771. OSSL_CMP_MSG_free(rp);
  772. return ret;
  773. }
  774. STACK_OF(OSSL_CMP_ITAV) *OSSL_CMP_exec_GENM_ses(OSSL_CMP_CTX *ctx)
  775. {
  776. OSSL_CMP_MSG *genm;
  777. OSSL_CMP_MSG *genp = NULL;
  778. STACK_OF(OSSL_CMP_ITAV) *rcvd_itavs = NULL;
  779. if (ctx == NULL) {
  780. ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
  781. return 0;
  782. }
  783. ctx->status = -1;
  784. if ((genm = ossl_cmp_genm_new(ctx)) == NULL)
  785. goto err;
  786. if (!send_receive_check(ctx, genm, &genp, OSSL_CMP_PKIBODY_GENP))
  787. goto err;
  788. /* received stack of itavs not to be freed with the genp */
  789. rcvd_itavs = genp->body->value.genp;
  790. genp->body->value.genp = NULL;
  791. err:
  792. OSSL_CMP_MSG_free(genm);
  793. OSSL_CMP_MSG_free(genp);
  794. return rcvd_itavs; /* recv_itavs == NULL indicates an error */
  795. }