SetBlob: free rgSetBlob on error path
[openssl.git] / crypto / asn1 / a_set.c
index a140cc14ad865fe39c84d3bb8363f61d6f8804aa..4d07dc043b81286ebbd8e8e85f79d6851b4a00e5 100644 (file)
 
 #include <stdio.h>
 #include "cryptlib.h"
-#include "asn1_mac.h"
+#include <openssl/asn1_mac.h>
+
+#ifndef NO_ASN1_OLD
 
-/* ASN1err(ASN1_F_ASN1_TYPE_NEW,ERR_R_MALLOC_FAILURE);
- */
 typedef struct
     {
     unsigned char *pbData;
@@ -73,8 +73,8 @@ typedef struct
  */
 static int SetBlobCmp(const void *elem1, const void *elem2 )
     {
-    MYBLOB *b1 = (MYBLOB *)elem1;
-    MYBLOB *b2 = (MYBLOB *)elem2;
+    const MYBLOB *b1 = (const MYBLOB *)elem1;
+    const MYBLOB *b2 = (const MYBLOB *)elem2;
     int r;
 
     r = memcmp(b1->pbData, b2->pbData,
@@ -84,14 +84,10 @@ static int SetBlobCmp(const void *elem1, const void *elem2 )
     return b1->cbData-b2->cbData;
     }
 
-int i2d_ASN1_SET(a,pp,func,ex_tag,ex_class,is_set)
-STACK *a;
-unsigned char **pp;
-int (*func)();
-int ex_tag;
-int ex_class;
-int is_set;    /* if TRUE, then sort the contents (i.e. it isn't a SEQUENCE) */
-
+/* int is_set:  if TRUE, then sort the contents (i.e. it isn't a SEQUENCE)    */
+int i2d_ASN1_SET(STACK_OF(OPENSSL_BLOCK) *a, unsigned char **pp,
+                i2d_of_void *i2d, int ex_tag, int ex_class,
+                int is_set)
        {
        int ret=0,r;
        int i;
@@ -101,8 +97,8 @@ int is_set;  /* if TRUE, then sort the contents (i.e. it isn't a SEQUENCE) */
         int totSize;
 
        if (a == NULL) return(0);
-       for (i=sk_num(a)-1; i>=0; i--)
-               ret+=func(sk_value(a,i),NULL);
+       for (i=sk_OPENSSL_BLOCK_num(a)-1; i>=0; i--)
+               ret+=i2d(sk_OPENSSL_BLOCK_value(a,i),NULL);
        r=ASN1_object_size(1,ret,ex_tag);
        if (pp == NULL) return(r);
 
@@ -113,23 +109,28 @@ int is_set;       /* if TRUE, then sort the contents (i.e. it isn't a SEQUENCE) */
        /* And then again by Ben */
        /* And again by Steve */
 
-       if(!is_set || (sk_num(a) < 2))
+       if(!is_set || (sk_OPENSSL_BLOCK_num(a) < 2))
                {
-               for (i=0; i<sk_num(a); i++)
-                       func(sk_value(a,i),&p);
+               for (i=0; i<sk_OPENSSL_BLOCK_num(a); i++)
+                       i2d(sk_OPENSSL_BLOCK_value(a,i),&p);
 
                *pp=p;
                return(r);
                }
 
         pStart  = p; /* Catch the beg of Setblobs*/
-        rgSetBlob = (MYBLOB *)Malloc( sk_num(a) * sizeof(MYBLOB)); /* In this array
-we will store the SET blobs */
+               /* In this array we will store the SET blobs */
+               rgSetBlob = OPENSSL_malloc(sk_OPENSSL_BLOCK_num(a) * sizeof(MYBLOB));
+               if (rgSetBlob == NULL)
+                       {
+                       ASN1err(ASN1_F_I2D_ASN1_SET,ERR_R_MALLOC_FAILURE);
+                       return(0);
+                       }
 
-        for (i=0; i<sk_num(a); i++)
+        for (i=0; i<sk_OPENSSL_BLOCK_num(a); i++)
                {
                 rgSetBlob[i].pbData = p;  /* catch each set encode blob */
-                func(sk_value(a,i),&p);
+                i2d(sk_OPENSSL_BLOCK_value(a,i),&p);
                 rgSetBlob[i].cbData = p - rgSetBlob[i].pbData; /* Length of this
 SetBlob
 */
@@ -139,12 +140,17 @@ SetBlob
 
  /* Now we have to sort the blobs. I am using a simple algo.
     *Sort ptrs *Copy to temp-mem *Copy from temp-mem to user-mem*/
-        qsort( rgSetBlob, sk_num(a), sizeof(MYBLOB), SetBlobCmp);
-        pTempMem = Malloc(totSize);
+        qsort( rgSetBlob, sk_OPENSSL_BLOCK_num(a), sizeof(MYBLOB), SetBlobCmp);
+               if (!(pTempMem = OPENSSL_malloc(totSize)))
+                       {
+                       OPENSSL_free(rgSetBlob);
+                       ASN1err(ASN1_F_I2D_ASN1_SET,ERR_R_MALLOC_FAILURE);
+                       return(0);
+                       }
 
 /* Copy to temp mem */
         p = pTempMem;
-        for(i=0; i<sk_num(a); ++i)
+        for(i=0; i<sk_OPENSSL_BLOCK_num(a); ++i)
                {
                 memcpy(p, rgSetBlob[i].pbData, rgSetBlob[i].cbData);
                 p += rgSetBlob[i].cbData;
@@ -152,26 +158,29 @@ SetBlob
 
 /* Copy back to user mem*/
         memcpy(pStart, pTempMem, totSize);
-        Free(pTempMem);
-        Free(rgSetBlob);
+        OPENSSL_free(pTempMem);
+        OPENSSL_free(rgSetBlob);
 
         return(r);
         }
 
-STACK *d2i_ASN1_SET(a,pp,length,func,free_func,ex_tag,ex_class)
-STACK **a;
-unsigned char **pp;
-long length;
-char *(*func)();
-void (*free_func)();
-int ex_tag;
-int ex_class;
+STACK_OF(OPENSSL_BLOCK) *d2i_ASN1_SET(STACK_OF(OPENSSL_BLOCK) **a,
+                             const unsigned char **pp,
+                             long length, d2i_of_void *d2i,
+                             void (*free_func)(OPENSSL_BLOCK), int ex_tag,
+                             int ex_class)
        {
-       ASN1_CTX c;
-       STACK *ret=NULL;
+       ASN1_const_CTX c;
+       STACK_OF(OPENSSL_BLOCK) *ret=NULL;
 
        if ((a == NULL) || ((*a) == NULL))
-               { if ((ret=sk_new(NULL)) == NULL) goto err; }
+               {
+               if ((ret=sk_OPENSSL_BLOCK_new_null()) == NULL)
+                       {
+                       ASN1err(ASN1_F_D2I_ASN1_SET,ERR_R_MALLOC_FAILURE);
+                       goto err;
+                       }
+               }
        else
                ret=(*a);
 
@@ -206,13 +215,15 @@ int ex_class;
                char *s;
 
                if (M_ASN1_D2I_end_sequence()) break;
-               if ((s=func(NULL,&c.p,c.slen,c.max-c.p)) == NULL)
+               /* XXX: This was called with 4 arguments, incorrectly, it seems
+                  if ((s=func(NULL,&c.p,c.slen,c.max-c.p)) == NULL) */
+               if ((s=d2i(NULL,&c.p,c.slen)) == NULL)
                        {
                        ASN1err(ASN1_F_D2I_ASN1_SET,ASN1_R_ERROR_PARSING_SET_ELEMENT);
-                       asn1_add_error(*pp,(int)(c.q- *pp));
+                       asn1_add_error(*pp,(int)(c.p- *pp));
                        goto err;
                        }
-               if (!sk_push(ret,s)) goto err;
+               if (!sk_OPENSSL_BLOCK_push(ret,s)) goto err;
                }
        if (a != NULL) (*a)=ret;
        *pp=c.p;
@@ -221,10 +232,11 @@ err:
        if ((ret != NULL) && ((a == NULL) || (*a != ret)))
                {
                if (free_func != NULL)
-                       sk_pop_free(ret,free_func);
+                       sk_OPENSSL_BLOCK_pop_free(ret,free_func);
                else
-                       sk_free(ret);
+                       sk_OPENSSL_BLOCK_free(ret);
                }
        return(NULL);
        }
 
+#endif