74870086028145ea580b949d10dbb90ef31353ae
[strongswan.git] / src / charon / config / proposal.c
1 /*
2 * Copyright (C) 2008 Tobias Brunner
3 * Copyright (C) 2006 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * $Id$
17 */
18
19 #include <string.h>
20
21 #include "proposal.h"
22
23 #include <daemon.h>
24 #include <utils/linked_list.h>
25 #include <utils/identification.h>
26 #include <utils/lexparser.h>
27 #include <crypto/prfs/prf.h>
28 #include <crypto/crypters/crypter.h>
29 #include <crypto/signers/signer.h>
30
31
32 ENUM(protocol_id_names, PROTO_NONE, PROTO_ESP,
33 "PROTO_NONE",
34 "IKE",
35 "AH",
36 "ESP",
37 );
38
39 ENUM_BEGIN(transform_type_names, UNDEFINED_TRANSFORM_TYPE, UNDEFINED_TRANSFORM_TYPE,
40 "UNDEFINED_TRANSFORM_TYPE");
41 ENUM_NEXT(transform_type_names, ENCRYPTION_ALGORITHM, EXTENDED_SEQUENCE_NUMBERS, UNDEFINED_TRANSFORM_TYPE,
42 "ENCRYPTION_ALGORITHM",
43 "PSEUDO_RANDOM_FUNCTION",
44 "INTEGRITY_ALGORITHM",
45 "DIFFIE_HELLMAN_GROUP",
46 "EXTENDED_SEQUENCE_NUMBERS");
47 ENUM_END(transform_type_names, EXTENDED_SEQUENCE_NUMBERS);
48
49 ENUM(extended_sequence_numbers_names, NO_EXT_SEQ_NUMBERS, EXT_SEQ_NUMBERS,
50 "NO_EXT_SEQ_NUMBERS",
51 "EXT_SEQ_NUMBERS",
52 );
53
54 typedef struct private_proposal_t private_proposal_t;
55 typedef struct algorithm_t algorithm_t;
56
57 /**
58 * Private data of an proposal_t object
59 */
60 struct private_proposal_t {
61
62 /**
63 * Public part
64 */
65 proposal_t public;
66
67 /**
68 * protocol (ESP or AH)
69 */
70 protocol_id_t protocol;
71
72 /**
73 * priority ordered list of encryption algorithms
74 */
75 linked_list_t *encryption_algos;
76
77 /**
78 * priority ordered list of integrity algorithms
79 */
80 linked_list_t *integrity_algos;
81
82 /**
83 * priority ordered list of pseudo random functions
84 */
85 linked_list_t *prf_algos;
86
87 /**
88 * priority ordered list of dh groups
89 */
90 linked_list_t *dh_groups;
91
92 /**
93 * priority ordered list of extended sequence number flags
94 */
95 linked_list_t *esns;
96
97 /**
98 * senders SPI
99 */
100 u_int64_t spi;
101 };
102
103 /**
104 * Struct used to store different kinds of algorithms.
105 */
106 struct algorithm_t {
107 /**
108 * Value from an encryption_algorithm_t/integrity_algorithm_t/...
109 */
110 u_int16_t algorithm;
111
112 /**
113 * the associated key size in bits, or zero if not needed
114 */
115 u_int16_t key_size;
116 };
117
118 /**
119 * Add algorithm/keysize to a algorithm list
120 */
121 static void add_algo(linked_list_t *list, u_int16_t algo, u_int16_t key_size)
122 {
123 algorithm_t *algo_key;
124
125 algo_key = malloc_thing(algorithm_t);
126 algo_key->algorithm = algo;
127 algo_key->key_size = key_size;
128 list->insert_last(list, (void*)algo_key);
129 }
130
131 /**
132 * Implements proposal_t.add_algorithm
133 */
134 static void add_algorithm(private_proposal_t *this, transform_type_t type,
135 u_int16_t algo, u_int16_t key_size)
136 {
137 switch (type)
138 {
139 case ENCRYPTION_ALGORITHM:
140 add_algo(this->encryption_algos, algo, key_size);
141 break;
142 case INTEGRITY_ALGORITHM:
143 add_algo(this->integrity_algos, algo, key_size);
144 break;
145 case PSEUDO_RANDOM_FUNCTION:
146 add_algo(this->prf_algos, algo, key_size);
147 break;
148 case DIFFIE_HELLMAN_GROUP:
149 add_algo(this->dh_groups, algo, 0);
150 break;
151 case EXTENDED_SEQUENCE_NUMBERS:
152 add_algo(this->esns, algo, 0);
153 break;
154 default:
155 break;
156 }
157 }
158
159 /**
160 * filter function for peer configs
161 */
162 static bool alg_filter(void *null, algorithm_t **in, u_int16_t *alg,
163 void **unused, u_int16_t *key_size)
164 {
165 algorithm_t *algo = *in;
166 *alg = algo->algorithm;
167 if (key_size)
168 {
169 *key_size = algo->key_size;
170 }
171 return TRUE;
172 }
173
174 /**
175 * Implements proposal_t.create_enumerator.
176 */
177 static enumerator_t *create_enumerator(private_proposal_t *this,
178 transform_type_t type)
179 {
180 linked_list_t *list;
181
182 switch (type)
183 {
184 case ENCRYPTION_ALGORITHM:
185 list = this->encryption_algos;
186 break;
187 case INTEGRITY_ALGORITHM:
188 list = this->integrity_algos;
189 break;
190 case PSEUDO_RANDOM_FUNCTION:
191 list = this->prf_algos;
192 break;
193 case DIFFIE_HELLMAN_GROUP:
194 list = this->dh_groups;
195 break;
196 case EXTENDED_SEQUENCE_NUMBERS:
197 list = this->esns;
198 break;
199 default:
200 return NULL;
201 }
202 return enumerator_create_filter(list->create_enumerator(list),
203 (void*)alg_filter, NULL, NULL);
204 }
205
206 /**
207 * Implements proposal_t.get_algorithm.
208 */
209 static bool get_algorithm(private_proposal_t *this, transform_type_t type,
210 u_int16_t *alg, u_int16_t *key_size)
211 {
212 enumerator_t *enumerator;
213 bool found = FALSE;
214
215 enumerator = create_enumerator(this, type);
216 if (enumerator->enumerate(enumerator, alg, key_size))
217 {
218 found = TRUE;
219 }
220 enumerator->destroy(enumerator);
221 return found;
222 }
223
224 /**
225 * Implements proposal_t.has_dh_group
226 */
227 static bool has_dh_group(private_proposal_t *this, diffie_hellman_group_t group)
228 {
229 bool result = FALSE;
230
231 if (this->dh_groups->get_count(this->dh_groups))
232 {
233 algorithm_t *current;
234 enumerator_t *enumerator;
235
236 enumerator = this->dh_groups->create_enumerator(this->dh_groups);
237 while (enumerator->enumerate(enumerator, (void**)&current))
238 {
239 if (current->algorithm == group)
240 {
241 result = TRUE;
242 break;
243 }
244 }
245 enumerator->destroy(enumerator);
246 }
247 else if (group == MODP_NONE)
248 {
249 result = TRUE;
250 }
251 return result;
252 }
253
254 /**
255 * Implementation of proposal_t.strip_dh.
256 */
257 static void strip_dh(private_proposal_t *this)
258 {
259 algorithm_t *alg;
260
261 while (this->dh_groups->remove_last(this->dh_groups, (void**)&alg) == SUCCESS)
262 {
263 free(alg);
264 }
265 }
266
267 /**
268 * Returns true if the given alg is an authenticated encryption algorithm
269 */
270 static bool is_authenticated_encryption(u_int16_t alg)
271 {
272 switch(alg)
273 {
274 case ENCR_AES_CCM_ICV8:
275 case ENCR_AES_CCM_ICV12:
276 case ENCR_AES_CCM_ICV16:
277 case ENCR_AES_GCM_ICV8:
278 case ENCR_AES_GCM_ICV12:
279 case ENCR_AES_GCM_ICV16:
280 return TRUE;
281 }
282 return FALSE;
283 }
284
285 /**
286 * Find a matching alg/keysize in two linked lists
287 */
288 static bool select_algo(linked_list_t *first, linked_list_t *second, bool *add,
289 u_int16_t *alg, size_t *key_size)
290 {
291 enumerator_t *e1, *e2;
292 algorithm_t *alg1, *alg2;
293
294 /* if in both are zero algorithms specified, we HAVE a match */
295 if (first->get_count(first) == 0 && second->get_count(second) == 0)
296 {
297 *add = FALSE;
298 return TRUE;
299 }
300
301 e1 = first->create_enumerator(first);
302 e2 = second->create_enumerator(second);
303 /* compare algs, order of algs in "first" is preferred */
304 while (e1->enumerate(e1, &alg1))
305 {
306 e2->destroy(e2);
307 e2 = second->create_enumerator(second);
308 while (e2->enumerate(e2, &alg2))
309 {
310 if (alg1->algorithm == alg2->algorithm &&
311 alg1->key_size == alg2->key_size)
312 {
313 /* ok, we have an algorithm */
314 *alg = alg1->algorithm;
315 *key_size = alg1->key_size;
316 *add = TRUE;
317 e1->destroy(e1);
318 e2->destroy(e2);
319 return TRUE;
320 }
321 }
322 }
323 /* no match in all comparisons */
324 e1->destroy(e1);
325 e2->destroy(e2);
326 return FALSE;
327 }
328
329 /**
330 * Implements proposal_t.select.
331 */
332 static proposal_t *select_proposal(private_proposal_t *this, private_proposal_t *other)
333 {
334 proposal_t *selected;
335 u_int16_t algo;
336 size_t key_size;
337 bool add;
338
339 DBG2(DBG_CFG, "selecting proposal:");
340
341 /* check protocol */
342 if (this->protocol != other->protocol)
343 {
344 DBG2(DBG_CFG, " protocol mismatch, skipping");
345 return NULL;
346 }
347
348 selected = proposal_create(this->protocol);
349
350 /* select encryption algorithm */
351 if (select_algo(this->encryption_algos, other->encryption_algos, &add, &algo, &key_size))
352 {
353 if (add)
354 {
355 selected->add_algorithm(selected, ENCRYPTION_ALGORITHM, algo, key_size);
356 }
357 }
358 else
359 {
360 selected->destroy(selected);
361 DBG2(DBG_CFG, " no acceptable ENCRYPTION_ALGORITHM found, skipping");
362 return NULL;
363 }
364 /* select integrity algorithm */
365 if (!is_authenticated_encryption(algo))
366 {
367 if (select_algo(this->integrity_algos, other->integrity_algos, &add, &algo, &key_size))
368 {
369 if (add)
370 {
371 selected->add_algorithm(selected, INTEGRITY_ALGORITHM, algo, key_size);
372 }
373 }
374 else
375 {
376 selected->destroy(selected);
377 DBG2(DBG_CFG, " no acceptable INTEGRITY_ALGORITHM found, skipping");
378 return NULL;
379 }
380 }
381 /* select prf algorithm */
382 if (select_algo(this->prf_algos, other->prf_algos, &add, &algo, &key_size))
383 {
384 if (add)
385 {
386 selected->add_algorithm(selected, PSEUDO_RANDOM_FUNCTION, algo, key_size);
387 }
388 }
389 else
390 {
391 selected->destroy(selected);
392 DBG2(DBG_CFG, " no acceptable PSEUDO_RANDOM_FUNCTION found, skipping");
393 return NULL;
394 }
395 /* select a DH-group */
396 if (select_algo(this->dh_groups, other->dh_groups, &add, &algo, &key_size))
397 {
398 if (add)
399 {
400 selected->add_algorithm(selected, DIFFIE_HELLMAN_GROUP, algo, 0);
401 }
402 }
403 else
404 {
405 selected->destroy(selected);
406 DBG2(DBG_CFG, " no acceptable DIFFIE_HELLMAN_GROUP found, skipping");
407 return NULL;
408 }
409 /* select if we use ESNs */
410 if (select_algo(this->esns, other->esns, &add, &algo, &key_size))
411 {
412 if (add)
413 {
414 selected->add_algorithm(selected, EXTENDED_SEQUENCE_NUMBERS, algo, 0);
415 }
416 }
417 else
418 {
419 selected->destroy(selected);
420 DBG2(DBG_CFG, " no acceptable EXTENDED_SEQUENCE_NUMBERS found, skipping");
421 return NULL;
422 }
423 DBG2(DBG_CFG, " proposal matches");
424
425 /* apply SPI from "other" */
426 selected->set_spi(selected, other->spi);
427
428 /* everything matched, return new proposal */
429 return selected;
430 }
431
432 /**
433 * Implements proposal_t.get_protocols.
434 */
435 static protocol_id_t get_protocol(private_proposal_t *this)
436 {
437 return this->protocol;
438 }
439
440 /**
441 * Implements proposal_t.set_spi.
442 */
443 static void set_spi(private_proposal_t *this, u_int64_t spi)
444 {
445 this->spi = spi;
446 }
447
448 /**
449 * Implements proposal_t.get_spi.
450 */
451 static u_int64_t get_spi(private_proposal_t *this)
452 {
453 return this->spi;
454 }
455
456 /**
457 * Clone a algorithm list
458 */
459 static void clone_algo_list(linked_list_t *list, linked_list_t *clone_list)
460 {
461 algorithm_t *algo, *clone_algo;
462 enumerator_t *enumerator;
463
464 enumerator = list->create_enumerator(list);
465 while (enumerator->enumerate(enumerator, &algo))
466 {
467 clone_algo = malloc_thing(algorithm_t);
468 memcpy(clone_algo, algo, sizeof(algorithm_t));
469 clone_list->insert_last(clone_list, (void*)clone_algo);
470 }
471 enumerator->destroy(enumerator);
472 }
473
474 /**
475 * check if an algorithm list equals
476 */
477 static bool algo_list_equals(linked_list_t *l1, linked_list_t *l2)
478 {
479 enumerator_t *e1, *e2;
480 algorithm_t *alg1, *alg2;
481 bool equals = TRUE;
482
483 if (l1->get_count(l1) != l2->get_count(l2))
484 {
485 return FALSE;
486 }
487
488 e1 = l1->create_enumerator(l1);
489 e2 = l2->create_enumerator(l2);
490 while (e1->enumerate(e1, &alg1) && e2->enumerate(e2, &alg2))
491 {
492 if (alg1->algorithm != alg2->algorithm ||
493 alg1->key_size != alg2->key_size)
494 {
495 equals = FALSE;
496 break;
497 }
498 }
499 e1->destroy(e1);
500 e2->destroy(e2);
501 return equals;
502 }
503
504 /**
505 * Implementation of proposal_t.equals.
506 */
507 static bool equals(private_proposal_t *this, private_proposal_t *other)
508 {
509 if (this == other)
510 {
511 return TRUE;
512 }
513 if (this->public.equals != other->public.equals)
514 {
515 return FALSE;
516 }
517 return (
518 algo_list_equals(this->encryption_algos, other->encryption_algos) &&
519 algo_list_equals(this->integrity_algos, other->integrity_algos) &&
520 algo_list_equals(this->prf_algos, other->prf_algos) &&
521 algo_list_equals(this->dh_groups, other->dh_groups) &&
522 algo_list_equals(this->esns, other->esns));
523 }
524
525 /**
526 * Implements proposal_t.clone
527 */
528 static proposal_t *clone_(private_proposal_t *this)
529 {
530 private_proposal_t *clone = (private_proposal_t*)proposal_create(this->protocol);
531
532 clone_algo_list(this->encryption_algos, clone->encryption_algos);
533 clone_algo_list(this->integrity_algos, clone->integrity_algos);
534 clone_algo_list(this->prf_algos, clone->prf_algos);
535 clone_algo_list(this->dh_groups, clone->dh_groups);
536 clone_algo_list(this->esns, clone->esns);
537
538 clone->spi = this->spi;
539
540 return &clone->public;
541 }
542
543 /**
544 * Checks the proposal read from a string.
545 */
546 static void check_proposal(private_proposal_t *this)
547 {
548 enumerator_t *e;
549 algorithm_t *alg;
550 bool all_aead = TRUE;
551
552 e = this->encryption_algos->create_enumerator(this->encryption_algos);
553 while (e->enumerate(e, &alg))
554 {
555 if (!is_authenticated_encryption(alg->algorithm))
556 {
557 all_aead = FALSE;
558 break;
559 }
560 }
561 e->destroy(e);
562
563 if (all_aead)
564 {
565 /* if all encryption algorithms in the proposal are authenticated encryption
566 * algorithms we MUST NOT propose any integrity algorithms */
567 while (this->integrity_algos->remove_last(this->integrity_algos, (void**)&alg) == SUCCESS)
568 {
569 free(alg);
570 }
571 }
572 }
573
574 /**
575 * add a algorithm identified by a string to the proposal.
576 * TODO: we could use gperf here.
577 */
578 static status_t add_string_algo(private_proposal_t *this, chunk_t alg)
579 {
580 if (strncmp(alg.ptr, "null", alg.len) == 0)
581 {
582 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_NULL, 0);
583 }
584 else if (strncmp(alg.ptr, "aes128", alg.len) == 0)
585 {
586 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 128);
587 }
588 else if (strncmp(alg.ptr, "aes192", alg.len) == 0)
589 {
590 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 192);
591 }
592 else if (strncmp(alg.ptr, "aes256", alg.len) == 0)
593 {
594 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 256);
595 }
596 else if (strstr(alg.ptr, "ccm"))
597 {
598 u_int16_t key_size, icv_size;
599
600 if (sscanf(alg.ptr, "aes%huccm%hu", &key_size, &icv_size) == 2)
601 {
602 if (key_size == 128 || key_size == 192 || key_size == 256)
603 {
604 switch (icv_size)
605 {
606 case 8: /* octets */
607 case 64: /* bits */
608 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CCM_ICV8, key_size);
609 break;
610 case 12: /* octets */
611 case 96: /* bits */
612 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CCM_ICV12, key_size);
613 break;
614 case 16: /* octets */
615 case 128: /* bits */
616 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CCM_ICV16, key_size);
617 break;
618 default:
619 /* invalid ICV size */
620 break;
621 }
622 }
623 }
624 }
625 else if (strstr(alg.ptr, "gcm"))
626 {
627 u_int16_t key_size, icv_size;
628
629 if (sscanf(alg.ptr, "aes%hugcm%hu", &key_size, &icv_size) == 2)
630 {
631 if (key_size == 128 || key_size == 192 || key_size == 256)
632 {
633 switch (icv_size)
634 {
635 case 8: /* octets */
636 case 64: /* bits */
637 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_GCM_ICV8, key_size);
638 break;
639 case 12: /* octets */
640 case 96: /* bits */
641 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_GCM_ICV12, key_size);
642 break;
643 case 16: /* octets */
644 case 128: /* bits */
645 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_GCM_ICV16, key_size);
646 break;
647 default:
648 /* invalid ICV size */
649 break;
650 }
651 }
652 }
653 }
654 else if (strncmp(alg.ptr, "3des", alg.len) == 0)
655 {
656 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_3DES, 0);
657 }
658 /* blowfish only uses some predefined key sizes yet */
659 else if (strncmp(alg.ptr, "blowfish128", alg.len) == 0)
660 {
661 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_BLOWFISH, 128);
662 }
663 else if (strncmp(alg.ptr, "blowfish192", alg.len) == 0)
664 {
665 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_BLOWFISH, 192);
666 }
667 else if (strncmp(alg.ptr, "blowfish256", alg.len) == 0)
668 {
669 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_BLOWFISH, 256);
670 }
671 else if (strncmp(alg.ptr, "sha", alg.len) == 0 ||
672 strncmp(alg.ptr, "sha1", alg.len) == 0)
673 {
674 /* sha means we use SHA for both, PRF and AUTH */
675 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA1_96, 0);
676 if (this->protocol == PROTO_IKE)
677 {
678 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_SHA1, 0);
679 }
680 }
681 else if (strncmp(alg.ptr, "sha256", alg.len) == 0 ||
682 strncmp(alg.ptr, "sha2_256", alg.len) == 0)
683 {
684 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_256_128, 0);
685 if (this->protocol == PROTO_IKE)
686 {
687 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_SHA2_256, 0);
688 }
689 }
690 else if (strncmp(alg.ptr, "sha384", alg.len) == 0 ||
691 strncmp(alg.ptr, "sha2_384", alg.len) == 0)
692 {
693 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_384_192, 0);
694 if (this->protocol == PROTO_IKE)
695 {
696 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_SHA2_384, 0);
697 }
698 }
699 else if (strncmp(alg.ptr, "sha512", alg.len) == 0 ||
700 strncmp(alg.ptr, "sha2_512", alg.len) == 0)
701 {
702 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_512_256, 0);
703 if (this->protocol == PROTO_IKE)
704 {
705 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_SHA2_512, 0);
706 }
707 }
708 else if (strncmp(alg.ptr, "md5", alg.len) == 0)
709 {
710 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_MD5_96, 0);
711 if (this->protocol == PROTO_IKE)
712 {
713 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_MD5, 0);
714 }
715 }
716 else if (strncmp(alg.ptr, "aesxcbc", alg.len) == 0)
717 {
718 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_AES_XCBC_96, 0);
719 if (this->protocol == PROTO_IKE)
720 {
721 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_AES128_XCBC, 0);
722 }
723 }
724 else if (strncmp(alg.ptr, "modp768", alg.len) == 0)
725 {
726 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_768_BIT, 0);
727 }
728 else if (strncmp(alg.ptr, "modp1024", alg.len) == 0)
729 {
730 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_1024_BIT, 0);
731 }
732 else if (strncmp(alg.ptr, "modp1536", alg.len) == 0)
733 {
734 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_1536_BIT, 0);
735 }
736 else if (strncmp(alg.ptr, "modp2048", alg.len) == 0)
737 {
738 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_2048_BIT, 0);
739 }
740 else if (strncmp(alg.ptr, "modp4096", alg.len) == 0)
741 {
742 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_4096_BIT, 0);
743 }
744 else if (strncmp(alg.ptr, "modp8192", alg.len) == 0)
745 {
746 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_8192_BIT, 0);
747 }
748 else
749 {
750 return FAILED;
751 }
752 return SUCCESS;
753 }
754
755 /**
756 * Implements proposal_t.destroy.
757 */
758 static void destroy(private_proposal_t *this)
759 {
760 this->encryption_algos->destroy_function(this->encryption_algos, free);
761 this->integrity_algos->destroy_function(this->integrity_algos, free);
762 this->prf_algos->destroy_function(this->prf_algos, free);
763 this->dh_groups->destroy_function(this->dh_groups, free);
764 this->esns->destroy_function(this->esns, free);
765 free(this);
766 }
767
768 /*
769 * Describtion in header-file
770 */
771 proposal_t *proposal_create(protocol_id_t protocol)
772 {
773 private_proposal_t *this = malloc_thing(private_proposal_t);
774
775 this->public.add_algorithm = (void (*)(proposal_t*,transform_type_t,u_int16_t,u_int16_t))add_algorithm;
776 this->public.create_enumerator = (enumerator_t* (*)(proposal_t*,transform_type_t))create_enumerator;
777 this->public.get_algorithm = (bool (*)(proposal_t*,transform_type_t,u_int16_t*,u_int16_t*))get_algorithm;
778 this->public.has_dh_group = (bool (*)(proposal_t*,diffie_hellman_group_t))has_dh_group;
779 this->public.strip_dh = (void(*)(proposal_t*))strip_dh;
780 this->public.select = (proposal_t* (*)(proposal_t*,proposal_t*))select_proposal;
781 this->public.get_protocol = (protocol_id_t(*)(proposal_t*))get_protocol;
782 this->public.set_spi = (void(*)(proposal_t*,u_int64_t))set_spi;
783 this->public.get_spi = (u_int64_t(*)(proposal_t*))get_spi;
784 this->public.equals = (bool(*)(proposal_t*, proposal_t *other))equals;
785 this->public.clone = (proposal_t*(*)(proposal_t*))clone_;
786 this->public.destroy = (void(*)(proposal_t*))destroy;
787
788 this->spi = 0;
789 this->protocol = protocol;
790
791 this->encryption_algos = linked_list_create();
792 this->integrity_algos = linked_list_create();
793 this->prf_algos = linked_list_create();
794 this->dh_groups = linked_list_create();
795 this->esns = linked_list_create();
796
797 return &this->public;
798 }
799
800 /*
801 * Describtion in header-file
802 */
803 proposal_t *proposal_create_default(protocol_id_t protocol)
804 {
805 private_proposal_t *this = (private_proposal_t*)proposal_create(protocol);
806
807 switch (protocol)
808 {
809 case PROTO_IKE:
810 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 128);
811 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 192);
812 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 256);
813 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_3DES, 0);
814 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_AES_XCBC_96, 0);
815 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_256_128, 0);
816 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA1_96, 0);
817 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_MD5_96, 0);
818 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_384_192, 0);
819 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_512_256, 0);
820 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_AES128_XCBC, 0);
821 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_SHA2_256, 0);
822 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_SHA1, 0);
823 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_MD5, 0);
824 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_SHA2_384, 0);
825 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_SHA2_512, 0);
826 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_2048_BIT, 0);
827 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_1536_BIT, 0);
828 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_1024_BIT, 0);
829 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_4096_BIT, 0);
830 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_8192_BIT, 0);
831 break;
832 case PROTO_ESP:
833 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 128);
834 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 192);
835 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 256);
836 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_3DES, 0);
837 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_BLOWFISH, 256);
838 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA1_96, 0);
839 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_AES_XCBC_96, 0);
840 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_MD5_96, 0);
841 add_algorithm(this, EXTENDED_SEQUENCE_NUMBERS, NO_EXT_SEQ_NUMBERS, 0);
842 break;
843 case PROTO_AH:
844 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA1_96, 0);
845 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_AES_XCBC_96, 0);
846 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_MD5_96, 0);
847 add_algorithm(this, EXTENDED_SEQUENCE_NUMBERS, NO_EXT_SEQ_NUMBERS, 0);
848 break;
849 default:
850 break;
851 }
852
853 return &this->public;
854 }
855
856 /*
857 * Describtion in header-file
858 */
859 proposal_t *proposal_create_from_string(protocol_id_t protocol, const char *algs)
860 {
861 private_proposal_t *this = (private_proposal_t*)proposal_create(protocol);
862 chunk_t string = {(void*)algs, strlen(algs)};
863 chunk_t alg;
864 status_t status = SUCCESS;
865
866 eat_whitespace(&string);
867 if (string.len < 1)
868 {
869 destroy(this);
870 return NULL;
871 }
872
873 /* get all tokens, separated by '-' */
874 while (extract_token(&alg, '-', &string))
875 {
876 status |= add_string_algo(this, alg);
877 }
878 if (string.len)
879 {
880 status |= add_string_algo(this, string);
881 }
882 if (status != SUCCESS)
883 {
884 destroy(this);
885 return NULL;
886 }
887
888 check_proposal(this);
889
890 if (protocol == PROTO_AH || protocol == PROTO_ESP)
891 {
892 add_algorithm(this, EXTENDED_SEQUENCE_NUMBERS, NO_EXT_SEQ_NUMBERS, 0);
893 }
894 return &this->public;
895 }