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