1 /* $NetBSD: gen_decode.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $ */
4 * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 decode_primitive (const char *typename
, const char *name
, const char *forwstr
)
46 "e = decode_%s(p, len, %s, &l);\n"
53 "e = der_get_%s(p, len, %s, &l);\n"
54 "if(e) %s;\np += l; len -= l; ret += l;\n",
62 find_tag (const Type
*t
,
63 Der_class
*cl
, Der_type
*ty
, unsigned *tag
)
77 errx(1, "Cannot have recursive CHOICE");
86 *tag
= UT_GeneralString
;
91 *tag
= UT_TeletexString
;
93 case TGeneralizedTime
:
96 *tag
= UT_GeneralizedTime
;
121 *tag
= UT_OctetString
;
123 case TPrintableString
:
126 *tag
= UT_PrintableString
;
141 *cl
= t
->tag
.tagclass
;
142 *ty
= is_primitive_type(t
->subtype
->type
) ? PRIM
: CONS
;
143 *tag
= t
->tag
.tagvalue
;
146 if ((t
->symbol
->stype
== Stype
&& t
->symbol
->type
== NULL
)
147 || t
->symbol
->stype
== SUndefined
) {
148 lex_error_message("%s is imported or still undefined, "
149 " can't generate tag checking data in CHOICE "
150 "without this information",
154 find_tag(t
->symbol
->type
, cl
, ty
, tag
);
164 *tag
= UT_UTF8String
;
171 case TUniversalString
:
174 *tag
= UT_UniversalString
;
179 *tag
= UT_VisibleString
;
187 range_check(const char *name
,
192 if (r
->min
== r
->max
+ 2 || r
->min
< r
->max
)
194 "if ((%s)->%s > %d) {\n"
195 "e = ASN1_MAX_CONSTRAINT; %s;\n"
197 name
, length
, r
->max
, forwstr
);
198 if (r
->min
- 1 == r
->max
|| r
->min
< r
->max
)
200 "if ((%s)->%s < %d) {\n"
201 "e = ASN1_MIN_CONSTRAINT; %s;\n"
203 name
, length
, r
->min
, forwstr
);
204 if (r
->max
== r
->min
)
206 "if ((%s)->%s != %d) {\n"
207 "e = ASN1_EXACT_CONSTRAINT; %s;\n"
209 name
, length
, r
->min
, forwstr
);
213 decode_type (const char *name
, const Type
*t
, int optional
,
214 const char *forwstr
, const char *tmpstr
, const char *dertype
,
221 "%s = calloc(1, sizeof(*%s));\n"
222 "if (%s == NULL) %s;\n",
223 name
, name
, name
, forwstr
);
225 "e = decode_%s(p, len, %s, &l);\n",
226 t
->symbol
->gen_name
, name
);
233 "p += l; len -= l; ret += l;\n"
241 "p += l; len -= l; ret += l;\n");
250 decode_primitive ("integer", "&enumint", forwstr
);
255 } else if (t
->range
== NULL
) {
256 decode_primitive ("heim_integer", name
, forwstr
);
257 } else if (t
->range
->min
== INT_MIN
&& t
->range
->max
== INT_MAX
) {
258 decode_primitive ("integer", name
, forwstr
);
259 } else if (t
->range
->min
== 0 && t
->range
->max
== UINT_MAX
) {
260 decode_primitive ("unsigned", name
, forwstr
);
261 } else if (t
->range
->min
== 0 && t
->range
->max
== INT_MAX
) {
262 decode_primitive ("unsigned", name
, forwstr
);
264 errx(1, "%s: unsupported range %d -> %d",
265 name
, t
->range
->min
, t
->range
->max
);
268 decode_primitive ("boolean", name
, forwstr
);
271 decode_primitive ("enumerated", name
, forwstr
);
276 "if (%s == CONS) {\n",
278 decode_primitive("octet_string_ber", name
, forwstr
);
282 decode_primitive ("octet_string", name
, forwstr
);
284 fprintf(codefile
, "}\n");
286 range_check(name
, "length", forwstr
, t
->range
);
292 if (ASN1_TAILQ_EMPTY(t
->members
)) {
293 decode_primitive ("bit_string", name
, forwstr
);
297 "if (len < 1) return ASN1_OVERRUN;\n"
298 "p++; len--; ret++;\n");
301 "if (len < 1) break;\n");
302 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
303 while (m
->val
/ 8 > pos
/ 8) {
305 "p++; len--; ret++;\n"
306 "if (len < 1) break;\n");
310 "(%s)->%s = (*p >> %d) & 1;\n",
311 name
, m
->gen_name
, 7 - m
->val
% 8);
316 "p += len; ret += len;\n");
322 if (t
->members
== NULL
)
325 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
331 if (asprintf (&s
, "%s(%s)->%s", m
->optional
? "" : "&",
332 name
, m
->gen_name
) < 0 || s
== NULL
)
334 decode_type (s
, m
->type
, m
->optional
, forwstr
, m
->gen_name
, NULL
,
345 if(t
->members
== NULL
)
348 fprintf(codefile
, "{\n");
349 fprintf(codefile
, "unsigned int members = 0;\n");
350 fprintf(codefile
, "while(len > 0) {\n");
355 "e = der_get_tag (p, len, &class, &type, &tag, NULL);\n"
356 "if(e) %s;\n", forwstr
);
357 fprintf(codefile
, "switch (MAKE_TAG(class, type, tag)) {\n");
359 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
362 assert(m
->type
->type
== TTag
);
364 fprintf(codefile
, "case MAKE_TAG(%s, %s, %s):\n",
365 classname(m
->type
->tag
.tagclass
),
366 is_primitive_type(m
->type
->subtype
->type
) ? "PRIM" : "CONS",
367 valuename(m
->type
->tag
.tagclass
, m
->type
->tag
.tagvalue
));
369 if (asprintf (&s
, "%s(%s)->%s", m
->optional
? "" : "&", name
, m
->gen_name
) < 0 || s
== NULL
)
373 "%s = calloc(1, sizeof(*%s));\n"
374 "if (%s == NULL) { e = ENOMEM; %s; }\n",
376 decode_type (s
, m
->type
, 0, forwstr
, m
->gen_name
, NULL
, depth
+ 1);
379 fprintf(codefile
, "members |= (1 << %d);\n", memno
);
381 fprintf(codefile
, "break;\n");
385 "return ASN1_MISPLACED_FIELD;\n"
387 fprintf(codefile
, "}\n");
388 fprintf(codefile
, "}\n");
390 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
393 if (asprintf (&s
, "%s->%s", name
, m
->gen_name
) < 0 || s
== NULL
)
395 fprintf(codefile
, "if((members & (1 << %d)) == 0)\n", memno
);
397 fprintf(codefile
, "%s = NULL;\n", s
);
399 gen_assign_defval(s
, m
->defval
);
401 fprintf(codefile
, "return ASN1_MISSING_FIELD;\n");
405 fprintf(codefile
, "}\n");
415 "size_t %s_origlen = len;\n"
416 "size_t %s_oldret = ret;\n"
417 "size_t %s_olen = 0;\n"
421 "(%s)->val = NULL;\n",
430 "while(ret < %s_origlen) {\n"
431 "size_t %s_nlen = %s_olen + sizeof(*((%s)->val));\n"
432 "if (%s_olen > %s_nlen) { e = ASN1_OVERFLOW; %s; }\n"
433 "%s_olen = %s_nlen;\n"
434 "%s_tmp = realloc((%s)->val, %s_olen);\n"
435 "if (%s_tmp == NULL) { e = ENOMEM; %s; }\n"
436 "(%s)->val = %s_tmp;\n",
438 tmpstr
, tmpstr
, name
,
439 tmpstr
, tmpstr
, forwstr
,
441 tmpstr
, name
, tmpstr
,
445 if (asprintf (&n
, "&(%s)->val[(%s)->len]", name
, name
) < 0 || n
== NULL
)
447 if (asprintf (&sname
, "%s_s_of", tmpstr
) < 0 || sname
== NULL
)
449 decode_type (n
, t
->subtype
, 0, forwstr
, sname
, NULL
, depth
+ 1);
452 "len = %s_origlen - ret;\n"
454 "ret += %s_oldret;\n"
459 range_check(name
, "len", forwstr
, t
->range
);
464 case TGeneralizedTime
:
465 decode_primitive ("generalized_time", name
, forwstr
);
468 decode_primitive ("general_string", name
, forwstr
);
471 decode_primitive ("general_string", name
, forwstr
);
474 char *tname
= NULL
, *typestring
= NULL
;
477 if (asprintf(&typestring
, "%s_type", tmpstr
) < 0 || typestring
== NULL
)
482 "size_t %s_datalen, %s_oldlen;\n"
484 tmpstr
, tmpstr
, typestring
);
487 "int is_indefinite%u;\n", depth
);
489 fprintf(codefile
, "e = der_match_tag_and_length(p, len, %s, &%s, %s, "
490 "&%s_datalen, &l);\n",
491 classname(t
->tag
.tagclass
),
493 valuename(t
->tag
.tagclass
, t
->tag
.tagvalue
),
496 /* XXX hardcode for now */
497 if (support_ber
&& t
->subtype
->type
== TOctetString
) {
501 "if (e == 0 && %s != %s) { e = ASN1_BAD_ID; }\n",
503 is_primitive_type(t
->subtype
->type
) ? "PRIM" : "CONS");
511 "%s = calloc(1, sizeof(*%s));\n"
512 "if (%s == NULL) { e = ENOMEM; %s; }\n",
513 name
, name
, name
, name
, forwstr
);
515 fprintf(codefile
, "if(e) %s;\n", forwstr
);
518 "p += l; len -= l; ret += l;\n"
519 "%s_oldlen = len;\n",
523 "if((is_indefinite%u = _heim_fix_dce(%s_datalen, &len)) < 0)\n"
524 "{ e = ASN1_BAD_FORMAT; %s; }\n"
525 "if (is_indefinite%u) { if (len < 2) { e = ASN1_OVERRUN; %s; } len -= 2; }",
526 depth
, tmpstr
, forwstr
, depth
, forwstr
);
529 "if (%s_datalen > len) { e = ASN1_OVERRUN; %s; }\n"
530 "len = %s_datalen;\n", tmpstr
, forwstr
, tmpstr
);
531 if (asprintf (&tname
, "%s_Tag", tmpstr
) < 0 || tname
== NULL
)
533 decode_type (name
, t
->subtype
, 0, forwstr
, tname
, ide
, depth
+ 1);
536 "if(is_indefinite%u){\n"
538 "e = der_match_tag_and_length(p, len, "
539 "(Der_class)0, &%s, UT_EndOfContent, "
540 "&%s_datalen, &l);\n"
542 "p += l; len -= l; ret += l;\n"
543 "if (%s != (Der_type)0) { e = ASN1_BAD_ID; %s; }\n"
549 typestring
, forwstr
);
551 "len = %s_oldlen - %s_datalen;\n",
563 Member
*m
, *have_ellipsis
= NULL
;
564 const char *els
= "";
566 if (t
->members
== NULL
)
569 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
570 const Type
*tt
= m
->type
;
581 find_tag(tt
, &cl
, &ty
, &tag
);
584 "%sif (der_match_tag(p, len, %s, %s, %s, NULL) == 0) {\n",
587 ty
? "CONS" : "PRIM",
589 if (asprintf (&s
, "%s(%s)->u.%s", m
->optional
? "" : "&",
590 name
, m
->gen_name
) < 0 || s
== NULL
)
592 decode_type (s
, m
->type
, m
->optional
, forwstr
, m
->gen_name
, NULL
,
595 "(%s)->element = %s;\n",
605 "(%s)->u.%s.data = calloc(1, len);\n"
606 "if ((%s)->u.%s.data == NULL) {\n"
609 "(%s)->u.%s.length = len;\n"
610 "memcpy((%s)->u.%s.data, p, len);\n"
611 "(%s)->element = %s;\n"
616 name
, have_ellipsis
->gen_name
,
617 name
, have_ellipsis
->gen_name
,
619 name
, have_ellipsis
->gen_name
,
620 name
, have_ellipsis
->gen_name
,
621 name
, have_ellipsis
->label
);
625 "e = ASN1_PARSE_ERROR;\n"
633 decode_primitive ("utctime", name
, forwstr
);
636 decode_primitive ("utf8string", name
, forwstr
);
638 case TPrintableString
:
639 decode_primitive ("printable_string", name
, forwstr
);
642 decode_primitive ("ia5_string", name
, forwstr
);
645 decode_primitive ("bmp_string", name
, forwstr
);
647 case TUniversalString
:
648 decode_primitive ("universal_string", name
, forwstr
);
651 decode_primitive ("visible_string", name
, forwstr
);
654 fprintf (codefile
, "/* NULL */\n");
657 decode_primitive ("oid", name
, forwstr
);
666 generate_type_decode (const Symbol
*s
)
668 int preserve
= preserve_type(s
->name
) ? TRUE
: FALSE
;
670 fprintf (codefile
, "int ASN1CALL\n"
671 "decode_%s(const unsigned char *p HEIMDAL_UNUSED_ATTRIBUTE,"
672 " size_t len HEIMDAL_UNUSED_ATTRIBUTE, %s *data, size_t *size)\n"
674 s
->gen_name
, s
->gen_name
);
676 switch (s
->type
->type
) {
681 case TGeneralizedTime
:
685 case TPrintableString
:
688 case TUniversalString
:
703 "size_t l HEIMDAL_UNUSED_ATTRIBUTE;\n"
704 "int e HEIMDAL_UNUSED_ATTRIBUTE;\n");
706 fprintf (codefile
, "const unsigned char *begin = p;\n");
708 fprintf (codefile
, "\n");
709 fprintf (codefile
, "memset(data, 0, sizeof(*data));\n"); /* hack to avoid `unused variable' */
711 decode_type ("data", s
->type
, 0, "goto fail", "Top", NULL
, 1);
714 "data->_save.data = calloc(1, ret);\n"
715 "if (data->_save.data == NULL) { \n"
716 "e = ENOMEM; goto fail; \n"
718 "data->_save.length = ret;\n"
719 "memcpy(data->_save.data, begin, ret);\n");
721 "if(size) *size = ret;\n"
732 fprintf (codefile
, "}\n\n");