o_time.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 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_MACOSX) && !defined(OPENSSL_SYS_SUNOS)
  101. if (gmtime_r(timer, result) == NULL)
  102. return NULL;
  103. ts = result;
  104. #elif !defined(OPENSSL_SYS_VMS) || defined(VMS_GMTIME_OK)
  105. ts = gmtime(timer);
  106. if (ts == NULL)
  107. return NULL;
  108. memcpy(result, ts, sizeof(struct tm));
  109. ts = result;
  110. #endif
  111. #if defined( OPENSSL_SYS_VMS) && !defined( VMS_GMTIME_OK)
  112. if (ts == NULL) {
  113. static $DESCRIPTOR(tabnam, "LNM$DCL_LOGICAL");
  114. static $DESCRIPTOR(lognam, "SYS$TIMEZONE_DIFFERENTIAL");
  115. char logvalue[256];
  116. unsigned int reslen = 0;
  117. # if __INITIAL_POINTER_SIZE == 64
  118. ILEB_64 itemlist[2], *pitem;
  119. # else
  120. ILE3 itemlist[2], *pitem;
  121. # endif
  122. int status;
  123. time_t t;
  124. /*
  125. * Setup an itemlist for the call to $TRNLNM - Translate Logical Name.
  126. */
  127. pitem = itemlist;
  128. # if __INITIAL_POINTER_SIZE == 64
  129. pitem->ileb_64$w_mbo = 1;
  130. pitem->ileb_64$w_code = LNM$_STRING;
  131. pitem->ileb_64$l_mbmo = -1;
  132. pitem->ileb_64$q_length = sizeof (logvalue);
  133. pitem->ileb_64$pq_bufaddr = logvalue;
  134. pitem->ileb_64$pq_retlen_addr = (unsigned __int64 *) &reslen;
  135. pitem++;
  136. /* Last item of the item list is null terminated */
  137. pitem->ileb_64$q_length = pitem->ileb_64$w_code = 0;
  138. # else
  139. pitem->ile3$w_length = sizeof (logvalue);
  140. pitem->ile3$w_code = LNM$_STRING;
  141. pitem->ile3$ps_bufaddr = logvalue;
  142. pitem->ile3$ps_retlen_addr = (unsigned short int *) &reslen;
  143. pitem++;
  144. /* Last item of the item list is null terminated */
  145. pitem->ile3$w_length = pitem->ile3$w_code = 0;
  146. # endif
  147. /* Get the value for SYS$TIMEZONE_DIFFERENTIAL */
  148. status = sys$trnlnm(0, &tabnam, &lognam, 0, itemlist);
  149. if (!(status & 1))
  150. return NULL;
  151. logvalue[reslen] = '\0';
  152. t = *timer;
  153. /* The following is extracted from the DEC C header time.h */
  154. /*
  155. ** Beginning in OpenVMS Version 7.0 mktime, time, ctime, strftime
  156. ** have two implementations. One implementation is provided
  157. ** for compatibility and deals with time in terms of local time,
  158. ** the other __utc_* deals with time in terms of UTC.
  159. */
  160. /*
  161. * We use the same conditions as in said time.h to check if we should
  162. * assume that t contains local time (and should therefore be
  163. * adjusted) or UTC (and should therefore be left untouched).
  164. */
  165. # if __CRTL_VER < 70000000 || defined _VMS_V6_SOURCE
  166. /* Get the numerical value of the equivalence string */
  167. status = atoi(logvalue);
  168. /* and use it to move time to GMT */
  169. t -= status;
  170. # endif
  171. /* then convert the result to the time structure */
  172. /*
  173. * Since there was no gmtime_r() to do this stuff for us, we have to
  174. * do it the hard way.
  175. */
  176. {
  177. /*-
  178. * The VMS epoch is the astronomical Smithsonian date,
  179. if I remember correctly, which is November 17, 1858.
  180. Furthermore, time is measure in thenths of microseconds
  181. and stored in quadwords (64 bit integers). unix_epoch
  182. below is January 1st 1970 expressed as a VMS time. The
  183. following code was used to get this number:
  184. #include <stdio.h>
  185. #include <stdlib.h>
  186. #include <lib$routines.h>
  187. #include <starlet.h>
  188. main()
  189. {
  190. unsigned long systime[2];
  191. unsigned short epoch_values[7] =
  192. { 1970, 1, 1, 0, 0, 0, 0 };
  193. lib$cvt_vectim(epoch_values, systime);
  194. printf("%u %u", systime[0], systime[1]);
  195. }
  196. */
  197. unsigned long unix_epoch[2] = { 1273708544, 8164711 };
  198. unsigned long deltatime[2];
  199. unsigned long systime[2];
  200. struct vms_vectime {
  201. short year, month, day, hour, minute, second, centi_second;
  202. } time_values;
  203. long operation;
  204. /*
  205. * Turn the number of seconds since January 1st 1970 to an
  206. * internal delta time. Note that lib$cvt_to_internal_time() will
  207. * assume that t is signed, and will therefore break on 32-bit
  208. * systems some time in 2038.
  209. */
  210. operation = LIB$K_DELTA_SECONDS;
  211. status = lib$cvt_to_internal_time(&operation, &t, deltatime);
  212. /*
  213. * Add the delta time with the Unix epoch and we have the current
  214. * UTC time in internal format
  215. */
  216. status = lib$add_times(unix_epoch, deltatime, systime);
  217. /* Turn the internal time into a time vector */
  218. status = sys$numtim(&time_values, systime);
  219. /* Fill in the struct tm with the result */
  220. result->tm_sec = time_values.second;
  221. result->tm_min = time_values.minute;
  222. result->tm_hour = time_values.hour;
  223. result->tm_mday = time_values.day;
  224. result->tm_mon = time_values.month - 1;
  225. result->tm_year = time_values.year - 1900;
  226. operation = LIB$K_DAY_OF_WEEK;
  227. status = lib$cvt_from_internal_time(&operation,
  228. &result->tm_wday, systime);
  229. result->tm_wday %= 7;
  230. operation = LIB$K_DAY_OF_YEAR;
  231. status = lib$cvt_from_internal_time(&operation,
  232. &result->tm_yday, systime);
  233. result->tm_yday--;
  234. result->tm_isdst = 0; /* There's no way to know... */
  235. ts = result;
  236. }
  237. }
  238. #endif
  239. return ts;
  240. }
  241. /*
  242. * Take a tm structure and add an offset to it. This avoids any OS issues
  243. * with restricted date types and overflows which cause the year 2038
  244. * problem.
  245. */
  246. #define SECS_PER_DAY (24 * 60 * 60)
  247. static long date_to_julian(int y, int m, int d);
  248. static void julian_to_date(long jd, int *y, int *m, int *d);
  249. static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
  250. long *pday, int *psec);
  251. int OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
  252. {
  253. int time_sec, time_year, time_month, time_day;
  254. long time_jd;
  255. /* Convert time and offset into julian day and seconds */
  256. if (!julian_adj(tm, off_day, offset_sec, &time_jd, &time_sec))
  257. return 0;
  258. /* Convert Julian day back to date */
  259. julian_to_date(time_jd, &time_year, &time_month, &time_day);
  260. if (time_year < 1900 || time_year > 9999)
  261. return 0;
  262. /* Update tm structure */
  263. tm->tm_year = time_year - 1900;
  264. tm->tm_mon = time_month - 1;
  265. tm->tm_mday = time_day;
  266. tm->tm_hour = time_sec / 3600;
  267. tm->tm_min = (time_sec / 60) % 60;
  268. tm->tm_sec = time_sec % 60;
  269. return 1;
  270. }
  271. int OPENSSL_gmtime_diff(int *pday, int *psec,
  272. const struct tm *from, const struct tm *to)
  273. {
  274. int from_sec, to_sec, diff_sec;
  275. long from_jd, to_jd, diff_day;
  276. if (!julian_adj(from, 0, 0, &from_jd, &from_sec))
  277. return 0;
  278. if (!julian_adj(to, 0, 0, &to_jd, &to_sec))
  279. return 0;
  280. diff_day = to_jd - from_jd;
  281. diff_sec = to_sec - from_sec;
  282. /* Adjust differences so both positive or both negative */
  283. if (diff_day > 0 && diff_sec < 0) {
  284. diff_day--;
  285. diff_sec += SECS_PER_DAY;
  286. }
  287. if (diff_day < 0 && diff_sec > 0) {
  288. diff_day++;
  289. diff_sec -= SECS_PER_DAY;
  290. }
  291. if (pday)
  292. *pday = (int)diff_day;
  293. if (psec)
  294. *psec = diff_sec;
  295. return 1;
  296. }
  297. /* Convert tm structure and offset into julian day and seconds */
  298. static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
  299. long *pday, int *psec)
  300. {
  301. int offset_hms, offset_day;
  302. long time_jd;
  303. int time_year, time_month, time_day;
  304. /* split offset into days and day seconds */
  305. offset_day = offset_sec / SECS_PER_DAY;
  306. /* Avoid sign issues with % operator */
  307. offset_hms = offset_sec - (offset_day * SECS_PER_DAY);
  308. offset_day += off_day;
  309. /* Add current time seconds to offset */
  310. offset_hms += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
  311. /* Adjust day seconds if overflow */
  312. if (offset_hms >= SECS_PER_DAY) {
  313. offset_day++;
  314. offset_hms -= SECS_PER_DAY;
  315. } else if (offset_hms < 0) {
  316. offset_day--;
  317. offset_hms += SECS_PER_DAY;
  318. }
  319. /*
  320. * Convert date of time structure into a Julian day number.
  321. */
  322. time_year = tm->tm_year + 1900;
  323. time_month = tm->tm_mon + 1;
  324. time_day = tm->tm_mday;
  325. time_jd = date_to_julian(time_year, time_month, time_day);
  326. /* Work out Julian day of new date */
  327. time_jd += offset_day;
  328. if (time_jd < 0)
  329. return 0;
  330. *pday = time_jd;
  331. *psec = offset_hms;
  332. return 1;
  333. }
  334. /*
  335. * Convert date to and from julian day Uses Fliegel & Van Flandern algorithm
  336. */
  337. static long date_to_julian(int y, int m, int d)
  338. {
  339. return (1461 * (y + 4800 + (m - 14) / 12)) / 4 +
  340. (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 -
  341. (3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 + d - 32075;
  342. }
  343. static void julian_to_date(long jd, int *y, int *m, int *d)
  344. {
  345. long L = jd + 68569;
  346. long n = (4 * L) / 146097;
  347. long i, j;
  348. L = L - (146097 * n + 3) / 4;
  349. i = (4000 * (L + 1)) / 1461001;
  350. L = L - (1461 * i) / 4 + 31;
  351. j = (80 * L) / 2447;
  352. *d = L - (2447 * j) / 80;
  353. L = j / 11;
  354. *m = j + 2 - (12 * L);
  355. *y = 100 * (n - 49) + i + L;
  356. }
  357. #ifdef OPENSSL_TIME_TEST
  358. # include <stdio.h>
  359. /*
  360. * Time checking test code. Check times are identical for a wide range of
  361. * offsets. This should be run on a machine with 64 bit time_t or it will
  362. * trigger the very errors the routines fix.
  363. */
  364. int main(int argc, char **argv)
  365. {
  366. long offset;
  367. for (offset = 0; offset < 1000000; offset++) {
  368. check_time(offset);
  369. check_time(-offset);
  370. check_time(offset * 1000);
  371. check_time(-offset * 1000);
  372. }
  373. }
  374. int check_time(long offset)
  375. {
  376. struct tm tm1, tm2, o1;
  377. int off_day, off_sec;
  378. long toffset;
  379. time_t t1, t2;
  380. time(&t1);
  381. t2 = t1 + offset;
  382. OPENSSL_gmtime(&t2, &tm2);
  383. OPENSSL_gmtime(&t1, &tm1);
  384. o1 = tm1;
  385. OPENSSL_gmtime_adj(&tm1, 0, offset);
  386. if ((tm1.tm_year != tm2.tm_year) ||
  387. (tm1.tm_mon != tm2.tm_mon) ||
  388. (tm1.tm_mday != tm2.tm_mday) ||
  389. (tm1.tm_hour != tm2.tm_hour) ||
  390. (tm1.tm_min != tm2.tm_min) || (tm1.tm_sec != tm2.tm_sec)) {
  391. fprintf(stderr, "TIME ERROR!!\n");
  392. fprintf(stderr, "Time1: %d/%d/%d, %d:%02d:%02d\n",
  393. tm2.tm_mday, tm2.tm_mon + 1, tm2.tm_year + 1900,
  394. tm2.tm_hour, tm2.tm_min, tm2.tm_sec);
  395. fprintf(stderr, "Time2: %d/%d/%d, %d:%02d:%02d\n",
  396. tm1.tm_mday, tm1.tm_mon + 1, tm1.tm_year + 1900,
  397. tm1.tm_hour, tm1.tm_min, tm1.tm_sec);
  398. return 0;
  399. }
  400. OPENSSL_gmtime_diff(&o1, &tm1, &off_day, &off_sec);
  401. toffset = (long)off_day *SECS_PER_DAY + off_sec;
  402. if (offset != toffset) {
  403. fprintf(stderr, "TIME OFFSET ERROR!!\n");
  404. fprintf(stderr, "Expected %ld, Got %ld (%d:%d)\n",
  405. offset, toffset, off_day, off_sec);
  406. return 0;
  407. }
  408. return 1;
  409. }
  410. #endif