o_time.c 12 KB

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