aboutsummaryrefslogtreecommitdiffstats
path: root/Source/charon/parser.c
blob: b072bc2585a1f4b4c36e4f49ddedea8a924bbdd2 (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
/**
 * @file parser.c
 *
 * @brief Generic parser class used to parse IKEv2-Header and Payload
 *
 */

/*
 * Copyright (C) 2005 Jan Hutter, Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * 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 <stdlib.h>
#include <arpa/inet.h>

#include "allocator.h"
#include "globals.h"
#include "types.h"
#include "parser.h"
#include "logger.h"

/**
 * @private data stored in a context
 * 
 * contains pointers and counters to store current state
 */
typedef struct private_parser_context_s private_parser_context_t;

struct private_parser_context_s {
	/**
	 * Public members
	 */
	parser_context_t public;

	/**
	 * Current bit for reading in input data
	 */
	u_int8_t bit_pos;
	
	/**
	 * Current byte for reading in input data
	 */
	u_int8_t *byte_pos;
	
	/**
	 * input data to parse
	 */
	u_int8_t *input;
	
	/**
	 * roof of input
	 */
	u_int8_t *input_roof;
	
	
};

/**
 * implementation of parser_context_t.destroy
 */
static status_t parser_context_destroy(private_parser_context_t *this)
{
	allocator_free(this);
	
	return SUCCESS;	
}


/**
 * @brief Private data of a parser_t object
 */
typedef struct private_parser_s private_parser_t;

struct private_parser_s {
	/**
	 * Public part of a generator object
	 */
	 parser_t public;

	/**
	 * list of payloads and their description
	 */
	payload_info_t **payload_infos;
	
	/**
	 * logger object
	 */
	logger_t *logger;
};

/**
 * implementation of parser_t.create_context
 */
static private_parser_context_t *create_context(private_parser_t *this, chunk_t data)
{
	private_parser_context_t *context = allocator_alloc_thing(private_parser_context_t);
	if (this == NULL)
	{
		return NULL;	
	}
	
	context->public.destroy = (status_t(*)(parser_context_t*)) parser_context_destroy;
	
	context->input = data.ptr;
	context->byte_pos = data.ptr;
	context->bit_pos = 0;
	context->input_roof = data.ptr + data.len;
	
	return context;
}

/**
 * implementation of parser_context_t.parse_payload
 */
static status_t parse_payload(private_parser_t *this, payload_type_t payload_type, void **data_struct, private_parser_context_t *context)
{
	payload_info_t *payload_info = NULL;
	
	/* find payload in null terminated list*/
	payload_info = *(this->payload_infos);
	while (payload_info)
	{
		if (payload_info->payload_type == payload_type)
		{
			void *output;
			int current;
			
			/* ok, do the parsing */
			output = allocator_alloc(payload_info->data_struct_length);
			
			for (current = 0; current < payload_info->encoding_rules_count; current++)
			{
				encoding_rule_t *rule = &(payload_info->ecoding_rules[current]);
				switch (rule->type)
				{
					case U_INT_4:
					{
						u_int8_t *output_pos = output + rule->offset;
						if (context->byte_pos + sizeof(u_int8_t) > context->input_roof)
						{
							this->logger->log(this->logger, ERROR, "not enough input to parse U_INT_4");
							allocator_free(output);
							return PARSE_ERROR;
						}
						switch (context->bit_pos)
						{
							case 0:
								*output_pos = *(context->byte_pos) >> 4;
								context->bit_pos = 4;
								break;
							case 4:	
								*output_pos = *(context->byte_pos) & 0x0F;
								context->bit_pos = 0;
								context->byte_pos++;
								break;
							default:
								this->logger->log(this->logger, ERROR, "found rule U_INT_4 on bitpos %d", context->bit_pos);
								allocator_free(output);
								return PARSE_ERROR;
						}
						break;
					}
					case U_INT_8:
					{
						u_int8_t *output_pos = output + rule->offset;
						if (context->byte_pos + sizeof(u_int8_t)  > context->input_roof)
						{
							this->logger->log(this->logger, ERROR, "not enough input to parse U_INT_8");
							allocator_free(output);
							return PARSE_ERROR;
						}
						if (context->bit_pos)
						{
							this->logger->log(this->logger, ERROR, "found rule U_INT_8 on bitpos %d", context->bit_pos);
							allocator_free(output);
							return PARSE_ERROR;
						}

						*output_pos = *(context->byte_pos);
						context->byte_pos++;
						break;
					}
					case U_INT_16:
					{
						u_int16_t *output_pos = output + rule->offset;
						if (context->byte_pos + sizeof(u_int16_t) > context->input_roof)
						{
							this->logger->log(this->logger, ERROR, "not enough input to parse U_INT_16");
							allocator_free(output);
							return PARSE_ERROR;
						}
						if (context->bit_pos)
						{
							this->logger->log(this->logger, ERROR, "found rule U_INT_16 on bitpos %d", context->bit_pos);
							allocator_free(output);
							return PARSE_ERROR;
						}
						if ((int)context->byte_pos % 2)
						{
							this->logger->log(this->logger, ERROR, "found rule U_INT_16 on odd bytepos");
							allocator_free(output);
							return PARSE_ERROR;
						}
						*output_pos = ntohs(*((u_int16_t*)context->byte_pos));
						context->byte_pos += 2;
						break;					
					}
					case U_INT_32:
					{
						u_int32_t *output_pos = output + rule->offset;
						if (context->byte_pos + sizeof(u_int32_t) > context->input_roof)
						{
							this->logger->log(this->logger, ERROR, "not enough input to parse U_INT_32");
							allocator_free(output);
							return PARSE_ERROR;
						}
						if (context->bit_pos)
						{
							this->logger->log(this->logger, ERROR, "found rule U_INT_32 on bitpos %d", context->bit_pos);
							allocator_free(output);
							return PARSE_ERROR;
						}
						if ((int)context->byte_pos % 4)
						{
							this->logger->log(this->logger, ERROR, "found rule U_INT_32 on unaligned bytepos");
							allocator_free(output);
							return PARSE_ERROR;
						}
						*output_pos = ntohl(*((u_int32_t*)context->byte_pos));
						context->byte_pos += 4;
						break;		
					}
					case U_INT_64:
					{
						u_int32_t *output_pos = output + rule->offset;
						if (context->byte_pos + 2 * sizeof(u_int32_t) > context->input_roof)
						{
							this->logger->log(this->logger, ERROR, "not enough input to parse U_INT_64");
							allocator_free(output);
							return PARSE_ERROR;
						}
						if (context->bit_pos)
						{
							this->logger->log(this->logger, ERROR, "found rule U_INT_64 on bitpos %d", context->bit_pos);
							allocator_free(output);
							return PARSE_ERROR;
						}
						if ((int)context->byte_pos % 8)
						{
							this->logger->log(this->logger, ERROR, "found rule U_INT_64 on unaligned bytepos");
							allocator_free(output);
							return PARSE_ERROR;
						}
						/* assuming little endian host order */
						*(output_pos + 1) = ntohl(*((u_int32_t*)context->byte_pos));
						context->byte_pos += 4;
						*output_pos = ntohl(*((u_int32_t*)context->byte_pos));
						context->byte_pos += 4;
						
						break;	
					}
					case RESERVED_BIT:
					{
						if (context->byte_pos > context->input_roof)
						{
							this->logger->log(this->logger, ERROR, "not enough input to parse RESERVED_BIT");
							allocator_free(output);
							return PARSE_ERROR;
						}
						context->bit_pos = (context->bit_pos + 1) % 8;
						if (context->bit_pos == 0) 
						{
							context->byte_pos++;	
						}
						break;
					}
					case RESERVED_BYTE:
					{
						if (context->byte_pos > context->input_roof)
						{
							this->logger->log(this->logger, ERROR, "not enough input to parse RESERVED_BYTE");
							allocator_free(output);
							return PARSE_ERROR;
						}
						if (context->bit_pos)
						{
							this->logger->log(this->logger, ERROR, "found rule RESERVED_BYTE on bitpos %d", context->bit_pos);
							allocator_free(output);
							return PARSE_ERROR;
						}
						context->byte_pos++;	
						break;
					}
					case FLAG:
					{
						bool *output_pos = output + rule->offset;
						u_int8_t mask;
						if (context->byte_pos > context->input_roof)
						{
							this->logger->log(this->logger, ERROR, "not enough input to parse FLAG");
							allocator_free(output);
							return PARSE_ERROR;
						}
						mask = 0x01 << (7 - context->bit_pos);
						*output_pos = *context->byte_pos & mask;
					
						if (*output_pos)
						{
							/* set to a "clean", comparable true */
							*output_pos = TRUE;
						} 
						context->bit_pos = (context->bit_pos + 1) % 8;
						if (context->bit_pos == 0) 
						{
							context->byte_pos++;	
						}
						break;
					}
					case LENGTH:
					{
						u_int32_t *output_pos = output + rule->offset;
						if (context->byte_pos + sizeof(u_int32_t) > context->input_roof)
						{
							this->logger->log(this->logger, ERROR, "not enough input to parse LENGTH");
							allocator_free(output);
							return PARSE_ERROR;
						}
						if (context->bit_pos)
						{
							this->logger->log(this->logger, ERROR, "found rule LENGTH on bitpos %d", context->bit_pos);
							allocator_free(output);
							return PARSE_ERROR;
						}
						if ((int)context->byte_pos % 4)
						{
							this->logger->log(this->logger, ERROR, "found rule LENGTH on unaligned bytepos");
							allocator_free(output);
							return PARSE_ERROR;
						}
						*output_pos = ntohl(*((u_int32_t*)context->byte_pos));
						context->byte_pos += 4;
						break;		
					
					}
					case SPI_SIZE:
					{
						
					}
					default:
					{
						this->logger->log(this->logger, ERROR, "parser found unknown type");
						allocator_free(output);
						return PARSE_ERROR;
					}
				}	
			}
			
			*data_struct = output;
			return SUCCESS;
		}
		payload_info++;
	}
	
	this->logger->log(this->logger, ERROR, "Payload not supported");
	return NOT_SUPPORTED;
}

/**
 * implementation of parser_t.destroy
 */
static status_t destroy(private_parser_t *this)
{
	global_logger_manager->destroy_logger(global_logger_manager,this->logger);
	allocator_free(this);	
	
	return SUCCESS;
}

/*
 * see header file
 */
parser_t *parser_create(payload_info_t **payload_infos)
{
	private_parser_t *this = allocator_alloc_thing(private_parser_t);
	
	if (this == NULL)
	{
		return NULL;
	}
	
	global_logger_manager->get_logger(global_logger_manager,PARSER,&(this->logger),"");
	
	if (this->logger == NULL)
	{
		allocator_free(this);
		return NULL;
	}
	this->public.create_context = (parser_context_t*(*)(parser_t*,chunk_t)) create_context;
	this->public.parse_payload = (status_t(*)(parser_t*,payload_type_t,void**,parser_context_t*)) parse_payload;
	this->public.destroy = (status_t(*)(parser_t*)) destroy;
	
	this->payload_infos = payload_infos;	
	
	
	return (parser_t*)this;
}