cmp_protect_test.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. static const char *ir_protected_f;
  13. static const char *genm_prot_Ed_f;
  14. static const char *ir_unprotected_f;
  15. static const char *ip_PBM_f;
  16. typedef struct test_fixture {
  17. const char *test_case_name;
  18. OSSL_CMP_CTX *cmp_ctx;
  19. /* for protection tests */
  20. OSSL_CMP_MSG *msg;
  21. OSSL_CMP_PKISI *si; /* for error and response messages */
  22. EVP_PKEY *pubkey;
  23. unsigned char *mem;
  24. int memlen;
  25. X509 *cert;
  26. STACK_OF(X509) *certs;
  27. STACK_OF(X509) *chain;
  28. int with_ss;
  29. int callback_arg;
  30. int expected;
  31. } CMP_PROTECT_TEST_FIXTURE;
  32. static OSSL_LIB_CTX *libctx = NULL;
  33. static OSSL_PROVIDER *default_null_provider = NULL, *provider = NULL;
  34. static void tear_down(CMP_PROTECT_TEST_FIXTURE *fixture)
  35. {
  36. if (fixture != NULL) {
  37. OSSL_CMP_CTX_free(fixture->cmp_ctx);
  38. OSSL_CMP_MSG_free(fixture->msg);
  39. OSSL_CMP_PKISI_free(fixture->si);
  40. OPENSSL_free(fixture->mem);
  41. sk_X509_free(fixture->certs);
  42. sk_X509_free(fixture->chain);
  43. OPENSSL_free(fixture);
  44. }
  45. }
  46. static CMP_PROTECT_TEST_FIXTURE *set_up(const char *const test_case_name)
  47. {
  48. CMP_PROTECT_TEST_FIXTURE *fixture;
  49. if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
  50. return NULL;
  51. fixture->test_case_name = test_case_name;
  52. if (!TEST_ptr(fixture->cmp_ctx = OSSL_CMP_CTX_new(libctx, NULL))) {
  53. tear_down(fixture);
  54. return NULL;
  55. }
  56. return fixture;
  57. }
  58. static EVP_PKEY *prot_RSA_key = NULL;
  59. #ifndef OPENSSL_NO_ECX
  60. static EVP_PKEY *prot_Ed_key = NULL;
  61. static OSSL_CMP_MSG *genm_protected_Ed;
  62. #endif
  63. static EVP_PKEY *server_key = NULL;
  64. static X509 *server_cert = NULL;
  65. static unsigned char rand_data[OSSL_CMP_TRANSACTIONID_LENGTH];
  66. static OSSL_CMP_MSG *ir_unprotected, *ir_protected;
  67. static X509 *endentity1 = NULL, *endentity2 = NULL,
  68. *root = NULL, *intermediate = NULL;
  69. static int execute_calc_protection_fails_test(CMP_PROTECT_TEST_FIXTURE *fixture)
  70. {
  71. ASN1_BIT_STRING *protection =
  72. ossl_cmp_calc_protection(fixture->cmp_ctx, fixture->msg);
  73. int res = TEST_ptr_null(protection);
  74. ASN1_BIT_STRING_free(protection);
  75. return res;
  76. }
  77. static int execute_calc_protection_pbmac_test(CMP_PROTECT_TEST_FIXTURE *fixture)
  78. {
  79. ASN1_BIT_STRING *protection =
  80. ossl_cmp_calc_protection(fixture->cmp_ctx, fixture->msg);
  81. int res = TEST_ptr(protection)
  82. && TEST_true(ASN1_STRING_cmp(protection,
  83. fixture->msg->protection) == 0);
  84. ASN1_BIT_STRING_free(protection);
  85. return res;
  86. }
  87. /*
  88. * This function works similarly to parts of verify_signature in cmp_vfy.c,
  89. * but without the need for an OSSL_CMP_CTX or an X509 certificate.
  90. */
  91. static int verify_signature(OSSL_CMP_MSG *msg,
  92. ASN1_BIT_STRING *protection,
  93. EVP_PKEY *pkey, EVP_MD *digest)
  94. {
  95. OSSL_CMP_PROTECTEDPART prot_part;
  96. prot_part.header = OSSL_CMP_MSG_get0_header(msg);
  97. prot_part.body = msg->body;
  98. return ASN1_item_verify_ex(ASN1_ITEM_rptr(OSSL_CMP_PROTECTEDPART),
  99. msg->header->protectionAlg, protection,
  100. &prot_part, NULL, pkey, libctx, NULL) > 0;
  101. }
  102. /* Calls OSSL_CMP_calc_protection and compares and verifies signature */
  103. static int execute_calc_protection_signature_test(CMP_PROTECT_TEST_FIXTURE *
  104. fixture)
  105. {
  106. ASN1_BIT_STRING *protection =
  107. ossl_cmp_calc_protection(fixture->cmp_ctx, fixture->msg);
  108. int ret = (TEST_ptr(protection)
  109. && TEST_true(verify_signature(fixture->msg, protection,
  110. fixture->pubkey,
  111. fixture->cmp_ctx->digest)));
  112. ASN1_BIT_STRING_free(protection);
  113. return ret;
  114. }
  115. static int test_cmp_calc_protection_no_key_no_secret(void)
  116. {
  117. SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
  118. if (!TEST_ptr(fixture->msg = load_pkimsg(ir_unprotected_f, libctx))
  119. || !TEST_ptr(fixture->msg->header->protectionAlg =
  120. X509_ALGOR_new() /* no specific alg needed here */)) {
  121. tear_down(fixture);
  122. fixture = NULL;
  123. }
  124. EXECUTE_TEST(execute_calc_protection_fails_test, tear_down);
  125. return result;
  126. }
  127. static int test_cmp_calc_protection_pkey(void)
  128. {
  129. SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
  130. fixture->pubkey = prot_RSA_key;
  131. if (!TEST_true(OSSL_CMP_CTX_set1_pkey(fixture->cmp_ctx, prot_RSA_key))
  132. || !TEST_ptr(fixture->msg = load_pkimsg(ir_protected_f, libctx))) {
  133. tear_down(fixture);
  134. fixture = NULL;
  135. }
  136. EXECUTE_TEST(execute_calc_protection_signature_test, tear_down);
  137. return result;
  138. }
  139. #ifndef OPENSSL_NO_ECX
  140. static int test_cmp_calc_protection_pkey_Ed(void)
  141. {
  142. SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
  143. fixture->pubkey = prot_Ed_key;
  144. if (!TEST_true(OSSL_CMP_CTX_set1_pkey(fixture->cmp_ctx, prot_Ed_key))
  145. || !TEST_ptr(fixture->msg = load_pkimsg(genm_prot_Ed_f, libctx))) {
  146. tear_down(fixture);
  147. fixture = NULL;
  148. }
  149. EXECUTE_TEST(execute_calc_protection_signature_test, tear_down);
  150. return result;
  151. }
  152. #endif
  153. static int test_cmp_calc_protection_pbmac(void)
  154. {
  155. unsigned char sec_insta[] = { 'i', 'n', 's', 't', 'a' };
  156. SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
  157. if (!TEST_true(OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx,
  158. sec_insta, sizeof(sec_insta)))
  159. || !TEST_ptr(fixture->msg = load_pkimsg(ip_PBM_f, libctx))) {
  160. tear_down(fixture);
  161. fixture = NULL;
  162. }
  163. EXECUTE_TEST(execute_calc_protection_pbmac_test, tear_down);
  164. return result;
  165. }
  166. static int execute_MSG_protect_test(CMP_PROTECT_TEST_FIXTURE *fixture)
  167. {
  168. return TEST_int_eq(fixture->expected,
  169. ossl_cmp_msg_protect(fixture->cmp_ctx, fixture->msg));
  170. }
  171. #define SET_OPT_UNPROTECTED_SEND(ctx, val) \
  172. OSSL_CMP_CTX_set_option((ctx), OSSL_CMP_OPT_UNPROTECTED_SEND, (val))
  173. static int test_MSG_protect_unprotected_request(void)
  174. {
  175. SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
  176. fixture->expected = 1;
  177. if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected))
  178. || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 1))) {
  179. tear_down(fixture);
  180. fixture = NULL;
  181. }
  182. EXECUTE_TEST(execute_MSG_protect_test, tear_down);
  183. return result;
  184. }
  185. static int test_MSG_protect_with_msg_sig_alg_protection_plus_rsa_key(void)
  186. {
  187. const size_t size = sizeof(rand_data) / 2;
  188. SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
  189. fixture->expected = 1;
  190. if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected))
  191. || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0))
  192. /*
  193. * Use half of the 16 bytes of random input
  194. * for each reference and secret value
  195. */
  196. || !TEST_true(OSSL_CMP_CTX_set1_referenceValue(fixture->cmp_ctx,
  197. rand_data, size))
  198. || !TEST_true(OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx,
  199. rand_data + size,
  200. size))) {
  201. tear_down(fixture);
  202. fixture = NULL;
  203. }
  204. EXECUTE_TEST(execute_MSG_protect_test, tear_down);
  205. return result;
  206. }
  207. static int test_MSG_protect_with_certificate_and_key(void)
  208. {
  209. SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
  210. fixture->expected = 1;
  211. if (!TEST_ptr(fixture->msg =
  212. OSSL_CMP_MSG_dup(ir_unprotected))
  213. || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0))
  214. || !TEST_true(OSSL_CMP_CTX_set1_pkey(fixture->cmp_ctx, server_key))
  215. || !TEST_true(OSSL_CMP_CTX_set1_cert(fixture->cmp_ctx,
  216. server_cert))) {
  217. tear_down(fixture);
  218. fixture = NULL;
  219. }
  220. EXECUTE_TEST(execute_MSG_protect_test, tear_down);
  221. return result;
  222. }
  223. static int test_MSG_protect_certificate_based_without_cert(void)
  224. {
  225. OSSL_CMP_CTX *ctx;
  226. SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
  227. ctx = fixture->cmp_ctx;
  228. fixture->expected = 0;
  229. if (!TEST_ptr(fixture->msg =
  230. OSSL_CMP_MSG_dup(ir_unprotected))
  231. || !TEST_true(SET_OPT_UNPROTECTED_SEND(ctx, 0))
  232. || !TEST_true(OSSL_CMP_CTX_set0_newPkey(ctx, 1, server_key))) {
  233. tear_down(fixture);
  234. fixture = NULL;
  235. }
  236. EVP_PKEY_up_ref(server_key);
  237. EXECUTE_TEST(execute_MSG_protect_test, tear_down);
  238. return result;
  239. }
  240. static int test_MSG_protect_no_key_no_secret(void)
  241. {
  242. SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
  243. fixture->expected = 0;
  244. if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected))
  245. || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0))) {
  246. tear_down(fixture);
  247. fixture = NULL;
  248. }
  249. EXECUTE_TEST(execute_MSG_protect_test, tear_down);
  250. return result;
  251. }
  252. static int test_MSG_protect_pbmac_no_sender(int with_ref)
  253. {
  254. static unsigned char secret[] = { 47, 11, 8, 15 };
  255. static unsigned char ref[] = { 0xca, 0xfe, 0xba, 0xbe };
  256. SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
  257. fixture->expected = with_ref;
  258. if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_unprotected))
  259. || !SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0)
  260. || !ossl_cmp_hdr_set1_sender(fixture->msg->header, NULL)
  261. || !OSSL_CMP_CTX_set1_secretValue(fixture->cmp_ctx,
  262. secret, sizeof(secret))
  263. || (!OSSL_CMP_CTX_set1_referenceValue(fixture->cmp_ctx,
  264. with_ref ? ref : NULL,
  265. sizeof(ref)))) {
  266. tear_down(fixture);
  267. fixture = NULL;
  268. }
  269. EXECUTE_TEST(execute_MSG_protect_test, tear_down);
  270. return result;
  271. }
  272. static int test_MSG_protect_pbmac_no_sender_with_ref(void)
  273. {
  274. return test_MSG_protect_pbmac_no_sender(1);
  275. }
  276. static int test_MSG_protect_pbmac_no_sender_no_ref(void)
  277. {
  278. return test_MSG_protect_pbmac_no_sender(0);
  279. }
  280. static int execute_MSG_add_extraCerts_test(CMP_PROTECT_TEST_FIXTURE *fixture)
  281. {
  282. return TEST_true(ossl_cmp_msg_add_extraCerts(fixture->cmp_ctx,
  283. fixture->msg));
  284. }
  285. static int test_MSG_add_extraCerts(void)
  286. {
  287. SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
  288. if (!TEST_ptr(fixture->msg = OSSL_CMP_MSG_dup(ir_protected))) {
  289. tear_down(fixture);
  290. fixture = NULL;
  291. }
  292. EXECUTE_TEST(execute_MSG_add_extraCerts_test, tear_down);
  293. return result;
  294. }
  295. #ifndef OPENSSL_NO_EC
  296. /* The cert chain tests use EC certs so we skip them in no-ec builds */
  297. static int execute_cmp_build_cert_chain_test(CMP_PROTECT_TEST_FIXTURE *fixture)
  298. {
  299. int ret = 0;
  300. OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
  301. X509_STORE *store;
  302. STACK_OF(X509) *chain =
  303. X509_build_chain(fixture->cert, fixture->certs, NULL,
  304. fixture->with_ss, ctx->libctx, ctx->propq);
  305. if (TEST_ptr(chain)) {
  306. /* Check whether chain built is equal to the expected one */
  307. ret = TEST_int_eq(0, STACK_OF_X509_cmp(chain, fixture->chain));
  308. OSSL_STACK_OF_X509_free(chain);
  309. }
  310. if (!ret)
  311. return 0;
  312. if (TEST_ptr(store = X509_STORE_new())
  313. && TEST_true(X509_STORE_add_cert(store, root))) {
  314. X509_VERIFY_PARAM_set_flags(X509_STORE_get0_param(store),
  315. X509_V_FLAG_NO_CHECK_TIME);
  316. chain = X509_build_chain(fixture->cert, fixture->certs, store,
  317. fixture->with_ss, ctx->libctx, ctx->propq);
  318. ret = TEST_int_eq(fixture->expected, chain != NULL);
  319. if (ret && chain != NULL) {
  320. /* Check whether chain built is equal to the expected one */
  321. ret = TEST_int_eq(0, STACK_OF_X509_cmp(chain, fixture->chain));
  322. OSSL_STACK_OF_X509_free(chain);
  323. }
  324. }
  325. X509_STORE_free(store);
  326. return ret;
  327. }
  328. static int test_cmp_build_cert_chain(void)
  329. {
  330. SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
  331. fixture->expected = 1;
  332. fixture->with_ss = 0;
  333. fixture->cert = endentity2;
  334. if (!TEST_ptr(fixture->certs = sk_X509_new_null())
  335. || !TEST_ptr(fixture->chain = sk_X509_new_null())
  336. || !TEST_true(sk_X509_push(fixture->certs, endentity1))
  337. || !TEST_true(sk_X509_push(fixture->certs, root))
  338. || !TEST_true(sk_X509_push(fixture->certs, intermediate))
  339. || !TEST_true(sk_X509_push(fixture->chain, endentity2))
  340. || !TEST_true(sk_X509_push(fixture->chain, intermediate))) {
  341. tear_down(fixture);
  342. fixture = NULL;
  343. }
  344. if (fixture != NULL) {
  345. result = execute_cmp_build_cert_chain_test(fixture);
  346. fixture->with_ss = 1;
  347. if (result && TEST_true(sk_X509_push(fixture->chain, root)))
  348. result = execute_cmp_build_cert_chain_test(fixture);
  349. }
  350. tear_down(fixture);
  351. return result;
  352. }
  353. static int test_cmp_build_cert_chain_missing_intermediate(void)
  354. {
  355. SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
  356. fixture->expected = 0;
  357. fixture->with_ss = 0;
  358. fixture->cert = endentity2;
  359. if (!TEST_ptr(fixture->certs = sk_X509_new_null())
  360. || !TEST_ptr(fixture->chain = sk_X509_new_null())
  361. || !TEST_true(sk_X509_push(fixture->certs, endentity1))
  362. || !TEST_true(sk_X509_push(fixture->certs, root))
  363. || !TEST_true(sk_X509_push(fixture->chain, endentity2))) {
  364. tear_down(fixture);
  365. fixture = NULL;
  366. }
  367. EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down);
  368. return result;
  369. }
  370. static int test_cmp_build_cert_chain_no_root(void)
  371. {
  372. SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
  373. fixture->expected = 1;
  374. fixture->with_ss = 0;
  375. fixture->cert = endentity2;
  376. if (!TEST_ptr(fixture->certs = sk_X509_new_null())
  377. || !TEST_ptr(fixture->chain = sk_X509_new_null())
  378. || !TEST_true(sk_X509_push(fixture->certs, endentity1))
  379. || !TEST_true(sk_X509_push(fixture->certs, intermediate))
  380. || !TEST_true(sk_X509_push(fixture->chain, endentity2))
  381. || !TEST_true(sk_X509_push(fixture->chain, intermediate))) {
  382. tear_down(fixture);
  383. fixture = NULL;
  384. }
  385. EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down);
  386. return result;
  387. }
  388. static int test_cmp_build_cert_chain_only_root(void)
  389. {
  390. SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
  391. fixture->expected = 1;
  392. fixture->with_ss = 0; /* still chain must include the only cert (root) */
  393. fixture->cert = root;
  394. if (!TEST_ptr(fixture->certs = sk_X509_new_null())
  395. || !TEST_ptr(fixture->chain = sk_X509_new_null())
  396. || !TEST_true(sk_X509_push(fixture->certs, root))
  397. || !TEST_true(sk_X509_push(fixture->chain, root))) {
  398. tear_down(fixture);
  399. fixture = NULL;
  400. }
  401. EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down);
  402. return result;
  403. }
  404. static int test_cmp_build_cert_chain_no_certs(void)
  405. {
  406. SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
  407. fixture->expected = 0;
  408. fixture->with_ss = 0;
  409. fixture->cert = endentity2;
  410. if (!TEST_ptr(fixture->certs = sk_X509_new_null())
  411. || !TEST_ptr(fixture->chain = sk_X509_new_null())
  412. || !TEST_true(sk_X509_push(fixture->chain, endentity2))) {
  413. tear_down(fixture);
  414. fixture = NULL;
  415. }
  416. EXECUTE_TEST(execute_cmp_build_cert_chain_test, tear_down);
  417. return result;
  418. }
  419. #endif /* OPENSSL_NO_EC */
  420. static int execute_X509_STORE_test(CMP_PROTECT_TEST_FIXTURE *fixture)
  421. {
  422. X509_STORE *store = X509_STORE_new();
  423. STACK_OF(X509) *sk = NULL;
  424. int res = 0;
  425. if (!TEST_true(ossl_cmp_X509_STORE_add1_certs(store,
  426. fixture->certs,
  427. fixture->callback_arg)))
  428. goto err;
  429. sk = X509_STORE_get1_all_certs(store);
  430. if (!TEST_int_eq(0, STACK_OF_X509_cmp(sk, fixture->chain)))
  431. goto err;
  432. res = 1;
  433. err:
  434. X509_STORE_free(store);
  435. OSSL_STACK_OF_X509_free(sk);
  436. return res;
  437. }
  438. static int test_X509_STORE(void)
  439. {
  440. SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
  441. fixture->callback_arg = 0; /* self-issued allowed */
  442. if (!TEST_ptr(fixture->certs = sk_X509_new_null())
  443. || !sk_X509_push(fixture->certs, endentity1)
  444. || !sk_X509_push(fixture->certs, endentity2)
  445. || !sk_X509_push(fixture->certs, root)
  446. || !sk_X509_push(fixture->certs, intermediate)
  447. || !TEST_ptr(fixture->chain = sk_X509_dup(fixture->certs))) {
  448. tear_down(fixture);
  449. fixture = NULL;
  450. }
  451. EXECUTE_TEST(execute_X509_STORE_test, tear_down);
  452. return result;
  453. }
  454. static int test_X509_STORE_only_self_issued(void)
  455. {
  456. SETUP_TEST_FIXTURE(CMP_PROTECT_TEST_FIXTURE, set_up);
  457. fixture->certs = sk_X509_new_null();
  458. fixture->chain = sk_X509_new_null();
  459. fixture->callback_arg = 1; /* only self-issued */
  460. if (!TEST_true(sk_X509_push(fixture->certs, endentity1))
  461. || !TEST_true(sk_X509_push(fixture->certs, endentity2))
  462. || !TEST_true(sk_X509_push(fixture->certs, root))
  463. || !TEST_true(sk_X509_push(fixture->certs, intermediate))
  464. || !TEST_true(sk_X509_push(fixture->chain, root))) {
  465. tear_down(fixture);
  466. fixture = NULL;
  467. }
  468. EXECUTE_TEST(execute_X509_STORE_test, tear_down);
  469. return result;
  470. }
  471. void cleanup_tests(void)
  472. {
  473. EVP_PKEY_free(prot_RSA_key);
  474. #ifndef OPENSSL_NO_ECX
  475. EVP_PKEY_free(prot_Ed_key);
  476. OSSL_CMP_MSG_free(genm_protected_Ed);
  477. #endif
  478. EVP_PKEY_free(server_key);
  479. X509_free(server_cert);
  480. X509_free(endentity1);
  481. X509_free(endentity2);
  482. X509_free(root);
  483. X509_free(intermediate);
  484. OSSL_CMP_MSG_free(ir_protected);
  485. OSSL_CMP_MSG_free(ir_unprotected);
  486. OSSL_PROVIDER_unload(default_null_provider);
  487. OSSL_PROVIDER_unload(provider);
  488. OSSL_LIB_CTX_free(libctx);
  489. }
  490. #define USAGE "prot_RSA.pem IR_protected.der prot_Ed.pem " \
  491. "GENM_protected_Ed.der IR_unprotected.der IP_PBM.der " \
  492. "server.crt server.pem EndEntity1.crt EndEntity2.crt Root_CA.crt " \
  493. "Intermediate_CA.crt module_name [module_conf_file]\n"
  494. OPT_TEST_DECLARE_USAGE(USAGE)
  495. int setup_tests(void)
  496. {
  497. char *prot_RSA_f;
  498. char *prot_Ed_f;
  499. char *server_key_f;
  500. char *server_cert_f;
  501. char *endentity1_f;
  502. char *endentity2_f;
  503. char *root_f;
  504. char *intermediate_f;
  505. if (!test_skip_common_options()) {
  506. TEST_error("Error parsing test options\n");
  507. return 0;
  508. }
  509. RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH);
  510. if (!TEST_ptr(prot_RSA_f = test_get_argument(0))
  511. || !TEST_ptr(ir_protected_f = test_get_argument(1))
  512. || !TEST_ptr(prot_Ed_f = test_get_argument(2))
  513. || !TEST_ptr(genm_prot_Ed_f = test_get_argument(3))
  514. || !TEST_ptr(ir_unprotected_f = test_get_argument(4))
  515. || !TEST_ptr(ip_PBM_f = test_get_argument(5))
  516. || !TEST_ptr(server_cert_f = test_get_argument(6))
  517. || !TEST_ptr(server_key_f = test_get_argument(7))
  518. || !TEST_ptr(endentity1_f = test_get_argument(8))
  519. || !TEST_ptr(endentity2_f = test_get_argument(9))
  520. || !TEST_ptr(root_f = test_get_argument(10))
  521. || !TEST_ptr(intermediate_f = test_get_argument(11))) {
  522. TEST_error("usage: cmp_protect_test %s", USAGE);
  523. return 0;
  524. }
  525. if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 12, USAGE))
  526. return 0;
  527. if (!TEST_ptr(server_key = load_pkey_pem(server_key_f, libctx))
  528. || !TEST_ptr(server_cert = load_cert_pem(server_cert_f, libctx)))
  529. return 0;
  530. if (!TEST_ptr(prot_RSA_key = load_pkey_pem(prot_RSA_f, libctx)))
  531. return 0;
  532. #ifndef OPENSSL_NO_ECX
  533. if (!TEST_ptr(prot_Ed_key = load_pkey_pem(prot_Ed_f, libctx)))
  534. return 0;
  535. #endif
  536. if (!TEST_ptr(ir_protected = load_pkimsg(ir_protected_f, libctx))
  537. #ifndef OPENSSL_NO_ECX
  538. || !TEST_ptr(genm_protected_Ed = load_pkimsg(genm_prot_Ed_f, libctx))
  539. #endif
  540. || !TEST_ptr(ir_unprotected = load_pkimsg(ir_unprotected_f, libctx)))
  541. return 0;
  542. if (!TEST_ptr(endentity1 = load_cert_pem(endentity1_f, libctx))
  543. || !TEST_ptr(endentity2 = load_cert_pem(endentity2_f, libctx))
  544. || !TEST_ptr(root = load_cert_pem(root_f, libctx))
  545. || !TEST_ptr(intermediate = load_cert_pem(intermediate_f, libctx)))
  546. return 0;
  547. if (!TEST_int_eq(1, RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH)))
  548. return 0;
  549. /* Message protection tests */
  550. ADD_TEST(test_cmp_calc_protection_no_key_no_secret);
  551. ADD_TEST(test_cmp_calc_protection_pkey);
  552. #ifndef OPENSSL_NO_ECX
  553. ADD_TEST(test_cmp_calc_protection_pkey_Ed);
  554. #endif
  555. ADD_TEST(test_cmp_calc_protection_pbmac);
  556. ADD_TEST(test_MSG_protect_with_msg_sig_alg_protection_plus_rsa_key);
  557. ADD_TEST(test_MSG_protect_with_certificate_and_key);
  558. ADD_TEST(test_MSG_protect_certificate_based_without_cert);
  559. ADD_TEST(test_MSG_protect_unprotected_request);
  560. ADD_TEST(test_MSG_protect_no_key_no_secret);
  561. ADD_TEST(test_MSG_protect_pbmac_no_sender_with_ref);
  562. ADD_TEST(test_MSG_protect_pbmac_no_sender_no_ref);
  563. ADD_TEST(test_MSG_add_extraCerts);
  564. #ifndef OPENSSL_NO_EC
  565. ADD_TEST(test_cmp_build_cert_chain);
  566. ADD_TEST(test_cmp_build_cert_chain_only_root);
  567. ADD_TEST(test_cmp_build_cert_chain_no_root);
  568. ADD_TEST(test_cmp_build_cert_chain_missing_intermediate);
  569. ADD_TEST(test_cmp_build_cert_chain_no_certs);
  570. #endif
  571. ADD_TEST(test_X509_STORE);
  572. ADD_TEST(test_X509_STORE_only_self_issued);
  573. return 1;
  574. }