4 * @brief Implementation of ike_auth_t transaction.
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
30 #include <encoding/payloads/sa_payload.h>
31 #include <encoding/payloads/id_payload.h>
32 #include <encoding/payloads/cert_payload.h>
33 #include <encoding/payloads/certreq_payload.h>
34 #include <encoding/payloads/auth_payload.h>
35 #include <encoding/payloads/ts_payload.h>
36 #include <sa/authenticators/authenticator.h>
37 #include <sa/child_sa.h>
40 typedef struct private_ike_auth_t private_ike_auth_t
;
43 * Private members of a ike_auth_t object..
45 struct private_ike_auth_t
{
48 * Public methods and transaction_t interface.
58 * Message sent by our peer, if already generated
63 * Message ID this transaction uses
68 * Times we did send the request
73 * initiator chosen nonce
78 * responder chosen nonce
83 * encoded request message of ike_sa_init transaction
88 * encoded response message of ike_sa_init transaction
90 chunk_t init_response
;
93 * connection definition used for IKE_SA setup
95 connection_t
*connection
;
98 * policy definition used CHILD_SA creation
103 * Negotiated proposal used for CHILD_SA
105 proposal_t
*proposal
;
108 * Negotiated traffic selectors for initiator
113 * Negotiated traffic selectors for responder
118 * CHILD_SA created along with IKE_AUTH
120 child_sa_t
*child_sa
;
123 * did other peer create a CHILD_SA?
128 * reqid to use for CHILD_SA setup
134 * Implementation of transaction_t.get_message_id.
136 static u_int32_t
get_message_id(private_ike_auth_t
*this)
138 return this->message_id
;
142 * Implementation of transaction_t.requested.
144 static u_int32_t
requested(private_ike_auth_t
*this)
146 return this->requested
++;
150 * Implementation of transaction_t.set_config.
152 static void set_config(private_ike_auth_t
*this,
153 connection_t
*connection
, policy_t
*policy
)
155 this->connection
= connection
;
156 this->policy
= policy
;
160 * Implementation of transaction_t.set_reqid.
162 static void set_reqid(private_ike_auth_t
*this, u_int32_t reqid
)
168 * Implementation of transaction_t.set_nonces.
170 static void set_nonces(private_ike_auth_t
*this, chunk_t nonce_i
, chunk_t nonce_r
)
172 this->nonce_i
= nonce_i
;
173 this->nonce_r
= nonce_r
;
177 * Implementation of transaction_t.set_init_messages.
179 static void set_init_messages(private_ike_auth_t
*this, chunk_t init_request
, chunk_t init_response
)
181 this->init_request
= init_request
;
182 this->init_response
= init_response
;
186 * Implementation of transaction_t.get_request.
188 static status_t
get_request(private_ike_auth_t
*this, message_t
**result
)
192 identification_t
*my_id
, *other_id
;
193 id_payload_t
*my_id_payload
;
195 /* check if we already have built a message (retransmission) */
198 *result
= this->message
;
202 me
= this->ike_sa
->get_my_host(this->ike_sa
);
203 other
= this->ike_sa
->get_other_host(this->ike_sa
);
204 my_id
= this->policy
->get_my_id(this->policy
);
205 other_id
= this->policy
->get_other_id(this->policy
);
207 /* build the request */
208 request
= message_create();
209 request
->set_source(request
, me
->clone(me
));
210 request
->set_destination(request
, other
->clone(other
));
211 request
->set_exchange_type(request
, IKE_AUTH
);
212 request
->set_request(request
, TRUE
);
213 request
->set_ike_sa_id(request
, this->ike_sa
->get_id(this->ike_sa
));
214 /* apply for caller */
216 /* store for retransmission */
217 this->message
= request
;
219 { /* build ID payload */
220 my_id_payload
= id_payload_create_from_identification(TRUE
, my_id
);
221 request
->add_payload(request
, (payload_t
*)my_id_payload
);
224 /* build certificate request payload */
225 if (this->connection
->get_certreq_policy(this->connection
) != CERT_NEVER_SEND
)
227 certreq_payload_t
*certreq_payload
;
228 identification_t
*other_ca
= this->policy
->get_other_ca(this->policy
);
230 certreq_payload
= (other_ca
->get_type(other_ca
) == ID_ANY
)
231 ?
certreq_payload_create_from_cacerts()
232 : certreq_payload_create_from_cacert(other_ca
);
234 if (certreq_payload
!= NULL
)
236 request
->add_payload(request
, (payload_t
*)certreq_payload
);
240 /* build certificate payload. TODO: Handle certreq from init_ike_sa. */
241 if (this->policy
->get_auth_method(this->policy
) == AUTH_RSA
242 && this->connection
->get_cert_policy(this->connection
) != CERT_NEVER_SEND
)
244 cert_payload_t
*cert_payload
;
246 x509_t
*cert
= charon
->credentials
->get_certificate(charon
->credentials
, my_id
);
250 cert_payload
= cert_payload_create_from_x509(cert
);
251 request
->add_payload(request
, (payload_t
*)cert_payload
);
255 DBG1(DBG_IKE
, "could not find my certificate, certificate payload omitted");
259 { /* build IDr payload, if other_id defined */
260 id_payload_t
*id_payload
;
261 if (!other_id
->contains_wildcards(other_id
))
263 id_payload
= id_payload_create_from_identification(FALSE
, other_id
);
264 request
->add_payload(request
, (payload_t
*)id_payload
);
268 { /* build auth payload */
269 authenticator_t
*authenticator
;
270 auth_payload_t
*auth_payload
;
271 auth_method_t auth_method
;
274 auth_method
= this->policy
->get_auth_method(this->policy
);
275 authenticator
= authenticator_create(this->ike_sa
, auth_method
);
276 if (authenticator
== NULL
)
278 SIG(IKE_UP_FAILED
, "auth method %N not supported, deleting IKE_SA",
279 auth_method_names
, auth_method
);
280 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
283 status
= authenticator
->build(authenticator
, this->init_request
,
284 this->nonce_r
, &auth_payload
);
285 authenticator
->destroy(authenticator
);
286 if (status
!= SUCCESS
)
288 SIG(IKE_UP_FAILED
, "could not generate AUTH data, deleting IKE_SA");
289 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
292 request
->add_payload(request
, (payload_t
*)auth_payload
);
295 { /* build SA payload for CHILD_SA */
296 linked_list_t
*proposal_list
;
297 sa_payload_t
*sa_payload
;
298 u_int32_t soft_lifetime
, hard_lifetime
;
301 proposal_list
= this->policy
->get_proposals(this->policy
);
302 soft_lifetime
= this->policy
->get_soft_lifetime(this->policy
);
303 hard_lifetime
= this->policy
->get_hard_lifetime(this->policy
);
304 enable_natt
= this->ike_sa
->is_natt_enabled(this->ike_sa
);
305 this->child_sa
= child_sa_create(this->reqid
, me
, other
, my_id
, other_id
,
306 soft_lifetime
, hard_lifetime
,
307 this->policy
->get_updown(this->policy
),
308 this->policy
->get_hostaccess(this->policy
),
310 this->child_sa
->set_name(this->child_sa
, this->policy
->get_name(this->policy
));
311 if (this->child_sa
->alloc(this->child_sa
, proposal_list
) != SUCCESS
)
313 SIG(IKE_UP_FAILED
, "could not install CHILD_SA, deleting IKE_SA");
314 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
317 sa_payload
= sa_payload_create_from_proposal_list(proposal_list
);
318 proposal_list
->destroy_offset(proposal_list
, offsetof(proposal_t
, destroy
));
319 request
->add_payload(request
, (payload_t
*)sa_payload
);
322 { /* build TSi payload */
323 linked_list_t
*ts_list
;
324 ts_payload_t
*ts_payload
;
326 ts_list
= this->policy
->get_my_traffic_selectors(this->policy
, me
);
327 ts_payload
= ts_payload_create_from_traffic_selectors(TRUE
, ts_list
);
328 ts_list
->destroy_offset(ts_list
, offsetof(traffic_selector_t
, destroy
));
330 request
->add_payload(request
, (payload_t
*)ts_payload
);
333 { /* build TSr payload */
334 linked_list_t
*ts_list
;
335 ts_payload_t
*ts_payload
;
337 ts_list
= this->policy
->get_other_traffic_selectors(this->policy
, other
);
338 ts_payload
= ts_payload_create_from_traffic_selectors(FALSE
, ts_list
);
339 ts_list
->destroy_offset(ts_list
, offsetof(traffic_selector_t
, destroy
));
341 request
->add_payload(request
, (payload_t
*)ts_payload
);
344 this->message_id
= this->ike_sa
->get_next_message_id(this->ike_sa
);
345 request
->set_message_id(request
, this->message_id
);
350 * Handle all kind of notifies
352 static status_t
process_notifies(private_ike_auth_t
*this, notify_payload_t
*notify_payload
)
354 notify_type_t notify_type
= notify_payload
->get_notify_type(notify_payload
);
356 DBG2(DBG_IKE
, "process notify type %N", notify_type_names
, notify_type
);
360 /* these notifies are not critical. no child_sa is built, but IKE stays alive */
361 case SINGLE_PAIR_REQUIRED
:
363 SIG(CHILD_UP_FAILED
, "received a SINGLE_PAIR_REQUIRED notify");
364 this->build_child
= FALSE
;
367 case TS_UNACCEPTABLE
:
369 SIG(CHILD_UP_FAILED
, "received TS_UNACCEPTABLE notify");
370 this->build_child
= FALSE
;
373 case NO_PROPOSAL_CHOSEN
:
375 SIG(CHILD_UP_FAILED
, "received NO_PROPOSAL_CHOSEN notify");
376 this->build_child
= FALSE
;
381 if (notify_type
< 16383)
383 SIG(IKE_UP_FAILED
, "received %N notify error, deleting IKE_SA",
384 notify_type_names
, notify_type
);
389 DBG1(DBG_IKE
, "received %N notify, ignored",
390 notify_type_names
, notify_type
);
398 * Build a notify message.
400 static void build_notify(notify_type_t type
, message_t
*message
, bool flush_message
)
402 notify_payload_t
*notify
;
407 iterator_t
*iterator
= message
->get_payload_iterator(message
);
408 while (iterator
->iterate(iterator
, (void**)&payload
))
410 payload
->destroy(payload
);
411 iterator
->remove(iterator
);
413 iterator
->destroy(iterator
);
416 notify
= notify_payload_create();
417 notify
->set_notify_type(notify
, type
);
418 message
->add_payload(message
, (payload_t
*)notify
);
422 * Import certificate requests from a certreq payload
424 static void add_certificate_request(certreq_payload_t
*certreq_payload
,
425 linked_list_t
*requested_ca_keyids
)
429 cert_encoding_t encoding
= certreq_payload
->get_cert_encoding(certreq_payload
);
431 if (encoding
!= CERT_X509_SIGNATURE
)
433 DBG1(DBG_IKE
, "certreq payload %N not supported, ignored",
434 cert_encoding_names
, encoding
);
438 keyids
= certreq_payload
->get_data(certreq_payload
);
440 while (keyids
.len
>= HASH_SIZE_SHA1
)
442 chunk_t keyid
= { keyids
.ptr
, HASH_SIZE_SHA1
};
443 x509_t
*cacert
= charon
->credentials
->get_ca_certificate_by_keyid(charon
->credentials
, keyid
);
447 DBG2(DBG_IKE
, "request for certificate issued by ca '%D'", cacert
->get_subject(cacert
));
448 requested_ca_keyids
->insert_last(requested_ca_keyids
, (void *)&keyid
);
452 DBG2(DBG_IKE
, "request for certificate issued by unknown ca");
454 DBG2(DBG_IKE
, " with keyid %#B", &keyid
);
456 keyids
.ptr
+= HASH_SIZE_SHA1
;
457 keyids
.len
-= HASH_SIZE_SHA1
;
462 * Import a certificate from a cert payload
464 static void import_certificate(cert_payload_t
*cert_payload
)
468 cert_encoding_t encoding
;
470 encoding
= cert_payload
->get_cert_encoding(cert_payload
);
471 if (encoding
!= CERT_X509_SIGNATURE
)
473 DBG1(DBG_IKE
, "certificate payload %N not supported, ignored",
474 cert_encoding_names
, encoding
);
477 cert
= x509_create_from_chunk(cert_payload
->get_data_clone(cert_payload
));
480 if (charon
->credentials
->verify(charon
->credentials
, cert
, &found
))
482 DBG2(DBG_IKE
, "received end entity certificate is trusted, added to store");
485 charon
->credentials
->add_end_certificate(charon
->credentials
, cert
);
494 DBG1(DBG_IKE
, "received end entity certificate is not trusted, discarded");
500 DBG1(DBG_IKE
, "parsing of received certificate failed, discarded");
505 * Install a CHILD_SA for usage
507 static status_t
install_child_sa(private_ike_auth_t
*this, bool initiator
)
509 prf_plus_t
*prf_plus
;
513 seed
= chunk_alloc(this->nonce_i
.len
+ this->nonce_r
.len
);
514 memcpy(seed
.ptr
, this->nonce_i
.ptr
, this->nonce_i
.len
);
515 memcpy(seed
.ptr
+ this->nonce_i
.len
, this->nonce_r
.ptr
, this->nonce_r
.len
);
516 prf_plus
= prf_plus_create(this->ike_sa
->get_child_prf(this->ike_sa
), seed
);
521 status
= this->child_sa
->update(this->child_sa
, this->proposal
, prf_plus
);
525 status
= this->child_sa
->add(this->child_sa
, this->proposal
, prf_plus
);
527 prf_plus
->destroy(prf_plus
);
528 if (status
!= SUCCESS
)
534 status
= this->child_sa
->add_policies(this->child_sa
, this->tsi
, this->tsr
);
538 status
= this->child_sa
->add_policies(this->child_sa
, this->tsr
, this->tsi
);
540 if (status
!= SUCCESS
)
545 /* add to IKE_SA, and remove from transaction */
546 this->child_sa
->set_state(this->child_sa
, CHILD_INSTALLED
);
547 this->ike_sa
->add_child_sa(this->ike_sa
, this->child_sa
);
548 this->child_sa
= NULL
;
553 * Implementation of transaction_t.get_response.
555 static status_t
get_response(private_ike_auth_t
*this, message_t
*request
,
556 message_t
**result
, transaction_t
**next
)
559 identification_t
*my_id
, *other_id
;
560 linked_list_t
*requested_ca_keyids
;
563 iterator_t
*payloads
;
565 id_payload_t
*idi_request
= NULL
;
566 id_payload_t
*idr_request
= NULL
;
567 auth_payload_t
*auth_request
= NULL
;
568 certreq_payload_t
*certreq_request
= NULL
;
569 cert_payload_t
*cert_request
= NULL
;
570 sa_payload_t
*sa_request
= NULL
;
571 ts_payload_t
*tsi_request
= NULL
;
572 ts_payload_t
*tsr_request
= NULL
;
573 id_payload_t
*idr_response
;
575 /* check if we already have built a response (retransmission) */
578 *result
= this->message
;
582 SIG(CHILD_UP_START
, "setting up CHILD_SA along with IKE_AUTH");
584 me
= this->ike_sa
->get_my_host(this->ike_sa
);
585 other
= this->ike_sa
->get_other_host(this->ike_sa
);
586 this->message_id
= request
->get_message_id(request
);
588 /* set up response */
589 response
= message_create();
590 response
->set_source(response
, me
->clone(me
));
591 response
->set_destination(response
, other
->clone(other
));
592 response
->set_exchange_type(response
, IKE_AUTH
);
593 response
->set_request(response
, FALSE
);
594 response
->set_message_id(response
, this->message_id
);
595 response
->set_ike_sa_id(response
, this->ike_sa
->get_id(this->ike_sa
));
596 this->message
= response
;
599 /* check message type */
600 if (request
->get_exchange_type(request
) != IKE_AUTH
)
602 SIG(IKE_UP_FAILED
, "IKE_AUTH response of invalid type, deleting IKE_SA");
603 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
607 /* initialize list of requested ca keyids */
608 requested_ca_keyids
= linked_list_create();
610 /* Iterate over all payloads. */
611 payloads
= request
->get_payload_iterator(request
);
612 while (payloads
->iterate(payloads
, (void**)&payload
))
614 switch (payload
->get_type(payload
))
617 idi_request
= (id_payload_t
*)payload
;
620 idr_request
= (id_payload_t
*)payload
;
623 auth_request
= (auth_payload_t
*)payload
;
625 case CERTIFICATE_REQUEST
:
626 certreq_request
= (certreq_payload_t
*)payload
;
627 add_certificate_request(certreq_request
, requested_ca_keyids
);
630 cert_request
= (cert_payload_t
*)payload
;
632 case SECURITY_ASSOCIATION
:
633 sa_request
= (sa_payload_t
*)payload
;
635 case TRAFFIC_SELECTOR_INITIATOR
:
636 tsi_request
= (ts_payload_t
*)payload
;
638 case TRAFFIC_SELECTOR_RESPONDER
:
639 tsr_request
= (ts_payload_t
*)payload
;
643 status
= process_notifies(this, (notify_payload_t
*)payload
);
644 if (status
== FAILED
)
646 payloads
->destroy(payloads
);
647 requested_ca_keyids
->destroy(requested_ca_keyids
);
648 /* we return SUCCESS, returned FAILED means do next transaction */
651 if (status
== DESTROY_ME
)
653 payloads
->destroy(payloads
);
654 requested_ca_keyids
->destroy(requested_ca_keyids
);
655 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
662 DBG1(DBG_IKE
, "ignoring %N payload",
663 payload_type_names
, payload
->get_type(payload
));
668 payloads
->destroy(payloads
);
670 /* check if we have all payloads */
671 if (!(idi_request
&& auth_request
&& sa_request
&& tsi_request
&& tsr_request
))
673 build_notify(INVALID_SYNTAX
, response
, TRUE
);
674 SIG(IKE_UP_FAILED
, "request message incomplete, deleting IKE_SA");
675 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
676 requested_ca_keyids
->destroy(requested_ca_keyids
);
680 { /* process ID payload */
681 other_id
= idi_request
->get_identification(idi_request
);
684 my_id
= idr_request
->get_identification(idr_request
);
688 my_id
= identification_create_from_encoding(ID_ANY
, CHUNK_INITIALIZER
);
693 { /* get a policy and process traffic selectors */
694 linked_list_t
*my_ts
, *other_ts
;
696 my_ts
= tsr_request
->get_traffic_selectors(tsr_request
);
697 other_ts
= tsi_request
->get_traffic_selectors(tsi_request
);
699 this->policy
= charon
->policies
->get_policy(charon
->policies
,
703 requested_ca_keyids
);
704 requested_ca_keyids
->destroy(requested_ca_keyids
);
708 this->tsr
= this->policy
->select_my_traffic_selectors(this->policy
, my_ts
, me
);
709 this->tsi
= this->policy
->select_other_traffic_selectors(this->policy
, other_ts
, other
);
711 my_ts
->destroy_offset(my_ts
, offsetof(traffic_selector_t
, destroy
));
712 other_ts
->destroy_offset(other_ts
, offsetof(traffic_selector_t
, destroy
));
714 /* TODO: We should check somehow if we have a policy, but with other
715 * traffic selectors. Then we would create a IKE_SA without a CHILD_SA. */
716 if (this->policy
== NULL
)
718 SIG(IKE_UP_FAILED
, "no acceptable policy for IDs %D - %D found, "
719 "deleting IKE_SA", my_id
, other_id
);
720 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
721 my_id
->destroy(my_id
);
722 other_id
->destroy(other_id
);
723 build_notify(AUTHENTICATION_FAILED
, response
, TRUE
);
726 my_id
->destroy(my_id
);
728 /* get my id from policy, which must contain a fully qualified valid id */
729 my_id
= this->policy
->get_my_id(this->policy
);
730 this->ike_sa
->set_my_id(this->ike_sa
, my_id
->clone(my_id
));
731 this->ike_sa
->set_other_id(this->ike_sa
, other_id
);
733 idr_response
= id_payload_create_from_identification(FALSE
, my_id
);
734 response
->add_payload(response
, (payload_t
*)idr_response
);
737 if (this->policy
->get_auth_method(this->policy
) == AUTH_RSA
738 && this->connection
->get_cert_policy(this->connection
) != CERT_NEVER_SEND
)
739 { /* build certificate payload */
741 cert_payload_t
*cert_payload
;
743 cert
= charon
->credentials
->get_certificate(charon
->credentials
, my_id
);
746 cert_payload
= cert_payload_create_from_x509(cert
);
747 response
->add_payload(response
, (payload_t
*)cert_payload
);
751 DBG1(DBG_IKE
, "could not find my certificate, cert payload omitted");
756 { /* process certificate payload */
757 import_certificate(cert_request
);
760 { /* process auth payload */
761 authenticator_t
*authenticator
;
762 auth_payload_t
*auth_response
;
763 auth_method_t auth_method
;
766 auth_method
= auth_request
->get_auth_method(auth_request
);
767 authenticator
= authenticator_create(this->ike_sa
, auth_method
);
768 if (authenticator
== NULL
)
770 SIG(IKE_UP_FAILED
, "auth method %N not supported, deleting IKE_SA",
771 auth_method_names
, auth_method
);
772 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
775 status
= authenticator
->verify(authenticator
, this->init_request
,
776 this->nonce_r
, auth_request
);
777 authenticator
->destroy(authenticator
);
778 if (status
!= SUCCESS
)
780 SIG(IKE_UP_FAILED
, "authentication failed, deleting IKE_SA");
781 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
782 build_notify(AUTHENTICATION_FAILED
, response
, TRUE
);
786 auth_method
= this->policy
->get_auth_method(this->policy
);
787 authenticator
= authenticator_create(this->ike_sa
, auth_method
);
788 if (authenticator
== NULL
)
790 SIG(IKE_UP_FAILED
, "auth method %N not supported, deleting IKE_SA",
791 auth_method_names
, auth_method
);
792 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
795 status
= authenticator
->build(authenticator
, this->init_response
,
796 this->nonce_i
, &auth_response
);
797 authenticator
->destroy(authenticator
);
798 if (status
!= SUCCESS
)
800 SIG(IKE_UP_FAILED
, "authentication data generation failed, deleting IKE_SA");
801 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
802 build_notify(AUTHENTICATION_FAILED
, response
, TRUE
);
805 response
->add_payload(response
, (payload_t
*)auth_response
);
808 SIG(IKE_UP_SUCCESS
, "IKE_SA '%s' established between %H[%D]...%H[%D]",
809 this->ike_sa
->get_name(this->ike_sa
), me
, my_id
, other
, other_id
);
811 { /* process SA payload */
812 linked_list_t
*proposal_list
;
813 sa_payload_t
*sa_response
;
814 ts_payload_t
*ts_response
;
816 u_int32_t soft_lifetime
, hard_lifetime
;
819 sa_response
= sa_payload_create();
821 /* get proposals from request, and select one with ours */
822 proposal_list
= sa_request
->get_proposals(sa_request
);
823 DBG2(DBG_IKE
, "selecting proposals:");
824 this->proposal
= this->policy
->select_proposal(this->policy
, proposal_list
);
825 proposal_list
->destroy_offset(proposal_list
, offsetof(proposal_t
, destroy
));
827 /* do we have a proposal? */
828 if (this->proposal
== NULL
)
830 SIG(CHILD_UP_FAILED
, "CHILD_SA proposals unacceptable, no CHILD_SA created");
831 DBG1(DBG_IKE
, "adding NO_PROPOSAL_CHOSEN notify to response");
832 build_notify(NO_PROPOSAL_CHOSEN
, response
, FALSE
);
834 /* do we have traffic selectors? */
835 else if (this->tsi
->get_count(this->tsi
) == 0 || this->tsr
->get_count(this->tsr
) == 0)
837 SIG(CHILD_UP_FAILED
, "CHILD_SA traffic selectors unacceptable, no CHILD_SA created");
838 DBG1(DBG_IKE
, "adding TS_UNACCEPTABLE notify to response");
839 build_notify(TS_UNACCEPTABLE
, response
, FALSE
);
843 /* create child sa */
844 soft_lifetime
= this->policy
->get_soft_lifetime(this->policy
);
845 hard_lifetime
= this->policy
->get_hard_lifetime(this->policy
);
846 use_natt
= this->ike_sa
->is_natt_enabled(this->ike_sa
);
847 this->child_sa
= child_sa_create(this->reqid
, me
, other
, my_id
, other_id
,
848 soft_lifetime
, hard_lifetime
,
849 this->policy
->get_updown(this->policy
),
850 this->policy
->get_hostaccess(this->policy
),
852 this->child_sa
->set_name(this->child_sa
, this->policy
->get_name(this->policy
));
853 if (install_child_sa(this, FALSE
) != SUCCESS
)
855 SIG(CHILD_UP_FAILED
, "installing CHILD_SA failed, no CHILD_SA created");
856 DBG1(DBG_IKE
, "adding NO_PROPOSAL_CHOSEN notify to response");
857 build_notify(NO_PROPOSAL_CHOSEN
, response
, FALSE
);
861 /* add proposal to sa payload */
862 sa_response
->add_proposal(sa_response
, this->proposal
);
863 SIG(CHILD_UP_SUCCESS
, "CHILD_SA created");
866 response
->add_payload(response
, (payload_t
*)sa_response
);
868 /* add ts payload after sa payload */
869 ts_response
= ts_payload_create_from_traffic_selectors(TRUE
, this->tsi
);
870 response
->add_payload(response
, (payload_t
*)ts_response
);
871 ts_response
= ts_payload_create_from_traffic_selectors(FALSE
, this->tsr
);
872 response
->add_payload(response
, (payload_t
*)ts_response
);
874 /* set established state */
875 this->ike_sa
->set_state(this->ike_sa
, IKE_ESTABLISHED
);
881 * Implementation of transaction_t.conclude
883 static status_t
conclude(private_ike_auth_t
*this, message_t
*response
,
884 transaction_t
**transaction
)
886 iterator_t
*payloads
;
889 identification_t
*other_id
, *my_id
;
890 ts_payload_t
*tsi_payload
= NULL
;
891 ts_payload_t
*tsr_payload
= NULL
;
892 id_payload_t
*idr_payload
= NULL
;
893 cert_payload_t
*cert_payload
= NULL
;
894 auth_payload_t
*auth_payload
= NULL
;
895 sa_payload_t
*sa_payload
= NULL
;
898 /* check message type */
899 if (response
->get_exchange_type(response
) != IKE_AUTH
)
901 SIG(IKE_UP_FAILED
, "IKE_AUTH response of invalid type, deleting IKE_SA");
902 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
906 me
= this->ike_sa
->get_my_host(this->ike_sa
);
907 other
= this->ike_sa
->get_other_host(this->ike_sa
);
909 /* Iterate over all payloads to collect them */
910 payloads
= response
->get_payload_iterator(response
);
911 while (payloads
->iterate(payloads
, (void**)&payload
))
913 switch (payload
->get_type(payload
))
916 idr_payload
= (id_payload_t
*)payload
;
919 auth_payload
= (auth_payload_t
*)payload
;
922 cert_payload
= (cert_payload_t
*)payload
;
924 case SECURITY_ASSOCIATION
:
925 sa_payload
= (sa_payload_t
*)payload
;
927 case TRAFFIC_SELECTOR_INITIATOR
:
928 tsi_payload
= (ts_payload_t
*)payload
;
930 case TRAFFIC_SELECTOR_RESPONDER
:
931 tsr_payload
= (ts_payload_t
*)payload
;
935 status
= process_notifies(this, (notify_payload_t
*)payload
);
936 if (status
== FAILED
)
938 payloads
->destroy(payloads
);
939 /* we return SUCCESS, as transaction completet */
942 if (status
== DESTROY_ME
)
944 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
945 payloads
->destroy(payloads
);
952 DBG1(DBG_IKE
, "ignoring payload %N",
953 payload_type_names
, payload
->get_type(payload
));
958 payloads
->destroy(payloads
);
960 if (!(idr_payload
&& auth_payload
&& sa_payload
&& tsi_payload
&& tsr_payload
))
962 SIG(IKE_UP_FAILED
, "response message incomplete, deleting IKE_SA");
963 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
967 { /* process idr payload */
968 identification_t
*configured_other_id
;
971 other_id
= idr_payload
->get_identification(idr_payload
);
972 configured_other_id
= this->policy
->get_other_id(this->policy
);
974 if (!other_id
->matches(other_id
, configured_other_id
, &wildcards
))
976 other_id
->destroy(other_id
);
977 SIG(IKE_UP_FAILED
, "other peer uses unacceptable ID (%D, excepted "
978 "%D), deleting IKE_SA", other_id
, configured_other_id
);
979 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
982 /* update other ID. It was already set, but may contain wildcards */
983 this->ike_sa
->set_other_id(this->ike_sa
, other_id
);
987 { /* process cert payload */
988 import_certificate(cert_payload
);
991 { /* authenticate peer */
992 authenticator_t
*authenticator
;
993 auth_method_t auth_method
;
996 my_id
= this->policy
->get_my_id(this->policy
);
997 auth_method
= auth_payload
->get_auth_method(auth_payload
);
998 authenticator
= authenticator_create(this->ike_sa
, auth_method
);
999 if (authenticator
== NULL
)
1001 SIG(IKE_UP_FAILED
, "auth method %N not supported, deleting IKE_SA",
1002 auth_method_names
, auth_method
);
1003 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
1006 status
= authenticator
->verify(authenticator
, this->init_response
,
1007 this->nonce_i
, auth_payload
);
1008 authenticator
->destroy(authenticator
);
1009 if (status
!= SUCCESS
)
1011 SIG(IKE_UP_FAILED
, "authentication of '%D' with %N failed, "
1012 "deleting IKE_SA", other_id
, auth_method_names
, auth_method
);
1013 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
1018 SIG(IKE_UP_SUCCESS
, "IKE_SA '%s' established between %H[%D]...%H[%D]",
1019 this->ike_sa
->get_name(this->ike_sa
), me
, my_id
, other
, other_id
);
1021 { /* process traffic selectors for us */
1022 linked_list_t
*ts_received
= tsi_payload
->get_traffic_selectors(tsi_payload
);
1023 this->tsi
= this->policy
->select_my_traffic_selectors(this->policy
, ts_received
, me
);
1024 ts_received
->destroy_offset(ts_received
, offsetof(traffic_selector_t
, destroy
));
1027 { /* process traffic selectors for other */
1028 linked_list_t
*ts_received
= tsr_payload
->get_traffic_selectors(tsr_payload
);
1029 this->tsr
= this->policy
->select_other_traffic_selectors(this->policy
, ts_received
, other
);
1030 ts_received
->destroy_offset(ts_received
, offsetof(traffic_selector_t
, destroy
));
1033 { /* process sa payload */
1034 linked_list_t
*proposal_list
;
1036 proposal_list
= sa_payload
->get_proposals(sa_payload
);
1037 /* we have to re-check here if other's selection is valid */
1038 this->proposal
= this->policy
->select_proposal(this->policy
, proposal_list
);
1039 proposal_list
->destroy_offset(proposal_list
, offsetof(proposal_t
, destroy
));
1041 /* everything fine to create CHILD? */
1042 if (this->proposal
== NULL
||
1043 this->tsi
->get_count(this->tsi
) == 0 ||
1044 this->tsr
->get_count(this->tsr
) == 0 ||
1047 SIG(CHILD_UP_FAILED
, "CHILD_SA negotiation failed, no CHILD_SA built");
1051 if (install_child_sa(this, TRUE
) != SUCCESS
)
1053 SIG(CHILD_UP_FAILED
, "installing CHILD_SA failed, no CHILD_SA built");
1054 /* TODO: we should send a DELETE for that CHILD to stay
1055 * synchronous with the peer */
1059 SIG(CHILD_UP_SUCCESS
, "CHILD_SA created");
1064 this->ike_sa
->set_state(this->ike_sa
, IKE_ESTABLISHED
);
1069 * implements transaction_t.destroy
1071 static void destroy(private_ike_auth_t
*this)
1073 DESTROY_IF(this->message
);
1074 DESTROY_IF(this->proposal
);
1075 DESTROY_IF(this->child_sa
);
1076 DESTROY_IF(this->policy
);
1077 DESTROY_IF(this->connection
);
1080 this->tsi
->destroy_offset(this->tsi
, offsetof(traffic_selector_t
, destroy
));
1084 this->tsr
->destroy_offset(this->tsr
, offsetof(traffic_selector_t
, destroy
));
1086 chunk_free(&this->nonce_i
);
1087 chunk_free(&this->nonce_r
);
1088 chunk_free(&this->init_request
);
1089 chunk_free(&this->init_response
);
1094 * Described in header.
1096 ike_auth_t
*ike_auth_create(ike_sa_t
*ike_sa
)
1098 private_ike_auth_t
*this = malloc_thing(private_ike_auth_t
);
1100 /* transaction interface functions */
1101 this->public.transaction
.get_request
= (status_t(*)(transaction_t
*,message_t
**))get_request
;
1102 this->public.transaction
.get_response
= (status_t(*)(transaction_t
*,message_t
*,message_t
**,transaction_t
**))get_response
;
1103 this->public.transaction
.conclude
= (status_t(*)(transaction_t
*,message_t
*,transaction_t
**))conclude
;
1104 this->public.transaction
.get_message_id
= (u_int32_t(*)(transaction_t
*))get_message_id
;
1105 this->public.transaction
.requested
= (u_int32_t(*)(transaction_t
*))requested
;
1106 this->public.transaction
.destroy
= (void(*)(transaction_t
*))destroy
;
1108 /* public functions */
1109 this->public.set_config
= (void(*)(ike_auth_t
*,connection_t
*,policy_t
*))set_config
;
1110 this->public.set_reqid
= (void(*)(ike_auth_t
*,u_int32_t
))set_reqid
;
1111 this->public.set_nonces
= (void(*)(ike_auth_t
*,chunk_t
,chunk_t
))set_nonces
;
1112 this->public.set_init_messages
= (void(*)(ike_auth_t
*,chunk_t
,chunk_t
))set_init_messages
;
1115 this->ike_sa
= ike_sa
;
1116 this->message_id
= 0;
1117 this->message
= NULL
;
1118 this->requested
= 0;
1119 this->nonce_i
= CHUNK_INITIALIZER
;
1120 this->nonce_r
= CHUNK_INITIALIZER
;
1121 this->init_request
= CHUNK_INITIALIZER
;
1122 this->init_response
= CHUNK_INITIALIZER
;
1123 this->child_sa
= NULL
;
1124 this->proposal
= NULL
;
1127 this->build_child
= TRUE
;
1130 return &this->public;