-/* $OpenBSD: tasn_dec.c,v 1.51 2022/04/26 20:00:18 jsing Exp $ */
+/* $OpenBSD: tasn_dec.c,v 1.52 2022/04/27 17:28:34 jsing Exp $ */
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
* project 2000.
*/
static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
char *inf, char *cst, const unsigned char **in, long len, int exptag,
- int expclass, char opt, ASN1_TLC *ctx);
+ int expclass, char opt);
static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
- long len, const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx,
- int depth);
+ long len, const ASN1_ITEM *it, int tag, int aclass, char opt, int depth);
static int asn1_template_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
- long len, const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx, int depth);
+ long len, const ASN1_TEMPLATE *tt, char opt, int depth);
static int asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in,
- long len, const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx, int depth);
+ long len, const ASN1_TEMPLATE *tt, char opt, int depth);
static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, const unsigned char **in,
- long len, const ASN1_ITEM *it, int tag, int aclass, char opt,
- ASN1_TLC *ctx);
+ long len, const ASN1_ITEM *it, int tag, int aclass, char opt);
static int asn1_ex_c2i(ASN1_VALUE **pval, CBS *content, int utype,
const ASN1_ITEM *it);
-static void
-asn1_tlc_invalidate(ASN1_TLC *ctx)
-{
- if (ctx != NULL)
- ctx->valid = 0;
-}
-
ASN1_VALUE *
ASN1_item_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
const ASN1_ITEM *it)
{
ASN1_VALUE *ptmpval = NULL;
- ASN1_TLC ctx;
-
- asn1_tlc_invalidate(&ctx);
if (pval == NULL)
pval = &ptmpval;
- if (asn1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &ctx, 0) <= 0)
+ if (asn1_item_ex_d2i(pval, in, len, it, -1, 0, 0, 0) <= 0)
return NULL;
return *pval;
ASN1_template_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
const ASN1_TEMPLATE *tt)
{
- ASN1_TLC ctx;
-
- asn1_tlc_invalidate(&ctx);
-
- return asn1_template_ex_d2i(pval, in, len, tt, 0, &ctx, 0);
+ return asn1_template_ex_d2i(pval, in, len, tt, 0, 0);
}
/* Decode an item, taking care of IMPLICIT tagging, if any.
static int
asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
- const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx,
- int depth)
+ const ASN1_ITEM *it, int tag, int aclass, char opt, int depth)
{
const ASN1_TEMPLATE *tt, *errtt = NULL;
const ASN1_EXTERN_FUNCS *ef;
const ASN1_AUX *aux = it->funcs;
ASN1_aux_cb *asn1_cb = NULL;
+ ASN1_TLC ctx = { 0 };
const unsigned char *p = NULL, *q;
unsigned char oclass;
char seq_eoc, seq_nolen, cst, isopt;
goto err;
}
return asn1_template_ex_d2i(pval, in, len,
- it->templates, opt, ctx, depth);
+ it->templates, opt, depth);
}
return asn1_d2i_ex_primitive(pval, in, len, it,
- tag, aclass, opt, ctx);
+ tag, aclass, opt);
break;
case ASN1_ITYPE_MSTRING:
p = *in;
/* Just read in tag and class */
ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
- &p, len, -1, 0, 1, ctx);
+ &p, len, -1, 0, 1);
if (!ret) {
ASN1error(ERR_R_NESTED_ASN1_ERROR);
goto err;
goto err;
}
return asn1_d2i_ex_primitive(pval, in, len,
- it, otag, 0, 0, ctx);
+ it, otag, 0, 0);
case ASN1_ITYPE_EXTERN:
/* Use new style d2i */
ef = it->funcs;
- return ef->asn1_ex_d2i(pval, in, len,
- it, tag, aclass, opt, ctx);
+ return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, &ctx);
case ASN1_ITYPE_CHOICE:
/*
/* We mark field as OPTIONAL so its absence
* can be recognised.
*/
- ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx,
- depth);
+ ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1,
+ depth);
/* If field not present, try the next one */
if (ret == -1)
continue;
}
/* Get SEQUENCE length and update len, p */
ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
- &p, len, tag, aclass, opt, ctx);
+ &p, len, tag, aclass, opt);
if (!ret) {
ASN1error(ERR_R_NESTED_ASN1_ERROR);
goto err;
* OPTIONAL */
ret = asn1_template_ex_d2i(pseqval, &p, len,
- seqtt, isopt, ctx, depth);
+ seqtt, isopt, depth);
if (!ret) {
errtt = seqtt;
goto err;
ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx)
{
- return asn1_item_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx, 0);
+ return asn1_item_ex_d2i(pval, in, len, it, tag, aclass, opt, 0);
}
/* Templates are handled with two separate functions.
static int
asn1_template_ex_d2i(ASN1_VALUE **val, const unsigned char **in, long inlen,
- const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx, int depth)
+ const ASN1_TEMPLATE *tt, char opt, int depth)
{
int flags, aclass;
int ret;
* get the info.
*/
ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
- &p, inlen, tt->tag, aclass, opt, ctx);
+ &p, inlen, tt->tag, aclass, opt);
q = p;
if (!ret) {
ASN1error(ERR_R_NESTED_ASN1_ERROR);
return 0;
}
/* We've found the field so it can't be OPTIONAL now */
- ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth);
+ ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, depth);
if (!ret) {
ASN1error(ERR_R_NESTED_ASN1_ERROR);
return 0;
}
}
} else
- return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx,
- depth);
+ return asn1_template_noexp_d2i(val, in, inlen, tt, opt, depth);
*in = p;
return 1;
static int
asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in, long len,
- const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx, int depth)
+ const ASN1_TEMPLATE *tt, char opt, int depth)
{
int flags, aclass;
int ret;
}
/* Get the tag */
ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
- &p, len, sktag, skaclass, opt, ctx);
+ &p, len, sktag, skaclass, opt);
if (!ret) {
ASN1error(ERR_R_NESTED_ASN1_ERROR);
return 0;
}
skfield = NULL;
if (!asn1_item_ex_d2i(&skfield, &p, len,
- tt->item, -1, 0, 0, ctx, depth)) {
+ tt->item, -1, 0, 0, depth)) {
ASN1error(ERR_R_NESTED_ASN1_ERROR);
goto err;
}
} else if (flags & ASN1_TFLG_IMPTAG) {
/* IMPLICIT tagging */
ret = asn1_item_ex_d2i(val, &p, len,
- tt->item, tt->tag, aclass, opt, ctx, depth);
+ tt->item, tt->tag, aclass, opt, depth);
if (!ret) {
ASN1error(ERR_R_NESTED_ASN1_ERROR);
goto err;
} else {
/* Nothing special */
ret = asn1_item_ex_d2i(val, &p, len, tt->item,
- -1, tt->flags & ASN1_TFLG_COMBINE, opt, ctx, depth);
+ -1, tt->flags & ASN1_TFLG_COMBINE, opt, depth);
if (!ret) {
ASN1error(ERR_R_NESTED_ASN1_ERROR);
goto err;
static int
asn1_d2i_ex_primitive(ASN1_VALUE **pval, const unsigned char **in, long inlen,
- const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx)
+ const ASN1_ITEM *it, int tag, int aclass, char opt)
{
int ret = 0, utype;
long plen;
}
p = *in;
ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
- &p, inlen, -1, 0, 0, ctx);
+ &p, inlen, -1, 0, 0);
if (!ret) {
ASN1error(ERR_R_NESTED_ASN1_ERROR);
return 0;
p = *in;
/* Check header */
ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
- &p, inlen, tag, aclass, opt, ctx);
+ &p, inlen, tag, aclass, opt);
if (!ret) {
ASN1error(ERR_R_NESTED_ASN1_ERROR);
return 0;
/* SEQUENCE, SET and "OTHER" are left in encoded form */
if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
(utype == V_ASN1_OTHER)) {
- /* Clear context cache for type OTHER because the auto clear
- * when we have a exact match wont work
- */
- if (utype == V_ASN1_OTHER) {
- asn1_tlc_invalidate(ctx);
- } else if (!cst) {
+ if (utype != V_ASN1_OTHER && !cst) {
/* SEQUENCE and SET must be constructed */
ASN1error(ASN1_R_TYPE_NOT_CONSTRUCTED);
return 0;
q = p;
/* Just read in a header: only care about the length */
if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
- -1, 0, 0, NULL)) {
+ -1, 0, 0)) {
ASN1error(ERR_R_NESTED_ASN1_ERROR);
return 0;
}
}
if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
- len, tag, aclass, 0, NULL)) {
+ len, tag, aclass, 0)) {
ASN1error(ERR_R_NESTED_ASN1_ERROR);
return 0;
}
static int
asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, char *inf,
char *cst, const unsigned char **in, long len, int exptag, int expclass,
- char opt, ASN1_TLC *ctx)
+ char opt)
{
int i;
int ptag, pclass;
p = *in;
q = p;
- if (ctx && ctx->valid) {
- i = ctx->ret;
- plen = ctx->plen;
- pclass = ctx->pclass;
- ptag = ctx->ptag;
- p += ctx->hdrlen;
- } else {
- i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
- if (ctx) {
- ctx->ret = i;
- ctx->plen = plen;
- ctx->pclass = pclass;
- ctx->ptag = ptag;
- ctx->hdrlen = p - q;
- ctx->valid = 1;
- /* If definite length, and no error, length +
- * header can't exceed total amount of data available.
- */
- if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {
- ASN1error(ASN1_R_TOO_LONG);
- asn1_tlc_invalidate(ctx);
- return 0;
- }
- }
- }
-
+ i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
if (i & 0x80) {
ASN1error(ASN1_R_BAD_OBJECT_HEADER);
- asn1_tlc_invalidate(ctx);
return 0;
}
if (exptag >= 0) {
*/
if (opt)
return -1;
- asn1_tlc_invalidate(ctx);
ASN1error(ASN1_R_WRONG_TAG);
return 0;
}
- /* We have a tag and class match:
- * assume we are going to do something with it */
- asn1_tlc_invalidate(ctx);
}
if (i & 1)