2 * EAP peer method: EAP-TTLS (draft-ietf-pppext-eap-ttls-03.txt)
3 * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
18 #include "eap_peer/eap_i.h"
19 #include "eap_peer/eap_tls_common.h"
20 #include "eap_peer/eap_config.h"
23 #include "eap_common/chap.h"
26 #include "eap_common/eap_ttls.h"
29 /* Maximum supported TTLS version
30 * 0 = draft-ietf-pppext-eap-ttls-03.txt / draft-funk-eap-ttls-v0-00.txt
31 * 1 = draft-funk-eap-ttls-v1-00.txt
33 #ifndef EAP_TTLS_VERSION
34 #define EAP_TTLS_VERSION 0 /* TTLSv1 implementation is not yet complete */
35 #endif /* EAP_TTLS_VERSION */
38 #define MSCHAPV2_KEY_LEN 16
39 #define MSCHAPV2_NT_RESPONSE_LEN 24
42 static void eap_ttls_deinit(struct eap_sm *sm, void *priv);
45 struct eap_ttls_data {
46 struct eap_ssl_data ssl;
49 int ttls_version, force_ttls_version;
51 const struct eap_method *phase2_method;
58 EAP_TTLS_PHASE2_MSCHAPV2,
59 EAP_TTLS_PHASE2_MSCHAP,
63 struct eap_method_type phase2_eap_type;
64 struct eap_method_type *phase2_eap_types;
65 size_t num_phase2_eap_types;
67 u8 auth_response[MSCHAPV2_AUTH_RESPONSE_LEN];
68 int auth_response_valid;
69 u8 master_key[MSCHAPV2_MASTER_KEY_LEN]; /* MSCHAPv2 master key */
71 int resuming; /* starting a resumed session */
72 int reauth; /* reauthentication */
75 struct wpabuf *pending_phase2_req;
84 static void * eap_ttls_init(struct eap_sm *sm)
86 struct eap_ttls_data *data;
87 struct eap_peer_config *config = eap_get_config(sm);
90 data = os_zalloc(sizeof(*data));
93 data->ttls_version = EAP_TTLS_VERSION;
94 data->force_ttls_version = -1;
96 data->phase2_type = EAP_TTLS_PHASE2_EAP;
98 #if EAP_TTLS_VERSION > 0
99 if (config && config->phase1) {
100 const char *pos = os_strstr(config->phase1, "ttlsver=");
102 data->force_ttls_version = atoi(pos + 8);
103 data->ttls_version = data->force_ttls_version;
104 wpa_printf(MSG_DEBUG, "EAP-TTLS: Forced TTLS version "
105 "%d", data->force_ttls_version);
108 #endif /* EAP_TTLS_VERSION */
110 if (config && config->phase2) {
111 if (os_strstr(config->phase2, "autheap=")) {
113 data->phase2_type = EAP_TTLS_PHASE2_EAP;
114 } else if (os_strstr(config->phase2, "auth=MSCHAPV2")) {
115 selected = "MSCHAPV2";
116 data->phase2_type = EAP_TTLS_PHASE2_MSCHAPV2;
117 } else if (os_strstr(config->phase2, "auth=MSCHAP")) {
119 data->phase2_type = EAP_TTLS_PHASE2_MSCHAP;
120 } else if (os_strstr(config->phase2, "auth=PAP")) {
122 data->phase2_type = EAP_TTLS_PHASE2_PAP;
123 } else if (os_strstr(config->phase2, "auth=CHAP")) {
125 data->phase2_type = EAP_TTLS_PHASE2_CHAP;
128 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase2 type: %s", selected);
130 if (data->phase2_type == EAP_TTLS_PHASE2_EAP) {
131 if (eap_peer_select_phase2_methods(config, "autheap=",
132 &data->phase2_eap_types,
133 &data->num_phase2_eap_types)
135 eap_ttls_deinit(sm, data);
139 data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
140 data->phase2_eap_type.method = EAP_TYPE_NONE;
143 #if EAP_TTLS_VERSION > 0
144 if (!(tls_capabilities(sm->ssl_ctx) & TLS_CAPABILITY_IA) &&
145 data->ttls_version > 0) {
146 if (data->force_ttls_version > 0) {
147 wpa_printf(MSG_INFO, "EAP-TTLS: Forced TTLSv%d and "
148 "TLS library does not support TLS/IA.",
149 data->force_ttls_version);
150 eap_ttls_deinit(sm, data);
153 data->ttls_version = 0;
155 #endif /* EAP_TTLS_VERSION */
161 static void eap_ttls_phase2_eap_deinit(struct eap_sm *sm,
162 struct eap_ttls_data *data)
164 if (data->phase2_priv && data->phase2_method) {
165 data->phase2_method->deinit(sm, data->phase2_priv);
166 data->phase2_method = NULL;
167 data->phase2_priv = NULL;
172 static void eap_ttls_deinit(struct eap_sm *sm, void *priv)
174 struct eap_ttls_data *data = priv;
177 eap_ttls_phase2_eap_deinit(sm, data);
178 os_free(data->phase2_eap_types);
179 if (data->ssl_initialized)
180 eap_peer_tls_ssl_deinit(sm, &data->ssl);
181 os_free(data->key_data);
182 wpabuf_free(data->pending_phase2_req);
187 static u8 * eap_ttls_avp_hdr(u8 *avphdr, u32 avp_code, u32 vendor_id,
188 int mandatory, size_t len)
190 struct ttls_avp_vendor *avp;
194 avp = (struct ttls_avp_vendor *) avphdr;
195 flags = mandatory ? AVP_FLAGS_MANDATORY : 0;
197 flags |= AVP_FLAGS_VENDOR;
198 hdrlen = sizeof(*avp);
199 avp->vendor_id = host_to_be32(vendor_id);
201 hdrlen = sizeof(struct ttls_avp);
204 avp->avp_code = host_to_be32(avp_code);
205 avp->avp_length = host_to_be32((flags << 24) | (hdrlen + len));
207 return avphdr + hdrlen;
211 static u8 * eap_ttls_avp_add(u8 *start, u8 *avphdr, u32 avp_code,
212 u32 vendor_id, int mandatory,
213 const u8 *data, size_t len)
216 pos = eap_ttls_avp_hdr(avphdr, avp_code, vendor_id, mandatory, len);
217 os_memcpy(pos, data, len);
224 static int eap_ttls_avp_encapsulate(struct wpabuf **resp, u32 avp_code,
230 msg = wpabuf_alloc(sizeof(struct ttls_avp) + wpabuf_len(*resp) + 4);
237 avp = wpabuf_mhead(msg);
238 pos = eap_ttls_avp_hdr(avp, avp_code, 0, mandatory, wpabuf_len(*resp));
239 os_memcpy(pos, wpabuf_head(*resp), wpabuf_len(*resp));
240 pos += wpabuf_len(*resp);
243 wpabuf_put(msg, pos - avp);
249 #if EAP_TTLS_VERSION > 0
250 static int eap_ttls_ia_permute_inner_secret(struct eap_sm *sm,
251 struct eap_ttls_data *data,
252 const u8 *key, size_t key_len)
259 buf_len = 2 + key_len;
260 buf = os_malloc(buf_len);
263 WPA_PUT_BE16(buf, key_len);
264 os_memcpy(buf + 2, key, key_len);
270 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Session keys for TLS/IA inner "
271 "secret permutation", buf, buf_len);
272 ret = tls_connection_ia_permute_inner_secret(sm->ssl_ctx,
279 #endif /* EAP_TTLS_VERSION */
282 static int eap_ttls_v0_derive_key(struct eap_sm *sm,
283 struct eap_ttls_data *data)
285 os_free(data->key_data);
286 data->key_data = eap_peer_tls_derive_key(sm, &data->ssl,
287 "ttls keying material",
289 if (!data->key_data) {
290 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to derive key");
294 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
295 data->key_data, EAP_TLS_KEY_LEN);
301 #if EAP_TTLS_VERSION > 0
302 static int eap_ttls_v1_derive_key(struct eap_sm *sm,
303 struct eap_ttls_data *data)
305 struct tls_keys keys;
308 os_free(data->key_data);
309 data->key_data = NULL;
311 os_memset(&keys, 0, sizeof(keys));
312 if (tls_connection_get_keys(sm->ssl_ctx, data->ssl.conn, &keys) ||
313 keys.client_random == NULL || keys.server_random == NULL ||
314 keys.inner_secret == NULL) {
315 wpa_printf(MSG_INFO, "EAP-TTLS: Could not get inner secret, "
316 "client random, or server random to derive keying "
321 rnd = os_malloc(keys.client_random_len + keys.server_random_len);
322 data->key_data = os_malloc(EAP_TLS_KEY_LEN);
323 if (rnd == NULL || data->key_data == NULL) {
324 wpa_printf(MSG_INFO, "EAP-TTLS: No memory for key derivation");
326 os_free(data->key_data);
327 data->key_data = NULL;
330 os_memcpy(rnd, keys.client_random, keys.client_random_len);
331 os_memcpy(rnd + keys.client_random_len, keys.server_random,
332 keys.server_random_len);
334 if (tls_prf(keys.inner_secret, keys.inner_secret_len,
335 "ttls v1 keying material", rnd, keys.client_random_len +
336 keys.server_random_len, data->key_data, EAP_TLS_KEY_LEN)) {
337 wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to derive key");
339 os_free(data->key_data);
340 data->key_data = NULL;
344 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: client/server random",
345 rnd, keys.client_random_len + keys.server_random_len);
346 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: TLS/IA inner secret",
347 keys.inner_secret, keys.inner_secret_len);
351 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
352 data->key_data, EAP_TLS_KEY_LEN);
356 #endif /* EAP_TTLS_VERSION */
359 static u8 * eap_ttls_implicit_challenge(struct eap_sm *sm,
360 struct eap_ttls_data *data, size_t len)
362 #if EAP_TTLS_VERSION > 0
363 struct tls_keys keys;
365 #endif /* EAP_TTLS_VERSION */
367 if (data->ttls_version == 0) {
368 return eap_peer_tls_derive_key(sm, &data->ssl,
369 "ttls challenge", len);
372 #if EAP_TTLS_VERSION > 0
374 os_memset(&keys, 0, sizeof(keys));
375 if (tls_connection_get_keys(sm->ssl_ctx, data->ssl.conn, &keys) ||
376 keys.client_random == NULL || keys.server_random == NULL ||
377 keys.inner_secret == NULL) {
378 wpa_printf(MSG_INFO, "EAP-TTLS: Could not get inner secret, "
379 "client random, or server random to derive "
380 "implicit challenge");
384 rnd = os_malloc(keys.client_random_len + keys.server_random_len);
385 challenge = os_malloc(len);
386 if (rnd == NULL || challenge == NULL) {
387 wpa_printf(MSG_INFO, "EAP-TTLS: No memory for implicit "
388 "challenge derivation");
393 os_memcpy(rnd, keys.server_random, keys.server_random_len);
394 os_memcpy(rnd + keys.server_random_len, keys.client_random,
395 keys.client_random_len);
397 if (tls_prf(keys.inner_secret, keys.inner_secret_len,
398 "inner application challenge", rnd,
399 keys.client_random_len + keys.server_random_len,
401 wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to derive implicit "
410 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived implicit challenge",
415 #else /* EAP_TTLS_VERSION */
419 #endif /* EAP_TTLS_VERSION */
423 static void eap_ttlsv1_phase2_eap_finish(struct eap_sm *sm,
424 struct eap_ttls_data *data,
425 struct eap_method_ret *ret)
427 #if EAP_TTLS_VERSION > 0
428 if (data->ttls_version > 0) {
429 const struct eap_method *m = data->phase2_method;
430 void *priv = data->phase2_priv;
432 /* TTLSv1 requires TLS/IA FinalPhaseFinished */
433 if (ret->decision == DECISION_UNCOND_SUCC)
434 ret->decision = DECISION_COND_SUCC;
435 ret->methodState = METHOD_CONT;
437 if (ret->decision == DECISION_COND_SUCC &&
438 m->isKeyAvailable && m->getKey &&
439 m->isKeyAvailable(sm, priv)) {
442 key = m->getKey(sm, priv, &key_len);
444 eap_ttls_ia_permute_inner_secret(
445 sm, data, key, key_len);
450 #endif /* EAP_TTLS_VERSION */
454 static void eap_ttls_phase2_select_eap_method(struct eap_ttls_data *data,
458 for (i = 0; i < data->num_phase2_eap_types; i++) {
459 if (data->phase2_eap_types[i].vendor != EAP_VENDOR_IETF ||
460 data->phase2_eap_types[i].method != method)
463 data->phase2_eap_type.vendor =
464 data->phase2_eap_types[i].vendor;
465 data->phase2_eap_type.method =
466 data->phase2_eap_types[i].method;
467 wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
468 "Phase 2 EAP vendor %d method %d",
469 data->phase2_eap_type.vendor,
470 data->phase2_eap_type.method);
476 static int eap_ttls_phase2_eap_process(struct eap_sm *sm,
477 struct eap_ttls_data *data,
478 struct eap_method_ret *ret,
479 struct eap_hdr *hdr, size_t len,
480 struct wpabuf **resp)
483 struct eap_method_ret iret;
485 os_memset(&iret, 0, sizeof(iret));
486 wpabuf_set(&msg, hdr, len);
487 *resp = data->phase2_method->process(sm, data->phase2_priv, &iret,
489 if ((iret.methodState == METHOD_DONE ||
490 iret.methodState == METHOD_MAY_CONT) &&
491 (iret.decision == DECISION_UNCOND_SUCC ||
492 iret.decision == DECISION_COND_SUCC ||
493 iret.decision == DECISION_FAIL)) {
494 ret->methodState = iret.methodState;
495 ret->decision = iret.decision;
497 eap_ttlsv1_phase2_eap_finish(sm, data, ret);
503 static int eap_ttls_phase2_request_eap_method(struct eap_sm *sm,
504 struct eap_ttls_data *data,
505 struct eap_method_ret *ret,
506 struct eap_hdr *hdr, size_t len,
507 u8 method, struct wpabuf **resp)
510 if (data->tnc_started && data->phase2_method &&
511 data->phase2_priv && method == EAP_TYPE_TNC &&
512 data->phase2_eap_type.method == EAP_TYPE_TNC)
513 return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len,
516 if (data->ready_for_tnc && !data->tnc_started &&
517 method == EAP_TYPE_TNC) {
518 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
520 data->tnc_started = 1;
523 if (data->tnc_started) {
524 if (data->phase2_eap_type.vendor != EAP_VENDOR_IETF ||
525 data->phase2_eap_type.method == EAP_TYPE_TNC) {
526 wpa_printf(MSG_DEBUG, "EAP-TTLS: Unexpected EAP "
527 "type %d for TNC", method);
531 data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
532 data->phase2_eap_type.method = method;
533 wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
534 "Phase 2 EAP vendor %d method %d (TNC)",
535 data->phase2_eap_type.vendor,
536 data->phase2_eap_type.method);
538 if (data->phase2_type == EAP_TTLS_PHASE2_EAP)
539 eap_ttls_phase2_eap_deinit(sm, data);
543 if (data->phase2_eap_type.vendor == EAP_VENDOR_IETF &&
544 data->phase2_eap_type.method == EAP_TYPE_NONE)
545 eap_ttls_phase2_select_eap_method(data, method);
547 if (method != data->phase2_eap_type.method || method == EAP_TYPE_NONE)
549 if (eap_peer_tls_phase2_nak(data->phase2_eap_types,
550 data->num_phase2_eap_types,
556 if (data->phase2_priv == NULL) {
557 data->phase2_method = eap_peer_get_eap_method(
558 EAP_VENDOR_IETF, method);
559 if (data->phase2_method) {
561 sm->mschapv2_full_key = 1;
562 data->phase2_priv = data->phase2_method->init(sm);
564 sm->mschapv2_full_key = 0;
567 if (data->phase2_priv == NULL || data->phase2_method == NULL) {
568 wpa_printf(MSG_INFO, "EAP-TTLS: failed to initialize "
569 "Phase 2 EAP method %d", method);
573 return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len, resp);
577 static int eap_ttls_phase2_request_eap(struct eap_sm *sm,
578 struct eap_ttls_data *data,
579 struct eap_method_ret *ret,
581 struct wpabuf **resp)
583 size_t len = be_to_host16(hdr->length);
585 struct eap_peer_config *config = eap_get_config(sm);
587 if (len <= sizeof(struct eap_hdr)) {
588 wpa_printf(MSG_INFO, "EAP-TTLS: too short "
589 "Phase 2 request (len=%lu)", (unsigned long) len);
592 pos = (u8 *) (hdr + 1);
593 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP Request: type=%d", *pos);
595 case EAP_TYPE_IDENTITY:
596 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
599 if (eap_ttls_phase2_request_eap_method(sm, data, ret, hdr, len,
606 (config->pending_req_identity || config->pending_req_password ||
607 config->pending_req_otp)) {
614 wpa_hexdump_buf(MSG_DEBUG, "EAP-TTLS: AVP encapsulate EAP Response",
616 return eap_ttls_avp_encapsulate(resp, RADIUS_ATTR_EAP_MESSAGE, 1);
620 static void eap_ttlsv1_permute_inner(struct eap_sm *sm,
621 struct eap_ttls_data *data)
623 #if EAP_TTLS_VERSION > 0
624 u8 session_key[2 * MSCHAPV2_KEY_LEN];
626 if (data->ttls_version == 0)
629 get_asymetric_start_key(data->master_key, session_key,
630 MSCHAPV2_KEY_LEN, 0, 0);
631 get_asymetric_start_key(data->master_key,
632 session_key + MSCHAPV2_KEY_LEN,
633 MSCHAPV2_KEY_LEN, 1, 0);
634 eap_ttls_ia_permute_inner_secret(sm, data, session_key,
635 sizeof(session_key));
636 #endif /* EAP_TTLS_VERSION */
640 static int eap_ttls_phase2_request_mschapv2(struct eap_sm *sm,
641 struct eap_ttls_data *data,
642 struct eap_method_ret *ret,
643 struct wpabuf **resp)
646 u8 *buf, *pos, *challenge, *peer_challenge;
647 const u8 *identity, *password;
648 size_t identity_len, password_len;
651 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAPV2 Request");
653 identity = eap_get_config_identity(sm, &identity_len);
654 password = eap_get_config_password2(sm, &password_len, &pwhash);
655 if (identity == NULL || password == NULL)
658 msg = wpabuf_alloc(identity_len + 1000);
660 wpa_printf(MSG_ERROR,
661 "EAP-TTLS/MSCHAPV2: Failed to allocate memory");
664 pos = buf = wpabuf_mhead(msg);
667 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
668 identity, identity_len);
670 /* MS-CHAP-Challenge */
671 challenge = eap_ttls_implicit_challenge(
672 sm, data, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN * 2 + 1);
673 if (challenge == NULL) {
675 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
676 "implicit challenge");
679 peer_challenge = challenge + 1 + EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
681 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
682 RADIUS_VENDOR_ID_MICROSOFT, 1,
683 challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
685 /* MS-CHAP2-Response */
686 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP2_RESPONSE,
687 RADIUS_VENDOR_ID_MICROSOFT, 1,
688 EAP_TTLS_MSCHAPV2_RESPONSE_LEN);
689 data->ident = challenge[EAP_TTLS_MSCHAPV2_CHALLENGE_LEN];
690 *pos++ = data->ident;
691 *pos++ = 0; /* Flags */
692 os_memcpy(pos, peer_challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
693 pos += EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
694 os_memset(pos, 0, 8); /* Reserved, must be zero */
696 mschapv2_derive_response(identity, identity_len, password,
697 password_len, pwhash, challenge,
698 peer_challenge, pos, data->auth_response,
700 data->auth_response_valid = 1;
702 eap_ttlsv1_permute_inner(sm, data);
708 wpabuf_put(msg, pos - buf);
711 if (sm->workaround && data->ttls_version == 0) {
712 /* At least FreeRADIUS seems to be terminating
713 * EAP-TTLS/MSHCAPV2 without the expected MS-CHAP-v2 Success
715 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: EAP workaround - "
716 "allow success without tunneled response");
717 ret->methodState = METHOD_MAY_CONT;
718 ret->decision = DECISION_COND_SUCC;
725 static int eap_ttls_phase2_request_mschap(struct eap_sm *sm,
726 struct eap_ttls_data *data,
727 struct eap_method_ret *ret,
728 struct wpabuf **resp)
731 u8 *buf, *pos, *challenge;
732 const u8 *identity, *password;
733 size_t identity_len, password_len;
736 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAP Request");
738 identity = eap_get_config_identity(sm, &identity_len);
739 password = eap_get_config_password2(sm, &password_len, &pwhash);
740 if (identity == NULL || password == NULL)
743 msg = wpabuf_alloc(identity_len + 1000);
745 wpa_printf(MSG_ERROR,
746 "EAP-TTLS/MSCHAP: Failed to allocate memory");
749 pos = buf = wpabuf_mhead(msg);
752 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
753 identity, identity_len);
755 /* MS-CHAP-Challenge */
756 challenge = eap_ttls_implicit_challenge(sm, data, EAP_TLS_KEY_LEN);
757 if (challenge == NULL) {
759 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAP: Failed to derive "
760 "implicit challenge");
764 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
765 RADIUS_VENDOR_ID_MICROSOFT, 1,
766 challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
768 /* MS-CHAP-Response */
769 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP_RESPONSE,
770 RADIUS_VENDOR_ID_MICROSOFT, 1,
771 EAP_TTLS_MSCHAP_RESPONSE_LEN);
772 data->ident = challenge[EAP_TTLS_MSCHAP_CHALLENGE_LEN];
773 *pos++ = data->ident;
774 *pos++ = 1; /* Flags: Use NT style passwords */
775 os_memset(pos, 0, 24); /* LM-Response */
778 challenge_response(challenge, password, pos); /* NT-Response */
779 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password hash",
782 nt_challenge_response(challenge, password, password_len,
783 pos); /* NT-Response */
784 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password",
785 password, password_len);
787 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP implicit challenge",
788 challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
789 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP response", pos, 24);
794 wpabuf_put(msg, pos - buf);
797 if (data->ttls_version > 0) {
798 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
799 * so do not allow connection to be terminated yet. */
800 ret->methodState = METHOD_CONT;
801 ret->decision = DECISION_COND_SUCC;
803 /* EAP-TTLS/MSCHAP does not provide tunneled success
804 * notification, so assume that Phase2 succeeds. */
805 ret->methodState = METHOD_DONE;
806 ret->decision = DECISION_COND_SUCC;
813 static int eap_ttls_phase2_request_pap(struct eap_sm *sm,
814 struct eap_ttls_data *data,
815 struct eap_method_ret *ret,
816 struct wpabuf **resp)
821 const u8 *identity, *password;
822 size_t identity_len, password_len;
824 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 PAP Request");
826 identity = eap_get_config_identity(sm, &identity_len);
827 password = eap_get_config_password(sm, &password_len);
828 if (identity == NULL || password == NULL)
831 msg = wpabuf_alloc(identity_len + password_len + 100);
833 wpa_printf(MSG_ERROR,
834 "EAP-TTLS/PAP: Failed to allocate memory");
837 pos = buf = wpabuf_mhead(msg);
840 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
841 identity, identity_len);
843 /* User-Password; in RADIUS, this is encrypted, but EAP-TTLS encrypts
844 * the data, so no separate encryption is used in the AVP itself.
845 * However, the password is padded to obfuscate its length. */
846 pad = (16 - (password_len & 15)) & 15;
847 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_USER_PASSWORD, 0, 1,
849 os_memcpy(pos, password, password_len);
851 os_memset(pos, 0, pad);
855 wpabuf_put(msg, pos - buf);
858 if (data->ttls_version > 0) {
859 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
860 * so do not allow connection to be terminated yet. */
861 ret->methodState = METHOD_CONT;
862 ret->decision = DECISION_COND_SUCC;
864 /* EAP-TTLS/PAP does not provide tunneled success notification,
865 * so assume that Phase2 succeeds. */
866 ret->methodState = METHOD_DONE;
867 ret->decision = DECISION_COND_SUCC;
874 static int eap_ttls_phase2_request_chap(struct eap_sm *sm,
875 struct eap_ttls_data *data,
876 struct eap_method_ret *ret,
877 struct wpabuf **resp)
880 u8 *buf, *pos, *challenge;
881 const u8 *identity, *password;
882 size_t identity_len, password_len;
884 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 CHAP Request");
886 identity = eap_get_config_identity(sm, &identity_len);
887 password = eap_get_config_password(sm, &password_len);
888 if (identity == NULL || password == NULL)
891 msg = wpabuf_alloc(identity_len + 1000);
893 wpa_printf(MSG_ERROR,
894 "EAP-TTLS/CHAP: Failed to allocate memory");
897 pos = buf = wpabuf_mhead(msg);
900 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
901 identity, identity_len);
904 challenge = eap_ttls_implicit_challenge(sm, data, EAP_TLS_KEY_LEN);
905 if (challenge == NULL) {
907 wpa_printf(MSG_ERROR, "EAP-TTLS/CHAP: Failed to derive "
908 "implicit challenge");
912 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_CHAP_CHALLENGE, 0, 1,
913 challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
916 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_CHAP_PASSWORD, 0, 1,
917 1 + EAP_TTLS_CHAP_PASSWORD_LEN);
918 data->ident = challenge[EAP_TTLS_CHAP_CHALLENGE_LEN];
919 *pos++ = data->ident;
921 /* MD5(Ident + Password + Challenge) */
922 chap_md5(data->ident, password, password_len, challenge,
923 EAP_TTLS_CHAP_CHALLENGE_LEN, pos);
925 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: CHAP username",
926 identity, identity_len);
927 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: CHAP password",
928 password, password_len);
929 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP implicit challenge",
930 challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
931 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP password",
932 pos, EAP_TTLS_CHAP_PASSWORD_LEN);
933 pos += EAP_TTLS_CHAP_PASSWORD_LEN;
937 wpabuf_put(msg, pos - buf);
940 if (data->ttls_version > 0) {
941 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
942 * so do not allow connection to be terminated yet. */
943 ret->methodState = METHOD_CONT;
944 ret->decision = DECISION_COND_SUCC;
946 /* EAP-TTLS/CHAP does not provide tunneled success
947 * notification, so assume that Phase2 succeeds. */
948 ret->methodState = METHOD_DONE;
949 ret->decision = DECISION_COND_SUCC;
956 static int eap_ttls_phase2_request(struct eap_sm *sm,
957 struct eap_ttls_data *data,
958 struct eap_method_ret *ret,
960 struct wpabuf **resp)
964 enum phase2_types phase2_type = data->phase2_type;
967 if (data->tnc_started) {
968 wpa_printf(MSG_DEBUG, "EAP-TTLS: Processing TNC");
969 phase2_type = EAP_TTLS_PHASE2_EAP;
973 if (phase2_type == EAP_TTLS_PHASE2_MSCHAPV2 ||
974 phase2_type == EAP_TTLS_PHASE2_MSCHAP ||
975 phase2_type == EAP_TTLS_PHASE2_PAP ||
976 phase2_type == EAP_TTLS_PHASE2_CHAP) {
977 if (eap_get_config_identity(sm, &len) == NULL) {
979 "EAP-TTLS: Identity not configured");
980 eap_sm_request_identity(sm);
981 if (eap_get_config_password(sm, &len) == NULL)
982 eap_sm_request_password(sm);
986 if (eap_get_config_password(sm, &len) == NULL) {
988 "EAP-TTLS: Password not configured");
989 eap_sm_request_password(sm);
994 switch (data->phase2_type) {
995 case EAP_TTLS_PHASE2_EAP:
996 res = eap_ttls_phase2_request_eap(sm, data, ret, hdr, resp);
998 case EAP_TTLS_PHASE2_MSCHAPV2:
999 res = eap_ttls_phase2_request_mschapv2(sm, data, ret, resp);
1001 case EAP_TTLS_PHASE2_MSCHAP:
1002 res = eap_ttls_phase2_request_mschap(sm, data, ret, resp);
1004 case EAP_TTLS_PHASE2_PAP:
1005 res = eap_ttls_phase2_request_pap(sm, data, ret, resp);
1007 case EAP_TTLS_PHASE2_CHAP:
1008 res = eap_ttls_phase2_request_chap(sm, data, ret, resp);
1011 wpa_printf(MSG_ERROR, "EAP-TTLS: Phase 2 - Unknown");
1017 ret->methodState = METHOD_DONE;
1018 ret->decision = DECISION_FAIL;
1025 #if EAP_TTLS_VERSION > 0
1026 static struct wpabuf * eap_ttls_build_phase_finished(
1027 struct eap_sm *sm, struct eap_ttls_data *data, int id, int final)
1032 const int max_len = 300;
1034 req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TTLS, 1 + max_len,
1035 EAP_CODE_RESPONSE, id);
1039 wpabuf_put_u8(req, data->ttls_version);
1041 pos = wpabuf_put(req, 0);
1042 len = tls_connection_ia_send_phase_finished(sm->ssl_ctx,
1044 final, pos, max_len);
1049 wpabuf_put(req, len);
1050 eap_update_len(req);
1054 #endif /* EAP_TTLS_VERSION */
1057 struct ttls_parse_avp {
1065 static int eap_ttls_parse_attr_eap(const u8 *dpos, size_t dlen,
1066 struct ttls_parse_avp *parse)
1068 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - EAP Message");
1069 if (parse->eapdata == NULL) {
1070 parse->eapdata = os_malloc(dlen);
1071 if (parse->eapdata == NULL) {
1072 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
1073 "memory for Phase 2 EAP data");
1076 os_memcpy(parse->eapdata, dpos, dlen);
1077 parse->eap_len = dlen;
1079 u8 *neweap = os_realloc(parse->eapdata, parse->eap_len + dlen);
1080 if (neweap == NULL) {
1081 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
1082 "memory for Phase 2 EAP data");
1085 os_memcpy(neweap + parse->eap_len, dpos, dlen);
1086 parse->eapdata = neweap;
1087 parse->eap_len += dlen;
1094 static int eap_ttls_parse_avp(u8 *pos, size_t left,
1095 struct ttls_parse_avp *parse)
1097 struct ttls_avp *avp;
1098 u32 avp_code, avp_length, vendor_id = 0;
1099 u8 avp_flags, *dpos;
1102 avp = (struct ttls_avp *) pos;
1103 avp_code = be_to_host32(avp->avp_code);
1104 avp_length = be_to_host32(avp->avp_length);
1105 avp_flags = (avp_length >> 24) & 0xff;
1106 avp_length &= 0xffffff;
1107 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP: code=%d flags=0x%02x "
1108 "length=%d", (int) avp_code, avp_flags,
1111 if (avp_length > left) {
1112 wpa_printf(MSG_WARNING, "EAP-TTLS: AVP overflow "
1113 "(len=%d, left=%lu) - dropped",
1114 (int) avp_length, (unsigned long) left);
1118 if (avp_length < sizeof(*avp)) {
1119 wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid AVP length %d",
1124 dpos = (u8 *) (avp + 1);
1125 dlen = avp_length - sizeof(*avp);
1126 if (avp_flags & AVP_FLAGS_VENDOR) {
1128 wpa_printf(MSG_WARNING, "EAP-TTLS: Vendor AVP "
1132 vendor_id = WPA_GET_BE32(dpos);
1133 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP vendor_id %d",
1139 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP data", dpos, dlen);
1141 if (vendor_id == 0 && avp_code == RADIUS_ATTR_EAP_MESSAGE) {
1142 if (eap_ttls_parse_attr_eap(dpos, dlen, parse) < 0)
1144 } else if (vendor_id == 0 && avp_code == RADIUS_ATTR_REPLY_MESSAGE) {
1145 /* This is an optional message that can be displayed to
1147 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: AVP - Reply-Message",
1149 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
1150 avp_code == RADIUS_ATTR_MS_CHAP2_SUCCESS) {
1151 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP2-Success",
1154 wpa_printf(MSG_WARNING, "EAP-TTLS: Unexpected "
1155 "MS-CHAP2-Success length "
1156 "(len=%lu, expected 43)",
1157 (unsigned long) dlen);
1160 parse->mschapv2 = dpos;
1161 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
1162 avp_code == RADIUS_ATTR_MS_CHAP_ERROR) {
1163 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP-Error",
1165 parse->mschapv2_error = 1;
1166 } else if (avp_flags & AVP_FLAGS_MANDATORY) {
1167 wpa_printf(MSG_WARNING, "EAP-TTLS: Unsupported mandatory AVP "
1168 "code %d vendor_id %d - dropped",
1169 (int) avp_code, (int) vendor_id);
1172 wpa_printf(MSG_DEBUG, "EAP-TTLS: Ignoring unsupported AVP "
1173 "code %d vendor_id %d",
1174 (int) avp_code, (int) vendor_id);
1181 static int eap_ttls_parse_avps(struct wpabuf *in_decrypted,
1182 struct ttls_parse_avp *parse)
1188 pos = wpabuf_mhead(in_decrypted);
1189 left = wpabuf_len(in_decrypted);
1190 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Decrypted Phase 2 AVPs", pos, left);
1191 if (left < sizeof(struct ttls_avp)) {
1192 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 AVP frame"
1193 " len=%lu expected %lu or more - dropped",
1194 (unsigned long) left,
1195 (unsigned long) sizeof(struct ttls_avp));
1200 os_memset(parse, 0, sizeof(*parse));
1203 avp_length = eap_ttls_parse_avp(pos, left, parse);
1207 pad = (4 - (avp_length & 3)) & 3;
1208 pos += avp_length + pad;
1209 if (left < avp_length + pad)
1212 left -= avp_length + pad;
1219 static u8 * eap_ttls_fake_identity_request(void)
1221 struct eap_hdr *hdr;
1224 wpa_printf(MSG_DEBUG, "EAP-TTLS: empty data in beginning of "
1225 "Phase 2 - use fake EAP-Request Identity");
1226 buf = os_malloc(sizeof(*hdr) + 1);
1228 wpa_printf(MSG_WARNING, "EAP-TTLS: failed to allocate "
1229 "memory for fake EAP-Identity Request");
1233 hdr = (struct eap_hdr *) buf;
1234 hdr->code = EAP_CODE_REQUEST;
1235 hdr->identifier = 0;
1236 hdr->length = host_to_be16(sizeof(*hdr) + 1);
1237 buf[sizeof(*hdr)] = EAP_TYPE_IDENTITY;
1243 static int eap_ttls_encrypt_response(struct eap_sm *sm,
1244 struct eap_ttls_data *data,
1245 struct wpabuf *resp, u8 identifier,
1246 struct wpabuf **out_data)
1251 wpa_hexdump_buf_key(MSG_DEBUG, "EAP-TTLS: Encrypting Phase 2 data",
1253 if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
1254 data->ttls_version, identifier,
1256 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to encrypt a Phase 2 "
1266 static int eap_ttls_process_phase2_eap(struct eap_sm *sm,
1267 struct eap_ttls_data *data,
1268 struct eap_method_ret *ret,
1269 struct ttls_parse_avp *parse,
1270 struct wpabuf **resp)
1272 struct eap_hdr *hdr;
1275 if (parse->eapdata == NULL) {
1276 wpa_printf(MSG_WARNING, "EAP-TTLS: No EAP Message in the "
1277 "packet - dropped");
1281 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP",
1282 parse->eapdata, parse->eap_len);
1283 hdr = (struct eap_hdr *) parse->eapdata;
1285 if (parse->eap_len < sizeof(*hdr)) {
1286 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 EAP "
1287 "frame (len=%lu, expected %lu or more) - dropped",
1288 (unsigned long) parse->eap_len,
1289 (unsigned long) sizeof(*hdr));
1292 len = be_to_host16(hdr->length);
1293 if (len > parse->eap_len) {
1294 wpa_printf(MSG_INFO, "EAP-TTLS: Length mismatch in Phase 2 "
1295 "EAP frame (EAP hdr len=%lu, EAP data len in "
1297 (unsigned long) len,
1298 (unsigned long) parse->eap_len);
1301 wpa_printf(MSG_DEBUG, "EAP-TTLS: received Phase 2: code=%d "
1302 "identifier=%d length=%lu",
1303 hdr->code, hdr->identifier, (unsigned long) len);
1304 switch (hdr->code) {
1305 case EAP_CODE_REQUEST:
1306 if (eap_ttls_phase2_request(sm, data, ret, hdr, resp)) {
1307 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
1308 "processing failed");
1313 wpa_printf(MSG_INFO, "EAP-TTLS: Unexpected code=%d in "
1314 "Phase 2 EAP header", hdr->code);
1322 static int eap_ttls_process_phase2_mschapv2(struct eap_sm *sm,
1323 struct eap_ttls_data *data,
1324 struct eap_method_ret *ret,
1325 struct ttls_parse_avp *parse)
1327 if (parse->mschapv2_error) {
1328 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Received "
1329 "MS-CHAP-Error - failed");
1330 ret->methodState = METHOD_DONE;
1331 ret->decision = DECISION_FAIL;
1332 /* Reply with empty data to ACK error */
1336 if (parse->mschapv2 == NULL) {
1337 wpa_printf(MSG_WARNING, "EAP-TTLS: no MS-CHAP2-Success AVP "
1338 "received for Phase2 MSCHAPV2");
1341 if (parse->mschapv2[0] != data->ident) {
1342 wpa_printf(MSG_WARNING, "EAP-TTLS: Ident mismatch for Phase 2 "
1343 "MSCHAPV2 (received Ident 0x%02x, expected 0x%02x)",
1344 parse->mschapv2[0], data->ident);
1347 if (!data->auth_response_valid ||
1348 mschapv2_verify_auth_response(data->auth_response,
1349 parse->mschapv2 + 1, 42)) {
1350 wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid authenticator "
1351 "response in Phase 2 MSCHAPV2 success request");
1355 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 MSCHAPV2 "
1356 "authentication succeeded");
1357 if (data->ttls_version > 0) {
1359 * EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report
1360 * success, so do not allow connection to be terminated
1363 ret->methodState = METHOD_CONT;
1364 ret->decision = DECISION_COND_SUCC;
1366 ret->methodState = METHOD_DONE;
1367 ret->decision = DECISION_UNCOND_SUCC;
1368 data->phase2_success = 1;
1372 * Reply with empty data; authentication server will reply
1373 * with EAP-Success after this.
1380 static int eap_ttls_process_tnc_start(struct eap_sm *sm,
1381 struct eap_ttls_data *data,
1382 struct eap_method_ret *ret,
1383 struct ttls_parse_avp *parse,
1384 struct wpabuf **resp)
1386 /* TNC uses inner EAP method after non-EAP TTLS phase 2. */
1387 if (parse->eapdata == NULL) {
1388 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
1389 "unexpected tunneled data (no EAP)");
1393 if (!data->ready_for_tnc) {
1394 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
1395 "EAP after non-EAP, but not ready for TNC");
1399 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
1401 data->tnc_started = 1;
1403 if (eap_ttls_process_phase2_eap(sm, data, ret, parse, resp) < 0)
1408 #endif /* EAP_TNC */
1411 static int eap_ttls_process_decrypted(struct eap_sm *sm,
1412 struct eap_ttls_data *data,
1413 struct eap_method_ret *ret,
1415 struct ttls_parse_avp *parse,
1416 struct wpabuf *in_decrypted,
1417 struct wpabuf **out_data)
1419 struct wpabuf *resp = NULL;
1420 struct eap_peer_config *config = eap_get_config(sm);
1422 enum phase2_types phase2_type = data->phase2_type;
1425 if (data->tnc_started)
1426 phase2_type = EAP_TTLS_PHASE2_EAP;
1427 #endif /* EAP_TNC */
1429 switch (phase2_type) {
1430 case EAP_TTLS_PHASE2_EAP:
1431 if (eap_ttls_process_phase2_eap(sm, data, ret, parse, &resp) <
1435 case EAP_TTLS_PHASE2_MSCHAPV2:
1436 res = eap_ttls_process_phase2_mschapv2(sm, data, ret, parse);
1438 if (res == 1 && parse->eapdata &&
1439 ret->methodState == METHOD_DONE &&
1440 ret->decision == DECISION_UNCOND_SUCC) {
1442 * TNC may be required as the next
1443 * authentication method within the tunnel.
1445 ret->methodState = METHOD_MAY_CONT;
1446 data->ready_for_tnc = 1;
1447 if (eap_ttls_process_tnc_start(sm, data, ret, parse,
1451 #endif /* EAP_TNC */
1453 case EAP_TTLS_PHASE2_MSCHAP:
1454 case EAP_TTLS_PHASE2_PAP:
1455 case EAP_TTLS_PHASE2_CHAP:
1457 if (eap_ttls_process_tnc_start(sm, data, ret, parse, &resp) <
1462 /* EAP-TTLS/{MSCHAP,PAP,CHAP} should not send any TLS tunneled
1463 * requests to the supplicant */
1464 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received unexpected "
1467 #endif /* EAP_TNC */
1471 if (eap_ttls_encrypt_response(sm, data, resp, identifier,
1474 } else if (config->pending_req_identity ||
1475 config->pending_req_password ||
1476 config->pending_req_otp ||
1477 config->pending_req_new_password) {
1478 wpabuf_free(data->pending_phase2_req);
1479 data->pending_phase2_req = wpabuf_dup(in_decrypted);
1486 #if EAP_TTLS_VERSION > 0
1487 static void eap_ttls_final_phase_finished(struct eap_sm *sm,
1488 struct eap_ttls_data *data,
1489 struct eap_method_ret *ret,
1491 struct wpabuf **out_data)
1493 wpa_printf(MSG_DEBUG, "EAP-TTLS: FinalPhaseFinished received");
1494 wpa_printf(MSG_INFO, "EAP-TTLS: TLS/IA authentication succeeded");
1495 ret->methodState = METHOD_DONE;
1496 ret->decision = DECISION_UNCOND_SUCC;
1497 data->phase2_success = 1;
1498 *out_data = eap_ttls_build_phase_finished(sm, data, identifier, 1);
1499 eap_ttls_v1_derive_key(sm, data);
1501 #endif /* EAP_TTLS_VERSION */
1504 static int eap_ttls_implicit_identity_request(struct eap_sm *sm,
1505 struct eap_ttls_data *data,
1506 struct eap_method_ret *ret,
1508 struct wpabuf **out_data)
1511 struct eap_hdr *hdr;
1512 struct wpabuf *resp;
1514 hdr = (struct eap_hdr *) eap_ttls_fake_identity_request();
1516 ret->methodState = METHOD_DONE;
1517 ret->decision = DECISION_FAIL;
1522 if (eap_ttls_phase2_request(sm, data, ret, hdr, &resp)) {
1523 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
1524 "processing failed");
1527 retval = eap_ttls_encrypt_response(sm, data, resp, identifier,
1534 ret->methodState = METHOD_DONE;
1535 ret->decision = DECISION_FAIL;
1542 static int eap_ttls_phase2_start(struct eap_sm *sm, struct eap_ttls_data *data,
1543 struct eap_method_ret *ret, u8 identifier,
1544 struct wpabuf **out_data)
1546 data->phase2_start = 0;
1549 * EAP-TTLS does not use Phase2 on fast re-auth; this must be done only
1550 * if TLS part was indeed resuming a previous session. Most
1551 * Authentication Servers terminate EAP-TTLS before reaching this
1552 * point, but some do not. Make wpa_supplicant stop phase 2 here, if
1556 tls_connection_resumed(sm->ssl_ctx, data->ssl.conn)) {
1557 wpa_printf(MSG_DEBUG, "EAP-TTLS: Session resumption - "
1559 *out_data = eap_peer_tls_build_ack(identifier, EAP_TYPE_TTLS,
1560 data->ttls_version);
1561 ret->methodState = METHOD_DONE;
1562 ret->decision = DECISION_UNCOND_SUCC;
1563 data->phase2_success = 1;
1567 return eap_ttls_implicit_identity_request(sm, data, ret, identifier,
1572 static int eap_ttls_decrypt(struct eap_sm *sm, struct eap_ttls_data *data,
1573 struct eap_method_ret *ret, u8 identifier,
1574 const struct wpabuf *in_data,
1575 struct wpabuf **out_data)
1577 struct wpabuf *in_decrypted = NULL;
1579 struct ttls_parse_avp parse;
1581 os_memset(&parse, 0, sizeof(parse));
1583 wpa_printf(MSG_DEBUG, "EAP-TTLS: received %lu bytes encrypted data for"
1585 in_data ? (unsigned long) wpabuf_len(in_data) : 0);
1587 if (data->pending_phase2_req) {
1588 wpa_printf(MSG_DEBUG, "EAP-TTLS: Pending Phase 2 request - "
1589 "skip decryption and use old data");
1590 /* Clear TLS reassembly state. */
1591 eap_peer_tls_reset_input(&data->ssl);
1593 in_decrypted = data->pending_phase2_req;
1594 data->pending_phase2_req = NULL;
1595 if (wpabuf_len(in_decrypted) == 0) {
1596 wpabuf_free(in_decrypted);
1597 return eap_ttls_implicit_identity_request(
1598 sm, data, ret, identifier, out_data);
1603 if ((in_data == NULL || wpabuf_len(in_data) == 0) &&
1604 data->phase2_start) {
1605 return eap_ttls_phase2_start(sm, data, ret, identifier,
1609 if (in_data == NULL || wpabuf_len(in_data) == 0) {
1610 /* Received TLS ACK - requesting more fragments */
1611 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
1613 identifier, NULL, out_data);
1616 retval = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
1620 #if EAP_TTLS_VERSION > 0
1621 if (data->ttls_version > 0 &&
1622 (in_decrypted == NULL || wpabuf_len(in_decrypted) == 0) &&
1623 tls_connection_ia_final_phase_finished(sm->ssl_ctx,
1625 eap_ttls_final_phase_finished(sm, data, ret, identifier,
1629 #endif /* EAP_TTLS_VERSION */
1632 data->phase2_start = 0;
1634 if (eap_ttls_parse_avps(in_decrypted, &parse) < 0) {
1639 retval = eap_ttls_process_decrypted(sm, data, ret, identifier,
1640 &parse, in_decrypted, out_data);
1643 wpabuf_free(in_decrypted);
1644 os_free(parse.eapdata);
1647 ret->methodState = METHOD_DONE;
1648 ret->decision = DECISION_FAIL;
1655 static int eap_ttls_process_start(struct eap_sm *sm,
1656 struct eap_ttls_data *data, u8 flags,
1657 struct eap_method_ret *ret)
1659 struct eap_peer_config *config = eap_get_config(sm);
1661 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start (server ver=%d, own ver=%d)",
1662 flags & EAP_PEAP_VERSION_MASK, data->ttls_version);
1663 #if EAP_TTLS_VERSION > 0
1664 if ((flags & EAP_PEAP_VERSION_MASK) < data->ttls_version)
1665 data->ttls_version = flags & EAP_PEAP_VERSION_MASK;
1666 if (data->force_ttls_version >= 0 &&
1667 data->force_ttls_version != data->ttls_version) {
1668 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to select "
1669 "forced TTLS version %d",
1670 data->force_ttls_version);
1671 ret->methodState = METHOD_DONE;
1672 ret->decision = DECISION_FAIL;
1673 ret->allowNotifications = FALSE;
1676 wpa_printf(MSG_DEBUG, "EAP-TTLS: Using TTLS version %d",
1677 data->ttls_version);
1679 if (data->ttls_version > 0)
1680 data->ssl.tls_ia = 1;
1681 #endif /* EAP_TTLS_VERSION */
1682 if (!data->ssl_initialized &&
1683 eap_peer_tls_ssl_init(sm, &data->ssl, config)) {
1684 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to initialize SSL.");
1687 data->ssl_initialized = 1;
1689 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start");
1695 static int eap_ttls_process_handshake(struct eap_sm *sm,
1696 struct eap_ttls_data *data,
1697 struct eap_method_ret *ret,
1699 const u8 *in_data, size_t in_len,
1700 struct wpabuf **out_data)
1704 res = eap_peer_tls_process_helper(sm, &data->ssl, EAP_TYPE_TTLS,
1705 data->ttls_version, identifier,
1706 in_data, in_len, out_data);
1708 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1709 wpa_printf(MSG_DEBUG, "EAP-TTLS: TLS done, proceed to "
1711 if (data->resuming) {
1712 wpa_printf(MSG_DEBUG, "EAP-TTLS: fast reauth - may "
1714 ret->decision = DECISION_COND_SUCC;
1715 ret->methodState = METHOD_MAY_CONT;
1717 data->phase2_start = 1;
1718 if (data->ttls_version == 0)
1719 eap_ttls_v0_derive_key(sm, data);
1721 if (*out_data == NULL || wpabuf_len(*out_data) == 0) {
1722 if (eap_ttls_decrypt(sm, data, ret, identifier,
1724 wpa_printf(MSG_WARNING, "EAP-TTLS: "
1725 "failed to process early "
1726 "start for Phase 2");
1736 * Application data included in the handshake message.
1738 wpabuf_free(data->pending_phase2_req);
1739 data->pending_phase2_req = *out_data;
1741 wpabuf_set(&msg, in_data, in_len);
1742 res = eap_ttls_decrypt(sm, data, ret, identifier, &msg,
1750 static void eap_ttls_check_auth_status(struct eap_sm *sm,
1751 struct eap_ttls_data *data,
1752 struct eap_method_ret *ret)
1754 if (data->ttls_version == 0 && ret->methodState == METHOD_DONE) {
1755 ret->allowNotifications = FALSE;
1756 if (ret->decision == DECISION_UNCOND_SUCC ||
1757 ret->decision == DECISION_COND_SUCC) {
1758 wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1759 "completed successfully");
1760 data->phase2_success = 1;
1762 if (!data->ready_for_tnc && !data->tnc_started) {
1764 * TNC may be required as the next
1765 * authentication method within the tunnel.
1767 ret->methodState = METHOD_MAY_CONT;
1768 data->ready_for_tnc = 1;
1770 #endif /* EAP_TNC */
1772 } else if (data->ttls_version == 0 &&
1773 ret->methodState == METHOD_MAY_CONT &&
1774 (ret->decision == DECISION_UNCOND_SUCC ||
1775 ret->decision == DECISION_COND_SUCC)) {
1776 wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1777 "completed successfully (MAY_CONT)");
1778 data->phase2_success = 1;
1783 static struct wpabuf * eap_ttls_process(struct eap_sm *sm, void *priv,
1784 struct eap_method_ret *ret,
1785 const struct wpabuf *reqData)
1790 struct wpabuf *resp;
1792 struct eap_ttls_data *data = priv;
1794 pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_TTLS, ret,
1795 reqData, &left, &flags);
1798 id = eap_get_id(reqData);
1800 if (flags & EAP_TLS_FLAGS_START) {
1801 if (eap_ttls_process_start(sm, data, flags, ret) < 0)
1804 /* draft-ietf-pppext-eap-ttls-03.txt, Ch. 8.1:
1805 * EAP-TTLS Start packet may, in a future specification, be
1806 * allowed to contain data. Client based on this draft version
1807 * must ignore such data but must not reject the Start packet.
1810 } else if (!data->ssl_initialized) {
1811 wpa_printf(MSG_DEBUG, "EAP-TTLS: First message did not "
1812 "include Start flag");
1813 ret->methodState = METHOD_DONE;
1814 ret->decision = DECISION_FAIL;
1815 ret->allowNotifications = FALSE;
1820 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1823 wpabuf_set(&msg, pos, left);
1824 res = eap_ttls_decrypt(sm, data, ret, id, &msg, &resp);
1826 res = eap_ttls_process_handshake(sm, data, ret, id,
1830 eap_ttls_check_auth_status(sm, data, ret);
1832 /* FIX: what about res == -1? Could just move all error processing into
1833 * the other functions and get rid of this res==1 case here. */
1836 return eap_peer_tls_build_ack(id, EAP_TYPE_TTLS,
1837 data->ttls_version);
1843 static Boolean eap_ttls_has_reauth_data(struct eap_sm *sm, void *priv)
1845 struct eap_ttls_data *data = priv;
1846 return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1847 data->phase2_success;
1851 static void eap_ttls_deinit_for_reauth(struct eap_sm *sm, void *priv)
1853 struct eap_ttls_data *data = priv;
1854 wpabuf_free(data->pending_phase2_req);
1855 data->pending_phase2_req = NULL;
1857 data->ready_for_tnc = 0;
1858 data->tnc_started = 0;
1859 #endif /* EAP_TNC */
1863 static void * eap_ttls_init_for_reauth(struct eap_sm *sm, void *priv)
1865 struct eap_ttls_data *data = priv;
1866 os_free(data->key_data);
1867 data->key_data = NULL;
1868 if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
1872 if (data->phase2_priv && data->phase2_method &&
1873 data->phase2_method->init_for_reauth)
1874 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
1875 data->phase2_start = 0;
1876 data->phase2_success = 0;
1883 static int eap_ttls_get_status(struct eap_sm *sm, void *priv, char *buf,
1884 size_t buflen, int verbose)
1886 struct eap_ttls_data *data = priv;
1889 len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
1890 ret = os_snprintf(buf + len, buflen - len,
1891 "EAP-TTLSv%d Phase2 method=",
1892 data->ttls_version);
1893 if (ret < 0 || (size_t) ret >= buflen - len)
1896 switch (data->phase2_type) {
1897 case EAP_TTLS_PHASE2_EAP:
1898 ret = os_snprintf(buf + len, buflen - len, "EAP-%s\n",
1899 data->phase2_method ?
1900 data->phase2_method->name : "?");
1902 case EAP_TTLS_PHASE2_MSCHAPV2:
1903 ret = os_snprintf(buf + len, buflen - len, "MSCHAPV2\n");
1905 case EAP_TTLS_PHASE2_MSCHAP:
1906 ret = os_snprintf(buf + len, buflen - len, "MSCHAP\n");
1908 case EAP_TTLS_PHASE2_PAP:
1909 ret = os_snprintf(buf + len, buflen - len, "PAP\n");
1911 case EAP_TTLS_PHASE2_CHAP:
1912 ret = os_snprintf(buf + len, buflen - len, "CHAP\n");
1918 if (ret < 0 || (size_t) ret >= buflen - len)
1926 static Boolean eap_ttls_isKeyAvailable(struct eap_sm *sm, void *priv)
1928 struct eap_ttls_data *data = priv;
1929 return data->key_data != NULL && data->phase2_success;
1933 static u8 * eap_ttls_getKey(struct eap_sm *sm, void *priv, size_t *len)
1935 struct eap_ttls_data *data = priv;
1938 if (data->key_data == NULL || !data->phase2_success)
1941 key = os_malloc(EAP_TLS_KEY_LEN);
1945 *len = EAP_TLS_KEY_LEN;
1946 os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
1952 int eap_peer_ttls_register(void)
1954 struct eap_method *eap;
1957 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1958 EAP_VENDOR_IETF, EAP_TYPE_TTLS, "TTLS");
1962 eap->init = eap_ttls_init;
1963 eap->deinit = eap_ttls_deinit;
1964 eap->process = eap_ttls_process;
1965 eap->isKeyAvailable = eap_ttls_isKeyAvailable;
1966 eap->getKey = eap_ttls_getKey;
1967 eap->get_status = eap_ttls_get_status;
1968 eap->has_reauth_data = eap_ttls_has_reauth_data;
1969 eap->deinit_for_reauth = eap_ttls_deinit_for_reauth;
1970 eap->init_for_reauth = eap_ttls_init_for_reauth;
1972 ret = eap_peer_method_register(eap);
1974 eap_peer_method_free(eap);