ddbc155c30c1aeb04a3bc3731a5a351c1e6a520a
[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 void 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 }
467
468 if (this->protocol == PROTO_ESP)
469 {
470 e = create_enumerator(this, ENCRYPTION_ALGORITHM);
471 while (e->enumerate(e, &alg, &ks))
472 {
473 if (!encryption_algorithm_is_aead(alg))
474 {
475 all_aead = FALSE;
476 break;
477 }
478 }
479 e->destroy(e);
480
481 if (all_aead)
482 {
483 /* if all encryption algorithms in the proposal are AEADs,
484 * we MUST NOT propose any integrity algorithms */
485 e = array_create_enumerator(this->transforms);
486 while (e->enumerate(e, &entry))
487 {
488 if (entry->type == INTEGRITY_ALGORITHM)
489 {
490 array_remove_at(this->transforms, e);
491 }
492 }
493 e->destroy(e);
494 }
495 }
496
497 if (this->protocol == PROTO_AH || this->protocol == PROTO_ESP)
498 {
499 e = create_enumerator(this, EXTENDED_SEQUENCE_NUMBERS);
500 if (!e->enumerate(e, NULL, NULL))
501 { /* ESN not specified, assume not supported */
502 add_algorithm(this, EXTENDED_SEQUENCE_NUMBERS, NO_EXT_SEQ_NUMBERS, 0);
503 }
504 e->destroy(e);
505 }
506
507 array_compress(this->transforms);
508 }
509
510 /**
511 * add a algorithm identified by a string to the proposal.
512 */
513 static bool add_string_algo(private_proposal_t *this, const char *alg)
514 {
515 const proposal_token_t *token;
516
517 token = lib->proposal->get_token(lib->proposal, alg);
518 if (token == NULL)
519 {
520 DBG1(DBG_CFG, "algorithm '%s' not recognized", alg);
521 return FALSE;
522 }
523
524 add_algorithm(this, token->type, token->algorithm, token->keysize);
525
526 return TRUE;
527 }
528
529 /**
530 * print all algorithms of a kind to buffer
531 */
532 static int print_alg(private_proposal_t *this, printf_hook_data_t *data,
533 u_int kind, void *names, bool *first)
534 {
535 enumerator_t *enumerator;
536 size_t written = 0;
537 uint16_t alg, size;
538
539 enumerator = create_enumerator(this, kind);
540 while (enumerator->enumerate(enumerator, &alg, &size))
541 {
542 if (*first)
543 {
544 written += print_in_hook(data, "%N", names, alg);
545 *first = FALSE;
546 }
547 else
548 {
549 written += print_in_hook(data, "/%N", names, alg);
550 }
551 if (size)
552 {
553 written += print_in_hook(data, "_%u", size);
554 }
555 }
556 enumerator->destroy(enumerator);
557 return written;
558 }
559
560 /**
561 * Described in header.
562 */
563 int proposal_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
564 const void *const *args)
565 {
566 private_proposal_t *this = *((private_proposal_t**)(args[0]));
567 linked_list_t *list = *((linked_list_t**)(args[0]));
568 enumerator_t *enumerator;
569 size_t written = 0;
570 bool first = TRUE;
571
572 if (this == NULL)
573 {
574 return print_in_hook(data, "(null)");
575 }
576
577 if (spec->hash)
578 {
579 enumerator = list->create_enumerator(list);
580 while (enumerator->enumerate(enumerator, &this))
581 { /* call recursivly */
582 if (first)
583 {
584 written += print_in_hook(data, "%P", this);
585 first = FALSE;
586 }
587 else
588 {
589 written += print_in_hook(data, ", %P", this);
590 }
591 }
592 enumerator->destroy(enumerator);
593 return written;
594 }
595
596 written = print_in_hook(data, "%N:", protocol_id_names, this->protocol);
597 written += print_alg(this, data, ENCRYPTION_ALGORITHM,
598 encryption_algorithm_names, &first);
599 written += print_alg(this, data, INTEGRITY_ALGORITHM,
600 integrity_algorithm_names, &first);
601 written += print_alg(this, data, PSEUDO_RANDOM_FUNCTION,
602 pseudo_random_function_names, &first);
603 written += print_alg(this, data, DIFFIE_HELLMAN_GROUP,
604 diffie_hellman_group_names, &first);
605 written += print_alg(this, data, EXTENDED_SEQUENCE_NUMBERS,
606 extended_sequence_numbers_names, &first);
607 return written;
608 }
609
610 METHOD(proposal_t, destroy, void,
611 private_proposal_t *this)
612 {
613 array_destroy(this->transforms);
614 free(this);
615 }
616
617 /*
618 * Described in header
619 */
620 proposal_t *proposal_create(protocol_id_t protocol, u_int number)
621 {
622 private_proposal_t *this;
623
624 INIT(this,
625 .public = {
626 .add_algorithm = _add_algorithm,
627 .create_enumerator = _create_enumerator,
628 .get_algorithm = _get_algorithm,
629 .has_dh_group = _has_dh_group,
630 .strip_dh = _strip_dh,
631 .select = _select_proposal,
632 .get_protocol = _get_protocol,
633 .set_spi = _set_spi,
634 .get_spi = _get_spi,
635 .get_number = _get_number,
636 .equals = _equals,
637 .clone = _clone_,
638 .destroy = _destroy,
639 },
640 .protocol = protocol,
641 .number = number,
642 .transforms = array_create(sizeof(entry_t), 0),
643 );
644
645 return &this->public;
646 }
647
648 /**
649 * Add supported IKE algorithms to proposal
650 */
651 static bool proposal_add_supported_ike(private_proposal_t *this, bool aead)
652 {
653 enumerator_t *enumerator;
654 encryption_algorithm_t encryption;
655 integrity_algorithm_t integrity;
656 pseudo_random_function_t prf;
657 diffie_hellman_group_t group;
658 const char *plugin_name;
659
660 if (aead)
661 {
662 /* Round 1 adds algorithms with at least 128 bit security strength */
663 enumerator = lib->crypto->create_aead_enumerator(lib->crypto);
664 while (enumerator->enumerate(enumerator, &encryption, &plugin_name))
665 {
666 switch (encryption)
667 {
668 case ENCR_AES_GCM_ICV16:
669 case ENCR_AES_CCM_ICV16:
670 case ENCR_CAMELLIA_CCM_ICV16:
671 /* we assume that we support all AES/Camellia sizes */
672 add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 128);
673 add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 192);
674 add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 256);
675 break;
676 case ENCR_CHACHA20_POLY1305:
677 add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 256);
678 break;
679 default:
680 break;
681 }
682 }
683 enumerator->destroy(enumerator);
684
685 /* Round 2 adds algorithms with less than 128 bit security strength */
686 enumerator = lib->crypto->create_aead_enumerator(lib->crypto);
687 while (enumerator->enumerate(enumerator, &encryption, &plugin_name))
688 {
689 switch (encryption)
690 {
691 case ENCR_AES_GCM_ICV12:
692 case ENCR_AES_GCM_ICV8:
693 case ENCR_AES_CCM_ICV12:
694 case ENCR_AES_CCM_ICV8:
695 case ENCR_CAMELLIA_CCM_ICV12:
696 case ENCR_CAMELLIA_CCM_ICV8:
697 /* we assume that we support all AES/Camellia sizes */
698 add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 128);
699 add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 192);
700 add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 256);
701 break;
702 default:
703 break;
704 }
705 }
706 enumerator->destroy(enumerator);
707
708 if (!array_count(this->transforms))
709 {
710 return FALSE;
711 }
712 }
713 else
714 {
715 /* Round 1 adds algorithms with at least 128 bit security strength */
716 enumerator = lib->crypto->create_crypter_enumerator(lib->crypto);
717 while (enumerator->enumerate(enumerator, &encryption, &plugin_name))
718 {
719 switch (encryption)
720 {
721 case ENCR_AES_CBC:
722 case ENCR_AES_CTR:
723 case ENCR_CAMELLIA_CBC:
724 case ENCR_CAMELLIA_CTR:
725 /* we assume that we support all AES/Camellia sizes */
726 add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 128);
727 add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 192);
728 add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 256);
729 break;
730 default:
731 break;
732 }
733 }
734 enumerator->destroy(enumerator);
735
736 /* Round 2 adds algorithms with less than 128 bit security strength */
737 enumerator = lib->crypto->create_crypter_enumerator(lib->crypto);
738 while (enumerator->enumerate(enumerator, &encryption, &plugin_name))
739 {
740 switch (encryption)
741 {
742 case ENCR_3DES:
743 add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 0);
744 break;
745 case ENCR_DES:
746 /* no, thanks */
747 break;
748 default:
749 break;
750 }
751 }
752 enumerator->destroy(enumerator);
753
754 if (!array_count(this->transforms))
755 {
756 return FALSE;
757 }
758
759 /* Round 1 adds algorithms with at least 128 bit security strength */
760 enumerator = lib->crypto->create_signer_enumerator(lib->crypto);
761 while (enumerator->enumerate(enumerator, &integrity, &plugin_name))
762 {
763 switch (integrity)
764 {
765 case AUTH_HMAC_SHA2_256_128:
766 case AUTH_HMAC_SHA2_384_192:
767 case AUTH_HMAC_SHA2_512_256:
768 add_algorithm(this, INTEGRITY_ALGORITHM, integrity, 0);
769 break;
770 default:
771 break;
772 }
773 }
774 enumerator->destroy(enumerator);
775
776 /* Round 2 adds algorithms with less than 128 bit security strength */
777 enumerator = lib->crypto->create_signer_enumerator(lib->crypto);
778 while (enumerator->enumerate(enumerator, &integrity, &plugin_name))
779 {
780 switch (integrity)
781 {
782 case AUTH_AES_XCBC_96:
783 case AUTH_AES_CMAC_96:
784 case AUTH_HMAC_SHA1_96:
785 case AUTH_HMAC_MD5_96:
786 add_algorithm(this, INTEGRITY_ALGORITHM, integrity, 0);
787 break;
788 default:
789 break;
790 }
791 }
792 enumerator->destroy(enumerator);
793 }
794
795 /* Round 1 adds algorithms with at least 128 bit security strength */
796 enumerator = lib->crypto->create_prf_enumerator(lib->crypto);
797 while (enumerator->enumerate(enumerator, &prf, &plugin_name))
798 {
799 switch (prf)
800 {
801 case PRF_HMAC_SHA2_256:
802 case PRF_HMAC_SHA2_384:
803 case PRF_HMAC_SHA2_512:
804 case PRF_AES128_XCBC:
805 case PRF_AES128_CMAC:
806 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, prf, 0);
807 break;
808 default:
809 break;
810 }
811 }
812 enumerator->destroy(enumerator);
813
814 /* Round 2 adds algorithms with less than 128 bit security strength */
815 enumerator = lib->crypto->create_prf_enumerator(lib->crypto);
816 while (enumerator->enumerate(enumerator, &prf, &plugin_name))
817 {
818 switch (prf)
819 {
820 case PRF_HMAC_SHA1:
821 case PRF_HMAC_MD5:
822 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, prf, 0);
823 break;
824 default:
825 break;
826 }
827 }
828 enumerator->destroy(enumerator);
829
830 /* Round 1 adds ECC and NTRU algorithms with at least 128 bit security strength */
831 enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
832 while (enumerator->enumerate(enumerator, &group, &plugin_name))
833 {
834 switch (group)
835 {
836 case ECP_256_BIT:
837 case ECP_384_BIT:
838 case ECP_521_BIT:
839 case ECP_256_BP:
840 case ECP_384_BP:
841 case ECP_512_BP:
842 case NTRU_128_BIT:
843 case NTRU_192_BIT:
844 case NTRU_256_BIT:
845 case NH_128_BIT:
846 add_algorithm(this, DIFFIE_HELLMAN_GROUP, group, 0);
847 break;
848 default:
849 break;
850 }
851 }
852 enumerator->destroy(enumerator);
853
854 /* Round 2 adds other algorithms with at least 128 bit security strength */
855 enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
856 while (enumerator->enumerate(enumerator, &group, &plugin_name))
857 {
858 switch (group)
859 {
860 case MODP_3072_BIT:
861 case MODP_4096_BIT:
862 case MODP_8192_BIT:
863 add_algorithm(this, DIFFIE_HELLMAN_GROUP, group, 0);
864 break;
865 default:
866 break;
867 }
868 }
869 enumerator->destroy(enumerator);
870
871 /* Round 3 adds algorithms with less than 128 bit security strength */
872 enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
873 while (enumerator->enumerate(enumerator, &group, &plugin_name))
874 {
875 switch (group)
876 {
877 case MODP_NULL:
878 /* only for testing purposes */
879 break;
880 case MODP_768_BIT:
881 /* weak */
882 break;
883 case MODP_2048_224:
884 case MODP_1536_BIT:
885 case MODP_1024_160:
886 case ECP_224_BIT:
887 case ECP_224_BP:
888 case ECP_192_BIT:
889 case NTRU_112_BIT:
890 /* rarely used */
891 break;
892 case MODP_2048_BIT:
893 case MODP_2048_256:
894 case MODP_1024_BIT:
895 add_algorithm(this, DIFFIE_HELLMAN_GROUP, group, 0);
896 break;
897 default:
898 break;
899 }
900 }
901 enumerator->destroy(enumerator);
902
903 return TRUE;
904 }
905
906 /*
907 * Described in header
908 */
909 proposal_t *proposal_create_default(protocol_id_t protocol)
910 {
911 private_proposal_t *this = (private_proposal_t*)proposal_create(protocol, 0);
912
913 switch (protocol)
914 {
915 case PROTO_IKE:
916 if (!proposal_add_supported_ike(this, FALSE))
917 {
918 destroy(this);
919 return NULL;
920 }
921 break;
922 case PROTO_ESP:
923 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 128);
924 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 192);
925 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 256);
926 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_3DES, 0);
927 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_BLOWFISH, 256);
928 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_256_128, 0);
929 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_384_192, 0);
930 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_512_256, 0);
931 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA1_96, 0);
932 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_AES_XCBC_96, 0);
933 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_MD5_96, 0);
934 add_algorithm(this, EXTENDED_SEQUENCE_NUMBERS, NO_EXT_SEQ_NUMBERS, 0);
935 break;
936 case PROTO_AH:
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 default:
946 break;
947 }
948 return &this->public;
949 }
950
951 /*
952 * Described in header
953 */
954 proposal_t *proposal_create_default_aead(protocol_id_t protocol)
955 {
956 private_proposal_t *this;
957
958 switch (protocol)
959 {
960 case PROTO_IKE:
961 this = (private_proposal_t*)proposal_create(protocol, 0);
962 if (!proposal_add_supported_ike(this, TRUE))
963 {
964 destroy(this);
965 return NULL;
966 }
967 return &this->public;
968 case PROTO_ESP:
969 /* we currently don't include any AEAD proposal for ESP, as we
970 * don't know if our kernel backend actually supports it. */
971 return NULL;
972 case PROTO_AH:
973 default:
974 return NULL;
975 }
976 }
977
978 /*
979 * Described in header
980 */
981 proposal_t *proposal_create_from_string(protocol_id_t protocol, const char *algs)
982 {
983 private_proposal_t *this;
984 enumerator_t *enumerator;
985 bool failed = TRUE;
986 char *alg;
987
988 this = (private_proposal_t*)proposal_create(protocol, 0);
989
990 /* get all tokens, separated by '-' */
991 enumerator = enumerator_create_token(algs, "-", " ");
992 while (enumerator->enumerate(enumerator, &alg))
993 {
994 if (!add_string_algo(this, alg))
995 {
996 failed = TRUE;
997 break;
998 }
999 failed = FALSE;
1000 }
1001 enumerator->destroy(enumerator);
1002
1003 if (failed)
1004 {
1005 destroy(this);
1006 return NULL;
1007 }
1008
1009 check_proposal(this);
1010
1011 return &this->public;
1012 }