Add uniqueids=never to ignore INITIAL_CONTACT notifies
[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 "%s.multiple_authentication", TRUE, charon->name);
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 this->peer_cfg->get_unique_policy(this->peer_cfg) != UNIQUE_NEVER)
440 {
441 host_t *host;
442
443 host = this->ike_sa->get_other_host(this->ike_sa);
444 if (!charon->ike_sa_manager->has_contact(charon->ike_sa_manager,
445 idi, idr, host->get_family(host)))
446 {
447 message->add_notify(message, FALSE, INITIAL_CONTACT, chunk_empty);
448 }
449 }
450
451 /* build authentication data */
452 this->my_auth = authenticator_create_builder(this->ike_sa, cfg,
453 this->other_nonce, this->my_nonce,
454 this->other_packet->get_data(this->other_packet),
455 this->my_packet->get_data(this->my_packet),
456 this->reserved);
457 if (!this->my_auth)
458 {
459 return FAILED;
460 }
461 }
462 switch (this->my_auth->build(this->my_auth, message))
463 {
464 case SUCCESS:
465 /* authentication step complete, reset authenticator */
466 cfg = auth_cfg_create();
467 cfg->merge(cfg, this->ike_sa->get_auth_cfg(this->ike_sa, TRUE), TRUE);
468 this->ike_sa->add_auth_cfg(this->ike_sa, TRUE, cfg);
469 this->my_auth->destroy(this->my_auth);
470 this->my_auth = NULL;
471 break;
472 case NEED_MORE:
473 break;
474 default:
475 return FAILED;
476 }
477
478 /* check for additional authentication rounds */
479 if (do_another_auth(this))
480 {
481 if (message->get_payload(message, AUTHENTICATION))
482 {
483 message->add_notify(message, FALSE, ANOTHER_AUTH_FOLLOWS, chunk_empty);
484 }
485 }
486 else
487 {
488 this->do_another_auth = FALSE;
489 }
490 return NEED_MORE;
491 }
492
493 METHOD(task_t, process_r, status_t,
494 private_ike_auth_t *this, message_t *message)
495 {
496 auth_cfg_t *cfg, *cand;
497 id_payload_t *id_payload;
498 identification_t *id;
499
500 if (message->get_exchange_type(message) == IKE_SA_INIT)
501 {
502 return collect_other_init_data(this, message);
503 }
504
505 if (this->my_auth == NULL && this->do_another_auth)
506 {
507 /* handle (optional) IDr payload, apply proposed identity */
508 id_payload = (id_payload_t*)message->get_payload(message, ID_RESPONDER);
509 if (id_payload)
510 {
511 id = id_payload->get_identification(id_payload);
512 }
513 else
514 {
515 id = identification_create_from_encoding(ID_ANY, chunk_empty);
516 }
517 this->ike_sa->set_my_id(this->ike_sa, id);
518 }
519
520 if (!this->expect_another_auth)
521 {
522 return NEED_MORE;
523 }
524
525 if (message->get_message_id(message) == 1)
526 { /* check for extensions in the first IKE_AUTH */
527 if (message->get_notify(message, MULTIPLE_AUTH_SUPPORTED))
528 {
529 this->ike_sa->enable_extension(this->ike_sa, EXT_MULTIPLE_AUTH);
530 }
531 if (message->get_notify(message, EAP_ONLY_AUTHENTICATION))
532 {
533 this->ike_sa->enable_extension(this->ike_sa,
534 EXT_EAP_ONLY_AUTHENTICATION);
535 }
536 }
537
538 if (this->other_auth == NULL)
539 {
540 /* handle IDi payload */
541 id_payload = (id_payload_t*)message->get_payload(message, ID_INITIATOR);
542 if (!id_payload)
543 {
544 DBG1(DBG_IKE, "IDi payload missing");
545 return FAILED;
546 }
547 id = id_payload->get_identification(id_payload);
548 get_reserved_id_bytes(this, id_payload);
549 this->ike_sa->set_other_id(this->ike_sa, id);
550 cfg = this->ike_sa->get_auth_cfg(this->ike_sa, FALSE);
551 cfg->add(cfg, AUTH_RULE_IDENTITY, id->clone(id));
552
553 if (this->peer_cfg == NULL)
554 {
555 if (!load_cfg_candidates(this))
556 {
557 this->authentication_failed = TRUE;
558 return NEED_MORE;
559 }
560 }
561 if (message->get_payload(message, AUTHENTICATION) == NULL)
562 { /* before authenticating with EAP, we need a EAP config */
563 cand = get_auth_cfg(this, FALSE);
564 while (!cand || (
565 (uintptr_t)cand->get(cand, AUTH_RULE_EAP_TYPE) == EAP_NAK &&
566 (uintptr_t)cand->get(cand, AUTH_RULE_EAP_VENDOR) == 0))
567 { /* peer requested EAP, but current config does not match */
568 DBG1(DBG_IKE, "peer requested EAP, config inacceptable");
569 this->peer_cfg->destroy(this->peer_cfg);
570 this->peer_cfg = NULL;
571 if (!update_cfg_candidates(this, FALSE))
572 {
573 this->authentication_failed = TRUE;
574 return NEED_MORE;
575 }
576 cand = get_auth_cfg(this, FALSE);
577 }
578 /* copy over the EAP specific rules for authentication */
579 cfg->add(cfg, AUTH_RULE_EAP_TYPE,
580 cand->get(cand, AUTH_RULE_EAP_TYPE));
581 cfg->add(cfg, AUTH_RULE_EAP_VENDOR,
582 cand->get(cand, AUTH_RULE_EAP_VENDOR));
583 id = (identification_t*)cand->get(cand, AUTH_RULE_EAP_IDENTITY);
584 if (id)
585 {
586 cfg->add(cfg, AUTH_RULE_EAP_IDENTITY, id->clone(id));
587 }
588 id = (identification_t*)cand->get(cand, AUTH_RULE_AAA_IDENTITY);
589 if (id)
590 {
591 cfg->add(cfg, AUTH_RULE_AAA_IDENTITY, id->clone(id));
592 }
593 }
594
595 /* verify authentication data */
596 this->other_auth = authenticator_create_verifier(this->ike_sa,
597 message, this->other_nonce, this->my_nonce,
598 this->other_packet->get_data(this->other_packet),
599 this->my_packet->get_data(this->my_packet),
600 this->reserved);
601 if (!this->other_auth)
602 {
603 this->authentication_failed = TRUE;
604 return NEED_MORE;
605 }
606 }
607 switch (this->other_auth->process(this->other_auth, message))
608 {
609 case SUCCESS:
610 this->other_auth->destroy(this->other_auth);
611 this->other_auth = NULL;
612 break;
613 case NEED_MORE:
614 if (message->get_payload(message, AUTHENTICATION))
615 { /* AUTH verification successful, but another build() needed */
616 break;
617 }
618 return NEED_MORE;
619 default:
620 this->authentication_failed = TRUE;
621 return NEED_MORE;
622 }
623
624 /* If authenticated (with non-EAP) and received INITIAL_CONTACT,
625 * delete any existing IKE_SAs with that peer. */
626 if (message->get_message_id(message) == 1 &&
627 message->get_notify(message, INITIAL_CONTACT))
628 {
629 this->initial_contact = TRUE;
630 }
631
632 /* another auth round done, invoke authorize hook */
633 if (!charon->bus->authorize(charon->bus, FALSE))
634 {
635 DBG1(DBG_IKE, "authorization hook forbids IKE_SA, cancelling");
636 this->authentication_failed = TRUE;
637 return NEED_MORE;
638 }
639
640 /* store authentication information */
641 cfg = auth_cfg_create();
642 cfg->merge(cfg, this->ike_sa->get_auth_cfg(this->ike_sa, FALSE), FALSE);
643 this->ike_sa->add_auth_cfg(this->ike_sa, FALSE, cfg);
644
645 if (!update_cfg_candidates(this, FALSE))
646 {
647 this->authentication_failed = TRUE;
648 return NEED_MORE;
649 }
650
651 if (message->get_notify(message, ANOTHER_AUTH_FOLLOWS) == NULL)
652 {
653 this->expect_another_auth = FALSE;
654 if (!update_cfg_candidates(this, TRUE))
655 {
656 this->authentication_failed = TRUE;
657 return NEED_MORE;
658 }
659 }
660 return NEED_MORE;
661 }
662
663 METHOD(task_t, build_r, status_t,
664 private_ike_auth_t *this, message_t *message)
665 {
666 auth_cfg_t *cfg;
667
668 if (message->get_exchange_type(message) == IKE_SA_INIT)
669 {
670 if (multiple_auth_enabled())
671 {
672 message->add_notify(message, FALSE, MULTIPLE_AUTH_SUPPORTED,
673 chunk_empty);
674 }
675 return collect_my_init_data(this, message);
676 }
677
678 if (this->authentication_failed || this->peer_cfg == NULL)
679 {
680 goto peer_auth_failed;
681 }
682
683 if (this->my_auth == NULL && this->do_another_auth)
684 {
685 identification_t *id, *id_cfg;
686 id_payload_t *id_payload;
687
688 /* add IDr */
689 cfg = this->ike_sa->get_auth_cfg(this->ike_sa, TRUE);
690 cfg->purge(cfg, TRUE);
691 cfg->merge(cfg, get_auth_cfg(this, TRUE), TRUE);
692
693 id_cfg = cfg->get(cfg, AUTH_RULE_IDENTITY);
694 id = this->ike_sa->get_my_id(this->ike_sa);
695 if (id->get_type(id) == ID_ANY)
696 { /* no IDr received, apply configured ID */
697 if (!id_cfg || id_cfg->contains_wildcards(id_cfg))
698 { /* no ID configured, use local IP address */
699 host_t *me;
700
701 DBG1(DBG_CFG, "no IDr configured, fall back on IP address");
702 me = this->ike_sa->get_my_host(this->ike_sa);
703 id_cfg = identification_create_from_sockaddr(
704 me->get_sockaddr(me));
705 cfg->add(cfg, AUTH_RULE_IDENTITY, id_cfg);
706 }
707 this->ike_sa->set_my_id(this->ike_sa, id_cfg->clone(id_cfg));
708 id = id_cfg;
709 }
710 else
711 { /* IDr received, check if it matches configuration */
712 if (id_cfg && !id->matches(id, id_cfg))
713 {
714 DBG1(DBG_CFG, "received IDr %Y, but require %Y", id, id_cfg);
715 goto peer_auth_failed;
716 }
717 }
718
719 id_payload = id_payload_create_from_identification(ID_RESPONDER, id);
720 get_reserved_id_bytes(this, id_payload);
721 message->add_payload(message, (payload_t*)id_payload);
722
723 if (this->initial_contact)
724 {
725 charon->ike_sa_manager->check_uniqueness(charon->ike_sa_manager,
726 this->ike_sa, TRUE);
727 this->initial_contact = FALSE;
728 }
729
730 if ((uintptr_t)cfg->get(cfg, AUTH_RULE_AUTH_CLASS) == AUTH_CLASS_EAP)
731 { /* EAP-only authentication */
732 if (!this->ike_sa->supports_extension(this->ike_sa,
733 EXT_EAP_ONLY_AUTHENTICATION))
734 {
735 DBG1(DBG_IKE, "configured EAP-only authentication, but peer "
736 "does not support it");
737 goto peer_auth_failed;
738 }
739 }
740 else
741 {
742 /* build authentication data */
743 this->my_auth = authenticator_create_builder(this->ike_sa, cfg,
744 this->other_nonce, this->my_nonce,
745 this->other_packet->get_data(this->other_packet),
746 this->my_packet->get_data(this->my_packet),
747 this->reserved);
748 if (!this->my_auth)
749 {
750 goto peer_auth_failed;
751 }
752 }
753 }
754
755 if (this->other_auth)
756 {
757 switch (this->other_auth->build(this->other_auth, message))
758 {
759 case SUCCESS:
760 this->other_auth->destroy(this->other_auth);
761 this->other_auth = NULL;
762 break;
763 case NEED_MORE:
764 break;
765 default:
766 if (message->get_payload(message, EXTENSIBLE_AUTHENTICATION))
767 { /* skip AUTHENTICATION_FAILED if we have EAP_FAILURE */
768 goto peer_auth_failed_no_notify;
769 }
770 goto peer_auth_failed;
771 }
772 }
773 if (this->my_auth)
774 {
775 switch (this->my_auth->build(this->my_auth, message))
776 {
777 case SUCCESS:
778 cfg = auth_cfg_create();
779 cfg->merge(cfg, this->ike_sa->get_auth_cfg(this->ike_sa, TRUE),
780 TRUE);
781 this->ike_sa->add_auth_cfg(this->ike_sa, TRUE, cfg);
782 this->my_auth->destroy(this->my_auth);
783 this->my_auth = NULL;
784 break;
785 case NEED_MORE:
786 break;
787 default:
788 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
789 chunk_empty);
790 return FAILED;
791 }
792 }
793
794 /* check for additional authentication rounds */
795 if (do_another_auth(this))
796 {
797 message->add_notify(message, FALSE, ANOTHER_AUTH_FOLLOWS, chunk_empty);
798 }
799 else
800 {
801 this->do_another_auth = FALSE;
802 }
803 if (!this->do_another_auth && !this->expect_another_auth)
804 {
805 if (charon->ike_sa_manager->check_uniqueness(charon->ike_sa_manager,
806 this->ike_sa, FALSE))
807 {
808 DBG1(DBG_IKE, "cancelling IKE_SA setup due to uniqueness policy");
809 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
810 chunk_empty);
811 return FAILED;
812 }
813 if (!charon->bus->authorize(charon->bus, TRUE))
814 {
815 DBG1(DBG_IKE, "final authorization hook forbids IKE_SA, cancelling");
816 goto peer_auth_failed;
817 }
818 DBG0(DBG_IKE, "IKE_SA %s[%d] established between %H[%Y]...%H[%Y]",
819 this->ike_sa->get_name(this->ike_sa),
820 this->ike_sa->get_unique_id(this->ike_sa),
821 this->ike_sa->get_my_host(this->ike_sa),
822 this->ike_sa->get_my_id(this->ike_sa),
823 this->ike_sa->get_other_host(this->ike_sa),
824 this->ike_sa->get_other_id(this->ike_sa));
825 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
826 charon->bus->ike_updown(charon->bus, this->ike_sa, TRUE);
827 return SUCCESS;
828 }
829 return NEED_MORE;
830
831 peer_auth_failed:
832 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
833 chunk_empty);
834 peer_auth_failed_no_notify:
835 charon->bus->alert(charon->bus, ALERT_PEER_AUTH_FAILED);
836 return FAILED;
837 }
838
839 METHOD(task_t, process_i, status_t,
840 private_ike_auth_t *this, message_t *message)
841 {
842 enumerator_t *enumerator;
843 payload_t *payload;
844 auth_cfg_t *cfg;
845 bool mutual_eap = FALSE;
846
847 if (message->get_exchange_type(message) == IKE_SA_INIT)
848 {
849 if (message->get_notify(message, MULTIPLE_AUTH_SUPPORTED) &&
850 multiple_auth_enabled())
851 {
852 this->ike_sa->enable_extension(this->ike_sa, EXT_MULTIPLE_AUTH);
853 }
854 return collect_other_init_data(this, message);
855 }
856
857 enumerator = message->create_payload_enumerator(message);
858 while (enumerator->enumerate(enumerator, &payload))
859 {
860 if (payload->get_type(payload) == NOTIFY)
861 {
862 notify_payload_t *notify = (notify_payload_t*)payload;
863 notify_type_t type = notify->get_notify_type(notify);
864
865 switch (type)
866 {
867 case NO_PROPOSAL_CHOSEN:
868 case SINGLE_PAIR_REQUIRED:
869 case NO_ADDITIONAL_SAS:
870 case INTERNAL_ADDRESS_FAILURE:
871 case FAILED_CP_REQUIRED:
872 case TS_UNACCEPTABLE:
873 case INVALID_SELECTORS:
874 /* these are errors, but are not critical as only the
875 * CHILD_SA won't get build, but IKE_SA establishes anyway */
876 break;
877 case MOBIKE_SUPPORTED:
878 case ADDITIONAL_IP4_ADDRESS:
879 case ADDITIONAL_IP6_ADDRESS:
880 /* handled in ike_mobike task */
881 break;
882 case AUTH_LIFETIME:
883 /* handled in ike_auth_lifetime task */
884 break;
885 case ME_ENDPOINT:
886 /* handled in ike_me task */
887 break;
888 default:
889 {
890 if (type <= 16383)
891 {
892 DBG1(DBG_IKE, "received %N notify error",
893 notify_type_names, type);
894 enumerator->destroy(enumerator);
895 return FAILED;
896 }
897 DBG2(DBG_IKE, "received %N notify",
898 notify_type_names, type);
899 break;
900 }
901 }
902 }
903 }
904 enumerator->destroy(enumerator);
905
906 if (this->expect_another_auth)
907 {
908 if (this->other_auth == NULL)
909 {
910 id_payload_t *id_payload;
911 identification_t *id;
912
913 /* handle IDr payload */
914 id_payload = (id_payload_t*)message->get_payload(message,
915 ID_RESPONDER);
916 if (!id_payload)
917 {
918 DBG1(DBG_IKE, "IDr payload missing");
919 goto peer_auth_failed;
920 }
921 id = id_payload->get_identification(id_payload);
922 get_reserved_id_bytes(this, id_payload);
923 this->ike_sa->set_other_id(this->ike_sa, id);
924 cfg = this->ike_sa->get_auth_cfg(this->ike_sa, FALSE);
925 cfg->add(cfg, AUTH_RULE_IDENTITY, id->clone(id));
926
927 if (message->get_payload(message, AUTHENTICATION))
928 {
929 /* verify authentication data */
930 this->other_auth = authenticator_create_verifier(this->ike_sa,
931 message, this->other_nonce, this->my_nonce,
932 this->other_packet->get_data(this->other_packet),
933 this->my_packet->get_data(this->my_packet),
934 this->reserved);
935 if (!this->other_auth)
936 {
937 goto peer_auth_failed;
938 }
939 }
940 else
941 {
942 /* responder omitted AUTH payload, indicating EAP-only */
943 mutual_eap = TRUE;
944 }
945 }
946 if (this->other_auth)
947 {
948 switch (this->other_auth->process(this->other_auth, message))
949 {
950 case SUCCESS:
951 break;
952 case NEED_MORE:
953 return NEED_MORE;
954 default:
955 goto peer_auth_failed;
956 }
957 this->other_auth->destroy(this->other_auth);
958 this->other_auth = NULL;
959 }
960 /* another auth round done, invoke authorize hook */
961 if (!charon->bus->authorize(charon->bus, FALSE))
962 {
963 DBG1(DBG_IKE, "authorization forbids IKE_SA, cancelling");
964 goto peer_auth_failed;
965 }
966
967 /* store authentication information, reset authenticator */
968 cfg = auth_cfg_create();
969 cfg->merge(cfg, this->ike_sa->get_auth_cfg(this->ike_sa, FALSE), FALSE);
970 this->ike_sa->add_auth_cfg(this->ike_sa, FALSE, cfg);
971 }
972
973 if (this->my_auth)
974 {
975 switch (this->my_auth->process(this->my_auth, message))
976 {
977 case SUCCESS:
978 cfg = auth_cfg_create();
979 cfg->merge(cfg, this->ike_sa->get_auth_cfg(this->ike_sa, TRUE),
980 TRUE);
981 this->ike_sa->add_auth_cfg(this->ike_sa, TRUE, cfg);
982 this->my_auth->destroy(this->my_auth);
983 this->my_auth = NULL;
984 this->do_another_auth = do_another_auth(this);
985 break;
986 case NEED_MORE:
987 break;
988 default:
989 return FAILED;
990 }
991 }
992 if (mutual_eap)
993 {
994 if (!this->my_auth || !this->my_auth->is_mutual(this->my_auth))
995 {
996 DBG1(DBG_IKE, "do not allow non-mutual EAP-only authentication");
997 goto peer_auth_failed;
998 }
999 DBG1(DBG_IKE, "allow mutual EAP-only authentication");
1000 }
1001
1002 if (message->get_notify(message, ANOTHER_AUTH_FOLLOWS) == NULL)
1003 {
1004 this->expect_another_auth = FALSE;
1005 }
1006 if (!this->expect_another_auth && !this->do_another_auth && !this->my_auth)
1007 {
1008 if (!update_cfg_candidates(this, TRUE))
1009 {
1010 goto peer_auth_failed;
1011 }
1012 if (!charon->bus->authorize(charon->bus, TRUE))
1013 {
1014 DBG1(DBG_IKE, "final authorization hook forbids IKE_SA, "
1015 "cancelling");
1016 goto peer_auth_failed;
1017 }
1018 DBG0(DBG_IKE, "IKE_SA %s[%d] established between %H[%Y]...%H[%Y]",
1019 this->ike_sa->get_name(this->ike_sa),
1020 this->ike_sa->get_unique_id(this->ike_sa),
1021 this->ike_sa->get_my_host(this->ike_sa),
1022 this->ike_sa->get_my_id(this->ike_sa),
1023 this->ike_sa->get_other_host(this->ike_sa),
1024 this->ike_sa->get_other_id(this->ike_sa));
1025 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
1026 charon->bus->ike_updown(charon->bus, this->ike_sa, TRUE);
1027 return SUCCESS;
1028 }
1029 return NEED_MORE;
1030
1031 peer_auth_failed:
1032 charon->bus->alert(charon->bus, ALERT_PEER_AUTH_FAILED);
1033 return FAILED;
1034 }
1035
1036 METHOD(task_t, get_type, task_type_t,
1037 private_ike_auth_t *this)
1038 {
1039 return TASK_IKE_AUTH;
1040 }
1041
1042 METHOD(task_t, migrate, void,
1043 private_ike_auth_t *this, ike_sa_t *ike_sa)
1044 {
1045 chunk_free(&this->my_nonce);
1046 chunk_free(&this->other_nonce);
1047 DESTROY_IF(this->my_packet);
1048 DESTROY_IF(this->other_packet);
1049 DESTROY_IF(this->peer_cfg);
1050 DESTROY_IF(this->my_auth);
1051 DESTROY_IF(this->other_auth);
1052 this->candidates->destroy_offset(this->candidates, offsetof(peer_cfg_t, destroy));
1053
1054 this->my_packet = NULL;
1055 this->other_packet = NULL;
1056 this->ike_sa = ike_sa;
1057 this->peer_cfg = NULL;
1058 this->my_auth = NULL;
1059 this->other_auth = NULL;
1060 this->do_another_auth = TRUE;
1061 this->expect_another_auth = TRUE;
1062 this->authentication_failed = FALSE;
1063 this->candidates = linked_list_create();
1064 }
1065
1066 METHOD(task_t, destroy, void,
1067 private_ike_auth_t *this)
1068 {
1069 chunk_free(&this->my_nonce);
1070 chunk_free(&this->other_nonce);
1071 DESTROY_IF(this->my_packet);
1072 DESTROY_IF(this->other_packet);
1073 DESTROY_IF(this->my_auth);
1074 DESTROY_IF(this->other_auth);
1075 DESTROY_IF(this->peer_cfg);
1076 this->candidates->destroy_offset(this->candidates, offsetof(peer_cfg_t, destroy));
1077 free(this);
1078 }
1079
1080 /*
1081 * Described in header.
1082 */
1083 ike_auth_t *ike_auth_create(ike_sa_t *ike_sa, bool initiator)
1084 {
1085 private_ike_auth_t *this;
1086
1087 INIT(this,
1088 .public = {
1089 .task = {
1090 .get_type = _get_type,
1091 .migrate = _migrate,
1092 .build = _build_r,
1093 .process = _process_r,
1094 .destroy = _destroy,
1095 },
1096 },
1097 .ike_sa = ike_sa,
1098 .initiator = initiator,
1099 .candidates = linked_list_create(),
1100 .do_another_auth = TRUE,
1101 .expect_another_auth = TRUE,
1102 );
1103 if (initiator)
1104 {
1105 this->public.task.build = _build_i;
1106 this->public.task.process = _process_i;
1107 }
1108 return &this->public;
1109 }