Moving charon to libcharon.
[strongswan.git] / src / libcharon / sa / tasks / ike_auth.c
1 /*
2 * Copyright (C) 2005-2009 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details
15 */
16
17 #include "ike_auth.h"
18
19 #include <string.h>
20
21 #include <daemon.h>
22 #include <encoding/payloads/id_payload.h>
23 #include <encoding/payloads/auth_payload.h>
24 #include <encoding/payloads/eap_payload.h>
25 #include <encoding/payloads/nonce_payload.h>
26 #include <sa/authenticators/eap_authenticator.h>
27
28 typedef struct private_ike_auth_t private_ike_auth_t;
29
30 /**
31 * Private members of a ike_auth_t task.
32 */
33 struct private_ike_auth_t {
34
35 /**
36 * Public methods and task_t interface.
37 */
38 ike_auth_t public;
39
40 /**
41 * Assigned IKE_SA.
42 */
43 ike_sa_t *ike_sa;
44
45 /**
46 * Are we the initiator?
47 */
48 bool initiator;
49
50 /**
51 * Nonce chosen by us in ike_init
52 */
53 chunk_t my_nonce;
54
55 /**
56 * Nonce chosen by peer in ike_init
57 */
58 chunk_t other_nonce;
59
60 /**
61 * IKE_SA_INIT message sent by us
62 */
63 packet_t *my_packet;
64
65 /**
66 * IKE_SA_INIT message sent by peer
67 */
68 packet_t *other_packet;
69
70 /**
71 * currently active authenticator, to authenticate us
72 */
73 authenticator_t *my_auth;
74
75 /**
76 * currently active authenticator, to authenticate peer
77 */
78 authenticator_t *other_auth;
79
80 /**
81 * peer_cfg candidates, ordered by priority
82 */
83 linked_list_t *candidates;
84
85 /**
86 * selected peer config (might change when using multiple authentications)
87 */
88 peer_cfg_t *peer_cfg;
89
90 /**
91 * have we planned an(other) authentication exchange?
92 */
93 bool do_another_auth;
94
95 /**
96 * has the peer announced another authentication exchange?
97 */
98 bool expect_another_auth;
99
100 /**
101 * should we send a AUTHENTICATION_FAILED notify?
102 */
103 bool authentication_failed;
104 };
105
106 /**
107 * check if multiple authentication extension is enabled, configuration-wise
108 */
109 static bool multiple_auth_enabled()
110 {
111 return lib->settings->get_bool(lib->settings,
112 "charon.multiple_authentication", TRUE);
113 }
114
115 /**
116 * collect the needed information in the IKE_SA_INIT exchange from our message
117 */
118 static status_t collect_my_init_data(private_ike_auth_t *this,
119 message_t *message)
120 {
121 nonce_payload_t *nonce;
122
123 /* get the nonce that was generated in ike_init */
124 nonce = (nonce_payload_t*)message->get_payload(message, NONCE);
125 if (nonce == NULL)
126 {
127 return FAILED;
128 }
129 this->my_nonce = nonce->get_nonce(nonce);
130
131 /* pre-generate the message, keep a copy */
132 if (this->ike_sa->generate_message(this->ike_sa, message,
133 &this->my_packet) != SUCCESS)
134 {
135 return FAILED;
136 }
137 return NEED_MORE;
138 }
139
140 /**
141 * collect the needed information in the IKE_SA_INIT exchange from others message
142 */
143 static status_t collect_other_init_data(private_ike_auth_t *this,
144 message_t *message)
145 {
146 /* we collect the needed information in the IKE_SA_INIT exchange */
147 nonce_payload_t *nonce;
148
149 /* get the nonce that was generated in ike_init */
150 nonce = (nonce_payload_t*)message->get_payload(message, NONCE);
151 if (nonce == NULL)
152 {
153 return FAILED;
154 }
155 this->other_nonce = nonce->get_nonce(nonce);
156
157 /* keep a copy of the received packet */
158 this->other_packet = message->get_packet(message);
159 return NEED_MORE;
160 }
161
162 /**
163 * Get the next authentication configuration
164 */
165 static auth_cfg_t *get_auth_cfg(private_ike_auth_t *this, bool local)
166 {
167 enumerator_t *e1, *e2;
168 auth_cfg_t *c1, *c2, *next = NULL;
169
170 /* find an available config not already done */
171 e1 = this->peer_cfg->create_auth_cfg_enumerator(this->peer_cfg, local);
172 while (e1->enumerate(e1, &c1))
173 {
174 bool found = FALSE;
175
176 e2 = this->ike_sa->create_auth_cfg_enumerator(this->ike_sa, local);
177 while (e2->enumerate(e2, &c2))
178 {
179 if (c2->complies(c2, c1, FALSE))
180 {
181 found = TRUE;
182 break;
183 }
184 }
185 e2->destroy(e2);
186 if (!found)
187 {
188 next = c1;
189 break;
190 }
191 }
192 e1->destroy(e1);
193 return next;
194 }
195
196 /**
197 * Check if we have should initiate another authentication round
198 */
199 static bool do_another_auth(private_ike_auth_t *this)
200 {
201 bool do_another = FALSE;
202 enumerator_t *done, *todo;
203 auth_cfg_t *done_cfg, *todo_cfg;
204
205 if (!this->ike_sa->supports_extension(this->ike_sa, EXT_MULTIPLE_AUTH))
206 {
207 return FALSE;
208 }
209
210 done = this->ike_sa->create_auth_cfg_enumerator(this->ike_sa, TRUE);
211 todo = this->peer_cfg->create_auth_cfg_enumerator(this->peer_cfg, TRUE);
212 while (todo->enumerate(todo, &todo_cfg))
213 {
214 if (!done->enumerate(done, &done_cfg))
215 {
216 done_cfg = this->ike_sa->get_auth_cfg(this->ike_sa, TRUE);
217 }
218 if (!done_cfg->complies(done_cfg, todo_cfg, FALSE))
219 {
220 do_another = TRUE;
221 break;
222 }
223 }
224 done->destroy(done);
225 todo->destroy(todo);
226 return do_another;
227 }
228
229 /**
230 * Get peer configuration candidates from backends
231 */
232 static bool load_cfg_candidates(private_ike_auth_t *this)
233 {
234 enumerator_t *enumerator;
235 peer_cfg_t *peer_cfg;
236 host_t *me, *other;
237 identification_t *my_id, *other_id;
238
239 me = this->ike_sa->get_my_host(this->ike_sa);
240 other = this->ike_sa->get_other_host(this->ike_sa);
241 my_id = this->ike_sa->get_my_id(this->ike_sa);
242 other_id = this->ike_sa->get_other_id(this->ike_sa);
243
244 enumerator = charon->backends->create_peer_cfg_enumerator(charon->backends,
245 me, other, my_id, other_id);
246 while (enumerator->enumerate(enumerator, &peer_cfg))
247 {
248 peer_cfg->get_ref(peer_cfg);
249 if (this->peer_cfg == NULL)
250 { /* best match */
251 this->peer_cfg = peer_cfg;
252 this->ike_sa->set_peer_cfg(this->ike_sa, peer_cfg);
253 }
254 else
255 {
256 this->candidates->insert_last(this->candidates, peer_cfg);
257 }
258 }
259 enumerator->destroy(enumerator);
260 if (this->peer_cfg)
261 {
262 DBG1(DBG_CFG, "selected peer config '%s'",
263 this->peer_cfg->get_name(this->peer_cfg));
264 return TRUE;
265 }
266 DBG1(DBG_CFG, "no matching peer config found");
267 return FALSE;
268 }
269
270 /**
271 * update the current peer candidate if necessary, using candidates
272 */
273 static bool update_cfg_candidates(private_ike_auth_t *this, bool strict)
274 {
275 do
276 {
277 if (this->peer_cfg)
278 {
279 bool complies = TRUE;
280 enumerator_t *e1, *e2, *tmp;
281 auth_cfg_t *c1, *c2;
282
283 e1 = this->ike_sa->create_auth_cfg_enumerator(this->ike_sa, FALSE);
284 e2 = this->peer_cfg->create_auth_cfg_enumerator(this->peer_cfg, FALSE);
285
286 if (strict)
287 { /* swap lists in strict mode: all configured rounds must be
288 * fulfilled. If !strict, we check only the rounds done so far. */
289 tmp = e1;
290 e1 = e2;
291 e2 = tmp;
292 }
293 while (e1->enumerate(e1, &c1))
294 {
295 /* check if done authentications comply to configured ones */
296 if ((!e2->enumerate(e2, &c2)) ||
297 (!strict && !c1->complies(c1, c2, TRUE)) ||
298 (strict && !c2->complies(c2, c1, TRUE)))
299 {
300 complies = FALSE;
301 break;
302 }
303 }
304 e1->destroy(e1);
305 e2->destroy(e2);
306 if (complies)
307 {
308 break;
309 }
310 DBG1(DBG_CFG, "selected peer config '%s' inacceptable",
311 this->peer_cfg->get_name(this->peer_cfg));
312 this->peer_cfg->destroy(this->peer_cfg);
313 }
314 if (this->candidates->remove_first(this->candidates,
315 (void**)&this->peer_cfg) != SUCCESS)
316 {
317 DBG1(DBG_CFG, "no alternative config found");
318 this->peer_cfg = NULL;
319 }
320 else
321 {
322 DBG1(DBG_CFG, "switching to peer config '%s'",
323 this->peer_cfg->get_name(this->peer_cfg));
324 this->ike_sa->set_peer_cfg(this->ike_sa, this->peer_cfg);
325 }
326 }
327 while (this->peer_cfg);
328
329 return this->peer_cfg != NULL;
330 }
331
332 /**
333 * Implementation of task_t.build for initiator
334 */
335 static status_t build_i(private_ike_auth_t *this, message_t *message)
336 {
337 auth_cfg_t *cfg;
338
339 if (message->get_exchange_type(message) == IKE_SA_INIT)
340 {
341 return collect_my_init_data(this, message);
342 }
343
344 if (this->peer_cfg == NULL)
345 {
346 this->peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
347 this->peer_cfg->get_ref(this->peer_cfg);
348 }
349
350 if (message->get_message_id(message) == 1)
351 { /* in the first IKE_AUTH ... */
352 if (this->ike_sa->supports_extension(this->ike_sa, EXT_MULTIPLE_AUTH))
353 { /* indicate support for multiple authentication */
354 message->add_notify(message, FALSE, MULTIPLE_AUTH_SUPPORTED,
355 chunk_empty);
356 }
357 /* indicate support for EAP-only authentication */
358 message->add_notify(message, FALSE, EAP_ONLY_AUTHENTICATION,
359 chunk_empty);
360 }
361
362 if (!this->do_another_auth && !this->my_auth)
363 { /* we have done our rounds */
364 return NEED_MORE;
365 }
366
367 /* check if an authenticator is in progress */
368 if (this->my_auth == NULL)
369 {
370 identification_t *id;
371 id_payload_t *id_payload;
372
373 /* clean up authentication config from a previous round */
374 cfg = this->ike_sa->get_auth_cfg(this->ike_sa, TRUE);
375 cfg->purge(cfg, TRUE);
376
377 /* add (optional) IDr */
378 cfg = get_auth_cfg(this, FALSE);
379 if (cfg)
380 {
381 id = cfg->get(cfg, AUTH_RULE_IDENTITY);
382 if (id && !id->contains_wildcards(id))
383 {
384 this->ike_sa->set_other_id(this->ike_sa, id->clone(id));
385 id_payload = id_payload_create_from_identification(
386 ID_RESPONDER, id);
387 message->add_payload(message, (payload_t*)id_payload);
388 }
389 }
390 /* add IDi */
391 cfg = this->ike_sa->get_auth_cfg(this->ike_sa, TRUE);
392 cfg->merge(cfg, get_auth_cfg(this, TRUE), TRUE);
393 id = cfg->get(cfg, AUTH_RULE_IDENTITY);
394 if (!id)
395 {
396 DBG1(DBG_CFG, "configuration misses IDi");
397 return FAILED;
398 }
399 this->ike_sa->set_my_id(this->ike_sa, id->clone(id));
400 id_payload = id_payload_create_from_identification(ID_INITIATOR, id);
401 message->add_payload(message, (payload_t*)id_payload);
402
403 /* build authentication data */
404 this->my_auth = authenticator_create_builder(this->ike_sa, cfg,
405 this->other_nonce, this->my_nonce,
406 this->other_packet->get_data(this->other_packet),
407 this->my_packet->get_data(this->my_packet));
408 if (!this->my_auth)
409 {
410 return FAILED;
411 }
412 }
413 switch (this->my_auth->build(this->my_auth, message))
414 {
415 case SUCCESS:
416 /* authentication step complete, reset authenticator */
417 cfg = auth_cfg_create();
418 cfg->merge(cfg, this->ike_sa->get_auth_cfg(this->ike_sa, TRUE), TRUE);
419 this->ike_sa->add_auth_cfg(this->ike_sa, TRUE, cfg);
420 this->my_auth->destroy(this->my_auth);
421 this->my_auth = NULL;
422 break;
423 case NEED_MORE:
424 break;
425 default:
426 return FAILED;
427 }
428
429 /* check for additional authentication rounds */
430 if (do_another_auth(this))
431 {
432 if (message->get_payload(message, AUTHENTICATION))
433 {
434 message->add_notify(message, FALSE, ANOTHER_AUTH_FOLLOWS, chunk_empty);
435 }
436 }
437 else
438 {
439 this->do_another_auth = FALSE;
440 }
441 return NEED_MORE;
442 }
443
444 /**
445 * Implementation of task_t.process for responder
446 */
447 static status_t process_r(private_ike_auth_t *this, message_t *message)
448 {
449 auth_cfg_t *cfg, *cand;
450 id_payload_t *id_payload;
451 identification_t *id;
452
453 if (message->get_exchange_type(message) == IKE_SA_INIT)
454 {
455 return collect_other_init_data(this, message);
456 }
457
458 if (this->my_auth == NULL && this->do_another_auth)
459 {
460 /* handle (optional) IDr payload, apply proposed identity */
461 id_payload = (id_payload_t*)message->get_payload(message, ID_RESPONDER);
462 if (id_payload)
463 {
464 id = id_payload->get_identification(id_payload);
465 }
466 else
467 {
468 id = identification_create_from_encoding(ID_ANY, chunk_empty);
469 }
470 this->ike_sa->set_my_id(this->ike_sa, id);
471 }
472
473 if (!this->expect_another_auth)
474 {
475 return NEED_MORE;
476 }
477
478 if (message->get_message_id(message) == 1)
479 { /* check for extensions in the first IKE_AUTH */
480 if (message->get_notify(message, MULTIPLE_AUTH_SUPPORTED))
481 {
482 this->ike_sa->enable_extension(this->ike_sa, EXT_MULTIPLE_AUTH);
483 }
484 if (this->ike_sa->supports_extension(this->ike_sa, EXT_STRONGSWAN) &&
485 message->get_notify(message, EAP_ONLY_AUTHENTICATION))
486 { /* EAP-only has no official notify, accept only from strongSwan */
487 this->ike_sa->enable_extension(this->ike_sa,
488 EXT_EAP_ONLY_AUTHENTICATION);
489 }
490 }
491
492 if (this->other_auth == NULL)
493 {
494 /* handle IDi payload */
495 id_payload = (id_payload_t*)message->get_payload(message, ID_INITIATOR);
496 if (!id_payload)
497 {
498 DBG1(DBG_IKE, "IDi payload missing");
499 return FAILED;
500 }
501 id = id_payload->get_identification(id_payload);
502 this->ike_sa->set_other_id(this->ike_sa, id);
503 cfg = this->ike_sa->get_auth_cfg(this->ike_sa, FALSE);
504 cfg->add(cfg, AUTH_RULE_IDENTITY, id->clone(id));
505
506 if (this->peer_cfg == NULL)
507 {
508 if (!load_cfg_candidates(this))
509 {
510 this->authentication_failed = TRUE;
511 return NEED_MORE;
512 }
513 }
514 if (message->get_payload(message, AUTHENTICATION) == NULL)
515 { /* before authenticating with EAP, we need a EAP config */
516 cand = get_auth_cfg(this, FALSE);
517 while (!cand || (
518 (uintptr_t)cand->get(cand, AUTH_RULE_EAP_TYPE) == EAP_NAK &&
519 (uintptr_t)cand->get(cand, AUTH_RULE_EAP_VENDOR) == 0))
520 { /* peer requested EAP, but current config does not match */
521 this->peer_cfg->destroy(this->peer_cfg);
522 this->peer_cfg = NULL;
523 if (!update_cfg_candidates(this, FALSE))
524 {
525 this->authentication_failed = TRUE;
526 return NEED_MORE;
527 }
528 cand = get_auth_cfg(this, FALSE);
529 }
530 cfg->merge(cfg, cand, TRUE);
531 }
532
533 /* verify authentication data */
534 this->other_auth = authenticator_create_verifier(this->ike_sa,
535 message, this->other_nonce, this->my_nonce,
536 this->other_packet->get_data(this->other_packet),
537 this->my_packet->get_data(this->my_packet));
538 if (!this->other_auth)
539 {
540 this->authentication_failed = TRUE;
541 return NEED_MORE;
542 }
543 }
544 switch (this->other_auth->process(this->other_auth, message))
545 {
546 case SUCCESS:
547 this->other_auth->destroy(this->other_auth);
548 this->other_auth = NULL;
549 break;
550 case NEED_MORE:
551 if (message->get_payload(message, AUTHENTICATION))
552 { /* AUTH verification successful, but another build() needed */
553 break;
554 }
555 return NEED_MORE;
556 default:
557 this->authentication_failed = TRUE;
558 return NEED_MORE;
559 }
560
561 /* store authentication information */
562 cfg = auth_cfg_create();
563 cfg->merge(cfg, this->ike_sa->get_auth_cfg(this->ike_sa, FALSE), FALSE);
564 this->ike_sa->add_auth_cfg(this->ike_sa, FALSE, cfg);
565
566 /* another auth round done, invoke authorize hook */
567 if (!charon->bus->authorize(charon->bus, FALSE))
568 {
569 DBG1(DBG_IKE, "authorization hook forbids IKE_SA, cancelling");
570 this->authentication_failed = TRUE;
571 return NEED_MORE;
572 }
573
574 if (!update_cfg_candidates(this, FALSE))
575 {
576 this->authentication_failed = TRUE;
577 return NEED_MORE;
578 }
579
580 if (message->get_notify(message, ANOTHER_AUTH_FOLLOWS) == NULL)
581 {
582 this->expect_another_auth = FALSE;
583 if (!update_cfg_candidates(this, TRUE))
584 {
585 this->authentication_failed = TRUE;
586 return NEED_MORE;
587 }
588 }
589 return NEED_MORE;
590 }
591
592 /**
593 * Implementation of task_t.build for responder
594 */
595 static status_t build_r(private_ike_auth_t *this, message_t *message)
596 {
597 auth_cfg_t *cfg;
598
599 if (message->get_exchange_type(message) == IKE_SA_INIT)
600 {
601 if (multiple_auth_enabled())
602 {
603 message->add_notify(message, FALSE, MULTIPLE_AUTH_SUPPORTED,
604 chunk_empty);
605 }
606 return collect_my_init_data(this, message);
607 }
608
609 if (this->authentication_failed || this->peer_cfg == NULL)
610 {
611 message->add_notify(message, TRUE, AUTHENTICATION_FAILED, chunk_empty);
612 return FAILED;
613 }
614
615 if (this->my_auth == NULL && this->do_another_auth)
616 {
617 identification_t *id, *id_cfg;
618 id_payload_t *id_payload;
619
620 /* add IDr */
621 cfg = this->ike_sa->get_auth_cfg(this->ike_sa, TRUE);
622 cfg->purge(cfg, TRUE);
623 cfg->merge(cfg, get_auth_cfg(this, TRUE), TRUE);
624
625 id_cfg = cfg->get(cfg, AUTH_RULE_IDENTITY);
626 id = this->ike_sa->get_my_id(this->ike_sa);
627 if (id->get_type(id) == ID_ANY)
628 { /* no IDr received, apply configured ID */
629 if (!id_cfg || id_cfg->contains_wildcards(id_cfg))
630 {
631 DBG1(DBG_CFG, "IDr not configured and negotiation failed");
632 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
633 chunk_empty);
634 return FAILED;
635 }
636 this->ike_sa->set_my_id(this->ike_sa, id_cfg->clone(id_cfg));
637 id = id_cfg;
638 }
639 else
640 { /* IDr received, check if it matches configuration */
641 if (id_cfg && !id->matches(id, id_cfg))
642 {
643 DBG1(DBG_CFG, "received IDr %Y, but require %Y", id, id_cfg);
644 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
645 chunk_empty);
646 return FAILED;
647 }
648 }
649
650 id_payload = id_payload_create_from_identification(ID_RESPONDER, id);
651 message->add_payload(message, (payload_t*)id_payload);
652
653 if ((uintptr_t)cfg->get(cfg, AUTH_RULE_AUTH_CLASS) == AUTH_CLASS_EAP)
654 { /* EAP-only authentication */
655 if (!this->ike_sa->supports_extension(this->ike_sa,
656 EXT_EAP_ONLY_AUTHENTICATION))
657 {
658 DBG1(DBG_IKE, "configured EAP-only authentication, but peer "
659 "does not support it");
660 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
661 chunk_empty);
662 return FAILED;
663 }
664 }
665 else
666 {
667 /* build authentication data */
668 this->my_auth = authenticator_create_builder(this->ike_sa, cfg,
669 this->other_nonce, this->my_nonce,
670 this->other_packet->get_data(this->other_packet),
671 this->my_packet->get_data(this->my_packet));
672 if (!this->my_auth)
673 {
674 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
675 chunk_empty);
676 return FAILED;
677 }
678 }
679 }
680
681 if (this->other_auth)
682 {
683 switch (this->other_auth->build(this->other_auth, message))
684 {
685 case SUCCESS:
686 this->other_auth->destroy(this->other_auth);
687 this->other_auth = NULL;
688 break;
689 case NEED_MORE:
690 break;
691 default:
692 if (!message->get_payload(message, EXTENSIBLE_AUTHENTICATION))
693 { /* skip AUTHENTICATION_FAILED if we have EAP_FAILURE */
694 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
695 chunk_empty);
696 }
697 return FAILED;
698 }
699 }
700 if (this->my_auth)
701 {
702 switch (this->my_auth->build(this->my_auth, message))
703 {
704 case SUCCESS:
705 cfg = auth_cfg_create();
706 cfg->merge(cfg, this->ike_sa->get_auth_cfg(this->ike_sa, TRUE),
707 TRUE);
708 this->ike_sa->add_auth_cfg(this->ike_sa, TRUE, cfg);
709 this->my_auth->destroy(this->my_auth);
710 this->my_auth = NULL;
711 break;
712 case NEED_MORE:
713 break;
714 default:
715 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
716 chunk_empty);
717 return FAILED;
718 }
719 }
720
721 /* check for additional authentication rounds */
722 if (do_another_auth(this))
723 {
724 message->add_notify(message, FALSE, ANOTHER_AUTH_FOLLOWS, chunk_empty);
725 }
726 else
727 {
728 this->do_another_auth = FALSE;
729 }
730 if (!this->do_another_auth && !this->expect_another_auth)
731 {
732 if (charon->ike_sa_manager->check_uniqueness(charon->ike_sa_manager,
733 this->ike_sa))
734 {
735 DBG1(DBG_IKE, "cancelling IKE_SA setup due uniqueness policy");
736 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
737 chunk_empty);
738 return FAILED;
739 }
740 if (!charon->bus->authorize(charon->bus, TRUE))
741 {
742 DBG1(DBG_IKE, "final authorization hook forbids IKE_SA, cancelling");
743 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
744 chunk_empty);
745 return FAILED;
746 }
747 DBG0(DBG_IKE, "IKE_SA %s[%d] established between %H[%Y]...%H[%Y]",
748 this->ike_sa->get_name(this->ike_sa),
749 this->ike_sa->get_unique_id(this->ike_sa),
750 this->ike_sa->get_my_host(this->ike_sa),
751 this->ike_sa->get_my_id(this->ike_sa),
752 this->ike_sa->get_other_host(this->ike_sa),
753 this->ike_sa->get_other_id(this->ike_sa));
754 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
755 charon->bus->ike_updown(charon->bus, this->ike_sa, TRUE);
756 return SUCCESS;
757 }
758 return NEED_MORE;
759 }
760
761 /**
762 * Implementation of task_t.process for initiator
763 */
764 static status_t process_i(private_ike_auth_t *this, message_t *message)
765 {
766 enumerator_t *enumerator;
767 payload_t *payload;
768 auth_cfg_t *cfg;
769 bool mutual_eap = FALSE;
770
771 if (message->get_exchange_type(message) == IKE_SA_INIT)
772 {
773 if (message->get_notify(message, MULTIPLE_AUTH_SUPPORTED) &&
774 multiple_auth_enabled())
775 {
776 this->ike_sa->enable_extension(this->ike_sa, EXT_MULTIPLE_AUTH);
777 }
778 return collect_other_init_data(this, message);
779 }
780
781 enumerator = message->create_payload_enumerator(message);
782 while (enumerator->enumerate(enumerator, &payload))
783 {
784 if (payload->get_type(payload) == NOTIFY)
785 {
786 notify_payload_t *notify = (notify_payload_t*)payload;
787 notify_type_t type = notify->get_notify_type(notify);
788
789 switch (type)
790 {
791 case NO_PROPOSAL_CHOSEN:
792 case SINGLE_PAIR_REQUIRED:
793 case NO_ADDITIONAL_SAS:
794 case INTERNAL_ADDRESS_FAILURE:
795 case FAILED_CP_REQUIRED:
796 case TS_UNACCEPTABLE:
797 case INVALID_SELECTORS:
798 /* these are errors, but are not critical as only the
799 * CHILD_SA won't get build, but IKE_SA establishes anyway */
800 break;
801 case MOBIKE_SUPPORTED:
802 case ADDITIONAL_IP4_ADDRESS:
803 case ADDITIONAL_IP6_ADDRESS:
804 /* handled in ike_mobike task */
805 break;
806 case AUTH_LIFETIME:
807 /* handled in ike_auth_lifetime task */
808 break;
809 case ME_ENDPOINT:
810 /* handled in ike_me task */
811 break;
812 default:
813 {
814 if (type < 16383)
815 {
816 DBG1(DBG_IKE, "received %N notify error",
817 notify_type_names, type);
818 enumerator->destroy(enumerator);
819 return FAILED;
820 }
821 DBG2(DBG_IKE, "received %N notify",
822 notify_type_names, type);
823 break;
824 }
825 }
826 }
827 }
828 enumerator->destroy(enumerator);
829
830 if (this->expect_another_auth)
831 {
832 if (this->other_auth == NULL)
833 {
834 id_payload_t *id_payload;
835 identification_t *id;
836
837 /* handle IDr payload */
838 id_payload = (id_payload_t*)message->get_payload(message,
839 ID_RESPONDER);
840 if (!id_payload)
841 {
842 DBG1(DBG_IKE, "IDr payload missing");
843 return FAILED;
844 }
845 id = id_payload->get_identification(id_payload);
846 this->ike_sa->set_other_id(this->ike_sa, id);
847 cfg = this->ike_sa->get_auth_cfg(this->ike_sa, FALSE);
848 cfg->add(cfg, AUTH_RULE_IDENTITY, id->clone(id));
849
850 if (message->get_payload(message, AUTHENTICATION))
851 {
852 /* verify authentication data */
853 this->other_auth = authenticator_create_verifier(this->ike_sa,
854 message, this->other_nonce, this->my_nonce,
855 this->other_packet->get_data(this->other_packet),
856 this->my_packet->get_data(this->my_packet));
857 if (!this->other_auth)
858 {
859 return FAILED;
860 }
861 }
862 else
863 {
864 /* responder omitted AUTH payload, indicating EAP-only */
865 mutual_eap = TRUE;
866 }
867 }
868 if (this->other_auth)
869 {
870 switch (this->other_auth->process(this->other_auth, message))
871 {
872 case SUCCESS:
873 break;
874 case NEED_MORE:
875 return NEED_MORE;
876 default:
877 return FAILED;
878 }
879 this->other_auth->destroy(this->other_auth);
880 this->other_auth = NULL;
881 }
882 /* store authentication information, reset authenticator */
883 cfg = auth_cfg_create();
884 cfg->merge(cfg, this->ike_sa->get_auth_cfg(this->ike_sa, FALSE), FALSE);
885 this->ike_sa->add_auth_cfg(this->ike_sa, FALSE, cfg);
886
887 /* another auth round done, invoke authorize hook */
888 if (!charon->bus->authorize(charon->bus, FALSE))
889 {
890 DBG1(DBG_IKE, "authorization forbids IKE_SA, cancelling");
891 return FAILED;
892 }
893 }
894
895 if (this->my_auth)
896 {
897 switch (this->my_auth->process(this->my_auth, message))
898 {
899 case SUCCESS:
900 cfg = auth_cfg_create();
901 cfg->merge(cfg, this->ike_sa->get_auth_cfg(this->ike_sa, TRUE),
902 TRUE);
903 this->ike_sa->add_auth_cfg(this->ike_sa, TRUE, cfg);
904 this->my_auth->destroy(this->my_auth);
905 this->my_auth = NULL;
906 this->do_another_auth = do_another_auth(this);
907 break;
908 case NEED_MORE:
909 break;
910 default:
911 return FAILED;
912 }
913 }
914 if (mutual_eap)
915 {
916 if (!this->my_auth || !this->my_auth->is_mutual(this->my_auth))
917 {
918 DBG1(DBG_IKE, "do not allow non-mutual EAP-only authentication");
919 return FAILED;
920 }
921 DBG1(DBG_IKE, "allow mutual EAP-only authentication");
922 }
923
924 if (message->get_notify(message, ANOTHER_AUTH_FOLLOWS) == NULL)
925 {
926 this->expect_another_auth = FALSE;
927 }
928 if (!this->expect_another_auth && !this->do_another_auth && !this->my_auth)
929 {
930 if (!update_cfg_candidates(this, TRUE))
931 {
932 return FAILED;
933 }
934 if (!charon->bus->authorize(charon->bus, TRUE))
935 {
936 DBG1(DBG_IKE, "final authorization hook forbids IKE_SA, cancelling");
937 return FAILED;
938 }
939 DBG0(DBG_IKE, "IKE_SA %s[%d] established between %H[%Y]...%H[%Y]",
940 this->ike_sa->get_name(this->ike_sa),
941 this->ike_sa->get_unique_id(this->ike_sa),
942 this->ike_sa->get_my_host(this->ike_sa),
943 this->ike_sa->get_my_id(this->ike_sa),
944 this->ike_sa->get_other_host(this->ike_sa),
945 this->ike_sa->get_other_id(this->ike_sa));
946 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
947 charon->bus->ike_updown(charon->bus, this->ike_sa, TRUE);
948 return SUCCESS;
949 }
950 return NEED_MORE;
951 }
952
953 /**
954 * Implementation of task_t.get_type
955 */
956 static task_type_t get_type(private_ike_auth_t *this)
957 {
958 return IKE_AUTHENTICATE;
959 }
960
961 /**
962 * Implementation of task_t.migrate
963 */
964 static void migrate(private_ike_auth_t *this, ike_sa_t *ike_sa)
965 {
966 chunk_free(&this->my_nonce);
967 chunk_free(&this->other_nonce);
968 DESTROY_IF(this->my_packet);
969 DESTROY_IF(this->other_packet);
970 DESTROY_IF(this->peer_cfg);
971 DESTROY_IF(this->my_auth);
972 DESTROY_IF(this->other_auth);
973 this->candidates->destroy_offset(this->candidates, offsetof(peer_cfg_t, destroy));
974
975 this->my_packet = NULL;
976 this->other_packet = NULL;
977 this->ike_sa = ike_sa;
978 this->peer_cfg = NULL;
979 this->my_auth = NULL;
980 this->other_auth = NULL;
981 this->do_another_auth = TRUE;
982 this->expect_another_auth = TRUE;
983 this->authentication_failed = FALSE;
984 this->candidates = linked_list_create();
985 }
986
987 /**
988 * Implementation of task_t.destroy
989 */
990 static void destroy(private_ike_auth_t *this)
991 {
992 chunk_free(&this->my_nonce);
993 chunk_free(&this->other_nonce);
994 DESTROY_IF(this->my_packet);
995 DESTROY_IF(this->other_packet);
996 DESTROY_IF(this->my_auth);
997 DESTROY_IF(this->other_auth);
998 DESTROY_IF(this->peer_cfg);
999 this->candidates->destroy_offset(this->candidates, offsetof(peer_cfg_t, destroy));
1000 free(this);
1001 }
1002
1003 /*
1004 * Described in header.
1005 */
1006 ike_auth_t *ike_auth_create(ike_sa_t *ike_sa, bool initiator)
1007 {
1008 private_ike_auth_t *this = malloc_thing(private_ike_auth_t);
1009
1010 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
1011 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
1012 this->public.task.destroy = (void(*)(task_t*))destroy;
1013
1014 if (initiator)
1015 {
1016 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
1017 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
1018 }
1019 else
1020 {
1021 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
1022 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
1023 }
1024
1025 this->ike_sa = ike_sa;
1026 this->initiator = initiator;
1027 this->my_nonce = chunk_empty;
1028 this->other_nonce = chunk_empty;
1029 this->my_packet = NULL;
1030 this->other_packet = NULL;
1031 this->peer_cfg = NULL;
1032 this->candidates = linked_list_create();
1033 this->my_auth = NULL;
1034 this->other_auth = NULL;
1035 this->do_another_auth = TRUE;
1036 this->expect_another_auth = TRUE;
1037 this->authentication_failed = FALSE;
1038
1039 return &this->public;
1040 }
1041