2
0

cmp_client_test.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. DEFINE_STACK_OF(X509)
  15. DEFINE_STACK_OF(OSSL_CMP_ITAV)
  16. static const char *server_key_f;
  17. static const char *server_cert_f;
  18. static const char *client_key_f;
  19. static const char *client_cert_f;
  20. static const char *pkcs10_f;
  21. typedef struct test_fixture {
  22. const char *test_case_name;
  23. OSSL_CMP_CTX *cmp_ctx;
  24. OSSL_CMP_SRV_CTX *srv_ctx;
  25. int expected;
  26. X509 *(*exec_cert_ses_cb) (OSSL_CMP_CTX *);
  27. STACK_OF(X509) *caPubs;
  28. } CMP_SES_TEST_FIXTURE;
  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())
  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())
  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->exec_cert_ses_cb = NULL;
  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;
  96. if (fixture->expected == 0)
  97. return TEST_ptr_null(fixture->exec_cert_ses_cb(fixture->cmp_ctx));
  98. if (!TEST_ptr(res = fixture->exec_cert_ses_cb(fixture->cmp_ctx))
  99. || !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->exec_cert_ses_cb = OSSL_CMP_exec_IR_ses;
  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->exec_cert_ses_cb = OSSL_CMP_exec_IR_ses;
  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->exec_cert_ses_cb = OSSL_CMP_exec_IR_ses;
  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->exec_cert_ses_cb = OSSL_CMP_exec_CR_ses;
  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->exec_cert_ses_cb = OSSL_CMP_exec_CR_ses;
  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->exec_cert_ses_cb = OSSL_CMP_exec_KUR_ses;
  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->exec_cert_ses_cb = OSSL_CMP_exec_P10CR_ses;
  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, &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, &check_after))
  220. && check_after == CHECK_AFTER
  221. && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
  222. && TEST_int_eq(fixture->expected, OSSL_CMP_try_certreq(ctx, TYPE, NULL))
  223. && TEST_int_eq(0,
  224. X509_cmp(OSSL_CMP_CTX_get0_newCert(ctx), client_cert));
  225. }
  226. static int test_try_certreq_poll(void)
  227. {
  228. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  229. fixture->expected = 1;
  230. EXECUTE_TEST(execute_try_certreq_poll_test, tear_down);
  231. return result;
  232. }
  233. static int execute_try_certreq_poll_abort_test(CMP_SES_TEST_FIXTURE *fixture)
  234. {
  235. OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
  236. int check_after;
  237. const int CHECK_AFTER = INT_MAX;
  238. const int TYPE = OSSL_CMP_CR;
  239. ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
  240. ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
  241. return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, &check_after))
  242. && check_after == CHECK_AFTER
  243. && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
  244. && TEST_int_eq(fixture->expected, OSSL_CMP_try_certreq(ctx, -1, NULL))
  245. && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(fixture->cmp_ctx), NULL);
  246. }
  247. static int test_try_certreq_poll_abort(void)
  248. {
  249. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  250. fixture->expected = 1;
  251. EXECUTE_TEST(execute_try_certreq_poll_abort_test, tear_down);
  252. return result;
  253. }
  254. static int test_exec_GENM_ses(void)
  255. {
  256. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  257. EXECUTE_TEST(execute_exec_GENM_ses_test, tear_down);
  258. return result;
  259. }
  260. static int execute_exchange_certConf_test(CMP_SES_TEST_FIXTURE *fixture)
  261. {
  262. int res =
  263. ossl_cmp_exchange_certConf(fixture->cmp_ctx,
  264. OSSL_CMP_PKIFAILUREINFO_addInfoNotAvailable,
  265. "abcdefg");
  266. return TEST_int_eq(fixture->expected, res);
  267. }
  268. static int execute_exchange_error_test(CMP_SES_TEST_FIXTURE *fixture)
  269. {
  270. int res =
  271. ossl_cmp_exchange_error(fixture->cmp_ctx,
  272. OSSL_CMP_PKISTATUS_rejection,
  273. 1 << OSSL_CMP_PKIFAILUREINFO_unsupportedVersion,
  274. "foo_status", 999, "foo_details");
  275. return TEST_int_eq(fixture->expected, res);
  276. }
  277. static int test_exchange_certConf(void)
  278. {
  279. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  280. fixture->expected = 0; /* client should not send certConf immediately */
  281. if (!ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx, X509_dup(client_cert))) {
  282. tear_down(fixture);
  283. fixture = NULL;
  284. }
  285. EXECUTE_TEST(execute_exchange_certConf_test, tear_down);
  286. return result;
  287. }
  288. static int test_exchange_error(void)
  289. {
  290. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  291. fixture->expected = 1; /* client may send error any time */
  292. EXECUTE_TEST(execute_exchange_error_test, tear_down);
  293. return result;
  294. }
  295. void cleanup_tests(void)
  296. {
  297. X509_free(server_cert);
  298. EVP_PKEY_free(server_key);
  299. X509_free(client_cert);
  300. EVP_PKEY_free(client_key);
  301. return;
  302. }
  303. int setup_tests(void)
  304. {
  305. if (!test_skip_common_options()) {
  306. TEST_error("Error parsing test options\n");
  307. return 0;
  308. }
  309. if (!TEST_ptr(server_key_f = test_get_argument(0))
  310. || !TEST_ptr(server_cert_f = test_get_argument(1))
  311. || !TEST_ptr(client_key_f = test_get_argument(2))
  312. || !TEST_ptr(client_cert_f = test_get_argument(3))
  313. || !TEST_ptr(pkcs10_f = test_get_argument(4))) {
  314. TEST_error("usage: cmp_client_test server.key server.crt client.key client.crt client.csr\n");
  315. return 0;
  316. }
  317. if (!TEST_ptr(server_key = load_pem_key(server_key_f))
  318. || !TEST_ptr(server_cert = load_pem_cert(server_cert_f))
  319. || !TEST_ptr(client_key = load_pem_key(client_key_f))
  320. || !TEST_ptr(client_cert = load_pem_cert(client_cert_f))
  321. || !TEST_int_eq(1, RAND_bytes(ref, sizeof(ref)))) {
  322. cleanup_tests();
  323. return 0;
  324. }
  325. ADD_TEST(test_exec_RR_ses);
  326. ADD_TEST(test_exec_RR_ses_receive_error);
  327. ADD_TEST(test_exec_CR_ses);
  328. ADD_TEST(test_exec_CR_ses_implicit_confirm);
  329. ADD_TEST(test_exec_IR_ses);
  330. ADD_TEST(test_exec_IR_ses_poll);
  331. ADD_TEST(test_exec_IR_ses_poll_timeout);
  332. ADD_TEST(test_exec_KUR_ses);
  333. ADD_TEST(test_exec_P10CR_ses);
  334. ADD_TEST(test_try_certreq_poll);
  335. ADD_TEST(test_try_certreq_poll_abort);
  336. ADD_TEST(test_exec_GENM_ses);
  337. ADD_TEST(test_exchange_certConf);
  338. ADD_TEST(test_exchange_error);
  339. return 1;
  340. }
  341. #else /* !defined (NDEBUG) */
  342. int setup_tests(void)
  343. {
  344. TEST_note("CMP session tests are disabled in this build (NDEBUG).");
  345. return 1;
  346. }
  347. #endif