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"
43 * This implementation supports only window size 1
48 * States in which a IKE_SA can actually be
50 typedef enum ike_sa_state_e ike_sa_state_t
;
55 * IKE_SA is is not in a state
60 * A IKE_SA_INIT-message was sent: role initiator
62 IKE_SA_INIT_REQUESTED
= 2,
65 * A IKE_SA_INIT-message was replied: role responder
67 IKE_SA_INIT_RESPONDED
= 3,
70 * An IKE_AUTH-message was sent after a successful
71 * IKE_SA_INIT-exchange: role initiator
73 IKE_AUTH_REQUESTED
= 4,
76 * An IKE_AUTH-message was replied: role responder.
77 * In this state, all the informations for an IKE_SA
78 * and one CHILD_SA are known.
80 IKE_SA_INITIALIZED
= 5
84 * string mappings for ike_sa_state
86 mapping_t ike_sa_state_m
[] = {
87 {NO_STATE
, "NO_STATE"},
88 {IKE_SA_INIT_REQUESTED
, "IKE_SA_INIT_REQUESTED"},
89 {IKE_SA_INIT_RESPONDED
, "IKE_SA_INIT_RESPONDED"},
90 {IKE_AUTH_REQUESTED
, "IKE_AUTH_REQUESTED"},
91 {IKE_SA_INITIALIZED
, "IKE_SA_INITIALIZED"},
97 * Private data of an message_t object
99 typedef struct private_ike_sa_s private_ike_sa_t
;
101 struct private_ike_sa_s
{
104 * Public part of a ike_sa_t object
108 status_t (*build_sa_payload
) (private_ike_sa_t
*this, sa_payload_t
**payload
);
109 status_t (*build_ke_payload
) (private_ike_sa_t
*this, ke_payload_t
**payload
);
110 status_t (*build_nonce_payload
) (private_ike_sa_t
*this, nonce_payload_t
**payload
);
112 status_t (*build_message
) (private_ike_sa_t
*this, exchange_type_t type
, bool request
, message_t
**message
);
115 status_t (*transto_ike_sa_init_requested
) (private_ike_sa_t
*this, char *name
);
116 status_t (*transto_ike_sa_init_responded
) (private_ike_sa_t
*this, message_t
*message
);
117 status_t (*transto_ike_auth_requested
) (private_ike_sa_t
*this, message_t
*message
);
121 * Identifier for the current IKE_SA
123 ike_sa_id_t
*ike_sa_id
;
126 * Linked List containing the child sa's of the current IKE_SA
128 linked_list_t
*child_sas
;
131 * Current state of the IKE_SA
133 ike_sa_state_t state
;
136 * this SA's source for random data
138 randomizer_t
*randomizer
;
141 * contains the last X responded messages
143 * X is windows size (here 1)
145 linked_list_t
*responded_messages
;
148 * contains the last X requested messages
150 * X is windows size (here 1)
152 linked_list_t
*requested_messages
;
165 * Diffie Hellman object used to compute shared secret
167 diffie_hellman_t
*diffie_hellman
;
169 * Diffie Hellman group number
171 u_int16_t dh_group_number
;
174 * Priority used get matching dh_group number
176 u_int16_t dh_group_priority
;
184 chunk_t received_nonce
;
189 * next message id to receive
191 u_int32_t message_id_in
;
194 * next message id to send
196 u_int32_t message_id_out
;
199 * a logger for this IKE_SA
205 * @brief implements function process_message of private_ike_sa_t
207 static status_t
process_message (private_ike_sa_t
*this, message_t
*message
)
209 u_int32_t message_id
;
211 is_request
= message
->get_request(message
);
212 this->logger
->log(this->logger
, CONTROL
|MORE
, "Process %s message of exchange type %s",(is_request
) ?
"REQUEST" : "RESPONSE",
213 mapping_find(exchange_type_m
,message
->get_exchange_type(message
)));
216 //message->get_exchange_type(message);
219 switch (message
->get_exchange_type(message
))
223 if (message
->get_request(message
)) {
224 if (this->state
== NO_STATE
)
226 /* state transission NO_STATE => IKE_SA_INIT_RESPONDED */
227 return this->transto_ike_sa_init_responded(this, message
);
232 if (this->state
== IKE_SA_INIT_REQUESTED
)
234 /* state transission IKE_SA_INIT_REQUESTED => IKE_AUTH_REQUESTED*/
235 return this->transto_ike_auth_requested(this, message
);
244 case CREATE_CHILD_SA
:
254 this->logger
->log(this->logger
, ERROR
, "processing %s-message not supported.",
255 mapping_find(exchange_type_m
,message
->get_exchange_type(message
)));
256 return NOT_SUPPORTED
;
259 this->logger
->log(this->logger
, ERROR
, "received %s-message in state %s, rejected.",
260 mapping_find(exchange_type_m
, message
->get_exchange_type(message
)),
261 mapping_find(ike_sa_state_m
, this->state
));
262 return INVALID_STATE
;
266 static status_t
build_message(private_ike_sa_t
*this, exchange_type_t type
, bool request
, message_t
**message
)
269 message_t
*new_message
;
270 host_t
*source
, *destination
;
272 new_message
= message_create();
273 if (new_message
== NULL
)
278 status
= this->me
.host
->clone(this->me
.host
, &source
);
279 status
|= this->other
.host
->clone(this->other
.host
, &destination
);
280 if (status
!= SUCCESS
)
282 new_message
->destroy(new_message
);
285 new_message
->set_source(new_message
, source
);
286 new_message
->set_destination(new_message
, destination
);
288 new_message
->set_exchange_type(new_message
, type
);
289 new_message
->set_request(new_message
, request
);
293 new_message
->set_message_id(new_message
, this->message_id_out
);
296 new_message
->set_message_id(new_message
, this->message_id_in
);
299 new_message
->set_ike_sa_id(new_message
, this->ike_sa_id
);
301 *message
= new_message
;
307 static status_t
transto_ike_sa_init_requested(private_ike_sa_t
*this, char *name
)
314 this->logger
->log(this->logger
, CONTROL
, "initializing connection");
316 status
= global_configuration_manager
->get_local_host(global_configuration_manager
, name
, &(this->me
.host
));
317 if (status
!= SUCCESS
)
322 status
= global_configuration_manager
->get_remote_host(global_configuration_manager
, name
, &(this->other
.host
));
323 if (status
!= SUCCESS
)
328 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
);
329 if (status
!= SUCCESS
)
334 this ->logger
->log(this->logger
, CONTROL
|MORE
, "create diffie hellman object");
335 if (this->ike_sa_init_data
.diffie_hellman
!= NULL
)
337 this->logger
->log(this->logger
, ERROR
, "Object of type diffie_hellman_t already existing!");
340 this->ike_sa_init_data
.diffie_hellman
= diffie_hellman_create(this->ike_sa_init_data
.dh_group_number
);
341 if (this->ike_sa_init_data
.diffie_hellman
== NULL
)
343 this->logger
->log(this->logger
, ERROR
, "Object of type diffie_hellman_t could not be created!");
347 if (this->ike_sa_init_data
.sent_nonce
.ptr
!= NULL
)
349 this->logger
->log(this->logger
, ERROR
, "Nonce for IKE_SA_INIT phase already existing!");
353 if (this->randomizer
->allocate_pseudo_random_bytes(this->randomizer
, 16, &(this->ike_sa_init_data
.sent_nonce
)) != SUCCESS
)
355 this->logger
->log(this->logger
, ERROR
, "Could not create nonce!");
360 /* going to build message */
362 status
= this->build_message(this, IKE_SA_INIT
, TRUE
, &message
);
363 if (status
!= SUCCESS
)
365 this->logger
->log(this->logger
, ERROR
, "Could not build message");
369 /* build SA payload */
370 status
= this->build_sa_payload(this, (sa_payload_t
**)&payload
);
371 if (status
!= SUCCESS
)
373 this->logger
->log(this->logger
, ERROR
, "Could not build SA payload");
374 message
->destroy(message
);
377 message
->add_payload(message
, payload
);
379 /* build KE payload */
380 status
= this->build_ke_payload(this,(ke_payload_t
**) &payload
);
381 if (status
!= SUCCESS
)
383 this->logger
->log(this->logger
, ERROR
, "Could not build KE payload");
384 message
->destroy(message
);
387 message
->add_payload(message
, payload
);
389 /* build Nonce payload */
390 status
= this->build_nonce_payload(this, (nonce_payload_t
**)&payload
);
391 if (status
!= SUCCESS
)
393 this->logger
->log(this->logger
, ERROR
, "Could not build NONCE payload");
394 message
->destroy(message
);
397 message
->add_payload(message
, payload
);
400 status
= message
->generate(message
, &packet
);
401 if (status
!= SUCCESS
)
403 this->logger
->log(this->logger
, ERROR
, "Could not generate message");
404 message
->destroy(message
);
408 status
= global_send_queue
->add(global_send_queue
, packet
);
409 if (status
!= SUCCESS
)
411 this->logger
->log(this->logger
, ERROR
, "Could not add packet to send queue");
412 message
->destroy(message
);
416 if ( this->requested_messages
->get_count(this->requested_messages
) >= WINDOW_SIZE
)
418 message_t
*removed_message
;
419 /* destroy message */
420 this->requested_messages
->remove_last(this->requested_messages
,(void **)&removed_message
);
421 removed_message
->destroy(removed_message
);
424 status
= this->requested_messages
->insert_first(this->requested_messages
,(void *) message
);
425 if (status
!= SUCCESS
)
427 this->logger
->log(this->logger
, ERROR
, "Could not store last received message");
428 message
->destroy(message
);
432 /* message counter can now be increased */
433 this->message_id_out
++;
435 /* states has NOW changed :-) */
436 this->state
= IKE_SA_INIT_REQUESTED
;
441 static status_t
transto_ike_sa_init_responded(private_ike_sa_t
*this, message_t
*message
)
444 linked_list_iterator_t
*payloads
;
447 status
= message
->parse_body(message
);
448 if (status
!= SUCCESS
)
457 status
= message
->get_payload_iterator(message
, &payloads
);
458 if (status
!= SUCCESS
)
460 respond
->destroy(respond
);
463 while (payloads
->has_next(payloads
))
466 payloads
->current(payloads
, (void**)payload
);
467 switch (payload
->get_type(payload
))
469 case SECURITY_ASSOCIATION
:
471 sa_payload_t
*sa_payload
;
472 linked_list_iterator_t
*proposals
;
474 sa_payload
= (sa_payload_t
*)payload
;
475 status
= sa_payload
->create_proposal_substructure_iterator(sa_payload
, &proposals
, TRUE
);
476 if (status
!= SUCCESS
)
478 payloads
->destroy(payloads
);
481 //global_configuration_manager->select_proposals_for_host
508 delete_job = (job_t *) delete_ike_sa_job_create(this->ike_sa_id);
509 if (delete_job == NULL)
511 this->logger->log(this->logger, ERROR, "Job to delete IKE SA could not be created");
514 status = global_job_queue->add(global_job_queue,delete_job);
515 if (status != SUCCESS)
517 this->logger->log(this->logger, ERROR, "%s Job to delete IKE SA could not be added to job queue",mapping_find(status_m,status));
518 delete_job->destroy_all(delete_job);
523 static status_t
transto_ike_auth_requested(private_ike_sa_t
*this, message_t
*message
)
529 * @brief implements function process_configuration of private_ike_sa_t
531 static status_t
initialize_connection(private_ike_sa_t
*this, char *name
)
533 /* work is done in transto_ike_sa_init_requested */
534 return (this->transto_ike_sa_init_requested(this,name
));
538 * @brief implements function private_ike_sa_t.get_id
540 static ike_sa_id_t
* get_id(private_ike_sa_t
*this)
542 return this->ike_sa_id
;
546 * implements private_ike_sa_t.build_sa_payload
548 static status_t
build_sa_payload(private_ike_sa_t
*this, sa_payload_t
**payload
)
550 sa_payload_t
* sa_payload
;
551 linked_list_iterator_t
*iterator
;
554 this->logger
->log(this->logger
, CONTROL
|MORE
, "building sa payload");
556 sa_payload
= sa_payload_create();
557 if (sa_payload
== NULL
)
561 status
= sa_payload
->create_proposal_substructure_iterator(sa_payload
, &iterator
, FALSE
);
562 if (status
!= SUCCESS
)
564 sa_payload
->destroy(sa_payload
);
567 status
= global_configuration_manager
->get_proposals_for_host(global_configuration_manager
, this->other
.host
, iterator
);
568 if (status
!= SUCCESS
)
570 sa_payload
->destroy(sa_payload
);
574 *payload
= sa_payload
;
579 static status_t
build_ke_payload(private_ike_sa_t
*this, ke_payload_t
**payload
)
581 ke_payload_t
*ke_payload
;
585 this->logger
->log(this->logger
, CONTROL
|MORE
, "building ke payload");
587 if (this->state
!= NO_STATE
)
589 this->logger
->log(this->logger
, ERROR
, "KE payload in state %s not supported",mapping_find(ike_sa_state_m
,this->state
));
593 switch(this->ike_sa_id
->is_initiator(this->ike_sa_id
))
597 this ->logger
->log(this->logger
, CONTROL
|MORE
, "get public dh value to send in ke payload");
598 status
= this->ike_sa_init_data
.diffie_hellman
->get_my_public_value(this->ike_sa_init_data
.diffie_hellman
,&key_data
);
599 if (status
!= SUCCESS
)
601 this->logger
->log(this->logger
, ERROR
, "Could not get my DH public value");
605 ke_payload
= ke_payload_create();
606 if (ke_payload
== NULL
)
608 this->logger
->log(this->logger
, ERROR
, "Could not create KE payload");
609 allocator_free_chunk(key_data
);
612 ke_payload
->set_dh_group_number(ke_payload
, MODP_1024_BIT
);
613 if (ke_payload
->set_key_exchange_data(ke_payload
, key_data
) != SUCCESS
)
615 this->logger
->log(this->logger
, ERROR
, "Could not set key exchange data of KE payload");
616 ke_payload
->destroy(ke_payload
);
617 allocator_free_chunk(key_data
);
620 allocator_free_chunk(key_data
);
622 *payload
= ke_payload
;
635 * implements private_ike_sa_t.build_nonce_payload
637 static status_t
build_nonce_payload(private_ike_sa_t
*this, nonce_payload_t
**payload
)
639 nonce_payload_t
*nonce_payload
;
641 this->logger
->log(this->logger
, CONTROL
|MORE
, "building nonce payload");
642 nonce_payload
= nonce_payload_create();
643 if (nonce_payload
== NULL
)
648 nonce_payload
->set_nonce(nonce_payload
, this->ike_sa_init_data
.sent_nonce
);
650 *payload
= nonce_payload
;
656 * @brief implements function destroy of private_ike_sa_t
658 static status_t
destroy (private_ike_sa_t
*this)
660 /* destroy child sa's */
661 while (this->child_sas
->get_count(this->child_sas
) > 0)
664 if (this->child_sas
->remove_first(this->child_sas
,&child_sa
) != SUCCESS
)
668 /* destroy child sa */
670 this->child_sas
->destroy(this->child_sas
);
672 /* destroy ike_sa_id */
673 this->ike_sa_id
->destroy(this->ike_sa_id
);
675 /* destroy stored requested messages */
676 while (this->requested_messages
->get_count(this->requested_messages
) > 0)
679 if (this->requested_messages
->remove_first(this->requested_messages
,(void **) &message
) != SUCCESS
)
683 message
->destroy(message
);
685 this->requested_messages
->destroy(this->requested_messages
);
687 /* destroy stored responded messages */
688 while (this->responded_messages
->get_count(this->responded_messages
) > 0)
691 if (this->responded_messages
->remove_first(this->responded_messages
,(void **) &message
) != SUCCESS
)
695 message
->destroy(message
);
697 this->responded_messages
->destroy(this->responded_messages
);
700 this->randomizer
->destroy(this->randomizer
);
701 if (this->ike_sa_init_data
.diffie_hellman
!= NULL
)
703 this->ike_sa_init_data
.diffie_hellman
->destroy(this->ike_sa_init_data
.diffie_hellman
);
705 if (this->ike_sa_init_data
.sent_nonce
.ptr
!= NULL
)
707 allocator_free_chunk(this->ike_sa_init_data
.sent_nonce
);
709 if (this->ike_sa_init_data
.received_nonce
.ptr
!= NULL
)
711 allocator_free_chunk(this->ike_sa_init_data
.received_nonce
);
714 global_logger_manager
->destroy_logger(global_logger_manager
, this->logger
);
716 allocator_free(this);
722 * Described in Header
724 ike_sa_t
* ike_sa_create(ike_sa_id_t
*ike_sa_id
)
726 private_ike_sa_t
*this = allocator_alloc_thing(private_ike_sa_t
);
733 /* Public functions */
734 this->public.process_message
= (status_t(*)(ike_sa_t
*, message_t
*)) process_message
;
735 this->public.initialize_connection
= (status_t(*)(ike_sa_t
*, char*)) initialize_connection
;
736 this->public.get_id
= (ike_sa_id_t
*(*)(ike_sa_t
*)) get_id
;
737 this->public.destroy
= (status_t(*)(ike_sa_t
*))destroy
;
739 /* private functions */
740 this->build_sa_payload
= build_sa_payload
;
741 this->build_nonce_payload
= build_nonce_payload
;
742 this->build_ke_payload
= build_ke_payload
;
743 this->build_message
= build_message
;
744 this->transto_ike_sa_init_requested
= transto_ike_sa_init_requested
;
745 this->transto_ike_sa_init_responded
= transto_ike_sa_init_responded
;
746 this->transto_ike_auth_requested
= transto_ike_auth_requested
;
750 /* initialize private fields */
751 if (ike_sa_id
->clone(ike_sa_id
,&(this->ike_sa_id
)) != SUCCESS
)
753 allocator_free(this);
756 this->child_sas
= linked_list_create();
757 if (this->child_sas
== NULL
)
759 this->ike_sa_id
->destroy(this->ike_sa_id
);
760 allocator_free(this);
763 this->randomizer
= randomizer_create();
764 if (this->randomizer
== NULL
)
766 this->child_sas
->destroy(this->child_sas
);
767 this->ike_sa_id
->destroy(this->ike_sa_id
);
768 allocator_free(this);
770 this->responded_messages
= linked_list_create();
771 if (this->responded_messages
== NULL
)
773 this->randomizer
->destroy(this->randomizer
);
774 this->child_sas
->destroy(this->child_sas
);
775 this->ike_sa_id
->destroy(this->ike_sa_id
);
776 allocator_free(this);
778 this->requested_messages
= linked_list_create();
779 if (this->requested_messages
== NULL
)
781 this->randomizer
->destroy(this->randomizer
);
782 this->child_sas
->destroy(this->child_sas
);
783 this->ike_sa_id
->destroy(this->ike_sa_id
);
784 this->responded_messages
->destroy(this->responded_messages
);
785 allocator_free(this);
788 this->logger
= global_logger_manager
->create_logger(global_logger_manager
, IKE_SA
, NULL
);
789 if (this->logger
== NULL
)
791 this->randomizer
->destroy(this->randomizer
);
792 this->child_sas
->destroy(this->child_sas
);
793 this->ike_sa_id
->destroy(this->ike_sa_id
);
794 this->responded_messages
->destroy(this->responded_messages
);
795 this->requested_messages
->destroy(this->requested_messages
);
796 allocator_free(this);
799 this->me
.host
= NULL
;
800 this->other
.host
= NULL
;
801 this->ike_sa_init_data
.diffie_hellman
= NULL
;
802 this->ike_sa_init_data
.dh_group_number
= 0;
803 /* 1 means highest priority */
804 this->ike_sa_init_data
.dh_group_priority
= 1;
805 this->ike_sa_init_data
.sent_nonce
.len
= 0;
806 this->ike_sa_init_data
.sent_nonce
.ptr
= NULL
;
807 this->ike_sa_init_data
.received_nonce
.len
= 0;
808 this->ike_sa_init_data
.received_nonce
.ptr
= NULL
;
809 this->message_id_out
= 0;
810 this->message_id_in
= 0;
813 /* at creation time, IKE_SA isn't in a specific state */
814 this->state
= NO_STATE
;
816 return (&this->public);