Another safe stack.

This commit is contained in:
Ben Laurie 1999-05-01 17:40:57 +00:00
parent 3a1a8467bd
commit 65d4927b8d
8 changed files with 70 additions and 55 deletions

View File

@ -907,7 +907,7 @@ start: for (;;)
min,max)) min,max))
goto err; goto err;
} }
if (sk_num(ri->subject->entries) == 0) if (sk_X509_NAME_ENTRY_num(ri->subject->entries) == 0)
{ {
BIO_printf(bio_err,"error, no objects specified in config file\n"); BIO_printf(bio_err,"error, no objects specified in config file\n");
goto err; goto err;

View File

@ -154,6 +154,10 @@ err:\
M_ASN1_D2I_get_imp_set(r,func,free_func, \ M_ASN1_D2I_get_imp_set(r,func,free_func, \
V_ASN1_SET,V_ASN1_UNIVERSAL); V_ASN1_SET,V_ASN1_UNIVERSAL);
#define M_ASN1_D2I_get_set_type(type,r,func,free_func) \
M_ASN1_D2I_get_imp_set_type(type,r,func,free_func, \
V_ASN1_SET,V_ASN1_UNIVERSAL);
#define M_ASN1_D2I_get_set_opt(r,func,free_func) \ #define M_ASN1_D2I_get_set_opt(r,func,free_func) \
if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \ if ((c.slen != 0) && (M_ASN1_next == (V_ASN1_UNIVERSAL| \
V_ASN1_CONSTRUCTED|V_ASN1_SET)))\ V_ASN1_CONSTRUCTED|V_ASN1_SET)))\

View File

@ -113,7 +113,7 @@ int i2d_X509_NAME(X509_NAME *a, unsigned char **pp)
static int i2d_X509_NAME_entries(X509_NAME *a) static int i2d_X509_NAME_entries(X509_NAME *a)
{ {
X509_NAME_ENTRY *ne,*fe=NULL; X509_NAME_ENTRY *ne,*fe=NULL;
STACK *sk; STACK_OF(X509_NAME_ENTRY) *sk;
BUF_MEM *buf=NULL; BUF_MEM *buf=NULL;
int set=0,r,ret=0; int set=0,r,ret=0;
int i; int i;
@ -121,9 +121,9 @@ static int i2d_X509_NAME_entries(X509_NAME *a)
int size=0; int size=0;
sk=a->entries; 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) if (fe == NULL)
{ {
fe=ne; fe=ne;
@ -154,9 +154,9 @@ static int i2d_X509_NAME_entries(X509_NAME *a)
ASN1_put_object(&p,1,ret,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL); ASN1_put_object(&p,1,ret,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
set= -1; 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) if (set != ne->set)
{ {
set=ne->set; set=ne->set;
@ -179,11 +179,11 @@ X509_NAME *d2i_X509_NAME(X509_NAME **a, unsigned char **pp, long length)
M_ASN1_D2I_vars(a,X509_NAME *,X509_NAME_new); M_ASN1_D2I_vars(a,X509_NAME *,X509_NAME_new);
orig= *pp; orig= *pp;
if (sk_num(ret->entries) > 0) if (sk_X509_NAME_ENTRY_num(ret->entries) > 0)
{ {
while (sk_num(ret->entries) > 0) while (sk_X509_NAME_ENTRY_num(ret->entries) > 0)
X509_NAME_ENTRY_free((X509_NAME_ENTRY *) X509_NAME_ENTRY_free(
sk_pop(ret->entries)); sk_X509_NAME_ENTRY_pop(ret->entries));
} }
M_ASN1_D2I_Init(); M_ASN1_D2I_Init();
@ -191,12 +191,12 @@ X509_NAME *d2i_X509_NAME(X509_NAME **a, unsigned char **pp, long length)
for (;;) for (;;)
{ {
if (M_ASN1_D2I_end_sequence()) break; if (M_ASN1_D2I_end_sequence()) break;
M_ASN1_D2I_get_set(ret->entries,d2i_X509_NAME_ENTRY, M_ASN1_D2I_get_set_type(X509_NAME_ENTRY,ret->entries,
d2i_X509_NAME_ENTRY,
X509_NAME_ENTRY_free); X509_NAME_ENTRY_free);
for (; idx < sk_num(ret->entries); idx++) for (; idx < sk_X509_NAME_ENTRY_num(ret->entries); idx++)
{ {
((X509_NAME_ENTRY *)sk_value(ret->entries,idx))->set= sk_X509_NAME_ENTRY_value(ret->entries,idx)->set=set;
set;
} }
set++; set++;
} }
@ -216,7 +216,7 @@ X509_NAME *X509_NAME_new(void)
ASN1_CTX c; ASN1_CTX c;
M_ASN1_New_Malloc(ret,X509_NAME); 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; } { c.line=__LINE__; goto err2; }
M_ASN1_New(ret->bytes,BUF_MEM_new); M_ASN1_New(ret->bytes,BUF_MEM_new);
ret->modified=1; ret->modified=1;
@ -244,8 +244,8 @@ void X509_NAME_free(X509_NAME *a)
return; return;
BUF_MEM_free(a->bytes); BUF_MEM_free(a->bytes);
sk_pop_free(a->entries,X509_NAME_ENTRY_free); sk_X509_NAME_ENTRY_pop_free(a->entries,X509_NAME_ENTRY_free);
Free((char *)a); Free(a);
} }
void X509_NAME_ENTRY_free(X509_NAME_ENTRY *a) void X509_NAME_ENTRY_free(X509_NAME_ENTRY *a)
@ -253,7 +253,7 @@ void X509_NAME_ENTRY_free(X509_NAME_ENTRY *a)
if (a == NULL) return; if (a == NULL) return;
ASN1_OBJECT_free(a->object); ASN1_OBJECT_free(a->object);
ASN1_BIT_STRING_free(a->value); ASN1_BIT_STRING_free(a->value);
Free((char *)a); Free(a);
} }
int X509_NAME_set(X509_NAME **xn, X509_NAME *name) int X509_NAME_set(X509_NAME **xn, X509_NAME *name)
@ -274,3 +274,5 @@ int X509_NAME_set(X509_NAME **xn, X509_NAME *name)
return(*xn != NULL); return(*xn != NULL);
} }
IMPLEMENT_STACK_OF(X509_NAME_ENTRY)
IMPLEMENT_ASN1_SET_OF(X509_NAME_ENTRY)

View File

@ -73,8 +73,9 @@ type *sk_##type##_set(STACK_OF(type) *sk,int n,type *v); \
void sk_##type##_zero(STACK_OF(type) *sk); \ void sk_##type##_zero(STACK_OF(type) *sk); \
int sk_##type##_push(STACK_OF(type) *sk,type *v); \ int sk_##type##_push(STACK_OF(type) *sk,type *v); \
int sk_##type##_find(STACK_OF(type) *sk,type *v); \ int sk_##type##_find(STACK_OF(type) *sk,type *v); \
void sk_##type##_delete(STACK_OF(type) *sk,int n); \ type *sk_##type##_delete(STACK_OF(type) *sk,int n); \
void sk_##type##_delete_ptr(STACK_OF(type) *sk,type *v); \ void sk_##type##_delete_ptr(STACK_OF(type) *sk,type *v); \
int sk_##type##_insert(STACK_OF(type) *sk,type *v,int n); \
void sk_##type##_set_cmp_func(STACK_OF(type) *sk,int (*cmp)(type **,type **)); \ void sk_##type##_set_cmp_func(STACK_OF(type) *sk,int (*cmp)(type **,type **)); \
STACK_OF(type) *sk_##type##_dup(STACK_OF(type) *sk); \ STACK_OF(type) *sk_##type##_dup(STACK_OF(type) *sk); \
void sk_##type##_pop_free(STACK_OF(type) *sk,void (*func)(type *)); \ void sk_##type##_pop_free(STACK_OF(type) *sk,void (*func)(type *)); \
@ -100,10 +101,12 @@ int sk_##type##_push(STACK_OF(type) *sk,type *v) \
{ return sk_push((STACK *)sk,(char *)v); } \ { return sk_push((STACK *)sk,(char *)v); } \
int sk_##type##_find(STACK_OF(type) *sk,type *v) \ int sk_##type##_find(STACK_OF(type) *sk,type *v) \
{ return sk_find((STACK *)sk,(char *)v); } \ { return sk_find((STACK *)sk,(char *)v); } \
void sk_##type##_delete(STACK_OF(type) *sk,int n) \ type *sk_##type##_delete(STACK_OF(type) *sk,int n) \
{ sk_delete((STACK *)sk,n); } \ { return (type *)sk_delete((STACK *)sk,n); } \
void sk_##type##_delete_ptr(STACK_OF(type) *sk,type *v) \ void sk_##type##_delete_ptr(STACK_OF(type) *sk,type *v) \
{ sk_delete_ptr((STACK *)sk,(char *)v); } \ { sk_delete_ptr((STACK *)sk,(char *)v); } \
int sk_##type##_insert(STACK_OF(type) *sk,type *v,int n) \
{ return sk_insert((STACK *)sk,(char *)v,n); } \
void sk_##type##_set_cmp_func(STACK_OF(type) *sk,int (*cmp)(type **,type **)) \ void sk_##type##_set_cmp_func(STACK_OF(type) *sk,int (*cmp)(type **,type **)) \
{ sk_set_cmp_func((STACK *)sk,cmp); } \ { sk_set_cmp_func((STACK *)sk,cmp); } \
STACK_OF(type) *sk_##type##_dup(STACK_OF(type) *sk) \ STACK_OF(type) *sk_##type##_dup(STACK_OF(type) *sk) \

View File

@ -136,10 +136,13 @@ typedef struct X509_name_entry_st
int size; /* temp variable */ int size; /* temp variable */
} X509_NAME_ENTRY; } X509_NAME_ENTRY;
DECLARE_STACK_OF(X509_NAME_ENTRY)
DECLARE_ASN1_SET_OF(X509_NAME_ENTRY)
/* we always keep X509_NAMEs in 2 forms. */ /* we always keep X509_NAMEs in 2 forms. */
typedef struct X509_name_st typedef struct X509_name_st
{ {
STACK *entries; /* of X509_NAME_ENTRY */ STACK_OF(X509_NAME_ENTRY) *entries;
int modified; /* true if 'bytes' needs to be built */ int modified; /* true if 'bytes' needs to be built */
#ifdef HEADER_BUFFER_H #ifdef HEADER_BUFFER_H
BUF_MEM *bytes; BUF_MEM *bytes;

View File

@ -143,12 +143,14 @@ int X509_NAME_cmp(X509_NAME *a, X509_NAME *b)
int i,j; int i,j;
X509_NAME_ENTRY *na,*nb; X509_NAME_ENTRY *na,*nb;
if (sk_num(a->entries) != sk_num(b->entries)) if (sk_X509_NAME_ENTRY_num(a->entries)
return(sk_num(a->entries)-sk_num(b->entries)); != sk_X509_NAME_ENTRY_num(b->entries))
for (i=sk_num(a->entries)-1; i>=0; i--) return sk_X509_NAME_ENTRY_num(a->entries)
-sk_X509_NAME_ENTRY_num(b->entries);
for (i=sk_X509_NAME_ENTRY_num(a->entries)-1; i>=0; i--)
{ {
na=(X509_NAME_ENTRY *)sk_value(a->entries,i); na=sk_X509_NAME_ENTRY_value(a->entries,i);
nb=(X509_NAME_ENTRY *)sk_value(b->entries,i); nb=sk_X509_NAME_ENTRY_value(b->entries,i);
j=na->value->length-nb->value->length; j=na->value->length-nb->value->length;
if (j) return(j); if (j) return(j);
j=memcmp(na->value->data,nb->value->data, j=memcmp(na->value->data,nb->value->data,
@ -161,10 +163,10 @@ int X509_NAME_cmp(X509_NAME *a, X509_NAME *b)
/* We will check the object types after checking the values /* We will check the object types after checking the values
* since the values will more often be different than the object * since the values will more often be different than the object
* types. */ * types. */
for (i=sk_num(a->entries)-1; i>=0; i--) for (i=sk_X509_NAME_ENTRY_num(a->entries)-1; i>=0; i--)
{ {
na=(X509_NAME_ENTRY *)sk_value(a->entries,i); na=sk_X509_NAME_ENTRY_value(a->entries,i);
nb=(X509_NAME_ENTRY *)sk_value(b->entries,i); nb=sk_X509_NAME_ENTRY_value(b->entries,i);
j=OBJ_cmp(na->object,nb->object); j=OBJ_cmp(na->object,nb->object);
if (j) return(j); if (j) return(j);
} }

View File

@ -96,9 +96,9 @@ char *X509_NAME_oneline(X509_NAME *a, char *buf, int len)
len--; /* space for '\0' */ len--; /* space for '\0' */
l=0; l=0;
for (i=0; (int)i<sk_num(a->entries); i++) for (i=0; i<sk_X509_NAME_ENTRY_num(a->entries); i++)
{ {
ne=(X509_NAME_ENTRY *)sk_value(a->entries,i); ne=sk_X509_NAME_ENTRY_value(a->entries,i);
n=OBJ_obj2nid(ne->object); n=OBJ_obj2nid(ne->object);
if ((n == NID_undef) || ((s=OBJ_nid2sn(n)) == NULL)) if ((n == NID_undef) || ((s=OBJ_nid2sn(n)) == NULL))
{ {

View File

@ -92,7 +92,7 @@ int X509_NAME_get_text_by_OBJ(X509_NAME *name, ASN1_OBJECT *obj, char *buf,
int X509_NAME_entry_count(X509_NAME *name) int X509_NAME_entry_count(X509_NAME *name)
{ {
if (name == NULL) return(0); if (name == NULL) return(0);
return(sk_num(name->entries)); return(sk_X509_NAME_ENTRY_num(name->entries));
} }
int X509_NAME_get_index_by_NID(X509_NAME *name, int nid, int lastpos) int X509_NAME_get_index_by_NID(X509_NAME *name, int nid, int lastpos)
@ -110,16 +110,16 @@ int X509_NAME_get_index_by_OBJ(X509_NAME *name, ASN1_OBJECT *obj,
{ {
int n; int n;
X509_NAME_ENTRY *ne; X509_NAME_ENTRY *ne;
STACK *sk; STACK_OF(X509_NAME_ENTRY) *sk;
if (name == NULL) return(-1); if (name == NULL) return(-1);
if (lastpos < 0) if (lastpos < 0)
lastpos= -1; lastpos= -1;
sk=name->entries; sk=name->entries;
n=sk_num(sk); n=sk_X509_NAME_ENTRY_num(sk);
for (lastpos++; lastpos < n; lastpos++) for (lastpos++; lastpos < n; lastpos++)
{ {
ne=(X509_NAME_ENTRY *)sk_value(sk,lastpos); ne=sk_X509_NAME_ENTRY_value(sk,lastpos);
if (OBJ_cmp(ne->object,obj) == 0) if (OBJ_cmp(ne->object,obj) == 0)
return(lastpos); return(lastpos);
} }
@ -128,32 +128,34 @@ int X509_NAME_get_index_by_OBJ(X509_NAME *name, ASN1_OBJECT *obj,
X509_NAME_ENTRY *X509_NAME_get_entry(X509_NAME *name, int loc) X509_NAME_ENTRY *X509_NAME_get_entry(X509_NAME *name, int loc)
{ {
if ( (name == NULL) || (sk_num(name->entries) <= loc) || (loc < 0)) if(name == NULL || sk_X509_NAME_ENTRY_num(name->entries) <= loc
|| loc < 0)
return(NULL); return(NULL);
else else
return((X509_NAME_ENTRY *)sk_value(name->entries,loc)); return(sk_X509_NAME_ENTRY_value(name->entries,loc));
} }
X509_NAME_ENTRY *X509_NAME_delete_entry(X509_NAME *name, int loc) X509_NAME_ENTRY *X509_NAME_delete_entry(X509_NAME *name, int loc)
{ {
X509_NAME_ENTRY *ret; X509_NAME_ENTRY *ret;
int i,n,set_prev,set_next; int i,n,set_prev,set_next;
STACK *sk; STACK_OF(X509_NAME_ENTRY) *sk;
if ((name == NULL) || (sk_num(name->entries) <= loc) || (loc < 0)) if (name == NULL || sk_X509_NAME_ENTRY_num(name->entries) <= loc
|| loc < 0)
return(NULL); return(NULL);
sk=name->entries; sk=name->entries;
ret=(X509_NAME_ENTRY *)sk_delete(sk,loc); ret=sk_X509_NAME_ENTRY_delete(sk,loc);
n=sk_num(sk); n=sk_X509_NAME_ENTRY_num(sk);
name->modified=1; name->modified=1;
if (loc == n) return(ret); if (loc == n) return(ret);
/* else we need to fixup the set field */ /* else we need to fixup the set field */
if (loc != 0) if (loc != 0)
set_prev=((X509_NAME_ENTRY *)sk_value(sk,loc-1))->set; set_prev=(sk_X509_NAME_ENTRY_value(sk,loc-1))->set;
else else
set_prev=ret->set-1; set_prev=ret->set-1;
set_next=((X509_NAME_ENTRY *)sk_value(sk,loc))->set; set_next=sk_X509_NAME_ENTRY_value(sk,loc)->set;
/* set_prev is the previous set /* set_prev is the previous set
* set is the current set * set is the current set
@ -165,7 +167,7 @@ X509_NAME_ENTRY *X509_NAME_delete_entry(X509_NAME *name, int loc)
* re-number down by 1 */ * re-number down by 1 */
if (set_prev+1 < set_next) if (set_prev+1 < set_next)
for (i=loc; i<n; i++) for (i=loc; i<n; i++)
((X509_NAME_ENTRY *)sk_value(sk,i))->set--; sk_X509_NAME_ENTRY_value(sk,i)->set--;
return(ret); return(ret);
} }
@ -176,11 +178,11 @@ int X509_NAME_add_entry(X509_NAME *name, X509_NAME_ENTRY *ne, int loc,
{ {
X509_NAME_ENTRY *new_name=NULL; X509_NAME_ENTRY *new_name=NULL;
int n,i,inc; int n,i,inc;
STACK *sk; STACK_OF(X509_NAME_ENTRY) *sk;
if (name == NULL) return(0); if (name == NULL) return(0);
sk=name->entries; sk=name->entries;
n=sk_num(sk); n=sk_X509_NAME_ENTRY_num(sk);
if (loc > n) loc=n; if (loc > n) loc=n;
else if (loc < 0) loc=n; else if (loc < 0) loc=n;
@ -195,7 +197,7 @@ int X509_NAME_add_entry(X509_NAME *name, X509_NAME_ENTRY *ne, int loc,
} }
else else
{ {
set=((X509_NAME_ENTRY *)sk_value(sk,loc-1))->set; set=sk_X509_NAME_ENTRY_value(sk,loc-1)->set;
inc=0; inc=0;
} }
} }
@ -204,29 +206,28 @@ int X509_NAME_add_entry(X509_NAME *name, X509_NAME_ENTRY *ne, int loc,
if (loc >= n) if (loc >= n)
{ {
if (loc != 0) if (loc != 0)
set=((X509_NAME_ENTRY *) set=sk_X509_NAME_ENTRY_value(sk,loc-1)->set+1;
sk_value(sk,loc-1))->set+1;
else else
set=0; set=0;
} }
else else
set=((X509_NAME_ENTRY *)sk_value(sk,loc))->set; set=sk_X509_NAME_ENTRY_value(sk,loc)->set;
inc=(set == 0)?1:0; inc=(set == 0)?1:0;
} }
if ((new_name=X509_NAME_ENTRY_dup(ne)) == NULL) if ((new_name=X509_NAME_ENTRY_dup(ne)) == NULL)
goto err; goto err;
new_name->set=set; new_name->set=set;
if (!sk_insert(sk,(char *)new_name,loc)) if (!sk_X509_NAME_ENTRY_insert(sk,new_name,loc))
{ {
X509err(X509_F_X509_NAME_ADD_ENTRY,ERR_R_MALLOC_FAILURE); X509err(X509_F_X509_NAME_ADD_ENTRY,ERR_R_MALLOC_FAILURE);
goto err; goto err;
} }
if (inc) if (inc)
{ {
n=sk_num(sk); n=sk_X509_NAME_ENTRY_num(sk);
for (i=loc+1; i<n; i++) for (i=loc+1; i<n; i++)
((X509_NAME_ENTRY *)sk_value(sk,i-1))->set+=1; sk_X509_NAME_ENTRY_value(sk,i-1)->set+=1;
} }
return(1); return(1);
err: err: