2000-12-08 19:09:35 +00:00
|
|
|
/* tasn_dec.c */
|
2008-11-05 18:39:08 +00:00
|
|
|
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
|
2000-12-08 19:09:35 +00:00
|
|
|
* project 2000.
|
|
|
|
*/
|
|
|
|
/* ====================================================================
|
2005-04-30 18:02:54 +00:00
|
|
|
* Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
|
2000-12-08 19:09:35 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* 3. All advertising materials mentioning features or use of this
|
|
|
|
* software must display the following acknowledgment:
|
|
|
|
* "This product includes software developed by the OpenSSL Project
|
|
|
|
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
|
|
|
|
*
|
|
|
|
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
|
|
|
* endorse or promote products derived from this software without
|
|
|
|
* prior written permission. For written permission, please contact
|
|
|
|
* licensing@OpenSSL.org.
|
|
|
|
*
|
|
|
|
* 5. Products derived from this software may not be called "OpenSSL"
|
|
|
|
* nor may "OpenSSL" appear in their names without prior written
|
|
|
|
* permission of the OpenSSL Project.
|
|
|
|
*
|
|
|
|
* 6. Redistributions of any form whatsoever must retain the following
|
|
|
|
* acknowledgment:
|
|
|
|
* "This product includes software developed by the OpenSSL Project
|
|
|
|
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
|
|
|
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
|
|
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
* ====================================================================
|
|
|
|
*
|
|
|
|
* This product includes cryptographic software written by Eric Young
|
|
|
|
* (eay@cryptsoft.com). This product includes software written by Tim
|
|
|
|
* Hudson (tjh@cryptsoft.com).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stddef.h>
|
2001-02-20 13:41:11 +00:00
|
|
|
#include <string.h>
|
2000-12-08 19:09:35 +00:00
|
|
|
#include <openssl/asn1.h>
|
|
|
|
#include <openssl/asn1t.h>
|
|
|
|
#include <openssl/objects.h>
|
|
|
|
#include <openssl/buffer.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
|
2004-03-15 23:15:26 +00:00
|
|
|
static int asn1_check_eoc(const unsigned char **in, long len);
|
2005-04-30 13:06:45 +00:00
|
|
|
static int asn1_find_end(const unsigned char **in, long len, char inf);
|
2004-04-24 17:02:48 +00:00
|
|
|
|
|
|
|
static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
|
2009-03-14 18:33:49 +00:00
|
|
|
char inf, int tag, int aclass, int depth);
|
2004-04-24 17:02:48 +00:00
|
|
|
|
2004-03-15 23:15:26 +00:00
|
|
|
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
|
2004-04-24 17:02:48 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
static int asn1_template_ex_d2i(ASN1_VALUE **pval,
|
|
|
|
const unsigned char **in, long len,
|
|
|
|
const ASN1_TEMPLATE *tt, char opt,
|
|
|
|
ASN1_TLC *ctx);
|
|
|
|
static int asn1_template_noexp_d2i(ASN1_VALUE **val,
|
|
|
|
const unsigned char **in, long len,
|
|
|
|
const ASN1_TEMPLATE *tt, char opt,
|
|
|
|
ASN1_TLC *ctx);
|
|
|
|
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);
|
2000-12-08 19:09:35 +00:00
|
|
|
|
2001-07-27 02:22:42 +00:00
|
|
|
/* Table to convert tags to bit values, used for MSTRING type */
|
2007-01-23 17:53:48 +00:00
|
|
|
static const unsigned long tag2bit[32] = {
|
2001-07-27 02:22:42 +00:00
|
|
|
0, 0, 0, B_ASN1_BIT_STRING, /* tags 0 - 3 */
|
|
|
|
B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,/* tags 4- 7 */
|
|
|
|
B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,/* tags 8-11 */
|
|
|
|
B_ASN1_UTF8STRING,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,/* tags 12-15 */
|
2006-02-19 13:44:47 +00:00
|
|
|
B_ASN1_SEQUENCE,0,B_ASN1_NUMERICSTRING,B_ASN1_PRINTABLESTRING, /* tags 16-19 */
|
2001-07-27 02:22:42 +00:00
|
|
|
B_ASN1_T61STRING,B_ASN1_VIDEOTEXSTRING,B_ASN1_IA5STRING, /* tags 20-22 */
|
|
|
|
B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME, /* tags 23-24 */
|
|
|
|
B_ASN1_GRAPHICSTRING,B_ASN1_ISO64STRING,B_ASN1_GENERALSTRING, /* tags 25-27 */
|
|
|
|
B_ASN1_UNIVERSALSTRING,B_ASN1_UNKNOWN,B_ASN1_BMPSTRING,B_ASN1_UNKNOWN, /* tags 28-31 */
|
|
|
|
};
|
|
|
|
|
|
|
|
unsigned long ASN1_tag2bit(int tag)
|
2004-04-24 17:02:48 +00:00
|
|
|
{
|
|
|
|
if ((tag < 0) || (tag > 30)) return 0;
|
2001-07-27 02:22:42 +00:00
|
|
|
return tag2bit[tag];
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2001-07-27 02:22:42 +00:00
|
|
|
|
2000-12-08 19:09:35 +00:00
|
|
|
/* Macro to initialize and invalidate the cache */
|
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
#define asn1_tlc_clear(c) if (c) (c)->valid = 0
|
2006-11-16 00:19:39 +00:00
|
|
|
/* Version to avoid compiler warning about 'c' always non-NULL */
|
|
|
|
#define asn1_tlc_clear_nc(c) (c)->valid = 0
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
/* Decode an ASN1 item, this currently behaves just
|
|
|
|
* like a standard 'd2i' function. 'in' points to
|
|
|
|
* a buffer to read the data from, in future we will
|
|
|
|
* have more advanced versions that can input data
|
|
|
|
* a piece at a time and this will simply be a special
|
|
|
|
* case.
|
|
|
|
*/
|
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
|
2008-11-12 03:58:08 +00:00
|
|
|
const unsigned char **in, long len, const ASN1_ITEM *it)
|
2004-04-24 17:02:48 +00:00
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
ASN1_TLC c;
|
|
|
|
ASN1_VALUE *ptmpval = NULL;
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!pval)
|
|
|
|
pval = &ptmpval;
|
2006-11-16 00:19:39 +00:00
|
|
|
asn1_tlc_clear_nc(&c);
|
2004-04-24 17:02:48 +00:00
|
|
|
if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
|
2000-12-08 19:09:35 +00:00
|
|
|
return *pval;
|
|
|
|
return NULL;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
int ASN1_template_d2i(ASN1_VALUE **pval,
|
|
|
|
const unsigned char **in, long len, const ASN1_TEMPLATE *tt)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
ASN1_TLC c;
|
2006-11-16 00:19:39 +00:00
|
|
|
asn1_tlc_clear_nc(&c);
|
2000-12-08 19:09:35 +00:00
|
|
|
return asn1_template_ex_d2i(pval, in, len, tt, 0, &c);
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Decode an item, taking care of IMPLICIT tagging, if any.
|
|
|
|
* If 'opt' set and tag mismatch return -1 to handle OPTIONAL
|
|
|
|
*/
|
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
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)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
const ASN1_TEMPLATE *tt, *errtt = NULL;
|
|
|
|
const ASN1_COMPAT_FUNCS *cf;
|
|
|
|
const ASN1_EXTERN_FUNCS *ef;
|
|
|
|
const ASN1_AUX *aux = it->funcs;
|
|
|
|
ASN1_aux_cb *asn1_cb;
|
2006-02-08 18:51:17 +00:00
|
|
|
const unsigned char *p = NULL, *q;
|
2004-03-15 23:15:26 +00:00
|
|
|
unsigned char *wp=NULL; /* BIG FAT WARNING! BREAKS CONST WHERE USED */
|
|
|
|
unsigned char imphack = 0, oclass;
|
2000-12-08 19:09:35 +00:00
|
|
|
char seq_eoc, seq_nolen, cst, isopt;
|
|
|
|
long tmplen;
|
|
|
|
int i;
|
|
|
|
int otag;
|
|
|
|
int ret = 0;
|
2010-06-12 14:13:23 +00:00
|
|
|
ASN1_VALUE **pchptr, *ptmpval;
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!pval)
|
|
|
|
return 0;
|
|
|
|
if (aux && aux->asn1_cb)
|
|
|
|
asn1_cb = aux->asn1_cb;
|
2000-12-08 19:09:35 +00:00
|
|
|
else asn1_cb = 0;
|
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
switch(it->itype)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
case ASN1_ITYPE_PRIMITIVE:
|
2004-04-24 17:02:48 +00:00
|
|
|
if (it->templates)
|
|
|
|
{
|
|
|
|
/* tagging or OPTIONAL is currently illegal on an item
|
|
|
|
* template because the flags can't get passed down.
|
|
|
|
* In practice this isn't a problem: we include the
|
|
|
|
* relevant flags from the item template in the
|
|
|
|
* template itself.
|
2001-02-25 14:11:31 +00:00
|
|
|
*/
|
2004-04-24 17:02:48 +00:00
|
|
|
if ((tag != -1) || opt)
|
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
|
|
|
|
ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
|
2001-02-25 14:11:31 +00:00
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
return asn1_template_ex_d2i(pval, in, len,
|
|
|
|
it->templates, opt, ctx);
|
2001-02-25 14:11:31 +00:00
|
|
|
}
|
2004-04-24 17:02:48 +00:00
|
|
|
return asn1_d2i_ex_primitive(pval, in, len, it,
|
|
|
|
tag, aclass, opt, ctx);
|
2000-12-08 19:09:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ASN1_ITYPE_MSTRING:
|
|
|
|
p = *in;
|
|
|
|
/* Just read in tag and class */
|
2004-04-24 17:02:48 +00:00
|
|
|
ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
|
|
|
|
&p, len, -1, 0, 1, ctx);
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
|
|
|
|
ERR_R_NESTED_ASN1_ERROR);
|
2000-12-08 19:09:35 +00:00
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
|
2000-12-08 19:09:35 +00:00
|
|
|
/* Must be UNIVERSAL class */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (oclass != V_ASN1_UNIVERSAL)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
/* If OPTIONAL, assume this is OK */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (opt) return -1;
|
|
|
|
ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
|
|
|
|
ASN1_R_MSTRING_NOT_UNIVERSAL);
|
2000-12-08 19:09:35 +00:00
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
/* Check tag matches bit map */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!(ASN1_tag2bit(otag) & it->utype))
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
/* If OPTIONAL, assume this is OK */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (opt)
|
|
|
|
return -1;
|
|
|
|
ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
|
|
|
|
ASN1_R_MSTRING_WRONG_TAG);
|
2000-12-08 19:09:35 +00:00
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
return asn1_d2i_ex_primitive(pval, in, len,
|
|
|
|
it, otag, 0, 0, ctx);
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
case ASN1_ITYPE_EXTERN:
|
|
|
|
/* Use new style d2i */
|
|
|
|
ef = it->funcs;
|
2004-04-24 17:02:48 +00:00
|
|
|
return ef->asn1_ex_d2i(pval, in, len,
|
|
|
|
it, tag, aclass, opt, ctx);
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
case ASN1_ITYPE_COMPAT:
|
|
|
|
/* we must resort to old style evil hackery */
|
|
|
|
cf = it->funcs;
|
|
|
|
|
|
|
|
/* If OPTIONAL see if it is there */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (opt)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
int exptag;
|
|
|
|
p = *in;
|
2004-04-24 17:02:48 +00:00
|
|
|
if (tag == -1)
|
|
|
|
exptag = it->utype;
|
2000-12-08 19:09:35 +00:00
|
|
|
else exptag = tag;
|
2004-04-24 17:02:48 +00:00
|
|
|
/* Don't care about anything other than presence
|
|
|
|
* of expected tag */
|
|
|
|
|
|
|
|
ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL,
|
|
|
|
&p, len, exptag, aclass, 1, ctx);
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
|
|
|
|
ERR_R_NESTED_ASN1_ERROR);
|
2000-12-08 19:09:35 +00:00
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
if (ret == -1)
|
|
|
|
return -1;
|
2000-12-08 19:09:35 +00:00
|
|
|
}
|
2004-04-24 17:02:48 +00:00
|
|
|
|
2000-12-08 19:09:35 +00:00
|
|
|
/* This is the old style evil hack IMPLICIT handling:
|
|
|
|
* since the underlying code is expecting a tag and
|
|
|
|
* class other than the one present we change the
|
|
|
|
* buffer temporarily then change it back afterwards.
|
|
|
|
* This doesn't and never did work for tags > 30.
|
|
|
|
*
|
|
|
|
* Yes this is *horrible* but it is only needed for
|
|
|
|
* old style d2i which will hopefully not be around
|
|
|
|
* for much longer.
|
|
|
|
* FIXME: should copy the buffer then modify it so
|
|
|
|
* the input buffer can be const: we should *always*
|
|
|
|
* copy because the old style d2i might modify the
|
|
|
|
* buffer.
|
|
|
|
*/
|
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
if (tag != -1)
|
|
|
|
{
|
2004-03-15 23:15:26 +00:00
|
|
|
wp = *(unsigned char **)in;
|
|
|
|
imphack = *wp;
|
2006-02-08 18:51:17 +00:00
|
|
|
if (p == NULL)
|
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
|
|
|
|
ERR_R_NESTED_ASN1_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
2004-04-24 17:02:48 +00:00
|
|
|
*wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED)
|
|
|
|
| it->utype);
|
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
ptmpval = cf->asn1_d2i(pval, in, len);
|
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
if (tag != -1)
|
|
|
|
*wp = imphack;
|
|
|
|
|
|
|
|
if (ptmpval)
|
|
|
|
return 1;
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
|
|
|
|
case ASN1_ITYPE_CHOICE:
|
2005-09-01 20:42:52 +00:00
|
|
|
if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
|
2000-12-08 19:09:35 +00:00
|
|
|
goto auxerr;
|
2001-04-02 00:59:19 +00:00
|
|
|
|
|
|
|
/* Allocate structure */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!*pval && !ASN1_item_ex_new(pval, it))
|
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
|
|
|
|
ERR_R_NESTED_ASN1_ERROR);
|
|
|
|
goto err;
|
2001-04-02 00:59:19 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
/* CHOICE type, try each possibility in turn */
|
|
|
|
p = *in;
|
2004-04-25 12:46:39 +00:00
|
|
|
for (i = 0, tt=it->templates; i < it->tcount; i++, tt++)
|
2004-04-24 17:02:48 +00:00
|
|
|
{
|
2001-04-02 00:59:19 +00:00
|
|
|
pchptr = asn1_get_field_ptr(pval, tt);
|
2000-12-08 19:09:35 +00:00
|
|
|
/* We mark field as OPTIONAL so its absence
|
|
|
|
* can be recognised.
|
|
|
|
*/
|
2001-04-02 00:59:19 +00:00
|
|
|
ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);
|
2000-12-08 19:09:35 +00:00
|
|
|
/* If field not present, try the next one */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (ret == -1)
|
|
|
|
continue;
|
2000-12-08 19:09:35 +00:00
|
|
|
/* If positive return, read OK, break loop */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (ret > 0)
|
|
|
|
break;
|
2000-12-08 19:09:35 +00:00
|
|
|
/* Otherwise must be an ASN1 parsing error */
|
|
|
|
errtt = tt;
|
2004-04-24 17:02:48 +00:00
|
|
|
ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
|
|
|
|
ERR_R_NESTED_ASN1_ERROR);
|
2001-02-25 14:11:31 +00:00
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
|
2000-12-08 19:09:35 +00:00
|
|
|
/* Did we fall off the end without reading anything? */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (i == it->tcount)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
/* If OPTIONAL, this is OK */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (opt)
|
|
|
|
{
|
2001-04-02 00:59:19 +00:00
|
|
|
/* Free and zero it */
|
|
|
|
ASN1_item_ex_free(pval, it);
|
|
|
|
return -1;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
|
|
|
|
ASN1_R_NO_MATCHING_CHOICE_TYPE);
|
2001-02-25 14:11:31 +00:00
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
|
2000-12-08 19:09:35 +00:00
|
|
|
asn1_set_choice_selector(pval, i, it);
|
|
|
|
*in = p;
|
2005-09-01 20:42:52 +00:00
|
|
|
if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
|
2000-12-08 19:09:35 +00:00
|
|
|
goto auxerr;
|
|
|
|
return 1;
|
|
|
|
|
2002-10-03 12:38:52 +00:00
|
|
|
case ASN1_ITYPE_NDEF_SEQUENCE:
|
2000-12-08 19:09:35 +00:00
|
|
|
case ASN1_ITYPE_SEQUENCE:
|
|
|
|
p = *in;
|
|
|
|
tmplen = len;
|
|
|
|
|
|
|
|
/* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (tag == -1)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
tag = V_ASN1_SEQUENCE;
|
|
|
|
aclass = V_ASN1_UNIVERSAL;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
/* Get SEQUENCE length and update len, p */
|
2004-04-24 17:02:48 +00:00
|
|
|
ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
|
|
|
|
&p, len, tag, aclass, opt, ctx);
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
|
|
|
|
ERR_R_NESTED_ASN1_ERROR);
|
2000-12-08 19:09:35 +00:00
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
else if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
if (aux && (aux->flags & ASN1_AFLG_BROKEN))
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
len = tmplen - (p - *in);
|
|
|
|
seq_nolen = 1;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
/* If indefinite we don't do a length check */
|
|
|
|
else seq_nolen = seq_eoc;
|
|
|
|
if (!cst)
|
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
|
|
|
|
ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
|
2000-12-08 19:09:35 +00:00
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
2004-04-27 18:33:40 +00:00
|
|
|
if (!*pval && !ASN1_item_ex_new(pval, it))
|
2004-04-24 17:02:48 +00:00
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
|
|
|
|
ERR_R_NESTED_ASN1_ERROR);
|
|
|
|
goto err;
|
2000-12-08 19:09:35 +00:00
|
|
|
}
|
2004-04-24 17:02:48 +00:00
|
|
|
|
2005-09-01 20:42:52 +00:00
|
|
|
if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
|
2000-12-08 19:09:35 +00:00
|
|
|
goto auxerr;
|
|
|
|
|
|
|
|
/* Get each field entry */
|
2004-04-24 17:02:48 +00:00
|
|
|
for (i = 0, tt = it->templates; i < it->tcount; i++, tt++)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
const ASN1_TEMPLATE *seqtt;
|
|
|
|
ASN1_VALUE **pseqval;
|
|
|
|
seqtt = asn1_do_adb(pval, tt, 1);
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!seqtt)
|
|
|
|
goto err;
|
2000-12-08 19:09:35 +00:00
|
|
|
pseqval = asn1_get_field_ptr(pval, seqtt);
|
|
|
|
/* Have we ran out of data? */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!len)
|
|
|
|
break;
|
2000-12-08 19:09:35 +00:00
|
|
|
q = p;
|
2004-04-24 17:02:48 +00:00
|
|
|
if (asn1_check_eoc(&p, len))
|
|
|
|
{
|
|
|
|
if (!seq_eoc)
|
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
|
|
|
|
ASN1_R_UNEXPECTED_EOC);
|
2000-12-08 19:09:35 +00:00
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
len -= p - q;
|
|
|
|
seq_eoc = 0;
|
|
|
|
q = p;
|
|
|
|
break;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
/* This determines the OPTIONAL flag value. The field
|
|
|
|
* cannot be omitted if it is the last of a SEQUENCE
|
|
|
|
* and there is still data to be read. This isn't
|
|
|
|
* strictly necessary but it increases efficiency in
|
|
|
|
* some cases.
|
2000-12-08 19:09:35 +00:00
|
|
|
*/
|
2004-04-24 17:02:48 +00:00
|
|
|
if (i == (it->tcount - 1))
|
|
|
|
isopt = 0;
|
2000-12-21 01:38:55 +00:00
|
|
|
else isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
|
2004-04-24 17:02:48 +00:00
|
|
|
/* attempt to read in field, allowing each to be
|
|
|
|
* OPTIONAL */
|
|
|
|
|
|
|
|
ret = asn1_template_ex_d2i(pseqval, &p, len,
|
|
|
|
seqtt, isopt, ctx);
|
|
|
|
if (!ret)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
errtt = seqtt;
|
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
else if (ret == -1)
|
|
|
|
{
|
|
|
|
/* OPTIONAL component absent.
|
|
|
|
* Free and zero the field.
|
2000-12-08 19:09:35 +00:00
|
|
|
*/
|
|
|
|
ASN1_template_free(pseqval, seqtt);
|
|
|
|
continue;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
/* Update length */
|
|
|
|
len -= p - q;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
|
2000-12-08 19:09:35 +00:00
|
|
|
/* Check for EOC if expecting one */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (seq_eoc && !asn1_check_eoc(&p, len))
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MISSING_EOC);
|
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
/* Check all data read */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!seq_nolen && len)
|
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
|
|
|
|
ASN1_R_SEQUENCE_LENGTH_MISMATCH);
|
2000-12-08 19:09:35 +00:00
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
/* If we get here we've got no more data in the SEQUENCE,
|
|
|
|
* however we may not have read all fields so check all
|
|
|
|
* remaining are OPTIONAL and clear any that are.
|
|
|
|
*/
|
2004-04-24 17:02:48 +00:00
|
|
|
for (; i < it->tcount; tt++, i++)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
const ASN1_TEMPLATE *seqtt;
|
|
|
|
seqtt = asn1_do_adb(pval, tt, 1);
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!seqtt)
|
|
|
|
goto err;
|
|
|
|
if (seqtt->flags & ASN1_TFLG_OPTIONAL)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
ASN1_VALUE **pseqval;
|
|
|
|
pseqval = asn1_get_field_ptr(pval, seqtt);
|
|
|
|
ASN1_template_free(pseqval, seqtt);
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
errtt = seqtt;
|
2004-04-24 17:02:48 +00:00
|
|
|
ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
|
|
|
|
ASN1_R_FIELD_MISSING);
|
2000-12-08 19:09:35 +00:00
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
}
|
|
|
|
/* Save encoding */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!asn1_enc_save(pval, *in, p - *in, it))
|
|
|
|
goto auxerr;
|
2000-12-08 19:09:35 +00:00
|
|
|
*in = p;
|
2005-09-01 20:42:52 +00:00
|
|
|
if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
|
2000-12-08 19:09:35 +00:00
|
|
|
goto auxerr;
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
auxerr:
|
|
|
|
ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR);
|
|
|
|
err:
|
|
|
|
ASN1_item_ex_free(pval, it);
|
2004-04-24 17:02:48 +00:00
|
|
|
if (errtt)
|
|
|
|
ERR_add_error_data(4, "Field=", errtt->field_name,
|
|
|
|
", Type=", it->sname);
|
|
|
|
else
|
|
|
|
ERR_add_error_data(2, "Type=", it->sname);
|
2000-12-08 19:09:35 +00:00
|
|
|
return 0;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
/* Templates are handled with two separate functions.
|
|
|
|
* One handles any EXPLICIT tag and the other handles the rest.
|
2000-12-08 19:09:35 +00:00
|
|
|
*/
|
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
static int asn1_template_ex_d2i(ASN1_VALUE **val,
|
|
|
|
const unsigned char **in, long inlen,
|
|
|
|
const ASN1_TEMPLATE *tt, char opt,
|
|
|
|
ASN1_TLC *ctx)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
int flags, aclass;
|
|
|
|
int ret;
|
|
|
|
long len;
|
2004-03-15 23:15:26 +00:00
|
|
|
const unsigned char *p, *q;
|
2000-12-08 19:09:35 +00:00
|
|
|
char exp_eoc;
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!val)
|
|
|
|
return 0;
|
2000-12-08 19:09:35 +00:00
|
|
|
flags = tt->flags;
|
|
|
|
aclass = flags & ASN1_TFLG_TAG_CLASS;
|
|
|
|
|
|
|
|
p = *in;
|
|
|
|
|
|
|
|
/* Check if EXPLICIT tag expected */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (flags & ASN1_TFLG_EXPTAG)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
char cst;
|
2004-04-24 17:02:48 +00:00
|
|
|
/* Need to work out amount of data available to the inner
|
|
|
|
* content and where it starts: so read in EXPLICIT header to
|
|
|
|
* get the info.
|
2000-12-08 19:09:35 +00:00
|
|
|
*/
|
2004-04-24 17:02:48 +00:00
|
|
|
ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
|
|
|
|
&p, inlen, tt->tag, aclass, opt, ctx);
|
2000-12-08 19:09:35 +00:00
|
|
|
q = p;
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
|
|
|
|
ERR_R_NESTED_ASN1_ERROR);
|
2000-12-08 19:09:35 +00:00
|
|
|
return 0;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
else if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
if (!cst)
|
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
|
|
|
|
ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
|
2000-12-08 19:09:35 +00:00
|
|
|
return 0;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
/* We've found the field so it can't be OPTIONAL now */
|
|
|
|
ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
|
|
|
|
ERR_R_NESTED_ASN1_ERROR);
|
2000-12-08 19:09:35 +00:00
|
|
|
return 0;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
/* We read the field in OK so update length */
|
|
|
|
len -= p - q;
|
2004-04-24 17:02:48 +00:00
|
|
|
if (exp_eoc)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
/* If NDEF we must have an EOC here */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!asn1_check_eoc(&p, len))
|
|
|
|
{
|
2005-05-11 03:45:39 +00:00
|
|
|
ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
|
2004-04-24 17:02:48 +00:00
|
|
|
ASN1_R_MISSING_EOC);
|
2000-12-08 19:09:35 +00:00
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
}
|
2004-04-24 17:02:48 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise we must hit the EXPLICIT tag end or its
|
|
|
|
* an error */
|
|
|
|
if (len)
|
|
|
|
{
|
2005-05-11 03:45:39 +00:00
|
|
|
ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
|
2004-04-24 17:02:48 +00:00
|
|
|
ASN1_R_EXPLICIT_LENGTH_MISMATCH);
|
2000-12-08 19:09:35 +00:00
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
}
|
|
|
|
}
|
2004-04-24 17:02:48 +00:00
|
|
|
else
|
|
|
|
return asn1_template_noexp_d2i(val, in, inlen,
|
|
|
|
tt, opt, ctx);
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
*in = p;
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
err:
|
|
|
|
ASN1_template_free(val, tt);
|
|
|
|
return 0;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
static int asn1_template_noexp_d2i(ASN1_VALUE **val,
|
|
|
|
const unsigned char **in, long len,
|
|
|
|
const ASN1_TEMPLATE *tt, char opt,
|
|
|
|
ASN1_TLC *ctx)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
int flags, aclass;
|
|
|
|
int ret;
|
2004-03-15 23:15:26 +00:00
|
|
|
const unsigned char *p, *q;
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!val)
|
|
|
|
return 0;
|
2000-12-08 19:09:35 +00:00
|
|
|
flags = tt->flags;
|
|
|
|
aclass = flags & ASN1_TFLG_TAG_CLASS;
|
|
|
|
|
|
|
|
p = *in;
|
|
|
|
q = p;
|
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
if (flags & ASN1_TFLG_SK_MASK)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
/* SET OF, SEQUENCE OF */
|
|
|
|
int sktag, skaclass;
|
|
|
|
char sk_eoc;
|
|
|
|
/* First work out expected inner tag value */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (flags & ASN1_TFLG_IMPTAG)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
sktag = tt->tag;
|
|
|
|
skaclass = aclass;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
skaclass = V_ASN1_UNIVERSAL;
|
2004-04-24 17:02:48 +00:00
|
|
|
if (flags & ASN1_TFLG_SET_OF)
|
|
|
|
sktag = V_ASN1_SET;
|
|
|
|
else
|
|
|
|
sktag = V_ASN1_SEQUENCE;
|
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
/* Get the tag */
|
2004-04-24 17:02:48 +00:00
|
|
|
ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
|
|
|
|
&p, len, sktag, skaclass, opt, ctx);
|
|
|
|
if (!ret)
|
|
|
|
{
|
2005-05-11 03:45:39 +00:00
|
|
|
ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
|
2004-04-24 17:02:48 +00:00
|
|
|
ERR_R_NESTED_ASN1_ERROR);
|
2000-12-08 19:09:35 +00:00
|
|
|
return 0;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
else if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
if (!*val)
|
|
|
|
*val = (ASN1_VALUE *)sk_new_null();
|
|
|
|
else
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
/* We've got a valid STACK: free up any items present */
|
2008-06-04 11:01:43 +00:00
|
|
|
STACK_OF(ASN1_VALUE) *sktmp
|
|
|
|
= (STACK_OF(ASN1_VALUE) *)*val;
|
2000-12-08 19:09:35 +00:00
|
|
|
ASN1_VALUE *vtmp;
|
2008-06-04 11:01:43 +00:00
|
|
|
while(sk_ASN1_VALUE_num(sktmp) > 0)
|
2004-04-24 17:02:48 +00:00
|
|
|
{
|
2008-06-04 11:01:43 +00:00
|
|
|
vtmp = sk_ASN1_VALUE_pop(sktmp);
|
2004-04-24 17:02:48 +00:00
|
|
|
ASN1_item_ex_free(&vtmp,
|
|
|
|
ASN1_ITEM_ptr(tt->item));
|
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
}
|
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!*val)
|
|
|
|
{
|
2005-05-11 03:45:39 +00:00
|
|
|
ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
|
2004-04-24 17:02:48 +00:00
|
|
|
ERR_R_MALLOC_FAILURE);
|
2000-12-08 19:09:35 +00:00
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
|
2000-12-08 19:09:35 +00:00
|
|
|
/* Read as many items as we can */
|
2004-04-24 17:02:48 +00:00
|
|
|
while(len > 0)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
ASN1_VALUE *skfield;
|
|
|
|
q = p;
|
|
|
|
/* See if EOC found */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (asn1_check_eoc(&p, len))
|
|
|
|
{
|
|
|
|
if (!sk_eoc)
|
|
|
|
{
|
2005-05-11 03:45:39 +00:00
|
|
|
ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
|
2004-04-24 17:02:48 +00:00
|
|
|
ASN1_R_UNEXPECTED_EOC);
|
2000-12-08 19:09:35 +00:00
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
len -= p - q;
|
|
|
|
sk_eoc = 0;
|
|
|
|
break;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
skfield = NULL;
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!ASN1_item_ex_d2i(&skfield, &p, len,
|
|
|
|
ASN1_ITEM_ptr(tt->item),
|
|
|
|
-1, 0, 0, ctx))
|
|
|
|
{
|
2005-05-11 03:45:39 +00:00
|
|
|
ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
|
2004-04-24 17:02:48 +00:00
|
|
|
ERR_R_NESTED_ASN1_ERROR);
|
2000-12-08 19:09:35 +00:00
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
len -= p - q;
|
2008-06-04 11:01:43 +00:00
|
|
|
if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val,
|
|
|
|
skfield))
|
2004-04-24 17:02:48 +00:00
|
|
|
{
|
2005-05-11 03:45:39 +00:00
|
|
|
ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
|
2004-04-24 17:02:48 +00:00
|
|
|
ERR_R_MALLOC_FAILURE);
|
2000-12-08 19:09:35 +00:00
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
}
|
2004-04-24 17:02:48 +00:00
|
|
|
if (sk_eoc)
|
|
|
|
{
|
2005-05-11 03:45:39 +00:00
|
|
|
ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
|
2000-12-08 19:09:35 +00:00
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
}
|
2004-04-24 17:02:48 +00:00
|
|
|
else if (flags & ASN1_TFLG_IMPTAG)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
/* IMPLICIT tagging */
|
2004-04-24 17:02:48 +00:00
|
|
|
ret = ASN1_item_ex_d2i(val, &p, len,
|
|
|
|
ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx);
|
|
|
|
if (!ret)
|
|
|
|
{
|
2005-05-11 03:45:39 +00:00
|
|
|
ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
|
2004-04-24 17:02:48 +00:00
|
|
|
ERR_R_NESTED_ASN1_ERROR);
|
2000-12-08 19:09:35 +00:00
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
else if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
/* Nothing special */
|
2004-04-24 17:02:48 +00:00
|
|
|
ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
|
|
|
|
-1, 0, opt, ctx);
|
|
|
|
if (!ret)
|
|
|
|
{
|
2005-05-11 03:45:39 +00:00
|
|
|
ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
|
2004-04-24 17:02:48 +00:00
|
|
|
ERR_R_NESTED_ASN1_ERROR);
|
2000-12-08 19:09:35 +00:00
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
else if (ret == -1)
|
|
|
|
return -1;
|
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
*in = p;
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
err:
|
|
|
|
ASN1_template_free(val, tt);
|
|
|
|
return 0;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
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)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
int ret = 0, utype;
|
|
|
|
long plen;
|
|
|
|
char cst, inf, free_cont = 0;
|
2004-03-15 23:15:26 +00:00
|
|
|
const unsigned char *p;
|
2000-12-08 19:09:35 +00:00
|
|
|
BUF_MEM buf;
|
2004-03-15 23:15:26 +00:00
|
|
|
const unsigned char *cont = NULL;
|
2000-12-08 19:09:35 +00:00
|
|
|
long len;
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!pval)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
|
|
|
|
return 0; /* Should never happen */
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
if (it->itype == ASN1_ITYPE_MSTRING)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
utype = tag;
|
|
|
|
tag = -1;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
utype = it->utype;
|
2000-12-08 19:09:35 +00:00
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
if (utype == V_ASN1_ANY)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
/* If type is ANY need to figure out type from tag */
|
|
|
|
unsigned char oclass;
|
2004-04-24 17:02:48 +00:00
|
|
|
if (tag >= 0)
|
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
|
|
|
|
ASN1_R_ILLEGAL_TAGGED_ANY);
|
2000-12-08 19:09:35 +00:00
|
|
|
return 0;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
if (opt)
|
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
|
|
|
|
ASN1_R_ILLEGAL_OPTIONAL_ANY);
|
2000-12-08 19:09:35 +00:00
|
|
|
return 0;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
p = *in;
|
2004-04-24 17:02:48 +00:00
|
|
|
ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
|
|
|
|
&p, inlen, -1, 0, 0, ctx);
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
|
|
|
|
ERR_R_NESTED_ASN1_ERROR);
|
2000-12-08 19:09:35 +00:00
|
|
|
return 0;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
if (oclass != V_ASN1_UNIVERSAL)
|
|
|
|
utype = V_ASN1_OTHER;
|
2000-12-08 19:09:35 +00:00
|
|
|
}
|
2004-04-24 17:02:48 +00:00
|
|
|
if (tag == -1)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
tag = utype;
|
|
|
|
aclass = V_ASN1_UNIVERSAL;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
p = *in;
|
|
|
|
/* Check header */
|
2004-04-24 17:02:48 +00:00
|
|
|
ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
|
|
|
|
&p, inlen, tag, aclass, opt, ctx);
|
|
|
|
if (!ret)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
|
|
|
|
return 0;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
else if (ret == -1)
|
|
|
|
return -1;
|
2006-09-28 13:20:44 +00:00
|
|
|
ret = 0;
|
2000-12-08 19:09:35 +00:00
|
|
|
/* SEQUENCE, SET and "OTHER" are left in encoded form */
|
2004-04-24 17:02:48 +00:00
|
|
|
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
|
2001-04-21 12:06:01 +00:00
|
|
|
*/
|
2004-04-24 17:02:48 +00:00
|
|
|
if (utype == V_ASN1_OTHER)
|
|
|
|
{
|
2001-04-21 12:06:01 +00:00
|
|
|
asn1_tlc_clear(ctx);
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
/* SEQUENCE and SET must be constructed */
|
2004-04-24 17:02:48 +00:00
|
|
|
else if (!cst)
|
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
|
|
|
|
ASN1_R_TYPE_NOT_CONSTRUCTED);
|
2000-12-08 19:09:35 +00:00
|
|
|
return 0;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
cont = *in;
|
|
|
|
/* If indefinite length constructed find the real end */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (inf)
|
|
|
|
{
|
2005-04-30 13:06:45 +00:00
|
|
|
if (!asn1_find_end(&p, plen, inf))
|
2004-04-24 17:02:48 +00:00
|
|
|
goto err;
|
2000-12-08 19:09:35 +00:00
|
|
|
len = p - cont;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
len = p - cont + plen;
|
|
|
|
p += plen;
|
|
|
|
buf.data = NULL;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
}
|
2004-04-24 17:02:48 +00:00
|
|
|
else if (cst)
|
|
|
|
{
|
2014-12-16 23:13:19 +00:00
|
|
|
if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
|
|
|
|
|| utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
|
|
|
|
|| utype == V_ASN1_ENUMERATED)
|
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
|
|
|
|
ASN1_R_TYPE_NOT_PRIMITIVE);
|
|
|
|
return 0;
|
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
buf.length = 0;
|
|
|
|
buf.max = 0;
|
|
|
|
buf.data = NULL;
|
|
|
|
/* Should really check the internal tags are correct but
|
|
|
|
* some things may get this wrong. The relevant specs
|
|
|
|
* say that constructed string types should be OCTET STRINGs
|
|
|
|
* internally irrespective of the type. So instead just check
|
|
|
|
* for UNIVERSAL class and ignore the tag.
|
|
|
|
*/
|
2009-03-14 18:33:49 +00:00
|
|
|
if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0))
|
2006-08-31 20:10:37 +00:00
|
|
|
{
|
|
|
|
free_cont = 1;
|
2004-04-24 17:02:48 +00:00
|
|
|
goto err;
|
2006-08-31 20:10:37 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
len = buf.length;
|
2001-01-04 01:46:36 +00:00
|
|
|
/* Append a final null to string */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!BUF_MEM_grow_clean(&buf, len + 1))
|
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
|
|
|
|
ERR_R_MALLOC_FAILURE);
|
2001-01-04 01:46:36 +00:00
|
|
|
return 0;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2001-01-04 01:46:36 +00:00
|
|
|
buf.data[len] = 0;
|
2004-03-15 23:15:26 +00:00
|
|
|
cont = (const unsigned char *)buf.data;
|
2000-12-08 19:09:35 +00:00
|
|
|
free_cont = 1;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
cont = p;
|
|
|
|
len = plen;
|
|
|
|
p += plen;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
/* We now have content length and type: translate into a structure */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
|
|
|
|
goto err;
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
*in = p;
|
|
|
|
ret = 1;
|
|
|
|
err:
|
2004-04-24 17:02:48 +00:00
|
|
|
if (free_cont && buf.data) OPENSSL_free(buf.data);
|
2000-12-08 19:09:35 +00:00
|
|
|
return ret;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
/* Translate ASN1 content octets into a structure */
|
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
|
|
|
|
int utype, char *free_cont, const ASN1_ITEM *it)
|
|
|
|
{
|
2003-09-30 16:47:33 +00:00
|
|
|
ASN1_VALUE **opval = NULL;
|
2000-12-08 19:09:35 +00:00
|
|
|
ASN1_STRING *stmp;
|
|
|
|
ASN1_TYPE *typ = NULL;
|
|
|
|
int ret = 0;
|
|
|
|
const ASN1_PRIMITIVE_FUNCS *pf;
|
2001-01-04 19:53:48 +00:00
|
|
|
ASN1_INTEGER **tint;
|
2000-12-08 19:09:35 +00:00
|
|
|
pf = it->funcs;
|
2004-04-24 17:02:48 +00:00
|
|
|
|
|
|
|
if (pf && pf->prim_c2i)
|
|
|
|
return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
|
2000-12-08 19:09:35 +00:00
|
|
|
/* If ANY type clear type and set pointer to internal value */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (it->utype == V_ASN1_ANY)
|
|
|
|
{
|
|
|
|
if (!*pval)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
typ = ASN1_TYPE_new();
|
2006-01-29 23:12:22 +00:00
|
|
|
if (typ == NULL)
|
|
|
|
goto err;
|
2000-12-08 19:09:35 +00:00
|
|
|
*pval = (ASN1_VALUE *)typ;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
typ = (ASN1_TYPE *)*pval;
|
|
|
|
|
|
|
|
if (utype != typ->type)
|
|
|
|
ASN1_TYPE_set(typ, utype, NULL);
|
2003-09-30 16:47:33 +00:00
|
|
|
opval = pval;
|
2006-11-16 00:19:39 +00:00
|
|
|
pval = &typ->value.asn1_value;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
switch(utype)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
case V_ASN1_OBJECT:
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
|
|
|
|
goto err;
|
2000-12-08 19:09:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case V_ASN1_NULL:
|
2004-04-24 17:02:48 +00:00
|
|
|
if (len)
|
|
|
|
{
|
2005-05-11 03:45:39 +00:00
|
|
|
ASN1err(ASN1_F_ASN1_EX_C2I,
|
2004-04-24 17:02:48 +00:00
|
|
|
ASN1_R_NULL_IS_WRONG_LENGTH);
|
2000-12-08 19:09:35 +00:00
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
*pval = (ASN1_VALUE *)1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case V_ASN1_BOOLEAN:
|
2004-04-24 17:02:48 +00:00
|
|
|
if (len != 1)
|
|
|
|
{
|
2005-05-11 03:45:39 +00:00
|
|
|
ASN1err(ASN1_F_ASN1_EX_C2I,
|
2004-04-24 17:02:48 +00:00
|
|
|
ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
|
2000-12-08 19:09:35 +00:00
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
ASN1_BOOLEAN *tbool;
|
|
|
|
tbool = (ASN1_BOOLEAN *)pval;
|
|
|
|
*tbool = *cont;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case V_ASN1_BIT_STRING:
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
|
|
|
|
goto err;
|
2000-12-08 19:09:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case V_ASN1_INTEGER:
|
|
|
|
case V_ASN1_NEG_INTEGER:
|
|
|
|
case V_ASN1_ENUMERATED:
|
|
|
|
case V_ASN1_NEG_ENUMERATED:
|
2001-01-04 19:53:48 +00:00
|
|
|
tint = (ASN1_INTEGER **)pval;
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!c2i_ASN1_INTEGER(tint, &cont, len))
|
|
|
|
goto err;
|
2001-01-04 19:53:48 +00:00
|
|
|
/* Fixup type to match the expected form */
|
|
|
|
(*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
|
2000-12-08 19:09:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case V_ASN1_OCTET_STRING:
|
|
|
|
case V_ASN1_NUMERICSTRING:
|
|
|
|
case V_ASN1_PRINTABLESTRING:
|
|
|
|
case V_ASN1_T61STRING:
|
|
|
|
case V_ASN1_VIDEOTEXSTRING:
|
|
|
|
case V_ASN1_IA5STRING:
|
|
|
|
case V_ASN1_UTCTIME:
|
|
|
|
case V_ASN1_GENERALIZEDTIME:
|
|
|
|
case V_ASN1_GRAPHICSTRING:
|
|
|
|
case V_ASN1_VISIBLESTRING:
|
|
|
|
case V_ASN1_GENERALSTRING:
|
|
|
|
case V_ASN1_UNIVERSALSTRING:
|
|
|
|
case V_ASN1_BMPSTRING:
|
|
|
|
case V_ASN1_UTF8STRING:
|
|
|
|
case V_ASN1_OTHER:
|
|
|
|
case V_ASN1_SET:
|
|
|
|
case V_ASN1_SEQUENCE:
|
|
|
|
default:
|
2009-03-25 12:53:50 +00:00
|
|
|
if (utype == V_ASN1_BMPSTRING && (len & 1))
|
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_ASN1_EX_C2I,
|
|
|
|
ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (utype == V_ASN1_UNIVERSALSTRING && (len & 3))
|
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_ASN1_EX_C2I,
|
|
|
|
ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
|
|
|
|
goto err;
|
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
/* All based on ASN1_STRING and handled the same */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!*pval)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
stmp = ASN1_STRING_type_new(utype);
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!stmp)
|
|
|
|
{
|
2005-05-11 03:45:39 +00:00
|
|
|
ASN1err(ASN1_F_ASN1_EX_C2I,
|
2004-04-24 17:02:48 +00:00
|
|
|
ERR_R_MALLOC_FAILURE);
|
2000-12-08 19:09:35 +00:00
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
*pval = (ASN1_VALUE *)stmp;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
stmp = (ASN1_STRING *)*pval;
|
|
|
|
stmp->type = utype;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
/* If we've already allocated a buffer use it */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (*free_cont)
|
|
|
|
{
|
|
|
|
if (stmp->data)
|
|
|
|
OPENSSL_free(stmp->data);
|
2004-03-15 23:15:26 +00:00
|
|
|
stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
|
2000-12-08 19:09:35 +00:00
|
|
|
stmp->length = len;
|
|
|
|
*free_cont = 0;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!ASN1_STRING_set(stmp, cont, len))
|
|
|
|
{
|
2005-05-11 03:45:39 +00:00
|
|
|
ASN1err(ASN1_F_ASN1_EX_C2I,
|
2004-04-24 17:02:48 +00:00
|
|
|
ERR_R_MALLOC_FAILURE);
|
2000-12-08 19:09:35 +00:00
|
|
|
ASN1_STRING_free(stmp);
|
|
|
|
*pval = NULL;
|
|
|
|
goto err;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
}
|
|
|
|
break;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
/* If ASN1_ANY and NULL type fix up value */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (typ && (utype == V_ASN1_NULL))
|
|
|
|
typ->value.ptr = NULL;
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
err:
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!ret)
|
2003-09-30 16:47:33 +00:00
|
|
|
{
|
|
|
|
ASN1_TYPE_free(typ);
|
|
|
|
if (opval)
|
|
|
|
*opval = NULL;
|
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
return ret;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
2005-04-30 13:06:45 +00:00
|
|
|
|
|
|
|
/* This function finds the end of an ASN1 structure when passed its maximum
|
|
|
|
* length, whether it is indefinite length and a pointer to the content.
|
|
|
|
* This is more efficient than calling asn1_collect because it does not
|
|
|
|
* recurse on each indefinite length header.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int asn1_find_end(const unsigned char **in, long len, char inf)
|
|
|
|
{
|
|
|
|
int expected_eoc;
|
|
|
|
long plen;
|
|
|
|
const unsigned char *p = *in, *q;
|
|
|
|
/* If not indefinite length constructed just add length */
|
|
|
|
if (inf == 0)
|
|
|
|
{
|
|
|
|
*in += len;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
expected_eoc = 1;
|
|
|
|
/* Indefinite length constructed form. Find the end when enough EOCs
|
|
|
|
* are found. If more indefinite length constructed headers
|
2005-04-30 18:07:30 +00:00
|
|
|
* are encountered increment the expected eoc count otherwise just
|
2005-04-30 13:06:45 +00:00
|
|
|
* skip to the end of the data.
|
|
|
|
*/
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
if(asn1_check_eoc(&p, len))
|
|
|
|
{
|
|
|
|
expected_eoc--;
|
|
|
|
if (expected_eoc == 0)
|
|
|
|
break;
|
|
|
|
len -= 2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (inf)
|
|
|
|
expected_eoc++;
|
|
|
|
else
|
|
|
|
p += plen;
|
|
|
|
len -= p - q;
|
|
|
|
}
|
|
|
|
if (expected_eoc)
|
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*in = p;
|
|
|
|
return 1;
|
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
/* This function collects the asn1 data from a constructred string
|
|
|
|
* type into a buffer. The values of 'in' and 'len' should refer
|
|
|
|
* to the contents of the constructed type and 'inf' should be set
|
2005-04-30 13:06:45 +00:00
|
|
|
* if it is indefinite length.
|
2000-12-08 19:09:35 +00:00
|
|
|
*/
|
|
|
|
|
2009-03-14 18:33:49 +00:00
|
|
|
#ifndef ASN1_MAX_STRING_NEST
|
|
|
|
/* This determines how many levels of recursion are permitted in ASN1
|
|
|
|
* string types. If it is not limited stack overflows can occur. If set
|
|
|
|
* to zero no recursion is allowed at all. Although zero should be adequate
|
|
|
|
* examples exist that require a value of 1. So 5 should be more than enough.
|
|
|
|
*/
|
|
|
|
#define ASN1_MAX_STRING_NEST 5
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
|
2009-03-14 18:33:49 +00:00
|
|
|
char inf, int tag, int aclass, int depth)
|
2004-04-24 17:02:48 +00:00
|
|
|
{
|
2004-03-15 23:15:26 +00:00
|
|
|
const unsigned char *p, *q;
|
2000-12-08 19:09:35 +00:00
|
|
|
long plen;
|
|
|
|
char cst, ininf;
|
|
|
|
p = *in;
|
|
|
|
inf &= 1;
|
2004-04-24 17:02:48 +00:00
|
|
|
/* If no buffer and not indefinite length constructed just pass over
|
|
|
|
* the encoded data */
|
|
|
|
if (!buf && !inf)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
*in += len;
|
|
|
|
return 1;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
while(len > 0)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
q = p;
|
|
|
|
/* Check for EOC */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (asn1_check_eoc(&p, len))
|
|
|
|
{
|
|
|
|
/* EOC is illegal outside indefinite length
|
|
|
|
* constructed form */
|
|
|
|
if (!inf)
|
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_ASN1_COLLECT,
|
|
|
|
ASN1_R_UNEXPECTED_EOC);
|
2000-12-08 19:09:35 +00:00
|
|
|
return 0;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
inf = 0;
|
|
|
|
break;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
|
|
|
|
len, tag, aclass, 0, NULL))
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
|
|
|
|
return 0;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
|
2000-12-08 19:09:35 +00:00
|
|
|
/* If indefinite length constructed update max length */
|
2004-04-24 17:02:48 +00:00
|
|
|
if (cst)
|
|
|
|
{
|
2009-03-14 18:33:49 +00:00
|
|
|
if (depth >= ASN1_MAX_STRING_NEST)
|
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_ASN1_COLLECT,
|
|
|
|
ASN1_R_NESTED_ASN1_STRING);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!asn1_collect(buf, &p, plen, ininf, tag, aclass,
|
|
|
|
depth + 1))
|
2004-04-24 17:02:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-01-31 18:36:29 +00:00
|
|
|
else if (plen && !collect_data(buf, &p, plen))
|
2004-04-24 17:02:48 +00:00
|
|
|
return 0;
|
2000-12-08 19:09:35 +00:00
|
|
|
len -= p - q;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
if (inf)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
|
|
|
|
return 0;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
*in = p;
|
|
|
|
return 1;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
2004-03-15 23:15:26 +00:00
|
|
|
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
|
2004-04-24 17:02:48 +00:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
if (buf)
|
|
|
|
{
|
|
|
|
len = buf->length;
|
|
|
|
if (!BUF_MEM_grow_clean(buf, len + plen))
|
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
|
|
|
|
return 0;
|
2000-12-08 19:09:35 +00:00
|
|
|
}
|
2004-04-24 17:02:48 +00:00
|
|
|
memcpy(buf->data + len, *p, plen);
|
2000-12-08 19:09:35 +00:00
|
|
|
}
|
2004-04-24 17:02:48 +00:00
|
|
|
*p += plen;
|
|
|
|
return 1;
|
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
/* Check for ASN1 EOC and swallow it if found */
|
|
|
|
|
2004-03-15 23:15:26 +00:00
|
|
|
static int asn1_check_eoc(const unsigned char **in, long len)
|
2004-04-24 17:02:48 +00:00
|
|
|
{
|
2004-03-15 23:15:26 +00:00
|
|
|
const unsigned char *p;
|
2004-04-24 17:02:48 +00:00
|
|
|
if (len < 2) return 0;
|
2000-12-08 19:09:35 +00:00
|
|
|
p = *in;
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!p[0] && !p[1])
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
*in += 2;
|
|
|
|
return 1;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
return 0;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
/* Check an ASN1 tag and length: a bit like ASN1_get_object
|
|
|
|
* but it sets the length for indefinite length constructed
|
|
|
|
* form, we don't know the exact length but we can set an
|
|
|
|
* upper bound to the amount of data available minus the
|
|
|
|
* header length just read.
|
|
|
|
*/
|
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
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)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
int i;
|
|
|
|
int ptag, pclass;
|
2008-11-12 03:58:08 +00:00
|
|
|
long plen;
|
2004-03-15 23:15:26 +00:00
|
|
|
const unsigned char *p, *q;
|
2000-12-08 19:09:35 +00:00
|
|
|
p = *in;
|
|
|
|
q = p;
|
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
if (ctx && ctx->valid)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
i = ctx->ret;
|
|
|
|
plen = ctx->plen;
|
|
|
|
pclass = ctx->pclass;
|
|
|
|
ptag = ctx->ptag;
|
|
|
|
p += ctx->hdrlen;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
|
2004-04-24 17:02:48 +00:00
|
|
|
if (ctx)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
ctx->ret = i;
|
|
|
|
ctx->plen = plen;
|
|
|
|
ctx->pclass = pclass;
|
|
|
|
ctx->ptag = ptag;
|
|
|
|
ctx->hdrlen = p - q;
|
|
|
|
ctx->valid = 1;
|
2002-08-23 00:02:11 +00:00
|
|
|
/* If definite length, and no error, length +
|
|
|
|
* header can't exceed total amount of data available.
|
2000-12-31 01:13:04 +00:00
|
|
|
*/
|
2004-04-24 17:02:48 +00:00
|
|
|
if (!(i & 0x81) && ((plen + ctx->hdrlen) > len))
|
|
|
|
{
|
|
|
|
ASN1err(ASN1_F_ASN1_CHECK_TLEN,
|
|
|
|
ASN1_R_TOO_LONG);
|
2000-12-31 01:13:04 +00:00
|
|
|
asn1_tlc_clear(ctx);
|
|
|
|
return 0;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-31 01:13:04 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
}
|
2001-04-21 12:06:01 +00:00
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
if (i & 0x80)
|
|
|
|
{
|
2000-12-08 19:09:35 +00:00
|
|
|
ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
|
|
|
|
asn1_tlc_clear(ctx);
|
|
|
|
return 0;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
if (exptag >= 0)
|
|
|
|
{
|
|
|
|
if ((exptag != ptag) || (expclass != pclass))
|
|
|
|
{
|
|
|
|
/* If type is OPTIONAL, not an error:
|
|
|
|
* indicate missing type.
|
2000-12-08 19:09:35 +00:00
|
|
|
*/
|
2004-04-24 17:02:48 +00:00
|
|
|
if (opt) return -1;
|
2000-12-08 19:09:35 +00:00
|
|
|
asn1_tlc_clear(ctx);
|
|
|
|
ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
|
|
|
|
return 0;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
|
|
|
/* We have a tag and class match:
|
|
|
|
* assume we are going to do something with it */
|
2000-12-08 19:09:35 +00:00
|
|
|
asn1_tlc_clear(ctx);
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|
2000-12-08 19:09:35 +00:00
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
if (i & 1)
|
|
|
|
plen = len - (p - q);
|
2000-12-08 19:09:35 +00:00
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
if (inf)
|
|
|
|
*inf = i & 1;
|
2000-12-08 19:09:35 +00:00
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
if (cst)
|
|
|
|
*cst = i & V_ASN1_CONSTRUCTED;
|
2000-12-08 19:09:35 +00:00
|
|
|
|
2004-04-24 17:02:48 +00:00
|
|
|
if (olen)
|
|
|
|
*olen = plen;
|
|
|
|
|
|
|
|
if (oclass)
|
|
|
|
*oclass = pclass;
|
|
|
|
|
|
|
|
if (otag)
|
|
|
|
*otag = ptag;
|
2000-12-08 19:09:35 +00:00
|
|
|
|
|
|
|
*in = p;
|
|
|
|
return 1;
|
2004-04-24 17:02:48 +00:00
|
|
|
}
|