155714Skris/* crypto/asn1/a_bytes.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#include <stdio.h>
6055714Skris#include "cryptlib.h"
61109998Smarkm#include <openssl/asn1.h>
6255714Skris
63160814Ssimonstatic int asn1_collate_primitive(ASN1_STRING *a, ASN1_const_CTX *c);
6455714Skris/* type is a 'bitmap' of acceptable string types.
6555714Skris */
66160814SsimonASN1_STRING *d2i_ASN1_type_bytes(ASN1_STRING **a, const unsigned char **pp,
6755714Skris	     long length, int type)
6855714Skris	{
6955714Skris	ASN1_STRING *ret=NULL;
70160814Ssimon	const unsigned char *p;
71160814Ssimon	unsigned char *s;
7255714Skris	long len;
7355714Skris	int inf,tag,xclass;
7455714Skris	int i=0;
7555714Skris
7655714Skris	p= *pp;
7755714Skris	inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
7855714Skris	if (inf & 0x80) goto err;
7955714Skris
8055714Skris	if (tag >= 32)
8155714Skris		{
82194206Ssimon		i=ASN1_R_TAG_VALUE_TOO_HIGH;
8355714Skris		goto err;
8455714Skris		}
85109998Smarkm	if (!(ASN1_tag2bit(tag) & type))
8655714Skris		{
8755714Skris		i=ASN1_R_WRONG_TYPE;
8855714Skris		goto err;
8955714Skris		}
9055714Skris
9155714Skris	/* If a bit-string, exit early */
9255714Skris	if (tag == V_ASN1_BIT_STRING)
9355714Skris		return(d2i_ASN1_BIT_STRING(a,pp,length));
9455714Skris
9555714Skris	if ((a == NULL) || ((*a) == NULL))
9655714Skris		{
9755714Skris		if ((ret=ASN1_STRING_new()) == NULL) return(NULL);
9855714Skris		}
9955714Skris	else
10055714Skris		ret=(*a);
10155714Skris
10255714Skris	if (len != 0)
10355714Skris		{
10468651Skris		s=(unsigned char *)OPENSSL_malloc((int)len+1);
10555714Skris		if (s == NULL)
10655714Skris			{
10755714Skris			i=ERR_R_MALLOC_FAILURE;
10855714Skris			goto err;
10955714Skris			}
11055714Skris		memcpy(s,p,(int)len);
11155714Skris		s[len]='\0';
11255714Skris		p+=len;
11355714Skris		}
11455714Skris	else
11555714Skris		s=NULL;
11655714Skris
11768651Skris	if (ret->data != NULL) OPENSSL_free(ret->data);
11855714Skris	ret->length=(int)len;
11955714Skris	ret->data=s;
12055714Skris	ret->type=tag;
12155714Skris	if (a != NULL) (*a)=ret;
12255714Skris	*pp=p;
12355714Skris	return(ret);
12455714Skriserr:
12555714Skris	ASN1err(ASN1_F_D2I_ASN1_TYPE_BYTES,i);
12655714Skris	if ((ret != NULL) && ((a == NULL) || (*a != ret)))
12755714Skris		ASN1_STRING_free(ret);
12855714Skris	return(NULL);
12955714Skris	}
13055714Skris
13155714Skrisint i2d_ASN1_bytes(ASN1_STRING *a, unsigned char **pp, int tag, int xclass)
13255714Skris	{
13355714Skris	int ret,r,constructed;
13455714Skris	unsigned char *p;
13555714Skris
13655714Skris	if (a == NULL)  return(0);
13755714Skris
13855714Skris	if (tag == V_ASN1_BIT_STRING)
13955714Skris		return(i2d_ASN1_BIT_STRING(a,pp));
14055714Skris
14155714Skris	ret=a->length;
14255714Skris	r=ASN1_object_size(0,ret,tag);
14355714Skris	if (pp == NULL) return(r);
14455714Skris	p= *pp;
14555714Skris
14655714Skris	if ((tag == V_ASN1_SEQUENCE) || (tag == V_ASN1_SET))
14755714Skris		constructed=1;
14855714Skris	else
14955714Skris		constructed=0;
15055714Skris	ASN1_put_object(&p,constructed,ret,tag,xclass);
15155714Skris	memcpy(p,a->data,a->length);
15255714Skris	p+=a->length;
15355714Skris	*pp= p;
15455714Skris	return(r);
15555714Skris	}
15655714Skris
157160814SsimonASN1_STRING *d2i_ASN1_bytes(ASN1_STRING **a, const unsigned char **pp,
158160814Ssimon	     long length, int Ptag, int Pclass)
15955714Skris	{
16055714Skris	ASN1_STRING *ret=NULL;
161160814Ssimon	const unsigned char *p;
162160814Ssimon	unsigned char *s;
16355714Skris	long len;
16455714Skris	int inf,tag,xclass;
16555714Skris	int i=0;
16655714Skris
16755714Skris	if ((a == NULL) || ((*a) == NULL))
16855714Skris		{
16955714Skris		if ((ret=ASN1_STRING_new()) == NULL) return(NULL);
17055714Skris		}
17155714Skris	else
17255714Skris		ret=(*a);
17355714Skris
17455714Skris	p= *pp;
17555714Skris	inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
17655714Skris	if (inf & 0x80)
17755714Skris		{
17855714Skris		i=ASN1_R_BAD_OBJECT_HEADER;
17955714Skris		goto err;
18055714Skris		}
18155714Skris
18255714Skris	if (tag != Ptag)
18355714Skris		{
18455714Skris		i=ASN1_R_WRONG_TAG;
18555714Skris		goto err;
18655714Skris		}
18755714Skris
18855714Skris	if (inf & V_ASN1_CONSTRUCTED)
18955714Skris		{
190160814Ssimon		ASN1_const_CTX c;
19155714Skris
19255714Skris		c.pp=pp;
19355714Skris		c.p=p;
19455714Skris		c.inf=inf;
19555714Skris		c.slen=len;
19655714Skris		c.tag=Ptag;
19755714Skris		c.xclass=Pclass;
19855714Skris		c.max=(length == 0)?0:(p+length);
19959191Skris		if (!asn1_collate_primitive(ret,&c))
20055714Skris			goto err;
20155714Skris		else
20255714Skris			{
20355714Skris			p=c.p;
20455714Skris			}
20555714Skris		}
20655714Skris	else
20755714Skris		{
20855714Skris		if (len != 0)
20955714Skris			{
21055714Skris			if ((ret->length < len) || (ret->data == NULL))
21155714Skris				{
21268651Skris				if (ret->data != NULL) OPENSSL_free(ret->data);
21368651Skris				s=(unsigned char *)OPENSSL_malloc((int)len + 1);
21455714Skris				if (s == NULL)
21555714Skris					{
21655714Skris					i=ERR_R_MALLOC_FAILURE;
21755714Skris					goto err;
21855714Skris					}
21955714Skris				}
22055714Skris			else
22155714Skris				s=ret->data;
22255714Skris			memcpy(s,p,(int)len);
22359191Skris			s[len] = '\0';
22455714Skris			p+=len;
22555714Skris			}
22655714Skris		else
22755714Skris			{
22855714Skris			s=NULL;
22968651Skris			if (ret->data != NULL) OPENSSL_free(ret->data);
23055714Skris			}
23155714Skris
23255714Skris		ret->length=(int)len;
23355714Skris		ret->data=s;
23455714Skris		ret->type=Ptag;
23555714Skris		}
23655714Skris
23755714Skris	if (a != NULL) (*a)=ret;
23855714Skris	*pp=p;
23955714Skris	return(ret);
24055714Skriserr:
24155714Skris	if ((ret != NULL) && ((a == NULL) || (*a != ret)))
24255714Skris		ASN1_STRING_free(ret);
24355714Skris	ASN1err(ASN1_F_D2I_ASN1_BYTES,i);
24455714Skris	return(NULL);
24555714Skris	}
24655714Skris
24755714Skris
24859191Skris/* We are about to parse 0..n d2i_ASN1_bytes objects, we are to collapse
24959191Skris * them into the one structure that is then returned */
25055714Skris/* There have been a few bug fixes for this function from
25155714Skris * Paul Keogh <paul.keogh@sse.ie>, many thanks to him */
252160814Ssimonstatic int asn1_collate_primitive(ASN1_STRING *a, ASN1_const_CTX *c)
25355714Skris	{
25455714Skris	ASN1_STRING *os=NULL;
25555714Skris	BUF_MEM b;
25655714Skris	int num;
25755714Skris
25855714Skris	b.length=0;
25955714Skris	b.max=0;
26055714Skris	b.data=NULL;
26155714Skris
26255714Skris	if (a == NULL)
26355714Skris		{
26455714Skris		c->error=ERR_R_PASSED_NULL_PARAMETER;
26555714Skris		goto err;
26655714Skris		}
26755714Skris
26855714Skris	num=0;
26955714Skris	for (;;)
27055714Skris		{
27155714Skris		if (c->inf & 1)
27255714Skris			{
273160814Ssimon			c->eos=ASN1_const_check_infinite_end(&c->p,
27455714Skris				(long)(c->max-c->p));
27555714Skris			if (c->eos) break;
27655714Skris			}
27755714Skris		else
27855714Skris			{
27955714Skris			if (c->slen <= 0) break;
28055714Skris			}
28155714Skris
28255714Skris		c->q=c->p;
28355714Skris		if (d2i_ASN1_bytes(&os,&c->p,c->max-c->p,c->tag,c->xclass)
28455714Skris			== NULL)
28555714Skris			{
28655714Skris			c->error=ERR_R_ASN1_LIB;
28755714Skris			goto err;
28855714Skris			}
28955714Skris
290109998Smarkm		if (!BUF_MEM_grow_clean(&b,num+os->length))
29155714Skris			{
29255714Skris			c->error=ERR_R_BUF_LIB;
29355714Skris			goto err;
29455714Skris			}
29555714Skris		memcpy(&(b.data[num]),os->data,os->length);
29655714Skris		if (!(c->inf & 1))
29755714Skris			c->slen-=(c->p-c->q);
29855714Skris		num+=os->length;
29955714Skris		}
30055714Skris
301160814Ssimon	if (!asn1_const_Finish(c)) goto err;
30255714Skris
30355714Skris	a->length=num;
30468651Skris	if (a->data != NULL) OPENSSL_free(a->data);
30555714Skris	a->data=(unsigned char *)b.data;
30655714Skris	if (os != NULL) ASN1_STRING_free(os);
30755714Skris	return(1);
30855714Skriserr:
30955714Skris	ASN1err(ASN1_F_ASN1_COLLATE_PRIMITIVE,c->error);
31055714Skris	if (os != NULL) ASN1_STRING_free(os);
31168651Skris	if (b.data != NULL) OPENSSL_free(b.data);
31255714Skris	return(0);
31355714Skris	}
31455714Skris
315