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