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 "utils/allocator.h"
29 #include "utils/linked_list.h"
30 #include "utils/logger_manager.h"
31 #include "utils/randomizer.h"
32 #include "payloads/sa_payload.h"
33 #include "payloads/nonce_payload.h"
34 #include "payloads/ke_payload.h"
35 #include "payloads/transform_substructure.h"
36 #include "payloads/transform_attribute.h"
40 * States in which a IKE_SA can actually be
42 typedef enum ike_sa_state_e ike_sa_state_t
;
47 * IKE_SA is is not in a state
52 * A IKE_SA_INIT-message was sent: role initiator
54 IKE_SA_INIT_REQUESTED
= 2,
57 * A IKE_SA_INIT-message was replied: role responder
59 IKE_SA_INIT_RESPONDED
= 3,
62 * An IKE_AUTH-message was sent after a successful
63 * IKE_SA_INIT-exchange: role initiator
65 IKE_AUTH_REQUESTED
= 4,
68 * An IKE_AUTH-message was replied: role responder.
69 * In this state, all the informations for an IKE_SA
70 * and one CHILD_SA are known.
72 IKE_SA_INITIALIZED
= 5
77 * Private data of an message_t object
79 typedef struct private_ike_sa_s private_ike_sa_t
;
81 struct private_ike_sa_s
{
84 * Public part of a ike_sa_t object
88 status_t (*build_sa_payload
) (private_ike_sa_t
*this, sa_payload_t
**payload
);
89 status_t (*build_nonce_payload
) (private_ike_sa_t
*this, nonce_payload_t
**payload
);
90 status_t (*build_ke_payload
) (private_ike_sa_t
*this, ke_payload_t
**payload
);
94 * Identifier for the current IKE_SA
96 ike_sa_id_t
*ike_sa_id
;
99 * Linked List containing the child sa's of the current IKE_SA
101 linked_list_t
*child_sas
;
104 * Current state of the IKE_SA
106 ike_sa_state_t current_state
;
109 * is this IKE_SA the original initiator of this IKE_SA
111 bool original_initiator
;
114 * this SA's source for random data
116 randomizer_t
*randomizer
;
118 linked_list_t
*sent_messages
;
129 * a logger for this IKE_SA
135 * @brief implements function process_message of private_ike_sa_t
137 static status_t
process_message (private_ike_sa_t
*this, message_t
*message
)
140 /* @TODO Add Message Processing here */
142 this->logger
->log(this->logger
, CONTROL_MORE
, "Process message of exchange type %s",mapping_find(exchange_type_m
,message
->get_exchange_type(message
)));
145 status
= message
->parse_body(message
);
154 this->logger
->log(this->logger
, ERROR
, "Error of type %s while parsing message body",mapping_find(status_m
,status
));
155 switch (this->current_state
)
160 /* create delete job for this ike_sa */
161 delete_job
= (job_t
*) delete_ike_sa_job_create(this->ike_sa_id
);
162 if (delete_job
== NULL
)
164 this->logger
->log(this->logger
, ERROR
, "Job to delete IKE SA could not be created");
167 status
= global_job_queue
->add(global_job_queue
,delete_job
);
168 if (status
!= SUCCESS
)
170 this->logger
->log(this->logger
, ERROR
, "%s Job to delete IKE SA could not be added to job queue",mapping_find(status_m
,status
));
171 delete_job
->destroy_all(delete_job
);
189 * @brief implements function process_configuration of private_ike_sa_t
191 static status_t
initialize_connection(private_ike_sa_t
*this, char *name
)
196 host_t
*source
, *destination
;
199 this->logger
->log(this->logger
, CONTROL
, "initializing connection");
201 this->original_initiator
= TRUE
;
203 status
= global_configuration_manager
->get_local_host(global_configuration_manager
, name
, &source
);
204 if (status
!= SUCCESS
)
209 status
= global_configuration_manager
->get_remote_host(global_configuration_manager
, name
, &destination
);
210 if (status
!= SUCCESS
)
215 message
= message_create();
223 message
->set_source(message
, source
);
224 message
->set_destination(message
, destination
);
226 message
->set_exchange_type(message
, IKE_SA_INIT
);
227 message
->set_original_initiator(message
, this->original_initiator
);
228 message
->set_message_id(message
, 0);
229 message
->set_ike_sa_id(message
, this->ike_sa_id
);
230 message
->set_request(message
, TRUE
);
232 status
= this->build_sa_payload(this, (sa_payload_t
**)&payload
);
233 if (status
!= SUCCESS
)
235 this->logger
->log(this->logger
, ERROR
, "Could not build SA payload");
236 message
->destroy(message
);
239 payload
->set_next_type(payload
, KEY_EXCHANGE
);
240 message
->add_payload(message
, payload
);
242 status
= this->build_ke_payload(this, (ke_payload_t
**)&payload
);
243 if (status
!= SUCCESS
)
245 this->logger
->log(this->logger
, ERROR
, "Could not build KE payload");
246 message
->destroy(message
);
249 payload
->set_next_type(payload
, NONCE
);
250 message
->add_payload(message
, payload
);
252 status
= this->build_nonce_payload(this, (nonce_payload_t
**)&payload
);
253 if (status
!= SUCCESS
)
255 this->logger
->log(this->logger
, ERROR
, "Could not build NONCE payload");
256 message
->destroy(message
);
259 payload
->set_next_type(payload
, NO_PAYLOAD
);
260 message
->add_payload(message
, payload
);
262 status
= message
->generate(message
, &packet
);
263 if (status
!= SUCCESS
)
265 this->logger
->log(this->logger
, ERROR
, "Could not generate message");
266 message
->destroy(message
);
271 global_send_queue
->add(global_send_queue
, packet
);
273 message
->destroy(message
);
280 * @brief implements function private_ike_sa_t.get_id
282 static ike_sa_id_t
* get_id(private_ike_sa_t
*this)
284 return this->ike_sa_id
;
288 * implements private_ike_sa_t.build_sa_payload
290 static status_t
build_sa_payload(private_ike_sa_t
*this, sa_payload_t
**payload
)
292 sa_payload_t
*sa_payload
;
293 proposal_substructure_t
*proposal
;
294 transform_substructure_t
*transform
;
295 transform_attribute_t
*attribute
;
298 this->logger
->log(this->logger
, CONTROL_MORE
, "building sa payload");
300 sa_payload
= sa_payload_create();
301 if (sa_payload
== NULL
)
307 { /* no loop, just to break */
308 proposal
= proposal_substructure_create();
309 if (proposal
== NULL
)
313 sa_payload
->add_proposal_substructure(sa_payload
, proposal
);
316 * Encryption Algorithm
318 transform
= transform_substructure_create();
319 if (transform
== NULL
)
323 proposal
->add_transform_substructure(proposal
, transform
);
324 transform
->set_is_last_transform(transform
, FALSE
);
325 transform
->set_transform_type(transform
, ENCRYPTION_ALGORITHM
);
326 transform
->set_transform_id(transform
, ENCR_AES_CBC
);
328 attribute
= transform_attribute_create();
329 if (attribute
== NULL
)
333 transform
->add_transform_attribute(transform
, attribute
);
334 attribute
->set_attribute_type(attribute
, KEY_LENGTH
);
335 attribute
->set_value(attribute
, 16);
338 * Pseudo-random Function
340 transform
= transform_substructure_create();
341 if (transform
== NULL
)
345 proposal
->add_transform_substructure(proposal
, transform
);
346 transform
->set_is_last_transform(transform
, FALSE
);
347 transform
->set_transform_type(transform
, PSEUDO_RANDOM_FUNCTION
);
348 transform
->set_transform_id(transform
, PRF_HMAC_SHA1
);
350 attribute
= transform_attribute_create();
351 if (attribute
== NULL
)
355 transform
->add_transform_attribute(transform
, attribute
);
356 attribute
->set_attribute_type(attribute
, KEY_LENGTH
);
357 attribute
->set_value(attribute
, 16);
361 * Integrity Algorithm
363 transform
= transform_substructure_create();
364 if (transform
== NULL
)
368 proposal
->add_transform_substructure(proposal
, transform
);
369 transform
->set_is_last_transform(transform
, FALSE
);
370 transform
->set_transform_type(transform
, INTEGRITIY_ALGORITHM
);
371 transform
->set_transform_id(transform
, AUTH_HMAC_SHA1_96
);
373 attribute
= transform_attribute_create();
374 if (attribute
== NULL
)
378 transform
->add_transform_attribute(transform
, attribute
);
379 attribute
->set_attribute_type(attribute
, KEY_LENGTH
);
380 attribute
->set_value(attribute
, 16);
384 * Diffie-Hellman Group
386 transform
= transform_substructure_create();
387 if (transform
== NULL
)
391 proposal
->add_transform_substructure(proposal
, transform
);
392 transform
->set_is_last_transform(transform
, FALSE
);
393 transform
->set_transform_type(transform
, DIFFIE_HELLMAN_GROUP
);
394 transform
->set_transform_id(transform
, MODP_1024_BIT
);
396 *payload
= sa_payload
;
406 * implements private_ike_sa_t.build_ke_payload
408 static status_t
build_ke_payload(private_ike_sa_t
*this, ke_payload_t
**payload
)
410 ke_payload_t
*ke_payload
;
414 this->logger
->log(this->logger
, CONTROL_MORE
, "building ke payload");
416 key_data
.ptr
= "12345";
417 key_data
.len
= strlen("12345");
420 ke_payload
= ke_payload_create();
421 if (ke_payload
== NULL
)
425 ke_payload
->set_dh_group_number(ke_payload
, MODP_1024_BIT
);
426 if (ke_payload
->set_key_exchange_data(ke_payload
, key_data
) != SUCCESS
)
428 ke_payload
->destroy(ke_payload
);
431 *payload
= ke_payload
;
436 * implements private_ike_sa_t.build_nonce_payload
438 static status_t
build_nonce_payload(private_ike_sa_t
*this, nonce_payload_t
**payload
)
440 nonce_payload_t
*nonce_payload
;
443 this->logger
->log(this->logger
, CONTROL_MORE
, "building nonce payload");
445 if (this->randomizer
->allocate_pseudo_random_bytes(this->randomizer
, 16, &nonce
) != SUCCESS
)
450 nonce_payload
= nonce_payload_create();
451 if (nonce_payload
== NULL
)
456 nonce_payload
->set_nonce(nonce_payload
, nonce
);
458 *payload
= nonce_payload
;
464 * @brief implements function destroy of private_ike_sa_t
466 static status_t
destroy (private_ike_sa_t
*this)
468 linked_list_iterator_t
*iterator
;
470 this->child_sas
->create_iterator(this->child_sas
, &iterator
, TRUE
);
471 while (iterator
->has_next(iterator
))
474 iterator
->current(iterator
, (void**)&payload
);
475 payload
->destroy(payload
);
477 iterator
->destroy(iterator
);
478 this->child_sas
->destroy(this->child_sas
);
480 this->ike_sa_id
->destroy(this->ike_sa_id
);
481 this->sent_messages
->destroy(this->sent_messages
);
482 this->randomizer
->destroy(this->randomizer
);
484 global_logger_manager
->destroy_logger(global_logger_manager
, this->logger
);
486 allocator_free(this);
492 * Described in Header
494 ike_sa_t
* ike_sa_create(ike_sa_id_t
*ike_sa_id
)
496 private_ike_sa_t
*this = allocator_alloc_thing(private_ike_sa_t
);
503 /* Public functions */
504 this->public.process_message
= (status_t(*)(ike_sa_t
*, message_t
*)) process_message
;
505 this->public.initialize_connection
= (status_t(*)(ike_sa_t
*, char*)) initialize_connection
;
506 this->public.get_id
= (ike_sa_id_t
*(*)(ike_sa_t
*)) get_id
;
507 this->public.destroy
= (status_t(*)(ike_sa_t
*))destroy
;
509 this->build_sa_payload
= build_sa_payload
;
510 this->build_ke_payload
= build_ke_payload
;
511 this->build_nonce_payload
= build_nonce_payload
;
515 /* initialize private fields */
516 if (ike_sa_id
->clone(ike_sa_id
,&(this->ike_sa_id
)) != SUCCESS
)
518 allocator_free(this);
521 this->child_sas
= linked_list_create();
522 if (this->child_sas
== NULL
)
524 this->ike_sa_id
->destroy(this->ike_sa_id
);
525 allocator_free(this);
528 this->randomizer
= randomizer_create();
529 if (this->randomizer
== NULL
)
531 this->child_sas
->destroy(this->child_sas
);
532 this->ike_sa_id
->destroy(this->ike_sa_id
);
533 allocator_free(this);
535 this->sent_messages
= linked_list_create();
536 if (this->sent_messages
== NULL
)
538 this->randomizer
->destroy(this->randomizer
);
539 this->child_sas
->destroy(this->child_sas
);
540 this->ike_sa_id
->destroy(this->ike_sa_id
);
541 allocator_free(this);
543 this->logger
= global_logger_manager
->create_logger(global_logger_manager
, IKE_SA
, NULL
);
544 if (this->logger
== NULL
)
546 this->randomizer
->destroy(this->randomizer
);
547 this->child_sas
->destroy(this->child_sas
);
548 this->ike_sa_id
->destroy(this->ike_sa_id
);
549 this->sent_messages
->destroy(this->sent_messages
);
550 allocator_free(this);
553 this->me
.host
= NULL
;
554 this->other
.host
= NULL
;
557 /* at creation time, IKE_SA isn't in a specific state */
558 this->current_state
= NO_STATE
;
560 return (&this->public);