Security fixes brought forward from 0.9.7.
[openssl.git] / crypto / asn1 / a_bytes.c
index 0c9132498cf99d98551ff2a9c5c1bed953ec9391..afd27b80e1b0e8a4db35c30dc87050fa966cac57 100644 (file)
@@ -1,5 +1,5 @@
 /* crypto/asn1/a_bytes.c */
-/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
+/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * This package is an SSL implementation written
 
 #include <stdio.h>
 #include "cryptlib.h"
-#include "asn1_mac.h"
+#include <openssl/asn1.h>
 
-/* ASN1err(ASN1_F_ASN1_TYPE_NEW,ASN1_R_ERROR_STACK);
- * ASN1err(ASN1_F_D2I_ASN1_TYPE_BYTES,ASN1_R_ERROR_STACK);
- * ASN1err(ASN1_F_D2I_ASN1_TYPE_BYTES,ASN1_R_WRONG_TYPE);
- * ASN1err(ASN1_F_ASN1_COLLATE_PRIMATIVE,ASN1_R_WRONG_TAG);
+static int asn1_collate_primitive(ASN1_STRING *a, ASN1_CTX *c);
+/* type is a 'bitmap' of acceptable string types.
  */
-
-static unsigned long tag2bit[32]={
-0,     0,      0,      0,      /* tags  0 -  3 */
-B_ASN1_OCTET_STRING,   0,      0,              B_ASN1_UNKNOWN,/* tags  4- 7 */
-B_ASN1_UNKNOWN,        B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,/* tags  8-11 */
-B_ASN1_UNKNOWN,        B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,/* tags 12-15 */
-0,     0,      B_ASN1_NUMERICSTRING,B_ASN1_PRINTABLESTRING,
-B_ASN1_T61STRING,B_ASN1_VIDEOTEXSTRING,B_ASN1_IA5STRING,0,
-0,B_ASN1_GRAPHICSTRING,B_ASN1_ISO64STRING,B_ASN1_GENERALSTRING,
-B_ASN1_UNIVERSALSTRING,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,
-       };
-
-#ifndef NOPROTO
-static int asn1_collate_primative(ASN1_STRING *a, ASN1_CTX *c);
-#else
-static int asn1_collate_primative();
-#endif
-
-/* type is a 'bitmap' of acceptable string types to be accepted.
- */
-ASN1_STRING *d2i_ASN1_type_bytes(a, pp, length, type)
-ASN1_STRING **a;
-unsigned char **pp;
-long length;
-int type;
+ASN1_STRING *d2i_ASN1_type_bytes(ASN1_STRING **a, unsigned char **pp,
+            long length, int type)
        {
        ASN1_STRING *ret=NULL;
        unsigned char *p,*s;
@@ -97,13 +72,6 @@ int type;
        int inf,tag,xclass;
        int i=0;
 
-       if ((a == NULL) || ((*a) == NULL))
-               {
-               if ((ret=ASN1_STRING_new()) == NULL) return(NULL);
-               }
-       else
-               ret=(*a);
-
        p= *pp;
        inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
        if (inf & 0x80) goto err;
@@ -113,14 +81,26 @@ int type;
                i=ASN1_R_TAG_VALUE_TOO_HIGH;;
                goto err;
                }
-       if (!(tag2bit[tag] & type))
+       if (!(ASN1_tag2bit(tag) & type))
                {
                i=ASN1_R_WRONG_TYPE;
                goto err;
                }
+
+       /* If a bit-string, exit early */
+       if (tag == V_ASN1_BIT_STRING)
+               return(d2i_ASN1_BIT_STRING(a,pp,length));
+
+       if ((a == NULL) || ((*a) == NULL))
+               {
+               if ((ret=ASN1_STRING_new()) == NULL) return(NULL);
+               }
+       else
+               ret=(*a);
+
        if (len != 0)
                {
-               s=(unsigned char *)Malloc((int)len+1);
+               s=(unsigned char *)OPENSSL_malloc((int)len+1);
                if (s == NULL)
                        {
                        i=ERR_R_MALLOC_FAILURE;
@@ -133,7 +113,7 @@ int type;
        else
                s=NULL;
 
-       if (ret->data != NULL) Free((char *)ret->data);
+       if (ret->data != NULL) OPENSSL_free(ret->data);
        ret->length=(int)len;
        ret->data=s;
        ret->type=tag;
@@ -147,16 +127,16 @@ err:
        return(NULL);
        }
 
-int i2d_ASN1_bytes(a, pp, tag, xclass)
-ASN1_STRING *a;
-unsigned char **pp;
-int tag;
-int xclass;
+int i2d_ASN1_bytes(ASN1_STRING *a, unsigned char **pp, int tag, int xclass)
        {
        int ret,r,constructed;
        unsigned char *p;
 
        if (a == NULL)  return(0);
+
+       if (tag == V_ASN1_BIT_STRING)
+               return(i2d_ASN1_BIT_STRING(a,pp));
+               
        ret=a->length;
        r=ASN1_object_size(0,ret,tag);
        if (pp == NULL) return(r);
@@ -173,12 +153,8 @@ int xclass;
        return(r);
        }
 
-ASN1_STRING *d2i_ASN1_bytes(a, pp, length, Ptag, Pclass)
-ASN1_STRING **a;
-unsigned char **pp;
-long length;
-int Ptag;
-int Pclass;
+ASN1_STRING *d2i_ASN1_bytes(ASN1_STRING **a, unsigned char **pp, long length,
+            int Ptag, int Pclass)
        {
        ASN1_STRING *ret=NULL;
        unsigned char *p,*s;
@@ -218,7 +194,7 @@ int Pclass;
                c.tag=Ptag;
                c.xclass=Pclass;
                c.max=(length == 0)?0:(p+length);
-               if (!asn1_collate_primative(ret,&c)) 
+               if (!asn1_collate_primitive(ret,&c)) 
                        goto err; 
                else
                        {
@@ -229,10 +205,10 @@ int Pclass;
                {
                if (len != 0)
                        {
-                       if (ret->length < len)
+                       if ((ret->length < len) || (ret->data == NULL))
                                {
-                               if (ret->data != NULL) Free((char *)ret->data);
-                               s=(unsigned char *)Malloc((int)len);
+                               if (ret->data != NULL) OPENSSL_free(ret->data);
+                               s=(unsigned char *)OPENSSL_malloc((int)len + 1);
                                if (s == NULL)
                                        {
                                        i=ERR_R_MALLOC_FAILURE;
@@ -242,12 +218,13 @@ int Pclass;
                        else
                                s=ret->data;
                        memcpy(s,p,(int)len);
+                       s[len] = '\0';
                        p+=len;
                        }
                else
                        {
                        s=NULL;
-                       if (ret->data != NULL) Free((char *)ret->data);
+                       if (ret->data != NULL) OPENSSL_free(ret->data);
                        }
 
                ret->length=(int)len;
@@ -266,13 +243,11 @@ err:
        }
 
 
-/* We are about to parse 0..n d2i_ASN1_bytes objects, we are to collapes
- * them into the one struture that is then returned */
+/* We are about to parse 0..n d2i_ASN1_bytes objects, we are to collapse
+ * them into the one structure that is then returned */
 /* There have been a few bug fixes for this function from
  * Paul Keogh <paul.keogh@sse.ie>, many thanks to him */
-static int asn1_collate_primative(a,c)
-ASN1_STRING *a;
-ASN1_CTX *c;
+static int asn1_collate_primitive(ASN1_STRING *a, ASN1_CTX *c)
        {
        ASN1_STRING *os=NULL;
        BUF_MEM b;
@@ -310,7 +285,7 @@ ASN1_CTX *c;
                        goto err;
                        }
 
-               if (!BUF_MEM_grow(&b,num+os->length))
+               if (!BUF_MEM_grow_clean(&b,num+os->length))
                        {
                        c->error=ERR_R_BUF_LIB;
                        goto err;
@@ -324,14 +299,14 @@ ASN1_CTX *c;
        if (!asn1_Finish(c)) goto err;
 
        a->length=num;
-       if (a->data != NULL) Free(a->data);
+       if (a->data != NULL) OPENSSL_free(a->data);
        a->data=(unsigned char *)b.data;
        if (os != NULL) ASN1_STRING_free(os);
        return(1);
 err:
-       ASN1err(ASN1_F_ASN1_COLLATE_PRIMATIVE,c->error);
+       ASN1err(ASN1_F_ASN1_COLLATE_PRIMITIVE,c->error);
        if (os != NULL) ASN1_STRING_free(os);
-       if (b.data != NULL) Free(b.data);
+       if (b.data != NULL) OPENSSL_free(b.data);
        return(0);
        }