]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - crypto/heimdal/lib/asn1/gen_free.c
heimdal: Fix multiple security vulnerabilities
[FreeBSD/FreeBSD.git] / crypto / heimdal / lib / asn1 / gen_free.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
36 RCSID("$Id$");
37
38 static void
39 free_primitive (const char *typename, const char *name)
40 {
41     fprintf (codefile, "der_free_%s(%s);\n", typename, name);
42 }
43
44 static void
45 free_type (const char *name, const Type *t, int preserve)
46 {
47     switch (t->type) {
48     case TType:
49 #if 0
50         free_type (name, t->symbol->type, preserve);
51 #endif
52         fprintf (codefile, "free_%s(%s);\n", t->symbol->gen_name, name);
53         break;
54     case TInteger:
55         if (t->range == NULL && t->members == NULL) {
56             free_primitive ("heim_integer", name);
57             break;
58         }
59     case TBoolean:
60     case TEnumerated :
61     case TNull:
62     case TGeneralizedTime:
63     case TUTCTime:
64         /*
65          * This doesn't do much, but it leaves zeros where garbage might
66          * otherwise have been found.  Gets us closer to having the equivalent
67          * of a memset()-to-zero data structure after calling the free
68          * functions.
69          */
70         fprintf(codefile, "*%s = 0;\n", name);
71         break;
72     case TBitString:
73         if (ASN1_TAILQ_EMPTY(t->members))
74             free_primitive("bit_string", name);
75         break;
76     case TOctetString:
77         free_primitive ("octet_string", name);
78         break;
79     case TChoice:
80     case TSet:
81     case TSequence: {
82         Member *m, *have_ellipsis = NULL;
83
84         if (t->members == NULL)
85             break;
86
87         if ((t->type == TSequence || t->type == TChoice) && preserve)
88             fprintf(codefile, "der_free_octet_string(&data->_save);\n");
89
90         if(t->type == TChoice)
91             fprintf(codefile, "switch((%s)->element) {\n", name);
92
93         ASN1_TAILQ_FOREACH(m, t->members, members) {
94             char *s;
95
96             if (m->ellipsis){
97                 have_ellipsis = m;
98                 continue;
99             }
100
101             if(t->type == TChoice)
102                 fprintf(codefile, "case %s:\n", m->label);
103             if (asprintf (&s, "%s(%s)->%s%s",
104                           m->optional ? "" : "&", name,
105                           t->type == TChoice ? "u." : "", m->gen_name) < 0 || s == NULL)
106                 errx(1, "malloc");
107             if(m->optional)
108                 fprintf(codefile, "if(%s) {\n", s);
109             free_type (s, m->type, FALSE);
110             if(m->optional)
111                 fprintf(codefile,
112                         "free(%s);\n"
113                         "%s = NULL;\n"
114                         "}\n",s, s);
115             free (s);
116             if(t->type == TChoice)
117                 fprintf(codefile, "break;\n");
118         }
119
120         if(t->type == TChoice) {
121             if (have_ellipsis)
122                 fprintf(codefile,
123                         "case %s:\n"
124                         "der_free_octet_string(&(%s)->u.%s);\n"
125                         "break;",
126                         have_ellipsis->label,
127                         name, have_ellipsis->gen_name);
128             fprintf(codefile, "}\n");
129         }
130         break;
131     }
132     case TSetOf:
133     case TSequenceOf: {
134         char *n;
135
136         fprintf (codefile, "while((%s)->len){\n", name);
137         if (asprintf (&n, "&(%s)->val[(%s)->len-1]", name, name) < 0 || n == NULL)
138             errx(1, "malloc");
139         free_type(n, t->subtype, FALSE);
140         fprintf(codefile,
141                 "(%s)->len--;\n"
142                 "}\n",
143                 name);
144         fprintf(codefile,
145                 "free((%s)->val);\n"
146                 "(%s)->val = NULL;\n", name, name);
147         free(n);
148         break;
149     }
150     case TGeneralString:
151         free_primitive ("general_string", name);
152         break;
153     case TTeletexString:
154         free_primitive ("general_string", name);
155         break;
156     case TUTF8String:
157         free_primitive ("utf8string", name);
158         break;
159     case TPrintableString:
160         free_primitive ("printable_string", name);
161         break;
162     case TIA5String:
163         free_primitive ("ia5_string", name);
164         break;
165     case TBMPString:
166         free_primitive ("bmp_string", name);
167         break;
168     case TUniversalString:
169         free_primitive ("universal_string", name);
170         break;
171     case TVisibleString:
172         free_primitive ("visible_string", name);
173         break;
174     case TTag:
175         free_type (name, t->subtype, preserve);
176         break;
177     case TOID :
178         free_primitive ("oid", name);
179         break;
180     default :
181         abort ();
182     }
183 }
184
185 void
186 generate_type_free (const Symbol *s)
187 {
188     int preserve = preserve_type(s->name) ? TRUE : FALSE;
189
190     fprintf (codefile, "void ASN1CALL\n"
191              "free_%s(%s *data)\n"
192              "{\n",
193              s->gen_name, s->gen_name);
194
195     free_type ("data", s->type, preserve);
196     fprintf (codefile, "}\n\n");
197 }
198