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