Accept non-encrypted INFORMATIONALs for ME connectivity checks
[strongswan.git] / src / libcharon / sa / tasks / ike_auth.c
1 /*
2 * Copyright (C) 2005-2009 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details
15 */
16
17 #include "ike_auth.h"
18
19 #include <string.h>
20
21 #include <daemon.h>
22 #include <encoding/payloads/id_payload.h>
23 #include <encoding/payloads/auth_payload.h>
24 #include <encoding/payloads/eap_payload.h>
25 #include <encoding/payloads/nonce_payload.h>
26 #include <sa/authenticators/eap_authenticator.h>
27
28 typedef struct private_ike_auth_t private_ike_auth_t;
29
30 /**
31 * Private members of a ike_auth_t task.
32 */
33 struct private_ike_auth_t {
34
35 /**
36 * Public methods and task_t interface.
37 */
38 ike_auth_t public;
39
40 /**
41 * Assigned IKE_SA.
42 */
43 ike_sa_t *ike_sa;
44
45 /**
46 * Are we the initiator?
47 */
48 bool initiator;
49
50 /**
51 * Nonce chosen by us in ike_init
52 */
53 chunk_t my_nonce;
54
55 /**
56 * Nonce chosen by peer in ike_init
57 */
58 chunk_t other_nonce;
59
60 /**
61 * IKE_SA_INIT message sent by us
62 */
63 packet_t *my_packet;
64
65 /**
66 * IKE_SA_INIT message sent by peer
67 */
68 packet_t *other_packet;
69
70 /**
71 * Reserved bytes of ID payload
72 */
73 char reserved[3];
74
75 /**
76 * currently active authenticator, to authenticate us
77 */
78 authenticator_t *my_auth;
79
80 /**
81 * currently active authenticator, to authenticate peer
82 */
83 authenticator_t *other_auth;
84
85 /**
86 * peer_cfg candidates, ordered by priority
87 */
88 linked_list_t *candidates;
89
90 /**
91 * selected peer config (might change when using multiple authentications)
92 */
93 peer_cfg_t *peer_cfg;
94
95 /**
96 * have we planned an(other) authentication exchange?
97 */
98 bool do_another_auth;
99
100 /**
101 * has the peer announced another authentication exchange?
102 */
103 bool expect_another_auth;
104
105 /**
106 * should we send a AUTHENTICATION_FAILED notify?
107 */
108 bool authentication_failed;
109
110 /**
111 * received an INITIAL_CONTACT?
112 */
113 bool initial_contact;
114 };
115
116 /**
117 * check if multiple authentication extension is enabled, configuration-wise
118 */
119 static bool multiple_auth_enabled()
120 {
121 return lib->settings->get_bool(lib->settings,
122 "charon.multiple_authentication", TRUE);
123 }
124
125 /**
126 * collect the needed information in the IKE_SA_INIT exchange from our message
127 */
128 static status_t collect_my_init_data(private_ike_auth_t *this,
129 message_t *message)
130 {
131 nonce_payload_t *nonce;
132
133 /* get the nonce that was generated in ike_init */
134 nonce = (nonce_payload_t*)message->get_payload(message, NONCE);
135 if (nonce == NULL)
136 {
137 return FAILED;
138 }
139 this->my_nonce = nonce->get_nonce(nonce);
140
141 /* pre-generate the message, keep a copy */
142 if (this->ike_sa->generate_message(this->ike_sa, message,
143 &this->my_packet) != SUCCESS)
144 {
145 return FAILED;
146 }
147 return NEED_MORE;
148 }
149
150 /**
151 * collect the needed information in the IKE_SA_INIT exchange from others message
152 */
153 static status_t collect_other_init_data(private_ike_auth_t *this,
154 message_t *message)
155 {
156 /* we collect the needed information in the IKE_SA_INIT exchange */
157 nonce_payload_t *nonce;
158
159 /* get the nonce that was generated in ike_init */
160 nonce = (nonce_payload_t*)message->get_payload(message, NONCE);
161 if (nonce == NULL)
162 {
163 return FAILED;
164 }
165 this->other_nonce = nonce->get_nonce(nonce);
166
167 /* keep a copy of the received packet */
168 this->other_packet = message->get_packet(message);
169 return NEED_MORE;
170 }
171
172 /**
173 * Get and store reserved bytes of id_payload, required for AUTH payload
174 */
175 static void get_reserved_id_bytes(private_ike_auth_t *this, id_payload_t *id)
176 {
177 u_int8_t *byte;
178 int i;
179
180 for (i = 0; i < countof(this->reserved); i++)
181 {
182 byte = payload_get_field(&id->payload_interface, RESERVED_BYTE, i);
183 if (byte)
184 {
185 this->reserved[i] = *byte;
186 }
187 }
188 }
189
190 /**
191 * Get the next authentication configuration
192 */
193 static auth_cfg_t *get_auth_cfg(private_ike_auth_t *this, bool local)
194 {
195 enumerator_t *e1, *e2;
196 auth_cfg_t *c1, *c2, *next = NULL;
197
198 /* find an available config not already done */
199 e1 = this->peer_cfg->create_auth_cfg_enumerator(this->peer_cfg, local);
200 while (e1->enumerate(e1, &c1))
201 {
202 bool found = FALSE;
203
204 e2 = this->ike_sa->create_auth_cfg_enumerator(this->ike_sa, local);
205 while (e2->enumerate(e2, &c2))
206 {
207 if (c2->complies(c2, c1, FALSE))
208 {
209 found = TRUE;
210 break;
211 }
212 }
213 e2->destroy(e2);
214 if (!found)
215 {
216 next = c1;
217 break;
218 }
219 }
220 e1->destroy(e1);
221 return next;
222 }
223
224 /**
225 * Check if we have should initiate another authentication round
226 */
227 static bool do_another_auth(private_ike_auth_t *this)
228 {
229 bool do_another = FALSE;
230 enumerator_t *done, *todo;
231 auth_cfg_t *done_cfg, *todo_cfg;
232
233 if (!this->ike_sa->supports_extension(this->ike_sa, EXT_MULTIPLE_AUTH))
234 {
235 return FALSE;
236 }
237
238 done = this->ike_sa->create_auth_cfg_enumerator(this->ike_sa, TRUE);
239 todo = this->peer_cfg->create_auth_cfg_enumerator(this->peer_cfg, TRUE);
240 while (todo->enumerate(todo, &todo_cfg))
241 {
242 if (!done->enumerate(done, &done_cfg))
243 {
244 done_cfg = this->ike_sa->get_auth_cfg(this->ike_sa, TRUE);
245 }
246 if (!done_cfg->complies(done_cfg, todo_cfg, FALSE))
247 {
248 do_another = TRUE;
249 break;
250 }
251 }
252 done->destroy(done);
253 todo->destroy(todo);
254 return do_another;
255 }
256
257 /**
258 * Get peer configuration candidates from backends
259 */
260 static bool load_cfg_candidates(private_ike_auth_t *this)
261 {
262 enumerator_t *enumerator;
263 peer_cfg_t *peer_cfg;
264 host_t *me, *other;
265 identification_t *my_id, *other_id;
266
267 me = this->ike_sa->get_my_host(this->ike_sa);
268 other = this->ike_sa->get_other_host(this->ike_sa);
269 my_id = this->ike_sa->get_my_id(this->ike_sa);
270 other_id = this->ike_sa->get_other_id(this->ike_sa);
271
272 enumerator = charon->backends->create_peer_cfg_enumerator(charon->backends,
273 me, other, my_id, other_id);
274 while (enumerator->enumerate(enumerator, &peer_cfg))
275 {
276 peer_cfg->get_ref(peer_cfg);
277 if (this->peer_cfg == NULL)
278 { /* best match */
279 this->peer_cfg = peer_cfg;
280 this->ike_sa->set_peer_cfg(this->ike_sa, peer_cfg);
281 }
282 else
283 {
284 this->candidates->insert_last(this->candidates, peer_cfg);
285 }
286 }
287 enumerator->destroy(enumerator);
288 if (this->peer_cfg)
289 {
290 DBG1(DBG_CFG, "selected peer config '%s'",
291 this->peer_cfg->get_name(this->peer_cfg));
292 return TRUE;
293 }
294 DBG1(DBG_CFG, "no matching peer config found");
295 return FALSE;
296 }
297
298 /**
299 * update the current peer candidate if necessary, using candidates
300 */
301 static bool update_cfg_candidates(private_ike_auth_t *this, bool strict)
302 {
303 do
304 {
305 if (this->peer_cfg)
306 {
307 bool complies = TRUE;
308 enumerator_t *e1, *e2, *tmp;
309 auth_cfg_t *c1, *c2;
310
311 e1 = this->ike_sa->create_auth_cfg_enumerator(this->ike_sa, FALSE);
312 e2 = this->peer_cfg->create_auth_cfg_enumerator(this->peer_cfg, FALSE);
313
314 if (strict)
315 { /* swap lists in strict mode: all configured rounds must be
316 * fulfilled. If !strict, we check only the rounds done so far. */
317 tmp = e1;
318 e1 = e2;
319 e2 = tmp;
320 }
321 while (e1->enumerate(e1, &c1))
322 {
323 /* check if done authentications comply to configured ones */
324 if ((!e2->enumerate(e2, &c2)) ||
325 (!strict && !c1->complies(c1, c2, TRUE)) ||
326 (strict && !c2->complies(c2, c1, TRUE)))
327 {
328 complies = FALSE;
329 break;
330 }
331 }
332 e1->destroy(e1);
333 e2->destroy(e2);
334 if (complies)
335 {
336 break;
337 }
338 DBG1(DBG_CFG, "selected peer config '%s' inacceptable",
339 this->peer_cfg->get_name(this->peer_cfg));
340 this->peer_cfg->destroy(this->peer_cfg);
341 }
342 if (this->candidates->remove_first(this->candidates,
343 (void**)&this->peer_cfg) != SUCCESS)
344 {
345 DBG1(DBG_CFG, "no alternative config found");
346 this->peer_cfg = NULL;
347 }
348 else
349 {
350 DBG1(DBG_CFG, "switching to peer config '%s'",
351 this->peer_cfg->get_name(this->peer_cfg));
352 this->ike_sa->set_peer_cfg(this->ike_sa, this->peer_cfg);
353 }
354 }
355 while (this->peer_cfg);
356
357 return this->peer_cfg != NULL;
358 }
359
360 /**
361 * Implementation of task_t.build for initiator
362 */
363 static status_t build_i(private_ike_auth_t *this, message_t *message)
364 {
365 auth_cfg_t *cfg;
366
367 if (message->get_exchange_type(message) == IKE_SA_INIT)
368 {
369 return collect_my_init_data(this, message);
370 }
371
372 if (this->peer_cfg == NULL)
373 {
374 this->peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
375 this->peer_cfg->get_ref(this->peer_cfg);
376 }
377
378 if (message->get_message_id(message) == 1)
379 { /* in the first IKE_AUTH ... */
380 if (this->ike_sa->supports_extension(this->ike_sa, EXT_MULTIPLE_AUTH))
381 { /* indicate support for multiple authentication */
382 message->add_notify(message, FALSE, MULTIPLE_AUTH_SUPPORTED,
383 chunk_empty);
384 }
385 /* indicate support for EAP-only authentication */
386 message->add_notify(message, FALSE, EAP_ONLY_AUTHENTICATION,
387 chunk_empty);
388 }
389
390 if (!this->do_another_auth && !this->my_auth)
391 { /* we have done our rounds */
392 return NEED_MORE;
393 }
394
395 /* check if an authenticator is in progress */
396 if (this->my_auth == NULL)
397 {
398 identification_t *idi, *idr = NULL;
399 id_payload_t *id_payload;
400
401 /* clean up authentication config from a previous round */
402 cfg = this->ike_sa->get_auth_cfg(this->ike_sa, TRUE);
403 cfg->purge(cfg, TRUE);
404
405 /* add (optional) IDr */
406 cfg = get_auth_cfg(this, FALSE);
407 if (cfg)
408 {
409 idr = cfg->get(cfg, AUTH_RULE_IDENTITY);
410 if (idr && !idr->contains_wildcards(idr))
411 {
412 this->ike_sa->set_other_id(this->ike_sa, idr->clone(idr));
413 id_payload = id_payload_create_from_identification(
414 ID_RESPONDER, idr);
415 message->add_payload(message, (payload_t*)id_payload);
416 }
417 }
418 /* add IDi */
419 cfg = this->ike_sa->get_auth_cfg(this->ike_sa, TRUE);
420 cfg->merge(cfg, get_auth_cfg(this, TRUE), TRUE);
421 idi = cfg->get(cfg, AUTH_RULE_IDENTITY);
422 if (!idi)
423 {
424 DBG1(DBG_CFG, "configuration misses IDi");
425 return FAILED;
426 }
427 this->ike_sa->set_my_id(this->ike_sa, idi->clone(idi));
428 id_payload = id_payload_create_from_identification(ID_INITIATOR, idi);
429 get_reserved_id_bytes(this, id_payload);
430 message->add_payload(message, (payload_t*)id_payload);
431
432 if (idr && message->get_message_id(message) == 1 &&
433 this->peer_cfg->get_unique_policy(this->peer_cfg) != UNIQUE_NO)
434 {
435 host_t *host;
436
437 host = this->ike_sa->get_other_host(this->ike_sa);
438 if (!charon->ike_sa_manager->has_contact(charon->ike_sa_manager,
439 idi, idr, host->get_family(host)))
440 {
441 message->add_notify(message, FALSE, INITIAL_CONTACT, chunk_empty);
442 }
443 }
444
445 /* build authentication data */
446 this->my_auth = authenticator_create_builder(this->ike_sa, cfg,
447 this->other_nonce, this->my_nonce,
448 this->other_packet->get_data(this->other_packet),
449 this->my_packet->get_data(this->my_packet),
450 this->reserved);
451 if (!this->my_auth)
452 {
453 return FAILED;
454 }
455 }
456 switch (this->my_auth->build(this->my_auth, message))
457 {
458 case SUCCESS:
459 /* authentication step complete, reset authenticator */
460 cfg = auth_cfg_create();
461 cfg->merge(cfg, this->ike_sa->get_auth_cfg(this->ike_sa, TRUE), TRUE);
462 this->ike_sa->add_auth_cfg(this->ike_sa, TRUE, cfg);
463 this->my_auth->destroy(this->my_auth);
464 this->my_auth = NULL;
465 break;
466 case NEED_MORE:
467 break;
468 default:
469 return FAILED;
470 }
471
472 /* check for additional authentication rounds */
473 if (do_another_auth(this))
474 {
475 if (message->get_payload(message, AUTHENTICATION))
476 {
477 message->add_notify(message, FALSE, ANOTHER_AUTH_FOLLOWS, chunk_empty);
478 }
479 }
480 else
481 {
482 this->do_another_auth = FALSE;
483 }
484 return NEED_MORE;
485 }
486
487 /**
488 * Implementation of task_t.process for responder
489 */
490 static status_t process_r(private_ike_auth_t *this, message_t *message)
491 {
492 auth_cfg_t *cfg, *cand;
493 id_payload_t *id_payload;
494 identification_t *id;
495
496 if (message->get_exchange_type(message) == IKE_SA_INIT)
497 {
498 return collect_other_init_data(this, message);
499 }
500
501 if (this->my_auth == NULL && this->do_another_auth)
502 {
503 /* handle (optional) IDr payload, apply proposed identity */
504 id_payload = (id_payload_t*)message->get_payload(message, ID_RESPONDER);
505 if (id_payload)
506 {
507 id = id_payload->get_identification(id_payload);
508 }
509 else
510 {
511 id = identification_create_from_encoding(ID_ANY, chunk_empty);
512 }
513 this->ike_sa->set_my_id(this->ike_sa, id);
514 }
515
516 if (!this->expect_another_auth)
517 {
518 return NEED_MORE;
519 }
520
521 if (message->get_message_id(message) == 1)
522 { /* check for extensions in the first IKE_AUTH */
523 if (message->get_notify(message, MULTIPLE_AUTH_SUPPORTED))
524 {
525 this->ike_sa->enable_extension(this->ike_sa, EXT_MULTIPLE_AUTH);
526 }
527 if (message->get_notify(message, EAP_ONLY_AUTHENTICATION))
528 {
529 this->ike_sa->enable_extension(this->ike_sa,
530 EXT_EAP_ONLY_AUTHENTICATION);
531 }
532 }
533
534 if (this->other_auth == NULL)
535 {
536 /* handle IDi payload */
537 id_payload = (id_payload_t*)message->get_payload(message, ID_INITIATOR);
538 if (!id_payload)
539 {
540 DBG1(DBG_IKE, "IDi payload missing");
541 return FAILED;
542 }
543 id = id_payload->get_identification(id_payload);
544 get_reserved_id_bytes(this, id_payload);
545 this->ike_sa->set_other_id(this->ike_sa, id);
546 cfg = this->ike_sa->get_auth_cfg(this->ike_sa, FALSE);
547 cfg->add(cfg, AUTH_RULE_IDENTITY, id->clone(id));
548
549 if (this->peer_cfg == NULL)
550 {
551 if (!load_cfg_candidates(this))
552 {
553 this->authentication_failed = TRUE;
554 return NEED_MORE;
555 }
556 }
557 if (message->get_payload(message, AUTHENTICATION) == NULL)
558 { /* before authenticating with EAP, we need a EAP config */
559 cand = get_auth_cfg(this, FALSE);
560 while (!cand || (
561 (uintptr_t)cand->get(cand, AUTH_RULE_EAP_TYPE) == EAP_NAK &&
562 (uintptr_t)cand->get(cand, AUTH_RULE_EAP_VENDOR) == 0))
563 { /* peer requested EAP, but current config does not match */
564 DBG1(DBG_IKE, "peer requested EAP, config inacceptable");
565 this->peer_cfg->destroy(this->peer_cfg);
566 this->peer_cfg = NULL;
567 if (!update_cfg_candidates(this, FALSE))
568 {
569 this->authentication_failed = TRUE;
570 return NEED_MORE;
571 }
572 cand = get_auth_cfg(this, FALSE);
573 }
574 /* copy over the EAP specific rules for authentication */
575 cfg->add(cfg, AUTH_RULE_EAP_TYPE,
576 cand->get(cand, AUTH_RULE_EAP_TYPE));
577 cfg->add(cfg, AUTH_RULE_EAP_VENDOR,
578 cand->get(cand, AUTH_RULE_EAP_VENDOR));
579 id = (identification_t*)cand->get(cand, AUTH_RULE_EAP_IDENTITY);
580 if (id)
581 {
582 cfg->add(cfg, AUTH_RULE_EAP_IDENTITY, id->clone(id));
583 }
584 id = (identification_t*)cand->get(cand, AUTH_RULE_AAA_IDENTITY);
585 if (id)
586 {
587 cfg->add(cfg, AUTH_RULE_AAA_IDENTITY, id->clone(id));
588 }
589 }
590
591 /* verify authentication data */
592 this->other_auth = authenticator_create_verifier(this->ike_sa,
593 message, this->other_nonce, this->my_nonce,
594 this->other_packet->get_data(this->other_packet),
595 this->my_packet->get_data(this->my_packet),
596 this->reserved);
597 if (!this->other_auth)
598 {
599 this->authentication_failed = TRUE;
600 return NEED_MORE;
601 }
602 }
603 switch (this->other_auth->process(this->other_auth, message))
604 {
605 case SUCCESS:
606 this->other_auth->destroy(this->other_auth);
607 this->other_auth = NULL;
608 break;
609 case NEED_MORE:
610 if (message->get_payload(message, AUTHENTICATION))
611 { /* AUTH verification successful, but another build() needed */
612 break;
613 }
614 return NEED_MORE;
615 default:
616 this->authentication_failed = TRUE;
617 return NEED_MORE;
618 }
619
620 /* If authenticated (with non-EAP) and received INITIAL_CONTACT,
621 * delete any existing IKE_SAs with that peer. */
622 if (message->get_message_id(message) == 1 &&
623 message->get_notify(message, INITIAL_CONTACT))
624 {
625 this->initial_contact = TRUE;
626 }
627
628 /* store authentication information */
629 cfg = auth_cfg_create();
630 cfg->merge(cfg, this->ike_sa->get_auth_cfg(this->ike_sa, FALSE), FALSE);
631 this->ike_sa->add_auth_cfg(this->ike_sa, FALSE, cfg);
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 if (!update_cfg_candidates(this, FALSE))
642 {
643 this->authentication_failed = TRUE;
644 return NEED_MORE;
645 }
646
647 if (message->get_notify(message, ANOTHER_AUTH_FOLLOWS) == NULL)
648 {
649 this->expect_another_auth = FALSE;
650 if (!update_cfg_candidates(this, TRUE))
651 {
652 this->authentication_failed = TRUE;
653 return NEED_MORE;
654 }
655 }
656 return NEED_MORE;
657 }
658
659 /**
660 * Implementation of task_t.build for responder
661 */
662 static status_t build_r(private_ike_auth_t *this, message_t *message)
663 {
664 auth_cfg_t *cfg;
665
666 if (message->get_exchange_type(message) == IKE_SA_INIT)
667 {
668 if (multiple_auth_enabled())
669 {
670 message->add_notify(message, FALSE, MULTIPLE_AUTH_SUPPORTED,
671 chunk_empty);
672 }
673 return collect_my_init_data(this, message);
674 }
675
676 if (this->authentication_failed || this->peer_cfg == NULL)
677 {
678 message->add_notify(message, TRUE, AUTHENTICATION_FAILED, chunk_empty);
679 return 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 {
698 DBG1(DBG_CFG, "IDr not configured and negotiation failed");
699 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
700 chunk_empty);
701 return FAILED;
702 }
703 this->ike_sa->set_my_id(this->ike_sa, id_cfg->clone(id_cfg));
704 id = id_cfg;
705 }
706 else
707 { /* IDr received, check if it matches configuration */
708 if (id_cfg && !id->matches(id, id_cfg))
709 {
710 DBG1(DBG_CFG, "received IDr %Y, but require %Y", id, id_cfg);
711 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
712 chunk_empty);
713 return FAILED;
714 }
715 }
716
717 id_payload = id_payload_create_from_identification(ID_RESPONDER, id);
718 get_reserved_id_bytes(this, id_payload);
719 message->add_payload(message, (payload_t*)id_payload);
720
721 if (this->initial_contact)
722 {
723 charon->ike_sa_manager->check_uniqueness(charon->ike_sa_manager,
724 this->ike_sa, TRUE);
725 this->initial_contact = FALSE;
726 }
727
728 if ((uintptr_t)cfg->get(cfg, AUTH_RULE_AUTH_CLASS) == AUTH_CLASS_EAP)
729 { /* EAP-only authentication */
730 if (!this->ike_sa->supports_extension(this->ike_sa,
731 EXT_EAP_ONLY_AUTHENTICATION))
732 {
733 DBG1(DBG_IKE, "configured EAP-only authentication, but peer "
734 "does not support it");
735 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
736 chunk_empty);
737 return 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 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
751 chunk_empty);
752 return FAILED;
753 }
754 }
755 }
756
757 if (this->other_auth)
758 {
759 switch (this->other_auth->build(this->other_auth, message))
760 {
761 case SUCCESS:
762 this->other_auth->destroy(this->other_auth);
763 this->other_auth = NULL;
764 break;
765 case NEED_MORE:
766 break;
767 default:
768 if (!message->get_payload(message, EXTENSIBLE_AUTHENTICATION))
769 { /* skip AUTHENTICATION_FAILED if we have EAP_FAILURE */
770 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
771 chunk_empty);
772 }
773 return FAILED;
774 }
775 }
776 if (this->my_auth)
777 {
778 switch (this->my_auth->build(this->my_auth, message))
779 {
780 case SUCCESS:
781 cfg = auth_cfg_create();
782 cfg->merge(cfg, this->ike_sa->get_auth_cfg(this->ike_sa, TRUE),
783 TRUE);
784 this->ike_sa->add_auth_cfg(this->ike_sa, TRUE, cfg);
785 this->my_auth->destroy(this->my_auth);
786 this->my_auth = NULL;
787 break;
788 case NEED_MORE:
789 break;
790 default:
791 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
792 chunk_empty);
793 return FAILED;
794 }
795 }
796
797 /* check for additional authentication rounds */
798 if (do_another_auth(this))
799 {
800 message->add_notify(message, FALSE, ANOTHER_AUTH_FOLLOWS, chunk_empty);
801 }
802 else
803 {
804 this->do_another_auth = FALSE;
805 }
806 if (!this->do_another_auth && !this->expect_another_auth)
807 {
808 if (charon->ike_sa_manager->check_uniqueness(charon->ike_sa_manager,
809 this->ike_sa, FALSE))
810 {
811 DBG1(DBG_IKE, "cancelling IKE_SA setup due uniqueness policy");
812 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
813 chunk_empty);
814 return FAILED;
815 }
816 if (!charon->bus->authorize(charon->bus, TRUE))
817 {
818 DBG1(DBG_IKE, "final authorization hook forbids IKE_SA, cancelling");
819 message->add_notify(message, TRUE, AUTHENTICATION_FAILED,
820 chunk_empty);
821 return FAILED;
822 }
823 DBG0(DBG_IKE, "IKE_SA %s[%d] established between %H[%Y]...%H[%Y]",
824 this->ike_sa->get_name(this->ike_sa),
825 this->ike_sa->get_unique_id(this->ike_sa),
826 this->ike_sa->get_my_host(this->ike_sa),
827 this->ike_sa->get_my_id(this->ike_sa),
828 this->ike_sa->get_other_host(this->ike_sa),
829 this->ike_sa->get_other_id(this->ike_sa));
830 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
831 charon->bus->ike_updown(charon->bus, this->ike_sa, TRUE);
832 return SUCCESS;
833 }
834 return NEED_MORE;
835 }
836
837 /**
838 * Implementation of task_t.process for initiator
839 */
840 static status_t process_i(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 return 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 return 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 return FAILED;
956 }
957 this->other_auth->destroy(this->other_auth);
958 this->other_auth = NULL;
959 }
960 /* store authentication information, reset authenticator */
961 cfg = auth_cfg_create();
962 cfg->merge(cfg, this->ike_sa->get_auth_cfg(this->ike_sa, FALSE), FALSE);
963 this->ike_sa->add_auth_cfg(this->ike_sa, FALSE, cfg);
964
965 /* another auth round done, invoke authorize hook */
966 if (!charon->bus->authorize(charon->bus, FALSE))
967 {
968 DBG1(DBG_IKE, "authorization forbids IKE_SA, cancelling");
969 return FAILED;
970 }
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 return 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 return FAILED;
1011 }
1012 if (!charon->bus->authorize(charon->bus, TRUE))
1013 {
1014 DBG1(DBG_IKE, "final authorization hook forbids IKE_SA, cancelling");
1015 return 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
1031 /**
1032 * Implementation of task_t.get_type
1033 */
1034 static task_type_t get_type(private_ike_auth_t *this)
1035 {
1036 return IKE_AUTHENTICATE;
1037 }
1038
1039 /**
1040 * Implementation of task_t.migrate
1041 */
1042 static void migrate(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 /**
1066 * Implementation of task_t.destroy
1067 */
1068 static void destroy(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 = malloc_thing(private_ike_auth_t);
1087
1088 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
1089 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
1090 this->public.task.destroy = (void(*)(task_t*))destroy;
1091
1092 if (initiator)
1093 {
1094 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
1095 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
1096 }
1097 else
1098 {
1099 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
1100 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
1101 }
1102
1103 this->ike_sa = ike_sa;
1104 this->initiator = initiator;
1105 this->my_nonce = chunk_empty;
1106 this->other_nonce = chunk_empty;
1107 this->my_packet = NULL;
1108 this->other_packet = NULL;
1109 this->peer_cfg = NULL;
1110 this->candidates = linked_list_create();
1111 this->my_auth = NULL;
1112 this->other_auth = NULL;
1113 this->do_another_auth = TRUE;
1114 this->expect_another_auth = TRUE;
1115 this->authentication_failed = FALSE;
1116
1117 return &this->public;
1118 }
1119