xref: /onnv-gate/usr/src/common/openssl/crypto/asn1/a_bytes.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/asn1/a_bytes.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include "cryptlib.h"
610Sstevel@tonic-gate #include <openssl/asn1.h>
620Sstevel@tonic-gate 
63*2139Sjp161948 static int asn1_collate_primitive(ASN1_STRING *a, ASN1_const_CTX *c);
640Sstevel@tonic-gate /* type is a 'bitmap' of acceptable string types.
650Sstevel@tonic-gate  */
d2i_ASN1_type_bytes(ASN1_STRING ** a,const unsigned char ** pp,long length,int type)66*2139Sjp161948 ASN1_STRING *d2i_ASN1_type_bytes(ASN1_STRING **a, const unsigned char **pp,
670Sstevel@tonic-gate 	     long length, int type)
680Sstevel@tonic-gate 	{
690Sstevel@tonic-gate 	ASN1_STRING *ret=NULL;
70*2139Sjp161948 	const unsigned char *p;
71*2139Sjp161948 	unsigned char *s;
720Sstevel@tonic-gate 	long len;
730Sstevel@tonic-gate 	int inf,tag,xclass;
740Sstevel@tonic-gate 	int i=0;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	p= *pp;
770Sstevel@tonic-gate 	inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
780Sstevel@tonic-gate 	if (inf & 0x80) goto err;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	if (tag >= 32)
810Sstevel@tonic-gate 		{
820Sstevel@tonic-gate 		i=ASN1_R_TAG_VALUE_TOO_HIGH;;
830Sstevel@tonic-gate 		goto err;
840Sstevel@tonic-gate 		}
850Sstevel@tonic-gate 	if (!(ASN1_tag2bit(tag) & type))
860Sstevel@tonic-gate 		{
870Sstevel@tonic-gate 		i=ASN1_R_WRONG_TYPE;
880Sstevel@tonic-gate 		goto err;
890Sstevel@tonic-gate 		}
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	/* If a bit-string, exit early */
920Sstevel@tonic-gate 	if (tag == V_ASN1_BIT_STRING)
930Sstevel@tonic-gate 		return(d2i_ASN1_BIT_STRING(a,pp,length));
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	if ((a == NULL) || ((*a) == NULL))
960Sstevel@tonic-gate 		{
970Sstevel@tonic-gate 		if ((ret=ASN1_STRING_new()) == NULL) return(NULL);
980Sstevel@tonic-gate 		}
990Sstevel@tonic-gate 	else
1000Sstevel@tonic-gate 		ret=(*a);
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	if (len != 0)
1030Sstevel@tonic-gate 		{
1040Sstevel@tonic-gate 		s=(unsigned char *)OPENSSL_malloc((int)len+1);
1050Sstevel@tonic-gate 		if (s == NULL)
1060Sstevel@tonic-gate 			{
1070Sstevel@tonic-gate 			i=ERR_R_MALLOC_FAILURE;
1080Sstevel@tonic-gate 			goto err;
1090Sstevel@tonic-gate 			}
1100Sstevel@tonic-gate 		memcpy(s,p,(int)len);
1110Sstevel@tonic-gate 		s[len]='\0';
1120Sstevel@tonic-gate 		p+=len;
1130Sstevel@tonic-gate 		}
1140Sstevel@tonic-gate 	else
1150Sstevel@tonic-gate 		s=NULL;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	if (ret->data != NULL) OPENSSL_free(ret->data);
1180Sstevel@tonic-gate 	ret->length=(int)len;
1190Sstevel@tonic-gate 	ret->data=s;
1200Sstevel@tonic-gate 	ret->type=tag;
1210Sstevel@tonic-gate 	if (a != NULL) (*a)=ret;
1220Sstevel@tonic-gate 	*pp=p;
1230Sstevel@tonic-gate 	return(ret);
1240Sstevel@tonic-gate err:
1250Sstevel@tonic-gate 	ASN1err(ASN1_F_D2I_ASN1_TYPE_BYTES,i);
1260Sstevel@tonic-gate 	if ((ret != NULL) && ((a == NULL) || (*a != ret)))
1270Sstevel@tonic-gate 		ASN1_STRING_free(ret);
1280Sstevel@tonic-gate 	return(NULL);
1290Sstevel@tonic-gate 	}
1300Sstevel@tonic-gate 
i2d_ASN1_bytes(ASN1_STRING * a,unsigned char ** pp,int tag,int xclass)1310Sstevel@tonic-gate int i2d_ASN1_bytes(ASN1_STRING *a, unsigned char **pp, int tag, int xclass)
1320Sstevel@tonic-gate 	{
1330Sstevel@tonic-gate 	int ret,r,constructed;
1340Sstevel@tonic-gate 	unsigned char *p;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	if (a == NULL)  return(0);
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	if (tag == V_ASN1_BIT_STRING)
1390Sstevel@tonic-gate 		return(i2d_ASN1_BIT_STRING(a,pp));
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	ret=a->length;
1420Sstevel@tonic-gate 	r=ASN1_object_size(0,ret,tag);
1430Sstevel@tonic-gate 	if (pp == NULL) return(r);
1440Sstevel@tonic-gate 	p= *pp;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	if ((tag == V_ASN1_SEQUENCE) || (tag == V_ASN1_SET))
1470Sstevel@tonic-gate 		constructed=1;
1480Sstevel@tonic-gate 	else
1490Sstevel@tonic-gate 		constructed=0;
1500Sstevel@tonic-gate 	ASN1_put_object(&p,constructed,ret,tag,xclass);
1510Sstevel@tonic-gate 	memcpy(p,a->data,a->length);
1520Sstevel@tonic-gate 	p+=a->length;
1530Sstevel@tonic-gate 	*pp= p;
1540Sstevel@tonic-gate 	return(r);
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate 
d2i_ASN1_bytes(ASN1_STRING ** a,const unsigned char ** pp,long length,int Ptag,int Pclass)157*2139Sjp161948 ASN1_STRING *d2i_ASN1_bytes(ASN1_STRING **a, const unsigned char **pp,
158*2139Sjp161948 	     long length, int Ptag, int Pclass)
1590Sstevel@tonic-gate 	{
1600Sstevel@tonic-gate 	ASN1_STRING *ret=NULL;
161*2139Sjp161948 	const unsigned char *p;
162*2139Sjp161948 	unsigned char *s;
1630Sstevel@tonic-gate 	long len;
1640Sstevel@tonic-gate 	int inf,tag,xclass;
1650Sstevel@tonic-gate 	int i=0;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	if ((a == NULL) || ((*a) == NULL))
1680Sstevel@tonic-gate 		{
1690Sstevel@tonic-gate 		if ((ret=ASN1_STRING_new()) == NULL) return(NULL);
1700Sstevel@tonic-gate 		}
1710Sstevel@tonic-gate 	else
1720Sstevel@tonic-gate 		ret=(*a);
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	p= *pp;
1750Sstevel@tonic-gate 	inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
1760Sstevel@tonic-gate 	if (inf & 0x80)
1770Sstevel@tonic-gate 		{
1780Sstevel@tonic-gate 		i=ASN1_R_BAD_OBJECT_HEADER;
1790Sstevel@tonic-gate 		goto err;
1800Sstevel@tonic-gate 		}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	if (tag != Ptag)
1830Sstevel@tonic-gate 		{
1840Sstevel@tonic-gate 		i=ASN1_R_WRONG_TAG;
1850Sstevel@tonic-gate 		goto err;
1860Sstevel@tonic-gate 		}
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	if (inf & V_ASN1_CONSTRUCTED)
1890Sstevel@tonic-gate 		{
190*2139Sjp161948 		ASN1_const_CTX c;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 		c.pp=pp;
1930Sstevel@tonic-gate 		c.p=p;
1940Sstevel@tonic-gate 		c.inf=inf;
1950Sstevel@tonic-gate 		c.slen=len;
1960Sstevel@tonic-gate 		c.tag=Ptag;
1970Sstevel@tonic-gate 		c.xclass=Pclass;
1980Sstevel@tonic-gate 		c.max=(length == 0)?0:(p+length);
1990Sstevel@tonic-gate 		if (!asn1_collate_primitive(ret,&c))
2000Sstevel@tonic-gate 			goto err;
2010Sstevel@tonic-gate 		else
2020Sstevel@tonic-gate 			{
2030Sstevel@tonic-gate 			p=c.p;
2040Sstevel@tonic-gate 			}
2050Sstevel@tonic-gate 		}
2060Sstevel@tonic-gate 	else
2070Sstevel@tonic-gate 		{
2080Sstevel@tonic-gate 		if (len != 0)
2090Sstevel@tonic-gate 			{
2100Sstevel@tonic-gate 			if ((ret->length < len) || (ret->data == NULL))
2110Sstevel@tonic-gate 				{
2120Sstevel@tonic-gate 				if (ret->data != NULL) OPENSSL_free(ret->data);
2130Sstevel@tonic-gate 				s=(unsigned char *)OPENSSL_malloc((int)len + 1);
2140Sstevel@tonic-gate 				if (s == NULL)
2150Sstevel@tonic-gate 					{
2160Sstevel@tonic-gate 					i=ERR_R_MALLOC_FAILURE;
2170Sstevel@tonic-gate 					goto err;
2180Sstevel@tonic-gate 					}
2190Sstevel@tonic-gate 				}
2200Sstevel@tonic-gate 			else
2210Sstevel@tonic-gate 				s=ret->data;
2220Sstevel@tonic-gate 			memcpy(s,p,(int)len);
2230Sstevel@tonic-gate 			s[len] = '\0';
2240Sstevel@tonic-gate 			p+=len;
2250Sstevel@tonic-gate 			}
2260Sstevel@tonic-gate 		else
2270Sstevel@tonic-gate 			{
2280Sstevel@tonic-gate 			s=NULL;
2290Sstevel@tonic-gate 			if (ret->data != NULL) OPENSSL_free(ret->data);
2300Sstevel@tonic-gate 			}
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 		ret->length=(int)len;
2330Sstevel@tonic-gate 		ret->data=s;
2340Sstevel@tonic-gate 		ret->type=Ptag;
2350Sstevel@tonic-gate 		}
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	if (a != NULL) (*a)=ret;
2380Sstevel@tonic-gate 	*pp=p;
2390Sstevel@tonic-gate 	return(ret);
2400Sstevel@tonic-gate err:
2410Sstevel@tonic-gate 	if ((ret != NULL) && ((a == NULL) || (*a != ret)))
2420Sstevel@tonic-gate 		ASN1_STRING_free(ret);
2430Sstevel@tonic-gate 	ASN1err(ASN1_F_D2I_ASN1_BYTES,i);
2440Sstevel@tonic-gate 	return(NULL);
2450Sstevel@tonic-gate 	}
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate /* We are about to parse 0..n d2i_ASN1_bytes objects, we are to collapse
2490Sstevel@tonic-gate  * them into the one structure that is then returned */
2500Sstevel@tonic-gate /* There have been a few bug fixes for this function from
2510Sstevel@tonic-gate  * Paul Keogh <paul.keogh@sse.ie>, many thanks to him */
asn1_collate_primitive(ASN1_STRING * a,ASN1_const_CTX * c)252*2139Sjp161948 static int asn1_collate_primitive(ASN1_STRING *a, ASN1_const_CTX *c)
2530Sstevel@tonic-gate 	{
2540Sstevel@tonic-gate 	ASN1_STRING *os=NULL;
2550Sstevel@tonic-gate 	BUF_MEM b;
2560Sstevel@tonic-gate 	int num;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	b.length=0;
2590Sstevel@tonic-gate 	b.max=0;
2600Sstevel@tonic-gate 	b.data=NULL;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	if (a == NULL)
2630Sstevel@tonic-gate 		{
2640Sstevel@tonic-gate 		c->error=ERR_R_PASSED_NULL_PARAMETER;
2650Sstevel@tonic-gate 		goto err;
2660Sstevel@tonic-gate 		}
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	num=0;
2690Sstevel@tonic-gate 	for (;;)
2700Sstevel@tonic-gate 		{
2710Sstevel@tonic-gate 		if (c->inf & 1)
2720Sstevel@tonic-gate 			{
273*2139Sjp161948 			c->eos=ASN1_const_check_infinite_end(&c->p,
2740Sstevel@tonic-gate 				(long)(c->max-c->p));
2750Sstevel@tonic-gate 			if (c->eos) break;
2760Sstevel@tonic-gate 			}
2770Sstevel@tonic-gate 		else
2780Sstevel@tonic-gate 			{
2790Sstevel@tonic-gate 			if (c->slen <= 0) break;
2800Sstevel@tonic-gate 			}
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 		c->q=c->p;
2830Sstevel@tonic-gate 		if (d2i_ASN1_bytes(&os,&c->p,c->max-c->p,c->tag,c->xclass)
2840Sstevel@tonic-gate 			== NULL)
2850Sstevel@tonic-gate 			{
2860Sstevel@tonic-gate 			c->error=ERR_R_ASN1_LIB;
2870Sstevel@tonic-gate 			goto err;
2880Sstevel@tonic-gate 			}
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 		if (!BUF_MEM_grow_clean(&b,num+os->length))
2910Sstevel@tonic-gate 			{
2920Sstevel@tonic-gate 			c->error=ERR_R_BUF_LIB;
2930Sstevel@tonic-gate 			goto err;
2940Sstevel@tonic-gate 			}
2950Sstevel@tonic-gate 		memcpy(&(b.data[num]),os->data,os->length);
2960Sstevel@tonic-gate 		if (!(c->inf & 1))
2970Sstevel@tonic-gate 			c->slen-=(c->p-c->q);
2980Sstevel@tonic-gate 		num+=os->length;
2990Sstevel@tonic-gate 		}
3000Sstevel@tonic-gate 
301*2139Sjp161948 	if (!asn1_const_Finish(c)) goto err;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	a->length=num;
3040Sstevel@tonic-gate 	if (a->data != NULL) OPENSSL_free(a->data);
3050Sstevel@tonic-gate 	a->data=(unsigned char *)b.data;
3060Sstevel@tonic-gate 	if (os != NULL) ASN1_STRING_free(os);
3070Sstevel@tonic-gate 	return(1);
3080Sstevel@tonic-gate err:
3090Sstevel@tonic-gate 	ASN1err(ASN1_F_ASN1_COLLATE_PRIMITIVE,c->error);
3100Sstevel@tonic-gate 	if (os != NULL) ASN1_STRING_free(os);
3110Sstevel@tonic-gate 	if (b.data != NULL) OPENSSL_free(b.data);
3120Sstevel@tonic-gate 	return(0);
3130Sstevel@tonic-gate 	}
3140Sstevel@tonic-gate 
315