155714Skris/* crypto/asn1/a_gentm.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
855714Skris *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1555714Skris *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
2255714Skris *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4055714Skris *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
5255714Skris *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris/* GENERALIZEDTIME implementation, written by Steve Henson. Based on UTCTIME */
6055714Skris
6155714Skris#include <stdio.h>
6255714Skris#include <time.h>
6355714Skris#include "cryptlib.h"
64109998Smarkm#include "o_time.h"
6555714Skris#include <openssl/asn1.h>
6655714Skris
67109998Smarkm#if 0
6859191Skris
6955714Skrisint i2d_ASN1_GENERALIZEDTIME(ASN1_GENERALIZEDTIME *a, unsigned char **pp)
7055714Skris	{
7155714Skris#ifdef CHARSET_EBCDIC
7255714Skris	/* KLUDGE! We convert to ascii before writing DER */
7355714Skris	int len;
7455714Skris	char tmp[24];
7555714Skris	ASN1_STRING tmpstr = *(ASN1_STRING *)a;
7655714Skris
7755714Skris	len = tmpstr.length;
7855714Skris	ebcdic2ascii(tmp, tmpstr.data, (len >= sizeof tmp) ? sizeof tmp : len);
7955714Skris	tmpstr.data = tmp;
8055714Skris
8155714Skris	a = (ASN1_GENERALIZEDTIME *) &tmpstr;
8255714Skris#endif
8355714Skris	return(i2d_ASN1_bytes((ASN1_STRING *)a,pp,
8455714Skris		V_ASN1_GENERALIZEDTIME,V_ASN1_UNIVERSAL));
8555714Skris	}
8655714Skris
8755714Skris
8855714SkrisASN1_GENERALIZEDTIME *d2i_ASN1_GENERALIZEDTIME(ASN1_GENERALIZEDTIME **a,
8955714Skris	     unsigned char **pp, long length)
9055714Skris	{
9155714Skris	ASN1_GENERALIZEDTIME *ret=NULL;
9255714Skris
9355714Skris	ret=(ASN1_GENERALIZEDTIME *)d2i_ASN1_bytes((ASN1_STRING **)a,pp,length,
9455714Skris		V_ASN1_GENERALIZEDTIME,V_ASN1_UNIVERSAL);
9555714Skris	if (ret == NULL)
9655714Skris		{
9755714Skris		ASN1err(ASN1_F_D2I_ASN1_GENERALIZEDTIME,ERR_R_NESTED_ASN1_ERROR);
9855714Skris		return(NULL);
9955714Skris		}
10055714Skris#ifdef CHARSET_EBCDIC
10155714Skris	ascii2ebcdic(ret->data, ret->data, ret->length);
10255714Skris#endif
10355714Skris	if (!ASN1_GENERALIZEDTIME_check(ret))
10455714Skris		{
10555714Skris		ASN1err(ASN1_F_D2I_ASN1_GENERALIZEDTIME,ASN1_R_INVALID_TIME_FORMAT);
10655714Skris		goto err;
10755714Skris		}
10855714Skris
10955714Skris	return(ret);
11055714Skriserr:
11155714Skris	if ((ret != NULL) && ((a == NULL) || (*a != ret)))
11259191Skris		M_ASN1_GENERALIZEDTIME_free(ret);
11355714Skris	return(NULL);
11455714Skris	}
11555714Skris
116109998Smarkm#endif
117109998Smarkm
11855714Skrisint ASN1_GENERALIZEDTIME_check(ASN1_GENERALIZEDTIME *d)
11955714Skris	{
120238405Sjkim	static const int min[9]={ 0, 0, 1, 1, 0, 0, 0, 0, 0};
121238405Sjkim	static const int max[9]={99, 99,12,31,23,59,59,12,59};
12255714Skris	char *a;
12355714Skris	int n,i,l,o;
12455714Skris
12555714Skris	if (d->type != V_ASN1_GENERALIZEDTIME) return(0);
12655714Skris	l=d->length;
12755714Skris	a=(char *)d->data;
12855714Skris	o=0;
12955714Skris	/* GENERALIZEDTIME is similar to UTCTIME except the year is
13055714Skris         * represented as YYYY. This stuff treats everything as a two digit
13155714Skris         * field so make first two fields 00 to 99
13255714Skris         */
13355714Skris	if (l < 13) goto err;
13455714Skris	for (i=0; i<7; i++)
13555714Skris		{
13655714Skris		if ((i == 6) && ((a[o] == 'Z') ||
13755714Skris			(a[o] == '+') || (a[o] == '-')))
13855714Skris			{ i++; break; }
13955714Skris		if ((a[o] < '0') || (a[o] > '9')) goto err;
14055714Skris		n= a[o]-'0';
14155714Skris		if (++o > l) goto err;
14255714Skris
14355714Skris		if ((a[o] < '0') || (a[o] > '9')) goto err;
14455714Skris		n=(n*10)+ a[o]-'0';
14555714Skris		if (++o > l) goto err;
14655714Skris
14755714Skris		if ((n < min[i]) || (n > max[i])) goto err;
14855714Skris		}
149109998Smarkm	/* Optional fractional seconds: decimal point followed by one
150109998Smarkm	 * or more digits.
151109998Smarkm	 */
152109998Smarkm	if (a[o] == '.')
153109998Smarkm		{
154109998Smarkm		if (++o > l) goto err;
155109998Smarkm		i = o;
156109998Smarkm		while ((a[o] >= '0') && (a[o] <= '9') && (o <= l))
157109998Smarkm			o++;
158109998Smarkm		/* Must have at least one digit after decimal point */
159109998Smarkm		if (i == o) goto err;
160109998Smarkm		}
161109998Smarkm
16255714Skris	if (a[o] == 'Z')
16355714Skris		o++;
16455714Skris	else if ((a[o] == '+') || (a[o] == '-'))
16555714Skris		{
16655714Skris		o++;
16755714Skris		if (o+4 > l) goto err;
16855714Skris		for (i=7; i<9; i++)
16955714Skris			{
17055714Skris			if ((a[o] < '0') || (a[o] > '9')) goto err;
17155714Skris			n= a[o]-'0';
17255714Skris			o++;
17355714Skris			if ((a[o] < '0') || (a[o] > '9')) goto err;
17455714Skris			n=(n*10)+ a[o]-'0';
17555714Skris			if ((n < min[i]) || (n > max[i])) goto err;
17655714Skris			o++;
17755714Skris			}
17855714Skris		}
179238405Sjkim	else
180238405Sjkim		{
181238405Sjkim		/* Missing time zone information. */
182238405Sjkim		goto err;
183238405Sjkim		}
18455714Skris	return(o == l);
18555714Skriserr:
18655714Skris	return(0);
18755714Skris	}
18855714Skris
189160814Ssimonint ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str)
19055714Skris	{
19155714Skris	ASN1_GENERALIZEDTIME t;
19255714Skris
19355714Skris	t.type=V_ASN1_GENERALIZEDTIME;
19455714Skris	t.length=strlen(str);
19555714Skris	t.data=(unsigned char *)str;
19655714Skris	if (ASN1_GENERALIZEDTIME_check(&t))
19755714Skris		{
19855714Skris		if (s != NULL)
19955714Skris			{
200160814Ssimon			if (!ASN1_STRING_set((ASN1_STRING *)s,
201160814Ssimon				(unsigned char *)str,t.length))
202160814Ssimon				return 0;
203109998Smarkm			s->type=V_ASN1_GENERALIZEDTIME;
20455714Skris			}
20555714Skris		return(1);
20655714Skris		}
20755714Skris	else
20855714Skris		return(0);
20955714Skris	}
21055714Skris
21155714SkrisASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
21255714Skris	     time_t t)
21355714Skris	{
214238405Sjkim		return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0);
215238405Sjkim	}
216238405Sjkim
217238405SjkimASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
218238405Sjkim	     time_t t, int offset_day, long offset_sec)
219238405Sjkim	{
22055714Skris	char *p;
22155714Skris	struct tm *ts;
22255714Skris	struct tm data;
223127128Snectar	size_t len = 20;
22455714Skris
22555714Skris	if (s == NULL)
22659191Skris		s=M_ASN1_GENERALIZEDTIME_new();
22755714Skris	if (s == NULL)
22855714Skris		return(NULL);
22955714Skris
230109998Smarkm	ts=OPENSSL_gmtime(&t, &data);
231109998Smarkm	if (ts == NULL)
232109998Smarkm		return(NULL);
233109998Smarkm
234238405Sjkim	if (offset_day || offset_sec)
235238405Sjkim		{
236238405Sjkim		if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
237238405Sjkim			return NULL;
238238405Sjkim		}
239238405Sjkim
24055714Skris	p=(char *)s->data;
241127128Snectar	if ((p == NULL) || ((size_t)s->length < len))
24255714Skris		{
243127128Snectar		p=OPENSSL_malloc(len);
244160814Ssimon		if (p == NULL)
245160814Ssimon			{
246238405Sjkim			ASN1err(ASN1_F_ASN1_GENERALIZEDTIME_ADJ,
247160814Ssimon				ERR_R_MALLOC_FAILURE);
248160814Ssimon			return(NULL);
249160814Ssimon			}
25055714Skris		if (s->data != NULL)
25168651Skris			OPENSSL_free(s->data);
25255714Skris		s->data=(unsigned char *)p;
25355714Skris		}
25455714Skris
255127128Snectar	BIO_snprintf(p,len,"%04d%02d%02d%02d%02d%02dZ",ts->tm_year + 1900,
256127128Snectar		     ts->tm_mon+1,ts->tm_mday,ts->tm_hour,ts->tm_min,ts->tm_sec);
25755714Skris	s->length=strlen(p);
25855714Skris	s->type=V_ASN1_GENERALIZEDTIME;
25955714Skris#ifdef CHARSET_EBCDIC_not
26055714Skris	ebcdic2ascii(s->data, s->data, s->length);
26155714Skris#endif
26255714Skris	return(s);
26355714Skris	}
264