o_time.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /* crypto/o_time.c */
  2. /*
  3. * Written by Richard Levitte (richard@levitte.org) for the OpenSSL project
  4. * 2001.
  5. */
  6. /*
  7. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  8. * 2008.
  9. */
  10. /* ====================================================================
  11. * Copyright (c) 2001-2018 The OpenSSL Project. All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * 2. Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in
  22. * the documentation and/or other materials provided with the
  23. * distribution.
  24. *
  25. * 3. All advertising materials mentioning features or use of this
  26. * software must display the following acknowledgment:
  27. * "This product includes software developed by the OpenSSL Project
  28. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  29. *
  30. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  31. * endorse or promote products derived from this software without
  32. * prior written permission. For written permission, please contact
  33. * licensing@OpenSSL.org.
  34. *
  35. * 5. Products derived from this software may not be called "OpenSSL"
  36. * nor may "OpenSSL" appear in their names without prior written
  37. * permission of the OpenSSL Project.
  38. *
  39. * 6. Redistributions of any form whatsoever must retain the following
  40. * acknowledgment:
  41. * "This product includes software developed by the OpenSSL Project
  42. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  43. *
  44. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  45. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  46. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  47. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  48. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  49. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  50. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  51. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  52. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  53. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  54. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  55. * OF THE POSSIBILITY OF SUCH DAMAGE.
  56. * ====================================================================
  57. *
  58. * This product includes cryptographic software written by Eric Young
  59. * (eay@cryptsoft.com). This product includes software written by Tim
  60. * Hudson (tjh@cryptsoft.com).
  61. *
  62. */
  63. #include <openssl/e_os2.h>
  64. #include <string.h>
  65. #include "o_time.h"
  66. #ifdef OPENSSL_SYS_VMS
  67. # if __CRTL_VER >= 70000000 && \
  68. (defined _POSIX_C_SOURCE || !defined _ANSI_C_SOURCE)
  69. # define VMS_GMTIME_OK
  70. # endif
  71. # ifndef VMS_GMTIME_OK
  72. # include <libdtdef.h>
  73. # include <lib$routines.h>
  74. # include <lnmdef.h>
  75. # include <starlet.h>
  76. # include <descrip.h>
  77. # include <stdlib.h>
  78. # endif /* ndef VMS_GMTIME_OK */
  79. /*
  80. * Needed to pick up the correct definitions and declarations in some of the
  81. * DEC C Header Files (*.H).
  82. */
  83. # define __NEW_STARLET 1
  84. # if (defined(__alpha) || defined(__ia64))
  85. # include <iledef.h>
  86. # else
  87. /* VAX */
  88. typedef struct _ile3 { /* Copied from ILEDEF.H for Alpha */
  89. # pragma __nomember_alignment
  90. unsigned short int ile3$w_length; /* Length of buffer in bytes */
  91. unsigned short int ile3$w_code; /* Item code value */
  92. void *ile3$ps_bufaddr; /* Buffer address */
  93. unsigned short int *ile3$ps_retlen_addr; /* Address of word for returned length */
  94. } ILE3;
  95. # endif /* alpha || ia64 */
  96. #endif /* OPENSSL_SYS_VMS */
  97. struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result)
  98. {
  99. struct tm *ts = NULL;
  100. #if defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_OS2) && (!defined(OPENSSL_SYS_VMS) || defined(gmtime_r)) && !defined(OPENSSL_SYS_SUNOS)
  101. if (gmtime_r(timer, result) == NULL)
  102. return NULL;
  103. ts = result;
  104. #elif defined (OPENSSL_SYS_WINDOWS) && defined(_MSC_VER) && _MSC_VER >= 1400
  105. if (gmtime_s(result, timer))
  106. return NULL;
  107. ts = result;
  108. #elif !defined(OPENSSL_SYS_VMS) || defined(VMS_GMTIME_OK)
  109. ts = gmtime(timer);
  110. if (ts == NULL)
  111. return NULL;
  112. memcpy(result, ts, sizeof(struct tm));
  113. ts = result;
  114. #endif
  115. #if defined( OPENSSL_SYS_VMS) && !defined( VMS_GMTIME_OK)
  116. if (ts == NULL) {
  117. static $DESCRIPTOR(tabnam, "LNM$DCL_LOGICAL");
  118. static $DESCRIPTOR(lognam, "SYS$TIMEZONE_DIFFERENTIAL");
  119. char logvalue[256];
  120. unsigned int reslen = 0;
  121. # if __INITIAL_POINTER_SIZE == 64
  122. ILEB_64 itemlist[2], *pitem;
  123. # else
  124. ILE3 itemlist[2], *pitem;
  125. # endif
  126. int status;
  127. time_t t;
  128. /*
  129. * Setup an itemlist for the call to $TRNLNM - Translate Logical Name.
  130. */
  131. pitem = itemlist;
  132. # if __INITIAL_POINTER_SIZE == 64
  133. pitem->ileb_64$w_mbo = 1;
  134. pitem->ileb_64$w_code = LNM$_STRING;
  135. pitem->ileb_64$l_mbmo = -1;
  136. pitem->ileb_64$q_length = sizeof(logvalue);
  137. pitem->ileb_64$pq_bufaddr = logvalue;
  138. pitem->ileb_64$pq_retlen_addr = (unsigned __int64 *) &reslen;
  139. pitem++;
  140. /* Last item of the item list is null terminated */
  141. pitem->ileb_64$q_length = pitem->ileb_64$w_code = 0;
  142. # else
  143. pitem->ile3$w_length = sizeof(logvalue);
  144. pitem->ile3$w_code = LNM$_STRING;
  145. pitem->ile3$ps_bufaddr = logvalue;
  146. pitem->ile3$ps_retlen_addr = (unsigned short int *) &reslen;
  147. pitem++;
  148. /* Last item of the item list is null terminated */
  149. pitem->ile3$w_length = pitem->ile3$w_code = 0;
  150. # endif
  151. /* Get the value for SYS$TIMEZONE_DIFFERENTIAL */
  152. status = sys$trnlnm(0, &tabnam, &lognam, 0, itemlist);
  153. if (!(status & 1))
  154. return NULL;
  155. logvalue[reslen] = '\0';
  156. t = *timer;
  157. /* The following is extracted from the DEC C header time.h */
  158. /*
  159. ** Beginning in OpenVMS Version 7.0 mktime, time, ctime, strftime
  160. ** have two implementations. One implementation is provided
  161. ** for compatibility and deals with time in terms of local time,
  162. ** the other __utc_* deals with time in terms of UTC.
  163. */
  164. /*
  165. * We use the same conditions as in said time.h to check if we should
  166. * assume that t contains local time (and should therefore be
  167. * adjusted) or UTC (and should therefore be left untouched).
  168. */
  169. # if __CRTL_VER < 70000000 || defined _VMS_V6_SOURCE
  170. /* Get the numerical value of the equivalence string */
  171. status = atoi(logvalue);
  172. /* and use it to move time to GMT */
  173. t -= status;
  174. # endif
  175. /* then convert the result to the time structure */
  176. /*
  177. * Since there was no gmtime_r() to do this stuff for us, we have to
  178. * do it the hard way.
  179. */
  180. {
  181. /*-
  182. * The VMS epoch is the astronomical Smithsonian date,
  183. if I remember correctly, which is November 17, 1858.
  184. Furthermore, time is measure in thenths of microseconds
  185. and stored in quadwords (64 bit integers). unix_epoch
  186. below is January 1st 1970 expressed as a VMS time. The
  187. following code was used to get this number:
  188. #include <stdio.h>
  189. #include <stdlib.h>
  190. #include <lib$routines.h>
  191. #include <starlet.h>
  192. main()
  193. {
  194. unsigned long systime[2];
  195. unsigned short epoch_values[7] =
  196. { 1970, 1, 1, 0, 0, 0, 0 };
  197. lib$cvt_vectim(epoch_values, systime);
  198. printf("%u %u", systime[0], systime[1]);
  199. }
  200. */
  201. unsigned long unix_epoch[2] = { 1273708544, 8164711 };
  202. unsigned long deltatime[2];
  203. unsigned long systime[2];
  204. struct vms_vectime {
  205. short year, month, day, hour, minute, second, centi_second;
  206. } time_values;
  207. long operation;
  208. /*
  209. * Turn the number of seconds since January 1st 1970 to an
  210. * internal delta time. Note that lib$cvt_to_internal_time() will
  211. * assume that t is signed, and will therefore break on 32-bit
  212. * systems some time in 2038.
  213. */
  214. operation = LIB$K_DELTA_SECONDS;
  215. status = lib$cvt_to_internal_time(&operation, &t, deltatime);
  216. /*
  217. * Add the delta time with the Unix epoch and we have the current
  218. * UTC time in internal format
  219. */
  220. status = lib$add_times(unix_epoch, deltatime, systime);
  221. /* Turn the internal time into a time vector */
  222. status = sys$numtim(&time_values, systime);
  223. /* Fill in the struct tm with the result */
  224. result->tm_sec = time_values.second;
  225. result->tm_min = time_values.minute;
  226. result->tm_hour = time_values.hour;
  227. result->tm_mday = time_values.day;
  228. result->tm_mon = time_values.month - 1;
  229. result->tm_year = time_values.year - 1900;
  230. operation = LIB$K_DAY_OF_WEEK;
  231. status = lib$cvt_from_internal_time(&operation,
  232. &result->tm_wday, systime);
  233. result->tm_wday %= 7;
  234. operation = LIB$K_DAY_OF_YEAR;
  235. status = lib$cvt_from_internal_time(&operation,
  236. &result->tm_yday, systime);
  237. result->tm_yday--;
  238. result->tm_isdst = 0; /* There's no way to know... */
  239. ts = result;
  240. }
  241. }
  242. #endif
  243. return ts;
  244. }
  245. /*
  246. * Take a tm structure and add an offset to it. This avoids any OS issues
  247. * with restricted date types and overflows which cause the year 2038
  248. * problem.
  249. */
  250. #define SECS_PER_DAY (24 * 60 * 60)
  251. static long date_to_julian(int y, int m, int d);
  252. static void julian_to_date(long jd, int *y, int *m, int *d);
  253. static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
  254. long *pday, int *psec);
  255. int OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
  256. {
  257. int time_sec, time_year, time_month, time_day;
  258. long time_jd;
  259. /* Convert time and offset into julian day and seconds */
  260. if (!julian_adj(tm, off_day, offset_sec, &time_jd, &time_sec))
  261. return 0;
  262. /* Convert Julian day back to date */
  263. julian_to_date(time_jd, &time_year, &time_month, &time_day);
  264. if (time_year < 1900 || time_year > 9999)
  265. return 0;
  266. /* Update tm structure */
  267. tm->tm_year = time_year - 1900;
  268. tm->tm_mon = time_month - 1;
  269. tm->tm_mday = time_day;
  270. tm->tm_hour = time_sec / 3600;
  271. tm->tm_min = (time_sec / 60) % 60;
  272. tm->tm_sec = time_sec % 60;
  273. return 1;
  274. }
  275. int OPENSSL_gmtime_diff(int *pday, int *psec,
  276. const struct tm *from, const struct tm *to)
  277. {
  278. int from_sec, to_sec, diff_sec;
  279. long from_jd, to_jd, diff_day;
  280. if (!julian_adj(from, 0, 0, &from_jd, &from_sec))
  281. return 0;
  282. if (!julian_adj(to, 0, 0, &to_jd, &to_sec))
  283. return 0;
  284. diff_day = to_jd - from_jd;
  285. diff_sec = to_sec - from_sec;
  286. /* Adjust differences so both positive or both negative */
  287. if (diff_day > 0 && diff_sec < 0) {
  288. diff_day--;
  289. diff_sec += SECS_PER_DAY;
  290. }
  291. if (diff_day < 0 && diff_sec > 0) {
  292. diff_day++;
  293. diff_sec -= SECS_PER_DAY;
  294. }
  295. if (pday)
  296. *pday = (int)diff_day;
  297. if (psec)
  298. *psec = diff_sec;
  299. return 1;
  300. }
  301. /* Convert tm structure and offset into julian day and seconds */
  302. static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
  303. long *pday, int *psec)
  304. {
  305. int offset_hms, offset_day;
  306. long time_jd;
  307. int time_year, time_month, time_day;
  308. /* split offset into days and day seconds */
  309. offset_day = offset_sec / SECS_PER_DAY;
  310. /* Avoid sign issues with % operator */
  311. offset_hms = offset_sec - (offset_day * SECS_PER_DAY);
  312. offset_day += off_day;
  313. /* Add current time seconds to offset */
  314. offset_hms += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
  315. /* Adjust day seconds if overflow */
  316. if (offset_hms >= SECS_PER_DAY) {
  317. offset_day++;
  318. offset_hms -= SECS_PER_DAY;
  319. } else if (offset_hms < 0) {
  320. offset_day--;
  321. offset_hms += SECS_PER_DAY;
  322. }
  323. /*
  324. * Convert date of time structure into a Julian day number.
  325. */
  326. time_year = tm->tm_year + 1900;
  327. time_month = tm->tm_mon + 1;
  328. time_day = tm->tm_mday;
  329. time_jd = date_to_julian(time_year, time_month, time_day);
  330. /* Work out Julian day of new date */
  331. time_jd += offset_day;
  332. if (time_jd < 0)
  333. return 0;
  334. *pday = time_jd;
  335. *psec = offset_hms;
  336. return 1;
  337. }
  338. /*
  339. * Convert date to and from julian day Uses Fliegel & Van Flandern algorithm
  340. */
  341. static long date_to_julian(int y, int m, int d)
  342. {
  343. return (1461 * (y + 4800 + (m - 14) / 12)) / 4 +
  344. (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 -
  345. (3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 + d - 32075;
  346. }
  347. static void julian_to_date(long jd, int *y, int *m, int *d)
  348. {
  349. long L = jd + 68569;
  350. long n = (4 * L) / 146097;
  351. long i, j;
  352. L = L - (146097 * n + 3) / 4;
  353. i = (4000 * (L + 1)) / 1461001;
  354. L = L - (1461 * i) / 4 + 31;
  355. j = (80 * L) / 2447;
  356. *d = L - (2447 * j) / 80;
  357. L = j / 11;
  358. *m = j + 2 - (12 * L);
  359. *y = 100 * (n - 49) + i + L;
  360. }
  361. #ifdef OPENSSL_TIME_TEST
  362. # include <stdio.h>
  363. /*
  364. * Time checking test code. Check times are identical for a wide range of
  365. * offsets. This should be run on a machine with 64 bit time_t or it will
  366. * trigger the very errors the routines fix.
  367. */
  368. int main(int argc, char **argv)
  369. {
  370. long offset;
  371. for (offset = 0; offset < 1000000; offset++) {
  372. check_time(offset);
  373. check_time(-offset);
  374. check_time(offset * 1000);
  375. check_time(-offset * 1000);
  376. }
  377. }
  378. int check_time(long offset)
  379. {
  380. struct tm tm1, tm2, o1;
  381. int off_day, off_sec;
  382. long toffset;
  383. time_t t1, t2;
  384. time(&t1);
  385. t2 = t1 + offset;
  386. OPENSSL_gmtime(&t2, &tm2);
  387. OPENSSL_gmtime(&t1, &tm1);
  388. o1 = tm1;
  389. OPENSSL_gmtime_adj(&tm1, 0, offset);
  390. if ((tm1.tm_year != tm2.tm_year) ||
  391. (tm1.tm_mon != tm2.tm_mon) ||
  392. (tm1.tm_mday != tm2.tm_mday) ||
  393. (tm1.tm_hour != tm2.tm_hour) ||
  394. (tm1.tm_min != tm2.tm_min) || (tm1.tm_sec != tm2.tm_sec)) {
  395. fprintf(stderr, "TIME ERROR!!\n");
  396. fprintf(stderr, "Time1: %d/%d/%d, %d:%02d:%02d\n",
  397. tm2.tm_mday, tm2.tm_mon + 1, tm2.tm_year + 1900,
  398. tm2.tm_hour, tm2.tm_min, tm2.tm_sec);
  399. fprintf(stderr, "Time2: %d/%d/%d, %d:%02d:%02d\n",
  400. tm1.tm_mday, tm1.tm_mon + 1, tm1.tm_year + 1900,
  401. tm1.tm_hour, tm1.tm_min, tm1.tm_sec);
  402. return 0;
  403. }
  404. OPENSSL_gmtime_diff(&o1, &tm1, &off_day, &off_sec);
  405. toffset = (long)off_day *SECS_PER_DAY + off_sec;
  406. if (offset != toffset) {
  407. fprintf(stderr, "TIME OFFSET ERROR!!\n");
  408. fprintf(stderr, "Expected %ld, Got %ld (%d:%d)\n",
  409. offset, toffset, off_day, off_sec);
  410. return 0;
  411. }
  412. return 1;
  413. }
  414. #endif