cmp_client.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. /*
  2. * Copyright 2007-2021 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. int is_enrollment = IS_CREP(expected_type)
  115. || expected_type == OSSL_CMP_PKIBODY_POLLREP
  116. || expected_type == OSSL_CMP_PKIBODY_PKICONF;
  117. const char *req_type_str =
  118. ossl_cmp_bodytype_to_string(ossl_cmp_msg_get_bodytype(req));
  119. const char *expected_type_str = ossl_cmp_bodytype_to_string(expected_type);
  120. int msg_timeout;
  121. int bt;
  122. time_t now = time(NULL);
  123. int time_left;
  124. OSSL_CMP_transfer_cb_t transfer_cb = ctx->transfer_cb;
  125. if (transfer_cb == NULL)
  126. transfer_cb = OSSL_CMP_MSG_http_perform;
  127. *rep = NULL;
  128. msg_timeout = ctx->msg_timeout; /* backup original value */
  129. if (is_enrollment && ctx->total_timeout > 0 /* timeout is not infinite */) {
  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 - time(NULL) < 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 = msg_timeout; /* restore original value */
  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: %ld",
  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)
  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. /* TODO: handle potentially multiple poll requests per message */
  232. if ((preq = ossl_cmp_pollReq_new(ctx, rid)) == NULL)
  233. goto err;
  234. if (!send_receive_check(ctx, preq, &prep, OSSL_CMP_PKIBODY_POLLREP))
  235. goto err;
  236. /* handle potential pollRep */
  237. if (ossl_cmp_msg_get_bodytype(prep) == OSSL_CMP_PKIBODY_POLLREP) {
  238. OSSL_CMP_POLLREPCONTENT *prc = prep->body->value.pollRep;
  239. OSSL_CMP_POLLREP *pollRep = NULL;
  240. int64_t check_after;
  241. char str[OSSL_CMP_PKISI_BUFLEN];
  242. int len;
  243. /* TODO: handle potentially multiple elements in pollRep */
  244. if (sk_OSSL_CMP_POLLREP_num(prc) > 1) {
  245. ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_RESPONSES_NOT_SUPPORTED);
  246. goto err;
  247. }
  248. pollRep = ossl_cmp_pollrepcontent_get0_pollrep(prc, rid);
  249. if (pollRep == NULL)
  250. goto err;
  251. if (!ASN1_INTEGER_get_int64(&check_after, pollRep->checkAfter)) {
  252. ERR_raise(ERR_LIB_CMP, CMP_R_BAD_CHECKAFTER_IN_POLLREP);
  253. goto err;
  254. }
  255. if (check_after < 0 || (uint64_t)check_after
  256. > (sleep ? ULONG_MAX / 1000 : INT_MAX)) {
  257. ERR_raise(ERR_LIB_CMP, CMP_R_CHECKAFTER_OUT_OF_RANGE);
  258. if (BIO_snprintf(str, OSSL_CMP_PKISI_BUFLEN, "value = %jd",
  259. check_after) >= 0)
  260. ERR_add_error_data(1, str);
  261. goto err;
  262. }
  263. if (ctx->total_timeout > 0) { /* timeout is not infinite */
  264. const int exp = 5; /* expected max time per msg round trip */
  265. int64_t time_left = (int64_t)(ctx->end_time - exp - time(NULL));
  266. if (time_left <= 0) {
  267. ERR_raise(ERR_LIB_CMP, CMP_R_TOTAL_TIMEOUT);
  268. goto err;
  269. }
  270. if (time_left < check_after)
  271. check_after = time_left;
  272. /* poll one last time just when timeout was reached */
  273. }
  274. if (pollRep->reason == NULL
  275. || (len = BIO_snprintf(str, OSSL_CMP_PKISI_BUFLEN,
  276. " with reason = '")) < 0) {
  277. *str = '\0';
  278. } else {
  279. char *text = ossl_sk_ASN1_UTF8STRING2text(pollRep->reason, ", ",
  280. sizeof(str) - len - 2);
  281. if (text == NULL
  282. || BIO_snprintf(str + len, sizeof(str) - len,
  283. "%s'", text) < 0)
  284. *str = '\0';
  285. OPENSSL_free(text);
  286. }
  287. ossl_cmp_log2(INFO, ctx,
  288. "received polling response%s; checkAfter = %ld seconds",
  289. str, check_after);
  290. OSSL_CMP_MSG_free(preq);
  291. preq = NULL;
  292. OSSL_CMP_MSG_free(prep);
  293. prep = NULL;
  294. if (sleep) {
  295. ossl_sleep((unsigned long)(1000 * check_after));
  296. } else {
  297. if (checkAfter != NULL)
  298. *checkAfter = (int)check_after;
  299. return -1; /* exits the loop */
  300. }
  301. } else {
  302. ossl_cmp_info(ctx, "received ip/cp/kup after polling");
  303. /* any other body type has been rejected by send_receive_check() */
  304. break;
  305. }
  306. }
  307. if (prep == NULL)
  308. goto err;
  309. OSSL_CMP_MSG_free(preq);
  310. *rep = prep;
  311. return 1;
  312. err:
  313. OSSL_CMP_MSG_free(preq);
  314. OSSL_CMP_MSG_free(prep);
  315. return 0;
  316. }
  317. /* Send certConf for IR, CR or KUR sequences and check response */
  318. int ossl_cmp_exchange_certConf(OSSL_CMP_CTX *ctx, int fail_info,
  319. const char *txt)
  320. {
  321. OSSL_CMP_MSG *certConf;
  322. OSSL_CMP_MSG *PKIconf = NULL;
  323. int res = 0;
  324. /* OSSL_CMP_certConf_new() also checks if all necessary options are set */
  325. if ((certConf = ossl_cmp_certConf_new(ctx, fail_info, txt)) == NULL)
  326. goto err;
  327. res = send_receive_check(ctx, certConf, &PKIconf, OSSL_CMP_PKIBODY_PKICONF);
  328. err:
  329. OSSL_CMP_MSG_free(certConf);
  330. OSSL_CMP_MSG_free(PKIconf);
  331. return res;
  332. }
  333. /* Send given error and check response */
  334. int ossl_cmp_exchange_error(OSSL_CMP_CTX *ctx, int status, int fail_info,
  335. const char *txt, int errorCode, const char *details)
  336. {
  337. OSSL_CMP_MSG *error = NULL;
  338. OSSL_CMP_PKISI *si = NULL;
  339. OSSL_CMP_MSG *PKIconf = NULL;
  340. int res = 0;
  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. EVP_PKEY *privkey;
  364. if (!ossl_assert(ctx != NULL && crep != NULL))
  365. return NULL;
  366. privkey = OSSL_CMP_CTX_get0_newPkey(ctx, 1);
  367. switch (ossl_cmp_pkisi_get_status(crep->status)) {
  368. case OSSL_CMP_PKISTATUS_waiting:
  369. ossl_cmp_err(ctx,
  370. "received \"waiting\" status for cert when actually aiming to extract cert");
  371. ERR_raise(ERR_LIB_CMP, CMP_R_ENCOUNTERED_WAITING);
  372. goto err;
  373. case OSSL_CMP_PKISTATUS_grantedWithMods:
  374. ossl_cmp_warn(ctx, "received \"grantedWithMods\" for certificate");
  375. break;
  376. case OSSL_CMP_PKISTATUS_accepted:
  377. break;
  378. /* get all information in case of a rejection before going to error */
  379. case OSSL_CMP_PKISTATUS_rejection:
  380. ossl_cmp_err(ctx, "received \"rejection\" status rather than cert");
  381. ERR_raise(ERR_LIB_CMP, CMP_R_REQUEST_REJECTED_BY_SERVER);
  382. goto err;
  383. case OSSL_CMP_PKISTATUS_revocationWarning:
  384. ossl_cmp_warn(ctx,
  385. "received \"revocationWarning\" - a revocation of the cert is imminent");
  386. break;
  387. case OSSL_CMP_PKISTATUS_revocationNotification:
  388. ossl_cmp_warn(ctx,
  389. "received \"revocationNotification\" - a revocation of the cert has occurred");
  390. break;
  391. case OSSL_CMP_PKISTATUS_keyUpdateWarning:
  392. if (bodytype != OSSL_CMP_PKIBODY_KUR) {
  393. ERR_raise(ERR_LIB_CMP, CMP_R_ENCOUNTERED_KEYUPDATEWARNING);
  394. goto err;
  395. }
  396. break;
  397. default:
  398. ossl_cmp_log1(ERROR, ctx,
  399. "received unsupported PKIStatus %d for certificate",
  400. ctx->status);
  401. ERR_raise(ERR_LIB_CMP, CMP_R_UNKNOWN_PKISTATUS);
  402. goto err;
  403. }
  404. crt = ossl_cmp_certresponse_get1_cert(crep, ctx, privkey);
  405. if (crt == NULL) /* according to PKIStatus, we can expect a cert */
  406. ERR_raise(ERR_LIB_CMP, CMP_R_CERTIFICATE_NOT_FOUND);
  407. return crt;
  408. err:
  409. if (OSSL_CMP_CTX_snprint_PKIStatus(ctx, buf, sizeof(buf)) != NULL)
  410. ERR_add_error_data(1, buf);
  411. return NULL;
  412. }
  413. /*-
  414. * Callback fn validating that the new certificate can be verified, using
  415. * ctx->certConf_cb_arg, which has been initialized using opt_out_trusted, and
  416. * ctx->untrusted, which at this point already contains msg->extraCerts.
  417. * Returns 0 on acceptance, else a bit field reflecting PKIFailureInfo.
  418. * Quoting from RFC 4210 section 5.1. Overall PKI Message:
  419. * The extraCerts field can contain certificates that may be useful to
  420. * the recipient. For example, this can be used by a CA or RA to
  421. * present an end entity with certificates that it needs to verify its
  422. * own new certificate (if, for example, the CA that issued the end
  423. * entity's certificate is not a root CA for the end entity). Note that
  424. * this field does not necessarily contain a certification path; the
  425. * recipient may have to sort, select from, or otherwise process the
  426. * extra certificates in order to use them.
  427. * Note: While often handy, there is no hard requirement by CMP that
  428. * an EE must be able to validate the certificates it gets enrolled.
  429. */
  430. int OSSL_CMP_certConf_cb(OSSL_CMP_CTX *ctx, X509 *cert, int fail_info,
  431. const char **text)
  432. {
  433. X509_STORE *out_trusted = OSSL_CMP_CTX_get_certConf_cb_arg(ctx);
  434. STACK_OF(X509) *chain = NULL;
  435. (void)text; /* make (artificial) use of var to prevent compiler warning */
  436. if (fail_info != 0) /* accept any error flagged by CMP core library */
  437. return fail_info;
  438. ossl_cmp_debug(ctx, "trying to build chain for newly enrolled cert");
  439. chain = ossl_cmp_build_cert_chain(ctx->libctx, ctx->propq,
  440. out_trusted /* may be NULL */,
  441. ctx->untrusted, cert);
  442. if (sk_X509_num(chain) > 0)
  443. X509_free(sk_X509_shift(chain)); /* remove leaf (EE) cert */
  444. if (out_trusted != NULL) {
  445. if (chain == NULL) {
  446. ossl_cmp_err(ctx, "failed building chain for newly enrolled cert");
  447. fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_incorrectData;
  448. } else {
  449. ossl_cmp_debug(ctx,
  450. "succeeded building proper chain for newly enrolled cert");
  451. }
  452. } else if (chain == NULL) {
  453. ossl_cmp_warn(ctx, "could not build approximate chain for newly enrolled cert, resorting to received extraCerts");
  454. chain = OSSL_CMP_CTX_get1_extraCertsIn(ctx);
  455. } else {
  456. ossl_cmp_debug(ctx,
  457. "success building approximate chain for newly enrolled cert");
  458. }
  459. (void)ossl_cmp_ctx_set1_newChain(ctx, chain);
  460. sk_X509_pop_free(chain, X509_free);
  461. return fail_info;
  462. }
  463. /*-
  464. * Perform the generic handling of certificate responses for IR/CR/KUR/P10CR.
  465. * Returns -1 on receiving pollRep if sleep == 0, setting the checkAfter value.
  466. * Returns 1 on success and provides the received PKIMESSAGE in *resp.
  467. * Returns 0 on error (which includes the case that timeout has been reached).
  468. * Regardless of success, caller is responsible for freeing *resp (unless NULL).
  469. */
  470. static int cert_response(OSSL_CMP_CTX *ctx, int sleep, int rid,
  471. OSSL_CMP_MSG **resp, int *checkAfter,
  472. int req_type, int expected_type)
  473. {
  474. EVP_PKEY *rkey = OSSL_CMP_CTX_get0_newPkey(ctx /* may be NULL */, 0);
  475. int fail_info = 0; /* no failure */
  476. const char *txt = NULL;
  477. OSSL_CMP_CERTREPMESSAGE *crepmsg;
  478. OSSL_CMP_CERTRESPONSE *crep;
  479. OSSL_CMP_certConf_cb_t cb;
  480. X509 *cert;
  481. char *subj = NULL;
  482. int ret = 1;
  483. if (!ossl_assert(ctx != NULL))
  484. return 0;
  485. retry:
  486. crepmsg = (*resp)->body->value.ip; /* same for cp and kup */
  487. if (sk_OSSL_CMP_CERTRESPONSE_num(crepmsg->response) > 1) {
  488. ERR_raise(ERR_LIB_CMP, CMP_R_MULTIPLE_RESPONSES_NOT_SUPPORTED);
  489. return 0;
  490. }
  491. /* TODO: handle potentially multiple CertResponses in CertRepMsg */
  492. crep = ossl_cmp_certrepmessage_get0_certresponse(crepmsg, rid);
  493. if (crep == NULL)
  494. return 0;
  495. if (!save_statusInfo(ctx, crep->status))
  496. return 0;
  497. if (rid == -1) {
  498. /* for OSSL_CMP_PKIBODY_P10CR learn CertReqId from response */
  499. rid = ossl_cmp_asn1_get_int(crep->certReqId);
  500. if (rid == -1) {
  501. ERR_raise(ERR_LIB_CMP, CMP_R_BAD_REQUEST_ID);
  502. return 0;
  503. }
  504. }
  505. if (ossl_cmp_pkisi_get_status(crep->status) == OSSL_CMP_PKISTATUS_waiting) {
  506. OSSL_CMP_MSG_free(*resp);
  507. *resp = NULL;
  508. if ((ret = poll_for_response(ctx, sleep, rid, resp, checkAfter)) != 0) {
  509. if (ret == -1) /* at this point implies sleep == 0 */
  510. return ret; /* waiting */
  511. goto retry; /* got ip/cp/kup, which may still indicate 'waiting' */
  512. } else {
  513. ERR_raise(ERR_LIB_CMP, CMP_R_POLLING_FAILED);
  514. return 0;
  515. }
  516. }
  517. cert = get1_cert_status(ctx, (*resp)->body->type, crep);
  518. if (cert == NULL) {
  519. ERR_add_error_data(1, "; cannot extract certificate from response");
  520. return 0;
  521. }
  522. if (!ossl_cmp_ctx_set0_newCert(ctx, cert))
  523. return 0;
  524. /*
  525. * if the CMP server returned certificates in the caPubs field, copy them
  526. * to the context so that they can be retrieved if necessary
  527. */
  528. if (crepmsg->caPubs != NULL
  529. && !ossl_cmp_ctx_set1_caPubs(ctx, crepmsg->caPubs))
  530. return 0;
  531. subj = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0);
  532. if (rkey != NULL
  533. /* X509_check_private_key() also works if rkey is just public key */
  534. && !(X509_check_private_key(ctx->newCert, rkey))) {
  535. fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_incorrectData;
  536. txt = "public key in new certificate does not match our enrollment key";
  537. /*-
  538. * not calling (void)ossl_cmp_exchange_error(ctx,
  539. * OSSL_CMP_PKISTATUS_rejection, fail_info, txt)
  540. * not throwing CMP_R_CERTIFICATE_NOT_ACCEPTED with txt
  541. * not returning 0
  542. * since we better leave this for the certConf_cb to decide
  543. */
  544. }
  545. /*
  546. * Execute the certification checking callback function,
  547. * which can determine whether to accept a newly enrolled certificate.
  548. * It may overrule the pre-decision reflected in 'fail_info' and '*txt'.
  549. */
  550. cb = ctx->certConf_cb != NULL ? ctx->certConf_cb : OSSL_CMP_certConf_cb;
  551. if ((fail_info = cb(ctx, ctx->newCert, fail_info, &txt)) != 0
  552. && txt == NULL)
  553. txt = "CMP client did not accept it";
  554. if (fail_info != 0) /* immediately log error before any certConf exchange */
  555. ossl_cmp_log1(ERROR, ctx,
  556. "rejecting newly enrolled cert with subject: %s", subj);
  557. /*
  558. * TODO: better move certConf exchange to do_certreq_seq() such that
  559. * also more low-level errors with CertReqMessages get reported to server
  560. */
  561. if (!ctx->disableConfirm
  562. && !ossl_cmp_hdr_has_implicitConfirm((*resp)->header)) {
  563. if (!ossl_cmp_exchange_certConf(ctx, fail_info, txt))
  564. ret = 0;
  565. }
  566. /* not throwing failure earlier as transfer_cb may call ERR_clear_error() */
  567. if (fail_info != 0) {
  568. ERR_raise_data(ERR_LIB_CMP, CMP_R_CERTIFICATE_NOT_ACCEPTED,
  569. "rejecting newly enrolled cert with subject: %s; %s",
  570. subj, txt);
  571. ret = 0;
  572. }
  573. OPENSSL_free(subj);
  574. return ret;
  575. }
  576. static int initial_certreq(OSSL_CMP_CTX *ctx,
  577. int req_type, const OSSL_CRMF_MSG *crm,
  578. OSSL_CMP_MSG **p_rep, int rep_type)
  579. {
  580. OSSL_CMP_MSG *req;
  581. int res;
  582. ctx->status = -1;
  583. if (!ossl_cmp_ctx_set0_newCert(ctx, NULL))
  584. return 0;
  585. if (ctx->total_timeout > 0) /* else ctx->end_time is not used */
  586. ctx->end_time = time(NULL) + ctx->total_timeout;
  587. /* also checks if all necessary options are set */
  588. if ((req = ossl_cmp_certreq_new(ctx, req_type, crm)) == NULL)
  589. return 0;
  590. res = send_receive_check(ctx, req, p_rep, rep_type);
  591. OSSL_CMP_MSG_free(req);
  592. return res;
  593. }
  594. int OSSL_CMP_try_certreq(OSSL_CMP_CTX *ctx, int req_type,
  595. const OSSL_CRMF_MSG *crm, int *checkAfter)
  596. {
  597. OSSL_CMP_MSG *rep = NULL;
  598. int is_p10 = req_type == OSSL_CMP_PKIBODY_P10CR;
  599. int rid = is_p10 ? -1 : OSSL_CMP_CERTREQID;
  600. int rep_type = is_p10 ? OSSL_CMP_PKIBODY_CP : req_type + 1;
  601. int res = 0;
  602. if (ctx == NULL) {
  603. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  604. return 0;
  605. }
  606. if (ctx->status != OSSL_CMP_PKISTATUS_waiting) { /* not polling already */
  607. if (!initial_certreq(ctx, req_type, crm, &rep, rep_type))
  608. goto err;
  609. } else {
  610. if (req_type < 0)
  611. return ossl_cmp_exchange_error(ctx, OSSL_CMP_PKISTATUS_rejection,
  612. 0 /* TODO better fail_info value? */,
  613. "polling aborted", 0 /* errorCode */,
  614. "by application");
  615. res = poll_for_response(ctx, 0 /* no sleep */, rid, &rep, checkAfter);
  616. if (res <= 0) /* waiting or error */
  617. return res;
  618. }
  619. res = cert_response(ctx, 0 /* no sleep */, rid, &rep, checkAfter,
  620. req_type, rep_type);
  621. err:
  622. OSSL_CMP_MSG_free(rep);
  623. return res;
  624. }
  625. /*-
  626. * Do the full sequence CR/IR/KUR/P10CR, CP/IP/KUP/CP,
  627. * certConf, PKIconf, and polling if required.
  628. * Will sleep as long as indicated by the server (according to checkAfter).
  629. * All enrollment options need to be present in the context.
  630. * TODO: another function to request two certificates at once should be created.
  631. * Returns pointer to received certificate, or NULL if none was received.
  632. */
  633. X509 *OSSL_CMP_exec_certreq(OSSL_CMP_CTX *ctx, int req_type,
  634. const OSSL_CRMF_MSG *crm)
  635. {
  636. OSSL_CMP_MSG *rep = NULL;
  637. int is_p10 = req_type == OSSL_CMP_PKIBODY_P10CR;
  638. int rid = is_p10 ? -1 : OSSL_CMP_CERTREQID;
  639. int rep_type = is_p10 ? OSSL_CMP_PKIBODY_CP : req_type + 1;
  640. X509 *result = NULL;
  641. if (ctx == NULL) {
  642. ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
  643. return NULL;
  644. }
  645. if (!initial_certreq(ctx, req_type, crm, &rep, rep_type))
  646. goto err;
  647. if (cert_response(ctx, 1 /* sleep */, rid, &rep, NULL, req_type, rep_type)
  648. <= 0)
  649. goto err;
  650. result = ctx->newCert;
  651. err:
  652. OSSL_CMP_MSG_free(rep);
  653. return result;
  654. }
  655. int OSSL_CMP_exec_RR_ses(OSSL_CMP_CTX *ctx)
  656. {
  657. OSSL_CMP_MSG *rr = NULL;
  658. OSSL_CMP_MSG *rp = NULL;
  659. const int num_RevDetails = 1;
  660. const int rsid = OSSL_CMP_REVREQSID;
  661. OSSL_CMP_REVREPCONTENT *rrep = NULL;
  662. OSSL_CMP_PKISI *si = NULL;
  663. char buf[OSSL_CMP_PKISI_BUFLEN];
  664. int ret = 0;
  665. if (ctx == NULL) {
  666. ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
  667. return 0;
  668. }
  669. if (ctx->oldCert == NULL && ctx->p10CSR == NULL) {
  670. ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_REFERENCE_CERT);
  671. return 0;
  672. }
  673. ctx->status = -1;
  674. /* OSSL_CMP_rr_new() also checks if all necessary options are set */
  675. if ((rr = ossl_cmp_rr_new(ctx)) == NULL)
  676. goto end;
  677. if (!send_receive_check(ctx, rr, &rp, OSSL_CMP_PKIBODY_RP))
  678. goto end;
  679. rrep = rp->body->value.rp;
  680. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  681. if (sk_OSSL_CMP_PKISI_num(rrep->status) != num_RevDetails) {
  682. ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_RP_COMPONENT_COUNT);
  683. goto end;
  684. }
  685. #else
  686. if (sk_OSSL_CMP_PKISI_num(rrep->status) < 1) {
  687. ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_RP_COMPONENT_COUNT);
  688. goto end;
  689. }
  690. #endif
  691. /* evaluate PKIStatus field */
  692. si = ossl_cmp_revrepcontent_get_pkisi(rrep, rsid);
  693. if (!save_statusInfo(ctx, si))
  694. goto err;
  695. switch (ossl_cmp_pkisi_get_status(si)) {
  696. case OSSL_CMP_PKISTATUS_accepted:
  697. ossl_cmp_info(ctx, "revocation accepted (PKIStatus=accepted)");
  698. ret = 1;
  699. break;
  700. case OSSL_CMP_PKISTATUS_grantedWithMods:
  701. ossl_cmp_info(ctx, "revocation accepted (PKIStatus=grantedWithMods)");
  702. ret = 1;
  703. break;
  704. case OSSL_CMP_PKISTATUS_rejection:
  705. ERR_raise(ERR_LIB_CMP, CMP_R_REQUEST_REJECTED_BY_SERVER);
  706. goto err;
  707. case OSSL_CMP_PKISTATUS_revocationWarning:
  708. ossl_cmp_info(ctx, "revocation accepted (PKIStatus=revocationWarning)");
  709. ret = 1;
  710. break;
  711. case OSSL_CMP_PKISTATUS_revocationNotification:
  712. /* interpretation as warning or error depends on CA */
  713. ossl_cmp_warn(ctx,
  714. "revocation accepted (PKIStatus=revocationNotification)");
  715. ret = 1;
  716. break;
  717. case OSSL_CMP_PKISTATUS_waiting:
  718. case OSSL_CMP_PKISTATUS_keyUpdateWarning:
  719. ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKISTATUS);
  720. goto err;
  721. default:
  722. ERR_raise(ERR_LIB_CMP, CMP_R_UNKNOWN_PKISTATUS);
  723. goto err;
  724. }
  725. /* check any present CertId in optional revCerts field */
  726. if (sk_OSSL_CRMF_CERTID_num(rrep->revCerts) >= 1) {
  727. OSSL_CRMF_CERTID *cid;
  728. OSSL_CRMF_CERTTEMPLATE *tmpl =
  729. sk_OSSL_CMP_REVDETAILS_value(rr->body->value.rr, rsid)->certDetails;
  730. const X509_NAME *issuer = OSSL_CRMF_CERTTEMPLATE_get0_issuer(tmpl);
  731. ASN1_INTEGER *serial = OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(tmpl);
  732. if (sk_OSSL_CRMF_CERTID_num(rrep->revCerts) != num_RevDetails) {
  733. ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_RP_COMPONENT_COUNT);
  734. ret = 0;
  735. goto err;
  736. }
  737. if ((cid = ossl_cmp_revrepcontent_get_CertId(rrep, rsid)) == NULL) {
  738. ret = 0;
  739. goto err;
  740. }
  741. if (X509_NAME_cmp(issuer, OSSL_CRMF_CERTID_get0_issuer(cid)) != 0) {
  742. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  743. ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_CERTID_IN_RP);
  744. ret = 0;
  745. goto err;
  746. #endif
  747. }
  748. if (ASN1_INTEGER_cmp(serial,
  749. OSSL_CRMF_CERTID_get0_serialNumber(cid)) != 0) {
  750. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  751. ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_SERIAL_IN_RP);
  752. ret = 0;
  753. goto err;
  754. #endif
  755. }
  756. }
  757. /* check number of any optionally present crls */
  758. if (rrep->crls != NULL && sk_X509_CRL_num(rrep->crls) != num_RevDetails) {
  759. ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_RP_COMPONENT_COUNT);
  760. ret = 0;
  761. goto err;
  762. }
  763. err:
  764. if (ret == 0
  765. && OSSL_CMP_CTX_snprint_PKIStatus(ctx, buf, sizeof(buf)) != NULL)
  766. ERR_add_error_data(1, buf);
  767. end:
  768. OSSL_CMP_MSG_free(rr);
  769. OSSL_CMP_MSG_free(rp);
  770. return ret;
  771. }
  772. STACK_OF(OSSL_CMP_ITAV) *OSSL_CMP_exec_GENM_ses(OSSL_CMP_CTX *ctx)
  773. {
  774. OSSL_CMP_MSG *genm;
  775. OSSL_CMP_MSG *genp = NULL;
  776. STACK_OF(OSSL_CMP_ITAV) *rcvd_itavs = NULL;
  777. if (ctx == NULL) {
  778. ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
  779. return 0;
  780. }
  781. ctx->status = -1;
  782. if ((genm = ossl_cmp_genm_new(ctx)) == NULL)
  783. goto err;
  784. if (!send_receive_check(ctx, genm, &genp, OSSL_CMP_PKIBODY_GENP))
  785. goto err;
  786. /* received stack of itavs not to be freed with the genp */
  787. rcvd_itavs = genp->body->value.genp;
  788. genp->body->value.genp = NULL;
  789. err:
  790. OSSL_CMP_MSG_free(genm);
  791. OSSL_CMP_MSG_free(genp);
  792. return rcvd_itavs; /* recv_itavs == NULL indicates an error */
  793. }