]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libcharon/encoding/payloads/proposal_substructure.c
Merge branch 'debian-testing'
[thirdparty/strongswan.git] / src / libcharon / encoding / payloads / proposal_substructure.c
1 /*
2 * Copyright (C) 2012 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, PROPOSAL_SUBSTRUCTURE or PROPOSAL_SUBSTRUCTURE_V1
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 + TRANSFORM_SUBSTRUCTURE_V1,
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 + 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_encr_transid_t;
228
229 /**
230 * IKEv1 Transform ID ESP authentication algorithm.
231 */
232 typedef enum {
233 IKEV1_ESP_AUTH_HMAC_MD5 = 1,
234 IKEV1_ESP_AUTH_HMAC_SHA = 2,
235 IKEV1_ESP_AUTH_DES_MAC = 3,
236 IKEV1_ESP_AUTH_KPDK = 4,
237 IKEV1_ESP_AUTH_HMAC_SHA2_256 = 5,
238 IKEV1_ESP_AUTH_HMAC_SHA2_384 = 6,
239 IKEV1_ESP_AUTH_HMAC_SHA2_512 = 7,
240 IKEV1_ESP_AUTH_HMAC_RIPEMD = 8,
241 IKEV1_ESP_AUTH_AES_XCBC_MAC = 9,
242 IKEV1_ESP_AUTH_SIG_RSA = 10,
243 IKEV1_ESP_AUTH_AES_128_GMAC = 11,
244 IKEV1_ESP_AUTH_AES_192_GMAC = 12,
245 IKEV1_ESP_AUTH_AES_256_GMAC = 13,
246 } ikev1_esp_auth_transid_it;
247
248 /**
249 * IKEv1 ESP Encapsulation mode.
250 */
251 typedef enum {
252 IKEV1_ENCAP_TUNNEL = 1,
253 IKEV1_ENCAP_TRANSPORT = 2,
254 IKEV1_ENCAP_UDP_TUNNEL = 3,
255 IKEV1_ENCAP_UDP_TRANSPORT = 4,
256 IKEV1_ENCAP_UDP_TUNNEL_DRAFT_00_03 = 61443,
257 IKEV1_ENCAP_UDP_TRANSPORT_DRAFT_00_03 = 61444,
258 } ikev1_esp_encap_t;
259
260 /**
261 * IKEv1 Life duration types.
262 */
263 typedef enum {
264 IKEV1_LIFE_TYPE_SECONDS = 1,
265 IKEV1_LIFE_TYPE_KILOBYTES = 2,
266 } ikev1_life_type_t;
267
268 /**
269 * IKEv1 authentication methods
270 */
271 typedef enum {
272 IKEV1_AUTH_PSK = 1,
273 IKEV1_AUTH_DSS_SIG = 2,
274 IKEV1_AUTH_RSA_SIG = 3,
275 IKEV1_AUTH_RSA_ENC = 4,
276 IKEV1_AUTH_RSA_ENC_REV = 5,
277 IKEV1_AUTH_ECDSA_256 = 9,
278 IKEV1_AUTH_ECDSA_384 = 10,
279 IKEV1_AUTH_ECDSA_521 = 11,
280 /* XAuth Modes */
281 IKEV1_AUTH_XAUTH_INIT_PSK = 65001,
282 IKEV1_AUTH_XAUTH_RESP_PSK = 65002,
283 IKEV1_AUTH_XAUTH_INIT_DSS = 65003,
284 IKEV1_AUTH_XAUTH_RESP_DSS = 65004,
285 IKEV1_AUTH_XAUTH_INIT_RSA = 65005,
286 IKEV1_AUTH_XAUTH_RESP_RSA = 65006,
287 IKEV1_AUTH_XAUTH_INIT_RSA_ENC = 65007,
288 IKEV1_AUTH_XAUTH_RESP_RSA_ENC = 65008,
289 IKEV1_AUTH_XAUTH_INIT_RSA_ENC_REV = 65009,
290 IKEV1_AUTH_XAUTH_RESP_RSA_ENC_REV = 65010,
291 /* Hybrid Modes */
292 IKEV1_AUTH_HYBRID_INIT_RSA = 64221,
293 IKEV1_AUTH_HYBRID_RESP_RSA = 64222,
294 IKEV1_AUTH_HYBRID_INIT_DSS = 64223,
295 IKEV1_AUTH_HYBRID_RESP_DSS = 64224,
296 } ikev1_auth_method_t;
297
298 /**
299 * IKEv1 IPComp transform IDs
300 */
301 typedef enum {
302 IKEV1_IPCOMP_OUI = 1,
303 IKEV1_IPCOMP_DEFLATE = 2,
304 IKEV1_IPCOMP_LZS = 3,
305 } ikev1_ipcomp_transform_t;
306
307 METHOD(payload_t, verify, status_t,
308 private_proposal_substructure_t *this)
309 {
310 status_t status = SUCCESS;
311 enumerator_t *enumerator;
312 payload_t *current;
313
314 if (this->next_payload != NO_PAYLOAD && this->next_payload != 2)
315 {
316 /* must be 0 or 2 */
317 DBG1(DBG_ENC, "inconsistent next payload");
318 return FAILED;
319 }
320 if (this->transforms_count != this->transforms->get_count(this->transforms))
321 {
322 /* must be the same! */
323 DBG1(DBG_ENC, "transform count invalid");
324 return FAILED;
325 }
326
327 switch (this->protocol_id)
328 {
329 case PROTO_IPCOMP:
330 if (this->spi.len != 2)
331 {
332 DBG1(DBG_ENC, "invalid CPI length in IPCOMP proposal");
333 return FAILED;
334 }
335 break;
336 case PROTO_AH:
337 case PROTO_ESP:
338 if (this->spi.len != 4)
339 {
340 DBG1(DBG_ENC, "invalid SPI length in %N proposal",
341 protocol_id_names, this->protocol_id);
342 return FAILED;
343 }
344 break;
345 case PROTO_IKE:
346 if (this->spi.len != 0 && this->spi.len != 8)
347 {
348 DBG1(DBG_ENC, "invalid SPI length in IKE proposal");
349 return FAILED;
350 }
351 break;
352 default:
353 break;
354 }
355 enumerator = this->transforms->create_enumerator(this->transforms);
356 while (enumerator->enumerate(enumerator, &current))
357 {
358 status = current->verify(current);
359 if (status != SUCCESS)
360 {
361 DBG1(DBG_ENC, "TRANSFORM_SUBSTRUCTURE verification failed");
362 break;
363 }
364 }
365 enumerator->destroy(enumerator);
366
367 /* proposal number is checked in SA payload */
368 return status;
369 }
370
371 METHOD(payload_t, get_encoding_rules, int,
372 private_proposal_substructure_t *this, encoding_rule_t **rules)
373 {
374 if (this->type == PROPOSAL_SUBSTRUCTURE)
375 {
376 *rules = encodings_v2;
377 return countof(encodings_v2);
378 }
379 *rules = encodings_v1;
380 return countof(encodings_v1);
381 }
382
383 METHOD(payload_t, get_header_length, int,
384 private_proposal_substructure_t *this)
385 {
386 return 8 + this->spi_size;
387 }
388
389 METHOD(payload_t, get_type, payload_type_t,
390 private_proposal_substructure_t *this)
391 {
392 return this->type;
393 }
394
395 METHOD(payload_t, get_next_type, payload_type_t,
396 private_proposal_substructure_t *this)
397 {
398 return this->next_payload;
399 }
400
401 METHOD(payload_t, set_next_type, void,
402 private_proposal_substructure_t *this, payload_type_t type)
403 {
404 }
405
406 /**
407 * (re-)compute the length of the payload.
408 */
409 static void compute_length(private_proposal_substructure_t *this)
410 {
411 enumerator_t *enumerator;
412 payload_t *transform;
413
414 this->transforms_count = 0;
415 this->proposal_length = get_header_length(this);
416 enumerator = this->transforms->create_enumerator(this->transforms);
417 while (enumerator->enumerate(enumerator, &transform))
418 {
419 this->proposal_length += transform->get_length(transform);
420 this->transforms_count++;
421 }
422 enumerator->destroy(enumerator);
423 }
424
425 METHOD(payload_t, get_length, size_t,
426 private_proposal_substructure_t *this)
427 {
428 return this->proposal_length;
429 }
430
431 /**
432 * Add a transform substructure to the proposal
433 */
434 static void add_transform_substructure(private_proposal_substructure_t *this,
435 transform_substructure_t *transform)
436 {
437 if (this->transforms->get_count(this->transforms) > 0)
438 {
439 transform_substructure_t *last;
440
441 this->transforms->get_last(this->transforms, (void **)&last);
442 last->set_is_last_transform(last, FALSE);
443 }
444 transform->set_is_last_transform(transform,TRUE);
445 this->transforms->insert_last(this->transforms, transform);
446 compute_length(this);
447 }
448
449 METHOD(proposal_substructure_t, set_is_last_proposal, void,
450 private_proposal_substructure_t *this, bool is_last)
451 {
452 this->next_payload = is_last ? 0 : PROPOSAL_TYPE_VALUE;
453 }
454
455 METHOD(proposal_substructure_t, set_proposal_number, void,
456 private_proposal_substructure_t *this,u_int8_t proposal_number)
457 {
458 this->proposal_number = proposal_number;
459 }
460
461 METHOD(proposal_substructure_t, get_proposal_number, u_int8_t,
462 private_proposal_substructure_t *this)
463 {
464 return this->proposal_number;
465 }
466
467 METHOD(proposal_substructure_t, set_protocol_id, void,
468 private_proposal_substructure_t *this,u_int8_t protocol_id)
469 {
470 this->protocol_id = protocol_id;
471 }
472
473 METHOD(proposal_substructure_t, get_protocol_id, u_int8_t,
474 private_proposal_substructure_t *this)
475 {
476 return this->protocol_id;
477 }
478
479 METHOD(proposal_substructure_t, set_spi, void,
480 private_proposal_substructure_t *this, chunk_t spi)
481 {
482 free(this->spi.ptr);
483 this->spi = chunk_clone(spi);
484 this->spi_size = spi.len;
485 compute_length(this);
486 }
487
488 METHOD(proposal_substructure_t, get_spi, chunk_t,
489 private_proposal_substructure_t *this)
490 {
491 return this->spi;
492 }
493
494 METHOD(proposal_substructure_t, get_cpi, bool,
495 private_proposal_substructure_t *this, u_int16_t *cpi)
496 {
497
498 transform_substructure_t *transform;
499 enumerator_t *enumerator;
500
501 if (this->protocol_id != PROTO_IPCOMP)
502 {
503 return FALSE;
504 }
505
506 enumerator = this->transforms->create_enumerator(this->transforms);
507 while (enumerator->enumerate(enumerator, &transform))
508 {
509 if (transform->get_transform_id(transform) == IKEV1_IPCOMP_DEFLATE)
510 {
511 if (cpi)
512 {
513 *cpi = *((u_int16_t*)this->spi.ptr);
514 }
515 enumerator->destroy(enumerator);
516 return TRUE;
517 }
518 }
519 enumerator->destroy(enumerator);
520 return FALSE;
521 }
522
523 /**
524 * Add a transform to a proposal for IKEv2
525 */
526 static void add_to_proposal_v2(proposal_t *proposal,
527 transform_substructure_t *transform)
528 {
529 transform_attribute_t *tattr;
530 enumerator_t *enumerator;
531 u_int16_t key_length = 0;
532
533 enumerator = transform->create_attribute_enumerator(transform);
534 while (enumerator->enumerate(enumerator, &tattr))
535 {
536 if (tattr->get_attribute_type(tattr) == TATTR_IKEV2_KEY_LENGTH)
537 {
538 key_length = tattr->get_value(tattr);
539 break;
540 }
541 }
542 enumerator->destroy(enumerator);
543
544 proposal->add_algorithm(proposal,
545 transform->get_transform_type_or_number(transform),
546 transform->get_transform_id(transform), key_length);
547 }
548
549 /**
550 * Map IKEv1 to IKEv2 algorithms
551 */
552 typedef struct {
553 u_int16_t ikev1;
554 u_int16_t ikev2;
555 } algo_map_t;
556
557 /**
558 * Encryption algorithm mapping
559 */
560 static algo_map_t map_encr[] = {
561 { IKEV1_ENCR_DES_CBC, ENCR_DES },
562 { IKEV1_ENCR_IDEA_CBC, ENCR_IDEA },
563 { IKEV1_ENCR_BLOWFISH_CBC, ENCR_BLOWFISH },
564 { IKEV1_ENCR_3DES_CBC, ENCR_3DES },
565 { IKEV1_ENCR_CAST_CBC, ENCR_CAST },
566 { IKEV1_ENCR_AES_CBC, ENCR_AES_CBC },
567 { IKEV1_ENCR_CAMELLIA_CBC, ENCR_CAMELLIA_CBC },
568 { IKEV1_ENCR_SERPENT_CBC, ENCR_SERPENT_CBC },
569 { IKEV1_ENCR_TWOFISH_CBC, ENCR_TWOFISH_CBC },
570 };
571
572 /**
573 * Integrity algorithm mapping
574 */
575 static algo_map_t map_integ[] = {
576 { IKEV1_HASH_MD5, AUTH_HMAC_MD5_96 },
577 { IKEV1_HASH_SHA1, AUTH_HMAC_SHA1_96 },
578 { IKEV1_HASH_SHA2_256, AUTH_HMAC_SHA2_256_128 },
579 { IKEV1_HASH_SHA2_384, AUTH_HMAC_SHA2_384_192 },
580 { IKEV1_HASH_SHA2_512, AUTH_HMAC_SHA2_512_256 },
581 };
582
583 /**
584 * PRF algorithm mapping
585 */
586 static algo_map_t map_prf[] = {
587 { IKEV1_HASH_MD5, PRF_HMAC_MD5 },
588 { IKEV1_HASH_SHA1, PRF_HMAC_SHA1 },
589 { IKEV1_HASH_SHA2_256, PRF_HMAC_SHA2_256 },
590 { IKEV1_HASH_SHA2_384, PRF_HMAC_SHA2_384 },
591 { IKEV1_HASH_SHA2_512, PRF_HMAC_SHA2_512 },
592 };
593
594 /**
595 * ESP encryption algorithm mapping
596 */
597 static algo_map_t map_esp_encr[] = {
598 { IKEV1_ESP_ENCR_DES_IV64, ENCR_DES_IV64 },
599 { IKEV1_ESP_ENCR_DES, ENCR_DES },
600 { IKEV1_ESP_ENCR_3DES, ENCR_3DES },
601 { IKEV1_ESP_ENCR_RC5, ENCR_RC5 },
602 { IKEV1_ESP_ENCR_IDEA, ENCR_IDEA },
603 { IKEV1_ESP_ENCR_CAST, ENCR_CAST },
604 { IKEV1_ESP_ENCR_BLOWFISH, ENCR_BLOWFISH },
605 { IKEV1_ESP_ENCR_3IDEA, ENCR_3IDEA },
606 { IKEV1_ESP_ENCR_DES_IV32, ENCR_DES_IV32 },
607 { IKEV1_ESP_ENCR_NULL, ENCR_NULL },
608 { IKEV1_ESP_ENCR_AES_CBC, ENCR_AES_CBC },
609 { IKEV1_ESP_ENCR_AES_CTR, ENCR_AES_CTR },
610 { IKEV1_ESP_ENCR_AES_CCM_8, ENCR_AES_CCM_ICV8 },
611 { IKEV1_ESP_ENCR_AES_CCM_12, ENCR_AES_CCM_ICV12 },
612 { IKEV1_ESP_ENCR_AES_CCM_16, ENCR_AES_CCM_ICV16 },
613 { IKEV1_ESP_ENCR_AES_GCM_8, ENCR_AES_GCM_ICV8 },
614 { IKEV1_ESP_ENCR_AES_GCM_12, ENCR_AES_GCM_ICV12 },
615 { IKEV1_ESP_ENCR_AES_GCM_16, ENCR_AES_GCM_ICV16 },
616 { IKEV1_ESP_ENCR_CAMELLIA, ENCR_CAMELLIA_CBC },
617 { IKEV1_ESP_ENCR_NULL_AUTH_AES_GMAC, ENCR_NULL_AUTH_AES_GMAC },
618 { IKEV1_ESP_ENCR_SERPENT, ENCR_SERPENT_CBC },
619 { IKEV1_ESP_ENCR_TWOFISH, ENCR_TWOFISH_CBC },
620 };
621
622 /**
623 * ESP authentication algorithm mapping
624 */
625 static algo_map_t map_esp_auth[] = {
626 { IKEV1_ESP_AUTH_HMAC_MD5, AUTH_HMAC_MD5_96 },
627 { IKEV1_ESP_AUTH_HMAC_SHA, AUTH_HMAC_SHA1_96 },
628 { IKEV1_ESP_AUTH_DES_MAC, AUTH_DES_MAC },
629 { IKEV1_ESP_AUTH_KPDK, AUTH_KPDK_MD5 },
630 { IKEV1_ESP_AUTH_HMAC_SHA2_256, AUTH_HMAC_SHA2_256_128 },
631 { IKEV1_ESP_AUTH_HMAC_SHA2_384, AUTH_HMAC_SHA2_384_192 },
632 { IKEV1_ESP_AUTH_HMAC_SHA2_512, AUTH_HMAC_SHA2_512_256 },
633 { IKEV1_ESP_AUTH_AES_XCBC_MAC, AUTH_AES_XCBC_96 },
634 { IKEV1_ESP_AUTH_AES_128_GMAC, AUTH_AES_128_GMAC },
635 { IKEV1_ESP_AUTH_AES_192_GMAC, AUTH_AES_192_GMAC },
636 { IKEV1_ESP_AUTH_AES_256_GMAC, AUTH_AES_256_GMAC },
637 };
638
639 /**
640 * Get IKEv2 algorithm from IKEv1 identifier
641 */
642 static u_int16_t get_alg_from_ikev1(transform_type_t type, u_int16_t value)
643 {
644 algo_map_t *map;
645 u_int16_t def;
646 int i, count;
647
648 switch (type)
649 {
650 case ENCRYPTION_ALGORITHM:
651 map = map_encr;
652 count = countof(map_encr);
653 def = ENCR_UNDEFINED;
654 break;
655 case INTEGRITY_ALGORITHM:
656 map = map_integ;
657 count = countof(map_integ);
658 def = AUTH_UNDEFINED;
659 break;
660 case PSEUDO_RANDOM_FUNCTION:
661 map = map_prf;
662 count = countof(map_prf);
663 def = PRF_UNDEFINED;
664 break;
665 default:
666 return 0;
667 }
668 for (i = 0; i < count; i++)
669 {
670 if (map[i].ikev1 == value)
671 {
672 return map[i].ikev2;
673 }
674 }
675 return def;
676 }
677
678 /**
679 * Get IKEv1 algorithm from IKEv2 identifier
680 */
681 static u_int16_t get_ikev1_from_alg(transform_type_t type, u_int16_t value)
682 {
683 algo_map_t *map;
684 int i, count;
685
686 switch (type)
687 {
688 case ENCRYPTION_ALGORITHM:
689 map = map_encr;
690 count = countof(map_encr);
691 break;
692 case INTEGRITY_ALGORITHM:
693 map = map_integ;
694 count = countof(map_integ);
695 break;
696 case PSEUDO_RANDOM_FUNCTION:
697 map = map_prf;
698 count = countof(map_prf);
699 break;
700 default:
701 return 0;
702 }
703 for (i = 0; i < count; i++)
704 {
705 if (map[i].ikev2 == value)
706 {
707 return map[i].ikev1;
708 }
709 }
710 return 0;
711 }
712
713 /**
714 * Get IKEv2 algorithm from IKEv1 ESP transaction ID
715 */
716 static u_int16_t get_alg_from_ikev1_transid(transform_type_t type, u_int16_t value)
717 {
718 algo_map_t *map;
719 u_int16_t def;
720 int i, count;
721
722 switch (type)
723 {
724 case ENCRYPTION_ALGORITHM:
725 map = map_esp_encr;
726 count = countof(map_esp_encr);
727 def = ENCR_UNDEFINED;
728 break;
729 case INTEGRITY_ALGORITHM:
730 map = map_esp_auth;
731 count = countof(map_esp_auth);
732 def = AUTH_UNDEFINED;
733 break;
734 default:
735 return 0;
736 }
737 for (i = 0; i < count; i++)
738 {
739 if (map[i].ikev1 == value)
740 {
741 return map[i].ikev2;
742 }
743 }
744 return def;
745 }
746
747 /**
748 * Get IKEv1 ESP transaction ID from IKEv2 identifier
749 */
750 static u_int16_t get_ikev1_transid_from_alg(transform_type_t type, u_int16_t value)
751 {
752 algo_map_t *map;
753 int i, count;
754
755 switch (type)
756 {
757 case ENCRYPTION_ALGORITHM:
758 map = map_esp_encr;
759 count = countof(map_esp_encr);
760 break;
761 case INTEGRITY_ALGORITHM:
762 map = map_esp_auth;
763 count = countof(map_esp_auth);
764 break;
765 default:
766 return 0;
767 }
768 for (i = 0; i < count; i++)
769 {
770 if (map[i].ikev2 == value)
771 {
772 return map[i].ikev1;
773 }
774 }
775 return 0;
776 }
777 /**
778 * Get IKEv1 authentication attribute from auth_method_t
779 */
780 static u_int16_t get_ikev1_auth(auth_method_t method)
781 {
782 switch (method)
783 {
784 case AUTH_RSA:
785 return IKEV1_AUTH_RSA_SIG;
786 case AUTH_DSS:
787 return IKEV1_AUTH_DSS_SIG;
788 case AUTH_XAUTH_INIT_PSK:
789 return IKEV1_AUTH_XAUTH_INIT_PSK;
790 case AUTH_XAUTH_RESP_PSK:
791 return IKEV1_AUTH_XAUTH_RESP_PSK;
792 case AUTH_XAUTH_INIT_RSA:
793 return IKEV1_AUTH_XAUTH_INIT_RSA;
794 case AUTH_XAUTH_RESP_RSA:
795 return IKEV1_AUTH_XAUTH_RESP_RSA;
796 case AUTH_HYBRID_INIT_RSA:
797 return IKEV1_AUTH_HYBRID_INIT_RSA;
798 case AUTH_HYBRID_RESP_RSA:
799 return IKEV1_AUTH_HYBRID_RESP_RSA;
800 case AUTH_ECDSA_256:
801 return IKEV1_AUTH_ECDSA_256;
802 case AUTH_ECDSA_384:
803 return IKEV1_AUTH_ECDSA_384;
804 case AUTH_ECDSA_521:
805 return IKEV1_AUTH_ECDSA_521;
806 case AUTH_PSK:
807 default:
808 return IKEV1_AUTH_PSK;
809 }
810 }
811
812 /**
813 * Get IKEv1 encapsulation mode
814 */
815 static u_int16_t get_ikev1_mode(ipsec_mode_t mode, encap_t udp)
816 {
817 switch (mode)
818 {
819 case MODE_TUNNEL:
820 switch (udp)
821 {
822 case ENCAP_UDP:
823 return IKEV1_ENCAP_UDP_TUNNEL;
824 case ENCAP_UDP_DRAFT_00_03:
825 return IKEV1_ENCAP_UDP_TUNNEL_DRAFT_00_03;
826 default:
827 return IKEV1_ENCAP_TUNNEL;
828 }
829 case MODE_TRANSPORT:
830 switch (udp)
831 {
832 case ENCAP_UDP:
833 return IKEV1_ENCAP_UDP_TRANSPORT;
834 case ENCAP_UDP_DRAFT_00_03:
835 return IKEV1_ENCAP_UDP_TRANSPORT_DRAFT_00_03;
836 default:
837 return IKEV1_ENCAP_TRANSPORT;
838 }
839 default:
840 return IKEV1_ENCAP_TUNNEL;
841 }
842 }
843
844 /**
845 * Add an IKE transform to a proposal for IKEv1
846 */
847 static void add_to_proposal_v1_ike(proposal_t *proposal,
848 transform_substructure_t *transform)
849 {
850 transform_attribute_type_t type;
851 transform_attribute_t *tattr;
852 enumerator_t *enumerator;
853 u_int16_t value, key_length = 0;
854 u_int16_t encr = ENCR_UNDEFINED;
855
856 enumerator = transform->create_attribute_enumerator(transform);
857 while (enumerator->enumerate(enumerator, &tattr))
858 {
859 type = tattr->get_attribute_type(tattr);
860 value = tattr->get_value(tattr);
861 switch (type)
862 {
863 case TATTR_PH1_ENCRYPTION_ALGORITHM:
864 encr = get_alg_from_ikev1(ENCRYPTION_ALGORITHM, value);
865 break;
866 case TATTR_PH1_KEY_LENGTH:
867 key_length = value;
868 break;
869 case TATTR_PH1_HASH_ALGORITHM:
870 proposal->add_algorithm(proposal, INTEGRITY_ALGORITHM,
871 get_alg_from_ikev1(INTEGRITY_ALGORITHM, value), 0);
872 proposal->add_algorithm(proposal, PSEUDO_RANDOM_FUNCTION,
873 get_alg_from_ikev1(PSEUDO_RANDOM_FUNCTION, value), 0);
874 break;
875 case TATTR_PH1_GROUP:
876 proposal->add_algorithm(proposal, DIFFIE_HELLMAN_GROUP,
877 value, 0);
878 break;
879 default:
880 break;
881 }
882 }
883 enumerator->destroy(enumerator);
884
885 if (encr != ENCR_UNDEFINED)
886 {
887 proposal->add_algorithm(proposal, ENCRYPTION_ALGORITHM, encr, key_length);
888 }
889 }
890
891 /**
892 * Add an ESP transform to a proposal for IKEv1
893 */
894 static void add_to_proposal_v1_esp(proposal_t *proposal,
895 transform_substructure_t *transform)
896 {
897 transform_attribute_type_t type;
898 transform_attribute_t *tattr;
899 enumerator_t *enumerator;
900 u_int16_t encr, value, key_length = 0;
901
902 enumerator = transform->create_attribute_enumerator(transform);
903 while (enumerator->enumerate(enumerator, &tattr))
904 {
905 type = tattr->get_attribute_type(tattr);
906 value = tattr->get_value(tattr);
907 switch (type)
908 {
909 case TATTR_PH2_KEY_LENGTH:
910 key_length = value;
911 break;
912 case TATTR_PH2_AUTH_ALGORITHM:
913 proposal->add_algorithm(proposal, INTEGRITY_ALGORITHM,
914 get_alg_from_ikev1_transid(INTEGRITY_ALGORITHM,
915 value), 0);
916 break;
917 case TATTR_PH2_GROUP:
918 proposal->add_algorithm(proposal, DIFFIE_HELLMAN_GROUP,
919 value, 0);
920 break;
921 default:
922 break;
923 }
924 }
925 enumerator->destroy(enumerator);
926
927 /* TODO-IKEv1: handle ESN attribute */
928 proposal->add_algorithm(proposal, EXTENDED_SEQUENCE_NUMBERS,
929 NO_EXT_SEQ_NUMBERS, 0);
930 encr = get_alg_from_ikev1_transid(ENCRYPTION_ALGORITHM,
931 transform->get_transform_id(transform));
932 if (encr)
933 {
934 proposal->add_algorithm(proposal, ENCRYPTION_ALGORITHM, encr,
935 key_length);
936 }
937 }
938
939 METHOD(proposal_substructure_t, get_proposals, void,
940 private_proposal_substructure_t *this, linked_list_t *proposals)
941 {
942 transform_substructure_t *transform;
943 enumerator_t *enumerator;
944 proposal_t *proposal = NULL;
945 u_int64_t spi = 0;
946
947 switch (this->spi.len)
948 {
949 case 4:
950 spi = *((u_int32_t*)this->spi.ptr);
951 break;
952 case 8:
953 spi = *((u_int64_t*)this->spi.ptr);
954 break;
955 default:
956 break;
957 }
958
959 enumerator = this->transforms->create_enumerator(this->transforms);
960 while (enumerator->enumerate(enumerator, &transform))
961 {
962 if (!proposal)
963 {
964 proposal = proposal_create(this->protocol_id, this->proposal_number);
965 proposal->set_spi(proposal, spi);
966 proposals->insert_last(proposals, proposal);
967 }
968 if (this->type == PROPOSAL_SUBSTRUCTURE)
969 {
970 add_to_proposal_v2(proposal, transform);
971 }
972 else
973 {
974 switch (this->protocol_id)
975 {
976 case PROTO_IKE:
977 add_to_proposal_v1_ike(proposal, transform);
978 break;
979 case PROTO_ESP:
980 add_to_proposal_v1_esp(proposal, transform);
981 break;
982 default:
983 break;
984 }
985 /* create a new proposal for each transform in IKEv1 */
986 proposal = NULL;
987 }
988 }
989 enumerator->destroy(enumerator);
990 }
991
992 METHOD(proposal_substructure_t, create_substructure_enumerator, enumerator_t*,
993 private_proposal_substructure_t *this)
994 {
995 return this->transforms->create_enumerator(this->transforms);
996 }
997
998 /**
999 * Get an attribute from any transform, 0 if not found
1000 */
1001 static u_int64_t get_attr(private_proposal_substructure_t *this,
1002 transform_attribute_type_t type)
1003 {
1004 enumerator_t *transforms, *attributes;
1005 transform_substructure_t *transform;
1006 transform_attribute_t *attr;
1007
1008 transforms = this->transforms->create_enumerator(this->transforms);
1009 while (transforms->enumerate(transforms, &transform))
1010 {
1011 attributes = transform->create_attribute_enumerator(transform);
1012 while (attributes->enumerate(attributes, &attr))
1013 {
1014 if (attr->get_attribute_type(attr) == type)
1015 {
1016 attributes->destroy(attributes);
1017 transforms->destroy(transforms);
1018 return attr->get_value(attr);
1019 }
1020 }
1021 attributes->destroy(attributes);
1022 }
1023 transforms->destroy(transforms);
1024 return 0;
1025 }
1026
1027 /**
1028 * Look up a lifetime duration of a given kind in all transforms
1029 */
1030 static u_int64_t get_life_duration(private_proposal_substructure_t *this,
1031 transform_attribute_type_t type_attr, ikev1_life_type_t type,
1032 transform_attribute_type_t dur_attr)
1033 {
1034 enumerator_t *transforms, *attributes;
1035 transform_substructure_t *transform;
1036 transform_attribute_t *attr;
1037
1038 transforms = this->transforms->create_enumerator(this->transforms);
1039 while (transforms->enumerate(transforms, &transform))
1040 {
1041 attributes = transform->create_attribute_enumerator(transform);
1042 while (attributes->enumerate(attributes, &attr))
1043 {
1044 if (attr->get_attribute_type(attr) == type_attr &&
1045 attr->get_value(attr) == type)
1046 { /* got type attribute, look for duration following next */
1047 while (attributes->enumerate(attributes, &attr))
1048 {
1049 if (attr->get_attribute_type(attr) == dur_attr)
1050 {
1051 attributes->destroy(attributes);
1052 transforms->destroy(transforms);
1053 return attr->get_value(attr);
1054 }
1055 }
1056 }
1057 }
1058 attributes->destroy(attributes);
1059 }
1060 transforms->destroy(transforms);
1061 return 0;
1062 }
1063
1064 METHOD(proposal_substructure_t, get_lifetime, u_int32_t,
1065 private_proposal_substructure_t *this)
1066 {
1067 u_int32_t duration;
1068
1069 switch (this->protocol_id)
1070 {
1071 case PROTO_IKE:
1072 return get_life_duration(this, TATTR_PH1_LIFE_TYPE,
1073 IKEV1_LIFE_TYPE_SECONDS, TATTR_PH1_LIFE_DURATION);
1074 case PROTO_ESP:
1075 duration = get_life_duration(this, TATTR_PH2_SA_LIFE_TYPE,
1076 IKEV1_LIFE_TYPE_SECONDS, TATTR_PH2_SA_LIFE_DURATION);
1077 if (!duration)
1078 { /* default to 8 hours, RFC 2407 */
1079 return 28800;
1080 }
1081 return duration;
1082 default:
1083 return 0;
1084 }
1085 }
1086
1087 METHOD(proposal_substructure_t, get_lifebytes, u_int64_t,
1088 private_proposal_substructure_t *this)
1089 {
1090 switch (this->protocol_id)
1091 {
1092 case PROTO_ESP:
1093 return 1000 * get_life_duration(this, TATTR_PH2_SA_LIFE_TYPE,
1094 IKEV1_LIFE_TYPE_KILOBYTES, TATTR_PH2_SA_LIFE_DURATION);
1095 case PROTO_IKE:
1096 default:
1097 return 0;
1098 }
1099 }
1100
1101 METHOD(proposal_substructure_t, get_auth_method, auth_method_t,
1102 private_proposal_substructure_t *this)
1103 {
1104 switch (get_attr(this, TATTR_PH1_AUTH_METHOD))
1105 {
1106 case IKEV1_AUTH_PSK:
1107 return AUTH_PSK;
1108 case IKEV1_AUTH_RSA_SIG:
1109 return AUTH_RSA;
1110 case IKEV1_AUTH_DSS_SIG:
1111 return AUTH_DSS;
1112 case IKEV1_AUTH_XAUTH_INIT_PSK:
1113 return AUTH_XAUTH_INIT_PSK;
1114 case IKEV1_AUTH_XAUTH_RESP_PSK:
1115 return AUTH_XAUTH_RESP_PSK;
1116 case IKEV1_AUTH_XAUTH_INIT_RSA:
1117 return AUTH_XAUTH_INIT_RSA;
1118 case IKEV1_AUTH_XAUTH_RESP_RSA:
1119 return AUTH_XAUTH_RESP_RSA;
1120 case IKEV1_AUTH_HYBRID_INIT_RSA:
1121 return AUTH_HYBRID_INIT_RSA;
1122 case IKEV1_AUTH_HYBRID_RESP_RSA:
1123 return AUTH_HYBRID_RESP_RSA;
1124 case IKEV1_AUTH_ECDSA_256:
1125 return AUTH_ECDSA_256;
1126 case IKEV1_AUTH_ECDSA_384:
1127 return AUTH_ECDSA_384;
1128 case IKEV1_AUTH_ECDSA_521:
1129 return AUTH_ECDSA_521;
1130 default:
1131 return AUTH_NONE;
1132 }
1133 }
1134
1135 METHOD(proposal_substructure_t, get_encap_mode, ipsec_mode_t,
1136 private_proposal_substructure_t *this, bool *udp)
1137 {
1138 *udp = FALSE;
1139 switch (get_attr(this, TATTR_PH2_ENCAP_MODE))
1140 {
1141 case IKEV1_ENCAP_TRANSPORT:
1142 return MODE_TRANSPORT;
1143 case IKEV1_ENCAP_TUNNEL:
1144 return MODE_TUNNEL;
1145 case IKEV1_ENCAP_UDP_TRANSPORT:
1146 case IKEV1_ENCAP_UDP_TRANSPORT_DRAFT_00_03:
1147 *udp = TRUE;
1148 return MODE_TRANSPORT;
1149 case IKEV1_ENCAP_UDP_TUNNEL:
1150 case IKEV1_ENCAP_UDP_TUNNEL_DRAFT_00_03:
1151 *udp = TRUE;
1152 return MODE_TUNNEL;
1153 default:
1154 /* default to TUNNEL, RFC 2407 says implementation specific */
1155 return MODE_TUNNEL;
1156 }
1157 }
1158
1159 METHOD2(payload_t, proposal_substructure_t, destroy, void,
1160 private_proposal_substructure_t *this)
1161 {
1162 this->transforms->destroy_offset(this->transforms,
1163 offsetof(payload_t, destroy));
1164 chunk_free(&this->spi);
1165 free(this);
1166 }
1167
1168 /*
1169 * Described in header.
1170 */
1171 proposal_substructure_t *proposal_substructure_create(payload_type_t type)
1172 {
1173 private_proposal_substructure_t *this;
1174
1175 INIT(this,
1176 .public = {
1177 .payload_interface = {
1178 .verify = _verify,
1179 .get_encoding_rules = _get_encoding_rules,
1180 .get_header_length = _get_header_length,
1181 .get_length = _get_length,
1182 .get_next_type = _get_next_type,
1183 .set_next_type = _set_next_type,
1184 .get_type = _get_type,
1185 .destroy = _destroy,
1186 },
1187 .set_proposal_number = _set_proposal_number,
1188 .get_proposal_number = _get_proposal_number,
1189 .set_protocol_id = _set_protocol_id,
1190 .get_protocol_id = _get_protocol_id,
1191 .set_is_last_proposal = _set_is_last_proposal,
1192 .get_proposals = _get_proposals,
1193 .create_substructure_enumerator = _create_substructure_enumerator,
1194 .set_spi = _set_spi,
1195 .get_spi = _get_spi,
1196 .get_cpi = _get_cpi,
1197 .get_lifetime = _get_lifetime,
1198 .get_lifebytes = _get_lifebytes,
1199 .get_auth_method = _get_auth_method,
1200 .get_encap_mode = _get_encap_mode,
1201 .destroy = _destroy,
1202 },
1203 .next_payload = NO_PAYLOAD,
1204 .transforms = linked_list_create(),
1205 .type = type,
1206 );
1207 compute_length(this);
1208
1209 return &this->public;
1210 }
1211
1212 /**
1213 * Add an IKEv1 IKE proposal to the substructure
1214 */
1215 static void set_from_proposal_v1_ike(private_proposal_substructure_t *this,
1216 proposal_t *proposal, u_int32_t lifetime,
1217 auth_method_t method, int number)
1218 {
1219 transform_substructure_t *transform;
1220 u_int16_t alg, key_size;
1221 enumerator_t *enumerator;
1222
1223 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE_V1,
1224 number, IKEV1_TRANSID_KEY_IKE);
1225
1226 enumerator = proposal->create_enumerator(proposal, ENCRYPTION_ALGORITHM);
1227 if (enumerator->enumerate(enumerator, &alg, &key_size))
1228 {
1229 alg = get_ikev1_from_alg(ENCRYPTION_ALGORITHM, alg);
1230 if (alg)
1231 {
1232 transform->add_transform_attribute(transform,
1233 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1234 TATTR_PH1_ENCRYPTION_ALGORITHM, alg));
1235 if (key_size)
1236 {
1237 transform->add_transform_attribute(transform,
1238 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1239 TATTR_PH1_KEY_LENGTH, key_size));
1240 }
1241 }
1242 }
1243 enumerator->destroy(enumerator);
1244
1245 /* encode the integrity algorithm as hash and assume use the same PRF */
1246 enumerator = proposal->create_enumerator(proposal, INTEGRITY_ALGORITHM);
1247 if (enumerator->enumerate(enumerator, &alg, &key_size))
1248 {
1249 alg = get_ikev1_from_alg(INTEGRITY_ALGORITHM, alg);
1250 if (alg)
1251 {
1252 transform->add_transform_attribute(transform,
1253 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1254 TATTR_PH1_HASH_ALGORITHM, alg));
1255 }
1256 }
1257 enumerator->destroy(enumerator);
1258
1259 enumerator = proposal->create_enumerator(proposal, DIFFIE_HELLMAN_GROUP);
1260 if (enumerator->enumerate(enumerator, &alg, &key_size))
1261 {
1262 transform->add_transform_attribute(transform,
1263 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1264 TATTR_PH1_GROUP, alg));
1265 }
1266 enumerator->destroy(enumerator);
1267
1268 transform->add_transform_attribute(transform,
1269 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1270 TATTR_PH1_AUTH_METHOD, get_ikev1_auth(method)));
1271 transform->add_transform_attribute(transform,
1272 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1273 TATTR_PH1_LIFE_TYPE, IKEV1_LIFE_TYPE_SECONDS));
1274 transform->add_transform_attribute(transform,
1275 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1276 TATTR_PH1_LIFE_DURATION, lifetime));
1277
1278 add_transform_substructure(this, transform);
1279 }
1280
1281 /**
1282 * Add an IKEv1 ESP proposal to the substructure
1283 */
1284 static void set_from_proposal_v1_esp(private_proposal_substructure_t *this,
1285 proposal_t *proposal, u_int32_t lifetime, u_int64_t lifebytes,
1286 ipsec_mode_t mode, encap_t udp, int number)
1287 {
1288 transform_substructure_t *transform = NULL;
1289 u_int16_t alg, key_size;
1290 enumerator_t *enumerator;
1291
1292 enumerator = proposal->create_enumerator(proposal, ENCRYPTION_ALGORITHM);
1293 if (enumerator->enumerate(enumerator, &alg, &key_size))
1294 {
1295 alg = get_ikev1_transid_from_alg(ENCRYPTION_ALGORITHM, alg);
1296 if (alg)
1297 {
1298 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE_V1,
1299 number, alg);
1300 if (key_size)
1301 {
1302 transform->add_transform_attribute(transform,
1303 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1304 TATTR_PH2_KEY_LENGTH, key_size));
1305 }
1306 }
1307 }
1308 enumerator->destroy(enumerator);
1309 if (!transform)
1310 {
1311 return;
1312 }
1313
1314 enumerator = proposal->create_enumerator(proposal, INTEGRITY_ALGORITHM);
1315 if (enumerator->enumerate(enumerator, &alg, &key_size))
1316 {
1317 alg = get_ikev1_transid_from_alg(INTEGRITY_ALGORITHM, alg);
1318 if (alg)
1319 {
1320 transform->add_transform_attribute(transform,
1321 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1322 TATTR_PH2_AUTH_ALGORITHM, alg));
1323 }
1324 }
1325 enumerator->destroy(enumerator);
1326
1327 enumerator = proposal->create_enumerator(proposal, DIFFIE_HELLMAN_GROUP);
1328 if (enumerator->enumerate(enumerator, &alg, &key_size))
1329 {
1330 transform->add_transform_attribute(transform,
1331 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1332 TATTR_PH2_GROUP, alg));
1333 }
1334 enumerator->destroy(enumerator);
1335
1336 transform->add_transform_attribute(transform,
1337 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1338 TATTR_PH2_ENCAP_MODE, get_ikev1_mode(mode, udp)));
1339 if (lifetime)
1340 {
1341 transform->add_transform_attribute(transform,
1342 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1343 TATTR_PH2_SA_LIFE_TYPE, IKEV1_LIFE_TYPE_SECONDS));
1344 transform->add_transform_attribute(transform,
1345 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1346 TATTR_PH2_SA_LIFE_DURATION, lifetime));
1347 }
1348 if (lifebytes)
1349 {
1350 transform->add_transform_attribute(transform,
1351 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1352 TATTR_PH2_SA_LIFE_TYPE, IKEV1_LIFE_TYPE_KILOBYTES));
1353 transform->add_transform_attribute(transform,
1354 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1355 TATTR_PH2_SA_LIFE_DURATION, lifebytes / 1000));
1356 }
1357
1358 add_transform_substructure(this, transform);
1359 }
1360
1361 /**
1362 * Add an IKEv2 proposal to the substructure
1363 */
1364 static void set_from_proposal_v2(private_proposal_substructure_t *this,
1365 proposal_t *proposal)
1366 {
1367 transform_substructure_t *transform;
1368 u_int16_t alg, key_size;
1369 enumerator_t *enumerator;
1370
1371 /* encryption algorithm is only available in ESP */
1372 enumerator = proposal->create_enumerator(proposal, ENCRYPTION_ALGORITHM);
1373 while (enumerator->enumerate(enumerator, &alg, &key_size))
1374 {
1375 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE,
1376 ENCRYPTION_ALGORITHM, alg);
1377 if (key_size)
1378 {
1379 transform->add_transform_attribute(transform,
1380 transform_attribute_create_value(TRANSFORM_ATTRIBUTE,
1381 TATTR_IKEV2_KEY_LENGTH, key_size));
1382 }
1383 add_transform_substructure(this, transform);
1384 }
1385 enumerator->destroy(enumerator);
1386
1387 /* integrity algorithms */
1388 enumerator = proposal->create_enumerator(proposal, INTEGRITY_ALGORITHM);
1389 while (enumerator->enumerate(enumerator, &alg, &key_size))
1390 {
1391 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE,
1392 INTEGRITY_ALGORITHM, alg);
1393 add_transform_substructure(this, transform);
1394 }
1395 enumerator->destroy(enumerator);
1396
1397 /* prf algorithms */
1398 enumerator = proposal->create_enumerator(proposal, PSEUDO_RANDOM_FUNCTION);
1399 while (enumerator->enumerate(enumerator, &alg, &key_size))
1400 {
1401 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE,
1402 PSEUDO_RANDOM_FUNCTION, alg);
1403 add_transform_substructure(this, transform);
1404 }
1405 enumerator->destroy(enumerator);
1406
1407 /* dh groups */
1408 enumerator = proposal->create_enumerator(proposal, DIFFIE_HELLMAN_GROUP);
1409 while (enumerator->enumerate(enumerator, &alg, NULL))
1410 {
1411 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE,
1412 DIFFIE_HELLMAN_GROUP, alg);
1413 add_transform_substructure(this, transform);
1414 }
1415 enumerator->destroy(enumerator);
1416
1417 /* extended sequence numbers */
1418 enumerator = proposal->create_enumerator(proposal, EXTENDED_SEQUENCE_NUMBERS);
1419 while (enumerator->enumerate(enumerator, &alg, NULL))
1420 {
1421 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE,
1422 EXTENDED_SEQUENCE_NUMBERS, alg);
1423 add_transform_substructure(this, transform);
1424 }
1425 enumerator->destroy(enumerator);
1426 }
1427
1428 /**
1429 * Set SPI and other data from proposal, compute length
1430 */
1431 static void set_data(private_proposal_substructure_t *this, proposal_t *proposal)
1432 {
1433 u_int64_t spi64;
1434 u_int32_t spi32;
1435
1436 /* add SPI, if necessary */
1437 switch (proposal->get_protocol(proposal))
1438 {
1439 case PROTO_AH:
1440 case PROTO_ESP:
1441 spi32 = proposal->get_spi(proposal);
1442 this->spi = chunk_clone(chunk_from_thing(spi32));
1443 this->spi_size = this->spi.len;
1444 break;
1445 case PROTO_IKE:
1446 spi64 = proposal->get_spi(proposal);
1447 if (spi64)
1448 { /* IKE only uses SPIS when rekeying, but on initial setup */
1449 this->spi = chunk_clone(chunk_from_thing(spi64));
1450 this->spi_size = this->spi.len;
1451 }
1452 break;
1453 default:
1454 break;
1455 }
1456 this->proposal_number = proposal->get_number(proposal);
1457 this->protocol_id = proposal->get_protocol(proposal);
1458 compute_length(this);
1459 }
1460
1461 /*
1462 * Described in header.
1463 */
1464 proposal_substructure_t *proposal_substructure_create_from_proposal_v2(
1465 proposal_t *proposal)
1466 {
1467 private_proposal_substructure_t *this;
1468
1469 this = (private_proposal_substructure_t*)
1470 proposal_substructure_create(SECURITY_ASSOCIATION);
1471 set_from_proposal_v2(this, proposal);
1472 set_data(this, proposal);
1473
1474 return &this->public;
1475 }
1476
1477 /**
1478 * See header.
1479 */
1480 proposal_substructure_t *proposal_substructure_create_from_proposal_v1(
1481 proposal_t *proposal, u_int32_t lifetime, u_int64_t lifebytes,
1482 auth_method_t auth, ipsec_mode_t mode, encap_t udp)
1483 {
1484 private_proposal_substructure_t *this;
1485
1486 this = (private_proposal_substructure_t*)
1487 proposal_substructure_create(PROPOSAL_SUBSTRUCTURE_V1);
1488 switch (proposal->get_protocol(proposal))
1489 {
1490 case PROTO_IKE:
1491 set_from_proposal_v1_ike(this, proposal, lifetime, auth, 1);
1492 break;
1493 case PROTO_ESP:
1494 set_from_proposal_v1_esp(this, proposal, lifetime,
1495 lifebytes, mode, udp, 1);
1496 break;
1497 default:
1498 break;
1499 }
1500 set_data(this, proposal);
1501
1502 return &this->public;
1503 }
1504
1505 /**
1506 * See header.
1507 */
1508 proposal_substructure_t *proposal_substructure_create_from_proposals_v1(
1509 linked_list_t *proposals, u_int32_t lifetime, u_int64_t lifebytes,
1510 auth_method_t auth, ipsec_mode_t mode, encap_t udp)
1511 {
1512 private_proposal_substructure_t *this = NULL;
1513 enumerator_t *enumerator;
1514 proposal_t *proposal;
1515 int number = 0;
1516
1517 enumerator = proposals->create_enumerator(proposals);
1518 while (enumerator->enumerate(enumerator, &proposal))
1519 {
1520 if (!this)
1521 {
1522 this = (private_proposal_substructure_t*)
1523 proposal_substructure_create_from_proposal_v1(
1524 proposal, lifetime, lifebytes, auth, mode, udp);
1525 ++number;
1526 }
1527 else
1528 {
1529 switch (proposal->get_protocol(proposal))
1530 {
1531 case PROTO_IKE:
1532 set_from_proposal_v1_ike(this, proposal, lifetime,
1533 auth, ++number);
1534 break;
1535 case PROTO_ESP:
1536 set_from_proposal_v1_esp(this, proposal, lifetime,
1537 lifebytes, mode, udp, ++number);
1538 break;
1539 default:
1540 break;
1541 }
1542 }
1543 }
1544 enumerator->destroy(enumerator);
1545
1546 return &this->public;
1547 }
1548
1549 /**
1550 * See header.
1551 */
1552 proposal_substructure_t *proposal_substructure_create_for_ipcomp_v1(
1553 u_int32_t lifetime, u_int64_t lifebytes, u_int16_t cpi,
1554 ipsec_mode_t mode, encap_t udp, u_int8_t proposal_number)
1555 {
1556 private_proposal_substructure_t *this;
1557 transform_substructure_t *transform;
1558
1559
1560 this = (private_proposal_substructure_t*)
1561 proposal_substructure_create(PROPOSAL_SUBSTRUCTURE_V1);
1562
1563 /* we currently support DEFLATE only */
1564 transform = transform_substructure_create_type(TRANSFORM_SUBSTRUCTURE_V1,
1565 1, IKEV1_IPCOMP_DEFLATE);
1566
1567 transform->add_transform_attribute(transform,
1568 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1569 TATTR_PH2_ENCAP_MODE, get_ikev1_mode(mode, udp)));
1570 if (lifetime)
1571 {
1572 transform->add_transform_attribute(transform,
1573 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1574 TATTR_PH2_SA_LIFE_TYPE, IKEV1_LIFE_TYPE_SECONDS));
1575 transform->add_transform_attribute(transform,
1576 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1577 TATTR_PH2_SA_LIFE_DURATION, lifetime));
1578 }
1579 if (lifebytes)
1580 {
1581 transform->add_transform_attribute(transform,
1582 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1583 TATTR_PH2_SA_LIFE_TYPE, IKEV1_LIFE_TYPE_KILOBYTES));
1584 transform->add_transform_attribute(transform,
1585 transform_attribute_create_value(TRANSFORM_ATTRIBUTE_V1,
1586 TATTR_PH2_SA_LIFE_DURATION, lifebytes / 1000));
1587 }
1588
1589 add_transform_substructure(this, transform);
1590
1591 this->spi = chunk_clone(chunk_from_thing(cpi));
1592 this->spi_size = this->spi.len;
1593 this->protocol_id = PROTO_IPCOMP;
1594 this->proposal_number = proposal_number;
1595
1596 compute_length(this);
1597
1598 return &this->public;
1599 }