ikev2: Only touch the DH object if we have a matching proposal
[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(PLV2_NONCE);
151 nonce_payload->set_nonce(nonce_payload, this->my_nonce);
152 ke_payload = ke_payload_create_from_diffie_hellman(PLV2_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 /* negotiate fragmentation if we are not rekeying */
166 if (!this->old_sa &&
167 this->config->fragmentation(this->config) != FRAGMENTATION_NO)
168 {
169 if (this->initiator ||
170 this->ike_sa->supports_extension(this->ike_sa,
171 EXT_IKE_FRAGMENTATION))
172 {
173 message->add_notify(message, FALSE, FRAGMENTATION_SUPPORTED,
174 chunk_empty);
175 }
176 }
177 }
178
179 /**
180 * Read payloads from message
181 */
182 static void process_payloads(private_ike_init_t *this, message_t *message)
183 {
184 enumerator_t *enumerator;
185 payload_t *payload;
186 ke_payload_t *ke_payload = NULL;
187
188 enumerator = message->create_payload_enumerator(message);
189 while (enumerator->enumerate(enumerator, &payload))
190 {
191 switch (payload->get_type(payload))
192 {
193 case PLV2_SECURITY_ASSOCIATION:
194 {
195 sa_payload_t *sa_payload = (sa_payload_t*)payload;
196 linked_list_t *proposal_list;
197 bool private;
198
199 proposal_list = sa_payload->get_proposals(sa_payload);
200 private = this->ike_sa->supports_extension(this->ike_sa,
201 EXT_STRONGSWAN);
202 this->proposal = this->config->select_proposal(this->config,
203 proposal_list, private);
204 if (!this->proposal)
205 {
206 charon->bus->alert(charon->bus, ALERT_PROPOSAL_MISMATCH_IKE,
207 proposal_list);
208 }
209 proposal_list->destroy_offset(proposal_list,
210 offsetof(proposal_t, destroy));
211 break;
212 }
213 case PLV2_KEY_EXCHANGE:
214 {
215 ke_payload = (ke_payload_t*)payload;
216
217 this->dh_group = ke_payload->get_dh_group_number(ke_payload);
218 break;
219 }
220 case PLV2_NONCE:
221 {
222 nonce_payload_t *nonce_payload = (nonce_payload_t*)payload;
223
224 this->other_nonce = nonce_payload->get_nonce(nonce_payload);
225 break;
226 }
227 case PLV2_NOTIFY:
228 {
229 notify_payload_t *notify = (notify_payload_t*)payload;
230
231 if (notify->get_notify_type(notify) == FRAGMENTATION_SUPPORTED)
232 {
233 this->ike_sa->enable_extension(this->ike_sa,
234 EXT_IKE_FRAGMENTATION);
235 }
236 }
237 default:
238 break;
239 }
240 }
241 enumerator->destroy(enumerator);
242
243 if (ke_payload && this->proposal &&
244 this->proposal->has_dh_group(this->proposal, this->dh_group))
245 {
246 if (!this->initiator)
247 {
248 this->dh = this->keymat->keymat.create_dh(
249 &this->keymat->keymat, this->dh_group);
250 }
251 if (this->dh)
252 {
253 this->dh->set_other_public_value(this->dh,
254 ke_payload->get_key_exchange_data(ke_payload));
255 }
256 }
257 }
258
259 METHOD(task_t, build_i, status_t,
260 private_ike_init_t *this, message_t *message)
261 {
262 this->config = this->ike_sa->get_ike_cfg(this->ike_sa);
263 DBG0(DBG_IKE, "initiating IKE_SA %s[%d] to %H",
264 this->ike_sa->get_name(this->ike_sa),
265 this->ike_sa->get_unique_id(this->ike_sa),
266 this->ike_sa->get_other_host(this->ike_sa));
267 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
268
269 if (this->retry >= MAX_RETRIES)
270 {
271 DBG1(DBG_IKE, "giving up after %d retries", MAX_RETRIES);
272 return FAILED;
273 }
274
275 /* if the DH group is set via use_dh_group(), we already have a DH object */
276 if (!this->dh)
277 {
278 this->dh_group = this->config->get_dh_group(this->config);
279 this->dh = this->keymat->keymat.create_dh(&this->keymat->keymat,
280 this->dh_group);
281 if (!this->dh)
282 {
283 DBG1(DBG_IKE, "configured DH group %N not supported",
284 diffie_hellman_group_names, this->dh_group);
285 return FAILED;
286 }
287 }
288
289 /* generate nonce only when we are trying the first time */
290 if (this->my_nonce.ptr == NULL)
291 {
292 nonce_gen_t *nonceg;
293
294 nonceg = this->keymat->keymat.create_nonce_gen(&this->keymat->keymat);
295 if (!nonceg)
296 {
297 DBG1(DBG_IKE, "no nonce generator found to create nonce");
298 return FAILED;
299 }
300 if (!nonceg->allocate_nonce(nonceg, NONCE_SIZE, &this->my_nonce))
301 {
302 DBG1(DBG_IKE, "nonce allocation failed");
303 nonceg->destroy(nonceg);
304 return FAILED;
305 }
306 nonceg->destroy(nonceg);
307 }
308
309 if (this->cookie.ptr)
310 {
311 message->add_notify(message, FALSE, COOKIE, this->cookie);
312 }
313
314 build_payloads(this, message);
315
316 #ifdef ME
317 {
318 chunk_t connect_id = this->ike_sa->get_connect_id(this->ike_sa);
319 if (connect_id.ptr)
320 {
321 message->add_notify(message, FALSE, ME_CONNECTID, connect_id);
322 }
323 }
324 #endif /* ME */
325
326 return NEED_MORE;
327 }
328
329 METHOD(task_t, process_r, status_t,
330 private_ike_init_t *this, message_t *message)
331 {
332 nonce_gen_t *nonceg;
333
334 this->config = this->ike_sa->get_ike_cfg(this->ike_sa);
335 DBG0(DBG_IKE, "%H is initiating an IKE_SA", message->get_source(message));
336 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
337
338 nonceg = this->keymat->keymat.create_nonce_gen(&this->keymat->keymat);
339 if (!nonceg)
340 {
341 DBG1(DBG_IKE, "no nonce generator found to create nonce");
342 return FAILED;
343 }
344 if (!nonceg->allocate_nonce(nonceg, NONCE_SIZE, &this->my_nonce))
345 {
346 DBG1(DBG_IKE, "nonce allocation failed");
347 nonceg->destroy(nonceg);
348 return FAILED;
349 }
350 nonceg->destroy(nonceg);
351
352 #ifdef ME
353 {
354 notify_payload_t *notify = message->get_notify(message, ME_CONNECTID);
355 if (notify)
356 {
357 chunk_t connect_id = notify->get_notification_data(notify);
358 DBG2(DBG_IKE, "received ME_CONNECTID %#B", &connect_id);
359 charon->connect_manager->stop_checks(charon->connect_manager,
360 connect_id);
361 }
362 }
363 #endif /* ME */
364
365 process_payloads(this, message);
366
367 return NEED_MORE;
368 }
369
370 /**
371 * Derive the keymat for the IKE_SA
372 */
373 static bool derive_keys(private_ike_init_t *this,
374 chunk_t nonce_i, chunk_t nonce_r)
375 {
376 keymat_v2_t *old_keymat;
377 pseudo_random_function_t prf_alg = PRF_UNDEFINED;
378 chunk_t skd = chunk_empty;
379 ike_sa_id_t *id;
380
381 id = this->ike_sa->get_id(this->ike_sa);
382 if (this->old_sa)
383 {
384 /* rekeying: Include old SKd, use old PRF, apply SPI */
385 old_keymat = (keymat_v2_t*)this->old_sa->get_keymat(this->old_sa);
386 prf_alg = old_keymat->get_skd(old_keymat, &skd);
387 if (this->initiator)
388 {
389 id->set_responder_spi(id, this->proposal->get_spi(this->proposal));
390 }
391 else
392 {
393 id->set_initiator_spi(id, this->proposal->get_spi(this->proposal));
394 }
395 }
396 if (!this->keymat->derive_ike_keys(this->keymat, this->proposal, this->dh,
397 nonce_i, nonce_r, id, prf_alg, skd))
398 {
399 return FALSE;
400 }
401 charon->bus->ike_keys(charon->bus, this->ike_sa, this->dh, chunk_empty,
402 nonce_i, nonce_r, this->old_sa, NULL);
403 return TRUE;
404 }
405
406 METHOD(task_t, build_r, status_t,
407 private_ike_init_t *this, message_t *message)
408 {
409 /* check if we have everything we need */
410 if (this->proposal == NULL ||
411 this->other_nonce.len == 0 || this->my_nonce.len == 0)
412 {
413 DBG1(DBG_IKE, "received proposals inacceptable");
414 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
415 return FAILED;
416 }
417 this->ike_sa->set_proposal(this->ike_sa, this->proposal);
418
419 if (this->dh == NULL ||
420 !this->proposal->has_dh_group(this->proposal, this->dh_group))
421 {
422 u_int16_t group;
423
424 if (this->proposal->get_algorithm(this->proposal, DIFFIE_HELLMAN_GROUP,
425 &group, NULL))
426 {
427 DBG1(DBG_IKE, "DH group %N inacceptable, requesting %N",
428 diffie_hellman_group_names, this->dh_group,
429 diffie_hellman_group_names, group);
430 this->dh_group = group;
431 group = htons(group);
432 message->add_notify(message, FALSE, INVALID_KE_PAYLOAD,
433 chunk_from_thing(group));
434 }
435 else
436 {
437 DBG1(DBG_IKE, "no acceptable proposal found");
438 }
439 return FAILED;
440 }
441
442 if (!derive_keys(this, this->other_nonce, this->my_nonce))
443 {
444 DBG1(DBG_IKE, "key derivation failed");
445 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
446 return FAILED;
447 }
448 build_payloads(this, message);
449 return SUCCESS;
450 }
451
452 /**
453 * Raise alerts for received notify errors
454 */
455 static void raise_alerts(private_ike_init_t *this, notify_type_t type)
456 {
457 linked_list_t *list;
458
459 switch (type)
460 {
461 case NO_PROPOSAL_CHOSEN:
462 list = this->config->get_proposals(this->config);
463 charon->bus->alert(charon->bus, ALERT_PROPOSAL_MISMATCH_IKE, list);
464 list->destroy_offset(list, offsetof(proposal_t, destroy));
465 break;
466 default:
467 break;
468 }
469 }
470
471 METHOD(task_t, process_i, status_t,
472 private_ike_init_t *this, message_t *message)
473 {
474 enumerator_t *enumerator;
475 payload_t *payload;
476
477 /* check for erroneous notifies */
478 enumerator = message->create_payload_enumerator(message);
479 while (enumerator->enumerate(enumerator, &payload))
480 {
481 if (payload->get_type(payload) == PLV2_NOTIFY)
482 {
483 notify_payload_t *notify = (notify_payload_t*)payload;
484 notify_type_t type = notify->get_notify_type(notify);
485
486 switch (type)
487 {
488 case INVALID_KE_PAYLOAD:
489 {
490 chunk_t data;
491 diffie_hellman_group_t bad_group;
492
493 bad_group = this->dh_group;
494 data = notify->get_notification_data(notify);
495 this->dh_group = ntohs(*((u_int16_t*)data.ptr));
496 DBG1(DBG_IKE, "peer didn't accept DH group %N, "
497 "it requested %N", diffie_hellman_group_names,
498 bad_group, diffie_hellman_group_names, this->dh_group);
499
500 if (this->old_sa == NULL)
501 { /* reset the IKE_SA if we are not rekeying */
502 this->ike_sa->reset(this->ike_sa);
503 }
504
505 enumerator->destroy(enumerator);
506 this->retry++;
507 return NEED_MORE;
508 }
509 case NAT_DETECTION_SOURCE_IP:
510 case NAT_DETECTION_DESTINATION_IP:
511 /* skip, handled in ike_natd_t */
512 break;
513 case MULTIPLE_AUTH_SUPPORTED:
514 /* handled in ike_auth_t */
515 break;
516 case COOKIE:
517 {
518 chunk_free(&this->cookie);
519 this->cookie = chunk_clone(notify->get_notification_data(notify));
520 this->ike_sa->reset(this->ike_sa);
521 enumerator->destroy(enumerator);
522 DBG2(DBG_IKE, "received %N notify", notify_type_names, type);
523 this->retry++;
524 return NEED_MORE;
525 }
526 default:
527 {
528 if (type <= 16383)
529 {
530 DBG1(DBG_IKE, "received %N notify error",
531 notify_type_names, type);
532 enumerator->destroy(enumerator);
533 raise_alerts(this, type);
534 return FAILED;
535 }
536 DBG2(DBG_IKE, "received %N notify",
537 notify_type_names, type);
538 break;
539 }
540 }
541 }
542 }
543 enumerator->destroy(enumerator);
544
545 process_payloads(this, message);
546
547 /* check if we have everything */
548 if (this->proposal == NULL ||
549 this->other_nonce.len == 0 || this->my_nonce.len == 0)
550 {
551 DBG1(DBG_IKE, "peers proposal selection invalid");
552 return FAILED;
553 }
554 this->ike_sa->set_proposal(this->ike_sa, this->proposal);
555
556 if (this->dh == NULL ||
557 !this->proposal->has_dh_group(this->proposal, this->dh_group))
558 {
559 DBG1(DBG_IKE, "peer DH group selection invalid");
560 return FAILED;
561 }
562
563 if (!derive_keys(this, this->my_nonce, this->other_nonce))
564 {
565 DBG1(DBG_IKE, "key derivation failed");
566 return FAILED;
567 }
568 return SUCCESS;
569 }
570
571 METHOD(task_t, get_type, task_type_t,
572 private_ike_init_t *this)
573 {
574 return TASK_IKE_INIT;
575 }
576
577 METHOD(task_t, migrate, void,
578 private_ike_init_t *this, ike_sa_t *ike_sa)
579 {
580 DESTROY_IF(this->proposal);
581 chunk_free(&this->other_nonce);
582
583 this->ike_sa = ike_sa;
584 this->keymat = (keymat_v2_t*)ike_sa->get_keymat(ike_sa);
585 this->proposal = NULL;
586 if (this->dh && this->dh->get_dh_group(this->dh) != this->dh_group)
587 { /* reset DH value only if group changed (INVALID_KE_PAYLOAD) */
588 this->dh->destroy(this->dh);
589 this->dh = this->keymat->keymat.create_dh(&this->keymat->keymat,
590 this->dh_group);
591 }
592 }
593
594 METHOD(task_t, destroy, void,
595 private_ike_init_t *this)
596 {
597 DESTROY_IF(this->dh);
598 DESTROY_IF(this->proposal);
599 chunk_free(&this->my_nonce);
600 chunk_free(&this->other_nonce);
601 chunk_free(&this->cookie);
602 free(this);
603 }
604
605 METHOD(ike_init_t, get_lower_nonce, chunk_t,
606 private_ike_init_t *this)
607 {
608 if (memcmp(this->my_nonce.ptr, this->other_nonce.ptr,
609 min(this->my_nonce.len, this->other_nonce.len)) < 0)
610 {
611 return this->my_nonce;
612 }
613 else
614 {
615 return this->other_nonce;
616 }
617 }
618
619 /*
620 * Described in header.
621 */
622 ike_init_t *ike_init_create(ike_sa_t *ike_sa, bool initiator, ike_sa_t *old_sa)
623 {
624 private_ike_init_t *this;
625
626 INIT(this,
627 .public = {
628 .task = {
629 .get_type = _get_type,
630 .migrate = _migrate,
631 .destroy = _destroy,
632 },
633 .get_lower_nonce = _get_lower_nonce,
634 },
635 .ike_sa = ike_sa,
636 .initiator = initiator,
637 .dh_group = MODP_NONE,
638 .keymat = (keymat_v2_t*)ike_sa->get_keymat(ike_sa),
639 .old_sa = old_sa,
640 );
641
642 if (initiator)
643 {
644 this->public.task.build = _build_i;
645 this->public.task.process = _process_i;
646 }
647 else
648 {
649 this->public.task.build = _build_r;
650 this->public.task.process = _process_r;
651 }
652
653 return &this->public;
654 }