cmp_client_test.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Copyright 2007-2022 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. || !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) == 1);
  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. return 1;
  91. }
  92. static int execute_exec_certrequest_ses_test(CMP_SES_TEST_FIXTURE *fixture)
  93. {
  94. X509 *res = OSSL_CMP_exec_certreq(fixture->cmp_ctx,
  95. fixture->req_type, NULL);
  96. if (fixture->expected == 0)
  97. return TEST_ptr_null(res);
  98. if (!TEST_ptr(res) || !TEST_int_eq(X509_cmp(res, client_cert), 0))
  99. return 0;
  100. if (fixture->caPubs != NULL) {
  101. STACK_OF(X509) *caPubs = OSSL_CMP_CTX_get1_caPubs(fixture->cmp_ctx);
  102. int ret = TEST_int_eq(STACK_OF_X509_cmp(fixture->caPubs, caPubs), 0);
  103. OSSL_STACK_OF_X509_free(caPubs);
  104. return ret;
  105. }
  106. return 1;
  107. }
  108. static int test_exec_RR_ses(void)
  109. {
  110. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  111. fixture->expected = 1;
  112. EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
  113. return result;
  114. }
  115. static int test_exec_RR_ses_receive_error(void)
  116. {
  117. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  118. ossl_cmp_mock_srv_set_statusInfo(fixture->srv_ctx,
  119. OSSL_CMP_PKISTATUS_rejection,
  120. OSSL_CMP_CTX_FAILINFO_signerNotTrusted,
  121. "test string");
  122. ossl_cmp_mock_srv_set_send_error(fixture->srv_ctx, 1);
  123. fixture->expected = 0;
  124. EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
  125. return result;
  126. }
  127. static int test_exec_IR_ses(void)
  128. {
  129. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  130. fixture->req_type = OSSL_CMP_IR;
  131. fixture->expected = 1;
  132. fixture->caPubs = sk_X509_new_null();
  133. sk_X509_push(fixture->caPubs, server_cert);
  134. sk_X509_push(fixture->caPubs, server_cert);
  135. ossl_cmp_mock_srv_set1_caPubsOut(fixture->srv_ctx, fixture->caPubs);
  136. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  137. return result;
  138. }
  139. static const int checkAfter = 1;
  140. static int test_exec_IR_ses_poll(void)
  141. {
  142. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  143. fixture->req_type = OSSL_CMP_IR;
  144. fixture->expected = 1;
  145. ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 2);
  146. ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, checkAfter);
  147. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  148. return result;
  149. }
  150. static int test_exec_IR_ses_poll_timeout(void)
  151. {
  152. const int pollCount = 3;
  153. const int tout = pollCount * checkAfter;
  154. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  155. fixture->req_type = OSSL_CMP_IR;
  156. fixture->expected = 0;
  157. ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, pollCount + 1);
  158. ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, checkAfter);
  159. OSSL_CMP_CTX_set_option(fixture->cmp_ctx, OSSL_CMP_OPT_TOTAL_TIMEOUT, tout);
  160. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  161. return result;
  162. }
  163. static int test_exec_CR_ses(void)
  164. {
  165. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  166. fixture->req_type = OSSL_CMP_CR;
  167. fixture->expected = 1;
  168. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  169. return result;
  170. }
  171. static int test_exec_CR_ses_implicit_confirm(void)
  172. {
  173. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  174. fixture->req_type = OSSL_CMP_CR;
  175. fixture->expected = 1;
  176. OSSL_CMP_CTX_set_option(fixture->cmp_ctx,
  177. OSSL_CMP_OPT_IMPLICIT_CONFIRM, 1);
  178. OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(fixture->srv_ctx, 1);
  179. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  180. return result;
  181. }
  182. static int test_exec_KUR_ses(void)
  183. {
  184. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  185. fixture->req_type = OSSL_CMP_KUR;
  186. fixture->expected = 1;
  187. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  188. return result;
  189. }
  190. static int test_exec_P10CR_ses(void)
  191. {
  192. X509_REQ *req = NULL;
  193. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  194. fixture->req_type = OSSL_CMP_P10CR;
  195. fixture->expected = 1;
  196. if (!TEST_ptr(req = load_csr_der(pkcs10_f, libctx))
  197. || !TEST_true(OSSL_CMP_CTX_set1_p10CSR(fixture->cmp_ctx, req))) {
  198. tear_down(fixture);
  199. fixture = NULL;
  200. }
  201. X509_REQ_free(req);
  202. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  203. return result;
  204. }
  205. static int execute_try_certreq_poll_test(CMP_SES_TEST_FIXTURE *fixture)
  206. {
  207. OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
  208. int check_after;
  209. const int CHECK_AFTER = 5;
  210. const int TYPE = OSSL_CMP_KUR;
  211. ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
  212. ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
  213. return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
  214. && check_after == CHECK_AFTER
  215. && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
  216. && 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(fixture->expected,
  220. OSSL_CMP_try_certreq(ctx, TYPE, NULL, NULL))
  221. && TEST_int_eq(0,
  222. X509_cmp(OSSL_CMP_CTX_get0_newCert(ctx), client_cert));
  223. }
  224. static int test_try_certreq_poll(void)
  225. {
  226. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  227. fixture->expected = 1;
  228. EXECUTE_TEST(execute_try_certreq_poll_test, tear_down);
  229. return result;
  230. }
  231. static int execute_try_certreq_poll_abort_test(CMP_SES_TEST_FIXTURE *fixture)
  232. {
  233. OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
  234. int check_after;
  235. const int CHECK_AFTER = INT_MAX;
  236. const int TYPE = OSSL_CMP_CR;
  237. ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
  238. ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
  239. return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
  240. && check_after == CHECK_AFTER
  241. && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
  242. && TEST_int_eq(fixture->expected,
  243. OSSL_CMP_try_certreq(ctx, -1, NULL, NULL))
  244. && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(fixture->cmp_ctx), NULL);
  245. }
  246. static int test_try_certreq_poll_abort(void)
  247. {
  248. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  249. fixture->expected = 1;
  250. EXECUTE_TEST(execute_try_certreq_poll_abort_test, tear_down);
  251. return result;
  252. }
  253. static int test_exec_GENM_ses(void)
  254. {
  255. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  256. EXECUTE_TEST(execute_exec_GENM_ses_test, tear_down);
  257. return result;
  258. }
  259. static int execute_exchange_certConf_test(CMP_SES_TEST_FIXTURE *fixture)
  260. {
  261. int res =
  262. ossl_cmp_exchange_certConf(fixture->cmp_ctx,
  263. OSSL_CMP_PKIFAILUREINFO_addInfoNotAvailable,
  264. "abcdefg");
  265. return TEST_int_eq(fixture->expected, res);
  266. }
  267. static int execute_exchange_error_test(CMP_SES_TEST_FIXTURE *fixture)
  268. {
  269. int res =
  270. ossl_cmp_exchange_error(fixture->cmp_ctx,
  271. OSSL_CMP_PKISTATUS_rejection,
  272. 1 << OSSL_CMP_PKIFAILUREINFO_unsupportedVersion,
  273. "foo_status", 999, "foo_details");
  274. return TEST_int_eq(fixture->expected, res);
  275. }
  276. static int test_exchange_certConf(void)
  277. {
  278. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  279. fixture->expected = 0; /* client should not send certConf immediately */
  280. if (!ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx, X509_dup(client_cert))) {
  281. tear_down(fixture);
  282. fixture = NULL;
  283. }
  284. EXECUTE_TEST(execute_exchange_certConf_test, tear_down);
  285. return result;
  286. }
  287. static int test_exchange_error(void)
  288. {
  289. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  290. fixture->expected = 1; /* client may send error any time */
  291. EXECUTE_TEST(execute_exchange_error_test, tear_down);
  292. return result;
  293. }
  294. void cleanup_tests(void)
  295. {
  296. X509_free(server_cert);
  297. EVP_PKEY_free(server_key);
  298. X509_free(client_cert);
  299. EVP_PKEY_free(client_key);
  300. OSSL_LIB_CTX_free(libctx);
  301. return;
  302. }
  303. #define USAGE "server.key server.crt client.key client.crt client.csr module_name [module_conf_file]\n"
  304. OPT_TEST_DECLARE_USAGE(USAGE)
  305. int setup_tests(void)
  306. {
  307. if (!test_skip_common_options()) {
  308. TEST_error("Error parsing test options\n");
  309. return 0;
  310. }
  311. if (!TEST_ptr(server_key_f = test_get_argument(0))
  312. || !TEST_ptr(server_cert_f = test_get_argument(1))
  313. || !TEST_ptr(client_key_f = test_get_argument(2))
  314. || !TEST_ptr(client_cert_f = test_get_argument(3))
  315. || !TEST_ptr(pkcs10_f = test_get_argument(4))) {
  316. TEST_error("usage: cmp_client_test %s", USAGE);
  317. return 0;
  318. }
  319. if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 5, USAGE))
  320. return 0;
  321. if (!TEST_ptr(server_key = load_pkey_pem(server_key_f, libctx))
  322. || !TEST_ptr(server_cert = load_cert_pem(server_cert_f, libctx))
  323. || !TEST_ptr(client_key = load_pkey_pem(client_key_f, libctx))
  324. || !TEST_ptr(client_cert = load_cert_pem(client_cert_f, libctx))
  325. || !TEST_int_eq(1, RAND_bytes_ex(libctx, ref, sizeof(ref), 0))) {
  326. cleanup_tests();
  327. return 0;
  328. }
  329. ADD_TEST(test_exec_RR_ses);
  330. ADD_TEST(test_exec_RR_ses_receive_error);
  331. ADD_TEST(test_exec_CR_ses);
  332. ADD_TEST(test_exec_CR_ses_implicit_confirm);
  333. ADD_TEST(test_exec_IR_ses);
  334. ADD_TEST(test_exec_IR_ses_poll);
  335. ADD_TEST(test_exec_IR_ses_poll_timeout);
  336. ADD_TEST(test_exec_KUR_ses);
  337. ADD_TEST(test_exec_P10CR_ses);
  338. ADD_TEST(test_try_certreq_poll);
  339. ADD_TEST(test_try_certreq_poll_abort);
  340. ADD_TEST(test_exec_GENM_ses);
  341. ADD_TEST(test_exchange_certConf);
  342. ADD_TEST(test_exchange_error);
  343. return 1;
  344. }