cmp_msg_test.c 18 KB

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