1109998Smarkm/* tasn_dec.c */
2296341Sdelphij/*
3296341Sdelphij * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4296341Sdelphij * 2000.
5109998Smarkm */
6109998Smarkm/* ====================================================================
7160814Ssimon * Copyright (c) 2000-2005 The OpenSSL Project.  All rights reserved.
8109998Smarkm *
9109998Smarkm * Redistribution and use in source and binary forms, with or without
10109998Smarkm * modification, are permitted provided that the following conditions
11109998Smarkm * are met:
12109998Smarkm *
13109998Smarkm * 1. Redistributions of source code must retain the above copyright
14296341Sdelphij *    notice, this list of conditions and the following disclaimer.
15109998Smarkm *
16109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright
17109998Smarkm *    notice, this list of conditions and the following disclaimer in
18109998Smarkm *    the documentation and/or other materials provided with the
19109998Smarkm *    distribution.
20109998Smarkm *
21109998Smarkm * 3. All advertising materials mentioning features or use of this
22109998Smarkm *    software must display the following acknowledgment:
23109998Smarkm *    "This product includes software developed by the OpenSSL Project
24109998Smarkm *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25109998Smarkm *
26109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27109998Smarkm *    endorse or promote products derived from this software without
28109998Smarkm *    prior written permission. For written permission, please contact
29109998Smarkm *    licensing@OpenSSL.org.
30109998Smarkm *
31109998Smarkm * 5. Products derived from this software may not be called "OpenSSL"
32109998Smarkm *    nor may "OpenSSL" appear in their names without prior written
33109998Smarkm *    permission of the OpenSSL Project.
34109998Smarkm *
35109998Smarkm * 6. Redistributions of any form whatsoever must retain the following
36109998Smarkm *    acknowledgment:
37109998Smarkm *    "This product includes software developed by the OpenSSL Project
38109998Smarkm *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39109998Smarkm *
40109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43109998Smarkm * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE.
52109998Smarkm * ====================================================================
53109998Smarkm *
54109998Smarkm * This product includes cryptographic software written by Eric Young
55109998Smarkm * (eay@cryptsoft.com).  This product includes software written by Tim
56109998Smarkm * Hudson (tjh@cryptsoft.com).
57109998Smarkm *
58109998Smarkm */
59109998Smarkm
60109998Smarkm#include <stddef.h>
61109998Smarkm#include <string.h>
62109998Smarkm#include <openssl/asn1.h>
63109998Smarkm#include <openssl/asn1t.h>
64109998Smarkm#include <openssl/objects.h>
65109998Smarkm#include <openssl/buffer.h>
66109998Smarkm#include <openssl/err.h>
67109998Smarkm
68160814Ssimonstatic int asn1_check_eoc(const unsigned char **in, long len);
69160814Ssimonstatic int asn1_find_end(const unsigned char **in, long len, char inf);
70109998Smarkm
71160814Ssimonstatic int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
72296341Sdelphij                        char inf, int tag, int aclass, int depth);
73160814Ssimon
74160814Ssimonstatic int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
75160814Ssimon
76160814Ssimonstatic int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
77296341Sdelphij                           char *inf, char *cst,
78296341Sdelphij                           const unsigned char **in, long len,
79296341Sdelphij                           int exptag, int expclass, char opt, ASN1_TLC *ctx);
80160814Ssimon
81160814Ssimonstatic int asn1_template_ex_d2i(ASN1_VALUE **pval,
82296341Sdelphij                                const unsigned char **in, long len,
83296341Sdelphij                                const ASN1_TEMPLATE *tt, char opt,
84296341Sdelphij                                ASN1_TLC *ctx);
85160814Ssimonstatic int asn1_template_noexp_d2i(ASN1_VALUE **val,
86296341Sdelphij                                   const unsigned char **in, long len,
87296341Sdelphij                                   const ASN1_TEMPLATE *tt, char opt,
88296341Sdelphij                                   ASN1_TLC *ctx);
89160814Ssimonstatic int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
90296341Sdelphij                                 const unsigned char **in, long len,
91296341Sdelphij                                 const ASN1_ITEM *it,
92296341Sdelphij                                 int tag, int aclass, char opt,
93296341Sdelphij                                 ASN1_TLC *ctx);
94160814Ssimon
95109998Smarkm/* Table to convert tags to bit values, used for MSTRING type */
96167612Ssimonstatic const unsigned long tag2bit[32] = {
97296341Sdelphij    /* tags  0 -  3 */
98296341Sdelphij    0, 0, 0, B_ASN1_BIT_STRING,
99296341Sdelphij    /* tags  4- 7 */
100296341Sdelphij    B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,
101296341Sdelphij    /* tags  8-11 */
102296341Sdelphij    B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
103296341Sdelphij    /* tags 12-15 */
104296341Sdelphij    B_ASN1_UTF8STRING, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
105296341Sdelphij    /* tags 16-19 */
106296341Sdelphij    B_ASN1_SEQUENCE, 0, B_ASN1_NUMERICSTRING, B_ASN1_PRINTABLESTRING,
107296341Sdelphij    /* tags 20-22 */
108296341Sdelphij    B_ASN1_T61STRING, B_ASN1_VIDEOTEXSTRING, B_ASN1_IA5STRING,
109296341Sdelphij    /* tags 23-24 */
110296341Sdelphij    B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME,
111296341Sdelphij    /* tags 25-27 */
112296341Sdelphij    B_ASN1_GRAPHICSTRING, B_ASN1_ISO64STRING, B_ASN1_GENERALSTRING,
113296341Sdelphij    /* tags 28-31 */
114296341Sdelphij    B_ASN1_UNIVERSALSTRING, B_ASN1_UNKNOWN, B_ASN1_BMPSTRING, B_ASN1_UNKNOWN,
115296341Sdelphij};
116109998Smarkm
117109998Smarkmunsigned long ASN1_tag2bit(int tag)
118296341Sdelphij{
119296341Sdelphij    if ((tag < 0) || (tag > 30))
120296341Sdelphij        return 0;
121296341Sdelphij    return tag2bit[tag];
122296341Sdelphij}
123109998Smarkm
124109998Smarkm/* Macro to initialize and invalidate the cache */
125109998Smarkm
126296341Sdelphij#define asn1_tlc_clear(c)       if (c) (c)->valid = 0
127238405Sjkim/* Version to avoid compiler warning about 'c' always non-NULL */
128296341Sdelphij#define asn1_tlc_clear_nc(c)    (c)->valid = 0
129109998Smarkm
130296341Sdelphij/*
131296341Sdelphij * Decode an ASN1 item, this currently behaves just like a standard 'd2i'
132296341Sdelphij * function. 'in' points to a buffer to read the data from, in future we
133296341Sdelphij * will have more advanced versions that can input data a piece at a time and
134296341Sdelphij * this will simply be a special case.
135109998Smarkm */
136109998Smarkm
137160814SsimonASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
138296341Sdelphij                          const unsigned char **in, long len,
139296341Sdelphij                          const ASN1_ITEM *it)
140296341Sdelphij{
141296341Sdelphij    ASN1_TLC c;
142296341Sdelphij    ASN1_VALUE *ptmpval = NULL;
143296341Sdelphij    if (!pval)
144296341Sdelphij        pval = &ptmpval;
145296341Sdelphij    asn1_tlc_clear_nc(&c);
146296341Sdelphij    if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
147296341Sdelphij        return *pval;
148296341Sdelphij    return NULL;
149296341Sdelphij}
150109998Smarkm
151160814Ssimonint ASN1_template_d2i(ASN1_VALUE **pval,
152296341Sdelphij                      const unsigned char **in, long len,
153296341Sdelphij                      const ASN1_TEMPLATE *tt)
154296341Sdelphij{
155296341Sdelphij    ASN1_TLC c;
156296341Sdelphij    asn1_tlc_clear_nc(&c);
157296341Sdelphij    return asn1_template_ex_d2i(pval, in, len, tt, 0, &c);
158296341Sdelphij}
159109998Smarkm
160296341Sdelphij/*
161296341Sdelphij * Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and
162296341Sdelphij * tag mismatch return -1 to handle OPTIONAL
163109998Smarkm */
164109998Smarkm
165160814Ssimonint ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
166296341Sdelphij                     const ASN1_ITEM *it,
167296341Sdelphij                     int tag, int aclass, char opt, ASN1_TLC *ctx)
168296341Sdelphij{
169296341Sdelphij    const ASN1_TEMPLATE *tt, *errtt = NULL;
170296341Sdelphij    const ASN1_COMPAT_FUNCS *cf;
171296341Sdelphij    const ASN1_EXTERN_FUNCS *ef;
172296341Sdelphij    const ASN1_AUX *aux = it->funcs;
173296341Sdelphij    ASN1_aux_cb *asn1_cb;
174296341Sdelphij    const unsigned char *p = NULL, *q;
175296341Sdelphij    unsigned char *wp = NULL;   /* BIG FAT WARNING! BREAKS CONST WHERE USED */
176296341Sdelphij    unsigned char imphack = 0, oclass;
177296341Sdelphij    char seq_eoc, seq_nolen, cst, isopt;
178296341Sdelphij    long tmplen;
179296341Sdelphij    int i;
180296341Sdelphij    int otag;
181296341Sdelphij    int ret = 0;
182296341Sdelphij    ASN1_VALUE **pchptr, *ptmpval;
183296341Sdelphij    int combine = aclass & ASN1_TFLG_COMBINE;
184296341Sdelphij    aclass &= ~ASN1_TFLG_COMBINE;
185296341Sdelphij    if (!pval)
186296341Sdelphij        return 0;
187296341Sdelphij    if (aux && aux->asn1_cb)
188296341Sdelphij        asn1_cb = aux->asn1_cb;
189296341Sdelphij    else
190296341Sdelphij        asn1_cb = 0;
191109998Smarkm
192296341Sdelphij    switch (it->itype) {
193296341Sdelphij    case ASN1_ITYPE_PRIMITIVE:
194296341Sdelphij        if (it->templates) {
195296341Sdelphij            /*
196296341Sdelphij             * tagging or OPTIONAL is currently illegal on an item template
197296341Sdelphij             * because the flags can't get passed down. In practice this
198296341Sdelphij             * isn't a problem: we include the relevant flags from the item
199296341Sdelphij             * template in the template itself.
200296341Sdelphij             */
201296341Sdelphij            if ((tag != -1) || opt) {
202296341Sdelphij                ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
203296341Sdelphij                        ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
204296341Sdelphij                goto err;
205296341Sdelphij            }
206296341Sdelphij            return asn1_template_ex_d2i(pval, in, len,
207296341Sdelphij                                        it->templates, opt, ctx);
208296341Sdelphij        }
209296341Sdelphij        return asn1_d2i_ex_primitive(pval, in, len, it,
210296341Sdelphij                                     tag, aclass, opt, ctx);
211296341Sdelphij        break;
212109998Smarkm
213296341Sdelphij    case ASN1_ITYPE_MSTRING:
214296341Sdelphij        p = *in;
215296341Sdelphij        /* Just read in tag and class */
216296341Sdelphij        ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
217296341Sdelphij                              &p, len, -1, 0, 1, ctx);
218296341Sdelphij        if (!ret) {
219296341Sdelphij            ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
220296341Sdelphij            goto err;
221296341Sdelphij        }
222160814Ssimon
223296341Sdelphij        /* Must be UNIVERSAL class */
224296341Sdelphij        if (oclass != V_ASN1_UNIVERSAL) {
225296341Sdelphij            /* If OPTIONAL, assume this is OK */
226296341Sdelphij            if (opt)
227296341Sdelphij                return -1;
228296341Sdelphij            ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MSTRING_NOT_UNIVERSAL);
229296341Sdelphij            goto err;
230296341Sdelphij        }
231296341Sdelphij        /* Check tag matches bit map */
232296341Sdelphij        if (!(ASN1_tag2bit(otag) & it->utype)) {
233296341Sdelphij            /* If OPTIONAL, assume this is OK */
234296341Sdelphij            if (opt)
235296341Sdelphij                return -1;
236296341Sdelphij            ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MSTRING_WRONG_TAG);
237296341Sdelphij            goto err;
238296341Sdelphij        }
239296341Sdelphij        return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
240109998Smarkm
241296341Sdelphij    case ASN1_ITYPE_EXTERN:
242296341Sdelphij        /* Use new style d2i */
243296341Sdelphij        ef = it->funcs;
244296341Sdelphij        return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
245109998Smarkm
246296341Sdelphij    case ASN1_ITYPE_COMPAT:
247296341Sdelphij        /* we must resort to old style evil hackery */
248296341Sdelphij        cf = it->funcs;
249109998Smarkm
250296341Sdelphij        /* If OPTIONAL see if it is there */
251296341Sdelphij        if (opt) {
252296341Sdelphij            int exptag;
253296341Sdelphij            p = *in;
254296341Sdelphij            if (tag == -1)
255296341Sdelphij                exptag = it->utype;
256296341Sdelphij            else
257296341Sdelphij                exptag = tag;
258296341Sdelphij            /*
259296341Sdelphij             * Don't care about anything other than presence of expected tag
260296341Sdelphij             */
261160814Ssimon
262296341Sdelphij            ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL,
263296341Sdelphij                                  &p, len, exptag, aclass, 1, ctx);
264296341Sdelphij            if (!ret) {
265296341Sdelphij                ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
266296341Sdelphij                goto err;
267296341Sdelphij            }
268296341Sdelphij            if (ret == -1)
269296341Sdelphij                return -1;
270296341Sdelphij        }
271160814Ssimon
272296341Sdelphij        /*
273296341Sdelphij         * This is the old style evil hack IMPLICIT handling: since the
274296341Sdelphij         * underlying code is expecting a tag and class other than the one
275296341Sdelphij         * present we change the buffer temporarily then change it back
276296341Sdelphij         * afterwards. This doesn't and never did work for tags > 30. Yes
277296341Sdelphij         * this is *horrible* but it is only needed for old style d2i which
278296341Sdelphij         * will hopefully not be around for much longer. FIXME: should copy
279296341Sdelphij         * the buffer then modify it so the input buffer can be const: we
280296341Sdelphij         * should *always* copy because the old style d2i might modify the
281296341Sdelphij         * buffer.
282296341Sdelphij         */
283109998Smarkm
284296341Sdelphij        if (tag != -1) {
285296341Sdelphij            wp = *(unsigned char **)in;
286296341Sdelphij            imphack = *wp;
287296341Sdelphij            if (p == NULL) {
288296341Sdelphij                ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
289296341Sdelphij                goto err;
290296341Sdelphij            }
291296341Sdelphij            *wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED)
292296341Sdelphij                                  | it->utype);
293296341Sdelphij        }
294109998Smarkm
295296341Sdelphij        ptmpval = cf->asn1_d2i(pval, in, len);
296109998Smarkm
297296341Sdelphij        if (tag != -1)
298296341Sdelphij            *wp = imphack;
299109998Smarkm
300296341Sdelphij        if (ptmpval)
301296341Sdelphij            return 1;
302160814Ssimon
303296341Sdelphij        ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
304296341Sdelphij        goto err;
305109998Smarkm
306296341Sdelphij    case ASN1_ITYPE_CHOICE:
307296341Sdelphij        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
308296341Sdelphij            goto auxerr;
309296341Sdelphij        if (*pval) {
310296341Sdelphij            /* Free up and zero CHOICE value if initialised */
311296341Sdelphij            i = asn1_get_choice_selector(pval, it);
312296341Sdelphij            if ((i >= 0) && (i < it->tcount)) {
313296341Sdelphij                tt = it->templates + i;
314296341Sdelphij                pchptr = asn1_get_field_ptr(pval, tt);
315296341Sdelphij                ASN1_template_free(pchptr, tt);
316296341Sdelphij                asn1_set_choice_selector(pval, -1, it);
317296341Sdelphij            }
318296341Sdelphij        } else if (!ASN1_item_ex_new(pval, it)) {
319296341Sdelphij            ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
320296341Sdelphij            goto err;
321296341Sdelphij        }
322296341Sdelphij        /* CHOICE type, try each possibility in turn */
323296341Sdelphij        p = *in;
324296341Sdelphij        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
325296341Sdelphij            pchptr = asn1_get_field_ptr(pval, tt);
326296341Sdelphij            /*
327296341Sdelphij             * We mark field as OPTIONAL so its absence can be recognised.
328296341Sdelphij             */
329296341Sdelphij            ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);
330296341Sdelphij            /* If field not present, try the next one */
331296341Sdelphij            if (ret == -1)
332296341Sdelphij                continue;
333296341Sdelphij            /* If positive return, read OK, break loop */
334296341Sdelphij            if (ret > 0)
335296341Sdelphij                break;
336296341Sdelphij            /* Otherwise must be an ASN1 parsing error */
337296341Sdelphij            errtt = tt;
338296341Sdelphij            ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
339296341Sdelphij            goto err;
340296341Sdelphij        }
341109998Smarkm
342296341Sdelphij        /* Did we fall off the end without reading anything? */
343296341Sdelphij        if (i == it->tcount) {
344296341Sdelphij            /* If OPTIONAL, this is OK */
345296341Sdelphij            if (opt) {
346296341Sdelphij                /* Free and zero it */
347296341Sdelphij                ASN1_item_ex_free(pval, it);
348296341Sdelphij                return -1;
349296341Sdelphij            }
350296341Sdelphij            ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_NO_MATCHING_CHOICE_TYPE);
351296341Sdelphij            goto err;
352296341Sdelphij        }
353109998Smarkm
354296341Sdelphij        asn1_set_choice_selector(pval, i, it);
355296341Sdelphij        *in = p;
356296341Sdelphij        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
357296341Sdelphij            goto auxerr;
358296341Sdelphij        return 1;
359160814Ssimon
360296341Sdelphij    case ASN1_ITYPE_NDEF_SEQUENCE:
361296341Sdelphij    case ASN1_ITYPE_SEQUENCE:
362296341Sdelphij        p = *in;
363296341Sdelphij        tmplen = len;
364160814Ssimon
365296341Sdelphij        /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
366296341Sdelphij        if (tag == -1) {
367296341Sdelphij            tag = V_ASN1_SEQUENCE;
368296341Sdelphij            aclass = V_ASN1_UNIVERSAL;
369296341Sdelphij        }
370296341Sdelphij        /* Get SEQUENCE length and update len, p */
371296341Sdelphij        ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
372296341Sdelphij                              &p, len, tag, aclass, opt, ctx);
373296341Sdelphij        if (!ret) {
374296341Sdelphij            ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
375296341Sdelphij            goto err;
376296341Sdelphij        } else if (ret == -1)
377296341Sdelphij            return -1;
378296341Sdelphij        if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
379296341Sdelphij            len = tmplen - (p - *in);
380296341Sdelphij            seq_nolen = 1;
381296341Sdelphij        }
382296341Sdelphij        /* If indefinite we don't do a length check */
383296341Sdelphij        else
384296341Sdelphij            seq_nolen = seq_eoc;
385296341Sdelphij        if (!cst) {
386296341Sdelphij            ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
387296341Sdelphij            goto err;
388296341Sdelphij        }
389109998Smarkm
390296341Sdelphij        if (!*pval && !ASN1_item_ex_new(pval, it)) {
391296341Sdelphij            ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
392296341Sdelphij            goto err;
393296341Sdelphij        }
394109998Smarkm
395296341Sdelphij        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
396296341Sdelphij            goto auxerr;
397109998Smarkm
398296341Sdelphij        /* Free up and zero any ADB found */
399296341Sdelphij        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
400296341Sdelphij            if (tt->flags & ASN1_TFLG_ADB_MASK) {
401296341Sdelphij                const ASN1_TEMPLATE *seqtt;
402296341Sdelphij                ASN1_VALUE **pseqval;
403296341Sdelphij                seqtt = asn1_do_adb(pval, tt, 1);
404296341Sdelphij                pseqval = asn1_get_field_ptr(pval, seqtt);
405296341Sdelphij                ASN1_template_free(pseqval, seqtt);
406296341Sdelphij            }
407296341Sdelphij        }
408160814Ssimon
409296341Sdelphij        /* Get each field entry */
410296341Sdelphij        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
411296341Sdelphij            const ASN1_TEMPLATE *seqtt;
412296341Sdelphij            ASN1_VALUE **pseqval;
413296341Sdelphij            seqtt = asn1_do_adb(pval, tt, 1);
414296341Sdelphij            if (!seqtt)
415296341Sdelphij                goto err;
416296341Sdelphij            pseqval = asn1_get_field_ptr(pval, seqtt);
417296341Sdelphij            /* Have we ran out of data? */
418296341Sdelphij            if (!len)
419296341Sdelphij                break;
420296341Sdelphij            q = p;
421296341Sdelphij            if (asn1_check_eoc(&p, len)) {
422296341Sdelphij                if (!seq_eoc) {
423296341Sdelphij                    ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_UNEXPECTED_EOC);
424296341Sdelphij                    goto err;
425296341Sdelphij                }
426296341Sdelphij                len -= p - q;
427296341Sdelphij                seq_eoc = 0;
428296341Sdelphij                q = p;
429296341Sdelphij                break;
430296341Sdelphij            }
431296341Sdelphij            /*
432296341Sdelphij             * This determines the OPTIONAL flag value. The field cannot be
433296341Sdelphij             * omitted if it is the last of a SEQUENCE and there is still
434296341Sdelphij             * data to be read. This isn't strictly necessary but it
435296341Sdelphij             * increases efficiency in some cases.
436296341Sdelphij             */
437296341Sdelphij            if (i == (it->tcount - 1))
438296341Sdelphij                isopt = 0;
439296341Sdelphij            else
440296341Sdelphij                isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
441296341Sdelphij            /*
442296341Sdelphij             * attempt to read in field, allowing each to be OPTIONAL
443296341Sdelphij             */
444109998Smarkm
445296341Sdelphij            ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx);
446296341Sdelphij            if (!ret) {
447296341Sdelphij                errtt = seqtt;
448296341Sdelphij                goto err;
449296341Sdelphij            } else if (ret == -1) {
450296341Sdelphij                /*
451296341Sdelphij                 * OPTIONAL component absent. Free and zero the field.
452296341Sdelphij                 */
453296341Sdelphij                ASN1_template_free(pseqval, seqtt);
454296341Sdelphij                continue;
455296341Sdelphij            }
456296341Sdelphij            /* Update length */
457296341Sdelphij            len -= p - q;
458296341Sdelphij        }
459280268Sdelphij
460296341Sdelphij        /* Check for EOC if expecting one */
461296341Sdelphij        if (seq_eoc && !asn1_check_eoc(&p, len)) {
462296341Sdelphij            ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MISSING_EOC);
463296341Sdelphij            goto err;
464296341Sdelphij        }
465296341Sdelphij        /* Check all data read */
466296341Sdelphij        if (!seq_nolen && len) {
467296341Sdelphij            ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
468296341Sdelphij            goto err;
469296341Sdelphij        }
470160814Ssimon
471296341Sdelphij        /*
472296341Sdelphij         * If we get here we've got no more data in the SEQUENCE, however we
473296341Sdelphij         * may not have read all fields so check all remaining are OPTIONAL
474296341Sdelphij         * and clear any that are.
475296341Sdelphij         */
476296341Sdelphij        for (; i < it->tcount; tt++, i++) {
477296341Sdelphij            const ASN1_TEMPLATE *seqtt;
478296341Sdelphij            seqtt = asn1_do_adb(pval, tt, 1);
479296341Sdelphij            if (!seqtt)
480296341Sdelphij                goto err;
481296341Sdelphij            if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
482296341Sdelphij                ASN1_VALUE **pseqval;
483296341Sdelphij                pseqval = asn1_get_field_ptr(pval, seqtt);
484296341Sdelphij                ASN1_template_free(pseqval, seqtt);
485296341Sdelphij            } else {
486296341Sdelphij                errtt = seqtt;
487296341Sdelphij                ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_FIELD_MISSING);
488296341Sdelphij                goto err;
489296341Sdelphij            }
490296341Sdelphij        }
491296341Sdelphij        /* Save encoding */
492296341Sdelphij        if (!asn1_enc_save(pval, *in, p - *in, it))
493296341Sdelphij            goto auxerr;
494296341Sdelphij        *in = p;
495296341Sdelphij        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
496296341Sdelphij            goto auxerr;
497296341Sdelphij        return 1;
498160814Ssimon
499296341Sdelphij    default:
500296341Sdelphij        return 0;
501296341Sdelphij    }
502296341Sdelphij auxerr:
503296341Sdelphij    ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR);
504296341Sdelphij err:
505296341Sdelphij    if (combine == 0)
506296341Sdelphij        ASN1_item_ex_free(pval, it);
507296341Sdelphij    if (errtt)
508296341Sdelphij        ERR_add_error_data(4, "Field=", errtt->field_name,
509296341Sdelphij                           ", Type=", it->sname);
510296341Sdelphij    else
511296341Sdelphij        ERR_add_error_data(2, "Type=", it->sname);
512296341Sdelphij    return 0;
513296341Sdelphij}
514109998Smarkm
515296341Sdelphij/*
516296341Sdelphij * Templates are handled with two separate functions. One handles any
517296341Sdelphij * EXPLICIT tag and the other handles the rest.
518109998Smarkm */
519109998Smarkm
520160814Ssimonstatic int asn1_template_ex_d2i(ASN1_VALUE **val,
521296341Sdelphij                                const unsigned char **in, long inlen,
522296341Sdelphij                                const ASN1_TEMPLATE *tt, char opt,
523296341Sdelphij                                ASN1_TLC *ctx)
524296341Sdelphij{
525296341Sdelphij    int flags, aclass;
526296341Sdelphij    int ret;
527296341Sdelphij    long len;
528296341Sdelphij    const unsigned char *p, *q;
529296341Sdelphij    char exp_eoc;
530296341Sdelphij    if (!val)
531296341Sdelphij        return 0;
532296341Sdelphij    flags = tt->flags;
533296341Sdelphij    aclass = flags & ASN1_TFLG_TAG_CLASS;
534109998Smarkm
535296341Sdelphij    p = *in;
536109998Smarkm
537296341Sdelphij    /* Check if EXPLICIT tag expected */
538296341Sdelphij    if (flags & ASN1_TFLG_EXPTAG) {
539296341Sdelphij        char cst;
540296341Sdelphij        /*
541296341Sdelphij         * Need to work out amount of data available to the inner content and
542296341Sdelphij         * where it starts: so read in EXPLICIT header to get the info.
543296341Sdelphij         */
544296341Sdelphij        ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
545296341Sdelphij                              &p, inlen, tt->tag, aclass, opt, ctx);
546296341Sdelphij        q = p;
547296341Sdelphij        if (!ret) {
548296341Sdelphij            ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
549296341Sdelphij            return 0;
550296341Sdelphij        } else if (ret == -1)
551296341Sdelphij            return -1;
552296341Sdelphij        if (!cst) {
553296341Sdelphij            ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
554296341Sdelphij                    ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
555296341Sdelphij            return 0;
556296341Sdelphij        }
557296341Sdelphij        /* We've found the field so it can't be OPTIONAL now */
558296341Sdelphij        ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
559296341Sdelphij        if (!ret) {
560296341Sdelphij            ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
561296341Sdelphij            return 0;
562296341Sdelphij        }
563296341Sdelphij        /* We read the field in OK so update length */
564296341Sdelphij        len -= p - q;
565296341Sdelphij        if (exp_eoc) {
566296341Sdelphij            /* If NDEF we must have an EOC here */
567296341Sdelphij            if (!asn1_check_eoc(&p, len)) {
568296341Sdelphij                ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ASN1_R_MISSING_EOC);
569296341Sdelphij                goto err;
570296341Sdelphij            }
571296341Sdelphij        } else {
572296341Sdelphij            /*
573296341Sdelphij             * Otherwise we must hit the EXPLICIT tag end or its an error
574296341Sdelphij             */
575296341Sdelphij            if (len) {
576296341Sdelphij                ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
577296341Sdelphij                        ASN1_R_EXPLICIT_LENGTH_MISMATCH);
578296341Sdelphij                goto err;
579296341Sdelphij            }
580296341Sdelphij        }
581296341Sdelphij    } else
582296341Sdelphij        return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx);
583109998Smarkm
584296341Sdelphij    *in = p;
585296341Sdelphij    return 1;
586109998Smarkm
587296341Sdelphij err:
588296341Sdelphij    ASN1_template_free(val, tt);
589296341Sdelphij    return 0;
590296341Sdelphij}
591109998Smarkm
592160814Ssimonstatic int asn1_template_noexp_d2i(ASN1_VALUE **val,
593296341Sdelphij                                   const unsigned char **in, long len,
594296341Sdelphij                                   const ASN1_TEMPLATE *tt, char opt,
595296341Sdelphij                                   ASN1_TLC *ctx)
596296341Sdelphij{
597296341Sdelphij    int flags, aclass;
598296341Sdelphij    int ret;
599296341Sdelphij    const unsigned char *p, *q;
600296341Sdelphij    if (!val)
601296341Sdelphij        return 0;
602296341Sdelphij    flags = tt->flags;
603296341Sdelphij    aclass = flags & ASN1_TFLG_TAG_CLASS;
604109998Smarkm
605296341Sdelphij    p = *in;
606296341Sdelphij    q = p;
607109998Smarkm
608296341Sdelphij    if (flags & ASN1_TFLG_SK_MASK) {
609296341Sdelphij        /* SET OF, SEQUENCE OF */
610296341Sdelphij        int sktag, skaclass;
611296341Sdelphij        char sk_eoc;
612296341Sdelphij        /* First work out expected inner tag value */
613296341Sdelphij        if (flags & ASN1_TFLG_IMPTAG) {
614296341Sdelphij            sktag = tt->tag;
615296341Sdelphij            skaclass = aclass;
616296341Sdelphij        } else {
617296341Sdelphij            skaclass = V_ASN1_UNIVERSAL;
618296341Sdelphij            if (flags & ASN1_TFLG_SET_OF)
619296341Sdelphij                sktag = V_ASN1_SET;
620296341Sdelphij            else
621296341Sdelphij                sktag = V_ASN1_SEQUENCE;
622296341Sdelphij        }
623296341Sdelphij        /* Get the tag */
624296341Sdelphij        ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
625296341Sdelphij                              &p, len, sktag, skaclass, opt, ctx);
626296341Sdelphij        if (!ret) {
627296341Sdelphij            ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
628296341Sdelphij            return 0;
629296341Sdelphij        } else if (ret == -1)
630296341Sdelphij            return -1;
631296341Sdelphij        if (!*val)
632296341Sdelphij            *val = (ASN1_VALUE *)sk_new_null();
633296341Sdelphij        else {
634296341Sdelphij            /*
635296341Sdelphij             * We've got a valid STACK: free up any items present
636296341Sdelphij             */
637296341Sdelphij            STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
638296341Sdelphij            ASN1_VALUE *vtmp;
639296341Sdelphij            while (sk_ASN1_VALUE_num(sktmp) > 0) {
640296341Sdelphij                vtmp = sk_ASN1_VALUE_pop(sktmp);
641296341Sdelphij                ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item));
642296341Sdelphij            }
643296341Sdelphij        }
644160814Ssimon
645296341Sdelphij        if (!*val) {
646296341Sdelphij            ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
647296341Sdelphij            goto err;
648296341Sdelphij        }
649109998Smarkm
650296341Sdelphij        /* Read as many items as we can */
651296341Sdelphij        while (len > 0) {
652296341Sdelphij            ASN1_VALUE *skfield;
653296341Sdelphij            q = p;
654296341Sdelphij            /* See if EOC found */
655296341Sdelphij            if (asn1_check_eoc(&p, len)) {
656296341Sdelphij                if (!sk_eoc) {
657296341Sdelphij                    ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
658296341Sdelphij                            ASN1_R_UNEXPECTED_EOC);
659296341Sdelphij                    goto err;
660296341Sdelphij                }
661296341Sdelphij                len -= p - q;
662296341Sdelphij                sk_eoc = 0;
663296341Sdelphij                break;
664296341Sdelphij            }
665296341Sdelphij            skfield = NULL;
666296341Sdelphij            if (!ASN1_item_ex_d2i(&skfield, &p, len,
667296341Sdelphij                                  ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx)) {
668296341Sdelphij                ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
669296341Sdelphij                        ERR_R_NESTED_ASN1_ERROR);
670296341Sdelphij                goto err;
671296341Sdelphij            }
672296341Sdelphij            len -= p - q;
673296341Sdelphij            if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
674296341Sdelphij                ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
675296341Sdelphij                goto err;
676296341Sdelphij            }
677296341Sdelphij        }
678296341Sdelphij        if (sk_eoc) {
679296341Sdelphij            ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
680296341Sdelphij            goto err;
681296341Sdelphij        }
682296341Sdelphij    } else if (flags & ASN1_TFLG_IMPTAG) {
683296341Sdelphij        /* IMPLICIT tagging */
684296341Sdelphij        ret = ASN1_item_ex_d2i(val, &p, len,
685296341Sdelphij                               ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
686296341Sdelphij                               ctx);
687296341Sdelphij        if (!ret) {
688296341Sdelphij            ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
689296341Sdelphij            goto err;
690296341Sdelphij        } else if (ret == -1)
691296341Sdelphij            return -1;
692296341Sdelphij    } else {
693296341Sdelphij        /* Nothing special */
694296341Sdelphij        ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
695296341Sdelphij                               -1, tt->flags & ASN1_TFLG_COMBINE, opt, ctx);
696296341Sdelphij        if (!ret) {
697296341Sdelphij            ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
698296341Sdelphij            goto err;
699296341Sdelphij        } else if (ret == -1)
700296341Sdelphij            return -1;
701296341Sdelphij    }
702109998Smarkm
703296341Sdelphij    *in = p;
704296341Sdelphij    return 1;
705109998Smarkm
706296341Sdelphij err:
707296341Sdelphij    ASN1_template_free(val, tt);
708296341Sdelphij    return 0;
709296341Sdelphij}
710296341Sdelphij
711160814Ssimonstatic int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
712296341Sdelphij                                 const unsigned char **in, long inlen,
713296341Sdelphij                                 const ASN1_ITEM *it,
714296341Sdelphij                                 int tag, int aclass, char opt, ASN1_TLC *ctx)
715296341Sdelphij{
716296341Sdelphij    int ret = 0, utype;
717296341Sdelphij    long plen;
718296341Sdelphij    char cst, inf, free_cont = 0;
719296341Sdelphij    const unsigned char *p;
720296341Sdelphij    BUF_MEM buf;
721296341Sdelphij    const unsigned char *cont = NULL;
722296341Sdelphij    long len;
723296341Sdelphij    if (!pval) {
724296341Sdelphij        ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
725296341Sdelphij        return 0;               /* Should never happen */
726296341Sdelphij    }
727109998Smarkm
728296341Sdelphij    if (it->itype == ASN1_ITYPE_MSTRING) {
729296341Sdelphij        utype = tag;
730296341Sdelphij        tag = -1;
731296341Sdelphij    } else
732296341Sdelphij        utype = it->utype;
733109998Smarkm
734296341Sdelphij    if (utype == V_ASN1_ANY) {
735296341Sdelphij        /* If type is ANY need to figure out type from tag */
736296341Sdelphij        unsigned char oclass;
737296341Sdelphij        if (tag >= 0) {
738296341Sdelphij            ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_TAGGED_ANY);
739296341Sdelphij            return 0;
740296341Sdelphij        }
741296341Sdelphij        if (opt) {
742296341Sdelphij            ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
743296341Sdelphij                    ASN1_R_ILLEGAL_OPTIONAL_ANY);
744296341Sdelphij            return 0;
745296341Sdelphij        }
746296341Sdelphij        p = *in;
747296341Sdelphij        ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
748296341Sdelphij                              &p, inlen, -1, 0, 0, ctx);
749296341Sdelphij        if (!ret) {
750296341Sdelphij            ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
751296341Sdelphij            return 0;
752296341Sdelphij        }
753296341Sdelphij        if (oclass != V_ASN1_UNIVERSAL)
754296341Sdelphij            utype = V_ASN1_OTHER;
755296341Sdelphij    }
756296341Sdelphij    if (tag == -1) {
757296341Sdelphij        tag = utype;
758296341Sdelphij        aclass = V_ASN1_UNIVERSAL;
759296341Sdelphij    }
760296341Sdelphij    p = *in;
761296341Sdelphij    /* Check header */
762296341Sdelphij    ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
763296341Sdelphij                          &p, inlen, tag, aclass, opt, ctx);
764296341Sdelphij    if (!ret) {
765296341Sdelphij        ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
766296341Sdelphij        return 0;
767296341Sdelphij    } else if (ret == -1)
768296341Sdelphij        return -1;
769296341Sdelphij    ret = 0;
770296341Sdelphij    /* SEQUENCE, SET and "OTHER" are left in encoded form */
771296341Sdelphij    if ((utype == V_ASN1_SEQUENCE)
772296341Sdelphij        || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {
773296341Sdelphij        /*
774296341Sdelphij         * Clear context cache for type OTHER because the auto clear when we
775296341Sdelphij         * have a exact match wont work
776296341Sdelphij         */
777296341Sdelphij        if (utype == V_ASN1_OTHER) {
778296341Sdelphij            asn1_tlc_clear(ctx);
779296341Sdelphij        }
780296341Sdelphij        /* SEQUENCE and SET must be constructed */
781296341Sdelphij        else if (!cst) {
782296341Sdelphij            ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
783296341Sdelphij                    ASN1_R_TYPE_NOT_CONSTRUCTED);
784296341Sdelphij            return 0;
785296341Sdelphij        }
786109998Smarkm
787296341Sdelphij        cont = *in;
788296341Sdelphij        /* If indefinite length constructed find the real end */
789296341Sdelphij        if (inf) {
790296341Sdelphij            if (!asn1_find_end(&p, plen, inf))
791296341Sdelphij                goto err;
792296341Sdelphij            len = p - cont;
793296341Sdelphij        } else {
794296341Sdelphij            len = p - cont + plen;
795296341Sdelphij            p += plen;
796296341Sdelphij            buf.data = NULL;
797296341Sdelphij        }
798296341Sdelphij    } else if (cst) {
799296341Sdelphij        if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
800296341Sdelphij            || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
801296341Sdelphij            || utype == V_ASN1_ENUMERATED) {
802296341Sdelphij            ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_TYPE_NOT_PRIMITIVE);
803296341Sdelphij            return 0;
804296341Sdelphij        }
805296341Sdelphij        buf.length = 0;
806296341Sdelphij        buf.max = 0;
807296341Sdelphij        buf.data = NULL;
808296341Sdelphij        /*
809296341Sdelphij         * Should really check the internal tags are correct but some things
810296341Sdelphij         * may get this wrong. The relevant specs say that constructed string
811296341Sdelphij         * types should be OCTET STRINGs internally irrespective of the type.
812296341Sdelphij         * So instead just check for UNIVERSAL class and ignore the tag.
813296341Sdelphij         */
814296341Sdelphij        if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
815296341Sdelphij            free_cont = 1;
816296341Sdelphij            goto err;
817296341Sdelphij        }
818296341Sdelphij        len = buf.length;
819296341Sdelphij        /* Append a final null to string */
820296341Sdelphij        if (!BUF_MEM_grow_clean(&buf, len + 1)) {
821296341Sdelphij            ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE);
822296341Sdelphij            return 0;
823296341Sdelphij        }
824296341Sdelphij        buf.data[len] = 0;
825296341Sdelphij        cont = (const unsigned char *)buf.data;
826296341Sdelphij        free_cont = 1;
827296341Sdelphij    } else {
828296341Sdelphij        cont = p;
829296341Sdelphij        len = plen;
830296341Sdelphij        p += plen;
831296341Sdelphij    }
832109998Smarkm
833296341Sdelphij    /* We now have content length and type: translate into a structure */
834296341Sdelphij    if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
835296341Sdelphij        goto err;
836109998Smarkm
837296341Sdelphij    *in = p;
838296341Sdelphij    ret = 1;
839296341Sdelphij err:
840296341Sdelphij    if (free_cont && buf.data)
841296341Sdelphij        OPENSSL_free(buf.data);
842296341Sdelphij    return ret;
843296341Sdelphij}
844109998Smarkm
845109998Smarkm/* Translate ASN1 content octets into a structure */
846109998Smarkm
847160814Ssimonint asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
848296341Sdelphij                int utype, char *free_cont, const ASN1_ITEM *it)
849296341Sdelphij{
850296341Sdelphij    ASN1_VALUE **opval = NULL;
851296341Sdelphij    ASN1_STRING *stmp;
852296341Sdelphij    ASN1_TYPE *typ = NULL;
853296341Sdelphij    int ret = 0;
854296341Sdelphij    const ASN1_PRIMITIVE_FUNCS *pf;
855296341Sdelphij    ASN1_INTEGER **tint;
856296341Sdelphij    pf = it->funcs;
857160814Ssimon
858296341Sdelphij    if (pf && pf->prim_c2i)
859296341Sdelphij        return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
860296341Sdelphij    /* If ANY type clear type and set pointer to internal value */
861296341Sdelphij    if (it->utype == V_ASN1_ANY) {
862296341Sdelphij        if (!*pval) {
863296341Sdelphij            typ = ASN1_TYPE_new();
864296341Sdelphij            if (typ == NULL)
865296341Sdelphij                goto err;
866296341Sdelphij            *pval = (ASN1_VALUE *)typ;
867296341Sdelphij        } else
868296341Sdelphij            typ = (ASN1_TYPE *)*pval;
869160814Ssimon
870296341Sdelphij        if (utype != typ->type)
871296341Sdelphij            ASN1_TYPE_set(typ, utype, NULL);
872296341Sdelphij        opval = pval;
873296341Sdelphij        pval = &typ->value.asn1_value;
874296341Sdelphij    }
875296341Sdelphij    switch (utype) {
876296341Sdelphij    case V_ASN1_OBJECT:
877296341Sdelphij        if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
878296341Sdelphij            goto err;
879296341Sdelphij        break;
880109998Smarkm
881296341Sdelphij    case V_ASN1_NULL:
882296341Sdelphij        if (len) {
883296341Sdelphij            ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_NULL_IS_WRONG_LENGTH);
884296341Sdelphij            goto err;
885296341Sdelphij        }
886296341Sdelphij        *pval = (ASN1_VALUE *)1;
887296341Sdelphij        break;
888109998Smarkm
889296341Sdelphij    case V_ASN1_BOOLEAN:
890296341Sdelphij        if (len != 1) {
891296341Sdelphij            ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
892296341Sdelphij            goto err;
893296341Sdelphij        } else {
894296341Sdelphij            ASN1_BOOLEAN *tbool;
895296341Sdelphij            tbool = (ASN1_BOOLEAN *)pval;
896296341Sdelphij            *tbool = *cont;
897296341Sdelphij        }
898296341Sdelphij        break;
899109998Smarkm
900296341Sdelphij    case V_ASN1_BIT_STRING:
901296341Sdelphij        if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
902296341Sdelphij            goto err;
903296341Sdelphij        break;
904109998Smarkm
905296341Sdelphij    case V_ASN1_INTEGER:
906296341Sdelphij    case V_ASN1_ENUMERATED:
907296341Sdelphij        tint = (ASN1_INTEGER **)pval;
908296341Sdelphij        if (!c2i_ASN1_INTEGER(tint, &cont, len))
909296341Sdelphij            goto err;
910296341Sdelphij        /* Fixup type to match the expected form */
911296341Sdelphij        (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
912296341Sdelphij        break;
913109998Smarkm
914296341Sdelphij    case V_ASN1_OCTET_STRING:
915296341Sdelphij    case V_ASN1_NUMERICSTRING:
916296341Sdelphij    case V_ASN1_PRINTABLESTRING:
917296341Sdelphij    case V_ASN1_T61STRING:
918296341Sdelphij    case V_ASN1_VIDEOTEXSTRING:
919296341Sdelphij    case V_ASN1_IA5STRING:
920296341Sdelphij    case V_ASN1_UTCTIME:
921296341Sdelphij    case V_ASN1_GENERALIZEDTIME:
922296341Sdelphij    case V_ASN1_GRAPHICSTRING:
923296341Sdelphij    case V_ASN1_VISIBLESTRING:
924296341Sdelphij    case V_ASN1_GENERALSTRING:
925296341Sdelphij    case V_ASN1_UNIVERSALSTRING:
926296341Sdelphij    case V_ASN1_BMPSTRING:
927296341Sdelphij    case V_ASN1_UTF8STRING:
928296341Sdelphij    case V_ASN1_OTHER:
929296341Sdelphij    case V_ASN1_SET:
930296341Sdelphij    case V_ASN1_SEQUENCE:
931296341Sdelphij    default:
932296341Sdelphij        if (utype == V_ASN1_BMPSTRING && (len & 1)) {
933296341Sdelphij            ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
934296341Sdelphij            goto err;
935296341Sdelphij        }
936296341Sdelphij        if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
937296341Sdelphij            ASN1err(ASN1_F_ASN1_EX_C2I,
938296341Sdelphij                    ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
939296341Sdelphij            goto err;
940296341Sdelphij        }
941296341Sdelphij        /* All based on ASN1_STRING and handled the same */
942296341Sdelphij        if (!*pval) {
943296341Sdelphij            stmp = ASN1_STRING_type_new(utype);
944296341Sdelphij            if (!stmp) {
945296341Sdelphij                ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
946296341Sdelphij                goto err;
947296341Sdelphij            }
948296341Sdelphij            *pval = (ASN1_VALUE *)stmp;
949296341Sdelphij        } else {
950296341Sdelphij            stmp = (ASN1_STRING *)*pval;
951296341Sdelphij            stmp->type = utype;
952296341Sdelphij        }
953296341Sdelphij        /* If we've already allocated a buffer use it */
954296341Sdelphij        if (*free_cont) {
955296341Sdelphij            if (stmp->data)
956296341Sdelphij                OPENSSL_free(stmp->data);
957296341Sdelphij            stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
958296341Sdelphij            stmp->length = len;
959296341Sdelphij            *free_cont = 0;
960296341Sdelphij        } else {
961296341Sdelphij            if (!ASN1_STRING_set(stmp, cont, len)) {
962296341Sdelphij                ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
963296341Sdelphij                ASN1_STRING_free(stmp);
964296341Sdelphij                *pval = NULL;
965296341Sdelphij                goto err;
966296341Sdelphij            }
967296341Sdelphij        }
968296341Sdelphij        break;
969296341Sdelphij    }
970296341Sdelphij    /* If ASN1_ANY and NULL type fix up value */
971296341Sdelphij    if (typ && (utype == V_ASN1_NULL))
972296341Sdelphij        typ->value.ptr = NULL;
973109998Smarkm
974296341Sdelphij    ret = 1;
975296341Sdelphij err:
976296341Sdelphij    if (!ret) {
977296341Sdelphij        ASN1_TYPE_free(typ);
978296341Sdelphij        if (opval)
979296341Sdelphij            *opval = NULL;
980296341Sdelphij    }
981296341Sdelphij    return ret;
982296341Sdelphij}
983109998Smarkm
984296341Sdelphij/*
985296341Sdelphij * This function finds the end of an ASN1 structure when passed its maximum
986296341Sdelphij * length, whether it is indefinite length and a pointer to the content. This
987296341Sdelphij * is more efficient than calling asn1_collect because it does not recurse on
988296341Sdelphij * each indefinite length header.
989160814Ssimon */
990160814Ssimon
991160814Ssimonstatic int asn1_find_end(const unsigned char **in, long len, char inf)
992296341Sdelphij{
993296341Sdelphij    int expected_eoc;
994296341Sdelphij    long plen;
995296341Sdelphij    const unsigned char *p = *in, *q;
996296341Sdelphij    /* If not indefinite length constructed just add length */
997296341Sdelphij    if (inf == 0) {
998296341Sdelphij        *in += len;
999296341Sdelphij        return 1;
1000296341Sdelphij    }
1001296341Sdelphij    expected_eoc = 1;
1002296341Sdelphij    /*
1003296341Sdelphij     * Indefinite length constructed form. Find the end when enough EOCs are
1004296341Sdelphij     * found. If more indefinite length constructed headers are encountered
1005296341Sdelphij     * increment the expected eoc count otherwise just skip to the end of the
1006296341Sdelphij     * data.
1007296341Sdelphij     */
1008296341Sdelphij    while (len > 0) {
1009296341Sdelphij        if (asn1_check_eoc(&p, len)) {
1010296341Sdelphij            expected_eoc--;
1011296341Sdelphij            if (expected_eoc == 0)
1012296341Sdelphij                break;
1013296341Sdelphij            len -= 2;
1014296341Sdelphij            continue;
1015296341Sdelphij        }
1016296341Sdelphij        q = p;
1017296341Sdelphij        /* Just read in a header: only care about the length */
1018296341Sdelphij        if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1019296341Sdelphij                             -1, 0, 0, NULL)) {
1020296341Sdelphij            ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
1021296341Sdelphij            return 0;
1022296341Sdelphij        }
1023296341Sdelphij        if (inf)
1024296341Sdelphij            expected_eoc++;
1025296341Sdelphij        else
1026296341Sdelphij            p += plen;
1027296341Sdelphij        len -= p - q;
1028296341Sdelphij    }
1029296341Sdelphij    if (expected_eoc) {
1030296341Sdelphij        ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
1031296341Sdelphij        return 0;
1032296341Sdelphij    }
1033296341Sdelphij    *in = p;
1034296341Sdelphij    return 1;
1035296341Sdelphij}
1036296341Sdelphij
1037296341Sdelphij/*
1038296341Sdelphij * This function collects the asn1 data from a constructred string type into
1039296341Sdelphij * a buffer. The values of 'in' and 'len' should refer to the contents of the
1040296341Sdelphij * constructed type and 'inf' should be set if it is indefinite length.
1041109998Smarkm */
1042109998Smarkm
1043194206Ssimon#ifndef ASN1_MAX_STRING_NEST
1044296341Sdelphij/*
1045296341Sdelphij * This determines how many levels of recursion are permitted in ASN1 string
1046296341Sdelphij * types. If it is not limited stack overflows can occur. If set to zero no
1047296341Sdelphij * recursion is allowed at all. Although zero should be adequate examples
1048296341Sdelphij * exist that require a value of 1. So 5 should be more than enough.
1049194206Ssimon */
1050296341Sdelphij# define ASN1_MAX_STRING_NEST 5
1051194206Ssimon#endif
1052194206Ssimon
1053160814Ssimonstatic int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1054296341Sdelphij                        char inf, int tag, int aclass, int depth)
1055296341Sdelphij{
1056296341Sdelphij    const unsigned char *p, *q;
1057296341Sdelphij    long plen;
1058296341Sdelphij    char cst, ininf;
1059296341Sdelphij    p = *in;
1060296341Sdelphij    inf &= 1;
1061296341Sdelphij    /*
1062296341Sdelphij     * If no buffer and not indefinite length constructed just pass over the
1063296341Sdelphij     * encoded data
1064296341Sdelphij     */
1065296341Sdelphij    if (!buf && !inf) {
1066296341Sdelphij        *in += len;
1067296341Sdelphij        return 1;
1068296341Sdelphij    }
1069296341Sdelphij    while (len > 0) {
1070296341Sdelphij        q = p;
1071296341Sdelphij        /* Check for EOC */
1072296341Sdelphij        if (asn1_check_eoc(&p, len)) {
1073296341Sdelphij            /*
1074296341Sdelphij             * EOC is illegal outside indefinite length constructed form
1075296341Sdelphij             */
1076296341Sdelphij            if (!inf) {
1077296341Sdelphij                ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_UNEXPECTED_EOC);
1078296341Sdelphij                return 0;
1079296341Sdelphij            }
1080296341Sdelphij            inf = 0;
1081296341Sdelphij            break;
1082296341Sdelphij        }
1083160814Ssimon
1084296341Sdelphij        if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1085296341Sdelphij                             len, tag, aclass, 0, NULL)) {
1086296341Sdelphij            ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
1087296341Sdelphij            return 0;
1088296341Sdelphij        }
1089160814Ssimon
1090296341Sdelphij        /* If indefinite length constructed update max length */
1091296341Sdelphij        if (cst) {
1092296341Sdelphij            if (depth >= ASN1_MAX_STRING_NEST) {
1093296341Sdelphij                ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_NESTED_ASN1_STRING);
1094296341Sdelphij                return 0;
1095296341Sdelphij            }
1096296341Sdelphij            if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1097296341Sdelphij                return 0;
1098296341Sdelphij        } else if (plen && !collect_data(buf, &p, plen))
1099296341Sdelphij            return 0;
1100296341Sdelphij        len -= p - q;
1101296341Sdelphij    }
1102296341Sdelphij    if (inf) {
1103296341Sdelphij        ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
1104296341Sdelphij        return 0;
1105296341Sdelphij    }
1106296341Sdelphij    *in = p;
1107296341Sdelphij    return 1;
1108296341Sdelphij}
1109109998Smarkm
1110160814Ssimonstatic int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1111296341Sdelphij{
1112296341Sdelphij    int len;
1113296341Sdelphij    if (buf) {
1114296341Sdelphij        len = buf->length;
1115296341Sdelphij        if (!BUF_MEM_grow_clean(buf, len + plen)) {
1116296341Sdelphij            ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
1117296341Sdelphij            return 0;
1118296341Sdelphij        }
1119296341Sdelphij        memcpy(buf->data + len, *p, plen);
1120296341Sdelphij    }
1121296341Sdelphij    *p += plen;
1122296341Sdelphij    return 1;
1123296341Sdelphij}
1124109998Smarkm
1125109998Smarkm/* Check for ASN1 EOC and swallow it if found */
1126109998Smarkm
1127160814Ssimonstatic int asn1_check_eoc(const unsigned char **in, long len)
1128296341Sdelphij{
1129296341Sdelphij    const unsigned char *p;
1130296341Sdelphij    if (len < 2)
1131296341Sdelphij        return 0;
1132296341Sdelphij    p = *in;
1133296341Sdelphij    if (!p[0] && !p[1]) {
1134296341Sdelphij        *in += 2;
1135296341Sdelphij        return 1;
1136296341Sdelphij    }
1137296341Sdelphij    return 0;
1138296341Sdelphij}
1139109998Smarkm
1140296341Sdelphij/*
1141296341Sdelphij * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
1142296341Sdelphij * length for indefinite length constructed form, we don't know the exact
1143296341Sdelphij * length but we can set an upper bound to the amount of data available minus
1144296341Sdelphij * the header length just read.
1145109998Smarkm */
1146109998Smarkm
1147160814Ssimonstatic int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1148296341Sdelphij                           char *inf, char *cst,
1149296341Sdelphij                           const unsigned char **in, long len,
1150296341Sdelphij                           int exptag, int expclass, char opt, ASN1_TLC *ctx)
1151296341Sdelphij{
1152296341Sdelphij    int i;
1153296341Sdelphij    int ptag, pclass;
1154296341Sdelphij    long plen;
1155296341Sdelphij    const unsigned char *p, *q;
1156296341Sdelphij    p = *in;
1157296341Sdelphij    q = p;
1158109998Smarkm
1159296341Sdelphij    if (ctx && ctx->valid) {
1160296341Sdelphij        i = ctx->ret;
1161296341Sdelphij        plen = ctx->plen;
1162296341Sdelphij        pclass = ctx->pclass;
1163296341Sdelphij        ptag = ctx->ptag;
1164296341Sdelphij        p += ctx->hdrlen;
1165296341Sdelphij    } else {
1166296341Sdelphij        i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1167296341Sdelphij        if (ctx) {
1168296341Sdelphij            ctx->ret = i;
1169296341Sdelphij            ctx->plen = plen;
1170296341Sdelphij            ctx->pclass = pclass;
1171296341Sdelphij            ctx->ptag = ptag;
1172296341Sdelphij            ctx->hdrlen = p - q;
1173296341Sdelphij            ctx->valid = 1;
1174296341Sdelphij            /*
1175296341Sdelphij             * If definite length, and no error, length + header can't exceed
1176296341Sdelphij             * total amount of data available.
1177296341Sdelphij             */
1178296341Sdelphij            if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {
1179296341Sdelphij                ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_TOO_LONG);
1180296341Sdelphij                asn1_tlc_clear(ctx);
1181296341Sdelphij                return 0;
1182296341Sdelphij            }
1183296341Sdelphij        }
1184296341Sdelphij    }
1185109998Smarkm
1186296341Sdelphij    if (i & 0x80) {
1187296341Sdelphij        ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
1188296341Sdelphij        asn1_tlc_clear(ctx);
1189296341Sdelphij        return 0;
1190296341Sdelphij    }
1191296341Sdelphij    if (exptag >= 0) {
1192296341Sdelphij        if ((exptag != ptag) || (expclass != pclass)) {
1193296341Sdelphij            /*
1194296341Sdelphij             * If type is OPTIONAL, not an error: indicate missing type.
1195296341Sdelphij             */
1196296341Sdelphij            if (opt)
1197296341Sdelphij                return -1;
1198296341Sdelphij            asn1_tlc_clear(ctx);
1199296341Sdelphij            ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
1200296341Sdelphij            return 0;
1201296341Sdelphij        }
1202296341Sdelphij        /*
1203296341Sdelphij         * We have a tag and class match: assume we are going to do something
1204296341Sdelphij         * with it
1205296341Sdelphij         */
1206296341Sdelphij        asn1_tlc_clear(ctx);
1207296341Sdelphij    }
1208109998Smarkm
1209296341Sdelphij    if (i & 1)
1210296341Sdelphij        plen = len - (p - q);
1211109998Smarkm
1212296341Sdelphij    if (inf)
1213296341Sdelphij        *inf = i & 1;
1214109998Smarkm
1215296341Sdelphij    if (cst)
1216296341Sdelphij        *cst = i & V_ASN1_CONSTRUCTED;
1217109998Smarkm
1218296341Sdelphij    if (olen)
1219296341Sdelphij        *olen = plen;
1220109998Smarkm
1221296341Sdelphij    if (oclass)
1222296341Sdelphij        *oclass = pclass;
1223160814Ssimon
1224296341Sdelphij    if (otag)
1225296341Sdelphij        *otag = ptag;
1226160814Ssimon
1227296341Sdelphij    *in = p;
1228296341Sdelphij    return 1;
1229296341Sdelphij}
1230