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