r19604: This is a massive commit, and I appologise in advance for it's size.
[samba.git] / source4 / heimdal / lib / asn1 / gen_decode.c
1 /*
2  * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden). 
4  * All rights reserved. 
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met: 
9  *
10  * 1. Redistributions of source code must retain the above copyright 
11  *    notice, this list of conditions and the following disclaimer. 
12  *
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. 
16  *
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. 
20  *
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. 
32  */
33
34 #include "gen_locl.h"
35 #include "lex.h"
36
37 RCSID("$Id: gen_decode.c,v 1.30 2006/09/24 09:13:12 lha Exp $");
38
39 static void
40 decode_primitive (const char *typename, const char *name, const char *forwstr)
41 {
42 #if 0
43     fprintf (codefile,
44              "e = decode_%s(p, len, %s, &l);\n"
45              "%s;\n",
46              typename,
47              name,
48              forwstr);
49 #else
50     fprintf (codefile,
51              "e = der_get_%s(p, len, %s, &l);\n"
52              "if(e) %s;\np += l; len -= l; ret += l;\n",
53              typename,
54              name,
55              forwstr);
56 #endif
57 }
58
59 static int
60 is_primitive_type(int type)
61 {
62     switch(type) {
63     case TInteger:
64     case TBoolean:
65     case TOctetString:
66     case TBitString:
67     case TEnumerated:
68     case TGeneralizedTime:
69     case TGeneralString:
70     case TOID:
71     case TUTCTime:
72     case TUTF8String:
73     case TPrintableString:
74     case TIA5String:
75     case TBMPString:
76     case TUniversalString:
77     case TNull:
78         return 1;
79     default:
80         return 0;
81     }
82 }
83
84 static void
85 find_tag (const Type *t,
86           Der_class *cl, Der_type *ty, unsigned *tag)
87 {
88     switch (t->type) {
89     case TBitString:
90         *cl  = ASN1_C_UNIV;
91         *ty  = PRIM;
92         *tag = UT_BitString;
93         break;
94     case TBoolean:
95         *cl  = ASN1_C_UNIV;
96         *ty  = PRIM;
97         *tag = UT_Boolean;
98         break;
99     case TChoice: 
100         errx(1, "Cannot have recursive CHOICE");
101     case TEnumerated:
102         *cl  = ASN1_C_UNIV;
103         *ty  = PRIM;
104         *tag = UT_Enumerated;
105         break;
106     case TGeneralString: 
107         *cl  = ASN1_C_UNIV;
108         *ty  = PRIM;
109         *tag = UT_GeneralString;
110         break;
111     case TGeneralizedTime: 
112         *cl  = ASN1_C_UNIV;
113         *ty  = PRIM;
114         *tag = UT_GeneralizedTime;
115         break;
116     case TIA5String:
117         *cl  = ASN1_C_UNIV;
118         *ty  = PRIM;
119         *tag = UT_IA5String;
120         break;
121     case TInteger: 
122         *cl  = ASN1_C_UNIV;
123         *ty  = PRIM;
124         *tag = UT_Integer;
125         break;
126     case TNull:
127         *cl  = ASN1_C_UNIV;
128         *ty  = PRIM;
129         *tag = UT_Null;
130         break;
131     case TOID: 
132         *cl  = ASN1_C_UNIV;
133         *ty  = PRIM;
134         *tag = UT_OID;
135         break;
136     case TOctetString: 
137         *cl  = ASN1_C_UNIV;
138         *ty  = PRIM;
139         *tag = UT_OctetString;
140         break;
141     case TPrintableString:
142         *cl  = ASN1_C_UNIV;
143         *ty  = PRIM;
144         *tag = UT_PrintableString;
145         break;
146     case TSequence: 
147     case TSequenceOf:
148         *cl  = ASN1_C_UNIV;
149         *ty  = CONS;
150         *tag = UT_Sequence;
151         break;
152     case TSet: 
153     case TSetOf:
154         *cl  = ASN1_C_UNIV;
155         *ty  = CONS;
156         *tag = UT_Set;
157         break;
158     case TTag: 
159         *cl  = t->tag.tagclass;
160         *ty  = is_primitive_type(t->subtype->type) ? PRIM : CONS;
161         *tag = t->tag.tagvalue;
162         break;
163     case TType: 
164         if ((t->symbol->stype == Stype && t->symbol->type == NULL)
165             || t->symbol->stype == SUndefined) {
166             error_message("%s is imported or still undefined, "
167                           " can't generate tag checking data in CHOICE "
168                           "without this information",
169                           t->symbol->name);
170             exit(1);
171         }
172         find_tag(t->symbol->type, cl, ty, tag);
173         return;
174     case TUTCTime: 
175         *cl  = ASN1_C_UNIV;
176         *ty  = PRIM;
177         *tag = UT_UTCTime;
178         break;
179     case TUTF8String:
180         *cl  = ASN1_C_UNIV;
181         *ty  = PRIM;
182         *tag = UT_UTF8String;
183         break;
184     case TBMPString:
185         *cl  = ASN1_C_UNIV;
186         *ty  = PRIM;
187         *tag = UT_BMPString;
188         break;
189     case TUniversalString:
190         *cl  = ASN1_C_UNIV;
191         *ty  = PRIM;
192         *tag = UT_UniversalString;
193         break;
194     default:
195         abort();
196     }
197 }
198
199 static int
200 decode_type (const char *name, const Type *t, int optional, 
201              const char *forwstr, const char *tmpstr)
202 {
203     switch (t->type) {
204     case TType: {
205         if (optional)
206             fprintf(codefile, 
207                     "%s = calloc(1, sizeof(*%s));\n"
208                     "if (%s == NULL) %s;\n",
209                     name, name, name, forwstr);
210         fprintf (codefile,
211                  "e = decode_%s(p, len, %s, &l);\n",
212                  t->symbol->gen_name, name);
213         if (optional) {
214             fprintf (codefile,
215                      "if(e) {\n"
216                      "free(%s);\n"
217                      "%s = NULL;\n"
218                      "} else {\n"
219                      "p += l; len -= l; ret += l;\n"
220                      "}\n",
221                      name, name);
222         } else {
223             fprintf (codefile,
224                      "if(e) %s;\n",
225                      forwstr);
226             fprintf (codefile,
227                      "p += l; len -= l; ret += l;\n");
228         }
229         break;
230     }
231     case TInteger:
232         if(t->members) {
233             char *s;
234             asprintf(&s, "(int*)%s", name);
235             if (s == NULL)
236                 errx (1, "out of memory");
237             decode_primitive ("integer", s, forwstr);
238             free(s);
239         } else if (t->range == NULL) {
240             decode_primitive ("heim_integer", name, forwstr);
241         } else if (t->range->min == INT_MIN && t->range->max == INT_MAX) {
242             decode_primitive ("integer", name, forwstr);
243         } else if (t->range->min == 0 && t->range->max == UINT_MAX) {
244             decode_primitive ("unsigned", name, forwstr);
245         } else if (t->range->min == 0 && t->range->max == INT_MAX) {
246             decode_primitive ("unsigned", name, forwstr);
247         } else
248             errx(1, "%s: unsupported range %d -> %d", 
249                  name, t->range->min, t->range->max);
250         break;
251     case TBoolean:
252       decode_primitive ("boolean", name, forwstr);
253       break;
254     case TEnumerated:
255         decode_primitive ("enumerated", name, forwstr);
256         break;
257     case TOctetString:
258         decode_primitive ("octet_string", name, forwstr);
259         break;
260     case TBitString: {
261         Member *m;
262         int pos = 0;
263
264         if (ASN1_TAILQ_EMPTY(t->members)) {
265             decode_primitive ("bit_string", name, forwstr);
266             break;
267         }
268         fprintf(codefile,
269                 "if (len < 1) return ASN1_OVERRUN;\n"
270                 "p++; len--; ret++;\n");
271         fprintf(codefile,
272                 "do {\n"
273                 "if (len < 1) break;\n");
274         ASN1_TAILQ_FOREACH(m, t->members, members) {
275             while (m->val / 8 > pos / 8) {
276                 fprintf (codefile,
277                          "p++; len--; ret++;\n"
278                          "if (len < 1) break;\n");
279                 pos += 8;
280             }
281             fprintf (codefile,
282                      "(%s)->%s = (*p >> %d) & 1;\n",
283                      name, m->gen_name, 7 - m->val % 8);
284         }
285         fprintf(codefile,
286                 "} while(0);\n");
287         fprintf (codefile,
288                  "p += len; ret += len;\n");
289         break;
290     }
291     case TSequence: {
292         Member *m;
293
294         if (t->members == NULL)
295             break;
296
297         ASN1_TAILQ_FOREACH(m, t->members, members) {
298             char *s;
299
300             if (m->ellipsis)
301                 continue;
302
303             asprintf (&s, "%s(%s)->%s", m->optional ? "" : "&",
304                       name, m->gen_name);
305             if (s == NULL)
306                 errx(1, "malloc");
307             decode_type (s, m->type, m->optional, forwstr, m->gen_name);
308             free (s);
309         }
310         
311         break;
312     }
313     case TSet: {
314         Member *m;
315         unsigned int memno;
316
317         if(t->members == NULL)
318             break;
319
320         fprintf(codefile, "{\n");
321         fprintf(codefile, "unsigned int members = 0;\n");
322         fprintf(codefile, "while(len > 0) {\n");
323         fprintf(codefile, 
324                 "Der_class class;\n"
325                 "Der_type type;\n"
326                 "int tag;\n"
327                 "e = der_get_tag (p, len, &class, &type, &tag, NULL);\n"
328                 "if(e) %s;\n", forwstr);
329         fprintf(codefile, "switch (MAKE_TAG(class, type, tag)) {\n");
330         memno = 0;
331         ASN1_TAILQ_FOREACH(m, t->members, members) {
332             char *s;
333
334             assert(m->type->type == TTag);
335
336             fprintf(codefile, "case MAKE_TAG(%s, %s, %s):\n",
337                     classname(m->type->tag.tagclass),
338                     is_primitive_type(m->type->subtype->type) ? "PRIM" : "CONS",
339                     valuename(m->type->tag.tagclass, m->type->tag.tagvalue));
340
341             asprintf (&s, "%s(%s)->%s", m->optional ? "" : "&", name, m->gen_name);
342             if (s == NULL)
343                 errx(1, "malloc");
344             if(m->optional)
345                 fprintf(codefile, 
346                         "%s = calloc(1, sizeof(*%s));\n"
347                         "if (%s == NULL) { e = ENOMEM; %s; }\n",
348                         s, s, s, forwstr);
349             decode_type (s, m->type, 0, forwstr, m->gen_name);
350             free (s);
351
352             fprintf(codefile, "members |= (1 << %d);\n", memno);
353             memno++;
354             fprintf(codefile, "break;\n");
355         }
356         fprintf(codefile, 
357                 "default:\n"
358                 "return ASN1_MISPLACED_FIELD;\n"
359                 "break;\n");
360         fprintf(codefile, "}\n");
361         fprintf(codefile, "}\n");
362         memno = 0;
363         ASN1_TAILQ_FOREACH(m, t->members, members) {
364             char *s;
365
366             asprintf (&s, "%s->%s", name, m->gen_name);
367             if (s == NULL)
368                 errx(1, "malloc");
369             fprintf(codefile, "if((members & (1 << %d)) == 0)\n", memno);
370             if(m->optional)
371                 fprintf(codefile, "%s = NULL;\n", s);
372             else if(m->defval)
373                 gen_assign_defval(s, m->defval);
374             else
375                 fprintf(codefile, "return ASN1_MISSING_FIELD;\n");
376             free(s);
377             memno++;
378         }
379         fprintf(codefile, "}\n");
380         break;
381     }
382     case TSetOf:
383     case TSequenceOf: {
384         char *n;
385         char *sname;
386
387         fprintf (codefile,
388                  "{\n"
389                  "size_t %s_origlen = len;\n"
390                  "size_t %s_oldret = ret;\n"
391                  "void *%s_tmp;\n"
392                  "ret = 0;\n"
393                  "(%s)->len = 0;\n"
394                  "(%s)->val = NULL;\n"
395                  "while(ret < %s_origlen) {\n"
396                  "%s_tmp = realloc((%s)->val, "
397                  "    sizeof(*((%s)->val)) * ((%s)->len + 1));\n"
398                  "if (%s_tmp == NULL) { %s; }\n"
399                  "(%s)->val = %s_tmp;\n",
400                  tmpstr, tmpstr, tmpstr,
401                  name, name,
402                  tmpstr, tmpstr,
403                  name, name, name,
404                  tmpstr, forwstr, 
405                  name, tmpstr);
406
407         asprintf (&n, "&(%s)->val[(%s)->len]", name, name);
408         if (n == NULL)
409             errx(1, "malloc");
410         asprintf (&sname, "%s_s_of", tmpstr);
411         if (sname == NULL)
412             errx(1, "malloc");
413         decode_type (n, t->subtype, 0, forwstr, sname);
414         fprintf (codefile, 
415                  "(%s)->len++;\n"
416                  "len = %s_origlen - ret;\n"
417                  "}\n"
418                  "ret += %s_oldret;\n"
419                  "}\n",
420                  name,
421                  tmpstr, tmpstr);
422         free (n);
423         free (sname);
424         break;
425     }
426     case TGeneralizedTime:
427         decode_primitive ("generalized_time", name, forwstr);
428         break;
429     case TGeneralString:
430         decode_primitive ("general_string", name, forwstr);
431         break;
432     case TTag:{
433         char *tname;
434
435         fprintf(codefile, 
436                 "{\n"
437                 "size_t %s_datalen, %s_oldlen;\n",
438                 tmpstr, tmpstr);
439         if(dce_fix)
440             fprintf(codefile, 
441                     "int dce_fix;\n");
442         fprintf(codefile, "e = der_match_tag_and_length(p, len, %s, %s, %s, "
443                 "&%s_datalen, &l);\n",
444                 classname(t->tag.tagclass),
445                 is_primitive_type(t->subtype->type) ? "PRIM" : "CONS",
446                 valuename(t->tag.tagclass, t->tag.tagvalue),
447                 tmpstr);
448         if(optional) {
449             fprintf(codefile, 
450                     "if(e) {\n"
451                     "%s = NULL;\n"
452                     "} else {\n"
453                      "%s = calloc(1, sizeof(*%s));\n"
454                      "if (%s == NULL) { e = ENOMEM; %s; }\n",
455                      name, name, name, name, forwstr);
456         } else {
457             fprintf(codefile, "if(e) %s;\n", forwstr);
458         }
459         fprintf (codefile,
460                  "p += l; len -= l; ret += l;\n"
461                  "%s_oldlen = len;\n",
462                  tmpstr);
463         if(dce_fix)
464             fprintf (codefile,
465                      "if((dce_fix = _heim_fix_dce(%s_datalen, &len)) < 0)\n"
466                      "{ e = ASN1_BAD_FORMAT; %s; }\n",
467                      tmpstr, forwstr);
468         else
469             fprintf(codefile, 
470                     "if (%s_datalen > len) { e = ASN1_OVERRUN; %s; }\n"
471                     "len = %s_datalen;\n", tmpstr, forwstr, tmpstr);
472         asprintf (&tname, "%s_Tag", tmpstr);
473         if (tname == NULL)
474             errx(1, "malloc");
475         decode_type (name, t->subtype, 0, forwstr, tname);
476         if(dce_fix)
477             fprintf(codefile,
478                     "if(dce_fix){\n"
479                     "e = der_match_tag_and_length (p, len, "
480                     "(Der_class)0,(Der_type)0, UT_EndOfContent, "
481                     "&%s_datalen, &l);\n"
482                     "if(e) %s;\np += l; len -= l; ret += l;\n"
483                     "} else \n", tmpstr, forwstr);
484         fprintf(codefile, 
485                 "len = %s_oldlen - %s_datalen;\n",
486                 tmpstr, tmpstr);
487         if(optional)
488             fprintf(codefile, 
489                     "}\n");
490         fprintf(codefile, 
491                 "}\n");
492         free(tname);
493         break;
494     }
495     case TChoice: {
496         Member *m, *have_ellipsis = NULL;
497         const char *els = "";
498
499         if (t->members == NULL)
500             break;
501
502         ASN1_TAILQ_FOREACH(m, t->members, members) {
503             const Type *tt = m->type;
504             char *s;
505             Der_class cl;
506             Der_type  ty;
507             unsigned  tag;
508             
509             if (m->ellipsis) {
510                 have_ellipsis = m;
511                 continue;
512             }
513
514             find_tag(tt, &cl, &ty, &tag);
515
516             fprintf(codefile,
517                     "%sif (der_match_tag(p, len, %s, %s, %s, NULL) == 0) {\n",
518                     els,
519                     classname(cl),
520                     ty ? "CONS" : "PRIM",
521                     valuename(cl, tag));
522             asprintf (&s, "%s(%s)->u.%s", m->optional ? "" : "&",
523                       name, m->gen_name);
524             if (s == NULL)
525                 errx(1, "malloc");
526             decode_type (s, m->type, m->optional, forwstr, m->gen_name);
527             fprintf(codefile,
528                     "(%s)->element = %s;\n",
529                     name, m->label);
530             free(s);
531             fprintf(codefile,
532                     "}\n");
533             els = "else ";
534         }
535         if (have_ellipsis) {
536             fprintf(codefile,
537                     "else {\n"
538                     "(%s)->u.%s.data = calloc(1, len);\n"
539                     "if ((%s)->u.%s.data == NULL) {\n"
540                     "e = ENOMEM; %s;\n"
541                     "}\n"
542                     "(%s)->u.%s.length = len;\n"
543                     "memcpy((%s)->u.%s.data, p, len);\n"
544                     "(%s)->element = %s;\n"
545                     "p += len;\n"
546                     "ret += len;\n"
547                     "len -= len;\n"
548                     "}\n",
549                     name, have_ellipsis->gen_name,
550                     name, have_ellipsis->gen_name,
551                     forwstr, 
552                     name, have_ellipsis->gen_name,
553                     name, have_ellipsis->gen_name,
554                     name, have_ellipsis->label);
555         } else {
556             fprintf(codefile,
557                     "else {\n"
558                     "e = ASN1_PARSE_ERROR;\n"
559                     "%s;\n"
560                     "}\n",
561                     forwstr);
562         }
563         break;
564     }
565     case TUTCTime:
566         decode_primitive ("utctime", name, forwstr);
567         break;
568     case TUTF8String:
569         decode_primitive ("utf8string", name, forwstr);
570         break;
571     case TPrintableString:
572         decode_primitive ("printable_string", name, forwstr);
573         break;
574     case TIA5String:
575         decode_primitive ("ia5_string", name, forwstr);
576         break;
577     case TBMPString:
578         decode_primitive ("bmp_string", name, forwstr);
579         break;
580     case TUniversalString:
581         decode_primitive ("universal_string", name, forwstr);
582         break;
583     case TNull:
584         fprintf (codefile, "/* NULL */\n");
585         break;
586     case TOID:
587         decode_primitive ("oid", name, forwstr);
588         break;
589     default :
590         abort ();
591     }
592     return 0;
593 }
594
595 void
596 generate_type_decode (const Symbol *s)
597 {
598     int preserve = preserve_type(s->name) ? TRUE : FALSE;
599
600     fprintf (headerfile,
601              "int    "
602              "decode_%s(const unsigned char *, size_t, %s *, size_t *);\n",
603              s->gen_name, s->gen_name);
604
605     fprintf (codefile, "int\n"
606              "decode_%s(const unsigned char *p,"
607              " size_t len, %s *data, size_t *size)\n"
608              "{\n",
609              s->gen_name, s->gen_name);
610
611     switch (s->type->type) {
612     case TInteger:
613     case TBoolean:
614     case TOctetString:
615     case TOID:
616     case TGeneralizedTime:
617     case TGeneralString:
618     case TUTF8String:
619     case TPrintableString:
620     case TIA5String:
621     case TBMPString:
622     case TUniversalString:
623     case TUTCTime:
624     case TNull:
625     case TEnumerated:
626     case TBitString:
627     case TSequence:
628     case TSequenceOf:
629     case TSet:
630     case TSetOf:
631     case TTag:
632     case TType:
633     case TChoice:
634         fprintf (codefile,
635                  "size_t ret = 0;\n"
636                  "size_t l;\n"
637                  "int e;\n");
638         if (preserve)
639             fprintf (codefile, "const unsigned char *begin = p;\n");
640
641         fprintf (codefile, "\n");
642         fprintf (codefile, "memset(data, 0, sizeof(*data));\n"); /* hack to avoid `unused variable' */
643
644         decode_type ("data", s->type, 0, "goto fail", "Top");
645         if (preserve)
646             fprintf (codefile,
647                      "data->_save.data = calloc(1, ret);\n"
648                      "if (data->_save.data == NULL) { \n"
649                      "e = ENOMEM; goto fail; \n"
650                      "}\n"
651                      "data->_save.length = ret;\n"
652                      "memcpy(data->_save.data, begin, ret);\n");
653         fprintf (codefile, 
654                  "if(size) *size = ret;\n"
655                  "return 0;\n");
656         fprintf (codefile,
657                  "fail:\n"
658                  "free_%s(data);\n"
659                  "return e;\n",
660                  s->gen_name);
661         break;
662     default:
663         abort ();
664     }
665     fprintf (codefile, "}\n\n");
666 }