5cb92d8f13af2e1bf0c30e2132ca9008cb068797
[strongswan.git] / Source / charon / encoding / generator.c
1 /**
2 * @file generator.c
3 *
4 * @brief Generic generator class used to generate IKEv2-header and payloads.
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 #include <arpa/inet.h>
26 #include <stdio.h>
27
28
29 #include "generator.h"
30
31 #include <types.h>
32 #include <globals.h>
33 #include <utils/allocator.h>
34 #include <utils/linked_list.h>
35 #include <utils/logger_manager.h>
36 #include <encoding/payloads/payload.h>
37 #include <encoding/payloads/proposal_substructure.h>
38 #include <encoding/payloads/transform_substructure.h>
39 #include <encoding/payloads/sa_payload.h>
40 #include <encoding/payloads/ke_payload.h>
41 #include <encoding/payloads/notify_payload.h>
42 #include <encoding/payloads/nonce_payload.h>
43
44
45 typedef struct private_generator_t private_generator_t;
46
47 /**
48 * Private part of a generator_t object
49 */
50 struct private_generator_t {
51 /**
52 * Public part of a generator_t object
53 */
54 generator_t public;
55
56 /* private functions and fields */
57
58
59 /**
60 * Generates a U_INT-Field type and writes it to buffer.
61 *
62 * @param this private_generator_t object
63 * @param int_type type of U_INT field (U_INT_4, U_INT_8, etc.)
64 * ATTRIBUTE_TYPE is also generated in this function
65 * @param offset offset of value in data struct
66 * @param generator_contexts generator_contexts_t object where the context is written or read from
67 * @return - SUCCESS if succeeded
68 * - OUT_OF_RES if out of ressources
69 */
70 status_t (*generate_u_int_type) (private_generator_t *this,encoding_type_t int_type,u_int32_t offset);
71
72 /**
73 * Get size of current buffer in bytes.
74 *
75 * @param this private_generator_t object
76 * @return Size of buffer in bytes
77 */
78 size_t (*get_current_buffer_size) (private_generator_t *this);
79
80 /**
81 * Get free space of current buffer in bytes.
82 *
83 * @param this private_generator_t object
84 * @return space in buffer in bytes
85 */
86 size_t (*get_current_buffer_space) (private_generator_t *this);
87
88 /**
89 * Get length of data in buffer (in bytes).
90 *
91 * @param this private_generator_t object
92 * @return length of data in bytes
93 */
94 size_t (*get_current_data_length) (private_generator_t *this);
95
96 /**
97 * Get current offset in buffer (in bytes).
98 *
99 * @param this private_generator_t object
100 * @return offset in bytes
101 */
102 u_int32_t (*get_current_buffer_offset) (private_generator_t *this);
103
104 /**
105 * Generates a RESERVED BIT field or a RESERVED BYTE field and writes
106 * it to the buffer.
107 *
108 * @param this private_generator_t object
109 * @param generator_contexts generator_contexts_t object where the context is written or read from
110 * @param bits number of bits to generate
111 * @return - SUCCESS if succeeded
112 * - OUT_OF_RES if out of ressources
113 * - FAILED if bit count not supported
114 */
115 status_t (*generate_reserved_field) (private_generator_t *this,int bits);
116
117 /**
118 * Generates a FLAG field
119 *
120 * @param this private_generator_t object
121 * @param generator_contexts generator_contexts_t object where the context is written or read from
122 * @param offset offset of flag value in data struct
123 * @return - SUCCESS if succeeded
124 * - OUT_OF_RES if out of ressources
125 */
126 status_t (*generate_flag) (private_generator_t *this,u_int32_t offset);
127
128 /**
129 * Writes the current buffer content into a chunk_t
130 *
131 * Memory of specific chunk_t gets allocated.
132 *
133 * @param this calling private_generator_t object
134 * @param data pointer of chunk_t to write to
135 * @return
136 * - SUCCESSFUL if succeeded
137 * - OUT_OF_RES otherwise
138 */
139 status_t (*write_chunk) (private_generator_t *this,chunk_t *data);
140
141 /**
142 * Generates a bytestream from a chunk_t
143 *
144 * @param this private_generator_t object
145 * @param offset offset of chunk_t value in data struct
146 * @return - SUCCESS if succeeded
147 * - OUT_OF_RES if out of ressources
148 */
149 status_t (*generate_from_chunk) (private_generator_t *this,u_int32_t offset);
150
151 /**
152 * Makes sure enough space is available in buffer to store amount of bits.
153 *
154 * If buffer is to small to hold the specific amount of bits it
155 * is increased using reallocation function of allocator.
156 *
157 * @param this calling private_generator_t object
158 * @param bits number of bits to make available in buffer
159 * @return
160 * - SUCCESSFUL if succeeded
161 * - OUT_OF_RES otherwise
162 */
163 status_t (*make_space_available) (private_generator_t *this,size_t bits);
164
165 /**
166 * Writes a specific amount of byte into the buffer.
167 *
168 * If buffer is to small to hold the specific amount of bytes it
169 * is increased.
170 *
171 * @param this calling private_generator_t object
172 * @param bytes pointer to bytes to write
173 * @param number_of_bytes number of bytes to write into buffer
174 * @return
175 * - SUCCESSFUL if succeeded
176 * - OUT_OF_RES otherwise
177 */
178 status_t (*write_bytes_to_buffer) (private_generator_t *this,void * bytes,size_t number_of_bytes);
179
180
181 /**
182 * Writes a specific amount of byte into the buffer at a specific offset.
183 *
184 * @warning buffer size is not check to hold the data if offset is to large.
185 *
186 * @param this calling private_generator_t object
187 * @param bytes pointer to bytes to write
188 * @param number_of_bytes number of bytes to write into buffer
189 * @param offset offset to write the data into
190 * @return
191 * - SUCCESSFUL if succeeded
192 * - OUT_OF_RES otherwise
193 */
194 status_t (*write_bytes_to_buffer_at_offset) (private_generator_t *this,void * bytes,size_t number_of_bytes,u_int32_t offset);
195
196 /**
197 * Buffer used to generate the data into.
198 */
199 u_int8_t *buffer;
200
201 /**
202 * Current write position in buffer (one byte aligned).
203 */
204 u_int8_t *out_position;
205
206 /**
207 * Position of last byte in buffer.
208 */
209 u_int8_t *roof_position;
210
211 /**
212 * Current bit writing to in current byte (between 0 and 7).
213 */
214 size_t current_bit;
215
216 /**
217 * Associated data struct to read informations from.
218 */
219 void * data_struct;
220
221 /*
222 * Last payload length position offset in the buffer
223 */
224 u_int32_t last_payload_length_position_offset;
225
226 /**
227 * Offset of the header length field in the buffer
228 */
229 u_int32_t header_length_position_offset;
230
231 /**
232 * Last SPI size
233 */
234 u_int8_t last_spi_size;
235
236 /*
237 * Attribute format of the last generated transform attribute
238 *
239 * Used to check if a variable value field is used or not for
240 * the transform attribute value.
241 */
242 bool attribute_format;
243
244 /*
245 * Depending on the value of attribute_format this field is used
246 * to hold the length of the transform attribute in bytes
247 */
248 u_int16_t attribute_length;
249
250 /**
251 * Associated Logger
252 */
253 logger_t *logger;
254 };
255
256 /**
257 * Implements private_generator_t's get_current_buffer_size function.
258 * See #private_generator_s.get_current_buffer_size.
259 */
260 static size_t get_current_buffer_size (private_generator_t *this)
261 {
262 return ((this->roof_position) - (this->buffer));
263 }
264
265 /**
266 * Implements private_generator_t's get_current_buffer_space function.
267 * See #private_generator_s.get_current_buffer_space.
268 */
269 static size_t get_current_buffer_space (private_generator_t *this)
270 {
271 /* we know, one byte more */
272 size_t space = (this->roof_position) - (this->out_position);
273 return (space);
274 }
275
276 /**
277 * Implements private_generator_t's get_current_buffer_space function.
278 * See #private_generator_s.get_current_buffer_space.
279 */
280 static size_t get_current_data_length (private_generator_t *this)
281 {
282 return (this->out_position - this->buffer);
283 }
284
285 /**
286 * Implements private_generator_t's get_current_buffer_offset function.
287 * See #private_generator_s.get_current_buffer_offset.
288 */
289 static u_int32_t get_current_buffer_offset (private_generator_t *this)
290 {
291 return (this->out_position - this->buffer);
292 }
293
294
295 /**
296 * Implements private_generator_t's generate_u_int_type function.
297 * See #private_generator_s.generate_u_int_type.
298 */
299 static status_t generate_u_int_type (private_generator_t *this,encoding_type_t int_type,u_int32_t offset)
300 {
301 size_t number_of_bits = 0;
302 status_t status;
303
304 /* find out number of bits of each U_INT type to check for enough space
305 in buffer */
306 switch (int_type)
307 {
308 case U_INT_4:
309 number_of_bits = 4;
310 break;
311 case U_INT_8:
312 number_of_bits = 8;
313 break;
314 case U_INT_16:
315 number_of_bits = 16;
316 break;
317 case U_INT_32:
318 number_of_bits = 32;
319 break;
320 case U_INT_64:
321 number_of_bits = 64;
322 break;
323 case ATTRIBUTE_TYPE:
324 number_of_bits = 15;
325 break;
326 case IKE_SPI:
327 number_of_bits = 64;
328 break;
329
330 default:
331 return FAILED;
332 }
333 /* U_INT Types of multiple then 8 bits must be aligned */
334 if (((number_of_bits % 8) == 0) && (this->current_bit != 0))
335 {
336 this->logger->log(this->logger, ERROR, "U_INT Type %s is not 8 Bit aligned",
337 mapping_find(encoding_type_m,int_type));
338 /* current bit has to be zero for values multiple of 8 bits */
339 return FAILED;
340 }
341
342 /* make sure enough space is available in buffer */
343 status = this->make_space_available(this,number_of_bits);
344 if (status != SUCCESS)
345 {
346 return status;
347 }
348 /* now handle each u int type differently */
349 switch (int_type)
350 {
351 case U_INT_4:
352 {
353 if (this->current_bit == 0)
354 {
355 /* highval of current byte in buffer has to be set to the new value*/
356 u_int8_t high_val = *((u_int8_t *)(this->data_struct + offset)) << 4;
357 /* lowval in buffer is not changed */
358 u_int8_t low_val = *(this->out_position) & 0x0F;
359 /* highval is set, low_val is not changed */
360 *(this->out_position) = high_val | low_val;
361 this->logger->log(this->logger, RAW|MOST, " => 0x%x", *(this->out_position));
362 /* write position is not changed, just bit position is moved */
363 this->current_bit = 4;
364 }
365 else if (this->current_bit == 4)
366 {
367 /* highval in buffer is not changed */
368 u_int high_val = *(this->out_position) & 0xF0;
369 /* lowval of current byte in buffer has to be set to the new value*/
370 u_int low_val = *((u_int8_t *)(this->data_struct + offset)) & 0x0F;
371 *(this->out_position) = high_val | low_val;
372 this->logger->log(this->logger, RAW|MOST, " => 0x%x", *(this->out_position));
373 this->out_position++;
374 this->current_bit = 0;
375
376 }
377 else
378 {
379 this->logger->log(this->logger, ERROR, "U_INT_4 Type is not 4 Bit aligned");
380 /* 4 Bit integers must have a 4 bit alignment */
381 return FAILED;
382 };
383 break;
384 }
385 case U_INT_8:
386 {
387 /* 8 bit values are written as they are */
388 *this->out_position = *((u_int8_t *)(this->data_struct + offset));
389 this->logger->log(this->logger, RAW|MOST, " => 0x%x", *(this->out_position));
390 this->out_position++;
391 break;
392
393 }
394 case ATTRIBUTE_TYPE:
395 {
396 /* attribute type must not change first bit uf current byte ! */
397 if (this->current_bit != 1)
398 {
399 this->logger->log(this->logger, ERROR, "ATTRIBUTE FORMAT flag is not set");
400 /* first bit has to be set! */
401 return FAILED;
402 }
403 /* get value of attribute format flag */
404 u_int8_t attribute_format_flag = *(this->out_position) & 0x80;
405 /* get attribute type value as 16 bit integer*/
406 u_int16_t int16_val = htons(*((u_int16_t*)(this->data_struct + offset)));
407 /* last bit must be unset */
408 int16_val = int16_val & 0xFF7F;
409
410 int16_val = int16_val | attribute_format_flag;
411 this->logger->log(this->logger, RAW|MOST, " => 0x%x", int16_val);
412 /* write bytes to buffer (set bit is overwritten)*/
413 this->write_bytes_to_buffer(this,&int16_val,sizeof(u_int16_t));
414 this->current_bit = 0;
415 break;
416
417 }
418 case U_INT_16:
419 {
420 u_int16_t int16_val = htons(*((u_int16_t*)(this->data_struct + offset)));
421 this->logger->log_bytes(this->logger, RAW|MOST, " =>", (void*)&int16_val, sizeof(int16_val));
422 this->write_bytes_to_buffer(this,&int16_val,sizeof(u_int16_t));
423 break;
424 }
425 case U_INT_32:
426 {
427 u_int32_t int32_val = htonl(*((u_int32_t*)(this->data_struct + offset)));
428 this->logger->log_bytes(this->logger, RAW|MOST, " =>", (void*)&int32_val, sizeof(int32_val));
429 this->write_bytes_to_buffer(this,&int32_val,sizeof(u_int32_t));
430 break;
431 }
432 case U_INT_64:
433 {
434 /* 64 bit integers are written as two 32 bit integers */
435 u_int32_t int32_val_low = htonl(*((u_int32_t*)(this->data_struct + offset)));
436 u_int32_t int32_val_high = htonl(*((u_int32_t*)(this->data_struct + offset) + 1));
437 this->logger->log_bytes(this->logger, RAW|MOST, " => (low)", (void*)&int32_val_low, sizeof(int32_val_low));
438 this->logger->log_bytes(this->logger, RAW|MOST, " => (high)", (void*)&int32_val_high, sizeof(int32_val_high));
439 /* TODO add support for big endian machines */
440 this->write_bytes_to_buffer(this,&int32_val_high,sizeof(u_int32_t));
441 this->write_bytes_to_buffer(this,&int32_val_low,sizeof(u_int32_t));
442 break;
443 }
444
445 case IKE_SPI:
446 {
447 /* 64 bit are written as they come :-) */
448 this->write_bytes_to_buffer(this,(this->data_struct + offset),sizeof(u_int64_t));
449 this->logger->log_bytes(this->logger, RAW|MOST, " =>", (void*)(this->data_struct + offset), sizeof(u_int64_t));
450 break;
451 }
452
453 default:
454 this->logger->log(this->logger, ERROR, "U_INT Type %s is not supported", mapping_find(encoding_type_m,int_type));
455 return FAILED;
456 }
457 return SUCCESS;
458 }
459
460 /**
461 * Implements private_generator_t's generate_reserved_field function.
462 * See #private_generator_s.generate_reserved_field.
463 */
464 static status_t generate_reserved_field(private_generator_t *this,int bits)
465 {
466 status_t status;
467
468 /* only one bit or 8 bit fields are supported */
469 if ((bits != 1) && (bits != 8))
470 {
471 this->logger->log(this->logger, ERROR, "Reserved field of %d bits cannot be generated", bits);
472 return FAILED;
473 }
474 /* make sure enough space is available in buffer */
475 status = this->make_space_available(this,bits);
476 if (status != SUCCESS)
477 {
478 return status;
479 }
480
481 if (bits == 1)
482 {
483 /* one bit processing */
484 u_int8_t reserved_bit = ~(1 << (7 - this->current_bit));
485 *(this->out_position) = *(this->out_position) & reserved_bit;
486 if (this->current_bit == 0)
487 {
488 /* memory must be zero */
489 *(this->out_position) = 0x00;
490 }
491
492
493 this->current_bit++;
494 if (this->current_bit >= 8)
495 {
496 this->current_bit = this->current_bit % 8;
497 this->out_position++;
498 }
499 }
500 else
501 {
502 /* one byte processing*/
503 if (this->current_bit > 0)
504 {
505 this->logger->log(this->logger, ERROR,
506 "Reserved field cannot be written cause allignement of current bit is %d",
507 this->current_bit);
508 return FAILED;
509 }
510 *(this->out_position) = 0x00;
511 this->out_position++;
512 }
513
514 return SUCCESS;
515
516
517 }
518
519 /**
520 * Implements private_generator_t's generate_flag function.
521 * See #private_generator_s.generate_flag.
522 */
523 static status_t generate_flag (private_generator_t *this,u_int32_t offset)
524 {
525 status_t status;
526 /* value of current flag */
527 u_int8_t flag_value;
528 /* position of flag in current byte */
529 u_int8_t flag;
530
531 /* if the value in the data_struct is TRUE, flag_value is set to 1, 0 otherwise */
532 flag_value = (*((bool *) (this->data_struct + offset))) ? 1 : 0;
533 /* get flag position */
534 flag = (flag_value << (7 - this->current_bit));
535
536 /* make sure one bit is available in buffer */
537 status = this->make_space_available(this,1);
538 if (status != SUCCESS)
539 {
540 return status;
541 }
542 if (this->current_bit == 0)
543 {
544 /* memory must be zero */
545 *(this->out_position) = 0x00;
546 }
547
548 *(this->out_position) = *(this->out_position) | flag;
549
550
551 this->logger->log(this->logger, RAW|MOST, " => 0x0%x", *(this->out_position));
552
553 this->current_bit++;
554 if (this->current_bit >= 8)
555 {
556 this->current_bit = this->current_bit % 8;
557 this->out_position++;
558 }
559 return SUCCESS;
560 }
561
562 /**
563 * Implements private_generator_t's generate_from_chunk function.
564 * See #private_generator_s.generate_from_chunk.
565 */
566 static status_t generate_from_chunk (private_generator_t *this,u_int32_t offset)
567 {
568 if (this->current_bit != 0)
569 {
570 this->logger->log(this->logger, ERROR, "can not generate a chunk at Bitpos %d", this->current_bit);
571 return FAILED;
572 }
573
574 /* position in buffer */
575 chunk_t *attribute_value = (chunk_t *)(this->data_struct + offset);
576
577 this->logger->log_chunk(this->logger, RAW|MOST, " =>", attribute_value);
578
579 /* use write_bytes_to_buffer function to do the job */
580 return this->write_bytes_to_buffer(this,attribute_value->ptr,attribute_value->len);
581
582 }
583
584 /**
585 * Implements private_generator_t's generator_context_make_space_available function.
586 * See #private_generator_s.generator_context_make_space_available.
587 */
588 static status_t make_space_available (private_generator_t *this, size_t bits)
589 {
590 while (((this->get_current_buffer_space(this) * 8) - this->current_bit) < bits)
591 {
592 /* must increase buffer */
593 u_int8_t *new_buffer;
594 size_t old_buffer_size = this->get_current_buffer_size(this);
595 size_t new_buffer_size = old_buffer_size + GENERATOR_DATA_BUFFER_INCREASE_VALUE;
596 size_t out_position_offset = ((this->out_position) - (this->buffer));
597
598 this->logger->log(this->logger, CONTROL|MOST, "increased gen buffer from %d to %d byte",
599 old_buffer_size, new_buffer_size);
600
601 /* Reallocate space for new buffer */
602 new_buffer = allocator_realloc(this->buffer,new_buffer_size);
603 if (new_buffer == NULL)
604 {
605 this->logger->log(this->logger, ERROR, "reallocation of gen buffer failed!!!");
606 return OUT_OF_RES;
607 }
608
609 this->buffer = new_buffer;
610
611 this->out_position = (this->buffer + out_position_offset);
612 this->roof_position = (this->buffer + new_buffer_size);
613 }
614 return SUCCESS;
615 }
616
617 /**
618 * Implements private_generator_t's write_bytes_to_buffer function.
619 * See #private_generator_s.write_bytes_to_buffer.
620 */
621 static status_t write_bytes_to_buffer (private_generator_t *this,void * bytes, size_t number_of_bytes)
622 {
623 int i;
624 status_t status;
625 u_int8_t *read_position = (u_int8_t *) bytes;
626
627 status = this->make_space_available(this,number_of_bytes * 8);
628 if (status != SUCCESS)
629 {
630 return status;
631 }
632
633 for (i = 0; i < number_of_bytes; i++)
634 {
635 *(this->out_position) = *(read_position);
636 read_position++;
637 this->out_position++;
638 }
639 return status;
640 }
641
642 /**
643 * Implements private_generator_t's write_bytes_to_buffer_at_offset function.
644 * See #private_generator_s.write_bytes_to_buffer_at_offset.
645 */
646 static status_t write_bytes_to_buffer_at_offset (private_generator_t *this,void * bytes,size_t number_of_bytes,u_int32_t offset)
647 {
648 int i;
649 status_t status;
650 u_int8_t *read_position = (u_int8_t *) bytes;
651 u_int8_t *write_position;
652 u_int32_t free_space_after_offset = (this->get_current_buffer_size(this) - offset);
653
654 /* check first if enough space for new data is available */
655 if (number_of_bytes > free_space_after_offset)
656 {
657 status = this->make_space_available(this,(number_of_bytes - free_space_after_offset) * 8);
658 }
659
660 write_position = this->buffer + offset;
661 for (i = 0; i < number_of_bytes; i++)
662 {
663 *(write_position) = *(read_position);
664 read_position++;
665 write_position++;
666 }
667 return SUCCESS;
668 }
669
670 /**
671 * Implements generator_t's write_chunk function.
672 * See #generator_s.write_chunk.
673 */
674 static status_t write_to_chunk (private_generator_t *this,chunk_t *data)
675 {
676 size_t data_length = this->get_current_data_length(this);
677 u_int32_t header_length_field = data_length;
678
679 /* write length into header length field */
680 if (this->header_length_position_offset > 0)
681 {
682 u_int32_t int32_val = htonl(header_length_field);
683 this->write_bytes_to_buffer_at_offset(this,&int32_val,sizeof(u_int32_t),this->header_length_position_offset);
684 }
685
686 if (this->current_bit > 0)
687 data_length++;
688 data->ptr = allocator_alloc(data_length);
689 if (data->ptr == NULL)
690 {
691 data->len = 0;
692 this->logger->log(this->logger, ERROR, "not enougth ressources to allocate chunk");
693 return OUT_OF_RES;
694 }
695 memcpy(data->ptr,this->buffer,data_length);
696 data->len = data_length;
697
698 this->logger->log_chunk(this->logger, RAW, "generated data of this parser", data);
699
700 return SUCCESS;
701 }
702
703 /**
704 * Implements generator_t's generate_payload function.
705 * See #generator_s.generate_payload.
706 */
707 static status_t generate_payload (private_generator_t *this,payload_t *payload)
708 {
709 int i;
710 status_t status;
711 this->data_struct = payload;
712 size_t rule_count;
713 encoding_rule_t *rules;
714 payload_type_t payload_type;
715 u_int8_t *payload_start;
716
717 /* get payload type */
718 payload_type = payload->get_type(payload);
719 /* spi size has to get reseted */
720 this->last_spi_size = 0;
721
722 payload_start = this->out_position;
723
724 this->logger->log(this->logger, CONTROL, "generating payload of type %s",
725 mapping_find(payload_type_m,payload_type));
726
727 /* each payload has its own encoding rules */
728 payload->get_encoding_rules(payload,&rules,&rule_count);
729
730 for (i = 0; i < rule_count;i++)
731 {
732 status = SUCCESS;
733 this->logger->log(this->logger, CONTROL|MORE, " generating rule %d %s",
734 i, mapping_find(encoding_type_m,rules[i].type));
735 switch (rules[i].type)
736 {
737 /* all u int values, IKE_SPI and ATTRIBUTE_TYPE are generated in generate_u_int_type */
738 case U_INT_4:
739 case U_INT_8:
740 case U_INT_16:
741 case U_INT_32:
742 case U_INT_64:
743 case IKE_SPI:
744 case ATTRIBUTE_TYPE:
745 {
746 status = this->generate_u_int_type(this,rules[i].type,rules[i].offset);
747 break;
748 }
749 case RESERVED_BIT:
750 {
751 status = this->generate_reserved_field(this,1);
752 break;
753 }
754 case RESERVED_BYTE:
755 {
756 status = this->generate_reserved_field(this,8);
757 break;
758 }
759 case FLAG:
760 {
761 status = this->generate_flag(this,rules[i].offset);
762 break;
763 }
764 case PAYLOAD_LENGTH:
765 {
766 /* position of payload lenght field is temporary stored */
767 this->last_payload_length_position_offset = this->get_current_buffer_offset(this);
768 /* payload length is generated like an U_INT_16 */
769 status = this->generate_u_int_type(this,U_INT_16,rules[i].offset);
770 break;
771 }
772 case HEADER_LENGTH:
773 {
774 /* position of header length field is temporary stored */
775 this->header_length_position_offset = this->get_current_buffer_offset(this);
776 /* header length is generated like an U_INT_32 */
777 status = this->generate_u_int_type(this,U_INT_32,rules[i].offset);
778 break;
779 }
780 case SPI_SIZE:
781 /* spi size is handled as 8 bit unsigned integer */
782 status = this->generate_u_int_type(this,U_INT_8,rules[i].offset);
783 /* last spi size is temporary stored */
784 this->last_spi_size = *((u_int8_t *)(this->data_struct + rules[i].offset));
785 break;
786 case SPI:
787 {
788 /* the SPI value is generated from chunk */
789 status = this->generate_from_chunk(this,rules[i].offset);
790 break;
791 }
792 case KEY_EXCHANGE_DATA:
793 {
794 /* the Key Exchange Data value is generated from chunk */
795 status = this->generate_from_chunk(this,rules[i].offset);
796 if (status != SUCCESS)
797 {
798 this->logger->log(this->logger, ERROR, "could no write key exchange data from chunk");
799 return status;
800 }
801
802 u_int32_t payload_length_position_offset = this->last_payload_length_position_offset;
803 /* Length of KE_PAYLOAD is calculated */
804 u_int16_t length_of_ke_payload = KE_PAYLOAD_HEADER_LENGTH + ((chunk_t *)(this->data_struct + rules[i].offset))->len;
805
806 u_int16_t int16_val = htons(length_of_ke_payload);
807 status = this->write_bytes_to_buffer_at_offset(this,&int16_val,sizeof(u_int16_t),payload_length_position_offset);
808 if (status != SUCCESS)
809 {
810 this->logger->log(this->logger, ERROR, "could not write payload length into buffer");
811 return status;
812 }
813 break;
814 }
815 case NOTIFICATION_DATA:
816 {
817 /* the Notification Data value is generated from chunk */
818 status = this->generate_from_chunk(this,rules[i].offset);
819 if (status != SUCCESS)
820 {
821 this->logger->log(this->logger, ERROR, "Could not generate notification data from chunk");
822 return status;
823 }
824
825 u_int32_t payload_length_position_offset = this->last_payload_length_position_offset;
826 /* Length of Notification PAYLOAD is calculated */
827 u_int16_t length_of_notify_payload = NOTIFY_PAYLOAD_HEADER_LENGTH + ((chunk_t *)(this->data_struct + rules[i].offset))->len;
828 length_of_notify_payload += this->last_spi_size;
829 u_int16_t int16_val = htons(length_of_notify_payload);
830
831 status = this->write_bytes_to_buffer_at_offset(this,&int16_val,sizeof(u_int16_t),payload_length_position_offset);
832 if (status != SUCCESS)
833 {
834 this->logger->log(this->logger, ERROR, "could not write payload length into buffer");
835 return status;
836 }
837 break;
838 }
839 case NONCE_DATA:
840 {
841 /* the Nonce Data value is generated from chunk */
842 status = this->generate_from_chunk(this, rules[i].offset);
843
844 if (status != SUCCESS)
845 {
846 this->logger->log(this->logger, ERROR, "could not write nonce data from chunk");
847 return status;
848 }
849
850 u_int32_t payload_length_position_offset = this->last_payload_length_position_offset;
851 /* Length of nonce PAYLOAD is calculated */
852 u_int16_t length_of_nonce_payload = NONCE_PAYLOAD_HEADER_LENGTH + ((chunk_t *)(this->data_struct + rules[i].offset))->len;
853 u_int16_t int16_val = htons(length_of_nonce_payload);
854
855 status = this->write_bytes_to_buffer_at_offset(this,&int16_val,sizeof(u_int16_t),payload_length_position_offset);
856 if (status != SUCCESS)
857 {
858 this->logger->log(this->logger, ERROR, "could not write payload length into buffer");
859 return status;
860 }
861 break;
862 }
863 case PROPOSALS:
864 {
865 /* before iterative generate the transforms, store the current payload length position */
866 u_int32_t payload_length_position_offset = this->last_payload_length_position_offset;
867 /* Length of SA_PAYLOAD is calculated */
868 u_int16_t length_of_sa_payload = SA_PAYLOAD_HEADER_LENGTH;
869 u_int16_t int16_val;
870 /* proposals are stored in a linked list and so accessed */
871 linked_list_t *proposals = *((linked_list_t **)(this->data_struct + rules[i].offset));
872
873 iterator_t *iterator;
874 /* create forward iterator */
875 status = proposals->create_iterator(proposals,&iterator,TRUE);
876 if (status != SUCCESS)
877 {
878 this->logger->log(this->logger, ERROR, "could not create iterator for proposals");
879 return status;
880 }
881 /* every proposal is processed (iterative call )*/
882 while (iterator->has_next(iterator))
883 {
884 payload_t *current_proposal;
885 u_int32_t before_generate_position_offset;
886 u_int32_t after_generate_position_offset;
887
888 status = iterator->current(iterator,(void **)&current_proposal);
889 if (status != SUCCESS)
890 {
891 iterator->destroy(iterator);
892 return status;
893 }
894 before_generate_position_offset = this->get_current_buffer_offset(this);
895 status = this->public.generate_payload(&(this->public),current_proposal);
896 after_generate_position_offset = this->get_current_buffer_offset(this);
897 if (status != SUCCESS)
898 {
899 iterator->destroy(iterator);
900 return status;
901 }
902
903 /* increase size of transform */
904 length_of_sa_payload += (after_generate_position_offset - before_generate_position_offset);
905 }
906 iterator->destroy(iterator);
907
908 int16_val = htons(length_of_sa_payload);
909 status = this->write_bytes_to_buffer_at_offset(this,&int16_val,sizeof(u_int16_t),payload_length_position_offset);
910 if (status != SUCCESS)
911 {
912 this->logger->log(this->logger, ERROR, "could not write payload length into buffer");
913 return status;
914 }
915 break;
916 }
917
918 case TRANSFORMS:
919 {
920 /* before iterative generate the transforms, store the current length position */
921 u_int32_t payload_length_position_offset = this->last_payload_length_position_offset;
922 u_int16_t length_of_proposal = PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH + this->last_spi_size;
923 u_int16_t int16_val;
924 linked_list_t *transforms = *((linked_list_t **)(this->data_struct + rules[i].offset));
925 iterator_t *iterator;
926
927 /* create forward iterator */
928 status = transforms->create_iterator(transforms,&iterator,TRUE);
929 if (status != SUCCESS)
930 {
931 return status;
932 }
933 while (iterator->has_next(iterator))
934 {
935 payload_t *current_transform;
936 u_int32_t before_generate_position_offset;
937 u_int32_t after_generate_position_offset;
938
939 status = iterator->current(iterator,(void **)&current_transform);
940 if (status != SUCCESS)
941 {
942 iterator->destroy(iterator);
943 return status;
944 }
945
946 before_generate_position_offset = this->get_current_buffer_offset(this);
947 status = this->public.generate_payload(&(this->public),current_transform);
948 after_generate_position_offset = this->get_current_buffer_offset(this);
949 if (status != SUCCESS)
950 {
951 iterator->destroy(iterator);
952 return status;
953 }
954
955 /* increase size of transform */
956 length_of_proposal += (after_generate_position_offset - before_generate_position_offset);
957 }
958
959 iterator->destroy(iterator);
960
961 int16_val = htons(length_of_proposal);
962 this->write_bytes_to_buffer_at_offset(this,&int16_val,sizeof(u_int16_t),payload_length_position_offset);
963
964 break;
965 }
966 case TRANSFORM_ATTRIBUTES:
967 {
968 /* before iterative generate the transform attributes, store the current length position */
969 u_int32_t transform_length_position_offset = this->last_payload_length_position_offset;
970
971 u_int16_t length_of_transform = TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH;
972 u_int16_t int16_val;
973 linked_list_t *transform_attributes =*((linked_list_t **)(this->data_struct + rules[i].offset));
974
975 iterator_t *iterator;
976 /* create forward iterator */
977 status = transform_attributes->create_iterator(transform_attributes,&iterator,TRUE);
978 if (status != SUCCESS)
979 {
980 return status;
981 }
982 while (iterator->has_next(iterator))
983 {
984 payload_t *current_attribute;
985 u_int32_t before_generate_position_offset;
986 u_int32_t after_generate_position_offset;
987
988 status = iterator->current(iterator,(void **)&current_attribute);
989 if (status != SUCCESS)
990 {
991 iterator->destroy(iterator);
992 return status;
993 }
994
995 before_generate_position_offset = this->get_current_buffer_offset(this);
996 this->public.generate_payload(&(this->public),current_attribute);
997 after_generate_position_offset = this->get_current_buffer_offset(this);
998
999 /* increase size of transform */
1000 length_of_transform += (after_generate_position_offset - before_generate_position_offset);
1001 }
1002
1003 iterator->destroy(iterator);
1004
1005 int16_val = htons(length_of_transform);
1006 this->write_bytes_to_buffer_at_offset(this,&int16_val,sizeof(u_int16_t),transform_length_position_offset);
1007
1008 break;
1009 }
1010 case ATTRIBUTE_FORMAT:
1011 {
1012 status = this->generate_flag(this,rules[i].offset);
1013 /* Attribute format is a flag which is stored in context*/
1014 this->attribute_format = *((bool *) (this->data_struct + rules[i].offset));
1015 break;
1016 }
1017
1018 case ATTRIBUTE_LENGTH_OR_VALUE:
1019 {
1020 if (this->attribute_format == FALSE)
1021 {
1022 status = this->generate_u_int_type(this,U_INT_16,rules[i].offset);
1023 /* this field hold the length of the attribute */
1024 this->attribute_length = *((u_int16_t *)(this->data_struct + rules[i].offset));
1025 }
1026 else
1027 {
1028 status = this->generate_u_int_type(this,U_INT_16,rules[i].offset);
1029 // status = this->write_bytes_to_buffer(this,(this->data_struct + rules[i].offset),2);
1030 }
1031 break;
1032 }
1033 case ATTRIBUTE_VALUE:
1034 {
1035 if (this->attribute_format == FALSE)
1036 {
1037 this->logger->log(this->logger, CONTROL|MOST, "attribute value has not fixed size");
1038 /* the attribute value is generated */
1039 status = this->generate_from_chunk(this,rules[i].offset);
1040 }
1041 break;
1042 }
1043 default:
1044 this->logger->log(this->logger, ERROR, "field type %s is not supported",
1045 mapping_find(encoding_type_m,rules[i].type));
1046 return NOT_SUPPORTED;
1047 }
1048 }
1049 this->logger->log_bytes(this->logger, RAW|MORE, "generated data for this payload",
1050 payload_start, this->out_position-payload_start);
1051
1052 return status;
1053 }
1054
1055 /**
1056 * Implements generator_t's destroy function.
1057 * See #generator_s.destroy.
1058 */
1059 static status_t destroy(private_generator_t *this)
1060 {
1061 allocator_free(this->buffer);
1062 global_logger_manager->destroy_logger(global_logger_manager,this->logger);
1063 allocator_free(this);
1064 return SUCCESS;
1065 }
1066
1067 /*
1068 * Described in header
1069 */
1070 generator_t * generator_create()
1071 {
1072 private_generator_t *this;
1073
1074 this = allocator_alloc_thing(private_generator_t);
1075 if (this == NULL)
1076 {
1077 return NULL;
1078 }
1079
1080 /* initiate public functions */
1081 this->public.generate_payload = (status_t(*)(generator_t*, payload_t *)) generate_payload;
1082 this->public.destroy = (status_t(*)(generator_t*)) destroy;
1083 this->public.write_to_chunk = (status_t (*) (generator_t *,chunk_t *)) write_to_chunk;
1084
1085
1086 /* initiate private functions */
1087 this->get_current_buffer_size = get_current_buffer_size;
1088 this->get_current_buffer_space = get_current_buffer_space;
1089 this->get_current_data_length = get_current_data_length;
1090 this->get_current_buffer_offset = get_current_buffer_offset;
1091 this->generate_u_int_type = generate_u_int_type;
1092 this->generate_reserved_field = generate_reserved_field;
1093 this->generate_flag = generate_flag;
1094 this->generate_from_chunk = generate_from_chunk;
1095 this->make_space_available = make_space_available;
1096 this->write_bytes_to_buffer = write_bytes_to_buffer;
1097 this->write_bytes_to_buffer_at_offset = write_bytes_to_buffer_at_offset;
1098
1099
1100 /* allocate memory for buffer */
1101 this->buffer = allocator_alloc(GENERATOR_DATA_BUFFER_SIZE);
1102 if (this->buffer == NULL)
1103 {
1104 allocator_free(this);
1105 return NULL;
1106 }
1107
1108 /* initiate private variables */
1109 this->out_position = this->buffer;
1110 this->roof_position = this->buffer + GENERATOR_DATA_BUFFER_SIZE;
1111 this->data_struct = NULL;
1112 this->current_bit = 0;
1113 this->last_payload_length_position_offset = 0;
1114 this->header_length_position_offset = 0;
1115 this->logger = global_logger_manager->create_logger(global_logger_manager,GENERATOR,NULL);
1116
1117 if (this->logger == NULL)
1118 {
1119 allocator_free(this->buffer);
1120 allocator_free(this);
1121 return NULL;
1122 }
1123 return &(this->public);
1124 }