3ab4d8ae4a11c139d061beafed5f6ba40eda664a
[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/authenticators/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 * mode the CHILD_SA uses: tranport, tunnel, BEET
134 */
135 mode_t mode;
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 * Build a notify message.
192 */
193 static void build_notify(notify_type_t type, message_t *message, bool flush_message)
194 {
195 notify_payload_t *notify;
196
197 if (flush_message)
198 {
199 payload_t *payload;
200 iterator_t *iterator = message->get_payload_iterator(message);
201 while (iterator->iterate(iterator, (void**)&payload))
202 {
203 payload->destroy(payload);
204 iterator->remove(iterator);
205 }
206 iterator->destroy(iterator);
207 }
208
209 notify = notify_payload_create();
210 notify->set_notify_type(notify, type);
211 message->add_payload(message, (payload_t*)notify);
212 }
213
214 /**
215 * Implementation of transaction_t.get_request.
216 */
217 static status_t get_request(private_ike_auth_t *this, message_t **result)
218 {
219 message_t *request;
220 host_t *me, *other;
221 identification_t *my_id, *other_id;
222 id_payload_t *my_id_payload;
223
224 /* check if we already have built a message (retransmission) */
225 if (this->message)
226 {
227 *result = this->message;
228 return SUCCESS;
229 }
230
231 me = this->ike_sa->get_my_host(this->ike_sa);
232 other = this->ike_sa->get_other_host(this->ike_sa);
233 my_id = this->policy->get_my_id(this->policy);
234 other_id = this->policy->get_other_id(this->policy);
235
236 /* build the request */
237 request = message_create();
238 request->set_source(request, me->clone(me));
239 request->set_destination(request, other->clone(other));
240 request->set_exchange_type(request, IKE_AUTH);
241 request->set_request(request, TRUE);
242 request->set_ike_sa_id(request, this->ike_sa->get_id(this->ike_sa));
243 /* apply for caller */
244 *result = request;
245 /* store for retransmission */
246 this->message = request;
247
248 { /* build ID payload */
249 my_id_payload = id_payload_create_from_identification(TRUE, my_id);
250 request->add_payload(request, (payload_t*)my_id_payload);
251 }
252
253 /* build certificate request payload */
254 if (this->connection->get_certreq_policy(this->connection) != CERT_NEVER_SEND)
255 {
256 certreq_payload_t *certreq_payload;
257 identification_t *other_ca = this->policy->get_other_ca(this->policy);
258
259 certreq_payload = (other_ca->get_type(other_ca) == ID_ANY)
260 ? certreq_payload_create_from_cacerts()
261 : certreq_payload_create_from_cacert(other_ca);
262
263 if (certreq_payload != NULL)
264 {
265 request->add_payload(request, (payload_t*)certreq_payload);
266 }
267 }
268
269 /* build certificate payload. TODO: Handle certreq from init_ike_sa. */
270 if (this->policy->get_auth_method(this->policy) == AUTH_RSA
271 && this->connection->get_cert_policy(this->connection) != CERT_NEVER_SEND)
272 {
273 cert_payload_t *cert_payload;
274
275 x509_t *cert = charon->credentials->get_certificate(charon->credentials, my_id);
276
277 if (cert)
278 {
279 cert_payload = cert_payload_create_from_x509(cert);
280 request->add_payload(request, (payload_t*)cert_payload);
281 }
282 else
283 {
284 DBG1(DBG_IKE, "could not find my certificate, certificate payload omitted");
285 }
286 }
287
288 { /* build IDr payload, if other_id defined */
289 id_payload_t *id_payload;
290 if (!other_id->contains_wildcards(other_id))
291 {
292 id_payload = id_payload_create_from_identification(FALSE, other_id);
293 request->add_payload(request, (payload_t*)id_payload);
294 }
295 }
296
297 { /* build auth payload */
298 authenticator_t *authenticator;
299 auth_payload_t *auth_payload;
300 auth_method_t auth_method;
301 status_t status;
302
303 auth_method = this->policy->get_auth_method(this->policy);
304 authenticator = authenticator_create(this->ike_sa, auth_method);
305 if (authenticator == NULL)
306 {
307 SIG(IKE_UP_FAILED, "auth method %N not supported, deleting IKE_SA",
308 auth_method_names, auth_method);
309 SIG(CHILD_UP_FAILED, "initiating CHILD_SA failed, unable to create IKE_SA");
310 return DESTROY_ME;
311 }
312 status = authenticator->build(authenticator, this->init_request,
313 this->nonce_r, &auth_payload);
314 authenticator->destroy(authenticator);
315 if (status != SUCCESS)
316 {
317 SIG(IKE_UP_FAILED, "could not generate AUTH data, deleting IKE_SA");
318 SIG(CHILD_UP_FAILED, "initiating CHILD_SA failed, unable to create IKE_SA");
319 return DESTROY_ME;
320 }
321 request->add_payload(request, (payload_t*)auth_payload);
322 }
323
324 { /* build SA payload for CHILD_SA */
325 linked_list_t *proposal_list;
326 sa_payload_t *sa_payload;
327 u_int32_t soft_lifetime, hard_lifetime;
328 bool enable_natt;
329
330 proposal_list = this->policy->get_proposals(this->policy);
331 soft_lifetime = this->policy->get_soft_lifetime(this->policy);
332 hard_lifetime = this->policy->get_hard_lifetime(this->policy);
333 enable_natt = this->ike_sa->is_natt_enabled(this->ike_sa);
334 this->child_sa = child_sa_create(this->reqid, me, other, my_id, other_id,
335 soft_lifetime, hard_lifetime,
336 this->policy->get_updown(this->policy),
337 this->policy->get_hostaccess(this->policy),
338 enable_natt);
339 this->child_sa->set_name(this->child_sa, this->policy->get_name(this->policy));
340 if (this->child_sa->alloc(this->child_sa, proposal_list) != SUCCESS)
341 {
342 SIG(IKE_UP_FAILED, "could not install CHILD_SA, deleting IKE_SA");
343 SIG(CHILD_UP_FAILED, "initiating CHILD_SA failed, unable to create IKE_SA");
344 return DESTROY_ME;
345 }
346 sa_payload = sa_payload_create_from_proposal_list(proposal_list);
347 proposal_list->destroy_offset(proposal_list, offsetof(proposal_t, destroy));
348 request->add_payload(request, (payload_t*)sa_payload);
349 }
350
351 /* notify for transport/BEET mode, we propose it
352 * independent of the traffic selectors */
353 switch (this->policy->get_mode(this->policy))
354 {
355 case MODE_TUNNEL:
356 /* is the default */
357 break;
358 case MODE_TRANSPORT:
359 if (this->ike_sa->is_natt_enabled(this->ike_sa))
360 {
361 DBG1(DBG_IKE, "not using tranport mode, as connection NATed");
362 }
363 else
364 {
365 build_notify(USE_TRANSPORT_MODE, request, FALSE);
366 }
367 break;
368 case MODE_BEET:
369 build_notify(USE_BEET_MODE, request, FALSE);
370 break;
371 }
372
373 { /* build TSi payload */
374 linked_list_t *ts_list;
375 ts_payload_t *ts_payload;
376
377 ts_list = this->policy->get_my_traffic_selectors(this->policy, me);
378 ts_payload = ts_payload_create_from_traffic_selectors(TRUE, ts_list);
379 ts_list->destroy_offset(ts_list, offsetof(traffic_selector_t, destroy));
380
381 request->add_payload(request, (payload_t*)ts_payload);
382 }
383
384 { /* build TSr payload */
385 linked_list_t *ts_list;
386 ts_payload_t *ts_payload;
387
388 ts_list = this->policy->get_other_traffic_selectors(this->policy, other);
389 ts_payload = ts_payload_create_from_traffic_selectors(FALSE, ts_list);
390 ts_list->destroy_offset(ts_list, offsetof(traffic_selector_t, destroy));
391
392 request->add_payload(request, (payload_t*)ts_payload);
393 }
394
395 this->message_id = this->ike_sa->get_next_message_id(this->ike_sa);
396 request->set_message_id(request, this->message_id);
397 return SUCCESS;
398 }
399
400 /**
401 * Handle all kind of notifies
402 */
403 static status_t process_notifies(private_ike_auth_t *this, notify_payload_t *notify_payload)
404 {
405 notify_type_t notify_type = notify_payload->get_notify_type(notify_payload);
406
407 DBG2(DBG_IKE, "process notify type %N", notify_type_names, notify_type);
408
409 switch (notify_type)
410 {
411 /* these notifies are not critical. no child_sa is built, but IKE stays alive */
412 case SINGLE_PAIR_REQUIRED:
413 {
414 SIG(CHILD_UP_FAILED, "received a SINGLE_PAIR_REQUIRED notify");
415 this->build_child = FALSE;
416 return SUCCESS;
417 }
418 case TS_UNACCEPTABLE:
419 {
420 SIG(CHILD_UP_FAILED, "received TS_UNACCEPTABLE notify");
421 this->build_child = FALSE;
422 return SUCCESS;
423 }
424 case NO_PROPOSAL_CHOSEN:
425 {
426 SIG(CHILD_UP_FAILED, "received NO_PROPOSAL_CHOSEN notify");
427 this->build_child = FALSE;
428 return SUCCESS;
429 }
430 case USE_TRANSPORT_MODE:
431 {
432 this->mode = MODE_TRANSPORT;
433 return SUCCESS;
434 }
435 case USE_BEET_MODE:
436 {
437 this->mode = MODE_BEET;
438 return SUCCESS;
439 }
440 default:
441 {
442 if (notify_type < 16383)
443 {
444 SIG(IKE_UP_FAILED, "received %N notify error, deleting IKE_SA",
445 notify_type_names, notify_type);
446 return DESTROY_ME;
447 }
448 else
449 {
450 DBG1(DBG_IKE, "received %N notify, ignored",
451 notify_type_names, notify_type);
452 return SUCCESS;
453 }
454 }
455 }
456 }
457
458 /**
459 * Import certificate requests from a certreq payload
460 */
461 static void add_certificate_request(certreq_payload_t *certreq_payload,
462 linked_list_t *requested_ca_keyids)
463 {
464 chunk_t keyids;
465
466 cert_encoding_t encoding = certreq_payload->get_cert_encoding(certreq_payload);
467
468 if (encoding != CERT_X509_SIGNATURE)
469 {
470 DBG1(DBG_IKE, "certreq payload %N not supported, ignored",
471 cert_encoding_names, encoding);
472 return;
473 }
474
475 keyids = certreq_payload->get_data(certreq_payload);
476
477 while (keyids.len >= HASH_SIZE_SHA1)
478 {
479 chunk_t keyid = { keyids.ptr, HASH_SIZE_SHA1};
480 x509_t *cacert = charon->credentials->get_ca_certificate_by_keyid(charon->credentials, keyid);
481
482 if (cacert)
483 {
484 DBG2(DBG_IKE, "request for certificate issued by ca '%D'", cacert->get_subject(cacert));
485 requested_ca_keyids->insert_last(requested_ca_keyids, (void *)&keyid);
486 }
487 else
488 {
489 DBG2(DBG_IKE, "request for certificate issued by unknown ca");
490 }
491 DBG2(DBG_IKE, " with keyid %#B", &keyid);
492
493 keyids.ptr += HASH_SIZE_SHA1;
494 keyids.len -= HASH_SIZE_SHA1;
495 }
496 }
497
498 /**
499 * Import a certificate from a cert payload
500 */
501 static void import_certificate(cert_payload_t *cert_payload)
502 {
503 bool found;
504 x509_t *cert;
505 cert_encoding_t encoding;
506
507 encoding = cert_payload->get_cert_encoding(cert_payload);
508 if (encoding != CERT_X509_SIGNATURE)
509 {
510 DBG1(DBG_IKE, "certificate payload %N not supported, ignored",
511 cert_encoding_names, encoding);
512 return;
513 }
514 cert = x509_create_from_chunk(cert_payload->get_data_clone(cert_payload));
515 if (cert)
516 {
517 if (charon->credentials->verify(charon->credentials, cert, &found))
518 {
519 DBG2(DBG_IKE, "received end entity certificate is trusted, added to store");
520 if (!found)
521 {
522 charon->credentials->add_end_certificate(charon->credentials, cert);
523 }
524 else
525 {
526 cert->destroy(cert);
527 }
528 }
529 else
530 {
531 DBG1(DBG_IKE, "received end entity certificate is not trusted, discarded");
532 cert->destroy(cert);
533 }
534 }
535 else
536 {
537 DBG1(DBG_IKE, "parsing of received certificate failed, discarded");
538 }
539 }
540
541 /**
542 * Check a list of traffic selectors if any selector belongs to host
543 */
544 static bool ts_list_is_host(linked_list_t *list, host_t *host)
545 {
546 traffic_selector_t *ts;
547 bool is_host = TRUE;
548 iterator_t *iterator = list->create_iterator(list, TRUE);
549
550 while (is_host && iterator->iterate(iterator, (void**)&ts))
551 {
552 is_host = is_host && ts->is_host(ts, host);
553 }
554 iterator->destroy(iterator);
555 return is_host;
556 }
557
558 /**
559 * Install a CHILD_SA for usage
560 */
561 static status_t install_child_sa(private_ike_auth_t *this, bool initiator)
562 {
563 prf_plus_t *prf_plus;
564 chunk_t seed;
565 status_t status;
566
567 seed = chunk_alloc(this->nonce_i.len + this->nonce_r.len);
568 memcpy(seed.ptr, this->nonce_i.ptr, this->nonce_i.len);
569 memcpy(seed.ptr + this->nonce_i.len, this->nonce_r.ptr, this->nonce_r.len);
570 prf_plus = prf_plus_create(this->ike_sa->get_child_prf(this->ike_sa), seed);
571 chunk_free(&seed);
572
573 if (initiator)
574 {
575 status = this->child_sa->update(this->child_sa, this->proposal,
576 this->mode, prf_plus);
577 }
578 else
579 {
580 status = this->child_sa->add(this->child_sa, this->proposal,
581 this->mode, prf_plus);
582 }
583 prf_plus->destroy(prf_plus);
584 if (status != SUCCESS)
585 {
586 return DESTROY_ME;
587 }
588 if (initiator)
589 {
590 status = this->child_sa->add_policies(this->child_sa, this->tsi,
591 this->tsr, this->mode);
592 }
593 else
594 {
595 status = this->child_sa->add_policies(this->child_sa, this->tsr,
596 this->tsi, this->mode);
597 }
598 if (status != SUCCESS)
599 {
600 return DESTROY_ME;
601 }
602
603 /* add to IKE_SA, and remove from transaction */
604 this->child_sa->set_state(this->child_sa, CHILD_INSTALLED);
605 this->ike_sa->add_child_sa(this->ike_sa, this->child_sa);
606 this->child_sa = NULL;
607 return SUCCESS;
608 }
609
610 /**
611 * Implementation of transaction_t.get_response.
612 */
613 static status_t get_response(private_ike_auth_t *this, message_t *request,
614 message_t **result, transaction_t **next)
615 {
616 host_t *me, *other;
617 identification_t *my_id, *other_id;
618 linked_list_t *requested_ca_keyids;
619 message_t *response;
620 status_t status;
621 iterator_t *payloads;
622 payload_t *payload;
623 id_payload_t *idi_request = NULL;
624 id_payload_t *idr_request = NULL;
625 auth_payload_t *auth_request = NULL;
626 certreq_payload_t *certreq_request = NULL;
627 cert_payload_t *cert_request = NULL;
628 sa_payload_t *sa_request = NULL;
629 ts_payload_t *tsi_request = NULL;
630 ts_payload_t *tsr_request = NULL;
631 id_payload_t *idr_response;
632
633 /* check if we already have built a response (retransmission) */
634 if (this->message)
635 {
636 *result = this->message;
637 return SUCCESS;
638 }
639
640 SIG(CHILD_UP_START, "setting up CHILD_SA along with IKE_AUTH");
641
642 me = this->ike_sa->get_my_host(this->ike_sa);
643 other = this->ike_sa->get_other_host(this->ike_sa);
644 this->message_id = request->get_message_id(request);
645
646 /* set up response */
647 response = message_create();
648 response->set_source(response, me->clone(me));
649 response->set_destination(response, other->clone(other));
650 response->set_exchange_type(response, IKE_AUTH);
651 response->set_request(response, FALSE);
652 response->set_message_id(response, this->message_id);
653 response->set_ike_sa_id(response, this->ike_sa->get_id(this->ike_sa));
654 this->message = response;
655 *result = response;
656
657 /* check message type */
658 if (request->get_exchange_type(request) != IKE_AUTH)
659 {
660 SIG(IKE_UP_FAILED, "IKE_AUTH response of invalid type, deleting IKE_SA");
661 SIG(CHILD_UP_FAILED, "initiating CHILD_SA failed, unable to create IKE_SA");
662 return DESTROY_ME;
663 }
664
665 /* initialize list of requested ca keyids */
666 requested_ca_keyids = linked_list_create();
667
668 /* Iterate over all payloads. */
669 payloads = request->get_payload_iterator(request);
670 while (payloads->iterate(payloads, (void**)&payload))
671 {
672 switch (payload->get_type(payload))
673 {
674 case ID_INITIATOR:
675 idi_request = (id_payload_t*)payload;
676 break;
677 case ID_RESPONDER:
678 idr_request = (id_payload_t*)payload;
679 break;
680 case AUTHENTICATION:
681 auth_request = (auth_payload_t*)payload;
682 break;
683 case CERTIFICATE_REQUEST:
684 certreq_request = (certreq_payload_t*)payload;
685 add_certificate_request(certreq_request, requested_ca_keyids);
686 break;
687 case CERTIFICATE:
688 cert_request = (cert_payload_t*)payload;
689 break;
690 case SECURITY_ASSOCIATION:
691 sa_request = (sa_payload_t*)payload;
692 break;
693 case TRAFFIC_SELECTOR_INITIATOR:
694 tsi_request = (ts_payload_t*)payload;
695 break;
696 case TRAFFIC_SELECTOR_RESPONDER:
697 tsr_request = (ts_payload_t*)payload;
698 break;
699 case NOTIFY:
700 {
701 status = process_notifies(this, (notify_payload_t*)payload);
702 if (status == FAILED)
703 {
704 payloads->destroy(payloads);
705 requested_ca_keyids->destroy(requested_ca_keyids);
706 /* we return SUCCESS, returned FAILED means do next transaction */
707 return SUCCESS;
708 }
709 if (status == DESTROY_ME)
710 {
711 payloads->destroy(payloads);
712 requested_ca_keyids->destroy(requested_ca_keyids);
713 SIG(CHILD_UP_FAILED, "initiating CHILD_SA failed, unable to create IKE_SA");
714 return DESTROY_ME;
715 }
716 break;
717 }
718 default:
719 {
720 DBG1(DBG_IKE, "ignoring %N payload",
721 payload_type_names, payload->get_type(payload));
722 break;
723 }
724 }
725 }
726 payloads->destroy(payloads);
727
728 /* check if we have all payloads */
729 if (!(idi_request && auth_request && sa_request && tsi_request && tsr_request))
730 {
731 build_notify(INVALID_SYNTAX, response, TRUE);
732 SIG(IKE_UP_FAILED, "request message incomplete, deleting IKE_SA");
733 SIG(CHILD_UP_FAILED, "initiating CHILD_SA failed, unable to create IKE_SA");
734 requested_ca_keyids->destroy(requested_ca_keyids);
735 return DESTROY_ME;
736 }
737
738 { /* process ID payload */
739 other_id = idi_request->get_identification(idi_request);
740 if (idr_request)
741 {
742 my_id = idr_request->get_identification(idr_request);
743 }
744 else
745 {
746 my_id = identification_create_from_encoding(ID_ANY, chunk_empty);
747 }
748 }
749
750
751 { /* get a policy and process traffic selectors */
752 linked_list_t *my_ts, *other_ts;
753
754 my_ts = tsr_request->get_traffic_selectors(tsr_request);
755 other_ts = tsi_request->get_traffic_selectors(tsi_request);
756
757 this->policy = charon->policies->get_policy(charon->policies,
758 my_id, other_id,
759 my_ts, other_ts,
760 me, other,
761 requested_ca_keyids);
762 requested_ca_keyids->destroy(requested_ca_keyids);
763
764 if (this->policy)
765 {
766 this->tsr = this->policy->select_my_traffic_selectors(this->policy, my_ts, me);
767 this->tsi = this->policy->select_other_traffic_selectors(this->policy, other_ts, other);
768 }
769 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
770 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
771
772 /* TODO: We should check somehow if we have a policy, but with other
773 * traffic selectors. Then we would create a IKE_SA without a CHILD_SA. */
774 if (this->policy == NULL)
775 {
776 SIG(IKE_UP_FAILED, "no acceptable policy for IDs %D - %D found, "
777 "deleting IKE_SA", my_id, other_id);
778 SIG(CHILD_UP_FAILED, "initiating CHILD_SA failed, unable to create IKE_SA");
779 my_id->destroy(my_id);
780 other_id->destroy(other_id);
781 build_notify(AUTHENTICATION_FAILED, response, TRUE);
782 return DESTROY_ME;
783 }
784 my_id->destroy(my_id);
785
786 /* get my id from policy, which must contain a fully qualified valid id */
787 my_id = this->policy->get_my_id(this->policy);
788 this->ike_sa->set_my_id(this->ike_sa, my_id->clone(my_id));
789 this->ike_sa->set_other_id(this->ike_sa, other_id);
790
791 idr_response = id_payload_create_from_identification(FALSE, my_id);
792 response->add_payload(response, (payload_t*)idr_response);
793 }
794
795 if (this->policy->get_auth_method(this->policy) == AUTH_RSA
796 && this->connection->get_cert_policy(this->connection) != CERT_NEVER_SEND)
797 { /* build certificate payload */
798 x509_t *cert;
799 cert_payload_t *cert_payload;
800
801 cert = charon->credentials->get_certificate(charon->credentials, my_id);
802 if (cert)
803 {
804 cert_payload = cert_payload_create_from_x509(cert);
805 response->add_payload(response, (payload_t *)cert_payload);
806 }
807 else
808 {
809 DBG1(DBG_IKE, "could not find my certificate, cert payload omitted");
810 }
811 }
812
813 if (cert_request)
814 { /* process certificate payload */
815 import_certificate(cert_request);
816 }
817
818 { /* process auth payload */
819 authenticator_t *authenticator;
820 auth_payload_t *auth_response;
821 auth_method_t auth_method;
822 status_t status;
823
824 auth_method = auth_request->get_auth_method(auth_request);
825 authenticator = authenticator_create(this->ike_sa, auth_method);
826 if (authenticator == NULL)
827 {
828 SIG(IKE_UP_FAILED, "auth method %N not supported, deleting IKE_SA",
829 auth_method_names, auth_method);
830 SIG(CHILD_UP_FAILED, "initiating CHILD_SA failed, unable to create IKE_SA");
831 return DESTROY_ME;
832 }
833 status = authenticator->verify(authenticator, this->init_request,
834 this->nonce_r, auth_request);
835 authenticator->destroy(authenticator);
836 if (status != SUCCESS)
837 {
838 SIG(IKE_UP_FAILED, "authentication failed, deleting IKE_SA");
839 SIG(CHILD_UP_FAILED, "initiating CHILD_SA failed, unable to create IKE_SA");
840 build_notify(AUTHENTICATION_FAILED, response, TRUE);
841 return DESTROY_ME;
842 }
843
844 auth_method = this->policy->get_auth_method(this->policy);
845 authenticator = authenticator_create(this->ike_sa, auth_method);
846 if (authenticator == NULL)
847 {
848 SIG(IKE_UP_FAILED, "auth method %N not supported, deleting IKE_SA",
849 auth_method_names, auth_method);
850 SIG(CHILD_UP_FAILED, "initiating CHILD_SA failed, unable to create IKE_SA");
851 return DESTROY_ME;
852 }
853 status = authenticator->build(authenticator, this->init_response,
854 this->nonce_i, &auth_response);
855 authenticator->destroy(authenticator);
856 if (status != SUCCESS)
857 {
858 SIG(IKE_UP_FAILED, "authentication data generation failed, deleting IKE_SA");
859 SIG(CHILD_UP_FAILED, "initiating CHILD_SA failed, unable to create IKE_SA");
860 build_notify(AUTHENTICATION_FAILED, response, TRUE);
861 return DESTROY_ME;
862 }
863 response->add_payload(response, (payload_t*)auth_response);
864 }
865
866 SIG(IKE_UP_SUCCESS, "IKE_SA '%s' established between %H[%D]...%H[%D]",
867 this->ike_sa->get_name(this->ike_sa), me, my_id, other, other_id);
868
869 { /* process SA payload */
870 linked_list_t *proposal_list;
871 sa_payload_t *sa_response;
872 ts_payload_t *ts_response;
873 bool use_natt;
874 u_int32_t soft_lifetime, hard_lifetime;
875
876 /* prepare reply */
877 sa_response = sa_payload_create();
878
879 /* get proposals from request, and select one with ours */
880 proposal_list = sa_request->get_proposals(sa_request);
881 DBG2(DBG_IKE, "selecting proposals:");
882 this->proposal = this->policy->select_proposal(this->policy, proposal_list);
883 proposal_list->destroy_offset(proposal_list, offsetof(proposal_t, destroy));
884
885 /* do we have a proposal? */
886 if (this->proposal == NULL)
887 {
888 SIG(CHILD_UP_FAILED, "CHILD_SA proposals unacceptable, no CHILD_SA created");
889 DBG1(DBG_IKE, "adding NO_PROPOSAL_CHOSEN notify to response");
890 build_notify(NO_PROPOSAL_CHOSEN, response, FALSE);
891 }
892 /* do we have traffic selectors? */
893 else if (this->tsi->get_count(this->tsi) == 0 || this->tsr->get_count(this->tsr) == 0)
894 {
895 SIG(CHILD_UP_FAILED, "CHILD_SA traffic selectors unacceptable, no CHILD_SA created");
896 DBG1(DBG_IKE, "adding TS_UNACCEPTABLE notify to response");
897 build_notify(TS_UNACCEPTABLE, response, FALSE);
898 }
899 else
900 {
901 /* create child sa */
902 soft_lifetime = this->policy->get_soft_lifetime(this->policy);
903 hard_lifetime = this->policy->get_hard_lifetime(this->policy);
904 use_natt = this->ike_sa->is_natt_enabled(this->ike_sa);
905 this->child_sa = child_sa_create(this->reqid, me, other, my_id, other_id,
906 soft_lifetime, hard_lifetime,
907 this->policy->get_updown(this->policy),
908 this->policy->get_hostaccess(this->policy),
909 use_natt);
910 this->child_sa->set_name(this->child_sa, this->policy->get_name(this->policy));
911
912 /* check mode, and include notify into reply */
913 switch (this->mode)
914 {
915 case MODE_TUNNEL:
916 /* is the default */
917 break;
918 case MODE_TRANSPORT:
919 if (!ts_list_is_host(this->tsi, other) ||
920 !ts_list_is_host(this->tsr, me))
921 {
922 this->mode = MODE_TUNNEL;
923 DBG1(DBG_IKE, "not using tranport mode, not host-to-host");
924 }
925 else if (this->ike_sa->is_natt_enabled(this->ike_sa))
926 {
927 this->mode = MODE_TUNNEL;
928 DBG1(DBG_IKE, "not using tranport mode, as connection NATed");
929 }
930 else
931 {
932 build_notify(USE_TRANSPORT_MODE, response, FALSE);
933 }
934 break;
935 case MODE_BEET:
936 if (!ts_list_is_host(this->tsi, NULL) ||
937 !ts_list_is_host(this->tsr, NULL))
938 {
939 this->mode = MODE_TUNNEL;
940 DBG1(DBG_IKE, "not using BEET mode, not host-to-host");
941 }
942 else
943 {
944 build_notify(USE_BEET_MODE, response, FALSE);
945 }
946 break;
947 }
948
949 if (install_child_sa(this, FALSE) != SUCCESS)
950 {
951 SIG(CHILD_UP_FAILED, "installing CHILD_SA failed, no CHILD_SA created");
952 DBG1(DBG_IKE, "adding NO_PROPOSAL_CHOSEN notify to response");
953 build_notify(NO_PROPOSAL_CHOSEN, response, FALSE);
954 }
955 else
956 {
957 /* add proposal to sa payload */
958 sa_response->add_proposal(sa_response, this->proposal);
959 SIG(CHILD_UP_SUCCESS, "CHILD_SA created");
960 }
961 }
962 response->add_payload(response, (payload_t*)sa_response);
963
964 /* add ts payload after sa payload */
965 ts_response = ts_payload_create_from_traffic_selectors(TRUE, this->tsi);
966 response->add_payload(response, (payload_t*)ts_response);
967 ts_response = ts_payload_create_from_traffic_selectors(FALSE, this->tsr);
968 response->add_payload(response, (payload_t*)ts_response);
969 }
970 /* set established state */
971 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
972 return SUCCESS;
973 }
974
975
976 /**
977 * Implementation of transaction_t.conclude
978 */
979 static status_t conclude(private_ike_auth_t *this, message_t *response,
980 transaction_t **transaction)
981 {
982 iterator_t *payloads;
983 payload_t *payload;
984 host_t *me, *other;
985 identification_t *other_id, *my_id;
986 ts_payload_t *tsi_payload = NULL;
987 ts_payload_t *tsr_payload = NULL;
988 id_payload_t *idr_payload = NULL;
989 cert_payload_t *cert_payload = NULL;
990 auth_payload_t *auth_payload = NULL;
991 sa_payload_t *sa_payload = NULL;
992 status_t status;
993
994 /* check message type */
995 if (response->get_exchange_type(response) != IKE_AUTH)
996 {
997 SIG(IKE_UP_FAILED, "IKE_AUTH response of invalid type, deleting IKE_SA");
998 SIG(CHILD_UP_FAILED, "initiating CHILD_SA failed, unable to create IKE_SA");
999 return DESTROY_ME;
1000 }
1001
1002 me = this->ike_sa->get_my_host(this->ike_sa);
1003 other = this->ike_sa->get_other_host(this->ike_sa);
1004
1005 /* Iterate over all payloads to collect them */
1006 payloads = response->get_payload_iterator(response);
1007 while (payloads->iterate(payloads, (void**)&payload))
1008 {
1009 switch (payload->get_type(payload))
1010 {
1011 case ID_RESPONDER:
1012 idr_payload = (id_payload_t*)payload;
1013 break;
1014 case AUTHENTICATION:
1015 auth_payload = (auth_payload_t*)payload;
1016 break;
1017 case CERTIFICATE:
1018 cert_payload = (cert_payload_t*)payload;
1019 break;
1020 case SECURITY_ASSOCIATION:
1021 sa_payload = (sa_payload_t*)payload;
1022 break;
1023 case TRAFFIC_SELECTOR_INITIATOR:
1024 tsi_payload = (ts_payload_t*)payload;
1025 break;
1026 case TRAFFIC_SELECTOR_RESPONDER:
1027 tsr_payload = (ts_payload_t*)payload;
1028 break;
1029 case NOTIFY:
1030 {
1031 status = process_notifies(this, (notify_payload_t*)payload);
1032 if (status == FAILED)
1033 {
1034 payloads->destroy(payloads);
1035 /* we return SUCCESS, as transaction completet */
1036 return SUCCESS;
1037 }
1038 if (status == DESTROY_ME)
1039 {
1040 SIG(CHILD_UP_FAILED, "initiating CHILD_SA failed, unable to create IKE_SA");
1041 payloads->destroy(payloads);
1042 return status;
1043 }
1044 break;
1045 }
1046 default:
1047 {
1048 DBG1(DBG_IKE, "ignoring payload %N",
1049 payload_type_names, payload->get_type(payload));
1050 break;
1051 }
1052 }
1053 }
1054 payloads->destroy(payloads);
1055
1056 if (!(idr_payload && auth_payload && sa_payload && tsi_payload && tsr_payload))
1057 {
1058 SIG(IKE_UP_FAILED, "response message incomplete, deleting IKE_SA");
1059 SIG(CHILD_UP_FAILED, "initiating CHILD_SA failed, unable to create IKE_SA");
1060 return DESTROY_ME;
1061 }
1062
1063 { /* process idr payload */
1064 identification_t *configured_other_id;
1065 int wildcards;
1066
1067 other_id = idr_payload->get_identification(idr_payload);
1068 configured_other_id = this->policy->get_other_id(this->policy);
1069
1070 if (!other_id->matches(other_id, configured_other_id, &wildcards))
1071 {
1072 other_id->destroy(other_id);
1073 SIG(IKE_UP_FAILED, "other peer uses unacceptable ID (%D, excepted "
1074 "%D), deleting IKE_SA", other_id, configured_other_id);
1075 SIG(CHILD_UP_FAILED, "initiating CHILD_SA failed, unable to create IKE_SA");
1076 return DESTROY_ME;
1077 }
1078 /* update other ID. It was already set, but may contain wildcards */
1079 this->ike_sa->set_other_id(this->ike_sa, other_id);
1080 }
1081
1082 if (cert_payload)
1083 { /* process cert payload */
1084 import_certificate(cert_payload);
1085 }
1086
1087 { /* authenticate peer */
1088 authenticator_t *authenticator;
1089 auth_method_t auth_method;
1090 status_t status;
1091
1092 my_id = this->policy->get_my_id(this->policy);
1093 auth_method = auth_payload->get_auth_method(auth_payload);
1094 authenticator = authenticator_create(this->ike_sa, auth_method);
1095 if (authenticator == NULL)
1096 {
1097 SIG(IKE_UP_FAILED, "auth method %N not supported, deleting IKE_SA",
1098 auth_method_names, auth_method);
1099 SIG(CHILD_UP_FAILED, "initiating CHILD_SA failed, unable to create IKE_SA");
1100 return DESTROY_ME;
1101 }
1102 status = authenticator->verify(authenticator, this->init_response,
1103 this->nonce_i, auth_payload);
1104 authenticator->destroy(authenticator);
1105 if (status != SUCCESS)
1106 {
1107 SIG(IKE_UP_FAILED, "authentication of '%D' with %N failed, "
1108 "deleting IKE_SA", other_id, auth_method_names, auth_method);
1109 SIG(CHILD_UP_FAILED, "initiating CHILD_SA failed, unable to create IKE_SA");
1110 return DESTROY_ME;
1111 }
1112 }
1113
1114 SIG(IKE_UP_SUCCESS, "IKE_SA '%s' established between %H[%D]...%H[%D]",
1115 this->ike_sa->get_name(this->ike_sa), me, my_id, other, other_id);
1116
1117 { /* process traffic selectors for us */
1118 linked_list_t *ts_received = tsi_payload->get_traffic_selectors(tsi_payload);
1119 this->tsi = this->policy->select_my_traffic_selectors(this->policy, ts_received, me);
1120 ts_received->destroy_offset(ts_received, offsetof(traffic_selector_t, destroy));
1121 }
1122
1123 { /* process traffic selectors for other */
1124 linked_list_t *ts_received = tsr_payload->get_traffic_selectors(tsr_payload);
1125 this->tsr = this->policy->select_other_traffic_selectors(this->policy, ts_received, other);
1126 ts_received->destroy_offset(ts_received, offsetof(traffic_selector_t, destroy));
1127 }
1128
1129 { /* process sa payload */
1130 linked_list_t *proposal_list;
1131
1132 proposal_list = sa_payload->get_proposals(sa_payload);
1133 /* we have to re-check here if other's selection is valid */
1134 this->proposal = this->policy->select_proposal(this->policy, proposal_list);
1135 proposal_list->destroy_offset(proposal_list, offsetof(proposal_t, destroy));
1136
1137 /* everything fine to create CHILD? */
1138 if (this->proposal == NULL ||
1139 this->tsi->get_count(this->tsi) == 0 ||
1140 this->tsr->get_count(this->tsr) == 0 ||
1141 !this->build_child)
1142 {
1143 SIG(CHILD_UP_FAILED, "CHILD_SA negotiation failed, no CHILD_SA built");
1144 }
1145 else
1146 {
1147 /* check mode if it is acceptable */
1148 switch (this->mode)
1149 {
1150 case MODE_TUNNEL:
1151 /* is the default */
1152 break;
1153 case MODE_TRANSPORT:
1154 /* TODO: we should close the CHILD_SA if negotiated
1155 * mode is not acceptable for us */
1156 if (!ts_list_is_host(this->tsi, me) ||
1157 !ts_list_is_host(this->tsr, other))
1158 {
1159 this->mode = MODE_TUNNEL;
1160 DBG1(DBG_IKE, "not using tranport mode, not host-to-host");
1161 }
1162 else if (this->ike_sa->is_natt_enabled(this->ike_sa))
1163 {
1164 this->mode = MODE_TUNNEL;
1165 DBG1(DBG_IKE, "not using tranport mode, as connection NATed");
1166 }
1167 break;
1168 case MODE_BEET:
1169 if (!ts_list_is_host(this->tsi, NULL) ||
1170 !ts_list_is_host(this->tsr, NULL))
1171 {
1172 this->mode = MODE_TUNNEL;
1173 DBG1(DBG_IKE, "not using BEET mode, not host-to-host");
1174 }
1175 break;
1176 }
1177
1178 if (install_child_sa(this, TRUE) != SUCCESS)
1179 {
1180 SIG(CHILD_UP_FAILED, "installing CHILD_SA failed, no CHILD_SA built");
1181 /* TODO: we should send a DELETE for that CHILD to stay
1182 * synchronous with the peer */
1183 }
1184 else
1185 {
1186 SIG(CHILD_UP_SUCCESS, "CHILD_SA created");
1187 }
1188 }
1189 }
1190 /* set new state */
1191 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
1192 return SUCCESS;
1193 }
1194
1195 /**
1196 * implements transaction_t.destroy
1197 */
1198 static void destroy(private_ike_auth_t *this)
1199 {
1200 DESTROY_IF(this->message);
1201 DESTROY_IF(this->proposal);
1202 DESTROY_IF(this->child_sa);
1203 DESTROY_IF(this->policy);
1204 DESTROY_IF(this->connection);
1205 if (this->tsi)
1206 {
1207 this->tsi->destroy_offset(this->tsi, offsetof(traffic_selector_t, destroy));
1208 }
1209 if (this->tsr)
1210 {
1211 this->tsr->destroy_offset(this->tsr, offsetof(traffic_selector_t, destroy));
1212 }
1213 chunk_free(&this->nonce_i);
1214 chunk_free(&this->nonce_r);
1215 chunk_free(&this->init_request);
1216 chunk_free(&this->init_response);
1217 free(this);
1218 }
1219
1220 /*
1221 * Described in header.
1222 */
1223 ike_auth_t *ike_auth_create(ike_sa_t *ike_sa)
1224 {
1225 private_ike_auth_t *this = malloc_thing(private_ike_auth_t);
1226
1227 /* transaction interface functions */
1228 this->public.transaction.get_request = (status_t(*)(transaction_t*,message_t**))get_request;
1229 this->public.transaction.get_response = (status_t(*)(transaction_t*,message_t*,message_t**,transaction_t**))get_response;
1230 this->public.transaction.conclude = (status_t(*)(transaction_t*,message_t*,transaction_t**))conclude;
1231 this->public.transaction.get_message_id = (u_int32_t(*)(transaction_t*))get_message_id;
1232 this->public.transaction.requested = (u_int32_t(*)(transaction_t*))requested;
1233 this->public.transaction.destroy = (void(*)(transaction_t*))destroy;
1234
1235 /* public functions */
1236 this->public.set_config = (void(*)(ike_auth_t*,connection_t*,policy_t*))set_config;
1237 this->public.set_reqid = (void(*)(ike_auth_t*,u_int32_t))set_reqid;
1238 this->public.set_nonces = (void(*)(ike_auth_t*,chunk_t,chunk_t))set_nonces;
1239 this->public.set_init_messages = (void(*)(ike_auth_t*,chunk_t,chunk_t))set_init_messages;
1240
1241 /* private data */
1242 this->ike_sa = ike_sa;
1243 this->message_id = 0;
1244 this->message = NULL;
1245 this->requested = 0;
1246 this->nonce_i = chunk_empty;
1247 this->nonce_r = chunk_empty;
1248 this->init_request = chunk_empty;
1249 this->init_response = chunk_empty;
1250 this->child_sa = NULL;
1251 this->proposal = NULL;
1252 this->tsi = NULL;
1253 this->tsr = NULL;
1254 this->build_child = TRUE;
1255 this->reqid = 0;
1256 this->mode = MODE_TUNNEL;
1257
1258 return &this->public;
1259 }