cmp_client_test.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*
  2. * Copyright 2007-2023 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_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_IR_ses_poll(int check_after, int poll_count,
  172. int total_timeout, int expect)
  173. {
  174. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  175. fixture->req_type = OSSL_CMP_IR;
  176. fixture->expected = expect;
  177. ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, check_after);
  178. ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, poll_count);
  179. OSSL_CMP_CTX_set_option(fixture->cmp_ctx,
  180. OSSL_CMP_OPT_TOTAL_TIMEOUT, total_timeout);
  181. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  182. return result;
  183. }
  184. static int checkAfter = 1;
  185. static int test_exec_IR_ses_poll_ok(void)
  186. {
  187. return test_exec_IR_ses_poll(checkAfter, 2, 0, OSSL_CMP_PKISTATUS_accepted);
  188. }
  189. static int test_exec_IR_ses_poll_no_timeout(void)
  190. {
  191. return test_exec_IR_ses_poll(checkAfter, 1 /* pollCount */, checkAfter + 1,
  192. OSSL_CMP_PKISTATUS_accepted);
  193. }
  194. static int test_exec_IR_ses_poll_total_timeout(void)
  195. {
  196. return test_exec_IR_ses_poll(checkAfter + 1, 2 /* pollCount */, checkAfter,
  197. OSSL_CMP_PKISTATUS_waiting);
  198. }
  199. static int test_exec_CR_ses(int implicit_confirm, int granted, int reject)
  200. {
  201. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  202. fixture->req_type = OSSL_CMP_CR;
  203. OSSL_CMP_CTX_set_option(fixture->cmp_ctx,
  204. OSSL_CMP_OPT_IMPLICIT_CONFIRM, implicit_confirm);
  205. OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(fixture->srv_ctx, granted);
  206. ossl_cmp_mock_srv_set_sendError(fixture->srv_ctx,
  207. reject ? OSSL_CMP_PKIBODY_CERTCONF : -1);
  208. fixture->expected = reject ? OSSL_CMP_PKISTATUS_rejection
  209. : OSSL_CMP_PKISTATUS_accepted;
  210. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  211. return result;
  212. }
  213. static int test_exec_CR_ses_explicit_confirm(void)
  214. {
  215. return test_exec_CR_ses(0, 0, 0)
  216. && test_exec_CR_ses(0, 0, 1 /* reject */);
  217. }
  218. static int test_exec_CR_ses_implicit_confirm(void)
  219. {
  220. return test_exec_CR_ses(1, 0, 0)
  221. && test_exec_CR_ses(1, 1 /* granted */, 0);
  222. }
  223. static int test_exec_KUR_ses(int transfer_error, int pubkey, int raverified)
  224. {
  225. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  226. fixture->req_type = OSSL_CMP_KUR;
  227. /* ctx->oldCert has already been set */
  228. if (transfer_error)
  229. OSSL_CMP_CTX_set_transfer_cb_arg(fixture->cmp_ctx, NULL);
  230. if (pubkey) {
  231. EVP_PKEY *key = raverified /* wrong key */ ? server_key : client_key;
  232. EVP_PKEY_up_ref(key);
  233. OSSL_CMP_CTX_set0_newPkey(fixture->cmp_ctx, 0 /* not priv */, key);
  234. OSSL_CMP_SRV_CTX_set_accept_raverified(fixture->srv_ctx, 1);
  235. }
  236. if (pubkey || raverified)
  237. OSSL_CMP_CTX_set_option(fixture->cmp_ctx, OSSL_CMP_OPT_POPO_METHOD,
  238. OSSL_CRMF_POPO_RAVERIFIED);
  239. fixture->expected = transfer_error ? OSSL_CMP_PKISTATUS_trans :
  240. raverified ? OSSL_CMP_PKISTATUS_rejection : OSSL_CMP_PKISTATUS_accepted;
  241. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  242. return result;
  243. }
  244. static int test_exec_KUR_ses_ok(void)
  245. {
  246. return test_exec_KUR_ses(0, 0, 0);
  247. }
  248. static int test_exec_KUR_ses_transfer_error(void)
  249. {
  250. return test_exec_KUR_ses(1, 0, 0);
  251. }
  252. static int test_exec_KUR_ses_wrong_popo(void)
  253. {
  254. #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION /* cf ossl_cmp_verify_popo() */
  255. return test_exec_KUR_ses(0, 0, 1);
  256. #else
  257. return 1;
  258. #endif
  259. }
  260. static int test_exec_KUR_ses_pub(void)
  261. {
  262. return test_exec_KUR_ses(0, 1, 0);
  263. }
  264. static int test_exec_KUR_ses_wrong_pub(void)
  265. {
  266. return test_exec_KUR_ses(0, 1, 1);
  267. }
  268. static int test_certConf_cb(OSSL_CMP_CTX *ctx, X509 *cert, int fail_info,
  269. const char **txt)
  270. {
  271. int *reject = OSSL_CMP_CTX_get_certConf_cb_arg(ctx);
  272. if (*reject) {
  273. *txt = "not to my taste";
  274. fail_info = OSSL_CMP_PKIFAILUREINFO_badCertTemplate;
  275. }
  276. return fail_info;
  277. }
  278. static int test_exec_P10CR_ses(int reject)
  279. {
  280. OSSL_CMP_CTX *ctx;
  281. X509_REQ *csr = NULL;
  282. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  283. fixture->req_type = OSSL_CMP_P10CR;
  284. fixture->expected = reject ? OSSL_CMP_PKISTATUS_rejection
  285. : OSSL_CMP_PKISTATUS_accepted;
  286. ctx = fixture->cmp_ctx;
  287. if (!TEST_ptr(csr = load_csr_der(pkcs10_f, libctx))
  288. || !TEST_true(OSSL_CMP_CTX_set1_p10CSR(ctx, csr))
  289. || !TEST_true(OSSL_CMP_CTX_set_certConf_cb(ctx, test_certConf_cb))
  290. || !TEST_true(OSSL_CMP_CTX_set_certConf_cb_arg(ctx, &reject))) {
  291. tear_down(fixture);
  292. fixture = NULL;
  293. }
  294. X509_REQ_free(csr);
  295. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  296. return result;
  297. }
  298. static int test_exec_P10CR_ses_ok(void)
  299. {
  300. return test_exec_P10CR_ses(0);
  301. }
  302. static int test_exec_P10CR_ses_reject(void)
  303. {
  304. return test_exec_P10CR_ses(1);
  305. }
  306. static int execute_try_certreq_poll_test(CMP_SES_TEST_FIXTURE *fixture)
  307. {
  308. OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
  309. int check_after;
  310. const int CHECK_AFTER = 5;
  311. const int TYPE = OSSL_CMP_KUR;
  312. ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
  313. ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
  314. return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
  315. && check_after == CHECK_AFTER
  316. && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
  317. && TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
  318. && check_after == CHECK_AFTER
  319. && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
  320. && TEST_int_eq(fixture->expected,
  321. OSSL_CMP_try_certreq(ctx, TYPE, NULL, NULL))
  322. && TEST_int_eq(0,
  323. X509_cmp(OSSL_CMP_CTX_get0_newCert(ctx), client_cert));
  324. }
  325. static int test_try_certreq_poll(void)
  326. {
  327. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  328. fixture->expected = 1;
  329. EXECUTE_TEST(execute_try_certreq_poll_test, tear_down);
  330. return result;
  331. }
  332. static int execute_try_certreq_poll_abort_test(CMP_SES_TEST_FIXTURE *fixture)
  333. {
  334. OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
  335. int check_after;
  336. const int CHECK_AFTER = 99;
  337. const int TYPE = OSSL_CMP_CR;
  338. ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
  339. ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
  340. return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
  341. && check_after == CHECK_AFTER
  342. && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
  343. && TEST_int_eq(fixture->expected,
  344. OSSL_CMP_try_certreq(ctx, -1 /* abort */, NULL, NULL))
  345. && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(fixture->cmp_ctx), NULL);
  346. }
  347. static int test_try_certreq_poll_abort(void)
  348. {
  349. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  350. fixture->expected = 1;
  351. EXECUTE_TEST(execute_try_certreq_poll_abort_test, tear_down);
  352. return result;
  353. }
  354. static int test_exec_GENM_ses(int transfer_error, int total_timeout, int expect)
  355. {
  356. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  357. if (transfer_error)
  358. OSSL_CMP_CTX_set_transfer_cb_arg(fixture->cmp_ctx, NULL);
  359. /*
  360. * cannot use OSSL_CMP_CTX_set_option(... OSSL_CMP_OPT_TOTAL_TIMEOUT)
  361. * here because this will correct total_timeout to be >= 0
  362. */
  363. fixture->cmp_ctx->total_timeout = total_timeout;
  364. fixture->expected = expect;
  365. EXECUTE_TEST(execute_exec_GENM_ses_test, tear_down);
  366. return result;
  367. }
  368. static int test_exec_GENM_ses_ok(void)
  369. {
  370. return test_exec_GENM_ses(0, 0, OSSL_CMP_PKISTATUS_accepted);
  371. }
  372. static int test_exec_GENM_ses_transfer_error(void)
  373. {
  374. return test_exec_GENM_ses(1, 0, OSSL_CMP_PKISTATUS_trans);
  375. }
  376. static int test_exec_GENM_ses_total_timeout(void)
  377. {
  378. return test_exec_GENM_ses(0, -1, OSSL_CMP_PKISTATUS_trans);
  379. }
  380. static int execute_exchange_certConf_test(CMP_SES_TEST_FIXTURE *fixture)
  381. {
  382. int res =
  383. ossl_cmp_exchange_certConf(fixture->cmp_ctx, OSSL_CMP_CERTREQID,
  384. OSSL_CMP_PKIFAILUREINFO_addInfoNotAvailable,
  385. "abcdefg");
  386. return TEST_int_eq(fixture->expected, res);
  387. }
  388. static int execute_exchange_error_test(CMP_SES_TEST_FIXTURE *fixture)
  389. {
  390. int res =
  391. ossl_cmp_exchange_error(fixture->cmp_ctx,
  392. OSSL_CMP_PKISTATUS_rejection,
  393. 1 << OSSL_CMP_PKIFAILUREINFO_unsupportedVersion,
  394. "foo_status", 999, "foo_details");
  395. return TEST_int_eq(fixture->expected, res);
  396. }
  397. static int test_exchange_certConf(void)
  398. {
  399. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  400. fixture->expected = 0; /* client should not send certConf immediately */
  401. if (!ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx, X509_dup(client_cert))) {
  402. tear_down(fixture);
  403. fixture = NULL;
  404. }
  405. EXECUTE_TEST(execute_exchange_certConf_test, tear_down);
  406. return result;
  407. }
  408. static int test_exchange_error(void)
  409. {
  410. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  411. fixture->expected = 1; /* client may send error any time */
  412. EXECUTE_TEST(execute_exchange_error_test, tear_down);
  413. return result;
  414. }
  415. void cleanup_tests(void)
  416. {
  417. X509_free(server_cert);
  418. EVP_PKEY_free(server_key);
  419. X509_free(client_cert);
  420. EVP_PKEY_free(client_key);
  421. OSSL_PROVIDER_unload(default_null_provider);
  422. OSSL_PROVIDER_unload(provider);
  423. OSSL_LIB_CTX_free(libctx);
  424. return;
  425. }
  426. #define USAGE "server.key server.crt client.key client.crt client.csr module_name [module_conf_file]\n"
  427. OPT_TEST_DECLARE_USAGE(USAGE)
  428. int setup_tests(void)
  429. {
  430. if (!test_skip_common_options()) {
  431. TEST_error("Error parsing test options\n");
  432. return 0;
  433. }
  434. if (!TEST_ptr(server_key_f = test_get_argument(0))
  435. || !TEST_ptr(server_cert_f = test_get_argument(1))
  436. || !TEST_ptr(client_key_f = test_get_argument(2))
  437. || !TEST_ptr(client_cert_f = test_get_argument(3))
  438. || !TEST_ptr(pkcs10_f = test_get_argument(4))) {
  439. TEST_error("usage: cmp_client_test %s", USAGE);
  440. return 0;
  441. }
  442. if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 5, USAGE))
  443. return 0;
  444. if (!TEST_ptr(server_key = load_pkey_pem(server_key_f, libctx))
  445. || !TEST_ptr(server_cert = load_cert_pem(server_cert_f, libctx))
  446. || !TEST_ptr(client_key = load_pkey_pem(client_key_f, libctx))
  447. || !TEST_ptr(client_cert = load_cert_pem(client_cert_f, libctx))
  448. || !TEST_int_eq(1, RAND_bytes_ex(libctx, ref, sizeof(ref), 0))) {
  449. cleanup_tests();
  450. return 0;
  451. }
  452. ADD_TEST(test_exec_RR_ses_ok);
  453. ADD_TEST(test_exec_RR_ses_request_error);
  454. ADD_TEST(test_exec_RR_ses_receive_error);
  455. ADD_TEST(test_exec_CR_ses_explicit_confirm);
  456. ADD_TEST(test_exec_CR_ses_implicit_confirm);
  457. ADD_TEST(test_exec_IR_ses);
  458. ADD_TEST(test_exec_IR_ses_poll_ok);
  459. ADD_TEST(test_exec_IR_ses_poll_no_timeout);
  460. ADD_TEST(test_exec_IR_ses_poll_total_timeout);
  461. ADD_TEST(test_exec_KUR_ses_ok);
  462. ADD_TEST(test_exec_KUR_ses_transfer_error);
  463. ADD_TEST(test_exec_KUR_ses_wrong_popo);
  464. ADD_TEST(test_exec_KUR_ses_pub);
  465. ADD_TEST(test_exec_KUR_ses_wrong_pub);
  466. ADD_TEST(test_exec_P10CR_ses_ok);
  467. ADD_TEST(test_exec_P10CR_ses_reject);
  468. ADD_TEST(test_try_certreq_poll);
  469. ADD_TEST(test_try_certreq_poll_abort);
  470. ADD_TEST(test_exec_GENM_ses_ok);
  471. ADD_TEST(test_exec_GENM_ses_transfer_error);
  472. ADD_TEST(test_exec_GENM_ses_total_timeout);
  473. ADD_TEST(test_exchange_certConf);
  474. ADD_TEST(test_exchange_error);
  475. return 1;
  476. }