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/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
) == RSA_DIGITAL_SIGNATURE
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 status
= authenticator
->compute_auth_data(authenticator
,
283 authenticator
->destroy(authenticator
);
284 if (status
!= SUCCESS
)
286 SIG(IKE_UP_FAILED
, "could not generate AUTH data, deleting IKE_SA");
287 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
290 request
->add_payload(request
, (payload_t
*)auth_payload
);
293 { /* build SA payload for CHILD_SA */
294 linked_list_t
*proposal_list
;
295 sa_payload_t
*sa_payload
;
296 u_int32_t soft_lifetime
, hard_lifetime
;
299 proposal_list
= this->policy
->get_proposals(this->policy
);
300 soft_lifetime
= this->policy
->get_soft_lifetime(this->policy
);
301 hard_lifetime
= this->policy
->get_hard_lifetime(this->policy
);
302 enable_natt
= this->ike_sa
->is_natt_enabled(this->ike_sa
);
303 this->child_sa
= child_sa_create(this->reqid
, me
, other
, my_id
, other_id
,
304 soft_lifetime
, hard_lifetime
,
305 this->policy
->get_updown(this->policy
),
306 this->policy
->get_hostaccess(this->policy
),
308 this->child_sa
->set_name(this->child_sa
, this->policy
->get_name(this->policy
));
309 if (this->child_sa
->alloc(this->child_sa
, proposal_list
) != SUCCESS
)
311 SIG(IKE_UP_FAILED
, "could not install CHILD_SA, deleting IKE_SA");
312 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
315 sa_payload
= sa_payload_create_from_proposal_list(proposal_list
);
316 proposal_list
->destroy_offset(proposal_list
, offsetof(proposal_t
, destroy
));
317 request
->add_payload(request
, (payload_t
*)sa_payload
);
320 { /* build TSi payload */
321 linked_list_t
*ts_list
;
322 ts_payload_t
*ts_payload
;
324 ts_list
= this->policy
->get_my_traffic_selectors(this->policy
, me
);
325 ts_payload
= ts_payload_create_from_traffic_selectors(TRUE
, ts_list
);
326 ts_list
->destroy_offset(ts_list
, offsetof(traffic_selector_t
, destroy
));
328 request
->add_payload(request
, (payload_t
*)ts_payload
);
331 { /* build TSr payload */
332 linked_list_t
*ts_list
;
333 ts_payload_t
*ts_payload
;
335 ts_list
= this->policy
->get_other_traffic_selectors(this->policy
, other
);
336 ts_payload
= ts_payload_create_from_traffic_selectors(FALSE
, ts_list
);
337 ts_list
->destroy_offset(ts_list
, offsetof(traffic_selector_t
, destroy
));
339 request
->add_payload(request
, (payload_t
*)ts_payload
);
342 this->message_id
= this->ike_sa
->get_next_message_id(this->ike_sa
);
343 request
->set_message_id(request
, this->message_id
);
348 * Handle all kind of notifies
350 static status_t
process_notifies(private_ike_auth_t
*this, notify_payload_t
*notify_payload
)
352 notify_type_t notify_type
= notify_payload
->get_notify_type(notify_payload
);
354 DBG2(DBG_IKE
, "process notify type %N", notify_type_names
, notify_type
);
358 /* these notifies are not critical. no child_sa is built, but IKE stays alive */
359 case SINGLE_PAIR_REQUIRED
:
361 SIG(CHILD_UP_FAILED
, "received a SINGLE_PAIR_REQUIRED notify");
362 this->build_child
= FALSE
;
365 case TS_UNACCEPTABLE
:
367 SIG(CHILD_UP_FAILED
, "received TS_UNACCEPTABLE notify");
368 this->build_child
= FALSE
;
371 case NO_PROPOSAL_CHOSEN
:
373 SIG(CHILD_UP_FAILED
, "received NO_PROPOSAL_CHOSEN notify");
374 this->build_child
= FALSE
;
379 if (notify_type
< 16383)
381 SIG(IKE_UP_FAILED
, "received %N notify error, deleting IKE_SA",
382 notify_type_names
, notify_type
);
387 DBG1(DBG_IKE
, "received %N notify, ignored",
388 notify_type_names
, notify_type
);
396 * Build a notify message.
398 static void build_notify(notify_type_t type
, message_t
*message
, bool flush_message
)
400 notify_payload_t
*notify
;
405 iterator_t
*iterator
= message
->get_payload_iterator(message
);
406 while (iterator
->iterate(iterator
, (void**)&payload
))
408 payload
->destroy(payload
);
409 iterator
->remove(iterator
);
411 iterator
->destroy(iterator
);
414 notify
= notify_payload_create();
415 notify
->set_notify_type(notify
, type
);
416 message
->add_payload(message
, (payload_t
*)notify
);
420 * Import certificate requests from a certreq payload
422 static void import_certificate_request(certreq_payload_t
*certreq_payload
)
425 cert_encoding_t encoding
= certreq_payload
->get_cert_encoding(certreq_payload
);
427 if (encoding
!= CERT_X509_SIGNATURE
)
429 DBG1(DBG_IKE
, "certreq payload %N not supported, ignored",
430 cert_encoding_names
, encoding
);
434 keyids
= certreq_payload
->get_data(certreq_payload
);
436 while (keyids
.len
>= HASH_SIZE_SHA1
)
438 chunk_t keyid
= { keyids
.ptr
, HASH_SIZE_SHA1
};
439 x509_t
*cacert
= charon
->credentials
->get_ca_certificate_by_keyid(charon
->credentials
, keyid
);
443 DBG1(DBG_IKE
, "request for certificate issued by ca '%D'", cacert
->get_subject(cacert
));
444 DBG2(DBG_IKE
, " with keyid %#B", &keyid
);
448 DBG1(DBG_IKE
, "request for certificate issued by unknown ca");
449 DBG1(DBG_IKE
, " with keyid %#B", &keyid
);
451 keyids
.ptr
+= HASH_SIZE_SHA1
;
452 keyids
.len
-= HASH_SIZE_SHA1
;
457 * Import a certificate from a cert payload
459 static void import_certificate(cert_payload_t
*cert_payload
)
463 cert_encoding_t encoding
;
465 encoding
= cert_payload
->get_cert_encoding(cert_payload
);
466 if (encoding
!= CERT_X509_SIGNATURE
)
468 DBG1(DBG_IKE
, "certificate payload %N not supported, ignored",
469 cert_encoding_names
, encoding
);
472 cert
= x509_create_from_chunk(cert_payload
->get_data_clone(cert_payload
));
475 if (charon
->credentials
->verify(charon
->credentials
, cert
, &found
))
477 DBG2(DBG_IKE
, "received end entity certificate is trusted, added to store");
480 charon
->credentials
->add_end_certificate(charon
->credentials
, cert
);
489 DBG1(DBG_IKE
, "received end entity certificate is not trusted, discarded");
495 DBG1(DBG_IKE
, "parsing of received certificate failed, discarded");
500 * Install a CHILD_SA for usage
502 static status_t
install_child_sa(private_ike_auth_t
*this, bool initiator
)
504 prf_plus_t
*prf_plus
;
508 seed
= chunk_alloc(this->nonce_i
.len
+ this->nonce_r
.len
);
509 memcpy(seed
.ptr
, this->nonce_i
.ptr
, this->nonce_i
.len
);
510 memcpy(seed
.ptr
+ this->nonce_i
.len
, this->nonce_r
.ptr
, this->nonce_r
.len
);
511 prf_plus
= prf_plus_create(this->ike_sa
->get_child_prf(this->ike_sa
), seed
);
516 status
= this->child_sa
->update(this->child_sa
, this->proposal
, prf_plus
);
520 status
= this->child_sa
->add(this->child_sa
, this->proposal
, prf_plus
);
522 prf_plus
->destroy(prf_plus
);
523 if (status
!= SUCCESS
)
529 status
= this->child_sa
->add_policies(this->child_sa
, this->tsi
, this->tsr
);
533 status
= this->child_sa
->add_policies(this->child_sa
, this->tsr
, this->tsi
);
535 if (status
!= SUCCESS
)
540 /* add to IKE_SA, and remove from transaction */
541 this->child_sa
->set_state(this->child_sa
, CHILD_INSTALLED
);
542 this->ike_sa
->add_child_sa(this->ike_sa
, this->child_sa
);
543 this->child_sa
= NULL
;
548 * Implementation of transaction_t.get_response.
550 static status_t
get_response(private_ike_auth_t
*this, message_t
*request
,
551 message_t
**result
, transaction_t
**next
)
554 identification_t
*my_id
, *other_id
;
557 iterator_t
*payloads
;
559 id_payload_t
*idi_request
= NULL
;
560 id_payload_t
*idr_request
= NULL
;
561 auth_payload_t
*auth_request
= NULL
;
562 certreq_payload_t
*certreq_request
= NULL
;
563 cert_payload_t
*cert_request
= NULL
;
564 sa_payload_t
*sa_request
= NULL
;
565 ts_payload_t
*tsi_request
= NULL
;
566 ts_payload_t
*tsr_request
= NULL
;
567 id_payload_t
*idr_response
;
569 /* check if we already have built a response (retransmission) */
572 *result
= this->message
;
576 SIG(CHILD_UP_START
, "setting up CHILD_SA along with IKE_AUTH");
578 me
= this->ike_sa
->get_my_host(this->ike_sa
);
579 other
= this->ike_sa
->get_other_host(this->ike_sa
);
580 this->message_id
= request
->get_message_id(request
);
582 /* set up response */
583 response
= message_create();
584 response
->set_source(response
, me
->clone(me
));
585 response
->set_destination(response
, other
->clone(other
));
586 response
->set_exchange_type(response
, IKE_AUTH
);
587 response
->set_request(response
, FALSE
);
588 response
->set_message_id(response
, this->message_id
);
589 response
->set_ike_sa_id(response
, this->ike_sa
->get_id(this->ike_sa
));
590 this->message
= response
;
593 /* check message type */
594 if (request
->get_exchange_type(request
) != IKE_AUTH
)
596 SIG(IKE_UP_FAILED
, "IKE_AUTH response of invalid type, deleting IKE_SA");
597 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
601 /* Iterate over all payloads. */
602 payloads
= request
->get_payload_iterator(request
);
603 while (payloads
->iterate(payloads
, (void**)&payload
))
605 switch (payload
->get_type(payload
))
608 idi_request
= (id_payload_t
*)payload
;
611 idr_request
= (id_payload_t
*)payload
;
614 auth_request
= (auth_payload_t
*)payload
;
616 case CERTIFICATE_REQUEST
:
617 certreq_request
= (certreq_payload_t
*)payload
;
620 cert_request
= (cert_payload_t
*)payload
;
622 case SECURITY_ASSOCIATION
:
623 sa_request
= (sa_payload_t
*)payload
;
625 case TRAFFIC_SELECTOR_INITIATOR
:
626 tsi_request
= (ts_payload_t
*)payload
;
628 case TRAFFIC_SELECTOR_RESPONDER
:
629 tsr_request
= (ts_payload_t
*)payload
;
633 status
= process_notifies(this, (notify_payload_t
*)payload
);
634 if (status
== FAILED
)
636 payloads
->destroy(payloads
);
637 /* we return SUCCESS, returned FAILED means do next transaction */
640 if (status
== DESTROY_ME
)
642 payloads
->destroy(payloads
);
643 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
650 DBG1(DBG_IKE
, "ignoring %N payload",
651 payload_type_names
, payload
->get_type(payload
));
656 payloads
->destroy(payloads
);
658 /* check if we have all payloads */
659 if (!(idi_request
&& auth_request
&& sa_request
&& tsi_request
&& tsr_request
))
661 build_notify(INVALID_SYNTAX
, response
, TRUE
);
662 SIG(IKE_UP_FAILED
, "request message incomplete, deleting IKE_SA");
663 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
667 { /* process ID payload */
668 other_id
= idi_request
->get_identification(idi_request
);
671 my_id
= idr_request
->get_identification(idr_request
);
675 my_id
= identification_create_from_encoding(ID_ANY
, CHUNK_INITIALIZER
);
679 { /* get a policy and process traffic selectors */
680 linked_list_t
*my_ts
, *other_ts
;
682 my_ts
= tsr_request
->get_traffic_selectors(tsr_request
);
683 other_ts
= tsi_request
->get_traffic_selectors(tsi_request
);
685 this->policy
= charon
->policies
->get_policy(charon
->policies
,
691 this->tsr
= this->policy
->select_my_traffic_selectors(this->policy
, my_ts
, me
);
692 this->tsi
= this->policy
->select_other_traffic_selectors(this->policy
, other_ts
, other
);
694 my_ts
->destroy_offset(my_ts
, offsetof(traffic_selector_t
, destroy
));
695 other_ts
->destroy_offset(other_ts
, offsetof(traffic_selector_t
, destroy
));
697 /* TODO: We should check somehow if we have a policy, but with other
698 * traffic selectors. Then we would create a IKE_SA without a CHILD_SA. */
699 if (this->policy
== NULL
)
701 SIG(IKE_UP_FAILED
, "no acceptable policy for IDs %D - %D found, "
702 "deleting IKE_SA", my_id
, other_id
);
703 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
704 my_id
->destroy(my_id
);
705 other_id
->destroy(other_id
);
706 build_notify(AUTHENTICATION_FAILED
, response
, TRUE
);
709 my_id
->destroy(my_id
);
711 /* get my id from policy, which must contain a fully qualified valid id */
712 my_id
= this->policy
->get_my_id(this->policy
);
713 this->ike_sa
->set_my_id(this->ike_sa
, my_id
->clone(my_id
));
714 this->ike_sa
->set_other_id(this->ike_sa
, other_id
);
716 idr_response
= id_payload_create_from_identification(FALSE
, my_id
);
717 response
->add_payload(response
, (payload_t
*)idr_response
);
720 if (this->policy
->get_auth_method(this->policy
) == RSA_DIGITAL_SIGNATURE
721 && this->connection
->get_cert_policy(this->connection
) != CERT_NEVER_SEND
)
722 { /* build certificate payload */
724 cert_payload_t
*cert_payload
;
726 cert
= charon
->credentials
->get_certificate(charon
->credentials
, my_id
);
729 cert_payload
= cert_payload_create_from_x509(cert
);
730 response
->add_payload(response
, (payload_t
*)cert_payload
);
734 DBG1(DBG_IKE
, "could not find my certificate, cert payload omitted");
739 { /* process certificate request payload */
740 import_certificate_request(certreq_request
);
744 { /* process certificate payload */
745 import_certificate(cert_request
);
748 { /* process auth payload */
749 authenticator_t
*authenticator
;
750 auth_payload_t
*auth_response
;
751 auth_method_t auth_method
;
754 auth_method
= this->policy
->get_auth_method(this->policy
);
755 authenticator
= authenticator_create(this->ike_sa
, auth_method
);
756 status
= authenticator
->verify_auth_data(authenticator
, auth_request
,
762 if (status
!= SUCCESS
)
764 SIG(IKE_UP_FAILED
, "authentication failed, deleting IKE_SA");
765 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
766 build_notify(AUTHENTICATION_FAILED
, response
, TRUE
);
767 authenticator
->destroy(authenticator
);
770 status
= authenticator
->compute_auth_data(authenticator
, &auth_response
,
776 authenticator
->destroy(authenticator
);
777 if (status
!= SUCCESS
)
779 SIG(IKE_UP_FAILED
, "authentication data generation failed, deleting IKE_SA");
780 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
781 build_notify(AUTHENTICATION_FAILED
, response
, TRUE
);
784 response
->add_payload(response
, (payload_t
*)auth_response
);
787 SIG(IKE_UP_SUCCESS
, "IKE_SA '%s' established between %H[%D]...%H[%D]",
788 this->ike_sa
->get_name(this->ike_sa
), me
, my_id
, other
, other_id
);
790 { /* process SA payload */
791 linked_list_t
*proposal_list
;
792 sa_payload_t
*sa_response
;
793 ts_payload_t
*ts_response
;
795 u_int32_t soft_lifetime
, hard_lifetime
;
798 sa_response
= sa_payload_create();
800 /* get proposals from request, and select one with ours */
801 proposal_list
= sa_request
->get_proposals(sa_request
);
802 DBG2(DBG_IKE
, "selecting proposals:");
803 this->proposal
= this->policy
->select_proposal(this->policy
, proposal_list
);
804 proposal_list
->destroy_offset(proposal_list
, offsetof(proposal_t
, destroy
));
806 /* do we have a proposal? */
807 if (this->proposal
== NULL
)
809 SIG(CHILD_UP_FAILED
, "CHILD_SA proposals unacceptable, no CHILD_SA created");
810 DBG1(DBG_IKE
, "adding NO_PROPOSAL_CHOSEN notify to response");
811 build_notify(NO_PROPOSAL_CHOSEN
, response
, FALSE
);
813 /* do we have traffic selectors? */
814 else if (this->tsi
->get_count(this->tsi
) == 0 || this->tsr
->get_count(this->tsr
) == 0)
816 SIG(CHILD_UP_FAILED
, "CHILD_SA traffic selectors unacceptable, no CHILD_SA created");
817 DBG1(DBG_IKE
, "adding TS_UNACCEPTABLE notify to response");
818 build_notify(TS_UNACCEPTABLE
, response
, FALSE
);
822 /* create child sa */
823 soft_lifetime
= this->policy
->get_soft_lifetime(this->policy
);
824 hard_lifetime
= this->policy
->get_hard_lifetime(this->policy
);
825 use_natt
= this->ike_sa
->is_natt_enabled(this->ike_sa
);
826 this->child_sa
= child_sa_create(this->reqid
, me
, other
, my_id
, other_id
,
827 soft_lifetime
, hard_lifetime
,
828 this->policy
->get_updown(this->policy
),
829 this->policy
->get_hostaccess(this->policy
),
831 this->child_sa
->set_name(this->child_sa
, this->policy
->get_name(this->policy
));
832 if (install_child_sa(this, FALSE
) != SUCCESS
)
834 SIG(CHILD_UP_FAILED
, "installing CHILD_SA failed, no CHILD_SA created");
835 DBG1(DBG_IKE
, "adding NO_PROPOSAL_CHOSEN notify to response");
836 build_notify(NO_PROPOSAL_CHOSEN
, response
, FALSE
);
840 /* add proposal to sa payload */
841 sa_response
->add_proposal(sa_response
, this->proposal
);
842 SIG(CHILD_UP_SUCCESS
, "CHILD_SA created");
845 response
->add_payload(response
, (payload_t
*)sa_response
);
847 /* add ts payload after sa payload */
848 ts_response
= ts_payload_create_from_traffic_selectors(TRUE
, this->tsi
);
849 response
->add_payload(response
, (payload_t
*)ts_response
);
850 ts_response
= ts_payload_create_from_traffic_selectors(FALSE
, this->tsr
);
851 response
->add_payload(response
, (payload_t
*)ts_response
);
853 /* set established state */
854 this->ike_sa
->set_state(this->ike_sa
, IKE_ESTABLISHED
);
860 * Implementation of transaction_t.conclude
862 static status_t
conclude(private_ike_auth_t
*this, message_t
*response
,
863 transaction_t
**transaction
)
865 iterator_t
*payloads
;
868 identification_t
*other_id
, *my_id
;
869 ts_payload_t
*tsi_payload
= NULL
;
870 ts_payload_t
*tsr_payload
= NULL
;
871 id_payload_t
*idr_payload
= NULL
;
872 cert_payload_t
*cert_payload
= NULL
;
873 auth_payload_t
*auth_payload
= NULL
;
874 sa_payload_t
*sa_payload
= NULL
;
877 /* check message type */
878 if (response
->get_exchange_type(response
) != IKE_AUTH
)
880 SIG(IKE_UP_FAILED
, "IKE_AUTH response of invalid type, deleting IKE_SA");
881 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
885 me
= this->ike_sa
->get_my_host(this->ike_sa
);
886 other
= this->ike_sa
->get_other_host(this->ike_sa
);
888 /* Iterate over all payloads to collect them */
889 payloads
= response
->get_payload_iterator(response
);
890 while (payloads
->iterate(payloads
, (void**)&payload
))
892 switch (payload
->get_type(payload
))
895 idr_payload
= (id_payload_t
*)payload
;
898 auth_payload
= (auth_payload_t
*)payload
;
901 cert_payload
= (cert_payload_t
*)payload
;
903 case SECURITY_ASSOCIATION
:
904 sa_payload
= (sa_payload_t
*)payload
;
906 case TRAFFIC_SELECTOR_INITIATOR
:
907 tsi_payload
= (ts_payload_t
*)payload
;
909 case TRAFFIC_SELECTOR_RESPONDER
:
910 tsr_payload
= (ts_payload_t
*)payload
;
914 status
= process_notifies(this, (notify_payload_t
*)payload
);
915 if (status
== FAILED
)
917 payloads
->destroy(payloads
);
918 /* we return SUCCESS, as transaction completet */
921 if (status
== DESTROY_ME
)
923 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
924 payloads
->destroy(payloads
);
931 DBG1(DBG_IKE
, "ignoring payload %N",
932 payload_type_names
, payload
->get_type(payload
));
937 payloads
->destroy(payloads
);
939 if (!(idr_payload
&& auth_payload
&& sa_payload
&& tsi_payload
&& tsr_payload
))
941 SIG(IKE_UP_FAILED
, "response message incomplete, deleting IKE_SA");
942 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
946 { /* process idr payload */
947 identification_t
*configured_other_id
;
950 other_id
= idr_payload
->get_identification(idr_payload
);
951 configured_other_id
= this->policy
->get_other_id(this->policy
);
953 if (!other_id
->matches(other_id
, configured_other_id
, &wildcards
))
955 other_id
->destroy(other_id
);
956 SIG(IKE_UP_FAILED
, "other peer uses unacceptable ID (%D, excepted "
957 "%D), deleting IKE_SA", other_id
, configured_other_id
);
958 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
961 /* update other ID. It was already set, but may contain wildcards */
962 this->ike_sa
->set_other_id(this->ike_sa
, other_id
);
966 { /* process cert payload */
967 import_certificate(cert_payload
);
970 { /* authenticate peer */
971 authenticator_t
*authenticator
;
972 auth_method_t auth_method
;
975 auth_method
= this->policy
->get_auth_method(this->policy
);
976 authenticator
= authenticator_create(this->ike_sa
, auth_method
);
977 my_id
= this->policy
->get_my_id(this->policy
);
979 status
= authenticator
->verify_auth_data(authenticator
,
986 authenticator
->destroy(authenticator
);
987 if (status
!= SUCCESS
)
989 SIG(IKE_UP_FAILED
, "authentication of '%D' with %N failed, "
990 "deleting IKE_SA", other_id
, auth_method_names
, auth_method
);
991 SIG(CHILD_UP_FAILED
, "initiating CHILD_SA failed, unable to create IKE_SA");
996 SIG(IKE_UP_SUCCESS
, "IKE_SA '%s' established between %H[%D]...%H[%D]",
997 this->ike_sa
->get_name(this->ike_sa
), me
, my_id
, other
, other_id
);
999 { /* process traffic selectors for us */
1000 linked_list_t
*ts_received
= tsi_payload
->get_traffic_selectors(tsi_payload
);
1001 this->tsi
= this->policy
->select_my_traffic_selectors(this->policy
, ts_received
, me
);
1002 ts_received
->destroy_offset(ts_received
, offsetof(traffic_selector_t
, destroy
));
1005 { /* process traffic selectors for other */
1006 linked_list_t
*ts_received
= tsr_payload
->get_traffic_selectors(tsr_payload
);
1007 this->tsr
= this->policy
->select_other_traffic_selectors(this->policy
, ts_received
, other
);
1008 ts_received
->destroy_offset(ts_received
, offsetof(traffic_selector_t
, destroy
));
1011 { /* process sa payload */
1012 linked_list_t
*proposal_list
;
1014 proposal_list
= sa_payload
->get_proposals(sa_payload
);
1015 /* we have to re-check here if other's selection is valid */
1016 this->proposal
= this->policy
->select_proposal(this->policy
, proposal_list
);
1017 proposal_list
->destroy_offset(proposal_list
, offsetof(proposal_t
, destroy
));
1019 /* everything fine to create CHILD? */
1020 if (this->proposal
== NULL
||
1021 this->tsi
->get_count(this->tsi
) == 0 ||
1022 this->tsr
->get_count(this->tsr
) == 0 ||
1025 SIG(CHILD_UP_FAILED
, "CHILD_SA negotiation failed, no CHILD_SA built");
1029 if (install_child_sa(this, TRUE
) != SUCCESS
)
1031 SIG(CHILD_UP_FAILED
, "installing CHILD_SA failed, no CHILD_SA built");
1032 /* TODO: we should send a DELETE for that CHILD to stay
1033 * synchronous with the peer */
1037 SIG(CHILD_UP_SUCCESS
, "CHILD_SA created");
1042 this->ike_sa
->set_state(this->ike_sa
, IKE_ESTABLISHED
);
1047 * implements transaction_t.destroy
1049 static void destroy(private_ike_auth_t
*this)
1051 DESTROY_IF(this->message
);
1052 DESTROY_IF(this->proposal
);
1053 DESTROY_IF(this->child_sa
);
1054 DESTROY_IF(this->policy
);
1055 DESTROY_IF(this->connection
);
1058 this->tsi
->destroy_offset(this->tsi
, offsetof(traffic_selector_t
, destroy
));
1062 this->tsr
->destroy_offset(this->tsr
, offsetof(traffic_selector_t
, destroy
));
1064 chunk_free(&this->nonce_i
);
1065 chunk_free(&this->nonce_r
);
1066 chunk_free(&this->init_request
);
1067 chunk_free(&this->init_response
);
1072 * Described in header.
1074 ike_auth_t
*ike_auth_create(ike_sa_t
*ike_sa
)
1076 private_ike_auth_t
*this = malloc_thing(private_ike_auth_t
);
1078 /* transaction interface functions */
1079 this->public.transaction
.get_request
= (status_t(*)(transaction_t
*,message_t
**))get_request
;
1080 this->public.transaction
.get_response
= (status_t(*)(transaction_t
*,message_t
*,message_t
**,transaction_t
**))get_response
;
1081 this->public.transaction
.conclude
= (status_t(*)(transaction_t
*,message_t
*,transaction_t
**))conclude
;
1082 this->public.transaction
.get_message_id
= (u_int32_t(*)(transaction_t
*))get_message_id
;
1083 this->public.transaction
.requested
= (u_int32_t(*)(transaction_t
*))requested
;
1084 this->public.transaction
.destroy
= (void(*)(transaction_t
*))destroy
;
1086 /* public functions */
1087 this->public.set_config
= (void(*)(ike_auth_t
*,connection_t
*,policy_t
*))set_config
;
1088 this->public.set_reqid
= (void(*)(ike_auth_t
*,u_int32_t
))set_reqid
;
1089 this->public.set_nonces
= (void(*)(ike_auth_t
*,chunk_t
,chunk_t
))set_nonces
;
1090 this->public.set_init_messages
= (void(*)(ike_auth_t
*,chunk_t
,chunk_t
))set_init_messages
;
1093 this->ike_sa
= ike_sa
;
1094 this->message_id
= 0;
1095 this->message
= NULL
;
1096 this->requested
= 0;
1097 this->nonce_i
= CHUNK_INITIALIZER
;
1098 this->nonce_r
= CHUNK_INITIALIZER
;
1099 this->init_request
= CHUNK_INITIALIZER
;
1100 this->init_response
= CHUNK_INITIALIZER
;
1101 this->child_sa
= NULL
;
1102 this->proposal
= NULL
;
1105 this->build_child
= TRUE
;
1108 return &this->public;