• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56 
57 #include <openssl/asn1.h>
58 
59 #include <limits.h>
60 #include <string.h>
61 
62 #include <openssl/asn1_mac.h>
63 #include <openssl/err.h>
64 #include <openssl/mem.h>
65 
66 
67 /* Cross-module errors from crypto/x509/i2d_pr.c */
68 OPENSSL_DECLARE_ERROR_REASON(ASN1, UNSUPPORTED_PUBLIC_KEY_TYPE);
69 
70 /* Cross-module errors from crypto/x509/asn1_gen.c.
71  * TODO(davidben): Remove these once asn1_gen.c is gone. */
72 OPENSSL_DECLARE_ERROR_REASON(ASN1, DEPTH_EXCEEDED);
73 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_BITSTRING_FORMAT);
74 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_BOOLEAN);
75 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_FORMAT);
76 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_HEX);
77 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_IMPLICIT_TAG);
78 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_INTEGER);
79 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_NESTED_TAGGING);
80 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_NULL_VALUE);
81 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_OBJECT);
82 OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_TIME_VALUE);
83 OPENSSL_DECLARE_ERROR_REASON(ASN1, INTEGER_NOT_ASCII_FORMAT);
84 OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_MODIFIER);
85 OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_NUMBER);
86 OPENSSL_DECLARE_ERROR_REASON(ASN1, LIST_ERROR);
87 OPENSSL_DECLARE_ERROR_REASON(ASN1, MISSING_VALUE);
88 OPENSSL_DECLARE_ERROR_REASON(ASN1, NOT_ASCII_FORMAT);
89 OPENSSL_DECLARE_ERROR_REASON(ASN1, OBJECT_NOT_ASCII_FORMAT);
90 OPENSSL_DECLARE_ERROR_REASON(ASN1, SEQUENCE_OR_SET_NEEDS_CONFIG);
91 OPENSSL_DECLARE_ERROR_REASON(ASN1, TIME_NOT_ASCII_FORMAT);
92 OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_FORMAT);
93 OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_TAG);
94 OPENSSL_DECLARE_ERROR_REASON(ASN1, UNSUPPORTED_TYPE);
95 
96 static int asn1_get_length(const unsigned char **pp,int *inf,long *rl,int max);
97 static void asn1_put_length(unsigned char **pp, int length);
98 
_asn1_check_infinite_end(const unsigned char ** p,long len)99 static int _asn1_check_infinite_end(const unsigned char **p, long len)
100 	{
101 	/* If there is 0 or 1 byte left, the length check should pick
102 	 * things up */
103 	if (len <= 0)
104 		return(1);
105 	else if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0))
106 		{
107 		(*p)+=2;
108 		return(1);
109 		}
110 	return(0);
111 	}
112 
ASN1_check_infinite_end(unsigned char ** p,long len)113 int ASN1_check_infinite_end(unsigned char **p, long len)
114 	{
115 	return _asn1_check_infinite_end((const unsigned char **)p, len);
116 	}
117 
ASN1_const_check_infinite_end(const unsigned char ** p,long len)118 int ASN1_const_check_infinite_end(const unsigned char **p, long len)
119 	{
120 	return _asn1_check_infinite_end(p, len);
121 	}
122 
123 
ASN1_get_object(const unsigned char ** pp,long * plength,int * ptag,int * pclass,long omax)124 int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
125 	int *pclass, long omax)
126 	{
127 	int i,ret;
128 	long l;
129 	const unsigned char *p= *pp;
130 	int tag,xclass,inf;
131 	long max=omax;
132 
133 	if (!max) goto err;
134 	ret=(*p&V_ASN1_CONSTRUCTED);
135 	xclass=(*p&V_ASN1_PRIVATE);
136 	i= *p&V_ASN1_PRIMITIVE_TAG;
137 	if (i == V_ASN1_PRIMITIVE_TAG)
138 		{		/* high-tag */
139 		p++;
140 		if (--max == 0) goto err;
141 		l=0;
142 		while (*p&0x80)
143 			{
144 			l<<=7L;
145 			l|= *(p++)&0x7f;
146 			if (--max == 0) goto err;
147 			if (l > (INT_MAX >> 7L)) goto err;
148 			}
149 		l<<=7L;
150 		l|= *(p++)&0x7f;
151 		tag=(int)l;
152 		if (--max == 0) goto err;
153 		}
154 	else
155 		{
156 		tag=i;
157 		p++;
158 		if (--max == 0) goto err;
159 		}
160 	*ptag=tag;
161 	*pclass=xclass;
162 	if (!asn1_get_length(&p,&inf,plength,(int)max)) goto err;
163 
164 	if (inf && !(ret & V_ASN1_CONSTRUCTED))
165 		goto err;
166 
167 #if 0
168 	fprintf(stderr,"p=%d + *plength=%ld > omax=%ld + *pp=%d  (%d > %d)\n",
169 		(int)p,*plength,omax,(int)*pp,(int)(p+ *plength),
170 		(int)(omax+ *pp));
171 
172 #endif
173 	if (*plength > (omax - (p - *pp)))
174 		{
175 		OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG);
176 		/* Set this so that even if things are not long enough
177 		 * the values are set correctly */
178 		ret|=0x80;
179 		}
180 	*pp=p;
181 	return(ret|inf);
182 err:
183 	OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG);
184 	return(0x80);
185 	}
186 
asn1_get_length(const unsigned char ** pp,int * inf,long * rl,int max)187 static int asn1_get_length(const unsigned char **pp, int *inf, long *rl, int max)
188 	{
189 	const unsigned char *p= *pp;
190 	unsigned long ret=0;
191 	unsigned int i;
192 
193 	if (max-- < 1) return(0);
194 	if (*p == 0x80)
195 		{
196 		*inf=1;
197 		ret=0;
198 		p++;
199 		}
200 	else
201 		{
202 		*inf=0;
203 		i= *p&0x7f;
204 		if (*(p++) & 0x80)
205 			{
206 			if (i > sizeof(long))
207 				return 0;
208 			if (max-- == 0) return(0);
209 			while (i-- > 0)
210 				{
211 				ret<<=8L;
212 				ret|= *(p++);
213 				if (max-- == 0) return(0);
214 				}
215 			}
216 		else
217 			ret=i;
218 		}
219 	if (ret > LONG_MAX)
220 		return 0;
221 	*pp=p;
222 	*rl=(long)ret;
223 	return(1);
224 	}
225 
226 /* class 0 is constructed
227  * constructed == 2 for indefinite length constructed */
ASN1_put_object(unsigned char ** pp,int constructed,int length,int tag,int xclass)228 void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
229 	     int xclass)
230 	{
231 	unsigned char *p= *pp;
232 	int i, ttag;
233 
234 	i=(constructed)?V_ASN1_CONSTRUCTED:0;
235 	i|=(xclass&V_ASN1_PRIVATE);
236 	if (tag < 31)
237 		*(p++)=i|(tag&V_ASN1_PRIMITIVE_TAG);
238 	else
239 		{
240 		*(p++)=i|V_ASN1_PRIMITIVE_TAG;
241 		for(i = 0, ttag = tag; ttag > 0; i++) ttag >>=7;
242 		ttag = i;
243 		while(i-- > 0)
244 			{
245 			p[i] = tag & 0x7f;
246 			if(i != (ttag - 1)) p[i] |= 0x80;
247 			tag >>= 7;
248 			}
249 		p += ttag;
250 		}
251 	if (constructed == 2)
252 		*(p++)=0x80;
253 	else
254 		asn1_put_length(&p,length);
255 	*pp=p;
256 	}
257 
ASN1_put_eoc(unsigned char ** pp)258 int ASN1_put_eoc(unsigned char **pp)
259 	{
260 	unsigned char *p = *pp;
261 	*p++ = 0;
262 	*p++ = 0;
263 	*pp = p;
264 	return 2;
265 	}
266 
asn1_put_length(unsigned char ** pp,int length)267 static void asn1_put_length(unsigned char **pp, int length)
268 	{
269 	unsigned char *p= *pp;
270 	int i,l;
271 	if (length <= 127)
272 		*(p++)=(unsigned char)length;
273 	else
274 		{
275 		l=length;
276 		for (i=0; l > 0; i++)
277 			l>>=8;
278 		*(p++)=i|0x80;
279 		l=i;
280 		while (i-- > 0)
281 			{
282 			p[i]=length&0xff;
283 			length>>=8;
284 			}
285 		p+=l;
286 		}
287 	*pp=p;
288 	}
289 
ASN1_object_size(int constructed,int length,int tag)290 int ASN1_object_size(int constructed, int length, int tag)
291 	{
292 	int ret;
293 
294 	ret=length;
295 	ret++;
296 	if (tag >= 31)
297 		{
298 		while (tag > 0)
299 			{
300 			tag>>=7;
301 			ret++;
302 			}
303 		}
304 	if (constructed == 2)
305 		return ret + 3;
306 	ret++;
307 	if (length > 127)
308 		{
309 		while (length > 0)
310 			{
311 			length>>=8;
312 			ret++;
313 			}
314 		}
315 	return(ret);
316 	}
317 
_asn1_Finish(ASN1_const_CTX * c)318 static int _asn1_Finish(ASN1_const_CTX *c)
319 	{
320 	if ((c->inf == (1|V_ASN1_CONSTRUCTED)) && (!c->eos))
321 		{
322 		if (!ASN1_const_check_infinite_end(&c->p,c->slen))
323 			{
324 			c->error=ASN1_R_MISSING_ASN1_EOS;
325 			return(0);
326 			}
327 		}
328 	if (	((c->slen != 0) && !(c->inf & 1)) ||
329 		((c->slen < 0) && (c->inf & 1)))
330 		{
331 		c->error=ASN1_R_ASN1_LENGTH_MISMATCH;
332 		return(0);
333 		}
334 	return(1);
335 	}
336 
asn1_Finish(ASN1_CTX * c)337 int asn1_Finish(ASN1_CTX *c)
338 	{
339 	return _asn1_Finish((ASN1_const_CTX *)c);
340 	}
341 
asn1_const_Finish(ASN1_const_CTX * c)342 int asn1_const_Finish(ASN1_const_CTX *c)
343 	{
344 	return _asn1_Finish(c);
345 	}
346 
asn1_GetSequence(ASN1_const_CTX * c,long * length)347 int asn1_GetSequence(ASN1_const_CTX *c, long *length)
348 	{
349 	const unsigned char *q;
350 
351 	q=c->p;
352 	c->inf=ASN1_get_object(&(c->p),&(c->slen),&(c->tag),&(c->xclass),
353 		*length);
354 	if (c->inf & 0x80)
355 		{
356 		c->error=ASN1_R_BAD_GET_ASN1_OBJECT_CALL;
357 		return(0);
358 		}
359 	if (c->tag != V_ASN1_SEQUENCE)
360 		{
361 		c->error=ASN1_R_EXPECTING_AN_ASN1_SEQUENCE;
362 		return(0);
363 		}
364 	(*length)-=(c->p-q);
365 	if (c->max && (*length < 0))
366 		{
367 		c->error=ASN1_R_ASN1_LENGTH_MISMATCH;
368 		return(0);
369 		}
370 	if (c->inf == (1|V_ASN1_CONSTRUCTED))
371 		c->slen= *length+ *(c->pp)-c->p;
372 	c->eos=0;
373 	return(1);
374 	}
375 
ASN1_STRING_copy(ASN1_STRING * dst,const ASN1_STRING * str)376 int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
377 	{
378 	if (str == NULL)
379 		return 0;
380 	dst->type = str->type;
381 	if (!ASN1_STRING_set(dst,str->data,str->length))
382 		return 0;
383 	dst->flags = str->flags;
384 	return 1;
385 	}
386 
ASN1_STRING_dup(const ASN1_STRING * str)387 ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
388 	{
389 	ASN1_STRING *ret;
390 	if (!str)
391 		 return NULL;
392 	ret=ASN1_STRING_new();
393 	if (!ret)
394 		return NULL;
395 	if (!ASN1_STRING_copy(ret,str))
396 		{
397 		ASN1_STRING_free(ret);
398 		return NULL;
399 		}
400 	return ret;
401 	}
402 
ASN1_STRING_set(ASN1_STRING * str,const void * _data,int len)403 int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
404 	{
405 	unsigned char *c;
406 	const char *data=_data;
407 
408 	if (len < 0)
409 		{
410 		if (data == NULL)
411 			return(0);
412 		else
413 			len=strlen(data);
414 		}
415 	if ((str->length < len) || (str->data == NULL))
416 		{
417 		c=str->data;
418 		if (c == NULL)
419 			str->data=OPENSSL_malloc(len+1);
420 		else
421 			str->data=OPENSSL_realloc(c,len+1);
422 
423 		if (str->data == NULL)
424 			{
425 			OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
426 			str->data=c;
427 			return(0);
428 			}
429 		}
430 	str->length=len;
431 	if (data != NULL)
432 		{
433 		memcpy(str->data,data,len);
434 		/* an allowance for strings :-) */
435 		str->data[len]='\0';
436 		}
437 	return(1);
438 	}
439 
ASN1_STRING_set0(ASN1_STRING * str,void * data,int len)440 void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
441 	{
442 	if (str->data)
443 		OPENSSL_free(str->data);
444 	str->data = data;
445 	str->length = len;
446 	}
447 
ASN1_STRING_new(void)448 ASN1_STRING *ASN1_STRING_new(void)
449 	{
450 	return(ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
451 	}
452 
453 
ASN1_STRING_type_new(int type)454 ASN1_STRING *ASN1_STRING_type_new(int type)
455 	{
456 	ASN1_STRING *ret;
457 
458 	ret=(ASN1_STRING *)OPENSSL_malloc(sizeof(ASN1_STRING));
459 	if (ret == NULL)
460 		{
461 		OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
462 		return(NULL);
463 		}
464 	ret->length=0;
465 	ret->type=type;
466 	ret->data=NULL;
467 	ret->flags=0;
468 	return(ret);
469 	}
470 
ASN1_STRING_free(ASN1_STRING * a)471 void ASN1_STRING_free(ASN1_STRING *a)
472 	{
473 	if (a == NULL) return;
474 	if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
475 		OPENSSL_free(a->data);
476 	OPENSSL_free(a);
477 	}
478 
ASN1_STRING_cmp(const ASN1_STRING * a,const ASN1_STRING * b)479 int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
480 	{
481 	int i;
482 
483 	i=(a->length-b->length);
484 	if (i == 0)
485 		{
486 		i=memcmp(a->data,b->data,a->length);
487 		if (i == 0)
488 			return(a->type-b->type);
489 		else
490 			return(i);
491 		}
492 	else
493 		return(i);
494 	}
495 
ASN1_STRING_length(const ASN1_STRING * x)496 int ASN1_STRING_length(const ASN1_STRING *x)
497 { return M_ASN1_STRING_length(x); }
498 
ASN1_STRING_length_set(ASN1_STRING * x,int len)499 void ASN1_STRING_length_set(ASN1_STRING *x, int len)
500 { M_ASN1_STRING_length_set(x, len); return; }
501 
ASN1_STRING_type(ASN1_STRING * x)502 int ASN1_STRING_type(ASN1_STRING *x)
503 { return M_ASN1_STRING_type(x); }
504 
ASN1_STRING_data(ASN1_STRING * x)505 unsigned char * ASN1_STRING_data(ASN1_STRING *x)
506 { return M_ASN1_STRING_data(x); }
507