a_time.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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. /*-
  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/asn1.h"
  18. #include "crypto/ctype.h"
  19. #include "internal/cryptlib.h"
  20. #include <openssl/asn1t.h>
  21. #include "asn1_local.h"
  22. IMPLEMENT_ASN1_MSTRING(ASN1_TIME, B_ASN1_TIME)
  23. IMPLEMENT_ASN1_FUNCTIONS(ASN1_TIME)
  24. IMPLEMENT_ASN1_DUP_FUNCTION(ASN1_TIME)
  25. static int is_utc(const int year)
  26. {
  27. if (50 <= year && year <= 149)
  28. return 1;
  29. return 0;
  30. }
  31. static int leap_year(const int year)
  32. {
  33. if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
  34. return 1;
  35. return 0;
  36. }
  37. /*
  38. * Compute the day of the week and the day of the year from the year, month
  39. * and day. The day of the year is straightforward, the day of the week uses
  40. * a form of Zeller's congruence. For this months start with March and are
  41. * numbered 4 through 15.
  42. */
  43. static void determine_days(struct tm *tm)
  44. {
  45. static const int ydays[12] = {
  46. 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
  47. };
  48. int y = tm->tm_year + 1900;
  49. int m = tm->tm_mon;
  50. int d = tm->tm_mday;
  51. int c;
  52. tm->tm_yday = ydays[m] + d - 1;
  53. if (m >= 2) {
  54. /* March and onwards can be one day further into the year */
  55. tm->tm_yday += leap_year(y);
  56. m += 2;
  57. } else {
  58. /* Treat January and February as part of the previous year */
  59. m += 14;
  60. y--;
  61. }
  62. c = y / 100;
  63. y %= 100;
  64. /* Zeller's congruence */
  65. tm->tm_wday = (d + (13 * m) / 5 + y + y / 4 + c / 4 + 5 * c + 6) % 7;
  66. }
  67. int ossl_asn1_time_to_tm(struct tm *tm, const ASN1_TIME *d)
  68. {
  69. static const int min[9] = { 0, 0, 1, 1, 0, 0, 0, 0, 0 };
  70. static const int max[9] = { 99, 99, 12, 31, 23, 59, 59, 12, 59 };
  71. static const int mdays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  72. char *a;
  73. int n, i, i2, l, o, min_l, strict = 0, end = 6, btz = 5, md;
  74. struct tm tmp;
  75. #if defined(CHARSET_EBCDIC)
  76. const char upper_z = 0x5A, num_zero = 0x30, period = 0x2E, minus = 0x2D, plus = 0x2B;
  77. #else
  78. const char upper_z = 'Z', num_zero = '0', period = '.', minus = '-', plus = '+';
  79. #endif
  80. /*
  81. * ASN1_STRING_FLAG_X509_TIME is used to enforce RFC 5280
  82. * time string format, in which:
  83. *
  84. * 1. "seconds" is a 'MUST'
  85. * 2. "Zulu" timezone is a 'MUST'
  86. * 3. "+|-" is not allowed to indicate a timezone
  87. */
  88. if (d->type == V_ASN1_UTCTIME) {
  89. min_l = 13;
  90. if (d->flags & ASN1_STRING_FLAG_X509_TIME) {
  91. strict = 1;
  92. }
  93. } else if (d->type == V_ASN1_GENERALIZEDTIME) {
  94. end = 7;
  95. btz = 6;
  96. min_l = 15;
  97. if (d->flags & ASN1_STRING_FLAG_X509_TIME) {
  98. strict = 1;
  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 (!ossl_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 (!ossl_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) && ossl_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 (!ossl_ascii_isdigit(a[o]))
  213. goto err;
  214. n = a[o] - num_zero;
  215. o++;
  216. if (!ossl_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 *ossl_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 (ts->tm_mon > INT_MAX - 1)
  273. goto err;
  274. if (type == V_ASN1_GENERALIZEDTIME) {
  275. if (ts->tm_year > INT_MAX - 1900)
  276. goto err;
  277. tmps->length = BIO_snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ",
  278. ts->tm_year + 1900, ts->tm_mon + 1,
  279. ts->tm_mday, ts->tm_hour, ts->tm_min,
  280. ts->tm_sec);
  281. } else {
  282. tmps->length = BIO_snprintf(p, len, "%02d%02d%02d%02d%02d%02dZ",
  283. ts->tm_year % 100, ts->tm_mon + 1,
  284. ts->tm_mday, ts->tm_hour, ts->tm_min,
  285. ts->tm_sec);
  286. }
  287. #ifdef CHARSET_EBCDIC
  288. ebcdic2ascii(tmps->data, tmps->data, tmps->length);
  289. #endif
  290. return tmps;
  291. err:
  292. if (tmps != s)
  293. ASN1_STRING_free(tmps);
  294. return NULL;
  295. }
  296. ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t)
  297. {
  298. return ASN1_TIME_adj(s, t, 0, 0);
  299. }
  300. ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t,
  301. int offset_day, long offset_sec)
  302. {
  303. struct tm *ts;
  304. struct tm data;
  305. ts = OPENSSL_gmtime(&t, &data);
  306. if (ts == NULL) {
  307. ERR_raise(ERR_LIB_ASN1, ASN1_R_ERROR_GETTING_TIME);
  308. return NULL;
  309. }
  310. if (offset_day || offset_sec) {
  311. if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
  312. return NULL;
  313. }
  314. return ossl_asn1_time_from_tm(s, ts, V_ASN1_UNDEF);
  315. }
  316. int ASN1_TIME_check(const ASN1_TIME *t)
  317. {
  318. if (t->type == V_ASN1_GENERALIZEDTIME)
  319. return ASN1_GENERALIZEDTIME_check(t);
  320. else if (t->type == V_ASN1_UTCTIME)
  321. return ASN1_UTCTIME_check(t);
  322. return 0;
  323. }
  324. /* Convert an ASN1_TIME structure to GeneralizedTime */
  325. ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(const ASN1_TIME *t,
  326. ASN1_GENERALIZEDTIME **out)
  327. {
  328. ASN1_GENERALIZEDTIME *ret = NULL;
  329. struct tm tm;
  330. if (!ASN1_TIME_to_tm(t, &tm))
  331. return NULL;
  332. if (out != NULL)
  333. ret = *out;
  334. ret = ossl_asn1_time_from_tm(ret, &tm, V_ASN1_GENERALIZEDTIME);
  335. if (out != NULL && ret != NULL)
  336. *out = ret;
  337. return ret;
  338. }
  339. int ASN1_TIME_set_string(ASN1_TIME *s, const char *str)
  340. {
  341. /* Try UTC, if that fails, try GENERALIZED */
  342. if (ASN1_UTCTIME_set_string(s, str))
  343. return 1;
  344. return ASN1_GENERALIZEDTIME_set_string(s, str);
  345. }
  346. int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str)
  347. {
  348. ASN1_TIME t;
  349. struct tm tm;
  350. int rv = 0;
  351. t.length = strlen(str);
  352. t.data = (unsigned char *)str;
  353. t.flags = ASN1_STRING_FLAG_X509_TIME;
  354. t.type = V_ASN1_UTCTIME;
  355. if (!ASN1_TIME_check(&t)) {
  356. t.type = V_ASN1_GENERALIZEDTIME;
  357. if (!ASN1_TIME_check(&t))
  358. goto out;
  359. }
  360. /*
  361. * Per RFC 5280 (section 4.1.2.5.), the valid input time
  362. * strings should be encoded with the following rules:
  363. *
  364. * 1. UTC: YYMMDDHHMMSSZ, if YY < 50 (20YY) --> UTC: YYMMDDHHMMSSZ
  365. * 2. UTC: YYMMDDHHMMSSZ, if YY >= 50 (19YY) --> UTC: YYMMDDHHMMSSZ
  366. * 3. G'd: YYYYMMDDHHMMSSZ, if YYYY >= 2050 --> G'd: YYYYMMDDHHMMSSZ
  367. * 4. G'd: YYYYMMDDHHMMSSZ, if YYYY < 2050 --> UTC: YYMMDDHHMMSSZ
  368. *
  369. * Only strings of the 4th rule should be reformatted, but since a
  370. * UTC can only present [1950, 2050), so if the given time string
  371. * is less than 1950 (e.g. 19230419000000Z), we do nothing...
  372. */
  373. if (s != NULL && t.type == V_ASN1_GENERALIZEDTIME) {
  374. if (!ossl_asn1_time_to_tm(&tm, &t))
  375. goto out;
  376. if (is_utc(tm.tm_year)) {
  377. t.length -= 2;
  378. /*
  379. * it's OK to let original t.data go since that's assigned
  380. * to a piece of memory allocated outside of this function.
  381. * new t.data would be freed after ASN1_STRING_copy is done.
  382. */
  383. t.data = OPENSSL_zalloc(t.length + 1);
  384. if (t.data == NULL)
  385. goto out;
  386. memcpy(t.data, str + 2, t.length);
  387. t.type = V_ASN1_UTCTIME;
  388. }
  389. }
  390. if (s == NULL || ASN1_STRING_copy((ASN1_STRING *)s, (ASN1_STRING *)&t))
  391. rv = 1;
  392. if (t.data != (unsigned char *)str)
  393. OPENSSL_free(t.data);
  394. out:
  395. return rv;
  396. }
  397. int ASN1_TIME_to_tm(const ASN1_TIME *s, struct tm *tm)
  398. {
  399. if (s == NULL) {
  400. time_t now_t;
  401. time(&now_t);
  402. memset(tm, 0, sizeof(*tm));
  403. if (OPENSSL_gmtime(&now_t, tm) != NULL)
  404. return 1;
  405. return 0;
  406. }
  407. return ossl_asn1_time_to_tm(tm, s);
  408. }
  409. int ASN1_TIME_diff(int *pday, int *psec,
  410. const ASN1_TIME *from, const ASN1_TIME *to)
  411. {
  412. struct tm tm_from, tm_to;
  413. if (!ASN1_TIME_to_tm(from, &tm_from))
  414. return 0;
  415. if (!ASN1_TIME_to_tm(to, &tm_to))
  416. return 0;
  417. return OPENSSL_gmtime_diff(pday, psec, &tm_from, &tm_to);
  418. }
  419. static const char _asn1_mon[12][4] = {
  420. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  421. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  422. };
  423. /* prints the time with the default date format (RFC 822) */
  424. int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
  425. {
  426. return ASN1_TIME_print_ex(bp, tm, ASN1_DTFLGS_RFC822);
  427. }
  428. /* returns 1 on success, 0 on BIO write error or parse failure */
  429. int ASN1_TIME_print_ex(BIO *bp, const ASN1_TIME *tm, unsigned long flags)
  430. {
  431. return ossl_asn1_time_print_ex(bp, tm, flags) > 0;
  432. }
  433. /* prints the time with the date format of ISO 8601 */
  434. /* returns 0 on BIO write error, else -1 in case of parse failure, else 1 */
  435. int ossl_asn1_time_print_ex(BIO *bp, const ASN1_TIME *tm, unsigned long flags)
  436. {
  437. char *v;
  438. int gmt = 0, l;
  439. struct tm stm;
  440. const char upper_z = 0x5A, period = 0x2E;
  441. /* ossl_asn1_time_to_tm will check the time type */
  442. if (!ossl_asn1_time_to_tm(&stm, tm))
  443. return BIO_write(bp, "Bad time value", 14) ? -1 : 0;
  444. l = tm->length;
  445. v = (char *)tm->data;
  446. if (v[l - 1] == upper_z)
  447. gmt = 1;
  448. if (tm->type == V_ASN1_GENERALIZEDTIME) {
  449. char *f = NULL;
  450. int f_len = 0;
  451. /*
  452. * Try to parse fractional seconds. '14' is the place of
  453. * 'fraction point' in a GeneralizedTime string.
  454. */
  455. if (tm->length > 15 && v[14] == period) {
  456. f = &v[14];
  457. f_len = 1;
  458. while (14 + f_len < l && ossl_ascii_isdigit(f[f_len]))
  459. ++f_len;
  460. }
  461. if ((flags & ASN1_DTFLGS_TYPE_MASK) == ASN1_DTFLGS_ISO8601) {
  462. return BIO_printf(bp, "%4d-%02d-%02d %02d:%02d:%02d%.*s%s",
  463. stm.tm_year + 1900, stm.tm_mon + 1,
  464. stm.tm_mday, stm.tm_hour,
  465. stm.tm_min, stm.tm_sec, f_len, f,
  466. (gmt ? "Z" : "")) > 0;
  467. }
  468. else {
  469. return BIO_printf(bp, "%s %2d %02d:%02d:%02d%.*s %d%s",
  470. _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour,
  471. stm.tm_min, stm.tm_sec, f_len, f, stm.tm_year + 1900,
  472. (gmt ? " GMT" : "")) > 0;
  473. }
  474. } else {
  475. if ((flags & ASN1_DTFLGS_TYPE_MASK) == ASN1_DTFLGS_ISO8601) {
  476. return BIO_printf(bp, "%4d-%02d-%02d %02d:%02d:%02d%s",
  477. stm.tm_year + 1900, stm.tm_mon + 1,
  478. stm.tm_mday, stm.tm_hour,
  479. stm.tm_min, stm.tm_sec,
  480. (gmt ? "Z" : "")) > 0;
  481. }
  482. else {
  483. return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d%s",
  484. _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour,
  485. stm.tm_min, stm.tm_sec, stm.tm_year + 1900,
  486. (gmt ? " GMT" : "")) > 0;
  487. }
  488. }
  489. }
  490. int ASN1_TIME_cmp_time_t(const ASN1_TIME *s, time_t t)
  491. {
  492. struct tm stm, ttm;
  493. int day, sec;
  494. if (!ASN1_TIME_to_tm(s, &stm))
  495. return -2;
  496. if (!OPENSSL_gmtime(&t, &ttm))
  497. return -2;
  498. if (!OPENSSL_gmtime_diff(&day, &sec, &ttm, &stm))
  499. return -2;
  500. if (day > 0 || sec > 0)
  501. return 1;
  502. if (day < 0 || sec < 0)
  503. return -1;
  504. return 0;
  505. }
  506. int ASN1_TIME_normalize(ASN1_TIME *t)
  507. {
  508. struct tm tm;
  509. if (t == NULL || !ASN1_TIME_to_tm(t, &tm))
  510. return 0;
  511. return ossl_asn1_time_from_tm(t, &tm, V_ASN1_UNDEF) != NULL;
  512. }
  513. int ASN1_TIME_compare(const ASN1_TIME *a, const ASN1_TIME *b)
  514. {
  515. int day, sec;
  516. if (!ASN1_TIME_diff(&day, &sec, b, a))
  517. return -2;
  518. if (day > 0 || sec > 0)
  519. return 1;
  520. if (day < 0 || sec < 0)
  521. return -1;
  522. return 0;
  523. }