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