From be0a52e88516c1eb017060d86c13da45a6448e80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Love=20H=C3=B6rnquist=20=C3=85strand?= Date: Mon, 15 Dec 2008 04:29:52 +0000 Subject: [PATCH] handle BER octet string, remove EoD tag before parsing content of a INDEF encoding to the greedy encodings in inside (like ANY) will swallow the EoD git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@24186 ec53bebd-3082-4978-b11e-865c3cabbd6b --- lib/asn1/gen_decode.c | 56 ++++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/lib/asn1/gen_decode.c b/lib/asn1/gen_decode.c index 2bd5acb47..62c000748 100644 --- a/lib/asn1/gen_decode.c +++ b/lib/asn1/gen_decode.c @@ -230,7 +230,7 @@ range_check(const char *name, static int decode_type (const char *name, const Type *t, int optional, - const char *forwstr, const char *tmpstr) + const char *forwstr, const char *tmpstr, const char *dertype) { switch (t->type) { case TType: { @@ -289,7 +289,17 @@ decode_type (const char *name, const Type *t, int optional, decode_primitive ("enumerated", name, forwstr); break; case TOctetString: + if (dertype) { + fprintf(codefile, + "if (%s == CONS) {\n", + dertype); + decode_primitive("octet_string_ber", name, forwstr); + fprintf(codefile, + "} else {\n"); + } decode_primitive ("octet_string", name, forwstr); + if (dertype) + fprintf(codefile, "}\n"); if (t->range) range_check(name, "length", forwstr, t->range); break; @@ -340,7 +350,7 @@ decode_type (const char *name, const Type *t, int optional, name, m->gen_name); if (s == NULL) errx(1, "malloc"); - decode_type (s, m->type, m->optional, forwstr, m->gen_name); + decode_type (s, m->type, m->optional, forwstr, m->gen_name, NULL); free (s); } @@ -382,7 +392,7 @@ decode_type (const char *name, const Type *t, int optional, "%s = calloc(1, sizeof(*%s));\n" "if (%s == NULL) { e = ENOMEM; %s; }\n", s, s, s, forwstr); - decode_type (s, m->type, 0, forwstr, m->gen_name); + decode_type (s, m->type, 0, forwstr, m->gen_name, NULL); free (s); fprintf(codefile, "members |= (1 << %d);\n", memno); @@ -458,7 +468,7 @@ decode_type (const char *name, const Type *t, int optional, asprintf (&sname, "%s_s_of", tmpstr); if (sname == NULL) errx(1, "malloc"); - decode_type (n, t->subtype, 0, forwstr, sname); + decode_type (n, t->subtype, 0, forwstr, sname, NULL); fprintf (codefile, "(%s)->len++;\n" "len = %s_origlen - ret;\n" @@ -480,21 +490,36 @@ decode_type (const char *name, const Type *t, int optional, decode_primitive ("general_string", name, forwstr); break; case TTag:{ - char *tname; + char *tname, *typestring; + char *ide = NULL; + + asprintf(&typestring, "%s_type", tmpstr); fprintf(codefile, "{\n" - "size_t %s_datalen, %s_oldlen;\n", - tmpstr, tmpstr); + "size_t %s_datalen, %s_oldlen;\n" + "Der_type %s;\n", + tmpstr, tmpstr, typestring); if(dce_fix) fprintf(codefile, "int dce_fix;\n"); - fprintf(codefile, "e = der_match_tag_and_length(p, len, %s, %s, %s, " + + fprintf(codefile, "e = der_match_tag_and_length2(p, len, %s, &%s, %s, " "&%s_datalen, &l);\n", classname(t->tag.tagclass), - is_primitive_type(t->subtype->type) ? "PRIM" : "CONS", + typestring, valuename(t->tag.tagclass, t->tag.tagvalue), tmpstr); + + /* XXX hardcode for now */ + if (dce_fix && t->subtype->type == TOctetString) { + ide = typestring; + } else { + fprintf(codefile, + "if (%s != %s) { e = ASN1_BAD_ID; }\n", + typestring, is_primitive_type(t->subtype->type) ? "PRIM" : "CONS"); + } + if(optional) { fprintf(codefile, "if(e) {\n" @@ -513,8 +538,9 @@ decode_type (const char *name, const Type *t, int optional, if(dce_fix) fprintf (codefile, "if((dce_fix = _heim_fix_dce(%s_datalen, &len)) < 0)\n" - "{ e = ASN1_BAD_FORMAT; %s; }\n", - tmpstr, forwstr); + "{ e = ASN1_BAD_FORMAT; %s; }\n" + "if (dce_fix) { if (len < 2) { e = ASN1_OVERRUN; %s; } len -= 2; }", + tmpstr, forwstr, forwstr); else fprintf(codefile, "if (%s_datalen > len) { e = ASN1_OVERRUN; %s; }\n" @@ -522,10 +548,11 @@ decode_type (const char *name, const Type *t, int optional, asprintf (&tname, "%s_Tag", tmpstr); if (tname == NULL) errx(1, "malloc"); - decode_type (name, t->subtype, 0, forwstr, tname); + decode_type (name, t->subtype, 0, forwstr, tname, ide); if(dce_fix) fprintf(codefile, "if(dce_fix){\n" + "len += 2;\n" "e = der_match_tag_and_length (p, len, " "(Der_class)0,(Der_type)0, UT_EndOfContent, " "&%s_datalen, &l);\n" @@ -540,6 +567,7 @@ decode_type (const char *name, const Type *t, int optional, fprintf(codefile, "}\n"); free(tname); + free(typestring); break; } case TChoice: { @@ -573,7 +601,7 @@ decode_type (const char *name, const Type *t, int optional, name, m->gen_name); if (s == NULL) errx(1, "malloc"); - decode_type (s, m->type, m->optional, forwstr, m->gen_name); + decode_type (s, m->type, m->optional, forwstr, m->gen_name, NULL); fprintf(codefile, "(%s)->element = %s;\n", name, m->label); @@ -695,7 +723,7 @@ generate_type_decode (const Symbol *s) fprintf (codefile, "\n"); fprintf (codefile, "memset(data, 0, sizeof(*data));\n"); /* hack to avoid `unused variable' */ - decode_type ("data", s->type, 0, "goto fail", "Top"); + decode_type ("data", s->type, 0, "goto fail", "Top", NULL); if (preserve) fprintf (codefile, "data->_save.data = calloc(1, ret);\n"