aboutsummaryrefslogtreecommitdiffstats
path: root/Source/charon/generator.c
blob: 4c28173cefb38f01329b7ad11f3c3d67ddbb79f2 (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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/**
 * @file generator.c
 *
 * @brief Generic generator class used to generate IKEv2-header and payloads.
 *
 */

/*
 * 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 <string.h>
#include <arpa/inet.h>
#include <stdio.h>

#include "allocator.h"
#include "types.h"
#include "generator.h"


typedef struct private_generator_context_s private_generator_context_t;

struct private_generator_context_s{
	/**
	 * Public part of the context
	 */
	generator_context_t public;

	/**
	 * Buffer used to generate the data into.
	 */
	u_int8_t *buffer;

	/**
	 * Current write position in buffer (one byte aligned).
	 */
	u_int8_t *out_position;

	/**
	 * Position of last byte in buffer.
	 */
	u_int8_t *roof_position;

	/**
	 * Current bit writing to in current byte (between 0 and 7).
	 */
	size_t current_bit;

	/**
	 * Associated data struct to read informations from.
	 */
	 void * data_struct;

	/**
	 * Writes the current buffer content into a chunk_t
	 * 
	 * Memory of specific chunk_t gets allocated.
	 *
 	 * @param generator_infos_t calling generator_infos_t object
	 * @param data				pointer of chunk_t to write to
	 * @return 
	 * 							- SUCCESSFUL if succeeded
	 * 							- OUT_OF_RES otherwise
	 */
	status_t (*write_chunk) (private_generator_context_t *this,chunk_t *data);
	

	/**
	 * Makes sure enough space is available in buffer to store amount of bits.
     *
	 * If buffer is to small to hold the specific amount of bits it 
	 * is increased using reallocation function of allocator.
	 *
 	 * @param generator_infos_t calling generator_infos_t object
	 * @param bits 				number of bits to make available in buffer
	 * @return 
	 * 							- SUCCESSFUL if succeeded
	 * 							- OUT_OF_RES otherwise
	 */
	status_t (*make_space_available) (private_generator_context_t *this,size_t bits);

	/**
	 * Writes a specific amount of byte into the buffer.
	 * 
	 * If buffer is to small to hold the specific amount of bytes it 
	 * is increased.
	 *
 	 * @param generator_infos_t calling generator_infos_t object
	 * @param bytes 				pointer to bytes to write
	 * @param number_of_bytes	number of bytes to write into buffer
	 * @return 
	 * 							- SUCCESSFUL if succeeded
	 * 							- OUT_OF_RES otherwise
	 */
	status_t (*write_bytes_to_buffer) (private_generator_context_t *this,void * bytes,size_t number_of_bytes);

	
};

/**
 * Implements generator_infos_t's increase_buffer function.
 * See #generator_infos_s.increase_buffer.
 */
static status_t generator_context_make_space_available (private_generator_context_t *this, size_t bits)
{
	while ((((this->roof_position - this->out_position) * 8) - this->current_bit) < bits)
	{
		size_t old_buffer_size = ((this->roof_position) - (	this->buffer));
		size_t new_buffer_size = old_buffer_size + GENERATOR_DATA_BUFFER_INCREASE_VALUE;
		size_t out_position_offset = ((this->out_position) - (this->buffer));
		u_int8_t *new_buffer;

		new_buffer = allocator_realloc(this->buffer,new_buffer_size);
		if (new_buffer == NULL)
		{
			return OUT_OF_RES;
		}

		this->buffer = new_buffer;

		this->out_position = (this->buffer + out_position_offset);
		this->roof_position = (this->buffer + new_buffer_size);

	}

	return SUCCESS;
}

/**
 * Implements generator_infos_t's write_bytes_to_buffer function.
 * See #generator_infos_s.write_bytes_to_buffer.
 */
static status_t generator_context_write_bytes_to_buffer (private_generator_context_t *this,void * bytes,size_t number_of_bytes)
{
	u_int8_t *read_position = (u_int8_t *) bytes;
	int i;
	status_t status;

	status = this->make_space_available(this,number_of_bytes * 8);

	if (status != SUCCESS)
	{
		return status;
	}

	for (i = 0; i < number_of_bytes; i++)
	{
		*(this->out_position) = *(read_position);
		read_position++;
		this->out_position++;
	}
	return status;
}

/**
 * Implements generator_infos_t's write_chunk function.
 * See #generator_infos_s.write_chunk.
 */
static status_t generator_context_write_chunk (private_generator_context_t *this,chunk_t *data)
{
	size_t data_length = this->out_position - this->buffer;

	if (this->current_bit > 0)
	data_length++;
	data->ptr = allocator_alloc(data_length);
	if (data->ptr == NULL)
	{
		data->len = 0;
		return OUT_OF_RES;
	}
	memcpy(data->ptr,this->buffer,data_length);
	data->len = data_length;
	return SUCCESS;
}


/**
 * Implements generator_infos_t's destroy function.
 * See #generator_infos_s.destroy.
 */
static status_t generator_context_destroy (private_generator_context_t *this)
{
	allocator_free(this->buffer);
	allocator_free(this);
	return SUCCESS;
}

/*
 * Described in header
 */
static generator_context_t * generator_context_create(generator_t *generator)
{
	private_generator_context_t *this = allocator_alloc_thing(private_generator_context_t);

	if (this == NULL)
	{
		return NULL;
	}

	/* object methods */
	this->public.destroy = (status_t (*) (generator_context_t *this))generator_context_destroy;
	this->make_space_available = generator_context_make_space_available;
	this->write_chunk = generator_context_write_chunk;
	this->write_bytes_to_buffer = generator_context_write_bytes_to_buffer;

	/* allocate memory for buffer */
	this->buffer = allocator_alloc(GENERATOR_DATA_BUFFER_SIZE);
	if (this->buffer == NULL)
	{
		allocator_free(this);
		return NULL;
	}

	/* set private data */
	this->out_position = this->buffer;
	this->roof_position = this->buffer + GENERATOR_DATA_BUFFER_SIZE;
	this->data_struct = NULL;
	this->current_bit = 0;
	return &(this->public);
}


/**
 * Private part of a generator_t object
 */
typedef struct private_generator_s private_generator_t;

struct private_generator_s {
	/**
	 * Public part of a generator_t object
	 */
	 generator_t public;

	/* private functions and fields */

	/**
	 * Generates a chunk_t with specific encoding rules.
	 *
	 * Iems are bytewhise written.
	 *
	 * @param this 					private_generator_t object
	 * @param data_struct 			data_struct to read data from
	 * @param encoding_rules 		pointer to first encoding_rule 
	 * 								of encoding rules array
	 * @param encoding_rules_count 	number of encoding rules 
	 * 								in encoding rules array
	 * @param data 					pointer to chunk_t where to write the data in
	 *
	 * @return 						- SUCCESS if succeeded
	 * 		  						- OUT_OF_RES if out of ressources
	 */
	status_t (*generate) (private_generator_t *this,void * data_struct,encoding_rule_t *encoding_rules, size_t encoding_rules_count, private_generator_context_t *generator_context);

	/**
	 * Generates a U_INT-Field type
	 *
	 * @param this 					private_generator_t object
	 * @param int_type 				type of U_INT field (U_INT_4, U_INT_8, etc.)
	 * @param offset 				offset of value in data struct
	 * @param generator_contexts		generator_contexts_t object where the context is written or read from
	 * @return 						- SUCCESS if succeeded
	 * 		  						- OUT_OF_RES if out of ressources
	 */
	status_t (*generate_u_int_type) (private_generator_t *this,encoding_type_t int_type,u_int32_t offset, private_generator_context_t *generator_context);

	/**
	 * Generates a RESERVED BIT field or a RESERVED BYTE field
	 *
	 * @param this 					private_generator_t object
	 * @param generator_contexts		generator_contexts_t object where the context is written or read from
	 * @param bits 					number of bits to generate
	 * @return 						- SUCCESS if succeeded
	 * 		  						- OUT_OF_RES if out of ressources
	 * 								- FAILED if bit count not supported
	 */
	status_t (*generate_reserved_field) (private_generator_t *this,private_generator_context_t *generator_context,int bits);
	
	/**
	 * Generates a FLAG field
	 *
	 * @param this 					private_generator_t object
	 * @param generator_contexts		generator_contexts_t object where the context is written or read from
	 * @param offset					offset of flag value in data struct
	 * @return 						- SUCCESS if succeeded
	 * 		  						- OUT_OF_RES if out of ressources
	 */
	status_t (*generate_flag) (private_generator_t *this,private_generator_context_t *generator_context,u_int32_t offset);

	/**
	 * Pointer to the payload informations needed to automatic
	 * generate a specific payload type
	 */
	payload_info_t **payload_infos;
};

/**
 * Implements private_generator_t's generate_u_int_type function.
 * See #private_generator_s.generate_u_int_type.
 */
static status_t generate_u_int_type (private_generator_t *this,encoding_type_t int_type,u_int32_t offset,private_generator_context_t *generator_context)
{
	size_t number_of_bits = 0;
	status_t status;


	switch (int_type)
	{
			case U_INT_4:
				number_of_bits = 4;
				break;
			case U_INT_8:
				number_of_bits = 8;
				break;
			case U_INT_16:
				number_of_bits = 16;
				break;
			case U_INT_32:
				number_of_bits = 32;
				break;
			case U_INT_64:
				number_of_bits = 64;
				break;
			default:
			return FAILED;
	}
	if (((number_of_bits % 8) == 0) && (generator_context->current_bit != 0))
	{
		/* current bit has to be zero for values greater then 4 bits */
		return FAILED;
	}

	status = generator_context->make_space_available(generator_context,number_of_bits);

	if (status != SUCCESS)
	{
		return status;
	}

	switch (int_type)
	{
			case U_INT_4:
			{
				if (generator_context->current_bit == 0)
				{
					u_int8_t high_val = *((u_int8_t *)(generator_context->data_struct + offset)) << 4;
					u_int8_t low_val = *(generator_context->out_position) & 0x0F;

					*(generator_context->out_position) = high_val | low_val;
					/* write position is not changed, just bit position is moved */
					generator_context->current_bit = 4;
				}
				else if (generator_context->current_bit == 4)
				{
					u_int high_val = *(generator_context->out_position) & 0xF0;
					u_int low_val = *((u_int8_t *)(generator_context->data_struct + offset)) & 0x0F;
					*(generator_context->out_position) = high_val | low_val;
					generator_context->out_position++;
					generator_context->current_bit = 0;

				}
				else
				{
					/* 4 Bit integers must have a 4 bit alignment */
					return FAILED;
				};
				break;
			}

			case U_INT_8:
			{
				*generator_context->out_position = *((u_int8_t *)(generator_context->data_struct + offset));
				generator_context->out_position++;
				break;

			}
			case U_INT_16:
			{
				u_int16_t int16_val = htons(*((u_int16_t*)(generator_context->data_struct + offset)));
				generator_context->write_bytes_to_buffer(generator_context,&int16_val,sizeof(u_int16_t));

				break;
			}
			case U_INT_32:
			{
				u_int32_t int32_val = htonl(*((u_int32_t*)(generator_context->data_struct + offset)));
				generator_context->write_bytes_to_buffer(generator_context,&int32_val,sizeof(u_int32_t));
				break;
			}
			case U_INT_64:
			{
				u_int32_t int32_val_low = htonl(*((u_int32_t*)(generator_context->data_struct + offset)));
				u_int32_t int32_val_high = htonl(*((u_int32_t*)(generator_context->data_struct + offset) + 1));
				generator_context->write_bytes_to_buffer(generator_context,&int32_val_high,sizeof(u_int32_t));
				generator_context->write_bytes_to_buffer(generator_context,&int32_val_low,sizeof(u_int32_t));
				break;
			}

			default:
			return FAILED;

	}

	return SUCCESS;
}

static status_t generate_reserved_field (private_generator_t *this,private_generator_context_t *generator_context,int bits)
{
	status_t status;
	
	if ((bits != 1) && (bits != 8))
	{
		return FAILED;
	}
	status = generator_context->make_space_available(generator_context,bits);
	if (status != SUCCESS)
	{
		return status;
	}
	
	if (bits == 1)
	{	
		u_int8_t reserved_bit = ~(1 << (7 - generator_context->current_bit));

		*(generator_context->out_position) = *(generator_context->out_position) & reserved_bit;
		generator_context->current_bit++;
		if (generator_context->current_bit >= 8)
		{
			generator_context->current_bit = generator_context->current_bit % 8;
			generator_context->out_position++;
		}
	}
	else
	{
		/* one byte */
		if (generator_context->current_bit > 0)
		{
			return FAILED;
		}
		*(generator_context->out_position) = 0x00;
		generator_context->out_position++;
	}

	return SUCCESS;
		
		
}

static status_t generate_flag (private_generator_t *this,private_generator_context_t *generator_context,u_int32_t offset)
{
	status_t status;
	u_int8_t flag_value = (*((bool *) (generator_context->data_struct + offset))) ? 1 : 0;
	u_int8_t flag = (flag_value << (7 - generator_context->current_bit));
	
	status = generator_context->make_space_available(generator_context,1);
	if (status != SUCCESS)
	{
		return status;
	}

	*(generator_context->out_position) = *(generator_context->out_position) | flag;

	generator_context->current_bit++;
	if (generator_context->current_bit >= 8)
	{
		generator_context->current_bit = generator_context->current_bit % 8;
		generator_context->out_position++;
	}
	return SUCCESS;
}

/**
 * Implements private_generator_t's generate function.
 * See #private_generator_s.generate.
 */
static status_t generate (private_generator_t *this,void * data_struct,encoding_rule_t *encoding_rules, size_t encoding_rules_count, private_generator_context_t *generator_context)
{
	int i;
	status_t status;

	if (generator_context == NULL)
	{
		return OUT_OF_RES;
	}

	for (i = 0; i < encoding_rules_count;i++)
	{
		status = SUCCESS;
		switch (encoding_rules[i].type)
		{
			/* all u int values are generated in generate_u_int_type */
			case U_INT_4:
			case U_INT_8:
			case U_INT_16:
			case U_INT_32:
			case U_INT_64:
				status = this->generate_u_int_type(this,encoding_rules[i].type,encoding_rules[i].offset,generator_context);
				break;
			case RESERVED_BIT:
			{
				status = this->generate_reserved_field(this,generator_context,1);
	
				break;
			}
			case RESERVED_BYTE:
			{
				status = this->generate_reserved_field(this,generator_context,8);
				break;
			} 
			case FLAG:
			{
				status = this->generate_flag(this,generator_context,encoding_rules[i].offset);
				break;
			}
			case LENGTH:
				/* length is generated like an U_INT_32 */
				status = this->generate_u_int_type(this,U_INT_32,encoding_rules[i].offset,generator_context);
				break;
			case SPI_SIZE:
				/* currently not implemented */
			default:
				break;
		}
		if (status != SUCCESS)
		{
			generator_context->public.destroy(&(generator_context->public));
			return status;
		}
	}

//	infos->destroy(infos);
	return status;
}

/**
 * Implements generator_t's generate_payload function.
 * See #generator_s.generate_payload.
 */
static status_t generate_payload (private_generator_t *this,payload_type_t payload_type,void * data_struct,  generator_context_t *generator_context)
{
	int i;
	
	 private_generator_context_t *private_generator_context = (private_generator_context_t *) generator_context;
	 
	 private_generator_context->data_struct = data_struct;

	/* check every payload info for specific type */
	for (i = 0; this->payload_infos[i] != NULL; i++)
	{
		if (this->payload_infos[i]->payload_type == payload_type)
		{
			/* found payload informations, generating is done in private function generate() */
			return (this->generate(this, data_struct,this->payload_infos[i]->ecoding_rules,this->payload_infos[i]->encoding_rules_count,private_generator_context));
		}
	}
	return NOT_SUPPORTED;
}

status_t write_to_chunk (private_generator_t *this,private_generator_context_t *generator_context, chunk_t *data)
{
	return generator_context->write_chunk(generator_context,data);
}

/**
 * Implements generator_t's destroy function.
 * See #generator_s.destroy.
 */
static status_t destroy(private_generator_t *this)
{
	allocator_free(this);
	return SUCCESS;
}

/*
 * Described in header
 */
generator_t * generator_create(payload_info_t ** payload_infos)
{
	private_generator_t *this;

	if (payload_infos == NULL)
	{
		return NULL;
	}

	this = allocator_alloc_thing(private_generator_t);
	if (this == NULL)
	{
		return NULL;
	}

	/* initiate public functions */
	this->public.create_context = (generator_context_t * (*) (generator_t *)) generator_context_create;
	this->public.generate_payload = (status_t(*)(generator_t*, payload_type_t, void *, generator_context_t *)) generate_payload;
	this->public.destroy = (status_t(*)(generator_t*)) destroy;
	this->public.write_to_chunk = (status_t (*) (generator_t *,generator_context_t *, chunk_t *)) write_to_chunk;
	/* initiate private functions */
	this->generate = generate;
	this->generate_u_int_type = generate_u_int_type;
	this->generate_reserved_field = generate_reserved_field;
	this->generate_flag = generate_flag;

	/* initiate private variables */
	this->payload_infos = payload_infos;

	return &(this->public);
}