Check for FindNextFile when defining it rather than FindFirstFile
[oweals/openssl.git] / crypto / asn1 / a_object.c
index 570aac71a742d254a6a4731bfe652b02674a9318..77b2768967e31cb9d0d2534253168ddbaa303a63 100644 (file)
@@ -83,13 +83,11 @@ int i2d_ASN1_OBJECT(ASN1_OBJECT *a, unsigned char **pp)
        return(objsize);
        }
 
-size_t a2d_ASN1_OBJECT(unsigned char *out, size_t olen, const char *buf,
-                      int num)
+int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)
        {
-       int i,len=0,c, use_bn;
-       unsigned first;
+       int i,first,len=0,c, use_bn;
        char ftmp[24], *tmp = ftmp;
-       size_t tmpsize = sizeof ftmp;
+       int tmpsize = sizeof ftmp;
        const char *p;
        unsigned long l;
        BIGNUM *bl = NULL;
@@ -141,7 +139,7 @@ size_t a2d_ASN1_OBJECT(unsigned char *out, size_t olen, const char *buf,
                                ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_DIGIT);
                                goto err;
                                }
-                       if (!use_bn && l > (ULONG_MAX / 10L))
+                       if (!use_bn && l >= ((ULONG_MAX - 80) / 10L))
                                {
                                use_bn = 1;
                                if (!bl)
@@ -152,11 +150,11 @@ size_t a2d_ASN1_OBJECT(unsigned char *out, size_t olen, const char *buf,
                        if (use_bn)
                                {
                                if (!BN_mul_word(bl, 10L)
-                                   || !BN_add_signed_word(bl, c-'0'))
+                                       || !BN_add_word(bl, c-'0'))
                                        goto err;
                                }
                        else
-                               l=l*10L+(c-'0');
+                               l=l*10L+(long)(c-'0');
                        }
                if (len == 0)
                        {
@@ -229,7 +227,7 @@ err:
        return(0);
        }
 
-int i2t_ASN1_OBJECT(char *buf, size_t buf_len, ASN1_OBJECT *a)
+int i2t_ASN1_OBJECT(char *buf, int buf_len, ASN1_OBJECT *a)
 {
        return OBJ_obj2txt(buf, buf_len, a, 0);
 }
@@ -237,7 +235,7 @@ int i2t_ASN1_OBJECT(char *buf, size_t buf_len, ASN1_OBJECT *a)
 int i2a_ASN1_OBJECT(BIO *bp, ASN1_OBJECT *a)
        {
        char buf[80], *p = buf;
-       size_t i;
+       int i;
 
        if ((a == NULL) || (a->data == NULL))
                return(BIO_write(bp,"NULL",4));
@@ -258,14 +256,13 @@ int i2a_ASN1_OBJECT(BIO *bp, ASN1_OBJECT *a)
        }
 
 ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
-                            size_t length)
+            long length)
 {
        const unsigned char *p;
-       size_t len;
+       long len;
        int tag,xclass;
        int inf,i;
        ASN1_OBJECT *ret = NULL;
-
        p= *pp;
        inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
        if (inf & 0x80)
@@ -286,13 +283,36 @@ err:
        ASN1err(ASN1_F_D2I_ASN1_OBJECT,i);
        return(NULL);
 }
+
 ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
-                            size_t len)
+            long len)
        {
        ASN1_OBJECT *ret=NULL;
        const unsigned char *p;
        unsigned char *data;
-       int i;
+       int i, length;
+
+       /* Sanity check OID encoding.
+        * Need at least one content octet.
+        * MSB must be clear in the last octet.
+        * can't have leading 0x80 in subidentifiers, see: X.690 8.19.2
+        */
+       if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL ||
+           p[len - 1] & 0x80)
+               {
+               ASN1err(ASN1_F_C2I_ASN1_OBJECT,ASN1_R_INVALID_OBJECT_ENCODING);
+               return NULL;
+               }
+       /* Now 0 < len <= INT_MAX, so the cast is safe. */
+       length = (int)len;
+       for (i = 0; i < length; i++, p++)
+               {
+               if (*p == 0x80 && (!i || !(p[-1] & 0x80)))
+                       {
+                       ASN1err(ASN1_F_C2I_ASN1_OBJECT,ASN1_R_INVALID_OBJECT_ENCODING);
+                       return NULL;
+                       }
+               }
 
        /* only the ASN1_OBJECTs from the 'table' will have values
         * for ->sn or ->ln */
@@ -308,23 +328,23 @@ ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
        data = (unsigned char *)ret->data;
        ret->data = NULL;
        /* once detached we can change it */
-       if ((data == NULL) || (ret->length < len))
+       if ((data == NULL) || (ret->length < length))
                {
                ret->length=0;
                if (data != NULL) OPENSSL_free(data);
-               data=OPENSSL_malloc(len ? len : 1);
+               data=(unsigned char *)OPENSSL_malloc(length);
                if (data == NULL)
                        { i=ERR_R_MALLOC_FAILURE; goto err; }
                ret->flags|=ASN1_OBJECT_FLAG_DYNAMIC_DATA;
                }
-       memcpy(data,p,len);
+       memcpy(data,p,length);
        /* reattach data to object, after which it remains const */
-       ret->data=data;
-       ret->length=len;
+       ret->data  =data;
+       ret->length=length;
        ret->sn=NULL;
        ret->ln=NULL;
        /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
-       p+=len;
+       p+=length;
 
        if (a != NULL) (*a)=ret;
        *pp=p;
@@ -376,7 +396,7 @@ void ASN1_OBJECT_free(ASN1_OBJECT *a)
                OPENSSL_free(a);
        }
 
-ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, size_t len,
+ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len,
             const char *sn, const char *ln)
        {
        ASN1_OBJECT o;