aboutsummaryrefslogtreecommitdiffstats
path: root/Source/lib/asn1/der_decoder.c
blob: f9a8425c155f7be395114af52c089b54bc16b7e4 (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
/**
 * @file der_decoder.c
 *
 * @brief Implementation of der_decoder_t.
 */

/*
 * Copyright (C) 2000-2004 Andreas Steffen
 * Copyright (C) 2006 Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * Some parts taken over from pluto/asn1.c
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include <gmp.h>

#include "der_decoder.h"

#include <utils/allocator.h>
#include <daemon.h>



typedef struct private_der_decoder_t private_der_decoder_t;

/**
 * Private data of a der_decoder_t object.
 */
struct private_der_decoder_t {
	/**
	 * Public interface for this signer.
	 */
	der_decoder_t public;
	
	/**
	 * Rule which was just processed
	 */
	asn1_rule_t *rule;
	
	/**
	 * First rule of the whole ruleset
	 */
	asn1_rule_t *first_rule;
	
	/**
	 * Output data struct
	 */
	void *output;
	
	/**
	 * Complex things like this need a logger ;-)
	 */
	logger_t *logger;
};

status_t read_hdr(private_der_decoder_t *this, chunk_t *data);

/**
 * Read a sequence from data, parse its contents recursivly
 */
status_t read_sequence(private_der_decoder_t *this, chunk_t data)
{
	status_t status;
	asn1_rule_t *next_rule;
	
	while(TRUE)
	{
		next_rule = this->rule + 1;
		if (next_rule->type == ASN1_END)
		{
			this->rule++;
			break;
		}
		status = read_hdr(this, &data);
		if (status != SUCCESS)
		{
			return status;
		}
	}
	
	this->logger->log(this->logger, CONTROL|LEVEL2, "Sequence end");
	return SUCCESS;
}

/**
 * Read choice of data, parse if one of the choosable types arise
 */
status_t read_choice(private_der_decoder_t *this, chunk_t *data)
{
	status_t status = PARSE_ERROR;
	asn1_rule_t *next_rule;
	bool found = FALSE;
	
	this->logger->log_chunk(this->logger, CONTROL|LEVEL2, "Choice data", *data);
	
	while(TRUE)
	{
		next_rule = this->rule + 1;
		if (next_rule->type == ASN1_END)
		{
			this->rule++;
			return status;
		}
		if (!found && *(data->ptr) == next_rule->type)
		{
			found = TRUE;
			status = read_hdr(this, data);
		}
		else
		{
			this->rule++;
		}
	}
	this->logger->log(this->logger, CONTROL|LEVEL2, "Choice end");
	return status;
}

/**
 * Read a utc or generalized time
 */
status_t read_time(private_der_decoder_t *this, chunk_t data)
{
	struct tm t;
	time_t tz_offset;
	u_char *eot = NULL;
	const char* format;
	time_t *result = (time_t*)((u_int8_t*)this->output + this->rule->data_offset);
	
	/* TODO: Test it */
	this->logger->log_chunk(this->logger, CONTROL|LEVEL2, "TIME", data);
	
	if ((eot = memchr(data.ptr, 'Z', data.len)) != NULL)
	{
		/* Zulu time with a zero time zone offset */
		tz_offset = 0;
	}
	else if ((eot = memchr(data.ptr, '+', data.len)) != NULL)
	{
		int tz_hour, tz_min;
		
		sscanf(eot+1, "%2d%2d", &tz_hour, &tz_min);
		/* positive time zone offset */
		tz_offset = 3600*tz_hour + 60*tz_min;
	}
	else if ((eot = memchr(data.ptr, '-', data.len)) != NULL)
	{
		int tz_hour, tz_min;
		
		sscanf(eot+1, "%2d%2d", &tz_hour, &tz_min);
		/* negative time zone offset */
		tz_offset = -3600*tz_hour - 60*tz_min;
	}
	else
	{
		/* error in time format */
		return PARSE_ERROR; 
	}

	if (this->rule->type == ASN1_UTCTIME)
	{
		format = "%2d%2d%2d%2d%2d";
	}
	else
	{
		format = "%4d%2d%2d%2d%2d";
	}
	
	sscanf(data.ptr, format, &t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min);
	
	/* is there a seconds field? */
	if ((eot - data.ptr) == ((this->rule->type == ASN1_UTCTIME)?12:14))
	{
		sscanf(eot-2, "%2d", &t.tm_sec);
	}
	else
	{
		t.tm_sec = 0;
	}

	/* representation of year */
	if (t.tm_year >= 1900)
	{
		t.tm_year -= 1900;
	}
	else if (t.tm_year >= 100)
	{
		return PARSE_ERROR;
	}
	else if (t.tm_year < 50)
	{
		t.tm_year += 100;
	}

	/* representation of month 0..11*/
	t.tm_mon--;

	/* set daylight saving time to off */
	t.tm_isdst = 0;

	/* compensate timezone */

	*result = mktime(&t) - timezone - tz_offset;
	return SUCCESS;
}

/**
 * Read an integer as u_int or as mpz_t
 */
status_t read_int(private_der_decoder_t *this, chunk_t data)
{
	this->logger->log_chunk(this->logger, CONTROL|LEVEL2, "ASN1_INTEGER", data);
	
	if (this->rule->flags & ASN1_MPZ)
	{
		mpz_t *mpz = (mpz_t*)((u_int8_t*)this->output + this->rule->data_offset);
		mpz_import(*mpz, data.len, 1, 1, 1, 0, data.ptr);
	}
	else
	{	
		u_int *integ = (u_int*)((u_int8_t*)this->output + this->rule->data_offset);
		
		*integ = 0;
		while (data.len-- > 0)
		{
			*integ = 256 * (*integ) + *data.ptr++;
		}
	}
	return SUCCESS;
}

/**
 * Read boolean value 
 */
status_t read_bool(private_der_decoder_t *this, chunk_t data)
{
	this->logger->log_chunk(this->logger, CONTROL|LEVEL2, "ASN1_BOOLEAN", data);

	bool *boolean = (u_int*)((u_int8_t*)this->output + this->rule->data_offset);
	
	*boolean = *data.ptr;
	
	return SUCCESS;
}

/**
 * Read an OID
 */
status_t read_oid(private_der_decoder_t *this, chunk_t data)
{
	this->logger->log_chunk(this->logger, CONTROL|LEVEL2, "ASN1_OID", data);
	/* TODO: OID parsing stuff */
	return SUCCESS;
}

/**
 * Read a bitstring
 */
status_t read_bitstring(private_der_decoder_t *this, chunk_t data)
{
	/* TODO: cleanly determine amount of unused bits */
	
	/* skip "unused-bits-in-following-byte"-byte */
	data.ptr += 1;
	data.len -= 1;
	
	if (data.len < 1)
	{
		return FAILED;
	}
	
	chunk_t *chunk = (chunk_t*)((u_int8_t*)this->output + this->rule->data_offset);
	
	*chunk = allocator_clone_chunk(data);
	
	this->logger->log_chunk(this->logger, CONTROL|LEVEL2, "ASN1_BITSTRING", data);
	return SUCCESS;
}

/**
 * Read any type which appears in a chunk
 */
status_t read_any(private_der_decoder_t *this, chunk_t data)
{	
	chunk_t *chunk = (chunk_t*)((u_int8_t*)this->output + this->rule->data_offset);
	
	*chunk = allocator_clone_chunk(data);
	
	this->logger->log_chunk(this->logger, CONTROL|LEVEL2, "ASN1_ANY", data);
	return SUCCESS;
}

/**
 * Read the length field of a type
 */
u_int32_t read_length(chunk_t *data)
{
	u_int8_t n;
	size_t len;
	
	if (data->len < 1)
	{
		return -1;
	}
	
	/* read first octet of length field */
	n = *data->ptr;
	data->ptr++; data->len--;

	if ((n & 0x80) == 0) 
	{
		/* single length octet */
		return n;
	}
	
	/* composite length, determine number of length octets */
	n &= 0x7f;
	
	if (n > data->len)
	{
		/* length longer than available bytes */
		return -1;
	}
	
	if (n > sizeof(len))
	{
		/* larger than size_t can hold */
		return -1;
	}
	
	len = 0;
	while (n-- > 0)
	{
		len = 256 * len + *data->ptr;
		data->ptr++; data->len--;
	}
	return len;
}

/**
 * Read the next field
 */
status_t read_hdr(private_der_decoder_t *this, chunk_t *data)
{
	chunk_t inner;
	/* TODO: Redo this that an average mid-european can understand it */
	
beginning:
	/* advance to the next rule */
	this->rule++;
	
	this->logger->log(this->logger, CONTROL|LEVEL2, "reading rule %d %s",
						this->rule - this->first_rule,
						mapping_find(asn1_type_m, this->rule->type));
	
	switch (this->rule->type)
	{
		case ASN1_END:
			/* ignore, handled outside */
			return SUCCESS;
		case ASN1_CHOICE:
			/* CHOICE has no type/length */
			break;
		default:
			/* anything else has type/length */
			if (data->len == 0)
			{
				goto beginning;
			}
			this->logger->log_chunk(this->logger, CONTROL|LEVEL3, "reading from:", *data);
			
			/* read type, advance in data */
			if (this->rule->type != ASN1_ANY && *(data->ptr) != this->rule->type)
			{
				if (this->rule->flags & ASN1_OPTIONAL)
				{
					goto beginning;
				}
				if (this->rule->flags & ASN1_DEFAULT)
				{
					goto beginning;
				}
				this->logger->log(this->logger, CONTROL|LEVEL2, "Bad byte found: %x, %x expected", 
								*data->ptr, this->rule->type);
				return PARSE_ERROR;
			}
			data->ptr++;
			data->len--;
			
			/* read length, advance in data */
			inner.len = read_length(data);
			if (inner.len == -1)
			{
				this->logger->log(this->logger, CONTROL|LEVEL2, "Error reading length");
				return PARSE_ERROR;
			}
			this->logger->log(this->logger, CONTROL|LEVEL2, "Length is %d", inner.len);
			inner.ptr = data->ptr;
			
			/* advance in data, at the size of the inner */
			data->ptr += inner.len;
			data->len -= inner.len;
	}
	
	/* process inner */
	while (TRUE)
	{
		switch (this->rule->type)
		{
			case ASN1_INTEGER:
				return read_int(this, inner);
			case ASN1_BOOLEAN:
				return read_bool(this, inner);
			case ASN1_SEQUENCE:
			case ASN1_SET:
				return read_sequence(this, inner);
			case ASN1_TAG_E_0:
			case ASN1_TAG_E_1:
			case ASN1_TAG_E_2:
			case ASN1_TAG_E_3:
			case ASN1_TAG_E_4:
			case ASN1_TAG_E_5:
			case ASN1_TAG_E_6:
			case ASN1_TAG_E_7:
				return read_hdr(this, &inner);
			case ASN1_TAG_I_0:
			case ASN1_TAG_I_1:
			case ASN1_TAG_I_2:
			case ASN1_TAG_I_3:
			case ASN1_TAG_I_4:
			case ASN1_TAG_I_5:
			case ASN1_TAG_I_6:
			case ASN1_TAG_I_7:
				this->rule++;
				continue;
			case ASN1_OID:
				return read_oid(this, inner);
			case ASN1_CHOICE:
				return read_choice(this, data);
			case ASN1_NULL:
				return SUCCESS;
			case ASN1_ANY:
				return read_any(this, inner);
			case ASN1_UTCTIME:
				return read_time(this, inner);
			case  ASN1_GENERALIZEDTIME:
				return read_time(this, inner);
			case ASN1_BITSTRING:
				return read_bitstring(this, inner);
			case ASN1_OCTETSTRING:
				return read_any(this, inner);
			default:
				return NOT_SUPPORTED;
		}
	}
}

/**
 * Implements der_decoder_t.decode
 */
status_t decode(private_der_decoder_t *this, chunk_t input, void *output)
{
	this->rule = this->first_rule - 1;
	this->output = output;
	/* start parsing recursivly */
	return read_hdr(this, &input);
}

/**
 * Implementation of der_decoder.destroy.
 */
static void destroy(private_der_decoder_t *this)
{
	this->logger->destroy(this->logger);
	allocator_free(this);
}

/*
 * Described in header.
 */
der_decoder_t *der_decoder_create(asn1_rule_t *rules)
{
	private_der_decoder_t *this = allocator_alloc_thing(private_der_decoder_t);
	
	/* public functions */
	this->public.decode = (status_t (*) (der_decoder_t*,chunk_t,void*))decode;
	this->public.destroy = (void (*) (der_decoder_t*))destroy;
	
	this->first_rule = rules;
	this->logger = logger_create("[DERDC]", CONTROL, FALSE, NULL);
	
	return &(this->public);
}