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
102 status_t (*build_sa_payload
) (private_ike_sa_t
*this, sa_payload_t
**payload
);
103 status_t (*build_ke_payload
) (private_ike_sa_t
*this, ke_payload_t
**payload
);
104 status_t (*build_nonce_payload
) (private_ike_sa_t
*this, nonce_payload_t
**payload
);
106 status_t (*build_message
) (private_ike_sa_t
*this, exchange_type_t type
, bool request
, message_t
**message
);
109 status_t (*transto_ike_sa_init_requested
) (private_ike_sa_t
*this, char *name
);
110 status_t (*transto_ike_sa_init_responded
) (private_ike_sa_t
*this, message_t
*message
);
111 status_t (*transto_ike_auth_requested
) (private_ike_sa_t
*this, message_t
*message
);
115 * Identifier for the current IKE_SA
117 ike_sa_id_t
*ike_sa_id
;
120 * Linked List containing the child sa's of the current IKE_SA
122 linked_list_t
*child_sas
;
125 * Current state of the IKE_SA
127 ike_sa_state_t state
;
130 * this SA's source for random data
132 randomizer_t
*randomizer
;
134 linked_list_t
*sent_messages
;
147 * Diffie Hellman object used to compute shared secret
149 diffie_hellman_t
*diffie_hellman
;
151 * Diffie Hellman group number
153 u_int16_t dh_group_number
;
156 * Priority used get matching dh_group number
158 u_int16_t dh_group_priority
;
163 * next message id to receive
165 u_int32_t message_id_in
;
168 * next message id to send
170 u_int32_t message_id_out
;
173 * a logger for this IKE_SA
179 * @brief implements function process_message of private_ike_sa_t
181 static status_t
process_message (private_ike_sa_t
*this, message_t
*message
)
183 this->logger
->log(this->logger
, CONTROL
|MORE
, "Process message of exchange type %s",
184 mapping_find(exchange_type_m
,message
->get_exchange_type(message
)));
186 switch (message
->get_exchange_type(message
))
190 if (message
->get_request(message
)) {
191 if (this->state
== NO_STATE
)
193 /* state transission NO_STATE => IKE_SA_INIT_RESPONDED */
194 return this->transto_ike_sa_init_responded(this, message
);
199 if (this->state
== IKE_SA_INIT_REQUESTED
)
201 /* state transission IKE_SA_INIT_REQUESTED => IKE_AUTH_REQUESTED*/
202 return this->transto_ike_auth_requested(this, message
);
211 case CREATE_CHILD_SA
:
221 this->logger
->log(this->logger
, ERROR
, "processing %s-message not supported.",
222 mapping_find(exchange_type_m
,message
->get_exchange_type(message
)));
223 return NOT_SUPPORTED
;
226 this->logger
->log(this->logger
, ERROR
, "received %s-message in state %s, rejected.",
227 mapping_find(exchange_type_m
, message
->get_exchange_type(message
)),
228 mapping_find(ike_sa_state_m
, this->state
));
229 return INVALID_STATE
;
233 static status_t
build_message(private_ike_sa_t
*this, exchange_type_t type
, bool request
, message_t
**message
)
236 message_t
*new_message
;
237 host_t
*source
, *destination
;
239 new_message
= message_create();
240 if (new_message
== NULL
)
245 status
= this->me
.host
->clone(this->me
.host
, &source
);
246 status
|= this->other
.host
->clone(this->other
.host
, &destination
);
247 if (status
!= SUCCESS
)
249 new_message
->destroy(new_message
);
252 new_message
->set_source(new_message
, source
);
253 new_message
->set_destination(new_message
, destination
);
255 new_message
->set_exchange_type(new_message
, type
);
256 new_message
->set_request(new_message
, request
);
258 new_message
->set_ike_sa_id(new_message
, this->ike_sa_id
);
260 *message
= new_message
;
266 static status_t
transto_ike_sa_init_requested(private_ike_sa_t
*this, char *name
)
271 static status_t
transto_ike_sa_init_responded(private_ike_sa_t
*this, message_t
*message
)
274 linked_list_iterator_t
*payloads
;
277 status
= message
->parse_body(message
);
278 if (status
!= SUCCESS
)
287 status
= message
->get_payload_iterator(message
, &payloads
);
288 if (status
!= SUCCESS
)
290 respond
->destroy(respond
);
293 while (payloads
->has_next(payloads
))
296 payloads
->current(payloads
, (void**)payload
);
297 switch (payload
->get_type(payload
))
299 case SECURITY_ASSOCIATION
:
301 sa_payload_t
*sa_payload
;
302 linked_list_iterator_t
*proposals
;
304 sa_payload
= (sa_payload_t
*)payload
;
305 status
= sa_payload
->create_proposal_substructure_iterator(sa_payload
, &proposals
, TRUE
);
306 if (status
!= SUCCESS
)
308 payloads
->destroy(payloads
);
311 //global_configuration_manager->select_proposals_for_host
338 delete_job = (job_t *) delete_ike_sa_job_create(this->ike_sa_id);
339 if (delete_job == NULL)
341 this->logger->log(this->logger, ERROR, "Job to delete IKE SA could not be created");
344 status = global_job_queue->add(global_job_queue,delete_job);
345 if (status != SUCCESS)
347 this->logger->log(this->logger, ERROR, "%s Job to delete IKE SA could not be added to job queue",mapping_find(status_m,status));
348 delete_job->destroy_all(delete_job);
353 static status_t
transto_ike_auth_requested(private_ike_sa_t
*this, message_t
*message
)
359 * @brief implements function process_configuration of private_ike_sa_t
361 static status_t
initialize_connection(private_ike_sa_t
*this, char *name
)
368 this->logger
->log(this->logger
, CONTROL
, "initializing connection");
370 status
= global_configuration_manager
->get_local_host(global_configuration_manager
, name
, &(this->me
.host
));
371 if (status
!= SUCCESS
)
376 status
= global_configuration_manager
->get_remote_host(global_configuration_manager
, name
, &(this->other
.host
));
377 if (status
!= SUCCESS
)
382 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
);
383 if (status
!= SUCCESS
)
388 status
= this->build_message(this, IKE_SA_INIT
, TRUE
, &message
);
389 if (status
!= SUCCESS
)
395 status
= this->build_sa_payload(this, (sa_payload_t
**)&payload
);
396 if (status
!= SUCCESS
)
398 this->logger
->log(this->logger
, ERROR
, "Could not build SA payload");
399 message
->destroy(message
);
402 message
->add_payload(message
, payload
);
405 ke_payload_t
*ke_payload
;
407 this ->logger
->log(this->logger
, CONTROL
|MORE
, "create diffie hellman object");
408 if (this->ike_sa_init_data
.diffie_hellman
!= NULL
)
410 this->logger
->log(this->logger
, ERROR
, "Object of type diffie_hellman_t already existing!");
411 message
->destroy(message
);
414 this->ike_sa_init_data
.diffie_hellman
= diffie_hellman_create(this->ike_sa_init_data
.dh_group_number
);
416 if (this->ike_sa_init_data
.diffie_hellman
== NULL
)
418 this->logger
->log(this->logger
, ERROR
, "Object of type diffie_hellman_t could not be created!");
419 message
->destroy(message
);
423 this ->logger
->log(this->logger
, CONTROL
|MORE
, "get public dh value to send in ke payload");
425 status
= this->ike_sa_init_data
.diffie_hellman
->get_my_public_value(this->ike_sa_init_data
.diffie_hellman
,&key_data
);
426 if (status
!= SUCCESS
)
428 this->logger
->log(this->logger
, ERROR
, "Could not get my DH public value");
429 message
->destroy(message
);
433 ke_payload
= ke_payload_create();
434 if (ke_payload
== NULL
)
436 this->logger
->log(this->logger
, ERROR
, "Could not build KE payload");
437 message
->destroy(message
);
438 allocator_free_chunk(key_data
);
441 ke_payload
->set_dh_group_number(ke_payload
, MODP_1024_BIT
);
442 if (ke_payload
->set_key_exchange_data(ke_payload
, key_data
) != SUCCESS
)
444 this->logger
->log(this->logger
, ERROR
, "Could not build KE payload");
445 ke_payload
->destroy(ke_payload
);
446 message
->destroy(message
);
447 allocator_free_chunk(key_data
);
450 allocator_free_chunk(key_data
);
452 payload
= (payload_t
*) ke_payload
;
455 message
->add_payload(message
, payload
);
457 status
= this->build_nonce_payload(this, (nonce_payload_t
**)&payload
);
458 if (status
!= SUCCESS
)
460 this->logger
->log(this->logger
, ERROR
, "Could not build NONCE payload");
461 message
->destroy(message
);
464 payload
->set_next_type(payload
, NO_PAYLOAD
);
465 message
->add_payload(message
, payload
);
467 status
= message
->generate(message
, &packet
);
468 if (status
!= SUCCESS
)
470 this->logger
->log(this->logger
, ERROR
, "Could not generate message");
471 message
->destroy(message
);
476 global_send_queue
->add(global_send_queue
, packet
);
478 message
->destroy(message
);
480 this->state
= IKE_SA_INIT_REQUESTED
;
486 * @brief implements function private_ike_sa_t.get_id
488 static ike_sa_id_t
* get_id(private_ike_sa_t
*this)
490 return this->ike_sa_id
;
494 * implements private_ike_sa_t.build_sa_payload
496 static status_t
build_sa_payload(private_ike_sa_t
*this, sa_payload_t
**payload
)
498 sa_payload_t
* sa_payload
;
499 linked_list_iterator_t
*iterator
;
502 this->logger
->log(this->logger
, CONTROL
|MORE
, "building sa payload");
504 sa_payload
= sa_payload_create();
505 if (sa_payload
== NULL
)
509 status
= sa_payload
->create_proposal_substructure_iterator(sa_payload
, &iterator
, FALSE
);
510 if (status
!= SUCCESS
)
512 sa_payload
->destroy(sa_payload
);
515 status
= global_configuration_manager
->get_proposals_for_host(global_configuration_manager
, this->other
.host
, iterator
);
516 if (status
!= SUCCESS
)
518 sa_payload
->destroy(sa_payload
);
522 *payload
= sa_payload
;
527 static status_t
build_ke_payload(private_ike_sa_t
*this, ke_payload_t
**payload
)
533 * implements private_ike_sa_t.build_nonce_payload
535 static status_t
build_nonce_payload(private_ike_sa_t
*this, nonce_payload_t
**payload
)
537 nonce_payload_t
*nonce_payload
;
540 this->logger
->log(this->logger
, CONTROL
|MORE
, "building nonce payload");
542 if (this->randomizer
->allocate_pseudo_random_bytes(this->randomizer
, 16, &nonce
) != SUCCESS
)
547 nonce_payload
= nonce_payload_create();
548 if (nonce_payload
== NULL
)
553 nonce_payload
->set_nonce(nonce_payload
, nonce
);
555 *payload
= nonce_payload
;
561 * @brief implements function destroy of private_ike_sa_t
563 static status_t
destroy (private_ike_sa_t
*this)
565 linked_list_iterator_t
*iterator
;
567 this->child_sas
->create_iterator(this->child_sas
, &iterator
, TRUE
);
568 while (iterator
->has_next(iterator
))
571 iterator
->current(iterator
, (void**)&payload
);
572 payload
->destroy(payload
);
574 iterator
->destroy(iterator
);
575 this->child_sas
->destroy(this->child_sas
);
577 this->ike_sa_id
->destroy(this->ike_sa_id
);
578 this->sent_messages
->destroy(this->sent_messages
);
579 this->randomizer
->destroy(this->randomizer
);
580 if (this->ike_sa_init_data
.diffie_hellman
!= NULL
)
582 this->ike_sa_init_data
.diffie_hellman
->destroy(this->ike_sa_init_data
.diffie_hellman
);
585 global_logger_manager
->destroy_logger(global_logger_manager
, this->logger
);
587 allocator_free(this);
593 * Described in Header
595 ike_sa_t
* ike_sa_create(ike_sa_id_t
*ike_sa_id
)
597 private_ike_sa_t
*this = allocator_alloc_thing(private_ike_sa_t
);
604 /* Public functions */
605 this->public.process_message
= (status_t(*)(ike_sa_t
*, message_t
*)) process_message
;
606 this->public.initialize_connection
= (status_t(*)(ike_sa_t
*, char*)) initialize_connection
;
607 this->public.get_id
= (ike_sa_id_t
*(*)(ike_sa_t
*)) get_id
;
608 this->public.destroy
= (status_t(*)(ike_sa_t
*))destroy
;
610 this->build_sa_payload
= build_sa_payload
;
611 this->build_nonce_payload
= build_nonce_payload
;
612 this->build_ke_payload
= build_ke_payload
;
614 this->build_message
= build_message
;
615 this->transto_ike_sa_init_requested
= transto_ike_sa_init_requested
;
616 this->transto_ike_sa_init_responded
= transto_ike_sa_init_responded
;
617 this->transto_ike_auth_requested
= transto_ike_auth_requested
;
621 /* initialize private fields */
622 if (ike_sa_id
->clone(ike_sa_id
,&(this->ike_sa_id
)) != SUCCESS
)
624 allocator_free(this);
627 this->child_sas
= linked_list_create();
628 if (this->child_sas
== NULL
)
630 this->ike_sa_id
->destroy(this->ike_sa_id
);
631 allocator_free(this);
634 this->randomizer
= randomizer_create();
635 if (this->randomizer
== NULL
)
637 this->child_sas
->destroy(this->child_sas
);
638 this->ike_sa_id
->destroy(this->ike_sa_id
);
639 allocator_free(this);
641 this->sent_messages
= linked_list_create();
642 if (this->sent_messages
== NULL
)
644 this->randomizer
->destroy(this->randomizer
);
645 this->child_sas
->destroy(this->child_sas
);
646 this->ike_sa_id
->destroy(this->ike_sa_id
);
647 allocator_free(this);
649 this->logger
= global_logger_manager
->create_logger(global_logger_manager
, IKE_SA
, NULL
);
650 if (this->logger
== NULL
)
652 this->randomizer
->destroy(this->randomizer
);
653 this->child_sas
->destroy(this->child_sas
);
654 this->ike_sa_id
->destroy(this->ike_sa_id
);
655 this->sent_messages
->destroy(this->sent_messages
);
656 allocator_free(this);
659 this->me
.host
= NULL
;
660 this->other
.host
= NULL
;
661 this->ike_sa_init_data
.diffie_hellman
= NULL
;
662 this->ike_sa_init_data
.dh_group_number
= 0;
663 /* 1 means highest priority */
664 this->ike_sa_init_data
.dh_group_priority
= 1;
665 this->message_id_out
= 0;
666 this->message_id_in
= 0;
669 /* at creation time, IKE_SA isn't in a specific state */
670 this->state
= NO_STATE
;
672 return (&this->public);