Added log statement if peer requests EAP, but current config does not allow it
[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 DBG1(DBG_IKE, "peer requested EAP, config inacceptable");
522 this->peer_cfg->destroy(this->peer_cfg);
523 this->peer_cfg = NULL;
524 if (!update_cfg_candidates(this, FALSE))
525 {
526 this->authentication_failed = TRUE;
527 return NEED_MORE;
528 }
529 cand = get_auth_cfg(this, FALSE);
530 }
531 /* copy over the EAP specific rules for authentication */
532 cfg->add(cfg, AUTH_RULE_EAP_TYPE,
533 cand->get(cand, AUTH_RULE_EAP_TYPE));
534 cfg->add(cfg, AUTH_RULE_EAP_VENDOR,
535 cand->get(cand, AUTH_RULE_EAP_VENDOR));
536 id = (identification_t*)cand->get(cand, AUTH_RULE_EAP_IDENTITY);
537 if (id)
538 {
539 cfg->add(cfg, AUTH_RULE_EAP_IDENTITY, id->clone(id));
540 }
541 }
542
543 /* verify authentication data */
544 this->other_auth = authenticator_create_verifier(this->ike_sa,
545 message, this->other_nonce, this->my_nonce,
546 this->other_packet->get_data(this->other_packet),
547 this->my_packet->get_data(this->my_packet));
548 if (!this->other_auth)
549 {
550 this->authentication_failed = TRUE;
551 return NEED_MORE;
552 }
553 }
554 switch (this->other_auth->process(this->other_auth, message))
555 {
556 case SUCCESS:
557 this->other_auth->destroy(this->other_auth);
558 this->other_auth = NULL;
559 break;
560 case NEED_MORE:
561 if (message->get_payload(message, AUTHENTICATION))
562 { /* AUTH verification successful, but another build() needed */
563 break;
564 }
565 return NEED_MORE;
566 default:
567 this->authentication_failed = TRUE;
568 return NEED_MORE;
569 }
570
571 /* store authentication information */
572 cfg = auth_cfg_create();
573 cfg->merge(cfg, this->ike_sa->get_auth_cfg(this->ike_sa, FALSE), FALSE);
574 this->ike_sa->add_auth_cfg(this->ike_sa, FALSE, cfg);
575
576 /* another auth round done, invoke authorize hook */
577 if (!charon->bus->authorize(charon->bus, FALSE))
578 {
579 DBG1(DBG_IKE, "authorization hook forbids IKE_SA, cancelling");
580 this->authentication_failed = TRUE;
581 return NEED_MORE;
582 }
583
584 if (!update_cfg_candidates(this, FALSE))
585 {
586 this->authentication_failed = TRUE;
587 return NEED_MORE;
588 }
589
590 if (message->get_notify(message, ANOTHER_AUTH_FOLLOWS) == NULL)
591 {
592 this->expect_another_auth = FALSE;
593 if (!update_cfg_candidates(this, TRUE))
594 {
595 this->authentication_failed = TRUE;
596 return NEED_MORE;
597 }
598 }
599 return NEED_MORE;
600 }
601
602 /**
603 * Implementation of task_t.build for responder
604 */
605 static status_t build_r(private_ike_auth_t *this, message_t *message)
606 {
607 auth_cfg_t *cfg;
608
609 if (message->get_exchange_type(message) == IKE_SA_INIT)
610 {
611 if (multiple_auth_enabled())
612 {
613 message->add_notify(message, FALSE, MULTIPLE_AUTH_SUPPORTED,
614 chunk_empty);
615 }
616 return collect_my_init_data(this, message);
617 }
618
619 if (this->authentication_failed || this->peer_cfg == NULL)
620 {
621 message->add_notify(message, TRUE, AUTHENTICATION_FAILED, chunk_empty);
622 return FAILED;
623 }
624
625 if (this->my_auth == NULL && this->do_another_auth)
626 {
627 identification_t *id, *id_cfg;
628 id_payload_t *id_payload;
629
630 /* add IDr */
631 cfg = this->ike_sa->get_auth_cfg(this->ike_sa, TRUE);
632 cfg->purge(cfg, TRUE);
633 cfg->merge(cfg, get_auth_cfg(this, TRUE), TRUE);
634
635 id_cfg = cfg->get(cfg, AUTH_RULE_IDENTITY);
636 id = this->ike_sa->get_my_id(this->ike_sa);
637 if (id->get_type(id) == ID_ANY)
638 { /* no IDr received, apply configured ID */
639 if (!id_cfg || id_cfg->contains_wildcards(id_cfg))
640 {
641 DBG1(DBG_CFG, "IDr not configured and negotiation failed");
642 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
643 chunk_empty);
644 return FAILED;
645 }
646 this->ike_sa->set_my_id(this->ike_sa, id_cfg->clone(id_cfg));
647 id = id_cfg;
648 }
649 else
650 { /* IDr received, check if it matches configuration */
651 if (id_cfg && !id->matches(id, id_cfg))
652 {
653 DBG1(DBG_CFG, "received IDr %Y, but require %Y", id, id_cfg);
654 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
655 chunk_empty);
656 return FAILED;
657 }
658 }
659
660 id_payload = id_payload_create_from_identification(ID_RESPONDER, id);
661 message->add_payload(message, (payload_t*)id_payload);
662
663 if ((uintptr_t)cfg->get(cfg, AUTH_RULE_AUTH_CLASS) == AUTH_CLASS_EAP)
664 { /* EAP-only authentication */
665 if (!this->ike_sa->supports_extension(this->ike_sa,
666 EXT_EAP_ONLY_AUTHENTICATION))
667 {
668 DBG1(DBG_IKE, "configured EAP-only authentication, but peer "
669 "does not support it");
670 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
671 chunk_empty);
672 return FAILED;
673 }
674 }
675 else
676 {
677 /* build authentication data */
678 this->my_auth = authenticator_create_builder(this->ike_sa, cfg,
679 this->other_nonce, this->my_nonce,
680 this->other_packet->get_data(this->other_packet),
681 this->my_packet->get_data(this->my_packet));
682 if (!this->my_auth)
683 {
684 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
685 chunk_empty);
686 return FAILED;
687 }
688 }
689 }
690
691 if (this->other_auth)
692 {
693 switch (this->other_auth->build(this->other_auth, message))
694 {
695 case SUCCESS:
696 this->other_auth->destroy(this->other_auth);
697 this->other_auth = NULL;
698 break;
699 case NEED_MORE:
700 break;
701 default:
702 if (!message->get_payload(message, EXTENSIBLE_AUTHENTICATION))
703 { /* skip AUTHENTICATION_FAILED if we have EAP_FAILURE */
704 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
705 chunk_empty);
706 }
707 return FAILED;
708 }
709 }
710 if (this->my_auth)
711 {
712 switch (this->my_auth->build(this->my_auth, message))
713 {
714 case SUCCESS:
715 cfg = auth_cfg_create();
716 cfg->merge(cfg, this->ike_sa->get_auth_cfg(this->ike_sa, TRUE),
717 TRUE);
718 this->ike_sa->add_auth_cfg(this->ike_sa, TRUE, cfg);
719 this->my_auth->destroy(this->my_auth);
720 this->my_auth = NULL;
721 break;
722 case NEED_MORE:
723 break;
724 default:
725 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
726 chunk_empty);
727 return FAILED;
728 }
729 }
730
731 /* check for additional authentication rounds */
732 if (do_another_auth(this))
733 {
734 message->add_notify(message, FALSE, ANOTHER_AUTH_FOLLOWS, chunk_empty);
735 }
736 else
737 {
738 this->do_another_auth = FALSE;
739 }
740 if (!this->do_another_auth && !this->expect_another_auth)
741 {
742 if (charon->ike_sa_manager->check_uniqueness(charon->ike_sa_manager,
743 this->ike_sa))
744 {
745 DBG1(DBG_IKE, "cancelling IKE_SA setup due uniqueness policy");
746 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
747 chunk_empty);
748 return FAILED;
749 }
750 if (!charon->bus->authorize(charon->bus, TRUE))
751 {
752 DBG1(DBG_IKE, "final authorization hook forbids IKE_SA, cancelling");
753 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
754 chunk_empty);
755 return FAILED;
756 }
757 DBG0(DBG_IKE, "IKE_SA %s[%d] established between %H[%Y]...%H[%Y]",
758 this->ike_sa->get_name(this->ike_sa),
759 this->ike_sa->get_unique_id(this->ike_sa),
760 this->ike_sa->get_my_host(this->ike_sa),
761 this->ike_sa->get_my_id(this->ike_sa),
762 this->ike_sa->get_other_host(this->ike_sa),
763 this->ike_sa->get_other_id(this->ike_sa));
764 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
765 charon->bus->ike_updown(charon->bus, this->ike_sa, TRUE);
766 return SUCCESS;
767 }
768 return NEED_MORE;
769 }
770
771 /**
772 * Implementation of task_t.process for initiator
773 */
774 static status_t process_i(private_ike_auth_t *this, message_t *message)
775 {
776 enumerator_t *enumerator;
777 payload_t *payload;
778 auth_cfg_t *cfg;
779 bool mutual_eap = FALSE;
780
781 if (message->get_exchange_type(message) == IKE_SA_INIT)
782 {
783 if (message->get_notify(message, MULTIPLE_AUTH_SUPPORTED) &&
784 multiple_auth_enabled())
785 {
786 this->ike_sa->enable_extension(this->ike_sa, EXT_MULTIPLE_AUTH);
787 }
788 return collect_other_init_data(this, message);
789 }
790
791 enumerator = message->create_payload_enumerator(message);
792 while (enumerator->enumerate(enumerator, &payload))
793 {
794 if (payload->get_type(payload) == NOTIFY)
795 {
796 notify_payload_t *notify = (notify_payload_t*)payload;
797 notify_type_t type = notify->get_notify_type(notify);
798
799 switch (type)
800 {
801 case NO_PROPOSAL_CHOSEN:
802 case SINGLE_PAIR_REQUIRED:
803 case NO_ADDITIONAL_SAS:
804 case INTERNAL_ADDRESS_FAILURE:
805 case FAILED_CP_REQUIRED:
806 case TS_UNACCEPTABLE:
807 case INVALID_SELECTORS:
808 /* these are errors, but are not critical as only the
809 * CHILD_SA won't get build, but IKE_SA establishes anyway */
810 break;
811 case MOBIKE_SUPPORTED:
812 case ADDITIONAL_IP4_ADDRESS:
813 case ADDITIONAL_IP6_ADDRESS:
814 /* handled in ike_mobike task */
815 break;
816 case AUTH_LIFETIME:
817 /* handled in ike_auth_lifetime task */
818 break;
819 case ME_ENDPOINT:
820 /* handled in ike_me task */
821 break;
822 default:
823 {
824 if (type < 16383)
825 {
826 DBG1(DBG_IKE, "received %N notify error",
827 notify_type_names, type);
828 enumerator->destroy(enumerator);
829 return FAILED;
830 }
831 DBG2(DBG_IKE, "received %N notify",
832 notify_type_names, type);
833 break;
834 }
835 }
836 }
837 }
838 enumerator->destroy(enumerator);
839
840 if (this->expect_another_auth)
841 {
842 if (this->other_auth == NULL)
843 {
844 id_payload_t *id_payload;
845 identification_t *id;
846
847 /* handle IDr payload */
848 id_payload = (id_payload_t*)message->get_payload(message,
849 ID_RESPONDER);
850 if (!id_payload)
851 {
852 DBG1(DBG_IKE, "IDr payload missing");
853 return FAILED;
854 }
855 id = id_payload->get_identification(id_payload);
856 this->ike_sa->set_other_id(this->ike_sa, id);
857 cfg = this->ike_sa->get_auth_cfg(this->ike_sa, FALSE);
858 cfg->add(cfg, AUTH_RULE_IDENTITY, id->clone(id));
859
860 if (message->get_payload(message, AUTHENTICATION))
861 {
862 /* verify authentication data */
863 this->other_auth = authenticator_create_verifier(this->ike_sa,
864 message, this->other_nonce, this->my_nonce,
865 this->other_packet->get_data(this->other_packet),
866 this->my_packet->get_data(this->my_packet));
867 if (!this->other_auth)
868 {
869 return FAILED;
870 }
871 }
872 else
873 {
874 /* responder omitted AUTH payload, indicating EAP-only */
875 mutual_eap = TRUE;
876 }
877 }
878 if (this->other_auth)
879 {
880 switch (this->other_auth->process(this->other_auth, message))
881 {
882 case SUCCESS:
883 break;
884 case NEED_MORE:
885 return NEED_MORE;
886 default:
887 return FAILED;
888 }
889 this->other_auth->destroy(this->other_auth);
890 this->other_auth = NULL;
891 }
892 /* store authentication information, reset authenticator */
893 cfg = auth_cfg_create();
894 cfg->merge(cfg, this->ike_sa->get_auth_cfg(this->ike_sa, FALSE), FALSE);
895 this->ike_sa->add_auth_cfg(this->ike_sa, FALSE, cfg);
896
897 /* another auth round done, invoke authorize hook */
898 if (!charon->bus->authorize(charon->bus, FALSE))
899 {
900 DBG1(DBG_IKE, "authorization forbids IKE_SA, cancelling");
901 return FAILED;
902 }
903 }
904
905 if (this->my_auth)
906 {
907 switch (this->my_auth->process(this->my_auth, message))
908 {
909 case SUCCESS:
910 cfg = auth_cfg_create();
911 cfg->merge(cfg, this->ike_sa->get_auth_cfg(this->ike_sa, TRUE),
912 TRUE);
913 this->ike_sa->add_auth_cfg(this->ike_sa, TRUE, cfg);
914 this->my_auth->destroy(this->my_auth);
915 this->my_auth = NULL;
916 this->do_another_auth = do_another_auth(this);
917 break;
918 case NEED_MORE:
919 break;
920 default:
921 return FAILED;
922 }
923 }
924 if (mutual_eap)
925 {
926 if (!this->my_auth || !this->my_auth->is_mutual(this->my_auth))
927 {
928 DBG1(DBG_IKE, "do not allow non-mutual EAP-only authentication");
929 return FAILED;
930 }
931 DBG1(DBG_IKE, "allow mutual EAP-only authentication");
932 }
933
934 if (message->get_notify(message, ANOTHER_AUTH_FOLLOWS) == NULL)
935 {
936 this->expect_another_auth = FALSE;
937 }
938 if (!this->expect_another_auth && !this->do_another_auth && !this->my_auth)
939 {
940 if (!update_cfg_candidates(this, TRUE))
941 {
942 return FAILED;
943 }
944 if (!charon->bus->authorize(charon->bus, TRUE))
945 {
946 DBG1(DBG_IKE, "final authorization hook forbids IKE_SA, cancelling");
947 return FAILED;
948 }
949 DBG0(DBG_IKE, "IKE_SA %s[%d] established between %H[%Y]...%H[%Y]",
950 this->ike_sa->get_name(this->ike_sa),
951 this->ike_sa->get_unique_id(this->ike_sa),
952 this->ike_sa->get_my_host(this->ike_sa),
953 this->ike_sa->get_my_id(this->ike_sa),
954 this->ike_sa->get_other_host(this->ike_sa),
955 this->ike_sa->get_other_id(this->ike_sa));
956 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
957 charon->bus->ike_updown(charon->bus, this->ike_sa, TRUE);
958 return SUCCESS;
959 }
960 return NEED_MORE;
961 }
962
963 /**
964 * Implementation of task_t.get_type
965 */
966 static task_type_t get_type(private_ike_auth_t *this)
967 {
968 return IKE_AUTHENTICATE;
969 }
970
971 /**
972 * Implementation of task_t.migrate
973 */
974 static void migrate(private_ike_auth_t *this, ike_sa_t *ike_sa)
975 {
976 chunk_free(&this->my_nonce);
977 chunk_free(&this->other_nonce);
978 DESTROY_IF(this->my_packet);
979 DESTROY_IF(this->other_packet);
980 DESTROY_IF(this->peer_cfg);
981 DESTROY_IF(this->my_auth);
982 DESTROY_IF(this->other_auth);
983 this->candidates->destroy_offset(this->candidates, offsetof(peer_cfg_t, destroy));
984
985 this->my_packet = NULL;
986 this->other_packet = NULL;
987 this->ike_sa = ike_sa;
988 this->peer_cfg = NULL;
989 this->my_auth = NULL;
990 this->other_auth = NULL;
991 this->do_another_auth = TRUE;
992 this->expect_another_auth = TRUE;
993 this->authentication_failed = FALSE;
994 this->candidates = linked_list_create();
995 }
996
997 /**
998 * Implementation of task_t.destroy
999 */
1000 static void destroy(private_ike_auth_t *this)
1001 {
1002 chunk_free(&this->my_nonce);
1003 chunk_free(&this->other_nonce);
1004 DESTROY_IF(this->my_packet);
1005 DESTROY_IF(this->other_packet);
1006 DESTROY_IF(this->my_auth);
1007 DESTROY_IF(this->other_auth);
1008 DESTROY_IF(this->peer_cfg);
1009 this->candidates->destroy_offset(this->candidates, offsetof(peer_cfg_t, destroy));
1010 free(this);
1011 }
1012
1013 /*
1014 * Described in header.
1015 */
1016 ike_auth_t *ike_auth_create(ike_sa_t *ike_sa, bool initiator)
1017 {
1018 private_ike_auth_t *this = malloc_thing(private_ike_auth_t);
1019
1020 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
1021 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
1022 this->public.task.destroy = (void(*)(task_t*))destroy;
1023
1024 if (initiator)
1025 {
1026 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
1027 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
1028 }
1029 else
1030 {
1031 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
1032 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
1033 }
1034
1035 this->ike_sa = ike_sa;
1036 this->initiator = initiator;
1037 this->my_nonce = chunk_empty;
1038 this->other_nonce = chunk_empty;
1039 this->my_packet = NULL;
1040 this->other_packet = NULL;
1041 this->peer_cfg = NULL;
1042 this->candidates = linked_list_create();
1043 this->my_auth = NULL;
1044 this->other_auth = NULL;
1045 this->do_another_auth = TRUE;
1046 this->expect_another_auth = TRUE;
1047 this->authentication_failed = FALSE;
1048
1049 return &this->public;
1050 }
1051