xref: /freebsd/crypto/heimdal/lib/asn1/gen_encode.c (revision 5e9cd1ae3e10592ed70e7575551cba1bbab04d84)
1b528cefcSMark Murray /*
25e9cd1aeSAssar Westerlund  * Copyright (c) 1997 - 2000 Kungliga Tekniska H�gskolan
3b528cefcSMark Murray  * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray  * All rights reserved.
5b528cefcSMark Murray  *
6b528cefcSMark Murray  * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray  * modification, are permitted provided that the following conditions
8b528cefcSMark Murray  * are met:
9b528cefcSMark Murray  *
10b528cefcSMark Murray  * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray  *
13b528cefcSMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray  *    documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray  *
17b528cefcSMark Murray  * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray  *    may be used to endorse or promote products derived from this software
19b528cefcSMark Murray  *    without specific prior written permission.
20b528cefcSMark Murray  *
21b528cefcSMark Murray  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray  * SUCH DAMAGE.
32b528cefcSMark Murray  */
33b528cefcSMark Murray 
34b528cefcSMark Murray #include "gen_locl.h"
35b528cefcSMark Murray 
365e9cd1aeSAssar Westerlund RCSID("$Id: gen_encode.c,v 1.11 2000/06/19 15:19:08 joda Exp $");
37b528cefcSMark Murray 
38b528cefcSMark Murray static void
39b528cefcSMark Murray encode_primitive (const char *typename, const char *name)
40b528cefcSMark Murray {
41b528cefcSMark Murray     fprintf (codefile,
42b528cefcSMark Murray 	     "e = encode_%s(p, len, %s, &l);\n"
43b528cefcSMark Murray 	     "BACK;\n",
44b528cefcSMark Murray 	     typename,
45b528cefcSMark Murray 	     name);
46b528cefcSMark Murray }
47b528cefcSMark Murray 
48b528cefcSMark Murray static void
49b528cefcSMark Murray encode_type (const char *name, const Type *t)
50b528cefcSMark Murray {
51b528cefcSMark Murray     switch (t->type) {
52b528cefcSMark Murray     case TType:
53b528cefcSMark Murray #if 0
54b528cefcSMark Murray 	encode_type (name, t->symbol->type);
55b528cefcSMark Murray #endif
56b528cefcSMark Murray 	fprintf (codefile,
57b528cefcSMark Murray 		 "e = encode_%s(p, len, %s, &l);\n"
58b528cefcSMark Murray 		 "BACK;\n",
59b528cefcSMark Murray 		 t->symbol->gen_name, name);
60b528cefcSMark Murray 	break;
61b528cefcSMark Murray     case TInteger:
625e9cd1aeSAssar Westerlund 	if(t->members == NULL)
63b528cefcSMark Murray 	    encode_primitive ("integer", name);
645e9cd1aeSAssar Westerlund 	else {
655e9cd1aeSAssar Westerlund 	    char *s;
665e9cd1aeSAssar Westerlund 	    asprintf(&s, "(const int*)%s", name);
675e9cd1aeSAssar Westerlund 	    if(s == NULL)
685e9cd1aeSAssar Westerlund 		errx(1, "out of memory");
695e9cd1aeSAssar Westerlund 	    encode_primitive ("integer", s);
705e9cd1aeSAssar Westerlund 	    free(s);
715e9cd1aeSAssar Westerlund 	}
725e9cd1aeSAssar Westerlund 	break;
735e9cd1aeSAssar Westerlund     case TUInteger:
745e9cd1aeSAssar Westerlund 	encode_primitive ("unsigned", name);
75b528cefcSMark Murray 	break;
76b528cefcSMark Murray     case TOctetString:
77b528cefcSMark Murray 	encode_primitive ("octet_string", name);
78b528cefcSMark Murray 	break;
79b528cefcSMark Murray     case TBitString: {
80b528cefcSMark Murray 	Member *m;
81b528cefcSMark Murray 	int pos;
82b528cefcSMark Murray 	int rest;
83b528cefcSMark Murray 	int tag = -1;
84b528cefcSMark Murray 
85b528cefcSMark Murray 	if (t->members == NULL)
86b528cefcSMark Murray 	    break;
87b528cefcSMark Murray 
88b528cefcSMark Murray 	fprintf (codefile, "{\n"
89b528cefcSMark Murray 		 "unsigned char c = 0;\n");
90b528cefcSMark Murray 	pos = t->members->prev->val;
91b528cefcSMark Murray 	/* fix for buggy MIT (and OSF?) code */
92b528cefcSMark Murray 	if (pos > 31)
93b528cefcSMark Murray 	    abort ();
94b528cefcSMark Murray 	/*
95b528cefcSMark Murray 	 * It seems that if we do not always set pos to 31 here, the MIT
96b528cefcSMark Murray 	 * code will do the wrong thing.
97b528cefcSMark Murray 	 *
98b528cefcSMark Murray 	 * I hate ASN.1 (and DER), but I hate it even more when everybody
99b528cefcSMark Murray 	 * has to screw it up differently.
100b528cefcSMark Murray 	 */
101b528cefcSMark Murray 	pos = 31;
102b528cefcSMark Murray 	rest = 7 - (pos % 8);
103b528cefcSMark Murray 
104b528cefcSMark Murray 	for (m = t->members->prev; m && tag != m->val; m = m->prev) {
105b528cefcSMark Murray 	    while (m->val / 8 < pos / 8) {
106b528cefcSMark Murray 		fprintf (codefile,
107b528cefcSMark Murray 			 "*p-- = c; len--; ret++;\n"
108b528cefcSMark Murray 			 "c = 0;\n");
109b528cefcSMark Murray 		pos -= 8;
110b528cefcSMark Murray 	    }
111b528cefcSMark Murray 	    fprintf (codefile,
112b528cefcSMark Murray 		     "if(%s->%s) c |= 1<<%d;\n", name, m->gen_name,
113b528cefcSMark Murray 		     7 - m->val % 8);
114b528cefcSMark Murray 
115b528cefcSMark Murray 	    if (tag == -1)
116b528cefcSMark Murray 		tag = m->val;
117b528cefcSMark Murray 	}
118b528cefcSMark Murray 
119b528cefcSMark Murray 	fprintf (codefile,
120b528cefcSMark Murray 		 "*p-- = c;\n"
121b528cefcSMark Murray 		 "*p-- = %d;\n"
122b528cefcSMark Murray 		 "len -= 2;\n"
123b528cefcSMark Murray 		 "ret += 2;\n"
124b528cefcSMark Murray 		 "}\n\n"
125b528cefcSMark Murray 		 "e = der_put_length_and_tag (p, len, ret, UNIV, PRIM,"
126b528cefcSMark Murray 		 "UT_BitString, &l);\n"
127b528cefcSMark Murray 		 "BACK;\n",
128b528cefcSMark Murray 		 rest);
129b528cefcSMark Murray 	break;
130b528cefcSMark Murray     }
131b528cefcSMark Murray     case TSequence: {
132b528cefcSMark Murray 	Member *m;
133b528cefcSMark Murray 	int tag = -1;
134b528cefcSMark Murray 
135b528cefcSMark Murray 	if (t->members == NULL)
136b528cefcSMark Murray 	    break;
137b528cefcSMark Murray 
138b528cefcSMark Murray 	for (m = t->members->prev; m && tag != m->val; m = m->prev) {
139b528cefcSMark Murray 	    char *s;
140b528cefcSMark Murray 
141b528cefcSMark Murray 	    asprintf (&s, "%s(%s)->%s", m->optional ? "" : "&", name, m->gen_name);
142b528cefcSMark Murray 	    if (m->optional)
143b528cefcSMark Murray 		fprintf (codefile,
144b528cefcSMark Murray 			 "if(%s)\n",
145b528cefcSMark Murray 			 s);
146b528cefcSMark Murray #if 1
147b528cefcSMark Murray 	    fprintf (codefile, "{\n"
148b528cefcSMark Murray 		     "int oldret = ret;\n"
149b528cefcSMark Murray 		     "ret = 0;\n");
150b528cefcSMark Murray #endif
151b528cefcSMark Murray 	    encode_type (s, m->type);
152b528cefcSMark Murray 	    fprintf (codefile,
153b528cefcSMark Murray 		     "e = der_put_length_and_tag (p, len, ret, CONTEXT, CONS, "
154b528cefcSMark Murray 		     "%d, &l);\n"
155b528cefcSMark Murray 		     "BACK;\n",
156b528cefcSMark Murray 		     m->val);
157b528cefcSMark Murray #if 1
158b528cefcSMark Murray 	    fprintf (codefile,
159b528cefcSMark Murray 		     "ret += oldret;\n"
160b528cefcSMark Murray 		     "}\n");
161b528cefcSMark Murray #endif
162b528cefcSMark Murray 	    if (tag == -1)
163b528cefcSMark Murray 		tag = m->val;
164b528cefcSMark Murray 	    free (s);
165b528cefcSMark Murray 	}
166b528cefcSMark Murray 	fprintf (codefile,
167b528cefcSMark Murray 		 "e = der_put_length_and_tag (p, len, ret, UNIV, CONS, UT_Sequence, &l);\n"
168b528cefcSMark Murray 		 "BACK;\n");
169b528cefcSMark Murray 	break;
170b528cefcSMark Murray     }
171b528cefcSMark Murray     case TSequenceOf: {
172b528cefcSMark Murray 	char *n;
173b528cefcSMark Murray 
174b528cefcSMark Murray 	fprintf (codefile,
175b528cefcSMark Murray 		 "for(i = (%s)->len - 1; i >= 0; --i) {\n"
176b528cefcSMark Murray #if 1
177b528cefcSMark Murray 		 "int oldret = ret;\n"
178b528cefcSMark Murray 		 "ret = 0;\n",
179b528cefcSMark Murray #else
180b528cefcSMark Murray 		 ,
181b528cefcSMark Murray #endif
182b528cefcSMark Murray 		 name);
183b528cefcSMark Murray 	asprintf (&n, "&(%s)->val[i]", name);
184b528cefcSMark Murray 	encode_type (n, t->subtype);
185b528cefcSMark Murray 	fprintf (codefile,
186b528cefcSMark Murray #if 1
187b528cefcSMark Murray 		 "ret += oldret;\n"
188b528cefcSMark Murray #endif
189b528cefcSMark Murray 		 "}\n"
190b528cefcSMark Murray 		 "e = der_put_length_and_tag (p, len, ret, UNIV, CONS, UT_Sequence, &l);\n"
191b528cefcSMark Murray 		 "BACK;\n");
192b528cefcSMark Murray 	free (n);
193b528cefcSMark Murray 	break;
194b528cefcSMark Murray     }
195b528cefcSMark Murray     case TGeneralizedTime:
196b528cefcSMark Murray 	encode_primitive ("generalized_time", name);
197b528cefcSMark Murray 	break;
198b528cefcSMark Murray     case TGeneralString:
199b528cefcSMark Murray 	encode_primitive ("general_string", name);
200b528cefcSMark Murray 	break;
201b528cefcSMark Murray     case TApplication:
202b528cefcSMark Murray 	encode_type (name, t->subtype);
203b528cefcSMark Murray 	fprintf (codefile,
204b528cefcSMark Murray 		 "e = der_put_length_and_tag (p, len, ret, APPL, CONS, %d, &l);\n"
205b528cefcSMark Murray 		 "BACK;\n",
206b528cefcSMark Murray 		 t->application);
207b528cefcSMark Murray 	break;
208b528cefcSMark Murray     default:
209b528cefcSMark Murray 	abort ();
210b528cefcSMark Murray     }
211b528cefcSMark Murray }
212b528cefcSMark Murray 
213b528cefcSMark Murray void
214b528cefcSMark Murray generate_type_encode (const Symbol *s)
215b528cefcSMark Murray {
216b528cefcSMark Murray   fprintf (headerfile,
217b528cefcSMark Murray 	   "int    "
218b528cefcSMark Murray 	   "encode_%s(unsigned char *, size_t, const %s *, size_t *);\n",
219b528cefcSMark Murray 	   s->gen_name, s->gen_name);
220b528cefcSMark Murray 
221b528cefcSMark Murray   fprintf (codefile, "#define BACK if (e) return e; p -= l; len -= l; ret += l\n\n");
222b528cefcSMark Murray 
223b528cefcSMark Murray 
224b528cefcSMark Murray   fprintf (codefile, "int\n"
225b528cefcSMark Murray 	   "encode_%s(unsigned char *p, size_t len,"
226b528cefcSMark Murray 	   " const %s *data, size_t *size)\n"
227b528cefcSMark Murray 	   "{\n",
228b528cefcSMark Murray 	   s->gen_name, s->gen_name);
229b528cefcSMark Murray 
230b528cefcSMark Murray   switch (s->type->type) {
231b528cefcSMark Murray   case TInteger:
2325e9cd1aeSAssar Westerlund   case TUInteger:
233b528cefcSMark Murray   case TOctetString:
234b528cefcSMark Murray   case TGeneralizedTime:
235b528cefcSMark Murray   case TGeneralString:
236b528cefcSMark Murray   case TBitString:
237b528cefcSMark Murray   case TSequence:
238b528cefcSMark Murray   case TSequenceOf:
239b528cefcSMark Murray   case TApplication:
240b528cefcSMark Murray   case TType:
241b528cefcSMark Murray     fprintf (codefile,
242b528cefcSMark Murray 	     "size_t ret = 0;\n"
243b528cefcSMark Murray 	     "size_t l;\n"
244b528cefcSMark Murray 	     "int i, e;\n\n");
245b528cefcSMark Murray     fprintf(codefile, "i = 0;\n"); /* hack to avoid `unused variable' */
246b528cefcSMark Murray 
247b528cefcSMark Murray       encode_type("data", s->type);
2485e9cd1aeSAssar Westerlund 
249b528cefcSMark Murray     fprintf (codefile, "*size = ret;\n"
250b528cefcSMark Murray 	     "return 0;\n");
251b528cefcSMark Murray     break;
252b528cefcSMark Murray   default:
253b528cefcSMark Murray     abort ();
254b528cefcSMark Murray   }
255b528cefcSMark Murray   fprintf (codefile, "}\n\n");
256b528cefcSMark Murray }
257