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
39 encode_primitive (const char *typename, const char *name)
42 "e = der_put_%s(p, len, %s, &l);\n"
43 "if (e) return e;\np -= l; len -= l; ret += l;\n\n",
49 classname(Der_class class)
51 const char *cn[] = { "ASN1_C_UNIV", "ASN1_C_APPL",
52 "ASN1_C_CONTEXT", "ASN1_C_PRIV" };
53 if(class < ASN1_C_UNIV || class > ASN1_C_PRIVATE)
60 valuename(Der_class class, int value)
67 #define X(Y) { Y, #Y }
75 X(UT_GeneralizedTime),
82 X(UT_ObjectDescriptor),
84 X(UT_PrintableString),
92 X(UT_UniversalString),
98 if(class == ASN1_C_UNIV) {
99 for(p = values; p->value != -1; p++)
100 if(p->value == value)
103 snprintf(s, sizeof(s), "%d", value);
108 encode_type (const char *name, const Type *t, const char *tmpstr)
115 encode_type (name, t->symbol->type);
118 "e = encode_%s(p, len, %s, &l);\n"
119 "if (e) return e;\np -= l; len -= l; ret += l;\n\n",
120 t->symbol->gen_name, name);
126 "int enumint = (int)*%s;\n",
128 encode_primitive ("integer", "&enumint");
129 fprintf(codefile, "}\n;");
130 } else if (t->range == NULL) {
131 encode_primitive ("heim_integer", name);
132 } else if (t->range->min == INT_MIN && t->range->max == INT_MAX) {
133 encode_primitive ("integer", name);
134 } else if (t->range->min == 0 && t->range->max == UINT_MAX) {
135 encode_primitive ("unsigned", name);
136 } else if (t->range->min == 0 && t->range->max == INT_MAX) {
137 encode_primitive ("unsigned", name);
139 errx(1, "%s: unsupported range %d -> %d",
140 name, t->range->min, t->range->max);
144 encode_primitive ("boolean", name);
148 encode_primitive ("octet_string", name);
155 if (ASN1_TAILQ_EMPTY(t->members)) {
156 encode_primitive("bit_string", name);
161 fprintf (codefile, "{\n"
162 "unsigned char c = 0;\n");
163 if (!rfc1510_bitstring)
166 "int bit_set = 0;\n");
168 pos = t->members->prev->val;
169 /* fix for buggy MIT (and OSF?) code */
174 * It seems that if we do not always set pos to 31 here, the MIT
175 * code will do the wrong thing.
177 * I hate ASN.1 (and DER), but I hate it even more when everybody
178 * has to screw it up differently.
180 pos = ASN1_TAILQ_LAST(t->members, memhead)->val;
181 if (rfc1510_bitstring) {
186 ASN1_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) {
187 while (m->val / 8 < pos / 8) {
188 if (!rfc1510_bitstring)
190 "if (c != 0 || bit_set) {\n");
192 "if (len < 1) return ASN1_OVERFLOW;\n"
193 "*p-- = c; len--; ret++;\n");
194 if (!rfc1510_bitstring)
199 "if (c & 1) break;\n"
213 name, m->gen_name, 7 - m->val % 8);
218 if (!rfc1510_bitstring)
220 "if (c != 0 || bit_set) {\n");
222 "if (len < 1) return ASN1_OVERFLOW;\n"
223 "*p-- = c; len--; ret++;\n");
224 if (!rfc1510_bitstring)
230 "if (c & 1) break;\n"
239 "if (len < 1) return ASN1_OVERFLOW;\n"
244 rfc1510_bitstring ? "0" : "rest");
249 encode_primitive ("enumerated", name);
258 if (t->members == NULL)
261 ASN1_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) {
267 if (asprintf (&s, "%s(%s)->%s", m->optional ? "" : "&", name, m->gen_name) < 0 || s == NULL)
269 fprintf(codefile, "/* %s */\n", m->name);
275 gen_compare_defval(s + 1, m->defval);
276 fprintf (codefile, "{\n");
277 fprintf (codefile, "size_t %s_oldret HEIMDAL_UNUSED_ATTRIBUTE = ret;\n", tmpstr);
278 fprintf (codefile, "ret = 0;\n");
279 encode_type (s, m->type, m->gen_name);
280 fprintf (codefile, "ret += %s_oldret;\n", tmpstr);
281 fprintf (codefile, "}\n");
290 "struct heim_octet_string *val;\n"
291 "size_t elen = 0, totallen = 0;\n"
295 "if ((%s)->len > UINT_MAX/sizeof(val[0]))\n"
300 "val = malloc(sizeof(val[0]) * (%s)->len);\n"
301 "if (val == NULL && (%s)->len != 0) return ENOMEM;\n",
305 "for(i = 0; i < (int)(%s)->len; i++) {\n",
309 "ASN1_MALLOC_ENCODE(%s, val[i].data, "
310 "val[i].length, &(%s)->val[i], &elen, eret);\n",
311 t->subtype->symbol->gen_name,
318 "free(val[i].data);\n"
324 "totallen += elen;\n"
328 "if (totallen > len) {\n"
329 "for (i = 0; i < (int)(%s)->len; i++) {\n"
330 "free(val[i].data);\n"
333 "return ASN1_OVERFLOW;\n"
338 "qsort(val, (%s)->len, sizeof(val[0]), _heim_der_set_sort);\n",
342 "for(i = (int)(%s)->len - 1; i >= 0; --i) {\n"
343 "p -= val[i].length;\n"
344 "ret += val[i].length;\n"
345 "memcpy(p + 1, val[i].data, val[i].length);\n"
346 "free(val[i].data);\n"
358 "for(i = (int)(%s)->len - 1; i >= 0; --i) {\n"
359 "size_t %s_for_oldret = ret;\n"
362 if (asprintf (&n, "&(%s)->val[i]", name) < 0 || n == NULL)
364 if (asprintf (&sname, "%s_S_Of", tmpstr) < 0 || sname == NULL)
366 encode_type (n, t->subtype, sname);
368 "ret += %s_for_oldret;\n"
375 case TGeneralizedTime:
376 encode_primitive ("generalized_time", name);
380 encode_primitive ("general_string", name);
384 encode_primitive ("general_string", name);
390 if (asprintf (&tname, "%s_tag", tmpstr) < 0 || tname == NULL)
392 c = encode_type (name, t->subtype, tname);
394 "e = der_put_length_and_tag (p, len, ret, %s, %s, %s, &l);\n"
395 "if (e) return e;\np -= l; len -= l; ret += l;\n\n",
396 classname(t->tag.tagclass),
398 valuename(t->tag.tagclass, t->tag.tagvalue));
403 Member *m, *have_ellipsis = NULL;
406 if (t->members == NULL)
409 fprintf(codefile, "\n");
411 if (asprintf (&s, "(%s)", name) < 0 || s == NULL)
413 fprintf(codefile, "switch(%s->element) {\n", s);
415 ASN1_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) {
423 fprintf (codefile, "case %s: {", m->label);
424 if (asprintf(&s2, "%s(%s)->u.%s", m->optional ? "" : "&",
425 s, m->gen_name) < 0 || s2 == NULL)
428 fprintf (codefile, "if(%s) {\n", s2);
429 fprintf (codefile, "size_t %s_oldret = ret;\n", tmpstr);
430 fprintf (codefile, "ret = 0;\n");
431 constructed = encode_type (s2, m->type, m->gen_name);
432 fprintf (codefile, "ret += %s_oldret;\n", tmpstr);
434 fprintf (codefile, "}\n");
435 fprintf(codefile, "break;\n");
436 fprintf(codefile, "}\n");
443 "if (len < (%s)->u.%s.length)\n"
444 "return ASN1_OVERFLOW;\n"
445 "p -= (%s)->u.%s.length;\n"
446 "ret += (%s)->u.%s.length;\n"
447 "memcpy(p + 1, (%s)->u.%s.data, (%s)->u.%s.length);\n"
450 have_ellipsis->label,
451 name, have_ellipsis->gen_name,
452 name, have_ellipsis->gen_name,
453 name, have_ellipsis->gen_name,
454 name, have_ellipsis->gen_name,
455 name, have_ellipsis->gen_name);
457 fprintf(codefile, "};\n");
461 encode_primitive ("oid", name);
465 encode_primitive ("utctime", name);
469 encode_primitive ("utf8string", name);
472 case TPrintableString:
473 encode_primitive ("printable_string", name);
477 encode_primitive ("ia5_string", name);
481 encode_primitive ("bmp_string", name);
484 case TUniversalString:
485 encode_primitive ("universal_string", name);
489 encode_primitive ("visible_string", name);
493 fprintf (codefile, "/* NULL */\n");
503 generate_type_encode (const Symbol *s)
505 fprintf (codefile, "int ASN1CALL\n"
506 "encode_%s(unsigned char *p HEIMDAL_UNUSED_ATTRIBUTE, size_t len HEIMDAL_UNUSED_ATTRIBUTE,"
507 " const %s *data, size_t *size)\n"
509 s->gen_name, s->gen_name);
511 switch (s->type->type) {
515 case TGeneralizedTime:
520 case TPrintableString:
523 case TUniversalString:
537 "size_t ret HEIMDAL_UNUSED_ATTRIBUTE = 0;\n"
538 "size_t l HEIMDAL_UNUSED_ATTRIBUTE;\n"
539 "int i HEIMDAL_UNUSED_ATTRIBUTE, e HEIMDAL_UNUSED_ATTRIBUTE;\n\n");
541 encode_type("data", s->type, "Top");
543 fprintf (codefile, "*size = ret;\n"
549 fprintf (codefile, "}\n\n");