a_time.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /*
  2. * Copyright 1999-2020 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. /*-
  10. * This is an implementation of the ASN1 Time structure which is:
  11. * Time ::= CHOICE {
  12. * utcTime UTCTime,
  13. * generalTime GeneralizedTime }
  14. */
  15. #include <stdio.h>
  16. #include <time.h>
  17. #include "crypto/ctype.h"
  18. #include "internal/cryptlib.h"
  19. #include <openssl/asn1t.h>
  20. #include "asn1_local.h"
  21. IMPLEMENT_ASN1_MSTRING(ASN1_TIME, B_ASN1_TIME)
  22. IMPLEMENT_ASN1_FUNCTIONS(ASN1_TIME)
  23. IMPLEMENT_ASN1_DUP_FUNCTION(ASN1_TIME)
  24. static int is_utc(const int year)
  25. {
  26. if (50 <= year && year <= 149)
  27. return 1;
  28. return 0;
  29. }
  30. static int leap_year(const int year)
  31. {
  32. if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
  33. return 1;
  34. return 0;
  35. }
  36. /*
  37. * Compute the day of the week and the day of the year from the year, month
  38. * and day. The day of the year is straightforward, the day of the week uses
  39. * a form of Zeller's congruence. For this months start with March and are
  40. * numbered 4 through 15.
  41. */
  42. static void determine_days(struct tm *tm)
  43. {
  44. static const int ydays[12] = {
  45. 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
  46. };
  47. int y = tm->tm_year + 1900;
  48. int m = tm->tm_mon;
  49. int d = tm->tm_mday;
  50. int c;
  51. tm->tm_yday = ydays[m] + d - 1;
  52. if (m >= 2) {
  53. /* March and onwards can be one day further into the year */
  54. tm->tm_yday += leap_year(y);
  55. m += 2;
  56. } else {
  57. /* Treat January and February as part of the previous year */
  58. m += 14;
  59. y--;
  60. }
  61. c = y / 100;
  62. y %= 100;
  63. /* Zeller's congruence */
  64. tm->tm_wday = (d + (13 * m) / 5 + y + y / 4 + c / 4 + 5 * c + 6) % 7;
  65. }
  66. int asn1_time_to_tm(struct tm *tm, const ASN1_TIME *d)
  67. {
  68. static const int min[9] = { 0, 0, 1, 1, 0, 0, 0, 0, 0 };
  69. static const int max[9] = { 99, 99, 12, 31, 23, 59, 59, 12, 59 };
  70. static const int mdays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  71. char *a;
  72. int n, i, i2, l, o, min_l = 11, strict = 0, end = 6, btz = 5, md;
  73. struct tm tmp;
  74. #if defined(CHARSET_EBCDIC)
  75. const char upper_z = 0x5A, num_zero = 0x30, period = 0x2E, minus = 0x2D, plus = 0x2B;
  76. #else
  77. const char upper_z = 'Z', num_zero = '0', period = '.', minus = '-', plus = '+';
  78. #endif
  79. /*
  80. * ASN1_STRING_FLAG_X509_TIME is used to enforce RFC 5280
  81. * time string format, in which:
  82. *
  83. * 1. "seconds" is a 'MUST'
  84. * 2. "Zulu" timezone is a 'MUST'
  85. * 3. "+|-" is not allowed to indicate a time zone
  86. */
  87. if (d->type == V_ASN1_UTCTIME) {
  88. if (d->flags & ASN1_STRING_FLAG_X509_TIME) {
  89. min_l = 13;
  90. strict = 1;
  91. }
  92. } else if (d->type == V_ASN1_GENERALIZEDTIME) {
  93. end = 7;
  94. btz = 6;
  95. if (d->flags & ASN1_STRING_FLAG_X509_TIME) {
  96. min_l = 15;
  97. strict = 1;
  98. } else {
  99. min_l = 13;
  100. }
  101. } else {
  102. return 0;
  103. }
  104. l = d->length;
  105. a = (char *)d->data;
  106. o = 0;
  107. memset(&tmp, 0, sizeof(tmp));
  108. /*
  109. * GENERALIZEDTIME is similar to UTCTIME except the year is represented
  110. * as YYYY. This stuff treats everything as a two digit field so make
  111. * first two fields 00 to 99
  112. */
  113. if (l < min_l)
  114. goto err;
  115. for (i = 0; i < end; i++) {
  116. if (!strict && (i == btz) && ((a[o] == upper_z) || (a[o] == plus) || (a[o] == minus))) {
  117. i++;
  118. break;
  119. }
  120. if (!ascii_isdigit(a[o]))
  121. goto err;
  122. n = a[o] - num_zero;
  123. /* incomplete 2-digital number */
  124. if (++o == l)
  125. goto err;
  126. if (!ascii_isdigit(a[o]))
  127. goto err;
  128. n = (n * 10) + a[o] - num_zero;
  129. /* no more bytes to read, but we haven't seen time-zone yet */
  130. if (++o == l)
  131. goto err;
  132. i2 = (d->type == V_ASN1_UTCTIME) ? i + 1 : i;
  133. if ((n < min[i2]) || (n > max[i2]))
  134. goto err;
  135. switch (i2) {
  136. case 0:
  137. /* UTC will never be here */
  138. tmp.tm_year = n * 100 - 1900;
  139. break;
  140. case 1:
  141. if (d->type == V_ASN1_UTCTIME)
  142. tmp.tm_year = n < 50 ? n + 100 : n;
  143. else
  144. tmp.tm_year += n;
  145. break;
  146. case 2:
  147. tmp.tm_mon = n - 1;
  148. break;
  149. case 3:
  150. /* check if tm_mday is valid in tm_mon */
  151. if (tmp.tm_mon == 1) {
  152. /* it's February */
  153. md = mdays[1] + leap_year(tmp.tm_year + 1900);
  154. } else {
  155. md = mdays[tmp.tm_mon];
  156. }
  157. if (n > md)
  158. goto err;
  159. tmp.tm_mday = n;
  160. determine_days(&tmp);
  161. break;
  162. case 4:
  163. tmp.tm_hour = n;
  164. break;
  165. case 5:
  166. tmp.tm_min = n;
  167. break;
  168. case 6:
  169. tmp.tm_sec = n;
  170. break;
  171. }
  172. }
  173. /*
  174. * Optional fractional seconds: decimal point followed by one or more
  175. * digits.
  176. */
  177. if (d->type == V_ASN1_GENERALIZEDTIME && a[o] == period) {
  178. if (strict)
  179. /* RFC 5280 forbids fractional seconds */
  180. goto err;
  181. if (++o == l)
  182. goto err;
  183. i = o;
  184. while ((o < l) && ascii_isdigit(a[o]))
  185. o++;
  186. /* Must have at least one digit after decimal point */
  187. if (i == o)
  188. goto err;
  189. /* no more bytes to read, but we haven't seen time-zone yet */
  190. if (o == l)
  191. goto err;
  192. }
  193. /*
  194. * 'o' will never point to '\0' at this point, the only chance
  195. * 'o' can point to '\0' is either the subsequent if or the first
  196. * else if is true.
  197. */
  198. if (a[o] == upper_z) {
  199. o++;
  200. } else if (!strict && ((a[o] == plus) || (a[o] == minus))) {
  201. int offsign = a[o] == minus ? 1 : -1;
  202. int offset = 0;
  203. o++;
  204. /*
  205. * if not equal, no need to do subsequent checks
  206. * since the following for-loop will add 'o' by 4
  207. * and the final return statement will check if 'l'
  208. * and 'o' are equal.
  209. */
  210. if (o + 4 != l)
  211. goto err;
  212. for (i = end; i < end + 2; i++) {
  213. if (!ascii_isdigit(a[o]))
  214. goto err;
  215. n = a[o] - num_zero;
  216. o++;
  217. if (!ascii_isdigit(a[o]))
  218. goto err;
  219. n = (n * 10) + a[o] - num_zero;
  220. i2 = (d->type == V_ASN1_UTCTIME) ? i + 1 : i;
  221. if ((n < min[i2]) || (n > max[i2]))
  222. goto err;
  223. /* if tm is NULL, no need to adjust */
  224. if (tm != NULL) {
  225. if (i == end)
  226. offset = n * 3600;
  227. else if (i == end + 1)
  228. offset += n * 60;
  229. }
  230. o++;
  231. }
  232. if (offset && !OPENSSL_gmtime_adj(&tmp, 0, offset * offsign))
  233. goto err;
  234. } else {
  235. /* not Z, or not +/- in non-strict mode */
  236. goto err;
  237. }
  238. if (o == l) {
  239. /* success, check if tm should be filled */
  240. if (tm != NULL)
  241. *tm = tmp;
  242. return 1;
  243. }
  244. err:
  245. return 0;
  246. }
  247. ASN1_TIME *asn1_time_from_tm(ASN1_TIME *s, struct tm *ts, int type)
  248. {
  249. char* p;
  250. ASN1_TIME *tmps = NULL;
  251. const size_t len = 20;
  252. if (type == V_ASN1_UNDEF) {
  253. if (is_utc(ts->tm_year))
  254. type = V_ASN1_UTCTIME;
  255. else
  256. type = V_ASN1_GENERALIZEDTIME;
  257. } else if (type == V_ASN1_UTCTIME) {
  258. if (!is_utc(ts->tm_year))
  259. goto err;
  260. } else if (type != V_ASN1_GENERALIZEDTIME) {
  261. goto err;
  262. }
  263. if (s == NULL)
  264. tmps = ASN1_STRING_new();
  265. else
  266. tmps = s;
  267. if (tmps == NULL)
  268. return NULL;
  269. if (!ASN1_STRING_set(tmps, NULL, len))
  270. goto err;
  271. tmps->type = type;
  272. p = (char*)tmps->data;
  273. if (type == V_ASN1_GENERALIZEDTIME)
  274. tmps->length = BIO_snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ",
  275. ts->tm_year + 1900, ts->tm_mon + 1,
  276. ts->tm_mday, ts->tm_hour, ts->tm_min,
  277. ts->tm_sec);
  278. else
  279. tmps->length = BIO_snprintf(p, len, "%02d%02d%02d%02d%02d%02dZ",
  280. ts->tm_year % 100, ts->tm_mon + 1,
  281. ts->tm_mday, ts->tm_hour, ts->tm_min,
  282. ts->tm_sec);
  283. #ifdef CHARSET_EBCDIC
  284. ebcdic2ascii(tmps->data, tmps->data, tmps->length);
  285. #endif
  286. return tmps;
  287. err:
  288. if (tmps != s)
  289. ASN1_STRING_free(tmps);
  290. return NULL;
  291. }
  292. ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t)
  293. {
  294. return ASN1_TIME_adj(s, t, 0, 0);
  295. }
  296. ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t,
  297. int offset_day, long offset_sec)
  298. {
  299. struct tm *ts;
  300. struct tm data;
  301. ts = OPENSSL_gmtime(&t, &data);
  302. if (ts == NULL) {
  303. ERR_raise(ERR_LIB_ASN1, ASN1_R_ERROR_GETTING_TIME);
  304. return NULL;
  305. }
  306. if (offset_day || offset_sec) {
  307. if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
  308. return NULL;
  309. }
  310. return asn1_time_from_tm(s, ts, V_ASN1_UNDEF);
  311. }
  312. int ASN1_TIME_check(const ASN1_TIME *t)
  313. {
  314. if (t->type == V_ASN1_GENERALIZEDTIME)
  315. return ASN1_GENERALIZEDTIME_check(t);
  316. else if (t->type == V_ASN1_UTCTIME)
  317. return ASN1_UTCTIME_check(t);
  318. return 0;
  319. }
  320. /* Convert an ASN1_TIME structure to GeneralizedTime */
  321. ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(const ASN1_TIME *t,
  322. ASN1_GENERALIZEDTIME **out)
  323. {
  324. ASN1_GENERALIZEDTIME *ret = NULL;
  325. struct tm tm;
  326. if (!ASN1_TIME_to_tm(t, &tm))
  327. return NULL;
  328. if (out != NULL)
  329. ret = *out;
  330. ret = asn1_time_from_tm(ret, &tm, V_ASN1_GENERALIZEDTIME);
  331. if (out != NULL && ret != NULL)
  332. *out = ret;
  333. return ret;
  334. }
  335. int ASN1_TIME_set_string(ASN1_TIME *s, const char *str)
  336. {
  337. /* Try UTC, if that fails, try GENERALIZED */
  338. if (ASN1_UTCTIME_set_string(s, str))
  339. return 1;
  340. return ASN1_GENERALIZEDTIME_set_string(s, str);
  341. }
  342. int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str)
  343. {
  344. ASN1_TIME t;
  345. struct tm tm;
  346. int rv = 0;
  347. t.length = strlen(str);
  348. t.data = (unsigned char *)str;
  349. t.flags = ASN1_STRING_FLAG_X509_TIME;
  350. t.type = V_ASN1_UTCTIME;
  351. if (!ASN1_TIME_check(&t)) {
  352. t.type = V_ASN1_GENERALIZEDTIME;
  353. if (!ASN1_TIME_check(&t))
  354. goto out;
  355. }
  356. /*
  357. * Per RFC 5280 (section 4.1.2.5.), the valid input time
  358. * strings should be encoded with the following rules:
  359. *
  360. * 1. UTC: YYMMDDHHMMSSZ, if YY < 50 (20YY) --> UTC: YYMMDDHHMMSSZ
  361. * 2. UTC: YYMMDDHHMMSSZ, if YY >= 50 (19YY) --> UTC: YYMMDDHHMMSSZ
  362. * 3. G'd: YYYYMMDDHHMMSSZ, if YYYY >= 2050 --> G'd: YYYYMMDDHHMMSSZ
  363. * 4. G'd: YYYYMMDDHHMMSSZ, if YYYY < 2050 --> UTC: YYMMDDHHMMSSZ
  364. *
  365. * Only strings of the 4th rule should be reformatted, but since a
  366. * UTC can only present [1950, 2050), so if the given time string
  367. * is less than 1950 (e.g. 19230419000000Z), we do nothing...
  368. */
  369. if (s != NULL && t.type == V_ASN1_GENERALIZEDTIME) {
  370. if (!asn1_time_to_tm(&tm, &t))
  371. goto out;
  372. if (is_utc(tm.tm_year)) {
  373. t.length -= 2;
  374. /*
  375. * it's OK to let original t.data go since that's assigned
  376. * to a piece of memory allocated outside of this function.
  377. * new t.data would be freed after ASN1_STRING_copy is done.
  378. */
  379. t.data = OPENSSL_zalloc(t.length + 1);
  380. if (t.data == NULL)
  381. goto out;
  382. memcpy(t.data, str + 2, t.length);
  383. t.type = V_ASN1_UTCTIME;
  384. }
  385. }
  386. if (s == NULL || ASN1_STRING_copy((ASN1_STRING *)s, (ASN1_STRING *)&t))
  387. rv = 1;
  388. if (t.data != (unsigned char *)str)
  389. OPENSSL_free(t.data);
  390. out:
  391. return rv;
  392. }
  393. int ASN1_TIME_to_tm(const ASN1_TIME *s, struct tm *tm)
  394. {
  395. if (s == NULL) {
  396. time_t now_t;
  397. time(&now_t);
  398. memset(tm, 0, sizeof(*tm));
  399. if (OPENSSL_gmtime(&now_t, tm) != NULL)
  400. return 1;
  401. return 0;
  402. }
  403. return asn1_time_to_tm(tm, s);
  404. }
  405. int ASN1_TIME_diff(int *pday, int *psec,
  406. const ASN1_TIME *from, const ASN1_TIME *to)
  407. {
  408. struct tm tm_from, tm_to;
  409. if (!ASN1_TIME_to_tm(from, &tm_from))
  410. return 0;
  411. if (!ASN1_TIME_to_tm(to, &tm_to))
  412. return 0;
  413. return OPENSSL_gmtime_diff(pday, psec, &tm_from, &tm_to);
  414. }
  415. static const char _asn1_mon[12][4] = {
  416. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  417. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  418. };
  419. int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
  420. {
  421. char *v;
  422. int gmt = 0, l;
  423. struct tm stm;
  424. const char upper_z = 0x5A, period = 0x2E;
  425. if (!asn1_time_to_tm(&stm, tm)) {
  426. /* asn1_time_to_tm will check the time type */
  427. goto err;
  428. }
  429. l = tm->length;
  430. v = (char *)tm->data;
  431. if (v[l - 1] == upper_z)
  432. gmt = 1;
  433. if (tm->type == V_ASN1_GENERALIZEDTIME) {
  434. char *f = NULL;
  435. int f_len = 0;
  436. /*
  437. * Try to parse fractional seconds. '14' is the place of
  438. * 'fraction point' in a GeneralizedTime string.
  439. */
  440. if (tm->length > 15 && v[14] == period) {
  441. f = &v[14];
  442. f_len = 1;
  443. while (14 + f_len < l && ascii_isdigit(f[f_len]))
  444. ++f_len;
  445. }
  446. return BIO_printf(bp, "%s %2d %02d:%02d:%02d%.*s %d%s",
  447. _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour,
  448. stm.tm_min, stm.tm_sec, f_len, f, stm.tm_year + 1900,
  449. (gmt ? " GMT" : "")) > 0;
  450. } else {
  451. return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d%s",
  452. _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour,
  453. stm.tm_min, stm.tm_sec, stm.tm_year + 1900,
  454. (gmt ? " GMT" : "")) > 0;
  455. }
  456. err:
  457. BIO_write(bp, "Bad time value", 14);
  458. return 0;
  459. }
  460. int ASN1_TIME_cmp_time_t(const ASN1_TIME *s, time_t t)
  461. {
  462. struct tm stm, ttm;
  463. int day, sec;
  464. if (!ASN1_TIME_to_tm(s, &stm))
  465. return -2;
  466. if (!OPENSSL_gmtime(&t, &ttm))
  467. return -2;
  468. if (!OPENSSL_gmtime_diff(&day, &sec, &ttm, &stm))
  469. return -2;
  470. if (day > 0 || sec > 0)
  471. return 1;
  472. if (day < 0 || sec < 0)
  473. return -1;
  474. return 0;
  475. }
  476. int ASN1_TIME_normalize(ASN1_TIME *t)
  477. {
  478. struct tm tm;
  479. if (!ASN1_TIME_to_tm(t, &tm))
  480. return 0;
  481. return asn1_time_from_tm(t, &tm, V_ASN1_UNDEF) != NULL;
  482. }
  483. int ASN1_TIME_compare(const ASN1_TIME *a, const ASN1_TIME *b)
  484. {
  485. int day, sec;
  486. if (!ASN1_TIME_diff(&day, &sec, b, a))
  487. return -2;
  488. if (day > 0 || sec > 0)
  489. return 1;
  490. if (day < 0 || sec < 0)
  491. return -1;
  492. return 0;
  493. }