Added missing comma after ME_CONNECT declaration.
[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 list */
116 { PAYLOAD_LIST + TRANSFORM_SUBSTRUCTURE_V1,
117 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 list */
141 { PAYLOAD_LIST + TRANSFORM_SUBSTRUCTURE,
142 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 /**
232 * IKEv1 authenticaiton methods
233 */
234 typedef enum {
235 IKEV1_AUTH_PSK = 1,
236 IKEV1_AUTH_DSS_SIG = 2,
237 IKEV1_AUTH_RSA_SIG = 3,
238 IKEV1_AUTH_RSA_ENC = 4,
239 IKEV1_AUTH_RSA_ENC_REV = 5,
240 } ikev1_auth_method_t;
241
242 METHOD(payload_t, verify, status_t,
243 private_proposal_substructure_t *this)
244 {
245 status_t status = SUCCESS;
246 enumerator_t *enumerator;
247 payload_t *current;
248
249 if (this->next_payload != NO_PAYLOAD && this->next_payload != 2)
250 {
251 /* must be 0 or 2 */
252 DBG1(DBG_ENC, "inconsistent next payload");
253 return FAILED;
254 }
255 if (this->transforms_count != this->transforms->get_count(this->transforms))
256 {
257 /* must be the same! */
258 DBG1(DBG_ENC, "transform count invalid");
259 return FAILED;
260 }
261
262 switch (this->protocol_id)
263 {
264 case PROTO_AH:
265 case PROTO_ESP:
266 if (this->spi.len != 4)
267 {
268 DBG1(DBG_ENC, "invalid SPI length in %N proposal",
269 protocol_id_names, this->protocol_id);
270 return FAILED;
271 }
272 break;
273 case PROTO_IKE:
274 if (this->spi.len != 0 && this->spi.len != 8)
275 {
276 DBG1(DBG_ENC, "invalid SPI length in IKE proposal");
277 return FAILED;
278 }
279 break;
280 default:
281 break;
282 }
283 enumerator = this->transforms->create_enumerator(this->transforms);
284 while (enumerator->enumerate(enumerator, &current))
285 {
286 status = current->verify(current);
287 if (status != SUCCESS)
288 {
289 DBG1(DBG_ENC, "TRANSFORM_SUBSTRUCTURE verification failed");
290 break;
291 }
292 }
293 enumerator->destroy(enumerator);
294
295 /* proposal number is checked in SA payload */
296 return status;
297 }
298
299 METHOD(payload_t, get_encoding_rules, int,
300 private_proposal_substructure_t *this, encoding_rule_t **rules)
301 {
302 if (this->type == PROPOSAL_SUBSTRUCTURE)
303 {
304 *rules = encodings_v2;
305 return countof(encodings_v2);
306 }
307 *rules = encodings_v1;
308 return countof(encodings_v1);
309 }
310
311 METHOD(payload_t, get_header_length, int,
312 private_proposal_substructure_t *this)
313 {
314 return 8 + this->spi_size;
315 }
316
317 METHOD(payload_t, get_type, payload_type_t,
318 private_proposal_substructure_t *this)
319 {
320 return this->type;
321 }
322
323 METHOD(payload_t, get_next_type, payload_type_t,
324 private_proposal_substructure_t *this)
325 {
326 return this->next_payload;
327 }
328
329 METHOD(payload_t, set_next_type, void,
330 private_proposal_substructure_t *this, payload_type_t type)
331 {
332 }
333
334 /**
335 * (re-)compute the length of the payload.
336 */
337 static void compute_length(private_proposal_substructure_t *this)
338 {
339 enumerator_t *enumerator;
340 payload_t *transform;
341
342 this->transforms_count = 0;
343 this->proposal_length = get_header_length(this);
344 enumerator = this->transforms->create_enumerator(this->transforms);
345 while (enumerator->enumerate(enumerator, &transform))
346 {
347 this->proposal_length += transform->get_length(transform);
348 this->transforms_count++;
349 }
350 enumerator->destroy(enumerator);
351 }
352
353 METHOD(payload_t, get_length, size_t,
354 private_proposal_substructure_t *this)
355 {
356 return this->proposal_length;
357 }
358
359 /**
360 * Add a transform substructure to the proposal
361 */
362 static void add_transform_substructure(private_proposal_substructure_t *this,
363 transform_substructure_t *transform)
364 {
365 if (this->transforms->get_count(this->transforms) > 0)
366 {
367 transform_substructure_t *last;
368
369 this->transforms->get_last(this->transforms, (void **)&last);
370 last->set_is_last_transform(last, FALSE);
371 }
372 transform->set_is_last_transform(transform,TRUE);
373 this->transforms->insert_last(this->transforms, transform);
374 compute_length(this);
375 }
376
377 METHOD(proposal_substructure_t, set_is_last_proposal, void,
378 private_proposal_substructure_t *this, bool is_last)
379 {
380 this->next_payload = is_last ? 0 : PROPOSAL_TYPE_VALUE;
381 }
382
383 METHOD(proposal_substructure_t, set_proposal_number, void,
384 private_proposal_substructure_t *this,u_int8_t proposal_number)
385 {
386 this->proposal_number = proposal_number;
387 }
388
389 METHOD(proposal_substructure_t, get_proposal_number, u_int8_t,
390 private_proposal_substructure_t *this)
391 {
392 return this->proposal_number;
393 }
394
395 METHOD(proposal_substructure_t, set_protocol_id, void,
396 private_proposal_substructure_t *this,u_int8_t protocol_id)
397 {
398 this->protocol_id = protocol_id;
399 }
400
401 METHOD(proposal_substructure_t, get_protocol_id, u_int8_t,
402 private_proposal_substructure_t *this)
403 {
404 return this->protocol_id;
405 }
406
407 METHOD(proposal_substructure_t, set_spi, void,
408 private_proposal_substructure_t *this, chunk_t spi)
409 {
410 free(this->spi.ptr);
411 this->spi = chunk_clone(spi);
412 this->spi_size = spi.len;
413 compute_length(this);
414 }
415
416 METHOD(proposal_substructure_t, get_spi, chunk_t,
417 private_proposal_substructure_t *this)
418 {
419 return this->spi;
420 }
421
422 /**
423 * Add a transform to a proposal for IKEv2
424 */
425 static void add_to_proposal_v2(proposal_t *proposal,
426 transform_substructure_t *transform)
427 {
428 transform_attribute_t *tattr;
429 enumerator_t *enumerator;
430 u_int16_t key_length = 0;
431
432 enumerator = transform->create_attribute_enumerator(transform);
433 while (enumerator->enumerate(enumerator, &tattr))
434 {
435 if (tattr->get_attribute_type(tattr) == TATTR_IKEV2_KEY_LENGTH)
436 {
437 key_length = tattr->get_value(tattr);
438 break;
439 }
440 }
441 enumerator->destroy(enumerator);
442
443 proposal->add_algorithm(proposal,
444 transform->get_transform_type_or_number(transform),
445 transform->get_transform_id(transform), key_length);
446 }
447
448 /**
449 * Map IKEv1 to IKEv2 algorithms
450 */
451 typedef struct {
452 u_int16_t ikev1;
453 u_int16_t ikev2;
454 } algo_map_t;
455
456 /**
457 * Encryption algorithm mapping
458 */
459 static algo_map_t map_encr[] = {
460 { IKEV1_ENCR_DES_CBC, ENCR_DES },
461 { IKEV1_ENCR_IDEA_CBC, ENCR_IDEA },
462 { IKEV1_ENCR_BLOWFISH_CBC, ENCR_BLOWFISH },
463 { IKEV1_ENCR_3DES_CBC, ENCR_3DES },
464 { IKEV1_ENCR_CAST_CBC, ENCR_CAST },
465 { IKEV1_ENCR_AES_CBC, ENCR_AES_CBC },
466 { IKEV1_ENCR_CAMELLIA_CBC, ENCR_CAMELLIA_CBC },
467 };
468
469 /**
470 * Integrity algorithm mapping
471 */
472 static algo_map_t map_integ[] = {
473 { IKEV1_HASH_MD5, AUTH_HMAC_MD5_96 },
474 { IKEV1_HASH_SHA1, AUTH_HMAC_SHA1_96 },
475 { IKEV1_HASH_SHA2_256, AUTH_HMAC_SHA2_256_128 },
476 { IKEV1_HASH_SHA2_384, AUTH_HMAC_SHA2_384_192 },
477 { IKEV1_HASH_SHA2_512, AUTH_HMAC_SHA2_512_256 },
478 };
479
480 /**
481 * PRF algorithm mapping
482 */
483 static algo_map_t map_prf[] = {
484 { IKEV1_HASH_MD5, PRF_HMAC_MD5 },
485 { IKEV1_HASH_SHA1, PRF_HMAC_SHA1 },
486 { IKEV1_HASH_SHA2_256, PRF_HMAC_SHA2_256 },
487 { IKEV1_HASH_SHA2_384, PRF_HMAC_SHA2_384 },
488 { IKEV1_HASH_SHA2_512, PRF_HMAC_SHA2_512 },
489 };
490
491 /**
492 * Get IKEv2 algorithm from IKEv1 identifier
493 */
494 static u_int16_t get_alg_from_ikev1(transform_type_t type, u_int16_t value)
495 {
496 algo_map_t *map;
497 u_int16_t def;
498 int i, count;
499
500 switch (type)
501 {
502 case ENCRYPTION_ALGORITHM:
503 map = map_encr;
504 count = countof(map_encr);
505 def = ENCR_UNDEFINED;
506 break;
507 case INTEGRITY_ALGORITHM:
508 map = map_integ;
509 count = countof(map_integ);
510 def = AUTH_UNDEFINED;
511 break;
512 case PSEUDO_RANDOM_FUNCTION:
513 map = map_prf;
514 count = countof(map_prf);
515 def = PRF_UNDEFINED;
516 break;
517 default:
518 return 0;
519 }
520 for (i = 0; i < count; i++)
521 {
522 if (map[i].ikev1 == value)
523 {
524 return map[i].ikev2;
525 }
526 }
527 return def;
528 }
529
530 /**
531 * Get IKEv1 algorithm from IKEv2 identifier
532 */
533 static u_int16_t get_ikev1_from_alg(transform_type_t type, u_int16_t value)
534 {
535 algo_map_t *map;
536 int i, count;
537
538 switch (type)
539 {
540 case ENCRYPTION_ALGORITHM:
541 map = map_encr;
542 count = countof(map_encr);
543 break;
544 case INTEGRITY_ALGORITHM:
545 map = map_integ;
546 count = countof(map_integ);
547 break;
548 case PSEUDO_RANDOM_FUNCTION:
549 map = map_prf;
550 count = countof(map_prf);
551 break;
552 default:
553 return 0;
554 }
555 for (i = 0; i < count; i++)
556 {
557 if (map[i].ikev2 == value)
558 {
559 return map[i].ikev1;
560 }
561 }
562 return 0;
563 }
564
565 /**
566 * Add an IKE transform to a proposal for IKEv1
567 */
568 static void add_to_proposal_v1_ike(proposal_t *proposal,
569 transform_substructure_t *transform)
570 {
571 transform_attribute_type_t type;
572 transform_attribute_t *tattr;
573 enumerator_t *enumerator;
574 u_int16_t value, key_length = 0;
575 u_int16_t encr = ENCR_UNDEFINED;
576
577 enumerator = transform->create_attribute_enumerator(transform);
578 while (enumerator->enumerate(enumerator, &tattr))
579 {
580 type = tattr->get_attribute_type(tattr);
581 value = tattr->get_value(tattr);
582 switch (type)
583 {
584 case TATTR_PH1_ENCRYPTION_ALGORITHM:
585 encr = get_alg_from_ikev1(ENCRYPTION_ALGORITHM, value);
586 break;
587 case TATTR_PH1_KEY_LENGTH:
588 key_length = value;
589 break;
590 case TATTR_PH1_HASH_ALGORITHM:
591 proposal->add_algorithm(proposal, INTEGRITY_ALGORITHM,
592 get_alg_from_ikev1(INTEGRITY_ALGORITHM, value), 0);
593 proposal->add_algorithm(proposal, PSEUDO_RANDOM_FUNCTION,
594 get_alg_from_ikev1(PSEUDO_RANDOM_FUNCTION, value), 0);
595 break;
596 case TATTR_PH1_GROUP:
597 proposal->add_algorithm(proposal, DIFFIE_HELLMAN_GROUP,
598 value, 0);
599 break;
600 default:
601 /* TODO-IKEv1: lifetimes, authentication and other attributes */
602 break;
603 }
604 }
605 enumerator->destroy(enumerator);
606
607 if (encr != ENCR_UNDEFINED)
608 {
609 proposal->add_algorithm(proposal, ENCRYPTION_ALGORITHM, encr, key_length);
610 }
611 }
612
613 /**
614 * Add an ESP transform to a proposal for IKEv1
615 */
616 static void add_to_proposal_v1_esp(proposal_t *proposal,
617 transform_substructure_t *transform)
618 {
619 transform_attribute_type_t type;
620 transform_attribute_t *tattr;
621 enumerator_t *enumerator;
622 u_int16_t value, key_length = 0;
623
624 enumerator = transform->create_attribute_enumerator(transform);
625 while (enumerator->enumerate(enumerator, &tattr))
626 {
627 type = tattr->get_attribute_type(tattr);
628 value = tattr->get_value(tattr);
629 switch (type)
630 {
631 case TATTR_PH2_KEY_LENGTH:
632 key_length = value;
633 break;
634 case TATTR_PH2_AUTH_ALGORITHM:
635 proposal->add_algorithm(proposal, INTEGRITY_ALGORITHM,
636 get_alg_from_ikev1(INTEGRITY_ALGORITHM, value), 0);
637 break;
638 default:
639 /* TODO-IKEv1: lifetimes other attributes */
640 break;
641 }
642 }
643 enumerator->destroy(enumerator);
644
645 /* TODO-IKEv1: handle ESN attribute */
646 proposal->add_algorithm(proposal, EXTENDED_SEQUENCE_NUMBERS,
647 NO_EXT_SEQ_NUMBERS, 0);
648
649 proposal->add_algorithm(proposal, ENCRYPTION_ALGORITHM,
650 transform->get_transform_id(transform), key_length);
651 }
652
653 METHOD(proposal_substructure_t, get_proposal, proposal_t*,
654 private_proposal_substructure_t *this)
655 {
656 transform_substructure_t *transform;
657 enumerator_t *enumerator;
658 proposal_t *proposal;
659
660 proposal = proposal_create(this->protocol_id, this->proposal_number);
661
662 enumerator = this->transforms->create_enumerator(this->transforms);
663 while (enumerator->enumerate(enumerator, &transform))
664 {
665 if (this->type == PROPOSAL_SUBSTRUCTURE)
666 {
667 add_to_proposal_v2(proposal, transform);
668 }
669 else
670 {
671 switch (this->protocol_id)
672 {
673 case PROTO_IKE:
674 add_to_proposal_v1_ike(proposal, transform);
675 break;
676 case PROTO_ESP:
677 add_to_proposal_v1_esp(proposal, transform);
678 break;
679 default:
680 break;
681 }
682 /* TODO-IKEv1: We currently accept the first set of transforms
683 * in a substructure only. We need to return multiple proposals,
684 * but this messes up proposal numbering, as we don't support
685 * transform numbering. */
686 break;
687 }
688 }
689 enumerator->destroy(enumerator);
690
691 switch (this->spi.len)
692 {
693 case 4:
694 proposal->set_spi(proposal, *((u_int32_t*)this->spi.ptr));
695 break;
696 case 8:
697 proposal->set_spi(proposal, *((u_int64_t*)this->spi.ptr));
698 break;
699 default:
700 break;
701 }
702 return proposal;
703 }
704
705 METHOD(proposal_substructure_t, create_substructure_enumerator, enumerator_t*,
706 private_proposal_substructure_t *this)
707 {
708 return this->transforms->create_enumerator(this->transforms);
709 }
710
711 METHOD2(payload_t, proposal_substructure_t, destroy, void,
712 private_proposal_substructure_t *this)
713 {
714 this->transforms->destroy_offset(this->transforms,
715 offsetof(payload_t, destroy));
716 chunk_free(&this->spi);
717 free(this);
718 }
719
720 /*
721 * Described in header.
722 */
723 proposal_substructure_t *proposal_substructure_create(payload_type_t type)
724 {
725 private_proposal_substructure_t *this;
726
727 INIT(this,
728 .public = {
729 .payload_interface = {
730 .verify = _verify,
731 .get_encoding_rules = _get_encoding_rules,
732 .get_header_length = _get_header_length,
733 .get_length = _get_length,
734 .get_next_type = _get_next_type,
735 .set_next_type = _set_next_type,
736 .get_type = _get_type,
737 .destroy = _destroy,
738 },
739 .set_proposal_number = _set_proposal_number,
740 .get_proposal_number = _get_proposal_number,
741 .set_protocol_id = _set_protocol_id,
742 .get_protocol_id = _get_protocol_id,
743 .set_is_last_proposal = _set_is_last_proposal,
744 .get_proposal = _get_proposal,
745 .create_substructure_enumerator = _create_substructure_enumerator,
746 .set_spi = _set_spi,
747 .get_spi = _get_spi,
748 .destroy = _destroy,
749 },
750 .next_payload = NO_PAYLOAD,
751 .transforms = linked_list_create(),
752 .type = type,
753 );
754 compute_length(this);
755
756 return &this->public;
757 }
758
759 /**
760 * Add an IKEv1 IKE proposal to the substructure
761 */
762 static void set_from_proposal_v1_ike(private_proposal_substructure_t *this,
763 proposal_t *proposal)
764 {
765 transform_substructure_t *transform;
766 u_int16_t alg, key_size;
767 enumerator_t *enumerator;
768
769 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE_V1,
770 0, IKEV1_TRANSID_KEY_IKE);
771
772 enumerator = proposal->create_enumerator(proposal, ENCRYPTION_ALGORITHM);
773 while (enumerator->enumerate(enumerator, &alg, &key_size))
774 {
775 alg = get_ikev1_from_alg(ENCRYPTION_ALGORITHM, alg);
776 if (alg)
777 {
778 transform->add_transform_attribute(transform,
779 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
780 TATTR_PH1_ENCRYPTION_ALGORITHM, alg));
781 if (key_size)
782 {
783 transform->add_transform_attribute(transform,
784 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
785 TATTR_PH1_KEY_LENGTH, key_size));
786 }
787 }
788 }
789 enumerator->destroy(enumerator);
790
791 /* encode the integrity algorithm as hash and assume use the same PRF */
792 enumerator = proposal->create_enumerator(proposal, INTEGRITY_ALGORITHM);
793 while (enumerator->enumerate(enumerator, &alg, &key_size))
794 {
795 alg = get_ikev1_from_alg(INTEGRITY_ALGORITHM, alg);
796 if (alg)
797 {
798 transform->add_transform_attribute(transform,
799 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
800 TATTR_PH1_HASH_ALGORITHM, alg));
801 }
802 }
803 enumerator->destroy(enumerator);
804
805 enumerator = proposal->create_enumerator(proposal, DIFFIE_HELLMAN_GROUP);
806 while (enumerator->enumerate(enumerator, &alg, &key_size))
807 {
808 transform->add_transform_attribute(transform,
809 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
810 TATTR_PH1_GROUP, alg));
811 }
812 enumerator->destroy(enumerator);
813
814 /* TODO-IKEv1: Add lifetime, non-fixed auth-method and other attributes */
815 transform->add_transform_attribute(transform,
816 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
817 TATTR_PH1_AUTH_METHOD, IKEV1_AUTH_PSK));
818
819 add_transform_substructure(this, transform);
820 }
821
822 /**
823 * Add an IKEv1 ESP proposal to the substructure
824 */
825 static void set_from_proposal_v1_esp(private_proposal_substructure_t *this,
826 proposal_t *proposal)
827 {
828 transform_substructure_t *transform = NULL;
829 u_int16_t alg, key_size;
830 enumerator_t *enumerator;
831
832 enumerator = proposal->create_enumerator(proposal, ENCRYPTION_ALGORITHM);
833 if (enumerator->enumerate(enumerator, &alg, &key_size))
834 {
835 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE_V1,
836 0, alg);
837 if (key_size)
838 {
839 transform->add_transform_attribute(transform,
840 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
841 TATTR_PH2_KEY_LENGTH, key_size));
842 }
843 }
844 enumerator->destroy(enumerator);
845 if (!transform)
846 {
847 return;
848 }
849
850 enumerator = proposal->create_enumerator(proposal, INTEGRITY_ALGORITHM);
851 while (enumerator->enumerate(enumerator, &alg, &key_size))
852 {
853 alg = get_ikev1_from_alg(INTEGRITY_ALGORITHM, alg);
854 if (alg)
855 {
856 transform->add_transform_attribute(transform,
857 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
858 TATTR_PH2_AUTH_ALGORITHM, alg));
859 }
860 }
861 enumerator->destroy(enumerator);
862
863 /* TODO-IKEv1: Add lifetime and other attributes, non-fixes ESN */
864 transform->add_transform_attribute(transform,
865 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
866 TATTR_PH2_EXT_SEQ_NUMBER, NO_EXT_SEQ_NUMBERS));
867
868 add_transform_substructure(this, transform);
869 }
870
871 /**
872 * Add an IKEv2 proposal to the substructure
873 */
874 static void set_from_proposal_v2(private_proposal_substructure_t *this,
875 proposal_t *proposal)
876 {
877 transform_substructure_t *transform;
878 u_int16_t alg, key_size;
879 enumerator_t *enumerator;
880
881 /* encryption algorithm is only available in ESP */
882 enumerator = proposal->create_enumerator(proposal, ENCRYPTION_ALGORITHM);
883 while (enumerator->enumerate(enumerator, &alg, &key_size))
884 {
885 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE,
886 ENCRYPTION_ALGORITHM, alg);
887 if (key_size)
888 {
889 transform->add_transform_attribute(transform,
890 transform_attribute_create_value(TRANSFORM_ATTRIBUTE,
891 TATTR_IKEV2_KEY_LENGTH, key_size));
892 }
893 add_transform_substructure(this, transform);
894 }
895 enumerator->destroy(enumerator);
896
897 /* integrity algorithms */
898 enumerator = proposal->create_enumerator(proposal, INTEGRITY_ALGORITHM);
899 while (enumerator->enumerate(enumerator, &alg, &key_size))
900 {
901 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE,
902 INTEGRITY_ALGORITHM, alg);
903 add_transform_substructure(this, transform);
904 }
905 enumerator->destroy(enumerator);
906
907 /* prf algorithms */
908 enumerator = proposal->create_enumerator(proposal, PSEUDO_RANDOM_FUNCTION);
909 while (enumerator->enumerate(enumerator, &alg, &key_size))
910 {
911 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE,
912 PSEUDO_RANDOM_FUNCTION, alg);
913 add_transform_substructure(this, transform);
914 }
915 enumerator->destroy(enumerator);
916
917 /* dh groups */
918 enumerator = proposal->create_enumerator(proposal, DIFFIE_HELLMAN_GROUP);
919 while (enumerator->enumerate(enumerator, &alg, NULL))
920 {
921 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE,
922 DIFFIE_HELLMAN_GROUP, alg);
923 add_transform_substructure(this, transform);
924 }
925 enumerator->destroy(enumerator);
926
927 /* extended sequence numbers */
928 enumerator = proposal->create_enumerator(proposal, EXTENDED_SEQUENCE_NUMBERS);
929 while (enumerator->enumerate(enumerator, &alg, NULL))
930 {
931 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE,
932 EXTENDED_SEQUENCE_NUMBERS, alg);
933 add_transform_substructure(this, transform);
934 }
935 enumerator->destroy(enumerator);
936 }
937
938 /*
939 * Described in header.
940 */
941 proposal_substructure_t *proposal_substructure_create_from_proposal(
942 payload_type_t type, proposal_t *proposal)
943 {
944 private_proposal_substructure_t *this;
945 u_int64_t spi64;
946 u_int32_t spi32;
947
948 this = (private_proposal_substructure_t*)proposal_substructure_create(type);
949
950 if (type == PROPOSAL_SUBSTRUCTURE)
951 {
952 set_from_proposal_v2(this, proposal);
953 }
954 else
955 {
956 switch (proposal->get_protocol(proposal))
957 {
958 case PROTO_IKE:
959 set_from_proposal_v1_ike(this, proposal);
960 break;
961 case PROTO_ESP:
962 set_from_proposal_v1_esp(this, proposal);
963 break;
964 default:
965 break;
966 }
967 }
968 /* add SPI, if necessary */
969 switch (proposal->get_protocol(proposal))
970 {
971 case PROTO_AH:
972 case PROTO_ESP:
973 spi32 = proposal->get_spi(proposal);
974 this->spi = chunk_clone(chunk_from_thing(spi32));
975 this->spi_size = this->spi.len;
976 break;
977 case PROTO_IKE:
978 spi64 = proposal->get_spi(proposal);
979 if (spi64)
980 { /* IKE only uses SPIS when rekeying, but on initial setup */
981 this->spi = chunk_clone(chunk_from_thing(spi64));
982 this->spi_size = this->spi.len;
983 }
984 break;
985 default:
986 break;
987 }
988 this->proposal_number = proposal->get_number(proposal);
989 this->protocol_id = proposal->get_protocol(proposal);
990 compute_length(this);
991
992 return &this->public;
993 }