cmp_client_test.c 20 KB

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