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 = calloc(1, 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, *pfree_%s = NULL;\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 = pfree_%s = calloc(1, len)) == NULL) return ENOMEM;\n"
469 /* Make p point to the last byte of the allocated buf */
471 tmpstr
, tmpstr
, tmpstr
, tmpstr
,
472 t
->subtype
->symbol
->gen_name
, name
, tmpstr
);
474 /* XXX Currently we generate code that leaks `pfree_%s` here. */
475 c
= encode_type (name
, t
->subtype
, tname
);
476 /* Explicit non-UNIVERSAL tags are always constructed */
477 if (!c
&& t
->tag
.tagclass
!= ASN1_C_UNIV
&& t
->tag
.tagenv
== TE_EXPLICIT
)
481 "if (len) { free(pfree_%s); return EINVAL; }\n"
483 * Here we have `p' pointing to one byte before the buffer
484 * we allocated above.
486 * [ T_wrong | LL | VVVV ] // temp buffer
493 * psave_<fieldName> still points to the last byte in the
494 * original buffer passed in where we should write the
495 * encoding of <fieldName>.
497 * We adjust psave_<fieldName> to point to before the TLV
498 * encoding of <fieldName> (with wrong tag) in the original
499 * buffer (this may NOT be a valid pointer, but we won't
502 * [ ... | T_wrong | LL | VVVVV | ... ] // original buffer
506 * +-- psave_<fieldName>
510 * We further adjust psave_<fieldName> to point to the last
511 * byte of what should be the T(ag) of the TLV encoding of
512 * <fieldName> (this is now a valid pointer), then...
514 * |<--->| (not written yet)
515 * | | |<-------->| (not written yet)
516 * [ ... | T_right | LL | VVVVV | ... ] // original buffer
520 * +-- psave_<fieldName>
522 "psave_%s += asn1_tag_length_%s;\n"
524 * ...copy the L(ength)V(alue) of the TLV encoding of
527 * [ ... | T_right | LL | VVVVV | ... ] // original buffer
531 * +-- psave_<fieldName> + 1
533 * |<----->| length is
534 * | | `l' - asn1_tag_length_<fieldName>
535 * [ T_wrong | LL | VVVV ] // temp buffer
539 * \ +-- p + 1 + asn1_tag_length_%s
542 "memcpy(psave_%s + 1, p + 1 + asn1_tag_length_%s, l - asn1_tag_length_%s);\n"
544 * Encode the IMPLICIT tag. Recall that encoders like
545 * der_put_tag() take a pointer to the last byte they
546 * should write to, and a length of bytes to the left of
547 * that that they are allowed to write into.
549 * [ ... | T_right | LL | VVVVV | ... ] // original buffer
553 * +-- psave_<fieldName>
555 "e = der_put_tag(psave_%s, %lu, %s, %s, %d, &l2_%s);\n"
556 "if (e) { free(pfree_%s); return e; }\n"
557 /* Restore `len' and adjust it (see `p' below) */
558 "len = lensave_%s - (l + %zu - asn1_tag_length_%s);\n"
560 * Adjust `ret' to account for the difference in size
561 * between the length of the right and wrong tags.
563 "ret += %zu - asn1_tag_length_%s;\n"
564 /* Free the buffer and restore `p' */
567 * Make `p' point into the original buffer again, to one
568 * byte before the bytes we wrote:
570 * [ ... | T_right | LL | VVVVV | ... ] // original buffer
576 "p = psave_%s - (1 + %lu - asn1_tag_length_%s); }\n",
577 tmpstr
, tmpstr
, tmpstr
, t
->subtype
->symbol
->name
,
578 tmpstr
, t
->subtype
->symbol
->name
, t
->subtype
->symbol
->name
,
579 tmpstr
, length_tag(t
->tag
.tagvalue
),
580 classname(t
->tag
.tagclass
),
585 tmpstr
, tmpstr
, length_tag(t
->tag
.tagvalue
), t
->subtype
->symbol
->name
,
586 length_tag(t
->tag
.tagvalue
), t
->subtype
->symbol
->name
,
587 tmpstr
, tmpstr
, length_tag(t
->tag
.tagvalue
), t
->subtype
->symbol
->name
);
590 "e = der_put_length_and_tag (p, len, ret, %s, %s, %s, &l);\n"
591 "if (e) return e;\np -= l; len -= l; ret += l;\n\n",
592 classname(t
->tag
.tagclass
),
594 valuename(t
->tag
.tagclass
, t
->tag
.tagvalue
));
600 Member
*m
, *have_ellipsis
= NULL
;
603 if (t
->members
== NULL
)
606 fprintf(codefile
, "\n");
608 if (asprintf (&s
, "(%s)", name
) < 0 || s
== NULL
)
610 fprintf(codefile
, "switch(%s->element) {\n", s
);
612 HEIM_TAILQ_FOREACH_REVERSE(m
, t
->members
, memhead
, members
) {
620 fprintf (codefile
, "case %s: {", m
->label
);
621 if (asprintf(&s2
, "%s(%s)->u.%s", m
->optional
? "" : "&",
622 s
, m
->gen_name
) < 0 || s2
== NULL
)
625 fprintf (codefile
, "if(%s) {\n", s2
);
626 fprintf (codefile
, "size_t %s_oldret = ret;\n", tmpstr
);
627 fprintf (codefile
, "ret = 0;\n");
628 constructed
= encode_type (s2
, m
->type
, m
->gen_name
);
629 fprintf (codefile
, "ret += %s_oldret;\n", tmpstr
);
631 fprintf (codefile
, "}\n");
632 fprintf(codefile
, "break;\n");
633 fprintf(codefile
, "}\n");
640 "if (len < (%s)->u.%s.length)\n"
641 "return ASN1_OVERFLOW;\n"
642 "p -= (%s)->u.%s.length;\n"
643 "ret += (%s)->u.%s.length;\n"
644 "memcpy(p + 1, (%s)->u.%s.data, (%s)->u.%s.length);\n"
647 have_ellipsis
->label
,
648 name
, have_ellipsis
->gen_name
,
649 name
, have_ellipsis
->gen_name
,
650 name
, have_ellipsis
->gen_name
,
651 name
, have_ellipsis
->gen_name
,
652 name
, have_ellipsis
->gen_name
);
654 fprintf(codefile
, "};\n");
658 encode_primitive ("oid", name
);
662 encode_primitive ("utctime", name
);
666 encode_primitive ("utf8string", name
);
669 case TPrintableString
:
670 encode_primitive ("printable_string", name
);
674 encode_primitive ("ia5_string", name
);
678 encode_primitive ("bmp_string", name
);
681 case TUniversalString
:
682 encode_primitive ("universal_string", name
);
686 encode_primitive ("visible_string", name
);
690 fprintf (codefile
, "/* NULL */\n");
700 generate_type_encode (const Symbol
*s
)
702 fprintf (codefile
, "int ASN1CALL\n"
703 "encode_%s(unsigned char *p HEIMDAL_UNUSED_ATTRIBUTE, size_t len HEIMDAL_UNUSED_ATTRIBUTE,"
704 " const %s *data, size_t *size)\n"
706 s
->gen_name
, s
->gen_name
);
708 switch (s
->type
->type
) {
712 case TGeneralizedTime
:
717 case TPrintableString
:
720 case TUniversalString
:
734 "size_t ret HEIMDAL_UNUSED_ATTRIBUTE = 0;\n"
735 "size_t l HEIMDAL_UNUSED_ATTRIBUTE;\n"
736 "int i HEIMDAL_UNUSED_ATTRIBUTE, e HEIMDAL_UNUSED_ATTRIBUTE;\n\n");
738 encode_type("data", s
->type
, "Top");
740 fprintf (codefile
, "*size = ret;\n"
746 fprintf (codefile
, "}\n\n");