Migrated vendor_id_payload to INIT/METHOD macros
[strongswan.git] / src / libcharon / encoding / generator.c
1 /*
2 * Copyright (C) 2005-2009 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <stdlib.h>
18 #include <string.h>
19 #include <arpa/inet.h>
20 #include <stdio.h>
21
22 #include "generator.h"
23
24 #include <library.h>
25 #include <daemon.h>
26 #include <utils/linked_list.h>
27 #include <encoding/payloads/payload.h>
28 #include <encoding/payloads/proposal_substructure.h>
29 #include <encoding/payloads/transform_substructure.h>
30 #include <encoding/payloads/sa_payload.h>
31 #include <encoding/payloads/ke_payload.h>
32 #include <encoding/payloads/notify_payload.h>
33 #include <encoding/payloads/nonce_payload.h>
34 #include <encoding/payloads/id_payload.h>
35 #include <encoding/payloads/auth_payload.h>
36 #include <encoding/payloads/cert_payload.h>
37 #include <encoding/payloads/certreq_payload.h>
38 #include <encoding/payloads/ts_payload.h>
39 #include <encoding/payloads/delete_payload.h>
40 #include <encoding/payloads/vendor_id_payload.h>
41 #include <encoding/payloads/cp_payload.h>
42 #include <encoding/payloads/configuration_attribute.h>
43 #include <encoding/payloads/eap_payload.h>
44 #include <encoding/payloads/unknown_payload.h>
45
46 /**
47 * Generating is done in a data buffer.
48 * This is the start size of this buffer in bytes.
49 */
50 #define GENERATOR_DATA_BUFFER_SIZE 500
51
52 /**
53 * Number of bytes to increase the buffer, if it is too small.
54 */
55 #define GENERATOR_DATA_BUFFER_INCREASE_VALUE 500
56
57 typedef struct private_generator_t private_generator_t;
58
59 /**
60 * Private part of a generator_t object.
61 */
62 struct private_generator_t {
63 /**
64 * Public part of a generator_t object.
65 */
66 generator_t public;
67
68 /**
69 * Buffer used to generate the data into.
70 */
71 u_int8_t *buffer;
72
73 /**
74 * Current write position in buffer (one byte aligned).
75 */
76 u_int8_t *out_position;
77
78 /**
79 * Position of last byte in buffer.
80 */
81 u_int8_t *roof_position;
82
83 /**
84 * Current bit writing to in current byte (between 0 and 7).
85 */
86 u_int8_t current_bit;
87
88 /**
89 * Associated data struct to read informations from.
90 */
91 void *data_struct;
92
93 /**
94 * Offset of the header length field in the buffer.
95 */
96 u_int32_t header_length_offset;
97
98 /**
99 * Attribute format of the last generated transform attribute.
100 *
101 * Used to check if a variable value field is used or not for
102 * the transform attribute value.
103 */
104 bool attribute_format;
105
106 /**
107 * Depending on the value of attribute_format this field is used
108 * to hold the length of the transform attribute in bytes.
109 */
110 u_int16_t attribute_length;
111 };
112
113 /**
114 * Get size of current buffer in bytes.
115 */
116 static int get_size(private_generator_t *this)
117 {
118 return this->roof_position - this->buffer;
119 }
120
121 /**
122 * Get free space of current buffer in bytes.
123 */
124 static int get_space(private_generator_t *this)
125 {
126 return this->roof_position - this->out_position;
127 }
128
129 /**
130 * Get length of data in buffer (in bytes).
131 */
132 static int get_length(private_generator_t *this)
133 {
134 return this->out_position - this->buffer;
135 }
136
137 /**
138 * Get current offset in buffer (in bytes).
139 */
140 static u_int32_t get_offset(private_generator_t *this)
141 {
142 return this->out_position - this->buffer;
143 }
144
145 /**
146 * Makes sure enough space is available in buffer to store amount of bits.
147 */
148 static void make_space_available(private_generator_t *this, int bits)
149 {
150 while ((get_space(this) * 8 - this->current_bit) < bits)
151 {
152 int old_buffer_size, new_buffer_size, out_position_offset;
153
154 old_buffer_size = get_size(this);
155 new_buffer_size = old_buffer_size + GENERATOR_DATA_BUFFER_INCREASE_VALUE;
156 out_position_offset = this->out_position - this->buffer;
157
158 DBG2(DBG_ENC, "increasing gen buffer from %d to %d byte",
159 old_buffer_size, new_buffer_size);
160
161 this->buffer = realloc(this->buffer,new_buffer_size);
162 this->out_position = (this->buffer + out_position_offset);
163 this->roof_position = (this->buffer + new_buffer_size);
164 }
165 }
166
167 /**
168 * Writes a specific amount of byte into the buffer.
169 */
170 static void write_bytes_to_buffer(private_generator_t *this, void *bytes,
171 int number_of_bytes)
172 {
173 int i;
174 u_int8_t *read_position = (u_int8_t *)bytes;
175
176 make_space_available(this, number_of_bytes * 8);
177
178 for (i = 0; i < number_of_bytes; i++)
179 {
180 *(this->out_position) = *(read_position);
181 read_position++;
182 this->out_position++;
183 }
184 }
185
186 /**
187 * Generates a U_INT-Field type and writes it to buffer.
188 */
189 static void generate_u_int_type(private_generator_t *this,
190 encoding_type_t int_type,u_int32_t offset)
191 {
192 int number_of_bits = 0;
193
194 /* find out number of bits of each U_INT type to check for enough space */
195 switch (int_type)
196 {
197 case U_INT_4:
198 number_of_bits = 4;
199 break;
200 case TS_TYPE:
201 case U_INT_8:
202 number_of_bits = 8;
203 break;
204 case U_INT_16:
205 case CONFIGURATION_ATTRIBUTE_LENGTH:
206 number_of_bits = 16;
207 break;
208 case U_INT_32:
209 number_of_bits = 32;
210 break;
211 case ATTRIBUTE_TYPE:
212 number_of_bits = 15;
213 break;
214 case IKE_SPI:
215 number_of_bits = 64;
216 break;
217 default:
218 DBG1(DBG_ENC, "U_INT Type %N is not supported",
219 encoding_type_names, int_type);
220 return;
221 }
222 if ((number_of_bits % 8) == 0 && this->current_bit != 0)
223 {
224 DBG1(DBG_ENC, "U_INT Type %N is not 8 Bit aligned",
225 encoding_type_names, int_type);
226 return;
227 }
228
229 make_space_available(this, number_of_bits);
230 switch (int_type)
231 {
232 case U_INT_4:
233 {
234 u_int8_t high, low;
235
236 if (this->current_bit == 0)
237 {
238 /* high of current byte in buffer has to be set to the new value*/
239 high = *((u_int8_t *)(this->data_struct + offset)) << 4;
240 /* low in buffer is not changed */
241 low = *(this->out_position) & 0x0F;
242 /* high is set, low_val is not changed */
243 *(this->out_position) = high | low;
244 DBG3(DBG_ENC, " => %d", *(this->out_position));
245 /* write position is not changed, just bit position is moved */
246 this->current_bit = 4;
247 }
248 else if (this->current_bit == 4)
249 {
250 /* high in buffer is not changed */
251 high = *(this->out_position) & 0xF0;
252 /* low of current byte in buffer has to be set to the new value*/
253 low = *((u_int8_t *)(this->data_struct + offset)) & 0x0F;
254 *(this->out_position) = high | low;
255 DBG3(DBG_ENC, " => %d", *(this->out_position));
256 this->out_position++;
257 this->current_bit = 0;
258 }
259 else
260 {
261 DBG1(DBG_ENC, "U_INT_4 Type is not 4 Bit aligned");
262 /* 4 Bit integers must have a 4 bit alignment */
263 return;
264 }
265 break;
266 }
267 case TS_TYPE:
268 case U_INT_8:
269 {
270 /* 8 bit values are written as they are */
271 *this->out_position = *((u_int8_t *)(this->data_struct + offset));
272 DBG3(DBG_ENC, " => %d", *(this->out_position));
273 this->out_position++;
274 break;
275 }
276 case ATTRIBUTE_TYPE:
277 {
278 u_int8_t attribute_format_flag;
279 u_int16_t val;
280
281 /* attribute type must not change first bit of current byte */
282 if (this->current_bit != 1)
283 {
284 DBG1(DBG_ENC, "ATTRIBUTE FORMAT flag is not set");
285 return;
286 }
287 attribute_format_flag = *(this->out_position) & 0x80;
288 /* get attribute type value as 16 bit integer*/
289 val = *((u_int16_t*)(this->data_struct + offset));
290 /* unset most significant bit */
291 val &= 0x7FFF;
292 if (attribute_format_flag)
293 {
294 val |= 0x8000;
295 }
296 val = htons(val);
297 DBG3(DBG_ENC, " => %d", val);
298 /* write bytes to buffer (set bit is overwritten) */
299 write_bytes_to_buffer(this, &val, sizeof(u_int16_t));
300 this->current_bit = 0;
301 break;
302
303 }
304 case U_INT_16:
305 case CONFIGURATION_ATTRIBUTE_LENGTH:
306 {
307 u_int16_t val = htons(*((u_int16_t*)(this->data_struct + offset)));
308 DBG3(DBG_ENC, " => %b", &val, sizeof(u_int16_t));
309 write_bytes_to_buffer(this, &val, sizeof(u_int16_t));
310 break;
311 }
312 case U_INT_32:
313 {
314 u_int32_t val = htonl(*((u_int32_t*)(this->data_struct + offset)));
315 DBG3(DBG_ENC, " => %b", &val, sizeof(u_int32_t));
316 write_bytes_to_buffer(this, &val, sizeof(u_int32_t));
317 break;
318 }
319 case IKE_SPI:
320 {
321 /* 64 bit are written as-is, no host order conversion */
322 write_bytes_to_buffer(this, this->data_struct + offset,
323 sizeof(u_int64_t));
324 DBG3(DBG_ENC, " => %b", this->data_struct + offset,
325 sizeof(u_int64_t));
326 break;
327 }
328 default:
329 {
330 DBG1(DBG_ENC, "U_INT Type %N is not supported",
331 encoding_type_names, int_type);
332 return;
333 }
334 }
335 }
336
337 /**
338 * Generate a reserved bit or byte
339 */
340 static void generate_reserved_field(private_generator_t *this, int bits)
341 {
342 /* only one bit or 8 bit fields are supported */
343 if (bits != 1 && bits != 8)
344 {
345 DBG1(DBG_ENC, "reserved field of %d bits cannot be generated", bits);
346 return ;
347 }
348 make_space_available(this, bits);
349
350 if (bits == 1)
351 {
352 u_int8_t reserved_bit = ~(1 << (7 - this->current_bit));
353
354 *(this->out_position) = *(this->out_position) & reserved_bit;
355 if (this->current_bit == 0)
356 {
357 /* memory must be zero */
358 *(this->out_position) = 0x00;
359 }
360 this->current_bit++;
361 if (this->current_bit >= 8)
362 {
363 this->current_bit = this->current_bit % 8;
364 this->out_position++;
365 }
366 }
367 else
368 {
369 if (this->current_bit > 0)
370 {
371 DBG1(DBG_ENC, "reserved field cannot be written cause "
372 "alignement of current bit is %d", this->current_bit);
373 return;
374 }
375 *(this->out_position) = 0x00;
376 this->out_position++;
377 }
378 }
379
380 /**
381 * Generate a FLAG filed
382 */
383 static void generate_flag(private_generator_t *this, u_int32_t offset)
384 {
385 u_int8_t flag_value;
386 u_int8_t flag;
387
388 flag_value = (*((bool *) (this->data_struct + offset))) ? 1 : 0;
389 /* get flag position */
390 flag = (flag_value << (7 - this->current_bit));
391
392 /* make sure one bit is available in buffer */
393 make_space_available(this, 1);
394 if (this->current_bit == 0)
395 {
396 /* memory must be zero */
397 *(this->out_position) = 0x00;
398 }
399
400 *(this->out_position) = *(this->out_position) | flag;
401 DBG3(DBG_ENC, " => %d", *this->out_position);
402
403 this->current_bit++;
404 if (this->current_bit >= 8)
405 {
406 this->current_bit = this->current_bit % 8;
407 this->out_position++;
408 }
409 }
410
411 /**
412 * Generates a bytestream from a chunk_t.
413 */
414 static void generate_from_chunk(private_generator_t *this, u_int32_t offset)
415 {
416 chunk_t *value;
417
418 if (this->current_bit != 0)
419 {
420 DBG1(DBG_ENC, "can not generate a chunk at Bitpos %d", this->current_bit);
421 return ;
422 }
423
424 value = (chunk_t *)(this->data_struct + offset);
425 DBG3(DBG_ENC, " => %B", value);
426
427 write_bytes_to_buffer(this, value->ptr, value->len);
428 }
429
430 METHOD(generator_t, get_chunk, chunk_t,
431 private_generator_t *this, u_int32_t **lenpos)
432 {
433 chunk_t data;
434
435 *lenpos = (u_int32_t*)(this->buffer + this->header_length_offset);
436 data = chunk_create(this->buffer, get_length(this));
437 DBG3(DBG_ENC, "generated data of this generator %B", &data);
438 return data;
439 }
440
441 METHOD(generator_t, generate_payload, void,
442 private_generator_t *this,payload_t *payload)
443 {
444 int i, offset_start;
445 size_t rule_count;
446 encoding_rule_t *rules;
447 payload_type_t payload_type;
448
449 this->data_struct = payload;
450 payload_type = payload->get_type(payload);
451
452 offset_start = this->out_position - this->buffer;
453
454 DBG2(DBG_ENC, "generating payload of type %N",
455 payload_type_names, payload_type);
456
457 /* each payload has its own encoding rules */
458 payload->get_encoding_rules(payload, &rules, &rule_count);
459
460 for (i = 0; i < rule_count;i++)
461 {
462 DBG2(DBG_ENC, " generating rule %d %N",
463 i, encoding_type_names, rules[i].type);
464 switch (rules[i].type)
465 {
466 case U_INT_4:
467 case U_INT_8:
468 case U_INT_16:
469 case U_INT_32:
470 case IKE_SPI:
471 case TS_TYPE:
472 case ATTRIBUTE_TYPE:
473 case CONFIGURATION_ATTRIBUTE_LENGTH:
474 generate_u_int_type(this, rules[i].type, rules[i].offset);
475 break;
476 case RESERVED_BIT:
477 generate_reserved_field(this, 1);
478 break;
479 case RESERVED_BYTE:
480 generate_reserved_field(this, 8);
481 break;
482 case FLAG:
483 generate_flag(this, rules[i].offset);
484 break;
485 case PAYLOAD_LENGTH:
486 generate_u_int_type(this, U_INT_16,rules[i].offset);
487 break;
488 case HEADER_LENGTH:
489 this->header_length_offset = get_offset(this);
490 generate_u_int_type(this ,U_INT_32, rules[i].offset);
491 break;
492 case SPI_SIZE:
493 generate_u_int_type(this, U_INT_8, rules[i].offset);
494 break;
495 case ADDRESS:
496 case SPI:
497 case KEY_EXCHANGE_DATA:
498 case NOTIFICATION_DATA:
499 case NONCE_DATA:
500 case ID_DATA:
501 case AUTH_DATA:
502 case CERT_DATA:
503 case CERTREQ_DATA:
504 case SPIS:
505 case CONFIGURATION_ATTRIBUTE_VALUE:
506 case VID_DATA:
507 case EAP_DATA:
508 case ENCRYPTED_DATA:
509 case UNKNOWN_DATA:
510 generate_from_chunk(this, rules[i].offset);
511 break;
512 case PROPOSALS:
513 case TRANSFORMS:
514 case TRANSFORM_ATTRIBUTES:
515 case CONFIGURATION_ATTRIBUTES:
516 case TRAFFIC_SELECTORS:
517 {
518 linked_list_t *proposals;
519 enumerator_t *enumerator;
520 payload_t *proposal;
521
522 proposals = *((linked_list_t **)
523 (this->data_struct + rules[i].offset));
524 enumerator = proposals->create_enumerator(proposals);
525 while (enumerator->enumerate(enumerator, &proposal))
526 {
527 generate_payload(this, proposal);
528 }
529 enumerator->destroy(enumerator);
530 break;
531 }
532 case ATTRIBUTE_FORMAT:
533 generate_flag(this, rules[i].offset);
534 /* Attribute format is a flag which is stored in context*/
535 this->attribute_format =
536 *((bool *)(this->data_struct + rules[i].offset));
537 break;
538 case ATTRIBUTE_LENGTH_OR_VALUE:
539 if (this->attribute_format)
540 {
541 generate_u_int_type(this, U_INT_16, rules[i].offset);
542 }
543 else
544 {
545 generate_u_int_type(this, U_INT_16, rules[i].offset);
546 /* this field hold the length of the attribute */
547 this->attribute_length =
548 *((u_int16_t *)(this->data_struct + rules[i].offset));
549 }
550 break;
551 case ATTRIBUTE_VALUE:
552 {
553 if (!this->attribute_format)
554 {
555 DBG2(DBG_ENC, "attribute value has not fixed size");
556 /* the attribute value is generated */
557 generate_from_chunk(this, rules[i].offset);
558 }
559 break;
560 }
561 default:
562 DBG1(DBG_ENC, "field type %N is not supported",
563 encoding_type_names, rules[i].type);
564 return;
565 }
566 }
567 DBG2(DBG_ENC, "generating %N payload finished",
568 payload_type_names, payload_type);
569 DBG3(DBG_ENC, "generated data for this payload %b",
570 this->buffer + offset_start,
571 this->out_position - this->buffer - offset_start);
572 }
573
574 METHOD(generator_t, destroy, void,
575 private_generator_t *this)
576 {
577 free(this->buffer);
578 free(this);
579 }
580
581 /*
582 * Described in header
583 */
584 generator_t *generator_create()
585 {
586 private_generator_t *this;
587
588 INIT(this,
589 .public = {
590 .get_chunk = _get_chunk,
591 .generate_payload = _generate_payload,
592 .destroy = _destroy,
593 },
594 .buffer = malloc(GENERATOR_DATA_BUFFER_SIZE),
595 );
596
597 this->out_position = this->buffer;
598 this->roof_position = this->buffer + GENERATOR_DATA_BUFFER_SIZE;
599
600 return &this->public;
601 }
602