a_time.c 15 KB

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