fixed double free bug
[strongswan.git] / src / charon / config / proposal.c
1 /**
2 * @file proposal.c
3 *
4 * @brief Implementation of proposal_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006 Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <string.h>
24
25 #include "proposal.h"
26
27 #include <daemon.h>
28 #include <utils/linked_list.h>
29 #include <utils/identification.h>
30 #include <utils/lexparser.h>
31 #include <crypto/prfs/prf.h>
32 #include <crypto/crypters/crypter.h>
33 #include <crypto/signers/signer.h>
34
35
36 ENUM(protocol_id_names, PROTO_NONE, PROTO_ESP,
37 "PROTO_NONE",
38 "IKE",
39 "AH",
40 "ESP",
41 );
42
43 ENUM_BEGIN(transform_type_names, UNDEFINED_TRANSFORM_TYPE, UNDEFINED_TRANSFORM_TYPE,
44 "UNDEFINED_TRANSFORM_TYPE");
45 ENUM_NEXT(transform_type_names, ENCRYPTION_ALGORITHM, EXTENDED_SEQUENCE_NUMBERS, UNDEFINED_TRANSFORM_TYPE,
46 "ENCRYPTION_ALGORITHM",
47 "PSEUDO_RANDOM_FUNCTION",
48 "INTEGRITY_ALGORITHM",
49 "DIFFIE_HELLMAN_GROUP",
50 "EXTENDED_SEQUENCE_NUMBERS");
51 ENUM_END(transform_type_names, EXTENDED_SEQUENCE_NUMBERS);
52
53 ENUM(extended_sequence_numbers_names, NO_EXT_SEQ_NUMBERS, EXT_SEQ_NUMBERS,
54 "NO_EXT_SEQ_NUMBERS",
55 "EXT_SEQ_NUMBERS",
56 );
57
58 typedef struct private_proposal_t private_proposal_t;
59
60 /**
61 * Private data of an proposal_t object
62 */
63 struct private_proposal_t {
64
65 /**
66 * Public part
67 */
68 proposal_t public;
69
70 /**
71 * protocol (ESP or AH)
72 */
73 protocol_id_t protocol;
74
75 /**
76 * priority ordered list of encryption algorithms
77 */
78 linked_list_t *encryption_algos;
79
80 /**
81 * priority ordered list of integrity algorithms
82 */
83 linked_list_t *integrity_algos;
84
85 /**
86 * priority ordered list of pseudo random functions
87 */
88 linked_list_t *prf_algos;
89
90 /**
91 * priority ordered list of dh groups
92 */
93 linked_list_t *dh_groups;
94
95 /**
96 * priority ordered list of extended sequence number flags
97 */
98 linked_list_t *esns;
99
100 /**
101 * senders SPI
102 */
103 u_int64_t spi;
104 };
105
106 /**
107 * Add algorithm/keysize to a algorithm list
108 */
109 static void add_algo(linked_list_t *list, u_int16_t algo, size_t key_size)
110 {
111 algorithm_t *algo_key;
112
113 algo_key = malloc_thing(algorithm_t);
114 algo_key->algorithm = algo;
115 algo_key->key_size = key_size;
116 list->insert_last(list, (void*)algo_key);
117 }
118
119 /**
120 * Implements proposal_t.add_algorithm
121 */
122 static void add_algorithm(private_proposal_t *this, transform_type_t type, u_int16_t algo, size_t key_size)
123 {
124 switch (type)
125 {
126 case ENCRYPTION_ALGORITHM:
127 add_algo(this->encryption_algos, algo, key_size);
128 break;
129 case INTEGRITY_ALGORITHM:
130 add_algo(this->integrity_algos, algo, key_size);
131 break;
132 case PSEUDO_RANDOM_FUNCTION:
133 add_algo(this->prf_algos, algo, key_size);
134 break;
135 case DIFFIE_HELLMAN_GROUP:
136 add_algo(this->dh_groups, algo, 0);
137 break;
138 case EXTENDED_SEQUENCE_NUMBERS:
139 add_algo(this->esns, algo, 0);
140 break;
141 default:
142 break;
143 }
144 }
145
146 /**
147 * Implements proposal_t.get_algorithm.
148 */
149 static bool get_algorithm(private_proposal_t *this, transform_type_t type, algorithm_t** algo)
150 {
151 linked_list_t *list;
152 switch (type)
153 {
154 case ENCRYPTION_ALGORITHM:
155 list = this->encryption_algos;
156 break;
157 case INTEGRITY_ALGORITHM:
158 list = this->integrity_algos;
159 break;
160 case PSEUDO_RANDOM_FUNCTION:
161 list = this->prf_algos;
162 break;
163 case DIFFIE_HELLMAN_GROUP:
164 list = this->dh_groups;
165 break;
166 case EXTENDED_SEQUENCE_NUMBERS:
167 list = this->esns;
168 break;
169 default:
170 return FALSE;
171 }
172 if (list->get_first(list, (void**)algo) != SUCCESS)
173 {
174 return FALSE;
175 }
176 return TRUE;
177 }
178
179 /**
180 * Implements proposal_t.create_algorithm_iterator.
181 */
182 static iterator_t *create_algorithm_iterator(private_proposal_t *this, transform_type_t type)
183 {
184 switch (type)
185 {
186 case ENCRYPTION_ALGORITHM:
187 return this->encryption_algos->create_iterator(this->encryption_algos, TRUE);
188 case INTEGRITY_ALGORITHM:
189 return this->integrity_algos->create_iterator(this->integrity_algos, TRUE);
190 case PSEUDO_RANDOM_FUNCTION:
191 return this->prf_algos->create_iterator(this->prf_algos, TRUE);
192 case DIFFIE_HELLMAN_GROUP:
193 return this->dh_groups->create_iterator(this->dh_groups, TRUE);
194 case EXTENDED_SEQUENCE_NUMBERS:
195 return this->esns->create_iterator(this->esns, TRUE);
196 default:
197 break;
198 }
199 return NULL;
200 }
201
202 /**
203 * Find a matching alg/keysize in two linked lists
204 */
205 static bool select_algo(linked_list_t *first, linked_list_t *second, bool *add, u_int16_t *alg, size_t *key_size)
206 {
207 iterator_t *first_iter, *second_iter;
208 algorithm_t *first_alg, *second_alg;
209
210 /* if in both are zero algorithms specified, we HAVE a match */
211 if (first->get_count(first) == 0 && second->get_count(second) == 0)
212 {
213 *add = FALSE;
214 return TRUE;
215 }
216
217 first_iter = first->create_iterator(first, TRUE);
218 second_iter = second->create_iterator(second, TRUE);
219 /* compare algs, order of algs in "first" is preferred */
220 while (first_iter->iterate(first_iter, (void**)&first_alg))
221 {
222 second_iter->reset(second_iter);
223 while (second_iter->iterate(second_iter, (void**)&second_alg))
224 {
225 DBG2(DBG_CFG, "comparing algo %d - %d, keylen %d - %d",
226 first_alg->algorithm, second_alg->algorithm,
227 first_alg->key_size, second_alg->key_size);
228 if (first_alg->algorithm == second_alg->algorithm &&
229 first_alg->key_size == second_alg->key_size)
230 {
231 /* ok, we have an algorithm */
232 *alg = first_alg->algorithm;
233 *key_size = first_alg->key_size;
234 *add = TRUE;
235 first_iter->destroy(first_iter);
236 second_iter->destroy(second_iter);
237 return TRUE;
238 }
239 }
240 }
241 /* no match in all comparisons */
242 first_iter->destroy(first_iter);
243 second_iter->destroy(second_iter);
244 return FALSE;
245 }
246
247 /**
248 * Implements proposal_t.select.
249 */
250 static proposal_t *select_proposal(private_proposal_t *this, private_proposal_t *other)
251 {
252 proposal_t *selected;
253 u_int16_t algo;
254 size_t key_size;
255 bool add;
256
257 DBG2(DBG_CFG, "selecting proposal:");
258
259 /* check protocol */
260 if (this->protocol != other->protocol)
261 {
262 DBG2(DBG_CFG, " protocol mismatch, skipping");
263 return NULL;
264 }
265
266 selected = proposal_create(this->protocol);
267
268 /* select encryption algorithm */
269 if (select_algo(this->encryption_algos, other->encryption_algos, &add, &algo, &key_size))
270 {
271 if (add)
272 {
273 selected->add_algorithm(selected, ENCRYPTION_ALGORITHM, algo, key_size);
274 }
275 }
276 else
277 {
278 selected->destroy(selected);
279 DBG2(DBG_CFG, " no acceptable ENCRYPTION_ALGORITHM found contained %d - %d, skipping",
280 this->encryption_algos->get_count(this->encryption_algos), other->encryption_algos->get_count(other->encryption_algos));
281 return NULL;
282 }
283 /* select integrity algorithm */
284 if (select_algo(this->integrity_algos, other->integrity_algos, &add, &algo, &key_size))
285 {
286 if (add)
287 {
288 selected->add_algorithm(selected, INTEGRITY_ALGORITHM, algo, key_size);
289 }
290 }
291 else
292 {
293 selected->destroy(selected);
294 DBG2(DBG_CFG, " no acceptable INTEGRITY_ALGORITHM found, skipping");
295 return NULL;
296 }
297 /* select prf algorithm */
298 if (select_algo(this->prf_algos, other->prf_algos, &add, &algo, &key_size))
299 {
300 if (add)
301 {
302 selected->add_algorithm(selected, PSEUDO_RANDOM_FUNCTION, algo, key_size);
303 }
304 }
305 else
306 {
307 selected->destroy(selected);
308 DBG2(DBG_CFG, " no acceptable PSEUDO_RANDOM_FUNCTION found, skipping");
309 return NULL;
310 }
311 /* select a DH-group */
312 if (select_algo(this->dh_groups, other->dh_groups, &add, &algo, &key_size))
313 {
314 if (add)
315 {
316 selected->add_algorithm(selected, DIFFIE_HELLMAN_GROUP, algo, 0);
317 }
318 }
319 else
320 {
321 selected->destroy(selected);
322 DBG2(DBG_CFG, " no acceptable DIFFIE_HELLMAN_GROUP found, skipping");
323 return NULL;
324 }
325 /* select if we use ESNs */
326 if (select_algo(this->esns, other->esns, &add, &algo, &key_size))
327 {
328 if (add)
329 {
330 selected->add_algorithm(selected, EXTENDED_SEQUENCE_NUMBERS, algo, 0);
331 }
332 }
333 else
334 {
335 selected->destroy(selected);
336 DBG2(DBG_CFG, " no acceptable EXTENDED_SEQUENCE_NUMBERS found, skipping");
337 return NULL;
338 }
339 DBG2(DBG_CFG, " proposal matches");
340
341 /* apply SPI from "other" */
342 selected->set_spi(selected, other->spi);
343
344 /* everything matched, return new proposal */
345 return selected;
346 }
347
348 /**
349 * Implements proposal_t.get_protocols.
350 */
351 static protocol_id_t get_protocol(private_proposal_t *this)
352 {
353 return this->protocol;
354 }
355
356 /**
357 * Implements proposal_t.set_spi.
358 */
359 static void set_spi(private_proposal_t *this, u_int64_t spi)
360 {
361 this->spi = spi;
362 }
363
364 /**
365 * Implements proposal_t.get_spi.
366 */
367 static u_int64_t get_spi(private_proposal_t *this)
368 {
369 return this->spi;
370 }
371
372 /**
373 * Clone a algorithm list
374 */
375 static void clone_algo_list(linked_list_t *list, linked_list_t *clone_list)
376 {
377 algorithm_t *algo, *clone_algo;
378 iterator_t *iterator = list->create_iterator(list, TRUE);
379 while (iterator->iterate(iterator, (void**)&algo))
380 {
381 clone_algo = malloc_thing(algorithm_t);
382 memcpy(clone_algo, algo, sizeof(algorithm_t));
383 clone_list->insert_last(clone_list, (void*)clone_algo);
384 }
385 iterator->destroy(iterator);
386 }
387
388 /**
389 * Implements proposal_t.clone
390 */
391 static proposal_t *clone_(private_proposal_t *this)
392 {
393 private_proposal_t *clone = (private_proposal_t*)proposal_create(this->protocol);
394
395 clone_algo_list(this->encryption_algos, clone->encryption_algos);
396 clone_algo_list(this->integrity_algos, clone->integrity_algos);
397 clone_algo_list(this->prf_algos, clone->prf_algos);
398 clone_algo_list(this->dh_groups, clone->dh_groups);
399 clone_algo_list(this->esns, clone->esns);
400
401 clone->spi = this->spi;
402
403 return &clone->public;
404 }
405
406 static status_t add_string_algo(private_proposal_t *this, chunk_t alg)
407 {
408 if (strncmp(alg.ptr, "null", alg.len) == 0)
409 {
410 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_NULL, 0);
411 }
412 else if (strncmp(alg.ptr, "aes128", alg.len) == 0)
413 {
414 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 128);
415 }
416 else if (strncmp(alg.ptr, "aes192", alg.len) == 0)
417 {
418 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 192);
419 }
420 else if (strncmp(alg.ptr, "aes256", alg.len) == 0)
421 {
422 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 256);
423 }
424 else if (strncmp(alg.ptr, "3des", alg.len) == 0)
425 {
426 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_3DES, 0);
427 }
428 /* blowfish only uses some predefined key sizes yet */
429 else if (strncmp(alg.ptr, "blowfish128", alg.len) == 0)
430 {
431 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_BLOWFISH, 128);
432 }
433 else if (strncmp(alg.ptr, "blowfish192", alg.len) == 0)
434 {
435 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_BLOWFISH, 192);
436 }
437 else if (strncmp(alg.ptr, "blowfish256", alg.len) == 0)
438 {
439 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_BLOWFISH, 256);
440 }
441 else if (strncmp(alg.ptr, "sha", alg.len) == 0 ||
442 strncmp(alg.ptr, "sha1", alg.len) == 0)
443 {
444 /* sha means we use SHA for both, PRF and AUTH */
445 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA1_96, 0);
446 if (this->protocol == PROTO_IKE)
447 {
448 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_SHA1, 0);
449 }
450 }
451 else if (strncmp(alg.ptr, "md5", alg.len) == 0)
452 {
453 /* same for MD5 */
454 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_MD5_96, 0);
455 if (this->protocol == PROTO_IKE)
456 {
457 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_MD5, 0);
458 }
459 }
460 else if (strncmp(alg.ptr, "modp768", alg.len) == 0)
461 {
462 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_768_BIT, 0);
463 }
464 else if (strncmp(alg.ptr, "modp1024", alg.len) == 0)
465 {
466 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_1024_BIT, 0);
467 }
468 else if (strncmp(alg.ptr, "modp1536", alg.len) == 0)
469 {
470 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_1536_BIT, 0);
471 }
472 else if (strncmp(alg.ptr, "modp2048", alg.len) == 0)
473 {
474 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_2048_BIT, 0);
475 }
476 else if (strncmp(alg.ptr, "modp4096", alg.len) == 0)
477 {
478 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_4096_BIT, 0);
479 }
480 else if (strncmp(alg.ptr, "modp8192", alg.len) == 0)
481 {
482 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_8192_BIT, 0);
483 }
484 else
485 {
486 return FAILED;
487 }
488 return SUCCESS;
489 }
490
491 /**
492 * Implements proposal_t.destroy.
493 */
494 static void destroy(private_proposal_t *this)
495 {
496 this->encryption_algos->destroy_function(this->encryption_algos, free);
497 this->integrity_algos->destroy_function(this->integrity_algos, free);
498 this->prf_algos->destroy_function(this->prf_algos, free);
499 this->dh_groups->destroy_function(this->dh_groups, free);
500 this->esns->destroy_function(this->esns, free);
501 free(this);
502 }
503
504 /*
505 * Describtion in header-file
506 */
507 proposal_t *proposal_create(protocol_id_t protocol)
508 {
509 private_proposal_t *this = malloc_thing(private_proposal_t);
510
511 this->public.add_algorithm = (void (*)(proposal_t*,transform_type_t,u_int16_t,size_t))add_algorithm;
512 this->public.create_algorithm_iterator = (iterator_t* (*)(proposal_t*,transform_type_t))create_algorithm_iterator;
513 this->public.get_algorithm = (bool (*)(proposal_t*,transform_type_t,algorithm_t**))get_algorithm;
514 this->public.select = (proposal_t* (*)(proposal_t*,proposal_t*))select_proposal;
515 this->public.get_protocol = (protocol_id_t(*)(proposal_t*))get_protocol;
516 this->public.set_spi = (void(*)(proposal_t*,u_int64_t))set_spi;
517 this->public.get_spi = (u_int64_t(*)(proposal_t*))get_spi;
518 this->public.clone = (proposal_t*(*)(proposal_t*))clone_;
519 this->public.destroy = (void(*)(proposal_t*))destroy;
520
521 this->spi = 0;
522 this->protocol = protocol;
523
524 this->encryption_algos = linked_list_create();
525 this->integrity_algos = linked_list_create();
526 this->prf_algos = linked_list_create();
527 this->dh_groups = linked_list_create();
528 this->esns = linked_list_create();
529
530 return &this->public;
531 }
532
533 /*
534 * Describtion in header-file
535 */
536 proposal_t *proposal_create_default(protocol_id_t protocol)
537 {
538 private_proposal_t *this = (private_proposal_t*)proposal_create(protocol);
539
540 switch (protocol)
541 {
542 case PROTO_IKE:
543 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 128);
544 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_3DES, 0);
545 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA1_96, 0);
546 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_MD5_96, 0);
547 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_SHA1, 0);
548 add_algorithm(this, PSEUDO_RANDOM_FUNCTION, PRF_HMAC_MD5, 0);
549 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_2048_BIT, 0);
550 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_1536_BIT, 0);
551 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_1024_BIT, 0);
552 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_4096_BIT, 0);
553 add_algorithm(this, DIFFIE_HELLMAN_GROUP, MODP_8192_BIT, 0);
554 break;
555 case PROTO_ESP:
556 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 128);
557 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 192);
558 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 256);
559 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_3DES, 0);
560 add_algorithm(this, ENCRYPTION_ALGORITHM, ENCR_BLOWFISH, 256);
561 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA1_96, 0);
562 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_MD5_96, 0);
563 add_algorithm(this, EXTENDED_SEQUENCE_NUMBERS, NO_EXT_SEQ_NUMBERS, 0);
564 break;
565 case PROTO_AH:
566 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA1_96, 0);
567 add_algorithm(this, INTEGRITY_ALGORITHM, AUTH_HMAC_MD5_96, 0);
568 add_algorithm(this, EXTENDED_SEQUENCE_NUMBERS, NO_EXT_SEQ_NUMBERS, 0);
569 break;
570 default:
571 break;
572 }
573
574 return &this->public;
575 }
576
577 /*
578 * Describtion in header-file
579 */
580 proposal_t *proposal_create_from_string(protocol_id_t protocol, const char *algs)
581 {
582 private_proposal_t *this = (private_proposal_t*)proposal_create(protocol);
583 chunk_t string = {(void*)algs, strlen(algs)};
584 chunk_t alg;
585 status_t status = SUCCESS;
586
587 eat_whitespace(&string);
588 if (string.len < 1)
589 {
590 destroy(this);
591 return NULL;
592 }
593
594 /* get all tokens, separated by '-' */
595 while (extract_token(&alg, '-', &string))
596 {
597 status |= add_string_algo(this, alg);
598 }
599 if (string.len)
600 {
601 status |= add_string_algo(this, string);
602 }
603 if (status != SUCCESS)
604 {
605 destroy(this);
606 return NULL;
607 }
608
609 if (protocol == PROTO_AH || protocol == PROTO_ESP)
610 {
611 add_algorithm(this, EXTENDED_SEQUENCE_NUMBERS, NO_EXT_SEQ_NUMBERS, 0);
612 }
613 return &this->public;
614 }