xref: /onnv-gate/usr/src/common/openssl/crypto/o_time.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/o_time.c -*- mode:C; c-file-style: "eay" -*- */
20Sstevel@tonic-gate /* Written by Richard Levitte (richard@levitte.org) for the OpenSSL
30Sstevel@tonic-gate  * project 2001.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate /* ====================================================================
60Sstevel@tonic-gate  * Copyright (c) 2001 The OpenSSL Project.  All rights reserved.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate  * are met:
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate  *
150Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
180Sstevel@tonic-gate  *    distribution.
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate  *    software must display the following acknowledgment:
220Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate  *    endorse or promote products derived from this software without
270Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
280Sstevel@tonic-gate  *    licensing@OpenSSL.org.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate  *    permission of the OpenSSL Project.
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate  *    acknowledgment:
360Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate  * ====================================================================
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
550Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <openssl/e_os2.h>
600Sstevel@tonic-gate #include <string.h>
610Sstevel@tonic-gate #include "o_time.h"
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
640Sstevel@tonic-gate # include <libdtdef.h>
650Sstevel@tonic-gate # include <lib$routines.h>
660Sstevel@tonic-gate # include <lnmdef.h>
670Sstevel@tonic-gate # include <starlet.h>
680Sstevel@tonic-gate # include <descrip.h>
690Sstevel@tonic-gate # include <stdlib.h>
700Sstevel@tonic-gate #endif
710Sstevel@tonic-gate 
OPENSSL_gmtime(const time_t * timer,struct tm * result)720Sstevel@tonic-gate struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result)
730Sstevel@tonic-gate 	{
740Sstevel@tonic-gate 	struct tm *ts = NULL;
750Sstevel@tonic-gate 
760Sstevel@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)
770Sstevel@tonic-gate 	/* should return &data, but doesn't on some systems,
780Sstevel@tonic-gate 	   so we don't even look at the return value */
790Sstevel@tonic-gate 	gmtime_r(timer,result);
800Sstevel@tonic-gate 	ts = result;
810Sstevel@tonic-gate #elif !defined(OPENSSL_SYS_VMS)
820Sstevel@tonic-gate 	ts = gmtime(timer);
830Sstevel@tonic-gate 	if (ts == NULL)
840Sstevel@tonic-gate 		return NULL;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	memcpy(result, ts, sizeof(struct tm));
870Sstevel@tonic-gate 	ts = result;
880Sstevel@tonic-gate #endif
890Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
900Sstevel@tonic-gate 	if (ts == NULL)
910Sstevel@tonic-gate 		{
920Sstevel@tonic-gate 		static $DESCRIPTOR(tabnam,"LNM$DCL_LOGICAL");
930Sstevel@tonic-gate 		static $DESCRIPTOR(lognam,"SYS$TIMEZONE_DIFFERENTIAL");
940Sstevel@tonic-gate 		char logvalue[256];
950Sstevel@tonic-gate 		unsigned int reslen = 0;
960Sstevel@tonic-gate 		struct {
970Sstevel@tonic-gate 			short buflen;
980Sstevel@tonic-gate 			short code;
990Sstevel@tonic-gate 			void *bufaddr;
1000Sstevel@tonic-gate 			unsigned int *reslen;
1010Sstevel@tonic-gate 		} itemlist[] = {
1020Sstevel@tonic-gate 			{ 0, LNM$_STRING, 0, 0 },
1030Sstevel@tonic-gate 			{ 0, 0, 0, 0 },
1040Sstevel@tonic-gate 		};
1050Sstevel@tonic-gate 		int status;
1060Sstevel@tonic-gate 		time_t t;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 		/* Get the value for SYS$TIMEZONE_DIFFERENTIAL */
1090Sstevel@tonic-gate 		itemlist[0].buflen = sizeof(logvalue);
1100Sstevel@tonic-gate 		itemlist[0].bufaddr = logvalue;
1110Sstevel@tonic-gate 		itemlist[0].reslen = &reslen;
1120Sstevel@tonic-gate 		status = sys$trnlnm(0, &tabnam, &lognam, 0, itemlist);
1130Sstevel@tonic-gate 		if (!(status & 1))
1140Sstevel@tonic-gate 			return NULL;
1150Sstevel@tonic-gate 		logvalue[reslen] = '\0';
1160Sstevel@tonic-gate 
117*2139Sjp161948 		t = *timer;
118*2139Sjp161948 
119*2139Sjp161948 /* The following is extracted from the DEC C header time.h */
120*2139Sjp161948 /*
121*2139Sjp161948 **  Beginning in OpenVMS Version 7.0 mktime, time, ctime, strftime
122*2139Sjp161948 **  have two implementations.  One implementation is provided
123*2139Sjp161948 **  for compatibility and deals with time in terms of local time,
124*2139Sjp161948 **  the other __utc_* deals with time in terms of UTC.
125*2139Sjp161948 */
126*2139Sjp161948 /* We use the same conditions as in said time.h to check if we should
127*2139Sjp161948    assume that t contains local time (and should therefore be adjusted)
128*2139Sjp161948    or UTC (and should therefore be left untouched). */
129*2139Sjp161948 #if __CRTL_VER < 70000000 || defined _VMS_V6_SOURCE
1300Sstevel@tonic-gate 		/* Get the numerical value of the equivalence string */
1310Sstevel@tonic-gate 		status = atoi(logvalue);
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 		/* and use it to move time to GMT */
134*2139Sjp161948 		t -= status;
135*2139Sjp161948 #endif
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 		/* then convert the result to the time structure */
138*2139Sjp161948 
1390Sstevel@tonic-gate 		/* Since there was no gmtime_r() to do this stuff for us,
1400Sstevel@tonic-gate 		   we have to do it the hard way. */
1410Sstevel@tonic-gate 		{
1420Sstevel@tonic-gate 		/* The VMS epoch is the astronomical Smithsonian date,
1430Sstevel@tonic-gate 		   if I remember correctly, which is November 17, 1858.
1440Sstevel@tonic-gate 		   Furthermore, time is measure in thenths of microseconds
1450Sstevel@tonic-gate 		   and stored in quadwords (64 bit integers).  unix_epoch
1460Sstevel@tonic-gate 		   below is January 1st 1970 expressed as a VMS time.  The
1470Sstevel@tonic-gate 		   following code was used to get this number:
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 		   #include <stdio.h>
1500Sstevel@tonic-gate 		   #include <stdlib.h>
1510Sstevel@tonic-gate 		   #include <lib$routines.h>
1520Sstevel@tonic-gate 		   #include <starlet.h>
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 		   main()
1550Sstevel@tonic-gate 		   {
1560Sstevel@tonic-gate 		     unsigned long systime[2];
1570Sstevel@tonic-gate 		     unsigned short epoch_values[7] =
1580Sstevel@tonic-gate 		       { 1970, 1, 1, 0, 0, 0, 0 };
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 		     lib$cvt_vectim(epoch_values, systime);
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 		     printf("%u %u", systime[0], systime[1]);
1630Sstevel@tonic-gate 		   }
1640Sstevel@tonic-gate 		*/
1650Sstevel@tonic-gate 		unsigned long unix_epoch[2] = { 1273708544, 8164711 };
1660Sstevel@tonic-gate 		unsigned long deltatime[2];
1670Sstevel@tonic-gate 		unsigned long systime[2];
1680Sstevel@tonic-gate 		struct vms_vectime
1690Sstevel@tonic-gate 			{
1700Sstevel@tonic-gate 			short year, month, day, hour, minute, second,
1710Sstevel@tonic-gate 				centi_second;
1720Sstevel@tonic-gate 			} time_values;
1730Sstevel@tonic-gate 		long operation;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 		/* Turn the number of seconds since January 1st 1970 to
1760Sstevel@tonic-gate 		   an internal delta time.
1770Sstevel@tonic-gate 		   Note that lib$cvt_to_internal_time() will assume
1780Sstevel@tonic-gate 		   that t is signed, and will therefore break on 32-bit
1790Sstevel@tonic-gate 		   systems some time in 2038.
1800Sstevel@tonic-gate 		*/
1810Sstevel@tonic-gate 		operation = LIB$K_DELTA_SECONDS;
1820Sstevel@tonic-gate 		status = lib$cvt_to_internal_time(&operation,
1830Sstevel@tonic-gate 			&t, deltatime);
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 		/* Add the delta time with the Unix epoch and we have
1860Sstevel@tonic-gate 		   the current UTC time in internal format */
1870Sstevel@tonic-gate 		status = lib$add_times(unix_epoch, deltatime, systime);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 		/* Turn the internal time into a time vector */
1900Sstevel@tonic-gate 		status = sys$numtim(&time_values, systime);
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 		/* Fill in the struct tm with the result */
1930Sstevel@tonic-gate 		result->tm_sec = time_values.second;
1940Sstevel@tonic-gate 		result->tm_min = time_values.minute;
1950Sstevel@tonic-gate 		result->tm_hour = time_values.hour;
1960Sstevel@tonic-gate 		result->tm_mday = time_values.day;
1970Sstevel@tonic-gate 		result->tm_mon = time_values.month - 1;
1980Sstevel@tonic-gate 		result->tm_year = time_values.year - 1900;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 		operation = LIB$K_DAY_OF_WEEK;
2010Sstevel@tonic-gate 		status = lib$cvt_from_internal_time(&operation,
2020Sstevel@tonic-gate 			&result->tm_wday, systime);
2030Sstevel@tonic-gate 		result->tm_wday %= 7;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 		operation = LIB$K_DAY_OF_YEAR;
2060Sstevel@tonic-gate 		status = lib$cvt_from_internal_time(&operation,
2070Sstevel@tonic-gate 			&result->tm_yday, systime);
2080Sstevel@tonic-gate 		result->tm_yday--;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 		result->tm_isdst = 0; /* There's no way to know... */
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 		ts = result;
2130Sstevel@tonic-gate 		}
2140Sstevel@tonic-gate 		}
2150Sstevel@tonic-gate #endif
2160Sstevel@tonic-gate 	return ts;
2170Sstevel@tonic-gate 	}
218