Added limiting encoding of IKEv1 SA payloads
[strongswan.git] / src / libcharon / encoding / payloads / proposal_substructure.c
1 /*
2 * Copyright (C) 2005-2010 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 <stddef.h>
18
19 #include "proposal_substructure.h"
20
21 #include <encoding/payloads/encodings.h>
22 #include <encoding/payloads/transform_substructure.h>
23 #include <library.h>
24 #include <utils/linked_list.h>
25 #include <daemon.h>
26
27 /**
28 * IKEv2 Value for a proposal payload.
29 */
30 #define PROPOSAL_TYPE_VALUE 2
31
32 typedef struct private_proposal_substructure_t private_proposal_substructure_t;
33
34 /**
35 * Private data of an proposal_substructure_t object.
36 */
37 struct private_proposal_substructure_t {
38
39 /**
40 * Public proposal_substructure_t interface.
41 */
42 proposal_substructure_t public;
43
44 /**
45 * Next payload type.
46 */
47 u_int8_t next_payload;
48
49 /**
50 * reserved byte
51 */
52 u_int8_t reserved;
53
54 /**
55 * Length of this payload.
56 */
57 u_int16_t proposal_length;
58
59 /**
60 * Proposal number.
61 */
62 u_int8_t proposal_number;
63
64 /**
65 * Protocol ID.
66 */
67 u_int8_t protocol_id;
68
69 /**
70 * SPI size of the following SPI.
71 */
72 u_int8_t spi_size;
73
74 /**
75 * Number of transforms.
76 */
77 u_int8_t transforms_count;
78
79 /**
80 * SPI is stored as chunk.
81 */
82 chunk_t spi;
83
84 /**
85 * Transforms are stored in a linked_list_t.
86 */
87 linked_list_t *transforms;
88
89 /**
90 * Type of this payload, PROPOSAL_SUBSTRUCTURE or PROPOSAL_SUBSTRUCTURE_V1
91 */
92 payload_type_t type;
93 };
94
95 /**
96 * Encoding rules for a IKEv1 Proposal substructure.
97 */
98 static encoding_rule_t encodings_v1[] = {
99 /* 1 Byte next payload type, stored in the field next_payload */
100 { U_INT_8, offsetof(private_proposal_substructure_t, next_payload) },
101 /* 1 Reserved Byte */
102 { RESERVED_BYTE, offsetof(private_proposal_substructure_t, reserved) },
103 /* Length of the whole proposal substructure payload*/
104 { PAYLOAD_LENGTH, offsetof(private_proposal_substructure_t, proposal_length) },
105 /* proposal number is a number of 8 bit */
106 { U_INT_8, offsetof(private_proposal_substructure_t, proposal_number) },
107 /* protocol ID is a number of 8 bit */
108 { U_INT_8, offsetof(private_proposal_substructure_t, protocol_id) },
109 /* SPI Size has its own type */
110 { SPI_SIZE, offsetof(private_proposal_substructure_t, spi_size) },
111 /* Number of transforms is a number of 8 bit */
112 { U_INT_8, offsetof(private_proposal_substructure_t, transforms_count) },
113 /* SPI is a chunk of variable size*/
114 { SPI, offsetof(private_proposal_substructure_t, spi) },
115 /* Transforms are stored in a transform substructure,
116 offset points to a linked_list_t pointer */
117 { TRANSFORMS_V1, offsetof(private_proposal_substructure_t, transforms) }
118 };
119
120 /**
121 * Encoding rules for a IKEv2 Proposal substructure.
122 */
123 static encoding_rule_t encodings_v2[] = {
124 /* 1 Byte next payload type, stored in the field next_payload */
125 { U_INT_8, offsetof(private_proposal_substructure_t, next_payload) },
126 /* 1 Reserved Byte */
127 { RESERVED_BYTE, offsetof(private_proposal_substructure_t, reserved) },
128 /* Length of the whole proposal substructure payload*/
129 { PAYLOAD_LENGTH, offsetof(private_proposal_substructure_t, proposal_length) },
130 /* proposal number is a number of 8 bit */
131 { U_INT_8, offsetof(private_proposal_substructure_t, proposal_number) },
132 /* protocol ID is a number of 8 bit */
133 { U_INT_8, offsetof(private_proposal_substructure_t, protocol_id) },
134 /* SPI Size has its own type */
135 { SPI_SIZE, offsetof(private_proposal_substructure_t, spi_size) },
136 /* Number of transforms is a number of 8 bit */
137 { U_INT_8, offsetof(private_proposal_substructure_t, transforms_count) },
138 /* SPI is a chunk of variable size*/
139 { SPI, offsetof(private_proposal_substructure_t, spi) },
140 /* Transforms are stored in a transform substructure,
141 offset points to a linked_list_t pointer */
142 { TRANSFORMS, offsetof(private_proposal_substructure_t, transforms) }
143 };
144
145 /*
146 1 2 3
147 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
148 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
149 ! 0 (last) or 2 ! RESERVED ! Proposal Length !
150 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
151 ! Proposal # ! Protocol ID ! SPI Size !# of Transforms!
152 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
153 ~ SPI (variable) ~
154 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
155 ! !
156 ~ <Transforms> ~
157 ! !
158 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
159 */
160
161 /**
162 * Encryption.
163 */
164 typedef enum {
165 IKEV1_ENCR_DES_CBC = 1,
166 IKEV1_ENCR_IDEA_CBC = 2,
167 IKEV1_ENCR_BLOWFISH_CBC = 3,
168 IKEV1_ENCR_RC5_R16_B64_CBC = 4,
169 IKEV1_ENCR_3DES_CBC = 5,
170 IKEV1_ENCR_CAST_CBC = 6,
171 IKEV1_ENCR_AES_CBC = 7,
172 IKEV1_ENCR_CAMELLIA_CBC = 8,
173 IKEV1_ENCR_LAST = 9,
174 } ikev1_encryption_t;
175
176 /**
177 * IKEv1 hash.
178 */
179 typedef enum {
180 IKEV1_HASH_MD5 = 1,
181 IKEV1_HASH_SHA1 = 2,
182 IKEV1_HASH_TIGER = 3,
183 IKEV1_HASH_SHA2_256 = 4,
184 IKEV1_HASH_SHA2_384 = 5,
185 IKEV1_HASH_SHA2_512 = 6,
186 } ikev1_hash_t;
187
188 /**
189 * IKEv1 Transform ID IKE.
190 */
191 typedef enum {
192 IKEV1_TRANSID_KEY_IKE = 1,
193 } ikev1_ike_transid_t;
194
195 /**
196 * IKEv1 Transform ID ESP.
197 */
198 typedef enum {
199 IKEV1_TRANSID_ESP_DES_IV64 = 1,
200 IKEV1_TRANSID_ESP_DES = 2,
201 IKEV1_TRANSID_ESP_3DES = 3,
202 IKEV1_TRANSID_ESP_RC5 = 4,
203 IKEV1_TRANSID_ESP_IDEA = 5,
204 IKEV1_TRANSID_ESP_CAST = 6,
205 IKEV1_TRANSID_ESP_BLOWFISH = 7,
206 IKEV1_TRANSID_ESP_3IDEA = 8,
207 IKEV1_TRANSID_ESP_DES_IV32 = 9,
208 IKEV1_TRANSID_ESP_RC4 = 10,
209 IKEV1_TRANSID_ESP_NULL = 11,
210 IKEV1_TRANSID_ESP_AES_CBC = 12,
211 } ikev1_esp_transid_t;
212
213 /**
214 * IKEv1 ESP Encapsulation mode.
215 */
216 typedef enum {
217 IKEV1_ENCAP_TUNNEL = 1,
218 IKEV1_ENCAP_TRANSPORT = 2,
219 IKEV1_ENCAP_UDP_TUNNEL = 3,
220 IKEV1_ENCAP_UDP_TRANSPORT = 4,
221 } ikev1_esp_encap_t;
222
223 /**
224 * IKEv1 Life duration types.
225 */
226 typedef enum {
227 IKEV1_LIFE_TYPE_SECONDS = 1,
228 IKEV1_LIFE_TYPE_KILOBYTES = 2,
229 } ikev1_life_type_t;
230
231 METHOD(payload_t, verify, status_t,
232 private_proposal_substructure_t *this)
233 {
234 status_t status = SUCCESS;
235 enumerator_t *enumerator;
236 payload_t *current;
237
238 if (this->next_payload != NO_PAYLOAD && this->next_payload != 2)
239 {
240 /* must be 0 or 2 */
241 DBG1(DBG_ENC, "inconsistent next payload");
242 return FAILED;
243 }
244 if (this->transforms_count != this->transforms->get_count(this->transforms))
245 {
246 /* must be the same! */
247 DBG1(DBG_ENC, "transform count invalid");
248 return FAILED;
249 }
250
251 switch (this->protocol_id)
252 {
253 case PROTO_AH:
254 case PROTO_ESP:
255 if (this->spi.len != 4)
256 {
257 DBG1(DBG_ENC, "invalid SPI length in %N proposal",
258 protocol_id_names, this->protocol_id);
259 return FAILED;
260 }
261 break;
262 case PROTO_IKE:
263 if (this->spi.len != 0 && this->spi.len != 8)
264 {
265 DBG1(DBG_ENC, "invalid SPI length in IKE proposal");
266 return FAILED;
267 }
268 break;
269 default:
270 break;
271 }
272 enumerator = this->transforms->create_enumerator(this->transforms);
273 while (enumerator->enumerate(enumerator, &current))
274 {
275 status = current->verify(current);
276 if (status != SUCCESS)
277 {
278 DBG1(DBG_ENC, "TRANSFORM_SUBSTRUCTURE verification failed");
279 break;
280 }
281 }
282 enumerator->destroy(enumerator);
283
284 /* proposal number is checked in SA payload */
285 return status;
286 }
287
288 METHOD(payload_t, get_encoding_rules, void,
289 private_proposal_substructure_t *this, encoding_rule_t **rules,
290 size_t *rule_count)
291 {
292 if (this->type == PROPOSAL_SUBSTRUCTURE)
293 {
294 *rules = encodings_v2;
295 *rule_count = countof(encodings_v2);
296 }
297 else
298 {
299 *rules = encodings_v1;
300 *rule_count = countof(encodings_v1);
301 }
302 }
303
304 METHOD(payload_t, get_type, payload_type_t,
305 private_proposal_substructure_t *this)
306 {
307 return this->type;
308 }
309
310 METHOD(payload_t, get_next_type, payload_type_t,
311 private_proposal_substructure_t *this)
312 {
313 return this->next_payload;
314 }
315
316 METHOD(payload_t, set_next_type, void,
317 private_proposal_substructure_t *this, payload_type_t type)
318 {
319 }
320
321 /**
322 * (re-)compute the length of the payload.
323 */
324 static void compute_length(private_proposal_substructure_t *this)
325 {
326 enumerator_t *enumerator;
327 payload_t *transform;
328
329 this->transforms_count = 0;
330 this->proposal_length = PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH + this->spi.len;
331 enumerator = this->transforms->create_enumerator(this->transforms);
332 while (enumerator->enumerate(enumerator, &transform))
333 {
334 this->proposal_length += transform->get_length(transform);
335 this->transforms_count++;
336 }
337 enumerator->destroy(enumerator);
338 }
339
340 METHOD(payload_t, get_length, size_t,
341 private_proposal_substructure_t *this)
342 {
343 return this->proposal_length;
344 }
345
346 /**
347 * Add a transform substructure to the proposal
348 */
349 static void add_transform_substructure(private_proposal_substructure_t *this,
350 transform_substructure_t *transform)
351 {
352 if (this->transforms->get_count(this->transforms) > 0)
353 {
354 transform_substructure_t *last;
355
356 this->transforms->get_last(this->transforms, (void **)&last);
357 last->set_is_last_transform(last, FALSE);
358 }
359 transform->set_is_last_transform(transform,TRUE);
360 this->transforms->insert_last(this->transforms, transform);
361 compute_length(this);
362 }
363
364 METHOD(proposal_substructure_t, set_is_last_proposal, void,
365 private_proposal_substructure_t *this, bool is_last)
366 {
367 this->next_payload = is_last ? 0 : PROPOSAL_TYPE_VALUE;
368 }
369
370 METHOD(proposal_substructure_t, set_proposal_number, void,
371 private_proposal_substructure_t *this,u_int8_t proposal_number)
372 {
373 this->proposal_number = proposal_number;
374 }
375
376 METHOD(proposal_substructure_t, get_proposal_number, u_int8_t,
377 private_proposal_substructure_t *this)
378 {
379 return this->proposal_number;
380 }
381
382 METHOD(proposal_substructure_t, set_protocol_id, void,
383 private_proposal_substructure_t *this,u_int8_t protocol_id)
384 {
385 this->protocol_id = protocol_id;
386 }
387
388 METHOD(proposal_substructure_t, get_protocol_id, u_int8_t,
389 private_proposal_substructure_t *this)
390 {
391 return this->protocol_id;
392 }
393
394 METHOD(proposal_substructure_t, set_spi, void,
395 private_proposal_substructure_t *this, chunk_t spi)
396 {
397 free(this->spi.ptr);
398 this->spi = chunk_clone(spi);
399 this->spi_size = spi.len;
400 compute_length(this);
401 }
402
403 METHOD(proposal_substructure_t, get_spi, chunk_t,
404 private_proposal_substructure_t *this)
405 {
406 return this->spi;
407 }
408
409 /**
410 * Add a transform to a proposal for IKEv2
411 */
412 static void add_to_proposal_v2(proposal_t *proposal,
413 transform_substructure_t *transform)
414 {
415 transform_attribute_t *tattr;
416 enumerator_t *enumerator;
417 u_int16_t key_length = 0;
418
419 enumerator = transform->create_attribute_enumerator(transform);
420 while (enumerator->enumerate(enumerator, &tattr))
421 {
422 if (tattr->get_attribute_type(tattr) == TATTR_IKEV2_KEY_LENGTH)
423 {
424 key_length = tattr->get_value(tattr);
425 break;
426 }
427 }
428 enumerator->destroy(enumerator);
429
430 proposal->add_algorithm(proposal,
431 transform->get_transform_type_or_number(transform),
432 transform->get_transform_id(transform), key_length);
433 }
434
435 /**
436 * Map IKEv1 to IKEv2 algorithms
437 */
438 typedef struct {
439 u_int16_t ikev1;
440 u_int16_t ikev2;
441 } algo_map_t;
442
443 /**
444 * Encryption algorithm mapping
445 */
446 static algo_map_t map_encr[] = {
447 { IKEV1_ENCR_DES_CBC, ENCR_DES },
448 { IKEV1_ENCR_IDEA_CBC, ENCR_IDEA },
449 { IKEV1_ENCR_BLOWFISH_CBC, ENCR_BLOWFISH },
450 { IKEV1_ENCR_3DES_CBC, ENCR_3DES },
451 { IKEV1_ENCR_CAST_CBC, ENCR_CAST },
452 { IKEV1_ENCR_AES_CBC, ENCR_AES_CBC },
453 { IKEV1_ENCR_CAMELLIA_CBC, ENCR_CAMELLIA_CBC },
454 };
455
456 /**
457 * Integrity algorithm mapping
458 */
459 static algo_map_t map_integ[] = {
460 { IKEV1_HASH_MD5, AUTH_HMAC_MD5_96 },
461 { IKEV1_HASH_SHA1, AUTH_HMAC_SHA1_96 },
462 { IKEV1_HASH_SHA2_256, AUTH_HMAC_SHA2_256_128 },
463 { IKEV1_HASH_SHA2_384, AUTH_HMAC_SHA2_384_192 },
464 { IKEV1_HASH_SHA2_512, AUTH_HMAC_SHA2_512_256 },
465 };
466
467 /**
468 * PRF algorithm mapping
469 */
470 static algo_map_t map_prf[] = {
471 { IKEV1_HASH_MD5, PRF_HMAC_MD5 },
472 { IKEV1_HASH_SHA1, PRF_HMAC_SHA1 },
473 { IKEV1_HASH_SHA2_256, PRF_HMAC_SHA2_256 },
474 { IKEV1_HASH_SHA2_384, PRF_HMAC_SHA2_384 },
475 { IKEV1_HASH_SHA2_512, PRF_HMAC_SHA2_512 },
476 };
477
478 /**
479 * Get IKEv2 algorithm from IKEv1 identifier
480 */
481 static u_int16_t get_alg_from_ikev1(transform_type_t type, u_int16_t value)
482 {
483 algo_map_t *map;
484 u_int16_t def;
485 int i, count;
486
487 switch (type)
488 {
489 case ENCRYPTION_ALGORITHM:
490 map = map_encr;
491 count = countof(map_encr);
492 def = ENCR_UNDEFINED;
493 break;
494 case INTEGRITY_ALGORITHM:
495 map = map_integ;
496 count = countof(map_integ);
497 def = AUTH_UNDEFINED;
498 break;
499 case PSEUDO_RANDOM_FUNCTION:
500 map = map_prf;
501 count = countof(map_prf);
502 def = PRF_UNDEFINED;
503 break;
504 default:
505 return 0;
506 }
507 for (i = 0; i < count; i++)
508 {
509 if (map[i].ikev1 == value)
510 {
511 return map[i].ikev2;
512 }
513 }
514 return def;
515 }
516
517 /**
518 * Get IKEv1 algorithm from IKEv2 identifier
519 */
520 static u_int16_t get_ikev1_from_alg(transform_type_t type, u_int16_t value)
521 {
522 algo_map_t *map;
523 int i, count;
524
525 switch (type)
526 {
527 case ENCRYPTION_ALGORITHM:
528 map = map_encr;
529 count = countof(map_encr);
530 break;
531 case INTEGRITY_ALGORITHM:
532 map = map_integ;
533 count = countof(map_integ);
534 break;
535 case PSEUDO_RANDOM_FUNCTION:
536 map = map_prf;
537 count = countof(map_prf);
538 break;
539 default:
540 return 0;
541 }
542 for (i = 0; i < count; i++)
543 {
544 if (map[i].ikev2 == value)
545 {
546 return map[i].ikev1;
547 }
548 }
549 return 0;
550 }
551
552 /**
553 * Add an IKE transform to a proposal for IKEv1
554 */
555 static void add_to_proposal_v1_ike(proposal_t *proposal,
556 transform_substructure_t *transform)
557 {
558 transform_attribute_type_t type;
559 transform_attribute_t *tattr;
560 enumerator_t *enumerator;
561 u_int16_t value, key_length = 0;
562 u_int16_t encr = ENCR_UNDEFINED;
563
564 enumerator = transform->create_attribute_enumerator(transform);
565 while (enumerator->enumerate(enumerator, &tattr))
566 {
567 type = tattr->get_attribute_type(tattr);
568 value = tattr->get_value(tattr);
569 switch (type)
570 {
571 case TATTR_PH1_ENCRYPTION_ALGORITHM:
572 encr = get_alg_from_ikev1(ENCRYPTION_ALGORITHM, value);
573 break;
574 case TATTR_PH1_KEY_LENGTH:
575 key_length = value;
576 break;
577 case TATTR_PH1_HASH_ALGORITHM:
578 proposal->add_algorithm(proposal, INTEGRITY_ALGORITHM,
579 get_alg_from_ikev1(INTEGRITY_ALGORITHM, value), 0);
580 proposal->add_algorithm(proposal, PSEUDO_RANDOM_FUNCTION,
581 get_alg_from_ikev1(PSEUDO_RANDOM_FUNCTION, value), 0);
582 break;
583 case TATTR_PH1_GROUP:
584 proposal->add_algorithm(proposal, DIFFIE_HELLMAN_GROUP,
585 value, 0);
586 break;
587 default:
588 /* TODO-IKEv1: lifetimes, authentication and other attributes */
589 break;
590 }
591 }
592 enumerator->destroy(enumerator);
593
594 if (encr != ENCR_UNDEFINED)
595 {
596 proposal->add_algorithm(proposal, ENCRYPTION_ALGORITHM, encr, key_length);
597 }
598 }
599
600 /**
601 * Add an ESP transform to a proposal for IKEv1
602 */
603 static void add_to_proposal_v1_esp(proposal_t *proposal,
604 transform_substructure_t *transform)
605 {
606 /* TODO-IKEv1: create ESP proposals */
607 }
608
609 METHOD(proposal_substructure_t, get_proposal, proposal_t*,
610 private_proposal_substructure_t *this)
611 {
612 transform_substructure_t *transform;
613 enumerator_t *enumerator;
614 proposal_t *proposal;
615
616 proposal = proposal_create(this->protocol_id, this->proposal_number);
617
618 enumerator = this->transforms->create_enumerator(this->transforms);
619 while (enumerator->enumerate(enumerator, &transform))
620 {
621 if (this->type == PROPOSAL_SUBSTRUCTURE)
622 {
623 add_to_proposal_v2(proposal, transform);
624 }
625 else
626 {
627 switch (this->protocol_id)
628 {
629 case PROTO_IKE:
630 add_to_proposal_v1_ike(proposal, transform);
631 break;
632 case PROTO_ESP:
633 add_to_proposal_v1_esp(proposal, transform);
634 break;
635 default:
636 break;
637 }
638 /* TODO-IKEv1: We currently accept the first set of transforms
639 * in a substructure only. We need to return multiple proposals,
640 * but this messes up proposal numbering, as we don't support
641 * transform numbering. */
642 break;
643 }
644 }
645 enumerator->destroy(enumerator);
646
647 switch (this->spi.len)
648 {
649 case 4:
650 proposal->set_spi(proposal, *((u_int32_t*)this->spi.ptr));
651 break;
652 case 8:
653 proposal->set_spi(proposal, *((u_int64_t*)this->spi.ptr));
654 break;
655 default:
656 break;
657 }
658 return proposal;
659 }
660
661 METHOD(proposal_substructure_t, create_substructure_enumerator, enumerator_t*,
662 private_proposal_substructure_t *this)
663 {
664 return this->transforms->create_enumerator(this->transforms);
665 }
666
667 METHOD2(payload_t, proposal_substructure_t, destroy, void,
668 private_proposal_substructure_t *this)
669 {
670 this->transforms->destroy_offset(this->transforms,
671 offsetof(payload_t, destroy));
672 chunk_free(&this->spi);
673 free(this);
674 }
675
676 /*
677 * Described in header.
678 */
679 proposal_substructure_t *proposal_substructure_create(payload_type_t type)
680 {
681 private_proposal_substructure_t *this;
682
683 INIT(this,
684 .public = {
685 .payload_interface = {
686 .verify = _verify,
687 .get_encoding_rules = _get_encoding_rules,
688 .get_length = _get_length,
689 .get_next_type = _get_next_type,
690 .set_next_type = _set_next_type,
691 .get_type = _get_type,
692 .destroy = _destroy,
693 },
694 .set_proposal_number = _set_proposal_number,
695 .get_proposal_number = _get_proposal_number,
696 .set_protocol_id = _set_protocol_id,
697 .get_protocol_id = _get_protocol_id,
698 .set_is_last_proposal = _set_is_last_proposal,
699 .get_proposal = _get_proposal,
700 .create_substructure_enumerator = _create_substructure_enumerator,
701 .set_spi = _set_spi,
702 .get_spi = _get_spi,
703 .destroy = _destroy,
704 },
705 .next_payload = NO_PAYLOAD,
706 .proposal_length = PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH,
707 .transforms = linked_list_create(),
708 .type = type,
709 );
710
711 return &this->public;
712 }
713
714 /**
715 * Add an IKEv1 IKE proposal to the substructure
716 */
717 static void set_from_proposal_v1_ike(private_proposal_substructure_t *this,
718 proposal_t *proposal)
719 {
720 transform_substructure_t *transform;
721 u_int16_t alg, key_size;
722 enumerator_t *enumerator;
723
724 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE_V1,
725 0, IKEV1_TRANSID_KEY_IKE);
726
727 enumerator = proposal->create_enumerator(proposal, ENCRYPTION_ALGORITHM);
728 while (enumerator->enumerate(enumerator, &alg, &key_size))
729 {
730 alg = get_ikev1_from_alg(ENCRYPTION_ALGORITHM, alg);
731 if (alg)
732 {
733 transform->add_transform_attribute(transform,
734 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
735 TATTR_PH1_ENCRYPTION_ALGORITHM, alg));
736 if (key_size)
737 {
738 transform->add_transform_attribute(transform,
739 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
740 TATTR_PH1_KEY_LENGTH, key_size));
741 }
742 }
743 }
744 enumerator->destroy(enumerator);
745
746 /* encode the integrity algorithm as hash and assume use the same PRF */
747 enumerator = proposal->create_enumerator(proposal, INTEGRITY_ALGORITHM);
748 while (enumerator->enumerate(enumerator, &alg, &key_size))
749 {
750 alg = get_ikev1_from_alg(INTEGRITY_ALGORITHM, alg);
751 if (alg)
752 {
753 transform->add_transform_attribute(transform,
754 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
755 TATTR_PH1_HASH_ALGORITHM, alg));
756 }
757 }
758 enumerator->destroy(enumerator);
759
760 enumerator = proposal->create_enumerator(proposal, DIFFIE_HELLMAN_GROUP);
761 while (enumerator->enumerate(enumerator, &alg, &key_size))
762 {
763 transform->add_transform_attribute(transform,
764 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
765 TATTR_PH1_GROUP, alg));
766 }
767 enumerator->destroy(enumerator);
768
769 /* TODO-IKEv1: Add lifetime, auth and other attributes */
770
771 add_transform_substructure(this, transform);
772 }
773
774 /**
775 * Add an IKEv1 ESP proposal to the substructure
776 */
777 static void set_from_proposal_v1_esp(private_proposal_substructure_t *this,
778 proposal_t *proposal)
779 {
780 /* TODO-IKEv1: add ESP proposal to transform substr */
781 }
782
783 /**
784 * Add an IKEv2 proposal to the substructure
785 */
786 static void set_from_proposal_v2(private_proposal_substructure_t *this,
787 proposal_t *proposal)
788 {
789 transform_substructure_t *transform;
790 u_int16_t alg, key_size;
791 enumerator_t *enumerator;
792
793 /* encryption algorithm is only available in ESP */
794 enumerator = proposal->create_enumerator(proposal, ENCRYPTION_ALGORITHM);
795 while (enumerator->enumerate(enumerator, &alg, &key_size))
796 {
797 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE,
798 ENCRYPTION_ALGORITHM, alg);
799 if (key_size)
800 {
801 transform->add_transform_attribute(transform,
802 transform_attribute_create_value(TRANSFORM_ATTRIBUTE,
803 TATTR_IKEV2_KEY_LENGTH, key_size));
804 }
805 add_transform_substructure(this, transform);
806 }
807 enumerator->destroy(enumerator);
808
809 /* integrity algorithms */
810 enumerator = proposal->create_enumerator(proposal, INTEGRITY_ALGORITHM);
811 while (enumerator->enumerate(enumerator, &alg, &key_size))
812 {
813 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE,
814 INTEGRITY_ALGORITHM, alg);
815 add_transform_substructure(this, transform);
816 }
817 enumerator->destroy(enumerator);
818
819 /* prf algorithms */
820 enumerator = proposal->create_enumerator(proposal, PSEUDO_RANDOM_FUNCTION);
821 while (enumerator->enumerate(enumerator, &alg, &key_size))
822 {
823 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE,
824 PSEUDO_RANDOM_FUNCTION, alg);
825 add_transform_substructure(this, transform);
826 }
827 enumerator->destroy(enumerator);
828
829 /* dh groups */
830 enumerator = proposal->create_enumerator(proposal, DIFFIE_HELLMAN_GROUP);
831 while (enumerator->enumerate(enumerator, &alg, NULL))
832 {
833 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE,
834 DIFFIE_HELLMAN_GROUP, alg);
835 add_transform_substructure(this, transform);
836 }
837 enumerator->destroy(enumerator);
838
839 /* extended sequence numbers */
840 enumerator = proposal->create_enumerator(proposal, EXTENDED_SEQUENCE_NUMBERS);
841 while (enumerator->enumerate(enumerator, &alg, NULL))
842 {
843 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE,
844 EXTENDED_SEQUENCE_NUMBERS, alg);
845 add_transform_substructure(this, transform);
846 }
847 enumerator->destroy(enumerator);
848 }
849
850 /*
851 * Described in header.
852 */
853 proposal_substructure_t *proposal_substructure_create_from_proposal(
854 payload_type_t type, proposal_t *proposal)
855 {
856 private_proposal_substructure_t *this;
857 u_int64_t spi64;
858 u_int32_t spi32;
859
860 this = (private_proposal_substructure_t*)proposal_substructure_create(type);
861
862 if (type == PROPOSAL_SUBSTRUCTURE)
863 {
864 set_from_proposal_v2(this, proposal);
865 }
866 else
867 {
868 switch (proposal->get_protocol(proposal))
869 {
870 case PROTO_IKE:
871 set_from_proposal_v1_ike(this, proposal);
872 break;
873 case PROTO_ESP:
874 set_from_proposal_v1_esp(this, proposal);
875 break;
876 default:
877 break;
878 }
879 }
880 /* add SPI, if necessary */
881 switch (proposal->get_protocol(proposal))
882 {
883 case PROTO_AH:
884 case PROTO_ESP:
885 spi32 = proposal->get_spi(proposal);
886 this->spi = chunk_clone(chunk_from_thing(spi32));
887 this->spi_size = this->spi.len;
888 break;
889 case PROTO_IKE:
890 spi64 = proposal->get_spi(proposal);
891 if (spi64)
892 { /* IKE only uses SPIS when rekeying, but on initial setup */
893 this->spi = chunk_clone(chunk_from_thing(spi64));
894 this->spi_size = this->spi.len;
895 }
896 break;
897 default:
898 break;
899 }
900 this->proposal_number = proposal->get_number(proposal);
901 this->protocol_id = proposal->get_protocol(proposal);
902 compute_length(this);
903
904 return &this->public;
905 }