cmp_client.c 33 KB

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