cmp_hdr_test.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 unsigned char rand_data[OSSL_CMP_TRANSACTIONID_LENGTH];
  13. typedef struct test_fixture {
  14. const char *test_case_name;
  15. int expected;
  16. OSSL_CMP_CTX *cmp_ctx;
  17. OSSL_CMP_PKIHEADER *hdr;
  18. } CMP_HDR_TEST_FIXTURE;
  19. static void tear_down(CMP_HDR_TEST_FIXTURE *fixture)
  20. {
  21. OSSL_CMP_PKIHEADER_free(fixture->hdr);
  22. OSSL_CMP_CTX_free(fixture->cmp_ctx);
  23. OPENSSL_free(fixture);
  24. }
  25. static CMP_HDR_TEST_FIXTURE *set_up(const char *const test_case_name)
  26. {
  27. CMP_HDR_TEST_FIXTURE *fixture;
  28. if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
  29. return NULL;
  30. fixture->test_case_name = test_case_name;
  31. if (!TEST_ptr(fixture->cmp_ctx = OSSL_CMP_CTX_new(NULL, NULL)))
  32. goto err;
  33. if (!TEST_ptr(fixture->hdr = OSSL_CMP_PKIHEADER_new()))
  34. goto err;
  35. return fixture;
  36. err:
  37. tear_down(fixture);
  38. return NULL;
  39. }
  40. static int execute_HDR_set_get_pvno_test(CMP_HDR_TEST_FIXTURE *fixture)
  41. {
  42. int pvno = 77;
  43. if (!TEST_int_eq(ossl_cmp_hdr_set_pvno(fixture->hdr, pvno), 1))
  44. return 0;
  45. if (!TEST_int_eq(ossl_cmp_hdr_get_pvno(fixture->hdr), pvno))
  46. return 0;
  47. return 1;
  48. }
  49. static int test_HDR_set_get_pvno(void)
  50. {
  51. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  52. fixture->expected = 1;
  53. EXECUTE_TEST(execute_HDR_set_get_pvno_test, tear_down);
  54. return result;
  55. }
  56. #define X509_NAME_ADD(n, rd, s) \
  57. X509_NAME_add_entry_by_txt((n), (rd), MBSTRING_ASC, (unsigned char *)(s), \
  58. -1, -1, 0)
  59. static int execute_HDR_get0_senderNonce_test(CMP_HDR_TEST_FIXTURE *fixture)
  60. {
  61. X509_NAME *sender = X509_NAME_new();
  62. ASN1_OCTET_STRING *sn;
  63. if (!TEST_ptr(sender))
  64. return 0;
  65. X509_NAME_ADD(sender, "CN", "A common sender name");
  66. if (!TEST_int_eq(OSSL_CMP_CTX_set1_subjectName(fixture->cmp_ctx, sender),
  67. 1))
  68. return 0;
  69. if (!TEST_int_eq(ossl_cmp_hdr_init(fixture->cmp_ctx, fixture->hdr),
  70. 1))
  71. return 0;
  72. sn = ossl_cmp_hdr_get0_senderNonce(fixture->hdr);
  73. if (!TEST_int_eq(ASN1_OCTET_STRING_cmp(fixture->cmp_ctx->senderNonce, sn),
  74. 0))
  75. return 0;
  76. X509_NAME_free(sender);
  77. return 1;
  78. }
  79. static int test_HDR_get0_senderNonce(void)
  80. {
  81. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  82. fixture->expected = 1;
  83. EXECUTE_TEST(execute_HDR_get0_senderNonce_test, tear_down);
  84. return result;
  85. }
  86. static int execute_HDR_set1_sender_test(CMP_HDR_TEST_FIXTURE *fixture)
  87. {
  88. X509_NAME *x509name = X509_NAME_new();
  89. if (!TEST_ptr(x509name))
  90. return 0;
  91. X509_NAME_ADD(x509name, "CN", "A common sender name");
  92. if (!TEST_int_eq(ossl_cmp_hdr_set1_sender(fixture->hdr, x509name), 1))
  93. return 0;
  94. if (!TEST_int_eq(fixture->hdr->sender->type, GEN_DIRNAME))
  95. return 0;
  96. if (!TEST_int_eq(X509_NAME_cmp(fixture->hdr->sender->d.directoryName,
  97. x509name), 0))
  98. return 0;
  99. X509_NAME_free(x509name);
  100. return 1;
  101. }
  102. static int test_HDR_set1_sender(void)
  103. {
  104. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  105. fixture->expected = 1;
  106. EXECUTE_TEST(execute_HDR_set1_sender_test, tear_down);
  107. return result;
  108. }
  109. static int execute_HDR_set1_recipient_test(CMP_HDR_TEST_FIXTURE *fixture)
  110. {
  111. X509_NAME *x509name = X509_NAME_new();
  112. if (!TEST_ptr(x509name))
  113. return 0;
  114. X509_NAME_ADD(x509name, "CN", "A common recipient name");
  115. if (!TEST_int_eq(ossl_cmp_hdr_set1_recipient(fixture->hdr, x509name), 1))
  116. return 0;
  117. if (!TEST_int_eq(fixture->hdr->recipient->type, GEN_DIRNAME))
  118. return 0;
  119. if (!TEST_int_eq(X509_NAME_cmp(fixture->hdr->recipient->d.directoryName,
  120. x509name), 0))
  121. return 0;
  122. X509_NAME_free(x509name);
  123. return 1;
  124. }
  125. static int test_HDR_set1_recipient(void)
  126. {
  127. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  128. fixture->expected = 1;
  129. EXECUTE_TEST(execute_HDR_set1_recipient_test, tear_down);
  130. return result;
  131. }
  132. static int execute_HDR_update_messageTime_test(CMP_HDR_TEST_FIXTURE *fixture)
  133. {
  134. struct tm hdrtm;
  135. time_t hdrtime, before, after, now;
  136. now = time(NULL);
  137. before = mktime(gmtime(&now));
  138. if (!TEST_true(ossl_cmp_hdr_update_messageTime(fixture->hdr)))
  139. return 0;
  140. if (!TEST_true(ASN1_TIME_to_tm(fixture->hdr->messageTime, &hdrtm)))
  141. return 0;
  142. hdrtime = mktime(&hdrtm);
  143. if (!TEST_true(before <= hdrtime))
  144. return 0;
  145. now = time(NULL);
  146. after = mktime(gmtime(&now));
  147. return TEST_true(hdrtime <= after);
  148. }
  149. static int test_HDR_update_messageTime(void)
  150. {
  151. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  152. fixture->expected = 1;
  153. EXECUTE_TEST(execute_HDR_update_messageTime_test, tear_down);
  154. return result;
  155. }
  156. static int execute_HDR_set1_senderKID_test(CMP_HDR_TEST_FIXTURE *fixture)
  157. {
  158. ASN1_OCTET_STRING *senderKID = ASN1_OCTET_STRING_new();
  159. int res = 0;
  160. if (!TEST_ptr(senderKID))
  161. return 0;
  162. if (!TEST_int_eq(ASN1_OCTET_STRING_set(senderKID, rand_data,
  163. sizeof(rand_data)), 1))
  164. goto err;
  165. if (!TEST_int_eq(ossl_cmp_hdr_set1_senderKID(fixture->hdr, senderKID), 1))
  166. goto err;
  167. if (!TEST_int_eq(ASN1_OCTET_STRING_cmp(fixture->hdr->senderKID,
  168. senderKID), 0))
  169. goto err;
  170. res = 1;
  171. err:
  172. ASN1_OCTET_STRING_free(senderKID);
  173. return res;
  174. }
  175. static int test_HDR_set1_senderKID(void)
  176. {
  177. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  178. fixture->expected = 1;
  179. EXECUTE_TEST(execute_HDR_set1_senderKID_test, tear_down);
  180. return result;
  181. }
  182. static int execute_HDR_push0_freeText_test(CMP_HDR_TEST_FIXTURE *fixture)
  183. {
  184. ASN1_UTF8STRING *text = ASN1_UTF8STRING_new();
  185. if (!TEST_ptr(text))
  186. return 0;
  187. if (!ASN1_STRING_set(text, "A free text", -1))
  188. goto err;
  189. if (!TEST_int_eq(ossl_cmp_hdr_push0_freeText(fixture->hdr, text), 1))
  190. goto err;
  191. if (!TEST_true(text == sk_ASN1_UTF8STRING_value(fixture->hdr->freeText, 0)))
  192. goto err;
  193. return 1;
  194. err:
  195. ASN1_UTF8STRING_free(text);
  196. return 0;
  197. }
  198. static int test_HDR_push0_freeText(void)
  199. {
  200. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  201. fixture->expected = 1;
  202. EXECUTE_TEST(execute_HDR_push0_freeText_test, tear_down);
  203. return result;
  204. }
  205. static int execute_HDR_push1_freeText_test(CMP_HDR_TEST_FIXTURE *fixture)
  206. {
  207. ASN1_UTF8STRING *text = ASN1_UTF8STRING_new();
  208. ASN1_UTF8STRING *pushed_text;
  209. int res = 0;
  210. if (!TEST_ptr(text))
  211. return 0;
  212. if (!ASN1_STRING_set(text, "A free text", -1))
  213. goto err;
  214. if (!TEST_int_eq(ossl_cmp_hdr_push1_freeText(fixture->hdr, text), 1))
  215. goto err;
  216. pushed_text = sk_ASN1_UTF8STRING_value(fixture->hdr->freeText, 0);
  217. if (!TEST_int_eq(ASN1_STRING_cmp(text, pushed_text), 0))
  218. goto err;
  219. res = 1;
  220. err:
  221. ASN1_UTF8STRING_free(text);
  222. return res;
  223. }
  224. static int test_HDR_push1_freeText(void)
  225. {
  226. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  227. fixture->expected = 1;
  228. EXECUTE_TEST(execute_HDR_push1_freeText_test, tear_down);
  229. return result;
  230. }
  231. static int
  232. execute_HDR_generalInfo_push0_item_test(CMP_HDR_TEST_FIXTURE *fixture)
  233. {
  234. OSSL_CMP_ITAV *itav = OSSL_CMP_ITAV_new();
  235. if (!TEST_ptr(itav))
  236. return 0;
  237. if (!TEST_int_eq(ossl_cmp_hdr_generalInfo_push0_item(fixture->hdr, itav),
  238. 1))
  239. return 0;
  240. if (!TEST_true(itav == sk_OSSL_CMP_ITAV_value(fixture->hdr->generalInfo,
  241. 0)))
  242. return 0;
  243. return 1;
  244. }
  245. static int test_HDR_generalInfo_push0_item(void)
  246. {
  247. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  248. fixture->expected = 1;
  249. EXECUTE_TEST(execute_HDR_generalInfo_push0_item_test, tear_down);
  250. return result;
  251. }
  252. static int
  253. execute_HDR_generalInfo_push1_items_test(CMP_HDR_TEST_FIXTURE *fixture)
  254. {
  255. const char oid[] = "1.2.3.4";
  256. char buf[20];
  257. OSSL_CMP_ITAV *itav, *pushed_itav;
  258. STACK_OF(OSSL_CMP_ITAV) *itavs = NULL, *ginfo;
  259. ASN1_INTEGER *asn1int = ASN1_INTEGER_new();
  260. ASN1_TYPE *val = ASN1_TYPE_new();
  261. ASN1_TYPE *pushed_val;
  262. int res = 0;
  263. if (!TEST_ptr(asn1int))
  264. return 0;
  265. if (!TEST_ptr(val)) {
  266. ASN1_INTEGER_free(asn1int);
  267. return 0;
  268. }
  269. ASN1_INTEGER_set(asn1int, 88);
  270. ASN1_TYPE_set(val, V_ASN1_INTEGER, asn1int);
  271. if (!TEST_ptr(itav = OSSL_CMP_ITAV_create(OBJ_txt2obj(oid, 1), val))) {
  272. ASN1_TYPE_free(val);
  273. return 0;
  274. }
  275. if (!TEST_true(OSSL_CMP_ITAV_push0_stack_item(&itavs, itav))) {
  276. OSSL_CMP_ITAV_free(itav);
  277. return 0;
  278. }
  279. if (!TEST_int_eq(ossl_cmp_hdr_generalInfo_push1_items(fixture->hdr, itavs),
  280. 1))
  281. goto err;
  282. ginfo = fixture->hdr->generalInfo;
  283. pushed_itav = sk_OSSL_CMP_ITAV_value(ginfo, 0);
  284. OBJ_obj2txt(buf, sizeof(buf), OSSL_CMP_ITAV_get0_type(pushed_itav), 0);
  285. if (!TEST_int_eq(memcmp(oid, buf, sizeof(oid)), 0))
  286. goto err;
  287. pushed_val = OSSL_CMP_ITAV_get0_value(sk_OSSL_CMP_ITAV_value(ginfo, 0));
  288. if (!TEST_int_eq(ASN1_TYPE_cmp(itav->infoValue.other, pushed_val), 0))
  289. goto err;
  290. res = 1;
  291. err:
  292. sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
  293. return res;
  294. }
  295. static int test_HDR_generalInfo_push1_items(void)
  296. {
  297. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  298. fixture->expected = 1;
  299. EXECUTE_TEST(execute_HDR_generalInfo_push1_items_test, tear_down);
  300. return result;
  301. }
  302. static int
  303. execute_HDR_set_and_check_implicitConfirm_test(CMP_HDR_TEST_FIXTURE
  304. * fixture)
  305. {
  306. return TEST_false(ossl_cmp_hdr_has_implicitConfirm(fixture->hdr))
  307. && TEST_true(ossl_cmp_hdr_set_implicitConfirm(fixture->hdr))
  308. && TEST_true(ossl_cmp_hdr_has_implicitConfirm(fixture->hdr));
  309. }
  310. static int test_HDR_set_and_check_implicit_confirm(void)
  311. {
  312. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  313. EXECUTE_TEST(execute_HDR_set_and_check_implicitConfirm_test, tear_down);
  314. return result;
  315. }
  316. static int execute_HDR_init_test(CMP_HDR_TEST_FIXTURE *fixture)
  317. {
  318. ASN1_OCTET_STRING *header_nonce, *header_transactionID;
  319. ASN1_OCTET_STRING *ctx_nonce;
  320. if (!TEST_int_eq(fixture->expected,
  321. ossl_cmp_hdr_init(fixture->cmp_ctx, fixture->hdr)))
  322. return 0;
  323. if (fixture->expected == 0)
  324. return 1;
  325. if (!TEST_int_eq(ossl_cmp_hdr_get_pvno(fixture->hdr), OSSL_CMP_PVNO))
  326. return 0;
  327. header_nonce = ossl_cmp_hdr_get0_senderNonce(fixture->hdr);
  328. if (!TEST_int_eq(0, ASN1_OCTET_STRING_cmp(header_nonce,
  329. fixture->cmp_ctx->senderNonce)))
  330. return 0;
  331. header_transactionID = OSSL_CMP_HDR_get0_transactionID(fixture->hdr);
  332. if (!TEST_true(0 == ASN1_OCTET_STRING_cmp(header_transactionID,
  333. fixture->cmp_ctx->transactionID)))
  334. return 0;
  335. header_nonce = OSSL_CMP_HDR_get0_recipNonce(fixture->hdr);
  336. ctx_nonce = fixture->cmp_ctx->recipNonce;
  337. if (ctx_nonce != NULL
  338. && (!TEST_ptr(header_nonce)
  339. || !TEST_int_eq(0, ASN1_OCTET_STRING_cmp(header_nonce,
  340. ctx_nonce))))
  341. return 0;
  342. return 1;
  343. }
  344. static int test_HDR_init_with_ref(void)
  345. {
  346. unsigned char ref[CMP_TEST_REFVALUE_LENGTH];
  347. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  348. fixture->expected = 1;
  349. if (!TEST_int_eq(1, RAND_bytes(ref, sizeof(ref)))
  350. || !TEST_true(OSSL_CMP_CTX_set1_referenceValue(fixture->cmp_ctx,
  351. ref, sizeof(ref)))) {
  352. tear_down(fixture);
  353. fixture = NULL;
  354. }
  355. EXECUTE_TEST(execute_HDR_init_test, tear_down);
  356. return result;
  357. }
  358. static int test_HDR_init_with_subject(void)
  359. {
  360. X509_NAME *subject = NULL;
  361. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  362. fixture->expected = 1;
  363. if (!TEST_ptr(subject = X509_NAME_new())
  364. || !TEST_true(X509_NAME_ADD(subject, "CN", "Common Name"))
  365. || !TEST_true(OSSL_CMP_CTX_set1_subjectName(fixture->cmp_ctx,
  366. subject))) {
  367. tear_down(fixture);
  368. fixture = NULL;
  369. }
  370. X509_NAME_free(subject);
  371. EXECUTE_TEST(execute_HDR_init_test, tear_down);
  372. return result;
  373. }
  374. void cleanup_tests(void)
  375. {
  376. return;
  377. }
  378. int setup_tests(void)
  379. {
  380. RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH);
  381. /* Message header tests */
  382. ADD_TEST(test_HDR_set_get_pvno);
  383. ADD_TEST(test_HDR_get0_senderNonce);
  384. ADD_TEST(test_HDR_set1_sender);
  385. ADD_TEST(test_HDR_set1_recipient);
  386. ADD_TEST(test_HDR_update_messageTime);
  387. ADD_TEST(test_HDR_set1_senderKID);
  388. ADD_TEST(test_HDR_push0_freeText);
  389. /* indirectly tests ossl_cmp_pkifreetext_push_str(): */
  390. ADD_TEST(test_HDR_push1_freeText);
  391. ADD_TEST(test_HDR_generalInfo_push0_item);
  392. ADD_TEST(test_HDR_generalInfo_push1_items);
  393. ADD_TEST(test_HDR_set_and_check_implicit_confirm);
  394. /* also tests public function OSSL_CMP_HDR_get0_transactionID(): */
  395. /* also tests public function OSSL_CMP_HDR_get0_recipNonce(): */
  396. /* also tests internal function ossl_cmp_hdr_get_pvno(): */
  397. ADD_TEST(test_HDR_init_with_ref);
  398. ADD_TEST(test_HDR_init_with_subject);
  399. return 1;
  400. }