1d15daebbfd2152661296996613ba24d2a0cc8af
[strongswan.git] / src / charon / sa / transactions / ike_auth.c
1 /**
2 * @file ike_auth.c
3 *
4 * @brief Implementation of ike_auth_t transaction.
5 *
6 */
7
8 /*
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
13 *
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>.
18 *
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
22 * for more details.
23 */
24
25 #include "ike_auth.h"
26
27 #include <string.h>
28
29 #include <daemon.h>
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>
38
39
40 typedef struct private_ike_auth_t private_ike_auth_t;
41
42 /**
43 * Private members of a ike_auth_t object..
44 */
45 struct private_ike_auth_t {
46
47 /**
48 * Public methods and transaction_t interface.
49 */
50 ike_auth_t public;
51
52 /**
53 * Assigned IKE_SA.
54 */
55 ike_sa_t *ike_sa;
56
57 /**
58 * Message sent by our peer, if already generated
59 */
60 message_t *message;
61
62 /**
63 * Message ID this transaction uses
64 */
65 u_int32_t message_id;
66
67 /**
68 * Times we did send the request
69 */
70 u_int32_t requested;
71
72 /**
73 * initiator chosen nonce
74 */
75 chunk_t nonce_i;
76
77 /**
78 * responder chosen nonce
79 */
80 chunk_t nonce_r;
81
82 /**
83 * encoded request message of ike_sa_init transaction
84 */
85 chunk_t init_request;
86
87 /**
88 * encoded response message of ike_sa_init transaction
89 */
90 chunk_t init_response;
91
92 /**
93 * connection definition used for IKE_SA setup
94 */
95 connection_t *connection;
96
97 /**
98 * policy definition used CHILD_SA creation
99 */
100 policy_t *policy;
101
102 /**
103 * Negotiated proposal used for CHILD_SA
104 */
105 proposal_t *proposal;
106
107 /**
108 * Negotiated traffic selectors for initiator
109 */
110 linked_list_t *tsi;
111
112 /**
113 * Negotiated traffic selectors for responder
114 */
115 linked_list_t *tsr;
116
117 /**
118 * CHILD_SA created along with IKE_AUTH
119 */
120 child_sa_t *child_sa;
121
122 /**
123 * did other peer create a CHILD_SA?
124 */
125 bool build_child;
126
127 /**
128 * reqid to use for CHILD_SA setup
129 */
130 u_int32_t reqid;
131
132 /**
133 * Assigned logger.
134 */
135 logger_t *logger;
136 };
137
138 /**
139 * Implementation of transaction_t.get_message_id.
140 */
141 static u_int32_t get_message_id(private_ike_auth_t *this)
142 {
143 return this->message_id;
144 }
145
146 /**
147 * Implementation of transaction_t.requested.
148 */
149 static u_int32_t requested(private_ike_auth_t *this)
150 {
151 return this->requested++;
152 }
153
154 /**
155 * Implementation of transaction_t.set_config.
156 */
157 static void set_config(private_ike_auth_t *this,
158 connection_t *connection, policy_t *policy)
159 {
160 this->connection = connection;
161 this->policy = policy;
162 }
163
164 /**
165 * Implementation of transaction_t.set_reqid.
166 */
167 static void set_reqid(private_ike_auth_t *this, u_int32_t reqid)
168 {
169 this->reqid = reqid;
170 }
171
172 /**
173 * Implementation of transaction_t.set_nonces.
174 */
175 static void set_nonces(private_ike_auth_t *this, chunk_t nonce_i, chunk_t nonce_r)
176 {
177 this->nonce_i = nonce_i;
178 this->nonce_r = nonce_r;
179 }
180
181 /**
182 * Implementation of transaction_t.set_init_messages.
183 */
184 static void set_init_messages(private_ike_auth_t *this, chunk_t init_request, chunk_t init_response)
185 {
186 this->init_request = init_request;
187 this->init_response = init_response;
188 }
189
190 /**
191 * destroy a list of traffic selectors
192 */
193 static void destroy_ts_list(linked_list_t *list)
194 {
195 if (list)
196 {
197 traffic_selector_t *ts;
198
199 while (list->remove_last(list, (void**)&ts) == SUCCESS)
200 {
201 ts->destroy(ts);
202 }
203 list->destroy(list);
204 }
205 }
206
207 /**
208 * destroy a list of proposals
209 */
210 static void destroy_proposal_list(linked_list_t *list)
211 {
212 proposal_t *proposal;
213
214 while (list->remove_last(list, (void**)&proposal) == SUCCESS)
215 {
216 proposal->destroy(proposal);
217 }
218 list->destroy(list);
219 }
220
221 /**
222 * Implementation of transaction_t.get_request.
223 */
224 static status_t get_request(private_ike_auth_t *this, message_t **result)
225 {
226 message_t *request;
227 host_t *me, *other;
228 identification_t *my_id, *other_id;
229 id_payload_t *my_id_payload;
230
231 /* check if we already have built a message (retransmission) */
232 if (this->message)
233 {
234 *result = this->message;
235 return SUCCESS;
236 }
237
238 me = this->ike_sa->get_my_host(this->ike_sa);
239 other = this->ike_sa->get_other_host(this->ike_sa);
240 my_id = this->policy->get_my_id(this->policy);
241 other_id = this->policy->get_other_id(this->policy);
242
243 /* build the request */
244 request = message_create();
245 request->set_source(request, me->clone(me));
246 request->set_destination(request, other->clone(other));
247 request->set_exchange_type(request, IKE_AUTH);
248 request->set_request(request, TRUE);
249 request->set_ike_sa_id(request, this->ike_sa->get_id(this->ike_sa));
250 /* apply for caller */
251 *result = request;
252 /* store for retransmission */
253 this->message = request;
254
255 { /* build ID payload */
256 my_id_payload = id_payload_create_from_identification(TRUE, my_id);
257 request->add_payload(request, (payload_t*)my_id_payload);
258 }
259
260 { /* TODO: build certreq payload */
261
262 }
263
264 if (this->connection->get_cert_policy(this->connection) != CERT_NEVER_SEND)
265 { /* build certificate payload. TODO: Handle certreq from init_ike_sa. */
266 x509_t *cert;
267 cert_payload_t *cert_payload;
268
269 cert = charon->credentials->get_certificate(charon->credentials, my_id);
270 if (cert)
271 {
272 cert_payload = cert_payload_create_from_x509(cert);
273 request->add_payload(request, (payload_t*)cert_payload);
274 }
275 else
276 {
277 this->logger->log(this->logger, ERROR,
278 "could not find my certificate, certificate payload omitted");
279 }
280 }
281
282 { /* build IDr payload, if other_id defined */
283 id_payload_t *id_payload;
284 if (!other_id->contains_wildcards(other_id))
285 {
286 id_payload = id_payload_create_from_identification(FALSE, other_id);
287 request->add_payload(request, (payload_t*)id_payload);
288 }
289 }
290
291 { /* build auth payload */
292 authenticator_t *authenticator;
293 auth_payload_t *auth_payload;
294 auth_method_t auth_method;
295 status_t status;
296
297 auth_method = this->connection->get_auth_method(this->connection);
298 authenticator = authenticator_create(this->ike_sa, auth_method);
299 status = authenticator->compute_auth_data(authenticator, &auth_payload,
300 this->init_request, this->nonce_r, my_id_payload, TRUE);
301 authenticator->destroy(authenticator);
302 if (status != SUCCESS)
303 {
304 this->logger->log(this->logger, AUDIT,
305 "could not generate AUTH data, deleting IKE_SA");
306 return DESTROY_ME;
307 }
308 request->add_payload(request, (payload_t*)auth_payload);
309 }
310
311 { /* build SA payload for CHILD_SA */
312 linked_list_t *proposal_list;
313 sa_payload_t *sa_payload;
314 u_int32_t soft_lifetime, hard_lifetime;
315
316 proposal_list = this->policy->get_proposals(this->policy);
317 soft_lifetime = this->policy->get_soft_lifetime(this->policy);
318 hard_lifetime = this->policy->get_hard_lifetime(this->policy);
319 this->child_sa = child_sa_create(this->reqid, me, other, soft_lifetime, hard_lifetime,
320 this->ike_sa->is_natt_enabled(this->ike_sa));
321 this->child_sa->set_name(this->child_sa, this->policy->get_name(this->policy));
322 if (this->child_sa->alloc(this->child_sa, proposal_list) != SUCCESS)
323 {
324 this->logger->log(this->logger, ERROR,
325 "could not install CHILD_SA, deleting IKE_SA");
326 return DESTROY_ME;
327 }
328 sa_payload = sa_payload_create_from_proposal_list(proposal_list);
329 destroy_proposal_list(proposal_list);
330 request->add_payload(request, (payload_t*)sa_payload);
331 }
332
333 { /* build TSi payload */
334 linked_list_t *ts_list;
335 ts_payload_t *ts_payload;
336
337 ts_list = this->policy->get_my_traffic_selectors(this->policy, me);
338 ts_payload = ts_payload_create_from_traffic_selectors(TRUE, ts_list);
339 destroy_ts_list(ts_list);
340
341 request->add_payload(request, (payload_t*)ts_payload);
342 }
343
344 { /* build TSr payload */
345 linked_list_t *ts_list;
346 ts_payload_t *ts_payload;
347
348 ts_list = this->policy->get_other_traffic_selectors(this->policy, other);
349 ts_payload = ts_payload_create_from_traffic_selectors(FALSE, ts_list);
350 destroy_ts_list(ts_list);
351
352 request->add_payload(request, (payload_t*)ts_payload);
353 }
354
355 this->message_id = this->ike_sa->get_next_message_id(this->ike_sa);
356 request->set_message_id(request, this->message_id);
357 return SUCCESS;
358 }
359
360 /**
361 * Handle all kind of notifies
362 */
363 static status_t process_notifies(private_ike_auth_t *this, notify_payload_t *notify_payload)
364 {
365 notify_type_t notify_type = notify_payload->get_notify_type(notify_payload);
366
367 this->logger->log(this->logger, CONTROL|LEVEL1, "process notify type %s",
368 mapping_find(notify_type_m, notify_type));
369
370 switch (notify_type)
371 {
372 /* these notifies are not critical. no child_sa is built, but IKE stays alive */
373 case SINGLE_PAIR_REQUIRED:
374 {
375 this->logger->log(this->logger, AUDIT,
376 "received a SINGLE_PAIR_REQUIRED notify");
377 this->build_child = FALSE;
378 return SUCCESS;
379 }
380 case TS_UNACCEPTABLE:
381 {
382 this->logger->log(this->logger, CONTROL,
383 "received TS_UNACCEPTABLE notify");
384 this->build_child = FALSE;
385 return SUCCESS;
386 }
387 case NO_PROPOSAL_CHOSEN:
388 {
389 this->logger->log(this->logger, CONTROL,
390 "received NO_PROPOSAL_CHOSEN notify");
391 this->build_child = FALSE;
392 return SUCCESS;
393 }
394 default:
395 {
396 if (notify_type < 16383)
397 {
398 this->logger->log(this->logger, AUDIT,
399 "received %s notify error (%d), deleting IKE_SA",
400 mapping_find(notify_type_m, notify_type),
401 notify_type);
402 return DESTROY_ME;
403 }
404 else
405 {
406 this->logger->log(this->logger, CONTROL,
407 "received %s notify (%d), ignored",
408 mapping_find(notify_type_m, notify_type),
409 notify_type);
410 return SUCCESS;
411 }
412 }
413 }
414 }
415
416 /**
417 * Build a notify message.
418 */
419 static void build_notify(notify_type_t type, message_t *message, bool flush_message)
420 {
421 notify_payload_t *notify;
422
423 if (flush_message)
424 {
425 payload_t *payload;
426 iterator_t *iterator = message->get_payload_iterator(message);
427 while (iterator->iterate(iterator, (void**)&payload))
428 {
429 payload->destroy(payload);
430 iterator->remove(iterator);
431 }
432 iterator->destroy(iterator);
433 }
434
435 notify = notify_payload_create();
436 notify->set_notify_type(notify, type);
437 message->add_payload(message, (payload_t*)notify);
438 }
439
440 /**
441 * Import a certificate from a cert payload
442 */
443 static void import_certificate(private_ike_auth_t *this, cert_payload_t *cert_payload)
444 {
445 bool found;
446 x509_t *cert;
447 cert_encoding_t encoding;
448
449 encoding = cert_payload->get_cert_encoding(cert_payload);
450 if (encoding != CERT_X509_SIGNATURE)
451 {
452 this->logger->log(this->logger, ERROR,
453 "certificate payload %s not supported, ignored",
454 enum_name(&cert_encoding_names, encoding));
455 return;
456 }
457 cert = x509_create_from_chunk(cert_payload->get_data_clone(cert_payload));
458
459 if (charon->credentials->verify(charon->credentials, cert, &found))
460 {
461 this->logger->log(this->logger, CONTROL|LEVEL1,
462 "received end entity certificate is trusted, added to store");
463 if (found)
464 {
465 cert->destroy(cert);
466 }
467 else
468 {
469 cert = charon->credentials->add_end_certificate(charon->credentials, cert);
470 }
471 }
472 else
473 {
474 this->logger->log(this->logger, CONTROL,
475 "received end entity certificate is not trusted, discarded");
476 }
477 }
478
479 /**
480 * Install a CHILD_SA for usage
481 */
482 static status_t install_child_sa(private_ike_auth_t *this, bool initiator)
483 {
484 prf_plus_t *prf_plus;
485 chunk_t seed;
486 status_t status;
487
488 seed = chunk_alloc(this->nonce_i.len + this->nonce_r.len);
489 memcpy(seed.ptr, this->nonce_i.ptr, this->nonce_i.len);
490 memcpy(seed.ptr + this->nonce_i.len, this->nonce_r.ptr, this->nonce_r.len);
491 prf_plus = prf_plus_create(this->ike_sa->get_child_prf(this->ike_sa), seed);
492 chunk_free(&seed);
493
494 if (initiator)
495 {
496 status = this->child_sa->update(this->child_sa, this->proposal, prf_plus);
497 }
498 else
499 {
500 status = this->child_sa->add(this->child_sa, this->proposal, prf_plus);
501 }
502 prf_plus->destroy(prf_plus);
503 if (status != SUCCESS)
504 {
505 return DESTROY_ME;
506 }
507 if (initiator)
508 {
509 status = this->child_sa->add_policies(this->child_sa, this->tsi, this->tsr);
510 }
511 else
512 {
513 status = this->child_sa->add_policies(this->child_sa, this->tsr, this->tsi);
514 }
515 if (status != SUCCESS)
516 {
517 return DESTROY_ME;
518 }
519 /* add to IKE_SA, and remove from transaction */
520 this->ike_sa->add_child_sa(this->ike_sa, this->child_sa);
521 this->child_sa = NULL;
522 return SUCCESS;
523 }
524
525 /**
526 * Implementation of transaction_t.get_response.
527 */
528 static status_t get_response(private_ike_auth_t *this, message_t *request,
529 message_t **result, transaction_t **next)
530 {
531 host_t *me, *other;
532 identification_t *my_id, *other_id;
533 message_t *response;
534 status_t status;
535 iterator_t *payloads;
536 id_payload_t *idi_request = NULL;
537 id_payload_t *idr_request = NULL;
538 auth_payload_t *auth_request = NULL;
539 cert_payload_t *cert_request = NULL;
540 sa_payload_t *sa_request = NULL;
541 ts_payload_t *tsi_request = NULL;
542 ts_payload_t *tsr_request = NULL;
543 id_payload_t *idr_response;
544
545 /* check if we already have built a response (retransmission) */
546 if (this->message)
547 {
548 *result = this->message;
549 return SUCCESS;
550 }
551
552 me = this->ike_sa->get_my_host(this->ike_sa);
553 other = this->ike_sa->get_other_host(this->ike_sa);
554 this->message_id = request->get_message_id(request);
555
556 /* set up response */
557 response = message_create();
558 response->set_source(response, me->clone(me));
559 response->set_destination(response, other->clone(other));
560 response->set_exchange_type(response, IKE_AUTH);
561 response->set_request(response, FALSE);
562 response->set_message_id(response, this->message_id);
563 response->set_ike_sa_id(response, this->ike_sa->get_id(this->ike_sa));
564 this->message = response;
565 *result = response;
566
567 /* check message type */
568 if (request->get_exchange_type(request) != IKE_AUTH)
569 {
570 this->logger->log(this->logger, ERROR,
571 "IKE_AUTH response of invalid type, deleting IKE_SA");
572 return DESTROY_ME;
573 }
574
575 /* Iterate over all payloads. */
576 payloads = request->get_payload_iterator(request);
577 while (payloads->has_next(payloads))
578 {
579 payload_t *payload;
580 payloads->current(payloads, (void**)&payload);
581 switch (payload->get_type(payload))
582 {
583 case ID_INITIATOR:
584 idi_request = (id_payload_t*)payload;
585 break;
586 case ID_RESPONDER:
587 idr_request = (id_payload_t*)payload;
588 break;
589 case AUTHENTICATION:
590 auth_request = (auth_payload_t*)payload;
591 break;
592 case CERTIFICATE:
593 cert_request = (cert_payload_t*)payload;
594 break;
595 case SECURITY_ASSOCIATION:
596 sa_request = (sa_payload_t*)payload;
597 break;
598 case TRAFFIC_SELECTOR_INITIATOR:
599 tsi_request = (ts_payload_t*)payload;
600 break;
601 case TRAFFIC_SELECTOR_RESPONDER:
602 tsr_request = (ts_payload_t*)payload;
603 break;
604 case NOTIFY:
605 {
606 status = process_notifies(this, (notify_payload_t*)payload);
607 if (status == FAILED)
608 {
609 payloads->destroy(payloads);
610 /* we return SUCCESS, returned FAILED means do next transaction */
611 return SUCCESS;
612 }
613 if (status == DESTROY_ME)
614 {
615 payloads->destroy(payloads);
616 return DESTROY_ME;
617 }
618 break;
619 }
620 default:
621 {
622 this->logger->log(this->logger, ERROR, "ignoring %s payload (%d)",
623 mapping_find(payload_type_m, payload->get_type(payload)),
624 payload->get_type(payload));
625 break;
626 }
627 }
628 }
629 payloads->destroy(payloads);
630
631 /* check if we have all payloads */
632 if (!(idi_request && auth_request && sa_request && tsi_request && tsr_request))
633 {
634 build_notify(INVALID_SYNTAX, response, TRUE);
635 this->logger->log(this->logger, AUDIT,
636 "request message incomplete, deleting IKE_SA");
637 return DESTROY_ME;
638 }
639
640 { /* process ID payload */
641 other_id = idi_request->get_identification(idi_request);
642 if (idr_request)
643 {
644 my_id = idr_request->get_identification(idr_request);
645 }
646 else
647 {
648 my_id = identification_create_from_encoding(ID_ANY, CHUNK_INITIALIZER);
649 }
650 }
651
652 { /* get a policy and process traffic selectors */
653 linked_list_t *my_ts, *other_ts;
654
655 my_ts = tsr_request->get_traffic_selectors(tsr_request);
656 other_ts = tsi_request->get_traffic_selectors(tsi_request);
657
658 this->policy = charon->policies->get_policy(charon->policies,
659 my_id, other_id,
660 my_ts, other_ts,
661 me, other);
662 if (this->policy)
663 {
664 this->tsr = this->policy->select_my_traffic_selectors(this->policy, my_ts, me);
665 this->tsi = this->policy->select_other_traffic_selectors(this->policy, other_ts, other);
666 }
667 destroy_ts_list(my_ts);
668 destroy_ts_list(other_ts);
669
670 /* TODO: We should check somehow if we have a policy, but with other
671 * traffic selectors. Then we would create a IKE_SA without a CHILD_SA. */
672 if (this->policy == NULL)
673 {
674 this->logger->log(this->logger, AUDIT,
675 "no acceptable policy for IDs %s - %s found, deleting IKE_SA",
676 my_id->get_string(my_id), other_id->get_string(other_id));
677 my_id->destroy(my_id);
678 other_id->destroy(other_id);
679 build_notify(AUTHENTICATION_FAILED, response, TRUE);
680 return DESTROY_ME;
681 }
682 my_id->destroy(my_id);
683
684 /* get my id from policy, which must contain a fully qualified valid id */
685 my_id = this->policy->get_my_id(this->policy);
686 this->ike_sa->set_my_id(this->ike_sa, my_id->clone(my_id));
687 this->ike_sa->set_other_id(this->ike_sa, other_id);
688
689 idr_response = id_payload_create_from_identification(FALSE, my_id);
690 response->add_payload(response, (payload_t*)idr_response);
691 }
692
693 if (this->connection->get_cert_policy(this->connection) != CERT_NEVER_SEND)
694 { /* build certificate payload */
695 x509_t *cert;
696 cert_payload_t *cert_payload;
697
698 cert = charon->credentials->get_certificate(charon->credentials, my_id);
699 if (cert == NULL)
700 {
701 this->logger->log(this->logger, ERROR,
702 "could not find my certificate, cert payload omitted");
703 }
704 cert_payload = cert_payload_create_from_x509(cert);
705 response->add_payload(response, (payload_t *)cert_payload);
706 }
707
708 if (cert_request)
709 { /* process certificate payload */
710 import_certificate(this, cert_request);
711 }
712
713 { /* process auth payload */
714 authenticator_t *authenticator;
715 auth_payload_t *auth_response;
716 auth_method_t auth_method;
717 status_t status;
718
719 auth_method = this->connection->get_auth_method(this->connection);
720 authenticator = authenticator_create(this->ike_sa, auth_method);
721 status = authenticator->verify_auth_data(authenticator, auth_request,
722 this->init_request,
723 this->nonce_r, idi_request,
724 TRUE);
725 if (status != SUCCESS)
726 {
727 this->logger->log(this->logger, AUDIT,
728 "authentication failed, deleting IKE_SA");
729 build_notify(AUTHENTICATION_FAILED, response, TRUE);
730 authenticator->destroy(authenticator);
731 return DESTROY_ME;
732 }
733 status = authenticator->compute_auth_data(authenticator, &auth_response,
734 this->init_response,
735 this->nonce_i, idr_response,
736 FALSE);
737 authenticator->destroy(authenticator);
738 if (status != SUCCESS)
739 {
740 this->logger->log(this->logger, AUDIT,
741 "authentication data generation failed, deleting IKE_SA");
742 build_notify(AUTHENTICATION_FAILED, response, TRUE);
743 return DESTROY_ME;
744 }
745 response->add_payload(response, (payload_t*)auth_response);
746 }
747
748 { /* process SA payload */
749 linked_list_t *proposal_list;
750 sa_payload_t *sa_response;
751 ts_payload_t *ts_response;
752 bool use_natt;
753 u_int32_t soft_lifetime, hard_lifetime;
754
755 /* prepare reply */
756 sa_response = sa_payload_create();
757
758 /* get proposals from request, and select one with ours */
759 proposal_list = sa_request->get_proposals(sa_request);
760 this->logger->log(this->logger, CONTROL|LEVEL1, "selecting proposals:");
761 this->proposal = this->policy->select_proposal(this->policy, proposal_list);
762 destroy_proposal_list(proposal_list);
763
764 /* do we have a proposal? */
765 if (this->proposal == NULL)
766 {
767 this->logger->log(this->logger, AUDIT,
768 "CHILD_SA proposals unacceptable, adding NO_PROPOSAL_CHOSEN notify");
769 build_notify(NO_PROPOSAL_CHOSEN, response, FALSE);
770 }
771 /* do we have traffic selectors? */
772 else if (this->tsi->get_count(this->tsi) == 0 || this->tsr->get_count(this->tsr) == 0)
773 {
774 this->logger->log(this->logger, AUDIT,
775 "CHILD_SA traffic selectors unacceptable, adding TS_UNACCEPTABLE notify");
776 build_notify(TS_UNACCEPTABLE, response, FALSE);
777 }
778 else
779 {
780 /* create child sa */
781 soft_lifetime = this->policy->get_soft_lifetime(this->policy);
782 hard_lifetime = this->policy->get_hard_lifetime(this->policy);
783 use_natt = this->ike_sa->is_natt_enabled(this->ike_sa);
784 this->child_sa = child_sa_create(this->reqid, me, other,
785 soft_lifetime, hard_lifetime,
786 use_natt);
787 this->child_sa->set_name(this->child_sa, this->policy->get_name(this->policy));
788 if (install_child_sa(this, FALSE) != SUCCESS)
789 {
790 this->logger->log(this->logger, ERROR,
791 "installing CHILD_SA failed, adding NO_PROPOSAL_CHOSEN notify");
792 build_notify(NO_PROPOSAL_CHOSEN, response, FALSE);
793 }
794 /* add proposal to sa payload */
795 sa_response->add_proposal(sa_response, this->proposal);
796 }
797 response->add_payload(response, (payload_t*)sa_response);
798
799 /* add ts payload after sa payload */
800 ts_response = ts_payload_create_from_traffic_selectors(TRUE, this->tsi);
801 response->add_payload(response, (payload_t*)ts_response);
802 ts_response = ts_payload_create_from_traffic_selectors(FALSE, this->tsr);
803 response->add_payload(response, (payload_t*)ts_response);
804 }
805 /* set established state */
806 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
807 return SUCCESS;
808 }
809
810
811 /**
812 * Implementation of transaction_t.conclude
813 */
814 static status_t conclude(private_ike_auth_t *this, message_t *response,
815 transaction_t **transaction)
816 {
817 iterator_t *payloads;
818 host_t *me, *other;
819 identification_t *other_id;
820 ts_payload_t *tsi_payload = NULL;
821 ts_payload_t *tsr_payload = NULL;
822 id_payload_t *idr_payload = NULL;
823 cert_payload_t *cert_payload = NULL;
824 auth_payload_t *auth_payload = NULL;
825 sa_payload_t *sa_payload = NULL;
826 status_t status;
827
828 /* check message type */
829 if (response->get_exchange_type(response) != IKE_AUTH)
830 {
831 this->logger->log(this->logger, ERROR,
832 "IKE_AUTH response of invalid type, deleting IKE_SA");
833 return DESTROY_ME;
834 }
835
836 me = this->ike_sa->get_my_host(this->ike_sa);
837 other = this->ike_sa->get_other_host(this->ike_sa);
838
839 /* Iterate over all payloads to collect them */
840 payloads = response->get_payload_iterator(response);
841 while (payloads->has_next(payloads))
842 {
843 payload_t *payload;
844 payloads->current(payloads, (void**)&payload);
845
846 switch (payload->get_type(payload))
847 {
848 case ID_RESPONDER:
849 idr_payload = (id_payload_t*)payload;
850 break;
851 case AUTHENTICATION:
852 auth_payload = (auth_payload_t*)payload;
853 break;
854 case CERTIFICATE:
855 cert_payload = (cert_payload_t*)payload;
856 break;
857 case SECURITY_ASSOCIATION:
858 sa_payload = (sa_payload_t*)payload;
859 break;
860 case TRAFFIC_SELECTOR_INITIATOR:
861 tsi_payload = (ts_payload_t*)payload;
862 break;
863 case TRAFFIC_SELECTOR_RESPONDER:
864 tsr_payload = (ts_payload_t*)payload;
865 break;
866 case NOTIFY:
867 {
868 status = process_notifies(this, (notify_payload_t*)payload);
869 if (status == FAILED)
870 {
871 payloads->destroy(payloads);
872 /* we return SUCCESS, returned FAILED means do next transaction */
873 return SUCCESS;
874 }
875 if (status == DESTROY_ME)
876 {
877 payloads->destroy(payloads);
878 return status;
879 }
880 break;
881 }
882 default:
883 {
884 this->logger->log(this->logger, CONTROL, "ignoring payload %s (%d)",
885 mapping_find(payload_type_m, payload->get_type(payload)),
886 payload->get_type(payload));
887 break;
888 }
889 }
890 }
891 payloads->destroy(payloads);
892
893 if (!(idr_payload && auth_payload && sa_payload && tsi_payload && tsr_payload))
894 {
895 this->logger->log(this->logger, AUDIT, "response message incomplete, deleting IKE_SA");
896 return DESTROY_ME;
897 }
898
899 { /* process idr payload */
900 identification_t *configured_other_id;
901 int wildcards;
902
903 other_id = idr_payload->get_identification(idr_payload);
904 configured_other_id = this->policy->get_other_id(this->policy);
905
906 if (!other_id->matches(other_id, configured_other_id, &wildcards))
907 {
908 other_id->destroy(other_id);
909 this->logger->log(this->logger, AUDIT,
910 "other peer uses unacceptable ID (%s, excepted %s), deleting IKE_SA",
911 other_id->get_string(other_id),
912 configured_other_id->get_string(configured_other_id));
913 return DESTROY_ME;
914 }
915 /* update other ID. It was already set, but may contain wildcards */
916 this->ike_sa->set_other_id(this->ike_sa, other_id);
917 }
918
919 if (cert_payload)
920 { /* process cert payload */
921 import_certificate(this, cert_payload);
922 }
923
924 { /* authenticate peer */
925 authenticator_t *authenticator;
926 auth_method_t auth_method;
927 status_t status;
928
929 auth_method = this->connection->get_auth_method(this->connection);
930 authenticator = authenticator_create(this->ike_sa, auth_method);
931 status = authenticator->verify_auth_data(authenticator, auth_payload,
932 this->init_response,
933 this->nonce_i, idr_payload,
934 FALSE);
935 authenticator->destroy(authenticator);
936 if (status != SUCCESS)
937 {
938 this->logger->log(this->logger, AUDIT, "authentication failed, deleting IKE_SA");
939 return DESTROY_ME;
940 }
941 }
942
943 { /* process traffic selectors for us */
944 linked_list_t *ts_received = tsi_payload->get_traffic_selectors(tsi_payload);
945 this->tsi = this->policy->select_my_traffic_selectors(this->policy, ts_received, me);
946 destroy_ts_list(ts_received);
947 }
948
949 { /* process traffic selectors for other */
950 linked_list_t *ts_received = tsr_payload->get_traffic_selectors(tsr_payload);
951 this->tsr = this->policy->select_other_traffic_selectors(this->policy, ts_received, other);
952 destroy_ts_list(ts_received);
953 }
954
955 { /* process sa payload */
956 linked_list_t *proposal_list;
957
958 proposal_list = sa_payload->get_proposals(sa_payload);
959 /* we have to re-check here if other's selection is valid */
960 this->proposal = this->policy->select_proposal(this->policy, proposal_list);
961 destroy_proposal_list(proposal_list);
962
963 /* everything fine to create CHILD? */
964 if (this->proposal == NULL ||
965 this->tsi->get_count(this->tsi) == 0 ||
966 this->tsr->get_count(this->tsr) == 0 ||
967 !this->build_child)
968 {
969 this->logger->log(this->logger, AUDIT,
970 "CHILD_SA creation failed");
971 }
972 else
973 {
974 if (install_child_sa(this, TRUE) != SUCCESS)
975 {
976 this->logger->log(this->logger, ERROR,
977 "installing CHILD_SA failed, no CHILD_SA built");
978 }
979 }
980 }
981 /* set new state */
982 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
983 return SUCCESS;
984 }
985
986 /**
987 * implements transaction_t.destroy
988 */
989 static void destroy(private_ike_auth_t *this)
990 {
991 DESTROY_IF(this->message);
992 DESTROY_IF(this->proposal);
993 DESTROY_IF(this->child_sa);
994 DESTROY_IF(this->policy);
995 DESTROY_IF(this->connection);
996 destroy_ts_list(this->tsi);
997 destroy_ts_list(this->tsr);
998 chunk_free(&this->nonce_i);
999 chunk_free(&this->nonce_r);
1000 chunk_free(&this->init_request);
1001 chunk_free(&this->init_response);
1002 free(this);
1003 }
1004
1005 /*
1006 * Described in header.
1007 */
1008 ike_auth_t *ike_auth_create(ike_sa_t *ike_sa)
1009 {
1010 private_ike_auth_t *this = malloc_thing(private_ike_auth_t);
1011
1012 /* transaction interface functions */
1013 this->public.transaction.get_request = (status_t(*)(transaction_t*,message_t**))get_request;
1014 this->public.transaction.get_response = (status_t(*)(transaction_t*,message_t*,message_t**,transaction_t**))get_response;
1015 this->public.transaction.conclude = (status_t(*)(transaction_t*,message_t*,transaction_t**))conclude;
1016 this->public.transaction.get_message_id = (u_int32_t(*)(transaction_t*))get_message_id;
1017 this->public.transaction.requested = (u_int32_t(*)(transaction_t*))requested;
1018 this->public.transaction.destroy = (void(*)(transaction_t*))destroy;
1019
1020 /* public functions */
1021 this->public.set_config = (void(*)(ike_auth_t*,connection_t*,policy_t*))set_config;
1022 this->public.set_reqid = (void(*)(ike_auth_t*,u_int32_t))set_reqid;
1023 this->public.set_nonces = (void(*)(ike_auth_t*,chunk_t,chunk_t))set_nonces;
1024 this->public.set_init_messages = (void(*)(ike_auth_t*,chunk_t,chunk_t))set_init_messages;
1025
1026 /* private data */
1027 this->ike_sa = ike_sa;
1028 this->message_id = 0;
1029 this->message = NULL;
1030 this->requested = 0;
1031 this->nonce_i = CHUNK_INITIALIZER;
1032 this->nonce_r = CHUNK_INITIALIZER;
1033 this->init_request = CHUNK_INITIALIZER;
1034 this->init_response = CHUNK_INITIALIZER;
1035 this->child_sa = NULL;
1036 this->proposal = NULL;
1037 this->tsi = NULL;
1038 this->tsr = NULL;
1039 this->build_child = TRUE;
1040 this->reqid = 0;
1041 this->logger = logger_manager->get_logger(logger_manager, IKE_SA);
1042
1043 return &this->public;
1044 }