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