4 * @brief Implementation of ike_sa_t.
9 * Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger
10 * Copyright (C) 2005-2006 Martin Willi
11 * Copyright (C) 2005 Jan Hutter
12 * Hochschule fuer Technik Rapperswil
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
32 #include <definitions.h>
33 #include <utils/linked_list.h>
34 #include <utils/logger_manager.h>
35 #include <crypto/diffie_hellman.h>
36 #include <crypto/prf_plus.h>
37 #include <crypto/crypters/crypter.h>
38 #include <crypto/hashers/hasher.h>
39 #include <encoding/payloads/sa_payload.h>
40 #include <encoding/payloads/nonce_payload.h>
41 #include <encoding/payloads/ke_payload.h>
42 #include <encoding/payloads/delete_payload.h>
43 #include <encoding/payloads/transform_substructure.h>
44 #include <encoding/payloads/transform_attribute.h>
45 #include <encoding/payloads/ts_payload.h>
46 #include <sa/transactions/transaction.h>
47 #include <sa/transactions/ike_sa_init.h>
48 #include <sa/transactions/delete_ike_sa.h>
49 #include <sa/transactions/create_child_sa.h>
50 #include <sa/transactions/delete_child_sa.h>
51 #include <sa/transactions/dead_peer_detection.h>
52 #include <queues/jobs/retransmit_request_job.h>
53 #include <queues/jobs/delete_established_ike_sa_job.h>
54 #include <queues/jobs/delete_half_open_ike_sa_job.h>
55 #include <queues/jobs/send_dpd_job.h>
56 #include <queues/jobs/send_keepalive_job.h>
60 * String mappings for ike_sa_state_t.
62 mapping_t ike_sa_state_m
[] = {
63 {IKE_CREATED
, "CREATED"},
64 {IKE_CONNECTING
, "CONNECTING"},
65 {IKE_ESTABLISHED
, "ESTABLISHED"},
66 {IKE_DELETING
, "DELETING"},
71 typedef struct private_ike_sa_t private_ike_sa_t
;
74 * Private data of an ike_sa_t object.
76 struct private_ike_sa_t
{
84 * Identifier for the current IKE_SA.
86 ike_sa_id_t
*ike_sa_id
;
89 * Linked List containing the child sa's of the current IKE_SA.
91 linked_list_t
*child_sas
;
94 * Current state of the IKE_SA
99 * Connection definition used for this IKE_SA
101 connection_t
*connection
;
104 * Policy definition used for this IKE_SA
109 * crypter for inbound traffic
111 crypter_t
*crypter_in
;
114 * crypter for outbound traffic
116 crypter_t
*crypter_out
;
119 * Signer for inbound traffic
124 * Signer for outbound traffic
126 signer_t
*signer_out
;
129 * Multi purpose prf, set key, use it, forget it
134 * Prf function for derivating keymat child SAs
139 * PRF, with key set to pi_key, used for authentication
144 * PRF, with key set to pr_key, used for authentication
149 * A logger for this IKE_SA.
156 hasher_t
*nat_hasher
;
159 * NAT status of local host.
164 * NAT status of remote host.
169 * message ID for next outgoung request
171 u_int32_t message_id_out
;
174 * Timestamp of last IKE message received on this SA
179 * Timestamp of last IKE message sent on this SA
181 time_t time_outbound
;
184 * List of queued transactions to process
186 linked_list_t
*transaction_queue
;
189 * Transaction currently initiated
190 * (only one supported yet, window size = 1)
192 transaction_t
*transaction_out
;
195 * last transaction initiated by peer processed.
196 * (only one supported yet, window size = 1)
197 * Stored for retransmission.
199 transaction_t
*transaction_in
;
202 * Next incoming transaction expected. Used to
203 * do multi transaction operations.
205 transaction_t
*transaction_in_next
;
209 * get the time of the latest traffic processed by the kernel
211 static time_t get_esp_time(private_ike_sa_t
* this, bool inbound
)
213 iterator_t
*iterator
;
214 child_sa_t
*child_sa
;
215 time_t latest
= 0, use_time
;
217 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
218 while (iterator
->iterate(iterator
, (void**)&child_sa
))
220 if (child_sa
->get_use_time(child_sa
, inbound
, &use_time
) == SUCCESS
)
222 latest
= max(latest
, use_time
);
225 iterator
->destroy(iterator
);
231 * get the time of the latest received traffice
233 static time_t get_time_inbound(private_ike_sa_t
*this)
235 return max(this->time_inbound
, get_esp_time(this, TRUE
));
239 * get the time of the latest sent traffic
241 static time_t get_time_outbound(private_ike_sa_t
*this)
243 return max(this->time_outbound
, get_esp_time(this, FALSE
));
248 * Update connection host, as addresses may change (NAT)
250 static void update_hosts(private_ike_sa_t
*this, host_t
*me
, host_t
*other
)
255 * 2.11. Address and Port Agility
257 * IKE runs over UDP ports 500 and 4500, and implicitly sets up ESP and
258 * AH associations for the same IP addresses it runs over. The IP
259 * addresses and ports in the outer header are, however, not themselves
260 * cryptographically protected, and IKE is designed to work even through
261 * Network Address Translation (NAT) boxes. An implementation MUST
262 * accept incoming requests even if the source port is not 500 or 4500,
263 * and MUST respond to the address and port from which the request was
264 * received. It MUST specify the address and port at which the request
265 * was received as the source address and port in the response. IKE
266 * functions identically over IPv4 or IPv6.
270 * There are cases where a NAT box decides to remove mappings that
271 * are still alive (for example, the keepalive interval is too long,
272 * or the NAT box is rebooted). To recover in these cases, hosts
273 * that are not behind a NAT SHOULD send all packets (including
274 * retransmission packets) to the IP address and port from the last
275 * valid authenticated packet from the other end (i.e., dynamically
276 * update the address). A host behind a NAT SHOULD NOT do this
277 * because it opens a DoS attack possibility. Any authenticated IKE
278 * packet or any authenticated UDP-encapsulated ESP packet can be
279 * used to detect that the IP address or the port has changed.
281 host_t
*old_other
= NULL
;
282 iterator_t
*iterator
= NULL
;
283 child_sa_t
*child_sa
= NULL
;
284 int my_changes
, other_changes
;
286 my_changes
= me
->get_differences(me
, this->connection
->get_my_host(this->connection
));
288 old_other
= this->connection
->get_other_host(this->connection
);
289 other_changes
= other
->get_differences(other
, old_other
);
291 if (!my_changes
&& !other_changes
)
298 this->connection
->update_my_host(this->connection
, me
->clone(me
));
303 /* update without restrictions if we are not NATted */
306 this->connection
->update_other_host(this->connection
, other
->clone(other
));
311 /* if we are natted, only port may change */
312 if (other_changes
& HOST_DIFF_ADDR
)
316 else if (other_changes
& HOST_DIFF_PORT
)
318 old_other
->set_port(old_other
, other
->get_port(other
));
321 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
322 while (iterator
->iterate(iterator
, (void**)&child_sa
))
324 child_sa
->update_hosts(child_sa
,
325 this->connection
->get_my_host(this->connection
),
326 this->connection
->get_other_host(this->connection
),
327 my_changes
, other_changes
);
328 /* TODO: what to do if update fails? Delete CHILD_SA? */
330 iterator
->destroy(iterator
);
334 * send a request and schedule retransmission
336 static status_t
transmit_request(private_ike_sa_t
*this)
341 retransmit_request_job_t
*job
;
342 u_int32_t transmitted
;
344 transaction_t
*transaction
= this->transaction_out
;
345 u_int32_t message_id
= transaction
->get_message_id(transaction
);
347 transmitted
= transaction
->requested(transaction
);
348 timeout
= charon
->configuration
->get_retransmit_timeout(charon
->configuration
,
352 this->logger
->log(this->logger
, ERROR
,
353 "giving up after %d retransmits, deleting IKE_SA",
358 status
= transaction
->get_request(transaction
, &request
);
359 if (status
!= SUCCESS
)
363 /* if we retransmit, the request is already generated */
364 if (transmitted
== 0)
366 status
= request
->generate(request
, this->crypter_out
, this->signer_out
, &packet
);
367 if (status
!= SUCCESS
)
374 this->logger
->log(this->logger
, CONTROL
,
375 "sending retransmit %d for %s request with message ID %d",
377 mapping_find(exchange_type_m
, request
->get_exchange_type(request
)),
379 packet
= request
->get_packet(request
);
382 charon
->send_queue
->add(charon
->send_queue
, packet
);
383 this->time_outbound
= time(NULL
);
385 /* schedule retransmission job */
386 job
= retransmit_request_job_create(message_id
, this->ike_sa_id
);
387 charon
->event_queue
->add_relative(charon
->event_queue
, (job_t
*)job
, timeout
);
392 * Implementation of ike_sa.retransmit_request.
394 static status_t
retransmit_request(private_ike_sa_t
*this, u_int32_t message_id
)
396 if (this->transaction_out
== NULL
||
397 this->transaction_out
->get_message_id(this->transaction_out
) != message_id
)
399 /* no retransmit necessary, transaction did already complete */
402 return transmit_request(this);
406 * Check for transactions in the queue and initiate the first transaction found.
408 static status_t
process_transaction_queue(private_ike_sa_t
*this)
410 if (this->transaction_out
)
412 /* already a transaction in progress */
418 if (this->transaction_queue
->remove_first(this->transaction_queue
,
419 (void**)&this->transaction_out
) != SUCCESS
)
421 /* transaction queue empty */
424 switch (transmit_request(this))
429 /* critical, IKE_SA unusable, destroy immediately */
430 this->logger
->log(this->logger
, ERROR
,
431 "transaction initiaton failed, deleting IKE_SA");
434 /* discard transaction, process next one */
435 this->logger
->log(this->logger
, ERROR
,
436 "transaction initiation failed, discarded");
437 this->transaction_out
->destroy(this->transaction_out
);
438 this->transaction_out
= NULL
;
439 /* handle next transaction */
446 * Queue a new transaction and execute the next outstanding transaction
448 static status_t
queue_transaction(private_ike_sa_t
*this, transaction_t
*transaction
, bool prefer
)
450 /* inject next transaction */
455 this->transaction_queue
->insert_first(this->transaction_queue
, transaction
);
459 this->transaction_queue
->insert_last(this->transaction_queue
, transaction
);
462 /* process a transaction */
463 return process_transaction_queue(this);
467 * process an incoming request.
469 static status_t
process_request(private_ike_sa_t
*this, message_t
*request
)
471 transaction_t
*last
, *current
= NULL
;
474 u_int32_t request_mid
;
477 request_mid
= request
->get_message_id(request
);
478 last
= this->transaction_in
;
480 /* check if message ID is correct */
483 u_int32_t last_mid
= last
->get_message_id(last
);
485 if (last_mid
== request_mid
)
487 /* retransmit detected */
488 this->logger
->log(this->logger
, ERROR
,
489 "received retransmitted request for message ID %d, retransmitting response",
491 last
->get_response(last
, request
, &response
, &this->transaction_in_next
);
492 packet
= response
->get_packet(response
);
493 charon
->send_queue
->add(charon
->send_queue
, packet
);
494 this->time_outbound
= time(NULL
);
498 if (last_mid
> request_mid
)
500 /* something seriously wrong here, message id may not decrease */
501 this->logger
->log(this->logger
, ERROR
,
502 "received request with message ID %d, excepted %d, ingored",
503 request_mid
, last_mid
+ 1);
506 /* we allow jumps in message IDs, as long as they are incremental */
507 if (last_mid
+ 1 < request_mid
)
509 this->logger
->log(this->logger
, ERROR
,
510 "received request with message ID %d, excepted %d",
511 request_mid
, last_mid
+ 1);
516 if (request_mid
!= 0)
518 /* warn, but allow it */
519 this->logger
->log(this->logger
, CONTROL
,
520 "first received request has message ID %d, excepted 0",
525 /* check if we already have a pre-created transaction for this request */
526 if (this->transaction_in_next
)
528 current
= this->transaction_in_next
;
529 this->transaction_in_next
= NULL
;
533 current
= transaction_create(&this->public, request
);
536 this->logger
->log(this->logger
, ERROR
,
537 "no idea how to handle received message (%d), ignored",
538 request
->get_exchange_type(request
));
543 /* send message. get_request() always gives a valid response */
544 status
= current
->get_response(current
, request
, &response
, &this->transaction_in_next
);
545 if (response
->generate(response
, this->crypter_out
, this->signer_out
, &packet
) != SUCCESS
)
547 this->logger
->log(this->logger
, ERROR
,
548 "response generation failed, discarding transaction");
549 current
->destroy(current
);
553 charon
->send_queue
->add(charon
->send_queue
, packet
);
554 this->time_outbound
= time(NULL
);
555 /* act depending on transaction result */
559 /* transactions says we should destroy the IKE_SA, so do it */
560 current
->destroy(current
);
563 /* store for retransmission, destroy old transaction */
564 this->transaction_in
= current
;
574 * process an incoming response
576 static status_t
process_response(private_ike_sa_t
*this, message_t
*response
)
578 transaction_t
*current
, *new = NULL
;
580 current
= this->transaction_out
;
581 /* check if message ID is that of our currently active transaction */
582 if (current
== NULL
||
583 current
->get_message_id(current
) !=
584 response
->get_message_id(response
))
586 this->logger
->log(this->logger
, ERROR
,
587 "received response with message ID %d not requested, ignored");
591 switch (current
->conclude(current
, response
, &new))
594 /* state requested to destroy IKE_SA */
597 /* discard transaction, process next one */
600 /* transaction comleted, remove */
601 current
->destroy(current
);
602 this->transaction_out
= NULL
;
604 /* queue new transaction */
605 return queue_transaction(this, new, TRUE
);
609 * send a notify back to the sender
611 static void send_notify_response(private_ike_sa_t
*this,
615 notify_payload_t
*notify
;
620 response
= message_create();
621 dst
= request
->get_source(request
);
622 src
= request
->get_destination(request
);
623 response
->set_source(response
, src
->clone(src
));
624 response
->set_destination(response
, dst
->clone(dst
));
625 response
->set_exchange_type(response
, request
->get_exchange_type(request
));
626 response
->set_request(response
, FALSE
);
627 response
->set_message_id(response
, request
->get_message_id(request
));
628 response
->set_ike_sa_id(response
, this->ike_sa_id
);
629 notify
= notify_payload_create_from_protocol_and_type(PROTO_NONE
, type
);
630 response
->add_payload(response
, (payload_t
*)notify
);
631 if (response
->generate(response
, this->crypter_out
, this->signer_out
, &packet
) != SUCCESS
)
633 response
->destroy(response
);
636 charon
->send_queue
->add(charon
->send_queue
, packet
);
637 this->time_outbound
= time(NULL
);
638 response
->destroy(response
);
644 * Implementation of ike_sa_t.process_message.
646 static status_t
process_message(private_ike_sa_t
*this, message_t
*message
)
651 is_request
= message
->get_request(message
);
653 status
= message
->parse_body(message
, this->crypter_in
, this->signer_in
);
654 if (status
!= SUCCESS
)
659 this->logger
->log(this->logger
, ERROR
,
660 "ciritcal unknown payloads found");
663 send_notify_response(this, message
, UNSUPPORTED_CRITICAL_PAYLOAD
);
667 this->logger
->log(this->logger
, ERROR
,
668 "message parsing failed");
671 send_notify_response(this, message
, INVALID_SYNTAX
);
675 this->logger
->log(this->logger
, ERROR
,
676 "message verification failed");
679 send_notify_response(this, message
, INVALID_SYNTAX
);
683 this->logger
->log(this->logger
, ERROR
,
684 "integrity check failed");
688 this->logger
->log(this->logger
, ERROR
,
689 "found encrypted message, but no keys available");
692 send_notify_response(this, message
, INVALID_SYNTAX
);
697 this->logger
->log(this->logger
, ERROR
,
698 "%s %s with message ID %d processing failed",
699 mapping_find(exchange_type_m
, message
->get_exchange_type(message
)),
700 message
->get_request(message
) ?
"request" : "response",
701 message
->get_message_id(message
));
705 /* check if message is trustworthy, and update connection information */
706 if ((this->state
== IKE_CREATED
&& this->connection
) ||
707 message
->get_exchange_type(message
) != IKE_SA_INIT
)
709 update_hosts(this, message
->get_destination(message
),
710 message
->get_source(message
));
711 this->time_inbound
= time(NULL
);
715 status
= process_request(this, message
);
719 status
= process_response(this, message
);
726 * Implementation of ike_sa_t.initiate.
728 static status_t
initiate(private_ike_sa_t
*this, connection_t
*connection
)
730 ike_sa_init_t
*ike_sa_init
;
732 /* set connection and policy */
733 this->connection
= connection
;
734 this->policy
= charon
->policies
->get_policy_by_name(charon
->policies
,
735 this->connection
->get_name(this->connection
));
736 if (this->policy
== NULL
)
738 this->logger
->log(this->logger
, ERROR
,
739 "no policy found for connection %s, aborting",
740 connection
->get_name(connection
));
743 this->message_id_out
= 1;
744 ike_sa_init
= ike_sa_init_create(&this->public);
745 return queue_transaction(this, (transaction_t
*)ike_sa_init
, TRUE
);
749 * Implementation of ike_sa_t.send_dpd
751 static status_t
send_dpd(private_ike_sa_t
*this)
754 time_t diff
, interval
;
755 status_t status
= SUCCESS
;
757 interval
= charon
->configuration
->get_dpd_interval(charon
->configuration
);
759 if (this->transaction_out
)
761 /* there is a transaction in progress. Come back later */
766 /* check if there was any inbound traffic */
768 last_in
= get_time_inbound(this);
770 diff
= now
- last_in
;
771 if (diff
>= interval
)
773 /* to long ago, initiate dead peer detection */
774 dead_peer_detection_t
*dpd
;
775 this->logger
->log(this->logger
, CONTROL
, "sending DPD request");
776 dpd
= dead_peer_detection_create(&this->public);
777 status
= queue_transaction(this, (transaction_t
*)dpd
, FALSE
);
781 /* recheck in "interval" seconds */
782 job
= send_dpd_job_create(this->ike_sa_id
);
783 charon
->event_queue
->add_relative(charon
->event_queue
, (job_t
*)job
,
784 (interval
- diff
) * 1000);
789 * Implementation of ike_sa_t.send_keepalive
791 static void send_keepalive(private_ike_sa_t
*this)
793 send_keepalive_job_t
*job
;
794 time_t last_out
, now
, diff
, interval
;
796 last_out
= get_time_outbound(this);
799 diff
= now
- last_out
;
800 interval
= charon
->configuration
->get_keepalive_interval(charon
->configuration
);
802 if (diff
>= interval
)
808 packet
= packet_create();
809 me
= this->connection
->get_my_host(this->connection
);
810 other
= this->connection
->get_other_host(this->connection
);
811 packet
->set_source(packet
, me
->clone(me
));
812 packet
->set_destination(packet
, other
->clone(other
));
813 data
.ptr
= malloc(1);
816 packet
->set_data(packet
, data
);
817 charon
->send_queue
->add(charon
->send_queue
, packet
);
818 this->logger
->log(this->logger
, CONTROL
, "sending keep alive");
821 job
= send_keepalive_job_create(this->ike_sa_id
);
822 charon
->event_queue
->add_relative(charon
->event_queue
, (job_t
*)job
,
823 (interval
- diff
) * 1000);
827 * Implementation of ike_sa_t.get_state.
829 static ike_sa_state_t
get_state(private_ike_sa_t
*this)
835 * Implementation of ike_sa_t.set_state.
837 static void set_state(private_ike_sa_t
*this, ike_sa_state_t state
)
839 this->logger
->log(this->logger
, CONTROL
, "state change: %s => %s",
840 mapping_find(ike_sa_state_m
, this->state
),
841 mapping_find(ike_sa_state_m
, state
));
842 if (state
== IKE_ESTABLISHED
)
844 host_t
*my_host
, *other_host
;
845 identification_t
*my_id
, *other_id
;
846 my_host
= this->connection
->get_my_host(this->connection
);
847 other_host
= this->connection
->get_other_host(this->connection
);
848 my_id
= this->policy
->get_my_id(this->policy
);
849 other_id
= this->policy
->get_other_id(this->policy
);
850 this->logger
->log(this->logger
, AUDIT
, "IKE_SA established: %s[%s]...%s[%s]",
851 my_host
->get_address(my_host
),
852 my_id
->get_string(my_id
),
853 other_host
->get_address(other_host
),
854 other_id
->get_string(other_id
));
862 * Implementation of protected_ike_sa_t.get_connection.
864 static connection_t
*get_connection(private_ike_sa_t
*this)
866 return this->connection
;
870 * Implementation of protected_ike_sa_t.set_connection.
872 static void set_connection(private_ike_sa_t
*this,connection_t
* connection
)
874 this->connection
= connection
;
878 * Implementation of protected_ike_sa_t.get_policy.
880 static policy_t
*get_policy(private_ike_sa_t
*this)
886 * Implementation of protected_ike_sa_t.set_policy.
888 static void set_policy(private_ike_sa_t
*this,policy_t
* policy
)
890 this->policy
= policy
;
894 * Implementation of protected_ike_sa_t.get_prf.
896 static prf_t
*get_prf(private_ike_sa_t
*this)
902 * Implementation of protected_ike_sa_t.get_prf.
904 static prf_t
*get_child_prf(private_ike_sa_t
*this)
906 return this->child_prf
;
910 * Implementation of protected_ike_sa_t.get_prf_auth_i.
912 static prf_t
*get_prf_auth_i(private_ike_sa_t
*this)
914 return this->prf_auth_i
;
918 * Implementation of protected_ike_sa_t.get_prf_auth_r.
920 static prf_t
*get_prf_auth_r(private_ike_sa_t
*this)
922 return this->prf_auth_r
;
925 * Implementation of ike_sa_t.get_id.
927 static ike_sa_id_t
* get_id(private_ike_sa_t
*this)
929 return this->ike_sa_id
;
933 * Implementation of protected_ike_sa_t.build_transforms.
935 static status_t
build_transforms(private_ike_sa_t
*this, proposal_t
*proposal
,
936 diffie_hellman_t
*dh
, chunk_t nonce_i
, chunk_t nonce_r
,
939 chunk_t nonces
, nonces_spis
, skeyseed
, key
, secret
;
940 u_int64_t spi_i
, spi_r
;
941 prf_plus_t
*prf_plus
;
944 crypter_t
*crypter_i
, *crypter_r
;
945 signer_t
*signer_i
, *signer_r
;
947 /* Build the PRF+ instance for deriving keys */
948 if (!proposal
->get_algorithm(proposal
, PSEUDO_RANDOM_FUNCTION
, &algo
))
950 this->logger
->log(this->logger
, ERROR
, "no PSEUDO_RANDOM_FUNCTION selected!");
953 this->prf
= prf_create(algo
->algorithm
);
954 if (this->prf
== NULL
)
956 this->logger
->log(this->logger
, ERROR
, "PSEUDO_RANDOM_FUNCTION %s not supported!",
957 mapping_find(pseudo_random_function_m
, algo
->algorithm
));
961 /* nonces = nonce_i | nonce_r */
962 nonces
= chunk_alloc(nonce_i
.len
+ nonce_r
.len
);
963 memcpy(nonces
.ptr
, nonce_i
.ptr
, nonce_i
.len
);
964 memcpy(nonces
.ptr
+ nonce_i
.len
, nonce_r
.ptr
, nonce_r
.len
);
966 /* prf_seed = nonce_i | nonce_r | spi_i | spi_r */
967 nonces_spis
= chunk_alloc(nonces
.len
+ 16);
968 memcpy(nonces_spis
.ptr
, nonces
.ptr
, nonces
.len
);
969 spi_i
= this->ike_sa_id
->get_initiator_spi(this->ike_sa_id
);
970 spi_r
= this->ike_sa_id
->get_responder_spi(this->ike_sa_id
);
971 memcpy(nonces_spis
.ptr
+ nonces
.len
, &spi_i
, 8);
972 memcpy(nonces_spis
.ptr
+ nonces
.len
+ 8, &spi_r
, 8);
974 /* SKEYSEED = prf(Ni | Nr, g^ir) */
975 dh
->get_shared_secret(dh
, &secret
);
976 this->logger
->log_chunk(this->logger
, PRIVATE
, "shared Diffie Hellman secret", secret
);
977 this->prf
->set_key(this->prf
, nonces
);
978 this->prf
->allocate_bytes(this->prf
, secret
, &skeyseed
);
979 this->logger
->log_chunk(this->logger
, PRIVATE
|LEVEL1
, "SKEYSEED", skeyseed
);
982 /* prf+ (SKEYSEED, Ni | Nr | SPIi | SPIr )
983 * = SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr
985 this->prf
->set_key(this->prf
, skeyseed
);
986 prf_plus
= prf_plus_create(this->prf
, nonces_spis
);
988 /* clean up unused stuff */
990 chunk_free(&nonces_spis
);
991 chunk_free(&skeyseed
);
993 /* SK_d used for prf+ to derive keys for child SAs */
994 this->child_prf
= prf_create(algo
->algorithm
);
995 key_size
= this->child_prf
->get_key_size(this->child_prf
);
996 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
997 this->logger
->log_chunk(this->logger
, PRIVATE
, "Sk_d secret", key
);
998 this->child_prf
->set_key(this->child_prf
, key
);
1001 /* SK_ai/SK_ar used for integrity protection */
1002 if (!proposal
->get_algorithm(proposal
, INTEGRITY_ALGORITHM
, &algo
))
1004 this->logger
->log(this->logger
, ERROR
, "no INTEGRITY_ALGORITHM selected?!");
1008 signer_i
= signer_create(algo
->algorithm
);
1009 signer_r
= signer_create(algo
->algorithm
);
1010 if (signer_i
== NULL
|| signer_r
== NULL
)
1012 this->logger
->log(this->logger
, ERROR
, "INTEGRITY_ALGORITHM %s not supported!",
1013 mapping_find(integrity_algorithm_m
,algo
->algorithm
));
1016 key_size
= signer_i
->get_key_size(signer_i
);
1018 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1019 this->logger
->log_chunk(this->logger
, CONTROL
|LEVEL1
, "Sk_ai secret", key
);
1020 signer_i
->set_key(signer_i
, key
);
1023 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1024 this->logger
->log_chunk(this->logger
, CONTROL
|LEVEL1
, "Sk_ar secret", key
);
1025 signer_r
->set_key(signer_r
, key
);
1030 this->signer_in
= signer_r
;
1031 this->signer_out
= signer_i
;
1035 this->signer_in
= signer_i
;
1036 this->signer_out
= signer_r
;
1039 /* SK_ei/SK_er used for encryption */
1040 if (!proposal
->get_algorithm(proposal
, ENCRYPTION_ALGORITHM
, &algo
))
1042 this->logger
->log(this->logger
, ERROR
, "no ENCRYPTION_ALGORITHM selected!");
1045 crypter_i
= crypter_create(algo
->algorithm
, algo
->key_size
/ 8);
1046 crypter_r
= crypter_create(algo
->algorithm
, algo
->key_size
/ 8);
1047 if (crypter_i
== NULL
|| crypter_r
== NULL
)
1049 this->logger
->log(this->logger
, ERROR
,
1050 "ENCRYPTION_ALGORITHM %s (key size %d) not supported!",
1051 mapping_find(encryption_algorithm_m
, algo
->algorithm
),
1055 key_size
= crypter_i
->get_key_size(crypter_i
);
1057 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1058 this->logger
->log_chunk(this->logger
, PRIVATE
, "Sk_ei secret", key
);
1059 crypter_i
->set_key(crypter_i
, key
);
1062 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1063 this->logger
->log_chunk(this->logger
, PRIVATE
, "Sk_er secret", key
);
1064 crypter_r
->set_key(crypter_r
, key
);
1069 this->crypter_in
= crypter_r
;
1070 this->crypter_out
= crypter_i
;
1074 this->crypter_in
= crypter_i
;
1075 this->crypter_out
= crypter_r
;
1078 /* SK_pi/SK_pr used for authentication */
1079 proposal
->get_algorithm(proposal
, PSEUDO_RANDOM_FUNCTION
, &algo
);
1080 this->prf_auth_i
= prf_create(algo
->algorithm
);
1081 this->prf_auth_r
= prf_create(algo
->algorithm
);
1083 key_size
= this->prf_auth_i
->get_key_size(this->prf_auth_i
);
1084 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1085 this->logger
->log_chunk(this->logger
, PRIVATE
, "Sk_pi secret", key
);
1086 this->prf_auth_i
->set_key(this->prf_auth_i
, key
);
1089 prf_plus
->allocate_bytes(prf_plus
, key_size
, &key
);
1090 this->logger
->log_chunk(this->logger
, PRIVATE
, "Sk_pr secret", key
);
1091 this->prf_auth_r
->set_key(this->prf_auth_r
, key
);
1094 /* all done, prf_plus not needed anymore */
1095 prf_plus
->destroy(prf_plus
);
1101 * Implementation of protected_ike_sa_t.add_child_sa.
1103 static void add_child_sa(private_ike_sa_t
*this, child_sa_t
*child_sa
)
1105 this->child_sas
->insert_last(this->child_sas
, child_sa
);
1109 * Implementation of protected_ike_sa_t.get_child_sa.
1111 static child_sa_t
* get_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
,
1112 u_int32_t spi
, bool inbound
)
1114 iterator_t
*iterator
;
1115 child_sa_t
*current
, *found
= NULL
;
1117 iterator
= this->child_sas
->create_iterator(this->child_sas
, FALSE
);
1118 while (iterator
->has_next(iterator
))
1120 iterator
->current(iterator
, (void**)¤t
);
1121 if (current
->get_spi(current
, inbound
) == spi
&&
1122 current
->get_protocol(current
) == protocol
)
1127 iterator
->destroy(iterator
);
1132 * Implementation of ike_sa_t.rekey_child_sa.
1134 static status_t
rekey_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1136 create_child_sa_t
*rekey
;
1137 child_sa_t
*child_sa
;
1139 child_sa
= get_child_sa(this, protocol
, spi
, TRUE
);
1140 if (child_sa
== NULL
)
1145 rekey
= create_child_sa_create(&this->public);
1146 rekey
->rekeys_child(rekey
, child_sa
);
1147 return queue_transaction(this, (transaction_t
*)rekey
, FALSE
);
1151 * Implementation of ike_sa_t.delete_child_sa.
1153 static status_t
delete_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1155 delete_child_sa_t
*del
;
1156 child_sa_t
*child_sa
;
1158 child_sa
= get_child_sa(this, protocol
, spi
, TRUE
);
1159 if (child_sa
== NULL
)
1164 del
= delete_child_sa_create(&this->public);
1165 del
->set_child_sa(del
, child_sa
);
1166 return queue_transaction(this, (transaction_t
*)del
, FALSE
);
1170 * Implementation of protected_ike_sa_t.destroy_child_sa.
1172 static status_t
destroy_child_sa(private_ike_sa_t
*this, protocol_id_t protocol
, u_int32_t spi
)
1174 iterator_t
*iterator
;
1175 child_sa_t
*child_sa
;
1176 status_t status
= NOT_FOUND
;
1178 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1179 while (iterator
->iterate(iterator
, (void**)&child_sa
))
1181 if (child_sa
->get_protocol(child_sa
) == protocol
&&
1182 child_sa
->get_spi(child_sa
, TRUE
) == spi
)
1184 child_sa
->destroy(child_sa
);
1185 iterator
->remove(iterator
);
1190 iterator
->destroy(iterator
);
1196 * Implementation of protected_ike_sa_t.log_status.
1198 static void log_status(private_ike_sa_t
*this, logger_t
*logger
, char *name
)
1200 iterator_t
*iterator
;
1201 child_sa_t
*child_sa
;
1202 host_t
*my_host
, *other_host
;
1203 identification_t
*my_id
= NULL
, *other_id
= NULL
;
1205 /* only log if name == NULL or name == connection_name */
1208 if (streq(this->connection
->get_name(this->connection
), name
))
1213 my_host
= this->connection
->get_my_host(this->connection
);
1214 other_host
= this->connection
->get_other_host(this->connection
);
1216 /* use policy information, if available */
1219 my_id
= this->policy
->get_my_id(this->policy
);
1220 other_id
= this->policy
->get_other_id(this->policy
);
1221 name
= this->policy
->get_name(this->policy
);
1225 name
= this->connection
->get_name(this->connection
);
1230 logger
= this->logger
;
1232 logger
->log(logger
, CONTROL
|LEVEL1
, " \"%s\": IKE_SA in state %s, SPIs: 0x%.16llx 0x%.16llx",
1234 mapping_find(ike_sa_state_m
, this->state
),
1235 this->ike_sa_id
->get_initiator_spi(this->ike_sa_id
),
1236 this->ike_sa_id
->get_responder_spi(this->ike_sa_id
));
1237 logger
->log(logger
, CONTROL
, " \"%s\": %s[%s]...%s[%s]",
1239 my_host
->get_address(my_host
),
1240 my_id ? my_id
->get_string(my_id
) : "(unknown)",
1241 other_host
->get_address(other_host
),
1242 other_id ? other_id
->get_string(other_id
) : "(unknown)");
1244 iterator
= this->child_sas
->create_iterator(this->child_sas
, TRUE
);
1245 while (iterator
->has_next(iterator
))
1247 iterator
->current(iterator
, (void**)&child_sa
);
1248 child_sa
->log_status(child_sa
, logger
, name
);
1250 iterator
->destroy(iterator
);
1254 * Implementation of public_ike_sa_t.delete.
1256 static status_t
delete_(private_ike_sa_t
*this)
1258 delete_ike_sa_t
*delete_ike_sa
;
1259 delete_ike_sa
= delete_ike_sa_create(&this->public);
1261 return queue_transaction(this, (transaction_t
*)delete_ike_sa
, FALSE
);
1265 * Implementation of ike_sa_t.get_next_message_id.
1267 static u_int32_t
get_next_message_id (private_ike_sa_t
*this)
1269 return this->message_id_out
++;
1273 * Implementation of ike_sa_t.is_natt_enabled.
1275 static bool is_natt_enabled (private_ike_sa_t
*this)
1277 return this->nat_here
|| this->nat_there
;
1281 * Implementation of protected_ike_sa_t.enable_natt.
1283 static void enable_natt (private_ike_sa_t
*this, bool local
)
1287 this->logger
->log(this->logger
, CONTROL
,
1288 "local host is behind NAT, using NAT-T, scheduled keep alives");
1289 this->nat_here
= TRUE
;
1290 send_keepalive(this);
1294 this->logger
->log(this->logger
, CONTROL
,
1295 "remote host is behind NAT, using NAT-T");
1296 this->nat_there
= TRUE
;
1301 * Implementation of protected_ike_sa_t.destroy.
1303 static void destroy(private_ike_sa_t
*this)
1305 child_sa_t
*child_sa
;
1306 transaction_t
*transaction
;
1308 this->logger
->log(this->logger
, CONTROL
|LEVEL2
, "going to destroy IKE SA %llu:%llu, role %s",
1309 this->ike_sa_id
->get_initiator_spi(this->ike_sa_id
),
1310 this->ike_sa_id
->get_responder_spi(this->ike_sa_id
),
1311 this->ike_sa_id
->is_initiator(this->ike_sa_id
) ?
"initiator" : "responder");
1313 if (this->state
== IKE_ESTABLISHED
)
1315 this->logger
->log(this->logger
, ERROR
,
1316 "destroying an established IKE SA without knowledge from remote peer!");
1319 while (this->child_sas
->remove_last(this->child_sas
, (void**)&child_sa
) == SUCCESS
)
1321 child_sa
->destroy(child_sa
);
1323 this->child_sas
->destroy(this->child_sas
);
1325 while (this->transaction_queue
->remove_last(this->transaction_queue
, (void**)&transaction
) == SUCCESS
)
1327 transaction
->destroy(transaction
);
1329 this->transaction_queue
->destroy(this->transaction_queue
);
1330 if (this->transaction_in
)
1332 this->transaction_in
->destroy(this->transaction_in
);
1334 if (this->transaction_in_next
)
1336 this->transaction_in_next
->destroy(this->transaction_in_next
);
1338 if (this->transaction_out
)
1340 this->transaction_out
->destroy(this->transaction_out
);
1342 if (this->crypter_in
)
1344 this->crypter_in
->destroy(this->crypter_in
);
1346 if (this->crypter_out
)
1348 this->crypter_out
->destroy(this->crypter_out
);
1350 if (this->signer_in
)
1352 this->signer_in
->destroy(this->signer_in
);
1354 if (this->signer_out
)
1356 this->signer_out
->destroy(this->signer_out
);
1360 this->prf
->destroy(this->prf
);
1362 if (this->child_prf
)
1364 this->child_prf
->destroy(this->child_prf
);
1366 if (this->prf_auth_i
)
1368 this->prf_auth_i
->destroy(this->prf_auth_i
);
1370 if (this->prf_auth_r
)
1372 this->prf_auth_r
->destroy(this->prf_auth_r
);
1374 if (this->connection
)
1376 host_t
*my_host
, *other_host
;
1377 identification_t
*my_id
= NULL
, *other_id
= NULL
;
1378 my_host
= this->connection
->get_my_host(this->connection
);
1379 other_host
= this->connection
->get_other_host(this->connection
);
1382 my_id
= this->policy
->get_my_id(this->policy
);
1383 other_id
= this->policy
->get_other_id(this->policy
);
1386 this->logger
->log(this->logger
, AUDIT
, "IKE_SA deleted between %s[%s]...%s[%s]",
1387 my_host
->get_address(my_host
),
1388 my_id ? my_id
->get_string(my_id
) : "(unknown)",
1389 other_host
->get_address(other_host
),
1390 other_id ? other_id
->get_string(other_id
) : "(unknown)");
1391 this->connection
->destroy(this->connection
);
1395 this->policy
->destroy(this->policy
);
1397 this->ike_sa_id
->destroy(this->ike_sa_id
);
1402 * Described in header.
1404 ike_sa_t
* ike_sa_create(ike_sa_id_t
*ike_sa_id
)
1406 private_ike_sa_t
*this = malloc_thing(private_ike_sa_t
);
1408 /* Public functions */
1409 this->public.get_state
= (ike_sa_state_t(*)(ike_sa_t
*)) get_state
;
1410 this->public.set_state
= (void(*)(ike_sa_t
*,ike_sa_state_t
)) set_state
;
1411 this->public.process_message
= (status_t(*)(ike_sa_t
*, message_t
*)) process_message
;
1412 this->public.initiate
= (status_t(*)(ike_sa_t
*,connection_t
*)) initiate
;
1413 this->public.get_id
= (ike_sa_id_t
*(*)(ike_sa_t
*)) get_id
;
1414 this->public.get_next_message_id
= (u_int32_t(*)(ike_sa_t
*)) get_next_message_id
;
1415 this->public.get_connection
= (connection_t
*(*)(ike_sa_t
*)) get_connection
;
1416 this->public.retransmit_request
= (status_t (*) (ike_sa_t
*, u_int32_t
)) retransmit_request
;
1417 this->public.log_status
= (void (*) (ike_sa_t
*,logger_t
*,char*))log_status
;
1418 this->public.delete = (status_t(*)(ike_sa_t
*))delete_
;
1419 this->public.destroy
= (void(*)(ike_sa_t
*))destroy
;
1420 this->public.send_dpd
= (status_t (*)(ike_sa_t
*)) send_dpd
;
1421 this->public.send_keepalive
= (void (*)(ike_sa_t
*)) send_keepalive
;
1422 this->public.get_prf
= (prf_t
*(*) (ike_sa_t
*)) get_prf
;
1423 this->public.get_child_prf
= (prf_t
*(*) (ike_sa_t
*)) get_child_prf
;
1424 this->public.get_prf_auth_i
= (prf_t
*(*) (ike_sa_t
*)) get_prf_auth_i
;
1425 this->public.get_prf_auth_r
= (prf_t
*(*) (ike_sa_t
*)) get_prf_auth_r
;
1426 this->public.set_connection
= (void (*) (ike_sa_t
*,connection_t
*)) set_connection
;
1427 this->public.get_connection
= (connection_t
*(*) (ike_sa_t
*)) get_connection
;
1428 this->public.set_policy
= (void (*) (ike_sa_t
*,policy_t
*)) set_policy
;
1429 this->public.get_policy
= (policy_t
*(*) (ike_sa_t
*)) get_policy
;
1430 this->public.build_transforms
= (status_t (*) (ike_sa_t
*,proposal_t
*,diffie_hellman_t
*,chunk_t
,chunk_t
,bool)) build_transforms
;
1431 this->public.add_child_sa
= (void (*) (ike_sa_t
*,child_sa_t
*)) add_child_sa
;
1432 this->public.get_child_sa
= (child_sa_t
* (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
,bool)) get_child_sa
;
1433 this->public.rekey_child_sa
= (status_t(*)(ike_sa_t
*,protocol_id_t
,u_int32_t
)) rekey_child_sa
;
1434 this->public.delete_child_sa
= (status_t(*)(ike_sa_t
*,protocol_id_t
,u_int32_t
)) delete_child_sa
;
1435 this->public.destroy_child_sa
= (status_t (*)(ike_sa_t
*,protocol_id_t
,u_int32_t
))destroy_child_sa
;
1436 this->public.enable_natt
= (void(*)(ike_sa_t
*, bool)) enable_natt
;
1437 this->public.is_natt_enabled
= (bool(*)(ike_sa_t
*)) is_natt_enabled
;
1439 /* initialize private fields */
1440 this->logger
= logger_manager
->get_logger(logger_manager
, IKE_SA
);
1441 this->ike_sa_id
= ike_sa_id
->clone(ike_sa_id
);
1442 this->child_sas
= linked_list_create();
1443 this->crypter_in
= NULL
;
1444 this->crypter_out
= NULL
;
1445 this->signer_in
= NULL
;
1446 this->signer_out
= NULL
;
1448 this->prf_auth_i
= NULL
;
1449 this->prf_auth_r
= NULL
;
1450 this->child_prf
= NULL
;
1451 this->connection
= NULL
;
1452 this->policy
= NULL
;
1453 this->nat_here
= FALSE
;
1454 this->nat_there
= FALSE
;
1455 this->transaction_queue
= linked_list_create();
1456 this->transaction_in
= NULL
;
1457 this->transaction_in_next
= NULL
;
1458 this->transaction_out
= NULL
;
1459 this->state
= IKE_CREATED
;
1460 /* we start with message ID out, as ike_sa_init does not use this counter */
1461 this->message_id_out
= 0;
1462 this->time_inbound
= 0;
1463 this->time_outbound
= 0;
1465 return &this->public;