sk_***_new_null() seems to be there to avoid exactly this sort of thing
[openssl.git] / crypto / asn1 / x_name.c
index 939b169..62e0ed0 100644 (file)
 
 #include <stdio.h>
 #include "cryptlib.h"
-#include "objects.h"
-#include "asn1_mac.h"
-
-/*
- * ASN1err(ASN1_F_D2I_X509_NAME,ERR_R_ASN1_LENGTH_MISMATCH);
- * ASN1err(ASN1_F_X509_NAME_NEW,ASN1_R_UNKNOWN_ATTRIBUTE_TYPE);
- * ASN1err(ASN1_F_D2I_X509_NAME_ENTRY,ERR_R_ASN1_LENGTH_MISMATCH);
- * ASN1err(ASN1_F_X509_NAME_ENTRY_NEW,ASN1_R_UNKNOWN_ATTRIBUTE_TYPE);
- */
+#include <openssl/objects.h>
+#include <openssl/asn1_mac.h>
+#include <openssl/x509.h>
 
-#ifndef NOPROTO
 static int i2d_X509_NAME_entries(X509_NAME *a);
-#else
-static int i2d_X509_NAME_entries();
-#endif
-
 int i2d_X509_NAME_ENTRY(X509_NAME_ENTRY *a, unsigned char **pp)
        {
        M_ASN1_I2D_vars(a);
@@ -125,7 +114,7 @@ int i2d_X509_NAME(X509_NAME *a, unsigned char **pp)
 static int i2d_X509_NAME_entries(X509_NAME *a)
        {
        X509_NAME_ENTRY *ne,*fe=NULL;
-       STACK *sk;
+       STACK_OF(X509_NAME_ENTRY) *sk;
        BUF_MEM *buf=NULL;
        int set=0,r,ret=0;
        int i;
@@ -133,9 +122,9 @@ static int i2d_X509_NAME_entries(X509_NAME *a)
        int size=0;
 
        sk=a->entries;
-       for (i=0; i<sk_num(sk); i++)
+       for (i=0; i<sk_X509_NAME_ENTRY_num(sk); i++)
                {
-               ne=(X509_NAME_ENTRY *)sk_value(sk,i);
+               ne=sk_X509_NAME_ENTRY_value(sk,i);
                if (fe == NULL)
                        {
                        fe=ne;
@@ -166,9 +155,9 @@ static int i2d_X509_NAME_entries(X509_NAME *a)
        ASN1_put_object(&p,1,ret,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
 
        set= -1;
-       for (i=0; i<sk_num(sk); i++)
+       for (i=0; i<sk_X509_NAME_ENTRY_num(sk); i++)
                {
-               ne=(X509_NAME_ENTRY *)sk_value(sk,i);
+               ne=sk_X509_NAME_ENTRY_value(sk,i);
                if (set != ne->set)
                        {
                        set=ne->set;
@@ -191,11 +180,11 @@ X509_NAME *d2i_X509_NAME(X509_NAME **a, unsigned char **pp, long length)
        M_ASN1_D2I_vars(a,X509_NAME *,X509_NAME_new);
 
        orig= *pp;
-       if (sk_num(ret->entries) > 0)
+       if (sk_X509_NAME_ENTRY_num(ret->entries) > 0)
                {
-               while (sk_num(ret->entries) > 0)
-                       X509_NAME_ENTRY_free((X509_NAME_ENTRY *)
-                               sk_pop(ret->entries));
+               while (sk_X509_NAME_ENTRY_num(ret->entries) > 0)
+                       X509_NAME_ENTRY_free(
+                                      sk_X509_NAME_ENTRY_pop(ret->entries));
                }
 
        M_ASN1_D2I_Init();
@@ -203,12 +192,12 @@ X509_NAME *d2i_X509_NAME(X509_NAME **a, unsigned char **pp, long length)
        for (;;)
                {
                if (M_ASN1_D2I_end_sequence()) break;
-               M_ASN1_D2I_get_set(ret->entries,d2i_X509_NAME_ENTRY,
-                       X509_NAME_ENTRY_free);
-               for (; idx < sk_num(ret->entries); idx++)
+               M_ASN1_D2I_get_set_type(X509_NAME_ENTRY,ret->entries,
+                                       d2i_X509_NAME_ENTRY,
+                                       X509_NAME_ENTRY_free);
+               for (; idx < sk_X509_NAME_ENTRY_num(ret->entries); idx++)
                        {
-                       ((X509_NAME_ENTRY *)sk_value(ret->entries,idx))->set=
-                               set;
+                       sk_X509_NAME_ENTRY_value(ret->entries,idx)->set=set;
                        }
                set++;
                }
@@ -228,7 +217,7 @@ X509_NAME *X509_NAME_new(void)
        ASN1_CTX c;
 
        M_ASN1_New_Malloc(ret,X509_NAME);
-       if ((ret->entries=sk_new(NULL)) == NULL)
+       if ((ret->entries=sk_X509_NAME_ENTRY_new_null()) == NULL)
                { c.line=__LINE__; goto err2; }
        M_ASN1_New(ret->bytes,BUF_MEM_new);
        ret->modified=1;
@@ -256,16 +245,16 @@ void X509_NAME_free(X509_NAME *a)
            return;
 
        BUF_MEM_free(a->bytes);
-       sk_pop_free(a->entries,X509_NAME_ENTRY_free);
-       Free((char *)a);
+       sk_X509_NAME_ENTRY_pop_free(a->entries,X509_NAME_ENTRY_free);
+       Free(a);
        }
 
 void X509_NAME_ENTRY_free(X509_NAME_ENTRY *a)
        {
        if (a == NULL) return;
        ASN1_OBJECT_free(a->object);
-       ASN1_BIT_STRING_free(a->value);
-       Free((char *)a);
+       M_ASN1_BIT_STRING_free(a->value);
+       Free(a);
        }
 
 int X509_NAME_set(X509_NAME **xn, X509_NAME *name)
@@ -286,3 +275,5 @@ int X509_NAME_set(X509_NAME **xn, X509_NAME *name)
        return(*xn != NULL);
        }
        
+IMPLEMENT_STACK_OF(X509_NAME_ENTRY)
+IMPLEMENT_ASN1_SET_OF(X509_NAME_ENTRY)