asn1_time_test.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * Copyright 1999-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* Time tests for the asn1 module */
  10. #include <limits.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <crypto/asn1.h>
  14. #include <openssl/asn1.h>
  15. #include <openssl/evp.h>
  16. #include <openssl/objects.h>
  17. #include "testutil.h"
  18. #include "internal/nelem.h"
  19. struct testdata {
  20. char *data; /* TIME string value */
  21. int type; /* GENERALIZED OR UTC */
  22. int expected_type; /* expected type after set/set_string_gmt */
  23. int check_result; /* check result */
  24. time_t t; /* expected time_t*/
  25. int cmp_result; /* comparison to baseline result */
  26. int convert_result; /* conversion result */
  27. };
  28. struct TESTDATA_asn1_to_utc {
  29. char *input;
  30. time_t expected;
  31. };
  32. static const struct TESTDATA_asn1_to_utc asn1_to_utc[] = {
  33. {
  34. /*
  35. * last second of standard time in central Europe in 2021
  36. * specified in GMT
  37. */
  38. "210328005959Z",
  39. 1616893199,
  40. },
  41. {
  42. /*
  43. * first second of daylight saving time in central Europe in 2021
  44. * specified in GMT
  45. */
  46. "210328010000Z",
  47. 1616893200,
  48. },
  49. {
  50. /*
  51. * last second of standard time in central Europe in 2021
  52. * specified in offset to GMT
  53. */
  54. "20210328015959+0100",
  55. 1616893199,
  56. },
  57. {
  58. /*
  59. * first second of daylight saving time in central Europe in 2021
  60. * specified in offset to GMT
  61. */
  62. "20210328030000+0200",
  63. 1616893200,
  64. },
  65. {
  66. /*
  67. * Invalid strings should get -1 as a result
  68. */
  69. "INVALID",
  70. -1,
  71. },
  72. };
  73. static struct testdata tbl_testdata_pos[] = {
  74. { "0", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, /* Bad time */
  75. { "ABCD", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  76. { "0ABCD", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  77. { "1-700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  78. { "`9700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  79. { "19700101000000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 0, },
  80. { "A00101000000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 0, },
  81. { "A9700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  82. { "1A700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  83. { "19A00101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  84. { "197A0101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  85. { "1970A101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  86. { "19700A01000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  87. { "197001A1000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  88. { "1970010A000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  89. { "19700101A00000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  90. { "197001010A0000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  91. { "1970010100A000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  92. { "19700101000A00Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  93. { "197001010000A0Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  94. { "1970010100000AZ", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  95. { "700101000000X", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 0, },
  96. { "19700101000000X", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  97. { "209912312359Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  98. { "199912310000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  99. { "9912312359Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 0, },
  100. { "9912310000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 0, },
  101. { "19700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 0, -1, 1, }, /* Epoch begins */
  102. { "700101000000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 0, -1, 1, }, /* ditto */
  103. { "20380119031407Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 0x7FFFFFFF, 1, 1, }, /* Max 32bit time_t */
  104. { "380119031407Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 0x7FFFFFFF, 1, 1, },
  105. { "20371231235959Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 2145916799, 1, 1, }, /* Just before 2038 */
  106. { "20371231235959Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 1, }, /* Bad UTC time */
  107. { "371231235959Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 2145916799, 1, 1, },
  108. { "19701006121456Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 24063296, -1, 1, },
  109. { "701006121456Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 24063296, -1, 1, },
  110. { "19991231000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, }, /* Match baseline */
  111. { "991231000000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  112. { "9912310000+0000", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  113. { "199912310000+0000", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  114. { "9912310000-0000", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  115. { "199912310000-0000", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  116. { "199912310100+0100", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  117. { "199912302300-0100", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  118. { "199912302300-A000", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 0, 946598400, 0, 1, },
  119. { "199912302300-0A00", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 0, 946598400, 0, 1, },
  120. { "9912310100+0100", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  121. { "9912302300-0100", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  122. };
  123. /* ASSUMES SIGNED TIME_T */
  124. static struct testdata tbl_testdata_neg[] = {
  125. { "19011213204552Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, INT_MIN, -1, 0, },
  126. { "691006121456Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, -7472704, -1, 1, },
  127. { "19691006121456Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, -7472704, -1, 1, },
  128. };
  129. /* explicit casts to time_t short warnings on systems with 32-bit time_t */
  130. static struct testdata tbl_testdata_pos_64bit[] = {
  131. { "20380119031408Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, (time_t)0x80000000, 1, 1, },
  132. { "20380119031409Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, (time_t)0x80000001, 1, 1, },
  133. { "380119031408Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, (time_t)0x80000000, 1, 1, },
  134. { "20500101120000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, (time_t)0x967b1ec0, 1, 0, },
  135. };
  136. /* ASSUMES SIGNED TIME_T */
  137. static struct testdata tbl_testdata_neg_64bit[] = {
  138. { "19011213204551Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, (time_t)-2147483649LL, -1, 0, },
  139. { "19000101120000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, (time_t)-2208945600LL, -1, 0, },
  140. };
  141. /* A baseline time to compare to */
  142. static ASN1_TIME gtime = {
  143. 15,
  144. V_ASN1_GENERALIZEDTIME,
  145. (unsigned char*)"19991231000000Z",
  146. 0
  147. };
  148. static time_t gtime_t = 946598400;
  149. static int test_table(struct testdata *tbl, int idx)
  150. {
  151. int error = 0;
  152. ASN1_TIME atime;
  153. ASN1_TIME *ptime;
  154. struct testdata *td = &tbl[idx];
  155. int day, sec;
  156. atime.data = (unsigned char*)td->data;
  157. atime.length = strlen((char*)atime.data);
  158. atime.type = td->type;
  159. atime.flags = 0;
  160. if (!TEST_int_eq(ASN1_TIME_check(&atime), td->check_result)) {
  161. TEST_info("ASN1_TIME_check(%s) unexpected result", atime.data);
  162. error = 1;
  163. }
  164. if (td->check_result == 0)
  165. return 1;
  166. if (!TEST_int_eq(ASN1_TIME_cmp_time_t(&atime, td->t), 0)) {
  167. TEST_info("ASN1_TIME_cmp_time_t(%s vs %ld) compare failed", atime.data, (long)td->t);
  168. error = 1;
  169. }
  170. if (!TEST_true(ASN1_TIME_diff(&day, &sec, &atime, &atime))) {
  171. TEST_info("ASN1_TIME_diff(%s) to self failed", atime.data);
  172. error = 1;
  173. }
  174. if (!TEST_int_eq(day, 0) || !TEST_int_eq(sec, 0)) {
  175. TEST_info("ASN1_TIME_diff(%s) to self not equal", atime.data);
  176. error = 1;
  177. }
  178. if (!TEST_true(ASN1_TIME_diff(&day, &sec, &gtime, &atime))) {
  179. TEST_info("ASN1_TIME_diff(%s) to baseline failed", atime.data);
  180. error = 1;
  181. } else if (!((td->cmp_result == 0 && TEST_true((day == 0 && sec == 0))) ||
  182. (td->cmp_result == -1 && TEST_true((day < 0 || sec < 0))) ||
  183. (td->cmp_result == 1 && TEST_true((day > 0 || sec > 0))))) {
  184. TEST_info("ASN1_TIME_diff(%s) to baseline bad comparison", atime.data);
  185. error = 1;
  186. }
  187. if (!TEST_int_eq(ASN1_TIME_cmp_time_t(&atime, gtime_t), td->cmp_result)) {
  188. TEST_info("ASN1_TIME_cmp_time_t(%s) to baseline bad comparison", atime.data);
  189. error = 1;
  190. }
  191. ptime = ASN1_TIME_set(NULL, td->t);
  192. if (!TEST_ptr(ptime)) {
  193. TEST_info("ASN1_TIME_set(%ld) failed", (long)td->t);
  194. error = 1;
  195. } else {
  196. int local_error = 0;
  197. if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, td->t), 0)) {
  198. TEST_info("ASN1_TIME_set(%ld) compare failed (%s->%s)",
  199. (long)td->t, td->data, ptime->data);
  200. local_error = error = 1;
  201. }
  202. if (!TEST_int_eq(ptime->type, td->expected_type)) {
  203. TEST_info("ASN1_TIME_set(%ld) unexpected type", (long)td->t);
  204. local_error = error = 1;
  205. }
  206. if (local_error)
  207. TEST_info("ASN1_TIME_set() = %*s", ptime->length, ptime->data);
  208. ASN1_TIME_free(ptime);
  209. }
  210. ptime = ASN1_TIME_new();
  211. if (!TEST_ptr(ptime)) {
  212. TEST_info("ASN1_TIME_new() failed");
  213. error = 1;
  214. } else {
  215. int local_error = 0;
  216. if (!TEST_int_eq(ASN1_TIME_set_string(ptime, td->data), td->check_result)) {
  217. TEST_info("ASN1_TIME_set_string_gmt(%s) failed", td->data);
  218. local_error = error = 1;
  219. }
  220. if (!TEST_int_eq(ASN1_TIME_normalize(ptime), td->check_result)) {
  221. TEST_info("ASN1_TIME_normalize(%s) failed", td->data);
  222. local_error = error = 1;
  223. }
  224. if (!TEST_int_eq(ptime->type, td->expected_type)) {
  225. TEST_info("ASN1_TIME_set_string_gmt(%s) unexpected type", td->data);
  226. local_error = error = 1;
  227. }
  228. day = sec = 0;
  229. if (!TEST_true(ASN1_TIME_diff(&day, &sec, ptime, &atime)) || !TEST_int_eq(day, 0) || !TEST_int_eq(sec, 0)) {
  230. TEST_info("ASN1_TIME_diff(day=%d, sec=%d, %s) after ASN1_TIME_set_string_gmt() failed", day, sec, td->data);
  231. local_error = error = 1;
  232. }
  233. if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, gtime_t), td->cmp_result)) {
  234. TEST_info("ASN1_TIME_cmp_time_t(%s) after ASN1_TIME_set_string_gnt() to baseline bad comparison", td->data);
  235. local_error = error = 1;
  236. }
  237. if (local_error)
  238. TEST_info("ASN1_TIME_set_string_gmt() = %*s", ptime->length, ptime->data);
  239. ASN1_TIME_free(ptime);
  240. }
  241. ptime = ASN1_TIME_new();
  242. if (!TEST_ptr(ptime)) {
  243. TEST_info("ASN1_TIME_new() failed");
  244. error = 1;
  245. } else {
  246. int local_error = 0;
  247. if (!TEST_int_eq(ASN1_TIME_set_string(ptime, td->data), td->check_result)) {
  248. TEST_info("ASN1_TIME_set_string(%s) failed", td->data);
  249. local_error = error = 1;
  250. }
  251. day = sec = 0;
  252. if (!TEST_true(ASN1_TIME_diff(&day, &sec, ptime, &atime)) || !TEST_int_eq(day, 0) || !TEST_int_eq(sec, 0)) {
  253. TEST_info("ASN1_TIME_diff(day=%d, sec=%d, %s) after ASN1_TIME_set_string() failed", day, sec, td->data);
  254. local_error = error = 1;
  255. }
  256. if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, gtime_t), td->cmp_result)) {
  257. TEST_info("ASN1_TIME_cmp_time_t(%s) after ASN1_TIME_set_string() to baseline bad comparison", td->data);
  258. local_error = error = 1;
  259. }
  260. if (local_error)
  261. TEST_info("ASN1_TIME_set_string() = %*s", ptime->length, ptime->data);
  262. ASN1_TIME_free(ptime);
  263. }
  264. if (td->type == V_ASN1_UTCTIME) {
  265. ptime = ASN1_TIME_to_generalizedtime(&atime, NULL);
  266. if (td->convert_result == 1 && !TEST_ptr(ptime)) {
  267. TEST_info("ASN1_TIME_to_generalizedtime(%s) failed", atime.data);
  268. error = 1;
  269. } else if (td->convert_result == 0 && !TEST_ptr_null(ptime)) {
  270. TEST_info("ASN1_TIME_to_generalizedtime(%s) should have failed", atime.data);
  271. error = 1;
  272. }
  273. if (ptime != NULL && !TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, td->t), 0)) {
  274. TEST_info("ASN1_TIME_to_generalizedtime(%s->%s) bad result", atime.data, ptime->data);
  275. error = 1;
  276. }
  277. ASN1_TIME_free(ptime);
  278. }
  279. /* else cannot simply convert GENERALIZEDTIME to UTCTIME */
  280. if (error)
  281. TEST_error("atime=%s", atime.data);
  282. return !error;
  283. }
  284. static int test_table_pos(int idx)
  285. {
  286. return test_table(tbl_testdata_pos, idx);
  287. }
  288. static int test_table_neg(int idx)
  289. {
  290. return test_table(tbl_testdata_neg, idx);
  291. }
  292. static int test_table_pos_64bit(int idx)
  293. {
  294. return test_table(tbl_testdata_pos_64bit, idx);
  295. }
  296. static int test_table_neg_64bit(int idx)
  297. {
  298. return test_table(tbl_testdata_neg_64bit, idx);
  299. }
  300. struct compare_testdata {
  301. ASN1_TIME t1;
  302. ASN1_TIME t2;
  303. int result;
  304. };
  305. static unsigned char TODAY_GEN_STR[] = "20170825000000Z";
  306. static unsigned char TOMORROW_GEN_STR[] = "20170826000000Z";
  307. static unsigned char TODAY_UTC_STR[] = "170825000000Z";
  308. static unsigned char TOMORROW_UTC_STR[] = "170826000000Z";
  309. #define TODAY_GEN { sizeof(TODAY_GEN_STR)-1, V_ASN1_GENERALIZEDTIME, TODAY_GEN_STR, 0 }
  310. #define TOMORROW_GEN { sizeof(TOMORROW_GEN_STR)-1, V_ASN1_GENERALIZEDTIME, TOMORROW_GEN_STR, 0 }
  311. #define TODAY_UTC { sizeof(TODAY_UTC_STR)-1, V_ASN1_UTCTIME, TODAY_UTC_STR, 0 }
  312. #define TOMORROW_UTC { sizeof(TOMORROW_UTC_STR)-1, V_ASN1_UTCTIME, TOMORROW_UTC_STR, 0 }
  313. static struct compare_testdata tbl_compare_testdata[] = {
  314. { TODAY_GEN, TODAY_GEN, 0 },
  315. { TODAY_GEN, TODAY_UTC, 0 },
  316. { TODAY_GEN, TOMORROW_GEN, -1 },
  317. { TODAY_GEN, TOMORROW_UTC, -1 },
  318. { TODAY_UTC, TODAY_GEN, 0 },
  319. { TODAY_UTC, TODAY_UTC, 0 },
  320. { TODAY_UTC, TOMORROW_GEN, -1 },
  321. { TODAY_UTC, TOMORROW_UTC, -1 },
  322. { TOMORROW_GEN, TODAY_GEN, 1 },
  323. { TOMORROW_GEN, TODAY_UTC, 1 },
  324. { TOMORROW_GEN, TOMORROW_GEN, 0 },
  325. { TOMORROW_GEN, TOMORROW_UTC, 0 },
  326. { TOMORROW_UTC, TODAY_GEN, 1 },
  327. { TOMORROW_UTC, TODAY_UTC, 1 },
  328. { TOMORROW_UTC, TOMORROW_GEN, 0 },
  329. { TOMORROW_UTC, TOMORROW_UTC, 0 }
  330. };
  331. static int test_table_compare(int idx)
  332. {
  333. struct compare_testdata *td = &tbl_compare_testdata[idx];
  334. return TEST_int_eq(ASN1_TIME_compare(&td->t1, &td->t2), td->result);
  335. }
  336. static int test_time_dup(void)
  337. {
  338. int ret = 0;
  339. ASN1_TIME *asn1_time = NULL;
  340. ASN1_TIME *asn1_time_dup = NULL;
  341. ASN1_TIME *asn1_gentime = NULL;
  342. asn1_time = ASN1_TIME_adj(NULL, time(NULL), 0, 0);
  343. if (asn1_time == NULL) {
  344. TEST_info("Internal error.");
  345. goto err;
  346. }
  347. asn1_gentime = ASN1_TIME_to_generalizedtime(asn1_time, NULL);
  348. if (asn1_gentime == NULL) {
  349. TEST_info("Internal error.");
  350. goto err;
  351. }
  352. asn1_time_dup = ASN1_TIME_dup(asn1_time);
  353. if (!TEST_ptr_ne(asn1_time_dup, NULL)) {
  354. TEST_info("ASN1_TIME_dup() failed.");
  355. goto err;
  356. }
  357. if (!TEST_int_eq(ASN1_TIME_compare(asn1_time, asn1_time_dup), 0)) {
  358. TEST_info("ASN1_TIME_dup() duplicated non-identical value.");
  359. goto err;
  360. }
  361. ASN1_STRING_free(asn1_time_dup);
  362. asn1_time_dup = ASN1_UTCTIME_dup(asn1_time);
  363. if (!TEST_ptr_ne(asn1_time_dup, NULL)) {
  364. TEST_info("ASN1_UTCTIME_dup() failed.");
  365. goto err;
  366. }
  367. if (!TEST_int_eq(ASN1_TIME_compare(asn1_time, asn1_time_dup), 0)) {
  368. TEST_info("ASN1_UTCTIME_dup() duplicated non-identical UTCTIME value.");
  369. goto err;
  370. }
  371. ASN1_STRING_free(asn1_time_dup);
  372. asn1_time_dup = ASN1_GENERALIZEDTIME_dup(asn1_gentime);
  373. if (!TEST_ptr_ne(asn1_time_dup, NULL)) {
  374. TEST_info("ASN1_GENERALIZEDTIME_dup() failed.");
  375. goto err;
  376. }
  377. if (!TEST_int_eq(ASN1_TIME_compare(asn1_gentime, asn1_time_dup), 0)) {
  378. TEST_info("ASN1_GENERALIZEDTIME_dup() dup'ed non-identical value.");
  379. goto err;
  380. }
  381. ret = 1;
  382. err:
  383. ASN1_STRING_free(asn1_time);
  384. ASN1_STRING_free(asn1_gentime);
  385. ASN1_STRING_free(asn1_time_dup);
  386. return ret;
  387. }
  388. static int convert_asn1_to_time_t(int idx)
  389. {
  390. time_t testdateutc;
  391. testdateutc = ossl_asn1_string_to_time_t(asn1_to_utc[idx].input);
  392. if (!TEST_time_t_eq(testdateutc, asn1_to_utc[idx].expected)) {
  393. TEST_info("ossl_asn1_string_to_time_t (%s) failed: expected %lli, got %lli\n",
  394. asn1_to_utc[idx].input,
  395. (long long int)asn1_to_utc[idx].expected,
  396. (long long int)testdateutc);
  397. return 0;
  398. }
  399. return 1;
  400. }
  401. /*
  402. * this test is here to exercise ossl_asn1_time_from_tm
  403. * with an integer year close to INT_MAX.
  404. */
  405. static int convert_tm_to_asn1_time(void)
  406. {
  407. /* we need 64 bit time_t */
  408. #if ((ULONG_MAX >> 31) >> 31) >= 1
  409. time_t t;
  410. ASN1_TIME *at;
  411. if (sizeof(time_t) * CHAR_BIT >= 64) {
  412. t = 67768011791126057ULL;
  413. at = ASN1_TIME_set(NULL, t);
  414. /*
  415. * If ASN1_TIME_set returns NULL, it means it could not handle the input
  416. * which is fine for this edge case.
  417. */
  418. ASN1_STRING_free(at);
  419. }
  420. #endif
  421. return 1;
  422. }
  423. int setup_tests(void)
  424. {
  425. /*
  426. * On platforms where |time_t| is an unsigned integer, t will be a
  427. * positive number.
  428. *
  429. * We check if we're on a platform with a signed |time_t| with '!(t > 0)'
  430. * because some compilers are picky if you do 't < 0', or even 't <= 0'
  431. * if |t| is unsigned.
  432. */
  433. time_t t = -1;
  434. /*
  435. * On some platforms, |time_t| is signed, but a negative value is an
  436. * error, and using it with gmtime() or localtime() generates a NULL.
  437. * If that is the case, we can't perform tests on negative values.
  438. */
  439. struct tm *ptm = localtime(&t);
  440. ADD_ALL_TESTS(test_table_pos, OSSL_NELEM(tbl_testdata_pos));
  441. if (!(t > 0) && ptm != NULL) {
  442. TEST_info("Adding negative-sign time_t tests");
  443. ADD_ALL_TESTS(test_table_neg, OSSL_NELEM(tbl_testdata_neg));
  444. }
  445. if (sizeof(time_t) > sizeof(uint32_t)) {
  446. TEST_info("Adding 64-bit time_t tests");
  447. ADD_ALL_TESTS(test_table_pos_64bit, OSSL_NELEM(tbl_testdata_pos_64bit));
  448. #ifndef __hpux
  449. if (!(t > 0) && ptm != NULL) {
  450. TEST_info("Adding negative-sign 64-bit time_t tests");
  451. ADD_ALL_TESTS(test_table_neg_64bit, OSSL_NELEM(tbl_testdata_neg_64bit));
  452. }
  453. #endif
  454. }
  455. ADD_ALL_TESTS(test_table_compare, OSSL_NELEM(tbl_compare_testdata));
  456. ADD_TEST(test_time_dup);
  457. ADD_ALL_TESTS(convert_asn1_to_time_t, OSSL_NELEM(asn1_to_utc));
  458. ADD_TEST(convert_tm_to_asn1_time);
  459. return 1;
  460. }