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