cmp_client_test.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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 "helpers/cmp_testlib.h"
  12. #include "cmp_mock_srv.h"
  13. static const char *server_key_f;
  14. static const char *server_cert_f;
  15. static const char *client_key_f;
  16. static const char *client_cert_f;
  17. static const char *pkcs10_f;
  18. typedef struct test_fixture {
  19. const char *test_case_name;
  20. OSSL_CMP_CTX *cmp_ctx;
  21. OSSL_CMP_SRV_CTX *srv_ctx;
  22. int req_type;
  23. int expected;
  24. STACK_OF(X509) *caPubs;
  25. } CMP_SES_TEST_FIXTURE;
  26. static OSSL_LIB_CTX *libctx = NULL;
  27. static OSSL_PROVIDER *default_null_provider = NULL, *provider = NULL;
  28. static EVP_PKEY *server_key = NULL;
  29. static X509 *server_cert = NULL;
  30. static EVP_PKEY *client_key = NULL;
  31. static X509 *client_cert = NULL;
  32. static unsigned char ref[CMP_TEST_REFVALUE_LENGTH];
  33. /*
  34. * For these unit tests, the client abandons message protection, and for
  35. * error messages the mock server does so as well.
  36. * Message protection and verification is tested in cmp_lib_test.c
  37. */
  38. static void tear_down(CMP_SES_TEST_FIXTURE *fixture)
  39. {
  40. OSSL_CMP_CTX_free(fixture->cmp_ctx);
  41. ossl_cmp_mock_srv_free(fixture->srv_ctx);
  42. sk_X509_free(fixture->caPubs);
  43. OPENSSL_free(fixture);
  44. }
  45. static CMP_SES_TEST_FIXTURE *set_up(const char *const test_case_name)
  46. {
  47. CMP_SES_TEST_FIXTURE *fixture;
  48. OSSL_CMP_CTX *srv_cmp_ctx = NULL;
  49. OSSL_CMP_CTX *ctx = NULL; /* for client */
  50. if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
  51. return NULL;
  52. fixture->test_case_name = test_case_name;
  53. if (!TEST_ptr(fixture->srv_ctx = ossl_cmp_mock_srv_new(libctx, NULL))
  54. || !OSSL_CMP_SRV_CTX_set_accept_unprotected(fixture->srv_ctx, 1)
  55. || !ossl_cmp_mock_srv_set1_refCert(fixture->srv_ctx, client_cert)
  56. || !ossl_cmp_mock_srv_set1_certOut(fixture->srv_ctx, client_cert)
  57. || (srv_cmp_ctx =
  58. OSSL_CMP_SRV_CTX_get0_cmp_ctx(fixture->srv_ctx)) == NULL
  59. || !OSSL_CMP_CTX_set1_cert(srv_cmp_ctx, server_cert)
  60. || !OSSL_CMP_CTX_set1_pkey(srv_cmp_ctx, server_key))
  61. goto err;
  62. if (!TEST_ptr(fixture->cmp_ctx = ctx = OSSL_CMP_CTX_new(libctx, NULL))
  63. || !OSSL_CMP_CTX_set_log_cb(fixture->cmp_ctx, print_to_bio_out)
  64. || !OSSL_CMP_CTX_set_transfer_cb(ctx, OSSL_CMP_CTX_server_perform)
  65. || !OSSL_CMP_CTX_set_transfer_cb_arg(ctx, fixture->srv_ctx)
  66. || !OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_SEND, 1)
  67. || !OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS, 1)
  68. || !OSSL_CMP_CTX_set1_oldCert(ctx, client_cert)
  69. || !OSSL_CMP_CTX_set1_pkey(ctx, client_key)
  70. /* client_key is by default used also for newPkey */
  71. || !OSSL_CMP_CTX_set1_srvCert(ctx, server_cert)
  72. || !OSSL_CMP_CTX_set1_referenceValue(ctx, ref, sizeof(ref)))
  73. goto err;
  74. fixture->req_type = -1;
  75. return fixture;
  76. err:
  77. tear_down(fixture);
  78. return NULL;
  79. }
  80. static int execute_exec_RR_ses_test(CMP_SES_TEST_FIXTURE *fixt)
  81. {
  82. return TEST_int_eq(OSSL_CMP_CTX_get_status(fixt->cmp_ctx),
  83. OSSL_CMP_PKISTATUS_unspecified)
  84. && TEST_int_eq(OSSL_CMP_exec_RR_ses(fixt->cmp_ctx),
  85. fixt->expected == OSSL_CMP_PKISTATUS_accepted)
  86. && TEST_int_eq(OSSL_CMP_CTX_get_status(fixt->cmp_ctx), fixt->expected);
  87. }
  88. static int execute_exec_GENM_ses_test_single(CMP_SES_TEST_FIXTURE *fixture)
  89. {
  90. OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
  91. ASN1_OBJECT *type = OBJ_txt2obj("1.3.6.1.5.5.7.4.2", 1);
  92. OSSL_CMP_ITAV *itav = OSSL_CMP_ITAV_create(type, NULL);
  93. STACK_OF(OSSL_CMP_ITAV) *itavs;
  94. OSSL_CMP_CTX_push0_genm_ITAV(ctx, itav);
  95. itavs = OSSL_CMP_exec_GENM_ses(ctx);
  96. sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
  97. return TEST_int_eq(OSSL_CMP_CTX_get_status(ctx), fixture->expected)
  98. && fixture->expected == OSSL_CMP_PKISTATUS_accepted ?
  99. TEST_ptr(itavs) : TEST_ptr_null(itavs);
  100. }
  101. static int execute_exec_GENM_ses_test(CMP_SES_TEST_FIXTURE *fixture)
  102. {
  103. return execute_exec_GENM_ses_test_single(fixture)
  104. && OSSL_CMP_CTX_reinit(fixture->cmp_ctx)
  105. && execute_exec_GENM_ses_test_single(fixture);
  106. }
  107. static int execute_exec_certrequest_ses_test(CMP_SES_TEST_FIXTURE *fixture)
  108. {
  109. OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
  110. X509 *res = OSSL_CMP_exec_certreq(ctx, fixture->req_type, NULL);
  111. int status = OSSL_CMP_CTX_get_status(ctx);
  112. OSSL_CMP_CTX_print_errors(ctx);
  113. if (!TEST_int_eq(status, fixture->expected)
  114. && !(fixture->expected == OSSL_CMP_PKISTATUS_waiting
  115. && TEST_int_eq(status, OSSL_CMP_PKISTATUS_trans)))
  116. return 0;
  117. if (fixture->expected != OSSL_CMP_PKISTATUS_accepted)
  118. return TEST_ptr_null(res);
  119. if (!TEST_ptr(res) || !TEST_int_eq(X509_cmp(res, client_cert), 0))
  120. return 0;
  121. if (fixture->caPubs != NULL) {
  122. STACK_OF(X509) *caPubs = OSSL_CMP_CTX_get1_caPubs(fixture->cmp_ctx);
  123. int ret = TEST_int_eq(STACK_OF_X509_cmp(fixture->caPubs, caPubs), 0);
  124. OSSL_STACK_OF_X509_free(caPubs);
  125. return ret;
  126. }
  127. return 1;
  128. }
  129. static int test_exec_RR_ses(int request_error)
  130. {
  131. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  132. if (request_error)
  133. OSSL_CMP_CTX_set1_oldCert(fixture->cmp_ctx, NULL);
  134. fixture->expected = request_error ? OSSL_CMP_PKISTATUS_request
  135. : OSSL_CMP_PKISTATUS_accepted;
  136. EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
  137. return result;
  138. }
  139. static int test_exec_RR_ses_ok(void)
  140. {
  141. return test_exec_RR_ses(0);
  142. }
  143. static int test_exec_RR_ses_request_error(void)
  144. {
  145. return test_exec_RR_ses(1);
  146. }
  147. static int test_exec_RR_ses_receive_error(void)
  148. {
  149. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  150. ossl_cmp_mock_srv_set_statusInfo(fixture->srv_ctx,
  151. OSSL_CMP_PKISTATUS_rejection,
  152. OSSL_CMP_CTX_FAILINFO_signerNotTrusted,
  153. "test string");
  154. ossl_cmp_mock_srv_set_sendError(fixture->srv_ctx, OSSL_CMP_PKIBODY_RR);
  155. fixture->expected = OSSL_CMP_PKISTATUS_rejection;
  156. EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
  157. return result;
  158. }
  159. static int test_exec_IR_ses(void)
  160. {
  161. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  162. fixture->req_type = OSSL_CMP_PKIBODY_IR;
  163. fixture->expected = OSSL_CMP_PKISTATUS_accepted;
  164. fixture->caPubs = sk_X509_new_null();
  165. sk_X509_push(fixture->caPubs, server_cert);
  166. sk_X509_push(fixture->caPubs, server_cert);
  167. ossl_cmp_mock_srv_set1_caPubsOut(fixture->srv_ctx, fixture->caPubs);
  168. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  169. return result;
  170. }
  171. static int test_exec_REQ_ses_poll(int req_type, int check_after,
  172. int poll_count, int total_timeout,
  173. int expect)
  174. {
  175. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  176. fixture->req_type = req_type;
  177. fixture->expected = expect;
  178. ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, check_after);
  179. ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, poll_count);
  180. OSSL_CMP_CTX_set_option(fixture->cmp_ctx,
  181. OSSL_CMP_OPT_TOTAL_TIMEOUT, total_timeout);
  182. if (req_type == OSSL_CMP_PKIBODY_IR) {
  183. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  184. } else if (req_type == OSSL_CMP_PKIBODY_GENM) {
  185. EXECUTE_TEST(execute_exec_GENM_ses_test, tear_down);
  186. }
  187. return result;
  188. }
  189. static int checkAfter = 1;
  190. static int test_exec_IR_ses_poll_ok(void)
  191. {
  192. return test_exec_REQ_ses_poll(OSSL_CMP_PKIBODY_IR, checkAfter, 2, 0,
  193. OSSL_CMP_PKISTATUS_accepted);
  194. }
  195. static int test_exec_IR_ses_poll_no_timeout(void)
  196. {
  197. return test_exec_REQ_ses_poll(OSSL_CMP_PKIBODY_IR, checkAfter,
  198. 2 /* pollCount */,
  199. checkAfter + 14, /* usually 4 is sufficient */
  200. OSSL_CMP_PKISTATUS_accepted);
  201. }
  202. static int test_exec_IR_ses_poll_total_timeout(void)
  203. {
  204. return !test_exec_REQ_ses_poll(OSSL_CMP_PKIBODY_IR, checkAfter + 1,
  205. 3 /* pollCount */, checkAfter + 6,
  206. OSSL_CMP_PKISTATUS_waiting);
  207. }
  208. static int test_exec_CR_ses(int implicit_confirm, int granted, int reject)
  209. {
  210. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  211. fixture->req_type = OSSL_CMP_PKIBODY_CR;
  212. OSSL_CMP_CTX_set_option(fixture->cmp_ctx,
  213. OSSL_CMP_OPT_IMPLICIT_CONFIRM, implicit_confirm);
  214. OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(fixture->srv_ctx, granted);
  215. ossl_cmp_mock_srv_set_sendError(fixture->srv_ctx,
  216. reject ? OSSL_CMP_PKIBODY_CERTCONF : -1);
  217. fixture->expected = reject ? OSSL_CMP_PKISTATUS_rejection
  218. : OSSL_CMP_PKISTATUS_accepted;
  219. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  220. return result;
  221. }
  222. static int test_exec_CR_ses_explicit_confirm(void)
  223. {
  224. return test_exec_CR_ses(0, 0, 0)
  225. && test_exec_CR_ses(0, 0, 1 /* reject */);
  226. }
  227. static int test_exec_CR_ses_implicit_confirm(void)
  228. {
  229. return test_exec_CR_ses(1, 0, 0)
  230. && test_exec_CR_ses(1, 1 /* granted */, 0);
  231. }
  232. static int test_exec_KUR_ses(int transfer_error, int pubkey, int raverified)
  233. {
  234. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  235. fixture->req_type = OSSL_CMP_PKIBODY_KUR;
  236. /* ctx->oldCert has already been set */
  237. if (transfer_error)
  238. OSSL_CMP_CTX_set_transfer_cb_arg(fixture->cmp_ctx, NULL);
  239. if (pubkey) {
  240. EVP_PKEY *key = raverified /* wrong key */ ? server_key : client_key;
  241. EVP_PKEY_up_ref(key);
  242. OSSL_CMP_CTX_set0_newPkey(fixture->cmp_ctx, 0 /* not priv */, key);
  243. OSSL_CMP_SRV_CTX_set_accept_raverified(fixture->srv_ctx, 1);
  244. }
  245. if (pubkey || raverified)
  246. OSSL_CMP_CTX_set_option(fixture->cmp_ctx, OSSL_CMP_OPT_POPO_METHOD,
  247. OSSL_CRMF_POPO_RAVERIFIED);
  248. fixture->expected = transfer_error ? OSSL_CMP_PKISTATUS_trans :
  249. raverified ? OSSL_CMP_PKISTATUS_rejection : OSSL_CMP_PKISTATUS_accepted;
  250. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  251. return result;
  252. }
  253. static int test_exec_KUR_ses_ok(void)
  254. {
  255. return test_exec_KUR_ses(0, 0, 0);
  256. }
  257. static int test_exec_KUR_ses_transfer_error(void)
  258. {
  259. return test_exec_KUR_ses(1, 0, 0);
  260. }
  261. static int test_exec_KUR_ses_wrong_popo(void)
  262. {
  263. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION /* cf ossl_cmp_verify_popo() */
  264. return test_exec_KUR_ses(0, 0, 1);
  265. #else
  266. return 1;
  267. #endif
  268. }
  269. static int test_exec_KUR_ses_pub(void)
  270. {
  271. return test_exec_KUR_ses(0, 1, 0);
  272. }
  273. static int test_exec_KUR_ses_wrong_pub(void)
  274. {
  275. return test_exec_KUR_ses(0, 1, 1);
  276. }
  277. static int test_certConf_cb(OSSL_CMP_CTX *ctx, X509 *cert, int fail_info,
  278. const char **txt)
  279. {
  280. int *reject = OSSL_CMP_CTX_get_certConf_cb_arg(ctx);
  281. if (*reject) {
  282. *txt = "not to my taste";
  283. fail_info = OSSL_CMP_PKIFAILUREINFO_badCertTemplate;
  284. }
  285. return fail_info;
  286. }
  287. static int test_exec_P10CR_ses(int reject)
  288. {
  289. OSSL_CMP_CTX *ctx;
  290. X509_REQ *csr = NULL;
  291. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  292. fixture->req_type = OSSL_CMP_PKIBODY_P10CR;
  293. fixture->expected = reject ? OSSL_CMP_PKISTATUS_rejection
  294. : OSSL_CMP_PKISTATUS_accepted;
  295. ctx = fixture->cmp_ctx;
  296. if (!TEST_ptr(csr = load_csr_der(pkcs10_f, libctx))
  297. || !TEST_true(OSSL_CMP_CTX_set1_p10CSR(ctx, csr))
  298. || !TEST_true(OSSL_CMP_CTX_set_certConf_cb(ctx, test_certConf_cb))
  299. || !TEST_true(OSSL_CMP_CTX_set_certConf_cb_arg(ctx, &reject))) {
  300. tear_down(fixture);
  301. fixture = NULL;
  302. }
  303. X509_REQ_free(csr);
  304. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  305. return result;
  306. }
  307. static int test_exec_P10CR_ses_ok(void)
  308. {
  309. return test_exec_P10CR_ses(0);
  310. }
  311. static int test_exec_P10CR_ses_reject(void)
  312. {
  313. return test_exec_P10CR_ses(1);
  314. }
  315. static int execute_try_certreq_poll_test(CMP_SES_TEST_FIXTURE *fixture)
  316. {
  317. OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
  318. int check_after;
  319. const int CHECK_AFTER = 0;
  320. const int TYPE = OSSL_CMP_PKIBODY_KUR;
  321. ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
  322. ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
  323. return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
  324. && check_after == CHECK_AFTER
  325. && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
  326. && TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
  327. && check_after == CHECK_AFTER
  328. && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
  329. && TEST_int_eq(fixture->expected,
  330. OSSL_CMP_try_certreq(ctx, TYPE, NULL, NULL))
  331. && TEST_int_eq(0,
  332. X509_cmp(OSSL_CMP_CTX_get0_newCert(ctx), client_cert));
  333. }
  334. static int test_try_certreq_poll(void)
  335. {
  336. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  337. fixture->expected = 1;
  338. EXECUTE_TEST(execute_try_certreq_poll_test, tear_down);
  339. return result;
  340. }
  341. static int execute_try_certreq_poll_abort_test(CMP_SES_TEST_FIXTURE *fixture)
  342. {
  343. OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
  344. int check_after;
  345. const int CHECK_AFTER = 99;
  346. const int TYPE = OSSL_CMP_PKIBODY_CR;
  347. ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
  348. ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
  349. return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
  350. && check_after == CHECK_AFTER
  351. && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
  352. && TEST_int_eq(fixture->expected,
  353. OSSL_CMP_try_certreq(ctx, -1 /* abort */, NULL, NULL))
  354. && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(fixture->cmp_ctx), NULL);
  355. }
  356. static int test_try_certreq_poll_abort(void)
  357. {
  358. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  359. fixture->expected = 1;
  360. EXECUTE_TEST(execute_try_certreq_poll_abort_test, tear_down);
  361. return result;
  362. }
  363. static int test_exec_GENM_ses_poll_ok(void)
  364. {
  365. return test_exec_REQ_ses_poll(OSSL_CMP_PKIBODY_GENM, checkAfter, 2, 0,
  366. OSSL_CMP_PKISTATUS_accepted);
  367. }
  368. static int test_exec_GENM_ses_poll_no_timeout(void)
  369. {
  370. return test_exec_REQ_ses_poll(OSSL_CMP_PKIBODY_GENM, checkAfter,
  371. 1 /* pollCount */, checkAfter + 1,
  372. OSSL_CMP_PKISTATUS_accepted);
  373. }
  374. static int test_exec_GENM_ses_poll_total_timeout(void)
  375. {
  376. return test_exec_REQ_ses_poll(OSSL_CMP_PKIBODY_GENM, checkAfter + 1,
  377. 3 /* pollCount */, checkAfter + 2,
  378. OSSL_CMP_PKISTATUS_waiting);
  379. }
  380. static int test_exec_GENM_ses(int transfer_error, int total_timeout, int expect)
  381. {
  382. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  383. if (transfer_error)
  384. OSSL_CMP_CTX_set_transfer_cb_arg(fixture->cmp_ctx, NULL);
  385. /*
  386. * cannot use OSSL_CMP_CTX_set_option(... OSSL_CMP_OPT_TOTAL_TIMEOUT)
  387. * here because this will correct total_timeout to be >= 0
  388. */
  389. fixture->cmp_ctx->total_timeout = total_timeout;
  390. fixture->expected = expect;
  391. EXECUTE_TEST(execute_exec_GENM_ses_test, tear_down);
  392. return result;
  393. }
  394. static int test_exec_GENM_ses_ok(void)
  395. {
  396. return test_exec_GENM_ses(0, 0, OSSL_CMP_PKISTATUS_accepted);
  397. }
  398. static int test_exec_GENM_ses_transfer_error(void)
  399. {
  400. return test_exec_GENM_ses(1, 0, OSSL_CMP_PKISTATUS_trans);
  401. }
  402. static int test_exec_GENM_ses_total_timeout(void)
  403. {
  404. return test_exec_GENM_ses(0, -1, OSSL_CMP_PKISTATUS_trans);
  405. }
  406. static int execute_exchange_certConf_test(CMP_SES_TEST_FIXTURE *fixture)
  407. {
  408. int res =
  409. ossl_cmp_exchange_certConf(fixture->cmp_ctx, OSSL_CMP_CERTREQID,
  410. OSSL_CMP_PKIFAILUREINFO_addInfoNotAvailable,
  411. "abcdefg");
  412. return TEST_int_eq(fixture->expected, res);
  413. }
  414. static int execute_exchange_error_test(CMP_SES_TEST_FIXTURE *fixture)
  415. {
  416. int res =
  417. ossl_cmp_exchange_error(fixture->cmp_ctx,
  418. OSSL_CMP_PKISTATUS_rejection,
  419. 1 << OSSL_CMP_PKIFAILUREINFO_unsupportedVersion,
  420. "foo_status", 999, "foo_details");
  421. return TEST_int_eq(fixture->expected, res);
  422. }
  423. static int test_exchange_certConf(void)
  424. {
  425. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  426. fixture->expected = 0; /* client should not send certConf immediately */
  427. if (!ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx, X509_dup(client_cert))) {
  428. tear_down(fixture);
  429. fixture = NULL;
  430. }
  431. EXECUTE_TEST(execute_exchange_certConf_test, tear_down);
  432. return result;
  433. }
  434. static int test_exchange_error(void)
  435. {
  436. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  437. fixture->expected = 1; /* client may send error any time */
  438. EXECUTE_TEST(execute_exchange_error_test, tear_down);
  439. return result;
  440. }
  441. void cleanup_tests(void)
  442. {
  443. X509_free(server_cert);
  444. EVP_PKEY_free(server_key);
  445. X509_free(client_cert);
  446. EVP_PKEY_free(client_key);
  447. OSSL_PROVIDER_unload(default_null_provider);
  448. OSSL_PROVIDER_unload(provider);
  449. OSSL_LIB_CTX_free(libctx);
  450. return;
  451. }
  452. #define USAGE "server.key server.crt client.key client.crt client.csr module_name [module_conf_file]\n"
  453. OPT_TEST_DECLARE_USAGE(USAGE)
  454. int setup_tests(void)
  455. {
  456. if (!test_skip_common_options()) {
  457. TEST_error("Error parsing test options\n");
  458. return 0;
  459. }
  460. if (!TEST_ptr(server_key_f = test_get_argument(0))
  461. || !TEST_ptr(server_cert_f = test_get_argument(1))
  462. || !TEST_ptr(client_key_f = test_get_argument(2))
  463. || !TEST_ptr(client_cert_f = test_get_argument(3))
  464. || !TEST_ptr(pkcs10_f = test_get_argument(4))) {
  465. TEST_error("usage: cmp_client_test %s", USAGE);
  466. return 0;
  467. }
  468. if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 5, USAGE))
  469. return 0;
  470. if (!TEST_ptr(server_key = load_pkey_pem(server_key_f, libctx))
  471. || !TEST_ptr(server_cert = load_cert_pem(server_cert_f, libctx))
  472. || !TEST_ptr(client_key = load_pkey_pem(client_key_f, libctx))
  473. || !TEST_ptr(client_cert = load_cert_pem(client_cert_f, libctx))
  474. || !TEST_int_eq(1, RAND_bytes_ex(libctx, ref, sizeof(ref), 0))) {
  475. cleanup_tests();
  476. return 0;
  477. }
  478. ADD_TEST(test_exec_RR_ses_ok);
  479. ADD_TEST(test_exec_RR_ses_request_error);
  480. ADD_TEST(test_exec_RR_ses_receive_error);
  481. ADD_TEST(test_exec_CR_ses_explicit_confirm);
  482. ADD_TEST(test_exec_CR_ses_implicit_confirm);
  483. ADD_TEST(test_exec_IR_ses);
  484. ADD_TEST(test_exec_IR_ses_poll_ok);
  485. ADD_TEST(test_exec_IR_ses_poll_no_timeout);
  486. ADD_TEST(test_exec_IR_ses_poll_total_timeout);
  487. ADD_TEST(test_exec_KUR_ses_ok);
  488. ADD_TEST(test_exec_KUR_ses_transfer_error);
  489. ADD_TEST(test_exec_KUR_ses_wrong_popo);
  490. ADD_TEST(test_exec_KUR_ses_pub);
  491. ADD_TEST(test_exec_KUR_ses_wrong_pub);
  492. ADD_TEST(test_exec_P10CR_ses_ok);
  493. ADD_TEST(test_exec_P10CR_ses_reject);
  494. ADD_TEST(test_try_certreq_poll);
  495. ADD_TEST(test_try_certreq_poll_abort);
  496. ADD_TEST(test_exec_GENM_ses_ok);
  497. ADD_TEST(test_exec_GENM_ses_transfer_error);
  498. ADD_TEST(test_exec_GENM_ses_total_timeout);
  499. ADD_TEST(test_exec_GENM_ses_poll_ok);
  500. ADD_TEST(test_exec_GENM_ses_poll_no_timeout);
  501. ADD_TEST(test_exec_GENM_ses_poll_total_timeout);
  502. ADD_TEST(test_exchange_certConf);
  503. ADD_TEST(test_exchange_error);
  504. return 1;
  505. }