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