kadmind: check for KADM5_PRIV_GET when op GET
[heimdal.git] / lib / asn1 / gen_encode.c
blob60433a00e70565da9a29611d2bf8d4dd50ee74c7
1 /*
2 * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "gen_locl.h"
36 static void
37 encode_primitive (const char *typename, const char *name)
39 fprintf (codefile,
40 "e = der_put_%s(p, len, %s, &l);\n"
41 "if (e) return e;\np -= l; len -= l; ret += l;\n\n",
42 typename,
43 name);
46 const char *
47 classname(Der_class class)
49 const char *cn[] = { "ASN1_C_UNIV", "ASN1_C_APPL",
50 "ASN1_C_CONTEXT", "ASN1_C_PRIV" };
51 if ((int)class >= sizeof(cn) / sizeof(cn[0]))
52 return "???";
53 return cn[class];
57 const char *
58 valuename(Der_class class, int value)
60 static char s[32];
61 struct {
62 int value;
63 const char *s;
64 } *p, values[] = {
65 #define X(Y) { Y, #Y }
66 X(UT_BMPString),
67 X(UT_BitString),
68 X(UT_Boolean),
69 X(UT_EmbeddedPDV),
70 X(UT_Enumerated),
71 X(UT_External),
72 X(UT_GeneralString),
73 X(UT_GeneralizedTime),
74 X(UT_GraphicString),
75 X(UT_IA5String),
76 X(UT_Integer),
77 X(UT_Null),
78 X(UT_NumericString),
79 X(UT_OID),
80 X(UT_ObjectDescriptor),
81 X(UT_OctetString),
82 X(UT_PrintableString),
83 X(UT_Real),
84 X(UT_RelativeOID),
85 X(UT_Sequence),
86 X(UT_Set),
87 X(UT_TeletexString),
88 X(UT_UTCTime),
89 X(UT_UTF8String),
90 X(UT_UniversalString),
91 X(UT_VideotexString),
92 X(UT_VisibleString),
93 #undef X
94 { -1, NULL }
96 if(class == ASN1_C_UNIV) {
97 for(p = values; p->value != -1; p++)
98 if(p->value == value)
99 return p->s;
101 snprintf(s, sizeof(s), "%d", value);
102 return s;
105 static int
106 encode_type (const char *name, const Type *t, const char *tmpstr)
108 int constructed = 1;
110 switch (t->type) {
111 case TType:
112 #if 0
113 encode_type (name, t->symbol->type);
114 #endif
115 fprintf (codefile,
116 "e = encode_%s(p, len, %s, &l);\n"
117 "if (e) return e;\np -= l; len -= l; ret += l;\n\n",
118 t->symbol->gen_name, name);
119 break;
120 case TInteger:
121 if(t->members) {
122 fprintf(codefile,
123 "{\n"
124 "int enumint = (int)*%s;\n",
125 name);
126 encode_primitive ("integer", "&enumint");
127 fprintf(codefile, "}\n;");
128 } else if (t->range == NULL) {
129 encode_primitive ("heim_integer", name);
130 } else if (t->range->min < INT_MIN && t->range->max <= INT64_MAX) {
131 encode_primitive ("integer64", name);
132 } else if (t->range->min >= 0 && t->range->max > UINT_MAX) {
133 encode_primitive ("unsigned64", 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
139 errx(1, "%s: unsupported range %lld -> %lld",
140 name, (long long)t->range->min, (long long)t->range->max);
142 constructed = 0;
143 break;
144 case TBoolean:
145 encode_primitive ("boolean", name);
146 constructed = 0;
147 break;
148 case TOctetString:
149 encode_primitive ("octet_string", name);
150 constructed = 0;
151 break;
152 case TBitString: {
153 Member *m;
154 int pos;
156 if (ASN1_TAILQ_EMPTY(t->members)) {
157 encode_primitive("bit_string", name);
158 constructed = 0;
159 break;
162 fprintf (codefile, "{\n"
163 "unsigned char c = 0;\n");
164 if (!rfc1510_bitstring)
165 fprintf (codefile,
166 "int rest = 0;\n"
167 "int bit_set = 0;\n");
168 #if 0
169 pos = t->members->prev->val;
170 /* fix for buggy MIT (and OSF?) code */
171 if (pos > 31)
172 abort ();
173 #endif
175 * It seems that if we do not always set pos to 31 here, the MIT
176 * code will do the wrong thing.
178 * I hate ASN.1 (and DER), but I hate it even more when everybody
179 * has to screw it up differently.
181 pos = ASN1_TAILQ_LAST(t->members, memhead)->val;
182 if (rfc1510_bitstring) {
183 if (pos < 31)
184 pos = 31;
187 ASN1_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) {
188 while (m->val / 8 < pos / 8) {
189 if (!rfc1510_bitstring)
190 fprintf (codefile,
191 "if (c != 0 || bit_set) {\n");
192 fprintf (codefile,
193 "if (len < 1) return ASN1_OVERFLOW;\n"
194 "*p-- = c; len--; ret++;\n");
195 if (!rfc1510_bitstring)
196 fprintf (codefile,
197 "if (!bit_set) {\n"
198 "rest = 0;\n"
199 "while(c) { \n"
200 "if (c & 1) break;\n"
201 "c = c >> 1;\n"
202 "rest++;\n"
203 "}\n"
204 "bit_set = 1;\n"
205 "}\n"
206 "}\n");
207 fprintf (codefile,
208 "c = 0;\n");
209 pos -= 8;
211 fprintf (codefile,
212 "if((%s)->%s) {\n"
213 "c |= 1<<%d;\n",
214 name, m->gen_name, 7 - m->val % 8);
215 fprintf (codefile,
216 "}\n");
219 if (!rfc1510_bitstring)
220 fprintf (codefile,
221 "if (c != 0 || bit_set) {\n");
222 fprintf (codefile,
223 "if (len < 1) return ASN1_OVERFLOW;\n"
224 "*p-- = c; len--; ret++;\n");
225 if (!rfc1510_bitstring)
226 fprintf (codefile,
227 "if (!bit_set) {\n"
228 "rest = 0;\n"
229 "if(c) { \n"
230 "while(c) { \n"
231 "if (c & 1) break;\n"
232 "c = c >> 1;\n"
233 "rest++;\n"
234 "}\n"
235 "}\n"
236 "}\n"
237 "}\n");
239 fprintf (codefile,
240 "if (len < 1) return ASN1_OVERFLOW;\n"
241 "*p-- = %s;\n"
242 "len -= 1;\n"
243 "ret += 1;\n"
244 "}\n\n",
245 rfc1510_bitstring ? "0" : "rest");
246 constructed = 0;
247 break;
249 case TEnumerated : {
250 encode_primitive ("enumerated", name);
251 constructed = 0;
252 break;
255 case TSet:
256 case TSequence: {
257 Member *m;
259 if (t->members == NULL)
260 break;
262 ASN1_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) {
263 char *s = NULL;
265 if (m->ellipsis)
266 continue;
268 if (asprintf (&s, "%s(%s)->%s", m->optional ? "" : "&", name, m->gen_name) < 0 || s == NULL)
269 errx(1, "malloc");
270 fprintf(codefile, "/* %s */\n", m->name);
271 if (m->optional)
272 fprintf (codefile,
273 "if(%s) ",
275 else if(m->defval)
276 gen_compare_defval(s + 1, m->defval);
277 fprintf (codefile, "{\n");
278 fprintf (codefile, "size_t %s_oldret HEIMDAL_UNUSED_ATTRIBUTE = ret;\n", tmpstr);
279 fprintf (codefile, "ret = 0;\n");
280 encode_type (s, m->type, m->gen_name);
281 fprintf (codefile, "ret += %s_oldret;\n", tmpstr);
282 fprintf (codefile, "}\n");
283 free (s);
285 break;
287 case TSetOf: {
289 fprintf(codefile,
290 "{\n"
291 "heim_octet_string *val;\n"
292 "size_t elen = 0, totallen = 0;\n"
293 "int eret = 0;\n");
295 fprintf(codefile,
296 "if ((%s)->len > UINT_MAX/sizeof(val[0]))\n"
297 "return ERANGE;\n",
298 name);
300 fprintf(codefile,
301 "val = malloc(sizeof(val[0]) * (%s)->len);\n"
302 "if (val == NULL && (%s)->len != 0) return ENOMEM;\n",
303 name, name);
305 fprintf(codefile,
306 "for(i = 0; i < (int)(%s)->len; i++) {\n",
307 name);
309 fprintf(codefile,
310 "ASN1_MALLOC_ENCODE(%s, val[i].data, "
311 "val[i].length, &(%s)->val[i], &elen, eret);\n",
312 t->subtype->symbol->gen_name,
313 name);
315 fprintf(codefile,
316 "if(eret) {\n"
317 "i--;\n"
318 "while (i >= 0) {\n"
319 "free(val[i].data);\n"
320 "i--;\n"
321 "}\n"
322 "free(val);\n"
323 "return eret;\n"
324 "}\n"
325 "totallen += elen;\n"
326 "}\n");
328 fprintf(codefile,
329 "if (totallen > len) {\n"
330 "for (i = 0; i < (int)(%s)->len; i++) {\n"
331 "free(val[i].data);\n"
332 "}\n"
333 "free(val);\n"
334 "return ASN1_OVERFLOW;\n"
335 "}\n",
336 name);
338 fprintf(codefile,
339 "qsort(val, (%s)->len, sizeof(val[0]), _heim_der_set_sort);\n",
340 name);
342 fprintf (codefile,
343 "for(i = (int)(%s)->len - 1; i >= 0; --i) {\n"
344 "p -= val[i].length;\n"
345 "ret += val[i].length;\n"
346 "memcpy(p + 1, val[i].data, val[i].length);\n"
347 "free(val[i].data);\n"
348 "}\n"
349 "free(val);\n"
350 "}\n",
351 name);
352 break;
354 case TSequenceOf: {
355 char *sname = NULL;
356 char *n = NULL;
358 fprintf (codefile,
359 "for(i = (int)(%s)->len - 1; i >= 0; --i) {\n"
360 "size_t %s_for_oldret = ret;\n"
361 "ret = 0;\n",
362 name, tmpstr);
363 if (asprintf (&n, "&(%s)->val[i]", name) < 0 || n == NULL)
364 errx(1, "malloc");
365 if (asprintf (&sname, "%s_S_Of", tmpstr) < 0 || sname == NULL)
366 errx(1, "malloc");
367 encode_type (n, t->subtype, sname);
368 fprintf (codefile,
369 "ret += %s_for_oldret;\n"
370 "}\n",
371 tmpstr);
372 free (n);
373 free (sname);
374 break;
376 case TGeneralizedTime:
377 encode_primitive ("generalized_time", name);
378 constructed = 0;
379 break;
380 case TGeneralString:
381 encode_primitive ("general_string", name);
382 constructed = 0;
383 break;
384 case TTeletexString:
385 encode_primitive ("general_string", name);
386 constructed = 0;
387 break;
388 case TTag: {
389 char *tname = NULL;
390 int c;
391 if (asprintf (&tname, "%s_tag", tmpstr) < 0 || tname == NULL)
392 errx(1, "malloc");
393 c = encode_type (name, t->subtype, tname);
394 fprintf (codefile,
395 "e = der_put_length_and_tag (p, len, ret, %s, %s, %s, &l);\n"
396 "if (e) return e;\np -= l; len -= l; ret += l;\n\n",
397 classname(t->tag.tagclass),
398 c ? "CONS" : "PRIM",
399 valuename(t->tag.tagclass, t->tag.tagvalue));
400 free (tname);
401 break;
403 case TChoice:{
404 Member *m, *have_ellipsis = NULL;
405 char *s = NULL;
407 if (t->members == NULL)
408 break;
410 fprintf(codefile, "\n");
412 if (asprintf (&s, "(%s)", name) < 0 || s == NULL)
413 errx(1, "malloc");
414 fprintf(codefile, "switch(%s->element) {\n", s);
416 ASN1_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) {
417 char *s2 = NULL;
419 if (m->ellipsis) {
420 have_ellipsis = m;
421 continue;
424 fprintf (codefile, "case %s: {", m->label);
425 if (asprintf(&s2, "%s(%s)->u.%s", m->optional ? "" : "&",
426 s, m->gen_name) < 0 || s2 == NULL)
427 errx(1, "malloc");
428 if (m->optional)
429 fprintf (codefile, "if(%s) {\n", s2);
430 fprintf (codefile, "size_t %s_oldret = ret;\n", tmpstr);
431 fprintf (codefile, "ret = 0;\n");
432 constructed = encode_type (s2, m->type, m->gen_name);
433 fprintf (codefile, "ret += %s_oldret;\n", tmpstr);
434 if(m->optional)
435 fprintf (codefile, "}\n");
436 fprintf(codefile, "break;\n");
437 fprintf(codefile, "}\n");
438 free (s2);
440 free (s);
441 if (have_ellipsis) {
442 fprintf(codefile,
443 "case %s: {\n"
444 "if (len < (%s)->u.%s.length)\n"
445 "return ASN1_OVERFLOW;\n"
446 "p -= (%s)->u.%s.length;\n"
447 "ret += (%s)->u.%s.length;\n"
448 "memcpy(p + 1, (%s)->u.%s.data, (%s)->u.%s.length);\n"
449 "break;\n"
450 "}\n",
451 have_ellipsis->label,
452 name, have_ellipsis->gen_name,
453 name, have_ellipsis->gen_name,
454 name, have_ellipsis->gen_name,
455 name, have_ellipsis->gen_name,
456 name, have_ellipsis->gen_name);
458 fprintf(codefile, "};\n");
459 break;
461 case TOID:
462 encode_primitive ("oid", name);
463 constructed = 0;
464 break;
465 case TUTCTime:
466 encode_primitive ("utctime", name);
467 constructed = 0;
468 break;
469 case TUTF8String:
470 encode_primitive ("utf8string", name);
471 constructed = 0;
472 break;
473 case TPrintableString:
474 encode_primitive ("printable_string", name);
475 constructed = 0;
476 break;
477 case TIA5String:
478 encode_primitive ("ia5_string", name);
479 constructed = 0;
480 break;
481 case TBMPString:
482 encode_primitive ("bmp_string", name);
483 constructed = 0;
484 break;
485 case TUniversalString:
486 encode_primitive ("universal_string", name);
487 constructed = 0;
488 break;
489 case TVisibleString:
490 encode_primitive ("visible_string", name);
491 constructed = 0;
492 break;
493 case TNull:
494 fprintf (codefile, "/* NULL */\n");
495 constructed = 0;
496 break;
497 default:
498 abort ();
500 return constructed;
503 void
504 generate_type_encode (const Symbol *s)
506 fprintf (codefile, "int ASN1CALL\n"
507 "encode_%s(unsigned char *p HEIMDAL_UNUSED_ATTRIBUTE, size_t len HEIMDAL_UNUSED_ATTRIBUTE,"
508 " const %s *data, size_t *size)\n"
509 "{\n",
510 s->gen_name, s->gen_name);
512 switch (s->type->type) {
513 case TInteger:
514 case TBoolean:
515 case TOctetString:
516 case TGeneralizedTime:
517 case TGeneralString:
518 case TTeletexString:
519 case TUTCTime:
520 case TUTF8String:
521 case TPrintableString:
522 case TIA5String:
523 case TBMPString:
524 case TUniversalString:
525 case TVisibleString:
526 case TNull:
527 case TBitString:
528 case TEnumerated:
529 case TOID:
530 case TSequence:
531 case TSequenceOf:
532 case TSet:
533 case TSetOf:
534 case TTag:
535 case TType:
536 case TChoice:
537 fprintf (codefile,
538 "size_t ret HEIMDAL_UNUSED_ATTRIBUTE = 0;\n"
539 "size_t l HEIMDAL_UNUSED_ATTRIBUTE;\n"
540 "int i HEIMDAL_UNUSED_ATTRIBUTE, e HEIMDAL_UNUSED_ATTRIBUTE;\n\n");
542 encode_type("data", s->type, "Top");
544 fprintf (codefile, "*size = ret;\n"
545 "return 0;\n");
546 break;
547 default:
548 abort ();
550 fprintf (codefile, "}\n\n");