Support EAP-only authentication for mutual and key deriving EAP methods
[strongswan.git] / src / charon / 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
770 if (message->get_exchange_type(message) == IKE_SA_INIT)
771 {
772 if (message->get_notify(message, MULTIPLE_AUTH_SUPPORTED) &&
773 multiple_auth_enabled())
774 {
775 this->ike_sa->enable_extension(this->ike_sa, EXT_MULTIPLE_AUTH);
776 }
777 return collect_other_init_data(this, message);
778 }
779
780 enumerator = message->create_payload_enumerator(message);
781 while (enumerator->enumerate(enumerator, &payload))
782 {
783 if (payload->get_type(payload) == NOTIFY)
784 {
785 notify_payload_t *notify = (notify_payload_t*)payload;
786 notify_type_t type = notify->get_notify_type(notify);
787
788 switch (type)
789 {
790 case NO_PROPOSAL_CHOSEN:
791 case SINGLE_PAIR_REQUIRED:
792 case NO_ADDITIONAL_SAS:
793 case INTERNAL_ADDRESS_FAILURE:
794 case FAILED_CP_REQUIRED:
795 case TS_UNACCEPTABLE:
796 case INVALID_SELECTORS:
797 /* these are errors, but are not critical as only the
798 * CHILD_SA won't get build, but IKE_SA establishes anyway */
799 break;
800 case MOBIKE_SUPPORTED:
801 case ADDITIONAL_IP4_ADDRESS:
802 case ADDITIONAL_IP6_ADDRESS:
803 /* handled in ike_mobike task */
804 break;
805 case AUTH_LIFETIME:
806 /* handled in ike_auth_lifetime task */
807 break;
808 case ME_ENDPOINT:
809 /* handled in ike_me task */
810 break;
811 default:
812 {
813 if (type < 16383)
814 {
815 DBG1(DBG_IKE, "received %N notify error",
816 notify_type_names, type);
817 enumerator->destroy(enumerator);
818 return FAILED;
819 }
820 DBG2(DBG_IKE, "received %N notify",
821 notify_type_names, type);
822 break;
823 }
824 }
825 }
826 }
827 enumerator->destroy(enumerator);
828
829 if (this->my_auth)
830 {
831 switch (this->my_auth->process(this->my_auth, message))
832 {
833 case SUCCESS:
834 cfg = auth_cfg_create();
835 cfg->merge(cfg, this->ike_sa->get_auth_cfg(this->ike_sa, TRUE),
836 TRUE);
837 this->ike_sa->add_auth_cfg(this->ike_sa, TRUE, cfg);
838 this->my_auth->destroy(this->my_auth);
839 this->my_auth = NULL;
840 this->do_another_auth = do_another_auth(this);
841 break;
842 case NEED_MORE:
843 break;
844 default:
845 return FAILED;
846 }
847 }
848
849 if (this->expect_another_auth)
850 {
851 if (this->other_auth == NULL)
852 {
853 id_payload_t *id_payload;
854 identification_t *id;
855
856 /* handle IDr payload */
857 id_payload = (id_payload_t*)message->get_payload(message,
858 ID_RESPONDER);
859 if (!id_payload)
860 {
861 DBG1(DBG_IKE, "IDr payload missing");
862 return FAILED;
863 }
864 id = id_payload->get_identification(id_payload);
865 this->ike_sa->set_other_id(this->ike_sa, id);
866 cfg = this->ike_sa->get_auth_cfg(this->ike_sa, FALSE);
867 cfg->add(cfg, AUTH_RULE_IDENTITY, id->clone(id));
868
869 if (message->get_payload(message, AUTHENTICATION))
870 {
871 /* verify authentication data */
872 this->other_auth = authenticator_create_verifier(this->ike_sa,
873 message, this->other_nonce, this->my_nonce,
874 this->other_packet->get_data(this->other_packet),
875 this->my_packet->get_data(this->my_packet));
876 if (!this->other_auth)
877 {
878 return FAILED;
879 }
880 }
881 else
882 { /* responder omited AUTH payload, indicating EAP-only */
883 if (!this->my_auth ||
884 !this->my_auth->is_mutual(this->my_auth))
885 {
886 return FAILED;
887 }
888 }
889 }
890 if (this->other_auth)
891 {
892 switch (this->other_auth->process(this->other_auth, message))
893 {
894 case SUCCESS:
895 break;
896 case NEED_MORE:
897 return NEED_MORE;
898 default:
899 return FAILED;
900 }
901 this->other_auth->destroy(this->other_auth);
902 this->other_auth = NULL;
903 }
904 /* store authentication information, reset authenticator */
905 cfg = auth_cfg_create();
906 cfg->merge(cfg, this->ike_sa->get_auth_cfg(this->ike_sa, FALSE), FALSE);
907 this->ike_sa->add_auth_cfg(this->ike_sa, FALSE, cfg);
908
909 /* another auth round done, invoke authorize hook */
910 if (!charon->bus->authorize(charon->bus, FALSE))
911 {
912 DBG1(DBG_IKE, "authorization forbids IKE_SA, cancelling");
913 return FAILED;
914 }
915 }
916
917 if (message->get_notify(message, ANOTHER_AUTH_FOLLOWS) == NULL)
918 {
919 this->expect_another_auth = FALSE;
920 }
921 if (!this->expect_another_auth && !this->do_another_auth && !this->my_auth)
922 {
923 if (!update_cfg_candidates(this, TRUE))
924 {
925 return FAILED;
926 }
927 if (!charon->bus->authorize(charon->bus, TRUE))
928 {
929 DBG1(DBG_IKE, "final authorization hook forbids IKE_SA, cancelling");
930 return FAILED;
931 }
932 DBG0(DBG_IKE, "IKE_SA %s[%d] established between %H[%Y]...%H[%Y]",
933 this->ike_sa->get_name(this->ike_sa),
934 this->ike_sa->get_unique_id(this->ike_sa),
935 this->ike_sa->get_my_host(this->ike_sa),
936 this->ike_sa->get_my_id(this->ike_sa),
937 this->ike_sa->get_other_host(this->ike_sa),
938 this->ike_sa->get_other_id(this->ike_sa));
939 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
940 charon->bus->ike_updown(charon->bus, this->ike_sa, TRUE);
941 return SUCCESS;
942 }
943 return NEED_MORE;
944 }
945
946 /**
947 * Implementation of task_t.get_type
948 */
949 static task_type_t get_type(private_ike_auth_t *this)
950 {
951 return IKE_AUTHENTICATE;
952 }
953
954 /**
955 * Implementation of task_t.migrate
956 */
957 static void migrate(private_ike_auth_t *this, ike_sa_t *ike_sa)
958 {
959 chunk_free(&this->my_nonce);
960 chunk_free(&this->other_nonce);
961 DESTROY_IF(this->my_packet);
962 DESTROY_IF(this->other_packet);
963 DESTROY_IF(this->peer_cfg);
964 DESTROY_IF(this->my_auth);
965 DESTROY_IF(this->other_auth);
966 this->candidates->destroy_offset(this->candidates, offsetof(peer_cfg_t, destroy));
967
968 this->my_packet = NULL;
969 this->other_packet = NULL;
970 this->ike_sa = ike_sa;
971 this->peer_cfg = NULL;
972 this->my_auth = NULL;
973 this->other_auth = NULL;
974 this->do_another_auth = TRUE;
975 this->expect_another_auth = TRUE;
976 this->authentication_failed = FALSE;
977 this->candidates = linked_list_create();
978 }
979
980 /**
981 * Implementation of task_t.destroy
982 */
983 static void destroy(private_ike_auth_t *this)
984 {
985 chunk_free(&this->my_nonce);
986 chunk_free(&this->other_nonce);
987 DESTROY_IF(this->my_packet);
988 DESTROY_IF(this->other_packet);
989 DESTROY_IF(this->my_auth);
990 DESTROY_IF(this->other_auth);
991 DESTROY_IF(this->peer_cfg);
992 this->candidates->destroy_offset(this->candidates, offsetof(peer_cfg_t, destroy));
993 free(this);
994 }
995
996 /*
997 * Described in header.
998 */
999 ike_auth_t *ike_auth_create(ike_sa_t *ike_sa, bool initiator)
1000 {
1001 private_ike_auth_t *this = malloc_thing(private_ike_auth_t);
1002
1003 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
1004 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
1005 this->public.task.destroy = (void(*)(task_t*))destroy;
1006
1007 if (initiator)
1008 {
1009 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
1010 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
1011 }
1012 else
1013 {
1014 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
1015 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
1016 }
1017
1018 this->ike_sa = ike_sa;
1019 this->initiator = initiator;
1020 this->my_nonce = chunk_empty;
1021 this->other_nonce = chunk_empty;
1022 this->my_packet = NULL;
1023 this->other_packet = NULL;
1024 this->peer_cfg = NULL;
1025 this->candidates = linked_list_create();
1026 this->my_auth = NULL;
1027 this->other_auth = NULL;
1028 this->do_another_auth = TRUE;
1029 this->expect_another_auth = TRUE;
1030 this->authentication_failed = FALSE;
1031
1032 return &this->public;
1033 }
1034