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 */
pchval = NULL;
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);
/* 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))
return 0;
/* 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;
ASN1_VALUE **pseqval;
@ -236,7 +236,7 @@ int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
return seqlen;
/* Output SEQUENCE header */
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;
ASN1_VALUE **pseqval;
@ -351,7 +351,7 @@ static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
/* Determine total length of items */
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);
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 (!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);
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;
/* 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);
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);
/* Output sorted DER encoding */
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);
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 == 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++)
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 */
void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it)
{
{
asn1_item_combine_free(&val, it, 0);
}
}
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
{
{
asn1_item_combine_free(pval, it, 0);
}
}
static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
{
{
const ASN1_TEMPLATE *tt = NULL, *seqtt;
const ASN1_EXTERN_FUNCS *ef;
const ASN1_COMPAT_FUNCS *cf;
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,42 +108,53 @@ 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;
}
}
break;
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
* 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.
*/
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;
}
}
break;
}
}
}
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;
@ -225,6 +263,6 @@ void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
ASN1_STRING_free((ASN1_STRING *)*pval);
*pval = NULL;
break;
}
}
*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
@ -64,27 +64,30 @@
#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);
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;
}
}
/* Allocate an ASN1 structure */
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;
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_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;
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->asn1_ex_new(pval, it))
if (ef && ef->asn1_ex_new)
{
if (!ef->asn1_ex_new(pval, it))
goto memerr;
}
}
break;
case ASN1_ITYPE_COMPAT:
cf = it->funcs;
if(cf && cf->asn1_new) {
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(!ASN1_template_new(pval, it->templates))
if (it->templates)
{
if (!ASN1_template_new(pval, it->templates))
goto memerr;
} else {
if(!ASN1_primitive_new(pval, it))
}
else if (!ASN1_primitive_new(pval, it))
goto memerr;
}
break;
case ASN1_ITYPE_MSTRING:
if(!ASN1_primitive_new(pval, it))
if (!ASN1_primitive_new(pval, it))
goto memerr;
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);
if(asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it))
if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it))
goto auxerr;
break;
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_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it))
if (!ASN1_template_new(pseqval, tt))
goto memerr;
}
if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it))
goto auxerr;
break;
}
#ifdef CRYPTO_MDEBUG
if(it->sname) CRYPTO_pop_info();
if (it->sname) CRYPTO_pop_info();
#endif
return 1;
memerr:
ASN1err(ASN1_F_ASN1_ITEM_NEW, ERR_R_MALLOC_FAILURE);
#ifdef CRYPTO_MDEBUG
if(it->sname) CRYPTO_pop_info();
if (it->sname) CRYPTO_pop_info();
#endif
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);
ASN1_item_ex_free(pval, it);
#ifdef CRYPTO_MDEBUG
if(it->sname) CRYPTO_pop_info();
if (it->sname) CRYPTO_pop_info();
#endif
return 0;
}
}
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;
if(ef && ef->asn1_ex_clear)
if (ef && ef->asn1_ex_clear)
ef->asn1_ex_clear(pval, it);
else *pval = NULL;
break;
case ASN1_ITYPE_PRIMITIVE:
if(it->templates)
if (it->templates)
asn1_template_clear(pval, it->templates);
else
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:
*pval = NULL;
break;
}
}
}
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;
}
}
*pval = (ASN1_VALUE *)skval;
ret = 1;
goto done;
}
}
/* Otherwise pass it back to the item routine */
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;
}
}
static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
{
{
/* 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;
else
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
* 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)
{
{
ASN1_TYPE *typ;
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;
@ -328,26 +365,30 @@ int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
default:
*pval = (ASN1_VALUE *)ASN1_STRING_type_new(utype);
break;
}
if(*pval) return 1;
}
if (*pval)
return 1;
return 0;
}
}
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->prim_clear)
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(utype == V_ASN1_BOOLEAN)
}
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
@ -74,23 +74,23 @@
*/
int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it)
{
{
int *sel = offset2ptr(*pval, it->utype);
return *sel;
}
}
/* Given an ASN1_ITEM CHOICE type set
* the selector value, return old value.
*/
int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it)
{
{
int *sel, ret;
sel = offset2ptr(*pval, it->utype);
ret = *sel;
*sel = value;
return ret;
}
}
/* 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
@ -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)
{
{
const ASN1_AUX *aux;
int *lck, ret;
if((it->itype != ASN1_ITYPE_SEQUENCE)
&& (it->itype != ASN1_ITYPE_NDEF_SEQUENCE)) return 0;
if ((it->itype != ASN1_ITYPE_SEQUENCE)
&& (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;
}
}
ret = CRYPTO_add(lck, op, aux->ref_lock);
#ifdef REF_PRINT
fprintf(stderr, "%s: Reference Count: %d\n", it->sname, *lck);
#endif
#ifdef REF_CHECK
if(ret < 0)
if (ret < 0)
fprintf(stderr, "%s, bad reference count\n", it->sname);
#endif
return ret;
}
}
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);
}
}
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;
}
}
}
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;
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;
}
}
/* 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 **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
* (int *).
*/
return pvaltmp;
}
}
/* Handle ANY DEFINED BY template, find the selector, look up
* 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,16 +235,18 @@ 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;
}
}
/* Convert type to a long:
* NB: don't check for NID_undef here because it
* 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);
else
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.
*/
for(atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
if(atbl->value == selector) return &atbl->tt;
for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
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;
}
}