Removed strayed code fragment
[strongswan.git] / src / charon / sa / tasks / ike_init.c
1 /*
2 * Copyright (C) 2008-2009 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
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 "ike_init.h"
19
20 #include <string.h>
21
22 #include <daemon.h>
23 #include <crypto/diffie_hellman.h>
24 #include <encoding/payloads/sa_payload.h>
25 #include <encoding/payloads/ke_payload.h>
26 #include <encoding/payloads/nonce_payload.h>
27
28 /** maximum retries to do with cookies/other dh groups */
29 #define MAX_RETRIES 5
30
31 typedef struct private_ike_init_t private_ike_init_t;
32
33 /**
34 * Private members of a ike_init_t task.
35 */
36 struct private_ike_init_t {
37
38 /**
39 * Public methods and task_t interface.
40 */
41 ike_init_t public;
42
43 /**
44 * Assigned IKE_SA.
45 */
46 ike_sa_t *ike_sa;
47
48 /**
49 * Are we the initiator?
50 */
51 bool initiator;
52
53 /**
54 * IKE config to establish
55 */
56 ike_cfg_t *config;
57
58 /**
59 * diffie hellman group to use
60 */
61 diffie_hellman_group_t dh_group;
62
63 /**
64 * diffie hellman key exchange
65 */
66 diffie_hellman_t *dh;
67
68 /**
69 * Keymat derivation (from IKE_SA)
70 */
71 keymat_t *keymat;
72
73 /**
74 * nonce chosen by us
75 */
76 chunk_t my_nonce;
77
78 /**
79 * nonce chosen by peer
80 */
81 chunk_t other_nonce;
82
83 /**
84 * Negotiated proposal used for IKE_SA
85 */
86 proposal_t *proposal;
87
88 /**
89 * Old IKE_SA which gets rekeyed
90 */
91 ike_sa_t *old_sa;
92
93 /**
94 * cookie received from responder
95 */
96 chunk_t cookie;
97
98 /**
99 * retries done so far after failure (cookie or bad dh group)
100 */
101 u_int retry;
102 };
103
104 /**
105 * build the payloads for the message
106 */
107 static void build_payloads(private_ike_init_t *this, message_t *message)
108 {
109 sa_payload_t *sa_payload;
110 ke_payload_t *ke_payload;
111 nonce_payload_t *nonce_payload;
112 linked_list_t *proposal_list;
113 ike_sa_id_t *id;
114 proposal_t *proposal;
115 iterator_t *iterator;
116
117 id = this->ike_sa->get_id(this->ike_sa);
118
119 this->config = this->ike_sa->get_ike_cfg(this->ike_sa);
120
121 if (this->initiator)
122 {
123 proposal_list = this->config->get_proposals(this->config);
124 if (this->old_sa)
125 {
126 /* include SPI of new IKE_SA when we are rekeying */
127 iterator = proposal_list->create_iterator(proposal_list, TRUE);
128 while (iterator->iterate(iterator, (void**)&proposal))
129 {
130 proposal->set_spi(proposal, id->get_initiator_spi(id));
131 }
132 iterator->destroy(iterator);
133 }
134
135 sa_payload = sa_payload_create_from_proposal_list(proposal_list);
136 proposal_list->destroy_offset(proposal_list, offsetof(proposal_t, destroy));
137 }
138 else
139 {
140 if (this->old_sa)
141 {
142 /* include SPI of new IKE_SA when we are rekeying */
143 this->proposal->set_spi(this->proposal, id->get_responder_spi(id));
144 }
145 sa_payload = sa_payload_create_from_proposal(this->proposal);
146 }
147 message->add_payload(message, (payload_t*)sa_payload);
148
149 nonce_payload = nonce_payload_create();
150 nonce_payload->set_nonce(nonce_payload, this->my_nonce);
151 ke_payload = ke_payload_create_from_diffie_hellman(this->dh);
152
153 if (this->old_sa)
154 { /* payload order differs if we are rekeying */
155 message->add_payload(message, (payload_t*)nonce_payload);
156 message->add_payload(message, (payload_t*)ke_payload);
157 }
158 else
159 {
160 message->add_payload(message, (payload_t*)ke_payload);
161 message->add_payload(message, (payload_t*)nonce_payload);
162 }
163 }
164
165 /**
166 * Read payloads from message
167 */
168 static void process_payloads(private_ike_init_t *this, message_t *message)
169 {
170 enumerator_t *enumerator;
171 payload_t *payload;
172
173 enumerator = message->create_payload_enumerator(message);
174 while (enumerator->enumerate(enumerator, &payload))
175 {
176 switch (payload->get_type(payload))
177 {
178 case SECURITY_ASSOCIATION:
179 {
180 sa_payload_t *sa_payload = (sa_payload_t*)payload;
181 linked_list_t *proposal_list;
182 bool private;
183
184 proposal_list = sa_payload->get_proposals(sa_payload);
185 private = this->ike_sa->supports_extension(this->ike_sa,
186 EXT_STRONGSWAN);
187 this->proposal = this->config->select_proposal(this->config,
188 proposal_list, private);
189 proposal_list->destroy_offset(proposal_list,
190 offsetof(proposal_t, destroy));
191 break;
192 }
193 case KEY_EXCHANGE:
194 {
195 ke_payload_t *ke_payload = (ke_payload_t*)payload;
196
197 this->dh_group = ke_payload->get_dh_group_number(ke_payload);
198 if (!this->initiator)
199 {
200 this->dh = this->keymat->create_dh(this->keymat,
201 this->dh_group);
202 }
203 if (this->dh)
204 {
205 this->dh->set_other_public_value(this->dh,
206 ke_payload->get_key_exchange_data(ke_payload));
207 }
208 break;
209 }
210 case NONCE:
211 {
212 nonce_payload_t *nonce_payload = (nonce_payload_t*)payload;
213
214 this->other_nonce = nonce_payload->get_nonce(nonce_payload);
215 break;
216 }
217 default:
218 break;
219 }
220 }
221 enumerator->destroy(enumerator);
222 }
223
224 /**
225 * Implementation of task_t.process for initiator
226 */
227 static status_t build_i(private_ike_init_t *this, message_t *message)
228 {
229 rng_t *rng;
230
231 this->config = this->ike_sa->get_ike_cfg(this->ike_sa);
232 DBG0(DBG_IKE, "initiating IKE_SA %s[%d] to %H",
233 this->ike_sa->get_name(this->ike_sa),
234 this->ike_sa->get_unique_id(this->ike_sa),
235 this->ike_sa->get_other_host(this->ike_sa));
236 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
237
238 if (this->retry >= MAX_RETRIES)
239 {
240 DBG1(DBG_IKE, "giving up after %d retries", MAX_RETRIES);
241 return FAILED;
242 }
243
244 /* if the DH group is set via use_dh_group(), we already have a DH object */
245 if (!this->dh)
246 {
247 this->dh_group = this->config->get_dh_group(this->config);
248 this->dh = this->keymat->create_dh(this->keymat, this->dh_group);
249 if (!this->dh)
250 {
251 DBG1(DBG_IKE, "configured DH group %N not supported",
252 diffie_hellman_group_names, this->dh_group);
253 return FAILED;
254 }
255 }
256
257 /* generate nonce only when we are trying the first time */
258 if (this->my_nonce.ptr == NULL)
259 {
260 rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
261 if (!rng)
262 {
263 DBG1(DBG_IKE, "error generating nonce");
264 return FAILED;
265 }
266 rng->allocate_bytes(rng, NONCE_SIZE, &this->my_nonce);
267 rng->destroy(rng);
268 }
269
270 if (this->cookie.ptr)
271 {
272 message->add_notify(message, FALSE, COOKIE, this->cookie);
273 }
274
275 build_payloads(this, message);
276
277 #ifdef ME
278 {
279 chunk_t connect_id = this->ike_sa->get_connect_id(this->ike_sa);
280 if (connect_id.ptr)
281 {
282 message->add_notify(message, FALSE, ME_CONNECTID, connect_id);
283 }
284 }
285 #endif /* ME */
286
287 return NEED_MORE;
288 }
289
290 /**
291 * Implementation of task_t.process for responder
292 */
293 static status_t process_r(private_ike_init_t *this, message_t *message)
294 {
295 rng_t *rng;
296
297 this->config = this->ike_sa->get_ike_cfg(this->ike_sa);
298 DBG0(DBG_IKE, "%H is initiating an IKE_SA", message->get_source(message));
299 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
300
301 rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
302 if (!rng)
303 {
304 DBG1(DBG_IKE, "error generating nonce");
305 return FAILED;
306 }
307 rng->allocate_bytes(rng, NONCE_SIZE, &this->my_nonce);
308 rng->destroy(rng);
309
310 #ifdef ME
311 {
312 notify_payload_t *notify = message->get_notify(message, ME_CONNECTID);
313 if (notify)
314 {
315 chunk_t connect_id = notify->get_notification_data(notify);
316 DBG2(DBG_IKE, "received ME_CONNECTID %#B", &connect_id);
317 charon->connect_manager->stop_checks(charon->connect_manager,
318 connect_id);
319 }
320 }
321 #endif /* ME */
322
323 process_payloads(this, message);
324
325 return NEED_MORE;
326 }
327
328 /**
329 * Derive the keymat for the IKE_SA
330 */
331 static bool derive_keys(private_ike_init_t *this,
332 chunk_t nonce_i, chunk_t nonce_r)
333 {
334 keymat_t *old_keymat;
335 pseudo_random_function_t prf_alg = PRF_UNDEFINED;
336 chunk_t skd = chunk_empty;
337 ike_sa_id_t *id;
338
339 id = this->ike_sa->get_id(this->ike_sa);
340 if (this->old_sa)
341 {
342 /* rekeying: Include old SKd, use old PRF, apply SPI */
343 old_keymat = this->old_sa->get_keymat(this->old_sa);
344 prf_alg = old_keymat->get_skd(old_keymat, &skd);
345 if (this->initiator)
346 {
347 id->set_responder_spi(id, this->proposal->get_spi(this->proposal));
348 }
349 else
350 {
351 id->set_initiator_spi(id, this->proposal->get_spi(this->proposal));
352 }
353 }
354 if (!this->keymat->derive_ike_keys(this->keymat, this->proposal, this->dh,
355 nonce_i, nonce_r, id, prf_alg, skd))
356 {
357 return FALSE;
358 }
359 charon->bus->ike_keys(charon->bus, this->ike_sa, this->dh,
360 nonce_i, nonce_r, this->old_sa);
361 return TRUE;
362 }
363
364 /**
365 * Implementation of task_t.build for responder
366 */
367 static status_t build_r(private_ike_init_t *this, message_t *message)
368 {
369 /* check if we have everything we need */
370 if (this->proposal == NULL ||
371 this->other_nonce.len == 0 || this->my_nonce.len == 0)
372 {
373 DBG1(DBG_IKE, "received proposals inacceptable");
374 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
375 return FAILED;
376 }
377 this->ike_sa->set_proposal(this->ike_sa, this->proposal);
378
379 if (this->dh == NULL ||
380 !this->proposal->has_dh_group(this->proposal, this->dh_group))
381 {
382 u_int16_t group;
383
384 if (this->proposal->get_algorithm(this->proposal, DIFFIE_HELLMAN_GROUP,
385 &group, NULL))
386 {
387 DBG1(DBG_IKE, "DH group %N inacceptable, requesting %N",
388 diffie_hellman_group_names, this->dh_group,
389 diffie_hellman_group_names, group);
390 this->dh_group = group;
391 group = htons(group);
392 message->add_notify(message, FALSE, INVALID_KE_PAYLOAD,
393 chunk_from_thing(group));
394 }
395 else
396 {
397 DBG1(DBG_IKE, "no acceptable proposal found");
398 }
399 return FAILED;
400 }
401
402 if (!derive_keys(this, this->other_nonce, this->my_nonce))
403 {
404 DBG1(DBG_IKE, "key derivation failed");
405 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
406 return FAILED;
407 }
408 build_payloads(this, message);
409 return SUCCESS;
410 }
411
412 /**
413 * Implementation of task_t.process for initiator
414 */
415 static status_t process_i(private_ike_init_t *this, message_t *message)
416 {
417 enumerator_t *enumerator;
418 payload_t *payload;
419
420 /* check for erronous notifies */
421 enumerator = message->create_payload_enumerator(message);
422 while (enumerator->enumerate(enumerator, &payload))
423 {
424 if (payload->get_type(payload) == NOTIFY)
425 {
426 notify_payload_t *notify = (notify_payload_t*)payload;
427 notify_type_t type = notify->get_notify_type(notify);
428
429 switch (type)
430 {
431 case INVALID_KE_PAYLOAD:
432 {
433 chunk_t data;
434 diffie_hellman_group_t bad_group;
435
436 bad_group = this->dh_group;
437 data = notify->get_notification_data(notify);
438 this->dh_group = ntohs(*((u_int16_t*)data.ptr));
439 DBG1(DBG_IKE, "peer didn't accept DH group %N, "
440 "it requested %N", diffie_hellman_group_names,
441 bad_group, diffie_hellman_group_names, this->dh_group);
442
443 if (this->old_sa == NULL)
444 { /* reset the IKE_SA if we are not rekeying */
445 this->ike_sa->reset(this->ike_sa);
446 }
447
448 enumerator->destroy(enumerator);
449 this->retry++;
450 return NEED_MORE;
451 }
452 case NAT_DETECTION_SOURCE_IP:
453 case NAT_DETECTION_DESTINATION_IP:
454 /* skip, handled in ike_natd_t */
455 break;
456 case MULTIPLE_AUTH_SUPPORTED:
457 /* handled in ike_auth_t */
458 break;
459 case COOKIE:
460 {
461 chunk_free(&this->cookie);
462 this->cookie = chunk_clone(notify->get_notification_data(notify));
463 this->ike_sa->reset(this->ike_sa);
464 enumerator->destroy(enumerator);
465 DBG2(DBG_IKE, "received %N notify", notify_type_names, type);
466 this->retry++;
467 return NEED_MORE;
468 }
469 default:
470 {
471 if (type < 16383)
472 {
473 DBG1(DBG_IKE, "received %N notify error",
474 notify_type_names, type);
475 enumerator->destroy(enumerator);
476 return FAILED;
477 }
478 DBG2(DBG_IKE, "received %N notify",
479 notify_type_names, type);
480 break;
481 }
482 }
483 }
484 }
485 enumerator->destroy(enumerator);
486
487 process_payloads(this, message);
488
489 /* check if we have everything */
490 if (this->proposal == NULL ||
491 this->other_nonce.len == 0 || this->my_nonce.len == 0)
492 {
493 DBG1(DBG_IKE, "peers proposal selection invalid");
494 return FAILED;
495 }
496 this->ike_sa->set_proposal(this->ike_sa, this->proposal);
497
498 if (this->dh == NULL ||
499 !this->proposal->has_dh_group(this->proposal, this->dh_group))
500 {
501 DBG1(DBG_IKE, "peer DH group selection invalid");
502 return FAILED;
503 }
504
505 if (!derive_keys(this, this->my_nonce, this->other_nonce))
506 {
507 DBG1(DBG_IKE, "key derivation failed");
508 return FAILED;
509 }
510 return SUCCESS;
511 }
512
513 /**
514 * Implementation of task_t.get_type
515 */
516 static task_type_t get_type(private_ike_init_t *this)
517 {
518 return IKE_INIT;
519 }
520
521 /**
522 * Implementation of task_t.get_type
523 */
524 static chunk_t get_lower_nonce(private_ike_init_t *this)
525 {
526 if (memcmp(this->my_nonce.ptr, this->other_nonce.ptr,
527 min(this->my_nonce.len, this->other_nonce.len)) < 0)
528 {
529 return this->my_nonce;
530 }
531 else
532 {
533 return this->other_nonce;
534 }
535 }
536
537 /**
538 * Implementation of task_t.migrate
539 */
540 static void migrate(private_ike_init_t *this, ike_sa_t *ike_sa)
541 {
542 DESTROY_IF(this->proposal);
543 chunk_free(&this->other_nonce);
544
545 this->ike_sa = ike_sa;
546 this->proposal = NULL;
547 DESTROY_IF(this->dh);
548 this->dh = this->keymat->create_dh(this->keymat, this->dh_group);
549 }
550
551 /**
552 * Implementation of task_t.destroy
553 */
554 static void destroy(private_ike_init_t *this)
555 {
556 DESTROY_IF(this->dh);
557 DESTROY_IF(this->proposal);
558 chunk_free(&this->my_nonce);
559 chunk_free(&this->other_nonce);
560 chunk_free(&this->cookie);
561 free(this);
562 }
563
564 /*
565 * Described in header.
566 */
567 ike_init_t *ike_init_create(ike_sa_t *ike_sa, bool initiator, ike_sa_t *old_sa)
568 {
569 private_ike_init_t *this = malloc_thing(private_ike_init_t);
570
571 this->public.get_lower_nonce = (chunk_t(*)(ike_init_t*))get_lower_nonce;
572 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
573 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
574 this->public.task.destroy = (void(*)(task_t*))destroy;
575 if (initiator)
576 {
577 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
578 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
579 }
580 else
581 {
582 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
583 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
584 }
585
586 this->ike_sa = ike_sa;
587 this->initiator = initiator;
588 this->dh_group = MODP_NONE;
589 this->dh = NULL;
590 this->keymat = ike_sa->get_keymat(ike_sa);
591 this->my_nonce = chunk_empty;
592 this->other_nonce = chunk_empty;
593 this->cookie = chunk_empty;
594 this->proposal = NULL;
595 this->config = NULL;
596 this->old_sa = old_sa;
597 this->retry = 0;
598
599 return &this->public;
600 }