ca19ba70089424b0df96e9e9c692cf057615a0c3
[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 /**
758 * Get an attribute from a selected transform
759 */
760 static u_int64_t get_attr_tfrm(transform_substructure_t *transform,
761 transform_attribute_type_t type)
762 {
763 enumerator_t *enumerator;
764 transform_attribute_t *attr;
765 u_int64_t value = 0;
766
767 enumerator = transform->create_attribute_enumerator(transform);
768 while (enumerator->enumerate(enumerator, &attr))
769 {
770 if (attr->get_attribute_type(attr) == type)
771 {
772 value = attr->get_value(attr);
773 break;
774 }
775 }
776 enumerator->destroy(enumerator);
777 return value;
778 }
779
780
781 /**
782 * Get an attribute from any transform, 0 if not found
783 */
784 static u_int64_t get_attr(private_proposal_substructure_t *this,
785 transform_attribute_type_t type, transform_substructure_t **sel)
786 {
787 transform_substructure_t *transform;
788 enumerator_t *enumerator;
789 u_int64_t value = 0;
790
791 enumerator = this->transforms->create_enumerator(this->transforms);
792 while (enumerator->enumerate(enumerator, &transform))
793 {
794 value = get_attr_tfrm(transform, type);
795 if (value)
796 {
797 if (sel)
798 {
799 *sel = transform;
800 }
801 break;
802 }
803 }
804 enumerator->destroy(enumerator);
805 return value;
806 }
807
808 METHOD(proposal_substructure_t, get_lifetime, u_int32_t,
809 private_proposal_substructure_t *this)
810 {
811 transform_substructure_t *transform;
812 transform_attribute_type_t type;
813
814 switch (this->protocol_id)
815 {
816 case PROTO_IKE:
817 type = get_attr(this, TATTR_PH1_LIFE_TYPE, &transform);
818 if (type == IKEV1_LIFE_TYPE_SECONDS)
819 {
820 return get_attr_tfrm(transform, TATTR_PH1_LIFE_DURATION);
821 }
822 break;
823 case PROTO_ESP:
824 type = get_attr(this, TATTR_PH2_SA_LIFE_TYPE, &transform);
825 if (type == IKEV1_LIFE_TYPE_SECONDS)
826 {
827 return get_attr_tfrm(transform, TATTR_PH2_SA_LIFE_DURATION);
828 }
829 else if (type != IKEV1_LIFE_TYPE_KILOBYTES)
830 { /* default to 8 hours, RFC 2407 */
831 return 28800;
832 }
833 break;
834 default:
835 break;
836 }
837 return 0;
838 }
839
840 METHOD(proposal_substructure_t, get_lifebytes, u_int64_t,
841 private_proposal_substructure_t *this)
842 {
843 transform_substructure_t *transform;
844 transform_attribute_type_t type;
845
846 switch (this->protocol_id)
847 {
848 case PROTO_IKE:
849 type = get_attr(this, TATTR_PH1_LIFE_TYPE, &transform);
850 if (type == IKEV1_LIFE_TYPE_KILOBYTES)
851 {
852 return get_attr_tfrm(transform, TATTR_PH1_LIFE_DURATION);
853 }
854 break;
855 case PROTO_ESP:
856 type = get_attr(this, TATTR_PH2_SA_LIFE_TYPE, &transform);
857 if (type == IKEV1_LIFE_TYPE_KILOBYTES)
858 {
859 return get_attr_tfrm(transform, TATTR_PH1_LIFE_DURATION);
860 }
861 break;
862 default:
863 break;
864 }
865 return 0;
866
867 }
868
869 METHOD(proposal_substructure_t, get_auth_method, auth_method_t,
870 private_proposal_substructure_t *this)
871 {
872 switch (get_attr(this, TATTR_PH1_AUTH_METHOD, NULL))
873 {
874 case IKEV1_AUTH_PSK:
875 return AUTH_PSK;
876 case IKEV1_AUTH_RSA_SIG:
877 return AUTH_RSA;
878 case IKEV1_AUTH_DSS_SIG:
879 return AUTH_DSS;
880 default:
881 /* TODO-IKEv1: XAUTH, ECDSA sigs */
882 return AUTH_NONE;
883 }
884 }
885
886 METHOD(proposal_substructure_t, get_encap_mode, ipsec_mode_t,
887 private_proposal_substructure_t *this, bool *udp)
888 {
889 *udp = FALSE;
890 switch (get_attr(this, TATTR_PH2_ENCAP_MODE, NULL))
891 {
892 case IKEV1_ENCAP_TRANSPORT:
893 return MODE_TRANSPORT;
894 case IKEV1_ENCAP_TUNNEL:
895 return MODE_TRANSPORT;
896 case IKEV1_ENCAP_UDP_TRANSPORT:
897 *udp = TRUE;
898 return MODE_TRANSPORT;
899 case IKEV1_ENCAP_UDP_TUNNEL:
900 *udp = TRUE;
901 return MODE_TUNNEL;
902 default:
903 /* default to TUNNEL, RFC 2407 says implementation specific */
904 return MODE_TUNNEL;
905 }
906 }
907
908 METHOD2(payload_t, proposal_substructure_t, destroy, void,
909 private_proposal_substructure_t *this)
910 {
911 this->transforms->destroy_offset(this->transforms,
912 offsetof(payload_t, destroy));
913 chunk_free(&this->spi);
914 free(this);
915 }
916
917 /*
918 * Described in header.
919 */
920 proposal_substructure_t *proposal_substructure_create(payload_type_t type)
921 {
922 private_proposal_substructure_t *this;
923
924 INIT(this,
925 .public = {
926 .payload_interface = {
927 .verify = _verify,
928 .get_encoding_rules = _get_encoding_rules,
929 .get_header_length = _get_header_length,
930 .get_length = _get_length,
931 .get_next_type = _get_next_type,
932 .set_next_type = _set_next_type,
933 .get_type = _get_type,
934 .destroy = _destroy,
935 },
936 .set_proposal_number = _set_proposal_number,
937 .get_proposal_number = _get_proposal_number,
938 .set_protocol_id = _set_protocol_id,
939 .get_protocol_id = _get_protocol_id,
940 .set_is_last_proposal = _set_is_last_proposal,
941 .get_proposals = _get_proposals,
942 .create_substructure_enumerator = _create_substructure_enumerator,
943 .set_spi = _set_spi,
944 .get_spi = _get_spi,
945 .get_lifetime = _get_lifetime,
946 .get_lifebytes = _get_lifebytes,
947 .get_auth_method = _get_auth_method,
948 .get_encap_mode = _get_encap_mode,
949 .destroy = _destroy,
950 },
951 .next_payload = NO_PAYLOAD,
952 .transforms = linked_list_create(),
953 .type = type,
954 );
955 compute_length(this);
956
957 return &this->public;
958 }
959
960 /**
961 * Add an IKEv1 IKE proposal to the substructure
962 */
963 static void set_from_proposal_v1_ike(private_proposal_substructure_t *this,
964 proposal_t *proposal, u_int32_t lifetime,
965 auth_method_t method, int number)
966 {
967 transform_substructure_t *transform;
968 u_int16_t alg, key_size;
969 enumerator_t *enumerator;
970
971 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE_V1,
972 number, IKEV1_TRANSID_KEY_IKE);
973
974 enumerator = proposal->create_enumerator(proposal, ENCRYPTION_ALGORITHM);
975 if (enumerator->enumerate(enumerator, &alg, &key_size))
976 {
977 alg = get_ikev1_from_alg(ENCRYPTION_ALGORITHM, alg);
978 if (alg)
979 {
980 transform->add_transform_attribute(transform,
981 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
982 TATTR_PH1_ENCRYPTION_ALGORITHM, alg));
983 if (key_size)
984 {
985 transform->add_transform_attribute(transform,
986 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
987 TATTR_PH1_KEY_LENGTH, key_size));
988 }
989 }
990 }
991 enumerator->destroy(enumerator);
992
993 /* encode the integrity algorithm as hash and assume use the same PRF */
994 enumerator = proposal->create_enumerator(proposal, INTEGRITY_ALGORITHM);
995 if (enumerator->enumerate(enumerator, &alg, &key_size))
996 {
997 alg = get_ikev1_from_alg(INTEGRITY_ALGORITHM, alg);
998 if (alg)
999 {
1000 transform->add_transform_attribute(transform,
1001 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1002 TATTR_PH1_HASH_ALGORITHM, alg));
1003 }
1004 }
1005 enumerator->destroy(enumerator);
1006
1007 enumerator = proposal->create_enumerator(proposal, DIFFIE_HELLMAN_GROUP);
1008 if (enumerator->enumerate(enumerator, &alg, &key_size))
1009 {
1010 transform->add_transform_attribute(transform,
1011 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1012 TATTR_PH1_GROUP, alg));
1013 }
1014 enumerator->destroy(enumerator);
1015
1016 transform->add_transform_attribute(transform,
1017 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1018 TATTR_PH1_AUTH_METHOD, get_ikev1_auth(method)));
1019 transform->add_transform_attribute(transform,
1020 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1021 TATTR_PH1_LIFE_TYPE, IKEV1_LIFE_TYPE_SECONDS));
1022 transform->add_transform_attribute(transform,
1023 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1024 TATTR_PH1_LIFE_DURATION, lifetime));
1025
1026 add_transform_substructure(this, transform);
1027 }
1028
1029 /**
1030 * Add an IKEv1 ESP proposal to the substructure
1031 */
1032 static void set_from_proposal_v1_esp(private_proposal_substructure_t *this,
1033 proposal_t *proposal, u_int32_t lifetime, u_int64_t lifebytes,
1034 ipsec_mode_t mode, bool udp, int number)
1035 {
1036 transform_substructure_t *transform = NULL;
1037 u_int16_t alg, key_size;
1038 enumerator_t *enumerator;
1039
1040 enumerator = proposal->create_enumerator(proposal, ENCRYPTION_ALGORITHM);
1041 if (enumerator->enumerate(enumerator, &alg, &key_size))
1042 {
1043 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE_V1,
1044 number, alg);
1045 if (key_size)
1046 {
1047 transform->add_transform_attribute(transform,
1048 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1049 TATTR_PH2_KEY_LENGTH, key_size));
1050 }
1051 }
1052 enumerator->destroy(enumerator);
1053 if (!transform)
1054 {
1055 return;
1056 }
1057
1058 enumerator = proposal->create_enumerator(proposal, INTEGRITY_ALGORITHM);
1059 if (enumerator->enumerate(enumerator, &alg, &key_size))
1060 {
1061 alg = get_ikev1_from_alg(INTEGRITY_ALGORITHM, alg);
1062 if (alg)
1063 {
1064 transform->add_transform_attribute(transform,
1065 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1066 TATTR_PH2_AUTH_ALGORITHM, alg));
1067 }
1068 }
1069 enumerator->destroy(enumerator);
1070
1071 transform->add_transform_attribute(transform,
1072 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1073 TATTR_PH2_ENCAP_MODE, get_ikev1_mode(mode, udp)));
1074 if (lifetime)
1075 {
1076 transform->add_transform_attribute(transform,
1077 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1078 TATTR_PH2_SA_LIFE_TYPE, IKEV1_LIFE_TYPE_SECONDS));
1079 transform->add_transform_attribute(transform,
1080 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1081 TATTR_PH2_SA_LIFE_DURATION, lifetime));
1082 }
1083 else if (lifebytes)
1084 {
1085 transform->add_transform_attribute(transform,
1086 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1087 TATTR_PH2_SA_LIFE_TYPE, IKEV1_LIFE_TYPE_KILOBYTES));
1088 transform->add_transform_attribute(transform,
1089 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1090 TATTR_PH2_SA_LIFE_DURATION, lifebytes / 1000));
1091 }
1092
1093 add_transform_substructure(this, transform);
1094 }
1095
1096 /**
1097 * Add an IKEv2 proposal to the substructure
1098 */
1099 static void set_from_proposal_v2(private_proposal_substructure_t *this,
1100 proposal_t *proposal)
1101 {
1102 transform_substructure_t *transform;
1103 u_int16_t alg, key_size;
1104 enumerator_t *enumerator;
1105
1106 /* encryption algorithm is only available in ESP */
1107 enumerator = proposal->create_enumerator(proposal, ENCRYPTION_ALGORITHM);
1108 while (enumerator->enumerate(enumerator, &alg, &key_size))
1109 {
1110 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE,
1111 ENCRYPTION_ALGORITHM, alg);
1112 if (key_size)
1113 {
1114 transform->add_transform_attribute(transform,
1115 transform_attribute_create_value(TRANSFORM_ATTRIBUTE,
1116 TATTR_IKEV2_KEY_LENGTH, key_size));
1117 }
1118 add_transform_substructure(this, transform);
1119 }
1120 enumerator->destroy(enumerator);
1121
1122 /* integrity algorithms */
1123 enumerator = proposal->create_enumerator(proposal, INTEGRITY_ALGORITHM);
1124 while (enumerator->enumerate(enumerator, &alg, &key_size))
1125 {
1126 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE,
1127 INTEGRITY_ALGORITHM, alg);
1128 add_transform_substructure(this, transform);
1129 }
1130 enumerator->destroy(enumerator);
1131
1132 /* prf algorithms */
1133 enumerator = proposal->create_enumerator(proposal, PSEUDO_RANDOM_FUNCTION);
1134 while (enumerator->enumerate(enumerator, &alg, &key_size))
1135 {
1136 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE,
1137 PSEUDO_RANDOM_FUNCTION, alg);
1138 add_transform_substructure(this, transform);
1139 }
1140 enumerator->destroy(enumerator);
1141
1142 /* dh groups */
1143 enumerator = proposal->create_enumerator(proposal, DIFFIE_HELLMAN_GROUP);
1144 while (enumerator->enumerate(enumerator, &alg, NULL))
1145 {
1146 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE,
1147 DIFFIE_HELLMAN_GROUP, alg);
1148 add_transform_substructure(this, transform);
1149 }
1150 enumerator->destroy(enumerator);
1151
1152 /* extended sequence numbers */
1153 enumerator = proposal->create_enumerator(proposal, EXTENDED_SEQUENCE_NUMBERS);
1154 while (enumerator->enumerate(enumerator, &alg, NULL))
1155 {
1156 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE,
1157 EXTENDED_SEQUENCE_NUMBERS, alg);
1158 add_transform_substructure(this, transform);
1159 }
1160 enumerator->destroy(enumerator);
1161 }
1162
1163 /**
1164 * Set SPI and other data from proposal, compute length
1165 */
1166 static void set_data(private_proposal_substructure_t *this, proposal_t *proposal)
1167 {
1168 u_int64_t spi64;
1169 u_int32_t spi32;
1170
1171 /* add SPI, if necessary */
1172 switch (proposal->get_protocol(proposal))
1173 {
1174 case PROTO_AH:
1175 case PROTO_ESP:
1176 spi32 = proposal->get_spi(proposal);
1177 this->spi = chunk_clone(chunk_from_thing(spi32));
1178 this->spi_size = this->spi.len;
1179 break;
1180 case PROTO_IKE:
1181 spi64 = proposal->get_spi(proposal);
1182 if (spi64)
1183 { /* IKE only uses SPIS when rekeying, but on initial setup */
1184 this->spi = chunk_clone(chunk_from_thing(spi64));
1185 this->spi_size = this->spi.len;
1186 }
1187 break;
1188 default:
1189 break;
1190 }
1191 this->proposal_number = proposal->get_number(proposal);
1192 this->protocol_id = proposal->get_protocol(proposal);
1193 compute_length(this);
1194 }
1195
1196 /*
1197 * Described in header.
1198 */
1199 proposal_substructure_t *proposal_substructure_create_from_proposal_v2(
1200 proposal_t *proposal)
1201 {
1202 private_proposal_substructure_t *this;
1203
1204 this = (private_proposal_substructure_t*)
1205 proposal_substructure_create(SECURITY_ASSOCIATION);
1206 set_from_proposal_v2(this, proposal);
1207 set_data(this, proposal);
1208
1209 return &this->public;
1210 }
1211
1212 /**
1213 * See header.
1214 */
1215 proposal_substructure_t *proposal_substructure_create_from_proposal_v1(
1216 proposal_t *proposal, u_int32_t lifetime, u_int64_t lifebytes,
1217 auth_method_t auth, ipsec_mode_t mode, bool udp)
1218 {
1219 private_proposal_substructure_t *this;
1220
1221 this = (private_proposal_substructure_t*)
1222 proposal_substructure_create(PROPOSAL_SUBSTRUCTURE_V1);
1223 switch (proposal->get_protocol(proposal))
1224 {
1225 case PROTO_IKE:
1226 set_from_proposal_v1_ike(this, proposal, lifetime, auth, 0);
1227 break;
1228 case PROTO_ESP:
1229 set_from_proposal_v1_esp(this, proposal, lifetime,
1230 lifebytes, mode, udp, 0);
1231 break;
1232 default:
1233 break;
1234 }
1235 set_data(this, proposal);
1236
1237 return &this->public;
1238 }
1239
1240 /**
1241 * See header.
1242 */
1243 proposal_substructure_t *proposal_substructure_create_from_proposals_v1(
1244 linked_list_t *proposals, u_int32_t lifetime, u_int64_t lifebytes,
1245 auth_method_t auth, ipsec_mode_t mode, bool udp)
1246 {
1247 private_proposal_substructure_t *this = NULL;
1248 enumerator_t *enumerator;
1249 proposal_t *proposal;
1250 int number = 0;
1251
1252 enumerator = proposals->create_enumerator(proposals);
1253 while (enumerator->enumerate(enumerator, &proposal))
1254 {
1255 if (!this)
1256 {
1257 this = (private_proposal_substructure_t*)
1258 proposal_substructure_create_from_proposal_v1(
1259 proposal, lifetime, lifebytes, auth, mode, udp);
1260 }
1261 else
1262 {
1263 switch (proposal->get_protocol(proposal))
1264 {
1265 case PROTO_IKE:
1266 set_from_proposal_v1_ike(this, proposal, lifetime,
1267 auth, ++number);
1268 break;
1269 case PROTO_ESP:
1270 set_from_proposal_v1_esp(this, proposal, lifetime,
1271 lifebytes, mode, udp, ++number);
1272 break;
1273 default:
1274 break;
1275 }
1276 }
1277 }
1278 enumerator->destroy(enumerator);
1279
1280 return &this->public;
1281 }