o_time.c 14 KB

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