1
0

cmp_client.c 38 KB

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