asn1_time_test.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 <stdio.h>
  11. #include <string.h>
  12. #include <openssl/asn1.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/objects.h>
  15. #include "testutil.h"
  16. #include "internal/nelem.h"
  17. struct testdata {
  18. char *data; /* TIME string value */
  19. int type; /* GENERALIZED OR UTC */
  20. int expected_type; /* expected type after set/set_string_gmt */
  21. int check_result; /* check result */
  22. time_t t; /* expected time_t*/
  23. int cmp_result; /* compariston to baseline result */
  24. int convert_result; /* convertion result */
  25. };
  26. static struct testdata tbl_testdata_pos[] = {
  27. { "0", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, }, /* Bad time */
  28. { "ABCD", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  29. { "0ABCD", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  30. { "1-700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  31. { "`9700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  32. { "19700101000000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 0, },
  33. { "A00101000000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 0, },
  34. { "A9700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  35. { "1A700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  36. { "19A00101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  37. { "197A0101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  38. { "1970A101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  39. { "19700A01000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  40. { "197001A1000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  41. { "1970010A000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  42. { "19700101A00000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  43. { "197001010A0000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  44. { "1970010100A000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  45. { "19700101000A00Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  46. { "197001010000A0Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  47. { "1970010100000AZ", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  48. { "700101000000X", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 0, },
  49. { "19700101000000X", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 0, 0, 0, 0, },
  50. { "19700101000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 0, -1, 1, }, /* Epoch begins */
  51. { "700101000000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 0, -1, 1, }, /* ditto */
  52. { "20380119031407Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 0x7FFFFFFF, 1, 1, }, /* Max 32bit time_t */
  53. { "380119031407Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 0x7FFFFFFF, 1, 1, },
  54. { "20371231235959Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 2145916799, 1, 1, }, /* Just before 2038 */
  55. { "20371231235959Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 0, 0, 0, 1, }, /* Bad UTC time */
  56. { "371231235959Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 2145916799, 1, 1, },
  57. { "19701006121456Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 24063296, -1, 1, },
  58. { "701006121456Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 24063296, -1, 1, },
  59. { "19991231000000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, }, /* Match baseline */
  60. { "199912310000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, }, /* In various flavors */
  61. { "991231000000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  62. { "9912310000Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  63. { "9912310000+0000", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  64. { "199912310000+0000", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  65. { "9912310000-0000", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  66. { "199912310000-0000", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  67. { "199912310100+0100", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  68. { "199912302300-0100", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  69. { "199912302300-A000", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 0, 946598400, 0, 1, },
  70. { "199912302300-0A00", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 0, 946598400, 0, 1, },
  71. { "9912310100+0100", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  72. { "9912302300-0100", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, 946598400, 0, 1, },
  73. };
  74. /* ASSUMES SIGNED TIME_T */
  75. static struct testdata tbl_testdata_neg[] = {
  76. { "19011213204552Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, INT_MIN, -1, 0, },
  77. { "691006121456Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, -7472704, -1, 1, },
  78. { "19691006121456Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, -7472704, -1, 1, },
  79. };
  80. /* explicit casts to time_t short warnings on systems with 32-bit time_t */
  81. static struct testdata tbl_testdata_pos_64bit[] = {
  82. { "20380119031408Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, (time_t)0x80000000, 1, 1, },
  83. { "20380119031409Z", V_ASN1_GENERALIZEDTIME, V_ASN1_UTCTIME, 1, (time_t)0x80000001, 1, 1, },
  84. { "380119031408Z", V_ASN1_UTCTIME, V_ASN1_UTCTIME, 1, (time_t)0x80000000, 1, 1, },
  85. { "20500101120000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, (time_t)0x967b1ec0, 1, 0, },
  86. };
  87. /* ASSUMES SIGNED TIME_T */
  88. static struct testdata tbl_testdata_neg_64bit[] = {
  89. { "19011213204551Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, (time_t)-2147483649LL, -1, 0, },
  90. { "19000101120000Z", V_ASN1_GENERALIZEDTIME, V_ASN1_GENERALIZEDTIME, 1, (time_t)-2208945600LL, -1, 0, },
  91. };
  92. /* A baseline time to compare to */
  93. static ASN1_TIME gtime = {
  94. 15,
  95. V_ASN1_GENERALIZEDTIME,
  96. (unsigned char*)"19991231000000Z",
  97. 0
  98. };
  99. static time_t gtime_t = 946598400;
  100. static int test_table(struct testdata *tbl, int idx)
  101. {
  102. int error = 0;
  103. ASN1_TIME atime;
  104. ASN1_TIME *ptime;
  105. struct testdata *td = &tbl[idx];
  106. int day, sec;
  107. atime.data = (unsigned char*)td->data;
  108. atime.length = strlen((char*)atime.data);
  109. atime.type = td->type;
  110. atime.flags = 0;
  111. if (!TEST_int_eq(ASN1_TIME_check(&atime), td->check_result)) {
  112. TEST_info("ASN1_TIME_check(%s) unexpected result", atime.data);
  113. error = 1;
  114. }
  115. if (td->check_result == 0)
  116. return 1;
  117. if (!TEST_int_eq(ASN1_TIME_cmp_time_t(&atime, td->t), 0)) {
  118. TEST_info("ASN1_TIME_cmp_time_t(%s vs %ld) compare failed", atime.data, (long)td->t);
  119. error = 1;
  120. }
  121. if (!TEST_true(ASN1_TIME_diff(&day, &sec, &atime, &atime))) {
  122. TEST_info("ASN1_TIME_diff(%s) to self failed", atime.data);
  123. error = 1;
  124. }
  125. if (!TEST_int_eq(day, 0) || !TEST_int_eq(sec, 0)) {
  126. TEST_info("ASN1_TIME_diff(%s) to self not equal", atime.data);
  127. error = 1;
  128. }
  129. if (!TEST_true(ASN1_TIME_diff(&day, &sec, &gtime, &atime))) {
  130. TEST_info("ASN1_TIME_diff(%s) to baseline failed", atime.data);
  131. error = 1;
  132. } else if (!((td->cmp_result == 0 && TEST_true((day == 0 && sec == 0))) ||
  133. (td->cmp_result == -1 && TEST_true((day < 0 || sec < 0))) ||
  134. (td->cmp_result == 1 && TEST_true((day > 0 || sec > 0))))) {
  135. TEST_info("ASN1_TIME_diff(%s) to baseline bad comparison", atime.data);
  136. error = 1;
  137. }
  138. if (!TEST_int_eq(ASN1_TIME_cmp_time_t(&atime, gtime_t), td->cmp_result)) {
  139. TEST_info("ASN1_TIME_cmp_time_t(%s) to baseline bad comparison", atime.data);
  140. error = 1;
  141. }
  142. ptime = ASN1_TIME_set(NULL, td->t);
  143. if (!TEST_ptr(ptime)) {
  144. TEST_info("ASN1_TIME_set(%ld) failed", (long)td->t);
  145. error = 1;
  146. } else {
  147. int local_error = 0;
  148. if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, td->t), 0)) {
  149. TEST_info("ASN1_TIME_set(%ld) compare failed (%s->%s)",
  150. (long)td->t, td->data, ptime->data);
  151. local_error = error = 1;
  152. }
  153. if (!TEST_int_eq(ptime->type, td->expected_type)) {
  154. TEST_info("ASN1_TIME_set(%ld) unexpected type", (long)td->t);
  155. local_error = error = 1;
  156. }
  157. if (local_error)
  158. TEST_info("ASN1_TIME_set() = %*s", ptime->length, ptime->data);
  159. ASN1_TIME_free(ptime);
  160. }
  161. ptime = ASN1_TIME_new();
  162. if (!TEST_ptr(ptime)) {
  163. TEST_info("ASN1_TIME_new() failed");
  164. error = 1;
  165. } else {
  166. int local_error = 0;
  167. if (!TEST_int_eq(ASN1_TIME_set_string(ptime, td->data), td->check_result)) {
  168. TEST_info("ASN1_TIME_set_string_gmt(%s) failed", td->data);
  169. local_error = error = 1;
  170. }
  171. if (!TEST_int_eq(ASN1_TIME_normalize(ptime), td->check_result)) {
  172. TEST_info("ASN1_TIME_normalize(%s) failed", td->data);
  173. local_error = error = 1;
  174. }
  175. if (!TEST_int_eq(ptime->type, td->expected_type)) {
  176. TEST_info("ASN1_TIME_set_string_gmt(%s) unexpected type", td->data);
  177. local_error = error = 1;
  178. }
  179. day = sec = 0;
  180. if (!TEST_true(ASN1_TIME_diff(&day, &sec, ptime, &atime)) || !TEST_int_eq(day, 0) || !TEST_int_eq(sec, 0)) {
  181. TEST_info("ASN1_TIME_diff(day=%d, sec=%d, %s) after ASN1_TIME_set_string_gmt() failed", day, sec, td->data);
  182. local_error = error = 1;
  183. }
  184. if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, gtime_t), td->cmp_result)) {
  185. TEST_info("ASN1_TIME_cmp_time_t(%s) after ASN1_TIME_set_string_gnt() to baseline bad comparison", td->data);
  186. local_error = error = 1;
  187. }
  188. if (local_error)
  189. TEST_info("ASN1_TIME_set_string_gmt() = %*s", ptime->length, ptime->data);
  190. ASN1_TIME_free(ptime);
  191. }
  192. ptime = ASN1_TIME_new();
  193. if (!TEST_ptr(ptime)) {
  194. TEST_info("ASN1_TIME_new() failed");
  195. error = 1;
  196. } else {
  197. int local_error = 0;
  198. if (!TEST_int_eq(ASN1_TIME_set_string(ptime, td->data), td->check_result)) {
  199. TEST_info("ASN1_TIME_set_string(%s) failed", td->data);
  200. local_error = error = 1;
  201. }
  202. day = sec = 0;
  203. if (!TEST_true(ASN1_TIME_diff(&day, &sec, ptime, &atime)) || !TEST_int_eq(day, 0) || !TEST_int_eq(sec, 0)) {
  204. TEST_info("ASN1_TIME_diff(day=%d, sec=%d, %s) after ASN1_TIME_set_string() failed", day, sec, td->data);
  205. local_error = error = 1;
  206. }
  207. if (!TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, gtime_t), td->cmp_result)) {
  208. TEST_info("ASN1_TIME_cmp_time_t(%s) after ASN1_TIME_set_string() to baseline bad comparison", td->data);
  209. local_error = error = 1;
  210. }
  211. if (local_error)
  212. TEST_info("ASN1_TIME_set_string() = %*s", ptime->length, ptime->data);
  213. ASN1_TIME_free(ptime);
  214. }
  215. if (td->type == V_ASN1_UTCTIME) {
  216. ptime = ASN1_TIME_to_generalizedtime(&atime, NULL);
  217. if (td->convert_result == 1 && !TEST_ptr(ptime)) {
  218. TEST_info("ASN1_TIME_to_generalizedtime(%s) failed", atime.data);
  219. error = 1;
  220. } else if (td->convert_result == 0 && !TEST_ptr_null(ptime)) {
  221. TEST_info("ASN1_TIME_to_generalizedtime(%s) should have failed", atime.data);
  222. error = 1;
  223. }
  224. if (ptime != NULL && !TEST_int_eq(ASN1_TIME_cmp_time_t(ptime, td->t), 0)) {
  225. TEST_info("ASN1_TIME_to_generalizedtime(%s->%s) bad result", atime.data, ptime->data);
  226. error = 1;
  227. }
  228. ASN1_TIME_free(ptime);
  229. }
  230. /* else cannot simply convert GENERALIZEDTIME to UTCTIME */
  231. if (error)
  232. TEST_error("atime=%s", atime.data);
  233. return !error;
  234. }
  235. static int test_table_pos(int idx)
  236. {
  237. return test_table(tbl_testdata_pos, idx);
  238. }
  239. static int test_table_neg(int idx)
  240. {
  241. return test_table(tbl_testdata_neg, idx);
  242. }
  243. static int test_table_pos_64bit(int idx)
  244. {
  245. return test_table(tbl_testdata_pos_64bit, idx);
  246. }
  247. static int test_table_neg_64bit(int idx)
  248. {
  249. return test_table(tbl_testdata_neg_64bit, idx);
  250. }
  251. struct compare_testdata {
  252. ASN1_TIME t1;
  253. ASN1_TIME t2;
  254. int result;
  255. };
  256. static unsigned char TODAY_GEN_STR[] = "20170825000000Z";
  257. static unsigned char TOMORROW_GEN_STR[] = "20170826000000Z";
  258. static unsigned char TODAY_UTC_STR[] = "170825000000Z";
  259. static unsigned char TOMORROW_UTC_STR[] = "170826000000Z";
  260. #define TODAY_GEN { sizeof(TODAY_GEN_STR)-1, V_ASN1_GENERALIZEDTIME, TODAY_GEN_STR, 0 }
  261. #define TOMORROW_GEN { sizeof(TOMORROW_GEN_STR)-1, V_ASN1_GENERALIZEDTIME, TOMORROW_GEN_STR, 0 }
  262. #define TODAY_UTC { sizeof(TODAY_UTC_STR)-1, V_ASN1_UTCTIME, TODAY_UTC_STR, 0 }
  263. #define TOMORROW_UTC { sizeof(TOMORROW_UTC_STR)-1, V_ASN1_UTCTIME, TOMORROW_UTC_STR, 0 }
  264. static struct compare_testdata tbl_compare_testdata[] = {
  265. { TODAY_GEN, TODAY_GEN, 0 },
  266. { TODAY_GEN, TODAY_UTC, 0 },
  267. { TODAY_GEN, TOMORROW_GEN, -1 },
  268. { TODAY_GEN, TOMORROW_UTC, -1 },
  269. { TODAY_UTC, TODAY_GEN, 0 },
  270. { TODAY_UTC, TODAY_UTC, 0 },
  271. { TODAY_UTC, TOMORROW_GEN, -1 },
  272. { TODAY_UTC, TOMORROW_UTC, -1 },
  273. { TOMORROW_GEN, TODAY_GEN, 1 },
  274. { TOMORROW_GEN, TODAY_UTC, 1 },
  275. { TOMORROW_GEN, TOMORROW_GEN, 0 },
  276. { TOMORROW_GEN, TOMORROW_UTC, 0 },
  277. { TOMORROW_UTC, TODAY_GEN, 1 },
  278. { TOMORROW_UTC, TODAY_UTC, 1 },
  279. { TOMORROW_UTC, TOMORROW_GEN, 0 },
  280. { TOMORROW_UTC, TOMORROW_UTC, 0 }
  281. };
  282. static int test_table_compare(int idx)
  283. {
  284. struct compare_testdata *td = &tbl_compare_testdata[idx];
  285. return TEST_int_eq(ASN1_TIME_compare(&td->t1, &td->t2), td->result);
  286. }
  287. int setup_tests(void)
  288. {
  289. /*
  290. * On platforms where |time_t| is an unsigned integer, t will be a
  291. * positive number.
  292. *
  293. * We check if we're on a platform with a signed |time_t| with '!(t > 0)'
  294. * because some compilers are picky if you do 't < 0', or even 't <= 0'
  295. * if |t| is unsigned.
  296. */
  297. time_t t = -1;
  298. /*
  299. * On some platforms, |time_t| is signed, but a negative value is an
  300. * error, and using it with gmtime() or localtime() generates a NULL.
  301. * If that is the case, we can't perform tests on negative values.
  302. */
  303. struct tm *ptm = localtime(&t);
  304. ADD_ALL_TESTS(test_table_pos, OSSL_NELEM(tbl_testdata_pos));
  305. if (!(t > 0) && ptm != NULL) {
  306. TEST_info("Adding negative-sign time_t tests");
  307. ADD_ALL_TESTS(test_table_neg, OSSL_NELEM(tbl_testdata_neg));
  308. }
  309. if (sizeof(time_t) > sizeof(uint32_t)) {
  310. TEST_info("Adding 64-bit time_t tests");
  311. ADD_ALL_TESTS(test_table_pos_64bit, OSSL_NELEM(tbl_testdata_pos_64bit));
  312. #ifndef __hpux
  313. if (!(t > 0) && ptm != NULL) {
  314. TEST_info("Adding negative-sign 64-bit time_t tests");
  315. ADD_ALL_TESTS(test_table_neg_64bit, OSSL_NELEM(tbl_testdata_neg_64bit));
  316. }
  317. #endif
  318. }
  319. ADD_ALL_TESTS(test_table_compare, OSSL_NELEM(tbl_compare_testdata));
  320. return 1;
  321. }