2
0

cmp_client_test.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 *fixt)
  80. {
  81. return TEST_int_eq(OSSL_CMP_CTX_get_status(fixt->cmp_ctx),
  82. OSSL_CMP_PKISTATUS_unspecified)
  83. && TEST_int_eq(OSSL_CMP_exec_RR_ses(fixt->cmp_ctx),
  84. fixt->expected == OSSL_CMP_PKISTATUS_accepted)
  85. && TEST_int_eq(OSSL_CMP_CTX_get_status(fixt->cmp_ctx), fixt->expected);
  86. }
  87. static int execute_exec_GENM_ses_test_single(CMP_SES_TEST_FIXTURE *fixture)
  88. {
  89. OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
  90. ASN1_OBJECT *type = OBJ_txt2obj("1.3.6.1.5.5.7.4.2", 1);
  91. OSSL_CMP_ITAV *itav = OSSL_CMP_ITAV_create(type, NULL);
  92. STACK_OF(OSSL_CMP_ITAV) *itavs;
  93. OSSL_CMP_CTX_push0_genm_ITAV(ctx, itav);
  94. itavs = OSSL_CMP_exec_GENM_ses(ctx);
  95. sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
  96. return TEST_int_eq(OSSL_CMP_CTX_get_status(ctx), fixture->expected)
  97. && fixture->expected == OSSL_CMP_PKISTATUS_accepted ?
  98. TEST_ptr(itavs) : TEST_ptr_null(itavs);
  99. }
  100. static int execute_exec_GENM_ses_test(CMP_SES_TEST_FIXTURE *fixture)
  101. {
  102. return execute_exec_GENM_ses_test_single(fixture)
  103. && OSSL_CMP_CTX_reinit(fixture->cmp_ctx)
  104. && execute_exec_GENM_ses_test_single(fixture);
  105. }
  106. static int execute_exec_certrequest_ses_test(CMP_SES_TEST_FIXTURE *fixture)
  107. {
  108. OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
  109. X509 *res = OSSL_CMP_exec_certreq(ctx, fixture->req_type, NULL);
  110. int status = OSSL_CMP_CTX_get_status(ctx);
  111. if (!TEST_int_eq(status, fixture->expected)
  112. && !(fixture->expected == OSSL_CMP_PKISTATUS_waiting
  113. && TEST_int_eq(status, OSSL_CMP_PKISTATUS_trans)))
  114. return 0;
  115. if (fixture->expected != OSSL_CMP_PKISTATUS_accepted)
  116. return TEST_ptr_null(res);
  117. if (!TEST_ptr(res) || !TEST_int_eq(X509_cmp(res, client_cert), 0))
  118. return 0;
  119. if (fixture->caPubs != NULL) {
  120. STACK_OF(X509) *caPubs = OSSL_CMP_CTX_get1_caPubs(fixture->cmp_ctx);
  121. int ret = TEST_int_eq(STACK_OF_X509_cmp(fixture->caPubs, caPubs), 0);
  122. OSSL_STACK_OF_X509_free(caPubs);
  123. return ret;
  124. }
  125. return 1;
  126. }
  127. static int test_exec_RR_ses(int request_error)
  128. {
  129. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  130. if (request_error)
  131. OSSL_CMP_CTX_set1_oldCert(fixture->cmp_ctx, NULL);
  132. fixture->expected = request_error ? OSSL_CMP_PKISTATUS_request
  133. : OSSL_CMP_PKISTATUS_accepted;
  134. EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
  135. return result;
  136. }
  137. static int test_exec_RR_ses_ok(void)
  138. {
  139. return test_exec_RR_ses(0);
  140. }
  141. static int test_exec_RR_ses_request_error(void)
  142. {
  143. return test_exec_RR_ses(1);
  144. }
  145. static int test_exec_RR_ses_receive_error(void)
  146. {
  147. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  148. ossl_cmp_mock_srv_set_statusInfo(fixture->srv_ctx,
  149. OSSL_CMP_PKISTATUS_rejection,
  150. OSSL_CMP_CTX_FAILINFO_signerNotTrusted,
  151. "test string");
  152. ossl_cmp_mock_srv_set_send_error(fixture->srv_ctx, 1);
  153. fixture->expected = OSSL_CMP_PKISTATUS_rejection;
  154. EXECUTE_TEST(execute_exec_RR_ses_test, tear_down);
  155. return result;
  156. }
  157. static int test_exec_IR_ses(void)
  158. {
  159. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  160. fixture->req_type = OSSL_CMP_IR;
  161. fixture->expected = OSSL_CMP_PKISTATUS_accepted;
  162. fixture->caPubs = sk_X509_new_null();
  163. sk_X509_push(fixture->caPubs, server_cert);
  164. sk_X509_push(fixture->caPubs, server_cert);
  165. ossl_cmp_mock_srv_set1_caPubsOut(fixture->srv_ctx, fixture->caPubs);
  166. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  167. return result;
  168. }
  169. static int test_exec_IR_ses_poll(int check_after, int poll_count,
  170. int total_timeout, int expect)
  171. {
  172. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  173. fixture->req_type = OSSL_CMP_IR;
  174. fixture->expected = expect;
  175. ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, check_after);
  176. ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, poll_count);
  177. OSSL_CMP_CTX_set_option(fixture->cmp_ctx,
  178. OSSL_CMP_OPT_TOTAL_TIMEOUT, total_timeout);
  179. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  180. return result;
  181. }
  182. static int checkAfter = 1;
  183. static int test_exec_IR_ses_poll_ok(void)
  184. {
  185. return test_exec_IR_ses_poll(checkAfter, 2, 0, OSSL_CMP_PKISTATUS_accepted);
  186. }
  187. static int test_exec_IR_ses_poll_no_timeout(void)
  188. {
  189. return test_exec_IR_ses_poll(checkAfter, 1 /* pollCount */, checkAfter + 1,
  190. OSSL_CMP_PKISTATUS_accepted);
  191. }
  192. static int test_exec_IR_ses_poll_total_timeout(void)
  193. {
  194. return test_exec_IR_ses_poll(checkAfter + 1, 2 /* pollCount */, checkAfter,
  195. OSSL_CMP_PKISTATUS_waiting);
  196. }
  197. static int test_exec_CR_ses(int implicit_confirm, int granted)
  198. {
  199. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  200. fixture->req_type = OSSL_CMP_CR;
  201. fixture->expected = OSSL_CMP_PKISTATUS_accepted;
  202. OSSL_CMP_CTX_set_option(fixture->cmp_ctx,
  203. OSSL_CMP_OPT_IMPLICIT_CONFIRM, implicit_confirm);
  204. OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(fixture->srv_ctx, granted);
  205. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  206. return result;
  207. }
  208. static int test_exec_CR_ses_explicit_confirm(void)
  209. {
  210. return test_exec_CR_ses(0, 0);
  211. }
  212. static int test_exec_CR_ses_implicit_confirm(void)
  213. {
  214. return test_exec_CR_ses(1, 0)
  215. && test_exec_CR_ses(1, 1);
  216. }
  217. static int test_exec_KUR_ses(int transfer_error)
  218. {
  219. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  220. fixture->req_type = OSSL_CMP_KUR;
  221. if (transfer_error)
  222. OSSL_CMP_CTX_set_transfer_cb_arg(fixture->cmp_ctx, NULL);
  223. fixture->expected = transfer_error ? OSSL_CMP_PKISTATUS_trans
  224. : OSSL_CMP_PKISTATUS_accepted;
  225. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  226. return result;
  227. }
  228. static int test_exec_KUR_ses_ok(void)
  229. {
  230. return test_exec_KUR_ses(0);
  231. }
  232. static int test_exec_KUR_ses_transfer_error(void)
  233. {
  234. return test_exec_KUR_ses(1);
  235. }
  236. static int test_exec_P10CR_ses(void)
  237. {
  238. X509_REQ *req = NULL;
  239. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  240. fixture->req_type = OSSL_CMP_P10CR;
  241. fixture->expected = OSSL_CMP_PKISTATUS_accepted;
  242. if (!TEST_ptr(req = load_csr_der(pkcs10_f, libctx))
  243. || !TEST_true(OSSL_CMP_CTX_set1_p10CSR(fixture->cmp_ctx, req))) {
  244. tear_down(fixture);
  245. fixture = NULL;
  246. }
  247. X509_REQ_free(req);
  248. EXECUTE_TEST(execute_exec_certrequest_ses_test, tear_down);
  249. return result;
  250. }
  251. static int execute_try_certreq_poll_test(CMP_SES_TEST_FIXTURE *fixture)
  252. {
  253. OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
  254. int check_after;
  255. const int CHECK_AFTER = 5;
  256. const int TYPE = OSSL_CMP_KUR;
  257. ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
  258. ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
  259. return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
  260. && check_after == CHECK_AFTER
  261. && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
  262. && TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
  263. && check_after == CHECK_AFTER
  264. && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
  265. && TEST_int_eq(fixture->expected,
  266. OSSL_CMP_try_certreq(ctx, TYPE, NULL, NULL))
  267. && TEST_int_eq(0,
  268. X509_cmp(OSSL_CMP_CTX_get0_newCert(ctx), client_cert));
  269. }
  270. static int test_try_certreq_poll(void)
  271. {
  272. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  273. fixture->expected = 1;
  274. EXECUTE_TEST(execute_try_certreq_poll_test, tear_down);
  275. return result;
  276. }
  277. static int execute_try_certreq_poll_abort_test(CMP_SES_TEST_FIXTURE *fixture)
  278. {
  279. OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
  280. int check_after;
  281. const int CHECK_AFTER = INT_MAX;
  282. const int TYPE = OSSL_CMP_CR;
  283. ossl_cmp_mock_srv_set_pollCount(fixture->srv_ctx, 3);
  284. ossl_cmp_mock_srv_set_checkAfterTime(fixture->srv_ctx, CHECK_AFTER);
  285. return TEST_int_eq(-1, OSSL_CMP_try_certreq(ctx, TYPE, NULL, &check_after))
  286. && check_after == CHECK_AFTER
  287. && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(ctx), NULL)
  288. && TEST_int_eq(fixture->expected,
  289. OSSL_CMP_try_certreq(ctx, -1, NULL, NULL))
  290. && TEST_ptr_eq(OSSL_CMP_CTX_get0_newCert(fixture->cmp_ctx), NULL);
  291. }
  292. static int test_try_certreq_poll_abort(void)
  293. {
  294. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  295. fixture->expected = 1;
  296. EXECUTE_TEST(execute_try_certreq_poll_abort_test, tear_down);
  297. return result;
  298. }
  299. static int test_exec_GENM_ses(int transfer_error)
  300. {
  301. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  302. if (transfer_error)
  303. OSSL_CMP_CTX_set_transfer_cb_arg(fixture->cmp_ctx, NULL);
  304. fixture->expected = transfer_error ? OSSL_CMP_PKISTATUS_trans
  305. : OSSL_CMP_PKISTATUS_accepted;
  306. EXECUTE_TEST(execute_exec_GENM_ses_test, tear_down);
  307. return result;
  308. }
  309. static int test_exec_GENM_ses_ok(void)
  310. {
  311. return test_exec_GENM_ses(0);
  312. }
  313. static int test_exec_GENM_ses_error(void)
  314. {
  315. return test_exec_GENM_ses(1);
  316. }
  317. static int execute_exchange_certConf_test(CMP_SES_TEST_FIXTURE *fixture)
  318. {
  319. int res =
  320. ossl_cmp_exchange_certConf(fixture->cmp_ctx,
  321. OSSL_CMP_PKIFAILUREINFO_addInfoNotAvailable,
  322. "abcdefg");
  323. return TEST_int_eq(fixture->expected, res);
  324. }
  325. static int execute_exchange_error_test(CMP_SES_TEST_FIXTURE *fixture)
  326. {
  327. int res =
  328. ossl_cmp_exchange_error(fixture->cmp_ctx,
  329. OSSL_CMP_PKISTATUS_rejection,
  330. 1 << OSSL_CMP_PKIFAILUREINFO_unsupportedVersion,
  331. "foo_status", 999, "foo_details");
  332. return TEST_int_eq(fixture->expected, res);
  333. }
  334. static int test_exchange_certConf(void)
  335. {
  336. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  337. fixture->expected = 0; /* client should not send certConf immediately */
  338. if (!ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx, X509_dup(client_cert))) {
  339. tear_down(fixture);
  340. fixture = NULL;
  341. }
  342. EXECUTE_TEST(execute_exchange_certConf_test, tear_down);
  343. return result;
  344. }
  345. static int test_exchange_error(void)
  346. {
  347. SETUP_TEST_FIXTURE(CMP_SES_TEST_FIXTURE, set_up);
  348. fixture->expected = 1; /* client may send error any time */
  349. EXECUTE_TEST(execute_exchange_error_test, tear_down);
  350. return result;
  351. }
  352. void cleanup_tests(void)
  353. {
  354. X509_free(server_cert);
  355. EVP_PKEY_free(server_key);
  356. X509_free(client_cert);
  357. EVP_PKEY_free(client_key);
  358. OSSL_LIB_CTX_free(libctx);
  359. return;
  360. }
  361. #define USAGE "server.key server.crt client.key client.crt client.csr module_name [module_conf_file]\n"
  362. OPT_TEST_DECLARE_USAGE(USAGE)
  363. int setup_tests(void)
  364. {
  365. if (!test_skip_common_options()) {
  366. TEST_error("Error parsing test options\n");
  367. return 0;
  368. }
  369. if (!TEST_ptr(server_key_f = test_get_argument(0))
  370. || !TEST_ptr(server_cert_f = test_get_argument(1))
  371. || !TEST_ptr(client_key_f = test_get_argument(2))
  372. || !TEST_ptr(client_cert_f = test_get_argument(3))
  373. || !TEST_ptr(pkcs10_f = test_get_argument(4))) {
  374. TEST_error("usage: cmp_client_test %s", USAGE);
  375. return 0;
  376. }
  377. if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 5, USAGE))
  378. return 0;
  379. if (!TEST_ptr(server_key = load_pkey_pem(server_key_f, libctx))
  380. || !TEST_ptr(server_cert = load_cert_pem(server_cert_f, libctx))
  381. || !TEST_ptr(client_key = load_pkey_pem(client_key_f, libctx))
  382. || !TEST_ptr(client_cert = load_cert_pem(client_cert_f, libctx))
  383. || !TEST_int_eq(1, RAND_bytes_ex(libctx, ref, sizeof(ref), 0))) {
  384. cleanup_tests();
  385. return 0;
  386. }
  387. ADD_TEST(test_exec_RR_ses_ok);
  388. ADD_TEST(test_exec_RR_ses_request_error);
  389. ADD_TEST(test_exec_RR_ses_receive_error);
  390. ADD_TEST(test_exec_CR_ses_explicit_confirm);
  391. ADD_TEST(test_exec_CR_ses_implicit_confirm);
  392. ADD_TEST(test_exec_IR_ses);
  393. ADD_TEST(test_exec_IR_ses_poll_ok);
  394. ADD_TEST(test_exec_IR_ses_poll_no_timeout);
  395. ADD_TEST(test_exec_IR_ses_poll_total_timeout);
  396. ADD_TEST(test_exec_KUR_ses_ok);
  397. ADD_TEST(test_exec_KUR_ses_transfer_error);
  398. ADD_TEST(test_exec_P10CR_ses);
  399. ADD_TEST(test_try_certreq_poll);
  400. ADD_TEST(test_try_certreq_poll_abort);
  401. ADD_TEST(test_exec_GENM_ses_ok);
  402. ADD_TEST(test_exec_GENM_ses_error);
  403. ADD_TEST(test_exchange_certConf);
  404. ADD_TEST(test_exchange_error);
  405. return 1;
  406. }