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