cmp_client.c 37 KB

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