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
36 /* XXX same as der_length_tag */
38 length_tag(unsigned int tag)
52 encode_primitive (const char *typename, const char *name)
55 "e = der_put_%s(p, len, %s, &l);\n"
56 "if (e) return e;\np -= l; len -= l; ret += l;\n\n",
62 classname(Der_class class)
64 const char *cn[] = { "ASN1_C_UNIV", "ASN1_C_APPL",
65 "ASN1_C_CONTEXT", "ASN1_C_PRIV" };
66 if ((int)class >= sizeof(cn) / sizeof(cn[0]))
73 valuename(Der_class class, int value)
80 #define X(Y) { Y, #Y }
88 X(UT_GeneralizedTime),
95 X(UT_ObjectDescriptor),
97 X(UT_PrintableString),
105 X(UT_UniversalString),
106 X(UT_VideotexString),
111 if(class == ASN1_C_UNIV) {
112 for(p = values; p->value != -1; p++)
113 if(p->value == value)
116 snprintf(s, sizeof(s), "%d", value);
121 encode_type (const char *name, const Type *t, const char *tmpstr)
128 encode_type (name, t->symbol->type);
131 "e = encode_%s(p, len, %s, &l);\n"
132 "if (e) return e;\np -= l; len -= l; ret += l;\n\n",
133 t->symbol->gen_name, name);
134 constructed = !is_primitive_type(t);
140 "int enumint = (int)*%s;\n",
142 encode_primitive("integer", "&enumint");
143 fprintf(codefile, "}\n;");
144 } else if (t->range == NULL) {
145 encode_primitive("heim_integer", name);
146 } else if (t->range->min < 0 &&
147 (t->range->min < INT_MIN || t->range->max > INT_MAX)) {
148 encode_primitive("integer64", name);
149 } else if (t->range->min < 0) {
150 encode_primitive("integer", name);
151 } else if (t->range->max > UINT_MAX) {
152 encode_primitive("unsigned64", name);
154 encode_primitive("unsigned", name);
160 encode_primitive ("boolean", name);
164 encode_primitive ("octet_string", name);
171 if (HEIM_TAILQ_EMPTY(t->members)) {
172 encode_primitive("bit_string", name);
177 fprintf (codefile, "{\n"
178 "unsigned char c = 0;\n");
179 if (!rfc1510_bitstring)
182 "int bit_set = 0;\n");
184 pos = t->members->prev->val;
185 /* fix for buggy MIT (and OSF?) code */
190 * It seems that if we do not always set pos to 31 here, the MIT
191 * code will do the wrong thing.
193 * I hate ASN.1 (and DER), but I hate it even more when everybody
194 * has to screw it up differently.
196 pos = HEIM_TAILQ_LAST(t->members, memhead)->val;
197 if (rfc1510_bitstring) {
202 HEIM_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) {
203 while (m->val / 8 < pos / 8) {
204 if (!rfc1510_bitstring)
206 "if (c != 0 || bit_set) {\n");
208 "if (len < 1) return ASN1_OVERFLOW;\n"
209 "*p-- = c; len--; ret++;\n");
210 if (!rfc1510_bitstring)
215 "if (c & 1) break;\n"
229 name, m->gen_name, 7 - m->val % 8);
234 if (!rfc1510_bitstring)
236 "if (c != 0 || bit_set) {\n");
238 "if (len < 1) return ASN1_OVERFLOW;\n"
239 "*p-- = c; len--; ret++;\n");
240 if (!rfc1510_bitstring)
246 "if (c & 1) break;\n"
255 "if (len < 1) return ASN1_OVERFLOW;\n"
260 rfc1510_bitstring ? "0" : "rest");
265 encode_primitive ("enumerated", name);
274 if (t->members == NULL)
277 HEIM_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) {
283 if (asprintf (&s, "%s(%s)->%s", m->optional ? "" : "&", name, m->gen_name) < 0 || s == NULL)
285 fprintf(codefile, "/* %s */\n", m->name);
291 gen_compare_defval(s + 1, m->defval);
292 fprintf (codefile, "{\n");
293 fprintf (codefile, "size_t %s_oldret HEIMDAL_UNUSED_ATTRIBUTE = ret;\n", tmpstr);
294 fprintf (codefile, "ret = 0;\n");
295 encode_type (s, m->type, m->gen_name);
296 fprintf (codefile, "ret += %s_oldret;\n", tmpstr);
297 fprintf (codefile, "}\n");
306 "heim_octet_string *val;\n"
307 "size_t elen = 0, totallen = 0;\n"
311 "if ((%s)->len > UINT_MAX/sizeof(val[0]))\n"
316 "val = malloc(sizeof(val[0]) * (%s)->len);\n"
317 "if (val == NULL && (%s)->len != 0) return ENOMEM;\n",
321 "for(i = 0; i < (int)(%s)->len; i++) {\n",
325 "ASN1_MALLOC_ENCODE(%s, val[i].data, "
326 "val[i].length, &(%s)->val[i], &elen, eret);\n",
327 t->subtype->symbol->gen_name,
334 "free(val[i].data);\n"
340 "totallen += elen;\n"
344 "if (totallen > len) {\n"
345 "for (i = 0; i < (int)(%s)->len; i++) {\n"
346 "free(val[i].data);\n"
349 "return ASN1_OVERFLOW;\n"
354 "qsort(val, (%s)->len, sizeof(val[0]), _heim_der_set_sort);\n",
358 "for(i = (int)(%s)->len - 1; i >= 0; --i) {\n"
359 "p -= val[i].length;\n"
360 "ret += val[i].length;\n"
361 "memcpy(p + 1, val[i].data, val[i].length);\n"
362 "free(val[i].data);\n"
374 "for(i = (int)(%s)->len - 1; i >= 0; --i) {\n"
375 "size_t %s_for_oldret = ret;\n"
378 if (asprintf (&n, "&(%s)->val[i]", name) < 0 || n == NULL)
380 if (asprintf (&sname, "%s_S_Of", tmpstr) < 0 || sname == NULL)
382 encode_type (n, t->subtype, sname);
384 "ret += %s_for_oldret;\n"
391 case TGeneralizedTime:
392 encode_primitive ("generalized_time", name);
396 encode_primitive ("general_string", name);
400 encode_primitive ("general_string", name);
406 int prim = !(t->tag.tagclass != ASN1_C_UNIV &&
407 t->tag.tagenv == TE_EXPLICIT) &&
408 is_primitive_type(t->subtype);
410 if (asprintf (&tname, "%s_tag", tmpstr) < 0 || tname == NULL)
415 * This is part of the fix to the bug where we treated IMPLICIT tags of
416 * named types as EXPLICIT. I.e.
418 * Foo ::= SEQUENCE { ... }
419 * Bar ::= SEQUENCE { foo [0] IMPLICIT Foo }
421 * would get a context [0] constructed tag *and* a universal sequence
422 * constructed tag when it should get only the first tag.
424 * Properly fixing this would require changing the signatures of the
425 * encode, length, and decode functions we generate to take an optional
426 * tag to replace the one the encoder would generate / decoder would
427 * expect. That would change the ABI, which... isn't stable, but it's
428 * a bit soon to make that change.
430 * So, we're looking for IMPLICIT, and if we see any, we generate code
431 * to replace the tag.
433 * On the decode side we need to know what tag to restore. For this we
434 * generate enums in the generated header.
436 * NOTE: We *do* "replace" the tags of IMPLICIT-tagged primitive types,
437 * but our primitive codec functions leave those tags out, which
438 * is why we don't have to der_replace_tag() them here.
441 * If the tag is IMPLICIT and it's not primitive and the subtype is not
442 * any kind of structure...
444 if (t->tag.tagenv == TE_IMPLICIT && !prim &&
445 t->subtype->type != TSequenceOf && t->subtype->type != TSetOf &&
446 t->subtype->type != TChoice) {
447 /* If it is a named type for a structured thing */
448 if (t->subtype->symbol &&
449 (t->subtype->type == TSequence ||
450 t->subtype->type == TSet))
452 else if (t->subtype->symbol && strcmp(t->subtype->symbol->name, "heim_any"))
454 } else if (t->tag.tagenv == TE_IMPLICIT && prim && t->subtype->symbol)
456 * Because the subtype is named we are generating its codec
457 * functions, and those will be adding their UNIVERSAL or whatever
458 * tags unlike our raw primtive codec library.
460 replace_tag = is_tagged_type(t->subtype->symbol->type);
464 "{ unsigned char *psave_%s = p;\n"
465 "size_t l2_%s, lensave_%s = len;\n"
466 "len = length_%s(%s);\n"
467 /* Allocate a temp buffer for the encoder */
468 "if ((p = malloc(len)) == NULL) return ENOMEM;\n"
469 /* Make p point to the last byte of the allocated buf */
471 tmpstr, tmpstr, tmpstr,
472 t->subtype->symbol->gen_name, name);
474 c = encode_type (name, t->subtype, tname);
475 /* Explicit non-UNIVERSAL tags are always constructed */
476 if (!c && t->tag.tagclass != ASN1_C_UNIV && t->tag.tagenv == TE_EXPLICIT)
480 "if (len) abort();\n"
482 * Here we have `p' pointing to one byte before the buffer
483 * we allocated above.
485 * [ T_wrong | LL | VVVV ] // temp buffer
492 * psave_<fieldName> still points to the last byte in the
493 * original buffer passed in where we should write the
494 * encoding of <fieldName>.
496 * We adjust psave_<fieldName> to point to before the TLV
497 * encoding of <fieldName> (with wrong tag) in the original
498 * buffer (this may NOT be a valid pointer, but we won't
501 * [ ... | T_wrong | LL | VVVVV | ... ] // original buffer
505 * +-- psave_<fieldName>
509 * We further adjust psave_<fieldName> to point to the last
510 * byte of what should be the T(ag) of the TLV encoding of
511 * <fieldName> (this is now a valid pointer), then...
513 * |<--->| (not written yet)
514 * | | |<-------->| (not written yet)
515 * [ ... | T_right | LL | VVVVV | ... ] // original buffer
519 * +-- psave_<fieldName>
521 "psave_%s += asn1_tag_length_%s;\n"
523 * ...copy the L(ength)V(alue) of the TLV encoding of
526 * [ ... | T_right | LL | VVVVV | ... ] // original buffer
530 * +-- psave_<fieldName> + 1
532 * |<----->| length is
533 * | | `l' - asn1_tag_length_<fieldName>
534 * [ T_wrong | LL | VVVV ] // temp buffer
538 * \ +-- p + 1 + asn1_tag_length_%s
541 "memcpy(psave_%s + 1, p + 1 + asn1_tag_length_%s, l - asn1_tag_length_%s);\n"
543 * Encode the IMPLICIT tag. Recall that encoders like
544 * der_put_tag() take a pointer to the last byte they
545 * should write to, and a length of bytes to the left of
546 * that that they are allowed to write into.
548 * [ ... | T_right | LL | VVVVV | ... ] // original buffer
552 * +-- psave_<fieldName>
554 "e = der_put_tag(psave_%s, %lu, %s, %s, %d, &l2_%s);\n"
556 /* Restore `len' and adjust it (see `p' below) */
557 "len = lensave_%s - (l + %lu - asn1_tag_length_%s);\n"
559 * Adjust `ret' to account for the difference in size
560 * between the length of the right and wrong tags.
562 "ret += %lu - asn1_tag_length_%s;\n"
563 /* Free the buffer and restore `p' */
566 * Make `p' point into the original buffer again, to one
567 * byte before the bytes we wrote:
569 * [ ... | T_right | LL | VVVVV | ... ] // original buffer
575 "p = psave_%s - (1 + %lu - asn1_tag_length_%s); }\n",
576 tmpstr, tmpstr, t->subtype->symbol->name,
577 tmpstr, t->subtype->symbol->name, t->subtype->symbol->name,
578 tmpstr, length_tag(t->tag.tagvalue),
579 classname(t->tag.tagclass),
584 tmpstr, length_tag(t->tag.tagvalue), t->subtype->symbol->name,
585 length_tag(t->tag.tagvalue), t->subtype->symbol->name,
586 tmpstr, length_tag(t->tag.tagvalue), t->subtype->symbol->name);
589 "e = der_put_length_and_tag (p, len, ret, %s, %s, %s, &l);\n"
590 "if (e) return e;\np -= l; len -= l; ret += l;\n\n",
591 classname(t->tag.tagclass),
593 valuename(t->tag.tagclass, t->tag.tagvalue));
599 Member *m, *have_ellipsis = NULL;
602 if (t->members == NULL)
605 fprintf(codefile, "\n");
607 if (asprintf (&s, "(%s)", name) < 0 || s == NULL)
609 fprintf(codefile, "switch(%s->element) {\n", s);
611 HEIM_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) {
619 fprintf (codefile, "case %s: {", m->label);
620 if (asprintf(&s2, "%s(%s)->u.%s", m->optional ? "" : "&",
621 s, m->gen_name) < 0 || s2 == NULL)
624 fprintf (codefile, "if(%s) {\n", s2);
625 fprintf (codefile, "size_t %s_oldret = ret;\n", tmpstr);
626 fprintf (codefile, "ret = 0;\n");
627 constructed = encode_type (s2, m->type, m->gen_name);
628 fprintf (codefile, "ret += %s_oldret;\n", tmpstr);
630 fprintf (codefile, "}\n");
631 fprintf(codefile, "break;\n");
632 fprintf(codefile, "}\n");
639 "if (len < (%s)->u.%s.length)\n"
640 "return ASN1_OVERFLOW;\n"
641 "p -= (%s)->u.%s.length;\n"
642 "ret += (%s)->u.%s.length;\n"
643 "memcpy(p + 1, (%s)->u.%s.data, (%s)->u.%s.length);\n"
646 have_ellipsis->label,
647 name, have_ellipsis->gen_name,
648 name, have_ellipsis->gen_name,
649 name, have_ellipsis->gen_name,
650 name, have_ellipsis->gen_name,
651 name, have_ellipsis->gen_name);
653 fprintf(codefile, "};\n");
657 encode_primitive ("oid", name);
661 encode_primitive ("utctime", name);
665 encode_primitive ("utf8string", name);
668 case TPrintableString:
669 encode_primitive ("printable_string", name);
673 encode_primitive ("ia5_string", name);
677 encode_primitive ("bmp_string", name);
680 case TUniversalString:
681 encode_primitive ("universal_string", name);
685 encode_primitive ("visible_string", name);
689 fprintf (codefile, "/* NULL */\n");
699 generate_type_encode (const Symbol *s)
701 fprintf (codefile, "int ASN1CALL\n"
702 "encode_%s(unsigned char *p HEIMDAL_UNUSED_ATTRIBUTE, size_t len HEIMDAL_UNUSED_ATTRIBUTE,"
703 " const %s *data, size_t *size)\n"
705 s->gen_name, s->gen_name);
707 switch (s->type->type) {
711 case TGeneralizedTime:
716 case TPrintableString:
719 case TUniversalString:
733 "size_t ret HEIMDAL_UNUSED_ATTRIBUTE = 0;\n"
734 "size_t l HEIMDAL_UNUSED_ATTRIBUTE;\n"
735 "int i HEIMDAL_UNUSED_ATTRIBUTE, e HEIMDAL_UNUSED_ATTRIBUTE;\n\n");
737 encode_type("data", s->type, "Top");
739 fprintf (codefile, "*size = ret;\n"
745 fprintf (codefile, "}\n\n");