cmp_hdr_test.c 13 KB

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