cmp_hdr_test.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 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. if (!TEST_ptr(senderKID))
  160. return 0;
  161. if (!TEST_int_eq(ASN1_OCTET_STRING_set(senderKID, rand_data,
  162. sizeof(rand_data)), 1))
  163. return 0;
  164. if (!TEST_int_eq(ossl_cmp_hdr_set1_senderKID(fixture->hdr, senderKID), 1))
  165. return 0;
  166. if (!TEST_int_eq(ASN1_OCTET_STRING_cmp(fixture->hdr->senderKID,
  167. senderKID), 0))
  168. return 0;
  169. ASN1_OCTET_STRING_free(senderKID);
  170. return 1;
  171. }
  172. static int test_HDR_set1_senderKID(void)
  173. {
  174. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  175. fixture->expected = 1;
  176. EXECUTE_TEST(execute_HDR_set1_senderKID_test, tear_down);
  177. return result;
  178. }
  179. static int execute_HDR_push0_freeText_test(CMP_HDR_TEST_FIXTURE *fixture)
  180. {
  181. ASN1_UTF8STRING *text = ASN1_UTF8STRING_new();
  182. if (!TEST_ptr(text))
  183. return 0;
  184. if (!ASN1_STRING_set(text, "A free text", -1))
  185. return 0;
  186. if (!TEST_int_eq(ossl_cmp_hdr_push0_freeText(fixture->hdr, text), 1))
  187. return 0;
  188. if (!TEST_true(text == sk_ASN1_UTF8STRING_value(fixture->hdr->freeText, 0)))
  189. return 0;
  190. return 1;
  191. }
  192. static int test_HDR_push0_freeText(void)
  193. {
  194. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  195. fixture->expected = 1;
  196. EXECUTE_TEST(execute_HDR_push0_freeText_test, tear_down);
  197. return result;
  198. }
  199. static int execute_HDR_push1_freeText_test(CMP_HDR_TEST_FIXTURE *fixture)
  200. {
  201. ASN1_UTF8STRING *text = ASN1_UTF8STRING_new();
  202. ASN1_UTF8STRING *pushed_text;
  203. if (!TEST_ptr(text))
  204. return 0;
  205. if (!ASN1_STRING_set(text, "A free text", -1))
  206. return 0;
  207. if (!TEST_int_eq(ossl_cmp_hdr_push1_freeText(fixture->hdr, text), 1))
  208. return 0;
  209. pushed_text = sk_ASN1_UTF8STRING_value(fixture->hdr->freeText, 0);
  210. if (!TEST_int_eq(ASN1_STRING_cmp(text, pushed_text), 0))
  211. return 0;
  212. ASN1_UTF8STRING_free(text);
  213. return 1;
  214. }
  215. static int test_HDR_push1_freeText(void)
  216. {
  217. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  218. fixture->expected = 1;
  219. EXECUTE_TEST(execute_HDR_push1_freeText_test, tear_down);
  220. return result;
  221. }
  222. static int
  223. execute_HDR_generalInfo_push0_item_test(CMP_HDR_TEST_FIXTURE *fixture)
  224. {
  225. OSSL_CMP_ITAV *itav = OSSL_CMP_ITAV_new();
  226. if (!TEST_ptr(itav))
  227. return 0;
  228. if (!TEST_int_eq(ossl_cmp_hdr_generalInfo_push0_item(fixture->hdr, itav),
  229. 1))
  230. return 0;
  231. if (!TEST_true(itav == sk_OSSL_CMP_ITAV_value(fixture->hdr->generalInfo,
  232. 0)))
  233. return 0;
  234. return 1;
  235. }
  236. static int test_HDR_generalInfo_push0_item(void)
  237. {
  238. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  239. fixture->expected = 1;
  240. EXECUTE_TEST(execute_HDR_generalInfo_push0_item_test, tear_down);
  241. return result;
  242. }
  243. static int
  244. execute_HDR_generalInfo_push1_items_test(CMP_HDR_TEST_FIXTURE *fixture)
  245. {
  246. const char oid[] = "1.2.3.4";
  247. char buf[20];
  248. OSSL_CMP_ITAV *itav, *pushed_itav;
  249. STACK_OF(OSSL_CMP_ITAV) *itavs = NULL, *ginfo;
  250. ASN1_INTEGER *asn1int = ASN1_INTEGER_new();
  251. ASN1_TYPE *val = ASN1_TYPE_new();
  252. ASN1_TYPE *pushed_val;
  253. if (!TEST_ptr(asn1int))
  254. return 0;
  255. if (!TEST_ptr(val))
  256. return 0;
  257. ASN1_INTEGER_set(asn1int, 88);
  258. ASN1_TYPE_set(val, V_ASN1_INTEGER, asn1int);
  259. itav = OSSL_CMP_ITAV_create(OBJ_txt2obj(oid, 1), val);
  260. OSSL_CMP_ITAV_push0_stack_item(&itavs, itav);
  261. if (!TEST_int_eq(ossl_cmp_hdr_generalInfo_push1_items(fixture->hdr, itavs),
  262. 1))
  263. return 0;
  264. ginfo = fixture->hdr->generalInfo;
  265. pushed_itav = sk_OSSL_CMP_ITAV_value(ginfo, 0);
  266. OBJ_obj2txt(buf, sizeof(buf), OSSL_CMP_ITAV_get0_type(pushed_itav), 0);
  267. if (!TEST_int_eq(memcmp(oid, buf, sizeof(oid)), 0))
  268. return 0;
  269. pushed_val = OSSL_CMP_ITAV_get0_value(sk_OSSL_CMP_ITAV_value(ginfo, 0));
  270. if (!TEST_int_eq(ASN1_TYPE_cmp(itav->infoValue.other, pushed_val), 0))
  271. return 0;
  272. sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
  273. return 1;
  274. }
  275. static int test_HDR_generalInfo_push1_items(void)
  276. {
  277. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  278. fixture->expected = 1;
  279. EXECUTE_TEST(execute_HDR_generalInfo_push1_items_test, tear_down);
  280. return result;
  281. }
  282. static int
  283. execute_HDR_set_and_check_implicitConfirm_test(CMP_HDR_TEST_FIXTURE
  284. * fixture)
  285. {
  286. return TEST_false(ossl_cmp_hdr_has_implicitConfirm(fixture->hdr))
  287. && TEST_true(ossl_cmp_hdr_set_implicitConfirm(fixture->hdr))
  288. && TEST_true(ossl_cmp_hdr_has_implicitConfirm(fixture->hdr));
  289. }
  290. static int test_HDR_set_and_check_implicit_confirm(void)
  291. {
  292. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  293. EXECUTE_TEST(execute_HDR_set_and_check_implicitConfirm_test, tear_down);
  294. return result;
  295. }
  296. static int execute_HDR_init_test(CMP_HDR_TEST_FIXTURE *fixture)
  297. {
  298. ASN1_OCTET_STRING *header_nonce, *header_transactionID;
  299. ASN1_OCTET_STRING *ctx_nonce;
  300. if (!TEST_int_eq(fixture->expected,
  301. ossl_cmp_hdr_init(fixture->cmp_ctx, fixture->hdr)))
  302. return 0;
  303. if (fixture->expected == 0)
  304. return 1;
  305. if (!TEST_int_eq(ossl_cmp_hdr_get_pvno(fixture->hdr), OSSL_CMP_PVNO))
  306. return 0;
  307. header_nonce = ossl_cmp_hdr_get0_senderNonce(fixture->hdr);
  308. if (!TEST_int_eq(0, ASN1_OCTET_STRING_cmp(header_nonce,
  309. fixture->cmp_ctx->senderNonce)))
  310. return 0;
  311. header_transactionID = OSSL_CMP_HDR_get0_transactionID(fixture->hdr);
  312. if (!TEST_true(0 == ASN1_OCTET_STRING_cmp(header_transactionID,
  313. fixture->cmp_ctx->transactionID)))
  314. return 0;
  315. header_nonce = OSSL_CMP_HDR_get0_recipNonce(fixture->hdr);
  316. ctx_nonce = fixture->cmp_ctx->recipNonce;
  317. if (ctx_nonce != NULL
  318. && (!TEST_ptr(header_nonce)
  319. || !TEST_int_eq(0, ASN1_OCTET_STRING_cmp(header_nonce,
  320. ctx_nonce))))
  321. return 0;
  322. return 1;
  323. }
  324. static int test_HDR_init_with_ref(void)
  325. {
  326. unsigned char ref[CMP_TEST_REFVALUE_LENGTH];
  327. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  328. fixture->expected = 1;
  329. if (!TEST_int_eq(1, RAND_bytes(ref, sizeof(ref)))
  330. || !TEST_true(OSSL_CMP_CTX_set1_referenceValue(fixture->cmp_ctx,
  331. ref, sizeof(ref)))) {
  332. tear_down(fixture);
  333. fixture = NULL;
  334. }
  335. EXECUTE_TEST(execute_HDR_init_test, tear_down);
  336. return result;
  337. }
  338. static int test_HDR_init_with_subject(void)
  339. {
  340. X509_NAME *subject = NULL;
  341. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  342. fixture->expected = 1;
  343. if (!TEST_ptr(subject = X509_NAME_new())
  344. || !TEST_true(X509_NAME_ADD(subject, "CN", "Common Name"))
  345. || !TEST_true(OSSL_CMP_CTX_set1_subjectName(fixture->cmp_ctx,
  346. subject))) {
  347. tear_down(fixture);
  348. fixture = NULL;
  349. }
  350. X509_NAME_free(subject);
  351. EXECUTE_TEST(execute_HDR_init_test, tear_down);
  352. return result;
  353. }
  354. void cleanup_tests(void)
  355. {
  356. return;
  357. }
  358. int setup_tests(void)
  359. {
  360. RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH);
  361. /* Message header tests */
  362. ADD_TEST(test_HDR_set_get_pvno);
  363. ADD_TEST(test_HDR_get0_senderNonce);
  364. ADD_TEST(test_HDR_set1_sender);
  365. ADD_TEST(test_HDR_set1_recipient);
  366. ADD_TEST(test_HDR_update_messageTime);
  367. ADD_TEST(test_HDR_set1_senderKID);
  368. ADD_TEST(test_HDR_push0_freeText);
  369. /* indirectly tests ossl_cmp_pkifreetext_push_str(): */
  370. ADD_TEST(test_HDR_push1_freeText);
  371. ADD_TEST(test_HDR_generalInfo_push0_item);
  372. ADD_TEST(test_HDR_generalInfo_push1_items);
  373. ADD_TEST(test_HDR_set_and_check_implicit_confirm);
  374. /* also tests public function OSSL_CMP_HDR_get0_transactionID(): */
  375. /* also tests public function OSSL_CMP_HDR_get0_recipNonce(): */
  376. /* also tests internal function ossl_cmp_hdr_get_pvno(): */
  377. ADD_TEST(test_HDR_init_with_ref);
  378. ADD_TEST(test_HDR_init_with_subject);
  379. /*
  380. * TODO make sure that total number of tests (here currently 24) is shown,
  381. * also for other cmp_*text.c. Currently the test drivers always show 1.
  382. */
  383. return 1;
  384. }