support of certreq payload in IKE_AUTH messages
[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 /**
134 * Implementation of transaction_t.get_message_id.
135 */
136 static u_int32_t get_message_id(private_ike_auth_t *this)
137 {
138 return this->message_id;
139 }
140
141 /**
142 * Implementation of transaction_t.requested.
143 */
144 static u_int32_t requested(private_ike_auth_t *this)
145 {
146 return this->requested++;
147 }
148
149 /**
150 * Implementation of transaction_t.set_config.
151 */
152 static void set_config(private_ike_auth_t *this,
153 connection_t *connection, policy_t *policy)
154 {
155 this->connection = connection;
156 this->policy = policy;
157 }
158
159 /**
160 * Implementation of transaction_t.set_reqid.
161 */
162 static void set_reqid(private_ike_auth_t *this, u_int32_t reqid)
163 {
164 this->reqid = reqid;
165 }
166
167 /**
168 * Implementation of transaction_t.set_nonces.
169 */
170 static void set_nonces(private_ike_auth_t *this, chunk_t nonce_i, chunk_t nonce_r)
171 {
172 this->nonce_i = nonce_i;
173 this->nonce_r = nonce_r;
174 }
175
176 /**
177 * Implementation of transaction_t.set_init_messages.
178 */
179 static void set_init_messages(private_ike_auth_t *this, chunk_t init_request, chunk_t init_response)
180 {
181 this->init_request = init_request;
182 this->init_response = init_response;
183 }
184
185 /**
186 * Implementation of transaction_t.get_request.
187 */
188 static status_t get_request(private_ike_auth_t *this, message_t **result)
189 {
190 message_t *request;
191 host_t *me, *other;
192 identification_t *my_id, *other_id;
193 id_payload_t *my_id_payload;
194
195 /* check if we already have built a message (retransmission) */
196 if (this->message)
197 {
198 *result = this->message;
199 return SUCCESS;
200 }
201
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);
206
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 */
215 *result = request;
216 /* store for retransmission */
217 this->message = request;
218
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);
222 }
223
224 /* build certificate request payload */
225 if (this->connection->get_certreq_policy(this->connection) != CERT_NEVER_SEND)
226 {
227 certreq_payload_t *certreq_payload;
228 identification_t *other_ca = this->policy->get_other_ca(this->policy);
229
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);
233
234 if (certreq_payload != NULL)
235 {
236 request->add_payload(request, (payload_t*)certreq_payload);
237 }
238 }
239
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)
243 {
244 cert_payload_t *cert_payload;
245
246 x509_t *cert = charon->credentials->get_certificate(charon->credentials, my_id);
247
248 if (cert)
249 {
250 cert_payload = cert_payload_create_from_x509(cert);
251 request->add_payload(request, (payload_t*)cert_payload);
252 }
253 else
254 {
255 DBG1(DBG_IKE, "could not find my certificate, certificate payload omitted");
256 }
257 }
258
259 { /* build IDr payload, if other_id defined */
260 id_payload_t *id_payload;
261 if (!other_id->contains_wildcards(other_id))
262 {
263 id_payload = id_payload_create_from_identification(FALSE, other_id);
264 request->add_payload(request, (payload_t*)id_payload);
265 }
266 }
267
268 { /* build auth payload */
269 authenticator_t *authenticator;
270 auth_payload_t *auth_payload;
271 auth_method_t auth_method;
272 status_t status;
273
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,
277 &auth_payload,
278 this->init_request,
279 this->nonce_r,
280 my_id,
281 other_id,
282 TRUE);
283 authenticator->destroy(authenticator);
284 if (status != SUCCESS)
285 {
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");
288 return DESTROY_ME;
289 }
290 request->add_payload(request, (payload_t*)auth_payload);
291 }
292
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;
297 bool enable_natt;
298
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),
307 enable_natt);
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)
310 {
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");
313 return DESTROY_ME;
314 }
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);
318 }
319
320 { /* build TSi payload */
321 linked_list_t *ts_list;
322 ts_payload_t *ts_payload;
323
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));
327
328 request->add_payload(request, (payload_t*)ts_payload);
329 }
330
331 { /* build TSr payload */
332 linked_list_t *ts_list;
333 ts_payload_t *ts_payload;
334
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));
338
339 request->add_payload(request, (payload_t*)ts_payload);
340 }
341
342 this->message_id = this->ike_sa->get_next_message_id(this->ike_sa);
343 request->set_message_id(request, this->message_id);
344 return SUCCESS;
345 }
346
347 /**
348 * Handle all kind of notifies
349 */
350 static status_t process_notifies(private_ike_auth_t *this, notify_payload_t *notify_payload)
351 {
352 notify_type_t notify_type = notify_payload->get_notify_type(notify_payload);
353
354 DBG2(DBG_IKE, "process notify type %N", notify_type_names, notify_type);
355
356 switch (notify_type)
357 {
358 /* these notifies are not critical. no child_sa is built, but IKE stays alive */
359 case SINGLE_PAIR_REQUIRED:
360 {
361 SIG(CHILD_UP_FAILED, "received a SINGLE_PAIR_REQUIRED notify");
362 this->build_child = FALSE;
363 return SUCCESS;
364 }
365 case TS_UNACCEPTABLE:
366 {
367 SIG(CHILD_UP_FAILED, "received TS_UNACCEPTABLE notify");
368 this->build_child = FALSE;
369 return SUCCESS;
370 }
371 case NO_PROPOSAL_CHOSEN:
372 {
373 SIG(CHILD_UP_FAILED, "received NO_PROPOSAL_CHOSEN notify");
374 this->build_child = FALSE;
375 return SUCCESS;
376 }
377 default:
378 {
379 if (notify_type < 16383)
380 {
381 SIG(IKE_UP_FAILED, "received %N notify error, deleting IKE_SA",
382 notify_type_names, notify_type);
383 return DESTROY_ME;
384 }
385 else
386 {
387 DBG1(DBG_IKE, "received %N notify, ignored",
388 notify_type_names, notify_type);
389 return SUCCESS;
390 }
391 }
392 }
393 }
394
395 /**
396 * Build a notify message.
397 */
398 static void build_notify(notify_type_t type, message_t *message, bool flush_message)
399 {
400 notify_payload_t *notify;
401
402 if (flush_message)
403 {
404 payload_t *payload;
405 iterator_t *iterator = message->get_payload_iterator(message);
406 while (iterator->iterate(iterator, (void**)&payload))
407 {
408 payload->destroy(payload);
409 iterator->remove(iterator);
410 }
411 iterator->destroy(iterator);
412 }
413
414 notify = notify_payload_create();
415 notify->set_notify_type(notify, type);
416 message->add_payload(message, (payload_t*)notify);
417 }
418
419 /**
420 * Import certificate requests from a certreq payload
421 */
422 static void import_certificate_request(certreq_payload_t *certreq_payload)
423 {
424 chunk_t keyids;
425 cert_encoding_t encoding = certreq_payload->get_cert_encoding(certreq_payload);
426
427 if (encoding != CERT_X509_SIGNATURE)
428 {
429 DBG1(DBG_IKE, "certreq payload %N not supported, ignored",
430 cert_encoding_names, encoding);
431 return;
432 }
433
434 keyids = certreq_payload->get_data(certreq_payload);
435
436 while (keyids.len >= HASH_SIZE_SHA1)
437 {
438 chunk_t keyid = { keyids.ptr, HASH_SIZE_SHA1};
439 x509_t *cacert = charon->credentials->get_ca_certificate_by_keyid(charon->credentials, keyid);
440
441 if (cacert)
442 {
443 DBG1(DBG_IKE, "request for certificate issued by ca '%D'", cacert->get_subject(cacert));
444 DBG2(DBG_IKE, " with keyid %#B", &keyid);
445 }
446 else
447 {
448 DBG1(DBG_IKE, "request for certificate issued by unknown ca");
449 DBG1(DBG_IKE, " with keyid %#B", &keyid);
450 }
451 keyids.ptr += HASH_SIZE_SHA1;
452 keyids.len -= HASH_SIZE_SHA1;
453 }
454 }
455
456 /**
457 * Import a certificate from a cert payload
458 */
459 static void import_certificate(cert_payload_t *cert_payload)
460 {
461 bool found;
462 x509_t *cert;
463 cert_encoding_t encoding;
464
465 encoding = cert_payload->get_cert_encoding(cert_payload);
466 if (encoding != CERT_X509_SIGNATURE)
467 {
468 DBG1(DBG_IKE, "certificate payload %N not supported, ignored",
469 cert_encoding_names, encoding);
470 return;
471 }
472 cert = x509_create_from_chunk(cert_payload->get_data_clone(cert_payload));
473 if (cert)
474 {
475 if (charon->credentials->verify(charon->credentials, cert, &found))
476 {
477 DBG2(DBG_IKE, "received end entity certificate is trusted, added to store");
478 if (!found)
479 {
480 charon->credentials->add_end_certificate(charon->credentials, cert);
481 }
482 else
483 {
484 cert->destroy(cert);
485 }
486 }
487 else
488 {
489 DBG1(DBG_IKE, "received end entity certificate is not trusted, discarded");
490 cert->destroy(cert);
491 }
492 }
493 else
494 {
495 DBG1(DBG_IKE, "parsing of received certificate failed, discarded");
496 }
497 }
498
499 /**
500 * Install a CHILD_SA for usage
501 */
502 static status_t install_child_sa(private_ike_auth_t *this, bool initiator)
503 {
504 prf_plus_t *prf_plus;
505 chunk_t seed;
506 status_t status;
507
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);
512 chunk_free(&seed);
513
514 if (initiator)
515 {
516 status = this->child_sa->update(this->child_sa, this->proposal, prf_plus);
517 }
518 else
519 {
520 status = this->child_sa->add(this->child_sa, this->proposal, prf_plus);
521 }
522 prf_plus->destroy(prf_plus);
523 if (status != SUCCESS)
524 {
525 return DESTROY_ME;
526 }
527 if (initiator)
528 {
529 status = this->child_sa->add_policies(this->child_sa, this->tsi, this->tsr);
530 }
531 else
532 {
533 status = this->child_sa->add_policies(this->child_sa, this->tsr, this->tsi);
534 }
535 if (status != SUCCESS)
536 {
537 return DESTROY_ME;
538 }
539
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;
544 return SUCCESS;
545 }
546
547 /**
548 * Implementation of transaction_t.get_response.
549 */
550 static status_t get_response(private_ike_auth_t *this, message_t *request,
551 message_t **result, transaction_t **next)
552 {
553 host_t *me, *other;
554 identification_t *my_id, *other_id;
555 message_t *response;
556 status_t status;
557 iterator_t *payloads;
558 payload_t *payload;
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;
568
569 /* check if we already have built a response (retransmission) */
570 if (this->message)
571 {
572 *result = this->message;
573 return SUCCESS;
574 }
575
576 SIG(CHILD_UP_START, "setting up CHILD_SA along with IKE_AUTH");
577
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);
581
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;
591 *result = response;
592
593 /* check message type */
594 if (request->get_exchange_type(request) != IKE_AUTH)
595 {
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");
598 return DESTROY_ME;
599 }
600
601 /* Iterate over all payloads. */
602 payloads = request->get_payload_iterator(request);
603 while (payloads->iterate(payloads, (void**)&payload))
604 {
605 switch (payload->get_type(payload))
606 {
607 case ID_INITIATOR:
608 idi_request = (id_payload_t*)payload;
609 break;
610 case ID_RESPONDER:
611 idr_request = (id_payload_t*)payload;
612 break;
613 case AUTHENTICATION:
614 auth_request = (auth_payload_t*)payload;
615 break;
616 case CERTIFICATE_REQUEST:
617 certreq_request = (certreq_payload_t*)payload;
618 break;
619 case CERTIFICATE:
620 cert_request = (cert_payload_t*)payload;
621 break;
622 case SECURITY_ASSOCIATION:
623 sa_request = (sa_payload_t*)payload;
624 break;
625 case TRAFFIC_SELECTOR_INITIATOR:
626 tsi_request = (ts_payload_t*)payload;
627 break;
628 case TRAFFIC_SELECTOR_RESPONDER:
629 tsr_request = (ts_payload_t*)payload;
630 break;
631 case NOTIFY:
632 {
633 status = process_notifies(this, (notify_payload_t*)payload);
634 if (status == FAILED)
635 {
636 payloads->destroy(payloads);
637 /* we return SUCCESS, returned FAILED means do next transaction */
638 return SUCCESS;
639 }
640 if (status == DESTROY_ME)
641 {
642 payloads->destroy(payloads);
643 SIG(CHILD_UP_FAILED, "initiating CHILD_SA failed, unable to create IKE_SA");
644 return DESTROY_ME;
645 }
646 break;
647 }
648 default:
649 {
650 DBG1(DBG_IKE, "ignoring %N payload",
651 payload_type_names, payload->get_type(payload));
652 break;
653 }
654 }
655 }
656 payloads->destroy(payloads);
657
658 /* check if we have all payloads */
659 if (!(idi_request && auth_request && sa_request && tsi_request && tsr_request))
660 {
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");
664 return DESTROY_ME;
665 }
666
667 { /* process ID payload */
668 other_id = idi_request->get_identification(idi_request);
669 if (idr_request)
670 {
671 my_id = idr_request->get_identification(idr_request);
672 }
673 else
674 {
675 my_id = identification_create_from_encoding(ID_ANY, CHUNK_INITIALIZER);
676 }
677 }
678
679 { /* get a policy and process traffic selectors */
680 linked_list_t *my_ts, *other_ts;
681
682 my_ts = tsr_request->get_traffic_selectors(tsr_request);
683 other_ts = tsi_request->get_traffic_selectors(tsi_request);
684
685 this->policy = charon->policies->get_policy(charon->policies,
686 my_id, other_id,
687 my_ts, other_ts,
688 me, other);
689 if (this->policy)
690 {
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);
693 }
694 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
695 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
696
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)
700 {
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);
707 return DESTROY_ME;
708 }
709 my_id->destroy(my_id);
710
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);
715
716 idr_response = id_payload_create_from_identification(FALSE, my_id);
717 response->add_payload(response, (payload_t*)idr_response);
718 }
719
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 */
723 x509_t *cert;
724 cert_payload_t *cert_payload;
725
726 cert = charon->credentials->get_certificate(charon->credentials, my_id);
727 if (cert)
728 {
729 cert_payload = cert_payload_create_from_x509(cert);
730 response->add_payload(response, (payload_t *)cert_payload);
731 }
732 else
733 {
734 DBG1(DBG_IKE, "could not find my certificate, cert payload omitted");
735 }
736 }
737
738 if (certreq_request)
739 { /* process certificate request payload */
740 import_certificate_request(certreq_request);
741 }
742
743 if (cert_request)
744 { /* process certificate payload */
745 import_certificate(cert_request);
746 }
747
748 { /* process auth payload */
749 authenticator_t *authenticator;
750 auth_payload_t *auth_response;
751 auth_method_t auth_method;
752 status_t status;
753
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,
757 this->init_request,
758 this->nonce_r,
759 my_id,
760 other_id,
761 TRUE);
762 if (status != SUCCESS)
763 {
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);
768 return DESTROY_ME;
769 }
770 status = authenticator->compute_auth_data(authenticator, &auth_response,
771 this->init_response,
772 this->nonce_i,
773 my_id,
774 other_id,
775 FALSE);
776 authenticator->destroy(authenticator);
777 if (status != SUCCESS)
778 {
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);
782 return DESTROY_ME;
783 }
784 response->add_payload(response, (payload_t*)auth_response);
785 }
786
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);
789
790 { /* process SA payload */
791 linked_list_t *proposal_list;
792 sa_payload_t *sa_response;
793 ts_payload_t *ts_response;
794 bool use_natt;
795 u_int32_t soft_lifetime, hard_lifetime;
796
797 /* prepare reply */
798 sa_response = sa_payload_create();
799
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));
805
806 /* do we have a proposal? */
807 if (this->proposal == NULL)
808 {
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);
812 }
813 /* do we have traffic selectors? */
814 else if (this->tsi->get_count(this->tsi) == 0 || this->tsr->get_count(this->tsr) == 0)
815 {
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);
819 }
820 else
821 {
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),
830 use_natt);
831 this->child_sa->set_name(this->child_sa, this->policy->get_name(this->policy));
832 if (install_child_sa(this, FALSE) != SUCCESS)
833 {
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);
837 }
838 else
839 {
840 /* add proposal to sa payload */
841 sa_response->add_proposal(sa_response, this->proposal);
842 SIG(CHILD_UP_SUCCESS, "CHILD_SA created");
843 }
844 }
845 response->add_payload(response, (payload_t*)sa_response);
846
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);
852 }
853 /* set established state */
854 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
855 return SUCCESS;
856 }
857
858
859 /**
860 * Implementation of transaction_t.conclude
861 */
862 static status_t conclude(private_ike_auth_t *this, message_t *response,
863 transaction_t **transaction)
864 {
865 iterator_t *payloads;
866 payload_t *payload;
867 host_t *me, *other;
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;
875 status_t status;
876
877 /* check message type */
878 if (response->get_exchange_type(response) != IKE_AUTH)
879 {
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");
882 return DESTROY_ME;
883 }
884
885 me = this->ike_sa->get_my_host(this->ike_sa);
886 other = this->ike_sa->get_other_host(this->ike_sa);
887
888 /* Iterate over all payloads to collect them */
889 payloads = response->get_payload_iterator(response);
890 while (payloads->iterate(payloads, (void**)&payload))
891 {
892 switch (payload->get_type(payload))
893 {
894 case ID_RESPONDER:
895 idr_payload = (id_payload_t*)payload;
896 break;
897 case AUTHENTICATION:
898 auth_payload = (auth_payload_t*)payload;
899 break;
900 case CERTIFICATE:
901 cert_payload = (cert_payload_t*)payload;
902 break;
903 case SECURITY_ASSOCIATION:
904 sa_payload = (sa_payload_t*)payload;
905 break;
906 case TRAFFIC_SELECTOR_INITIATOR:
907 tsi_payload = (ts_payload_t*)payload;
908 break;
909 case TRAFFIC_SELECTOR_RESPONDER:
910 tsr_payload = (ts_payload_t*)payload;
911 break;
912 case NOTIFY:
913 {
914 status = process_notifies(this, (notify_payload_t*)payload);
915 if (status == FAILED)
916 {
917 payloads->destroy(payloads);
918 /* we return SUCCESS, as transaction completet */
919 return SUCCESS;
920 }
921 if (status == DESTROY_ME)
922 {
923 SIG(CHILD_UP_FAILED, "initiating CHILD_SA failed, unable to create IKE_SA");
924 payloads->destroy(payloads);
925 return status;
926 }
927 break;
928 }
929 default:
930 {
931 DBG1(DBG_IKE, "ignoring payload %N",
932 payload_type_names, payload->get_type(payload));
933 break;
934 }
935 }
936 }
937 payloads->destroy(payloads);
938
939 if (!(idr_payload && auth_payload && sa_payload && tsi_payload && tsr_payload))
940 {
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");
943 return DESTROY_ME;
944 }
945
946 { /* process idr payload */
947 identification_t *configured_other_id;
948 int wildcards;
949
950 other_id = idr_payload->get_identification(idr_payload);
951 configured_other_id = this->policy->get_other_id(this->policy);
952
953 if (!other_id->matches(other_id, configured_other_id, &wildcards))
954 {
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");
959 return DESTROY_ME;
960 }
961 /* update other ID. It was already set, but may contain wildcards */
962 this->ike_sa->set_other_id(this->ike_sa, other_id);
963 }
964
965 if (cert_payload)
966 { /* process cert payload */
967 import_certificate(cert_payload);
968 }
969
970 { /* authenticate peer */
971 authenticator_t *authenticator;
972 auth_method_t auth_method;
973 status_t status;
974
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);
978
979 status = authenticator->verify_auth_data(authenticator,
980 auth_payload,
981 this->init_response,
982 this->nonce_i,
983 my_id,
984 other_id,
985 FALSE);
986 authenticator->destroy(authenticator);
987 if (status != SUCCESS)
988 {
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");
992 return DESTROY_ME;
993 }
994 }
995
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);
998
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));
1003 }
1004
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));
1009 }
1010
1011 { /* process sa payload */
1012 linked_list_t *proposal_list;
1013
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));
1018
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 ||
1023 !this->build_child)
1024 {
1025 SIG(CHILD_UP_FAILED, "CHILD_SA negotiation failed, no CHILD_SA built");
1026 }
1027 else
1028 {
1029 if (install_child_sa(this, TRUE) != SUCCESS)
1030 {
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 */
1034 }
1035 else
1036 {
1037 SIG(CHILD_UP_SUCCESS, "CHILD_SA created");
1038 }
1039 }
1040 }
1041 /* set new state */
1042 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
1043 return SUCCESS;
1044 }
1045
1046 /**
1047 * implements transaction_t.destroy
1048 */
1049 static void destroy(private_ike_auth_t *this)
1050 {
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);
1056 if (this->tsi)
1057 {
1058 this->tsi->destroy_offset(this->tsi, offsetof(traffic_selector_t, destroy));
1059 }
1060 if (this->tsr)
1061 {
1062 this->tsr->destroy_offset(this->tsr, offsetof(traffic_selector_t, destroy));
1063 }
1064 chunk_free(&this->nonce_i);
1065 chunk_free(&this->nonce_r);
1066 chunk_free(&this->init_request);
1067 chunk_free(&this->init_response);
1068 free(this);
1069 }
1070
1071 /*
1072 * Described in header.
1073 */
1074 ike_auth_t *ike_auth_create(ike_sa_t *ike_sa)
1075 {
1076 private_ike_auth_t *this = malloc_thing(private_ike_auth_t);
1077
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;
1085
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;
1091
1092 /* private data */
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;
1103 this->tsi = NULL;
1104 this->tsr = NULL;
1105 this->build_child = TRUE;
1106 this->reqid = 0;
1107
1108 return &this->public;
1109 }