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