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_deinit(struct eap_sm *sm, void *priv)
138 struct eap_ttls_data *data = priv;
141 eap_ttls_phase2_eap_deinit(sm, data);
142 os_free(data->phase2_eap_types);
143 eap_peer_tls_ssl_deinit(sm, &data->ssl);
144 os_free(data->key_data);
145 os_free(data->session_id);
146 wpabuf_free(data->pending_phase2_req);
151 static u8 * eap_ttls_avp_hdr(u8 *avphdr, u32 avp_code, u32 vendor_id,
152 int mandatory, size_t len)
154 struct ttls_avp_vendor *avp;
158 avp = (struct ttls_avp_vendor *) avphdr;
159 flags = mandatory ? AVP_FLAGS_MANDATORY : 0;
161 flags |= AVP_FLAGS_VENDOR;
162 hdrlen = sizeof(*avp);
163 avp->vendor_id = host_to_be32(vendor_id);
165 hdrlen = sizeof(struct ttls_avp);
168 avp->avp_code = host_to_be32(avp_code);
169 avp->avp_length = host_to_be32((flags << 24) | (u32) (hdrlen + len));
171 return avphdr + hdrlen;
175 static u8 * eap_ttls_avp_add(u8 *start, u8 *avphdr, u32 avp_code,
176 u32 vendor_id, int mandatory,
177 const u8 *data, size_t len)
180 pos = eap_ttls_avp_hdr(avphdr, avp_code, vendor_id, mandatory, len);
181 os_memcpy(pos, data, len);
188 static int eap_ttls_avp_encapsulate(struct wpabuf **resp, u32 avp_code,
194 msg = wpabuf_alloc(sizeof(struct ttls_avp) + wpabuf_len(*resp) + 4);
201 avp = wpabuf_mhead(msg);
202 pos = eap_ttls_avp_hdr(avp, avp_code, 0, mandatory, wpabuf_len(*resp));
203 os_memcpy(pos, wpabuf_head(*resp), wpabuf_len(*resp));
204 pos += wpabuf_len(*resp);
207 wpabuf_put(msg, pos - avp);
213 static int eap_ttls_v0_derive_key(struct eap_sm *sm,
214 struct eap_ttls_data *data)
216 os_free(data->key_data);
217 data->key_data = eap_peer_tls_derive_key(sm, &data->ssl,
218 "ttls keying material",
220 if (!data->key_data) {
221 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to derive key");
225 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
226 data->key_data, EAP_TLS_KEY_LEN);
228 os_free(data->session_id);
229 data->session_id = eap_peer_tls_derive_session_id(sm, &data->ssl,
232 if (data->session_id) {
233 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Derived Session-Id",
234 data->session_id, data->id_len);
236 wpa_printf(MSG_ERROR, "EAP-TTLS: Failed to derive Session-Id");
243 static u8 * eap_ttls_implicit_challenge(struct eap_sm *sm,
244 struct eap_ttls_data *data, size_t len)
246 return eap_peer_tls_derive_key(sm, &data->ssl, "ttls challenge", len);
250 static void eap_ttls_phase2_select_eap_method(struct eap_ttls_data *data,
254 for (i = 0; i < data->num_phase2_eap_types; i++) {
255 if (data->phase2_eap_types[i].vendor != EAP_VENDOR_IETF ||
256 data->phase2_eap_types[i].method != method)
259 data->phase2_eap_type.vendor =
260 data->phase2_eap_types[i].vendor;
261 data->phase2_eap_type.method =
262 data->phase2_eap_types[i].method;
263 wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
264 "Phase 2 EAP vendor %d method %d",
265 data->phase2_eap_type.vendor,
266 data->phase2_eap_type.method);
272 static int eap_ttls_phase2_eap_process(struct eap_sm *sm,
273 struct eap_ttls_data *data,
274 struct eap_method_ret *ret,
275 struct eap_hdr *hdr, size_t len,
276 struct wpabuf **resp)
279 struct eap_method_ret iret;
281 os_memset(&iret, 0, sizeof(iret));
282 wpabuf_set(&msg, hdr, len);
283 *resp = data->phase2_method->process(sm, data->phase2_priv, &iret,
285 if ((iret.methodState == METHOD_DONE ||
286 iret.methodState == METHOD_MAY_CONT) &&
287 (iret.decision == DECISION_UNCOND_SUCC ||
288 iret.decision == DECISION_COND_SUCC ||
289 iret.decision == DECISION_FAIL)) {
290 ret->methodState = iret.methodState;
291 ret->decision = iret.decision;
298 static int eap_ttls_phase2_request_eap_method(struct eap_sm *sm,
299 struct eap_ttls_data *data,
300 struct eap_method_ret *ret,
301 struct eap_hdr *hdr, size_t len,
302 u8 method, struct wpabuf **resp)
305 if (data->tnc_started && data->phase2_method &&
306 data->phase2_priv && method == EAP_TYPE_TNC &&
307 data->phase2_eap_type.method == EAP_TYPE_TNC)
308 return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len,
311 if (data->ready_for_tnc && !data->tnc_started &&
312 method == EAP_TYPE_TNC) {
313 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
315 data->tnc_started = 1;
318 if (data->tnc_started) {
319 if (data->phase2_eap_type.vendor != EAP_VENDOR_IETF ||
320 data->phase2_eap_type.method == EAP_TYPE_TNC) {
321 wpa_printf(MSG_DEBUG, "EAP-TTLS: Unexpected EAP "
322 "type %d for TNC", method);
326 data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
327 data->phase2_eap_type.method = method;
328 wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
329 "Phase 2 EAP vendor %d method %d (TNC)",
330 data->phase2_eap_type.vendor,
331 data->phase2_eap_type.method);
333 if (data->phase2_type == EAP_TTLS_PHASE2_EAP)
334 eap_ttls_phase2_eap_deinit(sm, data);
338 if (data->phase2_eap_type.vendor == EAP_VENDOR_IETF &&
339 data->phase2_eap_type.method == EAP_TYPE_NONE)
340 eap_ttls_phase2_select_eap_method(data, method);
342 if (method != data->phase2_eap_type.method || method == EAP_TYPE_NONE)
344 if (eap_peer_tls_phase2_nak(data->phase2_eap_types,
345 data->num_phase2_eap_types,
351 if (data->phase2_priv == NULL) {
352 data->phase2_method = eap_peer_get_eap_method(
353 EAP_VENDOR_IETF, method);
354 if (data->phase2_method) {
356 data->phase2_priv = data->phase2_method->init(sm);
360 if (data->phase2_priv == NULL || data->phase2_method == NULL) {
361 wpa_printf(MSG_INFO, "EAP-TTLS: failed to initialize "
362 "Phase 2 EAP method %d", method);
366 return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len, resp);
370 static int eap_ttls_phase2_request_eap(struct eap_sm *sm,
371 struct eap_ttls_data *data,
372 struct eap_method_ret *ret,
374 struct wpabuf **resp)
376 size_t len = be_to_host16(hdr->length);
378 struct eap_peer_config *config = eap_get_config(sm);
380 if (len <= sizeof(struct eap_hdr)) {
381 wpa_printf(MSG_INFO, "EAP-TTLS: too short "
382 "Phase 2 request (len=%lu)", (unsigned long) len);
385 pos = (u8 *) (hdr + 1);
386 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP Request: type=%d", *pos);
388 case EAP_TYPE_IDENTITY:
389 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
392 if (eap_ttls_phase2_request_eap_method(sm, data, ret, hdr, len,
399 (config->pending_req_identity || config->pending_req_password ||
400 config->pending_req_otp)) {
407 wpa_hexdump_buf(MSG_DEBUG, "EAP-TTLS: AVP encapsulate EAP Response",
409 return eap_ttls_avp_encapsulate(resp, RADIUS_ATTR_EAP_MESSAGE, 1);
413 static int eap_ttls_phase2_request_mschapv2(struct eap_sm *sm,
414 struct eap_ttls_data *data,
415 struct eap_method_ret *ret,
416 struct wpabuf **resp)
420 u8 *buf, *pos, *challenge, *peer_challenge;
421 const u8 *identity, *password;
422 size_t identity_len, password_len;
425 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAPV2 Request");
427 identity = eap_get_config_identity(sm, &identity_len);
428 password = eap_get_config_password2(sm, &password_len, &pwhash);
429 if (identity == NULL || password == NULL)
432 msg = wpabuf_alloc(identity_len + 1000);
434 wpa_printf(MSG_ERROR,
435 "EAP-TTLS/MSCHAPV2: Failed to allocate memory");
438 pos = buf = wpabuf_mhead(msg);
441 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
442 identity, identity_len);
444 /* MS-CHAP-Challenge */
445 challenge = eap_ttls_implicit_challenge(
446 sm, data, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN + 1);
447 if (challenge == NULL) {
449 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
450 "implicit challenge");
454 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
455 RADIUS_VENDOR_ID_MICROSOFT, 1,
456 challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
458 /* MS-CHAP2-Response */
459 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP2_RESPONSE,
460 RADIUS_VENDOR_ID_MICROSOFT, 1,
461 EAP_TTLS_MSCHAPV2_RESPONSE_LEN);
462 data->ident = challenge[EAP_TTLS_MSCHAPV2_CHALLENGE_LEN];
463 *pos++ = data->ident;
464 *pos++ = 0; /* Flags */
465 if (os_get_random(pos, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN) < 0) {
468 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to get "
469 "random data for peer challenge");
472 peer_challenge = pos;
473 pos += EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
474 os_memset(pos, 0, 8); /* Reserved, must be zero */
476 if (mschapv2_derive_response(identity, identity_len, password,
477 password_len, pwhash, challenge,
478 peer_challenge, pos, data->auth_response,
482 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
486 data->auth_response_valid = 1;
492 wpabuf_put(msg, pos - buf);
495 if (sm->workaround) {
496 /* At least FreeRADIUS seems to be terminating
497 * EAP-TTLS/MSHCAPV2 without the expected MS-CHAP-v2 Success
499 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: EAP workaround - "
500 "allow success without tunneled response");
501 ret->methodState = METHOD_MAY_CONT;
502 ret->decision = DECISION_COND_SUCC;
506 #else /* EAP_MSCHAPv2 */
507 wpa_printf(MSG_ERROR, "EAP-TTLS: MSCHAPv2 not included in the build");
509 #endif /* EAP_MSCHAPv2 */
513 static int eap_ttls_phase2_request_mschap(struct eap_sm *sm,
514 struct eap_ttls_data *data,
515 struct eap_method_ret *ret,
516 struct wpabuf **resp)
519 u8 *buf, *pos, *challenge;
520 const u8 *identity, *password;
521 size_t identity_len, password_len;
524 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAP Request");
526 identity = eap_get_config_identity(sm, &identity_len);
527 password = eap_get_config_password2(sm, &password_len, &pwhash);
528 if (identity == NULL || password == NULL)
531 msg = wpabuf_alloc(identity_len + 1000);
533 wpa_printf(MSG_ERROR,
534 "EAP-TTLS/MSCHAP: Failed to allocate memory");
537 pos = buf = wpabuf_mhead(msg);
540 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
541 identity, identity_len);
543 /* MS-CHAP-Challenge */
544 challenge = eap_ttls_implicit_challenge(
545 sm, data, EAP_TTLS_MSCHAP_CHALLENGE_LEN + 1);
546 if (challenge == NULL) {
548 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAP: Failed to derive "
549 "implicit challenge");
553 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
554 RADIUS_VENDOR_ID_MICROSOFT, 1,
555 challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
557 /* MS-CHAP-Response */
558 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP_RESPONSE,
559 RADIUS_VENDOR_ID_MICROSOFT, 1,
560 EAP_TTLS_MSCHAP_RESPONSE_LEN);
561 data->ident = challenge[EAP_TTLS_MSCHAP_CHALLENGE_LEN];
562 *pos++ = data->ident;
563 *pos++ = 1; /* Flags: Use NT style passwords */
564 os_memset(pos, 0, 24); /* LM-Response */
567 challenge_response(challenge, password, pos); /* NT-Response */
568 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password hash",
571 nt_challenge_response(challenge, password, password_len,
572 pos); /* NT-Response */
573 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password",
574 password, password_len);
576 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP implicit challenge",
577 challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
578 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP response", pos, 24);
583 wpabuf_put(msg, pos - buf);
586 /* EAP-TTLS/MSCHAP does not provide tunneled success
587 * notification, so assume that Phase2 succeeds. */
588 ret->methodState = METHOD_DONE;
589 ret->decision = DECISION_COND_SUCC;
595 static int eap_ttls_phase2_request_pap(struct eap_sm *sm,
596 struct eap_ttls_data *data,
597 struct eap_method_ret *ret,
598 struct wpabuf **resp)
603 const u8 *identity, *password;
604 size_t identity_len, password_len;
606 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 PAP Request");
608 identity = eap_get_config_identity(sm, &identity_len);
609 password = eap_get_config_password(sm, &password_len);
610 if (identity == NULL || password == NULL)
613 msg = wpabuf_alloc(identity_len + password_len + 100);
615 wpa_printf(MSG_ERROR,
616 "EAP-TTLS/PAP: Failed to allocate memory");
619 pos = buf = wpabuf_mhead(msg);
622 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
623 identity, identity_len);
625 /* User-Password; in RADIUS, this is encrypted, but EAP-TTLS encrypts
626 * the data, so no separate encryption is used in the AVP itself.
627 * However, the password is padded to obfuscate its length. */
628 pad = password_len == 0 ? 16 : (16 - (password_len & 15)) & 15;
629 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_USER_PASSWORD, 0, 1,
631 os_memcpy(pos, password, password_len);
633 os_memset(pos, 0, pad);
637 wpabuf_put(msg, pos - buf);
640 /* EAP-TTLS/PAP does not provide tunneled success notification,
641 * so assume that Phase2 succeeds. */
642 ret->methodState = METHOD_DONE;
643 ret->decision = DECISION_COND_SUCC;
649 static int eap_ttls_phase2_request_chap(struct eap_sm *sm,
650 struct eap_ttls_data *data,
651 struct eap_method_ret *ret,
652 struct wpabuf **resp)
655 u8 *buf, *pos, *challenge;
656 const u8 *identity, *password;
657 size_t identity_len, password_len;
659 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 CHAP Request");
661 identity = eap_get_config_identity(sm, &identity_len);
662 password = eap_get_config_password(sm, &password_len);
663 if (identity == NULL || password == NULL)
666 msg = wpabuf_alloc(identity_len + 1000);
668 wpa_printf(MSG_ERROR,
669 "EAP-TTLS/CHAP: Failed to allocate memory");
672 pos = buf = wpabuf_mhead(msg);
675 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
676 identity, identity_len);
679 challenge = eap_ttls_implicit_challenge(
680 sm, data, EAP_TTLS_CHAP_CHALLENGE_LEN + 1);
681 if (challenge == NULL) {
683 wpa_printf(MSG_ERROR, "EAP-TTLS/CHAP: Failed to derive "
684 "implicit challenge");
688 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_CHAP_CHALLENGE, 0, 1,
689 challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
692 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_CHAP_PASSWORD, 0, 1,
693 1 + EAP_TTLS_CHAP_PASSWORD_LEN);
694 data->ident = challenge[EAP_TTLS_CHAP_CHALLENGE_LEN];
695 *pos++ = data->ident;
697 /* MD5(Ident + Password + Challenge) */
698 chap_md5(data->ident, password, password_len, challenge,
699 EAP_TTLS_CHAP_CHALLENGE_LEN, pos);
701 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: CHAP username",
702 identity, identity_len);
703 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: CHAP password",
704 password, password_len);
705 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP implicit challenge",
706 challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
707 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP password",
708 pos, EAP_TTLS_CHAP_PASSWORD_LEN);
709 pos += EAP_TTLS_CHAP_PASSWORD_LEN;
713 wpabuf_put(msg, pos - buf);
716 /* EAP-TTLS/CHAP does not provide tunneled success
717 * notification, so assume that Phase2 succeeds. */
718 ret->methodState = METHOD_DONE;
719 ret->decision = DECISION_COND_SUCC;
725 static int eap_ttls_phase2_request(struct eap_sm *sm,
726 struct eap_ttls_data *data,
727 struct eap_method_ret *ret,
729 struct wpabuf **resp)
733 enum phase2_types phase2_type = data->phase2_type;
736 if (data->tnc_started) {
737 wpa_printf(MSG_DEBUG, "EAP-TTLS: Processing TNC");
738 phase2_type = EAP_TTLS_PHASE2_EAP;
742 if (phase2_type == EAP_TTLS_PHASE2_MSCHAPV2 ||
743 phase2_type == EAP_TTLS_PHASE2_MSCHAP ||
744 phase2_type == EAP_TTLS_PHASE2_PAP ||
745 phase2_type == EAP_TTLS_PHASE2_CHAP) {
746 if (eap_get_config_identity(sm, &len) == NULL) {
748 "EAP-TTLS: Identity not configured");
749 eap_sm_request_identity(sm);
750 if (eap_get_config_password(sm, &len) == NULL)
751 eap_sm_request_password(sm);
755 if (eap_get_config_password(sm, &len) == NULL) {
757 "EAP-TTLS: Password not configured");
758 eap_sm_request_password(sm);
763 switch (phase2_type) {
764 case EAP_TTLS_PHASE2_EAP:
765 res = eap_ttls_phase2_request_eap(sm, data, ret, hdr, resp);
767 case EAP_TTLS_PHASE2_MSCHAPV2:
768 res = eap_ttls_phase2_request_mschapv2(sm, data, ret, resp);
770 case EAP_TTLS_PHASE2_MSCHAP:
771 res = eap_ttls_phase2_request_mschap(sm, data, ret, resp);
773 case EAP_TTLS_PHASE2_PAP:
774 res = eap_ttls_phase2_request_pap(sm, data, ret, resp);
776 case EAP_TTLS_PHASE2_CHAP:
777 res = eap_ttls_phase2_request_chap(sm, data, ret, resp);
780 wpa_printf(MSG_ERROR, "EAP-TTLS: Phase 2 - Unknown");
786 ret->methodState = METHOD_DONE;
787 ret->decision = DECISION_FAIL;
794 struct ttls_parse_avp {
802 static int eap_ttls_parse_attr_eap(const u8 *dpos, size_t dlen,
803 struct ttls_parse_avp *parse)
805 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - EAP Message");
806 if (parse->eapdata == NULL) {
807 parse->eapdata = os_malloc(dlen);
808 if (parse->eapdata == NULL) {
809 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
810 "memory for Phase 2 EAP data");
813 os_memcpy(parse->eapdata, dpos, dlen);
814 parse->eap_len = dlen;
816 u8 *neweap = os_realloc(parse->eapdata, parse->eap_len + dlen);
817 if (neweap == NULL) {
818 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
819 "memory for Phase 2 EAP data");
822 os_memcpy(neweap + parse->eap_len, dpos, dlen);
823 parse->eapdata = neweap;
824 parse->eap_len += dlen;
831 static int eap_ttls_parse_avp(u8 *pos, size_t left,
832 struct ttls_parse_avp *parse)
834 struct ttls_avp *avp;
835 u32 avp_code, avp_length, vendor_id = 0;
839 avp = (struct ttls_avp *) pos;
840 avp_code = be_to_host32(avp->avp_code);
841 avp_length = be_to_host32(avp->avp_length);
842 avp_flags = (avp_length >> 24) & 0xff;
843 avp_length &= 0xffffff;
844 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP: code=%d flags=0x%02x "
845 "length=%d", (int) avp_code, avp_flags,
848 if (avp_length > left) {
849 wpa_printf(MSG_WARNING, "EAP-TTLS: AVP overflow "
850 "(len=%d, left=%lu) - dropped",
851 (int) avp_length, (unsigned long) left);
855 if (avp_length < sizeof(*avp)) {
856 wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid AVP length %d",
861 dpos = (u8 *) (avp + 1);
862 dlen = avp_length - sizeof(*avp);
863 if (avp_flags & AVP_FLAGS_VENDOR) {
865 wpa_printf(MSG_WARNING, "EAP-TTLS: Vendor AVP "
869 vendor_id = WPA_GET_BE32(dpos);
870 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP vendor_id %d",
876 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP data", dpos, dlen);
878 if (vendor_id == 0 && avp_code == RADIUS_ATTR_EAP_MESSAGE) {
879 if (eap_ttls_parse_attr_eap(dpos, dlen, parse) < 0)
881 } else if (vendor_id == 0 && avp_code == RADIUS_ATTR_REPLY_MESSAGE) {
882 /* This is an optional message that can be displayed to
884 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: AVP - Reply-Message",
886 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
887 avp_code == RADIUS_ATTR_MS_CHAP2_SUCCESS) {
888 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP2-Success",
891 wpa_printf(MSG_WARNING, "EAP-TTLS: Unexpected "
892 "MS-CHAP2-Success length "
893 "(len=%lu, expected 43)",
894 (unsigned long) dlen);
897 parse->mschapv2 = dpos;
898 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
899 avp_code == RADIUS_ATTR_MS_CHAP_ERROR) {
900 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP-Error",
902 parse->mschapv2_error = 1;
903 } else if (avp_flags & AVP_FLAGS_MANDATORY) {
904 wpa_printf(MSG_WARNING, "EAP-TTLS: Unsupported mandatory AVP "
905 "code %d vendor_id %d - dropped",
906 (int) avp_code, (int) vendor_id);
909 wpa_printf(MSG_DEBUG, "EAP-TTLS: Ignoring unsupported AVP "
910 "code %d vendor_id %d",
911 (int) avp_code, (int) vendor_id);
918 static int eap_ttls_parse_avps(struct wpabuf *in_decrypted,
919 struct ttls_parse_avp *parse)
925 pos = wpabuf_mhead(in_decrypted);
926 left = wpabuf_len(in_decrypted);
927 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Decrypted Phase 2 AVPs", pos, left);
928 if (left < sizeof(struct ttls_avp)) {
929 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 AVP frame"
930 " len=%lu expected %lu or more - dropped",
931 (unsigned long) left,
932 (unsigned long) sizeof(struct ttls_avp));
937 os_memset(parse, 0, sizeof(*parse));
940 avp_length = eap_ttls_parse_avp(pos, left, parse);
944 pad = (4 - (avp_length & 3)) & 3;
945 pos += avp_length + pad;
946 if (left < avp_length + pad)
949 left -= avp_length + pad;
956 static u8 * eap_ttls_fake_identity_request(void)
961 wpa_printf(MSG_DEBUG, "EAP-TTLS: empty data in beginning of "
962 "Phase 2 - use fake EAP-Request Identity");
963 buf = os_malloc(sizeof(*hdr) + 1);
965 wpa_printf(MSG_WARNING, "EAP-TTLS: failed to allocate "
966 "memory for fake EAP-Identity Request");
970 hdr = (struct eap_hdr *) buf;
971 hdr->code = EAP_CODE_REQUEST;
973 hdr->length = host_to_be16(sizeof(*hdr) + 1);
974 buf[sizeof(*hdr)] = EAP_TYPE_IDENTITY;
980 static int eap_ttls_encrypt_response(struct eap_sm *sm,
981 struct eap_ttls_data *data,
982 struct wpabuf *resp, u8 identifier,
983 struct wpabuf **out_data)
988 wpa_hexdump_buf_key(MSG_DEBUG, "EAP-TTLS: Encrypting Phase 2 data",
990 if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
991 data->ttls_version, identifier,
993 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to encrypt a Phase 2 "
1003 static int eap_ttls_process_phase2_eap(struct eap_sm *sm,
1004 struct eap_ttls_data *data,
1005 struct eap_method_ret *ret,
1006 struct ttls_parse_avp *parse,
1007 struct wpabuf **resp)
1009 struct eap_hdr *hdr;
1012 if (parse->eapdata == NULL) {
1013 wpa_printf(MSG_WARNING, "EAP-TTLS: No EAP Message in the "
1014 "packet - dropped");
1018 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP",
1019 parse->eapdata, parse->eap_len);
1020 hdr = (struct eap_hdr *) parse->eapdata;
1022 if (parse->eap_len < sizeof(*hdr)) {
1023 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 EAP "
1024 "frame (len=%lu, expected %lu or more) - dropped",
1025 (unsigned long) parse->eap_len,
1026 (unsigned long) sizeof(*hdr));
1029 len = be_to_host16(hdr->length);
1030 if (len > parse->eap_len) {
1031 wpa_printf(MSG_INFO, "EAP-TTLS: Length mismatch in Phase 2 "
1032 "EAP frame (EAP hdr len=%lu, EAP data len in "
1034 (unsigned long) len,
1035 (unsigned long) parse->eap_len);
1038 wpa_printf(MSG_DEBUG, "EAP-TTLS: received Phase 2: code=%d "
1039 "identifier=%d length=%lu",
1040 hdr->code, hdr->identifier, (unsigned long) len);
1041 switch (hdr->code) {
1042 case EAP_CODE_REQUEST:
1043 if (eap_ttls_phase2_request(sm, data, ret, hdr, resp)) {
1044 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
1045 "processing failed");
1050 wpa_printf(MSG_INFO, "EAP-TTLS: Unexpected code=%d in "
1051 "Phase 2 EAP header", hdr->code);
1059 static int eap_ttls_process_phase2_mschapv2(struct eap_sm *sm,
1060 struct eap_ttls_data *data,
1061 struct eap_method_ret *ret,
1062 struct ttls_parse_avp *parse)
1065 if (parse->mschapv2_error) {
1066 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Received "
1067 "MS-CHAP-Error - failed");
1068 ret->methodState = METHOD_DONE;
1069 ret->decision = DECISION_FAIL;
1070 /* Reply with empty data to ACK error */
1074 if (parse->mschapv2 == NULL) {
1076 if (data->phase2_success && parse->eapdata) {
1078 * Allow EAP-TNC to be started after successfully
1079 * completed MSCHAPV2.
1083 #endif /* EAP_TNC */
1084 wpa_printf(MSG_WARNING, "EAP-TTLS: no MS-CHAP2-Success AVP "
1085 "received for Phase2 MSCHAPV2");
1088 if (parse->mschapv2[0] != data->ident) {
1089 wpa_printf(MSG_WARNING, "EAP-TTLS: Ident mismatch for Phase 2 "
1090 "MSCHAPV2 (received Ident 0x%02x, expected 0x%02x)",
1091 parse->mschapv2[0], data->ident);
1094 if (!data->auth_response_valid ||
1095 mschapv2_verify_auth_response(data->auth_response,
1096 parse->mschapv2 + 1, 42)) {
1097 wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid authenticator "
1098 "response in Phase 2 MSCHAPV2 success request");
1102 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 MSCHAPV2 "
1103 "authentication succeeded");
1104 ret->methodState = METHOD_DONE;
1105 ret->decision = DECISION_UNCOND_SUCC;
1106 data->phase2_success = 1;
1109 * Reply with empty data; authentication server will reply
1110 * with EAP-Success after this.
1113 #else /* EAP_MSCHAPv2 */
1114 wpa_printf(MSG_ERROR, "EAP-TTLS: MSCHAPv2 not included in the build");
1116 #endif /* EAP_MSCHAPv2 */
1121 static int eap_ttls_process_tnc_start(struct eap_sm *sm,
1122 struct eap_ttls_data *data,
1123 struct eap_method_ret *ret,
1124 struct ttls_parse_avp *parse,
1125 struct wpabuf **resp)
1127 /* TNC uses inner EAP method after non-EAP TTLS phase 2. */
1128 if (parse->eapdata == NULL) {
1129 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
1130 "unexpected tunneled data (no EAP)");
1134 if (!data->ready_for_tnc) {
1135 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
1136 "EAP after non-EAP, but not ready for TNC");
1140 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
1142 data->tnc_started = 1;
1144 if (eap_ttls_process_phase2_eap(sm, data, ret, parse, resp) < 0)
1149 #endif /* EAP_TNC */
1152 static int eap_ttls_process_decrypted(struct eap_sm *sm,
1153 struct eap_ttls_data *data,
1154 struct eap_method_ret *ret,
1156 struct ttls_parse_avp *parse,
1157 struct wpabuf *in_decrypted,
1158 struct wpabuf **out_data)
1160 struct wpabuf *resp = NULL;
1161 struct eap_peer_config *config = eap_get_config(sm);
1163 enum phase2_types phase2_type = data->phase2_type;
1166 if (data->tnc_started)
1167 phase2_type = EAP_TTLS_PHASE2_EAP;
1168 #endif /* EAP_TNC */
1170 switch (phase2_type) {
1171 case EAP_TTLS_PHASE2_EAP:
1172 if (eap_ttls_process_phase2_eap(sm, data, ret, parse, &resp) <
1176 case EAP_TTLS_PHASE2_MSCHAPV2:
1177 res = eap_ttls_process_phase2_mschapv2(sm, data, ret, parse);
1179 if (res == 1 && parse->eapdata && data->phase2_success) {
1181 * TNC may be required as the next
1182 * authentication method within the tunnel.
1184 ret->methodState = METHOD_MAY_CONT;
1185 data->ready_for_tnc = 1;
1186 if (eap_ttls_process_tnc_start(sm, data, ret, parse,
1190 #endif /* EAP_TNC */
1192 case EAP_TTLS_PHASE2_MSCHAP:
1193 case EAP_TTLS_PHASE2_PAP:
1194 case EAP_TTLS_PHASE2_CHAP:
1196 if (eap_ttls_process_tnc_start(sm, data, ret, parse, &resp) <
1201 /* EAP-TTLS/{MSCHAP,PAP,CHAP} should not send any TLS tunneled
1202 * requests to the supplicant */
1203 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received unexpected "
1206 #endif /* EAP_TNC */
1210 if (eap_ttls_encrypt_response(sm, data, resp, identifier,
1213 } else if (config->pending_req_identity ||
1214 config->pending_req_password ||
1215 config->pending_req_otp ||
1216 config->pending_req_new_password) {
1217 wpabuf_free(data->pending_phase2_req);
1218 data->pending_phase2_req = wpabuf_dup(in_decrypted);
1225 static int eap_ttls_implicit_identity_request(struct eap_sm *sm,
1226 struct eap_ttls_data *data,
1227 struct eap_method_ret *ret,
1229 struct wpabuf **out_data)
1232 struct eap_hdr *hdr;
1233 struct wpabuf *resp;
1235 hdr = (struct eap_hdr *) eap_ttls_fake_identity_request();
1237 ret->methodState = METHOD_DONE;
1238 ret->decision = DECISION_FAIL;
1243 if (eap_ttls_phase2_request(sm, data, ret, hdr, &resp)) {
1244 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
1245 "processing failed");
1248 struct eap_peer_config *config = eap_get_config(sm);
1250 (config->pending_req_identity ||
1251 config->pending_req_password ||
1252 config->pending_req_otp ||
1253 config->pending_req_new_password)) {
1255 * Use empty buffer to force implicit request
1256 * processing when EAP request is re-processed after
1259 wpabuf_free(data->pending_phase2_req);
1260 data->pending_phase2_req = wpabuf_alloc(0);
1263 retval = eap_ttls_encrypt_response(sm, data, resp, identifier,
1270 ret->methodState = METHOD_DONE;
1271 ret->decision = DECISION_FAIL;
1278 static int eap_ttls_phase2_start(struct eap_sm *sm, struct eap_ttls_data *data,
1279 struct eap_method_ret *ret, u8 identifier,
1280 struct wpabuf **out_data)
1282 data->phase2_start = 0;
1285 * EAP-TTLS does not use Phase2 on fast re-auth; this must be done only
1286 * if TLS part was indeed resuming a previous session. Most
1287 * Authentication Servers terminate EAP-TTLS before reaching this
1288 * point, but some do not. Make wpa_supplicant stop phase 2 here, if
1292 tls_connection_resumed(sm->ssl_ctx, data->ssl.conn)) {
1293 wpa_printf(MSG_DEBUG, "EAP-TTLS: Session resumption - "
1295 *out_data = eap_peer_tls_build_ack(identifier, EAP_TYPE_TTLS,
1296 data->ttls_version);
1297 ret->methodState = METHOD_DONE;
1298 ret->decision = DECISION_UNCOND_SUCC;
1299 data->phase2_success = 1;
1303 return eap_ttls_implicit_identity_request(sm, data, ret, identifier,
1308 static int eap_ttls_decrypt(struct eap_sm *sm, struct eap_ttls_data *data,
1309 struct eap_method_ret *ret, u8 identifier,
1310 const struct wpabuf *in_data,
1311 struct wpabuf **out_data)
1313 struct wpabuf *in_decrypted = NULL;
1315 struct ttls_parse_avp parse;
1317 os_memset(&parse, 0, sizeof(parse));
1319 wpa_printf(MSG_DEBUG, "EAP-TTLS: received %lu bytes encrypted data for"
1321 in_data ? (unsigned long) wpabuf_len(in_data) : 0);
1323 if (data->pending_phase2_req) {
1324 wpa_printf(MSG_DEBUG, "EAP-TTLS: Pending Phase 2 request - "
1325 "skip decryption and use old data");
1326 /* Clear TLS reassembly state. */
1327 eap_peer_tls_reset_input(&data->ssl);
1329 in_decrypted = data->pending_phase2_req;
1330 data->pending_phase2_req = NULL;
1331 if (wpabuf_len(in_decrypted) == 0) {
1332 wpabuf_free(in_decrypted);
1333 return eap_ttls_implicit_identity_request(
1334 sm, data, ret, identifier, out_data);
1339 if ((in_data == NULL || wpabuf_len(in_data) == 0) &&
1340 data->phase2_start) {
1341 return eap_ttls_phase2_start(sm, data, ret, identifier,
1345 if (in_data == NULL || wpabuf_len(in_data) == 0) {
1346 /* Received TLS ACK - requesting more fragments */
1347 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
1349 identifier, NULL, out_data);
1352 retval = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
1357 data->phase2_start = 0;
1359 if (eap_ttls_parse_avps(in_decrypted, &parse) < 0) {
1364 retval = eap_ttls_process_decrypted(sm, data, ret, identifier,
1365 &parse, in_decrypted, out_data);
1368 wpabuf_free(in_decrypted);
1369 os_free(parse.eapdata);
1372 ret->methodState = METHOD_DONE;
1373 ret->decision = DECISION_FAIL;
1380 static int eap_ttls_process_handshake(struct eap_sm *sm,
1381 struct eap_ttls_data *data,
1382 struct eap_method_ret *ret,
1384 const u8 *in_data, size_t in_len,
1385 struct wpabuf **out_data)
1389 res = eap_peer_tls_process_helper(sm, &data->ssl, EAP_TYPE_TTLS,
1390 data->ttls_version, identifier,
1391 in_data, in_len, out_data);
1393 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1394 wpa_printf(MSG_DEBUG, "EAP-TTLS: TLS done, proceed to "
1396 if (data->resuming) {
1397 wpa_printf(MSG_DEBUG, "EAP-TTLS: fast reauth - may "
1399 ret->decision = DECISION_COND_SUCC;
1400 ret->methodState = METHOD_MAY_CONT;
1402 data->phase2_start = 1;
1403 eap_ttls_v0_derive_key(sm, data);
1405 if (*out_data == NULL || wpabuf_len(*out_data) == 0) {
1406 if (eap_ttls_decrypt(sm, data, ret, identifier,
1408 wpa_printf(MSG_WARNING, "EAP-TTLS: "
1409 "failed to process early "
1410 "start for Phase 2");
1420 * Application data included in the handshake message.
1422 wpabuf_free(data->pending_phase2_req);
1423 data->pending_phase2_req = *out_data;
1425 wpabuf_set(&msg, in_data, in_len);
1426 res = eap_ttls_decrypt(sm, data, ret, identifier, &msg,
1434 static void eap_ttls_check_auth_status(struct eap_sm *sm,
1435 struct eap_ttls_data *data,
1436 struct eap_method_ret *ret)
1438 if (ret->methodState == METHOD_DONE) {
1439 ret->allowNotifications = FALSE;
1440 if (ret->decision == DECISION_UNCOND_SUCC ||
1441 ret->decision == DECISION_COND_SUCC) {
1442 wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1443 "completed successfully");
1444 data->phase2_success = 1;
1446 if (!data->ready_for_tnc && !data->tnc_started) {
1448 * TNC may be required as the next
1449 * authentication method within the tunnel.
1451 ret->methodState = METHOD_MAY_CONT;
1452 data->ready_for_tnc = 1;
1454 #endif /* EAP_TNC */
1456 } else if (ret->methodState == METHOD_MAY_CONT &&
1457 (ret->decision == DECISION_UNCOND_SUCC ||
1458 ret->decision == DECISION_COND_SUCC)) {
1459 wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1460 "completed successfully (MAY_CONT)");
1461 data->phase2_success = 1;
1466 static struct wpabuf * eap_ttls_process(struct eap_sm *sm, void *priv,
1467 struct eap_method_ret *ret,
1468 const struct wpabuf *reqData)
1473 struct wpabuf *resp;
1475 struct eap_ttls_data *data = priv;
1477 pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_TTLS, ret,
1478 reqData, &left, &flags);
1481 id = eap_get_id(reqData);
1483 if (flags & EAP_TLS_FLAGS_START) {
1484 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start (server ver=%d, own "
1485 "ver=%d)", flags & EAP_TLS_VERSION_MASK,
1486 data->ttls_version);
1488 /* RFC 5281, Ch. 9.2:
1489 * "This packet MAY contain additional information in the form
1490 * of AVPs, which may provide useful hints to the client"
1491 * For now, ignore any potential extra data.
1497 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1500 wpabuf_set(&msg, pos, left);
1501 res = eap_ttls_decrypt(sm, data, ret, id, &msg, &resp);
1503 res = eap_ttls_process_handshake(sm, data, ret, id,
1507 eap_ttls_check_auth_status(sm, data, ret);
1509 /* FIX: what about res == -1? Could just move all error processing into
1510 * the other functions and get rid of this res==1 case here. */
1513 return eap_peer_tls_build_ack(id, EAP_TYPE_TTLS,
1514 data->ttls_version);
1520 static Boolean eap_ttls_has_reauth_data(struct eap_sm *sm, void *priv)
1522 struct eap_ttls_data *data = priv;
1523 return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1524 data->phase2_success;
1528 static void eap_ttls_deinit_for_reauth(struct eap_sm *sm, void *priv)
1530 struct eap_ttls_data *data = priv;
1531 wpabuf_free(data->pending_phase2_req);
1532 data->pending_phase2_req = NULL;
1534 data->ready_for_tnc = 0;
1535 data->tnc_started = 0;
1536 #endif /* EAP_TNC */
1540 static void * eap_ttls_init_for_reauth(struct eap_sm *sm, void *priv)
1542 struct eap_ttls_data *data = priv;
1543 os_free(data->key_data);
1544 data->key_data = NULL;
1545 os_free(data->session_id);
1546 data->session_id = NULL;
1547 if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
1551 if (data->phase2_priv && data->phase2_method &&
1552 data->phase2_method->init_for_reauth)
1553 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
1554 data->phase2_start = 0;
1555 data->phase2_success = 0;
1562 static int eap_ttls_get_status(struct eap_sm *sm, void *priv, char *buf,
1563 size_t buflen, int verbose)
1565 struct eap_ttls_data *data = priv;
1568 len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
1569 ret = os_snprintf(buf + len, buflen - len,
1570 "EAP-TTLSv%d Phase2 method=",
1571 data->ttls_version);
1572 if (ret < 0 || (size_t) ret >= buflen - len)
1575 switch (data->phase2_type) {
1576 case EAP_TTLS_PHASE2_EAP:
1577 ret = os_snprintf(buf + len, buflen - len, "EAP-%s\n",
1578 data->phase2_method ?
1579 data->phase2_method->name : "?");
1581 case EAP_TTLS_PHASE2_MSCHAPV2:
1582 ret = os_snprintf(buf + len, buflen - len, "MSCHAPV2\n");
1584 case EAP_TTLS_PHASE2_MSCHAP:
1585 ret = os_snprintf(buf + len, buflen - len, "MSCHAP\n");
1587 case EAP_TTLS_PHASE2_PAP:
1588 ret = os_snprintf(buf + len, buflen - len, "PAP\n");
1590 case EAP_TTLS_PHASE2_CHAP:
1591 ret = os_snprintf(buf + len, buflen - len, "CHAP\n");
1597 if (ret < 0 || (size_t) ret >= buflen - len)
1605 static Boolean eap_ttls_isKeyAvailable(struct eap_sm *sm, void *priv)
1607 struct eap_ttls_data *data = priv;
1608 return data->key_data != NULL && data->phase2_success;
1612 static u8 * eap_ttls_getKey(struct eap_sm *sm, void *priv, size_t *len)
1614 struct eap_ttls_data *data = priv;
1617 if (data->key_data == NULL || !data->phase2_success)
1620 key = os_malloc(EAP_TLS_KEY_LEN);
1624 *len = EAP_TLS_KEY_LEN;
1625 os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
1631 static u8 * eap_ttls_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
1633 struct eap_ttls_data *data = priv;
1636 if (data->session_id == NULL || !data->phase2_success)
1639 id = os_malloc(data->id_len);
1643 *len = data->id_len;
1644 os_memcpy(id, data->session_id, data->id_len);
1650 int eap_peer_ttls_register(void)
1652 struct eap_method *eap;
1655 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1656 EAP_VENDOR_IETF, EAP_TYPE_TTLS, "TTLS");
1660 eap->init = eap_ttls_init;
1661 eap->deinit = eap_ttls_deinit;
1662 eap->process = eap_ttls_process;
1663 eap->isKeyAvailable = eap_ttls_isKeyAvailable;
1664 eap->getKey = eap_ttls_getKey;
1665 eap->getSessionId = eap_ttls_get_session_id;
1666 eap->get_status = eap_ttls_get_status;
1667 eap->has_reauth_data = eap_ttls_has_reauth_data;
1668 eap->deinit_for_reauth = eap_ttls_deinit_for_reauth;
1669 eap->init_for_reauth = eap_ttls_init_for_reauth;
1671 ret = eap_peer_method_register(eap);
1673 eap_peer_method_free(eap);