5d852c50bb025ac6071c7ec470dd57f9582615e5
[strongswan.git] / Source / charon / generator.c
1 /**
2 * @file generator.c
3 *
4 * @brief Generic generator class used to generate IKEv2-Header and Payload
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "allocator.h"
27 #include "types.h"
28 #include "generator.h"
29
30
31 /**
32 * buffer_t: used for geneartor operations
33 */
34 typedef struct generator_infos_s generator_infos_t;
35
36 struct generator_infos_s {
37
38 /**
39 * Buffer used to generate to
40 */
41 u_int8_t *buffer;
42
43 /**
44 * current write position in buffer (one byte alligned)
45 */
46 u_int8_t *out_position;
47
48 /**
49 * position of last byte in buffer
50 */
51 u_int8_t *roof_position;
52
53 /**
54 * Current bit writing to
55 */
56 size_t current_bit;
57
58 /**
59 * Associated data struct to read informations from
60 */
61 void * data_struct;
62 /**
63 * @brief Destroys a generator_infos_t object
64 *
65 * @param generator_infos_t generator_infos_t object
66 * @return SUCCESSFUL if succeeded, FAILED otherwise
67 */
68 status_t (*destroy) (generator_infos_t *this);
69
70 /**
71 * Checks if enough space is available in buffer and if not,
72 * the buffer size is increased until at least the asked amount of space
73 * is available
74 *
75 * @param bits number of bits to make at leas available in buffer
76 * @param generator_infos_t generator_infos_t object
77 * @return SUCCESSFUL if succeeded, OUT_OF_RES otherwise
78 */
79 status_t (*make_space_available) (generator_infos_t *this,size_t bits);
80
81 status_t (*write_bytes_to_buffer) (generator_infos_t *this,void * bytes,size_t number_of_bytes);
82
83 status_t (*write_chunk) (generator_infos_t *this,chunk_t *data);
84 };
85
86 /**
87 * implements generator_infos_t's increase_buffer function
88 */
89 static status_t generator_info_make_space_available (generator_infos_t *this, size_t bits)
90 {
91 size_t free_bits = ((this->roof_position - this->out_position) * 8) - this->current_bit;
92
93 while (free_bits < bits)
94 {
95 size_t old_buffer_size = ((this->roof_position) - ( this->buffer));
96 size_t new_buffer_size = old_buffer_size + GENERATOR_DATA_BUFFER_INCREASE_VALUE;
97 size_t out_position_offset = ((this->out_position) - (this->buffer));
98 u_int8_t *new_buffer;
99
100 new_buffer = allocator_realloc(this->buffer,new_buffer_size);
101 if (new_buffer == NULL)
102 {
103 return OUT_OF_RES;
104 }
105
106 this->buffer = new_buffer;
107
108 this->out_position = (this->buffer + out_position_offset);
109 this->roof_position = (this->buffer + new_buffer_size);
110 }
111
112 return SUCCESS;
113 }
114
115 static status_t generator_info_write_bytes_to_buffer (generator_infos_t *this,void * bytes,size_t number_of_bytes)
116 {
117 u_int8_t *read_position = (u_int8_t *) bytes;
118 int i;
119 status_t status;
120
121 status = this->make_space_available(this,number_of_bytes * 8);
122
123 if (status != SUCCESS)
124 {
125 return status;
126 }
127
128 for (i = 0; i < number_of_bytes; i++)
129 {
130 *(this->out_position) = *(read_position);
131 read_position++;
132 this->out_position++;
133 }
134 return status;
135 }
136
137 static status_t generator_infos_write_chunk (generator_infos_t *this,chunk_t *data)
138 {
139 size_t data_length = this->out_position - this->buffer;
140 if (this->current_bit > 0)
141 data_length++;
142 data->ptr = allocator_alloc(data_length);
143 if (data->ptr == NULL)
144 {
145 data->len = 0;
146 return OUT_OF_RES;
147 }
148 memcpy(data->ptr,this->buffer,data_length);
149 data->len = data_length;
150 return SUCCESS;
151 }
152
153
154 static status_t generator_infos_destroy (generator_infos_t *this)
155 {
156 if (this == NULL)
157 {
158 return FAILED;
159 }
160 allocator_free(this->buffer);
161 allocator_free(this);
162 return SUCCESS;
163 }
164
165 /**
166 * Creates a generator_infos_t-object holding necessary informations
167 * for generating (buffer, data_struct, etc)
168 *
169 * @param data_struct where to read the data out
170 */
171 generator_infos_t * generator_infos_create(void *data_struct)
172 {
173 generator_infos_t *this = allocator_alloc_thing(generator_infos_t);
174
175 if (this == NULL)
176 {
177 return NULL;
178 }
179
180 /* object methods */
181 this->destroy = generator_infos_destroy;
182 this->make_space_available = generator_info_make_space_available;
183 this->write_chunk = generator_infos_write_chunk;
184 this->write_bytes_to_buffer = generator_info_write_bytes_to_buffer;
185
186 /* allocate memory for buffer */
187 this->buffer = allocator_alloc(GENERATOR_DATA_BUFFER_SIZE);
188 if (this->buffer == NULL)
189 {
190 allocator_free(this);
191 return NULL;
192 }
193
194 /* set private data */
195 this->out_position = this->buffer;
196 this->roof_position = this->buffer + GENERATOR_DATA_BUFFER_SIZE;
197 this->data_struct = data_struct;
198 this->current_bit = 0;
199 return (this);
200 }
201
202
203
204 /**
205 * Private data of a generator_t object
206 */
207 typedef struct private_generator_s private_generator_t;
208
209 struct private_generator_s {
210 /**
211 * Public part of a generator object
212 */
213 generator_t public;
214
215 /* private functions and fields */
216
217 /**
218 * Generates a chunk_t with specific encoding rules
219 *
220 * items are bytewhise written
221 *
222 * @param this private_generator_t-object
223 * @param data_struct data_struct to read data from
224 * @param encoding_rules pointer to first encoding_rule of encoding rules array
225 * @param encoding_rules_count number of encoding rules in encoding rules array
226 * @param data pointer to chunk where to write the data in
227 *
228 * @return SUCCESS if succeeded,
229 * OUT_OF_RES if out of ressources
230 */
231 status_t (*generate) (private_generator_t *this,void * data_struct,encoding_rule_t *encoding_rules, size_t encoding_rules_count, chunk_t *data);
232
233 /**
234 * TODO
235 */
236 status_t (*generate_u_int_type) (private_generator_t *this,encoding_type_t int_type,u_int32_t offset, generator_infos_t *generator_infos);
237
238 /**
239 * Pointer to the payload informations needed to automatic
240 * generate a specific payload type
241 */
242 payload_info_t **payload_infos;
243 };
244
245 /**
246 * implements private_generator_t's double_buffer function
247 */
248 static status_t generate_u_int_type (private_generator_t *this,encoding_type_t int_type,u_int32_t offset,generator_infos_t *generator_infos)
249 {
250 size_t number_of_bits = 0;
251 status_t status;
252
253
254 switch (int_type)
255 {
256 case U_INT_4:
257 number_of_bits = 4;
258 break;
259 case U_INT_8:
260 number_of_bits = 8;
261 break;
262 case U_INT_16:
263 number_of_bits = 16;
264 break;
265 case U_INT_32:
266 number_of_bits = 32;
267 break;
268 case U_INT_64:
269 number_of_bits = 64;
270 break;
271 default:
272 return FAILED;
273 }
274 if (((number_of_bits % 8) == 0) && (generator_infos->current_bit != 0))
275 {
276 /* current bit has to be zero for values greater then 4 bits */
277 return FAILED;
278 }
279
280 status = generator_infos->make_space_available(generator_infos,number_of_bits);
281
282 if (status != SUCCESS)
283 {
284 return status;
285 }
286
287 switch (int_type)
288 {
289 case U_INT_4:
290 {
291 if (generator_infos->current_bit == 0)
292 {
293 u_int8_t high_val = *((u_int8_t *)(generator_infos->data_struct + offset)) << 4;
294 u_int8_t low_val = *(generator_infos->out_position) & 0x0F;
295
296 *(generator_infos->out_position) = high_val | low_val;
297 /* write position is not changed, just bit position is moved */
298 generator_infos->current_bit = 4;
299 }
300 else if (generator_infos->current_bit == 4)
301 {
302 u_int high_val = *(generator_infos->out_position) & 0xF0;
303 u_int low_val = *((u_int8_t *)(generator_infos->data_struct + offset)) & 0x0F;
304 *(generator_infos->out_position) = high_val | low_val;
305 generator_infos->out_position++;
306 generator_infos->current_bit = 0;
307
308 }
309 else
310 {
311 /* 4 Bit integers must have a 4 bit alignment */
312 return FAILED;
313 };
314 break;
315 }
316
317 case U_INT_8:
318 {
319 *generator_infos->out_position = *((u_int8_t *)(generator_infos->data_struct + offset));
320 generator_infos->out_position++;
321 break;
322
323 }
324 case U_INT_16:
325 {
326 u_int16_t int16_val = htons(*((u_int16_t*)(generator_infos->data_struct + offset)));
327 generator_infos->write_bytes_to_buffer(generator_infos,&int16_val,sizeof(u_int16_t));
328
329 break;
330 }
331 case U_INT_32:
332 {
333 u_int32_t int32_val = htonl(*((u_int32_t*)(generator_infos->data_struct + offset)));
334 generator_infos->write_bytes_to_buffer(generator_infos,&int32_val,sizeof(u_int32_t));
335 break;
336 }
337 case U_INT_64:
338 {
339 u_int32_t int32_val_low = htonl(*((u_int32_t*)(generator_infos->data_struct + offset)));
340 u_int32_t int32_val_high = htonl(*((u_int32_t*)(generator_infos->data_struct + offset) + 1));
341 generator_infos->write_bytes_to_buffer(generator_infos,&int32_val_high,sizeof(u_int32_t));
342 generator_infos->write_bytes_to_buffer(generator_infos,&int32_val_low,sizeof(u_int32_t));
343 break;
344 }
345
346 default:
347 return FAILED;
348
349 }
350
351 return SUCCESS;
352 }
353
354 /**
355 * implements private_generator_t's generate function
356 */
357 static status_t generate (private_generator_t *this,void * data_struct,encoding_rule_t *encoding_rules, size_t encoding_rules_count, chunk_t *data)
358 {
359 int i;
360 status_t status;
361
362
363 generator_infos_t *infos = generator_infos_create(data_struct);
364
365 if (infos == NULL)
366 {
367 return OUT_OF_RES;
368 }
369
370
371 for (i = 0; i < encoding_rules_count;i++)
372 {
373 status = SUCCESS;
374 switch (encoding_rules[i].type)
375 {
376 /* all u int values are generated in generate_u_int_type */
377 case U_INT_4:
378 case U_INT_8:
379 case U_INT_16:
380 case U_INT_32:
381 case U_INT_64:
382 status = this->generate_u_int_type(this,encoding_rules[i].type,encoding_rules[i].offset,infos);
383 break;
384 case RESERVED_BIT:
385 {
386 u_int8_t reserved_bit = ~(1 << (7 - infos->current_bit));
387
388 *(infos->out_position) = *(infos->out_position) & reserved_bit;
389
390 infos->current_bit++;
391 if (infos->current_bit >= 8)
392 {
393 infos->current_bit = infos->current_bit % 8;
394 status = infos->make_space_available(infos,8);
395 infos->out_position++;
396 }
397 break;
398 }
399 case RESERVED_BYTE:
400 {
401 if (infos->current_bit > 0)
402 {
403 return FAILED;
404 }
405 *(infos->out_position) = 0x00;
406 infos->out_position++;
407 break;
408 }
409 case FLAG:
410 {
411 u_int8_t flag_value = (*((bool *) (infos->data_struct + encoding_rules[i].offset))) ? 1 : 0;
412 u_int8_t flag = (flag_value << (7 - infos->current_bit));
413
414 *(infos->out_position) = *(infos->out_position) | flag;
415
416 infos->current_bit++;
417 if (infos->current_bit >= 8)
418 {
419 infos->current_bit = infos->current_bit % 8;
420 status = infos->make_space_available(infos,8);
421 infos->out_position++;
422 }
423 break;
424 }
425 case LENGTH:
426 /* length is generated like an U_INT_32 */
427 status = this->generate_u_int_type(this,U_INT_32,encoding_rules[i].offset,infos);
428 case SPI_SIZE:
429 /* actually not implemented */
430 default:
431 break;
432 }
433 if (status != SUCCESS)
434 {
435 infos->destroy(infos);
436 return status;
437 }
438 }
439
440
441 status = infos->write_chunk(infos,data);
442 infos->destroy(infos);
443 return status;
444 }
445
446 static status_t generate_payload (private_generator_t *this,payload_type_t payload_type,void * data_struct, chunk_t *data)
447 {
448 int i;
449
450 /* check every payload info for specific type */
451 for (i = 0; this->payload_infos[i] != NULL; i++)
452 {
453 if (this->payload_infos[i]->payload_type == payload_type)
454 {
455 /* found payload informations, generating is done in private function generate() */
456 return (this->generate(this, data_struct,this->payload_infos[i]->ecoding_rules,this->payload_infos[i]->encoding_rules_count,data));
457 }
458 }
459 return NOT_SUPPORTED;
460 }
461
462 /**
463 * Implementation of generator_t's destroy function
464 */
465 static status_t destroy(private_generator_t *this)
466 {
467 if (this == NULL)
468 {
469 return FAILED;
470 }
471
472 allocator_free(this);
473 return SUCCESS;
474 }
475
476 /*
477 * Described in header
478 */
479 generator_t * generator_create(payload_info_t ** payload_infos)
480 {
481 private_generator_t *this;
482
483 if (payload_infos == NULL)
484 {
485 return NULL;
486 }
487
488 this = allocator_alloc_thing(private_generator_t);
489 if (this == NULL)
490 {
491 return NULL;
492 }
493
494 /* initiate public functions */
495 this->public.generate_payload = (status_t(*)(generator_t*, payload_type_t, void *, chunk_t *)) generate_payload;
496 this->public.destroy = (status_t(*)(generator_t*)) destroy;
497
498 /* initiate private functions */
499 this->generate = generate;
500 this->generate_u_int_type = generate_u_int_type;
501
502 /* initiate private variables */
503 this->payload_infos = payload_infos;
504
505 return &(this->public);
506 }