68 static int asn1_check_eoc(
const unsigned char **in,
long len);
69 static int asn1_find_end(
const unsigned char **in,
long len,
char inf);
71 static int asn1_collect(
BUF_MEM *buf,
const unsigned char **in,
long len,
72 char inf,
int tag,
int aclass,
int depth);
74 static int collect_data(
BUF_MEM *buf,
const unsigned char **
p,
long plen);
76 static int asn1_check_tlen(
long *olen,
int *otag,
unsigned char *oclass,
78 const unsigned char **in,
long len,
79 int exptag,
int expclass,
char opt,
82 static int asn1_template_ex_d2i(
ASN1_VALUE **pval,
83 const unsigned char **in,
long len,
86 static int asn1_template_noexp_d2i(
ASN1_VALUE **val,
87 const unsigned char **in,
long len,
90 static int asn1_d2i_ex_primitive(
ASN1_VALUE **pval,
91 const unsigned char **in,
long len,
93 int tag,
int aclass,
char opt,
ASN1_TLC *ctx);
96 static const unsigned long tag2bit[32] = {
110 if ((tag < 0) || (tag > 30))
return 0;
116 #define asn1_tlc_clear(c) if (c) (c)->valid = 0
118 #define asn1_tlc_clear_nc(c) (c)->valid = 0
129 const unsigned char **in,
long len,
const ASN1_ITEM *it)
146 return asn1_template_ex_d2i(pval, in, len, tt, 0, &c);
156 int tag,
int aclass,
char opt,
ASN1_TLC *ctx)
163 const unsigned char *
p = NULL, *
q;
164 unsigned char *wp=NULL;
165 unsigned char imphack = 0, oclass;
166 char seq_eoc, seq_nolen, cst, isopt;
189 if ((tag != -1) || opt)
195 return asn1_template_ex_d2i(pval, in, len,
198 return asn1_d2i_ex_primitive(pval, in, len, it,
199 tag, aclass, opt, ctx);
205 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
206 &p, len, -1, 0, 1, ctx);
233 return asn1_d2i_ex_primitive(pval, in, len,
234 it, otag, 0, 0, ctx);
240 it, tag, aclass, opt, ctx);
257 ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL,
258 &p, len, exptag, aclass, 1, ctx);
286 wp = *(
unsigned char **)in;
298 ptmpval = cf->
asn1_d2i(pval, in, len);
323 for (i = 0, tt=it->
templates; i < it->tcount; i++, tt++)
329 ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);
376 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
377 &p, len, tag, aclass, opt, ctx);
388 len = tmplen - (p - *in);
392 else seq_nolen = seq_eoc;
411 for (i = 0, tt = it->
templates; i < it->tcount; i++, tt++)
423 if (asn1_check_eoc(&p, len))
442 if (i == (it->
tcount - 1))
448 ret = asn1_template_ex_d2i(pseqval, &p, len,
468 if (seq_eoc && !asn1_check_eoc(&p, len))
474 if (!seq_nolen && len)
485 for (; i < it->
tcount; tt++, i++)
522 ", Type=", it->
sname);
532 static int asn1_template_ex_d2i(
ASN1_VALUE **val,
533 const unsigned char **in,
long inlen,
540 const unsigned char *
p, *
q;
557 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
558 &p, inlen, tt->
tag, aclass, opt, ctx);
575 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
587 if (!asn1_check_eoc(&p, len))
607 return asn1_template_noexp_d2i(val, in, inlen,
618 static int asn1_template_noexp_d2i(
ASN1_VALUE **val,
619 const unsigned char **in,
long len,
625 const unsigned char *
p, *
q;
654 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
655 &p, len, sktag, skaclass, opt, ctx);
693 if (asn1_check_eoc(&p, len))
729 else if (flags & ASN1_TFLG_IMPTAG)
766 static int asn1_d2i_ex_primitive(
ASN1_VALUE **pval,
767 const unsigned char **in,
long inlen,
769 int tag,
int aclass,
char opt,
ASN1_TLC *ctx)
773 char cst, inf, free_cont = 0;
774 const unsigned char *
p;
776 const unsigned char *cont = NULL;
795 unsigned char oclass;
809 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
810 &p, inlen, -1, 0, 0, ctx);
827 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
828 &p, inlen, tag, aclass, opt, ctx);
860 if (!asn1_find_end(&p, plen, inf))
866 len = p - cont + plen;
896 cont = (
const unsigned char *)buf.
data;
907 if (!
asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
920 int utype,
char *free_cont,
const ASN1_ITEM *it)
931 return pf->
prim_c2i(pval, cont, len, utype, free_cont, it);
937 typ = ASN1_TYPE_new();
945 if (utype != typ->
type)
1050 stmp->
data = (
unsigned char *)cont;
1075 ASN1_TYPE_free(typ);
1089 static int asn1_find_end(
const unsigned char **in,
long len,
char inf)
1093 const unsigned char *p = *in, *
q;
1108 if(asn1_check_eoc(&p, len))
1111 if (expected_eoc == 0)
1118 if(!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1144 #ifndef ASN1_MAX_STRING_NEST
1150 #define ASN1_MAX_STRING_NEST 5
1154 static int asn1_collect(
BUF_MEM *buf,
const unsigned char **in,
long len,
1155 char inf,
int tag,
int aclass,
int depth)
1157 const unsigned char *
p, *
q;
1173 if (asn1_check_eoc(&p, len))
1187 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1188 len, tag, aclass, 0, NULL))
1203 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass,
1207 else if (plen && !collect_data(buf, &p, plen))
1220 static int collect_data(
BUF_MEM *buf,
const unsigned char **p,
long plen)
1231 memcpy(buf->
data + len, *p, plen);
1239 static int asn1_check_eoc(
const unsigned char **in,
long len)
1241 const unsigned char *
p;
1242 if (len < 2)
return 0;
1259 static int asn1_check_tlen(
long *olen,
int *otag,
unsigned char *oclass,
1260 char *inf,
char *cst,
1261 const unsigned char **in,
long len,
1262 int exptag,
int expclass,
char opt,
1268 const unsigned char *
p, *
q;
1272 if (ctx && ctx->
valid)
1294 if (!(i & 0x81) && ((plen + ctx->
hdrlen) > len))
1312 if ((exptag != ptag) || (expclass != pclass))
1328 plen = len - (p -
q);