Use proper enum types in proposal_substructure.
[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 case AUTH_XAUTH_INIT_PSK:
587 return IKEV1_AUTH_XAUTH_INIT_PSK;
588 case AUTH_XAUTH_INIT_RSA:
589 return IKEV1_AUTH_XAUTH_INIT_RSA;
590 default:
591 /* TODO-IKEv1: Handle other XAUTH methods */
592 /* TODO-IKEv1: Handle ECDSA methods */
593 case AUTH_PSK:
594 return IKEV1_AUTH_PSK;
595 }
596 }
597
598 /**
599 * Get IKEv1 encapsulation mode
600 */
601 static u_int16_t get_ikev1_mode(ipsec_mode_t mode, bool udp)
602 {
603 switch (mode)
604 {
605 case MODE_TUNNEL:
606 return udp ? IKEV1_ENCAP_UDP_TUNNEL : IKEV1_ENCAP_TUNNEL;
607 case MODE_TRANSPORT:
608 return udp ? IKEV1_ENCAP_UDP_TRANSPORT : IKEV1_ENCAP_TRANSPORT;
609 default:
610 return IKEV1_ENCAP_TUNNEL;
611 }
612 }
613
614 /**
615 * Add an IKE transform to a proposal for IKEv1
616 */
617 static void add_to_proposal_v1_ike(proposal_t *proposal,
618 transform_substructure_t *transform)
619 {
620 transform_attribute_type_t type;
621 transform_attribute_t *tattr;
622 enumerator_t *enumerator;
623 u_int16_t value, key_length = 0;
624 u_int16_t encr = ENCR_UNDEFINED;
625
626 enumerator = transform->create_attribute_enumerator(transform);
627 while (enumerator->enumerate(enumerator, &tattr))
628 {
629 type = tattr->get_attribute_type(tattr);
630 value = tattr->get_value(tattr);
631 switch (type)
632 {
633 case TATTR_PH1_ENCRYPTION_ALGORITHM:
634 encr = get_alg_from_ikev1(ENCRYPTION_ALGORITHM, value);
635 break;
636 case TATTR_PH1_KEY_LENGTH:
637 key_length = value;
638 break;
639 case TATTR_PH1_HASH_ALGORITHM:
640 proposal->add_algorithm(proposal, INTEGRITY_ALGORITHM,
641 get_alg_from_ikev1(INTEGRITY_ALGORITHM, value), 0);
642 proposal->add_algorithm(proposal, PSEUDO_RANDOM_FUNCTION,
643 get_alg_from_ikev1(PSEUDO_RANDOM_FUNCTION, value), 0);
644 break;
645 case TATTR_PH1_GROUP:
646 proposal->add_algorithm(proposal, DIFFIE_HELLMAN_GROUP,
647 value, 0);
648 break;
649 default:
650 /* TODO-IKEv1: lifetimes, authentication and other attributes */
651 break;
652 }
653 }
654 enumerator->destroy(enumerator);
655
656 if (encr != ENCR_UNDEFINED)
657 {
658 proposal->add_algorithm(proposal, ENCRYPTION_ALGORITHM, encr, key_length);
659 }
660 }
661
662 /**
663 * Add an ESP transform to a proposal for IKEv1
664 */
665 static void add_to_proposal_v1_esp(proposal_t *proposal,
666 transform_substructure_t *transform)
667 {
668 transform_attribute_type_t type;
669 transform_attribute_t *tattr;
670 enumerator_t *enumerator;
671 u_int16_t value, key_length = 0;
672
673 enumerator = transform->create_attribute_enumerator(transform);
674 while (enumerator->enumerate(enumerator, &tattr))
675 {
676 type = tattr->get_attribute_type(tattr);
677 value = tattr->get_value(tattr);
678 switch (type)
679 {
680 case TATTR_PH2_KEY_LENGTH:
681 key_length = value;
682 break;
683 case TATTR_PH2_AUTH_ALGORITHM:
684 proposal->add_algorithm(proposal, INTEGRITY_ALGORITHM,
685 get_alg_from_ikev1(INTEGRITY_ALGORITHM, value), 0);
686 break;
687 default:
688 /* TODO-IKEv1: lifetimes other attributes */
689 break;
690 }
691 }
692 enumerator->destroy(enumerator);
693
694 /* TODO-IKEv1: handle ESN attribute */
695 proposal->add_algorithm(proposal, EXTENDED_SEQUENCE_NUMBERS,
696 NO_EXT_SEQ_NUMBERS, 0);
697
698 proposal->add_algorithm(proposal, ENCRYPTION_ALGORITHM,
699 transform->get_transform_id(transform), key_length);
700 }
701
702 METHOD(proposal_substructure_t, get_proposals, void,
703 private_proposal_substructure_t *this, linked_list_t *proposals)
704 {
705 transform_substructure_t *transform;
706 enumerator_t *enumerator;
707 proposal_t *proposal = NULL;
708 u_int64_t spi = 0;
709
710 switch (this->spi.len)
711 {
712 case 4:
713 spi = *((u_int32_t*)this->spi.ptr);
714 break;
715 case 8:
716 spi = *((u_int64_t*)this->spi.ptr);
717 break;
718 default:
719 break;
720 }
721
722 enumerator = this->transforms->create_enumerator(this->transforms);
723 while (enumerator->enumerate(enumerator, &transform))
724 {
725 if (!proposal)
726 {
727 proposal = proposal_create(this->protocol_id, this->proposal_number);
728 proposal->set_spi(proposal, spi);
729 proposals->insert_last(proposals, proposal);
730 }
731 if (this->type == PROPOSAL_SUBSTRUCTURE)
732 {
733 add_to_proposal_v2(proposal, transform);
734 }
735 else
736 {
737 switch (this->protocol_id)
738 {
739 case PROTO_IKE:
740 add_to_proposal_v1_ike(proposal, transform);
741 break;
742 case PROTO_ESP:
743 add_to_proposal_v1_esp(proposal, transform);
744 break;
745 default:
746 break;
747 }
748 /* create a new proposal for each transform in IKEv1 */
749 proposal = NULL;
750 }
751 }
752 enumerator->destroy(enumerator);
753 }
754
755 METHOD(proposal_substructure_t, create_substructure_enumerator, enumerator_t*,
756 private_proposal_substructure_t *this)
757 {
758 return this->transforms->create_enumerator(this->transforms);
759 }
760
761 /**
762 * Get an attribute from a selected transform
763 */
764 static u_int64_t get_attr_tfrm(transform_substructure_t *transform,
765 transform_attribute_type_t type)
766 {
767 enumerator_t *enumerator;
768 transform_attribute_t *attr;
769 u_int64_t value = 0;
770
771 enumerator = transform->create_attribute_enumerator(transform);
772 while (enumerator->enumerate(enumerator, &attr))
773 {
774 if (attr->get_attribute_type(attr) == type)
775 {
776 value = attr->get_value(attr);
777 break;
778 }
779 }
780 enumerator->destroy(enumerator);
781 return value;
782 }
783
784
785 /**
786 * Get an attribute from any transform, 0 if not found
787 */
788 static u_int64_t get_attr(private_proposal_substructure_t *this,
789 transform_attribute_type_t type, transform_substructure_t **sel)
790 {
791 transform_substructure_t *transform;
792 enumerator_t *enumerator;
793 u_int64_t value = 0;
794
795 enumerator = this->transforms->create_enumerator(this->transforms);
796 while (enumerator->enumerate(enumerator, &transform))
797 {
798 value = get_attr_tfrm(transform, type);
799 if (value)
800 {
801 if (sel)
802 {
803 *sel = transform;
804 }
805 break;
806 }
807 }
808 enumerator->destroy(enumerator);
809 return value;
810 }
811
812 METHOD(proposal_substructure_t, get_lifetime, u_int32_t,
813 private_proposal_substructure_t *this)
814 {
815 transform_substructure_t *transform;
816 ikev1_life_type_t type;
817
818 switch (this->protocol_id)
819 {
820 case PROTO_IKE:
821 type = get_attr(this, TATTR_PH1_LIFE_TYPE, &transform);
822 if (type == IKEV1_LIFE_TYPE_SECONDS)
823 {
824 return get_attr_tfrm(transform, TATTR_PH1_LIFE_DURATION);
825 }
826 break;
827 case PROTO_ESP:
828 type = get_attr(this, TATTR_PH2_SA_LIFE_TYPE, &transform);
829 if (type == IKEV1_LIFE_TYPE_SECONDS)
830 {
831 return get_attr_tfrm(transform, TATTR_PH2_SA_LIFE_DURATION);
832 }
833 else if (type != IKEV1_LIFE_TYPE_KILOBYTES)
834 { /* default to 8 hours, RFC 2407 */
835 return 28800;
836 }
837 break;
838 default:
839 break;
840 }
841 return 0;
842 }
843
844 METHOD(proposal_substructure_t, get_lifebytes, u_int64_t,
845 private_proposal_substructure_t *this)
846 {
847 transform_substructure_t *transform;
848 ikev1_life_type_t type;
849
850 switch (this->protocol_id)
851 {
852 case PROTO_IKE:
853 type = get_attr(this, TATTR_PH1_LIFE_TYPE, &transform);
854 if (type == IKEV1_LIFE_TYPE_KILOBYTES)
855 {
856 return get_attr_tfrm(transform, TATTR_PH1_LIFE_DURATION);
857 }
858 break;
859 case PROTO_ESP:
860 type = get_attr(this, TATTR_PH2_SA_LIFE_TYPE, &transform);
861 if (type == IKEV1_LIFE_TYPE_KILOBYTES)
862 {
863 return get_attr_tfrm(transform, TATTR_PH1_LIFE_DURATION);
864 }
865 break;
866 default:
867 break;
868 }
869 return 0;
870
871 }
872
873 METHOD(proposal_substructure_t, get_auth_method, auth_method_t,
874 private_proposal_substructure_t *this)
875 {
876 switch (get_attr(this, TATTR_PH1_AUTH_METHOD, NULL))
877 {
878 case IKEV1_AUTH_PSK:
879 return AUTH_PSK;
880 case IKEV1_AUTH_RSA_SIG:
881 return AUTH_RSA;
882 case IKEV1_AUTH_DSS_SIG:
883 return AUTH_DSS;
884 case IKEV1_AUTH_XAUTH_INIT_PSK:
885 return AUTH_XAUTH_INIT_PSK;
886 case IKEV1_AUTH_XAUTH_INIT_RSA:
887 return AUTH_XAUTH_INIT_RSA;
888 default:
889 /* TODO-IKEv1: other XAUTH, ECDSA sigs */
890 return AUTH_NONE;
891 }
892 }
893
894 METHOD(proposal_substructure_t, get_encap_mode, ipsec_mode_t,
895 private_proposal_substructure_t *this, bool *udp)
896 {
897 *udp = FALSE;
898 switch (get_attr(this, TATTR_PH2_ENCAP_MODE, NULL))
899 {
900 case IKEV1_ENCAP_TRANSPORT:
901 return MODE_TRANSPORT;
902 case IKEV1_ENCAP_TUNNEL:
903 return MODE_TRANSPORT;
904 case IKEV1_ENCAP_UDP_TRANSPORT:
905 *udp = TRUE;
906 return MODE_TRANSPORT;
907 case IKEV1_ENCAP_UDP_TUNNEL:
908 *udp = TRUE;
909 return MODE_TUNNEL;
910 default:
911 /* default to TUNNEL, RFC 2407 says implementation specific */
912 return MODE_TUNNEL;
913 }
914 }
915
916 METHOD2(payload_t, proposal_substructure_t, destroy, void,
917 private_proposal_substructure_t *this)
918 {
919 this->transforms->destroy_offset(this->transforms,
920 offsetof(payload_t, destroy));
921 chunk_free(&this->spi);
922 free(this);
923 }
924
925 /*
926 * Described in header.
927 */
928 proposal_substructure_t *proposal_substructure_create(payload_type_t type)
929 {
930 private_proposal_substructure_t *this;
931
932 INIT(this,
933 .public = {
934 .payload_interface = {
935 .verify = _verify,
936 .get_encoding_rules = _get_encoding_rules,
937 .get_header_length = _get_header_length,
938 .get_length = _get_length,
939 .get_next_type = _get_next_type,
940 .set_next_type = _set_next_type,
941 .get_type = _get_type,
942 .destroy = _destroy,
943 },
944 .set_proposal_number = _set_proposal_number,
945 .get_proposal_number = _get_proposal_number,
946 .set_protocol_id = _set_protocol_id,
947 .get_protocol_id = _get_protocol_id,
948 .set_is_last_proposal = _set_is_last_proposal,
949 .get_proposals = _get_proposals,
950 .create_substructure_enumerator = _create_substructure_enumerator,
951 .set_spi = _set_spi,
952 .get_spi = _get_spi,
953 .get_lifetime = _get_lifetime,
954 .get_lifebytes = _get_lifebytes,
955 .get_auth_method = _get_auth_method,
956 .get_encap_mode = _get_encap_mode,
957 .destroy = _destroy,
958 },
959 .next_payload = NO_PAYLOAD,
960 .transforms = linked_list_create(),
961 .type = type,
962 );
963 compute_length(this);
964
965 return &this->public;
966 }
967
968 /**
969 * Add an IKEv1 IKE proposal to the substructure
970 */
971 static void set_from_proposal_v1_ike(private_proposal_substructure_t *this,
972 proposal_t *proposal, u_int32_t lifetime,
973 auth_method_t method, int number)
974 {
975 transform_substructure_t *transform;
976 u_int16_t alg, key_size;
977 enumerator_t *enumerator;
978
979 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE_V1,
980 number, IKEV1_TRANSID_KEY_IKE);
981
982 enumerator = proposal->create_enumerator(proposal, ENCRYPTION_ALGORITHM);
983 if (enumerator->enumerate(enumerator, &alg, &key_size))
984 {
985 alg = get_ikev1_from_alg(ENCRYPTION_ALGORITHM, alg);
986 if (alg)
987 {
988 transform->add_transform_attribute(transform,
989 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
990 TATTR_PH1_ENCRYPTION_ALGORITHM, alg));
991 if (key_size)
992 {
993 transform->add_transform_attribute(transform,
994 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
995 TATTR_PH1_KEY_LENGTH, key_size));
996 }
997 }
998 }
999 enumerator->destroy(enumerator);
1000
1001 /* encode the integrity algorithm as hash and assume use the same PRF */
1002 enumerator = proposal->create_enumerator(proposal, INTEGRITY_ALGORITHM);
1003 if (enumerator->enumerate(enumerator, &alg, &key_size))
1004 {
1005 alg = get_ikev1_from_alg(INTEGRITY_ALGORITHM, alg);
1006 if (alg)
1007 {
1008 transform->add_transform_attribute(transform,
1009 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1010 TATTR_PH1_HASH_ALGORITHM, alg));
1011 }
1012 }
1013 enumerator->destroy(enumerator);
1014
1015 enumerator = proposal->create_enumerator(proposal, DIFFIE_HELLMAN_GROUP);
1016 if (enumerator->enumerate(enumerator, &alg, &key_size))
1017 {
1018 transform->add_transform_attribute(transform,
1019 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1020 TATTR_PH1_GROUP, alg));
1021 }
1022 enumerator->destroy(enumerator);
1023
1024 transform->add_transform_attribute(transform,
1025 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1026 TATTR_PH1_AUTH_METHOD, get_ikev1_auth(method)));
1027 transform->add_transform_attribute(transform,
1028 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1029 TATTR_PH1_LIFE_TYPE, IKEV1_LIFE_TYPE_SECONDS));
1030 transform->add_transform_attribute(transform,
1031 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1032 TATTR_PH1_LIFE_DURATION, lifetime));
1033
1034 add_transform_substructure(this, transform);
1035 }
1036
1037 /**
1038 * Add an IKEv1 ESP proposal to the substructure
1039 */
1040 static void set_from_proposal_v1_esp(private_proposal_substructure_t *this,
1041 proposal_t *proposal, u_int32_t lifetime, u_int64_t lifebytes,
1042 ipsec_mode_t mode, bool udp, int number)
1043 {
1044 transform_substructure_t *transform = NULL;
1045 u_int16_t alg, key_size;
1046 enumerator_t *enumerator;
1047
1048 enumerator = proposal->create_enumerator(proposal, ENCRYPTION_ALGORITHM);
1049 if (enumerator->enumerate(enumerator, &alg, &key_size))
1050 {
1051 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE_V1,
1052 number, alg);
1053 if (key_size)
1054 {
1055 transform->add_transform_attribute(transform,
1056 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1057 TATTR_PH2_KEY_LENGTH, key_size));
1058 }
1059 }
1060 enumerator->destroy(enumerator);
1061 if (!transform)
1062 {
1063 return;
1064 }
1065
1066 enumerator = proposal->create_enumerator(proposal, INTEGRITY_ALGORITHM);
1067 if (enumerator->enumerate(enumerator, &alg, &key_size))
1068 {
1069 alg = get_ikev1_from_alg(INTEGRITY_ALGORITHM, alg);
1070 if (alg)
1071 {
1072 transform->add_transform_attribute(transform,
1073 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1074 TATTR_PH2_AUTH_ALGORITHM, alg));
1075 }
1076 }
1077 enumerator->destroy(enumerator);
1078
1079 transform->add_transform_attribute(transform,
1080 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1081 TATTR_PH2_ENCAP_MODE, get_ikev1_mode(mode, udp)));
1082 if (lifetime)
1083 {
1084 transform->add_transform_attribute(transform,
1085 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1086 TATTR_PH2_SA_LIFE_TYPE, IKEV1_LIFE_TYPE_SECONDS));
1087 transform->add_transform_attribute(transform,
1088 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1089 TATTR_PH2_SA_LIFE_DURATION, lifetime));
1090 }
1091 else if (lifebytes)
1092 {
1093 transform->add_transform_attribute(transform,
1094 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1095 TATTR_PH2_SA_LIFE_TYPE, IKEV1_LIFE_TYPE_KILOBYTES));
1096 transform->add_transform_attribute(transform,
1097 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1098 TATTR_PH2_SA_LIFE_DURATION, lifebytes / 1000));
1099 }
1100
1101 add_transform_substructure(this, transform);
1102 }
1103
1104 /**
1105 * Add an IKEv2 proposal to the substructure
1106 */
1107 static void set_from_proposal_v2(private_proposal_substructure_t *this,
1108 proposal_t *proposal)
1109 {
1110 transform_substructure_t *transform;
1111 u_int16_t alg, key_size;
1112 enumerator_t *enumerator;
1113
1114 /* encryption algorithm is only available in ESP */
1115 enumerator = proposal->create_enumerator(proposal, ENCRYPTION_ALGORITHM);
1116 while (enumerator->enumerate(enumerator, &alg, &key_size))
1117 {
1118 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE,
1119 ENCRYPTION_ALGORITHM, alg);
1120 if (key_size)
1121 {
1122 transform->add_transform_attribute(transform,
1123 transform_attribute_create_value(TRANSFORM_ATTRIBUTE,
1124 TATTR_IKEV2_KEY_LENGTH, key_size));
1125 }
1126 add_transform_substructure(this, transform);
1127 }
1128 enumerator->destroy(enumerator);
1129
1130 /* integrity algorithms */
1131 enumerator = proposal->create_enumerator(proposal, INTEGRITY_ALGORITHM);
1132 while (enumerator->enumerate(enumerator, &alg, &key_size))
1133 {
1134 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE,
1135 INTEGRITY_ALGORITHM, alg);
1136 add_transform_substructure(this, transform);
1137 }
1138 enumerator->destroy(enumerator);
1139
1140 /* prf algorithms */
1141 enumerator = proposal->create_enumerator(proposal, PSEUDO_RANDOM_FUNCTION);
1142 while (enumerator->enumerate(enumerator, &alg, &key_size))
1143 {
1144 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE,
1145 PSEUDO_RANDOM_FUNCTION, alg);
1146 add_transform_substructure(this, transform);
1147 }
1148 enumerator->destroy(enumerator);
1149
1150 /* dh groups */
1151 enumerator = proposal->create_enumerator(proposal, DIFFIE_HELLMAN_GROUP);
1152 while (enumerator->enumerate(enumerator, &alg, NULL))
1153 {
1154 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE,
1155 DIFFIE_HELLMAN_GROUP, alg);
1156 add_transform_substructure(this, transform);
1157 }
1158 enumerator->destroy(enumerator);
1159
1160 /* extended sequence numbers */
1161 enumerator = proposal->create_enumerator(proposal, EXTENDED_SEQUENCE_NUMBERS);
1162 while (enumerator->enumerate(enumerator, &alg, NULL))
1163 {
1164 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE,
1165 EXTENDED_SEQUENCE_NUMBERS, alg);
1166 add_transform_substructure(this, transform);
1167 }
1168 enumerator->destroy(enumerator);
1169 }
1170
1171 /**
1172 * Set SPI and other data from proposal, compute length
1173 */
1174 static void set_data(private_proposal_substructure_t *this, proposal_t *proposal)
1175 {
1176 u_int64_t spi64;
1177 u_int32_t spi32;
1178
1179 /* add SPI, if necessary */
1180 switch (proposal->get_protocol(proposal))
1181 {
1182 case PROTO_AH:
1183 case PROTO_ESP:
1184 spi32 = proposal->get_spi(proposal);
1185 this->spi = chunk_clone(chunk_from_thing(spi32));
1186 this->spi_size = this->spi.len;
1187 break;
1188 case PROTO_IKE:
1189 spi64 = proposal->get_spi(proposal);
1190 if (spi64)
1191 { /* IKE only uses SPIS when rekeying, but on initial setup */
1192 this->spi = chunk_clone(chunk_from_thing(spi64));
1193 this->spi_size = this->spi.len;
1194 }
1195 break;
1196 default:
1197 break;
1198 }
1199 this->proposal_number = proposal->get_number(proposal);
1200 this->protocol_id = proposal->get_protocol(proposal);
1201 compute_length(this);
1202 }
1203
1204 /*
1205 * Described in header.
1206 */
1207 proposal_substructure_t *proposal_substructure_create_from_proposal_v2(
1208 proposal_t *proposal)
1209 {
1210 private_proposal_substructure_t *this;
1211
1212 this = (private_proposal_substructure_t*)
1213 proposal_substructure_create(SECURITY_ASSOCIATION);
1214 set_from_proposal_v2(this, proposal);
1215 set_data(this, proposal);
1216
1217 return &this->public;
1218 }
1219
1220 /**
1221 * See header.
1222 */
1223 proposal_substructure_t *proposal_substructure_create_from_proposal_v1(
1224 proposal_t *proposal, u_int32_t lifetime, u_int64_t lifebytes,
1225 auth_method_t auth, ipsec_mode_t mode, bool udp)
1226 {
1227 private_proposal_substructure_t *this;
1228
1229 this = (private_proposal_substructure_t*)
1230 proposal_substructure_create(PROPOSAL_SUBSTRUCTURE_V1);
1231 switch (proposal->get_protocol(proposal))
1232 {
1233 case PROTO_IKE:
1234 set_from_proposal_v1_ike(this, proposal, lifetime, auth, 0);
1235 break;
1236 case PROTO_ESP:
1237 set_from_proposal_v1_esp(this, proposal, lifetime,
1238 lifebytes, mode, udp, 0);
1239 break;
1240 default:
1241 break;
1242 }
1243 set_data(this, proposal);
1244
1245 return &this->public;
1246 }
1247
1248 /**
1249 * See header.
1250 */
1251 proposal_substructure_t *proposal_substructure_create_from_proposals_v1(
1252 linked_list_t *proposals, u_int32_t lifetime, u_int64_t lifebytes,
1253 auth_method_t auth, ipsec_mode_t mode, bool udp)
1254 {
1255 private_proposal_substructure_t *this = NULL;
1256 enumerator_t *enumerator;
1257 proposal_t *proposal;
1258 int number = 0;
1259
1260 enumerator = proposals->create_enumerator(proposals);
1261 while (enumerator->enumerate(enumerator, &proposal))
1262 {
1263 if (!this)
1264 {
1265 this = (private_proposal_substructure_t*)
1266 proposal_substructure_create_from_proposal_v1(
1267 proposal, lifetime, lifebytes, auth, mode, udp);
1268 }
1269 else
1270 {
1271 switch (proposal->get_protocol(proposal))
1272 {
1273 case PROTO_IKE:
1274 set_from_proposal_v1_ike(this, proposal, lifetime,
1275 auth, ++number);
1276 break;
1277 case PROTO_ESP:
1278 set_from_proposal_v1_esp(this, proposal, lifetime,
1279 lifebytes, mode, udp, ++number);
1280 break;
1281 default:
1282 break;
1283 }
1284 }
1285 }
1286 enumerator->destroy(enumerator);
1287
1288 return &this->public;
1289 }