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