summaryrefslogtreecommitdiff
path: root/source4/heimdal/lib/asn1/gen_encode.c
blob: 012d4677f4a39dc2fe8965553309ad759aed8dee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
 * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
 * (Royal Institute of Technology, Stockholm, Sweden).
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "gen_locl.h"

RCSID("$Id$");

static void
encode_primitive (const char *typename, const char *name)
{
    fprintf (codefile,
	     "e = der_put_%s(p, len, %s, &l);\n"
	     "if (e) return e;\np -= l; len -= l; ret += l;\n\n",
	     typename,
	     name);
}

const char *
classname(Der_class class)
{
    const char *cn[] = { "ASN1_C_UNIV", "ASN1_C_APPL",
			 "ASN1_C_CONTEXT", "ASN1_C_PRIV" };
    if(class < ASN1_C_UNIV || class > ASN1_C_PRIVATE)
	return "???";
    return cn[class];
}


const char *
valuename(Der_class class, int value)
{
    static char s[32];
    struct {
	int value;
	const char *s;
    } *p, values[] = {
#define X(Y) { Y, #Y }
	X(UT_BMPString),
	X(UT_BitString),
	X(UT_Boolean),
	X(UT_EmbeddedPDV),
	X(UT_Enumerated),
	X(UT_External),
	X(UT_GeneralString),
	X(UT_GeneralizedTime),
	X(UT_GraphicString),
	X(UT_IA5String),
	X(UT_Integer),
	X(UT_Null),
	X(UT_NumericString),
	X(UT_OID),
	X(UT_ObjectDescriptor),
	X(UT_OctetString),
	X(UT_PrintableString),
	X(UT_Real),
	X(UT_RelativeOID),
	X(UT_Sequence),
	X(UT_Set),
	X(UT_TeletexString),
	X(UT_UTCTime),
	X(UT_UTF8String),
	X(UT_UniversalString),
	X(UT_VideotexString),
	X(UT_VisibleString),
#undef X
	{ -1, NULL }
    };
    if(class == ASN1_C_UNIV) {
	for(p = values; p->value != -1; p++)
	    if(p->value == value)
		return p->s;
    }
    snprintf(s, sizeof(s), "%d", value);
    return s;
}

static int
encode_type (const char *name, const Type *t, const char *tmpstr)
{
    int constructed = 1;

    switch (t->type) {
    case TType:
#if 0
	encode_type (name, t->symbol->type);
#endif
	fprintf (codefile,
		 "e = encode_%s(p, len, %s, &l);\n"
		 "if (e) return e;\np -= l; len -= l; ret += l;\n\n",
		 t->symbol->gen_name, name);
	break;
    case TInteger:
	if(t->members) {
	    fprintf(codefile,
		    "{\n"
		    "int enumint = (int)*%s;\n",
		    name);
	    encode_primitive ("integer", "&enumint");
	    fprintf(codefile, "}\n;");
	} else if (t->range == NULL) {
	    encode_primitive ("heim_integer", name);
	} else if (t->range->min == INT_MIN && t->range->max == INT_MAX) {
	    encode_primitive ("integer", name);
	} else if (t->range->min == 0 && t->range->max == UINT_MAX) {
	    encode_primitive ("unsigned", name);
	} else if (t->range->min == 0 && t->range->max == INT_MAX) {
	    encode_primitive ("unsigned", name);
	} else
	    errx(1, "%s: unsupported range %d -> %d",
		 name, t->range->min, t->range->max);
	constructed = 0;
	break;
    case TBoolean:
	encode_primitive ("boolean", name);
	constructed = 0;
	break;
    case TOctetString:
	encode_primitive ("octet_string", name);
	constructed = 0;
	break;
    case TBitString: {
	Member *m;
	int pos;

	if (ASN1_TAILQ_EMPTY(t->members)) {
	    encode_primitive("bit_string", name);
	    constructed = 0;
	    break;
	}

	fprintf (codefile, "{\n"
		 "unsigned char c = 0;\n");
	if (!rfc1510_bitstring)
	    fprintf (codefile,
		     "int rest = 0;\n"
		     "int bit_set = 0;\n");
#if 0
	pos = t->members->prev->val;
	/* fix for buggy MIT (and OSF?) code */
	if (pos > 31)
	    abort ();
#endif
	/*
	 * It seems that if we do not always set pos to 31 here, the MIT
	 * code will do the wrong thing.
	 *
	 * I hate ASN.1 (and DER), but I hate it even more when everybody
	 * has to screw it up differently.
	 */
	pos = ASN1_TAILQ_LAST(t->members, memhead)->val;
	if (rfc1510_bitstring) {
	    if (pos < 31)
		pos = 31;
	}

	ASN1_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) {
	    while (m->val / 8 < pos / 8) {
		if (!rfc1510_bitstring)
		    fprintf (codefile,
			     "if (c != 0 || bit_set) {\n");
		fprintf (codefile,
			 "if (len < 1) return ASN1_OVERFLOW;\n"
			 "*p-- = c; len--; ret++;\n");
		if (!rfc1510_bitstring)
		    fprintf (codefile,
			     "if (!bit_set) {\n"
			     "rest = 0;\n"
			     "while(c) { \n"
			     "if (c & 1) break;\n"
			     "c = c >> 1;\n"
			     "rest++;\n"
			     "}\n"
			     "bit_set = 1;\n"
			     "}\n"
			     "}\n");
		fprintf (codefile,
			 "c = 0;\n");
		pos -= 8;
	    }
	    fprintf (codefile,
		     "if((%s)->%s) {\n"
		     "c |= 1<<%d;\n",
		     name, m->gen_name, 7 - m->val % 8);
	    fprintf (codefile,
		     "}\n");
	}

	if (!rfc1510_bitstring)
	    fprintf (codefile,
		     "if (c != 0 || bit_set) {\n");
	fprintf (codefile,
		 "if (len < 1) return ASN1_OVERFLOW;\n"
		 "*p-- = c; len--; ret++;\n");
	if (!rfc1510_bitstring)
	    fprintf (codefile,
		     "if (!bit_set) {\n"
		     "rest = 0;\n"
		     "if(c) { \n"
		     "while(c) { \n"
		     "if (c & 1) break;\n"
		     "c = c >> 1;\n"
		     "rest++;\n"
		     "}\n"
		     "}\n"
		     "}\n"
		     "}\n");

	fprintf (codefile,
		 "if (len < 1) return ASN1_OVERFLOW;\n"
		 "*p-- = %s;\n"
		 "len -= 1;\n"
		 "ret += 1;\n"
		 "}\n\n",
		 rfc1510_bitstring ? "0" : "rest");
	constructed = 0;
	break;
    }
    case TEnumerated : {
	encode_primitive ("enumerated", name);
	constructed = 0;
	break;
    }

    case TSet:
    case TSequence: {
	Member *m;

	if (t->members == NULL)
	    break;

	ASN1_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) {
	    char *s;

	    if (m->ellipsis)
		continue;

	    asprintf (&s, "%s(%s)->%s", m->optional ? "" : "&", name, m->gen_name);
	    if (s == NULL)
		errx(1, "malloc");
	    fprintf(codefile, "/* %s */\n", m->name);
	    if (m->optional)
		fprintf (codefile,
			 "if(%s) ",
			 s);
	    else if(m->defval)
		gen_compare_defval(s + 1, m->defval);
	    fprintf (codefile, "{\n");
	    fprintf (codefile, "size_t %s_oldret = ret;\n", tmpstr);
	    fprintf (codefile, "ret = 0;\n");
	    encode_type (s, m->type, m->gen_name);
	    fprintf (codefile, "ret += %s_oldret;\n", tmpstr);
	    fprintf (codefile, "}\n");
	    free (s);
	}
	break;
    }
    case TSetOf: {

	fprintf(codefile,
		"{\n"
		"struct heim_octet_string *val;\n"
		"size_t elen, totallen = 0;\n"
		"int eret;\n");

	fprintf(codefile,
		"if ((%s)->len > UINT_MAX/sizeof(val[0]))\n"
		"return ERANGE;\n",
		name);

	fprintf(codefile,
		"val = malloc(sizeof(val[0]) * (%s)->len);\n"
		"if (val == NULL && (%s)->len != 0) return ENOMEM;\n",
		name, name);

	fprintf(codefile,
		"for(i = 0; i < (%s)->len; i++) {\n",
		name);

	fprintf(codefile,
		"ASN1_MALLOC_ENCODE(%s, val[i].data, "
		"val[i].length, &(%s)->val[i], &elen, eret);\n",
		t->subtype->symbol->gen_name,
		name);

	fprintf(codefile,
		"if(eret) {\n"
		"i--;\n"
		"while (i >= 0) {\n"
		"free(val[i].data);\n"
		"i--;\n"
		"}\n"
		"free(val);\n"
		"return eret;\n"
		"}\n"
		"totallen += elen;\n"
		"}\n");

	fprintf(codefile,
		"if (totallen > len) {\n"
		"for (i = 0; i < (%s)->len; i++) {\n"
		"free(val[i].data);\n"
		"}\n"
		"free(val);\n"
		"return ASN1_OVERFLOW;\n"
		"}\n",
		name);

	fprintf(codefile,
		"qsort(val, (%s)->len, sizeof(val[0]), _heim_der_set_sort);\n",
		name);

	fprintf (codefile,
		 "for(i = (%s)->len - 1; i >= 0; --i) {\n"
		 "p -= val[i].length;\n"
		 "ret += val[i].length;\n"
		 "memcpy(p + 1, val[i].data, val[i].length);\n"
		 "free(val[i].data);\n"
		 "}\n"
		 "free(val);\n"
		 "}\n",
		 name);
	break;
    }
    case TSequenceOf: {
	char *n;
	char *sname;

	fprintf (codefile,
		 "for(i = (%s)->len - 1; i >= 0; --i) {\n"
		 "size_t %s_for_oldret = ret;\n"
		 "ret = 0;\n",
		 name, tmpstr);
	asprintf (&n, "&(%s)->val[i]", name);
	if (n == NULL)
	    errx(1, "malloc");
	asprintf (&sname, "%s_S_Of", tmpstr);
	if (sname == NULL)
	    errx(1, "malloc");
	encode_type (n, t->subtype, sname);
	fprintf (codefile,
		 "ret += %s_for_oldret;\n"
		 "}\n",
		 tmpstr);
	free (n);
	free (sname);
	break;
    }
    case TGeneralizedTime:
	encode_primitive ("generalized_time", name);
	constructed = 0;
	break;
    case TGeneralString:
	encode_primitive ("general_string", name);
	constructed = 0;
	break;
    case TTeletexString:
	encode_primitive ("general_string", name);
	constructed = 0;
	break;
    case TTag: {
    	char *tname;
	int c;
	asprintf (&tname, "%s_tag", tmpstr);
	if (tname == NULL)
	    errx(1, "malloc");
	c = encode_type (name, t->subtype, tname);
	fprintf (codefile,
		 "e = der_put_length_and_tag (p, len, ret, %s, %s, %s, &l);\n"
		 "if (e) return e;\np -= l; len -= l; ret += l;\n\n",
		 classname(t->tag.tagclass),
		 c ? "CONS" : "PRIM",
		 valuename(t->tag.tagclass, t->tag.tagvalue));
	free (tname);
	break;
    }
    case TChoice:{
	Member *m, *have_ellipsis = NULL;
	char *s;

	if (t->members == NULL)
	    break;

	fprintf(codefile, "\n");

	asprintf (&s, "(%s)", name);
	if (s == NULL)
	    errx(1, "malloc");
	fprintf(codefile, "switch(%s->element) {\n", s);

	ASN1_TAILQ_FOREACH_REVERSE(m, t->members, memhead, members) {
	    char *s2;

	    if (m->ellipsis) {
		have_ellipsis = m;
		continue;
	    }

	    fprintf (codefile, "case %s: {", m->label);
	    asprintf(&s2, "%s(%s)->u.%s", m->optional ? "" : "&",
		     s, m->gen_name);
	    if (s2 == NULL)
		errx(1, "malloc");
	    if (m->optional)
		fprintf (codefile, "if(%s) {\n", s2);
	    fprintf (codefile, "size_t %s_oldret = ret;\n", tmpstr);
	    fprintf (codefile, "ret = 0;\n");
	    constructed = encode_type (s2, m->type, m->gen_name);
	    fprintf (codefile, "ret += %s_oldret;\n", tmpstr);
	    if(m->optional)
		fprintf (codefile, "}\n");
	    fprintf(codefile, "break;\n");
	    fprintf(codefile, "}\n");
	    free (s2);
	}
	free (s);
	if (have_ellipsis) {
	    fprintf(codefile,
		    "case %s: {\n"
		    "if (len < (%s)->u.%s.length)\n"
		    "return ASN1_OVERFLOW;\n"
		    "p -= (%s)->u.%s.length;\n"
		    "ret += (%s)->u.%s.length;\n"
		    "memcpy(p + 1, (%s)->u.%s.data, (%s)->u.%s.length);\n"
		    "break;\n"
		    "}\n",
		    have_ellipsis->label,
		    name, have_ellipsis->gen_name,
		    name, have_ellipsis->gen_name,
		    name, have_ellipsis->gen_name,
		    name, have_ellipsis->gen_name,
		    name, have_ellipsis->gen_name);
	}
	fprintf(codefile, "};\n");
	break;
    }
    case TOID:
	encode_primitive ("oid", name);
	constructed = 0;
	break;
    case TUTCTime:
	encode_primitive ("utctime", name);
	constructed = 0;
	break;
    case TUTF8String:
	encode_primitive ("utf8string", name);
	constructed = 0;
	break;
    case TPrintableString:
	encode_primitive ("printable_string", name);
	constructed = 0;
	break;
    case TIA5String:
	encode_primitive ("ia5_string", name);
	constructed = 0;
	break;
    case TBMPString:
	encode_primitive ("bmp_string", name);
	constructed = 0;
	break;
    case TUniversalString:
	encode_primitive ("universal_string", name);
	constructed = 0;
	break;
    case TVisibleString:
	encode_primitive ("visible_string", name);
	constructed = 0;
	break;
    case TNull:
	fprintf (codefile, "/* NULL */\n");
	constructed = 0;
	break;
    default:
	abort ();
    }
    return constructed;
}

void
generate_type_encode (const Symbol *s)
{
    fprintf (headerfile,
	     "int    "
	     "encode_%s(unsigned char *, size_t, const %s *, size_t *);\n",
	     s->gen_name, s->gen_name);

    fprintf (codefile, "int\n"
	     "encode_%s(unsigned char *p, size_t len,"
	     " const %s *data, size_t *size)\n"
	     "{\n",
	     s->gen_name, s->gen_name);

    switch (s->type->type) {
    case TInteger:
    case TBoolean:
    case TOctetString:
    case TGeneralizedTime:
    case TGeneralString:
    case TTeletexString:
    case TUTCTime:
    case TUTF8String:
    case TPrintableString:
    case TIA5String:
    case TBMPString:
    case TUniversalString:
    case TVisibleString:
    case TNull:
    case TBitString:
    case TEnumerated:
    case TOID:
    case TSequence:
    case TSequenceOf:
    case TSet:
    case TSetOf:
    case TTag:
    case TType:
    case TChoice:
	fprintf (codefile,
		 "size_t ret = 0;\n"
		 "size_t l;\n"
		 "int i, e;\n\n");
	fprintf(codefile, "i = 0;\n"); /* hack to avoid `unused variable' */

	encode_type("data", s->type, "Top");

	fprintf (codefile, "*size = ret;\n"
		 "return 0;\n");
	break;
    default:
	abort ();
    }
    fprintf (codefile, "}\n\n");
}