fixed payload order (Nonce, KE) for IKE_SA_INIT
[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 to %H",
230 this->config->get_other_host(this->config));
231 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
232
233 if (this->retry++ >= MAX_RETRIES)
234 {
235 SIG(IKE_UP_FAILED, "giving up after %d retries", MAX_RETRIES);
236 return FAILED;
237 }
238
239 /* if the DH group is set via use_dh_group(), we already have a DH object */
240 if (!this->dh)
241 {
242 this->dh_group = this->config->get_dh_group(this->config);
243 this->dh = diffie_hellman_create(this->dh_group);
244 if (this->dh == NULL)
245 {
246 SIG(IKE_UP_FAILED, "configured DH group %N not supported",
247 diffie_hellman_group_names, this->dh_group);
248 return FAILED;
249 }
250 }
251
252 /* generate nonce only when we are trying the first time */
253 if (this->my_nonce.ptr == NULL)
254 {
255 randomizer = randomizer_create();
256 status = randomizer->allocate_pseudo_random_bytes(randomizer, NONCE_SIZE,
257 &this->my_nonce);
258 randomizer->destroy(randomizer);
259 if (status != SUCCESS)
260 {
261 SIG(IKE_UP_FAILED, "error generating random nonce value");
262 return FAILED;
263 }
264 }
265
266 if (this->cookie.ptr)
267 {
268 message->add_notify(message, FALSE, COOKIE, this->cookie);
269 }
270
271 build_payloads(this, message);
272
273
274 return NEED_MORE;
275 }
276
277 /**
278 * Implementation of task_t.process for initiator
279 */
280 static status_t process_r(private_ike_init_t *this, message_t *message)
281 {
282 randomizer_t *randomizer;
283
284 this->config = this->ike_sa->get_ike_cfg(this->ike_sa);
285 SIG(IKE_UP_FAILED, "%H is initiating an IKE_SA",
286 message->get_source(message));
287 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
288
289 randomizer = randomizer_create();
290 if (randomizer->allocate_pseudo_random_bytes(randomizer, NONCE_SIZE,
291 &this->my_nonce) != SUCCESS)
292 {
293 DBG1(DBG_IKE, "error generating random nonce value");
294 }
295 randomizer->destroy(randomizer);
296
297 process_payloads(this, message);
298
299 return NEED_MORE;
300 }
301
302 /**
303 * Implementation of task_t.build for responder
304 */
305 static status_t build_r(private_ike_init_t *this, message_t *message)
306 {
307 chunk_t secret;
308 status_t status;
309
310 /* check if we have everything we need */
311 if (this->proposal == NULL ||
312 this->other_nonce.len == 0 || this->my_nonce.len == 0)
313 {
314 SIG(IKE_UP_FAILED, "received proposals inacceptable");
315 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
316 return FAILED;
317 }
318
319 if (this->dh == NULL ||
320 !this->proposal->has_dh_group(this->proposal, this->dh_group) ||
321 this->dh->get_shared_secret(this->dh, &secret) != SUCCESS)
322 {
323 algorithm_t *algo;
324 if (this->proposal->get_algorithm(this->proposal, DIFFIE_HELLMAN_GROUP,
325 &algo))
326 {
327 u_int16_t group = algo->algorithm;
328 SIG(CHILD_UP_FAILED, "DH group %N inacceptable, requesting %N",
329 diffie_hellman_group_names, this->dh_group,
330 diffie_hellman_group_names, group);
331 this->dh_group = group;
332 group = htons(group);
333 message->add_notify(message, FALSE, INVALID_KE_PAYLOAD,
334 chunk_from_thing(group));
335 }
336 else
337 {
338 SIG(IKE_UP_FAILED, "no acceptable proposal found");
339 }
340 return FAILED;
341 }
342
343 if (this->old_sa)
344 {
345 ike_sa_id_t *id;
346 prf_t *prf, *child_prf;
347
348 /* Apply SPI if we are rekeying */
349 id = this->ike_sa->get_id(this->ike_sa);
350 id->set_initiator_spi(id, this->proposal->get_spi(this->proposal));
351
352 /* setup crypto keys for the rekeyed SA */
353 prf = this->old_sa->get_prf(this->old_sa);
354 child_prf = this->old_sa->get_child_prf(this->old_sa);
355 status = this->ike_sa->derive_keys(this->ike_sa, this->proposal, secret,
356 this->other_nonce, this->my_nonce,
357 FALSE, child_prf, prf);
358 }
359 else
360 {
361 /* setup crypto keys */
362 status = this->ike_sa->derive_keys(this->ike_sa, this->proposal, secret,
363 this->other_nonce, this->my_nonce,
364 FALSE, NULL, NULL);
365 }
366 if (status != SUCCESS)
367 {
368 SIG(IKE_UP_FAILED, "key derivation failed");
369 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
370 return FAILED;
371 }
372
373 build_payloads(this, message);
374
375 return SUCCESS;
376 }
377
378 /**
379 * Implementation of task_t.process for initiator
380 */
381 static status_t process_i(private_ike_init_t *this, message_t *message)
382 {
383 chunk_t secret;
384 status_t status;
385 iterator_t *iterator;
386 payload_t *payload;
387
388 /* check for erronous notifies */
389 iterator = message->get_payload_iterator(message);
390 while (iterator->iterate(iterator, (void**)&payload))
391 {
392 if (payload->get_type(payload) == NOTIFY)
393 {
394 notify_payload_t *notify = (notify_payload_t*)payload;
395 notify_type_t type = notify->get_notify_type(notify);
396
397 switch (type)
398 {
399 case INVALID_KE_PAYLOAD:
400 {
401 chunk_t data;
402 diffie_hellman_group_t bad_group;
403
404 bad_group = this->dh_group;
405 data = notify->get_notification_data(notify);
406 this->dh_group = ntohs(*((u_int16_t*)data.ptr));
407 DBG1(DBG_IKE, "peer didn't accept DH group %N, "
408 "it requested %N", diffie_hellman_group_names,
409 bad_group, diffie_hellman_group_names, this->dh_group);
410
411 if (this->old_sa == NULL)
412 { /* reset the IKE_SA if we are not rekeying */
413 this->ike_sa->reset(this->ike_sa);
414 }
415
416 iterator->destroy(iterator);
417 return NEED_MORE;
418 }
419 case NAT_DETECTION_SOURCE_IP:
420 case NAT_DETECTION_DESTINATION_IP:
421 /* skip, handled in ike_natd_t */
422 break;
423 case COOKIE:
424 {
425 chunk_free(&this->cookie);
426 this->cookie = chunk_clone(notify->get_notification_data(notify));
427 this->ike_sa->reset(this->ike_sa);
428 iterator->destroy(iterator);
429 DBG1(DBG_IKE, "received %N notify", notify_type_names, type);
430 return NEED_MORE;
431 }
432 default:
433 {
434 if (type < 16383)
435 {
436 SIG(IKE_UP_FAILED, "received %N notify error",
437 notify_type_names, type);
438 iterator->destroy(iterator);
439 return FAILED;
440 }
441 DBG1(DBG_IKE, "received %N notify",
442 notify_type_names, type);
443 break;
444 }
445 }
446 }
447 }
448 iterator->destroy(iterator);
449
450 process_payloads(this, message);
451
452 /* check if we have everything */
453 if (this->proposal == NULL ||
454 this->other_nonce.len == 0 || this->my_nonce.len == 0)
455 {
456 SIG(IKE_UP_FAILED, "peers proposal selection invalid");
457 return FAILED;
458 }
459
460 if (this->dh == NULL ||
461 !this->proposal->has_dh_group(this->proposal, this->dh_group) ||
462 this->dh->get_shared_secret(this->dh, &secret) != SUCCESS)
463 {
464 SIG(IKE_UP_FAILED, "peers DH group selection invalid");
465 return FAILED;
466 }
467
468 /* Apply SPI if we are rekeying */
469 if (this->old_sa)
470 {
471 ike_sa_id_t *id;
472 prf_t *prf, *child_prf;
473
474 id = this->ike_sa->get_id(this->ike_sa);
475 id->set_responder_spi(id, this->proposal->get_spi(this->proposal));
476
477 /* setup crypto keys for the rekeyed SA */
478 prf = this->old_sa->get_prf(this->old_sa);
479 child_prf = this->old_sa->get_child_prf(this->old_sa);
480 status = this->ike_sa->derive_keys(this->ike_sa, this->proposal, secret,
481 this->my_nonce, this->other_nonce,
482 TRUE, child_prf, prf);
483 }
484 else
485 {
486 /* setup crypto keys for a new SA */
487 status = this->ike_sa->derive_keys(this->ike_sa, this->proposal, secret,
488 this->my_nonce, this->other_nonce,
489 TRUE, NULL, NULL);
490 }
491 if (status != SUCCESS)
492 {
493 SIG(IKE_UP_FAILED, "key derivation failed");
494 return FAILED;
495 }
496 return SUCCESS;
497 }
498
499 /**
500 * Implementation of task_t.get_type
501 */
502 static task_type_t get_type(private_ike_init_t *this)
503 {
504 return IKE_INIT;
505 }
506
507 /**
508 * Implementation of task_t.get_type
509 */
510 static chunk_t get_lower_nonce(private_ike_init_t *this)
511 {
512 if (memcmp(this->my_nonce.ptr, this->other_nonce.ptr,
513 min(this->my_nonce.len, this->other_nonce.len)) < 0)
514 {
515 return this->my_nonce;
516 }
517 else
518 {
519 return this->other_nonce;
520 }
521 }
522
523 /**
524 * Implementation of task_t.migrate
525 */
526 static void migrate(private_ike_init_t *this, ike_sa_t *ike_sa)
527 {
528 DESTROY_IF(this->proposal);
529 DESTROY_IF(this->dh);
530 chunk_free(&this->other_nonce);
531
532 this->ike_sa = ike_sa;
533 this->proposal = NULL;
534 this->dh = diffie_hellman_create(this->dh_group);
535 }
536
537 /**
538 * Implementation of task_t.destroy
539 */
540 static void destroy(private_ike_init_t *this)
541 {
542 DESTROY_IF(this->proposal);
543 DESTROY_IF(this->dh);
544 chunk_free(&this->my_nonce);
545 chunk_free(&this->other_nonce);
546 chunk_free(&this->cookie);
547 free(this);
548 }
549
550 /*
551 * Described in header.
552 */
553 ike_init_t *ike_init_create(ike_sa_t *ike_sa, bool initiator, ike_sa_t *old_sa)
554 {
555 private_ike_init_t *this = malloc_thing(private_ike_init_t);
556
557 this->public.get_lower_nonce = (chunk_t(*)(ike_init_t*))get_lower_nonce;
558 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
559 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
560 this->public.task.destroy = (void(*)(task_t*))destroy;
561 if (initiator)
562 {
563 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
564 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
565 }
566 else
567 {
568 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
569 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
570 }
571
572 this->ike_sa = ike_sa;
573 this->initiator = initiator;
574 this->dh_group = MODP_NONE;
575 this->dh = NULL;
576 this->my_nonce = chunk_empty;
577 this->other_nonce = chunk_empty;
578 this->cookie = chunk_empty;
579 this->proposal = NULL;
580 this->config = NULL;
581 this->old_sa = old_sa;
582 this->retry = 0;
583
584 return &this->public;
585 }