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 * Nonce size in bytes of all sent nonces
47 * States in which a IKE_SA can actually be
49 typedef enum ike_sa_state_e ike_sa_state_t
;
54 * IKE_SA is is not in a state
59 * A IKE_SA_INIT-message was sent: role initiator
61 IKE_SA_INIT_REQUESTED
= 2,
64 * A IKE_SA_INIT-message was replied: role responder
66 IKE_SA_INIT_RESPONDED
= 3,
69 * An IKE_AUTH-message was sent after a successful
70 * IKE_SA_INIT-exchange: role initiator
72 IKE_AUTH_REQUESTED
= 4,
75 * An IKE_AUTH-message was replied: role responder.
76 * In this state, all the informations for an IKE_SA
77 * and one CHILD_SA are known.
79 IKE_SA_INITIALIZED
= 5
83 * string mappings for ike_sa_state
85 mapping_t ike_sa_state_m
[] = {
86 {NO_STATE
, "NO_STATE"},
87 {IKE_SA_INIT_REQUESTED
, "IKE_SA_INIT_REQUESTED"},
88 {IKE_SA_INIT_RESPONDED
, "IKE_SA_INIT_RESPONDED"},
89 {IKE_AUTH_REQUESTED
, "IKE_AUTH_REQUESTED"},
90 {IKE_SA_INITIALIZED
, "IKE_SA_INITIALIZED"},
96 * Private data of an message_t object
98 typedef struct private_ike_sa_s private_ike_sa_t
;
100 struct private_ike_sa_s
{
103 * Public part of a ike_sa_t object
108 * Builds an empty IKEv2-Message
110 * Depending on the type of message (request or response), the message id is
111 * either message_id_out or message_id_in.
114 * @param this calling object
115 * @param type exchange type of new message
116 * @param request TRUE, if message has to be a request
117 * @param message new message is stored at this location
122 status_t (*build_message
) (private_ike_sa_t
*this, exchange_type_t type
, bool request
, message_t
**message
);
124 status_t (*build_sa_payload
) (private_ike_sa_t
*this, sa_payload_t
**payload
);
125 status_t (*build_ke_payload
) (private_ike_sa_t
*this, ke_payload_t
**payload
);
126 status_t (*build_nonce_payload
) (private_ike_sa_t
*this, nonce_payload_t
**payload
);
128 status_t (*create_delete_job
) (private_ike_sa_t
*this);
129 status_t (*resend_last_reply
) (private_ike_sa_t
*this);
132 status_t (*transto_ike_sa_init_requested
) (private_ike_sa_t
*this, char *name
);
133 status_t (*transto_ike_sa_init_responded
) (private_ike_sa_t
*this, message_t
*message
);
134 status_t (*transto_ike_auth_requested
) (private_ike_sa_t
*this, message_t
*message
);
138 * Identifier for the current IKE_SA
140 ike_sa_id_t
*ike_sa_id
;
143 * Linked List containing the child sa's of the current IKE_SA
145 linked_list_t
*child_sas
;
148 * Current state of the IKE_SA
150 ike_sa_state_t state
;
153 * this SA's source for random data
155 randomizer_t
*randomizer
;
158 * contains the last responded message
161 message_t
*last_responded_message
;
164 * contains the last requested message
167 message_t
*last_requested_message
;
170 * Informations of this host
177 * Informations of the other host
186 * Diffie Hellman object used to compute shared secret
188 diffie_hellman_t
*diffie_hellman
;
191 * Diffie Hellman group number
193 u_int16_t dh_group_number
;
196 * Priority used get matching dh_group number
198 u_int16_t dh_group_priority
;
203 linked_list_t
*proposals
;
211 * received nonce value
213 chunk_t received_nonce
;
218 * next message id to receive
220 u_int32_t message_id_in
;
223 * next message id to send
225 u_int32_t message_id_out
;
228 * a logger for this IKE_SA
234 * @brief implements function process_message of private_ike_sa_t
236 static status_t
process_message (private_ike_sa_t
*this, message_t
*message
)
238 u_int32_t message_id
;
240 exchange_type_t exchange_type
;
241 /* we must process each request or response from remote host
242 * the check if a given message is possible for a given state is done in here
245 /* find out type of message (request or response) */
246 is_request
= message
->get_request(message
);
247 exchange_type
= message
->get_exchange_type(message
);
249 this->logger
->log(this->logger
, CONTROL
|MORE
, "Process %s message of exchange type %s",(is_request
) ?
"REQUEST" : "RESPONSE",
250 mapping_find(exchange_type_m
,exchange_type
));
252 message_id
= message
->get_message_id(message
);
255 * It has to be checked, if the message has to be resent cause of lost packets!
257 if (is_request
&& ( message_id
== (this->message_id_in
- 1)))
259 /* message can be resent ! */
260 this->logger
->log(this->logger
, CONTROL
|MORE
, "Resent message detected. Send stored reply");
261 return (this->resend_last_reply(this));
264 /* Now, the message id is checked for request AND reply */
267 /* In a request, the message has to be this->message_id_in (other case is already handled) */
268 if (message_id
!= this->message_id_in
)
270 this->logger
->log(this->logger
, ERROR
| MORE
, "Message request with message id %d received, but %d expected",message_id
,this->message_id_in
);
276 /* In a reply, the message has to be this->message_id_out -1 cause it is the reply to the last sent message*/
277 if (message_id
!= (this->message_id_out
- 1))
279 this->logger
->log(this->logger
, ERROR
| MORE
, "Message reply with message id %d received, but %d expected",message_id
,this->message_id_in
);
284 /* Now, the exchange type is checked and the appropriate transition handler is called*/
285 switch (message
->get_exchange_type(message
))
290 if (message
->get_request(message
)) {
291 if (this->state
== NO_STATE
)
293 /* state transission NO_STATE => IKE_SA_INIT_RESPONDED */
294 return this->transto_ike_sa_init_responded(this, message
);
299 if (this->state
== IKE_SA_INIT_REQUESTED
)
301 /* state transission IKE_SA_INIT_REQUESTED => IKE_AUTH_REQUESTED*/
302 return this->transto_ike_auth_requested(this, message
);
306 this->logger
->log(this->logger
, ERROR
| MORE
, "Message in current state %s not supported",mapping_find(ike_sa_state_m
,this->state
));
312 if (this->state
<= IKE_SA_INIT_REQUESTED
)
314 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
));
319 case CREATE_CHILD_SA
:
321 if (this->state
< IKE_SA_INITIALIZED
)
323 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
));
334 this->logger
->log(this->logger
, ERROR
, "processing %s-message not supported.",
335 mapping_find(exchange_type_m
,message
->get_exchange_type(message
)));
336 return NOT_SUPPORTED
;
339 this->logger
->log(this->logger
, CONTROL
, "received %s-message in state %s, not handled.",
340 mapping_find(exchange_type_m
, message
->get_exchange_type(message
)),
341 mapping_find(ike_sa_state_m
, this->state
));
342 return INVALID_STATE
;
346 * @brief Implements function build_message of private_ike_sa_t.
348 static status_t
build_message(private_ike_sa_t
*this, exchange_type_t type
, bool request
, message_t
**message
)
351 message_t
*new_message
;
352 host_t
*source
, *destination
;
354 this ->logger
->log(this->logger
, CONTROL
|MORE
, "build empty message");
355 new_message
= message_create();
356 if (new_message
== NULL
)
358 this->logger
->log(this->logger
, ERROR
, "Fatal error: could not create empty message object");
362 status
= this->me
.host
->clone(this->me
.host
, &source
);
363 if (status
!= SUCCESS
)
365 this->logger
->log(this->logger
, ERROR
, "Fatal error: could not clone my host information");
366 new_message
->destroy(new_message
);
369 status
= this->other
.host
->clone(this->other
.host
, &destination
);
370 if (status
!= SUCCESS
)
372 this->logger
->log(this->logger
, ERROR
, "Fatal error: could not clone other host information");
373 source
->destroy(source
);
374 new_message
->destroy(new_message
);
378 new_message
->set_source(new_message
, source
);
379 new_message
->set_destination(new_message
, destination
);
381 new_message
->set_exchange_type(new_message
, type
);
382 new_message
->set_request(new_message
, request
);
384 new_message
->set_message_id(new_message
, (request
) ?
this->message_id_out
: this->message_id_in
);
386 status
= new_message
->set_ike_sa_id(new_message
, this->ike_sa_id
);
387 if (status
!= SUCCESS
)
389 this->logger
->log(this->logger
, ERROR
, "Fatal error: could not set ike_sa_id of message");
390 new_message
->destroy(new_message
);
394 *message
= new_message
;
400 * @brief Implements function transto_ike_sa_init_requested of private_ike_sa_t.
402 static status_t
transto_ike_sa_init_requested(private_ike_sa_t
*this, char *name
)
408 linked_list_iterator_t
*proposal_iterator
;
410 this->logger
->log(this->logger
, CONTROL
, "Initializing connection %s",name
);
412 status
= global_configuration_manager
->get_local_host(global_configuration_manager
, name
, &(this->me
.host
));
413 if (status
!= SUCCESS
)
415 this->logger
->log(this->logger
, ERROR
| MORE
, "Could not retrieve local host configuration information for %s",name
);
419 status
= global_configuration_manager
->get_remote_host(global_configuration_manager
, name
, &(this->other
.host
));
420 if (status
!= SUCCESS
)
422 this->logger
->log(this->logger
, ERROR
| MORE
, "Could not retrieve remote host configuration information for %s",name
);
426 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
);
427 if (status
!= SUCCESS
)
429 this->logger
->log(this->logger
, ERROR
| MORE
, "Could not retrieve DH group number for %s",name
);
433 if (this->ike_sa_init_data
.proposals
->get_count(this->ike_sa_init_data
.proposals
) > 0)
435 this->logger
->log(this->logger
, ERROR
, "Proposals allready existing!");
439 status
= this->ike_sa_init_data
.proposals
->create_iterator(this->ike_sa_init_data
.proposals
, &proposal_iterator
, FALSE
);
440 if (status
!= SUCCESS
)
442 this->logger
->log(this->logger
, ERROR
, "Fatal error: Could not create iterator on list for proposals");
446 status
= global_configuration_manager
->get_proposals_for_host(global_configuration_manager
, this->other
.host
, proposal_iterator
);
447 if (status
!= SUCCESS
)
449 this->logger
->log(this->logger
, ERROR
| MORE
, "Could not retrieve Proposals for %s",name
);
452 /* not needed anymore */
453 proposal_iterator
->destroy(proposal_iterator
);
455 if (this->ike_sa_init_data
.diffie_hellman
!= NULL
)
457 this->logger
->log(this->logger
, ERROR
, "Object of type diffie_hellman_t already existing!");
460 this ->logger
->log(this->logger
, CONTROL
|MOST
, "create diffie hellman object");
461 this->ike_sa_init_data
.diffie_hellman
= diffie_hellman_create(this->ike_sa_init_data
.dh_group_number
);
462 if (this->ike_sa_init_data
.diffie_hellman
== NULL
)
464 this->logger
->log(this->logger
, ERROR
, "Object of type diffie_hellman_t could not be created!");
468 if (this->ike_sa_init_data
.sent_nonce
.ptr
!= NULL
)
470 this->logger
->log(this->logger
, ERROR
, "Nonce for IKE_SA_INIT phase already existing!");
474 if (this->randomizer
->allocate_pseudo_random_bytes(this->randomizer
, NONCE_SIZE
, &(this->ike_sa_init_data
.sent_nonce
)) != SUCCESS
)
476 this->logger
->log(this->logger
, ERROR
, "Could not create nonce!");
481 /* going to build message */
482 status
= this->build_message(this, IKE_SA_INIT
, TRUE
, &message
);
483 if (status
!= SUCCESS
)
485 this->logger
->log(this->logger
, ERROR
, "Could not build message");
489 /* build SA payload */
490 status
= this->build_sa_payload(this, (sa_payload_t
**)&payload
);
491 if (status
!= SUCCESS
)
493 this->logger
->log(this->logger
, ERROR
, "Could not build SA payload");
494 message
->destroy(message
);
498 this ->logger
->log(this->logger
, CONTROL
|MOST
, "add SA payload to message");
499 status
= message
->add_payload(message
, payload
);
500 if (status
!= SUCCESS
)
502 this->logger
->log(this->logger
, ERROR
, "Could not add SA payload to message");
503 message
->destroy(message
);
508 /* build KE payload */
509 status
= this->build_ke_payload(this,(ke_payload_t
**) &payload
);
510 if (status
!= SUCCESS
)
512 this->logger
->log(this->logger
, ERROR
, "Could not build KE payload");
513 message
->destroy(message
);
517 this ->logger
->log(this->logger
, CONTROL
|MOST
, "add KE payload to message");
518 status
= message
->add_payload(message
, payload
);
519 if (status
!= SUCCESS
)
521 this->logger
->log(this->logger
, ERROR
, "Could not add KE payload to message");
522 message
->destroy(message
);
526 /* build Nonce payload */
527 status
= this->build_nonce_payload(this, (nonce_payload_t
**)&payload
);
528 if (status
!= SUCCESS
)
530 this->logger
->log(this->logger
, ERROR
, "Could not build NONCE payload");
531 message
->destroy(message
);
535 this ->logger
->log(this->logger
, CONTROL
|MOST
, "add nonce payload to message");
536 status
= message
->add_payload(message
, payload
);
537 if (status
!= SUCCESS
)
539 this->logger
->log(this->logger
, ERROR
, "Could not add nonce payload to message");
540 message
->destroy(message
);
544 /* generate packet */
545 this ->logger
->log(this->logger
, CONTROL
|MOST
, "generate packet from message");
546 status
= message
->generate(message
, &packet
);
547 if (status
!= SUCCESS
)
549 this->logger
->log(this->logger
, ERROR
, "Fatal error: could not generate packet from message");
550 message
->destroy(message
);
554 this ->logger
->log(this->logger
, CONTROL
|MOST
, "Add packet to global send queue");
555 status
= global_send_queue
->add(global_send_queue
, packet
);
556 if (status
!= SUCCESS
)
558 this->logger
->log(this->logger
, ERROR
, "Could not add packet to send queue");
559 message
->destroy(message
);
563 if ( this->last_requested_message
!= NULL
)
565 /* destroy message */
566 this ->logger
->log(this->logger
, CONTROL
|MOST
, "Destroy stored last requested message");
567 this->last_requested_message
->destroy(this->last_requested_message
);
570 this->last_requested_message
= message
;
572 /* message counter can now be increased */
573 this->message_id_out
++;
575 /* state has NOW changed :-) */
576 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
) );
577 this->state
= IKE_SA_INIT_REQUESTED
;
582 static status_t
transto_ike_sa_init_responded(private_ike_sa_t
*this, message_t
*request
)
585 linked_list_iterator_t
*payloads
;
587 host_t
*source
, *destination
;
591 /* this is the first message we process, so copy host infos */
592 request
->get_source(request
, &source
);
593 request
->get_destination(request
, &destination
);
594 /* we need to clone them, since we destroy the message later */
595 destination
->clone(destination
, &(this->me
.host
));
596 source
->clone(source
, &(this->other
.host
));
598 /* parse incoming message */
599 status
= request
->parse_body(request
);
600 if (status
!= SUCCESS
)
602 this->logger
->log(this->logger
, ERROR
| MORE
, "Could not parse body of request message");
603 this->create_delete_job(this);
607 /* iterate over incoming payloads. We can be sure, the message contains only accepted payloads! */
608 status
= request
->get_payload_iterator(request
, &payloads
);
609 if (status
!= SUCCESS
)
611 this->logger
->log(this->logger
, ERROR
, "Fatal error: Could not get payload interator");
612 this->create_delete_job(this);
616 while (payloads
->has_next(payloads
))
620 /* get current payload */
621 payloads
->current(payloads
, (void**)&payload
);
623 this->logger
->log(this->logger
, CONTROL
|MORE
, "Processing payload of type %s", mapping_find(payload_type_m
, payload
->get_type(payload
)));
624 switch (payload
->get_type(payload
))
626 case SECURITY_ASSOCIATION
:
628 sa_payload_t
*sa_payload
= (sa_payload_t
*)payload
;
629 linked_list_iterator_t
*suggested_proposals
, *accepted_proposals
;
631 status
= this->ike_sa_init_data
.proposals
->create_iterator(this->ike_sa_init_data
.proposals
, &accepted_proposals
, FALSE
);
632 if (status
!= SUCCESS
)
634 this->logger
->log(this->logger
, ERROR
, "Fatal error: Could not create iterator on list for proposals");
635 payloads
->destroy(payloads
);
636 this->create_delete_job(this);
640 /* get the list of suggested proposals */
641 status
= sa_payload
->create_proposal_substructure_iterator(sa_payload
, &suggested_proposals
, TRUE
);
642 if (status
!= SUCCESS
)
644 this->logger
->log(this->logger
, ERROR
, "Fatal error: Could not create iterator on suggested proposals");
645 accepted_proposals
->destroy(accepted_proposals
);
646 payloads
->destroy(payloads
);
647 this->create_delete_job(this);
651 /* now let the configuration-manager select a subset of the proposals */
652 status
= global_configuration_manager
->select_proposals_for_host(global_configuration_manager
,
653 this->other
.host
, suggested_proposals
, accepted_proposals
);
654 if (status
!= SUCCESS
)
656 this->logger
->log(this->logger
, CONTROL
| MORE
, "No proposal of suggested proposals selected");
657 suggested_proposals
->destroy(suggested_proposals
);
658 accepted_proposals
->destroy(accepted_proposals
);
659 payloads
->destroy(payloads
);
660 this->create_delete_job(this);
664 /* iterators are not needed anymore */
665 suggested_proposals
->destroy(suggested_proposals
);
666 accepted_proposals
->destroy(accepted_proposals
);
668 this->logger
->log(this->logger
, CONTROL
| MORE
, "SA Payload processed");
669 /* ok, we have what we need for sa_payload (proposals are stored in this->proposals)*/
674 ke_payload_t
*ke_payload
= (ke_payload_t
*)payload
;
675 diffie_hellman_group_t group
;
676 diffie_hellman_t
*dh
;
679 group
= ke_payload
->get_dh_group_number(ke_payload
);
681 status
= global_configuration_manager
->is_dh_group_allowed_for_host(global_configuration_manager
,
682 this->other
.host
, group
, &allowed_group
);
684 if (status
!= SUCCESS
)
686 this->logger
->log(this->logger
, ERROR
| MORE
, "Could not get informations about DH group");
687 payloads
->destroy(payloads
);
688 this->create_delete_job(this);
693 /** @todo Send info reply */
696 /* create diffie hellman object to handle DH exchange */
697 dh
= diffie_hellman_create(group
);
700 this->logger
->log(this->logger
, ERROR
, "Could not generate DH object");
701 payloads
->destroy(payloads
);
702 this->create_delete_job(this);
706 this->logger
->log(this->logger
, CONTROL
| MORE
, "Set other DH public value");
708 status
= dh
->set_other_public_value(dh
, ke_payload
->get_key_exchange_data(ke_payload
));
709 if (status
!= SUCCESS
)
711 this->logger
->log(this->logger
, ERROR
, "Could not set other DH public value");
713 payloads
->destroy(payloads
);
714 this->create_delete_job(this);
718 if (this->ike_sa_init_data
.diffie_hellman
!= NULL
)
720 this->logger
->log(this->logger
, CONTROL
| MORE
, "Going to destroy allready existing diffie_hellman object");
721 this->ike_sa_init_data
.diffie_hellman
->destroy(this->ike_sa_init_data
.diffie_hellman
);
723 this->ike_sa_init_data
.diffie_hellman
= dh
;
725 this->logger
->log(this->logger
, CONTROL
| MORE
, "KE Payload processed");
730 nonce_payload_t
*nonce_payload
= (nonce_payload_t
*)payload
;
733 this->logger
->log(this->logger
, CONTROL
| MORE
, "Get nonce value and store it");
734 nonce_payload
->get_nonce(nonce_payload
, &nonce
);
735 /** @todo free if there is already one */
736 this->ike_sa_init_data
.received_nonce
.ptr
= allocator_clone_bytes(nonce
.ptr
, nonce
.len
);
737 this->ike_sa_init_data
.received_nonce
.len
= nonce
.len
;
738 if (this->ike_sa_init_data
.received_nonce
.ptr
== NULL
)
740 payloads
->destroy(payloads
);
741 this->create_delete_job(this);
745 this->logger
->log(this->logger
, CONTROL
| MORE
, "Nonce Payload processed");
750 this->logger
->log(this->logger
, ERROR
| MORE
, "Payload type not supported!");
751 payloads
->destroy(payloads
);
752 this->create_delete_job(this);
759 /* iterator can be destroyed */
760 payloads
->destroy(payloads
);
762 this->logger
->log(this->logger
, CONTROL
| MORE
, "Request successfully handled. Going to create reply.");
765 if (this->ike_sa_init_data
.sent_nonce
.ptr
!= NULL
)
767 this->logger
->log(this->logger
, ERROR
, "Nonce for IKE_SA_INIT phase already existing!");
771 this->logger
->log(this->logger
, CONTROL
| MOST
, "Going to create nonce.");
772 if (this->randomizer
->allocate_pseudo_random_bytes(this->randomizer
, NONCE_SIZE
, &(this->ike_sa_init_data
.sent_nonce
)) != SUCCESS
)
774 this->logger
->log(this->logger
, ERROR
, "Could not create nonce!");
779 /* set up the reply */
780 status
= this->build_message(this, IKE_SA_INIT
, FALSE
, &response
);
781 if (status
!= SUCCESS
)
783 this->logger
->log(this->logger
, ERROR
, "Could not create empty message");
784 this->create_delete_job(this);
788 /* build SA payload */
789 status
= this->build_sa_payload(this, (sa_payload_t
**)&payload
);
790 if (status
!= SUCCESS
)
792 this->logger
->log(this->logger
, ERROR
, "Could not build SA payload");
793 this->create_delete_job(this);
794 response
->destroy(response
);
798 this ->logger
->log(this->logger
, CONTROL
|MOST
, "add SA payload to message");
799 status
= response
->add_payload(response
, payload
);
800 if (status
!= SUCCESS
)
802 this->logger
->log(this->logger
, ERROR
, "Could not add SA payload to message");
803 this->create_delete_job(this);
804 response
->destroy(response
);
808 /* build KE payload */
809 status
= this->build_ke_payload(this,(ke_payload_t
**) &payload
);
810 if (status
!= SUCCESS
)
812 this->logger
->log(this->logger
, ERROR
, "Could not build KE payload");
813 this->create_delete_job(this);
814 response
->destroy(response
);
818 this ->logger
->log(this->logger
, CONTROL
|MOST
, "add KE payload to message");
819 status
= response
->add_payload(response
, payload
);
820 if (status
!= SUCCESS
)
822 this->logger
->log(this->logger
, ERROR
, "Could not add KE payload to message");
823 this->create_delete_job(this);
824 response
->destroy(response
);
828 /* build Nonce payload */
829 status
= this->build_nonce_payload(this, (nonce_payload_t
**)&payload
);
830 if (status
!= SUCCESS
)
832 this->logger
->log(this->logger
, ERROR
, "Could not build NONCE payload");
833 this->create_delete_job(this);
834 response
->destroy(response
);
838 this ->logger
->log(this->logger
, CONTROL
|MOST
, "add nonce payload to message");
839 status
= response
->add_payload(response
, payload
);
840 if (status
!= SUCCESS
)
842 this->logger
->log(this->logger
, ERROR
, "Could not add nonce payload to message");
843 this->create_delete_job(this);
844 response
->destroy(response
);
848 /* generate packet */
849 this ->logger
->log(this->logger
, CONTROL
|MOST
, "generate packet from message");
850 status
= response
->generate(response
, &packet
);
851 if (status
!= SUCCESS
)
853 this->logger
->log(this->logger
, ERROR
, "Fatal error: could not generate packet from message");
854 this->create_delete_job(this);
855 response
->destroy(response
);
859 this ->logger
->log(this->logger
, CONTROL
|MOST
, "Add packet to global send queue");
860 status
= global_send_queue
->add(global_send_queue
, packet
);
861 if (status
!= SUCCESS
)
863 this->logger
->log(this->logger
, ERROR
, "Could not add packet to send queue");
864 this->create_delete_job(this);
865 response
->destroy(response
);
869 if ( this->last_responded_message
!= NULL
)
871 /* destroy message */
872 this ->logger
->log(this->logger
, CONTROL
|MOST
, "Destroy stored last responded message");
873 this->last_responded_message
->destroy(this->last_responded_message
);
876 this->last_responded_message
= response
;
878 /* state has NOW changed :-) */
879 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
) );
880 this->state
= IKE_SA_INIT_RESPONDED
;
886 static status_t
transto_ike_auth_requested(private_ike_sa_t
*this, message_t
*response
)
889 linked_list_iterator_t
*payloads
;
892 /* parse incoming message */
893 status
= response
->parse_body(response
);
894 if (status
!= SUCCESS
)
896 this->logger
->log(this->logger
, ERROR
, "Could not parse body");
899 /* iterate over incoming payloads */
900 status
= response
->get_payload_iterator(response
, &payloads
);
901 if (status
!= SUCCESS
)
903 response
->destroy(response
);
906 while (payloads
->has_next(payloads
))
909 payloads
->current(payloads
, (void**)&payload
);
911 this->logger
->log(this->logger
, CONTROL
|MORE
, "Processing payload %s", mapping_find(payload_type_m
, payload
->get_type(payload
)));
912 switch (payload
->get_type(payload
))
914 // case SECURITY_ASSOCIATION:
916 // sa_payload_t *sa_payload = (sa_payload_t*)payload;
917 // linked_list_iterator_t *suggested_proposals, *accepted_proposals;
918 // /* create a list for accepted proposals */
919 // if (this->ike_sa_init_data.proposals == NULL) {
920 // this->ike_sa_init_data.proposals = linked_list_create();
924 // /** @todo destroy list contents */
926 // if (this->ike_sa_init_data.proposals == NULL)
928 // payloads->destroy(payloads);
929 // return OUT_OF_RES;
931 // status = this->ike_sa_init_data.proposals->create_iterator(this->ike_sa_init_data.proposals, &accepted_proposals, FALSE);
932 // if (status != SUCCESS)
934 // payloads->destroy(payloads);
938 // /* get the list of suggested proposals */
939 // status = sa_payload->create_proposal_substructure_iterator(sa_payload, &suggested_proposals, TRUE);
940 // if (status != SUCCESS)
942 // accepted_proposals->destroy(accepted_proposals);
943 // payloads->destroy(payloads);
947 // /* now let the configuration-manager select a subset of the proposals */
948 // status = global_configuration_manager->select_proposals_for_host(global_configuration_manager,
949 // this->other.host, suggested_proposals, accepted_proposals);
950 // if (status != SUCCESS)
952 // suggested_proposals->destroy(suggested_proposals);
953 // accepted_proposals->destroy(accepted_proposals);
954 // payloads->destroy(payloads);
958 // suggested_proposals->destroy(suggested_proposals);
959 // accepted_proposals->destroy(accepted_proposals);
961 // /* ok, we have what we need for sa_payload */
966 ke_payload_t
*ke_payload
= (ke_payload_t
*)payload
;
967 diffie_hellman_t
*dh
;
968 chunk_t shared_secret
;
970 dh
= this->ike_sa_init_data
.diffie_hellman
;
973 status
= dh
->set_other_public_value(dh
, ke_payload
->get_key_exchange_data(ke_payload
));
974 if (status
!= SUCCESS
)
977 payloads
->destroy(payloads
);
981 status
= dh
->get_shared_secret(dh
, &shared_secret
);
983 this->logger
->log_chunk(this->logger
, RAW
, "Shared secret", &shared_secret
);
989 nonce_payload_t
*nonce_payload
= (nonce_payload_t
*)payload
;
992 nonce_payload
->get_nonce(nonce_payload
, &nonce
);
993 /** @todo free if there is already one */
994 this->ike_sa_init_data
.received_nonce
.ptr
= allocator_clone_bytes(nonce
.ptr
, nonce
.len
);
995 this->ike_sa_init_data
.received_nonce
.len
= nonce
.len
;
996 if (this->ike_sa_init_data
.received_nonce
.ptr
== NULL
)
998 payloads
->destroy(payloads
);
1011 payloads
->destroy(payloads
);
1015 /* set up the reply */
1016 status
= this->build_message(this, IKE_SA_INIT
, FALSE
, &response
);
1017 if (status
!= SUCCESS
)
1019 this->create_delete_job(this);
1023 response
->destroy(response
);
1031 * @brief implements function process_configuration of private_ike_sa_t
1033 static status_t
initialize_connection(private_ike_sa_t
*this, char *name
)
1035 /* work is done in transto_ike_sa_init_requested */
1036 return (this->transto_ike_sa_init_requested(this,name
));
1040 * @brief implements function private_ike_sa_t.get_id
1042 static ike_sa_id_t
* get_id(private_ike_sa_t
*this)
1044 return this->ike_sa_id
;
1048 * implements private_ike_sa_t.build_sa_payload
1050 static status_t
build_sa_payload(private_ike_sa_t
*this, sa_payload_t
**payload
)
1052 sa_payload_t
* sa_payload
;
1053 linked_list_iterator_t
*proposal_iterator
;
1057 /* SA payload takes proposals from this->ike_sa_init_data.proposals and writes them to the created sa_payload */
1059 this->logger
->log(this->logger
, CONTROL
|MORE
, "building sa payload");
1061 status
= this->ike_sa_init_data
.proposals
->create_iterator(this->ike_sa_init_data
.proposals
, &proposal_iterator
, FALSE
);
1062 if (status
!= SUCCESS
)
1064 this->logger
->log(this->logger
, ERROR
, "Fatal error: Could not create iterator on list for proposals");
1068 sa_payload
= sa_payload_create();
1069 if (sa_payload
== NULL
)
1071 this->logger
->log(this->logger
, ERROR
, "Fatal error: Could not create SA payload object");
1075 while (proposal_iterator
->has_next(proposal_iterator
))
1077 proposal_substructure_t
*current_proposal
;
1078 proposal_substructure_t
*current_proposal_clone
;
1079 status
= proposal_iterator
->current(proposal_iterator
,(void **) ¤t_proposal
);
1080 if (status
!= SUCCESS
)
1082 this->logger
->log(this->logger
, ERROR
, "Could not get current proposal needed to copy");
1083 sa_payload
->destroy(sa_payload
);
1086 status
= current_proposal
->clone(current_proposal
,¤t_proposal_clone
);
1087 if (status
!= SUCCESS
)
1089 this->logger
->log(this->logger
, ERROR
, "Could not clone current proposal");
1090 sa_payload
->destroy(sa_payload
);
1094 status
= sa_payload
->add_proposal_substructure(sa_payload
,current_proposal_clone
);
1095 if (status
!= SUCCESS
)
1097 this->logger
->log(this->logger
, ERROR
, "Could not add cloned proposal to SA payload");
1098 sa_payload
->destroy(sa_payload
);
1104 this->logger
->log(this->logger
, CONTROL
|MORE
, "sa payload buildet");
1106 *payload
= sa_payload
;
1111 static status_t
build_ke_payload(private_ike_sa_t
*this, ke_payload_t
**payload
)
1113 ke_payload_t
*ke_payload
;
1117 this->logger
->log(this->logger
, CONTROL
|MORE
, "building ke payload");
1119 if (this->state
!= NO_STATE
)
1121 this->logger
->log(this->logger
, ERROR
, "KE payload in state %s not supported",mapping_find(ike_sa_state_m
,this->state
));
1125 switch(this->ike_sa_id
->is_initiator(this->ike_sa_id
))
1130 this ->logger
->log(this->logger
, CONTROL
|MORE
, "get public dh value to send in ke payload");
1131 status
= this->ike_sa_init_data
.diffie_hellman
->get_my_public_value(this->ike_sa_init_data
.diffie_hellman
,&key_data
);
1132 if (status
!= SUCCESS
)
1134 this->logger
->log(this->logger
, ERROR
, "Could not get my DH public value");
1138 ke_payload
= ke_payload_create();
1139 if (ke_payload
== NULL
)
1141 this->logger
->log(this->logger
, ERROR
, "Could not create KE payload");
1142 allocator_free_chunk(key_data
);
1145 ke_payload
->set_dh_group_number(ke_payload
, MODP_1024_BIT
);
1146 if (ke_payload
->set_key_exchange_data(ke_payload
, key_data
) != SUCCESS
)
1148 this->logger
->log(this->logger
, ERROR
, "Could not set key exchange data of KE payload");
1149 ke_payload
->destroy(ke_payload
);
1150 allocator_free_chunk(key_data
);
1153 allocator_free_chunk(key_data
);
1155 *payload
= ke_payload
;
1164 * implements private_ike_sa_t.build_nonce_payload
1166 static status_t
build_nonce_payload(private_ike_sa_t
*this, nonce_payload_t
**payload
)
1168 nonce_payload_t
*nonce_payload
;
1171 this->logger
->log(this->logger
, CONTROL
|MORE
, "building nonce payload");
1173 if (this->state
!= NO_STATE
)
1175 this->logger
->log(this->logger
, ERROR
, "Nonce payload in state %s not supported",mapping_find(ike_sa_state_m
,this->state
));
1179 nonce_payload
= nonce_payload_create();
1180 if (nonce_payload
== NULL
)
1182 this->logger
->log(this->logger
, ERROR
, "Fatal error: could not create nonce payload object");
1186 status
= nonce_payload
->set_nonce(nonce_payload
, this->ike_sa_init_data
.sent_nonce
);
1188 if (status
!= SUCCESS
)
1190 this->logger
->log(this->logger
, ERROR
, "Fatal error: could not set nonce data of payload");
1191 nonce_payload
->destroy(nonce_payload
);
1195 *payload
= nonce_payload
;
1201 * @brief implements function resend_last_reply of private_ike_sa_t
1203 status_t
resend_last_reply (private_ike_sa_t
*this)
1208 status
= this->last_responded_message
->generate(this->last_responded_message
, &packet
);
1209 if (status
!= SUCCESS
)
1211 this->logger
->log(this->logger
, ERROR
, "Could not generate message to resent");
1215 status
= global_send_queue
->add(global_send_queue
, packet
);
1216 if (status
!= SUCCESS
)
1218 this->logger
->log(this->logger
, ERROR
, "Could not add packet to send queue");
1219 packet
->destroy(packet
);
1225 status_t
create_delete_job (private_ike_sa_t
*this)
1230 this->logger
->log(this->logger
, CONTROL
| MORE
, "Going to create job to delete this IKE_SA");
1232 delete_job
= (job_t
*) delete_ike_sa_job_create(this->ike_sa_id
);
1233 if (delete_job
== NULL
)
1235 this->logger
->log(this->logger
, ERROR
, "Job to delete IKE SA could not be created");
1239 status
= global_job_queue
->add(global_job_queue
,delete_job
);
1240 if (status
!= SUCCESS
)
1242 this->logger
->log(this->logger
, ERROR
, "%s Job to delete IKE SA could not be added to job queue",mapping_find(status_m
,status
));
1243 delete_job
->destroy_all(delete_job
);
1250 * @brief implements function destroy of private_ike_sa_t
1252 static status_t
destroy (private_ike_sa_t
*this)
1255 this->logger
->log(this->logger
, CONTROL
| MORE
, "Going to destroy IKE_SA");
1257 /* destroy child sa's */
1258 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy all child_sa's");
1259 while (this->child_sas
->get_count(this->child_sas
) > 0)
1262 if (this->child_sas
->remove_first(this->child_sas
,&child_sa
) != SUCCESS
)
1266 /* destroy child sa */
1268 this->child_sas
->destroy(this->child_sas
);
1270 /* destroy ike_sa_id */
1271 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy assigned ike_sa_id");
1272 this->ike_sa_id
->destroy(this->ike_sa_id
);
1274 /* destroy stored requested message */
1275 if (this->last_requested_message
!= NULL
)
1277 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy last requested message");
1278 this->last_requested_message
->destroy(this->last_requested_message
);
1281 /* destroy stored responded messages */
1282 if (this->last_responded_message
!= NULL
)
1284 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy last responded message");
1285 this->last_responded_message
->destroy(this->last_responded_message
);
1288 /* destroy stored proposal */
1289 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy stored proposals");
1290 while (this->ike_sa_init_data
.proposals
->get_count(this->ike_sa_init_data
.proposals
) > 0)
1292 proposal_substructure_t
*current_proposal
;
1293 this->ike_sa_init_data
.proposals
->remove_first(this->ike_sa_init_data
.proposals
,(void **)¤t_proposal
);
1294 current_proposal
->destroy(current_proposal
);
1296 this->ike_sa_init_data
.proposals
->destroy(this->ike_sa_init_data
.proposals
);
1299 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy randomizer");
1300 this->randomizer
->destroy(this->randomizer
);
1303 /* destroy ike_sa_init data */
1304 this->logger
->log(this->logger
, CONTROL
| MOST
, "Going to destroy ike_sa_init data");
1305 if (this->ike_sa_init_data
.diffie_hellman
!= NULL
)
1307 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy diffie hellman object");
1308 this->ike_sa_init_data
.diffie_hellman
->destroy(this->ike_sa_init_data
.diffie_hellman
);
1310 if (this->ike_sa_init_data
.sent_nonce
.ptr
!= NULL
)
1312 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy sent nonce data");
1313 allocator_free_chunk(this->ike_sa_init_data
.sent_nonce
);
1315 if (this->ike_sa_init_data
.received_nonce
.ptr
!= NULL
)
1317 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy received nonce data");
1318 allocator_free_chunk(this->ike_sa_init_data
.received_nonce
);
1321 if (this->me
.host
!= NULL
)
1323 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy host informations of me");
1324 this->me
.host
->destroy(this->me
.host
);
1327 if (this->other
.host
!= NULL
)
1329 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy host informations of other");
1330 this->other
.host
->destroy(this->other
.host
);
1334 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy logger of IKE_SA");
1335 global_logger_manager
->destroy_logger(global_logger_manager
, this->logger
);
1337 allocator_free(this);
1342 * Described in Header
1344 ike_sa_t
* ike_sa_create(ike_sa_id_t
*ike_sa_id
)
1346 private_ike_sa_t
*this = allocator_alloc_thing(private_ike_sa_t
);
1352 /* Public functions */
1353 this->public.process_message
= (status_t(*)(ike_sa_t
*, message_t
*)) process_message
;
1354 this->public.initialize_connection
= (status_t(*)(ike_sa_t
*, char*)) initialize_connection
;
1355 this->public.get_id
= (ike_sa_id_t
*(*)(ike_sa_t
*)) get_id
;
1356 this->public.destroy
= (status_t(*)(ike_sa_t
*))destroy
;
1358 /* private functions */
1359 this->build_sa_payload
= build_sa_payload
;
1360 this->build_nonce_payload
= build_nonce_payload
;
1361 this->build_ke_payload
= build_ke_payload
;
1362 this->build_message
= build_message
;
1363 this->transto_ike_sa_init_requested
= transto_ike_sa_init_requested
;
1364 this->transto_ike_sa_init_responded
= transto_ike_sa_init_responded
;
1365 this->transto_ike_auth_requested
= transto_ike_auth_requested
;
1366 this->resend_last_reply
= resend_last_reply
;
1367 this->create_delete_job
= create_delete_job
;
1370 /* initialize private fields */
1371 this->logger
= global_logger_manager
->create_logger(global_logger_manager
, IKE_SA
, NULL
);
1372 if (this->logger
== NULL
)
1374 allocator_free(this);
1377 if (ike_sa_id
->clone(ike_sa_id
,&(this->ike_sa_id
)) != SUCCESS
)
1379 this->logger
->log(this->logger
, ERROR
, "Fatal error: Could not clone ike_sa_id");
1380 global_logger_manager
->destroy_logger(global_logger_manager
,this->logger
);
1381 allocator_free(this);
1384 this->child_sas
= linked_list_create();
1385 if (this->child_sas
== NULL
)
1387 this->logger
->log(this->logger
, ERROR
, "Fatal error: Could not create list for child_sa's");
1388 this->ike_sa_id
->destroy(this->ike_sa_id
);
1389 global_logger_manager
->destroy_logger(global_logger_manager
,this->logger
);
1390 allocator_free(this);
1393 this->randomizer
= randomizer_create();
1394 if (this->randomizer
== NULL
)
1396 this->logger
->log(this->logger
, ERROR
, "Fatal error: Could not create list for child_sa's");
1397 this->child_sas
->destroy(this->child_sas
);
1398 this->ike_sa_id
->destroy(this->ike_sa_id
);
1399 global_logger_manager
->destroy_logger(global_logger_manager
,this->logger
);
1400 allocator_free(this);
1403 this->me
.host
= NULL
;
1404 this->other
.host
= NULL
;
1405 this->ike_sa_init_data
.diffie_hellman
= NULL
;
1406 this->ike_sa_init_data
.dh_group_number
= 0;
1407 /* 1 means highest priority */
1408 this->ike_sa_init_data
.dh_group_priority
= 1;
1409 this->ike_sa_init_data
.sent_nonce
.len
= 0;
1410 this->ike_sa_init_data
.sent_nonce
.ptr
= NULL
;
1411 this->ike_sa_init_data
.received_nonce
.len
= 0;
1412 this->ike_sa_init_data
.received_nonce
.ptr
= NULL
;
1413 this->ike_sa_init_data
.proposals
= linked_list_create();
1414 if (this->ike_sa_init_data
.proposals
== NULL
)
1416 this->logger
->log(this->logger
, ERROR
, "Fatal error: Could not create list for child_sa's");
1417 this->child_sas
->destroy(this->child_sas
);
1418 this->ike_sa_id
->destroy(this->ike_sa_id
);
1419 this->randomizer
->destroy(this->randomizer
);
1420 global_logger_manager
->destroy_logger(global_logger_manager
,this->logger
);
1421 allocator_free(this);
1423 this->last_requested_message
= NULL
;
1424 this->last_responded_message
= NULL
;
1425 this->message_id_out
= 0;
1426 this->message_id_in
= 0;
1429 /* at creation time, IKE_SA isn't in a specific state */
1430 this->state
= NO_STATE
;
1432 return (&this->public);