Fix d4a4370050f7d72239b92a60ab9d4a2dd5e9fd84
[openssl.git] / crypto / asn1 / asn1_lib.c
index bb94257cee34ae74bac2061ff837f2a5620c7e9b..f1360ed7357dff33a4ad33fc903dfa5d56391b41 100644 (file)
@@ -64,7 +64,7 @@
 
 static int asn1_get_length(const unsigned char **pp,int *inf,long *rl,int max);
 static void asn1_put_length(unsigned char **pp, int length);
-const char *ASN1_version="ASN.1" OPENSSL_VERSION_PTEXT;
+const char ASN1_version[]="ASN.1" OPENSSL_VERSION_PTEXT;
 
 static int _asn1_check_infinite_end(const unsigned char **p, long len)
        {
@@ -131,6 +131,9 @@ int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,
        *pclass=xclass;
        if (!asn1_get_length(&p,&inf,plength,(int)max)) goto err;
 
+       if (inf && !(ret & V_ASN1_CONSTRUCTED))
+               goto err;
+
 #if 0
        fprintf(stderr,"p=%d + *plength=%ld > omax=%ld + *pp=%d  (%d > %d)\n", 
                (int)p,*plength,omax,(int)*pp,(int)(p+ *plength),
@@ -170,14 +173,20 @@ static int asn1_get_length(const unsigned char **pp, int *inf, long *rl, int max
                i= *p&0x7f;
                if (*(p++) & 0x80)
                        {
+                       if (max < (int)i)
+                               return 0;
+                       /* Skip leading zeroes */
+                       while (i && *p == 0)
+                               {
+                               p++;
+                               i--;
+                               }
                        if (i > sizeof(long))
                                return 0;
-                       if (max-- == 0) return(0);
                        while (i-- > 0)
                                {
                                ret<<=8L;
                                ret|= *(p++);
-                               if (max-- == 0) return(0);
                                }
                        }
                else
@@ -340,20 +349,31 @@ int asn1_GetSequence(ASN1_const_CTX *c, long *length)
        return(1);
        }
 
-ASN1_STRING *ASN1_STRING_dup(ASN1_STRING *str)
+int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
        {
-       ASN1_STRING *ret;
+       if (str == NULL)
+               return 0;
+       dst->type = str->type;
+       if (!ASN1_STRING_set(dst,str->data,str->length))
+               return 0;
+       dst->flags = str->flags;
+       return 1;
+       }
 
-       if (str == NULL) return(NULL);
-       if ((ret=ASN1_STRING_type_new(str->type)) == NULL)
-               return(NULL);
-       if (!ASN1_STRING_set(ret,str->data,str->length))
+ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str)
+       {
+       ASN1_STRING *ret;
+       if (!str)
+                return NULL;
+       ret=ASN1_STRING_new();
+       if (!ret)
+               return NULL;
+       if (!ASN1_STRING_copy(ret,str))
                {
                ASN1_STRING_free(ret);
-               return(NULL);
+               return NULL;
                }
-       ret->flags = str->flags;
-       return(ret);
+       return ret;
        }
 
 int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
@@ -393,6 +413,14 @@ int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
        return(1);
        }
 
+void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
+       {
+       if (str->data)
+               OPENSSL_free(str->data);
+       str->data = data;
+       str->length = len;
+       }
+
 ASN1_STRING *ASN1_STRING_new(void)
        {
        return(ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
@@ -419,11 +447,12 @@ ASN1_STRING *ASN1_STRING_type_new(int type)
 void ASN1_STRING_free(ASN1_STRING *a)
        {
        if (a == NULL) return;
-       if (a->data != NULL) OPENSSL_free(a->data);
+       if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
+               OPENSSL_free(a->data);
        OPENSSL_free(a);
        }
 
-int ASN1_STRING_cmp(ASN1_STRING *a, ASN1_STRING *b)
+int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
        {
        int i;
 
@@ -449,7 +478,7 @@ void asn1_add_error(const unsigned char *address, int offset)
        ERR_add_error_data(4,"address=",buf1," offset=",buf2);
        }
 
-int ASN1_STRING_length(ASN1_STRING *x)
+int ASN1_STRING_length(const ASN1_STRING *x)
 { return M_ASN1_STRING_length(x); }
 
 void ASN1_STRING_length_set(ASN1_STRING *x, int len)