4 * @brief Class ike_sa_t. An object of this type is managed by an
5 * ike_sa_manager_t object and represents an IKE_SA
10 * Copyright (C) 2005 Jan Hutter, Martin Willi
11 * Hochschule fuer Technik Rapperswil
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>.
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
28 #include "definitions.h"
29 #include "utils/allocator.h"
30 #include "utils/linked_list.h"
31 #include "utils/logger_manager.h"
32 #include "utils/randomizer.h"
33 #include "transforms/diffie_hellman.h"
34 #include "payloads/sa_payload.h"
35 #include "payloads/nonce_payload.h"
36 #include "payloads/ke_payload.h"
37 #include "payloads/transform_substructure.h"
38 #include "payloads/transform_attribute.h"
42 * States in which a IKE_SA can actually be
44 typedef enum ike_sa_state_e ike_sa_state_t
;
49 * IKE_SA is is not in a state
54 * A IKE_SA_INIT-message was sent: role initiator
56 IKE_SA_INIT_REQUESTED
= 2,
59 * A IKE_SA_INIT-message was replied: role responder
61 IKE_SA_INIT_RESPONDED
= 3,
64 * An IKE_AUTH-message was sent after a successful
65 * IKE_SA_INIT-exchange: role initiator
67 IKE_AUTH_REQUESTED
= 4,
70 * An IKE_AUTH-message was replied: role responder.
71 * In this state, all the informations for an IKE_SA
72 * and one CHILD_SA are known.
74 IKE_SA_INITIALIZED
= 5
78 * string mappings for ike_sa_state
80 mapping_t ike_sa_state_m
[] = {
81 {NO_STATE
, "NO_STATE"},
82 {IKE_SA_INIT_REQUESTED
, "IKE_SA_INIT_REQUESTED"},
83 {IKE_SA_INIT_RESPONDED
, "IKE_SA_INIT_RESPONDED"},
84 {IKE_AUTH_REQUESTED
, "IKE_AUTH_REQUESTED"},
85 {IKE_SA_INITIALIZED
, "IKE_SA_INITIALIZED"},
91 * Private data of an message_t object
93 typedef struct private_ike_sa_s private_ike_sa_t
;
95 struct private_ike_sa_s
{
98 * Public part of a ike_sa_t object
103 * Builds an empty IKEv2-Message
105 * Depending on the type of message (request or response), the message id is
106 * either message_id_out or message_id_in.
109 * @param this calling object
110 * @param type exchange type of new message
111 * @param request TRUE, if message has to be a request
112 * @param message new message is stored at this location
117 status_t (*build_message
) (private_ike_sa_t
*this, exchange_type_t type
, bool request
, message_t
**message
);
119 status_t (*build_sa_payload
) (private_ike_sa_t
*this, sa_payload_t
**payload
);
120 status_t (*build_ke_payload
) (private_ike_sa_t
*this, ke_payload_t
**payload
);
121 status_t (*build_nonce_payload
) (private_ike_sa_t
*this, nonce_payload_t
**payload
);
123 status_t (*create_delete_job
) (private_ike_sa_t
*this);
124 status_t (*resend_last_reply
) (private_ike_sa_t
*this);
127 status_t (*transto_ike_sa_init_requested
) (private_ike_sa_t
*this, char *name
);
128 status_t (*transto_ike_sa_init_responded
) (private_ike_sa_t
*this, message_t
*message
);
129 status_t (*transto_ike_auth_requested
) (private_ike_sa_t
*this, message_t
*message
);
133 * Identifier for the current IKE_SA
135 ike_sa_id_t
*ike_sa_id
;
138 * Linked List containing the child sa's of the current IKE_SA
140 linked_list_t
*child_sas
;
143 * Current state of the IKE_SA
145 ike_sa_state_t state
;
148 * this SA's source for random data
150 randomizer_t
*randomizer
;
153 * contains the last responded message
156 message_t
*last_responded_message
;
159 * contains the last requested message
162 message_t
*last_requested_message
;
165 * Informations of this host
172 * Informations of the other host
181 * Diffie Hellman object used to compute shared secret
183 diffie_hellman_t
*diffie_hellman
;
186 * Diffie Hellman group number
188 u_int16_t dh_group_number
;
191 * Priority used get matching dh_group number
193 u_int16_t dh_group_priority
;
198 linked_list_t
*proposals
;
206 * received nonce value
208 chunk_t received_nonce
;
213 * next message id to receive
215 u_int32_t message_id_in
;
218 * next message id to send
220 u_int32_t message_id_out
;
223 * a logger for this IKE_SA
229 * @brief implements function process_message of private_ike_sa_t
231 static status_t
process_message (private_ike_sa_t
*this, message_t
*message
)
233 u_int32_t message_id
;
235 exchange_type_t exchange_type
;
236 /* we must process each request or response from remote host
237 * the check if a given message is possible for a given state is done in here
240 /* find out type of message (request or response) */
241 is_request
= message
->get_request(message
);
242 exchange_type
= message
->get_exchange_type(message
);
244 this->logger
->log(this->logger
, CONTROL
|MORE
, "Process %s message of exchange type %s",(is_request
) ?
"REQUEST" : "RESPONSE",
245 mapping_find(exchange_type_m
,exchange_type
));
247 message_id
= message
->get_message_id(message
);
250 * It has to be checked, if the message has to be resent cause of lost packets!
252 if (is_request
&& ( message_id
== (this->message_id_in
- 1)))
254 /* message can be resent ! */
255 this->logger
->log(this->logger
, CONTROL
|MORE
, "Resent message detected. Send stored reply");
256 return (this->resend_last_reply(this));
259 /* Now, the message id is checked for request AND reply */
262 /* In a request, the message has to be this->message_id_in (other case is already handled) */
263 if (message_id
!= this->message_id_in
)
265 this->logger
->log(this->logger
, ERROR
| MORE
, "Message request with message id %d received, but %d expected",message_id
,this->message_id_in
);
271 /* In a reply, the message has to be this->message_id_out -1 cause it is the reply to the last sent message*/
272 if (message_id
!= (this->message_id_out
- 1))
274 this->logger
->log(this->logger
, ERROR
| MORE
, "Message reply with message id %d received, but %d expected",message_id
,this->message_id_in
);
279 /* Now, the exchange type is checked and the appropriate transition handler is called*/
280 switch (message
->get_exchange_type(message
))
285 if (message
->get_request(message
)) {
286 if (this->state
== NO_STATE
)
288 /* state transission NO_STATE => IKE_SA_INIT_RESPONDED */
289 return this->transto_ike_sa_init_responded(this, message
);
294 if (this->state
== IKE_SA_INIT_REQUESTED
)
296 /* state transission IKE_SA_INIT_REQUESTED => IKE_AUTH_REQUESTED*/
297 return this->transto_ike_auth_requested(this, message
);
304 if (this->state
<= IKE_SA_INIT_REQUESTED
)
306 this->logger
->log(this->logger
, ERROR
| MORE
, "Current state %s of IKE_SA does not allow IKE_AUTH message",mapping_find(ike_sa_state_m
,this->state
));
311 case CREATE_CHILD_SA
:
313 if (this->state
< IKE_SA_INITIALIZED
)
315 this->logger
->log(this->logger
, ERROR
| MORE
, "Current state %s of IKE_SA does not allow CREATE_CHILD_SA message",mapping_find(ike_sa_state_m
,this->state
));
326 this->logger
->log(this->logger
, ERROR
, "processing %s-message not supported.",
327 mapping_find(exchange_type_m
,message
->get_exchange_type(message
)));
328 return NOT_SUPPORTED
;
331 this->logger
->log(this->logger
, CONTROL
, "received %s-message in state %s, not handled.",
332 mapping_find(exchange_type_m
, message
->get_exchange_type(message
)),
333 mapping_find(ike_sa_state_m
, this->state
));
334 return INVALID_STATE
;
338 * @brief Implements function build_message of private_ike_sa_t.
340 static status_t
build_message(private_ike_sa_t
*this, exchange_type_t type
, bool request
, message_t
**message
)
343 message_t
*new_message
;
344 host_t
*source
, *destination
;
346 this ->logger
->log(this->logger
, CONTROL
|MORE
, "build empty message");
347 new_message
= message_create();
348 if (new_message
== NULL
)
350 this->logger
->log(this->logger
, ERROR
, "Fatal error: could not create empty message object");
354 status
= this->me
.host
->clone(this->me
.host
, &source
);
355 if (status
!= SUCCESS
)
357 this->logger
->log(this->logger
, ERROR
, "Fatal error: could not clone my host information");
358 new_message
->destroy(new_message
);
361 status
= this->other
.host
->clone(this->other
.host
, &destination
);
362 if (status
!= SUCCESS
)
364 this->logger
->log(this->logger
, ERROR
, "Fatal error: could not clone other host information");
365 source
->destroy(source
);
366 new_message
->destroy(new_message
);
370 new_message
->set_source(new_message
, source
);
371 new_message
->set_destination(new_message
, destination
);
373 new_message
->set_exchange_type(new_message
, type
);
374 new_message
->set_request(new_message
, request
);
376 new_message
->set_message_id(new_message
, (request
) ?
this->message_id_out
: this->message_id_in
);
378 status
= new_message
->set_ike_sa_id(new_message
, this->ike_sa_id
);
379 if (status
!= SUCCESS
)
381 this->logger
->log(this->logger
, ERROR
, "Fatal error: could not set ike_sa_id of message");
382 new_message
->destroy(new_message
);
386 *message
= new_message
;
392 * @brief Implements function transto_ike_sa_init_requested of private_ike_sa_t.
394 static status_t
transto_ike_sa_init_requested(private_ike_sa_t
*this, char *name
)
401 this->logger
->log(this->logger
, CONTROL
, "Initializing connection %s",name
);
403 status
= global_configuration_manager
->get_local_host(global_configuration_manager
, name
, &(this->me
.host
));
404 if (status
!= SUCCESS
)
406 this->logger
->log(this->logger
, ERROR
| MORE
, "Could not retrieve local host configuration information for %s",name
);
410 status
= global_configuration_manager
->get_remote_host(global_configuration_manager
, name
, &(this->other
.host
));
411 if (status
!= SUCCESS
)
413 this->logger
->log(this->logger
, ERROR
| MORE
, "Could not retrieve remote host configuration information for %s",name
);
417 status
= global_configuration_manager
->get_dh_group_number(global_configuration_manager
, name
, &(this->ike_sa_init_data
.dh_group_number
), this->ike_sa_init_data
.dh_group_priority
);
418 if (status
!= SUCCESS
)
420 this->logger
->log(this->logger
, ERROR
| MORE
, "Could not retrieve DH group number for %s",name
);
424 if (this->ike_sa_init_data
.diffie_hellman
!= NULL
)
426 this->logger
->log(this->logger
, ERROR
, "Object of type diffie_hellman_t already existing!");
429 this ->logger
->log(this->logger
, CONTROL
|MOST
, "create diffie hellman object");
430 this->ike_sa_init_data
.diffie_hellman
= diffie_hellman_create(this->ike_sa_init_data
.dh_group_number
);
431 if (this->ike_sa_init_data
.diffie_hellman
== NULL
)
433 this->logger
->log(this->logger
, ERROR
, "Object of type diffie_hellman_t could not be created!");
437 if (this->ike_sa_init_data
.sent_nonce
.ptr
!= NULL
)
439 this->logger
->log(this->logger
, ERROR
, "Nonce for IKE_SA_INIT phase already existing!");
443 if (this->randomizer
->allocate_pseudo_random_bytes(this->randomizer
, 16, &(this->ike_sa_init_data
.sent_nonce
)) != SUCCESS
)
445 this->logger
->log(this->logger
, ERROR
, "Could not create nonce!");
450 /* going to build message */
451 status
= this->build_message(this, IKE_SA_INIT
, TRUE
, &message
);
452 if (status
!= SUCCESS
)
454 this->logger
->log(this->logger
, ERROR
, "Could not build message");
458 /* build SA payload */
459 status
= this->build_sa_payload(this, (sa_payload_t
**)&payload
);
460 if (status
!= SUCCESS
)
462 this->logger
->log(this->logger
, ERROR
, "Could not build SA payload");
463 message
->destroy(message
);
467 this ->logger
->log(this->logger
, CONTROL
|MOST
, "add SA payload to message");
468 status
= message
->add_payload(message
, payload
);
469 if (status
!= SUCCESS
)
471 this->logger
->log(this->logger
, ERROR
, "Could not add SA payload to message");
472 message
->destroy(message
);
477 /* build KE payload */
478 status
= this->build_ke_payload(this,(ke_payload_t
**) &payload
);
479 if (status
!= SUCCESS
)
481 this->logger
->log(this->logger
, ERROR
, "Could not build KE payload");
482 message
->destroy(message
);
486 this ->logger
->log(this->logger
, CONTROL
|MOST
, "add KE payload to message");
487 status
= message
->add_payload(message
, payload
);
488 if (status
!= SUCCESS
)
490 this->logger
->log(this->logger
, ERROR
, "Could not add KE payload to message");
491 message
->destroy(message
);
495 /* build Nonce payload */
496 status
= this->build_nonce_payload(this, (nonce_payload_t
**)&payload
);
497 if (status
!= SUCCESS
)
499 this->logger
->log(this->logger
, ERROR
, "Could not build NONCE payload");
500 message
->destroy(message
);
504 this ->logger
->log(this->logger
, CONTROL
|MOST
, "add nonce payload to message");
505 status
= message
->add_payload(message
, payload
);
506 if (status
!= SUCCESS
)
508 this->logger
->log(this->logger
, ERROR
, "Could not add nonce payload to message");
509 message
->destroy(message
);
513 this ->logger
->log(this->logger
, CONTROL
|MOST
, "generate packet from message");
514 status
= message
->generate(message
, &packet
);
515 if (status
!= SUCCESS
)
517 this->logger
->log(this->logger
, ERROR
, "Fatal error: could not generate packet from message");
518 message
->destroy(message
);
522 this ->logger
->log(this->logger
, CONTROL
|MOST
, "Add packet to global send queue");
523 status
= global_send_queue
->add(global_send_queue
, packet
);
524 if (status
!= SUCCESS
)
526 this->logger
->log(this->logger
, ERROR
, "Could not add packet to send queue");
527 message
->destroy(message
);
531 if ( this->last_requested_message
!= NULL
)
533 /* destroy message */
534 this ->logger
->log(this->logger
, CONTROL
|MOST
, "Destroy stored last requested message");
535 this->last_requested_message
->destroy(this->last_requested_message
);
538 this->last_requested_message
= message
;
540 /* message counter can now be increased */
541 this->message_id_out
++;
543 /* states has NOW changed :-) */
544 this ->logger
->log(this->logger
, CONTROL
|MORE
, "Change state of IKE_SA from %s to %s",mapping_find(ike_sa_state_m
,this->state
),mapping_find(ike_sa_state_m
,IKE_SA_INIT_REQUESTED
) );
545 this->state
= IKE_SA_INIT_REQUESTED
;
550 static status_t
transto_ike_sa_init_responded(private_ike_sa_t
*this, message_t
*request
)
553 linked_list_iterator_t
*payloads
;
555 host_t
*source
, *destination
;
557 /* this is the first message we process, so copy host infos */
558 request
->get_source(request
, &source
);
559 request
->get_destination(request
, &destination
);
560 /* we need to clone them, since we destroy the message later */
561 destination
->clone(destination
, &(this->me
.host
));
562 source
->clone(source
, &(this->other
.host
));
564 /* parse incoming message */
565 status
= request
->parse_body(request
);
566 if (status
!= SUCCESS
)
568 this->logger
->log(this->logger
, ERROR
| MORE
, "Could not parse body of request message");
569 this->create_delete_job(this);
573 /* iterate over incoming payloads. We can be sure, the message contains only accepted payloads! */
574 status
= request
->get_payload_iterator(request
, &payloads
);
575 if (status
!= SUCCESS
)
577 this->logger
->log(this->logger
, ERROR
, "Fatal error: Could not get payload interator");
578 this->create_delete_job(this);
582 while (payloads
->has_next(payloads
))
586 /* get current payload */
587 payloads
->current(payloads
, (void**)&payload
);
589 this->logger
->log(this->logger
, CONTROL
|MORE
, "Processing payload of type %s", mapping_find(payload_type_m
, payload
->get_type(payload
)));
590 switch (payload
->get_type(payload
))
592 case SECURITY_ASSOCIATION
:
594 sa_payload_t
*sa_payload
= (sa_payload_t
*)payload
;
595 linked_list_iterator_t
*suggested_proposals
, *accepted_proposals
;
597 /* create a list for accepted proposals */
598 if (this->ike_sa_init_data
.proposals
== NULL
)
600 this->ike_sa_init_data
.proposals
= linked_list_create();
604 /* delete stored proposals */
605 while (this->ike_sa_init_data
.proposals
->get_count(this->ike_sa_init_data
.proposals
) > 0)
607 proposal_substructure_t
*current_proposal
;
608 this->ike_sa_init_data
.proposals
->remove_first(this->ike_sa_init_data
.proposals
,(void **)¤t_proposal
);
609 current_proposal
->destroy(current_proposal
);
612 if (this->ike_sa_init_data
.proposals
== NULL
)
614 /* destroy iterator and leave */
615 this->logger
->log(this->logger
, ERROR
, "Fatal error: Could not create list for proposals");
616 payloads
->destroy(payloads
);
617 this->create_delete_job(this);
620 status
= this->ike_sa_init_data
.proposals
->create_iterator(this->ike_sa_init_data
.proposals
, &accepted_proposals
, FALSE
);
621 if (status
!= SUCCESS
)
623 this->logger
->log(this->logger
, ERROR
, "Fatal error: Could not create iterator on list for proposals");
624 payloads
->destroy(payloads
);
625 this->create_delete_job(this);
629 /* get the list of suggested proposals */
630 status
= sa_payload
->create_proposal_substructure_iterator(sa_payload
, &suggested_proposals
, TRUE
);
631 if (status
!= SUCCESS
)
633 this->logger
->log(this->logger
, ERROR
, "Fatal error: Could not create iterator on suggested proposals");
634 accepted_proposals
->destroy(accepted_proposals
);
635 payloads
->destroy(payloads
);
636 this->create_delete_job(this);
640 /* now let the configuration-manager select a subset of the proposals */
641 status
= global_configuration_manager
->select_proposals_for_host(global_configuration_manager
,
642 this->other
.host
, suggested_proposals
, accepted_proposals
);
643 if (status
!= SUCCESS
)
645 this->logger
->log(this->logger
, CONTROL
| MORE
, "No proposal of suggested proposals selected");
646 suggested_proposals
->destroy(suggested_proposals
);
647 accepted_proposals
->destroy(accepted_proposals
);
648 payloads
->destroy(payloads
);
649 this->create_delete_job(this);
653 /* iterators are not needed anymore */
654 suggested_proposals
->destroy(suggested_proposals
);
655 accepted_proposals
->destroy(accepted_proposals
);
657 /* ok, we have what we need for sa_payload (proposals are stored in this->proposals)*/
662 ke_payload_t
*ke_payload
= (ke_payload_t
*)payload
;
663 diffie_hellman_group_t group
;
664 diffie_hellman_t
*dh
;
667 group
= ke_payload
->get_dh_group_number(ke_payload
);
669 status
= global_configuration_manager
->is_dh_group_allowed_for_host(global_configuration_manager
,
670 this->other
.host
, group
, &allowed_group
);
672 if (status
!= SUCCESS
)
674 this->logger
->log(this->logger
, ERROR
| MORE
, "Could not get informations about DH group");
675 payloads
->destroy(payloads
);
676 this->create_delete_job(this);
681 /** @todo Send info reply */
684 /* create diffie hellman object to handle DH exchange */
685 dh
= diffie_hellman_create(group
);
688 this->logger
->log(this->logger
, ERROR
, "Could not generate DH object");
689 payloads
->destroy(payloads
);
690 this->create_delete_job(this);
694 this->logger
->log(this->logger
, CONTROL
| MORE
, "Set other DH public value");
696 status
= dh
->set_other_public_value(dh
, ke_payload
->get_key_exchange_data(ke_payload
));
697 if (status
!= SUCCESS
)
699 this->logger
->log(this->logger
, ERROR
, "Could not set other DH public value");
701 payloads
->destroy(payloads
);
702 this->create_delete_job(this);
705 /** @todo destroy if there is already one */
706 this->ike_sa_init_data
.diffie_hellman
= dh
;
711 nonce_payload_t
*nonce_payload
= (nonce_payload_t
*)payload
;
714 this->logger
->log(this->logger
, CONTROL
| MORE
, "Get nonce value and store it");
715 nonce_payload
->get_nonce(nonce_payload
, &nonce
);
716 /** @todo free if there is already one */
717 this->ike_sa_init_data
.received_nonce
.ptr
= allocator_clone_bytes(nonce
.ptr
, nonce
.len
);
718 this->ike_sa_init_data
.received_nonce
.len
= nonce
.len
;
719 if (this->ike_sa_init_data
.received_nonce
.ptr
== NULL
)
721 payloads
->destroy(payloads
);
722 this->create_delete_job(this);
735 /* iterator can be destroyed */
736 payloads
->destroy(payloads
);
738 this->logger
->log(this->logger
, CONTROL
| MORE
, "Request successfully handled. Going to create reply.");
740 /* set up the reply */
741 status
= this->build_message(this, IKE_SA_INIT
, FALSE
, &response
);
742 if (status
!= SUCCESS
)
744 this->logger
->log(this->logger
, ERROR
, "Could not create empty message");
745 this->create_delete_job(this);
750 response
->destroy(response
);
756 static status_t
transto_ike_auth_requested(private_ike_sa_t
*this, message_t
*response
)
759 linked_list_iterator_t
*payloads
;
762 /* parse incoming message */
763 status
= response
->parse_body(response
);
764 if (status
!= SUCCESS
)
766 this->logger
->log(this->logger
, ERROR
, "Could not parse body");
769 /* iterate over incoming payloads */
770 status
= response
->get_payload_iterator(response
, &payloads
);
771 if (status
!= SUCCESS
)
773 response
->destroy(response
);
776 while (payloads
->has_next(payloads
))
779 payloads
->current(payloads
, (void**)&payload
);
781 this->logger
->log(this->logger
, CONTROL
|MORE
, "Processing payload %s", mapping_find(payload_type_m
, payload
->get_type(payload
)));
782 switch (payload
->get_type(payload
))
784 // case SECURITY_ASSOCIATION:
786 // sa_payload_t *sa_payload = (sa_payload_t*)payload;
787 // linked_list_iterator_t *suggested_proposals, *accepted_proposals;
788 // /* create a list for accepted proposals */
789 // if (this->ike_sa_init_data.proposals == NULL) {
790 // this->ike_sa_init_data.proposals = linked_list_create();
794 // /** @todo destroy list contents */
796 // if (this->ike_sa_init_data.proposals == NULL)
798 // payloads->destroy(payloads);
799 // return OUT_OF_RES;
801 // status = this->ike_sa_init_data.proposals->create_iterator(this->ike_sa_init_data.proposals, &accepted_proposals, FALSE);
802 // if (status != SUCCESS)
804 // payloads->destroy(payloads);
808 // /* get the list of suggested proposals */
809 // status = sa_payload->create_proposal_substructure_iterator(sa_payload, &suggested_proposals, TRUE);
810 // if (status != SUCCESS)
812 // accepted_proposals->destroy(accepted_proposals);
813 // payloads->destroy(payloads);
817 // /* now let the configuration-manager select a subset of the proposals */
818 // status = global_configuration_manager->select_proposals_for_host(global_configuration_manager,
819 // this->other.host, suggested_proposals, accepted_proposals);
820 // if (status != SUCCESS)
822 // suggested_proposals->destroy(suggested_proposals);
823 // accepted_proposals->destroy(accepted_proposals);
824 // payloads->destroy(payloads);
828 // suggested_proposals->destroy(suggested_proposals);
829 // accepted_proposals->destroy(accepted_proposals);
831 // /* ok, we have what we need for sa_payload */
836 ke_payload_t
*ke_payload
= (ke_payload_t
*)payload
;
837 diffie_hellman_t
*dh
;
838 chunk_t shared_secret
;
840 dh
= this->ike_sa_init_data
.diffie_hellman
;
845 status
= dh
->set_other_public_value(dh
, ke_payload
->get_key_exchange_data(ke_payload
));
846 if (status
!= SUCCESS
)
849 payloads
->destroy(payloads
);
853 status
= dh
->get_shared_secret(dh
, &shared_secret
);
855 this->logger
->log_chunk(this->logger
, RAW
, "Shared secret", &shared_secret
);
861 nonce_payload_t
*nonce_payload
= (nonce_payload_t
*)payload
;
864 nonce_payload
->get_nonce(nonce_payload
, &nonce
);
865 /** @todo free if there is already one */
866 this->ike_sa_init_data
.received_nonce
.ptr
= allocator_clone_bytes(nonce
.ptr
, nonce
.len
);
867 this->ike_sa_init_data
.received_nonce
.len
= nonce
.len
;
868 if (this->ike_sa_init_data
.received_nonce
.ptr
== NULL
)
870 payloads
->destroy(payloads
);
883 payloads
->destroy(payloads
);
887 /* set up the reply */
888 status
= this->build_message(this, IKE_SA_INIT
, FALSE
, &response
);
889 if (status
!= SUCCESS
)
891 this->create_delete_job(this);
895 response
->destroy(response
);
903 * @brief implements function process_configuration of private_ike_sa_t
905 static status_t
initialize_connection(private_ike_sa_t
*this, char *name
)
907 /* work is done in transto_ike_sa_init_requested */
908 return (this->transto_ike_sa_init_requested(this,name
));
912 * @brief implements function private_ike_sa_t.get_id
914 static ike_sa_id_t
* get_id(private_ike_sa_t
*this)
916 return this->ike_sa_id
;
920 * implements private_ike_sa_t.build_sa_payload
922 static status_t
build_sa_payload(private_ike_sa_t
*this, sa_payload_t
**payload
)
924 sa_payload_t
* sa_payload
;
925 linked_list_iterator_t
*iterator
;
928 this->logger
->log(this->logger
, CONTROL
|MORE
, "building sa payload");
930 sa_payload
= sa_payload_create();
931 if (sa_payload
== NULL
)
935 status
= sa_payload
->create_proposal_substructure_iterator(sa_payload
, &iterator
, FALSE
);
936 if (status
!= SUCCESS
)
938 sa_payload
->destroy(sa_payload
);
941 status
= global_configuration_manager
->get_proposals_for_host(global_configuration_manager
, this->other
.host
, iterator
);
942 if (status
!= SUCCESS
)
944 sa_payload
->destroy(sa_payload
);
948 *payload
= sa_payload
;
953 static status_t
build_ke_payload(private_ike_sa_t
*this, ke_payload_t
**payload
)
955 ke_payload_t
*ke_payload
;
959 this->logger
->log(this->logger
, CONTROL
|MORE
, "building ke payload");
961 if (this->state
!= NO_STATE
)
963 this->logger
->log(this->logger
, ERROR
, "KE payload in state %s not supported",mapping_find(ike_sa_state_m
,this->state
));
967 switch(this->ike_sa_id
->is_initiator(this->ike_sa_id
))
971 this ->logger
->log(this->logger
, CONTROL
|MORE
, "get public dh value to send in ke payload");
972 status
= this->ike_sa_init_data
.diffie_hellman
->get_my_public_value(this->ike_sa_init_data
.diffie_hellman
,&key_data
);
973 if (status
!= SUCCESS
)
975 this->logger
->log(this->logger
, ERROR
, "Could not get my DH public value");
979 ke_payload
= ke_payload_create();
980 if (ke_payload
== NULL
)
982 this->logger
->log(this->logger
, ERROR
, "Could not create KE payload");
983 allocator_free_chunk(key_data
);
986 ke_payload
->set_dh_group_number(ke_payload
, MODP_1024_BIT
);
987 if (ke_payload
->set_key_exchange_data(ke_payload
, key_data
) != SUCCESS
)
989 this->logger
->log(this->logger
, ERROR
, "Could not set key exchange data of KE payload");
990 ke_payload
->destroy(ke_payload
);
991 allocator_free_chunk(key_data
);
994 allocator_free_chunk(key_data
);
996 *payload
= ke_payload
;
1009 * implements private_ike_sa_t.build_nonce_payload
1011 static status_t
build_nonce_payload(private_ike_sa_t
*this, nonce_payload_t
**payload
)
1013 nonce_payload_t
*nonce_payload
;
1015 this->logger
->log(this->logger
, CONTROL
|MORE
, "building nonce payload");
1016 nonce_payload
= nonce_payload_create();
1017 if (nonce_payload
== NULL
)
1022 nonce_payload
->set_nonce(nonce_payload
, this->ike_sa_init_data
.sent_nonce
);
1024 *payload
= nonce_payload
;
1030 * @brief implements function resend_last_reply of private_ike_sa_t
1032 status_t
resend_last_reply (private_ike_sa_t
*this)
1037 status
= this->last_responded_message
->generate(this->last_responded_message
, &packet
);
1038 if (status
!= SUCCESS
)
1040 this->logger
->log(this->logger
, ERROR
, "Could not generate message to resent");
1044 status
= global_send_queue
->add(global_send_queue
, packet
);
1045 if (status
!= SUCCESS
)
1047 this->logger
->log(this->logger
, ERROR
, "Could not add packet to send queue");
1048 packet
->destroy(packet
);
1054 status_t
create_delete_job (private_ike_sa_t
*this)
1059 this->logger
->log(this->logger
, CONTROL
| MORE
, "Going to create job to delete this IKE_SA");
1061 delete_job
= (job_t
*) delete_ike_sa_job_create(this->ike_sa_id
);
1062 if (delete_job
== NULL
)
1064 this->logger
->log(this->logger
, ERROR
, "Job to delete IKE SA could not be created");
1068 status
= global_job_queue
->add(global_job_queue
,delete_job
);
1069 if (status
!= SUCCESS
)
1071 this->logger
->log(this->logger
, ERROR
, "%s Job to delete IKE SA could not be added to job queue",mapping_find(status_m
,status
));
1072 delete_job
->destroy_all(delete_job
);
1079 * @brief implements function destroy of private_ike_sa_t
1081 static status_t
destroy (private_ike_sa_t
*this)
1084 this->logger
->log(this->logger
, CONTROL
| MORE
, "Going to destroy IKE_SA");
1086 /* destroy child sa's */
1087 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy all child_sa's");
1088 while (this->child_sas
->get_count(this->child_sas
) > 0)
1091 if (this->child_sas
->remove_first(this->child_sas
,&child_sa
) != SUCCESS
)
1095 /* destroy child sa */
1097 this->child_sas
->destroy(this->child_sas
);
1099 /* destroy ike_sa_id */
1100 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy assigned ike_sa_id");
1101 this->ike_sa_id
->destroy(this->ike_sa_id
);
1103 /* destroy stored requested message */
1104 if (this->last_requested_message
!= NULL
)
1106 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy last requested message");
1107 this->last_requested_message
->destroy(this->last_requested_message
);
1110 /* destroy stored responded messages */
1111 if (this->last_responded_message
!= NULL
)
1113 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy last responded message");
1114 this->last_responded_message
->destroy(this->last_responded_message
);
1117 /* destroy stored proposal */
1118 if (this->ike_sa_init_data
.proposals
!= NULL
)
1120 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy stored proposals");
1121 while (this->ike_sa_init_data
.proposals
->get_count(this->ike_sa_init_data
.proposals
) > 0)
1123 proposal_substructure_t
*current_proposal
;
1124 this->ike_sa_init_data
.proposals
->remove_first(this->ike_sa_init_data
.proposals
,(void **)¤t_proposal
);
1125 current_proposal
->destroy(current_proposal
);
1127 this->ike_sa_init_data
.proposals
->destroy(this->ike_sa_init_data
.proposals
);
1130 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy randomizer");
1131 this->randomizer
->destroy(this->randomizer
);
1134 /* destroy ike_sa_init data */
1135 this->logger
->log(this->logger
, CONTROL
| MOST
, "Going to destroy ike_sa_init data");
1136 if (this->ike_sa_init_data
.diffie_hellman
!= NULL
)
1138 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy diffie hellman object");
1139 this->ike_sa_init_data
.diffie_hellman
->destroy(this->ike_sa_init_data
.diffie_hellman
);
1141 if (this->ike_sa_init_data
.sent_nonce
.ptr
!= NULL
)
1143 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy sent nonce data");
1144 allocator_free_chunk(this->ike_sa_init_data
.sent_nonce
);
1146 if (this->ike_sa_init_data
.received_nonce
.ptr
!= NULL
)
1148 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy received nonce data");
1149 allocator_free_chunk(this->ike_sa_init_data
.received_nonce
);
1152 if (this->me
.host
!= NULL
)
1154 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy host informations of me");
1155 this->me
.host
->destroy(this->me
.host
);
1158 if (this->other
.host
!= NULL
)
1160 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy host informations of other");
1161 this->other
.host
->destroy(this->other
.host
);
1165 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy logger of IKE_SA");
1166 global_logger_manager
->destroy_logger(global_logger_manager
, this->logger
);
1168 allocator_free(this);
1173 * Described in Header
1175 ike_sa_t
* ike_sa_create(ike_sa_id_t
*ike_sa_id
)
1177 private_ike_sa_t
*this = allocator_alloc_thing(private_ike_sa_t
);
1183 /* Public functions */
1184 this->public.process_message
= (status_t(*)(ike_sa_t
*, message_t
*)) process_message
;
1185 this->public.initialize_connection
= (status_t(*)(ike_sa_t
*, char*)) initialize_connection
;
1186 this->public.get_id
= (ike_sa_id_t
*(*)(ike_sa_t
*)) get_id
;
1187 this->public.destroy
= (status_t(*)(ike_sa_t
*))destroy
;
1189 /* private functions */
1190 this->build_sa_payload
= build_sa_payload
;
1191 this->build_nonce_payload
= build_nonce_payload
;
1192 this->build_ke_payload
= build_ke_payload
;
1193 this->build_message
= build_message
;
1194 this->transto_ike_sa_init_requested
= transto_ike_sa_init_requested
;
1195 this->transto_ike_sa_init_responded
= transto_ike_sa_init_responded
;
1196 this->transto_ike_auth_requested
= transto_ike_auth_requested
;
1197 this->resend_last_reply
= resend_last_reply
;
1198 this->create_delete_job
= create_delete_job
;
1201 /* initialize private fields */
1202 this->logger
= global_logger_manager
->create_logger(global_logger_manager
, IKE_SA
, NULL
);
1203 if (this->logger
== NULL
)
1205 allocator_free(this);
1208 if (ike_sa_id
->clone(ike_sa_id
,&(this->ike_sa_id
)) != SUCCESS
)
1210 this->logger
->log(this->logger
, ERROR
, "Fatal error: Could not clone ike_sa_id");
1211 global_logger_manager
->destroy_logger(global_logger_manager
,this->logger
);
1212 allocator_free(this);
1215 this->child_sas
= linked_list_create();
1216 if (this->child_sas
== NULL
)
1218 this->logger
->log(this->logger
, ERROR
, "Fatal error: Could not create list for child_sa's");
1219 this->ike_sa_id
->destroy(this->ike_sa_id
);
1220 global_logger_manager
->destroy_logger(global_logger_manager
,this->logger
);
1221 allocator_free(this);
1224 this->randomizer
= randomizer_create();
1225 if (this->randomizer
== NULL
)
1227 this->logger
->log(this->logger
, ERROR
, "Fatal error: Could not create list for child_sa's");
1228 this->child_sas
->destroy(this->child_sas
);
1229 this->ike_sa_id
->destroy(this->ike_sa_id
);
1230 global_logger_manager
->destroy_logger(global_logger_manager
,this->logger
);
1231 allocator_free(this);
1234 this->me
.host
= NULL
;
1235 this->other
.host
= NULL
;
1236 this->ike_sa_init_data
.diffie_hellman
= NULL
;
1237 this->ike_sa_init_data
.dh_group_number
= 0;
1238 /* 1 means highest priority */
1239 this->ike_sa_init_data
.dh_group_priority
= 1;
1240 this->ike_sa_init_data
.sent_nonce
.len
= 0;
1241 this->ike_sa_init_data
.sent_nonce
.ptr
= NULL
;
1242 this->ike_sa_init_data
.received_nonce
.len
= 0;
1243 this->ike_sa_init_data
.received_nonce
.ptr
= NULL
;
1244 this->ike_sa_init_data
.proposals
= NULL
;
1245 this->last_requested_message
= NULL
;
1246 this->last_responded_message
= NULL
;
1247 this->message_id_out
= 0;
1248 this->message_id_in
= 0;
1251 /* at creation time, IKE_SA isn't in a specific state */
1252 this->state
= NO_STATE
;
1254 return (&this->public);