improved fix
[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",
51 "EXT_SEQ",
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;
214
215 enumerator = create_enumerator(this, type);
216 if (enumerator->enumerate(enumerator, alg, key_size))
217 {
218 found = TRUE;
219 }
220 else
221 {
222 if (key_size)
223 {
224 *key_size = 0;
225 }
226 found = FALSE;
227 }
228 enumerator->destroy(enumerator);
229 return found;
230 }
231
232 /**
233 * Implements proposal_t.has_dh_group
234 */
235 static bool has_dh_group(private_proposal_t *this, diffie_hellman_group_t group)
236 {
237 bool result = FALSE;
238
239 if (this->dh_groups->get_count(this->dh_groups))
240 {
241 algorithm_t *current;
242 enumerator_t *enumerator;
243
244 enumerator = this->dh_groups->create_enumerator(this->dh_groups);
245 while (enumerator->enumerate(enumerator, (void**)&current))
246 {
247 if (current->algorithm == group)
248 {
249 result = TRUE;
250 break;
251 }
252 }
253 enumerator->destroy(enumerator);
254 }
255 else if (group == MODP_NONE)
256 {
257 result = TRUE;
258 }
259 return result;
260 }
261
262 /**
263 * Implementation of proposal_t.strip_dh.
264 */
265 static void strip_dh(private_proposal_t *this)
266 {
267 algorithm_t *alg;
268
269 while (this->dh_groups->remove_last(this->dh_groups, (void**)&alg) == SUCCESS)
270 {
271 free(alg);
272 }
273 }
274
275 /**
276 * Returns true if the given alg is an authenticated encryption algorithm
277 */
278 static bool is_authenticated_encryption(u_int16_t alg)
279 {
280 switch(alg)
281 {
282 case ENCR_AES_CCM_ICV8:
283 case ENCR_AES_CCM_ICV12:
284 case ENCR_AES_CCM_ICV16:
285 case ENCR_AES_GCM_ICV8:
286 case ENCR_AES_GCM_ICV12:
287 case ENCR_AES_GCM_ICV16:
288 return TRUE;
289 }
290 return FALSE;
291 }
292
293 /**
294 * Find a matching alg/keysize in two linked lists
295 */
296 static bool select_algo(linked_list_t *first, linked_list_t *second, bool *add,
297 u_int16_t *alg, size_t *key_size)
298 {
299 enumerator_t *e1, *e2;
300 algorithm_t *alg1, *alg2;
301
302 /* if in both are zero algorithms specified, we HAVE a match */
303 if (first->get_count(first) == 0 && second->get_count(second) == 0)
304 {
305 *add = FALSE;
306 return TRUE;
307 }
308
309 e1 = first->create_enumerator(first);
310 e2 = second->create_enumerator(second);
311 /* compare algs, order of algs in "first" is preferred */
312 while (e1->enumerate(e1, &alg1))
313 {
314 e2->destroy(e2);
315 e2 = second->create_enumerator(second);
316 while (e2->enumerate(e2, &alg2))
317 {
318 if (alg1->algorithm == alg2->algorithm &&
319 alg1->key_size == alg2->key_size)
320 {
321 /* ok, we have an algorithm */
322 *alg = alg1->algorithm;
323 *key_size = alg1->key_size;
324 *add = TRUE;
325 e1->destroy(e1);
326 e2->destroy(e2);
327 return TRUE;
328 }
329 }
330 }
331 /* no match in all comparisons */
332 e1->destroy(e1);
333 e2->destroy(e2);
334 return FALSE;
335 }
336
337 /**
338 * Implements proposal_t.select.
339 */
340 static proposal_t *select_proposal(private_proposal_t *this, private_proposal_t *other)
341 {
342 proposal_t *selected;
343 u_int16_t algo;
344 size_t key_size;
345 bool add;
346
347 DBG2(DBG_CFG, "selecting proposal:");
348
349 /* check protocol */
350 if (this->protocol != other->protocol)
351 {
352 DBG2(DBG_CFG, " protocol mismatch, skipping");
353 return NULL;
354 }
355
356 selected = proposal_create(this->protocol);
357
358 /* select encryption algorithm */
359 if (select_algo(this->encryption_algos, other->encryption_algos,
360 &add, &algo, &key_size))
361 {
362 if (add)
363 {
364 selected->add_algorithm(selected, ENCRYPTION_ALGORITHM,
365 algo, key_size);
366 }
367 }
368 else
369 {
370 selected->destroy(selected);
371 DBG2(DBG_CFG, " no acceptable %N found",
372 transform_type_names, ENCRYPTION_ALGORITHM);
373 return NULL;
374 }
375 /* select integrity algorithm */
376 if (!is_authenticated_encryption(algo))
377 {
378 if (select_algo(this->integrity_algos, other->integrity_algos,
379 &add, &algo, &key_size))
380 {
381 if (add)
382 {
383 selected->add_algorithm(selected, INTEGRITY_ALGORITHM,
384 algo, key_size);
385 }
386 }
387 else
388 {
389 selected->destroy(selected);
390 DBG2(DBG_CFG, " no acceptable %N found",
391 transform_type_names, INTEGRITY_ALGORITHM);
392 return NULL;
393 }
394 }
395 /* select prf algorithm */
396 if (select_algo(this->prf_algos, other->prf_algos,
397 &add, &algo, &key_size))
398 {
399 if (add)
400 {
401 selected->add_algorithm(selected, PSEUDO_RANDOM_FUNCTION,
402 algo, key_size);
403 }
404 }
405 else
406 {
407 selected->destroy(selected);
408 DBG2(DBG_CFG, " no acceptable %N found",
409 transform_type_names, PSEUDO_RANDOM_FUNCTION);
410 return NULL;
411 }
412 /* select a DH-group */
413 if (select_algo(this->dh_groups, other->dh_groups, &add, &algo, &key_size))
414 {
415 if (add)
416 {
417 selected->add_algorithm(selected, DIFFIE_HELLMAN_GROUP, algo, 0);
418 }
419 }
420 else
421 {
422 selected->destroy(selected);
423 DBG2(DBG_CFG, " no acceptable %N found",
424 transform_type_names, DIFFIE_HELLMAN_GROUP);
425 return NULL;
426 }
427 /* select if we use ESNs */
428 if (select_algo(this->esns, other->esns, &add, &algo, &key_size))
429 {
430 if (add)
431 {
432 selected->add_algorithm(selected, EXTENDED_SEQUENCE_NUMBERS, algo, 0);
433 }
434 }
435 else
436 {
437 selected->destroy(selected);
438 DBG2(DBG_CFG, " no acceptable %N found",
439 transform_type_names, EXTENDED_SEQUENCE_NUMBERS);
440 return NULL;
441 }
442 DBG2(DBG_CFG, " proposal matches");
443
444 /* apply SPI from "other" */
445 selected->set_spi(selected, other->spi);
446
447 /* everything matched, return new proposal */
448 return selected;
449 }
450
451 /**
452 * Implements proposal_t.get_protocols.
453 */
454 static protocol_id_t get_protocol(private_proposal_t *this)
455 {
456 return this->protocol;
457 }
458
459 /**
460 * Implements proposal_t.set_spi.
461 */
462 static void set_spi(private_proposal_t *this, u_int64_t spi)
463 {
464 this->spi = spi;
465 }
466
467 /**
468 * Implements proposal_t.get_spi.
469 */
470 static u_int64_t get_spi(private_proposal_t *this)
471 {
472 return this->spi;
473 }
474
475 /**
476 * Clone a algorithm list
477 */
478 static void clone_algo_list(linked_list_t *list, linked_list_t *clone_list)
479 {
480 algorithm_t *algo, *clone_algo;
481 enumerator_t *enumerator;
482
483 enumerator = list->create_enumerator(list);
484 while (enumerator->enumerate(enumerator, &algo))
485 {
486 clone_algo = malloc_thing(algorithm_t);
487 memcpy(clone_algo, algo, sizeof(algorithm_t));
488 clone_list->insert_last(clone_list, (void*)clone_algo);
489 }
490 enumerator->destroy(enumerator);
491 }
492
493 /**
494 * check if an algorithm list equals
495 */
496 static bool algo_list_equals(linked_list_t *l1, linked_list_t *l2)
497 {
498 enumerator_t *e1, *e2;
499 algorithm_t *alg1, *alg2;
500 bool equals = TRUE;
501
502 if (l1->get_count(l1) != l2->get_count(l2))
503 {
504 return FALSE;
505 }
506
507 e1 = l1->create_enumerator(l1);
508 e2 = l2->create_enumerator(l2);
509 while (e1->enumerate(e1, &alg1) && e2->enumerate(e2, &alg2))
510 {
511 if (alg1->algorithm != alg2->algorithm ||
512 alg1->key_size != alg2->key_size)
513 {
514 equals = FALSE;
515 break;
516 }
517 }
518 e1->destroy(e1);
519 e2->destroy(e2);
520 return equals;
521 }
522
523 /**
524 * Implementation of proposal_t.equals.
525 */
526 static bool equals(private_proposal_t *this, private_proposal_t *other)
527 {
528 if (this == other)
529 {
530 return TRUE;
531 }
532 if (this->public.equals != other->public.equals)
533 {
534 return FALSE;
535 }
536 return (
537 algo_list_equals(this->encryption_algos, other->encryption_algos) &&
538 algo_list_equals(this->integrity_algos, other->integrity_algos) &&
539 algo_list_equals(this->prf_algos, other->prf_algos) &&
540 algo_list_equals(this->dh_groups, other->dh_groups) &&
541 algo_list_equals(this->esns, other->esns));
542 }
543
544 /**
545 * Implements proposal_t.clone
546 */
547 static proposal_t *clone_(private_proposal_t *this)
548 {
549 private_proposal_t *clone = (private_proposal_t*)proposal_create(this->protocol);
550
551 clone_algo_list(this->encryption_algos, clone->encryption_algos);
552 clone_algo_list(this->integrity_algos, clone->integrity_algos);
553 clone_algo_list(this->prf_algos, clone->prf_algos);
554 clone_algo_list(this->dh_groups, clone->dh_groups);
555 clone_algo_list(this->esns, clone->esns);
556
557 clone->spi = this->spi;
558
559 return &clone->public;
560 }
561
562 /**
563 * Checks the proposal read from a string.
564 */
565 static void check_proposal(private_proposal_t *this)
566 {
567 enumerator_t *e;
568 algorithm_t *alg;
569 bool all_aead = TRUE;
570
571 e = this->encryption_algos->create_enumerator(this->encryption_algos);
572 while (e->enumerate(e, &alg))
573 {
574 if (!is_authenticated_encryption(alg->algorithm))
575 {
576 all_aead = FALSE;
577 break;
578 }
579 }
580 e->destroy(e);
581
582 if (all_aead)
583 {
584 /* if all encryption algorithms in the proposal are authenticated encryption
585 * algorithms we MUST NOT propose any integrity algorithms */
586 while (this->integrity_algos->remove_last(this->integrity_algos,
587 (void**)&alg) == SUCCESS)
588 {
589 free(alg);
590 }
591 }
592 }
593
594 /**
595 * add a algorithm identified by a string to the proposal.
596 * TODO: we could use gperf here.
597 */
598 static status_t add_string_algo(private_proposal_t *this, chunk_t alg)
599 {
600 if (strncmp(alg.ptr, "null", alg.len) == 0)
601 {
602 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_NULL, 0);
603 }
604 else if (strncmp(alg.ptr, "aes128", alg.len) == 0)
605 {
606 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 128);
607 }
608 else if (strncmp(alg.ptr, "aes192", alg.len) == 0)
609 {
610 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 192);
611 }
612 else if (strncmp(alg.ptr, "aes256", alg.len) == 0)
613 {
614 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 256);
615 }
616 else if (strstr(alg.ptr, "ccm"))
617 {
618 u_int16_t key_size, icv_size;
619
620 if (sscanf(alg.ptr, "aes%huccm%hu", &key_size, &icv_size) == 2)
621 {
622 if (key_size == 128 || key_size == 192 || key_size == 256)
623 {
624 switch (icv_size)
625 {
626 case 8: /* octets */
627 case 64: /* bits */
628 add_algorithm(this, ENCRYPTION_ALGORITHM,
629 ENCR_AES_CCM_ICV8, key_size);
630 break;
631 case 12: /* octets */
632 case 96: /* bits */
633 add_algorithm(this, ENCRYPTION_ALGORITHM,
634 ENCR_AES_CCM_ICV12, key_size);
635 break;
636 case 16: /* octets */
637 case 128: /* bits */
638 add_algorithm(this, ENCRYPTION_ALGORITHM,
639 ENCR_AES_CCM_ICV16, key_size);
640 break;
641 default:
642 /* invalid ICV size */
643 break;
644 }
645 }
646 }
647 }
648 else if (strstr(alg.ptr, "gcm"))
649 {
650 u_int16_t key_size, icv_size;
651
652 if (sscanf(alg.ptr, "aes%hugcm%hu", &key_size, &icv_size) == 2)
653 {
654 if (key_size == 128 || key_size == 192 || key_size == 256)
655 {
656 switch (icv_size)
657 {
658 case 8: /* octets */
659 case 64: /* bits */
660 add_algorithm(this, ENCRYPTION_ALGORITHM,
661 ENCR_AES_GCM_ICV8, key_size);
662 break;
663 case 12: /* octets */
664 case 96: /* bits */
665 add_algorithm(this, ENCRYPTION_ALGORITHM,
666 ENCR_AES_GCM_ICV12, key_size);
667 break;
668 case 16: /* octets */
669 case 128: /* bits */
670 add_algorithm(this, ENCRYPTION_ALGORITHM,
671 ENCR_AES_GCM_ICV16, key_size);
672 break;
673 default:
674 /* invalid ICV size */
675 break;
676 }
677 }
678 }
679 }
680 else if (strncmp(alg.ptr, "3des", alg.len) == 0)
681 {
682 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_3DES, 0);
683 }
684 /* blowfish only uses some predefined key sizes yet */
685 else if (strncmp(alg.ptr, "blowfish128", alg.len) == 0)
686 {
687 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_BLOWFISH, 128);
688 }
689 else if (strncmp(alg.ptr, "blowfish192", alg.len) == 0)
690 {
691 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_BLOWFISH, 192);
692 }
693 else if (strncmp(alg.ptr, "blowfish256", alg.len) == 0)
694 {
695 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_BLOWFISH, 256);
696 }
697 else if (strncmp(alg.ptr, "sha", alg.len) == 0 ||
698 strncmp(alg.ptr, "sha1", alg.len) == 0)
699 {
700 /* sha means we use SHA for both, PRF and AUTH */
701 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA1_96, 0);
702 if (this->protocol == PROTO_IKE)
703 {
704 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_SHA1, 0);
705 }
706 }
707 else if (strncmp(alg.ptr, "sha256", alg.len) == 0 ||
708 strncmp(alg.ptr, "sha2_256", alg.len) == 0)
709 {
710 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_256_128, 0);
711 if (this->protocol == PROTO_IKE)
712 {
713 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_SHA2_256, 0);
714 }
715 }
716 else if (strncmp(alg.ptr, "sha384", alg.len) == 0 ||
717 strncmp(alg.ptr, "sha2_384", alg.len) == 0)
718 {
719 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_384_192, 0);
720 if (this->protocol == PROTO_IKE)
721 {
722 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_SHA2_384, 0);
723 }
724 }
725 else if (strncmp(alg.ptr, "sha512", alg.len) == 0 ||
726 strncmp(alg.ptr, "sha2_512", alg.len) == 0)
727 {
728 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_512_256, 0);
729 if (this->protocol == PROTO_IKE)
730 {
731 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_SHA2_512, 0);
732 }
733 }
734 else if (strncmp(alg.ptr, "md5", alg.len) == 0)
735 {
736 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_MD5_96, 0);
737 if (this->protocol == PROTO_IKE)
738 {
739 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_MD5, 0);
740 }
741 }
742 else if (strncmp(alg.ptr, "aesxcbc", alg.len) == 0)
743 {
744 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_AES_XCBC_96, 0);
745 if (this->protocol == PROTO_IKE)
746 {
747 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_AES128_XCBC, 0);
748 }
749 }
750 else if (strncmp(alg.ptr, "modp768", alg.len) == 0)
751 {
752 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_768_BIT, 0);
753 }
754 else if (strncmp(alg.ptr, "modp1024", alg.len) == 0)
755 {
756 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_1024_BIT, 0);
757 }
758 else if (strncmp(alg.ptr, "modp1536", alg.len) == 0)
759 {
760 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_1536_BIT, 0);
761 }
762 else if (strncmp(alg.ptr, "modp2048", alg.len) == 0)
763 {
764 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_2048_BIT, 0);
765 }
766 else if (strncmp(alg.ptr, "modp3072", alg.len) == 0)
767 {
768 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_3072_BIT, 0);
769 }
770 else if (strncmp(alg.ptr, "modp4096", alg.len) == 0)
771 {
772 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_4096_BIT, 0);
773 }
774 else if (strncmp(alg.ptr, "modp6144", alg.len) == 0)
775 {
776 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_6144_BIT, 0);
777 }
778 else if (strncmp(alg.ptr, "modp8192", alg.len) == 0)
779 {
780 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_8192_BIT, 0);
781 }
782 else if (strncmp(alg.ptr, "ecp192", alg.len) == 0)
783 {
784 add_algorithm(this, DIFFIE_HELLMAN_GROUP, ECP_192_BIT, 0);
785 }
786 else if (strncmp(alg.ptr, "ecp224", alg.len) == 0)
787 {
788 add_algorithm(this, DIFFIE_HELLMAN_GROUP, ECP_224_BIT, 0);
789 }
790 else if (strncmp(alg.ptr, "ecp256", alg.len) == 0)
791 {
792 add_algorithm(this, DIFFIE_HELLMAN_GROUP, ECP_256_BIT, 0);
793 }
794 else if (strncmp(alg.ptr, "ecp384", alg.len) == 0)
795 {
796 add_algorithm(this, DIFFIE_HELLMAN_GROUP, ECP_384_BIT, 0);
797 }
798 else if (strncmp(alg.ptr, "ecp521", alg.len) == 0)
799 {
800 add_algorithm(this, DIFFIE_HELLMAN_GROUP, ECP_521_BIT, 0);
801 }
802 else
803 {
804 return FAILED;
805 }
806 return SUCCESS;
807 }
808
809 /**
810 * print all algorithms of a kind to stream
811 */
812 static int print_alg(private_proposal_t *this, FILE *stream, u_int kind,
813 void *names, bool *first)
814 {
815 enumerator_t *enumerator;
816 size_t written = 0;
817 u_int16_t alg, size;
818
819 enumerator = create_enumerator(this, kind);
820 while (enumerator->enumerate(enumerator, &alg, &size))
821 {
822 if (*first)
823 {
824 written += fprintf(stream, "%N", names, alg);
825 *first = FALSE;
826 }
827 else
828 {
829 written += fprintf(stream, "/%N", names, alg);
830 }
831 if (size)
832 {
833 written += fprintf(stream, "-%d", size);
834 }
835 }
836 enumerator->destroy(enumerator);
837 return written;
838 }
839
840 /**
841 * output handler in printf()
842 */
843 static int print(FILE *stream, const struct printf_info *info,
844 const void *const *args)
845 {
846 private_proposal_t *this = *((private_proposal_t**)(args[0]));
847 linked_list_t *list = *((linked_list_t**)(args[0]));
848 enumerator_t *enumerator;
849 size_t written = 0;
850 bool first = TRUE;
851
852 if (this == NULL)
853 {
854 return fprintf(stream, "(null)");
855 }
856
857 if (info->alt)
858 {
859 enumerator = list->create_enumerator(list);
860 while (enumerator->enumerate(enumerator, &this))
861 { /* call recursivly */
862 if (first)
863 {
864 written += fprintf(stream, "%P", this);
865 first = FALSE;
866 }
867 else
868 {
869 written += fprintf(stream, ", %P", this);
870 }
871 }
872 enumerator->destroy(enumerator);
873 return written;
874 }
875
876 written = fprintf(stream, "%N:", protocol_id_names, this->protocol);
877 written += print_alg(this, stream, ENCRYPTION_ALGORITHM,
878 encryption_algorithm_names, &first);
879 written += print_alg(this, stream, INTEGRITY_ALGORITHM,
880 integrity_algorithm_names, &first);
881 written += print_alg(this, stream, PSEUDO_RANDOM_FUNCTION,
882 pseudo_random_function_names, &first);
883 written += print_alg(this, stream, DIFFIE_HELLMAN_GROUP,
884 diffie_hellman_group_names, &first);
885 written += print_alg(this, stream, EXTENDED_SEQUENCE_NUMBERS,
886 extended_sequence_numbers_names, &first);
887 return written;
888 }
889
890 /**
891 * arginfo handler for printf() proposal
892 */
893 static int arginfo(const struct printf_info *info, size_t n, int *argtypes)
894 {
895 if (n > 0)
896 {
897 argtypes[0] = PA_POINTER;
898 }
899 return 1;
900 }
901
902 /**
903 * return printf hook functions for a proposal
904 */
905 printf_hook_functions_t proposal_get_printf_hooks()
906 {
907 printf_hook_functions_t hooks = {print, arginfo};
908
909 return hooks;
910 }
911
912 /**
913 * Implements proposal_t.destroy.
914 */
915 static void destroy(private_proposal_t *this)
916 {
917 this->encryption_algos->destroy_function(this->encryption_algos, free);
918 this->integrity_algos->destroy_function(this->integrity_algos, free);
919 this->prf_algos->destroy_function(this->prf_algos, free);
920 this->dh_groups->destroy_function(this->dh_groups, free);
921 this->esns->destroy_function(this->esns, free);
922 free(this);
923 }
924
925 /*
926 * Describtion in header-file
927 */
928 proposal_t *proposal_create(protocol_id_t protocol)
929 {
930 private_proposal_t *this = malloc_thing(private_proposal_t);
931
932 this->public.add_algorithm = (void (*)(proposal_t*,transform_type_t,u_int16_t,u_int16_t))add_algorithm;
933 this->public.create_enumerator = (enumerator_t* (*)(proposal_t*,transform_type_t))create_enumerator;
934 this->public.get_algorithm = (bool (*)(proposal_t*,transform_type_t,u_int16_t*,u_int16_t*))get_algorithm;
935 this->public.has_dh_group = (bool (*)(proposal_t*,diffie_hellman_group_t))has_dh_group;
936 this->public.strip_dh = (void(*)(proposal_t*))strip_dh;
937 this->public.select = (proposal_t* (*)(proposal_t*,proposal_t*))select_proposal;
938 this->public.get_protocol = (protocol_id_t(*)(proposal_t*))get_protocol;
939 this->public.set_spi = (void(*)(proposal_t*,u_int64_t))set_spi;
940 this->public.get_spi = (u_int64_t(*)(proposal_t*))get_spi;
941 this->public.equals = (bool(*)(proposal_t*, proposal_t *other))equals;
942 this->public.clone = (proposal_t*(*)(proposal_t*))clone_;
943 this->public.destroy = (void(*)(proposal_t*))destroy;
944
945 this->spi = 0;
946 this->protocol = protocol;
947
948 this->encryption_algos = linked_list_create();
949 this->integrity_algos = linked_list_create();
950 this->prf_algos = linked_list_create();
951 this->dh_groups = linked_list_create();
952 this->esns = linked_list_create();
953
954 return &this->public;
955 }
956
957 /**
958 * Add supported IKE algorithms to proposal
959 */
960 static void proposal_add_supported_ike(private_proposal_t *this)
961 {
962 enumerator_t *enumerator;
963 encryption_algorithm_t encryption;
964 integrity_algorithm_t integrity;
965 pseudo_random_function_t prf;
966 diffie_hellman_group_t group;
967
968 enumerator = lib->crypto->create_crypter_enumerator(lib->crypto);
969 while (enumerator->enumerate(enumerator, &encryption))
970 {
971 switch (encryption)
972 {
973 case ENCR_AES_CBC:
974 /* we assume that we support all AES sizes */
975 add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 128);
976 add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 192);
977 add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 256);
978 break;
979 case ENCR_3DES:
980 case ENCR_AES_CTR:
981 case ENCR_AES_CCM_ICV8:
982 case ENCR_AES_CCM_ICV12:
983 case ENCR_AES_CCM_ICV16:
984 case ENCR_AES_GCM_ICV8:
985 case ENCR_AES_GCM_ICV12:
986 case ENCR_AES_GCM_ICV16:
987 add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 0);
988 break;
989 case ENCR_DES:
990 /* no, thanks */
991 break;
992 default:
993 break;
994 }
995 }
996 enumerator->destroy(enumerator);
997
998 enumerator = lib->crypto->create_signer_enumerator(lib->crypto);
999 while (enumerator->enumerate(enumerator, &integrity))
1000 {
1001 switch (integrity)
1002 {
1003 case AUTH_HMAC_SHA1_96:
1004 case AUTH_HMAC_SHA2_256_128:
1005 case AUTH_HMAC_SHA2_384_192:
1006 case AUTH_HMAC_SHA2_512_256:
1007 case AUTH_HMAC_MD5_96:
1008 case AUTH_AES_XCBC_96:
1009 add_algorithm(this, INTEGRITY_ALGORITHM, integrity, 0);
1010 break;
1011 default:
1012 break;
1013 }
1014 }
1015 enumerator->destroy(enumerator);
1016
1017 enumerator = lib->crypto->create_prf_enumerator(lib->crypto);
1018 while (enumerator->enumerate(enumerator, &prf))
1019 {
1020 switch (prf)
1021 {
1022 case PRF_HMAC_SHA1:
1023 case PRF_HMAC_SHA2_256:
1024 case PRF_HMAC_SHA2_384:
1025 case PRF_HMAC_SHA2_512:
1026 case PRF_HMAC_MD5:
1027 case PRF_AES128_XCBC:
1028 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, prf, 0);
1029 break;
1030 default:
1031 break;
1032 }
1033 }
1034 enumerator->destroy(enumerator);
1035
1036 enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
1037 while (enumerator->enumerate(enumerator, &group))
1038 {
1039 switch (group)
1040 {
1041 case MODP_768_BIT:
1042 /* weak */
1043 break;
1044 case MODP_1024_BIT:
1045 case MODP_1536_BIT:
1046 case MODP_2048_BIT:
1047 case MODP_4096_BIT:
1048 case MODP_8192_BIT:
1049 case ECP_256_BIT:
1050 case ECP_384_BIT:
1051 case ECP_521_BIT:
1052 case ECP_192_BIT:
1053 case ECP_224_BIT:
1054 add_algorithm(this, DIFFIE_HELLMAN_GROUP, group, 0);
1055 break;
1056 default:
1057 break;
1058 }
1059 }
1060 enumerator->destroy(enumerator);
1061 }
1062
1063 /*
1064 * Describtion in header-file
1065 */
1066 proposal_t *proposal_create_default(protocol_id_t protocol)
1067 {
1068 private_proposal_t *this = (private_proposal_t*)proposal_create(protocol);
1069
1070 switch (protocol)
1071 {
1072 case PROTO_IKE:
1073 proposal_add_supported_ike(this);
1074 break;
1075 case PROTO_ESP:
1076 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 128);
1077 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 192);
1078 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 256);
1079 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_3DES, 0);
1080 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_BLOWFISH, 256);
1081 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA1_96, 0);
1082 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_AES_XCBC_96, 0);
1083 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_MD5_96, 0);
1084 add_algorithm(this, EXTENDED_SEQUENCE_NUMBERS, NO_EXT_SEQ_NUMBERS, 0);
1085 break;
1086 case PROTO_AH:
1087 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA1_96, 0);
1088 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_AES_XCBC_96, 0);
1089 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_MD5_96, 0);
1090 add_algorithm(this, EXTENDED_SEQUENCE_NUMBERS, NO_EXT_SEQ_NUMBERS, 0);
1091 break;
1092 default:
1093 break;
1094 }
1095 return &this->public;
1096 }
1097
1098 /*
1099 * Describtion in header-file
1100 */
1101 proposal_t *proposal_create_from_string(protocol_id_t protocol, const char *algs)
1102 {
1103 private_proposal_t *this = (private_proposal_t*)proposal_create(protocol);
1104 chunk_t string = {(void*)algs, strlen(algs)};
1105 chunk_t alg;
1106 status_t status = SUCCESS;
1107
1108 eat_whitespace(&string);
1109 if (string.len < 1)
1110 {
1111 destroy(this);
1112 return NULL;
1113 }
1114
1115 /* get all tokens, separated by '-' */
1116 while (extract_token(&alg, '-', &string))
1117 {
1118 status |= add_string_algo(this, alg);
1119 }
1120 if (string.len)
1121 {
1122 status |= add_string_algo(this, string);
1123 }
1124 if (status != SUCCESS)
1125 {
1126 destroy(this);
1127 return NULL;
1128 }
1129
1130 check_proposal(this);
1131
1132 if (protocol == PROTO_AH || protocol == PROTO_ESP)
1133 {
1134 add_algorithm(this, EXTENDED_SEQUENCE_NUMBERS, NO_EXT_SEQ_NUMBERS, 0);
1135 }
1136 return &this->public;
1137 }