aboutsummaryrefslogtreecommitdiffstats
path: root/src/charon/config/proposal.c
blob: da697392548903b00308ed0e514ddc130f69e2f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
/*
 * Copyright (C) 2008-2009 Tobias Brunner
 * Copyright (C) 2006 Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * $Id$
 */

#include <string.h>

#include "proposal.h"

#include <daemon.h>
#include <utils/linked_list.h>
#include <utils/identification.h>
#include <utils/lexparser.h>
#include <crypto/prfs/prf.h>
#include <crypto/crypters/crypter.h>
#include <crypto/signers/signer.h>


ENUM(protocol_id_names, PROTO_NONE, PROTO_ESP,
	"PROTO_NONE",
	"IKE",
	"AH",
	"ESP",
);

ENUM_BEGIN(transform_type_names, UNDEFINED_TRANSFORM_TYPE, UNDEFINED_TRANSFORM_TYPE, 
	"UNDEFINED_TRANSFORM_TYPE");
ENUM_NEXT(transform_type_names, ENCRYPTION_ALGORITHM, EXTENDED_SEQUENCE_NUMBERS, UNDEFINED_TRANSFORM_TYPE,
	"ENCRYPTION_ALGORITHM",
	"PSEUDO_RANDOM_FUNCTION",
	"INTEGRITY_ALGORITHM",
	"DIFFIE_HELLMAN_GROUP",
	"EXTENDED_SEQUENCE_NUMBERS");
ENUM_END(transform_type_names, EXTENDED_SEQUENCE_NUMBERS);

ENUM(extended_sequence_numbers_names, NO_EXT_SEQ_NUMBERS, EXT_SEQ_NUMBERS,
	"NO_EXT_SEQ",
	"EXT_SEQ",
);

typedef struct private_proposal_t private_proposal_t;
typedef struct algorithm_t algorithm_t;

/**
 * Private data of an proposal_t object
 */
struct private_proposal_t {

	/**
	 * Public part
	 */
	proposal_t public;
	
	/**
	 * protocol (ESP or AH)
	 */
	protocol_id_t protocol;
	
	/**
	 * priority ordered list of encryption algorithms
	 */
	linked_list_t *encryption_algos;
	
	/**
	 * priority ordered list of integrity algorithms
	 */
	linked_list_t *integrity_algos;
	
	/**
	 * priority ordered list of pseudo random functions
	 */
	linked_list_t *prf_algos;
	
	/**
	 * priority ordered list of dh groups
	 */
	linked_list_t *dh_groups;
	
	/**
	 * priority ordered list of extended sequence number flags
	 */
	linked_list_t *esns;
	
	/** 
	 * senders SPI
	 */
	u_int64_t spi;
};

/**
 * Struct used to store different kinds of algorithms. 
 */
struct algorithm_t {
	/**
	 * Value from an encryption_algorithm_t/integrity_algorithm_t/...
	 */
	u_int16_t algorithm;
	
	/**
	 * the associated key size in bits, or zero if not needed
	 */
	u_int16_t key_size;
};

/**
 * Add algorithm/keysize to a algorithm list
 */
static void add_algo(linked_list_t *list, u_int16_t algo, u_int16_t key_size)
{
	algorithm_t *algo_key;
	
	algo_key = malloc_thing(algorithm_t);
	algo_key->algorithm = algo;
	algo_key->key_size = key_size;
	list->insert_last(list, (void*)algo_key);
}

/**
 * Implements proposal_t.add_algorithm
 */
static void add_algorithm(private_proposal_t *this, transform_type_t type,
						  u_int16_t algo, u_int16_t key_size)
{
	switch (type)
	{
		case ENCRYPTION_ALGORITHM:
			add_algo(this->encryption_algos, algo, key_size);
			break;
		case INTEGRITY_ALGORITHM:
			add_algo(this->integrity_algos, algo, key_size);
			break;
		case PSEUDO_RANDOM_FUNCTION:
			add_algo(this->prf_algos, algo, key_size);
			break;
		case DIFFIE_HELLMAN_GROUP:
			add_algo(this->dh_groups, algo, 0);
			break;
		case EXTENDED_SEQUENCE_NUMBERS:
			add_algo(this->esns, algo, 0);
			break;
		default:
			break;
	}
}

/**
 * filter function for peer configs
 */
static bool alg_filter(void *null, algorithm_t **in, u_int16_t *alg,
					   void **unused, u_int16_t *key_size)
{
	algorithm_t *algo = *in;
	*alg = algo->algorithm;
	if (key_size)
	{
		*key_size = algo->key_size;
	}
	return TRUE;
}

/**
 * Implements proposal_t.create_enumerator.
 */
static enumerator_t *create_enumerator(private_proposal_t *this,
									   transform_type_t type)
{
	linked_list_t *list;

	switch (type)
	{
		case ENCRYPTION_ALGORITHM:
			list = this->encryption_algos;
			break;
		case INTEGRITY_ALGORITHM:
			list = this->integrity_algos;
			break;
		case PSEUDO_RANDOM_FUNCTION:
			list = this->prf_algos;
			break;
		case DIFFIE_HELLMAN_GROUP:
			list = this->dh_groups;
			break;
		case EXTENDED_SEQUENCE_NUMBERS:
			list = this->esns;
			break;
		default:
			return NULL;
	}
	return enumerator_create_filter(list->create_enumerator(list),
									(void*)alg_filter, NULL, NULL);
}

/**
 * Implements proposal_t.get_algorithm.
 */
static bool get_algorithm(private_proposal_t *this, transform_type_t type,
						  u_int16_t *alg, u_int16_t *key_size)
{
	enumerator_t *enumerator;
	bool found = FALSE;
	
	enumerator = create_enumerator(this, type);
	if (enumerator->enumerate(enumerator, alg, key_size))
	{
		found = TRUE;
	}
	enumerator->destroy(enumerator);
	return found;
}

/**
 * Implements proposal_t.has_dh_group
 */
static bool has_dh_group(private_proposal_t *this, diffie_hellman_group_t group)
{
	bool result = FALSE;
	
	if (this->dh_groups->get_count(this->dh_groups))
	{
		algorithm_t *current;
		enumerator_t *enumerator;
		
		enumerator = this->dh_groups->create_enumerator(this->dh_groups);
		while (enumerator->enumerate(enumerator, (void**)&current))
		{
			if (current->algorithm == group)
			{
				result = TRUE;
				break;
			}
		}
		enumerator->destroy(enumerator);
	}
	else if (group == MODP_NONE)
	{
		result = TRUE;
	}
	return result;
}

/**
 * Implementation of proposal_t.strip_dh.
 */
static void strip_dh(private_proposal_t *this)
{
	algorithm_t *alg;
	
	while (this->dh_groups->remove_last(this->dh_groups, (void**)&alg) == SUCCESS)
	{
		free(alg);
	}
}

/**
 * Returns true if the given alg is an authenticated encryption algorithm
 */
static bool is_authenticated_encryption(u_int16_t alg)
{
	switch(alg)
	{
		case ENCR_AES_CCM_ICV8:
		case ENCR_AES_CCM_ICV12:
		case ENCR_AES_CCM_ICV16:
		case ENCR_AES_GCM_ICV8:
		case ENCR_AES_GCM_ICV12:
		case ENCR_AES_GCM_ICV16:
			return TRUE;
	}
	return FALSE;
}

/**
 * Find a matching alg/keysize in two linked lists
 */
static bool select_algo(linked_list_t *first, linked_list_t *second, bool *add,
						u_int16_t *alg, size_t *key_size)
{
	enumerator_t *e1, *e2;
	algorithm_t *alg1, *alg2;
	
	/* if in both are zero algorithms specified, we HAVE a match */
	if (first->get_count(first) == 0 && second->get_count(second) == 0)
	{
		*add = FALSE;
		return TRUE;
	}
	
	e1 = first->create_enumerator(first);
	e2 = second->create_enumerator(second);
	/* compare algs, order of algs in "first" is preferred */
	while (e1->enumerate(e1, &alg1))
	{
		e2->destroy(e2);
		e2 = second->create_enumerator(second);
		while (e2->enumerate(e2, &alg2))
		{
			if (alg1->algorithm == alg2->algorithm &&
				alg1->key_size == alg2->key_size)
			{
				/* ok, we have an algorithm */
				*alg = alg1->algorithm;
				*key_size = alg1->key_size;
				*add = TRUE;
				e1->destroy(e1);
				e2->destroy(e2);
				return TRUE;
			}
		}
	}
	/* no match in all comparisons */
	e1->destroy(e1);
	e2->destroy(e2);
	return FALSE;
}

/**
 * Implements proposal_t.select.
 */
static proposal_t *select_proposal(private_proposal_t *this, private_proposal_t *other)
{
	proposal_t *selected;
	u_int16_t algo;
	size_t key_size;
	bool add;
	
	DBG2(DBG_CFG, "selecting proposal:");
	
	/* check protocol */
	if (this->protocol != other->protocol)
	{
		DBG2(DBG_CFG, "  protocol mismatch, skipping");
		return NULL;
	}
	
	selected = proposal_create(this->protocol);
	
	/* select encryption algorithm */
	if (select_algo(this->encryption_algos, other->encryption_algos,
					&add, &algo, &key_size))
	{
		if (add)
		{
			selected->add_algorithm(selected, ENCRYPTION_ALGORITHM,
									algo, key_size);
		}
	}
	else
	{
		selected->destroy(selected);
		DBG2(DBG_CFG, "  no acceptable %N found",
			 transform_type_names, ENCRYPTION_ALGORITHM);
		return NULL;
	}
	/* select integrity algorithm */
	if (!is_authenticated_encryption(algo))
	{
		if (select_algo(this->integrity_algos, other->integrity_algos,	
						&add, &algo, &key_size))
		{
			if (add)
			{
				selected->add_algorithm(selected, INTEGRITY_ALGORITHM,
										algo, key_size);
			}
		}
		else
		{
			selected->destroy(selected);
			DBG2(DBG_CFG, "  no acceptable %N found",
				 transform_type_names, INTEGRITY_ALGORITHM);
			return NULL;
		}
	}
	/* select prf algorithm */
	if (select_algo(this->prf_algos, other->prf_algos,
					&add, &algo, &key_size))
	{
		if (add)
		{
			selected->add_algorithm(selected, PSEUDO_RANDOM_FUNCTION,
									algo, key_size);
		}
	}
	else
	{
		selected->destroy(selected);
		DBG2(DBG_CFG, "  no acceptable %N found",
			 transform_type_names, PSEUDO_RANDOM_FUNCTION);
		return NULL;
	}
	/* select a DH-group */
	if (select_algo(this->dh_groups, other->dh_groups, &add, &algo, &key_size))
	{
		if (add)
		{
			selected->add_algorithm(selected, DIFFIE_HELLMAN_GROUP, algo, 0);
		}
	}
	else
	{
		selected->destroy(selected);
		DBG2(DBG_CFG, "  no acceptable %N found",
			 transform_type_names, DIFFIE_HELLMAN_GROUP);
		return NULL;
	}
	/* select if we use ESNs */
	if (select_algo(this->esns, other->esns, &add, &algo, &key_size))
	{
		if (add)
		{
			selected->add_algorithm(selected, EXTENDED_SEQUENCE_NUMBERS, algo, 0);
		}
	}
	else
	{
		selected->destroy(selected);
		DBG2(DBG_CFG, "  no acceptable %N found",
			 transform_type_names, EXTENDED_SEQUENCE_NUMBERS);
		return NULL;
	}
	DBG2(DBG_CFG, "  proposal matches");
	
	/* apply SPI from "other" */
	selected->set_spi(selected, other->spi);
	
	/* everything matched, return new proposal */
	return selected;
}

/**
 * Implements proposal_t.get_protocols.
 */
static protocol_id_t get_protocol(private_proposal_t *this)
{
	return this->protocol;
}

/**
 * Implements proposal_t.set_spi.
 */
static void set_spi(private_proposal_t *this, u_int64_t spi)
{
	this->spi = spi;
}

/**
 * Implements proposal_t.get_spi.
 */
static u_int64_t get_spi(private_proposal_t *this)
{
	return this->spi;
}

/**
 * Clone a algorithm list
 */
static void clone_algo_list(linked_list_t *list, linked_list_t *clone_list)
{
	algorithm_t *algo, *clone_algo;
	enumerator_t *enumerator;
	
	enumerator = list->create_enumerator(list);
	while (enumerator->enumerate(enumerator, &algo))
	{
		clone_algo = malloc_thing(algorithm_t);
		memcpy(clone_algo, algo, sizeof(algorithm_t));
		clone_list->insert_last(clone_list, (void*)clone_algo);
	}
	enumerator->destroy(enumerator);
}

/**
 * check if an algorithm list equals
 */
static bool algo_list_equals(linked_list_t *l1, linked_list_t *l2)
{
	enumerator_t *e1, *e2;
	algorithm_t *alg1, *alg2;
	bool equals = TRUE;
	
	if (l1->get_count(l1) != l2->get_count(l2))
	{
		return FALSE;
	}
	
	e1 = l1->create_enumerator(l1);
	e2 = l2->create_enumerator(l2);
	while (e1->enumerate(e1, &alg1) && e2->enumerate(e2, &alg2))
	{
		if (alg1->algorithm != alg2->algorithm ||
			alg1->key_size != alg2->key_size)
		{
			equals = FALSE;
			break;
		}
	}
	e1->destroy(e1);
	e2->destroy(e2);
	return equals;
}

/**
 * Implementation of proposal_t.equals.
 */
static bool equals(private_proposal_t *this, private_proposal_t *other)
{
	if (this == other)
	{
		return TRUE;
	}
	if (this->public.equals != other->public.equals)
	{
		return FALSE;
	}
	return (
		algo_list_equals(this->encryption_algos, other->encryption_algos) &&
		algo_list_equals(this->integrity_algos, other->integrity_algos) &&
		algo_list_equals(this->prf_algos, other->prf_algos) &&
		algo_list_equals(this->dh_groups, other->dh_groups) &&
		algo_list_equals(this->esns, other->esns));
}

/**
 * Implements proposal_t.clone
 */
static proposal_t *clone_(private_proposal_t *this)
{
	private_proposal_t *clone = (private_proposal_t*)proposal_create(this->protocol);
	
	clone_algo_list(this->encryption_algos, clone->encryption_algos);
	clone_algo_list(this->integrity_algos, clone->integrity_algos);
	clone_algo_list(this->prf_algos, clone->prf_algos);
	clone_algo_list(this->dh_groups, clone->dh_groups);
	clone_algo_list(this->esns, clone->esns);
	
	clone->spi = this->spi;
	
	return &clone->public;
}

/**
 * Checks the proposal read from a string.
 */
static void check_proposal(private_proposal_t *this)
{
	enumerator_t *e;
	algorithm_t *alg;
	bool all_aead = TRUE;
	
	e = this->encryption_algos->create_enumerator(this->encryption_algos);
	while (e->enumerate(e, &alg))
	{
		if (!is_authenticated_encryption(alg->algorithm))
		{
			all_aead = FALSE;
			break;
		}
	}
	e->destroy(e);
	
	if (all_aead)
	{
		/* if all encryption algorithms in the proposal are authenticated encryption
		 * algorithms we MUST NOT propose any integrity algorithms */
		while (this->integrity_algos->remove_last(this->integrity_algos,
												  (void**)&alg) == SUCCESS)
		{
			free(alg);
		}
	}
}

/**
 * add a algorithm identified by a string to the proposal.
 * TODO: we could use gperf here.
 */
static status_t add_string_algo(private_proposal_t *this, chunk_t alg)
{
	if (strncmp(alg.ptr, "null", alg.len) == 0)
	{
		add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_NULL, 0);
	}
	else if (strncmp(alg.ptr, "aes128", alg.len) == 0)
	{
		add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 128);
	}
	else if (strncmp(alg.ptr, "aes192", alg.len) == 0)
	{
		add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 192);
	}
	else if (strncmp(alg.ptr, "aes256", alg.len) == 0)
	{
		add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 256);
	}
	else if (strstr(alg.ptr, "ccm"))
	{
		u_int16_t key_size, icv_size;

		if (sscanf(alg.ptr, "aes%huccm%hu", &key_size, &icv_size) == 2)
		{
			if (key_size == 128 || key_size == 192 || key_size == 256)
			{
				switch (icv_size)
				{
					case   8: /* octets */
					case  64: /* bits */
						add_algorithm(this, ENCRYPTION_ALGORITHM,
									  ENCR_AES_CCM_ICV8, key_size);
						break;
					case  12: /* octets */
					case  96: /* bits */
						add_algorithm(this, ENCRYPTION_ALGORITHM,
									  ENCR_AES_CCM_ICV12, key_size);
						break;
					case  16: /* octets */
					case 128: /* bits */
						add_algorithm(this, ENCRYPTION_ALGORITHM,
									  ENCR_AES_CCM_ICV16, key_size);
						break;
					default:
						/* invalid ICV size */
						break;
				}
			}
		}
	}
	else if (strstr(alg.ptr, "gcm"))
	{
		u_int16_t key_size, icv_size;

		if (sscanf(alg.ptr, "aes%hugcm%hu", &key_size, &icv_size) == 2)
		{
			if (key_size == 128 || key_size == 192 || key_size == 256)
			{
				switch (icv_size)
				{
					case   8: /* octets */
					case  64: /* bits */
						add_algorithm(this, ENCRYPTION_ALGORITHM,
									  ENCR_AES_GCM_ICV8, key_size);
						break;
					case  12: /* octets */
					case  96: /* bits */
						add_algorithm(this, ENCRYPTION_ALGORITHM,
									  ENCR_AES_GCM_ICV12, key_size);
						break;
					case  16: /* octets */
					case 128: /* bits */
						add_algorithm(this, ENCRYPTION_ALGORITHM,
									  ENCR_AES_GCM_ICV16, key_size);
						break;
					default:
						/* invalid ICV size */
						break;
				}
			}
		}
	}
	else if (strncmp(alg.ptr, "3des", alg.len) == 0)
	{
		add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_3DES, 0);
	}
	/* blowfish only uses some predefined key sizes yet */
	else if (strncmp(alg.ptr, "blowfish128", alg.len) == 0)
	{
		add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_BLOWFISH, 128);
	}
	else if (strncmp(alg.ptr, "blowfish192", alg.len) == 0)
	{
		add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_BLOWFISH, 192);
	}
	else if (strncmp(alg.ptr, "blowfish256", alg.len) == 0)
	{
		add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_BLOWFISH, 256);
	}
	else if (strncmp(alg.ptr, "sha", alg.len) == 0 ||
			 strncmp(alg.ptr, "sha1", alg.len) == 0)
	{
		/* sha means we use SHA for both, PRF and AUTH */
		add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA1_96, 0);
		if (this->protocol == PROTO_IKE)
		{
			add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_SHA1, 0);
		}
	}  
	else if (strncmp(alg.ptr, "sha256", alg.len) == 0 ||
			 strncmp(alg.ptr, "sha2_256", alg.len) == 0)
	{
		add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_256_128, 0);
		if (this->protocol == PROTO_IKE)
		{
			add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_SHA2_256, 0);
		}
	}
	else if (strncmp(alg.ptr, "sha384", alg.len) == 0 ||
			 strncmp(alg.ptr, "sha2_384", alg.len) == 0)
	{
		add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_384_192, 0);
		if (this->protocol == PROTO_IKE)
		{
			add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_SHA2_384, 0);
		}
	}
	else if (strncmp(alg.ptr, "sha512", alg.len) == 0 ||
			 strncmp(alg.ptr, "sha2_512", alg.len) == 0)
	{
		add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA2_512_256, 0);
		if (this->protocol == PROTO_IKE)
		{
			add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_SHA2_512, 0);
		}
	}
	else if (strncmp(alg.ptr, "md5", alg.len) == 0)
	{
		add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_MD5_96, 0);
		if (this->protocol == PROTO_IKE)
		{
			add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_MD5, 0);
		}
	}
	else if (strncmp(alg.ptr, "aesxcbc", alg.len) == 0)
	{
		add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_AES_XCBC_96, 0);
		if (this->protocol == PROTO_IKE)
		{
			add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_AES128_XCBC, 0);
		}
	}
	else if (strncmp(alg.ptr, "modpnull", alg.len) == 0)
	{
		add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_NULL, 0);
	}
	else if (strncmp(alg.ptr, "modp768", alg.len) == 0)
	{
		add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_768_BIT, 0);
	}
	else if (strncmp(alg.ptr, "modp1024", alg.len) == 0)
	{
		add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_1024_BIT, 0);
	}
	else if (strncmp(alg.ptr, "modp1536", alg.len) == 0)
	{
		add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_1536_BIT, 0);
	}
	else if (strncmp(alg.ptr, "modp2048", alg.len) == 0)
	{
		add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_2048_BIT, 0);
	}
	else if (strncmp(alg.ptr, "modp3072", alg.len) == 0)
	{
		add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_3072_BIT, 0);
	}
	else if (strncmp(alg.ptr, "modp4096", alg.len) == 0)
	{
		add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_4096_BIT, 0);
	}
	else if (strncmp(alg.ptr, "modp6144", alg.len) == 0)
	{
		add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_6144_BIT, 0);
	}
	else if (strncmp(alg.ptr, "modp8192", alg.len) == 0)
	{
		add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_8192_BIT, 0);
	}
	else if (strncmp(alg.ptr, "ecp192", alg.len) == 0)
	{
		add_algorithm(this, DIFFIE_HELLMAN_GROUP, ECP_192_BIT, 0);
	}
	else if (strncmp(alg.ptr, "ecp224", alg.len) == 0)
	{
		add_algorithm(this, DIFFIE_HELLMAN_GROUP, ECP_224_BIT, 0);
	}
	else if (strncmp(alg.ptr, "ecp256", alg.len) == 0)
	{
		add_algorithm(this, DIFFIE_HELLMAN_GROUP, ECP_256_BIT, 0);
	}
	else if (strncmp(alg.ptr, "ecp384", alg.len) == 0)
	{
		add_algorithm(this, DIFFIE_HELLMAN_GROUP, ECP_384_BIT, 0);
	}
	else if (strncmp(alg.ptr, "ecp521", alg.len) == 0)
	{
		add_algorithm(this, DIFFIE_HELLMAN_GROUP, ECP_521_BIT, 0);
	}
	else
	{
		return FAILED;
	}
	return SUCCESS;
}

/**
 * print all algorithms of a kind to buffer
 */
static int print_alg(private_proposal_t *this, char **dst, int *len,
					 u_int kind, void *names, bool *first)
{
	enumerator_t *enumerator;
	size_t written = 0;
	u_int16_t alg, size;
	
	enumerator = create_enumerator(this, kind);
	while (enumerator->enumerate(enumerator, &alg, &size))
	{
		if (*first)
		{
			written += print_in_hook(*dst, *len, "%N", names, alg);
			*first = FALSE;
		}
		else
		{
			written += print_in_hook(*dst, *len, "/%N", names, alg);
		}
		if (size)
		{
			written += print_in_hook(*dst, *len, "-%d", size);
		}
	}
	enumerator->destroy(enumerator);
	return written;
}

/**
 * Described in header.
 */
int proposal_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
						 const void *const *args)
{
	private_proposal_t *this = *((private_proposal_t**)(args[0]));
	linked_list_t *list = *((linked_list_t**)(args[0]));
	enumerator_t *enumerator;
	size_t written = 0;
	bool first = TRUE;
	
	if (this == NULL)
	{
		return print_in_hook(dst, len, "(null)");
	}
	
	if (spec->hash)
	{
		enumerator = list->create_enumerator(list);
		while (enumerator->enumerate(enumerator, &this))
		{	/* call recursivly */
			if (first)
			{
				written += print_in_hook(dst, len, "%P", this);
				first = FALSE;
			}
			else
			{
				written += print_in_hook(dst, len, ", %P", this);
			}
		}
		enumerator->destroy(enumerator);
		return written;
	}
	
	written = print_in_hook(dst, len, "%N:", protocol_id_names, this->protocol);
	written += print_alg(this, &dst, &len, ENCRYPTION_ALGORITHM,
						 encryption_algorithm_names, &first);
	written += print_alg(this, &dst, &len, INTEGRITY_ALGORITHM,
						 integrity_algorithm_names, &first);
	written += print_alg(this, &dst, &len, PSEUDO_RANDOM_FUNCTION,
						 pseudo_random_function_names, &first);
	written += print_alg(this, &dst, &len, DIFFIE_HELLMAN_GROUP,
						 diffie_hellman_group_names, &first);
	written += print_alg(this, &dst, &len, EXTENDED_SEQUENCE_NUMBERS,
						 extended_sequence_numbers_names, &first);
	return written;
}

/**
 * Implements proposal_t.destroy.
 */
static void destroy(private_proposal_t *this)
{
	this->encryption_algos->destroy_function(this->encryption_algos, free);
	this->integrity_algos->destroy_function(this->integrity_algos, free);
	this->prf_algos->destroy_function(this->prf_algos, free);
	this->dh_groups->destroy_function(this->dh_groups, free);
	this->esns->destroy_function(this->esns, free);
	free(this);
}

/*
 * Describtion in header-file
 */
proposal_t *proposal_create(protocol_id_t protocol)
{
	private_proposal_t *this = malloc_thing(private_proposal_t);
	
	this->public.add_algorithm = (void (*)(proposal_t*,transform_type_t,u_int16_t,u_int16_t))add_algorithm;
	this->public.create_enumerator = (enumerator_t* (*)(proposal_t*,transform_type_t))create_enumerator;
	this->public.get_algorithm = (bool (*)(proposal_t*,transform_type_t,u_int16_t*,u_int16_t*))get_algorithm;
	this->public.has_dh_group = (bool (*)(proposal_t*,diffie_hellman_group_t))has_dh_group;
	this->public.strip_dh = (void(*)(proposal_t*))strip_dh;
	this->public.select = (proposal_t* (*)(proposal_t*,proposal_t*))select_proposal;
	this->public.get_protocol = (protocol_id_t(*)(proposal_t*))get_protocol;
	this->public.set_spi = (void(*)(proposal_t*,u_int64_t))set_spi;
	this->public.get_spi = (u_int64_t(*)(proposal_t*))get_spi;
	this->public.equals = (bool(*)(proposal_t*, proposal_t *other))equals;
	this->public.clone = (proposal_t*(*)(proposal_t*))clone_;
	this->public.destroy = (void(*)(proposal_t*))destroy;
	
	this->spi = 0;
	this->protocol = protocol;
	
	this->encryption_algos = linked_list_create();
	this->integrity_algos = linked_list_create();
	this->prf_algos = linked_list_create();
	this->dh_groups = linked_list_create();
	this->esns = linked_list_create();
	
	return &this->public;
}

/**
 * Add supported IKE algorithms to proposal
 */
static void proposal_add_supported_ike(private_proposal_t *this)
{
	enumerator_t *enumerator;
	encryption_algorithm_t encryption;
	integrity_algorithm_t integrity;
	pseudo_random_function_t prf;
	diffie_hellman_group_t group;
	
	enumerator = lib->crypto->create_crypter_enumerator(lib->crypto);
	while (enumerator->enumerate(enumerator, &encryption))
	{
		switch (encryption)
		{
			case ENCR_AES_CBC:
				/* we assume that we support all AES sizes */
				add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 128);
				add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 192);
				add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 256);
				break;
			case ENCR_3DES:
			case ENCR_AES_CTR:
			case ENCR_AES_CCM_ICV8:
			case ENCR_AES_CCM_ICV12:
			case ENCR_AES_CCM_ICV16:
			case ENCR_AES_GCM_ICV8:
			case ENCR_AES_GCM_ICV12:
			case ENCR_AES_GCM_ICV16:
				add_algorithm(this, ENCRYPTION_ALGORITHM, encryption, 0);
				break;
			case ENCR_DES:
				/* no, thanks */
				break;
			default:
				break;
		}	
	}
	enumerator->destroy(enumerator);
	
	enumerator = lib->crypto->create_signer_enumerator(lib->crypto);
	while (enumerator->enumerate(enumerator, &integrity))
	{
		switch (integrity)
		{
			case AUTH_HMAC_SHA1_96:
			case AUTH_HMAC_SHA2_256_128:
			case AUTH_HMAC_SHA2_384_192:
			case AUTH_HMAC_SHA2_512_256:
			case AUTH_HMAC_MD5_96:
			case AUTH_AES_XCBC_96:
				add_algorithm(this, INTEGRITY_ALGORITHM, integrity, 0);
				break;
			default:
				break;
		}	
	}
	enumerator->destroy(enumerator);
	
	enumerator = lib->crypto->create_prf_enumerator(lib->crypto);
	while (enumerator->enumerate(enumerator, &prf))
	{
		switch (prf)
		{
			case PRF_HMAC_SHA1:
			case PRF_HMAC_SHA2_256:
			case PRF_HMAC_SHA2_384:
			case PRF_HMAC_SHA2_512:
			case PRF_HMAC_MD5:
			case PRF_AES128_XCBC:
				add_algorithm(this, PSEUDO_RANDOM_FUNCTION, prf, 0);
				break;
			default:
				break;
		}
	}
	enumerator->destroy(enumerator);
	
	enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
	while (enumerator->enumerate(enumerator, &group))
	{
		switch (group)
		{
			case MODP_NULL:
				/* only for testing purposes */
				break;
			case MODP_768_BIT:
				/* weak */
				break;
			case MODP_1024_BIT:
			case MODP_1536_BIT:
			case MODP_2048_BIT:
			case MODP_4096_BIT:
			case MODP_8192_BIT:
			case ECP_256_BIT:
			case ECP_384_BIT:
			case ECP_521_BIT:
			case ECP_192_BIT:
			case ECP_224_BIT:
				add_algorithm(this, DIFFIE_HELLMAN_GROUP, group, 0);
				break;
			default:
				break;
		}
	}
	enumerator->destroy(enumerator);
}

/*
 * Describtion in header-file
 */
proposal_t *proposal_create_default(protocol_id_t protocol)
{
	private_proposal_t *this = (private_proposal_t*)proposal_create(protocol);
	
	switch (protocol)
	{
		case PROTO_IKE:
			proposal_add_supported_ike(this);
			break;
		case PROTO_ESP:
			add_algorithm(this, ENCRYPTION_ALGORITHM,   ENCR_AES_CBC,         128);
			add_algorithm(this, ENCRYPTION_ALGORITHM,   ENCR_AES_CBC,         192);
			add_algorithm(this, ENCRYPTION_ALGORITHM,   ENCR_AES_CBC,         256);
			add_algorithm(this, ENCRYPTION_ALGORITHM,   ENCR_3DES,              0);
			add_algorithm(this, ENCRYPTION_ALGORITHM,   ENCR_BLOWFISH,        256);
			add_algorithm(this, INTEGRITY_ALGORITHM,    AUTH_HMAC_SHA1_96,      0);
			add_algorithm(this, INTEGRITY_ALGORITHM,    AUTH_AES_XCBC_96,       0);
			add_algorithm(this, INTEGRITY_ALGORITHM,    AUTH_HMAC_MD5_96,       0);
			add_algorithm(this, EXTENDED_SEQUENCE_NUMBERS, NO_EXT_SEQ_NUMBERS,  0);
			break;
		case PROTO_AH:
			add_algorithm(this, INTEGRITY_ALGORITHM,    AUTH_HMAC_SHA1_96,      0);
			add_algorithm(this, INTEGRITY_ALGORITHM,    AUTH_AES_XCBC_96,       0);
			add_algorithm(this, INTEGRITY_ALGORITHM,    AUTH_HMAC_MD5_96,       0);
			add_algorithm(this, EXTENDED_SEQUENCE_NUMBERS, NO_EXT_SEQ_NUMBERS,  0);
			break;
		default:
			break;
	}
	return &this->public;
}

/*
 * Describtion in header-file
 */
proposal_t *proposal_create_from_string(protocol_id_t protocol, const char *algs)
{
	private_proposal_t *this = (private_proposal_t*)proposal_create(protocol);
	chunk_t string = {(void*)algs, strlen(algs)};
	chunk_t alg;
	status_t status = SUCCESS;
	
	eat_whitespace(&string);
	if (string.len < 1)
	{
		destroy(this);
		return NULL;
	}
	
	/* get all tokens, separated by '-' */
	while (extract_token(&alg, '-', &string))
	{
		status |= add_string_algo(this, alg);
	}
	if (string.len)
	{
		status |= add_string_algo(this, string);
	}
	if (status != SUCCESS)
	{
		destroy(this);
		return NULL;
	}
	
	check_proposal(this);
	
	if (protocol == PROTO_AH || protocol == PROTO_ESP)
	{
		add_algorithm(this, EXTENDED_SEQUENCE_NUMBERS, NO_EXT_SEQ_NUMBERS, 0);
	}
	return &this->public;
}