2 * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 decode_primitive (const char *typename
, const char *name
, const char *forwstr
)
44 "e = decode_%s(p, len, %s, &l);\n"
51 "e = der_get_%s(p, len, %s, &l);\n"
52 "if(e) %s;\np += l; len -= l; ret += l;\n",
60 find_tag (const Type
*t
,
61 Der_class
*cl
, Der_type
*ty
, unsigned *tag
)
75 errx(1, "Cannot have recursive CHOICE");
84 *tag
= UT_GeneralString
;
89 *tag
= UT_TeletexString
;
91 case TGeneralizedTime
:
94 *tag
= UT_GeneralizedTime
;
119 *tag
= UT_OctetString
;
121 case TPrintableString
:
124 *tag
= UT_PrintableString
;
139 *cl
= t
->tag
.tagclass
;
140 *ty
= is_primitive_type(t
->subtype
->type
) ? PRIM
: CONS
;
141 *tag
= t
->tag
.tagvalue
;
144 if ((t
->symbol
->stype
== Stype
&& t
->symbol
->type
== NULL
)
145 || t
->symbol
->stype
== SUndefined
) {
146 lex_error_message("%s is imported or still undefined, "
147 " can't generate tag checking data in CHOICE "
148 "without this information",
152 find_tag(t
->symbol
->type
, cl
, ty
, tag
);
162 *tag
= UT_UTF8String
;
169 case TUniversalString
:
172 *tag
= UT_UniversalString
;
177 *tag
= UT_VisibleString
;
185 range_check(const char *name
,
190 if (r
->min
== r
->max
+ 2 || r
->min
< r
->max
)
192 "if ((%s)->%s > %lld) {\n"
193 "e = ASN1_MAX_CONSTRAINT; %s;\n"
195 name
, length
, (long long)r
->max
, forwstr
);
196 if ((r
->min
- 1 == r
->max
|| r
->min
< r
->max
) && r
->min
> 0)
198 "if ((%s)->%s < %lld) {\n"
199 "e = ASN1_MIN_CONSTRAINT; %s;\n"
201 name
, length
, (long long)r
->min
, forwstr
);
202 if (r
->max
== r
->min
)
204 "if ((%s)->%s != %lld) {\n"
205 "e = ASN1_EXACT_CONSTRAINT; %s;\n"
207 name
, length
, (long long)r
->min
, forwstr
);
211 decode_type (const char *name
, const Type
*t
, int optional
,
212 const char *forwstr
, const char *tmpstr
, const char *dertype
,
219 "%s = calloc(1, sizeof(*%s));\n"
220 "if (%s == NULL) %s;\n",
221 name
, name
, name
, forwstr
);
223 "e = decode_%s(p, len, %s, &l);\n",
224 t
->symbol
->gen_name
, name
);
231 "p += l; len -= l; ret += l;\n"
239 "p += l; len -= l; ret += l;\n");
246 * This will produce a worning, how its hard to fix since:
247 * if its enum to an NameType, we can add appriate
248 * type-cast. If its not though, we have to figure out if
249 * there is negative enum enum and use appropriate
250 * signness and size on the intertype we cast the result
256 decode_primitive ("integer", "&enumint", forwstr
);
261 } else if (t
->range
== NULL
) {
262 decode_primitive ("heim_integer", name
, forwstr
);
263 } else if (t
->range
->min
< INT_MIN
&& t
->range
->max
<= INT64_MAX
) {
264 decode_primitive ("integer64", name
, forwstr
);
265 } else if (t
->range
->min
>= 0 && t
->range
->max
> UINT_MAX
) {
266 decode_primitive ("unsigned64", name
, forwstr
);
267 } else if (t
->range
->min
>= INT_MIN
&& t
->range
->max
<= INT_MAX
) {
268 decode_primitive ("integer", name
, forwstr
);
269 } else if (t
->range
->min
>= 0 && t
->range
->max
<= UINT_MAX
) {
270 decode_primitive ("unsigned", name
, forwstr
);
272 errx(1, "%s: unsupported range %lld -> %lld",
273 name
, (long long)t
->range
->min
, (long long)t
->range
->max
);
276 decode_primitive ("boolean", name
, forwstr
);
279 decode_primitive ("enumerated", name
, forwstr
);
284 "if (%s == CONS) {\n",
286 decode_primitive("octet_string_ber", name
, forwstr
);
290 decode_primitive ("octet_string", name
, forwstr
);
292 fprintf(codefile
, "}\n");
294 range_check(name
, "length", forwstr
, t
->range
);
300 if (ASN1_TAILQ_EMPTY(t
->members
)) {
301 decode_primitive ("bit_string", name
, forwstr
);
305 "if (len < 1) return ASN1_OVERRUN;\n"
306 "p++; len--; ret++;\n");
309 "if (len < 1) break;\n");
310 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
311 while (m
->val
/ 8 > pos
/ 8) {
313 "p++; len--; ret++;\n"
314 "if (len < 1) break;\n");
318 "(%s)->%s = (*p >> %d) & 1;\n",
319 name
, m
->gen_name
, 7 - m
->val
% 8);
324 "p += len; ret += len;\n");
330 if (t
->members
== NULL
)
333 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
339 if (asprintf (&s
, "%s(%s)->%s", m
->optional
? "" : "&",
340 name
, m
->gen_name
) < 0 || s
== NULL
)
342 decode_type (s
, m
->type
, m
->optional
, forwstr
, m
->gen_name
, NULL
,
353 if(t
->members
== NULL
)
356 fprintf(codefile
, "{\n");
357 fprintf(codefile
, "unsigned int members = 0;\n");
358 fprintf(codefile
, "while(len > 0) {\n");
363 "e = der_get_tag (p, len, &class, &type, &tag, NULL);\n"
364 "if(e) %s;\n", forwstr
);
365 fprintf(codefile
, "switch (MAKE_TAG(class, type, tag)) {\n");
367 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
370 assert(m
->type
->type
== TTag
);
372 fprintf(codefile
, "case MAKE_TAG(%s, %s, %s):\n",
373 classname(m
->type
->tag
.tagclass
),
374 is_primitive_type(m
->type
->subtype
->type
) ? "PRIM" : "CONS",
375 valuename(m
->type
->tag
.tagclass
, m
->type
->tag
.tagvalue
));
377 if (asprintf (&s
, "%s(%s)->%s", m
->optional
? "" : "&", name
, m
->gen_name
) < 0 || s
== NULL
)
381 "%s = calloc(1, sizeof(*%s));\n"
382 "if (%s == NULL) { e = ENOMEM; %s; }\n",
384 decode_type (s
, m
->type
, 0, forwstr
, m
->gen_name
, NULL
, depth
+ 1);
387 fprintf(codefile
, "members |= (1 << %d);\n", memno
);
389 fprintf(codefile
, "break;\n");
393 "return ASN1_MISPLACED_FIELD;\n"
395 fprintf(codefile
, "}\n");
396 fprintf(codefile
, "}\n");
398 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
401 if (asprintf (&s
, "%s->%s", name
, m
->gen_name
) < 0 || s
== NULL
)
403 fprintf(codefile
, "if((members & (1 << %d)) == 0)\n", memno
);
405 fprintf(codefile
, "%s = NULL;\n", s
);
407 gen_assign_defval(s
, m
->defval
);
409 fprintf(codefile
, "return ASN1_MISSING_FIELD;\n");
413 fprintf(codefile
, "}\n");
423 "size_t %s_origlen = len;\n"
424 "size_t %s_oldret = ret;\n"
425 "size_t %s_olen = 0;\n"
429 "(%s)->val = NULL;\n",
438 "while(ret < %s_origlen) {\n"
439 "size_t %s_nlen = %s_olen + sizeof(*((%s)->val));\n"
440 "if (%s_olen > %s_nlen) { e = ASN1_OVERFLOW; %s; }\n"
441 "%s_olen = %s_nlen;\n"
442 "%s_tmp = realloc((%s)->val, %s_olen);\n"
443 "if (%s_tmp == NULL) { e = ENOMEM; %s; }\n"
444 "(%s)->val = %s_tmp;\n",
446 tmpstr
, tmpstr
, name
,
447 tmpstr
, tmpstr
, forwstr
,
449 tmpstr
, name
, tmpstr
,
453 if (asprintf (&n
, "&(%s)->val[(%s)->len]", name
, name
) < 0 || n
== NULL
)
455 if (asprintf (&sname
, "%s_s_of", tmpstr
) < 0 || sname
== NULL
)
457 decode_type (n
, t
->subtype
, 0, forwstr
, sname
, NULL
, depth
+ 1);
460 "len = %s_origlen - ret;\n"
462 "ret += %s_oldret;\n"
467 range_check(name
, "len", forwstr
, t
->range
);
472 case TGeneralizedTime
:
473 decode_primitive ("generalized_time", name
, forwstr
);
476 decode_primitive ("general_string", name
, forwstr
);
479 decode_primitive ("general_string", name
, forwstr
);
482 char *tname
= NULL
, *typestring
= NULL
;
485 if (asprintf(&typestring
, "%s_type", tmpstr
) < 0 || typestring
== NULL
)
490 "size_t %s_datalen, %s_oldlen;\n"
492 tmpstr
, tmpstr
, typestring
);
495 "int is_indefinite%u;\n", depth
);
497 fprintf(codefile
, "e = der_match_tag_and_length(p, len, %s, &%s, %s, "
498 "&%s_datalen, &l);\n",
499 classname(t
->tag
.tagclass
),
501 valuename(t
->tag
.tagclass
, t
->tag
.tagvalue
),
504 /* XXX hardcode for now */
505 if (support_ber
&& t
->subtype
->type
== TOctetString
) {
509 "if (e == 0 && %s != %s) { e = ASN1_BAD_ID; }\n",
511 is_primitive_type(t
->subtype
->type
) ? "PRIM" : "CONS");
519 "%s = calloc(1, sizeof(*%s));\n"
520 "if (%s == NULL) { e = ENOMEM; %s; }\n",
521 name
, name
, name
, name
, forwstr
);
523 fprintf(codefile
, "if(e) %s;\n", forwstr
);
526 "p += l; len -= l; ret += l;\n"
527 "%s_oldlen = len;\n",
531 "if((is_indefinite%u = _heim_fix_dce(%s_datalen, &len)) < 0)\n"
532 "{ e = ASN1_BAD_FORMAT; %s; }\n"
533 "if (is_indefinite%u) { if (len < 2) { e = ASN1_OVERRUN; %s; } len -= 2; }",
534 depth
, tmpstr
, forwstr
, depth
, forwstr
);
537 "if (%s_datalen > len) { e = ASN1_OVERRUN; %s; }\n"
538 "len = %s_datalen;\n", tmpstr
, forwstr
, tmpstr
);
539 if (asprintf (&tname
, "%s_Tag", tmpstr
) < 0 || tname
== NULL
)
541 decode_type (name
, t
->subtype
, 0, forwstr
, tname
, ide
, depth
+ 1);
544 "if(is_indefinite%u){\n"
546 "e = der_match_tag_and_length(p, len, "
547 "(Der_class)0, &%s, UT_EndOfContent, "
548 "&%s_datalen, &l);\n"
550 "p += l; len -= l; ret += l;\n"
551 "if (%s != (Der_type)0) { e = ASN1_BAD_ID; %s; }\n"
557 typestring
, forwstr
);
559 "len = %s_oldlen - %s_datalen;\n",
571 Member
*m
, *have_ellipsis
= NULL
;
572 const char *els
= "";
574 if (t
->members
== NULL
)
577 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
578 const Type
*tt
= m
->type
;
589 find_tag(tt
, &cl
, &ty
, &tag
);
592 "%sif (der_match_tag(p, len, %s, %s, %s, NULL) == 0) {\n",
595 ty
? "CONS" : "PRIM",
597 if (asprintf (&s
, "%s(%s)->u.%s", m
->optional
? "" : "&",
598 name
, m
->gen_name
) < 0 || s
== NULL
)
600 decode_type (s
, m
->type
, m
->optional
, forwstr
, m
->gen_name
, NULL
,
603 "(%s)->element = %s;\n",
613 "(%s)->u.%s.data = calloc(1, len);\n"
614 "if ((%s)->u.%s.data == NULL) {\n"
617 "(%s)->u.%s.length = len;\n"
618 "memcpy((%s)->u.%s.data, p, len);\n"
619 "(%s)->element = %s;\n"
624 name
, have_ellipsis
->gen_name
,
625 name
, have_ellipsis
->gen_name
,
627 name
, have_ellipsis
->gen_name
,
628 name
, have_ellipsis
->gen_name
,
629 name
, have_ellipsis
->label
);
633 "e = ASN1_PARSE_ERROR;\n"
641 decode_primitive ("utctime", name
, forwstr
);
644 decode_primitive ("utf8string", name
, forwstr
);
646 case TPrintableString
:
647 decode_primitive ("printable_string", name
, forwstr
);
650 decode_primitive ("ia5_string", name
, forwstr
);
653 decode_primitive ("bmp_string", name
, forwstr
);
655 case TUniversalString
:
656 decode_primitive ("universal_string", name
, forwstr
);
659 decode_primitive ("visible_string", name
, forwstr
);
662 fprintf (codefile
, "/* NULL */\n");
665 decode_primitive ("oid", name
, forwstr
);
674 generate_type_decode (const Symbol
*s
)
676 int preserve
= preserve_type(s
->name
) ? TRUE
: FALSE
;
678 fprintf (codefile
, "int ASN1CALL\n"
679 "decode_%s(const unsigned char *p HEIMDAL_UNUSED_ATTRIBUTE,"
680 " size_t len HEIMDAL_UNUSED_ATTRIBUTE, %s *data, size_t *size)\n"
682 s
->gen_name
, s
->gen_name
);
684 switch (s
->type
->type
) {
689 case TGeneralizedTime
:
693 case TPrintableString
:
696 case TUniversalString
:
711 "size_t l HEIMDAL_UNUSED_ATTRIBUTE;\n"
712 "int e HEIMDAL_UNUSED_ATTRIBUTE;\n");
714 fprintf (codefile
, "const unsigned char *begin = p;\n");
716 fprintf (codefile
, "\n");
717 fprintf (codefile
, "memset(data, 0, sizeof(*data));\n"); /* hack to avoid `unused variable' */
719 decode_type ("data", s
->type
, 0, "goto fail", "Top", NULL
, 1);
722 "data->_save.data = calloc(1, ret);\n"
723 "if (data->_save.data == NULL) { \n"
724 "e = ENOMEM; goto fail; \n"
726 "data->_save.length = ret;\n"
727 "memcpy(data->_save.data, begin, ret);\n");
729 "if(size) *size = ret;\n"
740 fprintf (codefile
, "}\n\n");