1 /* $MirOS: src/lib/libssl/src/crypto/asn1/a_gentm.c,v 1.3 2005/04/29 13:52:29 tg Exp $ */
2 
3 /* crypto/asn1/a_gentm.c */
4 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5  * All rights reserved.
6  *
7  * This package is an SSL implementation written
8  * by Eric Young (eay@cryptsoft.com).
9  * The implementation was written so as to conform with Netscapes SSL.
10  *
11  * This library is free for commercial and non-commercial use as long as
12  * the following conditions are aheared to.  The following conditions
13  * apply to all code found in this distribution, be it the RC4, RSA,
14  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
15  * included with this distribution is covered by the same copyright terms
16  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
17  *
18  * Copyright remains Eric Young's, and as such any Copyright notices in
19  * the code are not to be removed.
20  * If this package is used in a product, Eric Young should be given attribution
21  * as the author of the parts of the library used.
22  * This can be in the form of a textual message at program startup or
23  * in documentation (online or textual) provided with the package.
24  *
25  * Redistribution and use in source and binary forms, with or without
26  * modification, are permitted provided that the following conditions
27  * are met:
28  * 1. Redistributions of source code must retain the copyright
29  *    notice, this list of conditions and the following disclaimer.
30  * 2. Redistributions in binary form must reproduce the above copyright
31  *    notice, this list of conditions and the following disclaimer in the
32  *    documentation and/or other materials provided with the distribution.
33  * 3. All advertising materials mentioning features or use of this software
34  *    must display the following acknowledgement:
35  *    "This product includes cryptographic software written by
36  *     Eric Young (eay@cryptsoft.com)"
37  *    The word 'cryptographic' can be left out if the rouines from the library
38  *    being used are not cryptographic related :-).
39  * 4. If you include any Windows specific code (or a derivative thereof) from
40  *    the apps directory (application code) you must include an acknowledgement:
41  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
42  *
43  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
44  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53  * SUCH DAMAGE.
54  *
55  * The licence and distribution terms for any publically available version or
56  * derivative of this code cannot be changed.  i.e. this code cannot simply be
57  * copied and put under another distribution licence
58  * [including the GNU Public Licence.]
59  */
60 
61 /* GENERALIZEDTIME implementation, written by Steve Henson. Based on UTCTIME */
62 
63 #include <stdio.h>
64 #include <time.h>
65 #include "cryptlib.h"
66 #include "o_time.h"
67 #include <openssl/asn1.h>
68 
69 #if 0
70 
71 int i2d_ASN1_GENERALIZEDTIME(ASN1_GENERALIZEDTIME *a, unsigned char **pp)
72 	{
73 #ifdef CHARSET_EBCDIC
74 	/* KLUDGE! We convert to ascii before writing DER */
75 	int len;
76 	char tmp[24];
77 	ASN1_STRING tmpstr = *(ASN1_STRING *)a;
78 
79 	len = tmpstr.length;
80 	ebcdic2ascii(tmp, tmpstr.data, (len >= sizeof tmp) ? sizeof tmp : len);
81 	tmpstr.data = tmp;
82 
83 	a = (ASN1_GENERALIZEDTIME *) &tmpstr;
84 #endif
85 	return(i2d_ASN1_bytes((ASN1_STRING *)a,pp,
86 		V_ASN1_GENERALIZEDTIME,V_ASN1_UNIVERSAL));
87 	}
88 
89 
90 ASN1_GENERALIZEDTIME *d2i_ASN1_GENERALIZEDTIME(ASN1_GENERALIZEDTIME **a,
91 	     unsigned char **pp, long length)
92 	{
93 	ASN1_GENERALIZEDTIME *ret=NULL;
94 
95 	ret=(ASN1_GENERALIZEDTIME *)d2i_ASN1_bytes((ASN1_STRING **)a,pp,length,
96 		V_ASN1_GENERALIZEDTIME,V_ASN1_UNIVERSAL);
97 	if (ret == NULL)
98 		{
99 		ASN1err(ASN1_F_D2I_ASN1_GENERALIZEDTIME,ERR_R_NESTED_ASN1_ERROR);
100 		return(NULL);
101 		}
102 #ifdef CHARSET_EBCDIC
103 	ascii2ebcdic(ret->data, ret->data, ret->length);
104 #endif
105 	if (!ASN1_GENERALIZEDTIME_check(ret))
106 		{
107 		ASN1err(ASN1_F_D2I_ASN1_GENERALIZEDTIME,ASN1_R_INVALID_TIME_FORMAT);
108 		goto err;
109 		}
110 
111 	return(ret);
112 err:
113 	if ((ret != NULL) && ((a == NULL) || (*a != ret)))
114 		M_ASN1_GENERALIZEDTIME_free(ret);
115 	return(NULL);
116 	}
117 
118 #endif
119 
ASN1_GENERALIZEDTIME_check(ASN1_GENERALIZEDTIME * d)120 int ASN1_GENERALIZEDTIME_check(ASN1_GENERALIZEDTIME *d)
121 	{
122 	static int min[9]={ 0, 0, 1, 1, 0, 0, 0, 0, 0};
123 	static int max[9]={99, 99,12,31,23,59,59,12,59};
124 	char *a;
125 	int n,i,l,o;
126 
127 	if (d->type != V_ASN1_GENERALIZEDTIME) return(0);
128 	l=d->length;
129 	a=(char *)d->data;
130 	o=0;
131 	/* GENERALIZEDTIME is similar to UTCTIME except the year is
132          * represented as YYYY. This stuff treats everything as a two digit
133          * field so make first two fields 00 to 99
134          */
135 	if (l < 13) goto err;
136 	for (i=0; i<7; i++)
137 		{
138 		if ((i == 6) && ((a[o] == 'Z') ||
139 			(a[o] == '+') || (a[o] == '-')))
140 			{ i++; break; }
141 		if ((a[o] < '0') || (a[o] > '9')) goto err;
142 		n= a[o]-'0';
143 		if (++o > l) goto err;
144 
145 		if ((a[o] < '0') || (a[o] > '9')) goto err;
146 		n=(n*10)+ a[o]-'0';
147 		if (++o > l) goto err;
148 
149 		if ((n < min[i]) || (n > max[i])) goto err;
150 		}
151 	/* Optional fractional seconds: decimal point followed by one
152 	 * or more digits.
153 	 */
154 	if (a[o] == '.')
155 		{
156 		if (++o > l) goto err;
157 		i = o;
158 		while ((a[o] >= '0') && (a[o] <= '9') && (o <= l))
159 			o++;
160 		/* Must have at least one digit after decimal point */
161 		if (i == o) goto err;
162 		}
163 
164 	if (a[o] == 'Z')
165 		o++;
166 	else if ((a[o] == '+') || (a[o] == '-'))
167 		{
168 		o++;
169 		if (o+4 > l) goto err;
170 		for (i=7; i<9; i++)
171 			{
172 			if ((a[o] < '0') || (a[o] > '9')) goto err;
173 			n= a[o]-'0';
174 			o++;
175 			if ((a[o] < '0') || (a[o] > '9')) goto err;
176 			n=(n*10)+ a[o]-'0';
177 			if ((n < min[i]) || (n > max[i])) goto err;
178 			o++;
179 			}
180 		}
181 	return(o == l);
182 err:
183 	return(0);
184 	}
185 
ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME * s,char * str)186 int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, char *str)
187 	{
188 	ASN1_GENERALIZEDTIME t;
189 
190 	t.type=V_ASN1_GENERALIZEDTIME;
191 	t.length=strlen(str);
192 	t.data=(unsigned char *)str;
193 	if (ASN1_GENERALIZEDTIME_check(&t))
194 		{
195 		if (s != NULL)
196 			{
197 			if (!ASN1_STRING_set((ASN1_STRING *)s,
198 				(unsigned char *)str,t.length))
199 				return 0;
200 			s->type=V_ASN1_GENERALIZEDTIME;
201 			}
202 		return(1);
203 		}
204 	else
205 		return(0);
206 	}
207 
ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME * s,time_t t)208 ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
209 	     time_t t)
210 	{
211 	char *p;
212 	struct tm *ts;
213 	struct tm data;
214 	size_t len = 20;
215 
216 	if (s == NULL)
217 		s=M_ASN1_GENERALIZEDTIME_new();
218 	if (s == NULL)
219 		return(NULL);
220 
221 	ts=OPENSSL_gmtime(&t, &data);
222 	if (ts == NULL)
223 		return(NULL);
224 
225 	p=(char *)s->data;
226 	if ((p == NULL) || ((size_t)s->length < len))
227 		{
228 		p=OPENSSL_malloc(len);
229 		if (p == NULL)
230 			{
231 			ASN1err(ASN1_F_ASN1_GENERALIZEDTIME_SET,
232 				ERR_R_MALLOC_FAILURE);
233 			return(NULL);
234 			}
235 		if (s->data != NULL)
236 			OPENSSL_free(s->data);
237 		s->data=(unsigned char *)p;
238 		}
239 
240 	BIO_snprintf(p,len,"%04lld%02d%02d%02d%02d%02dZ",
241 		     (int64_t)(ts->tm_year + 1900),
242 		     ts->tm_mon+1,ts->tm_mday,ts->tm_hour,ts->tm_min,ts->tm_sec);
243 	s->length=strlen(p);
244 	s->type=V_ASN1_GENERALIZEDTIME;
245 #ifdef CHARSET_EBCDIC_not
246 	ebcdic2ascii(s->data, s->data, s->length);
247 #endif
248 	return(s);
249 	}
250