2 * EAP peer method: EAP-TTLS (RFC 5281)
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
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 data->phase2_priv = data->phase2_method->init(sm);
565 if (data->phase2_priv == NULL || data->phase2_method == NULL) {
566 wpa_printf(MSG_INFO, "EAP-TTLS: failed to initialize "
567 "Phase 2 EAP method %d", method);
571 return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len, resp);
575 static int eap_ttls_phase2_request_eap(struct eap_sm *sm,
576 struct eap_ttls_data *data,
577 struct eap_method_ret *ret,
579 struct wpabuf **resp)
581 size_t len = be_to_host16(hdr->length);
583 struct eap_peer_config *config = eap_get_config(sm);
585 if (len <= sizeof(struct eap_hdr)) {
586 wpa_printf(MSG_INFO, "EAP-TTLS: too short "
587 "Phase 2 request (len=%lu)", (unsigned long) len);
590 pos = (u8 *) (hdr + 1);
591 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP Request: type=%d", *pos);
593 case EAP_TYPE_IDENTITY:
594 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
597 if (eap_ttls_phase2_request_eap_method(sm, data, ret, hdr, len,
604 (config->pending_req_identity || config->pending_req_password ||
605 config->pending_req_otp)) {
612 wpa_hexdump_buf(MSG_DEBUG, "EAP-TTLS: AVP encapsulate EAP Response",
614 return eap_ttls_avp_encapsulate(resp, RADIUS_ATTR_EAP_MESSAGE, 1);
618 static void eap_ttlsv1_permute_inner(struct eap_sm *sm,
619 struct eap_ttls_data *data)
621 #if EAP_TTLS_VERSION > 0
622 u8 session_key[2 * MSCHAPV2_KEY_LEN];
624 if (data->ttls_version == 0)
627 get_asymetric_start_key(data->master_key, session_key,
628 MSCHAPV2_KEY_LEN, 0, 0);
629 get_asymetric_start_key(data->master_key,
630 session_key + MSCHAPV2_KEY_LEN,
631 MSCHAPV2_KEY_LEN, 1, 0);
632 eap_ttls_ia_permute_inner_secret(sm, data, session_key,
633 sizeof(session_key));
634 #endif /* EAP_TTLS_VERSION */
638 static int eap_ttls_phase2_request_mschapv2(struct eap_sm *sm,
639 struct eap_ttls_data *data,
640 struct eap_method_ret *ret,
641 struct wpabuf **resp)
644 u8 *buf, *pos, *challenge, *peer_challenge;
645 const u8 *identity, *password;
646 size_t identity_len, password_len;
649 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAPV2 Request");
651 identity = eap_get_config_identity(sm, &identity_len);
652 password = eap_get_config_password2(sm, &password_len, &pwhash);
653 if (identity == NULL || password == NULL)
656 msg = wpabuf_alloc(identity_len + 1000);
658 wpa_printf(MSG_ERROR,
659 "EAP-TTLS/MSCHAPV2: Failed to allocate memory");
662 pos = buf = wpabuf_mhead(msg);
665 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
666 identity, identity_len);
668 /* MS-CHAP-Challenge */
669 challenge = eap_ttls_implicit_challenge(
670 sm, data, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN + 1);
671 if (challenge == NULL) {
673 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
674 "implicit challenge");
677 peer_challenge = challenge + 1 + EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
679 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
680 RADIUS_VENDOR_ID_MICROSOFT, 1,
681 challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
683 /* MS-CHAP2-Response */
684 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP2_RESPONSE,
685 RADIUS_VENDOR_ID_MICROSOFT, 1,
686 EAP_TTLS_MSCHAPV2_RESPONSE_LEN);
687 data->ident = challenge[EAP_TTLS_MSCHAPV2_CHALLENGE_LEN];
688 *pos++ = data->ident;
689 *pos++ = 0; /* Flags */
690 os_memcpy(pos, peer_challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
691 pos += EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
692 os_memset(pos, 0, 8); /* Reserved, must be zero */
694 if (mschapv2_derive_response(identity, identity_len, password,
695 password_len, pwhash, challenge,
696 peer_challenge, pos, data->auth_response,
699 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
703 data->auth_response_valid = 1;
705 eap_ttlsv1_permute_inner(sm, data);
711 wpabuf_put(msg, pos - buf);
714 if (sm->workaround && data->ttls_version == 0) {
715 /* At least FreeRADIUS seems to be terminating
716 * EAP-TTLS/MSHCAPV2 without the expected MS-CHAP-v2 Success
718 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: EAP workaround - "
719 "allow success without tunneled response");
720 ret->methodState = METHOD_MAY_CONT;
721 ret->decision = DECISION_COND_SUCC;
728 static int eap_ttls_phase2_request_mschap(struct eap_sm *sm,
729 struct eap_ttls_data *data,
730 struct eap_method_ret *ret,
731 struct wpabuf **resp)
734 u8 *buf, *pos, *challenge;
735 const u8 *identity, *password;
736 size_t identity_len, password_len;
739 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAP Request");
741 identity = eap_get_config_identity(sm, &identity_len);
742 password = eap_get_config_password2(sm, &password_len, &pwhash);
743 if (identity == NULL || password == NULL)
746 msg = wpabuf_alloc(identity_len + 1000);
748 wpa_printf(MSG_ERROR,
749 "EAP-TTLS/MSCHAP: Failed to allocate memory");
752 pos = buf = wpabuf_mhead(msg);
755 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
756 identity, identity_len);
758 /* MS-CHAP-Challenge */
759 challenge = eap_ttls_implicit_challenge(
760 sm, data, EAP_TTLS_MSCHAP_CHALLENGE_LEN + 1);
761 if (challenge == NULL) {
763 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAP: Failed to derive "
764 "implicit challenge");
768 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
769 RADIUS_VENDOR_ID_MICROSOFT, 1,
770 challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
772 /* MS-CHAP-Response */
773 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP_RESPONSE,
774 RADIUS_VENDOR_ID_MICROSOFT, 1,
775 EAP_TTLS_MSCHAP_RESPONSE_LEN);
776 data->ident = challenge[EAP_TTLS_MSCHAP_CHALLENGE_LEN];
777 *pos++ = data->ident;
778 *pos++ = 1; /* Flags: Use NT style passwords */
779 os_memset(pos, 0, 24); /* LM-Response */
782 challenge_response(challenge, password, pos); /* NT-Response */
783 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password hash",
786 nt_challenge_response(challenge, password, password_len,
787 pos); /* NT-Response */
788 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password",
789 password, password_len);
791 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP implicit challenge",
792 challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
793 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP response", pos, 24);
798 wpabuf_put(msg, pos - buf);
801 if (data->ttls_version > 0) {
802 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
803 * so do not allow connection to be terminated yet. */
804 ret->methodState = METHOD_CONT;
805 ret->decision = DECISION_COND_SUCC;
807 /* EAP-TTLS/MSCHAP does not provide tunneled success
808 * notification, so assume that Phase2 succeeds. */
809 ret->methodState = METHOD_DONE;
810 ret->decision = DECISION_COND_SUCC;
817 static int eap_ttls_phase2_request_pap(struct eap_sm *sm,
818 struct eap_ttls_data *data,
819 struct eap_method_ret *ret,
820 struct wpabuf **resp)
825 const u8 *identity, *password;
826 size_t identity_len, password_len;
828 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 PAP Request");
830 identity = eap_get_config_identity(sm, &identity_len);
831 password = eap_get_config_password(sm, &password_len);
832 if (identity == NULL || password == NULL)
835 msg = wpabuf_alloc(identity_len + password_len + 100);
837 wpa_printf(MSG_ERROR,
838 "EAP-TTLS/PAP: Failed to allocate memory");
841 pos = buf = wpabuf_mhead(msg);
844 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
845 identity, identity_len);
847 /* User-Password; in RADIUS, this is encrypted, but EAP-TTLS encrypts
848 * the data, so no separate encryption is used in the AVP itself.
849 * However, the password is padded to obfuscate its length. */
850 pad = (16 - (password_len & 15)) & 15;
851 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_USER_PASSWORD, 0, 1,
853 os_memcpy(pos, password, password_len);
855 os_memset(pos, 0, pad);
859 wpabuf_put(msg, pos - buf);
862 if (data->ttls_version > 0) {
863 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
864 * so do not allow connection to be terminated yet. */
865 ret->methodState = METHOD_CONT;
866 ret->decision = DECISION_COND_SUCC;
868 /* EAP-TTLS/PAP does not provide tunneled success notification,
869 * so assume that Phase2 succeeds. */
870 ret->methodState = METHOD_DONE;
871 ret->decision = DECISION_COND_SUCC;
878 static int eap_ttls_phase2_request_chap(struct eap_sm *sm,
879 struct eap_ttls_data *data,
880 struct eap_method_ret *ret,
881 struct wpabuf **resp)
884 u8 *buf, *pos, *challenge;
885 const u8 *identity, *password;
886 size_t identity_len, password_len;
888 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 CHAP Request");
890 identity = eap_get_config_identity(sm, &identity_len);
891 password = eap_get_config_password(sm, &password_len);
892 if (identity == NULL || password == NULL)
895 msg = wpabuf_alloc(identity_len + 1000);
897 wpa_printf(MSG_ERROR,
898 "EAP-TTLS/CHAP: Failed to allocate memory");
901 pos = buf = wpabuf_mhead(msg);
904 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
905 identity, identity_len);
908 challenge = eap_ttls_implicit_challenge(
909 sm, data, EAP_TTLS_CHAP_CHALLENGE_LEN + 1);
910 if (challenge == NULL) {
912 wpa_printf(MSG_ERROR, "EAP-TTLS/CHAP: Failed to derive "
913 "implicit challenge");
917 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_CHAP_CHALLENGE, 0, 1,
918 challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
921 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_CHAP_PASSWORD, 0, 1,
922 1 + EAP_TTLS_CHAP_PASSWORD_LEN);
923 data->ident = challenge[EAP_TTLS_CHAP_CHALLENGE_LEN];
924 *pos++ = data->ident;
926 /* MD5(Ident + Password + Challenge) */
927 chap_md5(data->ident, password, password_len, challenge,
928 EAP_TTLS_CHAP_CHALLENGE_LEN, pos);
930 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: CHAP username",
931 identity, identity_len);
932 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: CHAP password",
933 password, password_len);
934 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP implicit challenge",
935 challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
936 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP password",
937 pos, EAP_TTLS_CHAP_PASSWORD_LEN);
938 pos += EAP_TTLS_CHAP_PASSWORD_LEN;
942 wpabuf_put(msg, pos - buf);
945 if (data->ttls_version > 0) {
946 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
947 * so do not allow connection to be terminated yet. */
948 ret->methodState = METHOD_CONT;
949 ret->decision = DECISION_COND_SUCC;
951 /* EAP-TTLS/CHAP does not provide tunneled success
952 * notification, so assume that Phase2 succeeds. */
953 ret->methodState = METHOD_DONE;
954 ret->decision = DECISION_COND_SUCC;
961 static int eap_ttls_phase2_request(struct eap_sm *sm,
962 struct eap_ttls_data *data,
963 struct eap_method_ret *ret,
965 struct wpabuf **resp)
969 enum phase2_types phase2_type = data->phase2_type;
972 if (data->tnc_started) {
973 wpa_printf(MSG_DEBUG, "EAP-TTLS: Processing TNC");
974 phase2_type = EAP_TTLS_PHASE2_EAP;
978 if (phase2_type == EAP_TTLS_PHASE2_MSCHAPV2 ||
979 phase2_type == EAP_TTLS_PHASE2_MSCHAP ||
980 phase2_type == EAP_TTLS_PHASE2_PAP ||
981 phase2_type == EAP_TTLS_PHASE2_CHAP) {
982 if (eap_get_config_identity(sm, &len) == NULL) {
984 "EAP-TTLS: Identity not configured");
985 eap_sm_request_identity(sm);
986 if (eap_get_config_password(sm, &len) == NULL)
987 eap_sm_request_password(sm);
991 if (eap_get_config_password(sm, &len) == NULL) {
993 "EAP-TTLS: Password not configured");
994 eap_sm_request_password(sm);
999 switch (phase2_type) {
1000 case EAP_TTLS_PHASE2_EAP:
1001 res = eap_ttls_phase2_request_eap(sm, data, ret, hdr, resp);
1003 case EAP_TTLS_PHASE2_MSCHAPV2:
1004 res = eap_ttls_phase2_request_mschapv2(sm, data, ret, resp);
1006 case EAP_TTLS_PHASE2_MSCHAP:
1007 res = eap_ttls_phase2_request_mschap(sm, data, ret, resp);
1009 case EAP_TTLS_PHASE2_PAP:
1010 res = eap_ttls_phase2_request_pap(sm, data, ret, resp);
1012 case EAP_TTLS_PHASE2_CHAP:
1013 res = eap_ttls_phase2_request_chap(sm, data, ret, resp);
1016 wpa_printf(MSG_ERROR, "EAP-TTLS: Phase 2 - Unknown");
1022 ret->methodState = METHOD_DONE;
1023 ret->decision = DECISION_FAIL;
1030 #if EAP_TTLS_VERSION > 0
1031 static struct wpabuf * eap_ttls_build_phase_finished(
1032 struct eap_sm *sm, struct eap_ttls_data *data, int id, int final)
1037 const int max_len = 300;
1039 req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TTLS, 1 + max_len,
1040 EAP_CODE_RESPONSE, id);
1044 wpabuf_put_u8(req, data->ttls_version);
1046 pos = wpabuf_put(req, 0);
1047 len = tls_connection_ia_send_phase_finished(sm->ssl_ctx,
1049 final, pos, max_len);
1054 wpabuf_put(req, len);
1055 eap_update_len(req);
1059 #endif /* EAP_TTLS_VERSION */
1062 struct ttls_parse_avp {
1070 static int eap_ttls_parse_attr_eap(const u8 *dpos, size_t dlen,
1071 struct ttls_parse_avp *parse)
1073 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - EAP Message");
1074 if (parse->eapdata == NULL) {
1075 parse->eapdata = os_malloc(dlen);
1076 if (parse->eapdata == NULL) {
1077 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
1078 "memory for Phase 2 EAP data");
1081 os_memcpy(parse->eapdata, dpos, dlen);
1082 parse->eap_len = dlen;
1084 u8 *neweap = os_realloc(parse->eapdata, parse->eap_len + dlen);
1085 if (neweap == NULL) {
1086 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
1087 "memory for Phase 2 EAP data");
1090 os_memcpy(neweap + parse->eap_len, dpos, dlen);
1091 parse->eapdata = neweap;
1092 parse->eap_len += dlen;
1099 static int eap_ttls_parse_avp(u8 *pos, size_t left,
1100 struct ttls_parse_avp *parse)
1102 struct ttls_avp *avp;
1103 u32 avp_code, avp_length, vendor_id = 0;
1104 u8 avp_flags, *dpos;
1107 avp = (struct ttls_avp *) pos;
1108 avp_code = be_to_host32(avp->avp_code);
1109 avp_length = be_to_host32(avp->avp_length);
1110 avp_flags = (avp_length >> 24) & 0xff;
1111 avp_length &= 0xffffff;
1112 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP: code=%d flags=0x%02x "
1113 "length=%d", (int) avp_code, avp_flags,
1116 if (avp_length > left) {
1117 wpa_printf(MSG_WARNING, "EAP-TTLS: AVP overflow "
1118 "(len=%d, left=%lu) - dropped",
1119 (int) avp_length, (unsigned long) left);
1123 if (avp_length < sizeof(*avp)) {
1124 wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid AVP length %d",
1129 dpos = (u8 *) (avp + 1);
1130 dlen = avp_length - sizeof(*avp);
1131 if (avp_flags & AVP_FLAGS_VENDOR) {
1133 wpa_printf(MSG_WARNING, "EAP-TTLS: Vendor AVP "
1137 vendor_id = WPA_GET_BE32(dpos);
1138 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP vendor_id %d",
1144 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP data", dpos, dlen);
1146 if (vendor_id == 0 && avp_code == RADIUS_ATTR_EAP_MESSAGE) {
1147 if (eap_ttls_parse_attr_eap(dpos, dlen, parse) < 0)
1149 } else if (vendor_id == 0 && avp_code == RADIUS_ATTR_REPLY_MESSAGE) {
1150 /* This is an optional message that can be displayed to
1152 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: AVP - Reply-Message",
1154 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
1155 avp_code == RADIUS_ATTR_MS_CHAP2_SUCCESS) {
1156 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP2-Success",
1159 wpa_printf(MSG_WARNING, "EAP-TTLS: Unexpected "
1160 "MS-CHAP2-Success length "
1161 "(len=%lu, expected 43)",
1162 (unsigned long) dlen);
1165 parse->mschapv2 = dpos;
1166 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
1167 avp_code == RADIUS_ATTR_MS_CHAP_ERROR) {
1168 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP-Error",
1170 parse->mschapv2_error = 1;
1171 } else if (avp_flags & AVP_FLAGS_MANDATORY) {
1172 wpa_printf(MSG_WARNING, "EAP-TTLS: Unsupported mandatory AVP "
1173 "code %d vendor_id %d - dropped",
1174 (int) avp_code, (int) vendor_id);
1177 wpa_printf(MSG_DEBUG, "EAP-TTLS: Ignoring unsupported AVP "
1178 "code %d vendor_id %d",
1179 (int) avp_code, (int) vendor_id);
1186 static int eap_ttls_parse_avps(struct wpabuf *in_decrypted,
1187 struct ttls_parse_avp *parse)
1193 pos = wpabuf_mhead(in_decrypted);
1194 left = wpabuf_len(in_decrypted);
1195 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Decrypted Phase 2 AVPs", pos, left);
1196 if (left < sizeof(struct ttls_avp)) {
1197 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 AVP frame"
1198 " len=%lu expected %lu or more - dropped",
1199 (unsigned long) left,
1200 (unsigned long) sizeof(struct ttls_avp));
1205 os_memset(parse, 0, sizeof(*parse));
1208 avp_length = eap_ttls_parse_avp(pos, left, parse);
1212 pad = (4 - (avp_length & 3)) & 3;
1213 pos += avp_length + pad;
1214 if (left < avp_length + pad)
1217 left -= avp_length + pad;
1224 static u8 * eap_ttls_fake_identity_request(void)
1226 struct eap_hdr *hdr;
1229 wpa_printf(MSG_DEBUG, "EAP-TTLS: empty data in beginning of "
1230 "Phase 2 - use fake EAP-Request Identity");
1231 buf = os_malloc(sizeof(*hdr) + 1);
1233 wpa_printf(MSG_WARNING, "EAP-TTLS: failed to allocate "
1234 "memory for fake EAP-Identity Request");
1238 hdr = (struct eap_hdr *) buf;
1239 hdr->code = EAP_CODE_REQUEST;
1240 hdr->identifier = 0;
1241 hdr->length = host_to_be16(sizeof(*hdr) + 1);
1242 buf[sizeof(*hdr)] = EAP_TYPE_IDENTITY;
1248 static int eap_ttls_encrypt_response(struct eap_sm *sm,
1249 struct eap_ttls_data *data,
1250 struct wpabuf *resp, u8 identifier,
1251 struct wpabuf **out_data)
1256 wpa_hexdump_buf_key(MSG_DEBUG, "EAP-TTLS: Encrypting Phase 2 data",
1258 if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
1259 data->ttls_version, identifier,
1261 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to encrypt a Phase 2 "
1271 static int eap_ttls_process_phase2_eap(struct eap_sm *sm,
1272 struct eap_ttls_data *data,
1273 struct eap_method_ret *ret,
1274 struct ttls_parse_avp *parse,
1275 struct wpabuf **resp)
1277 struct eap_hdr *hdr;
1280 if (parse->eapdata == NULL) {
1281 wpa_printf(MSG_WARNING, "EAP-TTLS: No EAP Message in the "
1282 "packet - dropped");
1286 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP",
1287 parse->eapdata, parse->eap_len);
1288 hdr = (struct eap_hdr *) parse->eapdata;
1290 if (parse->eap_len < sizeof(*hdr)) {
1291 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 EAP "
1292 "frame (len=%lu, expected %lu or more) - dropped",
1293 (unsigned long) parse->eap_len,
1294 (unsigned long) sizeof(*hdr));
1297 len = be_to_host16(hdr->length);
1298 if (len > parse->eap_len) {
1299 wpa_printf(MSG_INFO, "EAP-TTLS: Length mismatch in Phase 2 "
1300 "EAP frame (EAP hdr len=%lu, EAP data len in "
1302 (unsigned long) len,
1303 (unsigned long) parse->eap_len);
1306 wpa_printf(MSG_DEBUG, "EAP-TTLS: received Phase 2: code=%d "
1307 "identifier=%d length=%lu",
1308 hdr->code, hdr->identifier, (unsigned long) len);
1309 switch (hdr->code) {
1310 case EAP_CODE_REQUEST:
1311 if (eap_ttls_phase2_request(sm, data, ret, hdr, resp)) {
1312 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
1313 "processing failed");
1318 wpa_printf(MSG_INFO, "EAP-TTLS: Unexpected code=%d in "
1319 "Phase 2 EAP header", hdr->code);
1327 static int eap_ttls_process_phase2_mschapv2(struct eap_sm *sm,
1328 struct eap_ttls_data *data,
1329 struct eap_method_ret *ret,
1330 struct ttls_parse_avp *parse)
1332 if (parse->mschapv2_error) {
1333 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Received "
1334 "MS-CHAP-Error - failed");
1335 ret->methodState = METHOD_DONE;
1336 ret->decision = DECISION_FAIL;
1337 /* Reply with empty data to ACK error */
1341 if (parse->mschapv2 == NULL) {
1343 if (data->phase2_success && parse->eapdata) {
1345 * Allow EAP-TNC to be started after successfully
1346 * completed MSCHAPV2.
1350 #endif /* EAP_TNC */
1351 wpa_printf(MSG_WARNING, "EAP-TTLS: no MS-CHAP2-Success AVP "
1352 "received for Phase2 MSCHAPV2");
1355 if (parse->mschapv2[0] != data->ident) {
1356 wpa_printf(MSG_WARNING, "EAP-TTLS: Ident mismatch for Phase 2 "
1357 "MSCHAPV2 (received Ident 0x%02x, expected 0x%02x)",
1358 parse->mschapv2[0], data->ident);
1361 if (!data->auth_response_valid ||
1362 mschapv2_verify_auth_response(data->auth_response,
1363 parse->mschapv2 + 1, 42)) {
1364 wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid authenticator "
1365 "response in Phase 2 MSCHAPV2 success request");
1369 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 MSCHAPV2 "
1370 "authentication succeeded");
1371 if (data->ttls_version > 0) {
1373 * EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report
1374 * success, so do not allow connection to be terminated
1377 ret->methodState = METHOD_CONT;
1378 ret->decision = DECISION_COND_SUCC;
1380 ret->methodState = METHOD_DONE;
1381 ret->decision = DECISION_UNCOND_SUCC;
1382 data->phase2_success = 1;
1386 * Reply with empty data; authentication server will reply
1387 * with EAP-Success after this.
1394 static int eap_ttls_process_tnc_start(struct eap_sm *sm,
1395 struct eap_ttls_data *data,
1396 struct eap_method_ret *ret,
1397 struct ttls_parse_avp *parse,
1398 struct wpabuf **resp)
1400 /* TNC uses inner EAP method after non-EAP TTLS phase 2. */
1401 if (parse->eapdata == NULL) {
1402 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
1403 "unexpected tunneled data (no EAP)");
1407 if (!data->ready_for_tnc) {
1408 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
1409 "EAP after non-EAP, but not ready for TNC");
1413 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
1415 data->tnc_started = 1;
1417 if (eap_ttls_process_phase2_eap(sm, data, ret, parse, resp) < 0)
1422 #endif /* EAP_TNC */
1425 static int eap_ttls_process_decrypted(struct eap_sm *sm,
1426 struct eap_ttls_data *data,
1427 struct eap_method_ret *ret,
1429 struct ttls_parse_avp *parse,
1430 struct wpabuf *in_decrypted,
1431 struct wpabuf **out_data)
1433 struct wpabuf *resp = NULL;
1434 struct eap_peer_config *config = eap_get_config(sm);
1436 enum phase2_types phase2_type = data->phase2_type;
1439 if (data->tnc_started)
1440 phase2_type = EAP_TTLS_PHASE2_EAP;
1441 #endif /* EAP_TNC */
1443 switch (phase2_type) {
1444 case EAP_TTLS_PHASE2_EAP:
1445 if (eap_ttls_process_phase2_eap(sm, data, ret, parse, &resp) <
1449 case EAP_TTLS_PHASE2_MSCHAPV2:
1450 res = eap_ttls_process_phase2_mschapv2(sm, data, ret, parse);
1452 if (res == 1 && parse->eapdata && data->phase2_success) {
1454 * TNC may be required as the next
1455 * authentication method within the tunnel.
1457 ret->methodState = METHOD_MAY_CONT;
1458 data->ready_for_tnc = 1;
1459 if (eap_ttls_process_tnc_start(sm, data, ret, parse,
1463 #endif /* EAP_TNC */
1465 case EAP_TTLS_PHASE2_MSCHAP:
1466 case EAP_TTLS_PHASE2_PAP:
1467 case EAP_TTLS_PHASE2_CHAP:
1469 if (eap_ttls_process_tnc_start(sm, data, ret, parse, &resp) <
1474 /* EAP-TTLS/{MSCHAP,PAP,CHAP} should not send any TLS tunneled
1475 * requests to the supplicant */
1476 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received unexpected "
1479 #endif /* EAP_TNC */
1483 if (eap_ttls_encrypt_response(sm, data, resp, identifier,
1486 } else if (config->pending_req_identity ||
1487 config->pending_req_password ||
1488 config->pending_req_otp ||
1489 config->pending_req_new_password) {
1490 wpabuf_free(data->pending_phase2_req);
1491 data->pending_phase2_req = wpabuf_dup(in_decrypted);
1498 #if EAP_TTLS_VERSION > 0
1499 static void eap_ttls_final_phase_finished(struct eap_sm *sm,
1500 struct eap_ttls_data *data,
1501 struct eap_method_ret *ret,
1503 struct wpabuf **out_data)
1505 wpa_printf(MSG_DEBUG, "EAP-TTLS: FinalPhaseFinished received");
1506 wpa_printf(MSG_INFO, "EAP-TTLS: TLS/IA authentication succeeded");
1507 ret->methodState = METHOD_DONE;
1508 ret->decision = DECISION_UNCOND_SUCC;
1509 data->phase2_success = 1;
1510 *out_data = eap_ttls_build_phase_finished(sm, data, identifier, 1);
1511 eap_ttls_v1_derive_key(sm, data);
1513 #endif /* EAP_TTLS_VERSION */
1516 static int eap_ttls_implicit_identity_request(struct eap_sm *sm,
1517 struct eap_ttls_data *data,
1518 struct eap_method_ret *ret,
1520 struct wpabuf **out_data)
1523 struct eap_hdr *hdr;
1524 struct wpabuf *resp;
1526 hdr = (struct eap_hdr *) eap_ttls_fake_identity_request();
1528 ret->methodState = METHOD_DONE;
1529 ret->decision = DECISION_FAIL;
1534 if (eap_ttls_phase2_request(sm, data, ret, hdr, &resp)) {
1535 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
1536 "processing failed");
1539 retval = eap_ttls_encrypt_response(sm, data, resp, identifier,
1546 ret->methodState = METHOD_DONE;
1547 ret->decision = DECISION_FAIL;
1554 static int eap_ttls_phase2_start(struct eap_sm *sm, struct eap_ttls_data *data,
1555 struct eap_method_ret *ret, u8 identifier,
1556 struct wpabuf **out_data)
1558 data->phase2_start = 0;
1561 * EAP-TTLS does not use Phase2 on fast re-auth; this must be done only
1562 * if TLS part was indeed resuming a previous session. Most
1563 * Authentication Servers terminate EAP-TTLS before reaching this
1564 * point, but some do not. Make wpa_supplicant stop phase 2 here, if
1568 tls_connection_resumed(sm->ssl_ctx, data->ssl.conn)) {
1569 wpa_printf(MSG_DEBUG, "EAP-TTLS: Session resumption - "
1571 *out_data = eap_peer_tls_build_ack(identifier, EAP_TYPE_TTLS,
1572 data->ttls_version);
1573 ret->methodState = METHOD_DONE;
1574 ret->decision = DECISION_UNCOND_SUCC;
1575 data->phase2_success = 1;
1579 return eap_ttls_implicit_identity_request(sm, data, ret, identifier,
1584 static int eap_ttls_decrypt(struct eap_sm *sm, struct eap_ttls_data *data,
1585 struct eap_method_ret *ret, u8 identifier,
1586 const struct wpabuf *in_data,
1587 struct wpabuf **out_data)
1589 struct wpabuf *in_decrypted = NULL;
1591 struct ttls_parse_avp parse;
1593 os_memset(&parse, 0, sizeof(parse));
1595 wpa_printf(MSG_DEBUG, "EAP-TTLS: received %lu bytes encrypted data for"
1597 in_data ? (unsigned long) wpabuf_len(in_data) : 0);
1599 if (data->pending_phase2_req) {
1600 wpa_printf(MSG_DEBUG, "EAP-TTLS: Pending Phase 2 request - "
1601 "skip decryption and use old data");
1602 /* Clear TLS reassembly state. */
1603 eap_peer_tls_reset_input(&data->ssl);
1605 in_decrypted = data->pending_phase2_req;
1606 data->pending_phase2_req = NULL;
1607 if (wpabuf_len(in_decrypted) == 0) {
1608 wpabuf_free(in_decrypted);
1609 return eap_ttls_implicit_identity_request(
1610 sm, data, ret, identifier, out_data);
1615 if ((in_data == NULL || wpabuf_len(in_data) == 0) &&
1616 data->phase2_start) {
1617 return eap_ttls_phase2_start(sm, data, ret, identifier,
1621 if (in_data == NULL || wpabuf_len(in_data) == 0) {
1622 /* Received TLS ACK - requesting more fragments */
1623 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
1625 identifier, NULL, out_data);
1628 retval = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
1632 #if EAP_TTLS_VERSION > 0
1633 if (data->ttls_version > 0 &&
1634 (in_decrypted == NULL || wpabuf_len(in_decrypted) == 0) &&
1635 tls_connection_ia_final_phase_finished(sm->ssl_ctx,
1637 eap_ttls_final_phase_finished(sm, data, ret, identifier,
1641 #endif /* EAP_TTLS_VERSION */
1644 data->phase2_start = 0;
1646 if (eap_ttls_parse_avps(in_decrypted, &parse) < 0) {
1651 retval = eap_ttls_process_decrypted(sm, data, ret, identifier,
1652 &parse, in_decrypted, out_data);
1655 wpabuf_free(in_decrypted);
1656 os_free(parse.eapdata);
1659 ret->methodState = METHOD_DONE;
1660 ret->decision = DECISION_FAIL;
1667 static int eap_ttls_process_start(struct eap_sm *sm,
1668 struct eap_ttls_data *data, u8 flags,
1669 struct eap_method_ret *ret)
1671 struct eap_peer_config *config = eap_get_config(sm);
1673 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start (server ver=%d, own ver=%d)",
1674 flags & EAP_PEAP_VERSION_MASK, data->ttls_version);
1675 #if EAP_TTLS_VERSION > 0
1676 if ((flags & EAP_PEAP_VERSION_MASK) < data->ttls_version)
1677 data->ttls_version = flags & EAP_PEAP_VERSION_MASK;
1678 if (data->force_ttls_version >= 0 &&
1679 data->force_ttls_version != data->ttls_version) {
1680 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to select "
1681 "forced TTLS version %d",
1682 data->force_ttls_version);
1683 ret->methodState = METHOD_DONE;
1684 ret->decision = DECISION_FAIL;
1685 ret->allowNotifications = FALSE;
1688 wpa_printf(MSG_DEBUG, "EAP-TTLS: Using TTLS version %d",
1689 data->ttls_version);
1691 if (data->ttls_version > 0)
1692 data->ssl.tls_ia = 1;
1693 #endif /* EAP_TTLS_VERSION */
1694 if (!data->ssl_initialized &&
1695 eap_peer_tls_ssl_init(sm, &data->ssl, config)) {
1696 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to initialize SSL.");
1699 data->ssl_initialized = 1;
1701 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start");
1707 static int eap_ttls_process_handshake(struct eap_sm *sm,
1708 struct eap_ttls_data *data,
1709 struct eap_method_ret *ret,
1711 const u8 *in_data, size_t in_len,
1712 struct wpabuf **out_data)
1716 res = eap_peer_tls_process_helper(sm, &data->ssl, EAP_TYPE_TTLS,
1717 data->ttls_version, identifier,
1718 in_data, in_len, out_data);
1720 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1721 wpa_printf(MSG_DEBUG, "EAP-TTLS: TLS done, proceed to "
1723 if (data->resuming) {
1724 wpa_printf(MSG_DEBUG, "EAP-TTLS: fast reauth - may "
1726 ret->decision = DECISION_COND_SUCC;
1727 ret->methodState = METHOD_MAY_CONT;
1729 data->phase2_start = 1;
1730 if (data->ttls_version == 0)
1731 eap_ttls_v0_derive_key(sm, data);
1733 if (*out_data == NULL || wpabuf_len(*out_data) == 0) {
1734 if (eap_ttls_decrypt(sm, data, ret, identifier,
1736 wpa_printf(MSG_WARNING, "EAP-TTLS: "
1737 "failed to process early "
1738 "start for Phase 2");
1748 * Application data included in the handshake message.
1750 wpabuf_free(data->pending_phase2_req);
1751 data->pending_phase2_req = *out_data;
1753 wpabuf_set(&msg, in_data, in_len);
1754 res = eap_ttls_decrypt(sm, data, ret, identifier, &msg,
1762 static void eap_ttls_check_auth_status(struct eap_sm *sm,
1763 struct eap_ttls_data *data,
1764 struct eap_method_ret *ret)
1766 if (data->ttls_version == 0 && ret->methodState == METHOD_DONE) {
1767 ret->allowNotifications = FALSE;
1768 if (ret->decision == DECISION_UNCOND_SUCC ||
1769 ret->decision == DECISION_COND_SUCC) {
1770 wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1771 "completed successfully");
1772 data->phase2_success = 1;
1774 if (!data->ready_for_tnc && !data->tnc_started) {
1776 * TNC may be required as the next
1777 * authentication method within the tunnel.
1779 ret->methodState = METHOD_MAY_CONT;
1780 data->ready_for_tnc = 1;
1782 #endif /* EAP_TNC */
1784 } else if (data->ttls_version == 0 &&
1785 ret->methodState == METHOD_MAY_CONT &&
1786 (ret->decision == DECISION_UNCOND_SUCC ||
1787 ret->decision == DECISION_COND_SUCC)) {
1788 wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1789 "completed successfully (MAY_CONT)");
1790 data->phase2_success = 1;
1795 static struct wpabuf * eap_ttls_process(struct eap_sm *sm, void *priv,
1796 struct eap_method_ret *ret,
1797 const struct wpabuf *reqData)
1802 struct wpabuf *resp;
1804 struct eap_ttls_data *data = priv;
1806 pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_TTLS, ret,
1807 reqData, &left, &flags);
1810 id = eap_get_id(reqData);
1812 if (flags & EAP_TLS_FLAGS_START) {
1813 if (eap_ttls_process_start(sm, data, flags, ret) < 0)
1816 /* RFC 5281, Ch. 9.2:
1817 * "This packet MAY contain additional information in the form
1818 * of AVPs, which may provide useful hints to the client"
1819 * For now, ignore any potential extra data.
1822 } else if (!data->ssl_initialized) {
1823 wpa_printf(MSG_DEBUG, "EAP-TTLS: First message did not "
1824 "include Start flag");
1825 ret->methodState = METHOD_DONE;
1826 ret->decision = DECISION_FAIL;
1827 ret->allowNotifications = FALSE;
1832 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1835 wpabuf_set(&msg, pos, left);
1836 res = eap_ttls_decrypt(sm, data, ret, id, &msg, &resp);
1838 res = eap_ttls_process_handshake(sm, data, ret, id,
1842 eap_ttls_check_auth_status(sm, data, ret);
1844 /* FIX: what about res == -1? Could just move all error processing into
1845 * the other functions and get rid of this res==1 case here. */
1848 return eap_peer_tls_build_ack(id, EAP_TYPE_TTLS,
1849 data->ttls_version);
1855 static Boolean eap_ttls_has_reauth_data(struct eap_sm *sm, void *priv)
1857 struct eap_ttls_data *data = priv;
1858 return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1859 data->phase2_success;
1863 static void eap_ttls_deinit_for_reauth(struct eap_sm *sm, void *priv)
1865 struct eap_ttls_data *data = priv;
1866 wpabuf_free(data->pending_phase2_req);
1867 data->pending_phase2_req = NULL;
1869 data->ready_for_tnc = 0;
1870 data->tnc_started = 0;
1871 #endif /* EAP_TNC */
1875 static void * eap_ttls_init_for_reauth(struct eap_sm *sm, void *priv)
1877 struct eap_ttls_data *data = priv;
1878 os_free(data->key_data);
1879 data->key_data = NULL;
1880 if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
1884 if (data->phase2_priv && data->phase2_method &&
1885 data->phase2_method->init_for_reauth)
1886 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
1887 data->phase2_start = 0;
1888 data->phase2_success = 0;
1895 static int eap_ttls_get_status(struct eap_sm *sm, void *priv, char *buf,
1896 size_t buflen, int verbose)
1898 struct eap_ttls_data *data = priv;
1901 len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
1902 ret = os_snprintf(buf + len, buflen - len,
1903 "EAP-TTLSv%d Phase2 method=",
1904 data->ttls_version);
1905 if (ret < 0 || (size_t) ret >= buflen - len)
1908 switch (data->phase2_type) {
1909 case EAP_TTLS_PHASE2_EAP:
1910 ret = os_snprintf(buf + len, buflen - len, "EAP-%s\n",
1911 data->phase2_method ?
1912 data->phase2_method->name : "?");
1914 case EAP_TTLS_PHASE2_MSCHAPV2:
1915 ret = os_snprintf(buf + len, buflen - len, "MSCHAPV2\n");
1917 case EAP_TTLS_PHASE2_MSCHAP:
1918 ret = os_snprintf(buf + len, buflen - len, "MSCHAP\n");
1920 case EAP_TTLS_PHASE2_PAP:
1921 ret = os_snprintf(buf + len, buflen - len, "PAP\n");
1923 case EAP_TTLS_PHASE2_CHAP:
1924 ret = os_snprintf(buf + len, buflen - len, "CHAP\n");
1930 if (ret < 0 || (size_t) ret >= buflen - len)
1938 static Boolean eap_ttls_isKeyAvailable(struct eap_sm *sm, void *priv)
1940 struct eap_ttls_data *data = priv;
1941 return data->key_data != NULL && data->phase2_success;
1945 static u8 * eap_ttls_getKey(struct eap_sm *sm, void *priv, size_t *len)
1947 struct eap_ttls_data *data = priv;
1950 if (data->key_data == NULL || !data->phase2_success)
1953 key = os_malloc(EAP_TLS_KEY_LEN);
1957 *len = EAP_TLS_KEY_LEN;
1958 os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
1964 int eap_peer_ttls_register(void)
1966 struct eap_method *eap;
1969 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1970 EAP_VENDOR_IETF, EAP_TYPE_TTLS, "TTLS");
1974 eap->init = eap_ttls_init;
1975 eap->deinit = eap_ttls_deinit;
1976 eap->process = eap_ttls_process;
1977 eap->isKeyAvailable = eap_ttls_isKeyAvailable;
1978 eap->getKey = eap_ttls_getKey;
1979 eap->get_status = eap_ttls_get_status;
1980 eap->has_reauth_data = eap_ttls_has_reauth_data;
1981 eap->deinit_for_reauth = eap_ttls_deinit_for_reauth;
1982 eap->init_for_reauth = eap_ttls_init_for_reauth;
1984 ret = eap_peer_method_register(eap);
1986 eap_peer_method_free(eap);