cmp_client.c 33 KB

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