Copy EAP specific attributes to auth config only
[strongswan.git] / src / libcharon / sa / authenticators / eap_authenticator.c
1 /*
2 * Copyright (C) 2006-2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "eap_authenticator.h"
17
18 #include <daemon.h>
19 #include <sa/authenticators/eap/eap_method.h>
20 #include <encoding/payloads/auth_payload.h>
21 #include <encoding/payloads/eap_payload.h>
22
23 typedef struct private_eap_authenticator_t private_eap_authenticator_t;
24
25 /**
26 * Private data of an eap_authenticator_t object.
27 */
28 struct private_eap_authenticator_t {
29
30 /**
31 * Public authenticator_t interface.
32 */
33 eap_authenticator_t public;
34
35 /**
36 * Assigned IKE_SA
37 */
38 ike_sa_t *ike_sa;
39
40 /**
41 * others nonce to include in AUTH calculation
42 */
43 chunk_t received_nonce;
44
45 /**
46 * our nonce to include in AUTH calculation
47 */
48 chunk_t sent_nonce;
49
50 /**
51 * others IKE_SA_INIT message data to include in AUTH calculation
52 */
53 chunk_t received_init;
54
55 /**
56 * our IKE_SA_INIT message data to include in AUTH calculation
57 */
58 chunk_t sent_init;
59
60 /**
61 * Current EAP method processing
62 */
63 eap_method_t *method;
64
65 /**
66 * MSK used to build and verify auth payload
67 */
68 chunk_t msk;
69
70 /**
71 * EAP authentication method completed successfully
72 */
73 bool eap_complete;
74
75 /**
76 * Set if we require mutual EAP due EAP-only authentication
77 */
78 bool require_mutual;
79
80 /**
81 * authentication payload verified successfully
82 */
83 bool auth_complete;
84
85 /**
86 * generated EAP payload
87 */
88 eap_payload_t *eap_payload;
89
90 /**
91 * EAP identity of peer
92 */
93 identification_t *eap_identity;
94 };
95
96 /**
97 * load an EAP method
98 */
99 static eap_method_t *load_method(private_eap_authenticator_t *this,
100 eap_type_t type, u_int32_t vendor, eap_role_t role)
101 {
102 identification_t *server, *peer;
103
104 if (role == EAP_SERVER)
105 {
106 server = this->ike_sa->get_my_id(this->ike_sa);
107 peer = this->ike_sa->get_other_id(this->ike_sa);
108 }
109 else
110 {
111 server = this->ike_sa->get_other_id(this->ike_sa);
112 peer = this->ike_sa->get_my_id(this->ike_sa);
113 }
114 if (this->eap_identity)
115 {
116 peer = this->eap_identity;
117 }
118 return charon->eap->create_instance(charon->eap, type, vendor,
119 role, server, peer);
120 }
121
122 /**
123 * Initiate EAP conversation as server
124 */
125 static eap_payload_t* server_initiate_eap(private_eap_authenticator_t *this,
126 bool do_identity)
127 {
128 auth_cfg_t *auth;
129 eap_type_t type;
130 identification_t *id;
131 u_int32_t vendor;
132 eap_payload_t *out;
133 char *action;
134
135 auth = this->ike_sa->get_auth_cfg(this->ike_sa, FALSE);
136
137 /* initiate EAP-Identity exchange if required */
138 if (!this->eap_identity && do_identity)
139 {
140 id = auth->get(auth, AUTH_RULE_EAP_IDENTITY);
141 if (id)
142 {
143 this->method = load_method(this, EAP_IDENTITY, 0, EAP_SERVER);
144 if (this->method)
145 {
146 if (this->method->initiate(this->method, &out) == NEED_MORE)
147 {
148 DBG1(DBG_IKE, "initiating EAP-Identity request");
149 return out;
150 }
151 this->method->destroy(this->method);
152 }
153 DBG1(DBG_IKE, "EAP-Identity request configured, but not supported");
154 }
155 }
156 /* invoke real EAP method */
157 type = (uintptr_t)auth->get(auth, AUTH_RULE_EAP_TYPE);
158 vendor = (uintptr_t)auth->get(auth, AUTH_RULE_EAP_VENDOR);
159 action = "loading";
160 this->method = load_method(this, type, vendor, EAP_SERVER);
161 if (this->method)
162 {
163 action = "initiating";
164 if (this->method->initiate(this->method, &out) == NEED_MORE)
165 {
166 if (vendor)
167 {
168 DBG1(DBG_IKE, "initiating EAP vendor type %d-%d method",
169 type, vendor);
170 }
171 else
172 {
173 DBG1(DBG_IKE, "initiating %N method", eap_type_names, type);
174 }
175 return out;
176 }
177 }
178 if (vendor)
179 {
180 DBG1(DBG_IKE, "%s EAP vendor type %d-%d method failed",
181 action, type, vendor);
182 }
183 else
184 {
185 DBG1(DBG_IKE, "%s %N method failed", action, eap_type_names, type);
186 }
187 return eap_payload_create_code(EAP_FAILURE, 0);
188 }
189
190 /**
191 * Replace the existing EAP-Identity in other auth config
192 */
193 static void replace_eap_identity(private_eap_authenticator_t *this)
194 {
195 enumerator_t *enumerator;
196 auth_rule_t rule;
197 auth_cfg_t *cfg;
198 void *ptr;
199
200 cfg = this->ike_sa->get_auth_cfg(this->ike_sa, FALSE);
201 enumerator = cfg->create_enumerator(cfg);
202 while (enumerator->enumerate(enumerator, &rule, &ptr))
203 {
204 if (rule == AUTH_RULE_EAP_IDENTITY)
205 {
206 cfg->replace(cfg, enumerator, AUTH_RULE_EAP_IDENTITY,
207 this->eap_identity->clone(this->eap_identity));
208 break;
209 }
210 }
211 enumerator->destroy(enumerator);
212 }
213
214 /**
215 * Handle EAP exchange as server
216 */
217 static eap_payload_t* server_process_eap(private_eap_authenticator_t *this,
218 eap_payload_t *in)
219 {
220 eap_type_t type, received_type;
221 u_int32_t vendor, received_vendor;
222 eap_payload_t *out;
223
224 if (in->get_code(in) != EAP_RESPONSE)
225 {
226 DBG1(DBG_IKE, "received %N, sending %N",
227 eap_code_names, in->get_code(in), eap_code_names, EAP_FAILURE);
228 return eap_payload_create_code(EAP_FAILURE, in->get_identifier(in));
229 }
230
231 type = this->method->get_type(this->method, &vendor);
232 received_type = in->get_type(in, &received_vendor);
233 if (type != received_type || vendor != received_vendor)
234 {
235 if (received_vendor == 0 && received_type == EAP_NAK)
236 {
237 DBG1(DBG_IKE, "received %N, sending %N",
238 eap_type_names, EAP_NAK, eap_code_names, EAP_FAILURE);
239 }
240 else
241 {
242 DBG1(DBG_IKE, "received invalid EAP response, sending %N",
243 eap_code_names, EAP_FAILURE);
244 }
245 return eap_payload_create_code(EAP_FAILURE, in->get_identifier(in));
246 }
247
248 switch (this->method->process(this->method, in, &out))
249 {
250 case NEED_MORE:
251 return out;
252 case SUCCESS:
253 if (!vendor && type == EAP_IDENTITY)
254 {
255 chunk_t data;
256
257 if (this->method->get_msk(this->method, &data) == SUCCESS)
258 {
259 this->eap_identity = identification_create_from_data(data);
260 DBG1(DBG_IKE, "received EAP identity '%Y'",
261 this->eap_identity);
262 replace_eap_identity(this);
263 }
264 /* restart EAP exchange, but with real method */
265 this->method->destroy(this->method);
266 return server_initiate_eap(this, FALSE);
267 }
268 if (this->method->get_msk(this->method, &this->msk) == SUCCESS)
269 {
270 this->msk = chunk_clone(this->msk);
271 }
272 if (vendor)
273 {
274 DBG1(DBG_IKE, "EAP vendor specific method %d-%d succeeded, "
275 "%sMSK established", type, vendor,
276 this->msk.ptr ? "" : "no ");
277 }
278 else
279 {
280 DBG1(DBG_IKE, "EAP method %N succeeded, %sMSK established",
281 eap_type_names, type, this->msk.ptr ? "" : "no ");
282 }
283 this->ike_sa->set_condition(this->ike_sa, COND_EAP_AUTHENTICATED,
284 TRUE);
285 this->eap_complete = TRUE;
286 return eap_payload_create_code(EAP_SUCCESS, in->get_identifier(in));
287 case FAILED:
288 default:
289 if (vendor)
290 {
291 DBG1(DBG_IKE, "EAP vendor specific method %d-%d failed for "
292 "peer %Y", type, vendor,
293 this->ike_sa->get_other_id(this->ike_sa));
294 }
295 else
296 {
297 DBG1(DBG_IKE, "EAP method %N failed for peer %Y",
298 eap_type_names, type,
299 this->ike_sa->get_other_id(this->ike_sa));
300 }
301 return eap_payload_create_code(EAP_FAILURE, in->get_identifier(in));
302 }
303 }
304
305 /**
306 * Processing method for a peer
307 */
308 static eap_payload_t* client_process_eap(private_eap_authenticator_t *this,
309 eap_payload_t *in)
310 {
311 eap_type_t type;
312 u_int32_t vendor;
313 auth_cfg_t *auth;
314 eap_payload_t *out;
315 identification_t *id;
316
317 type = in->get_type(in, &vendor);
318
319 if (!vendor && type == EAP_IDENTITY)
320 {
321 DESTROY_IF(this->eap_identity);
322 auth = this->ike_sa->get_auth_cfg(this->ike_sa, TRUE);
323 id = auth->get(auth, AUTH_RULE_EAP_IDENTITY);
324 if (!id || id->get_type(id) == ID_ANY)
325 {
326 id = this->ike_sa->get_my_id(this->ike_sa);
327 }
328 DBG1(DBG_IKE, "server requested %N, sending '%Y'",
329 eap_type_names, type, id);
330 this->eap_identity = id->clone(id);
331
332 this->method = load_method(this, type, vendor, EAP_PEER);
333 if (this->method)
334 {
335 if (this->method->process(this->method, in, &out) == SUCCESS)
336 {
337 this->method->destroy(this->method);
338 this->method = NULL;
339 return out;
340 }
341 this->method->destroy(this->method);
342 this->method = NULL;
343 }
344 DBG1(DBG_IKE, "%N not supported, sending EAP_NAK",
345 eap_type_names, type);
346 return eap_payload_create_nak(in->get_identifier(in));
347 }
348 if (this->method == NULL)
349 {
350 if (vendor)
351 {
352 DBG1(DBG_IKE, "server requested vendor specific EAP method %d-%d",
353 type, vendor);
354 }
355 else
356 {
357 DBG1(DBG_IKE, "server requested %N authentication",
358 eap_type_names, type);
359 }
360 this->method = load_method(this, type, vendor, EAP_PEER);
361 if (!this->method)
362 {
363 DBG1(DBG_IKE, "EAP method not supported, sending EAP_NAK");
364 return eap_payload_create_nak(in->get_identifier(in));
365 }
366 }
367
368 type = this->method->get_type(this->method, &vendor);
369
370 if (this->method->process(this->method, in, &out) == NEED_MORE)
371 { /* client methods should never return SUCCESS */
372 return out;
373 }
374
375 if (vendor)
376 {
377 DBG1(DBG_IKE, "vendor specific EAP method %d-%d failed", type, vendor);
378 }
379 else
380 {
381 DBG1(DBG_IKE, "%N method failed", eap_type_names, type);
382 }
383 return NULL;
384 }
385
386 /**
387 * Verify AUTH payload
388 */
389 static bool verify_auth(private_eap_authenticator_t *this, message_t *message,
390 chunk_t nonce, chunk_t init)
391 {
392 auth_payload_t *auth_payload;
393 chunk_t auth_data, recv_auth_data;
394 identification_t *other_id;
395 auth_cfg_t *auth;
396 keymat_t *keymat;
397
398 auth_payload = (auth_payload_t*)message->get_payload(message,
399 AUTHENTICATION);
400 if (!auth_payload)
401 {
402 DBG1(DBG_IKE, "AUTH payload missing");
403 return FALSE;
404 }
405 other_id = this->ike_sa->get_other_id(this->ike_sa);
406 keymat = this->ike_sa->get_keymat(this->ike_sa);
407 auth_data = keymat->get_psk_sig(keymat, TRUE, init, nonce,
408 this->msk, other_id);
409 recv_auth_data = auth_payload->get_data(auth_payload);
410 if (!auth_data.len || !chunk_equals(auth_data, recv_auth_data))
411 {
412 DBG1(DBG_IKE, "verification of AUTH payload with%s EAP MSK failed",
413 this->msk.ptr ? "" : "out");
414 chunk_free(&auth_data);
415 return FALSE;
416 }
417 chunk_free(&auth_data);
418
419 DBG1(DBG_IKE, "authentication of '%Y' with %N successful",
420 other_id, auth_class_names, AUTH_CLASS_EAP);
421 this->auth_complete = TRUE;
422 auth = this->ike_sa->get_auth_cfg(this->ike_sa, FALSE);
423 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_EAP);
424 return TRUE;
425 }
426
427 /**
428 * Build AUTH payload
429 */
430 static void build_auth(private_eap_authenticator_t *this, message_t *message,
431 chunk_t nonce, chunk_t init)
432 {
433 auth_payload_t *auth_payload;
434 identification_t *my_id;
435 chunk_t auth_data;
436 keymat_t *keymat;
437
438 my_id = this->ike_sa->get_my_id(this->ike_sa);
439 keymat = this->ike_sa->get_keymat(this->ike_sa);
440
441 DBG1(DBG_IKE, "authentication of '%Y' (myself) with %N",
442 my_id, auth_class_names, AUTH_CLASS_EAP);
443
444 auth_data = keymat->get_psk_sig(keymat, FALSE, init, nonce, this->msk, my_id);
445 auth_payload = auth_payload_create();
446 auth_payload->set_auth_method(auth_payload, AUTH_PSK);
447 auth_payload->set_data(auth_payload, auth_data);
448 message->add_payload(message, (payload_t*)auth_payload);
449 chunk_free(&auth_data);
450 }
451
452 /**
453 * Implementation of authenticator_t.process for a server
454 */
455 static status_t process_server(private_eap_authenticator_t *this,
456 message_t *message)
457 {
458 eap_payload_t *eap_payload;
459
460 if (this->eap_complete)
461 {
462 if (!verify_auth(this, message, this->sent_nonce, this->received_init))
463 {
464 return FAILED;
465 }
466 return NEED_MORE;
467 }
468
469 if (!this->method)
470 {
471 this->eap_payload = server_initiate_eap(this, TRUE);
472 }
473 else
474 {
475 eap_payload = (eap_payload_t*)message->get_payload(message,
476 EXTENSIBLE_AUTHENTICATION);
477 if (!eap_payload)
478 {
479 return FAILED;
480 }
481 this->eap_payload = server_process_eap(this, eap_payload);
482 }
483 return NEED_MORE;
484 }
485
486 /**
487 * Implementation of authenticator_t.build for a server
488 */
489 static status_t build_server(private_eap_authenticator_t *this,
490 message_t *message)
491 {
492 if (this->eap_payload)
493 {
494 eap_code_t code;
495
496 code = this->eap_payload->get_code(this->eap_payload);
497 message->add_payload(message, (payload_t*)this->eap_payload);
498 this->eap_payload = NULL;
499 if (code == EAP_FAILURE)
500 {
501 return FAILED;
502 }
503 return NEED_MORE;
504 }
505 if (this->eap_complete && this->auth_complete)
506 {
507 build_auth(this, message, this->received_nonce, this->sent_init);
508 return SUCCESS;
509 }
510 return FAILED;
511 }
512
513 /**
514 * Implementation of authenticator_t.process for a client
515 */
516 static status_t process_client(private_eap_authenticator_t *this,
517 message_t *message)
518 {
519 eap_payload_t *eap_payload;
520
521 if (this->eap_complete)
522 {
523 if (!verify_auth(this, message, this->sent_nonce, this->received_init))
524 {
525 return FAILED;
526 }
527 if (this->require_mutual && !this->method->is_mutual(this->method))
528 { /* we require mutual authentication due to EAP-only */
529 u_int32_t vendor;
530
531 DBG1(DBG_IKE, "EAP-only authentication requires a mutual and "
532 "MSK deriving EAP method, but %N is not",
533 eap_type_names, this->method->get_type(this->method, &vendor));
534 return FAILED;
535 }
536 return SUCCESS;
537 }
538
539 eap_payload = (eap_payload_t*)message->get_payload(message,
540 EXTENSIBLE_AUTHENTICATION);
541 if (eap_payload)
542 {
543 switch (eap_payload->get_code(eap_payload))
544 {
545 case EAP_REQUEST:
546 {
547 this->eap_payload = client_process_eap(this, eap_payload);
548 if (this->eap_payload)
549 {
550 return NEED_MORE;
551 }
552 return FAILED;
553 }
554 case EAP_SUCCESS:
555 {
556 eap_type_t type;
557 u_int32_t vendor;
558 auth_cfg_t *cfg;
559
560 if (this->method->get_msk(this->method, &this->msk) == SUCCESS)
561 {
562 this->msk = chunk_clone(this->msk);
563 }
564 type = this->method->get_type(this->method, &vendor);
565 if (vendor)
566 {
567 DBG1(DBG_IKE, "EAP vendor specific method %d-%d succeeded, "
568 "%sMSK established", type, vendor,
569 this->msk.ptr ? "" : "no ");
570 }
571 else
572 {
573 DBG1(DBG_IKE, "EAP method %N succeeded, %sMSK established",
574 eap_type_names, type, this->msk.ptr ? "" : "no ");
575 }
576 cfg = this->ike_sa->get_auth_cfg(this->ike_sa, TRUE);
577 cfg->add(cfg, AUTH_RULE_EAP_TYPE, type);
578 if (vendor)
579 {
580 cfg->add(cfg, AUTH_RULE_EAP_VENDOR, vendor);
581 }
582 this->eap_complete = TRUE;
583 return NEED_MORE;
584 }
585 case EAP_FAILURE:
586 default:
587 {
588 DBG1(DBG_IKE, "received %N, EAP authentication failed",
589 eap_code_names, eap_payload->get_code(eap_payload));
590 return FAILED;
591 }
592 }
593 }
594 return FAILED;
595 }
596
597 /**
598 * Implementation of authenticator_t.build for a client
599 */
600 static status_t build_client(private_eap_authenticator_t *this,
601 message_t *message)
602 {
603 if (this->eap_payload)
604 {
605 message->add_payload(message, (payload_t*)this->eap_payload);
606 this->eap_payload = NULL;
607 return NEED_MORE;
608 }
609 if (this->eap_complete)
610 {
611 build_auth(this, message, this->received_nonce, this->sent_init);
612 return NEED_MORE;
613 }
614 return NEED_MORE;
615 }
616
617 /**
618 * Implementation of authenticator_t.is_mutual.
619 */
620 static bool is_mutual(private_eap_authenticator_t *this)
621 {
622 /* we don't know yet, but insist on it after EAP is complete */
623 this->require_mutual = TRUE;
624 return TRUE;
625 }
626
627 /**
628 * Implementation of authenticator_t.destroy.
629 */
630 static void destroy(private_eap_authenticator_t *this)
631 {
632 DESTROY_IF(this->method);
633 DESTROY_IF(this->eap_payload);
634 DESTROY_IF(this->eap_identity);
635 chunk_free(&this->msk);
636 free(this);
637 }
638
639 /*
640 * Described in header.
641 */
642 eap_authenticator_t *eap_authenticator_create_builder(ike_sa_t *ike_sa,
643 chunk_t received_nonce, chunk_t sent_nonce,
644 chunk_t received_init, chunk_t sent_init)
645 {
646 private_eap_authenticator_t *this = malloc_thing(private_eap_authenticator_t);
647
648 this->public.authenticator.build = (status_t(*)(authenticator_t*, message_t *message))build_client;
649 this->public.authenticator.process = (status_t(*)(authenticator_t*, message_t *message))process_client;
650 this->public.authenticator.is_mutual = (bool(*)(authenticator_t*))is_mutual;
651 this->public.authenticator.destroy = (void(*)(authenticator_t*))destroy;
652
653 this->ike_sa = ike_sa;
654 this->received_init = received_init;
655 this->received_nonce = received_nonce;
656 this->sent_init = sent_init;
657 this->sent_nonce = sent_nonce;
658 this->msk = chunk_empty;
659 this->method = NULL;
660 this->eap_payload = NULL;
661 this->eap_complete = FALSE;
662 this->auth_complete = FALSE;
663 this->eap_identity = NULL;
664 this->require_mutual = FALSE;
665
666 return &this->public;
667 }
668
669 /*
670 * Described in header.
671 */
672 eap_authenticator_t *eap_authenticator_create_verifier(ike_sa_t *ike_sa,
673 chunk_t received_nonce, chunk_t sent_nonce,
674 chunk_t received_init, chunk_t sent_init)
675 {
676 private_eap_authenticator_t *this = malloc_thing(private_eap_authenticator_t);
677
678 this->public.authenticator.build = (status_t(*)(authenticator_t*, message_t *messageh))build_server;
679 this->public.authenticator.process = (status_t(*)(authenticator_t*, message_t *message))process_server;
680 this->public.authenticator.is_mutual = (bool(*)(authenticator_t*))is_mutual;
681 this->public.authenticator.destroy = (void(*)(authenticator_t*))destroy;
682
683 this->ike_sa = ike_sa;
684 this->received_init = received_init;
685 this->received_nonce = received_nonce;
686 this->sent_init = sent_init;
687 this->sent_nonce = sent_nonce;
688 this->msk = chunk_empty;
689 this->method = NULL;
690 this->eap_payload = NULL;
691 this->eap_complete = FALSE;
692 this->auth_complete = FALSE;
693 this->eap_identity = NULL;
694 this->require_mutual = FALSE;
695
696 return &this->public;
697 }
698