10Sstevel@tonic-gate /* tasn_dec.c */
20Sstevel@tonic-gate /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
30Sstevel@tonic-gate * project 2000.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate /* ====================================================================
62139Sjp161948 * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate * are met:
110Sstevel@tonic-gate *
120Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate *
150Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate * the documentation and/or other materials provided with the
180Sstevel@tonic-gate * distribution.
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate * software must display the following acknowledgment:
220Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
240Sstevel@tonic-gate *
250Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate * endorse or promote products derived from this software without
270Sstevel@tonic-gate * prior written permission. For written permission, please contact
280Sstevel@tonic-gate * licensing@OpenSSL.org.
290Sstevel@tonic-gate *
300Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate * permission of the OpenSSL Project.
330Sstevel@tonic-gate *
340Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate * acknowledgment:
360Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
380Sstevel@tonic-gate *
390Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate * ====================================================================
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim
550Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate *
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate
600Sstevel@tonic-gate #include <stddef.h>
610Sstevel@tonic-gate #include <string.h>
620Sstevel@tonic-gate #include <openssl/asn1.h>
630Sstevel@tonic-gate #include <openssl/asn1t.h>
640Sstevel@tonic-gate #include <openssl/objects.h>
650Sstevel@tonic-gate #include <openssl/buffer.h>
660Sstevel@tonic-gate #include <openssl/err.h>
670Sstevel@tonic-gate
682139Sjp161948 static int asn1_check_eoc(const unsigned char **in, long len);
692139Sjp161948 static int asn1_find_end(const unsigned char **in, long len, char inf);
702139Sjp161948
712139Sjp161948 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
722139Sjp161948 char inf, int tag, int aclass);
732139Sjp161948
742139Sjp161948 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
752139Sjp161948
762139Sjp161948 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
772139Sjp161948 char *inf, char *cst,
782139Sjp161948 const unsigned char **in, long len,
792139Sjp161948 int exptag, int expclass, char opt,
802139Sjp161948 ASN1_TLC *ctx);
812139Sjp161948
822139Sjp161948 static int asn1_template_ex_d2i(ASN1_VALUE **pval,
832139Sjp161948 const unsigned char **in, long len,
842139Sjp161948 const ASN1_TEMPLATE *tt, char opt,
852139Sjp161948 ASN1_TLC *ctx);
862139Sjp161948 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
872139Sjp161948 const unsigned char **in, long len,
882139Sjp161948 const ASN1_TEMPLATE *tt, char opt,
892139Sjp161948 ASN1_TLC *ctx);
902139Sjp161948 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
912139Sjp161948 const unsigned char **in, long len,
922139Sjp161948 const ASN1_ITEM *it,
932139Sjp161948 int tag, int aclass, char opt, ASN1_TLC *ctx);
940Sstevel@tonic-gate
950Sstevel@tonic-gate /* Table to convert tags to bit values, used for MSTRING type */
962139Sjp161948 static unsigned long tag2bit[32] = {
970Sstevel@tonic-gate 0, 0, 0, B_ASN1_BIT_STRING, /* tags 0 - 3 */
980Sstevel@tonic-gate B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,/* tags 4- 7 */
990Sstevel@tonic-gate B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,/* tags 8-11 */
1000Sstevel@tonic-gate B_ASN1_UTF8STRING,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,/* tags 12-15 */
1010Sstevel@tonic-gate 0, 0, B_ASN1_NUMERICSTRING,B_ASN1_PRINTABLESTRING, /* tags 16-19 */
1020Sstevel@tonic-gate B_ASN1_T61STRING,B_ASN1_VIDEOTEXSTRING,B_ASN1_IA5STRING, /* tags 20-22 */
1030Sstevel@tonic-gate B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME, /* tags 23-24 */
1040Sstevel@tonic-gate B_ASN1_GRAPHICSTRING,B_ASN1_ISO64STRING,B_ASN1_GENERALSTRING, /* tags 25-27 */
1050Sstevel@tonic-gate B_ASN1_UNIVERSALSTRING,B_ASN1_UNKNOWN,B_ASN1_BMPSTRING,B_ASN1_UNKNOWN, /* tags 28-31 */
1060Sstevel@tonic-gate };
1070Sstevel@tonic-gate
ASN1_tag2bit(int tag)1080Sstevel@tonic-gate unsigned long ASN1_tag2bit(int tag)
1092139Sjp161948 {
1102139Sjp161948 if ((tag < 0) || (tag > 30)) return 0;
1110Sstevel@tonic-gate return tag2bit[tag];
1122139Sjp161948 }
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate /* Macro to initialize and invalidate the cache */
1150Sstevel@tonic-gate
1162139Sjp161948 #define asn1_tlc_clear(c) if (c) (c)->valid = 0
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate /* Decode an ASN1 item, this currently behaves just
1190Sstevel@tonic-gate * like a standard 'd2i' function. 'in' points to
1200Sstevel@tonic-gate * a buffer to read the data from, in future we will
1210Sstevel@tonic-gate * have more advanced versions that can input data
1220Sstevel@tonic-gate * a piece at a time and this will simply be a special
1230Sstevel@tonic-gate * case.
1240Sstevel@tonic-gate */
1250Sstevel@tonic-gate
ASN1_item_d2i(ASN1_VALUE ** pval,const unsigned char ** in,long len,const ASN1_ITEM * it)1262139Sjp161948 ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
1272139Sjp161948 const unsigned char **in, long len, const ASN1_ITEM *it)
1282139Sjp161948 {
1290Sstevel@tonic-gate ASN1_TLC c;
1300Sstevel@tonic-gate ASN1_VALUE *ptmpval = NULL;
1312139Sjp161948 if (!pval)
1322139Sjp161948 pval = &ptmpval;
1330Sstevel@tonic-gate asn1_tlc_clear(&c);
1342139Sjp161948 if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
1350Sstevel@tonic-gate return *pval;
1360Sstevel@tonic-gate return NULL;
1372139Sjp161948 }
1380Sstevel@tonic-gate
ASN1_template_d2i(ASN1_VALUE ** pval,const unsigned char ** in,long len,const ASN1_TEMPLATE * tt)1392139Sjp161948 int ASN1_template_d2i(ASN1_VALUE **pval,
1402139Sjp161948 const unsigned char **in, long len, const ASN1_TEMPLATE *tt)
1412139Sjp161948 {
1420Sstevel@tonic-gate ASN1_TLC c;
1430Sstevel@tonic-gate asn1_tlc_clear(&c);
1440Sstevel@tonic-gate return asn1_template_ex_d2i(pval, in, len, tt, 0, &c);
1452139Sjp161948 }
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate /* Decode an item, taking care of IMPLICIT tagging, if any.
1490Sstevel@tonic-gate * If 'opt' set and tag mismatch return -1 to handle OPTIONAL
1500Sstevel@tonic-gate */
1510Sstevel@tonic-gate
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)1522139Sjp161948 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
1532139Sjp161948 const ASN1_ITEM *it,
1542139Sjp161948 int tag, int aclass, char opt, ASN1_TLC *ctx)
1552139Sjp161948 {
1560Sstevel@tonic-gate const ASN1_TEMPLATE *tt, *errtt = NULL;
1570Sstevel@tonic-gate const ASN1_COMPAT_FUNCS *cf;
1580Sstevel@tonic-gate const ASN1_EXTERN_FUNCS *ef;
1590Sstevel@tonic-gate const ASN1_AUX *aux = it->funcs;
1600Sstevel@tonic-gate ASN1_aux_cb *asn1_cb;
1612139Sjp161948 const unsigned char *p, *q;
1622139Sjp161948 unsigned char *wp=NULL; /* BIG FAT WARNING! BREAKS CONST WHERE USED */
1632139Sjp161948 unsigned char imphack = 0, oclass;
1640Sstevel@tonic-gate char seq_eoc, seq_nolen, cst, isopt;
1650Sstevel@tonic-gate long tmplen;
1660Sstevel@tonic-gate int i;
1670Sstevel@tonic-gate int otag;
1680Sstevel@tonic-gate int ret = 0;
1690Sstevel@tonic-gate ASN1_VALUE *pchval, **pchptr, *ptmpval;
1702139Sjp161948 if (!pval)
1712139Sjp161948 return 0;
1722139Sjp161948 if (aux && aux->asn1_cb)
1732139Sjp161948 asn1_cb = aux->asn1_cb;
1740Sstevel@tonic-gate else asn1_cb = 0;
1750Sstevel@tonic-gate
1762139Sjp161948 switch(it->itype)
1772139Sjp161948 {
1780Sstevel@tonic-gate case ASN1_ITYPE_PRIMITIVE:
1792139Sjp161948 if (it->templates)
1802139Sjp161948 {
1812139Sjp161948 /* tagging or OPTIONAL is currently illegal on an item
1822139Sjp161948 * template because the flags can't get passed down.
1832139Sjp161948 * In practice this isn't a problem: we include the
1842139Sjp161948 * relevant flags from the item template in the
1852139Sjp161948 * template itself.
1860Sstevel@tonic-gate */
1872139Sjp161948 if ((tag != -1) || opt)
1882139Sjp161948 {
1892139Sjp161948 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
1902139Sjp161948 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
1910Sstevel@tonic-gate goto err;
1922139Sjp161948 }
1932139Sjp161948 return asn1_template_ex_d2i(pval, in, len,
1942139Sjp161948 it->templates, opt, ctx);
1950Sstevel@tonic-gate }
1962139Sjp161948 return asn1_d2i_ex_primitive(pval, in, len, it,
1972139Sjp161948 tag, aclass, opt, ctx);
1980Sstevel@tonic-gate break;
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate case ASN1_ITYPE_MSTRING:
2010Sstevel@tonic-gate p = *in;
2020Sstevel@tonic-gate /* Just read in tag and class */
2032139Sjp161948 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
2042139Sjp161948 &p, len, -1, 0, 1, ctx);
2052139Sjp161948 if (!ret)
2062139Sjp161948 {
2072139Sjp161948 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
2082139Sjp161948 ERR_R_NESTED_ASN1_ERROR);
2090Sstevel@tonic-gate goto err;
2102139Sjp161948 }
2112139Sjp161948
2120Sstevel@tonic-gate /* Must be UNIVERSAL class */
2132139Sjp161948 if (oclass != V_ASN1_UNIVERSAL)
2142139Sjp161948 {
2150Sstevel@tonic-gate /* If OPTIONAL, assume this is OK */
2162139Sjp161948 if (opt) return -1;
2172139Sjp161948 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
2182139Sjp161948 ASN1_R_MSTRING_NOT_UNIVERSAL);
2190Sstevel@tonic-gate goto err;
2202139Sjp161948 }
2210Sstevel@tonic-gate /* Check tag matches bit map */
2222139Sjp161948 if (!(ASN1_tag2bit(otag) & it->utype))
2232139Sjp161948 {
2240Sstevel@tonic-gate /* If OPTIONAL, assume this is OK */
2252139Sjp161948 if (opt)
2262139Sjp161948 return -1;
2272139Sjp161948 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
2282139Sjp161948 ASN1_R_MSTRING_WRONG_TAG);
2290Sstevel@tonic-gate goto err;
2302139Sjp161948 }
2312139Sjp161948 return asn1_d2i_ex_primitive(pval, in, len,
2322139Sjp161948 it, otag, 0, 0, ctx);
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate case ASN1_ITYPE_EXTERN:
2350Sstevel@tonic-gate /* Use new style d2i */
2360Sstevel@tonic-gate ef = it->funcs;
2372139Sjp161948 return ef->asn1_ex_d2i(pval, in, len,
2382139Sjp161948 it, tag, aclass, opt, ctx);
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate case ASN1_ITYPE_COMPAT:
2410Sstevel@tonic-gate /* we must resort to old style evil hackery */
2420Sstevel@tonic-gate cf = it->funcs;
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate /* If OPTIONAL see if it is there */
2452139Sjp161948 if (opt)
2462139Sjp161948 {
2470Sstevel@tonic-gate int exptag;
2480Sstevel@tonic-gate p = *in;
2492139Sjp161948 if (tag == -1)
2502139Sjp161948 exptag = it->utype;
2510Sstevel@tonic-gate else exptag = tag;
2522139Sjp161948 /* Don't care about anything other than presence
2532139Sjp161948 * of expected tag */
2542139Sjp161948
2552139Sjp161948 ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL,
2562139Sjp161948 &p, len, exptag, aclass, 1, ctx);
2572139Sjp161948 if (!ret)
2582139Sjp161948 {
2592139Sjp161948 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
2602139Sjp161948 ERR_R_NESTED_ASN1_ERROR);
2610Sstevel@tonic-gate goto err;
2622139Sjp161948 }
2632139Sjp161948 if (ret == -1)
2642139Sjp161948 return -1;
2650Sstevel@tonic-gate }
2662139Sjp161948
2670Sstevel@tonic-gate /* This is the old style evil hack IMPLICIT handling:
2680Sstevel@tonic-gate * since the underlying code is expecting a tag and
2690Sstevel@tonic-gate * class other than the one present we change the
2700Sstevel@tonic-gate * buffer temporarily then change it back afterwards.
2710Sstevel@tonic-gate * This doesn't and never did work for tags > 30.
2720Sstevel@tonic-gate *
2730Sstevel@tonic-gate * Yes this is *horrible* but it is only needed for
2740Sstevel@tonic-gate * old style d2i which will hopefully not be around
2750Sstevel@tonic-gate * for much longer.
2760Sstevel@tonic-gate * FIXME: should copy the buffer then modify it so
2770Sstevel@tonic-gate * the input buffer can be const: we should *always*
2780Sstevel@tonic-gate * copy because the old style d2i might modify the
2790Sstevel@tonic-gate * buffer.
2800Sstevel@tonic-gate */
2810Sstevel@tonic-gate
2822139Sjp161948 if (tag != -1)
2832139Sjp161948 {
2842139Sjp161948 wp = *(unsigned char **)in;
2852139Sjp161948 imphack = *wp;
2862139Sjp161948 *wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED)
2872139Sjp161948 | it->utype);
2882139Sjp161948 }
2890Sstevel@tonic-gate
2900Sstevel@tonic-gate ptmpval = cf->asn1_d2i(pval, in, len);
2910Sstevel@tonic-gate
2922139Sjp161948 if (tag != -1)
2932139Sjp161948 *wp = imphack;
2940Sstevel@tonic-gate
2952139Sjp161948 if (ptmpval)
2962139Sjp161948 return 1;
2972139Sjp161948
2980Sstevel@tonic-gate ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
2990Sstevel@tonic-gate goto err;
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate case ASN1_ITYPE_CHOICE:
3032139Sjp161948 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it))
3040Sstevel@tonic-gate goto auxerr;
3050Sstevel@tonic-gate
3060Sstevel@tonic-gate /* Allocate structure */
3072139Sjp161948 if (!*pval && !ASN1_item_ex_new(pval, it))
3082139Sjp161948 {
3092139Sjp161948 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
3102139Sjp161948 ERR_R_NESTED_ASN1_ERROR);
3112139Sjp161948 goto err;
3120Sstevel@tonic-gate }
3130Sstevel@tonic-gate /* CHOICE type, try each possibility in turn */
3140Sstevel@tonic-gate pchval = NULL;
3150Sstevel@tonic-gate p = *in;
3162139Sjp161948 for (i = 0, tt=it->templates; i < it->tcount; i++, tt++)
3172139Sjp161948 {
3180Sstevel@tonic-gate pchptr = asn1_get_field_ptr(pval, tt);
3190Sstevel@tonic-gate /* We mark field as OPTIONAL so its absence
3200Sstevel@tonic-gate * can be recognised.
3210Sstevel@tonic-gate */
3220Sstevel@tonic-gate ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);
3230Sstevel@tonic-gate /* If field not present, try the next one */
3242139Sjp161948 if (ret == -1)
3252139Sjp161948 continue;
3260Sstevel@tonic-gate /* If positive return, read OK, break loop */
3272139Sjp161948 if (ret > 0)
3282139Sjp161948 break;
3290Sstevel@tonic-gate /* Otherwise must be an ASN1 parsing error */
3300Sstevel@tonic-gate errtt = tt;
3312139Sjp161948 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
3322139Sjp161948 ERR_R_NESTED_ASN1_ERROR);
3330Sstevel@tonic-gate goto err;
3342139Sjp161948 }
3352139Sjp161948
3360Sstevel@tonic-gate /* Did we fall off the end without reading anything? */
3372139Sjp161948 if (i == it->tcount)
3382139Sjp161948 {
3390Sstevel@tonic-gate /* If OPTIONAL, this is OK */
3402139Sjp161948 if (opt)
3412139Sjp161948 {
3420Sstevel@tonic-gate /* Free and zero it */
3430Sstevel@tonic-gate ASN1_item_ex_free(pval, it);
3440Sstevel@tonic-gate return -1;
3452139Sjp161948 }
3462139Sjp161948 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
3472139Sjp161948 ASN1_R_NO_MATCHING_CHOICE_TYPE);
3482139Sjp161948 goto err;
3490Sstevel@tonic-gate }
3502139Sjp161948
3510Sstevel@tonic-gate asn1_set_choice_selector(pval, i, it);
3520Sstevel@tonic-gate *in = p;
3532139Sjp161948 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it))
3540Sstevel@tonic-gate goto auxerr;
3550Sstevel@tonic-gate return 1;
3560Sstevel@tonic-gate
3572139Sjp161948 case ASN1_ITYPE_NDEF_SEQUENCE:
3580Sstevel@tonic-gate case ASN1_ITYPE_SEQUENCE:
3590Sstevel@tonic-gate p = *in;
3600Sstevel@tonic-gate tmplen = len;
3610Sstevel@tonic-gate
3620Sstevel@tonic-gate /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
3632139Sjp161948 if (tag == -1)
3642139Sjp161948 {
3650Sstevel@tonic-gate tag = V_ASN1_SEQUENCE;
3660Sstevel@tonic-gate aclass = V_ASN1_UNIVERSAL;
3672139Sjp161948 }
3680Sstevel@tonic-gate /* Get SEQUENCE length and update len, p */
3692139Sjp161948 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
3702139Sjp161948 &p, len, tag, aclass, opt, ctx);
3712139Sjp161948 if (!ret)
3722139Sjp161948 {
3732139Sjp161948 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
3742139Sjp161948 ERR_R_NESTED_ASN1_ERROR);
3750Sstevel@tonic-gate goto err;
3762139Sjp161948 }
3772139Sjp161948 else if (ret == -1)
3782139Sjp161948 return -1;
3792139Sjp161948 if (aux && (aux->flags & ASN1_AFLG_BROKEN))
3802139Sjp161948 {
3810Sstevel@tonic-gate len = tmplen - (p - *in);
3820Sstevel@tonic-gate seq_nolen = 1;
3832139Sjp161948 }
3842139Sjp161948 /* If indefinite we don't do a length check */
3852139Sjp161948 else seq_nolen = seq_eoc;
3862139Sjp161948 if (!cst)
3872139Sjp161948 {
3882139Sjp161948 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
3892139Sjp161948 ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
3900Sstevel@tonic-gate goto err;
3912139Sjp161948 }
3920Sstevel@tonic-gate
3932139Sjp161948 if (!*pval && !ASN1_item_ex_new(pval, it))
3942139Sjp161948 {
3952139Sjp161948 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
3962139Sjp161948 ERR_R_NESTED_ASN1_ERROR);
3972139Sjp161948 goto err;
3980Sstevel@tonic-gate }
3992139Sjp161948
4002139Sjp161948 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it))
4010Sstevel@tonic-gate goto auxerr;
4020Sstevel@tonic-gate
4030Sstevel@tonic-gate /* Get each field entry */
4042139Sjp161948 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++)
4052139Sjp161948 {
4060Sstevel@tonic-gate const ASN1_TEMPLATE *seqtt;
4070Sstevel@tonic-gate ASN1_VALUE **pseqval;
4080Sstevel@tonic-gate seqtt = asn1_do_adb(pval, tt, 1);
4092139Sjp161948 if (!seqtt)
4102139Sjp161948 goto err;
4110Sstevel@tonic-gate pseqval = asn1_get_field_ptr(pval, seqtt);
4120Sstevel@tonic-gate /* Have we ran out of data? */
4132139Sjp161948 if (!len)
4142139Sjp161948 break;
4150Sstevel@tonic-gate q = p;
4162139Sjp161948 if (asn1_check_eoc(&p, len))
4172139Sjp161948 {
4182139Sjp161948 if (!seq_eoc)
4192139Sjp161948 {
4202139Sjp161948 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
4212139Sjp161948 ASN1_R_UNEXPECTED_EOC);
4220Sstevel@tonic-gate goto err;
4232139Sjp161948 }
4240Sstevel@tonic-gate len -= p - q;
4250Sstevel@tonic-gate seq_eoc = 0;
4260Sstevel@tonic-gate q = p;
4270Sstevel@tonic-gate break;
4282139Sjp161948 }
4292139Sjp161948 /* This determines the OPTIONAL flag value. The field
4302139Sjp161948 * cannot be omitted if it is the last of a SEQUENCE
4312139Sjp161948 * and there is still data to be read. This isn't
4322139Sjp161948 * strictly necessary but it increases efficiency in
4332139Sjp161948 * some cases.
4340Sstevel@tonic-gate */
4352139Sjp161948 if (i == (it->tcount - 1))
4362139Sjp161948 isopt = 0;
4370Sstevel@tonic-gate else isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
4382139Sjp161948 /* attempt to read in field, allowing each to be
4392139Sjp161948 * OPTIONAL */
4402139Sjp161948
4412139Sjp161948 ret = asn1_template_ex_d2i(pseqval, &p, len,
4422139Sjp161948 seqtt, isopt, ctx);
4432139Sjp161948 if (!ret)
4442139Sjp161948 {
4450Sstevel@tonic-gate errtt = seqtt;
4460Sstevel@tonic-gate goto err;
4472139Sjp161948 }
4482139Sjp161948 else if (ret == -1)
4492139Sjp161948 {
4502139Sjp161948 /* OPTIONAL component absent.
4512139Sjp161948 * Free and zero the field.
4520Sstevel@tonic-gate */
4530Sstevel@tonic-gate ASN1_template_free(pseqval, seqtt);
4540Sstevel@tonic-gate continue;
4552139Sjp161948 }
4560Sstevel@tonic-gate /* Update length */
4570Sstevel@tonic-gate len -= p - q;
4582139Sjp161948 }
4592139Sjp161948
4600Sstevel@tonic-gate /* Check for EOC if expecting one */
4612139Sjp161948 if (seq_eoc && !asn1_check_eoc(&p, len))
4622139Sjp161948 {
4630Sstevel@tonic-gate ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MISSING_EOC);
4640Sstevel@tonic-gate goto err;
4652139Sjp161948 }
4660Sstevel@tonic-gate /* Check all data read */
4672139Sjp161948 if (!seq_nolen && len)
4682139Sjp161948 {
4692139Sjp161948 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
4702139Sjp161948 ASN1_R_SEQUENCE_LENGTH_MISMATCH);
4710Sstevel@tonic-gate goto err;
4722139Sjp161948 }
4730Sstevel@tonic-gate
4740Sstevel@tonic-gate /* If we get here we've got no more data in the SEQUENCE,
4750Sstevel@tonic-gate * however we may not have read all fields so check all
4760Sstevel@tonic-gate * remaining are OPTIONAL and clear any that are.
4770Sstevel@tonic-gate */
4782139Sjp161948 for (; i < it->tcount; tt++, i++)
4792139Sjp161948 {
4800Sstevel@tonic-gate const ASN1_TEMPLATE *seqtt;
4810Sstevel@tonic-gate seqtt = asn1_do_adb(pval, tt, 1);
4822139Sjp161948 if (!seqtt)
4832139Sjp161948 goto err;
4842139Sjp161948 if (seqtt->flags & ASN1_TFLG_OPTIONAL)
4852139Sjp161948 {
4860Sstevel@tonic-gate ASN1_VALUE **pseqval;
4870Sstevel@tonic-gate pseqval = asn1_get_field_ptr(pval, seqtt);
4880Sstevel@tonic-gate ASN1_template_free(pseqval, seqtt);
4892139Sjp161948 }
4902139Sjp161948 else
4912139Sjp161948 {
4920Sstevel@tonic-gate errtt = seqtt;
4932139Sjp161948 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
4942139Sjp161948 ASN1_R_FIELD_MISSING);
4950Sstevel@tonic-gate goto err;
4962139Sjp161948 }
4970Sstevel@tonic-gate }
4980Sstevel@tonic-gate /* Save encoding */
4992139Sjp161948 if (!asn1_enc_save(pval, *in, p - *in, it))
5002139Sjp161948 goto auxerr;
5010Sstevel@tonic-gate *in = p;
5022139Sjp161948 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it))
5030Sstevel@tonic-gate goto auxerr;
5040Sstevel@tonic-gate return 1;
5050Sstevel@tonic-gate
5060Sstevel@tonic-gate default:
5070Sstevel@tonic-gate return 0;
5082139Sjp161948 }
5090Sstevel@tonic-gate auxerr:
5100Sstevel@tonic-gate ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR);
5110Sstevel@tonic-gate err:
5120Sstevel@tonic-gate ASN1_item_ex_free(pval, it);
5132139Sjp161948 if (errtt)
5142139Sjp161948 ERR_add_error_data(4, "Field=", errtt->field_name,
5152139Sjp161948 ", Type=", it->sname);
5162139Sjp161948 else
5172139Sjp161948 ERR_add_error_data(2, "Type=", it->sname);
5180Sstevel@tonic-gate return 0;
5192139Sjp161948 }
5200Sstevel@tonic-gate
5212139Sjp161948 /* Templates are handled with two separate functions.
5222139Sjp161948 * One handles any EXPLICIT tag and the other handles the rest.
5230Sstevel@tonic-gate */
5240Sstevel@tonic-gate
asn1_template_ex_d2i(ASN1_VALUE ** val,const unsigned char ** in,long inlen,const ASN1_TEMPLATE * tt,char opt,ASN1_TLC * ctx)5252139Sjp161948 static int asn1_template_ex_d2i(ASN1_VALUE **val,
5262139Sjp161948 const unsigned char **in, long inlen,
5272139Sjp161948 const ASN1_TEMPLATE *tt, char opt,
5282139Sjp161948 ASN1_TLC *ctx)
5292139Sjp161948 {
5300Sstevel@tonic-gate int flags, aclass;
5310Sstevel@tonic-gate int ret;
5320Sstevel@tonic-gate long len;
5332139Sjp161948 const unsigned char *p, *q;
5340Sstevel@tonic-gate char exp_eoc;
5352139Sjp161948 if (!val)
5362139Sjp161948 return 0;
5370Sstevel@tonic-gate flags = tt->flags;
5380Sstevel@tonic-gate aclass = flags & ASN1_TFLG_TAG_CLASS;
5390Sstevel@tonic-gate
5400Sstevel@tonic-gate p = *in;
5410Sstevel@tonic-gate
5420Sstevel@tonic-gate /* Check if EXPLICIT tag expected */
5432139Sjp161948 if (flags & ASN1_TFLG_EXPTAG)
5442139Sjp161948 {
5450Sstevel@tonic-gate char cst;
5462139Sjp161948 /* Need to work out amount of data available to the inner
5472139Sjp161948 * content and where it starts: so read in EXPLICIT header to
5482139Sjp161948 * get the info.
5490Sstevel@tonic-gate */
5502139Sjp161948 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
5512139Sjp161948 &p, inlen, tt->tag, aclass, opt, ctx);
5520Sstevel@tonic-gate q = p;
5532139Sjp161948 if (!ret)
5542139Sjp161948 {
5552139Sjp161948 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
5562139Sjp161948 ERR_R_NESTED_ASN1_ERROR);
5570Sstevel@tonic-gate return 0;
5582139Sjp161948 }
5592139Sjp161948 else if (ret == -1)
5602139Sjp161948 return -1;
5612139Sjp161948 if (!cst)
5622139Sjp161948 {
5632139Sjp161948 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
5642139Sjp161948 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
5650Sstevel@tonic-gate return 0;
5662139Sjp161948 }
5670Sstevel@tonic-gate /* We've found the field so it can't be OPTIONAL now */
5680Sstevel@tonic-gate ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
5692139Sjp161948 if (!ret)
5702139Sjp161948 {
5712139Sjp161948 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
5722139Sjp161948 ERR_R_NESTED_ASN1_ERROR);
5730Sstevel@tonic-gate return 0;
5742139Sjp161948 }
5750Sstevel@tonic-gate /* We read the field in OK so update length */
5760Sstevel@tonic-gate len -= p - q;
5772139Sjp161948 if (exp_eoc)
5782139Sjp161948 {
5790Sstevel@tonic-gate /* If NDEF we must have an EOC here */
5802139Sjp161948 if (!asn1_check_eoc(&p, len))
5812139Sjp161948 {
5822139Sjp161948 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
5832139Sjp161948 ASN1_R_MISSING_EOC);
5840Sstevel@tonic-gate goto err;
5852139Sjp161948 }
5860Sstevel@tonic-gate }
5872139Sjp161948 else
5882139Sjp161948 {
5892139Sjp161948 /* Otherwise we must hit the EXPLICIT tag end or its
5902139Sjp161948 * an error */
5912139Sjp161948 if (len)
5922139Sjp161948 {
5932139Sjp161948 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
5942139Sjp161948 ASN1_R_EXPLICIT_LENGTH_MISMATCH);
5950Sstevel@tonic-gate goto err;
5962139Sjp161948 }
5970Sstevel@tonic-gate }
5980Sstevel@tonic-gate }
5992139Sjp161948 else
6002139Sjp161948 return asn1_template_noexp_d2i(val, in, inlen,
6012139Sjp161948 tt, opt, ctx);
6020Sstevel@tonic-gate
6030Sstevel@tonic-gate *in = p;
6040Sstevel@tonic-gate return 1;
6050Sstevel@tonic-gate
6060Sstevel@tonic-gate err:
6070Sstevel@tonic-gate ASN1_template_free(val, tt);
6080Sstevel@tonic-gate *val = NULL;
6090Sstevel@tonic-gate return 0;
6102139Sjp161948 }
6110Sstevel@tonic-gate
asn1_template_noexp_d2i(ASN1_VALUE ** val,const unsigned char ** in,long len,const ASN1_TEMPLATE * tt,char opt,ASN1_TLC * ctx)6122139Sjp161948 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
6132139Sjp161948 const unsigned char **in, long len,
6142139Sjp161948 const ASN1_TEMPLATE *tt, char opt,
6152139Sjp161948 ASN1_TLC *ctx)
6162139Sjp161948 {
6170Sstevel@tonic-gate int flags, aclass;
6180Sstevel@tonic-gate int ret;
6192139Sjp161948 const unsigned char *p, *q;
6202139Sjp161948 if (!val)
6212139Sjp161948 return 0;
6220Sstevel@tonic-gate flags = tt->flags;
6230Sstevel@tonic-gate aclass = flags & ASN1_TFLG_TAG_CLASS;
6240Sstevel@tonic-gate
6250Sstevel@tonic-gate p = *in;
6260Sstevel@tonic-gate q = p;
6270Sstevel@tonic-gate
6282139Sjp161948 if (flags & ASN1_TFLG_SK_MASK)
6292139Sjp161948 {
6300Sstevel@tonic-gate /* SET OF, SEQUENCE OF */
6310Sstevel@tonic-gate int sktag, skaclass;
6320Sstevel@tonic-gate char sk_eoc;
6330Sstevel@tonic-gate /* First work out expected inner tag value */
6342139Sjp161948 if (flags & ASN1_TFLG_IMPTAG)
6352139Sjp161948 {
6360Sstevel@tonic-gate sktag = tt->tag;
6370Sstevel@tonic-gate skaclass = aclass;
6382139Sjp161948 }
6392139Sjp161948 else
6402139Sjp161948 {
6410Sstevel@tonic-gate skaclass = V_ASN1_UNIVERSAL;
6422139Sjp161948 if (flags & ASN1_TFLG_SET_OF)
6432139Sjp161948 sktag = V_ASN1_SET;
6442139Sjp161948 else
6452139Sjp161948 sktag = V_ASN1_SEQUENCE;
6462139Sjp161948 }
6470Sstevel@tonic-gate /* Get the tag */
6482139Sjp161948 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
6492139Sjp161948 &p, len, sktag, skaclass, opt, ctx);
6502139Sjp161948 if (!ret)
6512139Sjp161948 {
6522139Sjp161948 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
6532139Sjp161948 ERR_R_NESTED_ASN1_ERROR);
6540Sstevel@tonic-gate return 0;
6552139Sjp161948 }
6562139Sjp161948 else if (ret == -1)
6572139Sjp161948 return -1;
6582139Sjp161948 if (!*val)
6592139Sjp161948 *val = (ASN1_VALUE *)sk_new_null();
6602139Sjp161948 else
6612139Sjp161948 {
6620Sstevel@tonic-gate /* We've got a valid STACK: free up any items present */
6630Sstevel@tonic-gate STACK *sktmp = (STACK *)*val;
6640Sstevel@tonic-gate ASN1_VALUE *vtmp;
6652139Sjp161948 while(sk_num(sktmp) > 0)
6662139Sjp161948 {
6670Sstevel@tonic-gate vtmp = (ASN1_VALUE *)sk_pop(sktmp);
6682139Sjp161948 ASN1_item_ex_free(&vtmp,
6692139Sjp161948 ASN1_ITEM_ptr(tt->item));
6702139Sjp161948 }
6710Sstevel@tonic-gate }
6720Sstevel@tonic-gate
6732139Sjp161948 if (!*val)
6742139Sjp161948 {
6752139Sjp161948 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
6762139Sjp161948 ERR_R_MALLOC_FAILURE);
6770Sstevel@tonic-gate goto err;
6782139Sjp161948 }
6792139Sjp161948
6800Sstevel@tonic-gate /* Read as many items as we can */
6812139Sjp161948 while(len > 0)
6822139Sjp161948 {
6830Sstevel@tonic-gate ASN1_VALUE *skfield;
6840Sstevel@tonic-gate q = p;
6850Sstevel@tonic-gate /* See if EOC found */
6862139Sjp161948 if (asn1_check_eoc(&p, len))
6872139Sjp161948 {
6882139Sjp161948 if (!sk_eoc)
6892139Sjp161948 {
6902139Sjp161948 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
6912139Sjp161948 ASN1_R_UNEXPECTED_EOC);
6920Sstevel@tonic-gate goto err;
6932139Sjp161948 }
6940Sstevel@tonic-gate len -= p - q;
6950Sstevel@tonic-gate sk_eoc = 0;
6960Sstevel@tonic-gate break;
6972139Sjp161948 }
6980Sstevel@tonic-gate skfield = NULL;
6992139Sjp161948 if (!ASN1_item_ex_d2i(&skfield, &p, len,
7002139Sjp161948 ASN1_ITEM_ptr(tt->item),
7012139Sjp161948 -1, 0, 0, ctx))
7022139Sjp161948 {
7032139Sjp161948 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
7042139Sjp161948 ERR_R_NESTED_ASN1_ERROR);
7050Sstevel@tonic-gate goto err;
7062139Sjp161948 }
7070Sstevel@tonic-gate len -= p - q;
7082139Sjp161948 if (!sk_push((STACK *)*val, (char *)skfield))
7092139Sjp161948 {
7102139Sjp161948 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
7112139Sjp161948 ERR_R_MALLOC_FAILURE);
7120Sstevel@tonic-gate goto err;
7132139Sjp161948 }
7142139Sjp161948 }
7152139Sjp161948 if (sk_eoc)
7162139Sjp161948 {
7172139Sjp161948 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
7182139Sjp161948 goto err;
7190Sstevel@tonic-gate }
7200Sstevel@tonic-gate }
7212139Sjp161948 else if (flags & ASN1_TFLG_IMPTAG)
7222139Sjp161948 {
7230Sstevel@tonic-gate /* IMPLICIT tagging */
7242139Sjp161948 ret = ASN1_item_ex_d2i(val, &p, len,
7252139Sjp161948 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx);
7262139Sjp161948 if (!ret)
7272139Sjp161948 {
7282139Sjp161948 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
7292139Sjp161948 ERR_R_NESTED_ASN1_ERROR);
7300Sstevel@tonic-gate goto err;
7312139Sjp161948 }
7322139Sjp161948 else if (ret == -1)
7332139Sjp161948 return -1;
7342139Sjp161948 }
7352139Sjp161948 else
7362139Sjp161948 {
7370Sstevel@tonic-gate /* Nothing special */
7382139Sjp161948 ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
7392139Sjp161948 -1, 0, opt, ctx);
7402139Sjp161948 if (!ret)
7412139Sjp161948 {
7422139Sjp161948 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
7432139Sjp161948 ERR_R_NESTED_ASN1_ERROR);
7440Sstevel@tonic-gate goto err;
7452139Sjp161948 }
7462139Sjp161948 else if (ret == -1)
7472139Sjp161948 return -1;
7482139Sjp161948 }
7490Sstevel@tonic-gate
7500Sstevel@tonic-gate *in = p;
7510Sstevel@tonic-gate return 1;
7520Sstevel@tonic-gate
7530Sstevel@tonic-gate err:
7540Sstevel@tonic-gate ASN1_template_free(val, tt);
7550Sstevel@tonic-gate *val = NULL;
7560Sstevel@tonic-gate return 0;
7572139Sjp161948 }
7580Sstevel@tonic-gate
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)7592139Sjp161948 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
7602139Sjp161948 const unsigned char **in, long inlen,
7612139Sjp161948 const ASN1_ITEM *it,
7622139Sjp161948 int tag, int aclass, char opt, ASN1_TLC *ctx)
7632139Sjp161948 {
7640Sstevel@tonic-gate int ret = 0, utype;
7650Sstevel@tonic-gate long plen;
7660Sstevel@tonic-gate char cst, inf, free_cont = 0;
7672139Sjp161948 const unsigned char *p;
7680Sstevel@tonic-gate BUF_MEM buf;
7692139Sjp161948 const unsigned char *cont = NULL;
7700Sstevel@tonic-gate long len;
7712139Sjp161948 if (!pval)
7722139Sjp161948 {
7730Sstevel@tonic-gate ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
7740Sstevel@tonic-gate return 0; /* Should never happen */
7752139Sjp161948 }
7760Sstevel@tonic-gate
7772139Sjp161948 if (it->itype == ASN1_ITYPE_MSTRING)
7782139Sjp161948 {
7790Sstevel@tonic-gate utype = tag;
7800Sstevel@tonic-gate tag = -1;
7812139Sjp161948 }
7822139Sjp161948 else
7832139Sjp161948 utype = it->utype;
7840Sstevel@tonic-gate
7852139Sjp161948 if (utype == V_ASN1_ANY)
7862139Sjp161948 {
7870Sstevel@tonic-gate /* If type is ANY need to figure out type from tag */
7880Sstevel@tonic-gate unsigned char oclass;
7892139Sjp161948 if (tag >= 0)
7902139Sjp161948 {
7912139Sjp161948 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
7922139Sjp161948 ASN1_R_ILLEGAL_TAGGED_ANY);
7930Sstevel@tonic-gate return 0;
7942139Sjp161948 }
7952139Sjp161948 if (opt)
7962139Sjp161948 {
7972139Sjp161948 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
7982139Sjp161948 ASN1_R_ILLEGAL_OPTIONAL_ANY);
7990Sstevel@tonic-gate return 0;
8002139Sjp161948 }
8010Sstevel@tonic-gate p = *in;
8022139Sjp161948 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
8032139Sjp161948 &p, inlen, -1, 0, 0, ctx);
8042139Sjp161948 if (!ret)
8052139Sjp161948 {
8062139Sjp161948 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
8072139Sjp161948 ERR_R_NESTED_ASN1_ERROR);
8080Sstevel@tonic-gate return 0;
8092139Sjp161948 }
8102139Sjp161948 if (oclass != V_ASN1_UNIVERSAL)
8112139Sjp161948 utype = V_ASN1_OTHER;
8120Sstevel@tonic-gate }
8132139Sjp161948 if (tag == -1)
8142139Sjp161948 {
8150Sstevel@tonic-gate tag = utype;
8160Sstevel@tonic-gate aclass = V_ASN1_UNIVERSAL;
8172139Sjp161948 }
8180Sstevel@tonic-gate p = *in;
8190Sstevel@tonic-gate /* Check header */
8202139Sjp161948 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
8212139Sjp161948 &p, inlen, tag, aclass, opt, ctx);
8222139Sjp161948 if (!ret)
8232139Sjp161948 {
8240Sstevel@tonic-gate ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
8250Sstevel@tonic-gate return 0;
8262139Sjp161948 }
8272139Sjp161948 else if (ret == -1)
8282139Sjp161948 return -1;
8292864Sjp161948 ret = 0;
8300Sstevel@tonic-gate /* SEQUENCE, SET and "OTHER" are left in encoded form */
8312139Sjp161948 if ((utype == V_ASN1_SEQUENCE)
8322139Sjp161948 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER))
8332139Sjp161948 {
8342139Sjp161948 /* Clear context cache for type OTHER because the auto clear
8352139Sjp161948 * when we have a exact match wont work
8360Sstevel@tonic-gate */
8372139Sjp161948 if (utype == V_ASN1_OTHER)
8382139Sjp161948 {
8390Sstevel@tonic-gate asn1_tlc_clear(ctx);
8402139Sjp161948 }
8410Sstevel@tonic-gate /* SEQUENCE and SET must be constructed */
8422139Sjp161948 else if (!cst)
8432139Sjp161948 {
8442139Sjp161948 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
8452139Sjp161948 ASN1_R_TYPE_NOT_CONSTRUCTED);
8460Sstevel@tonic-gate return 0;
8472139Sjp161948 }
8480Sstevel@tonic-gate
8490Sstevel@tonic-gate cont = *in;
8500Sstevel@tonic-gate /* If indefinite length constructed find the real end */
8512139Sjp161948 if (inf)
8522139Sjp161948 {
8532139Sjp161948 if (!asn1_find_end(&p, plen, inf))
8542139Sjp161948 goto err;
8550Sstevel@tonic-gate len = p - cont;
8562139Sjp161948 }
8572139Sjp161948 else
8582139Sjp161948 {
8590Sstevel@tonic-gate len = p - cont + plen;
8600Sstevel@tonic-gate p += plen;
8610Sstevel@tonic-gate buf.data = NULL;
8622139Sjp161948 }
8630Sstevel@tonic-gate }
8642139Sjp161948 else if (cst)
8652139Sjp161948 {
8660Sstevel@tonic-gate buf.length = 0;
8670Sstevel@tonic-gate buf.max = 0;
8680Sstevel@tonic-gate buf.data = NULL;
8690Sstevel@tonic-gate /* Should really check the internal tags are correct but
8700Sstevel@tonic-gate * some things may get this wrong. The relevant specs
8710Sstevel@tonic-gate * say that constructed string types should be OCTET STRINGs
8720Sstevel@tonic-gate * internally irrespective of the type. So instead just check
8730Sstevel@tonic-gate * for UNIVERSAL class and ignore the tag.
8740Sstevel@tonic-gate */
8752139Sjp161948 if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL))
8762139Sjp161948 goto err;
8770Sstevel@tonic-gate len = buf.length;
8780Sstevel@tonic-gate /* Append a final null to string */
8792139Sjp161948 if (!BUF_MEM_grow_clean(&buf, len + 1))
8802139Sjp161948 {
8812139Sjp161948 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
8822139Sjp161948 ERR_R_MALLOC_FAILURE);
8830Sstevel@tonic-gate return 0;
8842139Sjp161948 }
8852139Sjp161948 buf.data[len] = 0;
8862139Sjp161948 cont = (const unsigned char *)buf.data;
8872139Sjp161948 free_cont = 1;
8880Sstevel@tonic-gate }
8892139Sjp161948 else
8902139Sjp161948 {
8910Sstevel@tonic-gate cont = p;
8920Sstevel@tonic-gate len = plen;
8930Sstevel@tonic-gate p += plen;
8942139Sjp161948 }
8950Sstevel@tonic-gate
8960Sstevel@tonic-gate /* We now have content length and type: translate into a structure */
8972139Sjp161948 if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
8982139Sjp161948 goto err;
8990Sstevel@tonic-gate
9000Sstevel@tonic-gate *in = p;
9010Sstevel@tonic-gate ret = 1;
9020Sstevel@tonic-gate err:
9032139Sjp161948 if (free_cont && buf.data) OPENSSL_free(buf.data);
9040Sstevel@tonic-gate return ret;
9052139Sjp161948 }
9060Sstevel@tonic-gate
9070Sstevel@tonic-gate /* Translate ASN1 content octets into a structure */
9080Sstevel@tonic-gate
asn1_ex_c2i(ASN1_VALUE ** pval,const unsigned char * cont,int len,int utype,char * free_cont,const ASN1_ITEM * it)9092139Sjp161948 int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
9102139Sjp161948 int utype, char *free_cont, const ASN1_ITEM *it)
9112139Sjp161948 {
9120Sstevel@tonic-gate ASN1_VALUE **opval = NULL;
9130Sstevel@tonic-gate ASN1_STRING *stmp;
9140Sstevel@tonic-gate ASN1_TYPE *typ = NULL;
9150Sstevel@tonic-gate int ret = 0;
9160Sstevel@tonic-gate const ASN1_PRIMITIVE_FUNCS *pf;
9170Sstevel@tonic-gate ASN1_INTEGER **tint;
9180Sstevel@tonic-gate pf = it->funcs;
9192139Sjp161948
9202139Sjp161948 if (pf && pf->prim_c2i)
9212139Sjp161948 return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
9220Sstevel@tonic-gate /* If ANY type clear type and set pointer to internal value */
9232139Sjp161948 if (it->utype == V_ASN1_ANY)
9242139Sjp161948 {
9252139Sjp161948 if (!*pval)
9262139Sjp161948 {
9270Sstevel@tonic-gate typ = ASN1_TYPE_new();
9280Sstevel@tonic-gate *pval = (ASN1_VALUE *)typ;
9292139Sjp161948 }
9302139Sjp161948 else
9312139Sjp161948 typ = (ASN1_TYPE *)*pval;
9322139Sjp161948
9332139Sjp161948 if (utype != typ->type)
9342139Sjp161948 ASN1_TYPE_set(typ, utype, NULL);
9350Sstevel@tonic-gate opval = pval;
9360Sstevel@tonic-gate pval = (ASN1_VALUE **)&typ->value.ptr;
9372139Sjp161948 }
9382139Sjp161948 switch(utype)
9392139Sjp161948 {
9400Sstevel@tonic-gate case V_ASN1_OBJECT:
9412139Sjp161948 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
9422139Sjp161948 goto err;
9430Sstevel@tonic-gate break;
9440Sstevel@tonic-gate
9450Sstevel@tonic-gate case V_ASN1_NULL:
9462139Sjp161948 if (len)
9472139Sjp161948 {
9482139Sjp161948 ASN1err(ASN1_F_ASN1_EX_C2I,
9492139Sjp161948 ASN1_R_NULL_IS_WRONG_LENGTH);
9500Sstevel@tonic-gate goto err;
9512139Sjp161948 }
9520Sstevel@tonic-gate *pval = (ASN1_VALUE *)1;
9530Sstevel@tonic-gate break;
9540Sstevel@tonic-gate
9550Sstevel@tonic-gate case V_ASN1_BOOLEAN:
9562139Sjp161948 if (len != 1)
9572139Sjp161948 {
9582139Sjp161948 ASN1err(ASN1_F_ASN1_EX_C2I,
9592139Sjp161948 ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
9600Sstevel@tonic-gate goto err;
9612139Sjp161948 }
9622139Sjp161948 else
9632139Sjp161948 {
9640Sstevel@tonic-gate ASN1_BOOLEAN *tbool;
9650Sstevel@tonic-gate tbool = (ASN1_BOOLEAN *)pval;
9660Sstevel@tonic-gate *tbool = *cont;
9672139Sjp161948 }
9680Sstevel@tonic-gate break;
9690Sstevel@tonic-gate
9700Sstevel@tonic-gate case V_ASN1_BIT_STRING:
9712139Sjp161948 if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
9722139Sjp161948 goto err;
9730Sstevel@tonic-gate break;
9740Sstevel@tonic-gate
9750Sstevel@tonic-gate case V_ASN1_INTEGER:
9760Sstevel@tonic-gate case V_ASN1_NEG_INTEGER:
9770Sstevel@tonic-gate case V_ASN1_ENUMERATED:
9780Sstevel@tonic-gate case V_ASN1_NEG_ENUMERATED:
9790Sstevel@tonic-gate tint = (ASN1_INTEGER **)pval;
9802139Sjp161948 if (!c2i_ASN1_INTEGER(tint, &cont, len))
9812139Sjp161948 goto err;
9820Sstevel@tonic-gate /* Fixup type to match the expected form */
9830Sstevel@tonic-gate (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
9840Sstevel@tonic-gate break;
9850Sstevel@tonic-gate
9860Sstevel@tonic-gate case V_ASN1_OCTET_STRING:
9870Sstevel@tonic-gate case V_ASN1_NUMERICSTRING:
9880Sstevel@tonic-gate case V_ASN1_PRINTABLESTRING:
9890Sstevel@tonic-gate case V_ASN1_T61STRING:
9900Sstevel@tonic-gate case V_ASN1_VIDEOTEXSTRING:
9910Sstevel@tonic-gate case V_ASN1_IA5STRING:
9920Sstevel@tonic-gate case V_ASN1_UTCTIME:
9930Sstevel@tonic-gate case V_ASN1_GENERALIZEDTIME:
9940Sstevel@tonic-gate case V_ASN1_GRAPHICSTRING:
9950Sstevel@tonic-gate case V_ASN1_VISIBLESTRING:
9960Sstevel@tonic-gate case V_ASN1_GENERALSTRING:
9970Sstevel@tonic-gate case V_ASN1_UNIVERSALSTRING:
9980Sstevel@tonic-gate case V_ASN1_BMPSTRING:
9990Sstevel@tonic-gate case V_ASN1_UTF8STRING:
10000Sstevel@tonic-gate case V_ASN1_OTHER:
10010Sstevel@tonic-gate case V_ASN1_SET:
10020Sstevel@tonic-gate case V_ASN1_SEQUENCE:
10030Sstevel@tonic-gate default:
1004*9318SJan.Pechanec@Sun.COM if (utype == V_ASN1_BMPSTRING && (len & 1))
1005*9318SJan.Pechanec@Sun.COM {
1006*9318SJan.Pechanec@Sun.COM ASN1err(ASN1_F_ASN1_EX_C2I,
1007*9318SJan.Pechanec@Sun.COM ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
1008*9318SJan.Pechanec@Sun.COM goto err;
1009*9318SJan.Pechanec@Sun.COM }
1010*9318SJan.Pechanec@Sun.COM if (utype == V_ASN1_UNIVERSALSTRING && (len & 3))
1011*9318SJan.Pechanec@Sun.COM {
1012*9318SJan.Pechanec@Sun.COM ASN1err(ASN1_F_ASN1_EX_C2I,
1013*9318SJan.Pechanec@Sun.COM ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
1014*9318SJan.Pechanec@Sun.COM goto err;
1015*9318SJan.Pechanec@Sun.COM }
10160Sstevel@tonic-gate /* All based on ASN1_STRING and handled the same */
10172139Sjp161948 if (!*pval)
10182139Sjp161948 {
10190Sstevel@tonic-gate stmp = ASN1_STRING_type_new(utype);
10202139Sjp161948 if (!stmp)
10212139Sjp161948 {
10222139Sjp161948 ASN1err(ASN1_F_ASN1_EX_C2I,
10232139Sjp161948 ERR_R_MALLOC_FAILURE);
10240Sstevel@tonic-gate goto err;
10252139Sjp161948 }
10260Sstevel@tonic-gate *pval = (ASN1_VALUE *)stmp;
10272139Sjp161948 }
10282139Sjp161948 else
10292139Sjp161948 {
10300Sstevel@tonic-gate stmp = (ASN1_STRING *)*pval;
10310Sstevel@tonic-gate stmp->type = utype;
10322139Sjp161948 }
10330Sstevel@tonic-gate /* If we've already allocated a buffer use it */
10342139Sjp161948 if (*free_cont)
10352139Sjp161948 {
10362139Sjp161948 if (stmp->data)
10372139Sjp161948 OPENSSL_free(stmp->data);
10382139Sjp161948 stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
10390Sstevel@tonic-gate stmp->length = len;
10400Sstevel@tonic-gate *free_cont = 0;
10412139Sjp161948 }
10422139Sjp161948 else
10432139Sjp161948 {
10442139Sjp161948 if (!ASN1_STRING_set(stmp, cont, len))
10452139Sjp161948 {
10462139Sjp161948 ASN1err(ASN1_F_ASN1_EX_C2I,
10472139Sjp161948 ERR_R_MALLOC_FAILURE);
10480Sstevel@tonic-gate ASN1_STRING_free(stmp);
10490Sstevel@tonic-gate *pval = NULL;
10500Sstevel@tonic-gate goto err;
10512139Sjp161948 }
10520Sstevel@tonic-gate }
10530Sstevel@tonic-gate break;
10542139Sjp161948 }
10550Sstevel@tonic-gate /* If ASN1_ANY and NULL type fix up value */
10562139Sjp161948 if (typ && (utype == V_ASN1_NULL))
10572139Sjp161948 typ->value.ptr = NULL;
10580Sstevel@tonic-gate
10590Sstevel@tonic-gate ret = 1;
10600Sstevel@tonic-gate err:
10612139Sjp161948 if (!ret)
10620Sstevel@tonic-gate {
10630Sstevel@tonic-gate ASN1_TYPE_free(typ);
10640Sstevel@tonic-gate if (opval)
10650Sstevel@tonic-gate *opval = NULL;
10660Sstevel@tonic-gate }
10670Sstevel@tonic-gate return ret;
10682139Sjp161948 }
10692139Sjp161948
10702139Sjp161948
10712139Sjp161948 /* This function finds the end of an ASN1 structure when passed its maximum
10722139Sjp161948 * length, whether it is indefinite length and a pointer to the content.
10732139Sjp161948 * This is more efficient than calling asn1_collect because it does not
10742139Sjp161948 * recurse on each indefinite length header.
10752139Sjp161948 */
10760Sstevel@tonic-gate
asn1_find_end(const unsigned char ** in,long len,char inf)10772139Sjp161948 static int asn1_find_end(const unsigned char **in, long len, char inf)
10782139Sjp161948 {
10792139Sjp161948 int expected_eoc;
10802139Sjp161948 long plen;
10812139Sjp161948 const unsigned char *p = *in, *q;
10822139Sjp161948 /* If not indefinite length constructed just add length */
10832139Sjp161948 if (inf == 0)
10842139Sjp161948 {
10852139Sjp161948 *in += len;
10862139Sjp161948 return 1;
10872139Sjp161948 }
10882139Sjp161948 expected_eoc = 1;
10892139Sjp161948 /* Indefinite length constructed form. Find the end when enough EOCs
10902139Sjp161948 * are found. If more indefinite length constructed headers
10912139Sjp161948 * are encountered increment the expected eoc count otherwise just
10922139Sjp161948 * skip to the end of the data.
10932139Sjp161948 */
10942139Sjp161948 while (len > 0)
10952139Sjp161948 {
10962139Sjp161948 if(asn1_check_eoc(&p, len))
10972139Sjp161948 {
10982139Sjp161948 expected_eoc--;
10992139Sjp161948 if (expected_eoc == 0)
11002139Sjp161948 break;
11012139Sjp161948 len -= 2;
11022139Sjp161948 continue;
11032139Sjp161948 }
11042139Sjp161948 q = p;
11052139Sjp161948 /* Just read in a header: only care about the length */
11062139Sjp161948 if(!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
11072139Sjp161948 -1, 0, 0, NULL))
11082139Sjp161948 {
11092139Sjp161948 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
11102139Sjp161948 return 0;
11112139Sjp161948 }
11122139Sjp161948 if (inf)
11132139Sjp161948 expected_eoc++;
11142139Sjp161948 else
11152139Sjp161948 p += plen;
11162139Sjp161948 len -= p - q;
11172139Sjp161948 }
11182139Sjp161948 if (expected_eoc)
11192139Sjp161948 {
11202139Sjp161948 ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
11212139Sjp161948 return 0;
11222139Sjp161948 }
11232139Sjp161948 *in = p;
11242139Sjp161948 return 1;
11252139Sjp161948 }
11260Sstevel@tonic-gate /* This function collects the asn1 data from a constructred string
11270Sstevel@tonic-gate * type into a buffer. The values of 'in' and 'len' should refer
11280Sstevel@tonic-gate * to the contents of the constructed type and 'inf' should be set
11292139Sjp161948 * if it is indefinite length.
11300Sstevel@tonic-gate */
11310Sstevel@tonic-gate
asn1_collect(BUF_MEM * buf,const unsigned char ** in,long len,char inf,int tag,int aclass)11322139Sjp161948 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
11332139Sjp161948 char inf, int tag, int aclass)
11342139Sjp161948 {
11352139Sjp161948 const unsigned char *p, *q;
11360Sstevel@tonic-gate long plen;
11370Sstevel@tonic-gate char cst, ininf;
11380Sstevel@tonic-gate p = *in;
11390Sstevel@tonic-gate inf &= 1;
11402139Sjp161948 /* If no buffer and not indefinite length constructed just pass over
11412139Sjp161948 * the encoded data */
11422139Sjp161948 if (!buf && !inf)
11432139Sjp161948 {
11440Sstevel@tonic-gate *in += len;
11450Sstevel@tonic-gate return 1;
11462139Sjp161948 }
11472139Sjp161948 while(len > 0)
11482139Sjp161948 {
11490Sstevel@tonic-gate q = p;
11500Sstevel@tonic-gate /* Check for EOC */
11512139Sjp161948 if (asn1_check_eoc(&p, len))
11522139Sjp161948 {
11532139Sjp161948 /* EOC is illegal outside indefinite length
11542139Sjp161948 * constructed form */
11552139Sjp161948 if (!inf)
11562139Sjp161948 {
11572139Sjp161948 ASN1err(ASN1_F_ASN1_COLLECT,
11582139Sjp161948 ASN1_R_UNEXPECTED_EOC);
11590Sstevel@tonic-gate return 0;
11602139Sjp161948 }
11610Sstevel@tonic-gate inf = 0;
11620Sstevel@tonic-gate break;
11632139Sjp161948 }
11642139Sjp161948
11652139Sjp161948 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
11662139Sjp161948 len, tag, aclass, 0, NULL))
11672139Sjp161948 {
11680Sstevel@tonic-gate ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
11690Sstevel@tonic-gate return 0;
11702139Sjp161948 }
11712139Sjp161948
11720Sstevel@tonic-gate /* If indefinite length constructed update max length */
11732139Sjp161948 if (cst)
11742139Sjp161948 {
11752139Sjp161948 #ifdef OPENSSL_ALLOW_NESTED_ASN1_STRINGS
11762139Sjp161948 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass))
11772139Sjp161948 return 0;
11782139Sjp161948 #else
11792139Sjp161948 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_NESTED_ASN1_STRING);
11802139Sjp161948 return 0;
11812139Sjp161948 #endif
11822139Sjp161948 }
11832139Sjp161948 else if (!collect_data(buf, &p, plen))
11842139Sjp161948 return 0;
11852139Sjp161948 len -= p - q;
11860Sstevel@tonic-gate }
11872139Sjp161948 if (inf)
11882139Sjp161948 {
11890Sstevel@tonic-gate ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
11900Sstevel@tonic-gate return 0;
11912139Sjp161948 }
11920Sstevel@tonic-gate *in = p;
11930Sstevel@tonic-gate return 1;
11942139Sjp161948 }
11950Sstevel@tonic-gate
collect_data(BUF_MEM * buf,const unsigned char ** p,long plen)11962139Sjp161948 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
11972139Sjp161948 {
11982139Sjp161948 int len;
11992139Sjp161948 if (buf)
12002139Sjp161948 {
12012139Sjp161948 len = buf->length;
12022139Sjp161948 if (!BUF_MEM_grow_clean(buf, len + plen))
12032139Sjp161948 {
12042139Sjp161948 ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
12052139Sjp161948 return 0;
12060Sstevel@tonic-gate }
12072139Sjp161948 memcpy(buf->data + len, *p, plen);
12080Sstevel@tonic-gate }
12092139Sjp161948 *p += plen;
12102139Sjp161948 return 1;
12112139Sjp161948 }
12120Sstevel@tonic-gate
12130Sstevel@tonic-gate /* Check for ASN1 EOC and swallow it if found */
12140Sstevel@tonic-gate
asn1_check_eoc(const unsigned char ** in,long len)12152139Sjp161948 static int asn1_check_eoc(const unsigned char **in, long len)
12162139Sjp161948 {
12172139Sjp161948 const unsigned char *p;
12182139Sjp161948 if (len < 2) return 0;
12190Sstevel@tonic-gate p = *in;
12202139Sjp161948 if (!p[0] && !p[1])
12212139Sjp161948 {
12220Sstevel@tonic-gate *in += 2;
12230Sstevel@tonic-gate return 1;
12242139Sjp161948 }
12250Sstevel@tonic-gate return 0;
12262139Sjp161948 }
12270Sstevel@tonic-gate
12280Sstevel@tonic-gate /* Check an ASN1 tag and length: a bit like ASN1_get_object
12290Sstevel@tonic-gate * but it sets the length for indefinite length constructed
12300Sstevel@tonic-gate * form, we don't know the exact length but we can set an
12310Sstevel@tonic-gate * upper bound to the amount of data available minus the
12320Sstevel@tonic-gate * header length just read.
12330Sstevel@tonic-gate */
12340Sstevel@tonic-gate
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)12352139Sjp161948 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
12362139Sjp161948 char *inf, char *cst,
12372139Sjp161948 const unsigned char **in, long len,
12382139Sjp161948 int exptag, int expclass, char opt,
12392139Sjp161948 ASN1_TLC *ctx)
12402139Sjp161948 {
12410Sstevel@tonic-gate int i;
12420Sstevel@tonic-gate int ptag, pclass;
12430Sstevel@tonic-gate long plen;
12442139Sjp161948 const unsigned char *p, *q;
12450Sstevel@tonic-gate p = *in;
12460Sstevel@tonic-gate q = p;
12470Sstevel@tonic-gate
12482139Sjp161948 if (ctx && ctx->valid)
12492139Sjp161948 {
12500Sstevel@tonic-gate i = ctx->ret;
12510Sstevel@tonic-gate plen = ctx->plen;
12520Sstevel@tonic-gate pclass = ctx->pclass;
12530Sstevel@tonic-gate ptag = ctx->ptag;
12540Sstevel@tonic-gate p += ctx->hdrlen;
12552139Sjp161948 }
12562139Sjp161948 else
12572139Sjp161948 {
12580Sstevel@tonic-gate i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
12592139Sjp161948 if (ctx)
12602139Sjp161948 {
12610Sstevel@tonic-gate ctx->ret = i;
12620Sstevel@tonic-gate ctx->plen = plen;
12630Sstevel@tonic-gate ctx->pclass = pclass;
12640Sstevel@tonic-gate ctx->ptag = ptag;
12650Sstevel@tonic-gate ctx->hdrlen = p - q;
12660Sstevel@tonic-gate ctx->valid = 1;
12670Sstevel@tonic-gate /* If definite length, and no error, length +
12680Sstevel@tonic-gate * header can't exceed total amount of data available.
12690Sstevel@tonic-gate */
12702139Sjp161948 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len))
12712139Sjp161948 {
12722139Sjp161948 ASN1err(ASN1_F_ASN1_CHECK_TLEN,
12732139Sjp161948 ASN1_R_TOO_LONG);
12740Sstevel@tonic-gate asn1_tlc_clear(ctx);
12750Sstevel@tonic-gate return 0;
12762139Sjp161948 }
12770Sstevel@tonic-gate }
12780Sstevel@tonic-gate }
12790Sstevel@tonic-gate
12802139Sjp161948 if (i & 0x80)
12812139Sjp161948 {
12820Sstevel@tonic-gate ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
12830Sstevel@tonic-gate asn1_tlc_clear(ctx);
12840Sstevel@tonic-gate return 0;
12852139Sjp161948 }
12862139Sjp161948 if (exptag >= 0)
12872139Sjp161948 {
12882139Sjp161948 if ((exptag != ptag) || (expclass != pclass))
12892139Sjp161948 {
12902139Sjp161948 /* If type is OPTIONAL, not an error:
12912139Sjp161948 * indicate missing type.
12920Sstevel@tonic-gate */
12932139Sjp161948 if (opt) return -1;
12940Sstevel@tonic-gate asn1_tlc_clear(ctx);
12950Sstevel@tonic-gate ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
12960Sstevel@tonic-gate return 0;
12972139Sjp161948 }
12982139Sjp161948 /* We have a tag and class match:
12992139Sjp161948 * assume we are going to do something with it */
13000Sstevel@tonic-gate asn1_tlc_clear(ctx);
13012139Sjp161948 }
13020Sstevel@tonic-gate
13032139Sjp161948 if (i & 1)
13042139Sjp161948 plen = len - (p - q);
13052139Sjp161948
13062139Sjp161948 if (inf)
13072139Sjp161948 *inf = i & 1;
13080Sstevel@tonic-gate
13092139Sjp161948 if (cst)
13102139Sjp161948 *cst = i & V_ASN1_CONSTRUCTED;
13110Sstevel@tonic-gate
13122139Sjp161948 if (olen)
13132139Sjp161948 *olen = plen;
13140Sstevel@tonic-gate
13152139Sjp161948 if (oclass)
13162139Sjp161948 *oclass = pclass;
13172139Sjp161948
13182139Sjp161948 if (otag)
13192139Sjp161948 *otag = ptag;
13200Sstevel@tonic-gate
13210Sstevel@tonic-gate *in = p;
13220Sstevel@tonic-gate return 1;
13232139Sjp161948 }
1324