cmp_msg_test.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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 *newkey_f;
  13. static const char *server_cert_f;
  14. static const char *pkcs10_f;
  15. typedef struct test_fixture {
  16. const char *test_case_name;
  17. OSSL_CMP_CTX *cmp_ctx;
  18. /* for msg create tests */
  19. int bodytype;
  20. int err_code;
  21. /* for certConf */
  22. int fail_info;
  23. /* for protection tests */
  24. OSSL_CMP_MSG *msg;
  25. int expected;
  26. /* for error and response messages */
  27. OSSL_CMP_PKISI *si;
  28. } CMP_MSG_TEST_FIXTURE;
  29. static OSSL_LIB_CTX *libctx = NULL;
  30. static OSSL_PROVIDER *default_null_provider = NULL, *provider = NULL;
  31. static unsigned char ref[CMP_TEST_REFVALUE_LENGTH];
  32. static void tear_down(CMP_MSG_TEST_FIXTURE *fixture)
  33. {
  34. OSSL_CMP_CTX_free(fixture->cmp_ctx);
  35. OSSL_CMP_MSG_free(fixture->msg);
  36. OSSL_CMP_PKISI_free(fixture->si);
  37. OPENSSL_free(fixture);
  38. }
  39. #define SET_OPT_UNPROTECTED_SEND(ctx, val) \
  40. OSSL_CMP_CTX_set_option((ctx), OSSL_CMP_OPT_UNPROTECTED_SEND, (val))
  41. static CMP_MSG_TEST_FIXTURE *set_up(const char *const test_case_name)
  42. {
  43. CMP_MSG_TEST_FIXTURE *fixture;
  44. if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
  45. return NULL;
  46. fixture->test_case_name = test_case_name;
  47. if (!TEST_ptr(fixture->cmp_ctx = OSSL_CMP_CTX_new(libctx, NULL))
  48. || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 1))
  49. || !TEST_true(OSSL_CMP_CTX_set1_referenceValue(fixture->cmp_ctx,
  50. ref, sizeof(ref)))) {
  51. tear_down(fixture);
  52. return NULL;
  53. }
  54. return fixture;
  55. }
  56. static EVP_PKEY *newkey = NULL;
  57. static X509 *cert = NULL;
  58. #define EXECUTE_MSG_CREATION_TEST(expr) \
  59. do { \
  60. OSSL_CMP_MSG *msg = NULL; \
  61. int good = fixture->expected != 0 ? \
  62. TEST_ptr(msg = (expr)) && TEST_true(valid_asn1_encoding(msg)) : \
  63. TEST_ptr_null(msg = (expr)); \
  64. \
  65. OSSL_CMP_MSG_free(msg); \
  66. ERR_print_errors_fp(stderr); \
  67. return good; \
  68. } while (0)
  69. /*-
  70. * The following tests call a cmp message creation function.
  71. * if fixture->expected != 0:
  72. * returns 1 if the message is created and syntactically correct.
  73. * if fixture->expected == 0
  74. * returns 1 if message creation returns NULL
  75. */
  76. static int execute_certreq_create_test(CMP_MSG_TEST_FIXTURE *fixture)
  77. {
  78. EXECUTE_MSG_CREATION_TEST(ossl_cmp_certreq_new(fixture->cmp_ctx,
  79. fixture->bodytype,
  80. NULL));
  81. }
  82. static int execute_errormsg_create_test(CMP_MSG_TEST_FIXTURE *fixture)
  83. {
  84. EXECUTE_MSG_CREATION_TEST(ossl_cmp_error_new(fixture->cmp_ctx, fixture->si,
  85. fixture->err_code,
  86. "details", 0));
  87. }
  88. static int execute_rr_create_test(CMP_MSG_TEST_FIXTURE *fixture)
  89. {
  90. EXECUTE_MSG_CREATION_TEST(ossl_cmp_rr_new(fixture->cmp_ctx));
  91. }
  92. static int execute_certconf_create_test(CMP_MSG_TEST_FIXTURE *fixture)
  93. {
  94. EXECUTE_MSG_CREATION_TEST(ossl_cmp_certConf_new
  95. (fixture->cmp_ctx, OSSL_CMP_CERTREQID,
  96. fixture->fail_info, NULL));
  97. }
  98. static int execute_genm_create_test(CMP_MSG_TEST_FIXTURE *fixture)
  99. {
  100. EXECUTE_MSG_CREATION_TEST(ossl_cmp_genm_new(fixture->cmp_ctx));
  101. }
  102. static int execute_pollreq_create_test(CMP_MSG_TEST_FIXTURE *fixture)
  103. {
  104. EXECUTE_MSG_CREATION_TEST(ossl_cmp_pollReq_new(fixture->cmp_ctx, 4711));
  105. }
  106. static int execute_pkimessage_create_test(CMP_MSG_TEST_FIXTURE *fixture)
  107. {
  108. EXECUTE_MSG_CREATION_TEST(ossl_cmp_msg_create
  109. (fixture->cmp_ctx, fixture->bodytype));
  110. }
  111. static int set1_newPkey(OSSL_CMP_CTX *ctx, EVP_PKEY *pkey)
  112. {
  113. if (!EVP_PKEY_up_ref(pkey))
  114. return 0;
  115. if (!OSSL_CMP_CTX_set0_newPkey(ctx, 1, pkey)) {
  116. EVP_PKEY_free(pkey);
  117. return 0;
  118. }
  119. return 1;
  120. }
  121. static int test_cmp_create_ir_protection_set(void)
  122. {
  123. OSSL_CMP_CTX *ctx;
  124. unsigned char secret[16];
  125. SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
  126. ctx = fixture->cmp_ctx;
  127. fixture->bodytype = OSSL_CMP_PKIBODY_IR;
  128. fixture->err_code = -1;
  129. fixture->expected = 1;
  130. if (!TEST_int_eq(1, RAND_bytes_ex(libctx, secret, sizeof(secret), 0))
  131. || !TEST_true(SET_OPT_UNPROTECTED_SEND(ctx, 0))
  132. || !TEST_true(set1_newPkey(ctx, newkey))
  133. || !TEST_true(OSSL_CMP_CTX_set1_secretValue(ctx, secret,
  134. sizeof(secret)))) {
  135. tear_down(fixture);
  136. fixture = NULL;
  137. }
  138. EXECUTE_TEST(execute_certreq_create_test, tear_down);
  139. return result;
  140. }
  141. static int test_cmp_create_ir_protection_fails(void)
  142. {
  143. SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
  144. fixture->bodytype = OSSL_CMP_PKIBODY_IR;
  145. fixture->err_code = -1;
  146. fixture->expected = 0;
  147. if (!TEST_true(OSSL_CMP_CTX_set1_pkey(fixture->cmp_ctx, newkey))
  148. || !TEST_true(SET_OPT_UNPROTECTED_SEND(fixture->cmp_ctx, 0))
  149. /* newkey used by default for signing does not match cert: */
  150. || !TEST_true(OSSL_CMP_CTX_set1_cert(fixture->cmp_ctx, cert))) {
  151. tear_down(fixture);
  152. fixture = NULL;
  153. }
  154. EXECUTE_TEST(execute_certreq_create_test, tear_down);
  155. return result;
  156. }
  157. static int test_cmp_create_cr_without_key(void)
  158. {
  159. SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
  160. fixture->bodytype = OSSL_CMP_PKIBODY_CR;
  161. fixture->err_code = -1;
  162. fixture->expected = 0;
  163. EXECUTE_TEST(execute_certreq_create_test, tear_down);
  164. return result;
  165. }
  166. static int test_cmp_create_cr(void)
  167. {
  168. SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
  169. fixture->bodytype = OSSL_CMP_PKIBODY_CR;
  170. fixture->err_code = -1;
  171. fixture->expected = 1;
  172. if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
  173. tear_down(fixture);
  174. fixture = NULL;
  175. }
  176. EXECUTE_TEST(execute_certreq_create_test, tear_down);
  177. return result;
  178. }
  179. static int test_cmp_create_certreq_with_invalid_bodytype(void)
  180. {
  181. SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
  182. fixture->bodytype = OSSL_CMP_PKIBODY_RR;
  183. fixture->err_code = -1;
  184. fixture->expected = 0;
  185. if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
  186. tear_down(fixture);
  187. fixture = NULL;
  188. }
  189. EXECUTE_TEST(execute_certreq_create_test, tear_down);
  190. return result;
  191. }
  192. static int test_cmp_create_p10cr(void)
  193. {
  194. OSSL_CMP_CTX *ctx;
  195. X509_REQ *p10cr = NULL;
  196. SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
  197. ctx = fixture->cmp_ctx;
  198. fixture->bodytype = OSSL_CMP_PKIBODY_P10CR;
  199. fixture->err_code = CMP_R_ERROR_CREATING_CERTREQ;
  200. fixture->expected = 1;
  201. if (!TEST_ptr(p10cr = load_csr_der(pkcs10_f, libctx))
  202. || !TEST_true(set1_newPkey(ctx, newkey))
  203. || !TEST_true(OSSL_CMP_CTX_set1_p10CSR(ctx, p10cr))) {
  204. tear_down(fixture);
  205. fixture = NULL;
  206. }
  207. X509_REQ_free(p10cr);
  208. EXECUTE_TEST(execute_certreq_create_test, tear_down);
  209. return result;
  210. }
  211. static int test_cmp_create_p10cr_null(void)
  212. {
  213. SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
  214. fixture->bodytype = OSSL_CMP_PKIBODY_P10CR;
  215. fixture->err_code = CMP_R_ERROR_CREATING_CERTREQ;
  216. fixture->expected = 0;
  217. if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
  218. tear_down(fixture);
  219. fixture = NULL;
  220. }
  221. EXECUTE_TEST(execute_certreq_create_test, tear_down);
  222. return result;
  223. }
  224. static int test_cmp_create_kur(void)
  225. {
  226. SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
  227. fixture->bodytype = OSSL_CMP_PKIBODY_KUR;
  228. fixture->err_code = -1;
  229. fixture->expected = 1;
  230. if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))
  231. || !TEST_true(OSSL_CMP_CTX_set1_oldCert(fixture->cmp_ctx, cert))) {
  232. tear_down(fixture);
  233. fixture = NULL;
  234. }
  235. EXECUTE_TEST(execute_certreq_create_test, tear_down);
  236. return result;
  237. }
  238. static int test_cmp_create_kur_without_oldcert(void)
  239. {
  240. SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
  241. fixture->bodytype = OSSL_CMP_PKIBODY_KUR;
  242. fixture->err_code = -1;
  243. fixture->expected = 0;
  244. if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
  245. tear_down(fixture);
  246. fixture = NULL;
  247. }
  248. EXECUTE_TEST(execute_certreq_create_test, tear_down);
  249. return result;
  250. }
  251. static int test_cmp_create_certconf(void)
  252. {
  253. SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
  254. fixture->fail_info = 0;
  255. fixture->expected = 1;
  256. if (!TEST_true(ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx,
  257. X509_dup(cert)))) {
  258. tear_down(fixture);
  259. fixture = NULL;
  260. }
  261. EXECUTE_TEST(execute_certconf_create_test, tear_down);
  262. return result;
  263. }
  264. static int test_cmp_create_certconf_badAlg(void)
  265. {
  266. SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
  267. fixture->fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_badAlg;
  268. fixture->expected = 1;
  269. if (!TEST_true(ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx,
  270. X509_dup(cert)))) {
  271. tear_down(fixture);
  272. fixture = NULL;
  273. }
  274. EXECUTE_TEST(execute_certconf_create_test, tear_down);
  275. return result;
  276. }
  277. static int test_cmp_create_certconf_fail_info_max(void)
  278. {
  279. SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
  280. fixture->fail_info = 1 << OSSL_CMP_PKIFAILUREINFO_MAX;
  281. fixture->expected = 1;
  282. if (!TEST_true(ossl_cmp_ctx_set0_newCert(fixture->cmp_ctx,
  283. X509_dup(cert)))) {
  284. tear_down(fixture);
  285. fixture = NULL;
  286. }
  287. EXECUTE_TEST(execute_certconf_create_test, tear_down);
  288. return result;
  289. }
  290. static int test_cmp_create_error_msg(void)
  291. {
  292. SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
  293. fixture->si = OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_rejection,
  294. OSSL_CMP_PKIFAILUREINFO_systemFailure,
  295. NULL);
  296. fixture->err_code = -1;
  297. fixture->expected = 1; /* expected: message creation is successful */
  298. if (!TEST_true(set1_newPkey(fixture->cmp_ctx, newkey))) {
  299. tear_down(fixture);
  300. fixture = NULL;
  301. }
  302. EXECUTE_TEST(execute_errormsg_create_test, tear_down);
  303. return result;
  304. }
  305. static int test_cmp_create_pollreq(void)
  306. {
  307. SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
  308. fixture->expected = 1;
  309. EXECUTE_TEST(execute_pollreq_create_test, tear_down);
  310. return result;
  311. }
  312. static int test_cmp_create_rr(void)
  313. {
  314. SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
  315. fixture->expected = 1;
  316. if (!TEST_true(OSSL_CMP_CTX_set1_oldCert(fixture->cmp_ctx, cert))) {
  317. tear_down(fixture);
  318. fixture = NULL;
  319. }
  320. EXECUTE_TEST(execute_rr_create_test, tear_down);
  321. return result;
  322. }
  323. static int test_cmp_create_genm(void)
  324. {
  325. OSSL_CMP_ITAV *iv = NULL;
  326. SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
  327. fixture->expected = 1;
  328. iv = OSSL_CMP_ITAV_create(OBJ_nid2obj(NID_id_it_implicitConfirm), NULL);
  329. if (!TEST_ptr(iv)
  330. || !TEST_true(OSSL_CMP_CTX_push0_genm_ITAV(fixture->cmp_ctx, iv))) {
  331. OSSL_CMP_ITAV_free(iv);
  332. tear_down(fixture);
  333. fixture = NULL;
  334. }
  335. EXECUTE_TEST(execute_genm_create_test, tear_down);
  336. return result;
  337. }
  338. static int execute_certrep_create(CMP_MSG_TEST_FIXTURE *fixture)
  339. {
  340. OSSL_CMP_CTX *ctx = fixture->cmp_ctx;
  341. OSSL_CMP_CERTREPMESSAGE *crepmsg = OSSL_CMP_CERTREPMESSAGE_new();
  342. OSSL_CMP_CERTRESPONSE *read_cresp, *cresp = OSSL_CMP_CERTRESPONSE_new();
  343. X509 *certfromresp = NULL;
  344. int res = 0;
  345. if (crepmsg == NULL || cresp == NULL)
  346. goto err;
  347. if (!ASN1_INTEGER_set(cresp->certReqId, 99))
  348. goto err;
  349. if ((cresp->certifiedKeyPair = OSSL_CMP_CERTIFIEDKEYPAIR_new()) == NULL)
  350. goto err;
  351. cresp->certifiedKeyPair->certOrEncCert->type =
  352. OSSL_CMP_CERTORENCCERT_CERTIFICATE;
  353. if ((cresp->certifiedKeyPair->certOrEncCert->value.certificate =
  354. X509_dup(cert)) == NULL
  355. || !sk_OSSL_CMP_CERTRESPONSE_push(crepmsg->response, cresp))
  356. goto err;
  357. cresp = NULL;
  358. read_cresp = ossl_cmp_certrepmessage_get0_certresponse(crepmsg, 99);
  359. if (!TEST_ptr(read_cresp))
  360. goto err;
  361. if (!TEST_ptr_null(ossl_cmp_certrepmessage_get0_certresponse(crepmsg, 88)))
  362. goto err;
  363. certfromresp = ossl_cmp_certresponse_get1_cert(ctx, read_cresp);
  364. if (certfromresp == NULL || !TEST_int_eq(X509_cmp(cert, certfromresp), 0))
  365. goto err;
  366. res = 1;
  367. err:
  368. X509_free(certfromresp);
  369. OSSL_CMP_CERTRESPONSE_free(cresp);
  370. OSSL_CMP_CERTREPMESSAGE_free(crepmsg);
  371. return res;
  372. }
  373. static int test_cmp_create_certrep(void)
  374. {
  375. SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
  376. EXECUTE_TEST(execute_certrep_create, tear_down);
  377. return result;
  378. }
  379. static int execute_rp_create(CMP_MSG_TEST_FIXTURE *fixture)
  380. {
  381. OSSL_CMP_PKISI *si = OSSL_CMP_STATUSINFO_new(33, 44, "a text");
  382. X509_NAME *issuer = X509_NAME_new();
  383. ASN1_INTEGER *serial = ASN1_INTEGER_new();
  384. OSSL_CRMF_CERTID *cid = NULL;
  385. OSSL_CMP_MSG *rpmsg = NULL;
  386. int res = 0;
  387. if (si == NULL || issuer == NULL || serial == NULL)
  388. goto err;
  389. if (!X509_NAME_add_entry_by_txt(issuer, "CN", MBSTRING_ASC,
  390. (unsigned char *)"The Issuer", -1, -1, 0)
  391. || !ASN1_INTEGER_set(serial, 99)
  392. || (cid = OSSL_CRMF_CERTID_gen(issuer, serial)) == NULL
  393. || (rpmsg = ossl_cmp_rp_new(fixture->cmp_ctx, si, cid, 1)) == NULL)
  394. goto err;
  395. if (!TEST_ptr(ossl_cmp_revrepcontent_get_CertId(rpmsg->body->value.rp, 0)))
  396. goto err;
  397. if (!TEST_ptr(ossl_cmp_revrepcontent_get_pkisi(rpmsg->body->value.rp, 0)))
  398. goto err;
  399. res = 1;
  400. err:
  401. ASN1_INTEGER_free(serial);
  402. X509_NAME_free(issuer);
  403. OSSL_CRMF_CERTID_free(cid);
  404. OSSL_CMP_PKISI_free(si);
  405. OSSL_CMP_MSG_free(rpmsg);
  406. return res;
  407. }
  408. static int test_cmp_create_rp(void)
  409. {
  410. SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
  411. EXECUTE_TEST(execute_rp_create, tear_down);
  412. return result;
  413. }
  414. static int execute_pollrep_create(CMP_MSG_TEST_FIXTURE *fixture)
  415. {
  416. OSSL_CMP_MSG *pollrep;
  417. int res = 0;
  418. pollrep = ossl_cmp_pollRep_new(fixture->cmp_ctx, 77, 2000);
  419. if (!TEST_ptr(pollrep))
  420. return 0;
  421. if (!TEST_ptr(ossl_cmp_pollrepcontent_get0_pollrep(pollrep->body->
  422. value.pollRep, 77)))
  423. goto err;
  424. if (!TEST_ptr_null(ossl_cmp_pollrepcontent_get0_pollrep(pollrep->body->
  425. value.pollRep, 88)))
  426. goto err;
  427. res = 1;
  428. err:
  429. OSSL_CMP_MSG_free(pollrep);
  430. return res;
  431. }
  432. static int test_cmp_create_pollrep(void)
  433. {
  434. SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
  435. EXECUTE_TEST(execute_pollrep_create, tear_down);
  436. return result;
  437. }
  438. static int test_cmp_pkimessage_create(int bodytype)
  439. {
  440. X509_REQ *p10cr = NULL;
  441. SETUP_TEST_FIXTURE(CMP_MSG_TEST_FIXTURE, set_up);
  442. switch (fixture->bodytype = bodytype) {
  443. case OSSL_CMP_PKIBODY_P10CR:
  444. fixture->expected = 1;
  445. p10cr = load_csr_der(pkcs10_f, libctx);
  446. if (!TEST_true(OSSL_CMP_CTX_set1_p10CSR(fixture->cmp_ctx, p10cr))) {
  447. tear_down(fixture);
  448. fixture = NULL;
  449. }
  450. X509_REQ_free(p10cr);
  451. break;
  452. case OSSL_CMP_PKIBODY_IR:
  453. case OSSL_CMP_PKIBODY_IP:
  454. case OSSL_CMP_PKIBODY_CR:
  455. case OSSL_CMP_PKIBODY_CP:
  456. case OSSL_CMP_PKIBODY_KUR:
  457. case OSSL_CMP_PKIBODY_KUP:
  458. case OSSL_CMP_PKIBODY_RR:
  459. case OSSL_CMP_PKIBODY_RP:
  460. case OSSL_CMP_PKIBODY_PKICONF:
  461. case OSSL_CMP_PKIBODY_GENM:
  462. case OSSL_CMP_PKIBODY_GENP:
  463. case OSSL_CMP_PKIBODY_ERROR:
  464. case OSSL_CMP_PKIBODY_CERTCONF:
  465. case OSSL_CMP_PKIBODY_POLLREQ:
  466. case OSSL_CMP_PKIBODY_POLLREP:
  467. fixture->expected = 1;
  468. break;
  469. default:
  470. fixture->expected = 0;
  471. break;
  472. }
  473. EXECUTE_TEST(execute_pkimessage_create_test, tear_down);
  474. return result;
  475. }
  476. void cleanup_tests(void)
  477. {
  478. EVP_PKEY_free(newkey);
  479. X509_free(cert);
  480. OSSL_PROVIDER_unload(default_null_provider);
  481. OSSL_PROVIDER_unload(provider);
  482. OSSL_LIB_CTX_free(libctx);
  483. }
  484. #define USAGE "new.key server.crt pkcs10.der module_name [module_conf_file]\n"
  485. OPT_TEST_DECLARE_USAGE(USAGE)
  486. int setup_tests(void)
  487. {
  488. if (!test_skip_common_options()) {
  489. TEST_error("Error parsing test options\n");
  490. return 0;
  491. }
  492. if (!TEST_ptr(newkey_f = test_get_argument(0))
  493. || !TEST_ptr(server_cert_f = test_get_argument(1))
  494. || !TEST_ptr(pkcs10_f = test_get_argument(2))) {
  495. TEST_error("usage: cmp_msg_test %s", USAGE);
  496. return 0;
  497. }
  498. if (!test_arg_libctx(&libctx, &default_null_provider, &provider, 3, USAGE))
  499. return 0;
  500. if (!TEST_ptr(newkey = load_pkey_pem(newkey_f, libctx))
  501. || !TEST_ptr(cert = load_cert_pem(server_cert_f, libctx))
  502. || !TEST_int_eq(1, RAND_bytes_ex(libctx, ref, sizeof(ref), 0))) {
  503. cleanup_tests();
  504. return 0;
  505. }
  506. /* Message creation tests */
  507. ADD_TEST(test_cmp_create_certreq_with_invalid_bodytype);
  508. ADD_TEST(test_cmp_create_ir_protection_fails);
  509. ADD_TEST(test_cmp_create_ir_protection_set);
  510. ADD_TEST(test_cmp_create_error_msg);
  511. ADD_TEST(test_cmp_create_certconf);
  512. ADD_TEST(test_cmp_create_certconf_badAlg);
  513. ADD_TEST(test_cmp_create_certconf_fail_info_max);
  514. ADD_TEST(test_cmp_create_kur);
  515. ADD_TEST(test_cmp_create_kur_without_oldcert);
  516. ADD_TEST(test_cmp_create_cr);
  517. ADD_TEST(test_cmp_create_cr_without_key);
  518. ADD_TEST(test_cmp_create_p10cr);
  519. ADD_TEST(test_cmp_create_p10cr_null);
  520. ADD_TEST(test_cmp_create_pollreq);
  521. ADD_TEST(test_cmp_create_rr);
  522. ADD_TEST(test_cmp_create_rp);
  523. ADD_TEST(test_cmp_create_genm);
  524. ADD_TEST(test_cmp_create_certrep);
  525. ADD_TEST(test_cmp_create_pollrep);
  526. ADD_ALL_TESTS_NOSUBTEST(test_cmp_pkimessage_create,
  527. OSSL_CMP_PKIBODY_POLLREP + 1);
  528. return 1;
  529. }