2 * Copyright (C) 2008 Tobias Brunner
3 * Copyright (C) 2005-2007 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
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>.
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
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>
31 /** maximum retries to do with cookies/other dh groups */
34 typedef struct private_ike_init_t private_ike_init_t
;
37 * Private members of a ike_init_t task.
39 struct private_ike_init_t
{
42 * Public methods and task_t interface.
52 * Are we the initiator?
57 * IKE config to establish
62 * diffie hellman group to use
64 diffie_hellman_group_t dh_group
;
67 * Diffie hellman object used to generate public DH value.
77 * nonce chosen by peer
82 * Negotiated proposal used for IKE_SA
87 * Old IKE_SA which gets rekeyed
92 * cookie received from responder
97 * retries done so far after failure (cookie or bad dh group)
103 * build the payloads for the message
105 static void build_payloads(private_ike_init_t
*this, message_t
*message
)
107 sa_payload_t
*sa_payload
;
108 ke_payload_t
*ke_payload
;
109 nonce_payload_t
*nonce_payload
;
110 linked_list_t
*proposal_list
;
112 proposal_t
*proposal
;
113 iterator_t
*iterator
;
115 id
= this->ike_sa
->get_id(this->ike_sa
);
117 this->config
= this->ike_sa
->get_ike_cfg(this->ike_sa
);
121 proposal_list
= this->config
->get_proposals(this->config
);
124 /* include SPI of new IKE_SA when we are rekeying */
125 iterator
= proposal_list
->create_iterator(proposal_list
, TRUE
);
126 while (iterator
->iterate(iterator
, (void**)&proposal
))
128 proposal
->set_spi(proposal
, id
->get_initiator_spi(id
));
130 iterator
->destroy(iterator
);
133 sa_payload
= sa_payload_create_from_proposal_list(proposal_list
);
134 proposal_list
->destroy_offset(proposal_list
, offsetof(proposal_t
, destroy
));
140 /* include SPI of new IKE_SA when we are rekeying */
141 this->proposal
->set_spi(this->proposal
, id
->get_responder_spi(id
));
143 sa_payload
= sa_payload_create_from_proposal(this->proposal
);
145 message
->add_payload(message
, (payload_t
*)sa_payload
);
147 nonce_payload
= nonce_payload_create();
148 nonce_payload
->set_nonce(nonce_payload
, this->my_nonce
);
149 ke_payload
= ke_payload_create_from_diffie_hellman(this->dh
);
152 { /* payload order differs if we are rekeying */
153 message
->add_payload(message
, (payload_t
*)nonce_payload
);
154 message
->add_payload(message
, (payload_t
*)ke_payload
);
158 message
->add_payload(message
, (payload_t
*)ke_payload
);
159 message
->add_payload(message
, (payload_t
*)nonce_payload
);
164 * Read payloads from message
166 static void process_payloads(private_ike_init_t
*this, message_t
*message
)
168 iterator_t
*iterator
;
171 iterator
= message
->get_payload_iterator(message
);
172 while (iterator
->iterate(iterator
, (void**)&payload
))
174 switch (payload
->get_type(payload
))
176 case SECURITY_ASSOCIATION
:
178 sa_payload_t
*sa_payload
= (sa_payload_t
*)payload
;
179 linked_list_t
*proposal_list
;
181 proposal_list
= sa_payload
->get_proposals(sa_payload
);
182 this->proposal
= this->config
->select_proposal(this->config
,
184 proposal_list
->destroy_offset(proposal_list
,
185 offsetof(proposal_t
, destroy
));
190 ke_payload_t
*ke_payload
= (ke_payload_t
*)payload
;
192 this->dh_group
= ke_payload
->get_dh_group_number(ke_payload
);
193 if (!this->initiator
)
195 this->dh
= lib
->crypto
->create_dh(lib
->crypto
, this->dh_group
);
199 this->dh
->set_other_public_value(this->dh
,
200 ke_payload
->get_key_exchange_data(ke_payload
));
206 nonce_payload_t
*nonce_payload
= (nonce_payload_t
*)payload
;
208 this->other_nonce
= nonce_payload
->get_nonce(nonce_payload
);
213 vendor_id_payload_t
*vendor_id
= (vendor_id_payload_t
*)payload
;
214 chunk_t vid
= vendor_id
->get_data(vendor_id
);
216 DBG1(DBG_ENC
, "received vendor id: %#B", &vid
);
222 iterator
->destroy(iterator
);
226 * Implementation of task_t.process for initiator
228 static status_t
build_i(private_ike_init_t
*this, message_t
*message
)
232 this->config
= this->ike_sa
->get_ike_cfg(this->ike_sa
);
233 DBG0(DBG_IKE
, "initiating IKE_SA %s[%d] to %H",
234 this->ike_sa
->get_name(this->ike_sa
),
235 this->ike_sa
->get_unique_id(this->ike_sa
),
236 this->ike_sa
->get_other_host(this->ike_sa
));
237 this->ike_sa
->set_state(this->ike_sa
, IKE_CONNECTING
);
239 if (this->retry
++ >= MAX_RETRIES
)
241 DBG1(DBG_IKE
, "giving up after %d retries", MAX_RETRIES
);
245 /* if the DH group is set via use_dh_group(), we already have a DH object */
248 this->dh_group
= this->config
->get_dh_group(this->config
);
249 this->dh
= lib
->crypto
->create_dh(lib
->crypto
, this->dh_group
);
250 if (this->dh
== NULL
)
252 DBG1(DBG_IKE
, "configured DH group %N not supported",
253 diffie_hellman_group_names
, this->dh_group
);
258 /* generate nonce only when we are trying the first time */
259 if (this->my_nonce
.ptr
== NULL
)
261 rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
);
264 DBG1(DBG_IKE
, "error generating nonce");
267 rng
->allocate_bytes(rng
, NONCE_SIZE
, &this->my_nonce
);
271 if (this->cookie
.ptr
)
273 message
->add_notify(message
, FALSE
, COOKIE
, this->cookie
);
276 build_payloads(this, message
);
280 chunk_t connect_id
= this->ike_sa
->get_connect_id(this->ike_sa
);
283 message
->add_notify(message
, FALSE
, ME_CONNECTID
, connect_id
);
292 * Implementation of task_t.process for responder
294 static status_t
process_r(private_ike_init_t
*this, message_t
*message
)
298 this->config
= this->ike_sa
->get_ike_cfg(this->ike_sa
);
299 DBG0(DBG_IKE
, "%H is initiating an IKE_SA", message
->get_source(message
));
300 this->ike_sa
->set_state(this->ike_sa
, IKE_CONNECTING
);
302 rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
);
305 DBG1(DBG_IKE
, "error generating nonce");
308 rng
->allocate_bytes(rng
, NONCE_SIZE
, &this->my_nonce
);
313 chunk_t connect_id
= chunk_empty
;
314 iterator_t
*iterator
;
317 /* check for a ME_CONNECTID notify */
318 iterator
= message
->get_payload_iterator(message
);
319 while (iterator
->iterate(iterator
, (void**)&payload
))
321 if (payload
->get_type(payload
) == NOTIFY
)
323 notify_payload_t
*notify
= (notify_payload_t
*)payload
;
324 notify_type_t type
= notify
->get_notify_type(notify
);
330 chunk_free(&connect_id
);
331 connect_id
= chunk_clone(notify
->get_notification_data(notify
));
332 DBG2(DBG_IKE
, "received ME_CONNECTID %#B", &connect_id
);
339 DBG1(DBG_IKE
, "received %N notify error",
340 notify_type_names
, type
);
343 DBG2(DBG_IKE
, "received %N notify",
344 notify_type_names
, type
);
350 iterator
->destroy(iterator
);
354 charon
->connect_manager
->stop_checks(charon
->connect_manager
,
356 chunk_free(&connect_id
);
361 process_payloads(this, message
);
367 * Implementation of task_t.build for responder
369 static status_t
build_r(private_ike_init_t
*this, message_t
*message
)
374 /* check if we have everything we need */
375 if (this->proposal
== NULL
||
376 this->other_nonce
.len
== 0 || this->my_nonce
.len
== 0)
378 DBG1(DBG_IKE
, "received proposals inacceptable");
379 message
->add_notify(message
, TRUE
, NO_PROPOSAL_CHOSEN
, chunk_empty
);
383 if (this->dh
== NULL
||
384 !this->proposal
->has_dh_group(this->proposal
, this->dh_group
) ||
385 this->dh
->get_shared_secret(this->dh
, &secret
) != SUCCESS
)
389 if (this->proposal
->get_algorithm(this->proposal
, DIFFIE_HELLMAN_GROUP
,
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
));
402 DBG1(DBG_IKE
, "no acceptable proposal found");
410 prf_t
*prf
, *child_prf
;
412 /* Apply SPI if we are rekeying */
413 id
= this->ike_sa
->get_id(this->ike_sa
);
414 id
->set_initiator_spi(id
, this->proposal
->get_spi(this->proposal
));
416 /* setup crypto keys for the rekeyed SA */
417 prf
= this->old_sa
->get_prf(this->old_sa
);
418 child_prf
= this->old_sa
->get_child_prf(this->old_sa
);
419 status
= this->ike_sa
->derive_keys(this->ike_sa
, this->proposal
, secret
,
420 this->other_nonce
, this->my_nonce
,
421 FALSE
, child_prf
, prf
);
425 /* setup crypto keys */
426 status
= this->ike_sa
->derive_keys(this->ike_sa
, this->proposal
, secret
,
427 this->other_nonce
, this->my_nonce
,
430 if (status
!= SUCCESS
)
432 DBG1(DBG_IKE
, "key derivation failed");
433 message
->add_notify(message
, TRUE
, NO_PROPOSAL_CHOSEN
, chunk_empty
);
437 build_payloads(this, message
);
442 * Implementation of task_t.process for initiator
444 static status_t
process_i(private_ike_init_t
*this, message_t
*message
)
448 iterator_t
*iterator
;
451 /* check for erronous notifies */
452 iterator
= message
->get_payload_iterator(message
);
453 while (iterator
->iterate(iterator
, (void**)&payload
))
455 if (payload
->get_type(payload
) == NOTIFY
)
457 notify_payload_t
*notify
= (notify_payload_t
*)payload
;
458 notify_type_t type
= notify
->get_notify_type(notify
);
462 case INVALID_KE_PAYLOAD
:
465 diffie_hellman_group_t bad_group
;
467 bad_group
= this->dh_group
;
468 data
= notify
->get_notification_data(notify
);
469 this->dh_group
= ntohs(*((u_int16_t
*)data
.ptr
));
470 DBG1(DBG_IKE
, "peer didn't accept DH group %N, "
471 "it requested %N", diffie_hellman_group_names
,
472 bad_group
, diffie_hellman_group_names
, this->dh_group
);
474 if (this->old_sa
== NULL
)
475 { /* reset the IKE_SA if we are not rekeying */
476 this->ike_sa
->reset(this->ike_sa
);
479 iterator
->destroy(iterator
);
482 case NAT_DETECTION_SOURCE_IP
:
483 case NAT_DETECTION_DESTINATION_IP
:
484 /* skip, handled in ike_natd_t */
488 chunk_free(&this->cookie
);
489 this->cookie
= chunk_clone(notify
->get_notification_data(notify
));
490 this->ike_sa
->reset(this->ike_sa
);
491 iterator
->destroy(iterator
);
492 DBG2(DBG_IKE
, "received %N notify", notify_type_names
, type
);
499 DBG1(DBG_IKE
, "received %N notify error",
500 notify_type_names
, type
);
501 iterator
->destroy(iterator
);
504 DBG2(DBG_IKE
, "received %N notify",
505 notify_type_names
, type
);
511 iterator
->destroy(iterator
);
513 process_payloads(this, message
);
515 /* check if we have everything */
516 if (this->proposal
== NULL
||
517 this->other_nonce
.len
== 0 || this->my_nonce
.len
== 0)
519 DBG1(DBG_IKE
, "peers proposal selection invalid");
523 if (this->dh
== NULL
||
524 !this->proposal
->has_dh_group(this->proposal
, this->dh_group
) ||
525 this->dh
->get_shared_secret(this->dh
, &secret
) != SUCCESS
)
527 DBG1(DBG_IKE
, "peer DH group selection invalid");
531 /* Apply SPI if we are rekeying */
535 prf_t
*prf
, *child_prf
;
537 id
= this->ike_sa
->get_id(this->ike_sa
);
538 id
->set_responder_spi(id
, this->proposal
->get_spi(this->proposal
));
540 /* setup crypto keys for the rekeyed SA */
541 prf
= this->old_sa
->get_prf(this->old_sa
);
542 child_prf
= this->old_sa
->get_child_prf(this->old_sa
);
543 status
= this->ike_sa
->derive_keys(this->ike_sa
, this->proposal
, secret
,
544 this->my_nonce
, this->other_nonce
,
545 TRUE
, child_prf
, prf
);
549 /* setup crypto keys for a new SA */
550 status
= this->ike_sa
->derive_keys(this->ike_sa
, this->proposal
, secret
,
551 this->my_nonce
, this->other_nonce
,
554 if (status
!= SUCCESS
)
556 DBG1(DBG_IKE
, "key derivation failed");
564 * Implementation of task_t.get_type
566 static task_type_t
get_type(private_ike_init_t
*this)
572 * Implementation of task_t.get_type
574 static chunk_t
get_lower_nonce(private_ike_init_t
*this)
576 if (memcmp(this->my_nonce
.ptr
, this->other_nonce
.ptr
,
577 min(this->my_nonce
.len
, this->other_nonce
.len
)) < 0)
579 return this->my_nonce
;
583 return this->other_nonce
;
588 * Implementation of task_t.migrate
590 static void migrate(private_ike_init_t
*this, ike_sa_t
*ike_sa
)
592 DESTROY_IF(this->proposal
);
593 DESTROY_IF(this->dh
);
594 chunk_free(&this->other_nonce
);
596 this->ike_sa
= ike_sa
;
597 this->proposal
= NULL
;
598 this->dh
= lib
->crypto
->create_dh(lib
->crypto
, this->dh_group
);
602 * Implementation of task_t.destroy
604 static void destroy(private_ike_init_t
*this)
606 DESTROY_IF(this->proposal
);
607 DESTROY_IF(this->dh
);
608 chunk_free(&this->my_nonce
);
609 chunk_free(&this->other_nonce
);
610 chunk_free(&this->cookie
);
615 * Described in header.
617 ike_init_t
*ike_init_create(ike_sa_t
*ike_sa
, bool initiator
, ike_sa_t
*old_sa
)
619 private_ike_init_t
*this = malloc_thing(private_ike_init_t
);
621 this->public.get_lower_nonce
= (chunk_t(*)(ike_init_t
*))get_lower_nonce
;
622 this->public.task
.get_type
= (task_type_t(*)(task_t
*))get_type
;
623 this->public.task
.migrate
= (void(*)(task_t
*,ike_sa_t
*))migrate
;
624 this->public.task
.destroy
= (void(*)(task_t
*))destroy
;
627 this->public.task
.build
= (status_t(*)(task_t
*,message_t
*))build_i
;
628 this->public.task
.process
= (status_t(*)(task_t
*,message_t
*))process_i
;
632 this->public.task
.build
= (status_t(*)(task_t
*,message_t
*))build_r
;
633 this->public.task
.process
= (status_t(*)(task_t
*,message_t
*))process_r
;
636 this->ike_sa
= ike_sa
;
637 this->initiator
= initiator
;
638 this->dh_group
= MODP_NONE
;
640 this->my_nonce
= chunk_empty
;
641 this->other_nonce
= chunk_empty
;
642 this->cookie
= chunk_empty
;
643 this->proposal
= NULL
;
645 this->old_sa
= old_sa
;
648 return &this->public;