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

@ -84,16 +84,23 @@ static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int c
const ASN1_AUX *aux = it->funcs;
ASN1_aux_cb *asn1_cb;
int i;
if(!pval) return;
if((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval) return;
if(aux && aux->asn1_cb) asn1_cb = aux->asn1_cb;
else asn1_cb = 0;
if (!pval)
return;
if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
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:
if(it->templates) ASN1_template_free(pval, it->templates);
else ASN1_primitive_free(pval, it);
if (it->templates)
ASN1_template_free(pval, it->templates);
else
ASN1_primitive_free(pval, it);
break;
case ASN1_ITYPE_MSTRING:
@ -101,20 +108,26 @@ static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int c
break;
case ASN1_ITYPE_CHOICE:
if(asn1_cb) {
if (asn1_cb)
{
i = asn1_cb(ASN1_OP_FREE_PRE, pval, it);
if(i == 2) return;
if (i == 2)
return;
}
i = asn1_get_choice_selector(pval, it);
if(asn1_cb) asn1_cb(ASN1_OP_FREE_PRE, pval, it);
if((i >= 0) && (i < it->tcount)) {
if (asn1_cb)
asn1_cb(ASN1_OP_FREE_PRE, pval, it);
if ((i >= 0) && (i < it->tcount))
{
ASN1_VALUE **pchval;
tt = it->templates + i;
pchval = asn1_get_field_ptr(pval, tt);
ASN1_template_free(pchval, tt);
}
if(asn1_cb) asn1_cb(ASN1_OP_FREE_POST, pval, it);
if(!combine) {
if (asn1_cb)
asn1_cb(ASN1_OP_FREE_POST, pval, it);
if (!combine)
{
OPENSSL_free(*pval);
*pval = NULL;
}
@ -122,20 +135,25 @@ static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int c
case ASN1_ITYPE_COMPAT:
cf = it->funcs;
if(cf && cf->asn1_free) cf->asn1_free(*pval);
if (cf && cf->asn1_free)
cf->asn1_free(*pval);
break;
case ASN1_ITYPE_EXTERN:
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;
case ASN1_ITYPE_NDEF_SEQUENCE:
case ASN1_ITYPE_SEQUENCE:
if(asn1_do_lock(pval, -1, it) > 0) return;
if(asn1_cb) {
if (asn1_do_lock(pval, -1, it) > 0)
return;
if (asn1_cb)
{
i = asn1_cb(ASN1_OP_FREE_PRE, pval, it);
if(i == 2) return;
if (i == 2)
return;
}
asn1_enc_free(pval, it);
/* If we free up as normal we will invalidate any
@ -144,15 +162,19 @@ static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int c
* free up in reverse order.
*/
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;
seqtt = asn1_do_adb(pval, tt, 0);
if(!seqtt) continue;
if (!seqtt)
continue;
pseqval = asn1_get_field_ptr(pval, seqtt);
ASN1_template_free(pseqval, seqtt);
}
if(asn1_cb) asn1_cb(ASN1_OP_FREE_POST, pval, it);
if(!combine) {
if (asn1_cb)
asn1_cb(ASN1_OP_FREE_POST, pval, it);
if (!combine)
{
OPENSSL_free(*pval);
*pval = NULL;
}
@ -163,45 +185,61 @@ static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int c
void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
{
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;
for(i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
for (i = 0; i < sk_ASN1_VALUE_num(sk); i++)
{
ASN1_VALUE *vtmp;
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);
*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);
}
void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
{
int utype;
if(it) {
if (it)
{
const ASN1_PRIMITIVE_FUNCS *pf;
pf = it->funcs;
if(pf && pf->prim_free) {
if (pf && pf->prim_free)
{
pf->prim_free(pval, it);
return;
}
}
/* Special case: if 'it' is NULL free contents of ASN1_TYPE */
if(!it) {
if (!it)
{
ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
utype = typ->type;
pval = (ASN1_VALUE **)&typ->value.ptr;
if(!*pval) return;
} else if(it->itype == ASN1_ITYPE_MSTRING) {
if (!*pval)
return;
}
else if (it->itype == ASN1_ITYPE_MSTRING)
{
utype = -1;
if(!*pval) return;
} else {
if (!*pval)
return;
}
else
{
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:
ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
break;

View File

@ -3,7 +3,7 @@
* 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
* modification, are permitted provided that the following conditions
@ -64,7 +64,8 @@
#include <openssl/asn1t.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_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
@ -72,7 +73,8 @@ void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
{
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;
}
@ -83,7 +85,8 @@ int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
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_COMPAT_FUNCS *cf;
@ -92,20 +95,25 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int
ASN1_aux_cb *asn1_cb;
ASN1_VALUE **pseqval;
int i;
if(aux && aux->asn1_cb) asn1_cb = aux->asn1_cb;
else asn1_cb = 0;
if (aux && aux->asn1_cb)
asn1_cb = aux->asn1_cb;
else
asn1_cb = 0;
if (!combine) *pval = NULL;
#ifdef CRYPTO_MDEBUG
if(it->sname) CRYPTO_push_info(it->sname);
if (it->sname)
CRYPTO_push_info(it->sname);
#endif
switch(it->itype) {
switch(it->itype)
{
case ASN1_ITYPE_EXTERN:
ef = it->funcs;
if(ef && ef->asn1_ex_new) {
if (ef && ef->asn1_ex_new)
{
if (!ef->asn1_ex_new(pval, it))
goto memerr;
}
@ -115,18 +123,19 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int
cf = it->funcs;
if (cf && cf->asn1_new) {
*pval = cf->asn1_new();
if(!*pval) goto memerr;
if (!*pval)
goto memerr;
}
break;
case ASN1_ITYPE_PRIMITIVE:
if(it->templates) {
if (it->templates)
{
if (!ASN1_template_new(pval, it->templates))
goto memerr;
} else {
if(!ASN1_primitive_new(pval, it))
goto memerr;
}
else if (!ASN1_primitive_new(pval, it))
goto memerr;
break;
case ASN1_ITYPE_MSTRING:
@ -135,19 +144,25 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int
break;
case ASN1_ITYPE_CHOICE:
if(asn1_cb) {
if (asn1_cb)
{
i = asn1_cb(ASN1_OP_NEW_PRE, pval, it);
if(!i) goto auxerr;
if(i==2) {
if (!i)
goto auxerr;
if (i==2)
{
#ifdef CRYPTO_MDEBUG
if(it->sname) CRYPTO_pop_info();
if (it->sname)
CRYPTO_pop_info();
#endif
return 1;
}
}
if(!combine) {
if (!combine)
{
*pval = OPENSSL_malloc(it->size);
if(!*pval) goto memerr;
if (!*pval)
goto memerr;
memset(*pval, 0, it->size);
}
asn1_set_choice_selector(pval, -1, it);
@ -157,26 +172,34 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int
case ASN1_ITYPE_NDEF_SEQUENCE:
case ASN1_ITYPE_SEQUENCE:
if(asn1_cb) {
if (asn1_cb)
{
i = asn1_cb(ASN1_OP_NEW_PRE, pval, it);
if(!i) goto auxerr;
if(i==2) {
if (!i)
goto auxerr;
if (i==2)
{
#ifdef CRYPTO_MDEBUG
if(it->sname) CRYPTO_pop_info();
if (it->sname)
CRYPTO_pop_info();
#endif
return 1;
}
}
if(!combine) {
if (!combine)
{
*pval = OPENSSL_malloc(it->size);
if(!*pval) goto memerr;
if (!*pval)
goto memerr;
memset(*pval, 0, it->size);
asn1_do_lock(pval, 0, 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);
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))
goto auxerr;
@ -208,7 +231,8 @@ static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
{
const ASN1_EXTERN_FUNCS *ef;
switch(it->itype) {
switch(it->itype)
{
case ASN1_ITYPE_EXTERN:
ef = it->funcs;
@ -243,24 +267,29 @@ int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
{
const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
int ret;
if(tt->flags & ASN1_TFLG_OPTIONAL) {
if (tt->flags & ASN1_TFLG_OPTIONAL)
{
asn1_template_clear(pval, tt);
return 1;
}
/* If ANY DEFINED BY nothing to do */
if(tt->flags & ASN1_TFLG_ADB_MASK) {
if (tt->flags & ASN1_TFLG_ADB_MASK)
{
*pval = NULL;
return 1;
}
#ifdef CRYPTO_MDEBUG
if(tt->field_name) CRYPTO_push_info(tt->field_name);
if (tt->field_name)
CRYPTO_push_info(tt->field_name);
#endif
/* 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;
skval = sk_ASN1_VALUE_new_null();
if(!skval) {
if (!skval)
{
ASN1err(ASN1_F_ASN1_TEMPLATE_NEW, ERR_R_MALLOC_FAILURE);
ret = 0;
goto done;
@ -273,7 +302,8 @@ int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
done:
#ifdef CRYPTO_MDEBUG
if(it->sname) CRYPTO_pop_info();
if (it->sname)
CRYPTO_pop_info();
#endif
return ret;
}
@ -288,8 +318,8 @@ static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
}
/* NB: could probably combine most of the real XXX_new() behaviour and junk all the old
* functions.
/* NB: could probably combine most of the real XXX_new() behaviour and junk
* all the old functions.
*/
int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
@ -298,10 +328,16 @@ int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
int utype;
const ASN1_PRIMITIVE_FUNCS *pf;
pf = it->funcs;
if(pf && pf->prim_new) return pf->prim_new(pval, it);
if(!it || (it->itype == ASN1_ITYPE_MSTRING)) utype = -1;
else utype = it->utype;
switch(utype) {
if (pf && pf->prim_new)
return pf->prim_new(pval, it);
if (!it || (it->itype == ASN1_ITYPE_MSTRING))
utype = -1;
else
utype = it->utype;
switch(utype)
{
case V_ASN1_OBJECT:
*pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
return 1;
@ -319,7 +355,8 @@ int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
case V_ASN1_ANY:
typ = OPENSSL_malloc(sizeof(ASN1_TYPE));
if(!typ) return 0;
if (!typ)
return 0;
typ->value.ptr = NULL;
typ->type = -1;
*pval = (ASN1_VALUE *)typ;
@ -329,7 +366,8 @@ int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
*pval = (ASN1_VALUE *)ASN1_STRING_type_new(utype);
break;
}
if(*pval) return 1;
if (*pval)
return 1;
return 0;
}
@ -338,15 +376,18 @@ void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
int utype;
const ASN1_PRIMITIVE_FUNCS *pf;
pf = it->funcs;
if(pf) {
if (pf)
{
if (pf->prim_clear)
pf->prim_clear(pval, it);
else
*pval = NULL;
return;
}
if(!it || (it->itype == ASN1_ITYPE_MSTRING)) utype = -1;
else utype = it->utype;
if (!it || (it->itype == ASN1_ITYPE_MSTRING))
utype = -1;
else
utype = it->utype;
if (utype == V_ASN1_BOOLEAN)
*(ASN1_BOOLEAN *)pval = it->size;
else *pval = NULL;

View File

@ -3,7 +3,7 @@
* 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
* modification, are permitted provided that the following conditions
@ -103,11 +103,14 @@ int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
const ASN1_AUX *aux;
int *lck, ret;
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;
if(!aux || !(aux->flags & ASN1_AFLG_REFCOUNT)) return 0;
if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT))
return 0;
lck = offset2ptr(*pval, aux->ref_offset);
if(op == 0) {
if (op == 0)
{
*lck = 1;
return 1;
}
@ -125,9 +128,11 @@ int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
{
const ASN1_AUX *aux;
if(!pval || !*pval) return NULL;
if (!pval || !*pval)
return NULL;
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);
}
@ -135,7 +140,8 @@ void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
{
ASN1_ENCODING *enc;
enc = asn1_get_enc_ptr(pval, it);
if(enc) {
if (enc)
{
enc->enc = NULL;
enc->len = 0;
enc->modified = 1;
@ -146,23 +152,29 @@ void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
{
ASN1_ENCODING *enc;
enc = asn1_get_enc_ptr(pval, it);
if(enc) {
if(enc->enc) OPENSSL_free(enc->enc);
if (enc)
{
if (enc->enc)
OPENSSL_free(enc->enc);
enc->enc = NULL;
enc->len = 0;
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;
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);
if(!enc->enc) return 0;
if (!enc->enc)
return 0;
memcpy(enc->enc, in, inlen);
enc->len = inlen;
enc->modified = 0;
@ -170,16 +182,20 @@ int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen, const A
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;
enc = asn1_get_enc_ptr(pval, it);
if(!enc || enc->modified) return 0;
if(out) {
if (!enc || enc->modified)
return 0;
if (out)
{
memcpy(*out, enc->enc, enc->len);
*out += enc->len;
}
if(len) *len = enc->len;
if (len)
*len = enc->len;
return 1;
}
@ -187,7 +203,8 @@ int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, const ASN
ASN1_VALUE ** asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
{
ASN1_VALUE **pvaltmp;
if(tt->flags & ASN1_TFLG_COMBINE) return pval;
if (tt->flags & ASN1_TFLG_COMBINE)
return pval;
pvaltmp = offset2ptr(*pval, tt->offset);
/* NOTE for BOOLEAN types the field is just a plain
* int so we can't return int **, so settle for
@ -200,14 +217,16 @@ ASN1_VALUE ** asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
* 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_TABLE *atbl;
long selector;
ASN1_VALUE **sfld;
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 */
adb = ASN1_ADB_ptr(tt->item);
@ -216,8 +235,10 @@ const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int
sfld = offset2ptr(*pval, adb->offset);
/* Check if NULL */
if(!sfld) {
if(!adb->null_tt) goto err;
if (!sfld)
{
if (!adb->null_tt)
goto err;
return adb->null_tt;
}
@ -239,16 +260,20 @@ const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int
*/
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 */
/* No match, return default type */
if(!adb->default_tt) goto err;
if (!adb->default_tt)
goto err;
return adb->default_tt;
err:
/* 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;
}