cmp_hdr_test.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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, tmptm;
  135. time_t hdrtime, before, after, now;
  136. now = time(NULL);
  137. /*
  138. * Trial and error reveals that passing the return value from gmtime
  139. * directly to mktime in a mingw 32 bit build gives unexpected results. To
  140. * work around this we take a copy of the return value first.
  141. */
  142. tmptm = *gmtime(&now);
  143. before = mktime(&tmptm);
  144. if (!TEST_true(ossl_cmp_hdr_update_messageTime(fixture->hdr)))
  145. return 0;
  146. if (!TEST_true(ASN1_TIME_to_tm(fixture->hdr->messageTime, &hdrtm)))
  147. return 0;
  148. hdrtime = mktime(&hdrtm);
  149. if (!TEST_time_t_le(before, hdrtime))
  150. return 0;
  151. now = time(NULL);
  152. tmptm = *gmtime(&now);
  153. after = mktime(&tmptm);
  154. return TEST_time_t_le(hdrtime, after);
  155. }
  156. static int test_HDR_update_messageTime(void)
  157. {
  158. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  159. fixture->expected = 1;
  160. EXECUTE_TEST(execute_HDR_update_messageTime_test, tear_down);
  161. return result;
  162. }
  163. static int execute_HDR_set1_senderKID_test(CMP_HDR_TEST_FIXTURE *fixture)
  164. {
  165. ASN1_OCTET_STRING *senderKID = ASN1_OCTET_STRING_new();
  166. int res = 0;
  167. if (!TEST_ptr(senderKID))
  168. return 0;
  169. if (!TEST_int_eq(ASN1_OCTET_STRING_set(senderKID, rand_data,
  170. sizeof(rand_data)), 1))
  171. goto err;
  172. if (!TEST_int_eq(ossl_cmp_hdr_set1_senderKID(fixture->hdr, senderKID), 1))
  173. goto err;
  174. if (!TEST_int_eq(ASN1_OCTET_STRING_cmp(fixture->hdr->senderKID,
  175. senderKID), 0))
  176. goto err;
  177. res = 1;
  178. err:
  179. ASN1_OCTET_STRING_free(senderKID);
  180. return res;
  181. }
  182. static int test_HDR_set1_senderKID(void)
  183. {
  184. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  185. fixture->expected = 1;
  186. EXECUTE_TEST(execute_HDR_set1_senderKID_test, tear_down);
  187. return result;
  188. }
  189. static int execute_HDR_push0_freeText_test(CMP_HDR_TEST_FIXTURE *fixture)
  190. {
  191. ASN1_UTF8STRING *text = ASN1_UTF8STRING_new();
  192. if (!TEST_ptr(text))
  193. return 0;
  194. if (!ASN1_STRING_set(text, "A free text", -1))
  195. goto err;
  196. if (!TEST_int_eq(ossl_cmp_hdr_push0_freeText(fixture->hdr, text), 1))
  197. goto err;
  198. if (!TEST_true(text == sk_ASN1_UTF8STRING_value(fixture->hdr->freeText, 0)))
  199. goto err;
  200. return 1;
  201. err:
  202. ASN1_UTF8STRING_free(text);
  203. return 0;
  204. }
  205. static int test_HDR_push0_freeText(void)
  206. {
  207. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  208. fixture->expected = 1;
  209. EXECUTE_TEST(execute_HDR_push0_freeText_test, tear_down);
  210. return result;
  211. }
  212. static int execute_HDR_push1_freeText_test(CMP_HDR_TEST_FIXTURE *fixture)
  213. {
  214. ASN1_UTF8STRING *text = ASN1_UTF8STRING_new();
  215. ASN1_UTF8STRING *pushed_text;
  216. int res = 0;
  217. if (!TEST_ptr(text))
  218. return 0;
  219. if (!ASN1_STRING_set(text, "A free text", -1))
  220. goto err;
  221. if (!TEST_int_eq(ossl_cmp_hdr_push1_freeText(fixture->hdr, text), 1))
  222. goto err;
  223. pushed_text = sk_ASN1_UTF8STRING_value(fixture->hdr->freeText, 0);
  224. if (!TEST_int_eq(ASN1_STRING_cmp(text, pushed_text), 0))
  225. goto err;
  226. res = 1;
  227. err:
  228. ASN1_UTF8STRING_free(text);
  229. return res;
  230. }
  231. static int test_HDR_push1_freeText(void)
  232. {
  233. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  234. fixture->expected = 1;
  235. EXECUTE_TEST(execute_HDR_push1_freeText_test, tear_down);
  236. return result;
  237. }
  238. static int
  239. execute_HDR_generalInfo_push0_item_test(CMP_HDR_TEST_FIXTURE *fixture)
  240. {
  241. OSSL_CMP_ITAV *itav = OSSL_CMP_ITAV_new();
  242. if (!TEST_ptr(itav))
  243. return 0;
  244. if (!TEST_int_eq(ossl_cmp_hdr_generalInfo_push0_item(fixture->hdr, itav),
  245. 1))
  246. return 0;
  247. if (!TEST_true(itav == sk_OSSL_CMP_ITAV_value(fixture->hdr->generalInfo,
  248. 0)))
  249. return 0;
  250. return 1;
  251. }
  252. static int test_HDR_generalInfo_push0_item(void)
  253. {
  254. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  255. fixture->expected = 1;
  256. EXECUTE_TEST(execute_HDR_generalInfo_push0_item_test, tear_down);
  257. return result;
  258. }
  259. static int
  260. execute_HDR_generalInfo_push1_items_test(CMP_HDR_TEST_FIXTURE *fixture)
  261. {
  262. const char oid[] = "1.2.3.4";
  263. char buf[20];
  264. OSSL_CMP_ITAV *itav, *pushed_itav;
  265. STACK_OF(OSSL_CMP_ITAV) *itavs = NULL, *ginfo;
  266. ASN1_INTEGER *asn1int = ASN1_INTEGER_new();
  267. ASN1_TYPE *val = ASN1_TYPE_new();
  268. ASN1_TYPE *pushed_val;
  269. int res = 0;
  270. if (!TEST_ptr(asn1int))
  271. return 0;
  272. if (!TEST_ptr(val)) {
  273. ASN1_INTEGER_free(asn1int);
  274. return 0;
  275. }
  276. ASN1_INTEGER_set(asn1int, 88);
  277. ASN1_TYPE_set(val, V_ASN1_INTEGER, asn1int);
  278. if (!TEST_ptr(itav = OSSL_CMP_ITAV_create(OBJ_txt2obj(oid, 1), val))) {
  279. ASN1_TYPE_free(val);
  280. return 0;
  281. }
  282. if (!TEST_true(OSSL_CMP_ITAV_push0_stack_item(&itavs, itav))) {
  283. OSSL_CMP_ITAV_free(itav);
  284. return 0;
  285. }
  286. if (!TEST_int_eq(ossl_cmp_hdr_generalInfo_push1_items(fixture->hdr, itavs),
  287. 1))
  288. goto err;
  289. ginfo = fixture->hdr->generalInfo;
  290. pushed_itav = sk_OSSL_CMP_ITAV_value(ginfo, 0);
  291. OBJ_obj2txt(buf, sizeof(buf), OSSL_CMP_ITAV_get0_type(pushed_itav), 0);
  292. if (!TEST_int_eq(memcmp(oid, buf, sizeof(oid)), 0))
  293. goto err;
  294. pushed_val = OSSL_CMP_ITAV_get0_value(sk_OSSL_CMP_ITAV_value(ginfo, 0));
  295. if (!TEST_int_eq(ASN1_TYPE_cmp(itav->infoValue.other, pushed_val), 0))
  296. goto err;
  297. res = 1;
  298. err:
  299. sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
  300. return res;
  301. }
  302. static int test_HDR_generalInfo_push1_items(void)
  303. {
  304. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  305. fixture->expected = 1;
  306. EXECUTE_TEST(execute_HDR_generalInfo_push1_items_test, tear_down);
  307. return result;
  308. }
  309. static int
  310. execute_HDR_set_and_check_implicitConfirm_test(CMP_HDR_TEST_FIXTURE
  311. * fixture)
  312. {
  313. return TEST_false(ossl_cmp_hdr_has_implicitConfirm(fixture->hdr))
  314. && TEST_true(ossl_cmp_hdr_set_implicitConfirm(fixture->hdr))
  315. && TEST_true(ossl_cmp_hdr_has_implicitConfirm(fixture->hdr));
  316. }
  317. static int test_HDR_set_and_check_implicit_confirm(void)
  318. {
  319. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  320. EXECUTE_TEST(execute_HDR_set_and_check_implicitConfirm_test, tear_down);
  321. return result;
  322. }
  323. static int execute_HDR_init_test(CMP_HDR_TEST_FIXTURE *fixture)
  324. {
  325. ASN1_OCTET_STRING *header_nonce, *header_transactionID;
  326. ASN1_OCTET_STRING *ctx_nonce;
  327. if (!TEST_int_eq(fixture->expected,
  328. ossl_cmp_hdr_init(fixture->cmp_ctx, fixture->hdr)))
  329. return 0;
  330. if (fixture->expected == 0)
  331. return 1;
  332. if (!TEST_int_eq(ossl_cmp_hdr_get_pvno(fixture->hdr), OSSL_CMP_PVNO))
  333. return 0;
  334. header_nonce = ossl_cmp_hdr_get0_senderNonce(fixture->hdr);
  335. if (!TEST_int_eq(0, ASN1_OCTET_STRING_cmp(header_nonce,
  336. fixture->cmp_ctx->senderNonce)))
  337. return 0;
  338. header_transactionID = OSSL_CMP_HDR_get0_transactionID(fixture->hdr);
  339. if (!TEST_true(0 == ASN1_OCTET_STRING_cmp(header_transactionID,
  340. fixture->cmp_ctx->transactionID)))
  341. return 0;
  342. header_nonce = OSSL_CMP_HDR_get0_recipNonce(fixture->hdr);
  343. ctx_nonce = fixture->cmp_ctx->recipNonce;
  344. if (ctx_nonce != NULL
  345. && (!TEST_ptr(header_nonce)
  346. || !TEST_int_eq(0, ASN1_OCTET_STRING_cmp(header_nonce,
  347. ctx_nonce))))
  348. return 0;
  349. return 1;
  350. }
  351. static int test_HDR_init_with_ref(void)
  352. {
  353. unsigned char ref[CMP_TEST_REFVALUE_LENGTH];
  354. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  355. fixture->expected = 1;
  356. if (!TEST_int_eq(1, RAND_bytes(ref, sizeof(ref)))
  357. || !TEST_true(OSSL_CMP_CTX_set1_referenceValue(fixture->cmp_ctx,
  358. ref, sizeof(ref)))) {
  359. tear_down(fixture);
  360. fixture = NULL;
  361. }
  362. EXECUTE_TEST(execute_HDR_init_test, tear_down);
  363. return result;
  364. }
  365. static int test_HDR_init_with_subject(void)
  366. {
  367. X509_NAME *subject = NULL;
  368. SETUP_TEST_FIXTURE(CMP_HDR_TEST_FIXTURE, set_up);
  369. fixture->expected = 1;
  370. if (!TEST_ptr(subject = X509_NAME_new())
  371. || !TEST_true(X509_NAME_ADD(subject, "CN", "Common Name"))
  372. || !TEST_true(OSSL_CMP_CTX_set1_subjectName(fixture->cmp_ctx,
  373. subject))) {
  374. tear_down(fixture);
  375. fixture = NULL;
  376. }
  377. X509_NAME_free(subject);
  378. EXECUTE_TEST(execute_HDR_init_test, tear_down);
  379. return result;
  380. }
  381. void cleanup_tests(void)
  382. {
  383. return;
  384. }
  385. int setup_tests(void)
  386. {
  387. RAND_bytes(rand_data, OSSL_CMP_TRANSACTIONID_LENGTH);
  388. /* Message header tests */
  389. ADD_TEST(test_HDR_set_get_pvno);
  390. ADD_TEST(test_HDR_get0_senderNonce);
  391. ADD_TEST(test_HDR_set1_sender);
  392. ADD_TEST(test_HDR_set1_recipient);
  393. ADD_TEST(test_HDR_update_messageTime);
  394. ADD_TEST(test_HDR_set1_senderKID);
  395. ADD_TEST(test_HDR_push0_freeText);
  396. /* indirectly tests ossl_cmp_pkifreetext_push_str(): */
  397. ADD_TEST(test_HDR_push1_freeText);
  398. ADD_TEST(test_HDR_generalInfo_push0_item);
  399. ADD_TEST(test_HDR_generalInfo_push1_items);
  400. ADD_TEST(test_HDR_set_and_check_implicit_confirm);
  401. /* also tests public function OSSL_CMP_HDR_get0_transactionID(): */
  402. /* also tests public function OSSL_CMP_HDR_get0_recipNonce(): */
  403. /* also tests internal function ossl_cmp_hdr_get_pvno(): */
  404. ADD_TEST(test_HDR_init_with_ref);
  405. ADD_TEST(test_HDR_init_with_subject);
  406. return 1;
  407. }