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