More ASN1 reformat/tidy.

This commit is contained in:
Dr. Stephen Henson 2004-04-25 12:46:39 +00:00
parent 8845420f4e
commit f3f52d7f45
5 changed files with 308 additions and 204 deletions

View File

@ -312,7 +312,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
/* CHOICE type, try each possibility in turn */ /* CHOICE type, try each possibility in turn */
pchval = NULL; pchval = NULL;
p = *in; p = *in;
for(i = 0, tt=it->templates; i < it->tcount; i++, tt++) for (i = 0, tt=it->templates; i < it->tcount; i++, tt++)
{ {
pchptr = asn1_get_field_ptr(pval, tt); pchptr = asn1_get_field_ptr(pval, tt);
/* We mark field as OPTIONAL so its absence /* We mark field as OPTIONAL so its absence

View File

@ -218,7 +218,7 @@ int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it)) if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it))
return 0; return 0;
/* First work out sequence content length */ /* First work out sequence content length */
for(i = 0, tt = it->templates; i < it->tcount; tt++, i++) for (i = 0, tt = it->templates; i < it->tcount; tt++, i++)
{ {
const ASN1_TEMPLATE *seqtt; const ASN1_TEMPLATE *seqtt;
ASN1_VALUE **pseqval; ASN1_VALUE **pseqval;
@ -236,7 +236,7 @@ int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
return seqlen; return seqlen;
/* Output SEQUENCE header */ /* Output SEQUENCE header */
ASN1_put_object(out, ndef, seqcontlen, tag, aclass); ASN1_put_object(out, ndef, seqcontlen, tag, aclass);
for(i = 0, tt = it->templates; i < it->tcount; tt++, i++) for (i = 0, tt = it->templates; i < it->tcount; tt++, i++)
{ {
const ASN1_TEMPLATE *seqtt; const ASN1_TEMPLATE *seqtt;
ASN1_VALUE **pseqval; ASN1_VALUE **pseqval;
@ -351,7 +351,7 @@ static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
/* Determine total length of items */ /* Determine total length of items */
skcontlen = 0; skcontlen = 0;
for(i = 0; i < sk_ASN1_VALUE_num(sk); i++) for (i = 0; i < sk_ASN1_VALUE_num(sk); i++)
{ {
skitem = sk_ASN1_VALUE_value(sk, i); skitem = sk_ASN1_VALUE_value(sk, i);
skcontlen += ASN1_item_ex_i2d(&skitem, NULL, skcontlen += ASN1_item_ex_i2d(&skitem, NULL,
@ -460,7 +460,7 @@ static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
/* If not sorting just output each item */ /* If not sorting just output each item */
if (!do_sort) if (!do_sort)
{ {
for(i = 0; i < sk_ASN1_VALUE_num(sk); i++) for (i = 0; i < sk_ASN1_VALUE_num(sk); i++)
{ {
skitem = sk_ASN1_VALUE_value(sk, i); skitem = sk_ASN1_VALUE_value(sk, i);
ASN1_item_ex_i2d(&skitem, out, item, -1, iclass); ASN1_item_ex_i2d(&skitem, out, item, -1, iclass);
@ -470,7 +470,7 @@ static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
p = tmpdat; p = tmpdat;
/* Doing sort: build up a list of each member's DER encoding */ /* Doing sort: build up a list of each member's DER encoding */
for(i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++)
{ {
skitem = sk_ASN1_VALUE_value(sk, i); skitem = sk_ASN1_VALUE_value(sk, i);
tder->data = p; tder->data = p;
@ -482,7 +482,7 @@ static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp); qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp);
/* Output sorted DER encoding */ /* Output sorted DER encoding */
p = *out; p = *out;
for(i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++)
{ {
memcpy(p, tder->data, tder->length); memcpy(p, tder->data, tder->length);
p += tder->length; p += tder->length;
@ -491,7 +491,7 @@ static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
/* If do_sort is 2 then reorder the STACK */ /* If do_sort is 2 then reorder the STACK */
if (do_sort == 2) if (do_sort == 2)
{ {
for(i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk);
i++, tder++) i++, tder++)
sk_ASN1_VALUE_set(sk, i, tder->field); sk_ASN1_VALUE_set(sk, i, tder->field);
} }

View File

@ -67,33 +67,40 @@ static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int c
/* Free up an ASN1 structure */ /* Free up an ASN1 structure */
void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it) void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it)
{ {
asn1_item_combine_free(&val, it, 0); asn1_item_combine_free(&val, it, 0);
} }
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
{ {
asn1_item_combine_free(pval, it, 0); asn1_item_combine_free(pval, it, 0);
} }
static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine) static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
{ {
const ASN1_TEMPLATE *tt = NULL, *seqtt; const ASN1_TEMPLATE *tt = NULL, *seqtt;
const ASN1_EXTERN_FUNCS *ef; const ASN1_EXTERN_FUNCS *ef;
const ASN1_COMPAT_FUNCS *cf; const ASN1_COMPAT_FUNCS *cf;
const ASN1_AUX *aux = it->funcs; const ASN1_AUX *aux = it->funcs;
ASN1_aux_cb *asn1_cb; ASN1_aux_cb *asn1_cb;
int i; int i;
if(!pval) return; if (!pval)
if((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval) return; return;
if(aux && aux->asn1_cb) asn1_cb = aux->asn1_cb; if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
else asn1_cb = 0; return;
if (aux && aux->asn1_cb)
asn1_cb = aux->asn1_cb;
else
asn1_cb = 0;
switch(it->itype) { switch(it->itype)
{
case ASN1_ITYPE_PRIMITIVE: case ASN1_ITYPE_PRIMITIVE:
if(it->templates) ASN1_template_free(pval, it->templates); if (it->templates)
else ASN1_primitive_free(pval, it); ASN1_template_free(pval, it->templates);
else
ASN1_primitive_free(pval, it);
break; break;
case ASN1_ITYPE_MSTRING: case ASN1_ITYPE_MSTRING:
@ -101,42 +108,53 @@ static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int c
break; break;
case ASN1_ITYPE_CHOICE: case ASN1_ITYPE_CHOICE:
if(asn1_cb) { if (asn1_cb)
{
i = asn1_cb(ASN1_OP_FREE_PRE, pval, it); i = asn1_cb(ASN1_OP_FREE_PRE, pval, it);
if(i == 2) return; if (i == 2)
} return;
}
i = asn1_get_choice_selector(pval, it); i = asn1_get_choice_selector(pval, it);
if(asn1_cb) asn1_cb(ASN1_OP_FREE_PRE, pval, it); if (asn1_cb)
if((i >= 0) && (i < it->tcount)) { asn1_cb(ASN1_OP_FREE_PRE, pval, it);
if ((i >= 0) && (i < it->tcount))
{
ASN1_VALUE **pchval; ASN1_VALUE **pchval;
tt = it->templates + i; tt = it->templates + i;
pchval = asn1_get_field_ptr(pval, tt); pchval = asn1_get_field_ptr(pval, tt);
ASN1_template_free(pchval, tt); ASN1_template_free(pchval, tt);
} }
if(asn1_cb) asn1_cb(ASN1_OP_FREE_POST, pval, it); if (asn1_cb)
if(!combine) { asn1_cb(ASN1_OP_FREE_POST, pval, it);
if (!combine)
{
OPENSSL_free(*pval); OPENSSL_free(*pval);
*pval = NULL; *pval = NULL;
} }
break; break;
case ASN1_ITYPE_COMPAT: case ASN1_ITYPE_COMPAT:
cf = it->funcs; cf = it->funcs;
if(cf && cf->asn1_free) cf->asn1_free(*pval); if (cf && cf->asn1_free)
cf->asn1_free(*pval);
break; break;
case ASN1_ITYPE_EXTERN: case ASN1_ITYPE_EXTERN:
ef = it->funcs; ef = it->funcs;
if(ef && ef->asn1_ex_free) ef->asn1_ex_free(pval, it); if (ef && ef->asn1_ex_free)
ef->asn1_ex_free(pval, it);
break; break;
case ASN1_ITYPE_NDEF_SEQUENCE: case ASN1_ITYPE_NDEF_SEQUENCE:
case ASN1_ITYPE_SEQUENCE: case ASN1_ITYPE_SEQUENCE:
if(asn1_do_lock(pval, -1, it) > 0) return; if (asn1_do_lock(pval, -1, it) > 0)
if(asn1_cb) { return;
if (asn1_cb)
{
i = asn1_cb(ASN1_OP_FREE_PRE, pval, it); i = asn1_cb(ASN1_OP_FREE_PRE, pval, it);
if(i == 2) return; if (i == 2)
} return;
}
asn1_enc_free(pval, it); asn1_enc_free(pval, it);
/* If we free up as normal we will invalidate any /* If we free up as normal we will invalidate any
* ANY DEFINED BY field and we wont be able to * ANY DEFINED BY field and we wont be able to
@ -144,64 +162,84 @@ static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int c
* free up in reverse order. * free up in reverse order.
*/ */
tt = it->templates + it->tcount - 1; tt = it->templates + it->tcount - 1;
for(i = 0; i < it->tcount; tt--, i++) { for (i = 0; i < it->tcount; tt--, i++)
{
ASN1_VALUE **pseqval; ASN1_VALUE **pseqval;
seqtt = asn1_do_adb(pval, tt, 0); seqtt = asn1_do_adb(pval, tt, 0);
if(!seqtt) continue; if (!seqtt)
continue;
pseqval = asn1_get_field_ptr(pval, seqtt); pseqval = asn1_get_field_ptr(pval, seqtt);
ASN1_template_free(pseqval, seqtt); ASN1_template_free(pseqval, seqtt);
} }
if(asn1_cb) asn1_cb(ASN1_OP_FREE_POST, pval, it); if (asn1_cb)
if(!combine) { asn1_cb(ASN1_OP_FREE_POST, pval, it);
if (!combine)
{
OPENSSL_free(*pval); OPENSSL_free(*pval);
*pval = NULL; *pval = NULL;
} }
break; break;
}
} }
}
void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
{ {
int i; int i;
if(tt->flags & ASN1_TFLG_SK_MASK) { if (tt->flags & ASN1_TFLG_SK_MASK)
{
STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval; STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
for(i = 0; i < sk_ASN1_VALUE_num(sk); i++) { for (i = 0; i < sk_ASN1_VALUE_num(sk); i++)
{
ASN1_VALUE *vtmp; ASN1_VALUE *vtmp;
vtmp = sk_ASN1_VALUE_value(sk, i); vtmp = sk_ASN1_VALUE_value(sk, i);
asn1_item_combine_free(&vtmp, ASN1_ITEM_ptr(tt->item), 0); asn1_item_combine_free(&vtmp, ASN1_ITEM_ptr(tt->item),
} 0);
}
sk_ASN1_VALUE_free(sk); sk_ASN1_VALUE_free(sk);
*pval = NULL; *pval = NULL;
} else asn1_item_combine_free(pval, ASN1_ITEM_ptr(tt->item), }
else
asn1_item_combine_free(pval, ASN1_ITEM_ptr(tt->item),
tt->flags & ASN1_TFLG_COMBINE); tt->flags & ASN1_TFLG_COMBINE);
} }
void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it) void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
{ {
int utype; int utype;
if(it) { if (it)
{
const ASN1_PRIMITIVE_FUNCS *pf; const ASN1_PRIMITIVE_FUNCS *pf;
pf = it->funcs; pf = it->funcs;
if(pf && pf->prim_free) { if (pf && pf->prim_free)
{
pf->prim_free(pval, it); pf->prim_free(pval, it);
return; return;
}
} }
}
/* Special case: if 'it' is NULL free contents of ASN1_TYPE */ /* Special case: if 'it' is NULL free contents of ASN1_TYPE */
if(!it) { if (!it)
{
ASN1_TYPE *typ = (ASN1_TYPE *)*pval; ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
utype = typ->type; utype = typ->type;
pval = (ASN1_VALUE **)&typ->value.ptr; pval = (ASN1_VALUE **)&typ->value.ptr;
if(!*pval) return; if (!*pval)
} else if(it->itype == ASN1_ITYPE_MSTRING) { return;
}
else if (it->itype == ASN1_ITYPE_MSTRING)
{
utype = -1; utype = -1;
if(!*pval) return; if (!*pval)
} else { return;
}
else
{
utype = it->utype; utype = it->utype;
if((utype != V_ASN1_BOOLEAN) && !*pval) return; if ((utype != V_ASN1_BOOLEAN) && !*pval)
} return;
}
switch(utype) { switch(utype)
{
case V_ASN1_OBJECT: case V_ASN1_OBJECT:
ASN1_OBJECT_free((ASN1_OBJECT *)*pval); ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
break; break;
@ -225,6 +263,6 @@ void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
ASN1_STRING_free((ASN1_STRING *)*pval); ASN1_STRING_free((ASN1_STRING *)*pval);
*pval = NULL; *pval = NULL;
break; break;
} }
*pval = NULL; *pval = NULL;
} }

View File

@ -3,7 +3,7 @@
* project 2000. * project 2000.
*/ */
/* ==================================================================== /* ====================================================================
* Copyright (c) 2000 The OpenSSL Project. All rights reserved. * Copyright (c) 2000-2004 The OpenSSL Project. All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -64,27 +64,30 @@
#include <openssl/asn1t.h> #include <openssl/asn1t.h>
#include <string.h> #include <string.h>
static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine); static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
int combine);
static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it); static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt); static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it); void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it) ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
{ {
ASN1_VALUE *ret = NULL; ASN1_VALUE *ret = NULL;
if(ASN1_item_ex_new(&ret, it) > 0) return ret; if (ASN1_item_ex_new(&ret, it) > 0)
return ret;
return NULL; return NULL;
} }
/* Allocate an ASN1 structure */ /* Allocate an ASN1 structure */
int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
{ {
return asn1_item_ex_combine_new(pval, it, 0); return asn1_item_ex_combine_new(pval, it, 0);
} }
static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine) static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
{ int combine)
{
const ASN1_TEMPLATE *tt = NULL; const ASN1_TEMPLATE *tt = NULL;
const ASN1_COMPAT_FUNCS *cf; const ASN1_COMPAT_FUNCS *cf;
const ASN1_EXTERN_FUNCS *ef; const ASN1_EXTERN_FUNCS *ef;
@ -92,105 +95,125 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int
ASN1_aux_cb *asn1_cb; ASN1_aux_cb *asn1_cb;
ASN1_VALUE **pseqval; ASN1_VALUE **pseqval;
int i; int i;
if(aux && aux->asn1_cb) asn1_cb = aux->asn1_cb; if (aux && aux->asn1_cb)
else asn1_cb = 0; asn1_cb = aux->asn1_cb;
else
asn1_cb = 0;
if(!combine) *pval = NULL; if (!combine) *pval = NULL;
#ifdef CRYPTO_MDEBUG #ifdef CRYPTO_MDEBUG
if(it->sname) CRYPTO_push_info(it->sname); if (it->sname)
CRYPTO_push_info(it->sname);
#endif #endif
switch(it->itype) { switch(it->itype)
{
case ASN1_ITYPE_EXTERN: case ASN1_ITYPE_EXTERN:
ef = it->funcs; ef = it->funcs;
if(ef && ef->asn1_ex_new) { if (ef && ef->asn1_ex_new)
if(!ef->asn1_ex_new(pval, it)) {
if (!ef->asn1_ex_new(pval, it))
goto memerr; goto memerr;
} }
break; break;
case ASN1_ITYPE_COMPAT: case ASN1_ITYPE_COMPAT:
cf = it->funcs; cf = it->funcs;
if(cf && cf->asn1_new) { if (cf && cf->asn1_new) {
*pval = cf->asn1_new(); *pval = cf->asn1_new();
if(!*pval) goto memerr; if (!*pval)
goto memerr;
} }
break; break;
case ASN1_ITYPE_PRIMITIVE: case ASN1_ITYPE_PRIMITIVE:
if(it->templates) { if (it->templates)
if(!ASN1_template_new(pval, it->templates)) {
if (!ASN1_template_new(pval, it->templates))
goto memerr; goto memerr;
} else { }
if(!ASN1_primitive_new(pval, it)) else if (!ASN1_primitive_new(pval, it))
goto memerr; goto memerr;
}
break; break;
case ASN1_ITYPE_MSTRING: case ASN1_ITYPE_MSTRING:
if(!ASN1_primitive_new(pval, it)) if (!ASN1_primitive_new(pval, it))
goto memerr; goto memerr;
break; break;
case ASN1_ITYPE_CHOICE: case ASN1_ITYPE_CHOICE:
if(asn1_cb) { if (asn1_cb)
{
i = asn1_cb(ASN1_OP_NEW_PRE, pval, it); i = asn1_cb(ASN1_OP_NEW_PRE, pval, it);
if(!i) goto auxerr; if (!i)
if(i==2) { goto auxerr;
if (i==2)
{
#ifdef CRYPTO_MDEBUG #ifdef CRYPTO_MDEBUG
if(it->sname) CRYPTO_pop_info(); if (it->sname)
CRYPTO_pop_info();
#endif #endif
return 1; return 1;
}
} }
} if (!combine)
if(!combine) { {
*pval = OPENSSL_malloc(it->size); *pval = OPENSSL_malloc(it->size);
if(!*pval) goto memerr; if (!*pval)
goto memerr;
memset(*pval, 0, it->size); memset(*pval, 0, it->size);
} }
asn1_set_choice_selector(pval, -1, it); asn1_set_choice_selector(pval, -1, it);
if(asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it)) if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it))
goto auxerr; goto auxerr;
break; break;
case ASN1_ITYPE_NDEF_SEQUENCE: case ASN1_ITYPE_NDEF_SEQUENCE:
case ASN1_ITYPE_SEQUENCE: case ASN1_ITYPE_SEQUENCE:
if(asn1_cb) { if (asn1_cb)
{
i = asn1_cb(ASN1_OP_NEW_PRE, pval, it); i = asn1_cb(ASN1_OP_NEW_PRE, pval, it);
if(!i) goto auxerr; if (!i)
if(i==2) { goto auxerr;
if (i==2)
{
#ifdef CRYPTO_MDEBUG #ifdef CRYPTO_MDEBUG
if(it->sname) CRYPTO_pop_info(); if (it->sname)
CRYPTO_pop_info();
#endif #endif
return 1; return 1;
}
} }
} if (!combine)
if(!combine) { {
*pval = OPENSSL_malloc(it->size); *pval = OPENSSL_malloc(it->size);
if(!*pval) goto memerr; if (!*pval)
goto memerr;
memset(*pval, 0, it->size); memset(*pval, 0, it->size);
asn1_do_lock(pval, 0, it); asn1_do_lock(pval, 0, it);
asn1_enc_init(pval, it); asn1_enc_init(pval, it);
} }
for(i = 0, tt = it->templates; i < it->tcount; tt++, i++) { for (i = 0, tt = it->templates; i < it->tcount; tt++, i++)
{
pseqval = asn1_get_field_ptr(pval, tt); pseqval = asn1_get_field_ptr(pval, tt);
if(!ASN1_template_new(pseqval, tt)) goto memerr; if (!ASN1_template_new(pseqval, tt))
} goto memerr;
if(asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it)) }
if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it))
goto auxerr; goto auxerr;
break; break;
} }
#ifdef CRYPTO_MDEBUG #ifdef CRYPTO_MDEBUG
if(it->sname) CRYPTO_pop_info(); if (it->sname) CRYPTO_pop_info();
#endif #endif
return 1; return 1;
memerr: memerr:
ASN1err(ASN1_F_ASN1_ITEM_NEW, ERR_R_MALLOC_FAILURE); ASN1err(ASN1_F_ASN1_ITEM_NEW, ERR_R_MALLOC_FAILURE);
#ifdef CRYPTO_MDEBUG #ifdef CRYPTO_MDEBUG
if(it->sname) CRYPTO_pop_info(); if (it->sname) CRYPTO_pop_info();
#endif #endif
return 0; return 0;
@ -198,28 +221,29 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int
ASN1err(ASN1_F_ASN1_ITEM_NEW, ASN1_R_AUX_ERROR); ASN1err(ASN1_F_ASN1_ITEM_NEW, ASN1_R_AUX_ERROR);
ASN1_item_ex_free(pval, it); ASN1_item_ex_free(pval, it);
#ifdef CRYPTO_MDEBUG #ifdef CRYPTO_MDEBUG
if(it->sname) CRYPTO_pop_info(); if (it->sname) CRYPTO_pop_info();
#endif #endif
return 0; return 0;
} }
static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
{ {
const ASN1_EXTERN_FUNCS *ef; const ASN1_EXTERN_FUNCS *ef;
switch(it->itype) { switch(it->itype)
{
case ASN1_ITYPE_EXTERN: case ASN1_ITYPE_EXTERN:
ef = it->funcs; ef = it->funcs;
if(ef && ef->asn1_ex_clear) if (ef && ef->asn1_ex_clear)
ef->asn1_ex_clear(pval, it); ef->asn1_ex_clear(pval, it);
else *pval = NULL; else *pval = NULL;
break; break;
case ASN1_ITYPE_PRIMITIVE: case ASN1_ITYPE_PRIMITIVE:
if(it->templates) if (it->templates)
asn1_template_clear(pval, it->templates); asn1_template_clear(pval, it->templates);
else else
asn1_primitive_clear(pval, it); asn1_primitive_clear(pval, it);
@ -235,73 +259,85 @@ static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
case ASN1_ITYPE_NDEF_SEQUENCE: case ASN1_ITYPE_NDEF_SEQUENCE:
*pval = NULL; *pval = NULL;
break; break;
}
} }
}
int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
{ {
const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item); const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
int ret; int ret;
if(tt->flags & ASN1_TFLG_OPTIONAL) { if (tt->flags & ASN1_TFLG_OPTIONAL)
{
asn1_template_clear(pval, tt); asn1_template_clear(pval, tt);
return 1; return 1;
} }
/* If ANY DEFINED BY nothing to do */ /* If ANY DEFINED BY nothing to do */
if(tt->flags & ASN1_TFLG_ADB_MASK) { if (tt->flags & ASN1_TFLG_ADB_MASK)
{
*pval = NULL; *pval = NULL;
return 1; return 1;
} }
#ifdef CRYPTO_MDEBUG #ifdef CRYPTO_MDEBUG
if(tt->field_name) CRYPTO_push_info(tt->field_name); if (tt->field_name)
CRYPTO_push_info(tt->field_name);
#endif #endif
/* If SET OF or SEQUENCE OF, its a STACK */ /* If SET OF or SEQUENCE OF, its a STACK */
if(tt->flags & ASN1_TFLG_SK_MASK) { if (tt->flags & ASN1_TFLG_SK_MASK)
{
STACK_OF(ASN1_VALUE) *skval; STACK_OF(ASN1_VALUE) *skval;
skval = sk_ASN1_VALUE_new_null(); skval = sk_ASN1_VALUE_new_null();
if(!skval) { if (!skval)
{
ASN1err(ASN1_F_ASN1_TEMPLATE_NEW, ERR_R_MALLOC_FAILURE); ASN1err(ASN1_F_ASN1_TEMPLATE_NEW, ERR_R_MALLOC_FAILURE);
ret = 0; ret = 0;
goto done; goto done;
} }
*pval = (ASN1_VALUE *)skval; *pval = (ASN1_VALUE *)skval;
ret = 1; ret = 1;
goto done; goto done;
} }
/* Otherwise pass it back to the item routine */ /* Otherwise pass it back to the item routine */
ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE); ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
done: done:
#ifdef CRYPTO_MDEBUG #ifdef CRYPTO_MDEBUG
if(it->sname) CRYPTO_pop_info(); if (it->sname)
CRYPTO_pop_info();
#endif #endif
return ret; return ret;
} }
static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
{ {
/* If ADB or STACK just NULL the field */ /* If ADB or STACK just NULL the field */
if(tt->flags & (ASN1_TFLG_ADB_MASK|ASN1_TFLG_SK_MASK)) if (tt->flags & (ASN1_TFLG_ADB_MASK|ASN1_TFLG_SK_MASK))
*pval = NULL; *pval = NULL;
else else
asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item)); asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
} }
/* NB: could probably combine most of the real XXX_new() behaviour and junk all the old /* NB: could probably combine most of the real XXX_new() behaviour and junk
* functions. * all the old functions.
*/ */
int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it) int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
{ {
ASN1_TYPE *typ; ASN1_TYPE *typ;
int utype; int utype;
const ASN1_PRIMITIVE_FUNCS *pf; const ASN1_PRIMITIVE_FUNCS *pf;
pf = it->funcs; pf = it->funcs;
if(pf && pf->prim_new) return pf->prim_new(pval, it);
if(!it || (it->itype == ASN1_ITYPE_MSTRING)) utype = -1; if (pf && pf->prim_new)
else utype = it->utype; return pf->prim_new(pval, it);
switch(utype) {
if (!it || (it->itype == ASN1_ITYPE_MSTRING))
utype = -1;
else
utype = it->utype;
switch(utype)
{
case V_ASN1_OBJECT: case V_ASN1_OBJECT:
*pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef); *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
return 1; return 1;
@ -319,7 +355,8 @@ int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
case V_ASN1_ANY: case V_ASN1_ANY:
typ = OPENSSL_malloc(sizeof(ASN1_TYPE)); typ = OPENSSL_malloc(sizeof(ASN1_TYPE));
if(!typ) return 0; if (!typ)
return 0;
typ->value.ptr = NULL; typ->value.ptr = NULL;
typ->type = -1; typ->type = -1;
*pval = (ASN1_VALUE *)typ; *pval = (ASN1_VALUE *)typ;
@ -328,26 +365,30 @@ int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
default: default:
*pval = (ASN1_VALUE *)ASN1_STRING_type_new(utype); *pval = (ASN1_VALUE *)ASN1_STRING_type_new(utype);
break; break;
} }
if(*pval) return 1; if (*pval)
return 1;
return 0; return 0;
} }
void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
{ {
int utype; int utype;
const ASN1_PRIMITIVE_FUNCS *pf; const ASN1_PRIMITIVE_FUNCS *pf;
pf = it->funcs; pf = it->funcs;
if(pf) { if (pf)
if(pf->prim_clear) {
if (pf->prim_clear)
pf->prim_clear(pval, it); pf->prim_clear(pval, it);
else else
*pval = NULL; *pval = NULL;
return; return;
} }
if(!it || (it->itype == ASN1_ITYPE_MSTRING)) utype = -1; if (!it || (it->itype == ASN1_ITYPE_MSTRING))
else utype = it->utype; utype = -1;
if(utype == V_ASN1_BOOLEAN) else
utype = it->utype;
if (utype == V_ASN1_BOOLEAN)
*(ASN1_BOOLEAN *)pval = it->size; *(ASN1_BOOLEAN *)pval = it->size;
else *pval = NULL; else *pval = NULL;
} }

View File

@ -3,7 +3,7 @@
* project 2000. * project 2000.
*/ */
/* ==================================================================== /* ====================================================================
* Copyright (c) 2000 The OpenSSL Project. All rights reserved. * Copyright (c) 2000-2004 The OpenSSL Project. All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -74,23 +74,23 @@
*/ */
int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it) int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it)
{ {
int *sel = offset2ptr(*pval, it->utype); int *sel = offset2ptr(*pval, it->utype);
return *sel; return *sel;
} }
/* Given an ASN1_ITEM CHOICE type set /* Given an ASN1_ITEM CHOICE type set
* the selector value, return old value. * the selector value, return old value.
*/ */
int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it) int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it)
{ {
int *sel, ret; int *sel, ret;
sel = offset2ptr(*pval, it->utype); sel = offset2ptr(*pval, it->utype);
ret = *sel; ret = *sel;
*sel = value; *sel = value;
return ret; return ret;
} }
/* Do reference counting. The value 'op' decides what to do. /* Do reference counting. The value 'op' decides what to do.
* if it is +1 then the count is incremented. If op is 0 count is * if it is +1 then the count is incremented. If op is 0 count is
@ -99,115 +99,134 @@ int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it)
*/ */
int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it) int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
{ {
const ASN1_AUX *aux; const ASN1_AUX *aux;
int *lck, ret; int *lck, ret;
if((it->itype != ASN1_ITYPE_SEQUENCE) if ((it->itype != ASN1_ITYPE_SEQUENCE)
&& (it->itype != ASN1_ITYPE_NDEF_SEQUENCE)) return 0; && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
return 0;
aux = it->funcs; aux = it->funcs;
if(!aux || !(aux->flags & ASN1_AFLG_REFCOUNT)) return 0; if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT))
return 0;
lck = offset2ptr(*pval, aux->ref_offset); lck = offset2ptr(*pval, aux->ref_offset);
if(op == 0) { if (op == 0)
{
*lck = 1; *lck = 1;
return 1; return 1;
} }
ret = CRYPTO_add(lck, op, aux->ref_lock); ret = CRYPTO_add(lck, op, aux->ref_lock);
#ifdef REF_PRINT #ifdef REF_PRINT
fprintf(stderr, "%s: Reference Count: %d\n", it->sname, *lck); fprintf(stderr, "%s: Reference Count: %d\n", it->sname, *lck);
#endif #endif
#ifdef REF_CHECK #ifdef REF_CHECK
if(ret < 0) if (ret < 0)
fprintf(stderr, "%s, bad reference count\n", it->sname); fprintf(stderr, "%s, bad reference count\n", it->sname);
#endif #endif
return ret; return ret;
} }
static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it) static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
{ {
const ASN1_AUX *aux; const ASN1_AUX *aux;
if(!pval || !*pval) return NULL; if (!pval || !*pval)
return NULL;
aux = it->funcs; aux = it->funcs;
if(!aux || !(aux->flags & ASN1_AFLG_ENCODING)) return NULL; if (!aux || !(aux->flags & ASN1_AFLG_ENCODING))
return NULL;
return offset2ptr(*pval, aux->enc_offset); return offset2ptr(*pval, aux->enc_offset);
} }
void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it) void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
{ {
ASN1_ENCODING *enc; ASN1_ENCODING *enc;
enc = asn1_get_enc_ptr(pval, it); enc = asn1_get_enc_ptr(pval, it);
if(enc) { if (enc)
{
enc->enc = NULL; enc->enc = NULL;
enc->len = 0; enc->len = 0;
enc->modified = 1; enc->modified = 1;
}
} }
}
void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it) void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
{ {
ASN1_ENCODING *enc; ASN1_ENCODING *enc;
enc = asn1_get_enc_ptr(pval, it); enc = asn1_get_enc_ptr(pval, it);
if(enc) { if (enc)
if(enc->enc) OPENSSL_free(enc->enc); {
if (enc->enc)
OPENSSL_free(enc->enc);
enc->enc = NULL; enc->enc = NULL;
enc->len = 0; enc->len = 0;
enc->modified = 1; enc->modified = 1;
}
} }
}
int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen, const ASN1_ITEM *it) int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
{ const ASN1_ITEM *it)
{
ASN1_ENCODING *enc; ASN1_ENCODING *enc;
enc = asn1_get_enc_ptr(pval, it); enc = asn1_get_enc_ptr(pval, it);
if(!enc) return 1; if (!enc)
return 1;
if(enc->enc) OPENSSL_free(enc->enc); if (enc->enc)
OPENSSL_free(enc->enc);
enc->enc = OPENSSL_malloc(inlen); enc->enc = OPENSSL_malloc(inlen);
if(!enc->enc) return 0; if (!enc->enc)
return 0;
memcpy(enc->enc, in, inlen); memcpy(enc->enc, in, inlen);
enc->len = inlen; enc->len = inlen;
enc->modified = 0; enc->modified = 0;
return 1; return 1;
} }
int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, const ASN1_ITEM *it) int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
{ const ASN1_ITEM *it)
{
ASN1_ENCODING *enc; ASN1_ENCODING *enc;
enc = asn1_get_enc_ptr(pval, it); enc = asn1_get_enc_ptr(pval, it);
if(!enc || enc->modified) return 0; if (!enc || enc->modified)
if(out) { return 0;
if (out)
{
memcpy(*out, enc->enc, enc->len); memcpy(*out, enc->enc, enc->len);
*out += enc->len; *out += enc->len;
} }
if(len) *len = enc->len; if (len)
*len = enc->len;
return 1; return 1;
} }
/* Given an ASN1_TEMPLATE get a pointer to a field */ /* Given an ASN1_TEMPLATE get a pointer to a field */
ASN1_VALUE ** asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) ASN1_VALUE ** asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
{ {
ASN1_VALUE **pvaltmp; ASN1_VALUE **pvaltmp;
if(tt->flags & ASN1_TFLG_COMBINE) return pval; if (tt->flags & ASN1_TFLG_COMBINE)
return pval;
pvaltmp = offset2ptr(*pval, tt->offset); pvaltmp = offset2ptr(*pval, tt->offset);
/* NOTE for BOOLEAN types the field is just a plain /* NOTE for BOOLEAN types the field is just a plain
* int so we can't return int **, so settle for * int so we can't return int **, so settle for
* (int *). * (int *).
*/ */
return pvaltmp; return pvaltmp;
} }
/* Handle ANY DEFINED BY template, find the selector, look up /* Handle ANY DEFINED BY template, find the selector, look up
* the relevant ASN1_TEMPLATE in the table and return it. * the relevant ASN1_TEMPLATE in the table and return it.
*/ */
const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int nullerr) const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
{ int nullerr)
{
const ASN1_ADB *adb; const ASN1_ADB *adb;
const ASN1_ADB_TABLE *atbl; const ASN1_ADB_TABLE *atbl;
long selector; long selector;
ASN1_VALUE **sfld; ASN1_VALUE **sfld;
int i; int i;
if(!(tt->flags & ASN1_TFLG_ADB_MASK)) return tt; if (!(tt->flags & ASN1_TFLG_ADB_MASK))
return tt;
/* Else ANY DEFINED BY ... get the table */ /* Else ANY DEFINED BY ... get the table */
adb = ASN1_ADB_ptr(tt->item); adb = ASN1_ADB_ptr(tt->item);
@ -216,16 +235,18 @@ const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int
sfld = offset2ptr(*pval, adb->offset); sfld = offset2ptr(*pval, adb->offset);
/* Check if NULL */ /* Check if NULL */
if(!sfld) { if (!sfld)
if(!adb->null_tt) goto err; {
if (!adb->null_tt)
goto err;
return adb->null_tt; return adb->null_tt;
} }
/* Convert type to a long: /* Convert type to a long:
* NB: don't check for NID_undef here because it * NB: don't check for NID_undef here because it
* might be a legitimate value in the table * might be a legitimate value in the table
*/ */
if(tt->flags & ASN1_TFLG_ADB_OID) if (tt->flags & ASN1_TFLG_ADB_OID)
selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld); selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
else else
selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld); selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
@ -238,17 +259,21 @@ const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int
* linear search. * linear search.
*/ */
for(atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++) for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
if(atbl->value == selector) return &atbl->tt; if (atbl->value == selector)
return &atbl->tt;
/* FIXME: need to search application table too */ /* FIXME: need to search application table too */
/* No match, return default type */ /* No match, return default type */
if(!adb->default_tt) goto err; if (!adb->default_tt)
goto err;
return adb->default_tt; return adb->default_tt;
err: err:
/* FIXME: should log the value or OID of unsupported type */ /* FIXME: should log the value or OID of unsupported type */
if(nullerr) ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE); if (nullerr)
ASN1err(ASN1_F_ASN1_DO_ADB,
ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
return NULL; return NULL;
} }