2 * EAP peer method: EAP-TTLS (RFC 5281)
3 * Copyright (c) 2004-2011, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "crypto/ms_funcs.h"
13 #include "crypto/sha1.h"
14 #include "crypto/tls.h"
15 #include "eap_common/chap.h"
16 #include "eap_common/eap_ttls.h"
19 #include "eap_tls_common.h"
20 #include "eap_config.h"
23 #define EAP_TTLS_VERSION 0
26 static void eap_ttls_deinit(struct eap_sm *sm, void *priv);
29 struct eap_ttls_data {
30 struct eap_ssl_data ssl;
34 const struct eap_method *phase2_method;
41 EAP_TTLS_PHASE2_MSCHAPV2,
42 EAP_TTLS_PHASE2_MSCHAP,
46 struct eap_method_type phase2_eap_type;
47 struct eap_method_type *phase2_eap_types;
48 size_t num_phase2_eap_types;
50 u8 auth_response[MSCHAPV2_AUTH_RESPONSE_LEN];
51 int auth_response_valid;
52 u8 master_key[MSCHAPV2_MASTER_KEY_LEN]; /* MSCHAPv2 master key */
54 int resuming; /* starting a resumed session */
55 int reauth; /* reauthentication */
60 struct wpabuf *pending_phase2_req;
69 static void * eap_ttls_init(struct eap_sm *sm)
71 struct eap_ttls_data *data;
72 struct eap_peer_config *config = eap_get_config(sm);
75 data = os_zalloc(sizeof(*data));
78 data->ttls_version = EAP_TTLS_VERSION;
80 data->phase2_type = EAP_TTLS_PHASE2_EAP;
82 if (config && config->phase2) {
83 if (os_strstr(config->phase2, "autheap=")) {
85 data->phase2_type = EAP_TTLS_PHASE2_EAP;
86 } else if (os_strstr(config->phase2, "auth=MSCHAPV2")) {
87 selected = "MSCHAPV2";
88 data->phase2_type = EAP_TTLS_PHASE2_MSCHAPV2;
89 } else if (os_strstr(config->phase2, "auth=MSCHAP")) {
91 data->phase2_type = EAP_TTLS_PHASE2_MSCHAP;
92 } else if (os_strstr(config->phase2, "auth=PAP")) {
94 data->phase2_type = EAP_TTLS_PHASE2_PAP;
95 } else if (os_strstr(config->phase2, "auth=CHAP")) {
97 data->phase2_type = EAP_TTLS_PHASE2_CHAP;
100 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase2 type: %s", selected);
102 if (data->phase2_type == EAP_TTLS_PHASE2_EAP) {
103 if (eap_peer_select_phase2_methods(config, "autheap=",
104 &data->phase2_eap_types,
105 &data->num_phase2_eap_types)
107 eap_ttls_deinit(sm, data);
111 data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
112 data->phase2_eap_type.method = EAP_TYPE_NONE;
115 if (eap_peer_tls_ssl_init(sm, &data->ssl, config, EAP_TYPE_TTLS)) {
116 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to initialize SSL.");
117 eap_ttls_deinit(sm, data);
125 static void eap_ttls_phase2_eap_deinit(struct eap_sm *sm,
126 struct eap_ttls_data *data)
128 if (data->phase2_priv && data->phase2_method) {
129 data->phase2_method->deinit(sm, data->phase2_priv);
130 data->phase2_method = NULL;
131 data->phase2_priv = NULL;
136 static void eap_ttls_free_key(struct eap_ttls_data *data)
138 if (data->key_data) {
139 bin_clear_free(data->key_data, EAP_TLS_KEY_LEN + EAP_EMSK_LEN);
140 data->key_data = NULL;
145 static void eap_ttls_deinit(struct eap_sm *sm, void *priv)
147 struct eap_ttls_data *data = priv;
150 eap_ttls_phase2_eap_deinit(sm, data);
151 os_free(data->phase2_eap_types);
152 eap_peer_tls_ssl_deinit(sm, &data->ssl);
153 eap_ttls_free_key(data);
154 os_free(data->session_id);
155 wpabuf_free(data->pending_phase2_req);
160 static u8 * eap_ttls_avp_hdr(u8 *avphdr, u32 avp_code, u32 vendor_id,
161 int mandatory, size_t len)
163 struct ttls_avp_vendor *avp;
167 avp = (struct ttls_avp_vendor *) avphdr;
168 flags = mandatory ? AVP_FLAGS_MANDATORY : 0;
170 flags |= AVP_FLAGS_VENDOR;
171 hdrlen = sizeof(*avp);
172 avp->vendor_id = host_to_be32(vendor_id);
174 hdrlen = sizeof(struct ttls_avp);
177 avp->avp_code = host_to_be32(avp_code);
178 avp->avp_length = host_to_be32(((u32) flags << 24) |
179 (u32) (hdrlen + len));
181 return avphdr + hdrlen;
185 static u8 * eap_ttls_avp_add(u8 *start, u8 *avphdr, u32 avp_code,
186 u32 vendor_id, int mandatory,
187 const u8 *data, size_t len)
190 pos = eap_ttls_avp_hdr(avphdr, avp_code, vendor_id, mandatory, len);
191 os_memcpy(pos, data, len);
198 static int eap_ttls_avp_encapsulate(struct wpabuf **resp, u32 avp_code,
204 msg = wpabuf_alloc(sizeof(struct ttls_avp) + wpabuf_len(*resp) + 4);
211 avp = wpabuf_mhead(msg);
212 pos = eap_ttls_avp_hdr(avp, avp_code, 0, mandatory, wpabuf_len(*resp));
213 os_memcpy(pos, wpabuf_head(*resp), wpabuf_len(*resp));
214 pos += wpabuf_len(*resp);
217 wpabuf_put(msg, pos - avp);
223 static int eap_ttls_v0_derive_key(struct eap_sm *sm,
224 struct eap_ttls_data *data)
226 eap_ttls_free_key(data);
227 data->key_data = eap_peer_tls_derive_key(sm, &data->ssl,
228 "ttls keying material",
231 if (!data->key_data) {
232 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to derive key");
236 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
237 data->key_data, EAP_TLS_KEY_LEN);
238 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived EMSK",
239 data->key_data + EAP_TLS_KEY_LEN,
242 os_free(data->session_id);
243 data->session_id = eap_peer_tls_derive_session_id(sm, &data->ssl,
246 if (data->session_id) {
247 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Derived Session-Id",
248 data->session_id, data->id_len);
250 wpa_printf(MSG_ERROR, "EAP-TTLS: Failed to derive Session-Id");
258 static u8 * eap_ttls_implicit_challenge(struct eap_sm *sm,
259 struct eap_ttls_data *data, size_t len)
261 return eap_peer_tls_derive_key(sm, &data->ssl, "ttls challenge", len);
263 #endif /* CONFIG_FIPS */
266 static void eap_ttls_phase2_select_eap_method(struct eap_ttls_data *data,
270 for (i = 0; i < data->num_phase2_eap_types; i++) {
271 if (data->phase2_eap_types[i].vendor != EAP_VENDOR_IETF ||
272 data->phase2_eap_types[i].method != method)
275 data->phase2_eap_type.vendor =
276 data->phase2_eap_types[i].vendor;
277 data->phase2_eap_type.method =
278 data->phase2_eap_types[i].method;
279 wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
280 "Phase 2 EAP vendor %d method %d",
281 data->phase2_eap_type.vendor,
282 data->phase2_eap_type.method);
288 static int eap_ttls_phase2_eap_process(struct eap_sm *sm,
289 struct eap_ttls_data *data,
290 struct eap_method_ret *ret,
291 struct eap_hdr *hdr, size_t len,
292 struct wpabuf **resp)
295 struct eap_method_ret iret;
297 os_memset(&iret, 0, sizeof(iret));
298 wpabuf_set(&msg, hdr, len);
299 *resp = data->phase2_method->process(sm, data->phase2_priv, &iret,
301 if ((iret.methodState == METHOD_DONE ||
302 iret.methodState == METHOD_MAY_CONT) &&
303 (iret.decision == DECISION_UNCOND_SUCC ||
304 iret.decision == DECISION_COND_SUCC ||
305 iret.decision == DECISION_FAIL)) {
306 ret->methodState = iret.methodState;
307 ret->decision = iret.decision;
314 static int eap_ttls_phase2_request_eap_method(struct eap_sm *sm,
315 struct eap_ttls_data *data,
316 struct eap_method_ret *ret,
317 struct eap_hdr *hdr, size_t len,
318 u8 method, struct wpabuf **resp)
321 if (data->tnc_started && data->phase2_method &&
322 data->phase2_priv && method == EAP_TYPE_TNC &&
323 data->phase2_eap_type.method == EAP_TYPE_TNC)
324 return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len,
327 if (data->ready_for_tnc && !data->tnc_started &&
328 method == EAP_TYPE_TNC) {
329 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
331 data->tnc_started = 1;
334 if (data->tnc_started) {
335 if (data->phase2_eap_type.vendor != EAP_VENDOR_IETF ||
336 data->phase2_eap_type.method == EAP_TYPE_TNC) {
337 wpa_printf(MSG_DEBUG, "EAP-TTLS: Unexpected EAP "
338 "type %d for TNC", method);
342 data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
343 data->phase2_eap_type.method = method;
344 wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
345 "Phase 2 EAP vendor %d method %d (TNC)",
346 data->phase2_eap_type.vendor,
347 data->phase2_eap_type.method);
349 if (data->phase2_type == EAP_TTLS_PHASE2_EAP)
350 eap_ttls_phase2_eap_deinit(sm, data);
354 if (data->phase2_eap_type.vendor == EAP_VENDOR_IETF &&
355 data->phase2_eap_type.method == EAP_TYPE_NONE)
356 eap_ttls_phase2_select_eap_method(data, method);
358 if (method != data->phase2_eap_type.method || method == EAP_TYPE_NONE)
360 if (eap_peer_tls_phase2_nak(data->phase2_eap_types,
361 data->num_phase2_eap_types,
367 if (data->phase2_priv == NULL) {
368 data->phase2_method = eap_peer_get_eap_method(
369 EAP_VENDOR_IETF, method);
370 if (data->phase2_method) {
372 data->phase2_priv = data->phase2_method->init(sm);
376 if (data->phase2_priv == NULL || data->phase2_method == NULL) {
377 wpa_printf(MSG_INFO, "EAP-TTLS: failed to initialize "
378 "Phase 2 EAP method %d", method);
382 return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len, resp);
386 static int eap_ttls_phase2_request_eap(struct eap_sm *sm,
387 struct eap_ttls_data *data,
388 struct eap_method_ret *ret,
390 struct wpabuf **resp)
392 size_t len = be_to_host16(hdr->length);
394 struct eap_peer_config *config = eap_get_config(sm);
396 if (len <= sizeof(struct eap_hdr)) {
397 wpa_printf(MSG_INFO, "EAP-TTLS: too short "
398 "Phase 2 request (len=%lu)", (unsigned long) len);
401 pos = (u8 *) (hdr + 1);
402 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP Request: type=%d", *pos);
404 case EAP_TYPE_IDENTITY:
405 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
408 if (eap_ttls_phase2_request_eap_method(sm, data, ret, hdr, len,
415 (config->pending_req_identity || config->pending_req_password ||
416 config->pending_req_otp)) {
423 wpa_hexdump_buf(MSG_DEBUG, "EAP-TTLS: AVP encapsulate EAP Response",
425 return eap_ttls_avp_encapsulate(resp, RADIUS_ATTR_EAP_MESSAGE, 1);
429 static int eap_ttls_phase2_request_mschapv2(struct eap_sm *sm,
430 struct eap_ttls_data *data,
431 struct eap_method_ret *ret,
432 struct wpabuf **resp)
435 wpa_printf(MSG_ERROR, "EAP-TTLS: MSCHAPV2 not supported in FIPS build");
437 #else /* CONFIG_FIPS */
440 u8 *buf, *pos, *challenge, *peer_challenge;
441 const u8 *identity, *password;
442 size_t identity_len, password_len;
445 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAPV2 Request");
447 identity = eap_get_config_identity(sm, &identity_len);
448 password = eap_get_config_password2(sm, &password_len, &pwhash);
449 if (identity == NULL || password == NULL)
452 msg = wpabuf_alloc(identity_len + 1000);
454 wpa_printf(MSG_ERROR,
455 "EAP-TTLS/MSCHAPV2: Failed to allocate memory");
458 pos = buf = wpabuf_mhead(msg);
461 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
462 identity, identity_len);
464 /* MS-CHAP-Challenge */
465 challenge = eap_ttls_implicit_challenge(
466 sm, data, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN + 1);
467 if (challenge == NULL) {
469 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
470 "implicit challenge");
474 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
475 RADIUS_VENDOR_ID_MICROSOFT, 1,
476 challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
478 /* MS-CHAP2-Response */
479 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP2_RESPONSE,
480 RADIUS_VENDOR_ID_MICROSOFT, 1,
481 EAP_TTLS_MSCHAPV2_RESPONSE_LEN);
482 data->ident = challenge[EAP_TTLS_MSCHAPV2_CHALLENGE_LEN];
483 *pos++ = data->ident;
484 *pos++ = 0; /* Flags */
485 if (os_get_random(pos, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN) < 0) {
488 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to get "
489 "random data for peer challenge");
492 peer_challenge = pos;
493 pos += EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
494 os_memset(pos, 0, 8); /* Reserved, must be zero */
496 if (mschapv2_derive_response(identity, identity_len, password,
497 password_len, pwhash, challenge,
498 peer_challenge, pos, data->auth_response,
502 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
506 data->auth_response_valid = 1;
512 wpabuf_put(msg, pos - buf);
516 #else /* EAP_MSCHAPv2 */
517 wpa_printf(MSG_ERROR, "EAP-TTLS: MSCHAPv2 not included in the build");
519 #endif /* EAP_MSCHAPv2 */
520 #endif /* CONFIG_FIPS */
524 static int eap_ttls_phase2_request_mschap(struct eap_sm *sm,
525 struct eap_ttls_data *data,
526 struct eap_method_ret *ret,
527 struct wpabuf **resp)
530 wpa_printf(MSG_ERROR, "EAP-TTLS: MSCHAP not supported in FIPS build");
532 #else /* CONFIG_FIPS */
534 u8 *buf, *pos, *challenge;
535 const u8 *identity, *password;
536 size_t identity_len, password_len;
539 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAP Request");
541 identity = eap_get_config_identity(sm, &identity_len);
542 password = eap_get_config_password2(sm, &password_len, &pwhash);
543 if (identity == NULL || password == NULL)
546 msg = wpabuf_alloc(identity_len + 1000);
548 wpa_printf(MSG_ERROR,
549 "EAP-TTLS/MSCHAP: Failed to allocate memory");
552 pos = buf = wpabuf_mhead(msg);
555 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
556 identity, identity_len);
558 /* MS-CHAP-Challenge */
559 challenge = eap_ttls_implicit_challenge(
560 sm, data, EAP_TTLS_MSCHAP_CHALLENGE_LEN + 1);
561 if (challenge == NULL) {
563 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAP: Failed to derive "
564 "implicit challenge");
568 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
569 RADIUS_VENDOR_ID_MICROSOFT, 1,
570 challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
572 /* MS-CHAP-Response */
573 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP_RESPONSE,
574 RADIUS_VENDOR_ID_MICROSOFT, 1,
575 EAP_TTLS_MSCHAP_RESPONSE_LEN);
576 data->ident = challenge[EAP_TTLS_MSCHAP_CHALLENGE_LEN];
577 *pos++ = data->ident;
578 *pos++ = 1; /* Flags: Use NT style passwords */
579 os_memset(pos, 0, 24); /* LM-Response */
582 challenge_response(challenge, password, pos); /* NT-Response */
583 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password hash",
586 nt_challenge_response(challenge, password, password_len,
587 pos); /* NT-Response */
588 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password",
589 password, password_len);
591 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP implicit challenge",
592 challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
593 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP response", pos, 24);
598 wpabuf_put(msg, pos - buf);
601 /* EAP-TTLS/MSCHAP does not provide tunneled success
602 * notification, so assume that Phase2 succeeds. */
603 ret->methodState = METHOD_DONE;
604 ret->decision = DECISION_COND_SUCC;
607 #endif /* CONFIG_FIPS */
611 static int eap_ttls_phase2_request_pap(struct eap_sm *sm,
612 struct eap_ttls_data *data,
613 struct eap_method_ret *ret,
614 struct wpabuf **resp)
619 const u8 *identity, *password;
620 size_t identity_len, password_len;
622 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 PAP Request");
624 identity = eap_get_config_identity(sm, &identity_len);
625 password = eap_get_config_password(sm, &password_len);
626 if (identity == NULL || password == NULL)
629 msg = wpabuf_alloc(identity_len + password_len + 100);
631 wpa_printf(MSG_ERROR,
632 "EAP-TTLS/PAP: Failed to allocate memory");
635 pos = buf = wpabuf_mhead(msg);
638 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
639 identity, identity_len);
641 /* User-Password; in RADIUS, this is encrypted, but EAP-TTLS encrypts
642 * the data, so no separate encryption is used in the AVP itself.
643 * However, the password is padded to obfuscate its length. */
644 pad = password_len == 0 ? 16 : (16 - (password_len & 15)) & 15;
645 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_USER_PASSWORD, 0, 1,
647 os_memcpy(pos, password, password_len);
649 os_memset(pos, 0, pad);
653 wpabuf_put(msg, pos - buf);
656 /* EAP-TTLS/PAP does not provide tunneled success notification,
657 * so assume that Phase2 succeeds. */
658 ret->methodState = METHOD_DONE;
659 ret->decision = DECISION_COND_SUCC;
665 static int eap_ttls_phase2_request_chap(struct eap_sm *sm,
666 struct eap_ttls_data *data,
667 struct eap_method_ret *ret,
668 struct wpabuf **resp)
671 wpa_printf(MSG_ERROR, "EAP-TTLS: CHAP not supported in FIPS build");
673 #else /* CONFIG_FIPS */
675 u8 *buf, *pos, *challenge;
676 const u8 *identity, *password;
677 size_t identity_len, password_len;
679 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 CHAP Request");
681 identity = eap_get_config_identity(sm, &identity_len);
682 password = eap_get_config_password(sm, &password_len);
683 if (identity == NULL || password == NULL)
686 msg = wpabuf_alloc(identity_len + 1000);
688 wpa_printf(MSG_ERROR,
689 "EAP-TTLS/CHAP: Failed to allocate memory");
692 pos = buf = wpabuf_mhead(msg);
695 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
696 identity, identity_len);
699 challenge = eap_ttls_implicit_challenge(
700 sm, data, EAP_TTLS_CHAP_CHALLENGE_LEN + 1);
701 if (challenge == NULL) {
703 wpa_printf(MSG_ERROR, "EAP-TTLS/CHAP: Failed to derive "
704 "implicit challenge");
708 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_CHAP_CHALLENGE, 0, 1,
709 challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
712 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_CHAP_PASSWORD, 0, 1,
713 1 + EAP_TTLS_CHAP_PASSWORD_LEN);
714 data->ident = challenge[EAP_TTLS_CHAP_CHALLENGE_LEN];
715 *pos++ = data->ident;
717 /* MD5(Ident + Password + Challenge) */
718 chap_md5(data->ident, password, password_len, challenge,
719 EAP_TTLS_CHAP_CHALLENGE_LEN, pos);
721 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: CHAP username",
722 identity, identity_len);
723 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: CHAP password",
724 password, password_len);
725 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP implicit challenge",
726 challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
727 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP password",
728 pos, EAP_TTLS_CHAP_PASSWORD_LEN);
729 pos += EAP_TTLS_CHAP_PASSWORD_LEN;
733 wpabuf_put(msg, pos - buf);
736 /* EAP-TTLS/CHAP does not provide tunneled success
737 * notification, so assume that Phase2 succeeds. */
738 ret->methodState = METHOD_DONE;
739 ret->decision = DECISION_COND_SUCC;
742 #endif /* CONFIG_FIPS */
746 static int eap_ttls_phase2_request(struct eap_sm *sm,
747 struct eap_ttls_data *data,
748 struct eap_method_ret *ret,
750 struct wpabuf **resp)
754 enum phase2_types phase2_type = data->phase2_type;
757 if (data->tnc_started) {
758 wpa_printf(MSG_DEBUG, "EAP-TTLS: Processing TNC");
759 phase2_type = EAP_TTLS_PHASE2_EAP;
763 if (phase2_type == EAP_TTLS_PHASE2_MSCHAPV2 ||
764 phase2_type == EAP_TTLS_PHASE2_MSCHAP ||
765 phase2_type == EAP_TTLS_PHASE2_PAP ||
766 phase2_type == EAP_TTLS_PHASE2_CHAP) {
767 if (eap_get_config_identity(sm, &len) == NULL) {
769 "EAP-TTLS: Identity not configured");
770 eap_sm_request_identity(sm);
771 if (eap_get_config_password(sm, &len) == NULL)
772 eap_sm_request_password(sm);
776 if (eap_get_config_password(sm, &len) == NULL) {
778 "EAP-TTLS: Password not configured");
779 eap_sm_request_password(sm);
784 switch (phase2_type) {
785 case EAP_TTLS_PHASE2_EAP:
786 res = eap_ttls_phase2_request_eap(sm, data, ret, hdr, resp);
788 case EAP_TTLS_PHASE2_MSCHAPV2:
789 res = eap_ttls_phase2_request_mschapv2(sm, data, ret, resp);
791 case EAP_TTLS_PHASE2_MSCHAP:
792 res = eap_ttls_phase2_request_mschap(sm, data, ret, resp);
794 case EAP_TTLS_PHASE2_PAP:
795 res = eap_ttls_phase2_request_pap(sm, data, ret, resp);
797 case EAP_TTLS_PHASE2_CHAP:
798 res = eap_ttls_phase2_request_chap(sm, data, ret, resp);
801 wpa_printf(MSG_ERROR, "EAP-TTLS: Phase 2 - Unknown");
807 ret->methodState = METHOD_DONE;
808 ret->decision = DECISION_FAIL;
815 struct ttls_parse_avp {
823 static int eap_ttls_parse_attr_eap(const u8 *dpos, size_t dlen,
824 struct ttls_parse_avp *parse)
826 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - EAP Message");
827 if (parse->eapdata == NULL) {
828 parse->eapdata = os_malloc(dlen);
829 if (parse->eapdata == NULL) {
830 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
831 "memory for Phase 2 EAP data");
834 os_memcpy(parse->eapdata, dpos, dlen);
835 parse->eap_len = dlen;
837 u8 *neweap = os_realloc(parse->eapdata, parse->eap_len + dlen);
838 if (neweap == NULL) {
839 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
840 "memory for Phase 2 EAP data");
843 os_memcpy(neweap + parse->eap_len, dpos, dlen);
844 parse->eapdata = neweap;
845 parse->eap_len += dlen;
852 static int eap_ttls_parse_avp(u8 *pos, size_t left,
853 struct ttls_parse_avp *parse)
855 struct ttls_avp *avp;
856 u32 avp_code, avp_length, vendor_id = 0;
860 avp = (struct ttls_avp *) pos;
861 avp_code = be_to_host32(avp->avp_code);
862 avp_length = be_to_host32(avp->avp_length);
863 avp_flags = (avp_length >> 24) & 0xff;
864 avp_length &= 0xffffff;
865 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP: code=%d flags=0x%02x "
866 "length=%d", (int) avp_code, avp_flags,
869 if (avp_length > left) {
870 wpa_printf(MSG_WARNING, "EAP-TTLS: AVP overflow "
871 "(len=%d, left=%lu) - dropped",
872 (int) avp_length, (unsigned long) left);
876 if (avp_length < sizeof(*avp)) {
877 wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid AVP length %d",
882 dpos = (u8 *) (avp + 1);
883 dlen = avp_length - sizeof(*avp);
884 if (avp_flags & AVP_FLAGS_VENDOR) {
886 wpa_printf(MSG_WARNING, "EAP-TTLS: Vendor AVP "
890 vendor_id = WPA_GET_BE32(dpos);
891 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP vendor_id %d",
897 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP data", dpos, dlen);
899 if (vendor_id == 0 && avp_code == RADIUS_ATTR_EAP_MESSAGE) {
900 if (eap_ttls_parse_attr_eap(dpos, dlen, parse) < 0)
902 } else if (vendor_id == 0 && avp_code == RADIUS_ATTR_REPLY_MESSAGE) {
903 /* This is an optional message that can be displayed to
905 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: AVP - Reply-Message",
907 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
908 avp_code == RADIUS_ATTR_MS_CHAP2_SUCCESS) {
909 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP2-Success",
912 wpa_printf(MSG_WARNING, "EAP-TTLS: Unexpected "
913 "MS-CHAP2-Success length "
914 "(len=%lu, expected 43)",
915 (unsigned long) dlen);
918 parse->mschapv2 = dpos;
919 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
920 avp_code == RADIUS_ATTR_MS_CHAP_ERROR) {
921 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP-Error",
923 parse->mschapv2_error = 1;
924 } else if (avp_flags & AVP_FLAGS_MANDATORY) {
925 wpa_printf(MSG_WARNING, "EAP-TTLS: Unsupported mandatory AVP "
926 "code %d vendor_id %d - dropped",
927 (int) avp_code, (int) vendor_id);
930 wpa_printf(MSG_DEBUG, "EAP-TTLS: Ignoring unsupported AVP "
931 "code %d vendor_id %d",
932 (int) avp_code, (int) vendor_id);
939 static int eap_ttls_parse_avps(struct wpabuf *in_decrypted,
940 struct ttls_parse_avp *parse)
946 pos = wpabuf_mhead(in_decrypted);
947 left = wpabuf_len(in_decrypted);
948 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Decrypted Phase 2 AVPs", pos, left);
949 if (left < sizeof(struct ttls_avp)) {
950 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 AVP frame"
951 " len=%lu expected %lu or more - dropped",
952 (unsigned long) left,
953 (unsigned long) sizeof(struct ttls_avp));
958 os_memset(parse, 0, sizeof(*parse));
961 avp_length = eap_ttls_parse_avp(pos, left, parse);
965 pad = (4 - (avp_length & 3)) & 3;
966 pos += avp_length + pad;
967 if (left < avp_length + pad)
970 left -= avp_length + pad;
977 static u8 * eap_ttls_fake_identity_request(void)
982 wpa_printf(MSG_DEBUG, "EAP-TTLS: empty data in beginning of "
983 "Phase 2 - use fake EAP-Request Identity");
984 buf = os_malloc(sizeof(*hdr) + 1);
986 wpa_printf(MSG_WARNING, "EAP-TTLS: failed to allocate "
987 "memory for fake EAP-Identity Request");
991 hdr = (struct eap_hdr *) buf;
992 hdr->code = EAP_CODE_REQUEST;
994 hdr->length = host_to_be16(sizeof(*hdr) + 1);
995 buf[sizeof(*hdr)] = EAP_TYPE_IDENTITY;
1001 static int eap_ttls_encrypt_response(struct eap_sm *sm,
1002 struct eap_ttls_data *data,
1003 struct wpabuf *resp, u8 identifier,
1004 struct wpabuf **out_data)
1009 wpa_hexdump_buf_key(MSG_DEBUG, "EAP-TTLS: Encrypting Phase 2 data",
1011 if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
1012 data->ttls_version, identifier,
1014 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to encrypt a Phase 2 "
1025 static int eap_ttls_process_phase2_eap(struct eap_sm *sm,
1026 struct eap_ttls_data *data,
1027 struct eap_method_ret *ret,
1028 struct ttls_parse_avp *parse,
1029 struct wpabuf **resp)
1031 struct eap_hdr *hdr;
1034 if (parse->eapdata == NULL) {
1035 wpa_printf(MSG_WARNING, "EAP-TTLS: No EAP Message in the "
1036 "packet - dropped");
1040 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP",
1041 parse->eapdata, parse->eap_len);
1042 hdr = (struct eap_hdr *) parse->eapdata;
1044 if (parse->eap_len < sizeof(*hdr)) {
1045 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 EAP "
1046 "frame (len=%lu, expected %lu or more) - dropped",
1047 (unsigned long) parse->eap_len,
1048 (unsigned long) sizeof(*hdr));
1051 len = be_to_host16(hdr->length);
1052 if (len > parse->eap_len) {
1053 wpa_printf(MSG_INFO, "EAP-TTLS: Length mismatch in Phase 2 "
1054 "EAP frame (EAP hdr len=%lu, EAP data len in "
1056 (unsigned long) len,
1057 (unsigned long) parse->eap_len);
1060 wpa_printf(MSG_DEBUG, "EAP-TTLS: received Phase 2: code=%d "
1061 "identifier=%d length=%lu",
1062 hdr->code, hdr->identifier, (unsigned long) len);
1063 switch (hdr->code) {
1064 case EAP_CODE_REQUEST:
1065 if (eap_ttls_phase2_request(sm, data, ret, hdr, resp)) {
1066 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
1067 "processing failed");
1072 wpa_printf(MSG_INFO, "EAP-TTLS: Unexpected code=%d in "
1073 "Phase 2 EAP header", hdr->code);
1081 static int eap_ttls_process_phase2_mschapv2(struct eap_sm *sm,
1082 struct eap_ttls_data *data,
1083 struct eap_method_ret *ret,
1084 struct ttls_parse_avp *parse)
1087 if (parse->mschapv2_error) {
1088 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Received "
1089 "MS-CHAP-Error - failed");
1090 ret->methodState = METHOD_DONE;
1091 ret->decision = DECISION_FAIL;
1092 /* Reply with empty data to ACK error */
1096 if (parse->mschapv2 == NULL) {
1098 if (data->phase2_success && parse->eapdata) {
1100 * Allow EAP-TNC to be started after successfully
1101 * completed MSCHAPV2.
1105 #endif /* EAP_TNC */
1106 wpa_printf(MSG_WARNING, "EAP-TTLS: no MS-CHAP2-Success AVP "
1107 "received for Phase2 MSCHAPV2");
1110 if (parse->mschapv2[0] != data->ident) {
1111 wpa_printf(MSG_WARNING, "EAP-TTLS: Ident mismatch for Phase 2 "
1112 "MSCHAPV2 (received Ident 0x%02x, expected 0x%02x)",
1113 parse->mschapv2[0], data->ident);
1116 if (!data->auth_response_valid ||
1117 mschapv2_verify_auth_response(data->auth_response,
1118 parse->mschapv2 + 1, 42)) {
1119 wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid authenticator "
1120 "response in Phase 2 MSCHAPV2 success request");
1124 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 MSCHAPV2 "
1125 "authentication succeeded");
1126 ret->methodState = METHOD_DONE;
1127 ret->decision = DECISION_UNCOND_SUCC;
1128 data->phase2_success = 1;
1131 * Reply with empty data; authentication server will reply
1132 * with EAP-Success after this.
1135 #else /* EAP_MSCHAPv2 */
1136 wpa_printf(MSG_ERROR, "EAP-TTLS: MSCHAPv2 not included in the build");
1138 #endif /* EAP_MSCHAPv2 */
1143 static int eap_ttls_process_tnc_start(struct eap_sm *sm,
1144 struct eap_ttls_data *data,
1145 struct eap_method_ret *ret,
1146 struct ttls_parse_avp *parse,
1147 struct wpabuf **resp)
1149 /* TNC uses inner EAP method after non-EAP TTLS phase 2. */
1150 if (parse->eapdata == NULL) {
1151 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
1152 "unexpected tunneled data (no EAP)");
1156 if (!data->ready_for_tnc) {
1157 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
1158 "EAP after non-EAP, but not ready for TNC");
1162 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
1164 data->tnc_started = 1;
1166 if (eap_ttls_process_phase2_eap(sm, data, ret, parse, resp) < 0)
1171 #endif /* EAP_TNC */
1174 static int eap_ttls_process_decrypted(struct eap_sm *sm,
1175 struct eap_ttls_data *data,
1176 struct eap_method_ret *ret,
1178 struct ttls_parse_avp *parse,
1179 struct wpabuf *in_decrypted,
1180 struct wpabuf **out_data)
1182 struct wpabuf *resp = NULL;
1183 struct eap_peer_config *config = eap_get_config(sm);
1185 enum phase2_types phase2_type = data->phase2_type;
1188 if (data->tnc_started)
1189 phase2_type = EAP_TTLS_PHASE2_EAP;
1190 #endif /* EAP_TNC */
1192 switch (phase2_type) {
1193 case EAP_TTLS_PHASE2_EAP:
1194 if (eap_ttls_process_phase2_eap(sm, data, ret, parse, &resp) <
1198 case EAP_TTLS_PHASE2_MSCHAPV2:
1199 res = eap_ttls_process_phase2_mschapv2(sm, data, ret, parse);
1201 if (res == 1 && parse->eapdata && data->phase2_success) {
1203 * TNC may be required as the next
1204 * authentication method within the tunnel.
1206 ret->methodState = METHOD_MAY_CONT;
1207 data->ready_for_tnc = 1;
1208 if (eap_ttls_process_tnc_start(sm, data, ret, parse,
1212 #endif /* EAP_TNC */
1214 case EAP_TTLS_PHASE2_MSCHAP:
1215 case EAP_TTLS_PHASE2_PAP:
1216 case EAP_TTLS_PHASE2_CHAP:
1218 if (eap_ttls_process_tnc_start(sm, data, ret, parse, &resp) <
1223 /* EAP-TTLS/{MSCHAP,PAP,CHAP} should not send any TLS tunneled
1224 * requests to the supplicant */
1225 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received unexpected "
1228 #endif /* EAP_TNC */
1232 if (eap_ttls_encrypt_response(sm, data, resp, identifier,
1235 } else if (config->pending_req_identity ||
1236 config->pending_req_password ||
1237 config->pending_req_otp ||
1238 config->pending_req_new_password) {
1239 wpabuf_free(data->pending_phase2_req);
1240 data->pending_phase2_req = wpabuf_dup(in_decrypted);
1247 static int eap_ttls_implicit_identity_request(struct eap_sm *sm,
1248 struct eap_ttls_data *data,
1249 struct eap_method_ret *ret,
1251 struct wpabuf **out_data)
1254 struct eap_hdr *hdr;
1255 struct wpabuf *resp;
1257 hdr = (struct eap_hdr *) eap_ttls_fake_identity_request();
1259 ret->methodState = METHOD_DONE;
1260 ret->decision = DECISION_FAIL;
1265 if (eap_ttls_phase2_request(sm, data, ret, hdr, &resp)) {
1266 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
1267 "processing failed");
1270 struct eap_peer_config *config = eap_get_config(sm);
1272 (config->pending_req_identity ||
1273 config->pending_req_password ||
1274 config->pending_req_otp ||
1275 config->pending_req_new_password)) {
1277 * Use empty buffer to force implicit request
1278 * processing when EAP request is re-processed after
1281 wpabuf_free(data->pending_phase2_req);
1282 data->pending_phase2_req = wpabuf_alloc(0);
1285 retval = eap_ttls_encrypt_response(sm, data, resp, identifier,
1292 ret->methodState = METHOD_DONE;
1293 ret->decision = DECISION_FAIL;
1300 static int eap_ttls_phase2_start(struct eap_sm *sm, struct eap_ttls_data *data,
1301 struct eap_method_ret *ret, u8 identifier,
1302 struct wpabuf **out_data)
1304 data->phase2_start = 0;
1307 * EAP-TTLS does not use Phase2 on fast re-auth; this must be done only
1308 * if TLS part was indeed resuming a previous session. Most
1309 * Authentication Servers terminate EAP-TTLS before reaching this
1310 * point, but some do not. Make wpa_supplicant stop phase 2 here, if
1314 tls_connection_resumed(sm->ssl_ctx, data->ssl.conn)) {
1315 wpa_printf(MSG_DEBUG, "EAP-TTLS: Session resumption - "
1317 *out_data = eap_peer_tls_build_ack(identifier, EAP_TYPE_TTLS,
1318 data->ttls_version);
1319 ret->methodState = METHOD_DONE;
1320 ret->decision = DECISION_UNCOND_SUCC;
1321 data->phase2_success = 1;
1325 return eap_ttls_implicit_identity_request(sm, data, ret, identifier,
1330 static int eap_ttls_decrypt(struct eap_sm *sm, struct eap_ttls_data *data,
1331 struct eap_method_ret *ret, u8 identifier,
1332 const struct wpabuf *in_data,
1333 struct wpabuf **out_data)
1335 struct wpabuf *in_decrypted = NULL;
1337 struct ttls_parse_avp parse;
1339 os_memset(&parse, 0, sizeof(parse));
1341 wpa_printf(MSG_DEBUG, "EAP-TTLS: received %lu bytes encrypted data for"
1343 in_data ? (unsigned long) wpabuf_len(in_data) : 0);
1345 if (data->pending_phase2_req) {
1346 wpa_printf(MSG_DEBUG, "EAP-TTLS: Pending Phase 2 request - "
1347 "skip decryption and use old data");
1348 /* Clear TLS reassembly state. */
1349 eap_peer_tls_reset_input(&data->ssl);
1351 in_decrypted = data->pending_phase2_req;
1352 data->pending_phase2_req = NULL;
1353 if (wpabuf_len(in_decrypted) == 0) {
1354 wpabuf_free(in_decrypted);
1355 return eap_ttls_implicit_identity_request(
1356 sm, data, ret, identifier, out_data);
1361 if ((in_data == NULL || wpabuf_len(in_data) == 0) &&
1362 data->phase2_start) {
1363 return eap_ttls_phase2_start(sm, data, ret, identifier,
1367 if (in_data == NULL || wpabuf_len(in_data) == 0) {
1368 /* Received TLS ACK - requesting more fragments */
1369 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
1371 identifier, NULL, out_data);
1374 retval = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
1379 data->phase2_start = 0;
1381 if (eap_ttls_parse_avps(in_decrypted, &parse) < 0) {
1386 retval = eap_ttls_process_decrypted(sm, data, ret, identifier,
1387 &parse, in_decrypted, out_data);
1390 wpabuf_free(in_decrypted);
1391 os_free(parse.eapdata);
1394 ret->methodState = METHOD_DONE;
1395 ret->decision = DECISION_FAIL;
1402 static int eap_ttls_process_handshake(struct eap_sm *sm,
1403 struct eap_ttls_data *data,
1404 struct eap_method_ret *ret,
1406 const struct wpabuf *in_data,
1407 struct wpabuf **out_data)
1411 res = eap_peer_tls_process_helper(sm, &data->ssl, EAP_TYPE_TTLS,
1412 data->ttls_version, identifier,
1415 wpa_printf(MSG_DEBUG, "EAP-TTLS: TLS processing failed");
1416 ret->methodState = METHOD_DONE;
1417 ret->decision = DECISION_FAIL;
1421 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1422 wpa_printf(MSG_DEBUG, "EAP-TTLS: TLS done, proceed to "
1424 if (data->resuming) {
1425 wpa_printf(MSG_DEBUG, "EAP-TTLS: fast reauth - may "
1427 ret->decision = DECISION_COND_SUCC;
1428 ret->methodState = METHOD_MAY_CONT;
1430 data->phase2_start = 1;
1431 eap_ttls_v0_derive_key(sm, data);
1433 if (*out_data == NULL || wpabuf_len(*out_data) == 0) {
1434 if (eap_ttls_decrypt(sm, data, ret, identifier,
1436 wpa_printf(MSG_WARNING, "EAP-TTLS: "
1437 "failed to process early "
1438 "start for Phase 2");
1447 * Application data included in the handshake message.
1449 wpabuf_free(data->pending_phase2_req);
1450 data->pending_phase2_req = *out_data;
1452 res = eap_ttls_decrypt(sm, data, ret, identifier, in_data,
1460 static void eap_ttls_check_auth_status(struct eap_sm *sm,
1461 struct eap_ttls_data *data,
1462 struct eap_method_ret *ret)
1464 if (ret->methodState == METHOD_DONE) {
1465 ret->allowNotifications = FALSE;
1466 if (ret->decision == DECISION_UNCOND_SUCC ||
1467 ret->decision == DECISION_COND_SUCC) {
1468 wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1469 "completed successfully");
1470 data->phase2_success = 1;
1472 if (!data->ready_for_tnc && !data->tnc_started) {
1474 * TNC may be required as the next
1475 * authentication method within the tunnel.
1477 ret->methodState = METHOD_MAY_CONT;
1478 data->ready_for_tnc = 1;
1480 #endif /* EAP_TNC */
1482 } else if (ret->methodState == METHOD_MAY_CONT &&
1483 (ret->decision == DECISION_UNCOND_SUCC ||
1484 ret->decision == DECISION_COND_SUCC)) {
1485 wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1486 "completed successfully (MAY_CONT)");
1487 data->phase2_success = 1;
1492 static struct wpabuf * eap_ttls_process(struct eap_sm *sm, void *priv,
1493 struct eap_method_ret *ret,
1494 const struct wpabuf *reqData)
1499 struct wpabuf *resp;
1501 struct eap_ttls_data *data = priv;
1504 pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_TTLS, ret,
1505 reqData, &left, &flags);
1508 id = eap_get_id(reqData);
1510 if (flags & EAP_TLS_FLAGS_START) {
1511 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start (server ver=%d, own "
1512 "ver=%d)", flags & EAP_TLS_VERSION_MASK,
1513 data->ttls_version);
1515 /* RFC 5281, Ch. 9.2:
1516 * "This packet MAY contain additional information in the form
1517 * of AVPs, which may provide useful hints to the client"
1518 * For now, ignore any potential extra data.
1523 wpabuf_set(&msg, pos, left);
1526 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1528 res = eap_ttls_decrypt(sm, data, ret, id, &msg, &resp);
1530 res = eap_ttls_process_handshake(sm, data, ret, id,
1534 eap_ttls_check_auth_status(sm, data, ret);
1536 /* FIX: what about res == -1? Could just move all error processing into
1537 * the other functions and get rid of this res==1 case here. */
1540 return eap_peer_tls_build_ack(id, EAP_TYPE_TTLS,
1541 data->ttls_version);
1547 static Boolean eap_ttls_has_reauth_data(struct eap_sm *sm, void *priv)
1549 struct eap_ttls_data *data = priv;
1550 return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1551 data->phase2_success;
1555 static void eap_ttls_deinit_for_reauth(struct eap_sm *sm, void *priv)
1557 struct eap_ttls_data *data = priv;
1558 wpabuf_free(data->pending_phase2_req);
1559 data->pending_phase2_req = NULL;
1561 data->ready_for_tnc = 0;
1562 data->tnc_started = 0;
1563 #endif /* EAP_TNC */
1567 static void * eap_ttls_init_for_reauth(struct eap_sm *sm, void *priv)
1569 struct eap_ttls_data *data = priv;
1570 eap_ttls_free_key(data);
1571 os_free(data->session_id);
1572 data->session_id = NULL;
1573 if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
1577 if (data->phase2_priv && data->phase2_method &&
1578 data->phase2_method->init_for_reauth)
1579 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
1580 data->phase2_start = 0;
1581 data->phase2_success = 0;
1588 static int eap_ttls_get_status(struct eap_sm *sm, void *priv, char *buf,
1589 size_t buflen, int verbose)
1591 struct eap_ttls_data *data = priv;
1594 len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
1595 ret = os_snprintf(buf + len, buflen - len,
1596 "EAP-TTLSv%d Phase2 method=",
1597 data->ttls_version);
1598 if (os_snprintf_error(buflen - len, ret))
1601 switch (data->phase2_type) {
1602 case EAP_TTLS_PHASE2_EAP:
1603 ret = os_snprintf(buf + len, buflen - len, "EAP-%s\n",
1604 data->phase2_method ?
1605 data->phase2_method->name : "?");
1607 case EAP_TTLS_PHASE2_MSCHAPV2:
1608 ret = os_snprintf(buf + len, buflen - len, "MSCHAPV2\n");
1610 case EAP_TTLS_PHASE2_MSCHAP:
1611 ret = os_snprintf(buf + len, buflen - len, "MSCHAP\n");
1613 case EAP_TTLS_PHASE2_PAP:
1614 ret = os_snprintf(buf + len, buflen - len, "PAP\n");
1616 case EAP_TTLS_PHASE2_CHAP:
1617 ret = os_snprintf(buf + len, buflen - len, "CHAP\n");
1623 if (os_snprintf_error(buflen - len, ret))
1631 static Boolean eap_ttls_isKeyAvailable(struct eap_sm *sm, void *priv)
1633 struct eap_ttls_data *data = priv;
1634 return data->key_data != NULL && data->phase2_success;
1638 static u8 * eap_ttls_getKey(struct eap_sm *sm, void *priv, size_t *len)
1640 struct eap_ttls_data *data = priv;
1643 if (data->key_data == NULL || !data->phase2_success)
1646 key = os_malloc(EAP_TLS_KEY_LEN);
1650 *len = EAP_TLS_KEY_LEN;
1651 os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
1657 static u8 * eap_ttls_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
1659 struct eap_ttls_data *data = priv;
1662 if (data->session_id == NULL || !data->phase2_success)
1665 id = os_malloc(data->id_len);
1669 *len = data->id_len;
1670 os_memcpy(id, data->session_id, data->id_len);
1676 static u8 * eap_ttls_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
1678 struct eap_ttls_data *data = priv;
1681 if (data->key_data == NULL)
1684 key = os_malloc(EAP_EMSK_LEN);
1688 *len = EAP_EMSK_LEN;
1689 os_memcpy(key, data->key_data + EAP_TLS_KEY_LEN, EAP_EMSK_LEN);
1695 int eap_peer_ttls_register(void)
1697 struct eap_method *eap;
1700 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1701 EAP_VENDOR_IETF, EAP_TYPE_TTLS, "TTLS");
1705 eap->init = eap_ttls_init;
1706 eap->deinit = eap_ttls_deinit;
1707 eap->process = eap_ttls_process;
1708 eap->isKeyAvailable = eap_ttls_isKeyAvailable;
1709 eap->getKey = eap_ttls_getKey;
1710 eap->getSessionId = eap_ttls_get_session_id;
1711 eap->get_status = eap_ttls_get_status;
1712 eap->has_reauth_data = eap_ttls_has_reauth_data;
1713 eap->deinit_for_reauth = eap_ttls_deinit_for_reauth;
1714 eap->init_for_reauth = eap_ttls_init_for_reauth;
1715 eap->get_emsk = eap_ttls_get_emsk;
1717 ret = eap_peer_method_register(eap);
1719 eap_peer_method_free(eap);