Fix more error codes.
[openssl.git] / crypto / asn1 / x_name.c
index 34f4e4c..681e5d1 100644 (file)
@@ -61,7 +61,7 @@
 #include <openssl/asn1t.h>
 #include <openssl/x509.h>
 
-static int x509_name_ex_d2i(ASN1_VALUE **val, unsigned char **in, long len, const ASN1_ITEM *it,
+static int x509_name_ex_d2i(ASN1_VALUE **val, const unsigned char **in, long len, const ASN1_ITEM *it,
                                        int tag, int aclass, char opt, ASN1_TLC *ctx);
 
 static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);
@@ -76,6 +76,7 @@ ASN1_SEQUENCE(X509_NAME_ENTRY) = {
 } ASN1_SEQUENCE_END(X509_NAME_ENTRY)
 
 IMPLEMENT_ASN1_FUNCTIONS(X509_NAME_ENTRY)
+IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME_ENTRY)
 
 /* For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY }
  * so declare two template wrappers for this
@@ -107,6 +108,7 @@ const ASN1_EXTERN_FUNCS x509_name_ff = {
 IMPLEMENT_EXTERN_ASN1(X509_NAME, V_ASN1_SEQUENCE, x509_name_ff) 
 
 IMPLEMENT_ASN1_FUNCTIONS(X509_NAME)
+IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME)
 
 static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it)
 {
@@ -119,8 +121,15 @@ static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it)
        ret->modified=1;
        *val = (ASN1_VALUE *)ret;
        return 1;
-       memerr:
-       ASN1err(ASN1_F_X509_NAME_NEW, ERR_R_MALLOC_FAILURE);
+
+ memerr:
+       ASN1err(ASN1_F_X509_NAME_EX_NEW, ERR_R_MALLOC_FAILURE);
+       if (ret)
+               {
+               if (ret->entries)
+                       sk_X509_NAME_ENTRY_free(ret->entries);
+               OPENSSL_free(ret);
+               }
        return 0;
 }
 
@@ -147,25 +156,26 @@ static void sk_internal_free(void *a)
        sk_free(a);
 }
 
-static int x509_name_ex_d2i(ASN1_VALUE **val, unsigned char **in, long len, const ASN1_ITEM *it,
+static int x509_name_ex_d2i(ASN1_VALUE **val, const unsigned char **in, long len, const ASN1_ITEM *it,
                                        int tag, int aclass, char opt, ASN1_TLC *ctx)
 {
-       unsigned char *p = *in, *q;
-       STACK *intname = NULL;
+       const unsigned char *p = *in, *q;
+       STACK *intname = NULL, **intname_pp = &intname;
        int i, j, ret;
-       X509_NAME *nm = NULL;
+       X509_NAME *nm = NULL, **nm_pp = &nm;
        STACK_OF(X509_NAME_ENTRY) *entries;
        X509_NAME_ENTRY *entry;
        q = p;
 
        /* Get internal representation of Name */
-       ret = ASN1_item_ex_d2i((ASN1_VALUE **)&intname, &p, len, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
-                                                               tag, aclass, opt, ctx);
+       ret = ASN1_item_ex_d2i((ASN1_VALUE **)intname_pp,
+                              &p, len, ASN1_ITEM_rptr(X509_NAME_INTERNAL),
+                              tag, aclass, opt, ctx);
        
        if(ret <= 0) return ret;
 
        if(*val) x509_name_ex_free(val, NULL);
-       if(!x509_name_ex_new((ASN1_VALUE **)&nm, NULL)) goto err;
+       if(!x509_name_ex_new((ASN1_VALUE **)nm_pp, NULL)) goto err;
        /* We've decoded it: now cache encoding */
        if(!BUF_MEM_grow(nm->bytes, p - q)) goto err;
        memcpy(nm->bytes->data, q, p - q);
@@ -187,7 +197,7 @@ static int x509_name_ex_d2i(ASN1_VALUE **val, unsigned char **in, long len, cons
        *in = p;
        return ret;
        err:
-       ASN1err(ASN1_F_D2I_X509_NAME, ERR_R_NESTED_ASN1_ERROR);
+       ASN1err(ASN1_F_X509_NAME_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
        return 0;
 }
 
@@ -209,7 +219,7 @@ static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out, const ASN1_IT
 
 static int x509_name_encode(X509_NAME *a)
 {
-       STACK *intname = NULL;
+       STACK *intname = NULL, **intname_pp = &intname;
        int len;
        unsigned char *p;
        STACK_OF(X509_NAME_ENTRY) *entries = NULL;
@@ -227,16 +237,18 @@ static int x509_name_encode(X509_NAME *a)
                }
                if(!sk_X509_NAME_ENTRY_push(entries, entry)) goto memerr;
        }
-       len = ASN1_item_ex_i2d((ASN1_VALUE **)&intname, NULL, ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
+       len = ASN1_item_ex_i2d((ASN1_VALUE **)intname_pp, NULL,
+                              ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
        if (!BUF_MEM_grow(a->bytes,len)) goto memerr;
        p=(unsigned char *)a->bytes->data;
-       ASN1_item_ex_i2d((ASN1_VALUE **)&intname, &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
+       ASN1_item_ex_i2d((ASN1_VALUE **)intname_pp,
+                        &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1);
        sk_pop_free(intname, sk_internal_free);
        a->modified = 0;
        return len;
        memerr:
        sk_pop_free(intname, sk_internal_free);
-       ASN1err(ASN1_F_D2I_X509_NAME, ERR_R_MALLOC_FAILURE);
+       ASN1err(ASN1_F_X509_NAME_ENCODE, ERR_R_MALLOC_FAILURE);
        return -1;
 }