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