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