xref: /onnv-gate/usr/src/common/openssl/crypto/o_time.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* crypto/o_time.c -*- mode:C; c-file-style: "eay" -*- */
2*0Sstevel@tonic-gate /* Written by Richard Levitte (richard@levitte.org) for the OpenSSL
3*0Sstevel@tonic-gate  * project 2001.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate /* ====================================================================
6*0Sstevel@tonic-gate  * Copyright (c) 2001 The OpenSSL Project.  All rights reserved.
7*0Sstevel@tonic-gate  *
8*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
9*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
10*0Sstevel@tonic-gate  * are met:
11*0Sstevel@tonic-gate  *
12*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
13*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
14*0Sstevel@tonic-gate  *
15*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
16*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
17*0Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
18*0Sstevel@tonic-gate  *    distribution.
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
21*0Sstevel@tonic-gate  *    software must display the following acknowledgment:
22*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
23*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24*0Sstevel@tonic-gate  *
25*0Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26*0Sstevel@tonic-gate  *    endorse or promote products derived from this software without
27*0Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
28*0Sstevel@tonic-gate  *    licensing@OpenSSL.org.
29*0Sstevel@tonic-gate  *
30*0Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
31*0Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
32*0Sstevel@tonic-gate  *    permission of the OpenSSL Project.
33*0Sstevel@tonic-gate  *
34*0Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
35*0Sstevel@tonic-gate  *    acknowledgment:
36*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
37*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38*0Sstevel@tonic-gate  *
39*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40*0Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42*0Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43*0Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44*0Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45*0Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46*0Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48*0Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49*0Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50*0Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
51*0Sstevel@tonic-gate  * ====================================================================
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
54*0Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
55*0Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
56*0Sstevel@tonic-gate  *
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #include <openssl/e_os2.h>
60*0Sstevel@tonic-gate #include <string.h>
61*0Sstevel@tonic-gate #include "o_time.h"
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
64*0Sstevel@tonic-gate # include <libdtdef.h>
65*0Sstevel@tonic-gate # include <lib$routines.h>
66*0Sstevel@tonic-gate # include <lnmdef.h>
67*0Sstevel@tonic-gate # include <starlet.h>
68*0Sstevel@tonic-gate # include <descrip.h>
69*0Sstevel@tonic-gate # include <stdlib.h>
70*0Sstevel@tonic-gate #endif
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result)
73*0Sstevel@tonic-gate 	{
74*0Sstevel@tonic-gate 	struct tm *ts = NULL;
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate #if defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_OS2) && !defined(__CYGWIN32__) && (!defined(OPENSSL_SYS_VMS) || defined(gmtime_r)) && !defined(OPENSSL_SYS_MACOSX) && !defined(OPENSSL_SYS_SUNOS)
77*0Sstevel@tonic-gate 	/* should return &data, but doesn't on some systems,
78*0Sstevel@tonic-gate 	   so we don't even look at the return value */
79*0Sstevel@tonic-gate 	gmtime_r(timer,result);
80*0Sstevel@tonic-gate 	ts = result;
81*0Sstevel@tonic-gate #elif !defined(OPENSSL_SYS_VMS)
82*0Sstevel@tonic-gate 	ts = gmtime(timer);
83*0Sstevel@tonic-gate 	if (ts == NULL)
84*0Sstevel@tonic-gate 		return NULL;
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	memcpy(result, ts, sizeof(struct tm));
87*0Sstevel@tonic-gate 	ts = result;
88*0Sstevel@tonic-gate #endif
89*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
90*0Sstevel@tonic-gate 	if (ts == NULL)
91*0Sstevel@tonic-gate 		{
92*0Sstevel@tonic-gate 		static $DESCRIPTOR(tabnam,"LNM$DCL_LOGICAL");
93*0Sstevel@tonic-gate 		static $DESCRIPTOR(lognam,"SYS$TIMEZONE_DIFFERENTIAL");
94*0Sstevel@tonic-gate 		char logvalue[256];
95*0Sstevel@tonic-gate 		unsigned int reslen = 0;
96*0Sstevel@tonic-gate 		struct {
97*0Sstevel@tonic-gate 			short buflen;
98*0Sstevel@tonic-gate 			short code;
99*0Sstevel@tonic-gate 			void *bufaddr;
100*0Sstevel@tonic-gate 			unsigned int *reslen;
101*0Sstevel@tonic-gate 		} itemlist[] = {
102*0Sstevel@tonic-gate 			{ 0, LNM$_STRING, 0, 0 },
103*0Sstevel@tonic-gate 			{ 0, 0, 0, 0 },
104*0Sstevel@tonic-gate 		};
105*0Sstevel@tonic-gate 		int status;
106*0Sstevel@tonic-gate 		time_t t;
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 		/* Get the value for SYS$TIMEZONE_DIFFERENTIAL */
109*0Sstevel@tonic-gate 		itemlist[0].buflen = sizeof(logvalue);
110*0Sstevel@tonic-gate 		itemlist[0].bufaddr = logvalue;
111*0Sstevel@tonic-gate 		itemlist[0].reslen = &reslen;
112*0Sstevel@tonic-gate 		status = sys$trnlnm(0, &tabnam, &lognam, 0, itemlist);
113*0Sstevel@tonic-gate 		if (!(status & 1))
114*0Sstevel@tonic-gate 			return NULL;
115*0Sstevel@tonic-gate 		logvalue[reslen] = '\0';
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 		/* Get the numerical value of the equivalence string */
118*0Sstevel@tonic-gate 		status = atoi(logvalue);
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 		/* and use it to move time to GMT */
121*0Sstevel@tonic-gate 		t = *timer - status;
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 		/* then convert the result to the time structure */
124*0Sstevel@tonic-gate #ifndef OPENSSL_THREADS
125*0Sstevel@tonic-gate 		ts=(struct tm *)localtime(&t);
126*0Sstevel@tonic-gate #else
127*0Sstevel@tonic-gate 		/* Since there was no gmtime_r() to do this stuff for us,
128*0Sstevel@tonic-gate 		   we have to do it the hard way. */
129*0Sstevel@tonic-gate 		{
130*0Sstevel@tonic-gate 		/* The VMS epoch is the astronomical Smithsonian date,
131*0Sstevel@tonic-gate 		   if I remember correctly, which is November 17, 1858.
132*0Sstevel@tonic-gate 		   Furthermore, time is measure in thenths of microseconds
133*0Sstevel@tonic-gate 		   and stored in quadwords (64 bit integers).  unix_epoch
134*0Sstevel@tonic-gate 		   below is January 1st 1970 expressed as a VMS time.  The
135*0Sstevel@tonic-gate 		   following code was used to get this number:
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 		   #include <stdio.h>
138*0Sstevel@tonic-gate 		   #include <stdlib.h>
139*0Sstevel@tonic-gate 		   #include <lib$routines.h>
140*0Sstevel@tonic-gate 		   #include <starlet.h>
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 		   main()
143*0Sstevel@tonic-gate 		   {
144*0Sstevel@tonic-gate 		     unsigned long systime[2];
145*0Sstevel@tonic-gate 		     unsigned short epoch_values[7] =
146*0Sstevel@tonic-gate 		       { 1970, 1, 1, 0, 0, 0, 0 };
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 		     lib$cvt_vectim(epoch_values, systime);
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 		     printf("%u %u", systime[0], systime[1]);
151*0Sstevel@tonic-gate 		   }
152*0Sstevel@tonic-gate 		*/
153*0Sstevel@tonic-gate 		unsigned long unix_epoch[2] = { 1273708544, 8164711 };
154*0Sstevel@tonic-gate 		unsigned long deltatime[2];
155*0Sstevel@tonic-gate 		unsigned long systime[2];
156*0Sstevel@tonic-gate 		struct vms_vectime
157*0Sstevel@tonic-gate 			{
158*0Sstevel@tonic-gate 			short year, month, day, hour, minute, second,
159*0Sstevel@tonic-gate 				centi_second;
160*0Sstevel@tonic-gate 			} time_values;
161*0Sstevel@tonic-gate 		long operation;
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 		/* Turn the number of seconds since January 1st 1970 to
164*0Sstevel@tonic-gate 		   an internal delta time.
165*0Sstevel@tonic-gate 		   Note that lib$cvt_to_internal_time() will assume
166*0Sstevel@tonic-gate 		   that t is signed, and will therefore break on 32-bit
167*0Sstevel@tonic-gate 		   systems some time in 2038.
168*0Sstevel@tonic-gate 		*/
169*0Sstevel@tonic-gate 		operation = LIB$K_DELTA_SECONDS;
170*0Sstevel@tonic-gate 		status = lib$cvt_to_internal_time(&operation,
171*0Sstevel@tonic-gate 			&t, deltatime);
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 		/* Add the delta time with the Unix epoch and we have
174*0Sstevel@tonic-gate 		   the current UTC time in internal format */
175*0Sstevel@tonic-gate 		status = lib$add_times(unix_epoch, deltatime, systime);
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 		/* Turn the internal time into a time vector */
178*0Sstevel@tonic-gate 		status = sys$numtim(&time_values, systime);
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 		/* Fill in the struct tm with the result */
181*0Sstevel@tonic-gate 		result->tm_sec = time_values.second;
182*0Sstevel@tonic-gate 		result->tm_min = time_values.minute;
183*0Sstevel@tonic-gate 		result->tm_hour = time_values.hour;
184*0Sstevel@tonic-gate 		result->tm_mday = time_values.day;
185*0Sstevel@tonic-gate 		result->tm_mon = time_values.month - 1;
186*0Sstevel@tonic-gate 		result->tm_year = time_values.year - 1900;
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 		operation = LIB$K_DAY_OF_WEEK;
189*0Sstevel@tonic-gate 		status = lib$cvt_from_internal_time(&operation,
190*0Sstevel@tonic-gate 			&result->tm_wday, systime);
191*0Sstevel@tonic-gate 		result->tm_wday %= 7;
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 		operation = LIB$K_DAY_OF_YEAR;
194*0Sstevel@tonic-gate 		status = lib$cvt_from_internal_time(&operation,
195*0Sstevel@tonic-gate 			&result->tm_yday, systime);
196*0Sstevel@tonic-gate 		result->tm_yday--;
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 		result->tm_isdst = 0; /* There's no way to know... */
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 		ts = result;
201*0Sstevel@tonic-gate #endif
202*0Sstevel@tonic-gate 		}
203*0Sstevel@tonic-gate 		}
204*0Sstevel@tonic-gate #endif
205*0Sstevel@tonic-gate 	return ts;
206*0Sstevel@tonic-gate 	}
207