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