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 > %d) {\n"
193 "e = ASN1_MAX_CONSTRAINT; %s;\n"
195 name, length, r->max, forwstr);
196 if (r->min - 1 == r->max || r->min < r->max)
198 "if ((%s)->%s < %d) {\n"
199 "e = ASN1_MIN_CONSTRAINT; %s;\n"
201 name, length, r->min, forwstr);
202 if (r->max == r->min)
204 "if ((%s)->%s != %d) {\n"
205 "e = ASN1_EXACT_CONSTRAINT; %s;\n"
207 name, length, 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");
248 decode_primitive ("integer", "&enumint", forwstr);
253 } else if (t->range == NULL) {
254 decode_primitive ("heim_integer", name, forwstr);
255 } else if (t->range->min == INT_MIN && t->range->max == INT_MAX) {
256 decode_primitive ("integer", name, forwstr);
257 } else if (t->range->min == 0 && t->range->max == UINT_MAX) {
258 decode_primitive ("unsigned", name, forwstr);
259 } else if (t->range->min == 0 && t->range->max == INT_MAX) {
260 decode_primitive ("unsigned", name, forwstr);
262 errx(1, "%s: unsupported range %d -> %d",
263 name, t->range->min, t->range->max);
266 decode_primitive ("boolean", name, forwstr);
269 decode_primitive ("enumerated", name, forwstr);
274 "if (%s == CONS) {\n",
276 decode_primitive("octet_string_ber", name, forwstr);
280 decode_primitive ("octet_string", name, forwstr);
282 fprintf(codefile, "}\n");
284 range_check(name, "length", forwstr, t->range);
290 if (ASN1_TAILQ_EMPTY(t->members)) {
291 decode_primitive ("bit_string", name, forwstr);
295 "if (len < 1) return ASN1_OVERRUN;\n"
296 "p++; len--; ret++;\n");
299 "if (len < 1) break;\n");
300 ASN1_TAILQ_FOREACH(m, t->members, members) {
301 while (m->val / 8 > pos / 8) {
303 "p++; len--; ret++;\n"
304 "if (len < 1) break;\n");
308 "(%s)->%s = (*p >> %d) & 1;\n",
309 name, m->gen_name, 7 - m->val % 8);
314 "p += len; ret += len;\n");
320 if (t->members == NULL)
323 ASN1_TAILQ_FOREACH(m, t->members, members) {
329 if (asprintf (&s, "%s(%s)->%s", m->optional ? "" : "&",
330 name, m->gen_name) < 0 || s == NULL)
332 decode_type (s, m->type, m->optional, forwstr, m->gen_name, NULL,
343 if(t->members == NULL)
346 fprintf(codefile, "{\n");
347 fprintf(codefile, "unsigned int members = 0;\n");
348 fprintf(codefile, "while(len > 0) {\n");
353 "e = der_get_tag (p, len, &class, &type, &tag, NULL);\n"
354 "if(e) %s;\n", forwstr);
355 fprintf(codefile, "switch (MAKE_TAG(class, type, tag)) {\n");
357 ASN1_TAILQ_FOREACH(m, t->members, members) {
360 assert(m->type->type == TTag);
362 fprintf(codefile, "case MAKE_TAG(%s, %s, %s):\n",
363 classname(m->type->tag.tagclass),
364 is_primitive_type(m->type->subtype->type) ? "PRIM" : "CONS",
365 valuename(m->type->tag.tagclass, m->type->tag.tagvalue));
367 if (asprintf (&s, "%s(%s)->%s", m->optional ? "" : "&", name, m->gen_name) < 0 || s == NULL)
371 "%s = calloc(1, sizeof(*%s));\n"
372 "if (%s == NULL) { e = ENOMEM; %s; }\n",
374 decode_type (s, m->type, 0, forwstr, m->gen_name, NULL, depth + 1);
377 fprintf(codefile, "members |= (1 << %d);\n", memno);
379 fprintf(codefile, "break;\n");
383 "return ASN1_MISPLACED_FIELD;\n"
385 fprintf(codefile, "}\n");
386 fprintf(codefile, "}\n");
388 ASN1_TAILQ_FOREACH(m, t->members, members) {
391 if (asprintf (&s, "%s->%s", name, m->gen_name) < 0 || s == NULL)
393 fprintf(codefile, "if((members & (1 << %d)) == 0)\n", memno);
395 fprintf(codefile, "%s = NULL;\n", s);
397 gen_assign_defval(s, m->defval);
399 fprintf(codefile, "return ASN1_MISSING_FIELD;\n");
403 fprintf(codefile, "}\n");
413 "size_t %s_origlen = len;\n"
414 "size_t %s_oldret = ret;\n"
415 "size_t %s_olen = 0;\n"
419 "(%s)->val = NULL;\n",
428 "while(ret < %s_origlen) {\n"
429 "size_t %s_nlen = %s_olen + sizeof(*((%s)->val));\n"
430 "if (%s_olen > %s_nlen) { e = ASN1_OVERFLOW; %s; }\n"
431 "%s_olen = %s_nlen;\n"
432 "%s_tmp = realloc((%s)->val, %s_olen);\n"
433 "if (%s_tmp == NULL) { e = ENOMEM; %s; }\n"
434 "(%s)->val = %s_tmp;\n",
436 tmpstr, tmpstr, name,
437 tmpstr, tmpstr, forwstr,
439 tmpstr, name, tmpstr,
443 if (asprintf (&n, "&(%s)->val[(%s)->len]", name, name) < 0 || n == NULL)
445 if (asprintf (&sname, "%s_s_of", tmpstr) < 0 || sname == NULL)
447 decode_type (n, t->subtype, 0, forwstr, sname, NULL, depth + 1);
450 "len = %s_origlen - ret;\n"
452 "ret += %s_oldret;\n"
457 range_check(name, "len", forwstr, t->range);
462 case TGeneralizedTime:
463 decode_primitive ("generalized_time", name, forwstr);
466 decode_primitive ("general_string", name, forwstr);
469 decode_primitive ("general_string", name, forwstr);
472 char *tname = NULL, *typestring = NULL;
475 if (asprintf(&typestring, "%s_type", tmpstr) < 0 || typestring == NULL)
480 "size_t %s_datalen, %s_oldlen;\n"
482 tmpstr, tmpstr, typestring);
485 "int is_indefinite%u;\n", depth);
487 fprintf(codefile, "e = der_match_tag_and_length(p, len, %s, &%s, %s, "
488 "&%s_datalen, &l);\n",
489 classname(t->tag.tagclass),
491 valuename(t->tag.tagclass, t->tag.tagvalue),
494 /* XXX hardcode for now */
495 if (support_ber && t->subtype->type == TOctetString) {
499 "if (e == 0 && %s != %s) { e = ASN1_BAD_ID; }\n",
501 is_primitive_type(t->subtype->type) ? "PRIM" : "CONS");
509 "%s = calloc(1, sizeof(*%s));\n"
510 "if (%s == NULL) { e = ENOMEM; %s; }\n",
511 name, name, name, name, forwstr);
513 fprintf(codefile, "if(e) %s;\n", forwstr);
516 "p += l; len -= l; ret += l;\n"
517 "%s_oldlen = len;\n",
521 "if((is_indefinite%u = _heim_fix_dce(%s_datalen, &len)) < 0)\n"
522 "{ e = ASN1_BAD_FORMAT; %s; }\n"
523 "if (is_indefinite%u) { if (len < 2) { e = ASN1_OVERRUN; %s; } len -= 2; }",
524 depth, tmpstr, forwstr, depth, forwstr);
527 "if (%s_datalen > len) { e = ASN1_OVERRUN; %s; }\n"
528 "len = %s_datalen;\n", tmpstr, forwstr, tmpstr);
529 if (asprintf (&tname, "%s_Tag", tmpstr) < 0 || tname == NULL)
531 decode_type (name, t->subtype, 0, forwstr, tname, ide, depth + 1);
534 "if(is_indefinite%u){\n"
536 "e = der_match_tag_and_length(p, len, "
537 "(Der_class)0, &%s, UT_EndOfContent, "
538 "&%s_datalen, &l);\n"
540 "p += l; len -= l; ret += l;\n"
541 "if (%s != (Der_type)0) { e = ASN1_BAD_ID; %s; }\n"
547 typestring, forwstr);
549 "len = %s_oldlen - %s_datalen;\n",
561 Member *m, *have_ellipsis = NULL;
562 const char *els = "";
564 if (t->members == NULL)
567 ASN1_TAILQ_FOREACH(m, t->members, members) {
568 const Type *tt = m->type;
579 find_tag(tt, &cl, &ty, &tag);
582 "%sif (der_match_tag(p, len, %s, %s, %s, NULL) == 0) {\n",
585 ty ? "CONS" : "PRIM",
587 if (asprintf (&s, "%s(%s)->u.%s", m->optional ? "" : "&",
588 name, m->gen_name) < 0 || s == NULL)
590 decode_type (s, m->type, m->optional, forwstr, m->gen_name, NULL,
593 "(%s)->element = %s;\n",
603 "(%s)->u.%s.data = calloc(1, len);\n"
604 "if ((%s)->u.%s.data == NULL) {\n"
607 "(%s)->u.%s.length = len;\n"
608 "memcpy((%s)->u.%s.data, p, len);\n"
609 "(%s)->element = %s;\n"
614 name, have_ellipsis->gen_name,
615 name, have_ellipsis->gen_name,
617 name, have_ellipsis->gen_name,
618 name, have_ellipsis->gen_name,
619 name, have_ellipsis->label);
623 "e = ASN1_PARSE_ERROR;\n"
631 decode_primitive ("utctime", name, forwstr);
634 decode_primitive ("utf8string", name, forwstr);
636 case TPrintableString:
637 decode_primitive ("printable_string", name, forwstr);
640 decode_primitive ("ia5_string", name, forwstr);
643 decode_primitive ("bmp_string", name, forwstr);
645 case TUniversalString:
646 decode_primitive ("universal_string", name, forwstr);
649 decode_primitive ("visible_string", name, forwstr);
652 fprintf (codefile, "/* NULL */\n");
655 decode_primitive ("oid", name, forwstr);
664 generate_type_decode (const Symbol *s)
666 int preserve = preserve_type(s->name) ? TRUE : FALSE;
668 fprintf (codefile, "int ASN1CALL\n"
669 "decode_%s(const unsigned char *p HEIMDAL_UNUSED_ATTRIBUTE,"
670 " size_t len HEIMDAL_UNUSED_ATTRIBUTE, %s *data, size_t *size)\n"
672 s->gen_name, s->gen_name);
674 switch (s->type->type) {
679 case TGeneralizedTime:
683 case TPrintableString:
686 case TUniversalString:
701 "size_t l HEIMDAL_UNUSED_ATTRIBUTE;\n"
702 "int e HEIMDAL_UNUSED_ATTRIBUTE;\n");
704 fprintf (codefile, "const unsigned char *begin = p;\n");
706 fprintf (codefile, "\n");
707 fprintf (codefile, "memset(data, 0, sizeof(*data));\n"); /* hack to avoid `unused variable' */
709 decode_type ("data", s->type, 0, "goto fail", "Top", NULL, 1);
712 "data->_save.data = calloc(1, ret);\n"
713 "if (data->_save.data == NULL) { \n"
714 "e = ENOMEM; goto fail; \n"
716 "data->_save.length = ret;\n"
717 "memcpy(data->_save.data, begin, ret);\n");
719 "if(size) *size = ret;\n"
730 fprintf (codefile, "}\n\n");