cmp_msg_test.c 18 KB

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