- encryptino payload
[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 default:
453 {
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 }
458 return SUCCESS;
459 }
460
461 /**
462 * Implements private_generator_t's generate_reserved_field function.
463 * See #private_generator_s.generate_reserved_field.
464 */
465 static status_t generate_reserved_field(private_generator_t *this,int bits)
466 {
467 status_t status;
468
469 /* only one bit or 8 bit fields are supported */
470 if ((bits != 1) && (bits != 8))
471 {
472 this->logger->log(this->logger, ERROR, "Reserved field of %d bits cannot be generated", bits);
473 return FAILED;
474 }
475 /* make sure enough space is available in buffer */
476 status = this->make_space_available(this,bits);
477 if (status != SUCCESS)
478 {
479 return status;
480 }
481
482 if (bits == 1)
483 {
484 /* one bit processing */
485 u_int8_t reserved_bit = ~(1 << (7 - this->current_bit));
486 *(this->out_position) = *(this->out_position) & reserved_bit;
487 if (this->current_bit == 0)
488 {
489 /* memory must be zero */
490 *(this->out_position) = 0x00;
491 }
492
493
494 this->current_bit++;
495 if (this->current_bit >= 8)
496 {
497 this->current_bit = this->current_bit % 8;
498 this->out_position++;
499 }
500 }
501 else
502 {
503 /* one byte processing*/
504 if (this->current_bit > 0)
505 {
506 this->logger->log(this->logger, ERROR,
507 "Reserved field cannot be written cause allignement of current bit is %d",
508 this->current_bit);
509 return FAILED;
510 }
511 *(this->out_position) = 0x00;
512 this->out_position++;
513 }
514
515 return SUCCESS;
516
517
518 }
519
520 /**
521 * Implements private_generator_t's generate_flag function.
522 * See #private_generator_s.generate_flag.
523 */
524 static status_t generate_flag (private_generator_t *this,u_int32_t offset)
525 {
526 status_t status;
527 /* value of current flag */
528 u_int8_t flag_value;
529 /* position of flag in current byte */
530 u_int8_t flag;
531
532 /* if the value in the data_struct is TRUE, flag_value is set to 1, 0 otherwise */
533 flag_value = (*((bool *) (this->data_struct + offset))) ? 1 : 0;
534 /* get flag position */
535 flag = (flag_value << (7 - this->current_bit));
536
537 /* make sure one bit is available in buffer */
538 status = this->make_space_available(this,1);
539 if (status != SUCCESS)
540 {
541 return status;
542 }
543 if (this->current_bit == 0)
544 {
545 /* memory must be zero */
546 *(this->out_position) = 0x00;
547 }
548
549 *(this->out_position) = *(this->out_position) | flag;
550
551
552 this->logger->log(this->logger, RAW|MOST, " => 0x0%x", *(this->out_position));
553
554 this->current_bit++;
555 if (this->current_bit >= 8)
556 {
557 this->current_bit = this->current_bit % 8;
558 this->out_position++;
559 }
560 return SUCCESS;
561 }
562
563 /**
564 * Implements private_generator_t's generate_from_chunk function.
565 * See #private_generator_s.generate_from_chunk.
566 */
567 static status_t generate_from_chunk (private_generator_t *this,u_int32_t offset)
568 {
569 if (this->current_bit != 0)
570 {
571 this->logger->log(this->logger, ERROR, "can not generate a chunk at Bitpos %d", this->current_bit);
572 return FAILED;
573 }
574
575 /* position in buffer */
576 chunk_t *attribute_value = (chunk_t *)(this->data_struct + offset);
577
578 this->logger->log_chunk(this->logger, RAW|MOST, " =>", attribute_value);
579
580 /* use write_bytes_to_buffer function to do the job */
581 return this->write_bytes_to_buffer(this,attribute_value->ptr,attribute_value->len);
582
583 }
584
585 /**
586 * Implements private_generator_t's generator_context_make_space_available function.
587 * See #private_generator_s.generator_context_make_space_available.
588 */
589 static status_t make_space_available (private_generator_t *this, size_t bits)
590 {
591 while (((this->get_current_buffer_space(this) * 8) - this->current_bit) < bits)
592 {
593 /* must increase buffer */
594 u_int8_t *new_buffer;
595 size_t old_buffer_size = this->get_current_buffer_size(this);
596 size_t new_buffer_size = old_buffer_size + GENERATOR_DATA_BUFFER_INCREASE_VALUE;
597 size_t out_position_offset = ((this->out_position) - (this->buffer));
598
599 this->logger->log(this->logger, CONTROL|MOST, "increased gen buffer from %d to %d byte",
600 old_buffer_size, new_buffer_size);
601
602 /* Reallocate space for new buffer */
603 new_buffer = allocator_realloc(this->buffer,new_buffer_size);
604 if (new_buffer == NULL)
605 {
606 this->logger->log(this->logger, ERROR, "reallocation of gen buffer failed!!!");
607 return OUT_OF_RES;
608 }
609
610 this->buffer = new_buffer;
611
612 this->out_position = (this->buffer + out_position_offset);
613 this->roof_position = (this->buffer + new_buffer_size);
614 }
615 return SUCCESS;
616 }
617
618 /**
619 * Implements private_generator_t's write_bytes_to_buffer function.
620 * See #private_generator_s.write_bytes_to_buffer.
621 */
622 static status_t write_bytes_to_buffer (private_generator_t *this,void * bytes, size_t number_of_bytes)
623 {
624 int i;
625 status_t status;
626 u_int8_t *read_position = (u_int8_t *) bytes;
627
628 status = this->make_space_available(this,number_of_bytes * 8);
629 if (status != SUCCESS)
630 {
631 return status;
632 }
633
634 for (i = 0; i < number_of_bytes; i++)
635 {
636 *(this->out_position) = *(read_position);
637 read_position++;
638 this->out_position++;
639 }
640 return status;
641 }
642
643 /**
644 * Implements private_generator_t's write_bytes_to_buffer_at_offset function.
645 * See #private_generator_s.write_bytes_to_buffer_at_offset.
646 */
647 static status_t write_bytes_to_buffer_at_offset (private_generator_t *this,void * bytes,size_t number_of_bytes,u_int32_t offset)
648 {
649 int i;
650 status_t status;
651 u_int8_t *read_position = (u_int8_t *) bytes;
652 u_int8_t *write_position;
653 u_int32_t free_space_after_offset = (this->get_current_buffer_size(this) - offset);
654
655 /* check first if enough space for new data is available */
656 if (number_of_bytes > free_space_after_offset)
657 {
658 status = this->make_space_available(this,(number_of_bytes - free_space_after_offset) * 8);
659 }
660
661 write_position = this->buffer + offset;
662 for (i = 0; i < number_of_bytes; i++)
663 {
664 *(write_position) = *(read_position);
665 read_position++;
666 write_position++;
667 }
668 return SUCCESS;
669 }
670
671 /**
672 * Implements generator_t's write_chunk function.
673 * See #generator_s.write_chunk.
674 */
675 static status_t write_to_chunk (private_generator_t *this,chunk_t *data)
676 {
677 size_t data_length = this->get_current_data_length(this);
678 u_int32_t header_length_field = data_length;
679
680 /* write length into header length field */
681 if (this->header_length_position_offset > 0)
682 {
683 u_int32_t int32_val = htonl(header_length_field);
684 this->write_bytes_to_buffer_at_offset(this,&int32_val,sizeof(u_int32_t),this->header_length_position_offset);
685 }
686
687 if (this->current_bit > 0)
688 data_length++;
689 data->ptr = allocator_alloc(data_length);
690 if (data->ptr == NULL)
691 {
692 data->len = 0;
693 this->logger->log(this->logger, ERROR, "not enougth ressources to allocate chunk");
694 return OUT_OF_RES;
695 }
696 memcpy(data->ptr,this->buffer,data_length);
697 data->len = data_length;
698
699 this->logger->log_chunk(this->logger, RAW, "generated data of this parser", data);
700
701 return SUCCESS;
702 }
703
704 /**
705 * Implements generator_t's generate_payload function.
706 * See #generator_s.generate_payload.
707 */
708 static status_t generate_payload (private_generator_t *this,payload_t *payload)
709 {
710 int i;
711 status_t status;
712 this->data_struct = payload;
713 size_t rule_count;
714 encoding_rule_t *rules;
715 payload_type_t payload_type;
716 u_int8_t *payload_start;
717
718 /* get payload type */
719 payload_type = payload->get_type(payload);
720 /* spi size has to get reseted */
721 this->last_spi_size = 0;
722
723 payload_start = this->out_position;
724
725 this->logger->log(this->logger, CONTROL, "generating payload of type %s",
726 mapping_find(payload_type_m,payload_type));
727
728 /* each payload has its own encoding rules */
729 payload->get_encoding_rules(payload,&rules,&rule_count);
730
731 for (i = 0; i < rule_count;i++)
732 {
733 status = SUCCESS;
734 this->logger->log(this->logger, CONTROL|MORE, " generating rule %d %s",
735 i, mapping_find(encoding_type_m,rules[i].type));
736 switch (rules[i].type)
737 {
738 /* all u int values, IKE_SPI and ATTRIBUTE_TYPE are generated in generate_u_int_type */
739 case U_INT_4:
740 case U_INT_8:
741 case U_INT_16:
742 case U_INT_32:
743 case U_INT_64:
744 case IKE_SPI:
745 case ATTRIBUTE_TYPE:
746 {
747 status = this->generate_u_int_type(this,rules[i].type,rules[i].offset);
748 break;
749 }
750 case RESERVED_BIT:
751 {
752 status = this->generate_reserved_field(this,1);
753 break;
754 }
755 case RESERVED_BYTE:
756 {
757 status = this->generate_reserved_field(this,8);
758 break;
759 }
760 case FLAG:
761 {
762 status = this->generate_flag(this,rules[i].offset);
763 break;
764 }
765 case PAYLOAD_LENGTH:
766 {
767 /* position of payload lenght field is temporary stored */
768 this->last_payload_length_position_offset = this->get_current_buffer_offset(this);
769 /* payload length is generated like an U_INT_16 */
770 status = this->generate_u_int_type(this,U_INT_16,rules[i].offset);
771 break;
772 }
773 case HEADER_LENGTH:
774 {
775 /* position of header length field is temporary stored */
776 this->header_length_position_offset = this->get_current_buffer_offset(this);
777 /* header length is generated like an U_INT_32 */
778 status = this->generate_u_int_type(this,U_INT_32,rules[i].offset);
779 break;
780 }
781 case SPI_SIZE:
782 /* spi size is handled as 8 bit unsigned integer */
783 status = this->generate_u_int_type(this,U_INT_8,rules[i].offset);
784 /* last spi size is temporary stored */
785 this->last_spi_size = *((u_int8_t *)(this->data_struct + rules[i].offset));
786 break;
787 case SPI:
788 {
789 /* the SPI value is generated from chunk */
790 status = this->generate_from_chunk(this,rules[i].offset);
791 break;
792 }
793 case KEY_EXCHANGE_DATA:
794 {
795 /* the Key Exchange Data value is generated from chunk */
796 status = this->generate_from_chunk(this,rules[i].offset);
797 if (status != SUCCESS)
798 {
799 this->logger->log(this->logger, ERROR, "could no write key exchange data from chunk");
800 return status;
801 }
802
803 u_int32_t payload_length_position_offset = this->last_payload_length_position_offset;
804 /* Length of KE_PAYLOAD is calculated */
805 u_int16_t length_of_ke_payload = KE_PAYLOAD_HEADER_LENGTH + ((chunk_t *)(this->data_struct + rules[i].offset))->len;
806
807 u_int16_t int16_val = htons(length_of_ke_payload);
808 status = this->write_bytes_to_buffer_at_offset(this,&int16_val,sizeof(u_int16_t),payload_length_position_offset);
809 if (status != SUCCESS)
810 {
811 this->logger->log(this->logger, ERROR, "could not write payload length into buffer");
812 return status;
813 }
814 break;
815 }
816 case NOTIFICATION_DATA:
817 {
818 /* the Notification Data value is generated from chunk */
819 status = this->generate_from_chunk(this,rules[i].offset);
820 if (status != SUCCESS)
821 {
822 this->logger->log(this->logger, ERROR, "Could not generate notification data from chunk");
823 return status;
824 }
825
826 u_int32_t payload_length_position_offset = this->last_payload_length_position_offset;
827 /* Length of Notification PAYLOAD is calculated */
828 u_int16_t length_of_notify_payload = NOTIFY_PAYLOAD_HEADER_LENGTH + ((chunk_t *)(this->data_struct + rules[i].offset))->len;
829 length_of_notify_payload += this->last_spi_size;
830 u_int16_t int16_val = htons(length_of_notify_payload);
831
832 status = this->write_bytes_to_buffer_at_offset(this,&int16_val,sizeof(u_int16_t),payload_length_position_offset);
833 if (status != SUCCESS)
834 {
835 this->logger->log(this->logger, ERROR, "could not write payload length into buffer");
836 return status;
837 }
838 break;
839 }
840 case NONCE_DATA:
841 {
842 /* the Nonce Data value is generated from chunk */
843 status = this->generate_from_chunk(this, rules[i].offset);
844
845 if (status != SUCCESS)
846 {
847 this->logger->log(this->logger, ERROR, "could not write nonce data from chunk");
848 return status;
849 }
850
851 u_int32_t payload_length_position_offset = this->last_payload_length_position_offset;
852 /* Length of nonce PAYLOAD is calculated */
853 u_int16_t length_of_nonce_payload = NONCE_PAYLOAD_HEADER_LENGTH + ((chunk_t *)(this->data_struct + rules[i].offset))->len;
854 u_int16_t int16_val = htons(length_of_nonce_payload);
855
856 status = this->write_bytes_to_buffer_at_offset(this,&int16_val,sizeof(u_int16_t),payload_length_position_offset);
857 if (status != SUCCESS)
858 {
859 this->logger->log(this->logger, ERROR, "could not write payload length into buffer");
860 return status;
861 }
862 break;
863 }
864 case PROPOSALS:
865 {
866 /* before iterative generate the transforms, store the current payload length position */
867 u_int32_t payload_length_position_offset = this->last_payload_length_position_offset;
868 /* Length of SA_PAYLOAD is calculated */
869 u_int16_t length_of_sa_payload = SA_PAYLOAD_HEADER_LENGTH;
870 u_int16_t int16_val;
871 /* proposals are stored in a linked list and so accessed */
872 linked_list_t *proposals = *((linked_list_t **)(this->data_struct + rules[i].offset));
873
874 iterator_t *iterator;
875 /* create forward iterator */
876 status = proposals->create_iterator(proposals,&iterator,TRUE);
877 if (status != SUCCESS)
878 {
879 this->logger->log(this->logger, ERROR, "could not create iterator for proposals");
880 return status;
881 }
882 /* every proposal is processed (iterative call )*/
883 while (iterator->has_next(iterator))
884 {
885 payload_t *current_proposal;
886 u_int32_t before_generate_position_offset;
887 u_int32_t after_generate_position_offset;
888
889 status = iterator->current(iterator,(void **)&current_proposal);
890 if (status != SUCCESS)
891 {
892 iterator->destroy(iterator);
893 return status;
894 }
895 before_generate_position_offset = this->get_current_buffer_offset(this);
896 status = this->public.generate_payload(&(this->public),current_proposal);
897 after_generate_position_offset = this->get_current_buffer_offset(this);
898 if (status != SUCCESS)
899 {
900 iterator->destroy(iterator);
901 return status;
902 }
903
904 /* increase size of transform */
905 length_of_sa_payload += (after_generate_position_offset - before_generate_position_offset);
906 }
907 iterator->destroy(iterator);
908
909 int16_val = htons(length_of_sa_payload);
910 status = this->write_bytes_to_buffer_at_offset(this,&int16_val,sizeof(u_int16_t),payload_length_position_offset);
911 if (status != SUCCESS)
912 {
913 this->logger->log(this->logger, ERROR, "could not write payload length into buffer");
914 return status;
915 }
916 break;
917 }
918
919 case TRANSFORMS:
920 {
921 /* before iterative generate the transforms, store the current length position */
922 u_int32_t payload_length_position_offset = this->last_payload_length_position_offset;
923 u_int16_t length_of_proposal = PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH + this->last_spi_size;
924 u_int16_t int16_val;
925 linked_list_t *transforms = *((linked_list_t **)(this->data_struct + rules[i].offset));
926 iterator_t *iterator;
927
928 /* create forward iterator */
929 status = transforms->create_iterator(transforms,&iterator,TRUE);
930 if (status != SUCCESS)
931 {
932 return status;
933 }
934 while (iterator->has_next(iterator))
935 {
936 payload_t *current_transform;
937 u_int32_t before_generate_position_offset;
938 u_int32_t after_generate_position_offset;
939
940 status = iterator->current(iterator,(void **)&current_transform);
941 if (status != SUCCESS)
942 {
943 iterator->destroy(iterator);
944 return status;
945 }
946
947 before_generate_position_offset = this->get_current_buffer_offset(this);
948 status = this->public.generate_payload(&(this->public),current_transform);
949 after_generate_position_offset = this->get_current_buffer_offset(this);
950 if (status != SUCCESS)
951 {
952 iterator->destroy(iterator);
953 return status;
954 }
955
956 /* increase size of transform */
957 length_of_proposal += (after_generate_position_offset - before_generate_position_offset);
958 }
959
960 iterator->destroy(iterator);
961
962 int16_val = htons(length_of_proposal);
963 this->write_bytes_to_buffer_at_offset(this,&int16_val,sizeof(u_int16_t),payload_length_position_offset);
964
965 break;
966 }
967 case TRANSFORM_ATTRIBUTES:
968 {
969 /* before iterative generate the transform attributes, store the current length position */
970 u_int32_t transform_length_position_offset = this->last_payload_length_position_offset;
971
972 u_int16_t length_of_transform = TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH;
973 u_int16_t int16_val;
974 linked_list_t *transform_attributes =*((linked_list_t **)(this->data_struct + rules[i].offset));
975
976 iterator_t *iterator;
977 /* create forward iterator */
978 status = transform_attributes->create_iterator(transform_attributes,&iterator,TRUE);
979 if (status != SUCCESS)
980 {
981 return status;
982 }
983 while (iterator->has_next(iterator))
984 {
985 payload_t *current_attribute;
986 u_int32_t before_generate_position_offset;
987 u_int32_t after_generate_position_offset;
988
989 status = iterator->current(iterator,(void **)&current_attribute);
990 if (status != SUCCESS)
991 {
992 iterator->destroy(iterator);
993 return status;
994 }
995
996 before_generate_position_offset = this->get_current_buffer_offset(this);
997 this->public.generate_payload(&(this->public),current_attribute);
998 after_generate_position_offset = this->get_current_buffer_offset(this);
999
1000 /* increase size of transform */
1001 length_of_transform += (after_generate_position_offset - before_generate_position_offset);
1002 }
1003
1004 iterator->destroy(iterator);
1005
1006 int16_val = htons(length_of_transform);
1007 this->write_bytes_to_buffer_at_offset(this,&int16_val,sizeof(u_int16_t),transform_length_position_offset);
1008
1009 break;
1010 }
1011 case ATTRIBUTE_FORMAT:
1012 {
1013 status = this->generate_flag(this,rules[i].offset);
1014 /* Attribute format is a flag which is stored in context*/
1015 this->attribute_format = *((bool *) (this->data_struct + rules[i].offset));
1016 break;
1017 }
1018
1019 case ATTRIBUTE_LENGTH_OR_VALUE:
1020 {
1021 if (this->attribute_format == FALSE)
1022 {
1023 status = this->generate_u_int_type(this,U_INT_16,rules[i].offset);
1024 /* this field hold the length of the attribute */
1025 this->attribute_length = *((u_int16_t *)(this->data_struct + rules[i].offset));
1026 }
1027 else
1028 {
1029 status = this->generate_u_int_type(this,U_INT_16,rules[i].offset);
1030 // status = this->write_bytes_to_buffer(this,(this->data_struct + rules[i].offset),2);
1031 }
1032 break;
1033 }
1034 case ATTRIBUTE_VALUE:
1035 {
1036 if (this->attribute_format == FALSE)
1037 {
1038 this->logger->log(this->logger, CONTROL|MOST, "attribute value has not fixed size");
1039 /* the attribute value is generated */
1040 status = this->generate_from_chunk(this,rules[i].offset);
1041 if (status != SUCCESS)
1042 {
1043 this->logger->log(this->logger, ERROR, "could not write attribute value from chunk");
1044 return status;
1045 }
1046 }
1047 break;
1048 }
1049 case ENCRYPTED_DATA:
1050 {
1051 status = this->generate_from_chunk(this, rules[i].offset);
1052 if (status != SUCCESS)
1053 {
1054 this->logger->log(this->logger, ERROR, "could not write encrypted data from chunk");
1055 return status;
1056 }
1057 break;
1058 }
1059 default:
1060 this->logger->log(this->logger, ERROR, "field type %s is not supported",
1061 mapping_find(encoding_type_m,rules[i].type));
1062 return NOT_SUPPORTED;
1063 }
1064 }
1065 this->logger->log_bytes(this->logger, RAW|MORE, "generated data for this payload",
1066 payload_start, this->out_position-payload_start);
1067
1068 return status;
1069 }
1070
1071 /**
1072 * Implements generator_t's destroy function.
1073 * See #generator_s.destroy.
1074 */
1075 static status_t destroy(private_generator_t *this)
1076 {
1077 allocator_free(this->buffer);
1078 global_logger_manager->destroy_logger(global_logger_manager,this->logger);
1079 allocator_free(this);
1080 return SUCCESS;
1081 }
1082
1083 /*
1084 * Described in header
1085 */
1086 generator_t * generator_create()
1087 {
1088 private_generator_t *this;
1089
1090 this = allocator_alloc_thing(private_generator_t);
1091 if (this == NULL)
1092 {
1093 return NULL;
1094 }
1095
1096 /* initiate public functions */
1097 this->public.generate_payload = (status_t(*)(generator_t*, payload_t *)) generate_payload;
1098 this->public.destroy = (status_t(*)(generator_t*)) destroy;
1099 this->public.write_to_chunk = (status_t (*) (generator_t *,chunk_t *)) write_to_chunk;
1100
1101
1102 /* initiate private functions */
1103 this->get_current_buffer_size = get_current_buffer_size;
1104 this->get_current_buffer_space = get_current_buffer_space;
1105 this->get_current_data_length = get_current_data_length;
1106 this->get_current_buffer_offset = get_current_buffer_offset;
1107 this->generate_u_int_type = generate_u_int_type;
1108 this->generate_reserved_field = generate_reserved_field;
1109 this->generate_flag = generate_flag;
1110 this->generate_from_chunk = generate_from_chunk;
1111 this->make_space_available = make_space_available;
1112 this->write_bytes_to_buffer = write_bytes_to_buffer;
1113 this->write_bytes_to_buffer_at_offset = write_bytes_to_buffer_at_offset;
1114
1115
1116 /* allocate memory for buffer */
1117 this->buffer = allocator_alloc(GENERATOR_DATA_BUFFER_SIZE);
1118 if (this->buffer == NULL)
1119 {
1120 allocator_free(this);
1121 return NULL;
1122 }
1123
1124 /* initiate private variables */
1125 this->out_position = this->buffer;
1126 this->roof_position = this->buffer + GENERATOR_DATA_BUFFER_SIZE;
1127 this->data_struct = NULL;
1128 this->current_bit = 0;
1129 this->last_payload_length_position_offset = 0;
1130 this->header_length_position_offset = 0;
1131 this->logger = global_logger_manager->create_logger(global_logger_manager,GENERATOR,NULL);
1132
1133 if (this->logger == NULL)
1134 {
1135 allocator_free(this->buffer);
1136 allocator_free(this);
1137 return NULL;
1138 }
1139 return &(this->public);
1140 }