cmp_hdr_test.c 13 KB

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