a_time.c 15 KB

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