proposal: Make DH groups mandatory in IKE proposals parsed from strings
[strongswan.git] / src / libcharon / config / proposal.c
1 /*
2 * Copyright (C) 2008-2016 Tobias Brunner
3 * Copyright (C) 2006-2010 Martin Willi
4 * Copyright (C) 2013-2015 Andreas Steffen
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include <string.h>
19
20 #include "proposal.h"
21
22 #include <daemon.h>
23 #include <collections/array.h>
24 #include <utils/identification.h>
25
26 #include <crypto/transform.h>
27 #include <crypto/prfs/prf.h>
28 #include <crypto/crypters/crypter.h>
29 #include <crypto/signers/signer.h>
30
31 ENUM(protocol_id_names, PROTO_NONE, PROTO_IPCOMP,
32 "PROTO_NONE",
33 "IKE",
34 "AH",
35 "ESP",
36 "IPCOMP",
37 );
38
39 typedef struct private_proposal_t private_proposal_t;
40
41 /**
42 * Private data of an proposal_t object
43 */
44 struct private_proposal_t {
45
46 /**
47 * Public part
48 */
49 proposal_t public;
50
51 /**
52 * protocol (ESP or AH)
53 */
54 protocol_id_t protocol;
55
56 /**
57 * Priority ordered list of transforms, as entry_t
58 */
59 array_t *transforms;
60
61 /**
62 * senders SPI
63 */
64 uint64_t spi;
65
66 /**
67 * Proposal number
68 */
69 u_int number;
70 };
71
72 /**
73 * Struct used to store different kinds of algorithms.
74 */
75 typedef struct {
76 /** Type of the transform */
77 transform_type_t type;
78 /** algorithm identifier */
79 uint16_t alg;
80 /** key size in bits, or zero if not needed */
81 uint16_t key_size;
82 } entry_t;
83
84 METHOD(proposal_t, add_algorithm, void,
85 private_proposal_t *this, transform_type_t type,
86 uint16_t alg, uint16_t key_size)
87 {
88 entry_t entry = {
89 .type = type,
90 .alg = alg,
91 .key_size = key_size,
92 };
93
94 array_insert(this->transforms, ARRAY_TAIL, &entry);
95 }
96
97 /**
98 * filter function for peer configs
99 */
100 static bool alg_filter(uintptr_t type, entry_t **in, uint16_t *alg,
101 void **unused, uint16_t *key_size)
102 {
103 entry_t *entry = *in;
104
105 if (entry->type != type)
106 {
107 return FALSE;
108 }
109 if (alg)
110 {
111 *alg = entry->alg;
112 }
113 if (key_size)
114 {
115 *key_size = entry->key_size;
116 }
117 return TRUE;
118 }
119
120 METHOD(proposal_t, create_enumerator, enumerator_t*,
121 private_proposal_t *this, transform_type_t type)
122 {
123 return enumerator_create_filter(
124 array_create_enumerator(this->transforms),
125 (void*)alg_filter, (void*)(uintptr_t)type, NULL);
126 }
127
128 METHOD(proposal_t, get_algorithm, bool,
129 private_proposal_t *this, transform_type_t type,
130 uint16_t *alg, uint16_t *key_size)
131 {
132 enumerator_t *enumerator;
133 bool found = FALSE;
134
135 enumerator = create_enumerator(this, type);
136 if (enumerator->enumerate(enumerator, alg, key_size))
137 {
138 found = TRUE;
139 }
140 enumerator->destroy(enumerator);
141
142 return found;
143 }
144
145 METHOD(proposal_t, has_dh_group, bool,
146 private_proposal_t *this, diffie_hellman_group_t group)
147 {
148 bool found = FALSE, any = FALSE;
149 enumerator_t *enumerator;
150 uint16_t current;
151
152 enumerator = create_enumerator(this, DIFFIE_HELLMAN_GROUP);
153 while (enumerator->enumerate(enumerator, &current, NULL))
154 {
155 any = TRUE;
156 if (current == group)
157 {
158 found = TRUE;
159 break;
160 }
161 }
162 enumerator->destroy(enumerator);
163
164 if (!any && group == MODP_NONE)
165 {
166 found = TRUE;
167 }
168 return found;
169 }
170
171 METHOD(proposal_t, strip_dh, void,
172 private_proposal_t *this, diffie_hellman_group_t keep)
173 {
174 enumerator_t *enumerator;
175 entry_t *entry;
176
177 enumerator = array_create_enumerator(this->transforms);
178 while (enumerator->enumerate(enumerator, &entry))
179 {
180 if (entry->type == DIFFIE_HELLMAN_GROUP &&
181 entry->alg != keep)
182 {
183 array_remove_at(this->transforms, enumerator);
184 }
185 }
186 enumerator->destroy(enumerator);
187 }
188
189 /**
190 * Select a matching proposal from this and other, insert into selected.
191 */
192 static bool select_algo(private_proposal_t *this, proposal_t *other,
193 proposal_t *selected, transform_type_t type, bool priv)
194 {
195 enumerator_t *e1, *e2;
196 uint16_t alg1, alg2, ks1, ks2;
197 bool found = FALSE, optional = FALSE;
198
199 if (type == INTEGRITY_ALGORITHM &&
200 selected->get_algorithm(selected, ENCRYPTION_ALGORITHM, &alg1, NULL) &&
201 encryption_algorithm_is_aead(alg1))
202 {
203 /* no integrity algorithm required, we have an AEAD */
204 return TRUE;
205 }
206 if (type == DIFFIE_HELLMAN_GROUP)
207 {
208 optional = this->protocol == PROTO_ESP || this->protocol == PROTO_AH;
209 }
210
211 e1 = create_enumerator(this, type);
212 e2 = other->create_enumerator(other, type);
213 if (!e1->enumerate(e1, &alg1, NULL))
214 {
215 if (!e2->enumerate(e2, &alg2, NULL))
216 {
217 found = TRUE;
218 }
219 else if (optional)
220 {
221 do
222 { /* if NONE is proposed, we accept the proposal */
223 found = !alg2;
224 }
225 while (!found && e2->enumerate(e2, &alg2, NULL));
226 }
227 }
228 else if (!e2->enumerate(e2, NULL, NULL))
229 {
230 if (optional)
231 {
232 do
233 { /* if NONE is proposed, we accept the proposal */
234 found = !alg1;
235 }
236 while (!found && e1->enumerate(e1, &alg1, NULL));
237 }
238 }
239
240 e1->destroy(e1);
241 e1 = create_enumerator(this, type);
242 /* compare algs, order of algs in "first" is preferred */
243 while (!found && e1->enumerate(e1, &alg1, &ks1))
244 {
245 e2->destroy(e2);
246 e2 = other->create_enumerator(other, type);
247 while (e2->enumerate(e2, &alg2, &ks2))
248 {
249 if (alg1 == alg2 && ks1 == ks2)
250 {
251 if (!priv && alg1 >= 1024)
252 {
253 /* accept private use algorithms only if requested */
254 DBG1(DBG_CFG, "an algorithm from private space would match, "
255 "but peer implementation is unknown, skipped");
256 continue;
257 }
258 selected->add_algorithm(selected, type, alg1, ks1);
259 found = TRUE;
260 break;
261 }
262 }
263 }
264 /* no match in all comparisons */
265 e1->destroy(e1);
266 e2->destroy(e2);
267
268 if (!found)
269 {
270 DBG2(DBG_CFG, " no acceptable %N found", transform_type_names, type);
271 }
272 return found;
273 }
274
275 METHOD(proposal_t, select_proposal, proposal_t*,
276 private_proposal_t *this, proposal_t *other, bool private)
277 {
278 proposal_t *selected;
279
280 DBG2(DBG_CFG, "selecting proposal:");
281
282 if (this->protocol != other->get_protocol(other))
283 {
284 DBG2(DBG_CFG, " protocol mismatch, skipping");
285 return NULL;
286 }
287
288 selected = proposal_create(this->protocol, other->get_number(other));
289
290 if (!select_algo(this, other, selected, ENCRYPTION_ALGORITHM, private) ||
291 !select_algo(this, other, selected, PSEUDO_RANDOM_FUNCTION, private) ||
292 !select_algo(this, other, selected, INTEGRITY_ALGORITHM, private) ||
293 !select_algo(this, other, selected, DIFFIE_HELLMAN_GROUP, private) ||
294 !select_algo(this, other, selected, EXTENDED_SEQUENCE_NUMBERS, private))
295 {
296 selected->destroy(selected);
297 return NULL;
298 }
299
300 DBG2(DBG_CFG, " proposal matches");
301 selected->set_spi(selected, other->get_spi(other));
302 return selected;
303 }
304
305 METHOD(proposal_t, get_protocol, protocol_id_t,
306 private_proposal_t *this)
307 {
308 return this->protocol;
309 }
310
311 METHOD(proposal_t, set_spi, void,
312 private_proposal_t *this, uint64_t spi)
313 {
314 this->spi = spi;
315 }
316
317 METHOD(proposal_t, get_spi, uint64_t,
318 private_proposal_t *this)
319 {
320 return this->spi;
321 }
322
323 /**
324 * Check if two proposals have the same algorithms for a given transform type
325 */
326 static bool algo_list_equals(private_proposal_t *this, proposal_t *other,
327 transform_type_t type)
328 {
329 enumerator_t *e1, *e2;
330 uint16_t alg1, alg2, ks1, ks2;
331 bool equals = TRUE;
332
333 e1 = create_enumerator(this, type);
334 e2 = other->create_enumerator(other, type);
335 while (e1->enumerate(e1, &alg1, &ks1))
336 {
337 if (!e2->enumerate(e2, &alg2, &ks2))
338 {
339 /* this has more algs */
340 equals = FALSE;
341 break;
342 }
343 if (alg1 != alg2 || ks1 != ks2)
344 {
345 equals = FALSE;
346 break;
347 }
348 }
349 if (e2->enumerate(e2, &alg2, &ks2))
350 {
351 /* other has more algs */
352 equals = FALSE;
353 }
354 e1->destroy(e1);
355 e2->destroy(e2);
356
357 return equals;
358 }
359
360 METHOD(proposal_t, get_number, u_int,
361 private_proposal_t *this)
362 {
363 return this->number;
364 }
365
366 METHOD(proposal_t, equals, bool,
367 private_proposal_t *this, proposal_t *other)
368 {
369 if (&this->public == other)
370 {
371 return TRUE;
372 }
373 return (
374 algo_list_equals(this, other, ENCRYPTION_ALGORITHM) &&
375 algo_list_equals(this, other, INTEGRITY_ALGORITHM) &&
376 algo_list_equals(this, other, PSEUDO_RANDOM_FUNCTION) &&
377 algo_list_equals(this, other, DIFFIE_HELLMAN_GROUP) &&
378 algo_list_equals(this, other, EXTENDED_SEQUENCE_NUMBERS));
379 }
380
381 METHOD(proposal_t, clone_, proposal_t*,
382 private_proposal_t *this)
383 {
384 private_proposal_t *clone;
385 enumerator_t *enumerator;
386 entry_t *entry;
387
388 clone = (private_proposal_t*)proposal_create(this->protocol, 0);
389
390 enumerator = array_create_enumerator(this->transforms);
391 while (enumerator->enumerate(enumerator, &entry))
392 {
393 array_insert(clone->transforms, ARRAY_TAIL, entry);
394 }
395 enumerator->destroy(enumerator);
396
397 clone->spi = this->spi;
398 clone->number = this->number;
399
400 return &clone->public;
401 }
402
403 /**
404 * Map integrity algorithms to the PRF functions using the same algorithm.
405 */
406 static const struct {
407 integrity_algorithm_t integ;
408 pseudo_random_function_t prf;
409 } integ_prf_map[] = {
410 {AUTH_HMAC_SHA1_96, PRF_HMAC_SHA1 },
411 {AUTH_HMAC_SHA1_160, PRF_HMAC_SHA1 },
412 {AUTH_HMAC_SHA2_256_128, PRF_HMAC_SHA2_256 },
413 {AUTH_HMAC_SHA2_384_192, PRF_HMAC_SHA2_384 },
414 {AUTH_HMAC_SHA2_512_256, PRF_HMAC_SHA2_512 },
415 {AUTH_HMAC_MD5_96, PRF_HMAC_MD5 },
416 {AUTH_HMAC_MD5_128, PRF_HMAC_MD5 },
417 {AUTH_AES_XCBC_96, PRF_AES128_XCBC },
418 {AUTH_CAMELLIA_XCBC_96, PRF_CAMELLIA128_XCBC },
419 {AUTH_AES_CMAC_96, PRF_AES128_CMAC },
420 };
421
422 /**
423 * Checks the proposal read from a string.
424 */
425 static bool check_proposal(private_proposal_t *this)
426 {
427 enumerator_t *e;
428 entry_t *entry;
429 uint16_t alg, ks;
430 bool all_aead = TRUE;
431 int i;
432
433 if (this->protocol == PROTO_IKE)
434 {
435 e = create_enumerator(this, PSEUDO_RANDOM_FUNCTION);
436 if (!e->enumerate(e, &alg, &ks))
437 {
438 /* No explicit PRF found. We assume the same algorithm as used
439 * for integrity checking */
440 e->destroy(e);
441 e = create_enumerator(this, INTEGRITY_ALGORITHM);
442 while (e->enumerate(e, &alg, &ks))
443 {
444 for (i = 0; i < countof(integ_prf_map); i++)
445 {
446 if (alg == integ_prf_map[i].integ)
447 {
448 add_algorithm(this, PSEUDO_RANDOM_FUNCTION,
449 integ_prf_map[i].prf, 0);
450 break;
451 }
452 }
453 }
454 }
455 e->destroy(e);
456 /* remove MODP_NONE from IKE proposal */
457 e = array_create_enumerator(this->transforms);
458 while (e->enumerate(e, &entry))
459 {
460 if (entry->type == DIFFIE_HELLMAN_GROUP && !entry->alg)
461 {
462 array_remove_at(this->transforms, e);
463 }
464 }
465 e->destroy(e);
466 e = create_enumerator(this, DIFFIE_HELLMAN_GROUP);
467 if (!e->enumerate(e, &alg, &ks))
468 {
469 DBG1(DBG_CFG, "a DH group is mandatory in IKE proposals");
470 e->destroy(e);
471 return FALSE;
472 }
473 e->destroy(e);
474 }
475
476 if (this->protocol == PROTO_ESP)
477 {
478 e = create_enumerator(this, ENCRYPTION_ALGORITHM);
479 while (e->enumerate(e, &alg, &ks))
480 {
481 if (!encryption_algorithm_is_aead(alg))
482 {
483 all_aead = FALSE;
484 break;
485 }
486 }
487 e->destroy(e);
488
489 if (all_aead)
490 {
491 /* if all encryption algorithms in the proposal are AEADs,
492 * we MUST NOT propose any integrity algorithms */
493 e = array_create_enumerator(this->transforms);
494 while (e->enumerate(e, &entry))
495 {
496 if (entry->type == INTEGRITY_ALGORITHM)
497 {
498 array_remove_at(this->transforms, e);
499 }
500 }
501 e->destroy(e);
502 }
503 }
504
505 if (this->protocol == PROTO_AH || this->protocol == PROTO_ESP)
506 {
507 e = create_enumerator(this, EXTENDED_SEQUENCE_NUMBERS);
508 if (!e->enumerate(e, NULL, NULL))
509 { /* ESN not specified, assume not supported */
510 add_algorithm(this, EXTENDED_SEQUENCE_NUMBERS, NO_EXT_SEQ_NUMBERS, 0);
511 }
512 e->destroy(e);
513 }
514
515 array_compress(this->transforms);
516 return TRUE;
517 }
518
519 /**
520 * add a algorithm identified by a string to the proposal.
521 */
522 static bool add_string_algo(private_proposal_t *this, const char *alg)
523 {
524 const proposal_token_t *token;
525
526 token = lib->proposal->get_token(lib->proposal, alg);
527 if (token == NULL)
528 {
529 DBG1(DBG_CFG, "algorithm '%s' not recognized", alg);
530 return FALSE;
531 }
532
533 add_algorithm(this, token->type, token->algorithm, token->keysize);
534
535 return TRUE;
536 }
537
538 /**
539 * print all algorithms of a kind to buffer
540 */
541 static int print_alg(private_proposal_t *this, printf_hook_data_t *data,
542 u_int kind, void *names, bool *first)
543 {
544 enumerator_t *enumerator;
545 size_t written = 0;
546 uint16_t alg, size;
547
548 enumerator = create_enumerator(this, kind);
549 while (enumerator->enumerate(enumerator, &alg, &size))
550 {
551 if (*first)
552 {
553 written += print_in_hook(data, "%N", names, alg);
554 *first = FALSE;
555 }
556 else
557 {
558 written += print_in_hook(data, "/%N", names, alg);
559 }
560 if (size)
561 {
562 written += print_in_hook(data, "_%u", size);
563 }
564 }
565 enumerator->destroy(enumerator);
566 return written;
567 }
568
569 /**
570 * Described in header.
571 */
572 int proposal_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
573 const void *const *args)
574 {
575 private_proposal_t *this = *((private_proposal_t**)(args[0]));
576 linked_list_t *list = *((linked_list_t**)(args[0]));
577 enumerator_t *enumerator;
578 size_t written = 0;
579 bool first = TRUE;
580
581 if (this == NULL)
582 {
583 return print_in_hook(data, "(null)");
584 }
585
586 if (spec->hash)
587 {
588 enumerator = list->create_enumerator(list);
589 while (enumerator->enumerate(enumerator, &this))
590 { /* call recursivly */
591 if (first)
592 {
593 written += print_in_hook(data, "%P", this);
594 first = FALSE;
595 }
596 else
597 {
598 written += print_in_hook(data, ", %P", this);
599 }
600 }
601 enumerator->destroy(enumerator);
602 return written;
603 }
604
605 written = print_in_hook(data, "%N:", protocol_id_names, this->protocol);
606 written += print_alg(this, data, ENCRYPTION_ALGORITHM,
607 encryption_algorithm_names, &first);
608 written += print_alg(this, data, INTEGRITY_ALGORITHM,
609 integrity_algorithm_names, &first);
610 written += print_alg(this, data, PSEUDO_RANDOM_FUNCTION,
611 pseudo_random_function_names, &first);
612 written += print_alg(this, data, DIFFIE_HELLMAN_GROUP,
613 diffie_hellman_group_names, &first);
614 written += print_alg(this, data, EXTENDED_SEQUENCE_NUMBERS,
615 extended_sequence_numbers_names, &first);
616 return written;
617 }
618
619 METHOD(proposal_t, destroy, void,
620 private_proposal_t *this)
621 {
622 array_destroy(this->transforms);
623 free(this);
624 }
625
626 /*
627 * Described in header
628 */
629 proposal_t *proposal_create(protocol_id_t protocol, u_int number)
630 {
631 private_proposal_t *this;
632
633 INIT(this,
634 .public = {
635 .add_algorithm = _add_algorithm,
636 .create_enumerator = _create_enumerator,
637 .get_algorithm = _get_algorithm,
638 .has_dh_group = _has_dh_group,
639 .strip_dh = _strip_dh,
640 .select = _select_proposal,
641 .get_protocol = _get_protocol,
642 .set_spi = _set_spi,
643 .get_spi = _get_spi,
644 .get_number = _get_number,
645 .equals = _equals,
646 .clone = _clone_,
647 .destroy = _destroy,
648 },
649 .protocol = protocol,
650 .number = number,
651 .transforms = array_create(sizeof(entry_t), 0),
652 );
653
654 return &this->public;
655 }
656
657 /**
658 * Add supported IKE algorithms to proposal
659 */
660 static bool proposal_add_supported_ike(private_proposal_t *this, bool aead)
661 {
662 enumerator_t *enumerator;
663 encryption_algorithm_t encryption;
664 integrity_algorithm_t integrity;
665 pseudo_random_function_t prf;
666 diffie_hellman_group_t group;
667 const char *plugin_name;
668
669 if (aead)
670 {
671 /* Round 1 adds algorithms with at least 128 bit security strength */
672 enumerator = lib->crypto->create_aead_enumerator(lib->crypto);
673 while (enumerator->enumerate(enumerator, &encryption, &plugin_name))
674 {
675 switch (encryption)
676 {
677 case ENCR_AES_GCM_ICV16:
678 case ENCR_AES_CCM_ICV16:
679 case ENCR_CAMELLIA_CCM_ICV16:
680 /* we assume that we support all AES/Camellia sizes */
681 add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 128);
682 add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 192);
683 add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 256);
684 break;
685 case ENCR_CHACHA20_POLY1305:
686 add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 256);
687 break;
688 default:
689 break;
690 }
691 }
692 enumerator->destroy(enumerator);
693
694 /* Round 2 adds algorithms with less than 128 bit security strength */
695 enumerator = lib->crypto->create_aead_enumerator(lib->crypto);
696 while (enumerator->enumerate(enumerator, &encryption, &plugin_name))
697 {
698 switch (encryption)
699 {
700 case ENCR_AES_GCM_ICV12:
701 case ENCR_AES_GCM_ICV8:
702 case ENCR_AES_CCM_ICV12:
703 case ENCR_AES_CCM_ICV8:
704 case ENCR_CAMELLIA_CCM_ICV12:
705 case ENCR_CAMELLIA_CCM_ICV8:
706 /* we assume that we support all AES/Camellia sizes */
707 add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 128);
708 add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 192);
709 add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 256);
710 break;
711 default:
712 break;
713 }
714 }
715 enumerator->destroy(enumerator);
716
717 if (!array_count(this->transforms))
718 {
719 return FALSE;
720 }
721 }
722 else
723 {
724 /* Round 1 adds algorithms with at least 128 bit security strength */
725 enumerator = lib->crypto->create_crypter_enumerator(lib->crypto);
726 while (enumerator->enumerate(enumerator, &encryption, &plugin_name))
727 {
728 switch (encryption)
729 {
730 case ENCR_AES_CBC:
731 case ENCR_AES_CTR:
732 case ENCR_CAMELLIA_CBC:
733 case ENCR_CAMELLIA_CTR:
734 /* we assume that we support all AES/Camellia sizes */
735 add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 128);
736 add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 192);
737 add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 256);
738 break;
739 default:
740 break;
741 }
742 }
743 enumerator->destroy(enumerator);
744
745 /* Round 2 adds algorithms with less than 128 bit security strength */
746 enumerator = lib->crypto->create_crypter_enumerator(lib->crypto);
747 while (enumerator->enumerate(enumerator, &encryption, &plugin_name))
748 {
749 switch (encryption)
750 {
751 case ENCR_3DES:
752 add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 0);
753 break;
754 case ENCR_DES:
755 /* no, thanks */
756 break;
757 default:
758 break;
759 }
760 }
761 enumerator->destroy(enumerator);
762
763 if (!array_count(this->transforms))
764 {
765 return FALSE;
766 }
767
768 /* Round 1 adds algorithms with at least 128 bit security strength */
769 enumerator = lib->crypto->create_signer_enumerator(lib->crypto);
770 while (enumerator->enumerate(enumerator, &integrity, &plugin_name))
771 {
772 switch (integrity)
773 {
774 case AUTH_HMAC_SHA2_256_128:
775 case AUTH_HMAC_SHA2_384_192:
776 case AUTH_HMAC_SHA2_512_256:
777 add_algorithm(this, INTEGRITY_ALGORITHM, integrity, 0);
778 break;
779 default:
780 break;
781 }
782 }
783 enumerator->destroy(enumerator);
784
785 /* Round 2 adds algorithms with less than 128 bit security strength */
786 enumerator = lib->crypto->create_signer_enumerator(lib->crypto);
787 while (enumerator->enumerate(enumerator, &integrity, &plugin_name))
788 {
789 switch (integrity)
790 {
791 case AUTH_AES_XCBC_96:
792 case AUTH_AES_CMAC_96:
793 case AUTH_HMAC_SHA1_96:
794 case AUTH_HMAC_MD5_96:
795 add_algorithm(this, INTEGRITY_ALGORITHM, integrity, 0);
796 break;
797 default:
798 break;
799 }
800 }
801 enumerator->destroy(enumerator);
802 }
803
804 /* Round 1 adds algorithms with at least 128 bit security strength */
805 enumerator = lib->crypto->create_prf_enumerator(lib->crypto);
806 while (enumerator->enumerate(enumerator, &prf, &plugin_name))
807 {
808 switch (prf)
809 {
810 case PRF_HMAC_SHA2_256:
811 case PRF_HMAC_SHA2_384:
812 case PRF_HMAC_SHA2_512:
813 case PRF_AES128_XCBC:
814 case PRF_AES128_CMAC:
815 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, prf, 0);
816 break;
817 default:
818 break;
819 }
820 }
821 enumerator->destroy(enumerator);
822
823 /* Round 2 adds algorithms with less than 128 bit security strength */
824 enumerator = lib->crypto->create_prf_enumerator(lib->crypto);
825 while (enumerator->enumerate(enumerator, &prf, &plugin_name))
826 {
827 switch (prf)
828 {
829 case PRF_HMAC_SHA1:
830 case PRF_HMAC_MD5:
831 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, prf, 0);
832 break;
833 default:
834 break;
835 }
836 }
837 enumerator->destroy(enumerator);
838
839 /* Round 1 adds ECC and NTRU algorithms with at least 128 bit security strength */
840 enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
841 while (enumerator->enumerate(enumerator, &group, &plugin_name))
842 {
843 switch (group)
844 {
845 case ECP_256_BIT:
846 case ECP_384_BIT:
847 case ECP_521_BIT:
848 case ECP_256_BP:
849 case ECP_384_BP:
850 case ECP_512_BP:
851 case NTRU_128_BIT:
852 case NTRU_192_BIT:
853 case NTRU_256_BIT:
854 case NH_128_BIT:
855 add_algorithm(this, DIFFIE_HELLMAN_GROUP, group, 0);
856 break;
857 default:
858 break;
859 }
860 }
861 enumerator->destroy(enumerator);
862
863 /* Round 2 adds other algorithms with at least 128 bit security strength */
864 enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
865 while (enumerator->enumerate(enumerator, &group, &plugin_name))
866 {
867 switch (group)
868 {
869 case MODP_3072_BIT:
870 case MODP_4096_BIT:
871 case MODP_8192_BIT:
872 add_algorithm(this, DIFFIE_HELLMAN_GROUP, group, 0);
873 break;
874 default:
875 break;
876 }
877 }
878 enumerator->destroy(enumerator);
879
880 /* Round 3 adds algorithms with less than 128 bit security strength */
881 enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
882 while (enumerator->enumerate(enumerator, &group, &plugin_name))
883 {
884 switch (group)
885 {
886 case MODP_NULL:
887 /* only for testing purposes */
888 break;
889 case MODP_768_BIT:
890 /* weak */
891 break;
892 case MODP_2048_224:
893 case MODP_1536_BIT:
894 case MODP_1024_160:
895 case ECP_224_BIT:
896 case ECP_224_BP:
897 case ECP_192_BIT:
898 case NTRU_112_BIT:
899 /* rarely used */
900 break;
901 case MODP_2048_BIT:
902 case MODP_2048_256:
903 case MODP_1024_BIT:
904 add_algorithm(this, DIFFIE_HELLMAN_GROUP, group, 0);
905 break;
906 default:
907 break;
908 }
909 }
910 enumerator->destroy(enumerator);
911
912 return TRUE;
913 }
914
915 /*
916 * Described in header
917 */
918 proposal_t *proposal_create_default(protocol_id_t protocol)
919 {
920 private_proposal_t *this = (private_proposal_t*)proposal_create(protocol, 0);
921
922 switch (protocol)
923 {
924 case PROTO_IKE:
925 if (!proposal_add_supported_ike(this, FALSE))
926 {
927 destroy(this);
928 return NULL;
929 }
930 break;
931 case PROTO_ESP:
932 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 128);
933 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 192);
934 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 256);
935 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_3DES, 0);
936 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_BLOWFISH, 256);
937 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_256_128, 0);
938 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_384_192, 0);
939 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_512_256, 0);
940 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA1_96, 0);
941 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_AES_XCBC_96, 0);
942 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_MD5_96, 0);
943 add_algorithm(this, EXTENDED_SEQUENCE_NUMBERS, NO_EXT_SEQ_NUMBERS, 0);
944 break;
945 case PROTO_AH:
946 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_256_128, 0);
947 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_384_192, 0);
948 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_512_256, 0);
949 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA1_96, 0);
950 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_AES_XCBC_96, 0);
951 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_MD5_96, 0);
952 add_algorithm(this, EXTENDED_SEQUENCE_NUMBERS, NO_EXT_SEQ_NUMBERS, 0);
953 break;
954 default:
955 break;
956 }
957 return &this->public;
958 }
959
960 /*
961 * Described in header
962 */
963 proposal_t *proposal_create_default_aead(protocol_id_t protocol)
964 {
965 private_proposal_t *this;
966
967 switch (protocol)
968 {
969 case PROTO_IKE:
970 this = (private_proposal_t*)proposal_create(protocol, 0);
971 if (!proposal_add_supported_ike(this, TRUE))
972 {
973 destroy(this);
974 return NULL;
975 }
976 return &this->public;
977 case PROTO_ESP:
978 /* we currently don't include any AEAD proposal for ESP, as we
979 * don't know if our kernel backend actually supports it. */
980 return NULL;
981 case PROTO_AH:
982 default:
983 return NULL;
984 }
985 }
986
987 /*
988 * Described in header
989 */
990 proposal_t *proposal_create_from_string(protocol_id_t protocol, const char *algs)
991 {
992 private_proposal_t *this;
993 enumerator_t *enumerator;
994 bool failed = TRUE;
995 char *alg;
996
997 this = (private_proposal_t*)proposal_create(protocol, 0);
998
999 /* get all tokens, separated by '-' */
1000 enumerator = enumerator_create_token(algs, "-", " ");
1001 while (enumerator->enumerate(enumerator, &alg))
1002 {
1003 if (!add_string_algo(this, alg))
1004 {
1005 failed = TRUE;
1006 break;
1007 }
1008 failed = FALSE;
1009 }
1010 enumerator->destroy(enumerator);
1011
1012 if (failed || !check_proposal(this))
1013 {
1014 destroy(this);
1015 return NULL;
1016 }
1017
1018 return &this->public;
1019 }