cmp_client_test.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Copyright 2007-2020 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_testlib.h"
  12. #include "apps/cmp_mock_srv.h"
  13. #ifndef NDEBUG /* tests need mock server, which is available only if !NDEBUG */
  14. static const char *server_key_f;
  15. static const char *server_cert_f;
  16. static const char *client_key_f;
  17. static const char *client_cert_f;
  18. static const char *pkcs10_f;
  19. typedef struct test_fixture {
  20. const char *test_case_name;
  21. OSSL_CMP_CTX *cmp_ctx;
  22. OSSL_CMP_SRV_CTX *srv_ctx;
  23. int req_type;
  24. int expected;
  25. STACK_OF(X509) *caPubs;
  26. } CMP_SES_TEST_FIXTURE;
  27. static OSSL_LIB_CTX *libctx = NULL;
  28. static OSSL_PROVIDER *default_null_provider = NULL, *provider = NULL;
  29. static EVP_PKEY *server_key = NULL;
  30. static X509 *server_cert = NULL;
  31. static EVP_PKEY *client_key = NULL;
  32. static X509 *client_cert = NULL;
  33. static unsigned char ref[CMP_TEST_REFVALUE_LENGTH];
  34. /*
  35. * For these unit tests, the client abandons message protection, and for
  36. * error messages the mock server does so as well.
  37. * Message protection and verification is tested in cmp_lib_test.c
  38. */
  39. static void tear_down(CMP_SES_TEST_FIXTURE *fixture)
  40. {
  41. OSSL_CMP_CTX_free(fixture->cmp_ctx);
  42. ossl_cmp_mock_srv_free(fixture->srv_ctx);
  43. sk_X509_free(fixture->caPubs);
  44. OPENSSL_free(fixture);
  45. }
  46. static CMP_SES_TEST_FIXTURE *set_up(const char *const test_case_name)
  47. {
  48. CMP_SES_TEST_FIXTURE *fixture;
  49. OSSL_CMP_CTX *srv_cmp_ctx = NULL;
  50. OSSL_CMP_CTX *ctx = NULL; /* for client */
  51. if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
  52. return NULL;
  53. fixture->test_case_name = test_case_name;
  54. if (!TEST_ptr(fixture->srv_ctx = ossl_cmp_mock_srv_new(libctx, NULL))
  55. || !OSSL_CMP_SRV_CTX_set_accept_unprotected(fixture->srv_ctx, 1)
  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. || !OSSL_CMP_CTX_set1_srvCert(ctx, server_cert)
  71. || !OSSL_CMP_CTX_set1_referenceValue(ctx, ref, sizeof(ref)))
  72. goto err;
  73. fixture->req_type = -1;
  74. return fixture;
  75. err:
  76. tear_down(fixture);
  77. return NULL;
  78. }
  79. static int execute_exec_RR_ses_test(CMP_SES_TEST_FIXTURE *fixture)
  80. {
  81. return TEST_int_eq(fixture->expected,
  82. OSSL_CMP_exec_RR_ses(fixture->cmp_ctx) == client_cert);
  83. }
  84. static int execute_exec_GENM_ses_test(CMP_SES_TEST_FIXTURE *fixture)
  85. {
  86. STACK_OF(OSSL_CMP_ITAV) *itavs = NULL;
  87. if (!TEST_ptr(itavs = OSSL_CMP_exec_GENM_ses(fixture->cmp_ctx)))
  88. return 0;
  89. sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
  90. /* TODO: check if the returned value is the expected one (same as sent) */
  91. return 1;
  92. }
  93. static int execute_exec_certrequest_ses_test(CMP_SES_TEST_FIXTURE *fixture)
  94. {
  95. X509 *res = OSSL_CMP_exec_certreq(fixture->cmp_ctx,
  96. fixture->req_type, NULL);
  97. if (fixture->expected == 0)
  98. return TEST_ptr_null(res);
  99. if (!TEST_ptr(res) || !TEST_int_eq(X509_cmp(res, client_cert), 0))
  100. return 0;
  101. /* TODO: check that cerfConf has been exchanged unless implicitConfirm */
  102. if (fixture->caPubs != NULL) {
  103. STACK_OF(X509) *caPubs = OSSL_CMP_CTX_get1_caPubs(fixture->cmp_ctx);
  104. int ret = TEST_int_eq(STACK_OF_X509_cmp(fixture->caPubs, caPubs), 0);
  105. sk_X509_pop_free(caPubs, X509_free);
  106. return ret;
  107. }
  108. return 1;
  109. }
  110. static int test_exec_RR_ses(void)
  111. {
  112. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  113. fixture->expected = 1;
  114. EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
  115. return result;
  116. }
  117. static int test_exec_RR_ses_receive_error(void)
  118. {
  119. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  120. ossl_cmp_mock_srv_set_statusInfo(fixture->srv_ctx,
  121. OSSL_CMP_PKISTATUS_rejection,
  122. OSSL_CMP_CTX_FAILINFO_signerNotTrusted,
  123. "test string");
  124. ossl_cmp_mock_srv_set_send_error(fixture->srv_ctx, 1);
  125. fixture->expected = 0;
  126. EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
  127. return result;
  128. }
  129. static int test_exec_IR_ses(void)
  130. {
  131. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  132. fixture->req_type = OSSL_CMP_IR;
  133. fixture->expected = 1;
  134. fixture->caPubs = sk_X509_new_null();
  135. sk_X509_push(fixture->caPubs, server_cert);
  136. sk_X509_push(fixture->caPubs, server_cert);
  137. ossl_cmp_mock_srv_set1_caPubsOut(fixture->srv_ctx, fixture->caPubs);
  138. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  139. return result;
  140. }
  141. static const int checkAfter = 1;
  142. static int test_exec_IR_ses_poll(void)
  143. {
  144. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  145. fixture->req_type = OSSL_CMP_IR;
  146. fixture->expected = 1;
  147. ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 2);
  148. ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, checkAfter);
  149. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  150. /* TODO: check that 2 rounds are done or session takes 2..3 seconds */
  151. return result;
  152. }
  153. static int test_exec_IR_ses_poll_timeout(void)
  154. {
  155. const int pollCount = 3;
  156. const int tout = pollCount * checkAfter;
  157. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  158. fixture->req_type = OSSL_CMP_IR;
  159. fixture->expected = 0;
  160. ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, pollCount + 1);
  161. ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, checkAfter);
  162. OSSL_CMP_CTX_set_option(fixture->cmp_ctx, OSSL_CMP_OPT_TOTAL_TIMEOUT, tout);
  163. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  164. return result;
  165. }
  166. static int test_exec_CR_ses(void)
  167. {
  168. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  169. fixture->req_type = OSSL_CMP_CR;
  170. fixture->expected = 1;
  171. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  172. return result;
  173. }
  174. static int test_exec_CR_ses_implicit_confirm(void)
  175. {
  176. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  177. fixture->req_type = OSSL_CMP_CR;
  178. fixture->expected = 1;
  179. OSSL_CMP_CTX_set_option(fixture->cmp_ctx,
  180. OSSL_CMP_OPT_IMPLICIT_CONFIRM, 1);
  181. OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(fixture->srv_ctx, 1);
  182. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  183. return result;
  184. }
  185. static int test_exec_KUR_ses(void)
  186. {
  187. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  188. fixture->req_type = OSSL_CMP_KUR;
  189. fixture->expected = 1;
  190. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  191. return result;
  192. }
  193. static int test_exec_P10CR_ses(void)
  194. {
  195. X509_REQ *req = NULL;
  196. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  197. fixture->req_type = OSSL_CMP_P10CR;
  198. fixture->expected = 1;
  199. if (!TEST_ptr(req = load_csr(pkcs10_f))
  200. || !TEST_true(OSSL_CMP_CTX_set1_p10CSR(fixture->cmp_ctx, req))) {
  201. tear_down(fixture);
  202. fixture = NULL;
  203. }
  204. X509_REQ_free(req);
  205. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  206. return result;
  207. }
  208. static int execute_try_certreq_poll_test(CMP_SES_TEST_FIXTURE *fixture)
  209. {
  210. OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
  211. int check_after;
  212. const int CHECK_AFTER = 5;
  213. const int TYPE = OSSL_CMP_KUR;
  214. ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
  215. ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
  216. return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
  217. && check_after == CHECK_AFTER
  218. && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
  219. && TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
  220. && check_after == CHECK_AFTER
  221. && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
  222. && TEST_int_eq(fixture->expected,
  223. OSSL_CMP_try_certreq(ctx, TYPE, NULL, NULL))
  224. && TEST_int_eq(0,
  225. X509_cmp(OSSL_CMP_CTX_get0_newCert(ctx), client_cert));
  226. }
  227. static int test_try_certreq_poll(void)
  228. {
  229. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  230. fixture->expected = 1;
  231. EXECUTE_TEST(execute_try_certreq_poll_test, tear_down);
  232. return result;
  233. }
  234. static int execute_try_certreq_poll_abort_test(CMP_SES_TEST_FIXTURE *fixture)
  235. {
  236. OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
  237. int check_after;
  238. const int CHECK_AFTER = INT_MAX;
  239. const int TYPE = OSSL_CMP_CR;
  240. ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
  241. ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
  242. return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
  243. && check_after == CHECK_AFTER
  244. && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
  245. && TEST_int_eq(fixture->expected,
  246. OSSL_CMP_try_certreq(ctx, -1, NULL, NULL))
  247. && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(fixture->cmp_ctx), NULL);
  248. }
  249. static int test_try_certreq_poll_abort(void)
  250. {
  251. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  252. fixture->expected = 1;
  253. EXECUTE_TEST(execute_try_certreq_poll_abort_test, tear_down);
  254. return result;
  255. }
  256. static int test_exec_GENM_ses(void)
  257. {
  258. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  259. EXECUTE_TEST(execute_exec_GENM_ses_test, tear_down);
  260. return result;
  261. }
  262. static int execute_exchange_certConf_test(CMP_SES_TEST_FIXTURE *fixture)
  263. {
  264. int res =
  265. ossl_cmp_exchange_certConf(fixture->cmp_ctx,
  266. OSSL_CMP_PKIFAILUREINFO_addInfoNotAvailable,
  267. "abcdefg");
  268. return TEST_int_eq(fixture->expected, res);
  269. }
  270. static int execute_exchange_error_test(CMP_SES_TEST_FIXTURE *fixture)
  271. {
  272. int res =
  273. ossl_cmp_exchange_error(fixture->cmp_ctx,
  274. OSSL_CMP_PKISTATUS_rejection,
  275. 1 << OSSL_CMP_PKIFAILUREINFO_unsupportedVersion,
  276. "foo_status", 999, "foo_details");
  277. return TEST_int_eq(fixture->expected, res);
  278. }
  279. static int test_exchange_certConf(void)
  280. {
  281. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  282. fixture->expected = 0; /* client should not send certConf immediately */
  283. if (!ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx, X509_dup(client_cert))) {
  284. tear_down(fixture);
  285. fixture = NULL;
  286. }
  287. EXECUTE_TEST(execute_exchange_certConf_test, tear_down);
  288. return result;
  289. }
  290. static int test_exchange_error(void)
  291. {
  292. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  293. fixture->expected = 1; /* client may send error any time */
  294. EXECUTE_TEST(execute_exchange_error_test, tear_down);
  295. return result;
  296. }
  297. void cleanup_tests(void)
  298. {
  299. X509_free(server_cert);
  300. EVP_PKEY_free(server_key);
  301. X509_free(client_cert);
  302. EVP_PKEY_free(client_key);
  303. OSSL_LIB_CTX_free(libctx);
  304. return;
  305. }
  306. # define USAGE "server.key server.crt client.key client.crt client.csr module_name [module_conf_file]\n"
  307. OPT_TEST_DECLARE_USAGE(USAGE)
  308. int setup_tests(void)
  309. {
  310. if (!test_skip_common_options()) {
  311. TEST_error("Error parsing test options\n");
  312. return 0;
  313. }
  314. if (!TEST_ptr(server_key_f = test_get_argument(0))
  315. || !TEST_ptr(server_cert_f = test_get_argument(1))
  316. || !TEST_ptr(client_key_f = test_get_argument(2))
  317. || !TEST_ptr(client_cert_f = test_get_argument(3))
  318. || !TEST_ptr(pkcs10_f = test_get_argument(4))) {
  319. TEST_error("usage: cmp_client_test %s", USAGE);
  320. return 0;
  321. }
  322. if (!test_get_libctx(&libctx, &default_null_provider, &provider, 5, USAGE))
  323. return 0;
  324. if (!TEST_ptr(server_key = load_pem_key(server_key_f, libctx))
  325. || !TEST_ptr(server_cert = load_pem_cert(server_cert_f, libctx))
  326. || !TEST_ptr(client_key = load_pem_key(client_key_f, libctx))
  327. || !TEST_ptr(client_cert = load_pem_cert(client_cert_f, libctx))
  328. || !TEST_int_eq(1, RAND_bytes_ex(libctx, ref, sizeof(ref)))) {
  329. cleanup_tests();
  330. return 0;
  331. }
  332. ADD_TEST(test_exec_RR_ses);
  333. ADD_TEST(test_exec_RR_ses_receive_error);
  334. ADD_TEST(test_exec_CR_ses);
  335. ADD_TEST(test_exec_CR_ses_implicit_confirm);
  336. ADD_TEST(test_exec_IR_ses);
  337. ADD_TEST(test_exec_IR_ses_poll);
  338. ADD_TEST(test_exec_IR_ses_poll_timeout);
  339. ADD_TEST(test_exec_KUR_ses);
  340. ADD_TEST(test_exec_P10CR_ses);
  341. ADD_TEST(test_try_certreq_poll);
  342. ADD_TEST(test_try_certreq_poll_abort);
  343. ADD_TEST(test_exec_GENM_ses);
  344. ADD_TEST(test_exchange_certConf);
  345. ADD_TEST(test_exchange_error);
  346. return 1;
  347. }
  348. #else /* !defined (NDEBUG) */
  349. int setup_tests(void)
  350. {
  351. TEST_note("CMP session tests are disabled in this build (NDEBUG).");
  352. return 1;
  353. }
  354. #endif