sl@0: /* crypto/asn1/a_bytes.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: sl@0: #include sl@0: #include "cryptlib.h" sl@0: #include sl@0: sl@0: static int asn1_collate_primitive(ASN1_STRING *a, ASN1_const_CTX *c); sl@0: /* type is a 'bitmap' of acceptable string types. sl@0: */ sl@0: EXPORT_C ASN1_STRING *d2i_ASN1_type_bytes(ASN1_STRING **a, const unsigned char **pp, sl@0: long length, int type) sl@0: { sl@0: ASN1_STRING *ret=NULL; sl@0: const unsigned char *p; sl@0: unsigned char *s; sl@0: long len; sl@0: int inf,tag,xclass; sl@0: int i=0; sl@0: sl@0: p= *pp; sl@0: inf=ASN1_get_object(&p,&len,&tag,&xclass,length); sl@0: if (inf & 0x80) goto err; sl@0: sl@0: if (tag >= 32) sl@0: { sl@0: i=ASN1_R_TAG_VALUE_TOO_HIGH;; sl@0: goto err; sl@0: } sl@0: if (!(ASN1_tag2bit(tag) & type)) sl@0: { sl@0: i=ASN1_R_WRONG_TYPE; sl@0: goto err; sl@0: } sl@0: sl@0: /* If a bit-string, exit early */ sl@0: if (tag == V_ASN1_BIT_STRING) sl@0: return(d2i_ASN1_BIT_STRING(a,pp,length)); sl@0: sl@0: if ((a == NULL) || ((*a) == NULL)) sl@0: { sl@0: if ((ret=ASN1_STRING_new()) == NULL) return(NULL); sl@0: } sl@0: else sl@0: ret=(*a); sl@0: sl@0: if (len != 0) sl@0: { sl@0: s=(unsigned char *)OPENSSL_malloc((int)len+1); sl@0: if (s == NULL) sl@0: { sl@0: i=ERR_R_MALLOC_FAILURE; sl@0: goto err; sl@0: } sl@0: memcpy(s,p,(int)len); sl@0: s[len]='\0'; sl@0: p+=len; sl@0: } sl@0: else sl@0: s=NULL; sl@0: sl@0: if (ret->data != NULL) OPENSSL_free(ret->data); sl@0: ret->length=(int)len; sl@0: ret->data=s; sl@0: ret->type=tag; sl@0: if (a != NULL) (*a)=ret; sl@0: *pp=p; sl@0: return(ret); sl@0: err: sl@0: ASN1err(ASN1_F_D2I_ASN1_TYPE_BYTES,i); sl@0: if ((ret != NULL) && ((a == NULL) || (*a != ret))) sl@0: ASN1_STRING_free(ret); sl@0: return(NULL); sl@0: } sl@0: sl@0: EXPORT_C int i2d_ASN1_bytes(ASN1_STRING *a, unsigned char **pp, int tag, int xclass) sl@0: { sl@0: int ret,r,constructed; sl@0: unsigned char *p; sl@0: sl@0: if (a == NULL) return(0); sl@0: sl@0: if (tag == V_ASN1_BIT_STRING) sl@0: return(i2d_ASN1_BIT_STRING(a,pp)); sl@0: sl@0: ret=a->length; sl@0: r=ASN1_object_size(0,ret,tag); sl@0: if (pp == NULL) return(r); sl@0: p= *pp; sl@0: sl@0: if ((tag == V_ASN1_SEQUENCE) || (tag == V_ASN1_SET)) sl@0: constructed=1; sl@0: else sl@0: constructed=0; sl@0: ASN1_put_object(&p,constructed,ret,tag,xclass); sl@0: memcpy(p,a->data,a->length); sl@0: p+=a->length; sl@0: *pp= p; sl@0: return(r); sl@0: } sl@0: sl@0: EXPORT_C ASN1_STRING *d2i_ASN1_bytes(ASN1_STRING **a, const unsigned char **pp, sl@0: long length, int Ptag, int Pclass) sl@0: { sl@0: ASN1_STRING *ret=NULL; sl@0: const unsigned char *p; sl@0: unsigned char *s; sl@0: long len; sl@0: int inf,tag,xclass; sl@0: int i=0; sl@0: sl@0: if ((a == NULL) || ((*a) == NULL)) sl@0: { sl@0: if ((ret=ASN1_STRING_new()) == NULL) return(NULL); sl@0: } sl@0: else sl@0: ret=(*a); sl@0: sl@0: p= *pp; sl@0: inf=ASN1_get_object(&p,&len,&tag,&xclass,length); sl@0: if (inf & 0x80) sl@0: { sl@0: i=ASN1_R_BAD_OBJECT_HEADER; sl@0: goto err; sl@0: } sl@0: sl@0: if (tag != Ptag) sl@0: { sl@0: i=ASN1_R_WRONG_TAG; sl@0: goto err; sl@0: } sl@0: sl@0: if (inf & V_ASN1_CONSTRUCTED) sl@0: { sl@0: ASN1_const_CTX c; sl@0: sl@0: c.pp=pp; sl@0: c.p=p; sl@0: c.inf=inf; sl@0: c.slen=len; sl@0: c.tag=Ptag; sl@0: c.xclass=Pclass; sl@0: c.max=(length == 0)?0:(p+length); sl@0: if (!asn1_collate_primitive(ret,&c)) sl@0: goto err; sl@0: else sl@0: { sl@0: p=c.p; sl@0: } sl@0: } sl@0: else sl@0: { sl@0: if (len != 0) sl@0: { sl@0: if ((ret->length < len) || (ret->data == NULL)) sl@0: { sl@0: if (ret->data != NULL) OPENSSL_free(ret->data); sl@0: s=(unsigned char *)OPENSSL_malloc((int)len + 1); sl@0: if (s == NULL) sl@0: { sl@0: i=ERR_R_MALLOC_FAILURE; sl@0: goto err; sl@0: } sl@0: } sl@0: else sl@0: s=ret->data; sl@0: memcpy(s,p,(int)len); sl@0: s[len] = '\0'; sl@0: p+=len; sl@0: } sl@0: else sl@0: { sl@0: s=NULL; sl@0: if (ret->data != NULL) OPENSSL_free(ret->data); sl@0: } sl@0: sl@0: ret->length=(int)len; sl@0: ret->data=s; sl@0: ret->type=Ptag; sl@0: } sl@0: sl@0: if (a != NULL) (*a)=ret; sl@0: *pp=p; sl@0: return(ret); sl@0: err: sl@0: if ((ret != NULL) && ((a == NULL) || (*a != ret))) sl@0: ASN1_STRING_free(ret); sl@0: ASN1err(ASN1_F_D2I_ASN1_BYTES,i); sl@0: return(NULL); sl@0: } sl@0: sl@0: sl@0: /* We are about to parse 0..n d2i_ASN1_bytes objects, we are to collapse sl@0: * them into the one structure that is then returned */ sl@0: /* There have been a few bug fixes for this function from sl@0: * Paul Keogh , many thanks to him */ sl@0: static int asn1_collate_primitive(ASN1_STRING *a, ASN1_const_CTX *c) sl@0: { sl@0: ASN1_STRING *os=NULL; sl@0: BUF_MEM b; sl@0: int num; sl@0: sl@0: b.length=0; sl@0: b.max=0; sl@0: b.data=NULL; sl@0: sl@0: if (a == NULL) sl@0: { sl@0: c->error=ERR_R_PASSED_NULL_PARAMETER; sl@0: goto err; sl@0: } sl@0: sl@0: num=0; sl@0: for (;;) sl@0: { sl@0: if (c->inf & 1) sl@0: { sl@0: c->eos=ASN1_const_check_infinite_end(&c->p, sl@0: (long)(c->max-c->p)); sl@0: if (c->eos) break; sl@0: } sl@0: else sl@0: { sl@0: if (c->slen <= 0) break; sl@0: } sl@0: sl@0: c->q=c->p; sl@0: if (d2i_ASN1_bytes(&os,&c->p,c->max-c->p,c->tag,c->xclass) sl@0: == NULL) sl@0: { sl@0: c->error=ERR_R_ASN1_LIB; sl@0: goto err; sl@0: } sl@0: sl@0: if (!BUF_MEM_grow_clean(&b,num+os->length)) sl@0: { sl@0: c->error=ERR_R_BUF_LIB; sl@0: goto err; sl@0: } sl@0: memcpy(&(b.data[num]),os->data,os->length); sl@0: if (!(c->inf & 1)) sl@0: c->slen-=(c->p-c->q); sl@0: num+=os->length; sl@0: } sl@0: sl@0: if (!asn1_const_Finish(c)) goto err; sl@0: sl@0: a->length=num; sl@0: if (a->data != NULL) OPENSSL_free(a->data); sl@0: a->data=(unsigned char *)b.data; sl@0: if (os != NULL) ASN1_STRING_free(os); sl@0: return(1); sl@0: err: sl@0: ASN1err(ASN1_F_ASN1_COLLATE_PRIMITIVE,c->error); sl@0: if (os != NULL) ASN1_STRING_free(os); sl@0: if (b.data != NULL) OPENSSL_free(b.data); sl@0: return(0); sl@0: } sl@0: