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