e0da8ce41dd13b807dce8983cc457178774ef69b
[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 enumerator_t *e;
361 algorithm_t *alg;
362
363 selected->destroy(selected);
364 DBG2(DBG_CFG, " no acceptable ENCRYPTION_ALGORITHM found");
365 DBG2(DBG_CFG, " list of received ENCRYPTION_ALGORITHM proposals:");
366 e = other->encryption_algos->create_enumerator(other->encryption_algos);
367 while (e->enumerate(e, &alg))
368 {
369 DBG2(DBG_CFG, " %N-%d", encryption_algorithm_names,
370 alg->algorithm, alg->key_size);
371 }
372 e->destroy(e);
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, &add, &algo, &key_size))
379 {
380 if (add)
381 {
382 selected->add_algorithm(selected, INTEGRITY_ALGORITHM, algo, key_size);
383 }
384 }
385 else
386 {
387 enumerator_t *e;
388 algorithm_t *alg;
389
390 selected->destroy(selected);
391 DBG2(DBG_CFG, " no acceptable INTEGRITY_ALGORITHM found");
392 DBG2(DBG_CFG, " list of received INTEGRITY_ALGORITHM proposals:");
393 e = other->integrity_algos->create_enumerator(other->integrity_algos);
394 while (e->enumerate(e, &alg))
395 {
396 DBG2(DBG_CFG, " %N", integrity_algorithm_names, alg->algorithm);
397 }
398 e->destroy(e);
399 return NULL;
400 }
401 }
402 /* select prf algorithm */
403 if (select_algo(this->prf_algos, other->prf_algos, &add, &algo, &key_size))
404 {
405 if (add)
406 {
407 selected->add_algorithm(selected, PSEUDO_RANDOM_FUNCTION, algo, key_size);
408 }
409 }
410 else
411 {
412 selected->destroy(selected);
413 DBG2(DBG_CFG, " no acceptable PSEUDO_RANDOM_FUNCTION found, skipping");
414 return NULL;
415 }
416 /* select a DH-group */
417 if (select_algo(this->dh_groups, other->dh_groups, &add, &algo, &key_size))
418 {
419 if (add)
420 {
421 selected->add_algorithm(selected, DIFFIE_HELLMAN_GROUP, algo, 0);
422 }
423 }
424 else
425 {
426 selected->destroy(selected);
427 DBG2(DBG_CFG, " no acceptable DIFFIE_HELLMAN_GROUP found, skipping");
428 return NULL;
429 }
430 /* select if we use ESNs */
431 if (select_algo(this->esns, other->esns, &add, &algo, &key_size))
432 {
433 if (add)
434 {
435 selected->add_algorithm(selected, EXTENDED_SEQUENCE_NUMBERS, algo, 0);
436 }
437 }
438 else
439 {
440 selected->destroy(selected);
441 DBG2(DBG_CFG, " no acceptable EXTENDED_SEQUENCE_NUMBERS found, skipping");
442 return NULL;
443 }
444 DBG2(DBG_CFG, " proposal matches");
445
446 /* apply SPI from "other" */
447 selected->set_spi(selected, other->spi);
448
449 /* everything matched, return new proposal */
450 return selected;
451 }
452
453 /**
454 * Implements proposal_t.get_protocols.
455 */
456 static protocol_id_t get_protocol(private_proposal_t *this)
457 {
458 return this->protocol;
459 }
460
461 /**
462 * Implements proposal_t.set_spi.
463 */
464 static void set_spi(private_proposal_t *this, u_int64_t spi)
465 {
466 this->spi = spi;
467 }
468
469 /**
470 * Implements proposal_t.get_spi.
471 */
472 static u_int64_t get_spi(private_proposal_t *this)
473 {
474 return this->spi;
475 }
476
477 /**
478 * Clone a algorithm list
479 */
480 static void clone_algo_list(linked_list_t *list, linked_list_t *clone_list)
481 {
482 algorithm_t *algo, *clone_algo;
483 enumerator_t *enumerator;
484
485 enumerator = list->create_enumerator(list);
486 while (enumerator->enumerate(enumerator, &algo))
487 {
488 clone_algo = malloc_thing(algorithm_t);
489 memcpy(clone_algo, algo, sizeof(algorithm_t));
490 clone_list->insert_last(clone_list, (void*)clone_algo);
491 }
492 enumerator->destroy(enumerator);
493 }
494
495 /**
496 * check if an algorithm list equals
497 */
498 static bool algo_list_equals(linked_list_t *l1, linked_list_t *l2)
499 {
500 enumerator_t *e1, *e2;
501 algorithm_t *alg1, *alg2;
502 bool equals = TRUE;
503
504 if (l1->get_count(l1) != l2->get_count(l2))
505 {
506 return FALSE;
507 }
508
509 e1 = l1->create_enumerator(l1);
510 e2 = l2->create_enumerator(l2);
511 while (e1->enumerate(e1, &alg1) && e2->enumerate(e2, &alg2))
512 {
513 if (alg1->algorithm != alg2->algorithm ||
514 alg1->key_size != alg2->key_size)
515 {
516 equals = FALSE;
517 break;
518 }
519 }
520 e1->destroy(e1);
521 e2->destroy(e2);
522 return equals;
523 }
524
525 /**
526 * Implementation of proposal_t.equals.
527 */
528 static bool equals(private_proposal_t *this, private_proposal_t *other)
529 {
530 if (this == other)
531 {
532 return TRUE;
533 }
534 if (this->public.equals != other->public.equals)
535 {
536 return FALSE;
537 }
538 return (
539 algo_list_equals(this->encryption_algos, other->encryption_algos) &&
540 algo_list_equals(this->integrity_algos, other->integrity_algos) &&
541 algo_list_equals(this->prf_algos, other->prf_algos) &&
542 algo_list_equals(this->dh_groups, other->dh_groups) &&
543 algo_list_equals(this->esns, other->esns));
544 }
545
546 /**
547 * Implements proposal_t.clone
548 */
549 static proposal_t *clone_(private_proposal_t *this)
550 {
551 private_proposal_t *clone = (private_proposal_t*)proposal_create(this->protocol);
552
553 clone_algo_list(this->encryption_algos, clone->encryption_algos);
554 clone_algo_list(this->integrity_algos, clone->integrity_algos);
555 clone_algo_list(this->prf_algos, clone->prf_algos);
556 clone_algo_list(this->dh_groups, clone->dh_groups);
557 clone_algo_list(this->esns, clone->esns);
558
559 clone->spi = this->spi;
560
561 return &clone->public;
562 }
563
564 /**
565 * Checks the proposal read from a string.
566 */
567 static void check_proposal(private_proposal_t *this)
568 {
569 enumerator_t *e;
570 algorithm_t *alg;
571 bool all_aead = TRUE;
572
573 e = this->encryption_algos->create_enumerator(this->encryption_algos);
574 while (e->enumerate(e, &alg))
575 {
576 if (!is_authenticated_encryption(alg->algorithm))
577 {
578 all_aead = FALSE;
579 break;
580 }
581 }
582 e->destroy(e);
583
584 if (all_aead)
585 {
586 /* if all encryption algorithms in the proposal are authenticated encryption
587 * algorithms we MUST NOT propose any integrity algorithms */
588 while (this->integrity_algos->remove_last(this->integrity_algos, (void**)&alg) == SUCCESS)
589 {
590 free(alg);
591 }
592 }
593 }
594
595 /**
596 * add a algorithm identified by a string to the proposal.
597 * TODO: we could use gperf here.
598 */
599 static status_t add_string_algo(private_proposal_t *this, chunk_t alg)
600 {
601 if (strncmp(alg.ptr, "null", alg.len) == 0)
602 {
603 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_NULL, 0);
604 }
605 else if (strncmp(alg.ptr, "aes128", alg.len) == 0)
606 {
607 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 128);
608 }
609 else if (strncmp(alg.ptr, "aes192", alg.len) == 0)
610 {
611 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 192);
612 }
613 else if (strncmp(alg.ptr, "aes256", alg.len) == 0)
614 {
615 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 256);
616 }
617 else if (strstr(alg.ptr, "ccm"))
618 {
619 u_int16_t key_size, icv_size;
620
621 if (sscanf(alg.ptr, "aes%huccm%hu", &key_size, &icv_size) == 2)
622 {
623 if (key_size == 128 || key_size == 192 || key_size == 256)
624 {
625 switch (icv_size)
626 {
627 case 8: /* octets */
628 case 64: /* bits */
629 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CCM_ICV8, key_size);
630 break;
631 case 12: /* octets */
632 case 96: /* bits */
633 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CCM_ICV12, key_size);
634 break;
635 case 16: /* octets */
636 case 128: /* bits */
637 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CCM_ICV16, key_size);
638 break;
639 default:
640 /* invalid ICV size */
641 break;
642 }
643 }
644 }
645 }
646 else if (strstr(alg.ptr, "gcm"))
647 {
648 u_int16_t key_size, icv_size;
649
650 if (sscanf(alg.ptr, "aes%hugcm%hu", &key_size, &icv_size) == 2)
651 {
652 if (key_size == 128 || key_size == 192 || key_size == 256)
653 {
654 switch (icv_size)
655 {
656 case 8: /* octets */
657 case 64: /* bits */
658 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_GCM_ICV8, key_size);
659 break;
660 case 12: /* octets */
661 case 96: /* bits */
662 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_GCM_ICV12, key_size);
663 break;
664 case 16: /* octets */
665 case 128: /* bits */
666 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_GCM_ICV16, key_size);
667 break;
668 default:
669 /* invalid ICV size */
670 break;
671 }
672 }
673 }
674 }
675 else if (strncmp(alg.ptr, "3des", alg.len) == 0)
676 {
677 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_3DES, 0);
678 }
679 /* blowfish only uses some predefined key sizes yet */
680 else if (strncmp(alg.ptr, "blowfish128", alg.len) == 0)
681 {
682 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_BLOWFISH, 128);
683 }
684 else if (strncmp(alg.ptr, "blowfish192", alg.len) == 0)
685 {
686 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_BLOWFISH, 192);
687 }
688 else if (strncmp(alg.ptr, "blowfish256", alg.len) == 0)
689 {
690 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_BLOWFISH, 256);
691 }
692 else if (strncmp(alg.ptr, "sha", alg.len) == 0 ||
693 strncmp(alg.ptr, "sha1", alg.len) == 0)
694 {
695 /* sha means we use SHA for both, PRF and AUTH */
696 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA1_96, 0);
697 if (this->protocol == PROTO_IKE)
698 {
699 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_SHA1, 0);
700 }
701 }
702 else if (strncmp(alg.ptr, "sha256", alg.len) == 0 ||
703 strncmp(alg.ptr, "sha2_256", alg.len) == 0)
704 {
705 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_256_128, 0);
706 if (this->protocol == PROTO_IKE)
707 {
708 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_SHA2_256, 0);
709 }
710 }
711 else if (strncmp(alg.ptr, "sha384", alg.len) == 0 ||
712 strncmp(alg.ptr, "sha2_384", alg.len) == 0)
713 {
714 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_384_192, 0);
715 if (this->protocol == PROTO_IKE)
716 {
717 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_SHA2_384, 0);
718 }
719 }
720 else if (strncmp(alg.ptr, "sha512", alg.len) == 0 ||
721 strncmp(alg.ptr, "sha2_512", alg.len) == 0)
722 {
723 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_512_256, 0);
724 if (this->protocol == PROTO_IKE)
725 {
726 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_SHA2_512, 0);
727 }
728 }
729 else if (strncmp(alg.ptr, "md5", alg.len) == 0)
730 {
731 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_MD5_96, 0);
732 if (this->protocol == PROTO_IKE)
733 {
734 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_MD5, 0);
735 }
736 }
737 else if (strncmp(alg.ptr, "aesxcbc", alg.len) == 0)
738 {
739 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_AES_XCBC_96, 0);
740 if (this->protocol == PROTO_IKE)
741 {
742 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_AES128_XCBC, 0);
743 }
744 }
745 else if (strncmp(alg.ptr, "modp768", alg.len) == 0)
746 {
747 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_768_BIT, 0);
748 }
749 else if (strncmp(alg.ptr, "modp1024", alg.len) == 0)
750 {
751 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_1024_BIT, 0);
752 }
753 else if (strncmp(alg.ptr, "modp1536", alg.len) == 0)
754 {
755 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_1536_BIT, 0);
756 }
757 else if (strncmp(alg.ptr, "modp2048", alg.len) == 0)
758 {
759 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_2048_BIT, 0);
760 }
761 else if (strncmp(alg.ptr, "modp4096", alg.len) == 0)
762 {
763 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_4096_BIT, 0);
764 }
765 else if (strncmp(alg.ptr, "modp8192", alg.len) == 0)
766 {
767 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_8192_BIT, 0);
768 }
769 else if (strncmp(alg.ptr, "ecp192", alg.len) == 0)
770 {
771 add_algorithm(this, DIFFIE_HELLMAN_GROUP, ECP_192_BIT, 0);
772 }
773 else if (strncmp(alg.ptr, "ecp224", alg.len) == 0)
774 {
775 add_algorithm(this, DIFFIE_HELLMAN_GROUP, ECP_224_BIT, 0);
776 }
777 else if (strncmp(alg.ptr, "ecp256", alg.len) == 0)
778 {
779 add_algorithm(this, DIFFIE_HELLMAN_GROUP, ECP_256_BIT, 0);
780 }
781 else if (strncmp(alg.ptr, "ecp384", alg.len) == 0)
782 {
783 add_algorithm(this, DIFFIE_HELLMAN_GROUP, ECP_384_BIT, 0);
784 }
785 else if (strncmp(alg.ptr, "ecp521", alg.len) == 0)
786 {
787 add_algorithm(this, DIFFIE_HELLMAN_GROUP, ECP_521_BIT, 0);
788 }
789 else
790 {
791 return FAILED;
792 }
793 return SUCCESS;
794 }
795
796 /**
797 * Implements proposal_t.destroy.
798 */
799 static void destroy(private_proposal_t *this)
800 {
801 this->encryption_algos->destroy_function(this->encryption_algos, free);
802 this->integrity_algos->destroy_function(this->integrity_algos, free);
803 this->prf_algos->destroy_function(this->prf_algos, free);
804 this->dh_groups->destroy_function(this->dh_groups, free);
805 this->esns->destroy_function(this->esns, free);
806 free(this);
807 }
808
809 /*
810 * Describtion in header-file
811 */
812 proposal_t *proposal_create(protocol_id_t protocol)
813 {
814 private_proposal_t *this = malloc_thing(private_proposal_t);
815
816 this->public.add_algorithm = (void (*)(proposal_t*,transform_type_t,u_int16_t,u_int16_t))add_algorithm;
817 this->public.create_enumerator = (enumerator_t* (*)(proposal_t*,transform_type_t))create_enumerator;
818 this->public.get_algorithm = (bool (*)(proposal_t*,transform_type_t,u_int16_t*,u_int16_t*))get_algorithm;
819 this->public.has_dh_group = (bool (*)(proposal_t*,diffie_hellman_group_t))has_dh_group;
820 this->public.strip_dh = (void(*)(proposal_t*))strip_dh;
821 this->public.select = (proposal_t* (*)(proposal_t*,proposal_t*))select_proposal;
822 this->public.get_protocol = (protocol_id_t(*)(proposal_t*))get_protocol;
823 this->public.set_spi = (void(*)(proposal_t*,u_int64_t))set_spi;
824 this->public.get_spi = (u_int64_t(*)(proposal_t*))get_spi;
825 this->public.equals = (bool(*)(proposal_t*, proposal_t *other))equals;
826 this->public.clone = (proposal_t*(*)(proposal_t*))clone_;
827 this->public.destroy = (void(*)(proposal_t*))destroy;
828
829 this->spi = 0;
830 this->protocol = protocol;
831
832 this->encryption_algos = linked_list_create();
833 this->integrity_algos = linked_list_create();
834 this->prf_algos = linked_list_create();
835 this->dh_groups = linked_list_create();
836 this->esns = linked_list_create();
837
838 return &this->public;
839 }
840
841 /*
842 * Describtion in header-file
843 */
844 proposal_t *proposal_create_default(protocol_id_t protocol)
845 {
846 private_proposal_t *this = (private_proposal_t*)proposal_create(protocol);
847
848 switch (protocol)
849 {
850 case PROTO_IKE:
851 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 128);
852 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 192);
853 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 256);
854 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_3DES, 0);
855 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_AES_XCBC_96, 0);
856 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_256_128, 0);
857 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA1_96, 0);
858 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_MD5_96, 0);
859 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_384_192, 0);
860 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_512_256, 0);
861 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_AES128_XCBC, 0);
862 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_SHA2_256, 0);
863 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_SHA1, 0);
864 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_MD5, 0);
865 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_SHA2_384, 0);
866 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_SHA2_512, 0);
867 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_2048_BIT, 0);
868 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_1536_BIT, 0);
869 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_1024_BIT, 0);
870 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_4096_BIT, 0);
871 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_8192_BIT, 0);
872 break;
873 case PROTO_ESP:
874 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 128);
875 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 192);
876 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 256);
877 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_3DES, 0);
878 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_BLOWFISH, 256);
879 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA1_96, 0);
880 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_AES_XCBC_96, 0);
881 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_MD5_96, 0);
882 add_algorithm(this, EXTENDED_SEQUENCE_NUMBERS, NO_EXT_SEQ_NUMBERS, 0);
883 break;
884 case PROTO_AH:
885 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA1_96, 0);
886 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_AES_XCBC_96, 0);
887 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_MD5_96, 0);
888 add_algorithm(this, EXTENDED_SEQUENCE_NUMBERS, NO_EXT_SEQ_NUMBERS, 0);
889 break;
890 default:
891 break;
892 }
893
894 return &this->public;
895 }
896
897 /*
898 * Describtion in header-file
899 */
900 proposal_t *proposal_create_from_string(protocol_id_t protocol, const char *algs)
901 {
902 private_proposal_t *this = (private_proposal_t*)proposal_create(protocol);
903 chunk_t string = {(void*)algs, strlen(algs)};
904 chunk_t alg;
905 status_t status = SUCCESS;
906
907 eat_whitespace(&string);
908 if (string.len < 1)
909 {
910 destroy(this);
911 return NULL;
912 }
913
914 /* get all tokens, separated by '-' */
915 while (extract_token(&alg, '-', &string))
916 {
917 status |= add_string_algo(this, alg);
918 }
919 if (string.len)
920 {
921 status |= add_string_algo(this, string);
922 }
923 if (status != SUCCESS)
924 {
925 destroy(this);
926 return NULL;
927 }
928
929 check_proposal(this);
930
931 if (protocol == PROTO_AH || protocol == PROTO_ESP)
932 {
933 add_algorithm(this, EXTENDED_SEQUENCE_NUMBERS, NO_EXT_SEQ_NUMBERS, 0);
934 }
935 return &this->public;
936 }