1 /* $NetBSD: gen_encode.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $ */
4 * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 encode_primitive (const char *typename
, const char *name
)
44 "e = der_put_%s(p, len, %s, &l);\n"
45 "if (e) return e;\np -= l; len -= l; ret += l;\n\n",
51 classname(Der_class
class)
53 const char *cn
[] = { "ASN1_C_UNIV", "ASN1_C_APPL",
54 "ASN1_C_CONTEXT", "ASN1_C_PRIV" };
55 if(class < ASN1_C_UNIV
|| class > ASN1_C_PRIVATE
)
62 valuename(Der_class
class, int value
)
69 #define X(Y) { Y, #Y }
77 X(UT_GeneralizedTime
),
84 X(UT_ObjectDescriptor
),
86 X(UT_PrintableString
),
94 X(UT_UniversalString
),
100 if(class == ASN1_C_UNIV
) {
101 for(p
= values
; p
->value
!= -1; p
++)
102 if(p
->value
== value
)
105 snprintf(s
, sizeof(s
), "%d", value
);
110 encode_type (const char *name
, const Type
*t
, const char *tmpstr
)
117 encode_type (name
, t
->symbol
->type
);
120 "e = encode_%s(p, len, %s, &l);\n"
121 "if (e) return e;\np -= l; len -= l; ret += l;\n\n",
122 t
->symbol
->gen_name
, name
);
128 "int enumint = (int)*%s;\n",
130 encode_primitive ("integer", "&enumint");
131 fprintf(codefile
, "}\n;");
132 } else if (t
->range
== NULL
) {
133 encode_primitive ("heim_integer", name
);
134 } else if (t
->range
->min
== INT_MIN
&& t
->range
->max
== INT_MAX
) {
135 encode_primitive ("integer", name
);
136 } else if (t
->range
->min
== 0 && t
->range
->max
== UINT_MAX
) {
137 encode_primitive ("unsigned", name
);
138 } else if (t
->range
->min
== 0 && t
->range
->max
== INT_MAX
) {
139 encode_primitive ("unsigned", name
);
141 errx(1, "%s: unsupported range %d -> %d",
142 name
, t
->range
->min
, t
->range
->max
);
146 encode_primitive ("boolean", name
);
150 encode_primitive ("octet_string", name
);
157 if (ASN1_TAILQ_EMPTY(t
->members
)) {
158 encode_primitive("bit_string", name
);
163 fprintf (codefile
, "{\n"
164 "unsigned char c = 0;\n");
165 if (!rfc1510_bitstring
)
168 "int bit_set = 0;\n");
170 pos
= t
->members
->prev
->val
;
171 /* fix for buggy MIT (and OSF?) code */
176 * It seems that if we do not always set pos to 31 here, the MIT
177 * code will do the wrong thing.
179 * I hate ASN.1 (and DER), but I hate it even more when everybody
180 * has to screw it up differently.
182 pos
= ASN1_TAILQ_LAST(t
->members
, memhead
)->val
;
183 if (rfc1510_bitstring
) {
188 ASN1_TAILQ_FOREACH_REVERSE(m
, t
->members
, memhead
, members
) {
189 while (m
->val
/ 8 < pos
/ 8) {
190 if (!rfc1510_bitstring
)
192 "if (c != 0 || bit_set) {\n");
194 "if (len < 1) return ASN1_OVERFLOW;\n"
195 "*p-- = c; len--; ret++;\n");
196 if (!rfc1510_bitstring
)
201 "if (c & 1) break;\n"
215 name
, m
->gen_name
, 7 - m
->val
% 8);
220 if (!rfc1510_bitstring
)
222 "if (c != 0 || bit_set) {\n");
224 "if (len < 1) return ASN1_OVERFLOW;\n"
225 "*p-- = c; len--; ret++;\n");
226 if (!rfc1510_bitstring
)
232 "if (c & 1) break;\n"
241 "if (len < 1) return ASN1_OVERFLOW;\n"
246 rfc1510_bitstring
? "0" : "rest");
251 encode_primitive ("enumerated", name
);
260 if (t
->members
== NULL
)
263 ASN1_TAILQ_FOREACH_REVERSE(m
, t
->members
, memhead
, members
) {
269 if (asprintf (&s
, "%s(%s)->%s", m
->optional
? "" : "&", name
, m
->gen_name
) < 0 || s
== NULL
)
271 fprintf(codefile
, "/* %s */\n", m
->name
);
277 gen_compare_defval(s
+ 1, m
->defval
);
278 fprintf (codefile
, "{\n");
279 fprintf (codefile
, "size_t %s_oldret HEIMDAL_UNUSED_ATTRIBUTE = ret;\n", tmpstr
);
280 fprintf (codefile
, "ret = 0;\n");
281 encode_type (s
, m
->type
, m
->gen_name
);
282 fprintf (codefile
, "ret += %s_oldret;\n", tmpstr
);
283 fprintf (codefile
, "}\n");
292 "struct heim_octet_string *val;\n"
293 "size_t elen = 0, totallen = 0;\n"
297 "if ((%s)->len > UINT_MAX/sizeof(val[0]))\n"
302 "val = malloc(sizeof(val[0]) * (%s)->len);\n"
303 "if (val == NULL && (%s)->len != 0) return ENOMEM;\n",
307 "for(i = 0; i < (int)(%s)->len; i++) {\n",
311 "ASN1_MALLOC_ENCODE(%s, val[i].data, "
312 "val[i].length, &(%s)->val[i], &elen, eret);\n",
313 t
->subtype
->symbol
->gen_name
,
320 "free(val[i].data);\n"
326 "totallen += elen;\n"
330 "if (totallen > len) {\n"
331 "for (i = 0; i < (int)(%s)->len; i++) {\n"
332 "free(val[i].data);\n"
335 "return ASN1_OVERFLOW;\n"
340 "qsort(val, (%s)->len, sizeof(val[0]), _heim_der_set_sort);\n",
344 "for(i = (int)(%s)->len - 1; i >= 0; --i) {\n"
345 "p -= val[i].length;\n"
346 "ret += val[i].length;\n"
347 "memcpy(p + 1, val[i].data, val[i].length);\n"
348 "free(val[i].data);\n"
360 "for(i = (int)(%s)->len - 1; i >= 0; --i) {\n"
361 "size_t %s_for_oldret = ret;\n"
364 if (asprintf (&n
, "&(%s)->val[i]", name
) < 0 || n
== NULL
)
366 if (asprintf (&sname
, "%s_S_Of", tmpstr
) < 0 || sname
== NULL
)
368 encode_type (n
, t
->subtype
, sname
);
370 "ret += %s_for_oldret;\n"
377 case TGeneralizedTime
:
378 encode_primitive ("generalized_time", name
);
382 encode_primitive ("general_string", name
);
386 encode_primitive ("general_string", name
);
392 if (asprintf (&tname
, "%s_tag", tmpstr
) < 0 || tname
== NULL
)
394 c
= encode_type (name
, t
->subtype
, tname
);
396 "e = der_put_length_and_tag (p, len, ret, %s, %s, %s, &l);\n"
397 "if (e) return e;\np -= l; len -= l; ret += l;\n\n",
398 classname(t
->tag
.tagclass
),
400 valuename(t
->tag
.tagclass
, t
->tag
.tagvalue
));
405 Member
*m
, *have_ellipsis
= NULL
;
408 if (t
->members
== NULL
)
411 fprintf(codefile
, "\n");
413 if (asprintf (&s
, "(%s)", name
) < 0 || s
== NULL
)
415 fprintf(codefile
, "switch(%s->element) {\n", s
);
417 ASN1_TAILQ_FOREACH_REVERSE(m
, t
->members
, memhead
, members
) {
425 fprintf (codefile
, "case %s: {", m
->label
);
426 if (asprintf(&s2
, "%s(%s)->u.%s", m
->optional
? "" : "&",
427 s
, m
->gen_name
) < 0 || s2
== NULL
)
430 fprintf (codefile
, "if(%s) {\n", s2
);
431 fprintf (codefile
, "size_t %s_oldret = ret;\n", tmpstr
);
432 fprintf (codefile
, "ret = 0;\n");
433 constructed
= encode_type (s2
, m
->type
, m
->gen_name
);
434 fprintf (codefile
, "ret += %s_oldret;\n", tmpstr
);
436 fprintf (codefile
, "}\n");
437 fprintf(codefile
, "break;\n");
438 fprintf(codefile
, "}\n");
445 "if (len < (%s)->u.%s.length)\n"
446 "return ASN1_OVERFLOW;\n"
447 "p -= (%s)->u.%s.length;\n"
448 "ret += (%s)->u.%s.length;\n"
449 "memcpy(p + 1, (%s)->u.%s.data, (%s)->u.%s.length);\n"
452 have_ellipsis
->label
,
453 name
, have_ellipsis
->gen_name
,
454 name
, have_ellipsis
->gen_name
,
455 name
, have_ellipsis
->gen_name
,
456 name
, have_ellipsis
->gen_name
,
457 name
, have_ellipsis
->gen_name
);
459 fprintf(codefile
, "};\n");
463 encode_primitive ("oid", name
);
467 encode_primitive ("utctime", name
);
471 encode_primitive ("utf8string", name
);
474 case TPrintableString
:
475 encode_primitive ("printable_string", name
);
479 encode_primitive ("ia5_string", name
);
483 encode_primitive ("bmp_string", name
);
486 case TUniversalString
:
487 encode_primitive ("universal_string", name
);
491 encode_primitive ("visible_string", name
);
495 fprintf (codefile
, "/* NULL */\n");
505 generate_type_encode (const Symbol
*s
)
507 fprintf (codefile
, "int ASN1CALL\n"
508 "encode_%s(unsigned char *p HEIMDAL_UNUSED_ATTRIBUTE, size_t len HEIMDAL_UNUSED_ATTRIBUTE,"
509 " const %s *data, size_t *size)\n"
511 s
->gen_name
, s
->gen_name
);
513 switch (s
->type
->type
) {
517 case TGeneralizedTime
:
522 case TPrintableString
:
525 case TUniversalString
:
539 "size_t ret HEIMDAL_UNUSED_ATTRIBUTE = 0;\n"
540 "size_t l HEIMDAL_UNUSED_ATTRIBUTE;\n"
541 "int i HEIMDAL_UNUSED_ATTRIBUTE, e HEIMDAL_UNUSED_ATTRIBUTE;\n\n");
543 encode_type("data", s
->type
, "Top");
545 fprintf (codefile
, "*size = ret;\n"
551 fprintf (codefile
, "}\n\n");