connection name to IKE_SA initiating
[strongswan.git] / src / charon / sa / tasks / ike_init.c
1 /**
2 * @file ike_init.c
3 *
4 * @brief Implementation of the ike_init task.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2007 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include "ike_init.h"
25
26 #include <string.h>
27
28 #include <daemon.h>
29 #include <crypto/diffie_hellman.h>
30 #include <encoding/payloads/sa_payload.h>
31 #include <encoding/payloads/ke_payload.h>
32 #include <encoding/payloads/nonce_payload.h>
33
34 /** maximum retries to do with cookies/other dh groups */
35 #define MAX_RETRIES 5
36
37 typedef struct private_ike_init_t private_ike_init_t;
38
39 /**
40 * Private members of a ike_init_t task.
41 */
42 struct private_ike_init_t {
43
44 /**
45 * Public methods and task_t interface.
46 */
47 ike_init_t public;
48
49 /**
50 * Assigned IKE_SA.
51 */
52 ike_sa_t *ike_sa;
53
54 /**
55 * Are we the initiator?
56 */
57 bool initiator;
58
59 /**
60 * IKE config to establish
61 */
62 ike_cfg_t *config;
63
64 /**
65 * diffie hellman group to use
66 */
67 diffie_hellman_group_t dh_group;
68
69 /**
70 * Diffie hellman object used to generate public DH value.
71 */
72 diffie_hellman_t *dh;
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 iterator_t *iterator;
172 payload_t *payload;
173
174 iterator = message->get_payload_iterator(message);
175 while (iterator->iterate(iterator, (void**)&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 = diffie_hellman_create(this->dh_group);
199 }
200 if (this->dh)
201 {
202 this->dh->set_other_public_value(this->dh,
203 ke_payload->get_key_exchange_data(ke_payload));
204 }
205 break;
206 }
207 case NONCE:
208 {
209 nonce_payload_t *nonce_payload = (nonce_payload_t*)payload;
210 this->other_nonce = nonce_payload->get_nonce(nonce_payload);
211 break;
212 }
213 default:
214 break;
215 }
216 }
217 iterator->destroy(iterator);
218 }
219
220 /**
221 * Implementation of task_t.process for initiator
222 */
223 static status_t build_i(private_ike_init_t *this, message_t *message)
224 {
225 randomizer_t *randomizer;
226 status_t status;
227
228 this->config = this->ike_sa->get_ike_cfg(this->ike_sa);
229 SIG(IKE_UP_START, "initiating IKE_SA '%s' to %H",
230 this->ike_sa->get_name(this->ike_sa),
231 this->config->get_other_host(this->config));
232 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
233
234 if (this->retry++ >= MAX_RETRIES)
235 {
236 SIG(IKE_UP_FAILED, "giving up after %d retries", MAX_RETRIES);
237 return FAILED;
238 }
239
240 /* if the DH group is set via use_dh_group(), we already have a DH object */
241 if (!this->dh)
242 {
243 this->dh_group = this->config->get_dh_group(this->config);
244 this->dh = diffie_hellman_create(this->dh_group);
245 if (this->dh == NULL)
246 {
247 SIG(IKE_UP_FAILED, "configured DH group %N not supported",
248 diffie_hellman_group_names, this->dh_group);
249 return FAILED;
250 }
251 }
252
253 /* generate nonce only when we are trying the first time */
254 if (this->my_nonce.ptr == NULL)
255 {
256 randomizer = randomizer_create();
257 status = randomizer->allocate_pseudo_random_bytes(randomizer, NONCE_SIZE,
258 &this->my_nonce);
259 randomizer->destroy(randomizer);
260 if (status != SUCCESS)
261 {
262 SIG(IKE_UP_FAILED, "error generating random nonce value");
263 return FAILED;
264 }
265 }
266
267 if (this->cookie.ptr)
268 {
269 message->add_notify(message, FALSE, COOKIE, this->cookie);
270 }
271
272 build_payloads(this, message);
273
274
275 return NEED_MORE;
276 }
277
278 /**
279 * Implementation of task_t.process for initiator
280 */
281 static status_t process_r(private_ike_init_t *this, message_t *message)
282 {
283 randomizer_t *randomizer;
284
285 this->config = this->ike_sa->get_ike_cfg(this->ike_sa);
286 SIG(IKE_UP_FAILED, "%H is initiating an IKE_SA",
287 message->get_source(message));
288 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
289
290 randomizer = randomizer_create();
291 if (randomizer->allocate_pseudo_random_bytes(randomizer, NONCE_SIZE,
292 &this->my_nonce) != SUCCESS)
293 {
294 DBG1(DBG_IKE, "error generating random nonce value");
295 }
296 randomizer->destroy(randomizer);
297
298 process_payloads(this, message);
299
300 return NEED_MORE;
301 }
302
303 /**
304 * Implementation of task_t.build for responder
305 */
306 static status_t build_r(private_ike_init_t *this, message_t *message)
307 {
308 chunk_t secret;
309 status_t status;
310
311 /* check if we have everything we need */
312 if (this->proposal == NULL ||
313 this->other_nonce.len == 0 || this->my_nonce.len == 0)
314 {
315 SIG(IKE_UP_FAILED, "received proposals inacceptable");
316 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
317 return FAILED;
318 }
319
320 if (this->dh == NULL ||
321 !this->proposal->has_dh_group(this->proposal, this->dh_group) ||
322 this->dh->get_shared_secret(this->dh, &secret) != SUCCESS)
323 {
324 algorithm_t *algo;
325 if (this->proposal->get_algorithm(this->proposal, DIFFIE_HELLMAN_GROUP,
326 &algo))
327 {
328 u_int16_t group = algo->algorithm;
329 SIG(CHILD_UP_FAILED, "DH group %N inacceptable, requesting %N",
330 diffie_hellman_group_names, this->dh_group,
331 diffie_hellman_group_names, group);
332 this->dh_group = group;
333 group = htons(group);
334 message->add_notify(message, FALSE, INVALID_KE_PAYLOAD,
335 chunk_from_thing(group));
336 }
337 else
338 {
339 SIG(IKE_UP_FAILED, "no acceptable proposal found");
340 }
341 return FAILED;
342 }
343
344 if (this->old_sa)
345 {
346 ike_sa_id_t *id;
347 prf_t *prf, *child_prf;
348
349 /* Apply SPI if we are rekeying */
350 id = this->ike_sa->get_id(this->ike_sa);
351 id->set_initiator_spi(id, this->proposal->get_spi(this->proposal));
352
353 /* setup crypto keys for the rekeyed SA */
354 prf = this->old_sa->get_prf(this->old_sa);
355 child_prf = this->old_sa->get_child_prf(this->old_sa);
356 status = this->ike_sa->derive_keys(this->ike_sa, this->proposal, secret,
357 this->other_nonce, this->my_nonce,
358 FALSE, child_prf, prf);
359 }
360 else
361 {
362 /* setup crypto keys */
363 status = this->ike_sa->derive_keys(this->ike_sa, this->proposal, secret,
364 this->other_nonce, this->my_nonce,
365 FALSE, NULL, NULL);
366 }
367 if (status != SUCCESS)
368 {
369 SIG(IKE_UP_FAILED, "key derivation failed");
370 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
371 return FAILED;
372 }
373
374 build_payloads(this, message);
375
376 return SUCCESS;
377 }
378
379 /**
380 * Implementation of task_t.process for initiator
381 */
382 static status_t process_i(private_ike_init_t *this, message_t *message)
383 {
384 chunk_t secret;
385 status_t status;
386 iterator_t *iterator;
387 payload_t *payload;
388
389 /* check for erronous notifies */
390 iterator = message->get_payload_iterator(message);
391 while (iterator->iterate(iterator, (void**)&payload))
392 {
393 if (payload->get_type(payload) == NOTIFY)
394 {
395 notify_payload_t *notify = (notify_payload_t*)payload;
396 notify_type_t type = notify->get_notify_type(notify);
397
398 switch (type)
399 {
400 case INVALID_KE_PAYLOAD:
401 {
402 chunk_t data;
403 diffie_hellman_group_t bad_group;
404
405 bad_group = this->dh_group;
406 data = notify->get_notification_data(notify);
407 this->dh_group = ntohs(*((u_int16_t*)data.ptr));
408 DBG1(DBG_IKE, "peer didn't accept DH group %N, "
409 "it requested %N", diffie_hellman_group_names,
410 bad_group, diffie_hellman_group_names, this->dh_group);
411
412 if (this->old_sa == NULL)
413 { /* reset the IKE_SA if we are not rekeying */
414 this->ike_sa->reset(this->ike_sa);
415 }
416
417 iterator->destroy(iterator);
418 return NEED_MORE;
419 }
420 case NAT_DETECTION_SOURCE_IP:
421 case NAT_DETECTION_DESTINATION_IP:
422 /* skip, handled in ike_natd_t */
423 break;
424 case COOKIE:
425 {
426 chunk_free(&this->cookie);
427 this->cookie = chunk_clone(notify->get_notification_data(notify));
428 this->ike_sa->reset(this->ike_sa);
429 iterator->destroy(iterator);
430 DBG1(DBG_IKE, "received %N notify", notify_type_names, type);
431 return NEED_MORE;
432 }
433 default:
434 {
435 if (type < 16383)
436 {
437 SIG(IKE_UP_FAILED, "received %N notify error",
438 notify_type_names, type);
439 iterator->destroy(iterator);
440 return FAILED;
441 }
442 DBG1(DBG_IKE, "received %N notify",
443 notify_type_names, type);
444 break;
445 }
446 }
447 }
448 }
449 iterator->destroy(iterator);
450
451 process_payloads(this, message);
452
453 /* check if we have everything */
454 if (this->proposal == NULL ||
455 this->other_nonce.len == 0 || this->my_nonce.len == 0)
456 {
457 SIG(IKE_UP_FAILED, "peers proposal selection invalid");
458 return FAILED;
459 }
460
461 if (this->dh == NULL ||
462 !this->proposal->has_dh_group(this->proposal, this->dh_group) ||
463 this->dh->get_shared_secret(this->dh, &secret) != SUCCESS)
464 {
465 SIG(IKE_UP_FAILED, "peers DH group selection invalid");
466 return FAILED;
467 }
468
469 /* Apply SPI if we are rekeying */
470 if (this->old_sa)
471 {
472 ike_sa_id_t *id;
473 prf_t *prf, *child_prf;
474
475 id = this->ike_sa->get_id(this->ike_sa);
476 id->set_responder_spi(id, this->proposal->get_spi(this->proposal));
477
478 /* setup crypto keys for the rekeyed SA */
479 prf = this->old_sa->get_prf(this->old_sa);
480 child_prf = this->old_sa->get_child_prf(this->old_sa);
481 status = this->ike_sa->derive_keys(this->ike_sa, this->proposal, secret,
482 this->my_nonce, this->other_nonce,
483 TRUE, child_prf, prf);
484 }
485 else
486 {
487 /* setup crypto keys for a new SA */
488 status = this->ike_sa->derive_keys(this->ike_sa, this->proposal, secret,
489 this->my_nonce, this->other_nonce,
490 TRUE, NULL, NULL);
491 }
492 if (status != SUCCESS)
493 {
494 SIG(IKE_UP_FAILED, "key derivation failed");
495 return FAILED;
496 }
497 return SUCCESS;
498 }
499
500 /**
501 * Implementation of task_t.get_type
502 */
503 static task_type_t get_type(private_ike_init_t *this)
504 {
505 return IKE_INIT;
506 }
507
508 /**
509 * Implementation of task_t.get_type
510 */
511 static chunk_t get_lower_nonce(private_ike_init_t *this)
512 {
513 if (memcmp(this->my_nonce.ptr, this->other_nonce.ptr,
514 min(this->my_nonce.len, this->other_nonce.len)) < 0)
515 {
516 return this->my_nonce;
517 }
518 else
519 {
520 return this->other_nonce;
521 }
522 }
523
524 /**
525 * Implementation of task_t.migrate
526 */
527 static void migrate(private_ike_init_t *this, ike_sa_t *ike_sa)
528 {
529 DESTROY_IF(this->proposal);
530 DESTROY_IF(this->dh);
531 chunk_free(&this->other_nonce);
532
533 this->ike_sa = ike_sa;
534 this->proposal = NULL;
535 this->dh = diffie_hellman_create(this->dh_group);
536 }
537
538 /**
539 * Implementation of task_t.destroy
540 */
541 static void destroy(private_ike_init_t *this)
542 {
543 DESTROY_IF(this->proposal);
544 DESTROY_IF(this->dh);
545 chunk_free(&this->my_nonce);
546 chunk_free(&this->other_nonce);
547 chunk_free(&this->cookie);
548 free(this);
549 }
550
551 /*
552 * Described in header.
553 */
554 ike_init_t *ike_init_create(ike_sa_t *ike_sa, bool initiator, ike_sa_t *old_sa)
555 {
556 private_ike_init_t *this = malloc_thing(private_ike_init_t);
557
558 this->public.get_lower_nonce = (chunk_t(*)(ike_init_t*))get_lower_nonce;
559 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
560 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
561 this->public.task.destroy = (void(*)(task_t*))destroy;
562 if (initiator)
563 {
564 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
565 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
566 }
567 else
568 {
569 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
570 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
571 }
572
573 this->ike_sa = ike_sa;
574 this->initiator = initiator;
575 this->dh_group = MODP_NONE;
576 this->dh = NULL;
577 this->my_nonce = chunk_empty;
578 this->other_nonce = chunk_empty;
579 this->cookie = chunk_empty;
580 this->proposal = NULL;
581 this->config = NULL;
582 this->old_sa = old_sa;
583 this->retry = 0;
584
585 return &this->public;
586 }