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