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 is_primitive_type(int type
)
68 case TGeneralizedTime
:
73 case TPrintableString
:
76 case TUniversalString
:
86 find_tag (const Type
*t
,
87 Der_class
*cl
, Der_type
*ty
, unsigned *tag
)
101 errx(1, "Cannot have recursive CHOICE");
105 *tag
= UT_Enumerated
;
110 *tag
= UT_GeneralString
;
112 case TGeneralizedTime
:
115 *tag
= UT_GeneralizedTime
;
140 *tag
= UT_OctetString
;
142 case TPrintableString
:
145 *tag
= UT_PrintableString
;
160 *cl
= t
->tag
.tagclass
;
161 *ty
= is_primitive_type(t
->subtype
->type
) ? PRIM
: CONS
;
162 *tag
= t
->tag
.tagvalue
;
165 if ((t
->symbol
->stype
== Stype
&& t
->symbol
->type
== NULL
)
166 || t
->symbol
->stype
== SUndefined
) {
167 error_message("%s is imported or still undefined, "
168 " can't generate tag checking data in CHOICE "
169 "without this information",
173 find_tag(t
->symbol
->type
, cl
, ty
, tag
);
183 *tag
= UT_UTF8String
;
190 case TUniversalString
:
193 *tag
= UT_UniversalString
;
198 *tag
= UT_VisibleString
;
206 range_check(const char *name
,
211 if (r
->min
== r
->max
+ 2 || r
->min
< r
->max
)
213 "if ((%s)->%s > %d) {\n"
214 "e = ASN1_MAX_CONSTRAINT; %s;\n"
216 name
, length
, r
->max
, forwstr
);
217 if (r
->min
- 1 == r
->max
|| r
->min
< r
->max
)
219 "if ((%s)->%s < %d) {\n"
220 "e = ASN1_MIN_CONSTRAINT; %s;\n"
222 name
, length
, r
->min
, forwstr
);
223 if (r
->max
== r
->min
)
225 "if ((%s)->%s != %d) {\n"
226 "e = ASN1_EXACT_CONSTRAINT; %s;\n"
228 name
, length
, r
->min
, forwstr
);
232 decode_type (const char *name
, const Type
*t
, int optional
,
233 const char *forwstr
, const char *tmpstr
, const char *dertype
)
239 "%s = calloc(1, sizeof(*%s));\n"
240 "if (%s == NULL) %s;\n",
241 name
, name
, name
, forwstr
);
243 "e = decode_%s(p, len, %s, &l);\n",
244 t
->symbol
->gen_name
, name
);
251 "p += l; len -= l; ret += l;\n"
259 "p += l; len -= l; ret += l;\n");
268 decode_primitive ("integer", "&enumint", forwstr
);
273 } else if (t
->range
== NULL
) {
274 decode_primitive ("heim_integer", name
, forwstr
);
275 } else if (t
->range
->min
== INT_MIN
&& t
->range
->max
== INT_MAX
) {
276 decode_primitive ("integer", name
, forwstr
);
277 } else if (t
->range
->min
== 0 && t
->range
->max
== UINT_MAX
) {
278 decode_primitive ("unsigned", name
, forwstr
);
279 } else if (t
->range
->min
== 0 && t
->range
->max
== INT_MAX
) {
280 decode_primitive ("unsigned", name
, forwstr
);
282 errx(1, "%s: unsupported range %d -> %d",
283 name
, t
->range
->min
, t
->range
->max
);
286 decode_primitive ("boolean", name
, forwstr
);
289 decode_primitive ("enumerated", name
, forwstr
);
294 "if (%s == CONS) {\n",
296 decode_primitive("octet_string_ber", name
, forwstr
);
300 decode_primitive ("octet_string", name
, forwstr
);
302 fprintf(codefile
, "}\n");
304 range_check(name
, "length", forwstr
, t
->range
);
310 if (ASN1_TAILQ_EMPTY(t
->members
)) {
311 decode_primitive ("bit_string", name
, forwstr
);
315 "if (len < 1) return ASN1_OVERRUN;\n"
316 "p++; len--; ret++;\n");
319 "if (len < 1) break;\n");
320 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
321 while (m
->val
/ 8 > pos
/ 8) {
323 "p++; len--; ret++;\n"
324 "if (len < 1) break;\n");
328 "(%s)->%s = (*p >> %d) & 1;\n",
329 name
, m
->gen_name
, 7 - m
->val
% 8);
334 "p += len; ret += len;\n");
340 if (t
->members
== NULL
)
343 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
349 asprintf (&s
, "%s(%s)->%s", m
->optional
? "" : "&",
353 decode_type (s
, m
->type
, m
->optional
, forwstr
, m
->gen_name
, NULL
);
363 if(t
->members
== NULL
)
366 fprintf(codefile
, "{\n");
367 fprintf(codefile
, "unsigned int members = 0;\n");
368 fprintf(codefile
, "while(len > 0) {\n");
373 "e = der_get_tag (p, len, &class, &type, &tag, NULL);\n"
374 "if(e) %s;\n", forwstr
);
375 fprintf(codefile
, "switch (MAKE_TAG(class, type, tag)) {\n");
377 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
380 assert(m
->type
->type
== TTag
);
382 fprintf(codefile
, "case MAKE_TAG(%s, %s, %s):\n",
383 classname(m
->type
->tag
.tagclass
),
384 is_primitive_type(m
->type
->subtype
->type
) ? "PRIM" : "CONS",
385 valuename(m
->type
->tag
.tagclass
, m
->type
->tag
.tagvalue
));
387 asprintf (&s
, "%s(%s)->%s", m
->optional
? "" : "&", name
, m
->gen_name
);
392 "%s = calloc(1, sizeof(*%s));\n"
393 "if (%s == NULL) { e = ENOMEM; %s; }\n",
395 decode_type (s
, m
->type
, 0, forwstr
, m
->gen_name
, NULL
);
398 fprintf(codefile
, "members |= (1 << %d);\n", memno
);
400 fprintf(codefile
, "break;\n");
404 "return ASN1_MISPLACED_FIELD;\n"
406 fprintf(codefile
, "}\n");
407 fprintf(codefile
, "}\n");
409 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
412 asprintf (&s
, "%s->%s", name
, m
->gen_name
);
415 fprintf(codefile
, "if((members & (1 << %d)) == 0)\n", memno
);
417 fprintf(codefile
, "%s = NULL;\n", s
);
419 gen_assign_defval(s
, m
->defval
);
421 fprintf(codefile
, "return ASN1_MISSING_FIELD;\n");
425 fprintf(codefile
, "}\n");
435 "size_t %s_origlen = len;\n"
436 "size_t %s_oldret = ret;\n"
437 "size_t %s_olen = 0;\n"
441 "(%s)->val = NULL;\n",
450 "while(ret < %s_origlen) {\n"
451 "size_t %s_nlen = %s_olen + sizeof(*((%s)->val));\n"
452 "if (%s_olen > %s_nlen) { e = ASN1_OVERFLOW; %s; }\n"
453 "%s_olen = %s_nlen;\n"
454 "%s_tmp = realloc((%s)->val, %s_olen);\n"
455 "if (%s_tmp == NULL) { e = ENOMEM; %s; }\n"
456 "(%s)->val = %s_tmp;\n",
458 tmpstr
, tmpstr
, name
,
459 tmpstr
, tmpstr
, forwstr
,
461 tmpstr
, name
, tmpstr
,
465 asprintf (&n
, "&(%s)->val[(%s)->len]", name
, name
);
468 asprintf (&sname
, "%s_s_of", tmpstr
);
471 decode_type (n
, t
->subtype
, 0, forwstr
, sname
, NULL
);
474 "len = %s_origlen - ret;\n"
476 "ret += %s_oldret;\n"
481 range_check(name
, "len", forwstr
, t
->range
);
486 case TGeneralizedTime
:
487 decode_primitive ("generalized_time", name
, forwstr
);
490 decode_primitive ("general_string", name
, forwstr
);
493 char *tname
, *typestring
;
496 asprintf(&typestring
, "%s_type", tmpstr
);
500 "size_t %s_datalen, %s_oldlen;\n"
502 tmpstr
, tmpstr
, typestring
);
505 "int is_indefinite;\n");
507 fprintf(codefile
, "e = der_match_tag_and_length(p, len, %s, &%s, %s, "
508 "&%s_datalen, &l);\n",
509 classname(t
->tag
.tagclass
),
511 valuename(t
->tag
.tagclass
, t
->tag
.tagvalue
),
514 /* XXX hardcode for now */
515 if (support_ber
&& t
->subtype
->type
== TOctetString
) {
519 "if (e == 0 && %s != %s) { e = ASN1_BAD_ID; }\n",
521 is_primitive_type(t
->subtype
->type
) ? "PRIM" : "CONS");
529 "%s = calloc(1, sizeof(*%s));\n"
530 "if (%s == NULL) { e = ENOMEM; %s; }\n",
531 name
, name
, name
, name
, forwstr
);
533 fprintf(codefile
, "if(e) %s;\n", forwstr
);
536 "p += l; len -= l; ret += l;\n"
537 "%s_oldlen = len;\n",
541 "if((is_indefinite = _heim_fix_dce(%s_datalen, &len)) < 0)\n"
542 "{ e = ASN1_BAD_FORMAT; %s; }\n"
543 "if (is_indefinite) { if (len < 2) { e = ASN1_OVERRUN; %s; } len -= 2; }",
544 tmpstr
, forwstr
, forwstr
);
547 "if (%s_datalen > len) { e = ASN1_OVERRUN; %s; }\n"
548 "len = %s_datalen;\n", tmpstr
, forwstr
, tmpstr
);
549 asprintf (&tname
, "%s_Tag", tmpstr
);
552 decode_type (name
, t
->subtype
, 0, forwstr
, tname
, ide
);
555 "if(is_indefinite){\n"
557 "e = der_match_tag_and_length(p, len, "
558 "(Der_class)0, &%s, UT_EndOfContent, "
559 "&%s_datalen, &l);\n"
561 "p += l; len -= l; ret += l;\n"
562 "if (%s != (Der_type)0) { e = ASN1_BAD_ID; %s; }\n"
567 typestring
, forwstr
);
569 "len = %s_oldlen - %s_datalen;\n",
581 Member
*m
, *have_ellipsis
= NULL
;
582 const char *els
= "";
584 if (t
->members
== NULL
)
587 ASN1_TAILQ_FOREACH(m
, t
->members
, members
) {
588 const Type
*tt
= m
->type
;
599 find_tag(tt
, &cl
, &ty
, &tag
);
602 "%sif (der_match_tag(p, len, %s, %s, %s, NULL) == 0) {\n",
605 ty
? "CONS" : "PRIM",
607 asprintf (&s
, "%s(%s)->u.%s", m
->optional
? "" : "&",
611 decode_type (s
, m
->type
, m
->optional
, forwstr
, m
->gen_name
, NULL
);
613 "(%s)->element = %s;\n",
623 "(%s)->u.%s.data = calloc(1, len);\n"
624 "if ((%s)->u.%s.data == NULL) {\n"
627 "(%s)->u.%s.length = len;\n"
628 "memcpy((%s)->u.%s.data, p, len);\n"
629 "(%s)->element = %s;\n"
634 name
, have_ellipsis
->gen_name
,
635 name
, have_ellipsis
->gen_name
,
637 name
, have_ellipsis
->gen_name
,
638 name
, have_ellipsis
->gen_name
,
639 name
, have_ellipsis
->label
);
643 "e = ASN1_PARSE_ERROR;\n"
651 decode_primitive ("utctime", name
, forwstr
);
654 decode_primitive ("utf8string", name
, forwstr
);
656 case TPrintableString
:
657 decode_primitive ("printable_string", name
, forwstr
);
660 decode_primitive ("ia5_string", name
, forwstr
);
663 decode_primitive ("bmp_string", name
, forwstr
);
665 case TUniversalString
:
666 decode_primitive ("universal_string", name
, forwstr
);
669 decode_primitive ("visible_string", name
, forwstr
);
672 fprintf (codefile
, "/* NULL */\n");
675 decode_primitive ("oid", name
, forwstr
);
684 generate_type_decode (const Symbol
*s
)
686 int preserve
= preserve_type(s
->name
) ? TRUE
: FALSE
;
690 "decode_%s(const unsigned char *, size_t, %s *, size_t *);\n",
691 s
->gen_name
, s
->gen_name
);
693 fprintf (codefile
, "int\n"
694 "decode_%s(const unsigned char *p,"
695 " size_t len, %s *data, size_t *size)\n"
697 s
->gen_name
, s
->gen_name
);
699 switch (s
->type
->type
) {
704 case TGeneralizedTime
:
707 case TPrintableString
:
710 case TUniversalString
:
728 fprintf (codefile
, "const unsigned char *begin = p;\n");
730 fprintf (codefile
, "\n");
731 fprintf (codefile
, "memset(data, 0, sizeof(*data));\n"); /* hack to avoid `unused variable' */
733 decode_type ("data", s
->type
, 0, "goto fail", "Top", NULL
);
736 "data->_save.data = calloc(1, ret);\n"
737 "if (data->_save.data == NULL) { \n"
738 "e = ENOMEM; goto fail; \n"
740 "data->_save.length = ret;\n"
741 "memcpy(data->_save.data, begin, ret);\n");
743 "if(size) *size = ret;\n"
754 fprintf (codefile
, "}\n\n");