ikev1: Assume a default key length of 128-bit for AES-CBC
[strongswan.git] / src / libcharon / encoding / payloads / proposal_substructure.c
1 /*
2 * Copyright (C) 2012-2014 Tobias Brunner
3 * Copyright (C) 2005-2010 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include <stddef.h>
19
20 #include "proposal_substructure.h"
21
22 #include <encoding/payloads/encodings.h>
23 #include <encoding/payloads/transform_substructure.h>
24 #include <library.h>
25 #include <collections/linked_list.h>
26 #include <daemon.h>
27
28 /**
29 * IKEv2 Value for a proposal payload.
30 */
31 #define PROPOSAL_TYPE_VALUE 2
32
33 typedef struct private_proposal_substructure_t private_proposal_substructure_t;
34
35 /**
36 * Private data of an proposal_substructure_t object.
37 */
38 struct private_proposal_substructure_t {
39
40 /**
41 * Public proposal_substructure_t interface.
42 */
43 proposal_substructure_t public;
44
45 /**
46 * Next payload type.
47 */
48 u_int8_t next_payload;
49
50 /**
51 * reserved byte
52 */
53 u_int8_t reserved;
54
55 /**
56 * Length of this payload.
57 */
58 u_int16_t proposal_length;
59
60 /**
61 * Proposal number.
62 */
63 u_int8_t proposal_number;
64
65 /**
66 * Protocol ID.
67 */
68 u_int8_t protocol_id;
69
70 /**
71 * SPI size of the following SPI.
72 */
73 u_int8_t spi_size;
74
75 /**
76 * Number of transforms.
77 */
78 u_int8_t transforms_count;
79
80 /**
81 * SPI is stored as chunk.
82 */
83 chunk_t spi;
84
85 /**
86 * Transforms are stored in a linked_list_t.
87 */
88 linked_list_t *transforms;
89
90 /**
91 * Type of this payload, PLV2_PROPOSAL_SUBSTRUCTURE or PLV1_PROPOSAL_SUBSTRUCTURE
92 */
93 payload_type_t type;
94 };
95
96 /**
97 * Encoding rules for a IKEv1 Proposal substructure.
98 */
99 static encoding_rule_t encodings_v1[] = {
100 /* 1 Byte next payload type, stored in the field next_payload */
101 { U_INT_8, offsetof(private_proposal_substructure_t, next_payload) },
102 /* 1 Reserved Byte */
103 { RESERVED_BYTE, offsetof(private_proposal_substructure_t, reserved) },
104 /* Length of the whole proposal substructure payload*/
105 { PAYLOAD_LENGTH, offsetof(private_proposal_substructure_t, proposal_length) },
106 /* proposal number is a number of 8 bit */
107 { U_INT_8, offsetof(private_proposal_substructure_t, proposal_number) },
108 /* protocol ID is a number of 8 bit */
109 { U_INT_8, offsetof(private_proposal_substructure_t, protocol_id) },
110 /* SPI Size has its own type */
111 { SPI_SIZE, offsetof(private_proposal_substructure_t, spi_size) },
112 /* Number of transforms is a number of 8 bit */
113 { U_INT_8, offsetof(private_proposal_substructure_t, transforms_count) },
114 /* SPI is a chunk of variable size*/
115 { SPI, offsetof(private_proposal_substructure_t, spi) },
116 /* Transforms are stored in a transform substructure list */
117 { PAYLOAD_LIST + PLV1_TRANSFORM_SUBSTRUCTURE,
118 offsetof(private_proposal_substructure_t, transforms) },
119 };
120
121 /**
122 * Encoding rules for a IKEv2 Proposal substructure.
123 */
124 static encoding_rule_t encodings_v2[] = {
125 /* 1 Byte next payload type, stored in the field next_payload */
126 { U_INT_8, offsetof(private_proposal_substructure_t, next_payload) },
127 /* 1 Reserved Byte */
128 { RESERVED_BYTE, offsetof(private_proposal_substructure_t, reserved) },
129 /* Length of the whole proposal substructure payload*/
130 { PAYLOAD_LENGTH, offsetof(private_proposal_substructure_t, proposal_length) },
131 /* proposal number is a number of 8 bit */
132 { U_INT_8, offsetof(private_proposal_substructure_t, proposal_number) },
133 /* protocol ID is a number of 8 bit */
134 { U_INT_8, offsetof(private_proposal_substructure_t, protocol_id) },
135 /* SPI Size has its own type */
136 { SPI_SIZE, offsetof(private_proposal_substructure_t, spi_size) },
137 /* Number of transforms is a number of 8 bit */
138 { U_INT_8, offsetof(private_proposal_substructure_t, transforms_count) },
139 /* SPI is a chunk of variable size*/
140 { SPI, offsetof(private_proposal_substructure_t, spi) },
141 /* Transforms are stored in a transform substructure list */
142 { PAYLOAD_LIST + PLV2_TRANSFORM_SUBSTRUCTURE,
143 offsetof(private_proposal_substructure_t, transforms) },
144 };
145
146 /*
147 1 2 3
148 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
149 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
150 ! 0 (last) or 2 ! RESERVED ! Proposal Length !
151 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
152 ! Proposal # ! Protocol ID ! SPI Size !# of Transforms!
153 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
154 ~ SPI (variable) ~
155 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
156 ! !
157 ~ <Transforms> ~
158 ! !
159 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
160 */
161
162 /**
163 * Encryption.
164 */
165 typedef enum {
166 IKEV1_ENCR_DES_CBC = 1,
167 IKEV1_ENCR_IDEA_CBC = 2,
168 IKEV1_ENCR_BLOWFISH_CBC = 3,
169 IKEV1_ENCR_RC5_R16_B64_CBC = 4,
170 IKEV1_ENCR_3DES_CBC = 5,
171 IKEV1_ENCR_CAST_CBC = 6,
172 IKEV1_ENCR_AES_CBC = 7,
173 IKEV1_ENCR_CAMELLIA_CBC = 8,
174 /* FreeS/WAN proprietary */
175 IKEV1_ENCR_SERPENT_CBC = 65004,
176 IKEV1_ENCR_TWOFISH_CBC = 65005,
177 } ikev1_encryption_t;
178
179 /**
180 * IKEv1 hash.
181 */
182 typedef enum {
183 IKEV1_HASH_MD5 = 1,
184 IKEV1_HASH_SHA1 = 2,
185 IKEV1_HASH_TIGER = 3,
186 IKEV1_HASH_SHA2_256 = 4,
187 IKEV1_HASH_SHA2_384 = 5,
188 IKEV1_HASH_SHA2_512 = 6,
189 } ikev1_hash_t;
190
191 /**
192 * IKEv1 Transform ID IKE.
193 */
194 typedef enum {
195 IKEV1_TRANSID_KEY_IKE = 1,
196 } ikev1_ike_transid_t;
197
198 /**
199 * IKEv1 Transform ID ESP encryption algorithm.
200 */
201 typedef enum {
202 IKEV1_ESP_ENCR_DES_IV64 = 1,
203 IKEV1_ESP_ENCR_DES = 2,
204 IKEV1_ESP_ENCR_3DES = 3,
205 IKEV1_ESP_ENCR_RC5 = 4,
206 IKEV1_ESP_ENCR_IDEA = 5,
207 IKEV1_ESP_ENCR_CAST = 6,
208 IKEV1_ESP_ENCR_BLOWFISH = 7,
209 IKEV1_ESP_ENCR_3IDEA = 8,
210 IKEV1_ESP_ENCR_DES_IV32 = 9,
211 IKEV1_ESP_ENCR_RC4 = 10,
212 IKEV1_ESP_ENCR_NULL = 11,
213 IKEV1_ESP_ENCR_AES_CBC = 12,
214 IKEV1_ESP_ENCR_AES_CTR = 13,
215 IKEV1_ESP_ENCR_AES_CCM_8 = 14,
216 IKEV1_ESP_ENCR_AES_CCM_12 = 15,
217 IKEV1_ESP_ENCR_AES_CCM_16 = 16,
218 IKEV1_ESP_ENCR_AES_GCM_8 = 18,
219 IKEV1_ESP_ENCR_AES_GCM_12 = 19,
220 IKEV1_ESP_ENCR_AES_GCM_16 = 20,
221 IKEV1_ESP_ENCR_SEED_CBC = 21,
222 IKEV1_ESP_ENCR_CAMELLIA = 22,
223 IKEV1_ESP_ENCR_NULL_AUTH_AES_GMAC = 23,
224 /* FreeS/WAN proprietary */
225 IKEV1_ESP_ENCR_SERPENT = 252,
226 IKEV1_ESP_ENCR_TWOFISH = 253,
227 } ikev1_esp_transid_t;
228
229 /**
230 * IKEv1 Transform ID AH authentication algorithm.
231 */
232 typedef enum {
233 IKEV1_AH_HMAC_MD5 = 2,
234 IKEV1_AH_HMAC_SHA = 3,
235 IKEV1_AH_DES_MAC = 4,
236 IKEV1_AH_HMAC_SHA2_256 = 5,
237 IKEV1_AH_HMAC_SHA2_384 = 6,
238 IKEV1_AH_HMAC_SHA2_512 = 7,
239 IKEV1_AH_RIPEMD = 8,
240 IKEV1_AH_AES_XCBC_MAC = 9,
241 IKEV1_AH_RSA = 10,
242 IKEV1_AH_AES_128_GMAC = 11,
243 IKEV1_AH_AES_192_GMAC = 12,
244 IKEV1_AH_AES_256_GMAC = 13,
245 } ikev1_ah_transid_t;
246
247 /**
248 * IKEv1 authentication algorithm.
249 */
250 typedef enum {
251 IKEV1_AUTH_HMAC_MD5 = 1,
252 IKEV1_AUTH_HMAC_SHA = 2,
253 IKEV1_AUTH_DES_MAC = 3,
254 IKEV1_AUTH_KPDK = 4,
255 IKEV1_AUTH_HMAC_SHA2_256 = 5,
256 IKEV1_AUTH_HMAC_SHA2_384 = 6,
257 IKEV1_AUTH_HMAC_SHA2_512 = 7,
258 IKEV1_AUTH_HMAC_RIPEMD = 8,
259 IKEV1_AUTH_AES_XCBC_MAC = 9,
260 IKEV1_AUTH_SIG_RSA = 10,
261 IKEV1_AUTH_AES_128_GMAC = 11,
262 IKEV1_AUTH_AES_192_GMAC = 12,
263 IKEV1_AUTH_AES_256_GMAC = 13,
264 } ikev1_auth_algo_t;
265
266 /**
267 * IKEv1 ESP Encapsulation mode.
268 */
269 typedef enum {
270 IKEV1_ENCAP_TUNNEL = 1,
271 IKEV1_ENCAP_TRANSPORT = 2,
272 IKEV1_ENCAP_UDP_TUNNEL = 3,
273 IKEV1_ENCAP_UDP_TRANSPORT = 4,
274 IKEV1_ENCAP_UDP_TUNNEL_DRAFT_00_03 = 61443,
275 IKEV1_ENCAP_UDP_TRANSPORT_DRAFT_00_03 = 61444,
276 } ikev1_esp_encap_t;
277
278 /**
279 * IKEv1 Life duration types.
280 */
281 typedef enum {
282 IKEV1_LIFE_TYPE_SECONDS = 1,
283 IKEV1_LIFE_TYPE_KILOBYTES = 2,
284 } ikev1_life_type_t;
285
286 /**
287 * IKEv1 authentication methods
288 */
289 typedef enum {
290 IKEV1_AUTH_PSK = 1,
291 IKEV1_AUTH_DSS_SIG = 2,
292 IKEV1_AUTH_RSA_SIG = 3,
293 IKEV1_AUTH_RSA_ENC = 4,
294 IKEV1_AUTH_RSA_ENC_REV = 5,
295 IKEV1_AUTH_ECDSA_256 = 9,
296 IKEV1_AUTH_ECDSA_384 = 10,
297 IKEV1_AUTH_ECDSA_521 = 11,
298 /* XAuth Modes */
299 IKEV1_AUTH_XAUTH_INIT_PSK = 65001,
300 IKEV1_AUTH_XAUTH_RESP_PSK = 65002,
301 IKEV1_AUTH_XAUTH_INIT_DSS = 65003,
302 IKEV1_AUTH_XAUTH_RESP_DSS = 65004,
303 IKEV1_AUTH_XAUTH_INIT_RSA = 65005,
304 IKEV1_AUTH_XAUTH_RESP_RSA = 65006,
305 IKEV1_AUTH_XAUTH_INIT_RSA_ENC = 65007,
306 IKEV1_AUTH_XAUTH_RESP_RSA_ENC = 65008,
307 IKEV1_AUTH_XAUTH_INIT_RSA_ENC_REV = 65009,
308 IKEV1_AUTH_XAUTH_RESP_RSA_ENC_REV = 65010,
309 /* Hybrid Modes */
310 IKEV1_AUTH_HYBRID_INIT_RSA = 64221,
311 IKEV1_AUTH_HYBRID_RESP_RSA = 64222,
312 IKEV1_AUTH_HYBRID_INIT_DSS = 64223,
313 IKEV1_AUTH_HYBRID_RESP_DSS = 64224,
314 } ikev1_auth_method_t;
315
316 /**
317 * IKEv1 IPComp transform IDs
318 */
319 typedef enum {
320 IKEV1_IPCOMP_OUI = 1,
321 IKEV1_IPCOMP_DEFLATE = 2,
322 IKEV1_IPCOMP_LZS = 3,
323 } ikev1_ipcomp_transform_t;
324
325 METHOD(payload_t, verify, status_t,
326 private_proposal_substructure_t *this)
327 {
328 status_t status = SUCCESS;
329 enumerator_t *enumerator;
330 payload_t *current;
331
332 if (this->next_payload != PL_NONE && this->next_payload != 2)
333 {
334 /* must be 0 or 2 */
335 DBG1(DBG_ENC, "inconsistent next payload");
336 return FAILED;
337 }
338 if (this->transforms_count != this->transforms->get_count(this->transforms))
339 {
340 /* must be the same! */
341 DBG1(DBG_ENC, "transform count invalid");
342 return FAILED;
343 }
344
345 switch (this->protocol_id)
346 {
347 case PROTO_IPCOMP:
348 if (this->spi.len != 2 && this->spi.len != 4)
349 {
350 DBG1(DBG_ENC, "invalid CPI length in IPCOMP proposal");
351 return FAILED;
352 }
353 break;
354 case PROTO_AH:
355 case PROTO_ESP:
356 if (this->spi.len != 4)
357 {
358 DBG1(DBG_ENC, "invalid SPI length in %N proposal",
359 protocol_id_names, this->protocol_id);
360 return FAILED;
361 }
362 break;
363 case PROTO_IKE:
364 if (this->type == PLV1_PROPOSAL_SUBSTRUCTURE)
365 {
366 if (this->spi.len <= 16)
367 { /* according to RFC 2409, section 3.5 anything between
368 * 0 and 16 is fine */
369 break;
370 }
371 }
372 else if (this->spi.len == 0 || this->spi.len == 8)
373 {
374 break;
375 }
376 DBG1(DBG_ENC, "invalid SPI length in IKE proposal");
377 return FAILED;
378 default:
379 break;
380 }
381 enumerator = this->transforms->create_enumerator(this->transforms);
382 while (enumerator->enumerate(enumerator, &current))
383 {
384 status = current->verify(current);
385 if (status != SUCCESS)
386 {
387 DBG1(DBG_ENC, "TRANSFORM_SUBSTRUCTURE verification failed");
388 break;
389 }
390 }
391 enumerator->destroy(enumerator);
392
393 /* proposal number is checked in SA payload */
394 return status;
395 }
396
397 METHOD(payload_t, get_encoding_rules, int,
398 private_proposal_substructure_t *this, encoding_rule_t **rules)
399 {
400 if (this->type == PLV2_PROPOSAL_SUBSTRUCTURE)
401 {
402 *rules = encodings_v2;
403 return countof(encodings_v2);
404 }
405 *rules = encodings_v1;
406 return countof(encodings_v1);
407 }
408
409 METHOD(payload_t, get_header_length, int,
410 private_proposal_substructure_t *this)
411 {
412 return 8 + this->spi_size;
413 }
414
415 METHOD(payload_t, get_type, payload_type_t,
416 private_proposal_substructure_t *this)
417 {
418 return this->type;
419 }
420
421 METHOD(payload_t, get_next_type, payload_type_t,
422 private_proposal_substructure_t *this)
423 {
424 return this->next_payload;
425 }
426
427 METHOD(payload_t, set_next_type, void,
428 private_proposal_substructure_t *this, payload_type_t type)
429 {
430 }
431
432 /**
433 * (re-)compute the length of the payload.
434 */
435 static void compute_length(private_proposal_substructure_t *this)
436 {
437 enumerator_t *enumerator;
438 payload_t *transform;
439
440 this->transforms_count = 0;
441 this->proposal_length = get_header_length(this);
442 enumerator = this->transforms->create_enumerator(this->transforms);
443 while (enumerator->enumerate(enumerator, &transform))
444 {
445 this->proposal_length += transform->get_length(transform);
446 this->transforms_count++;
447 }
448 enumerator->destroy(enumerator);
449 }
450
451 METHOD(payload_t, get_length, size_t,
452 private_proposal_substructure_t *this)
453 {
454 return this->proposal_length;
455 }
456
457 /**
458 * Add a transform substructure to the proposal
459 */
460 static void add_transform_substructure(private_proposal_substructure_t *this,
461 transform_substructure_t *transform)
462 {
463 if (this->transforms->get_count(this->transforms) > 0)
464 {
465 transform_substructure_t *last;
466
467 this->transforms->get_last(this->transforms, (void **)&last);
468 last->set_is_last_transform(last, FALSE);
469 }
470 transform->set_is_last_transform(transform,TRUE);
471 this->transforms->insert_last(this->transforms, transform);
472 compute_length(this);
473 }
474
475 METHOD(proposal_substructure_t, set_is_last_proposal, void,
476 private_proposal_substructure_t *this, bool is_last)
477 {
478 this->next_payload = is_last ? 0 : PROPOSAL_TYPE_VALUE;
479 }
480
481 METHOD(proposal_substructure_t, set_proposal_number, void,
482 private_proposal_substructure_t *this,u_int8_t proposal_number)
483 {
484 this->proposal_number = proposal_number;
485 }
486
487 METHOD(proposal_substructure_t, get_proposal_number, u_int8_t,
488 private_proposal_substructure_t *this)
489 {
490 return this->proposal_number;
491 }
492
493 METHOD(proposal_substructure_t, set_protocol_id, void,
494 private_proposal_substructure_t *this,u_int8_t protocol_id)
495 {
496 this->protocol_id = protocol_id;
497 }
498
499 METHOD(proposal_substructure_t, get_protocol_id, u_int8_t,
500 private_proposal_substructure_t *this)
501 {
502 return this->protocol_id;
503 }
504
505 METHOD(proposal_substructure_t, set_spi, void,
506 private_proposal_substructure_t *this, chunk_t spi)
507 {
508 free(this->spi.ptr);
509 this->spi = chunk_clone(spi);
510 this->spi_size = spi.len;
511 compute_length(this);
512 }
513
514 METHOD(proposal_substructure_t, get_spi, chunk_t,
515 private_proposal_substructure_t *this)
516 {
517 return this->spi;
518 }
519
520 METHOD(proposal_substructure_t, get_cpi, bool,
521 private_proposal_substructure_t *this, u_int16_t *cpi)
522 {
523
524 transform_substructure_t *transform;
525 enumerator_t *enumerator;
526
527 if (this->protocol_id != PROTO_IPCOMP)
528 {
529 return FALSE;
530 }
531
532 enumerator = this->transforms->create_enumerator(this->transforms);
533 while (enumerator->enumerate(enumerator, &transform))
534 {
535 if (transform->get_transform_id(transform) == IKEV1_IPCOMP_DEFLATE)
536 {
537 if (cpi)
538 {
539 *cpi = htons(untoh16(this->spi.ptr + this->spi.len - 2));
540 }
541 enumerator->destroy(enumerator);
542 return TRUE;
543 }
544 }
545 enumerator->destroy(enumerator);
546 return FALSE;
547 }
548
549 /**
550 * Add a transform to a proposal for IKEv2
551 */
552 static void add_to_proposal_v2(proposal_t *proposal,
553 transform_substructure_t *transform)
554 {
555 transform_attribute_t *tattr;
556 enumerator_t *enumerator;
557 u_int16_t key_length = 0;
558
559 enumerator = transform->create_attribute_enumerator(transform);
560 while (enumerator->enumerate(enumerator, &tattr))
561 {
562 if (tattr->get_attribute_type(tattr) == TATTR_IKEV2_KEY_LENGTH)
563 {
564 key_length = tattr->get_value(tattr);
565 break;
566 }
567 }
568 enumerator->destroy(enumerator);
569
570 proposal->add_algorithm(proposal,
571 transform->get_transform_type_or_number(transform),
572 transform->get_transform_id(transform), key_length);
573 }
574
575 /**
576 * Map IKEv1 to IKEv2 algorithms
577 */
578 typedef struct {
579 u_int16_t ikev1;
580 u_int16_t ikev2;
581 } algo_map_t;
582
583 /**
584 * Encryption algorithm mapping
585 */
586 static algo_map_t map_encr[] = {
587 { IKEV1_ENCR_DES_CBC, ENCR_DES },
588 { IKEV1_ENCR_IDEA_CBC, ENCR_IDEA },
589 { IKEV1_ENCR_BLOWFISH_CBC, ENCR_BLOWFISH },
590 { IKEV1_ENCR_3DES_CBC, ENCR_3DES },
591 { IKEV1_ENCR_CAST_CBC, ENCR_CAST },
592 { IKEV1_ENCR_AES_CBC, ENCR_AES_CBC },
593 { IKEV1_ENCR_CAMELLIA_CBC, ENCR_CAMELLIA_CBC },
594 { IKEV1_ENCR_SERPENT_CBC, ENCR_SERPENT_CBC },
595 { IKEV1_ENCR_TWOFISH_CBC, ENCR_TWOFISH_CBC },
596 };
597
598 /**
599 * Integrity algorithm mapping
600 */
601 static algo_map_t map_integ[] = {
602 { IKEV1_HASH_MD5, AUTH_HMAC_MD5_96 },
603 { IKEV1_HASH_SHA1, AUTH_HMAC_SHA1_96 },
604 { IKEV1_HASH_SHA2_256, AUTH_HMAC_SHA2_256_128 },
605 { IKEV1_HASH_SHA2_384, AUTH_HMAC_SHA2_384_192 },
606 { IKEV1_HASH_SHA2_512, AUTH_HMAC_SHA2_512_256 },
607 };
608
609 /**
610 * PRF algorithm mapping
611 */
612 static algo_map_t map_prf[] = {
613 { IKEV1_HASH_MD5, PRF_HMAC_MD5 },
614 { IKEV1_HASH_SHA1, PRF_HMAC_SHA1 },
615 { IKEV1_HASH_SHA2_256, PRF_HMAC_SHA2_256 },
616 { IKEV1_HASH_SHA2_384, PRF_HMAC_SHA2_384 },
617 { IKEV1_HASH_SHA2_512, PRF_HMAC_SHA2_512 },
618 };
619
620 /**
621 * ESP encryption algorithm mapping
622 */
623 static algo_map_t map_esp[] = {
624 { IKEV1_ESP_ENCR_DES_IV64, ENCR_DES_IV64 },
625 { IKEV1_ESP_ENCR_DES, ENCR_DES },
626 { IKEV1_ESP_ENCR_3DES, ENCR_3DES },
627 { IKEV1_ESP_ENCR_RC5, ENCR_RC5 },
628 { IKEV1_ESP_ENCR_IDEA, ENCR_IDEA },
629 { IKEV1_ESP_ENCR_CAST, ENCR_CAST },
630 { IKEV1_ESP_ENCR_BLOWFISH, ENCR_BLOWFISH },
631 { IKEV1_ESP_ENCR_3IDEA, ENCR_3IDEA },
632 { IKEV1_ESP_ENCR_DES_IV32, ENCR_DES_IV32 },
633 { IKEV1_ESP_ENCR_NULL, ENCR_NULL },
634 { IKEV1_ESP_ENCR_AES_CBC, ENCR_AES_CBC },
635 { IKEV1_ESP_ENCR_AES_CTR, ENCR_AES_CTR },
636 { IKEV1_ESP_ENCR_AES_CCM_8, ENCR_AES_CCM_ICV8 },
637 { IKEV1_ESP_ENCR_AES_CCM_12, ENCR_AES_CCM_ICV12 },
638 { IKEV1_ESP_ENCR_AES_CCM_16, ENCR_AES_CCM_ICV16 },
639 { IKEV1_ESP_ENCR_AES_GCM_8, ENCR_AES_GCM_ICV8 },
640 { IKEV1_ESP_ENCR_AES_GCM_12, ENCR_AES_GCM_ICV12 },
641 { IKEV1_ESP_ENCR_AES_GCM_16, ENCR_AES_GCM_ICV16 },
642 { IKEV1_ESP_ENCR_CAMELLIA, ENCR_CAMELLIA_CBC },
643 { IKEV1_ESP_ENCR_NULL_AUTH_AES_GMAC, ENCR_NULL_AUTH_AES_GMAC },
644 { IKEV1_ESP_ENCR_SERPENT, ENCR_SERPENT_CBC },
645 { IKEV1_ESP_ENCR_TWOFISH, ENCR_TWOFISH_CBC },
646 };
647
648 /**
649 * AH authentication algorithm mapping
650 */
651 static algo_map_t map_ah[] = {
652 { IKEV1_AH_HMAC_MD5, AUTH_HMAC_MD5_96 },
653 { IKEV1_AH_HMAC_SHA, AUTH_HMAC_SHA1_96 },
654 { IKEV1_AH_DES_MAC, AUTH_DES_MAC },
655 { IKEV1_AH_HMAC_SHA2_256, AUTH_HMAC_SHA2_256_128 },
656 { IKEV1_AH_HMAC_SHA2_384, AUTH_HMAC_SHA2_384_192 },
657 { IKEV1_AH_HMAC_SHA2_512, AUTH_HMAC_SHA2_512_256 },
658 { IKEV1_AH_AES_XCBC_MAC, AUTH_AES_XCBC_96 },
659 { IKEV1_AH_AES_128_GMAC, AUTH_AES_128_GMAC },
660 { IKEV1_AH_AES_192_GMAC, AUTH_AES_192_GMAC },
661 { IKEV1_AH_AES_256_GMAC, AUTH_AES_256_GMAC },
662 };
663
664 /**
665 * ESP/AH authentication algorithm mapping
666 */
667 static algo_map_t map_auth[] = {
668 { IKEV1_AUTH_HMAC_MD5, AUTH_HMAC_MD5_96 },
669 { IKEV1_AUTH_HMAC_SHA, AUTH_HMAC_SHA1_96 },
670 { IKEV1_AUTH_DES_MAC, AUTH_DES_MAC },
671 { IKEV1_AUTH_KPDK, AUTH_KPDK_MD5 },
672 { IKEV1_AUTH_HMAC_SHA2_256, AUTH_HMAC_SHA2_256_128 },
673 { IKEV1_AUTH_HMAC_SHA2_384, AUTH_HMAC_SHA2_384_192 },
674 { IKEV1_AUTH_HMAC_SHA2_512, AUTH_HMAC_SHA2_512_256 },
675 { IKEV1_AUTH_AES_XCBC_MAC, AUTH_AES_XCBC_96 },
676 { IKEV1_AUTH_AES_128_GMAC, AUTH_AES_128_GMAC },
677 { IKEV1_AUTH_AES_192_GMAC, AUTH_AES_192_GMAC },
678 { IKEV1_AUTH_AES_256_GMAC, AUTH_AES_256_GMAC },
679 };
680
681 /**
682 * Map an IKEv1 to an IKEv2 identifier
683 */
684 static u_int16_t ikev2_from_ikev1(algo_map_t *map, int count, u_int16_t def,
685 u_int16_t value)
686 {
687 int i;
688
689 for (i = 0; i < count; i++)
690 {
691 if (map[i].ikev1 == value)
692 {
693 return map[i].ikev2;
694 }
695 }
696 return def;
697 }
698
699 /**
700 * Map an IKEv2 to an IKEv1 identifier
701 */
702 static u_int16_t ikev1_from_ikev2(algo_map_t *map, int count, u_int16_t value)
703 {
704 int i;
705
706 for (i = 0; i < count; i++)
707 {
708 if (map[i].ikev2 == value)
709 {
710 return map[i].ikev1;
711 }
712 }
713 return 0;
714 }
715
716 /**
717 * Get IKEv2 algorithm from IKEv1 identifier
718 */
719 static u_int16_t get_alg_from_ikev1(transform_type_t type, u_int16_t value)
720 {
721 switch (type)
722 {
723 case ENCRYPTION_ALGORITHM:
724 return ikev2_from_ikev1(map_encr, countof(map_encr),
725 ENCR_UNDEFINED, value);
726 case INTEGRITY_ALGORITHM:
727 return ikev2_from_ikev1(map_integ, countof(map_integ),
728 AUTH_UNDEFINED, value);
729 case PSEUDO_RANDOM_FUNCTION:
730 return ikev2_from_ikev1(map_prf, countof(map_prf),
731 PRF_UNDEFINED, value);
732 default:
733 return 0;
734 }
735 }
736
737 /**
738 * Get IKEv1 algorithm from IKEv2 identifier
739 */
740 static u_int16_t get_ikev1_from_alg(transform_type_t type, u_int16_t value)
741 {
742 switch (type)
743 {
744 case ENCRYPTION_ALGORITHM:
745 return ikev1_from_ikev2(map_encr, countof(map_encr), value);
746 case INTEGRITY_ALGORITHM:
747 return ikev1_from_ikev2(map_integ, countof(map_integ), value);
748 case PSEUDO_RANDOM_FUNCTION:
749 return ikev1_from_ikev2(map_prf, countof(map_prf), value);
750 default:
751 return 0;
752 }
753 }
754
755 /**
756 * Get IKEv2 algorithm from IKEv1 ESP/AH transform ID
757 */
758 static u_int16_t get_alg_from_ikev1_transid(transform_type_t type,
759 u_int16_t value)
760 {
761 switch (type)
762 {
763 case ENCRYPTION_ALGORITHM:
764 return ikev2_from_ikev1(map_esp, countof(map_esp),
765 ENCR_UNDEFINED, value);
766 case INTEGRITY_ALGORITHM:
767 return ikev2_from_ikev1(map_ah, countof(map_ah),
768 AUTH_UNDEFINED, value);
769 default:
770 return 0;
771 }
772 }
773
774 /**
775 * Get IKEv1 ESP/AH transform ID from IKEv2 identifier
776 */
777 static u_int16_t get_ikev1_transid_from_alg(transform_type_t type,
778 u_int16_t value)
779 {
780 switch (type)
781 {
782 case ENCRYPTION_ALGORITHM:
783 return ikev1_from_ikev2(map_esp, countof(map_esp), value);
784 case INTEGRITY_ALGORITHM:
785 return ikev1_from_ikev2(map_ah, countof(map_ah), value);
786 default:
787 return 0;
788 }
789 }
790
791 /**
792 * Get IKEv1 authentication algorithm from IKEv2 identifier
793 */
794 static u_int16_t get_alg_from_ikev1_auth(u_int16_t value)
795 {
796 return ikev2_from_ikev1(map_auth, countof(map_auth), AUTH_UNDEFINED, value);
797 }
798
799 /**
800 * Get IKEv1 authentication algorithm from IKEv2 identifier
801 */
802 static u_int16_t get_ikev1_auth_from_alg(u_int16_t value)
803 {
804 return ikev1_from_ikev2(map_auth, countof(map_auth), value);
805 }
806
807 /**
808 * Get IKEv1 authentication attribute from auth_method_t
809 */
810 static u_int16_t get_ikev1_auth(auth_method_t method)
811 {
812 switch (method)
813 {
814 case AUTH_RSA:
815 return IKEV1_AUTH_RSA_SIG;
816 case AUTH_DSS:
817 return IKEV1_AUTH_DSS_SIG;
818 case AUTH_XAUTH_INIT_PSK:
819 return IKEV1_AUTH_XAUTH_INIT_PSK;
820 case AUTH_XAUTH_RESP_PSK:
821 return IKEV1_AUTH_XAUTH_RESP_PSK;
822 case AUTH_XAUTH_INIT_RSA:
823 return IKEV1_AUTH_XAUTH_INIT_RSA;
824 case AUTH_XAUTH_RESP_RSA:
825 return IKEV1_AUTH_XAUTH_RESP_RSA;
826 case AUTH_HYBRID_INIT_RSA:
827 return IKEV1_AUTH_HYBRID_INIT_RSA;
828 case AUTH_HYBRID_RESP_RSA:
829 return IKEV1_AUTH_HYBRID_RESP_RSA;
830 case AUTH_ECDSA_256:
831 return IKEV1_AUTH_ECDSA_256;
832 case AUTH_ECDSA_384:
833 return IKEV1_AUTH_ECDSA_384;
834 case AUTH_ECDSA_521:
835 return IKEV1_AUTH_ECDSA_521;
836 case AUTH_PSK:
837 default:
838 return IKEV1_AUTH_PSK;
839 }
840 }
841
842 /**
843 * Get IKEv1 encapsulation mode
844 */
845 static u_int16_t get_ikev1_mode(ipsec_mode_t mode, encap_t udp)
846 {
847 switch (mode)
848 {
849 case MODE_TUNNEL:
850 switch (udp)
851 {
852 case ENCAP_UDP:
853 return IKEV1_ENCAP_UDP_TUNNEL;
854 case ENCAP_UDP_DRAFT_00_03:
855 return IKEV1_ENCAP_UDP_TUNNEL_DRAFT_00_03;
856 default:
857 return IKEV1_ENCAP_TUNNEL;
858 }
859 case MODE_TRANSPORT:
860 switch (udp)
861 {
862 case ENCAP_UDP:
863 return IKEV1_ENCAP_UDP_TRANSPORT;
864 case ENCAP_UDP_DRAFT_00_03:
865 return IKEV1_ENCAP_UDP_TRANSPORT_DRAFT_00_03;
866 default:
867 return IKEV1_ENCAP_TRANSPORT;
868 }
869 default:
870 return IKEV1_ENCAP_TUNNEL;
871 }
872 }
873
874 /**
875 * Add an IKE transform to a proposal for IKEv1
876 */
877 static void add_to_proposal_v1_ike(proposal_t *proposal,
878 transform_substructure_t *transform)
879 {
880 transform_attribute_type_t type;
881 transform_attribute_t *tattr;
882 enumerator_t *enumerator;
883 u_int16_t value, key_length = 0;
884 u_int16_t encr = ENCR_UNDEFINED;
885
886 enumerator = transform->create_attribute_enumerator(transform);
887 while (enumerator->enumerate(enumerator, &tattr))
888 {
889 type = tattr->get_attribute_type(tattr);
890 value = tattr->get_value(tattr);
891 switch (type)
892 {
893 case TATTR_PH1_ENCRYPTION_ALGORITHM:
894 encr = get_alg_from_ikev1(ENCRYPTION_ALGORITHM, value);
895 break;
896 case TATTR_PH1_KEY_LENGTH:
897 key_length = value;
898 break;
899 case TATTR_PH1_HASH_ALGORITHM:
900 proposal->add_algorithm(proposal, INTEGRITY_ALGORITHM,
901 get_alg_from_ikev1(INTEGRITY_ALGORITHM, value), 0);
902 proposal->add_algorithm(proposal, PSEUDO_RANDOM_FUNCTION,
903 get_alg_from_ikev1(PSEUDO_RANDOM_FUNCTION, value), 0);
904 break;
905 case TATTR_PH1_GROUP:
906 proposal->add_algorithm(proposal, DIFFIE_HELLMAN_GROUP,
907 value, 0);
908 break;
909 default:
910 break;
911 }
912 }
913 enumerator->destroy(enumerator);
914
915 if (encr != ENCR_UNDEFINED)
916 {
917 if (encr == ENCR_AES_CBC && !key_length)
918 { /* some implementations don't send a Key Length attribute for
919 * AES-128, early drafts of RFC 3602 allowed that */
920 key_length = 128;
921 }
922 proposal->add_algorithm(proposal, ENCRYPTION_ALGORITHM, encr, key_length);
923 }
924 }
925
926 /**
927 * Add an ESP/AH transform to a proposal for IKEv1
928 */
929 static void add_to_proposal_v1(proposal_t *proposal,
930 transform_substructure_t *transform, protocol_id_t proto)
931 {
932 transform_attribute_type_t type;
933 transform_attribute_t *tattr;
934 enumerator_t *enumerator;
935 u_int16_t encr, value, key_length = 0;
936
937 enumerator = transform->create_attribute_enumerator(transform);
938 while (enumerator->enumerate(enumerator, &tattr))
939 {
940 type = tattr->get_attribute_type(tattr);
941 value = tattr->get_value(tattr);
942 switch (type)
943 {
944 case TATTR_PH2_KEY_LENGTH:
945 key_length = value;
946 break;
947 case TATTR_PH2_AUTH_ALGORITHM:
948 proposal->add_algorithm(proposal, INTEGRITY_ALGORITHM,
949 get_alg_from_ikev1_auth(value), 0);
950 break;
951 case TATTR_PH2_GROUP:
952 proposal->add_algorithm(proposal, DIFFIE_HELLMAN_GROUP,
953 value, 0);
954 break;
955 default:
956 break;
957 }
958 }
959 enumerator->destroy(enumerator);
960
961 /* TODO-IKEv1: handle ESN attribute */
962 proposal->add_algorithm(proposal, EXTENDED_SEQUENCE_NUMBERS,
963 NO_EXT_SEQ_NUMBERS, 0);
964 if (proto == PROTO_ESP)
965 {
966 encr = get_alg_from_ikev1_transid(ENCRYPTION_ALGORITHM,
967 transform->get_transform_id(transform));
968 if (encr)
969 {
970 if (encr == ENCR_AES_CBC && !key_length)
971 { /* some implementations don't send a Key Length attribute for
972 * AES-128, early drafts of RFC 3602 allowed that for IKE, some
973 * also seem to do it for ESP */
974 key_length = 128;
975 }
976 proposal->add_algorithm(proposal, ENCRYPTION_ALGORITHM, encr,
977 key_length);
978 }
979 }
980 }
981
982 METHOD(proposal_substructure_t, get_proposals, void,
983 private_proposal_substructure_t *this, linked_list_t *proposals)
984 {
985 transform_substructure_t *transform;
986 enumerator_t *enumerator;
987 proposal_t *proposal = NULL;
988 u_int64_t spi = 0;
989
990 switch (this->spi.len)
991 {
992 case 4:
993 spi = *((u_int32_t*)this->spi.ptr);
994 break;
995 case 8:
996 spi = *((u_int64_t*)this->spi.ptr);
997 break;
998 default:
999 break;
1000 }
1001
1002 enumerator = this->transforms->create_enumerator(this->transforms);
1003 while (enumerator->enumerate(enumerator, &transform))
1004 {
1005 if (!proposal)
1006 {
1007 proposal = proposal_create(this->protocol_id, this->proposal_number);
1008 proposal->set_spi(proposal, spi);
1009 proposals->insert_last(proposals, proposal);
1010 }
1011 if (this->type == PLV2_PROPOSAL_SUBSTRUCTURE)
1012 {
1013 add_to_proposal_v2(proposal, transform);
1014 }
1015 else
1016 {
1017 switch (this->protocol_id)
1018 {
1019 case PROTO_IKE:
1020 add_to_proposal_v1_ike(proposal, transform);
1021 break;
1022 case PROTO_ESP:
1023 case PROTO_AH:
1024 add_to_proposal_v1(proposal, transform, this->protocol_id);
1025 break;
1026 default:
1027 break;
1028 }
1029 /* create a new proposal for each transform in IKEv1 */
1030 proposal = NULL;
1031 }
1032 }
1033 enumerator->destroy(enumerator);
1034 }
1035
1036 METHOD(proposal_substructure_t, create_substructure_enumerator, enumerator_t*,
1037 private_proposal_substructure_t *this)
1038 {
1039 return this->transforms->create_enumerator(this->transforms);
1040 }
1041
1042 /**
1043 * Get an attribute from any transform, 0 if not found
1044 */
1045 static u_int64_t get_attr(private_proposal_substructure_t *this,
1046 transform_attribute_type_t type)
1047 {
1048 enumerator_t *transforms, *attributes;
1049 transform_substructure_t *transform;
1050 transform_attribute_t *attr;
1051
1052 transforms = this->transforms->create_enumerator(this->transforms);
1053 while (transforms->enumerate(transforms, &transform))
1054 {
1055 attributes = transform->create_attribute_enumerator(transform);
1056 while (attributes->enumerate(attributes, &attr))
1057 {
1058 if (attr->get_attribute_type(attr) == type)
1059 {
1060 attributes->destroy(attributes);
1061 transforms->destroy(transforms);
1062 return attr->get_value(attr);
1063 }
1064 }
1065 attributes->destroy(attributes);
1066 }
1067 transforms->destroy(transforms);
1068 return 0;
1069 }
1070
1071 /**
1072 * Look up a lifetime duration of a given kind in all transforms
1073 */
1074 static u_int64_t get_life_duration(private_proposal_substructure_t *this,
1075 transform_attribute_type_t type_attr, ikev1_life_type_t type,
1076 transform_attribute_type_t dur_attr)
1077 {
1078 enumerator_t *transforms, *attributes;
1079 transform_substructure_t *transform;
1080 transform_attribute_t *attr;
1081
1082 transforms = this->transforms->create_enumerator(this->transforms);
1083 while (transforms->enumerate(transforms, &transform))
1084 {
1085 attributes = transform->create_attribute_enumerator(transform);
1086 while (attributes->enumerate(attributes, &attr))
1087 {
1088 if (attr->get_attribute_type(attr) == type_attr &&
1089 attr->get_value(attr) == type)
1090 { /* got type attribute, look for duration following next */
1091 while (attributes->enumerate(attributes, &attr))
1092 {
1093 if (attr->get_attribute_type(attr) == dur_attr)
1094 {
1095 attributes->destroy(attributes);
1096 transforms->destroy(transforms);
1097 return attr->get_value(attr);
1098 }
1099 }
1100 }
1101 }
1102 attributes->destroy(attributes);
1103 }
1104 transforms->destroy(transforms);
1105 return 0;
1106 }
1107
1108 METHOD(proposal_substructure_t, get_lifetime, u_int32_t,
1109 private_proposal_substructure_t *this)
1110 {
1111 u_int32_t duration;
1112
1113 switch (this->protocol_id)
1114 {
1115 case PROTO_IKE:
1116 return get_life_duration(this, TATTR_PH1_LIFE_TYPE,
1117 IKEV1_LIFE_TYPE_SECONDS, TATTR_PH1_LIFE_DURATION);
1118 case PROTO_ESP:
1119 case PROTO_AH:
1120 duration = get_life_duration(this, TATTR_PH2_SA_LIFE_TYPE,
1121 IKEV1_LIFE_TYPE_SECONDS, TATTR_PH2_SA_LIFE_DURATION);
1122 if (!duration)
1123 { /* default to 8 hours, RFC 2407 */
1124 return 28800;
1125 }
1126 return duration;
1127 default:
1128 return 0;
1129 }
1130 }
1131
1132 METHOD(proposal_substructure_t, get_lifebytes, u_int64_t,
1133 private_proposal_substructure_t *this)
1134 {
1135 switch (this->protocol_id)
1136 {
1137 case PROTO_ESP:
1138 case PROTO_AH:
1139 return 1000 * get_life_duration(this, TATTR_PH2_SA_LIFE_TYPE,
1140 IKEV1_LIFE_TYPE_KILOBYTES, TATTR_PH2_SA_LIFE_DURATION);
1141 case PROTO_IKE:
1142 default:
1143 return 0;
1144 }
1145 }
1146
1147 METHOD(proposal_substructure_t, get_auth_method, auth_method_t,
1148 private_proposal_substructure_t *this)
1149 {
1150 switch (get_attr(this, TATTR_PH1_AUTH_METHOD))
1151 {
1152 case IKEV1_AUTH_PSK:
1153 return AUTH_PSK;
1154 case IKEV1_AUTH_RSA_SIG:
1155 return AUTH_RSA;
1156 case IKEV1_AUTH_DSS_SIG:
1157 return AUTH_DSS;
1158 case IKEV1_AUTH_XAUTH_INIT_PSK:
1159 return AUTH_XAUTH_INIT_PSK;
1160 case IKEV1_AUTH_XAUTH_RESP_PSK:
1161 return AUTH_XAUTH_RESP_PSK;
1162 case IKEV1_AUTH_XAUTH_INIT_RSA:
1163 return AUTH_XAUTH_INIT_RSA;
1164 case IKEV1_AUTH_XAUTH_RESP_RSA:
1165 return AUTH_XAUTH_RESP_RSA;
1166 case IKEV1_AUTH_HYBRID_INIT_RSA:
1167 return AUTH_HYBRID_INIT_RSA;
1168 case IKEV1_AUTH_HYBRID_RESP_RSA:
1169 return AUTH_HYBRID_RESP_RSA;
1170 case IKEV1_AUTH_ECDSA_256:
1171 return AUTH_ECDSA_256;
1172 case IKEV1_AUTH_ECDSA_384:
1173 return AUTH_ECDSA_384;
1174 case IKEV1_AUTH_ECDSA_521:
1175 return AUTH_ECDSA_521;
1176 default:
1177 return AUTH_NONE;
1178 }
1179 }
1180
1181 METHOD(proposal_substructure_t, get_encap_mode, ipsec_mode_t,
1182 private_proposal_substructure_t *this, bool *udp)
1183 {
1184 *udp = FALSE;
1185 switch (get_attr(this, TATTR_PH2_ENCAP_MODE))
1186 {
1187 case IKEV1_ENCAP_TRANSPORT:
1188 return MODE_TRANSPORT;
1189 case IKEV1_ENCAP_TUNNEL:
1190 return MODE_TUNNEL;
1191 case IKEV1_ENCAP_UDP_TRANSPORT:
1192 case IKEV1_ENCAP_UDP_TRANSPORT_DRAFT_00_03:
1193 *udp = TRUE;
1194 return MODE_TRANSPORT;
1195 case IKEV1_ENCAP_UDP_TUNNEL:
1196 case IKEV1_ENCAP_UDP_TUNNEL_DRAFT_00_03:
1197 *udp = TRUE;
1198 return MODE_TUNNEL;
1199 default:
1200 /* default to TUNNEL, RFC 2407 says implementation specific */
1201 return MODE_TUNNEL;
1202 }
1203 }
1204
1205 METHOD2(payload_t, proposal_substructure_t, destroy, void,
1206 private_proposal_substructure_t *this)
1207 {
1208 this->transforms->destroy_offset(this->transforms,
1209 offsetof(payload_t, destroy));
1210 chunk_free(&this->spi);
1211 free(this);
1212 }
1213
1214 /*
1215 * Described in header.
1216 */
1217 proposal_substructure_t *proposal_substructure_create(payload_type_t type)
1218 {
1219 private_proposal_substructure_t *this;
1220
1221 INIT(this,
1222 .public = {
1223 .payload_interface = {
1224 .verify = _verify,
1225 .get_encoding_rules = _get_encoding_rules,
1226 .get_header_length = _get_header_length,
1227 .get_length = _get_length,
1228 .get_next_type = _get_next_type,
1229 .set_next_type = _set_next_type,
1230 .get_type = _get_type,
1231 .destroy = _destroy,
1232 },
1233 .set_proposal_number = _set_proposal_number,
1234 .get_proposal_number = _get_proposal_number,
1235 .set_protocol_id = _set_protocol_id,
1236 .get_protocol_id = _get_protocol_id,
1237 .set_is_last_proposal = _set_is_last_proposal,
1238 .get_proposals = _get_proposals,
1239 .create_substructure_enumerator = _create_substructure_enumerator,
1240 .set_spi = _set_spi,
1241 .get_spi = _get_spi,
1242 .get_cpi = _get_cpi,
1243 .get_lifetime = _get_lifetime,
1244 .get_lifebytes = _get_lifebytes,
1245 .get_auth_method = _get_auth_method,
1246 .get_encap_mode = _get_encap_mode,
1247 .destroy = _destroy,
1248 },
1249 .next_payload = PL_NONE,
1250 .transforms = linked_list_create(),
1251 .type = type,
1252 );
1253 compute_length(this);
1254
1255 return &this->public;
1256 }
1257
1258 /**
1259 * Add an IKEv1 IKE proposal to the substructure
1260 */
1261 static void set_from_proposal_v1_ike(private_proposal_substructure_t *this,
1262 proposal_t *proposal, u_int32_t lifetime,
1263 auth_method_t method, int number)
1264 {
1265 transform_substructure_t *transform;
1266 u_int16_t alg, key_size;
1267 enumerator_t *enumerator;
1268
1269 transform = transform_substructure_create_type(PLV1_TRANSFORM_SUBSTRUCTURE,
1270 number, IKEV1_TRANSID_KEY_IKE);
1271
1272 enumerator = proposal->create_enumerator(proposal, ENCRYPTION_ALGORITHM);
1273 while (enumerator->enumerate(enumerator, &alg, &key_size))
1274 {
1275 alg = get_ikev1_from_alg(ENCRYPTION_ALGORITHM, alg);
1276 if (alg)
1277 {
1278 transform->add_transform_attribute(transform,
1279 transform_attribute_create_value(PLV1_TRANSFORM_ATTRIBUTE,
1280 TATTR_PH1_ENCRYPTION_ALGORITHM, alg));
1281 if (key_size)
1282 {
1283 transform->add_transform_attribute(transform,
1284 transform_attribute_create_value(PLV1_TRANSFORM_ATTRIBUTE,
1285 TATTR_PH1_KEY_LENGTH, key_size));
1286 }
1287 break;
1288 }
1289 }
1290 enumerator->destroy(enumerator);
1291
1292 /* encode the integrity algorithm as hash and assume use the same PRF */
1293 enumerator = proposal->create_enumerator(proposal, INTEGRITY_ALGORITHM);
1294 while (enumerator->enumerate(enumerator, &alg, &key_size))
1295 {
1296 alg = get_ikev1_from_alg(INTEGRITY_ALGORITHM, alg);
1297 if (alg)
1298 {
1299 transform->add_transform_attribute(transform,
1300 transform_attribute_create_value(PLV1_TRANSFORM_ATTRIBUTE,
1301 TATTR_PH1_HASH_ALGORITHM, alg));
1302 break;
1303 }
1304 }
1305 enumerator->destroy(enumerator);
1306
1307 enumerator = proposal->create_enumerator(proposal, DIFFIE_HELLMAN_GROUP);
1308 if (enumerator->enumerate(enumerator, &alg, &key_size))
1309 {
1310 transform->add_transform_attribute(transform,
1311 transform_attribute_create_value(PLV1_TRANSFORM_ATTRIBUTE,
1312 TATTR_PH1_GROUP, alg));
1313 }
1314 enumerator->destroy(enumerator);
1315
1316 transform->add_transform_attribute(transform,
1317 transform_attribute_create_value(PLV1_TRANSFORM_ATTRIBUTE,
1318 TATTR_PH1_AUTH_METHOD, get_ikev1_auth(method)));
1319 transform->add_transform_attribute(transform,
1320 transform_attribute_create_value(PLV1_TRANSFORM_ATTRIBUTE,
1321 TATTR_PH1_LIFE_TYPE, IKEV1_LIFE_TYPE_SECONDS));
1322 transform->add_transform_attribute(transform,
1323 transform_attribute_create_value(PLV1_TRANSFORM_ATTRIBUTE,
1324 TATTR_PH1_LIFE_DURATION, lifetime));
1325
1326 add_transform_substructure(this, transform);
1327 }
1328
1329 /**
1330 * Add an IKEv1 ESP/AH proposal to the substructure
1331 */
1332 static void set_from_proposal_v1(private_proposal_substructure_t *this,
1333 proposal_t *proposal, u_int32_t lifetime, u_int64_t lifebytes,
1334 ipsec_mode_t mode, encap_t udp, int number)
1335 {
1336 transform_substructure_t *transform = NULL;
1337 u_int16_t alg, transid, key_size;
1338 enumerator_t *enumerator;
1339
1340 enumerator = proposal->create_enumerator(proposal, ENCRYPTION_ALGORITHM);
1341 if (enumerator->enumerate(enumerator, &alg, &key_size))
1342 {
1343 transid = get_ikev1_transid_from_alg(ENCRYPTION_ALGORITHM, alg);
1344 if (transid)
1345 {
1346 transform = transform_substructure_create_type(
1347 PLV1_TRANSFORM_SUBSTRUCTURE, number, transid);
1348 if (key_size)
1349 {
1350 transform->add_transform_attribute(transform,
1351 transform_attribute_create_value(PLV1_TRANSFORM_ATTRIBUTE,
1352 TATTR_PH2_KEY_LENGTH, key_size));
1353 }
1354 }
1355 }
1356 enumerator->destroy(enumerator);
1357
1358 enumerator = proposal->create_enumerator(proposal, INTEGRITY_ALGORITHM);
1359 if (enumerator->enumerate(enumerator, &alg, &key_size))
1360 {
1361 transid = get_ikev1_transid_from_alg(INTEGRITY_ALGORITHM, alg);
1362 alg = get_ikev1_auth_from_alg(alg);
1363 if (transid && alg)
1364 {
1365 if (!transform)
1366 {
1367 transform = transform_substructure_create_type(
1368 PLV1_TRANSFORM_SUBSTRUCTURE, number, transid);
1369 }
1370 transform->add_transform_attribute(transform,
1371 transform_attribute_create_value(PLV1_TRANSFORM_ATTRIBUTE,
1372 TATTR_PH2_AUTH_ALGORITHM, alg));
1373 }
1374 }
1375 enumerator->destroy(enumerator);
1376
1377 if (!transform)
1378 {
1379 return;
1380 }
1381
1382 enumerator = proposal->create_enumerator(proposal, DIFFIE_HELLMAN_GROUP);
1383 if (enumerator->enumerate(enumerator, &alg, &key_size))
1384 {
1385 transform->add_transform_attribute(transform,
1386 transform_attribute_create_value(PLV1_TRANSFORM_ATTRIBUTE,
1387 TATTR_PH2_GROUP, alg));
1388 }
1389 enumerator->destroy(enumerator);
1390
1391 transform->add_transform_attribute(transform,
1392 transform_attribute_create_value(PLV1_TRANSFORM_ATTRIBUTE,
1393 TATTR_PH2_ENCAP_MODE, get_ikev1_mode(mode, udp)));
1394 if (lifetime)
1395 {
1396 transform->add_transform_attribute(transform,
1397 transform_attribute_create_value(PLV1_TRANSFORM_ATTRIBUTE,
1398 TATTR_PH2_SA_LIFE_TYPE, IKEV1_LIFE_TYPE_SECONDS));
1399 transform->add_transform_attribute(transform,
1400 transform_attribute_create_value(PLV1_TRANSFORM_ATTRIBUTE,
1401 TATTR_PH2_SA_LIFE_DURATION, lifetime));
1402 }
1403 if (lifebytes)
1404 {
1405 transform->add_transform_attribute(transform,
1406 transform_attribute_create_value(PLV1_TRANSFORM_ATTRIBUTE,
1407 TATTR_PH2_SA_LIFE_TYPE, IKEV1_LIFE_TYPE_KILOBYTES));
1408 transform->add_transform_attribute(transform,
1409 transform_attribute_create_value(PLV1_TRANSFORM_ATTRIBUTE,
1410 TATTR_PH2_SA_LIFE_DURATION, lifebytes / 1000));
1411 }
1412
1413 add_transform_substructure(this, transform);
1414 }
1415
1416 /**
1417 * Add an IKEv2 proposal to the substructure
1418 */
1419 static void set_from_proposal_v2(private_proposal_substructure_t *this,
1420 proposal_t *proposal)
1421 {
1422 transform_substructure_t *transform;
1423 u_int16_t alg, key_size;
1424 enumerator_t *enumerator;
1425
1426 /* encryption algorithm is only available in ESP */
1427 enumerator = proposal->create_enumerator(proposal, ENCRYPTION_ALGORITHM);
1428 while (enumerator->enumerate(enumerator, &alg, &key_size))
1429 {
1430 transform = transform_substructure_create_type(PLV2_TRANSFORM_SUBSTRUCTURE,
1431 ENCRYPTION_ALGORITHM, alg);
1432 if (key_size)
1433 {
1434 transform->add_transform_attribute(transform,
1435 transform_attribute_create_value(PLV2_TRANSFORM_ATTRIBUTE,
1436 TATTR_IKEV2_KEY_LENGTH, key_size));
1437 }
1438 add_transform_substructure(this, transform);
1439 }
1440 enumerator->destroy(enumerator);
1441
1442 /* integrity algorithms */
1443 enumerator = proposal->create_enumerator(proposal, INTEGRITY_ALGORITHM);
1444 while (enumerator->enumerate(enumerator, &alg, &key_size))
1445 {
1446 transform = transform_substructure_create_type(PLV2_TRANSFORM_SUBSTRUCTURE,
1447 INTEGRITY_ALGORITHM, alg);
1448 add_transform_substructure(this, transform);
1449 }
1450 enumerator->destroy(enumerator);
1451
1452 /* prf algorithms */
1453 enumerator = proposal->create_enumerator(proposal, PSEUDO_RANDOM_FUNCTION);
1454 while (enumerator->enumerate(enumerator, &alg, &key_size))
1455 {
1456 transform = transform_substructure_create_type(PLV2_TRANSFORM_SUBSTRUCTURE,
1457 PSEUDO_RANDOM_FUNCTION, alg);
1458 add_transform_substructure(this, transform);
1459 }
1460 enumerator->destroy(enumerator);
1461
1462 /* dh groups */
1463 enumerator = proposal->create_enumerator(proposal, DIFFIE_HELLMAN_GROUP);
1464 while (enumerator->enumerate(enumerator, &alg, NULL))
1465 {
1466 transform = transform_substructure_create_type(PLV2_TRANSFORM_SUBSTRUCTURE,
1467 DIFFIE_HELLMAN_GROUP, alg);
1468 add_transform_substructure(this, transform);
1469 }
1470 enumerator->destroy(enumerator);
1471
1472 /* extended sequence numbers */
1473 enumerator = proposal->create_enumerator(proposal, EXTENDED_SEQUENCE_NUMBERS);
1474 while (enumerator->enumerate(enumerator, &alg, NULL))
1475 {
1476 transform = transform_substructure_create_type(PLV2_TRANSFORM_SUBSTRUCTURE,
1477 EXTENDED_SEQUENCE_NUMBERS, alg);
1478 add_transform_substructure(this, transform);
1479 }
1480 enumerator->destroy(enumerator);
1481 }
1482
1483 /**
1484 * Set SPI and other data from proposal, compute length
1485 */
1486 static void set_data(private_proposal_substructure_t *this, proposal_t *proposal)
1487 {
1488 u_int64_t spi64;
1489 u_int32_t spi32;
1490
1491 /* add SPI, if necessary */
1492 switch (proposal->get_protocol(proposal))
1493 {
1494 case PROTO_AH:
1495 case PROTO_ESP:
1496 spi32 = proposal->get_spi(proposal);
1497 this->spi = chunk_clone(chunk_from_thing(spi32));
1498 this->spi_size = this->spi.len;
1499 break;
1500 case PROTO_IKE:
1501 spi64 = proposal->get_spi(proposal);
1502 if (spi64)
1503 { /* IKE only uses SPIS when rekeying, but on initial setup */
1504 this->spi = chunk_clone(chunk_from_thing(spi64));
1505 this->spi_size = this->spi.len;
1506 }
1507 break;
1508 default:
1509 break;
1510 }
1511 this->proposal_number = proposal->get_number(proposal);
1512 this->protocol_id = proposal->get_protocol(proposal);
1513 compute_length(this);
1514 }
1515
1516 /*
1517 * Described in header.
1518 */
1519 proposal_substructure_t *proposal_substructure_create_from_proposal_v2(
1520 proposal_t *proposal)
1521 {
1522 private_proposal_substructure_t *this;
1523
1524 this = (private_proposal_substructure_t*)
1525 proposal_substructure_create(PLV2_SECURITY_ASSOCIATION);
1526 set_from_proposal_v2(this, proposal);
1527 set_data(this, proposal);
1528
1529 return &this->public;
1530 }
1531
1532 /**
1533 * See header.
1534 */
1535 proposal_substructure_t *proposal_substructure_create_from_proposal_v1(
1536 proposal_t *proposal, u_int32_t lifetime, u_int64_t lifebytes,
1537 auth_method_t auth, ipsec_mode_t mode, encap_t udp)
1538 {
1539 private_proposal_substructure_t *this;
1540
1541 this = (private_proposal_substructure_t*)
1542 proposal_substructure_create(PLV1_PROPOSAL_SUBSTRUCTURE);
1543 switch (proposal->get_protocol(proposal))
1544 {
1545 case PROTO_IKE:
1546 set_from_proposal_v1_ike(this, proposal, lifetime, auth, 1);
1547 break;
1548 case PROTO_ESP:
1549 case PROTO_AH:
1550 set_from_proposal_v1(this, proposal, lifetime,
1551 lifebytes, mode, udp, 1);
1552 break;
1553 default:
1554 break;
1555 }
1556 set_data(this, proposal);
1557
1558 return &this->public;
1559 }
1560
1561 /**
1562 * See header.
1563 */
1564 proposal_substructure_t *proposal_substructure_create_from_proposals_v1(
1565 linked_list_t *proposals, u_int32_t lifetime, u_int64_t lifebytes,
1566 auth_method_t auth, ipsec_mode_t mode, encap_t udp)
1567 {
1568 private_proposal_substructure_t *this = NULL;
1569 enumerator_t *enumerator;
1570 proposal_t *proposal;
1571 int number = 0;
1572
1573 enumerator = proposals->create_enumerator(proposals);
1574 while (enumerator->enumerate(enumerator, &proposal))
1575 {
1576 if (!this)
1577 {
1578 this = (private_proposal_substructure_t*)
1579 proposal_substructure_create_from_proposal_v1(
1580 proposal, lifetime, lifebytes, auth, mode, udp);
1581 ++number;
1582 }
1583 else
1584 {
1585 switch (proposal->get_protocol(proposal))
1586 {
1587 case PROTO_IKE:
1588 set_from_proposal_v1_ike(this, proposal, lifetime,
1589 auth, ++number);
1590 break;
1591 case PROTO_ESP:
1592 case PROTO_AH:
1593 set_from_proposal_v1(this, proposal, lifetime,
1594 lifebytes, mode, udp, ++number);
1595 break;
1596 default:
1597 break;
1598 }
1599 }
1600 }
1601 enumerator->destroy(enumerator);
1602
1603 return &this->public;
1604 }
1605
1606 /**
1607 * See header.
1608 */
1609 proposal_substructure_t *proposal_substructure_create_for_ipcomp_v1(
1610 u_int32_t lifetime, u_int64_t lifebytes, u_int16_t cpi,
1611 ipsec_mode_t mode, encap_t udp, u_int8_t proposal_number)
1612 {
1613 private_proposal_substructure_t *this;
1614 transform_substructure_t *transform;
1615
1616
1617 this = (private_proposal_substructure_t*)
1618 proposal_substructure_create(PLV1_PROPOSAL_SUBSTRUCTURE);
1619
1620 /* we currently support DEFLATE only */
1621 transform = transform_substructure_create_type(PLV1_TRANSFORM_SUBSTRUCTURE,
1622 1, IKEV1_IPCOMP_DEFLATE);
1623
1624 transform->add_transform_attribute(transform,
1625 transform_attribute_create_value(PLV1_TRANSFORM_ATTRIBUTE,
1626 TATTR_PH2_ENCAP_MODE, get_ikev1_mode(mode, udp)));
1627 if (lifetime)
1628 {
1629 transform->add_transform_attribute(transform,
1630 transform_attribute_create_value(PLV1_TRANSFORM_ATTRIBUTE,
1631 TATTR_PH2_SA_LIFE_TYPE, IKEV1_LIFE_TYPE_SECONDS));
1632 transform->add_transform_attribute(transform,
1633 transform_attribute_create_value(PLV1_TRANSFORM_ATTRIBUTE,
1634 TATTR_PH2_SA_LIFE_DURATION, lifetime));
1635 }
1636 if (lifebytes)
1637 {
1638 transform->add_transform_attribute(transform,
1639 transform_attribute_create_value(PLV1_TRANSFORM_ATTRIBUTE,
1640 TATTR_PH2_SA_LIFE_TYPE, IKEV1_LIFE_TYPE_KILOBYTES));
1641 transform->add_transform_attribute(transform,
1642 transform_attribute_create_value(PLV1_TRANSFORM_ATTRIBUTE,
1643 TATTR_PH2_SA_LIFE_DURATION, lifebytes / 1000));
1644 }
1645
1646 add_transform_substructure(this, transform);
1647
1648 this->spi = chunk_clone(chunk_from_thing(cpi));
1649 this->spi_size = this->spi.len;
1650 this->protocol_id = PROTO_IPCOMP;
1651 this->proposal_number = proposal_number;
1652
1653 compute_length(this);
1654
1655 return &this->public;
1656 }