cmp_client.c 38 KB

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