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 "crypto/ms_funcs.h"
19 #include "crypto/sha1.h"
20 #include "crypto/tls.h"
21 #include "eap_common/chap.h"
22 #include "eap_common/eap_ttls.h"
25 #include "eap_tls_common.h"
26 #include "eap_config.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;
76 int chbind_req_sent; /* channel binding request was sent */
84 /* draft-ietf-emu-chbind-13 section 5.3 */
101 static void * eap_ttls_init(struct eap_sm *sm)
103 struct eap_ttls_data *data;
104 struct eap_peer_config *config = eap_get_config(sm);
107 data = os_zalloc(sizeof(*data));
110 data->ttls_version = EAP_TTLS_VERSION;
111 data->force_ttls_version = -1;
113 data->phase2_type = EAP_TTLS_PHASE2_EAP;
115 #if EAP_TTLS_VERSION > 0
116 if (config && config->phase1) {
117 const char *pos = os_strstr(config->phase1, "ttlsver=");
119 data->force_ttls_version = atoi(pos + 8);
120 data->ttls_version = data->force_ttls_version;
121 wpa_printf(MSG_DEBUG, "EAP-TTLS: Forced TTLS version "
122 "%d", data->force_ttls_version);
125 #endif /* EAP_TTLS_VERSION */
127 if (config && config->phase2) {
128 if (os_strstr(config->phase2, "autheap=")) {
130 data->phase2_type = EAP_TTLS_PHASE2_EAP;
131 } else if (os_strstr(config->phase2, "auth=MSCHAPV2")) {
132 selected = "MSCHAPV2";
133 data->phase2_type = EAP_TTLS_PHASE2_MSCHAPV2;
134 } else if (os_strstr(config->phase2, "auth=MSCHAP")) {
136 data->phase2_type = EAP_TTLS_PHASE2_MSCHAP;
137 } else if (os_strstr(config->phase2, "auth=PAP")) {
139 data->phase2_type = EAP_TTLS_PHASE2_PAP;
140 } else if (os_strstr(config->phase2, "auth=CHAP")) {
142 data->phase2_type = EAP_TTLS_PHASE2_CHAP;
145 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase2 type: %s", selected);
147 if (data->phase2_type == EAP_TTLS_PHASE2_EAP) {
148 if (eap_peer_select_phase2_methods(config, "autheap=",
149 &data->phase2_eap_types,
150 &data->num_phase2_eap_types)
152 eap_ttls_deinit(sm, data);
156 data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
157 data->phase2_eap_type.method = EAP_TYPE_NONE;
160 #if EAP_TTLS_VERSION > 0
161 if (!(tls_capabilities(sm->ssl_ctx) & TLS_CAPABILITY_IA) &&
162 data->ttls_version > 0) {
163 if (data->force_ttls_version > 0) {
164 wpa_printf(MSG_INFO, "EAP-TTLS: Forced TTLSv%d and "
165 "TLS library does not support TLS/IA.",
166 data->force_ttls_version);
167 eap_ttls_deinit(sm, data);
170 data->ttls_version = 0;
172 #endif /* EAP_TTLS_VERSION */
178 static void eap_ttls_phase2_eap_deinit(struct eap_sm *sm,
179 struct eap_ttls_data *data)
181 if (data->phase2_priv && data->phase2_method) {
182 data->phase2_method->deinit(sm, data->phase2_priv);
183 data->phase2_method = NULL;
184 data->phase2_priv = NULL;
189 static void eap_ttls_deinit(struct eap_sm *sm, void *priv)
191 struct eap_ttls_data *data = priv;
194 eap_ttls_phase2_eap_deinit(sm, data);
195 os_free(data->phase2_eap_types);
196 if (data->ssl_initialized)
197 eap_peer_tls_ssl_deinit(sm, &data->ssl);
198 os_free(data->key_data);
199 wpabuf_free(data->pending_phase2_req);
204 static u8 * eap_ttls_avp_hdr(u8 *avphdr, u32 avp_code, u32 vendor_id,
205 int mandatory, size_t len)
207 struct ttls_avp_vendor *avp;
211 avp = (struct ttls_avp_vendor *) avphdr;
212 flags = mandatory ? AVP_FLAGS_MANDATORY : 0;
214 flags |= AVP_FLAGS_VENDOR;
215 hdrlen = sizeof(*avp);
216 avp->vendor_id = host_to_be32(vendor_id);
218 hdrlen = sizeof(struct ttls_avp);
221 avp->avp_code = host_to_be32(avp_code);
222 avp->avp_length = host_to_be32((flags << 24) | (hdrlen + len));
224 return avphdr + hdrlen;
228 static u8 * eap_ttls_avp_add(u8 *start, u8 *avphdr, u32 avp_code,
229 u32 vendor_id, int mandatory,
230 const u8 *data, size_t len)
233 pos = eap_ttls_avp_hdr(avphdr, avp_code, vendor_id, mandatory, len);
234 os_memcpy(pos, data, len);
241 static int eap_ttls_avp_encapsulate(struct wpabuf **resp, u32 avp_code,
247 msg = wpabuf_alloc(sizeof(struct ttls_avp) + wpabuf_len(*resp) + 4);
254 avp = wpabuf_mhead(msg);
255 pos = eap_ttls_avp_hdr(avp, avp_code, 0, mandatory, wpabuf_len(*resp));
256 os_memcpy(pos, wpabuf_head(*resp), wpabuf_len(*resp));
257 pos += wpabuf_len(*resp);
260 wpabuf_put(msg, pos - avp);
265 /* chop up resp into multiple vsa's as necessary*/
266 static int eap_ttls_avp_vsa_encapsulate(struct wpabuf **resp, u32 vendor,
267 u8 attr, int mandatory)
271 size_t size = wpabuf_len(*resp);
272 size_t num_msgs = 1 + (size / 248);
273 size_t msg_wrapper_size = sizeof(struct ttls_avp_vendor) + 4;
274 size_t allocated_total = num_msgs * (4 + msg_wrapper_size) + size;
276 msg = wpabuf_alloc(allocated_total);
282 src = wpabuf_mhead(*resp);
283 avp = wpabuf_mhead(msg);
285 int avp_size = size > 248 ? 248 : size;
287 pos = eap_ttls_avp_hdr(avp, attr, vendor, mandatory,
289 os_memcpy(pos, src, avp_size);
293 wpabuf_put(msg, pos - avp);
296 /* check avp-wpabuf_mhead(msg) < allocated_total */
302 #if EAP_TTLS_VERSION > 0
303 static int eap_ttls_ia_permute_inner_secret(struct eap_sm *sm,
304 struct eap_ttls_data *data,
305 const u8 *key, size_t key_len)
312 buf_len = 2 + key_len;
313 buf = os_malloc(buf_len);
316 WPA_PUT_BE16(buf, key_len);
317 os_memcpy(buf + 2, key, key_len);
323 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Session keys for TLS/IA inner "
324 "secret permutation", buf, buf_len);
325 ret = tls_connection_ia_permute_inner_secret(sm->ssl_ctx,
332 #endif /* EAP_TTLS_VERSION */
335 static int eap_ttls_v0_derive_key(struct eap_sm *sm,
336 struct eap_ttls_data *data)
338 os_free(data->key_data);
339 data->key_data = eap_peer_tls_derive_key(sm, &data->ssl,
340 "ttls keying material",
342 if (!data->key_data) {
343 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to derive key");
347 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
348 data->key_data, EAP_TLS_KEY_LEN);
354 #if EAP_TTLS_VERSION > 0
355 static int eap_ttls_v1_derive_key(struct eap_sm *sm,
356 struct eap_ttls_data *data)
358 struct tls_keys keys;
361 os_free(data->key_data);
362 data->key_data = NULL;
364 os_memset(&keys, 0, sizeof(keys));
365 if (tls_connection_get_keys(sm->ssl_ctx, data->ssl.conn, &keys) ||
366 keys.client_random == NULL || keys.server_random == NULL ||
367 keys.inner_secret == NULL) {
368 wpa_printf(MSG_INFO, "EAP-TTLS: Could not get inner secret, "
369 "client random, or server random to derive keying "
374 rnd = os_malloc(keys.client_random_len + keys.server_random_len);
375 data->key_data = os_malloc(EAP_TLS_KEY_LEN);
376 if (rnd == NULL || data->key_data == NULL) {
377 wpa_printf(MSG_INFO, "EAP-TTLS: No memory for key derivation");
379 os_free(data->key_data);
380 data->key_data = NULL;
383 os_memcpy(rnd, keys.client_random, keys.client_random_len);
384 os_memcpy(rnd + keys.client_random_len, keys.server_random,
385 keys.server_random_len);
387 if (tls_prf(keys.inner_secret, keys.inner_secret_len,
388 "ttls v1 keying material", rnd, keys.client_random_len +
389 keys.server_random_len, data->key_data, EAP_TLS_KEY_LEN)) {
390 wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to derive key");
392 os_free(data->key_data);
393 data->key_data = NULL;
397 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: client/server random",
398 rnd, keys.client_random_len + keys.server_random_len);
399 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: TLS/IA inner secret",
400 keys.inner_secret, keys.inner_secret_len);
404 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
405 data->key_data, EAP_TLS_KEY_LEN);
409 #endif /* EAP_TTLS_VERSION */
412 static u8 * eap_ttls_implicit_challenge(struct eap_sm *sm,
413 struct eap_ttls_data *data, size_t len)
415 #if EAP_TTLS_VERSION > 0
416 struct tls_keys keys;
418 #endif /* EAP_TTLS_VERSION */
420 if (data->ttls_version == 0) {
421 return eap_peer_tls_derive_key(sm, &data->ssl,
422 "ttls challenge", len);
425 #if EAP_TTLS_VERSION > 0
427 os_memset(&keys, 0, sizeof(keys));
428 if (tls_connection_get_keys(sm->ssl_ctx, data->ssl.conn, &keys) ||
429 keys.client_random == NULL || keys.server_random == NULL ||
430 keys.inner_secret == NULL) {
431 wpa_printf(MSG_INFO, "EAP-TTLS: Could not get inner secret, "
432 "client random, or server random to derive "
433 "implicit challenge");
437 rnd = os_malloc(keys.client_random_len + keys.server_random_len);
438 challenge = os_malloc(len);
439 if (rnd == NULL || challenge == NULL) {
440 wpa_printf(MSG_INFO, "EAP-TTLS: No memory for implicit "
441 "challenge derivation");
446 os_memcpy(rnd, keys.server_random, keys.server_random_len);
447 os_memcpy(rnd + keys.server_random_len, keys.client_random,
448 keys.client_random_len);
450 if (tls_prf(keys.inner_secret, keys.inner_secret_len,
451 "inner application challenge", rnd,
452 keys.client_random_len + keys.server_random_len,
454 wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to derive implicit "
463 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived implicit challenge",
468 #else /* EAP_TTLS_VERSION */
472 #endif /* EAP_TTLS_VERSION */
476 static void eap_ttlsv1_phase2_eap_finish(struct eap_sm *sm,
477 struct eap_ttls_data *data,
478 struct eap_method_ret *ret)
480 #if EAP_TTLS_VERSION > 0
481 if (data->ttls_version > 0) {
482 const struct eap_method *m = data->phase2_method;
483 void *priv = data->phase2_priv;
485 /* TTLSv1 requires TLS/IA FinalPhaseFinished */
486 if (ret->decision == DECISION_UNCOND_SUCC)
487 ret->decision = DECISION_COND_SUCC;
488 ret->methodState = METHOD_CONT;
490 if (ret->decision == DECISION_COND_SUCC &&
491 m->isKeyAvailable && m->getKey &&
492 m->isKeyAvailable(sm, priv)) {
495 key = m->getKey(sm, priv, &key_len);
497 eap_ttls_ia_permute_inner_secret(
498 sm, data, key, key_len);
503 #endif /* EAP_TTLS_VERSION */
507 static void eap_ttls_phase2_select_eap_method(struct eap_ttls_data *data,
511 for (i = 0; i < data->num_phase2_eap_types; i++) {
512 if (data->phase2_eap_types[i].vendor != EAP_VENDOR_IETF ||
513 data->phase2_eap_types[i].method != method)
516 data->phase2_eap_type.vendor =
517 data->phase2_eap_types[i].vendor;
518 data->phase2_eap_type.method =
519 data->phase2_eap_types[i].method;
520 wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
521 "Phase 2 EAP vendor %d method %d",
522 data->phase2_eap_type.vendor,
523 data->phase2_eap_type.method);
529 static int eap_ttls_phase2_eap_process(struct eap_sm *sm,
530 struct eap_ttls_data *data,
531 struct eap_method_ret *ret,
532 struct eap_hdr *hdr, size_t len,
533 struct wpabuf **resp)
536 struct eap_method_ret iret;
538 os_memset(&iret, 0, sizeof(iret));
539 wpabuf_set(&msg, hdr, len);
540 *resp = data->phase2_method->process(sm, data->phase2_priv, &iret,
542 if ((iret.methodState == METHOD_DONE ||
543 iret.methodState == METHOD_MAY_CONT) &&
544 (iret.decision == DECISION_UNCOND_SUCC ||
545 iret.decision == DECISION_COND_SUCC ||
546 iret.decision == DECISION_FAIL)) {
547 ret->methodState = iret.methodState;
548 ret->decision = iret.decision;
550 eap_ttlsv1_phase2_eap_finish(sm, data, ret);
556 static int eap_ttls_phase2_request_eap_method(struct eap_sm *sm,
557 struct eap_ttls_data *data,
558 struct eap_method_ret *ret,
559 struct eap_hdr *hdr, size_t len,
560 u8 method, struct wpabuf **resp)
563 if (data->tnc_started && data->phase2_method &&
564 data->phase2_priv && method == EAP_TYPE_TNC &&
565 data->phase2_eap_type.method == EAP_TYPE_TNC)
566 return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len,
569 if (data->ready_for_tnc && !data->tnc_started &&
570 method == EAP_TYPE_TNC) {
571 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
573 data->tnc_started = 1;
576 if (data->tnc_started) {
577 if (data->phase2_eap_type.vendor != EAP_VENDOR_IETF ||
578 data->phase2_eap_type.method == EAP_TYPE_TNC) {
579 wpa_printf(MSG_DEBUG, "EAP-TTLS: Unexpected EAP "
580 "type %d for TNC", method);
584 data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
585 data->phase2_eap_type.method = method;
586 wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
587 "Phase 2 EAP vendor %d method %d (TNC)",
588 data->phase2_eap_type.vendor,
589 data->phase2_eap_type.method);
591 if (data->phase2_type == EAP_TTLS_PHASE2_EAP)
592 eap_ttls_phase2_eap_deinit(sm, data);
596 if (data->phase2_eap_type.vendor == EAP_VENDOR_IETF &&
597 data->phase2_eap_type.method == EAP_TYPE_NONE)
598 eap_ttls_phase2_select_eap_method(data, method);
600 if (method != data->phase2_eap_type.method || method == EAP_TYPE_NONE)
602 if (eap_peer_tls_phase2_nak(data->phase2_eap_types,
603 data->num_phase2_eap_types,
609 if (data->phase2_priv == NULL) {
610 data->phase2_method = eap_peer_get_eap_method(
611 EAP_VENDOR_IETF, method);
612 if (data->phase2_method) {
614 data->phase2_priv = data->phase2_method->init(sm);
618 if (data->phase2_priv == NULL || data->phase2_method == NULL) {
619 wpa_printf(MSG_INFO, "EAP-TTLS: failed to initialize "
620 "Phase 2 EAP method %d", method);
624 return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len, resp);
628 static int eap_ttls_phase2_request_eap(struct eap_sm *sm,
629 struct eap_ttls_data *data,
630 struct eap_method_ret *ret,
632 struct wpabuf **resp)
634 size_t len = be_to_host16(hdr->length);
636 struct eap_peer_config *config = eap_get_config(sm);
638 if (len <= sizeof(struct eap_hdr)) {
639 wpa_printf(MSG_INFO, "EAP-TTLS: too short "
640 "Phase 2 request (len=%lu)", (unsigned long) len);
643 pos = (u8 *) (hdr + 1);
644 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP Request: type=%d", *pos);
646 case EAP_TYPE_IDENTITY:
647 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
650 if (eap_ttls_phase2_request_eap_method(sm, data, ret, hdr, len,
657 (config->pending_req_identity || config->pending_req_password ||
658 config->pending_req_otp)) {
665 wpa_hexdump_buf(MSG_DEBUG, "EAP-TTLS: AVP encapsulate EAP Response",
667 return eap_ttls_avp_encapsulate(resp, RADIUS_ATTR_EAP_MESSAGE, 1);
671 static void eap_ttlsv1_permute_inner(struct eap_sm *sm,
672 struct eap_ttls_data *data)
674 #if EAP_TTLS_VERSION > 0
675 u8 session_key[2 * MSCHAPV2_KEY_LEN];
677 if (data->ttls_version == 0)
680 get_asymetric_start_key(data->master_key, session_key,
681 MSCHAPV2_KEY_LEN, 0, 0);
682 get_asymetric_start_key(data->master_key,
683 session_key + MSCHAPV2_KEY_LEN,
684 MSCHAPV2_KEY_LEN, 1, 0);
685 eap_ttls_ia_permute_inner_secret(sm, data, session_key,
686 sizeof(session_key));
687 #endif /* EAP_TTLS_VERSION */
691 static int eap_ttls_phase2_request_mschapv2(struct eap_sm *sm,
692 struct eap_ttls_data *data,
693 struct eap_method_ret *ret,
694 struct wpabuf **resp)
697 u8 *buf, *pos, *challenge, *peer_challenge;
698 const u8 *identity, *password;
699 size_t identity_len, password_len;
702 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAPV2 Request");
704 identity = eap_get_config_identity(sm, &identity_len);
705 password = eap_get_config_password2(sm, &password_len, &pwhash);
706 if (identity == NULL || password == NULL)
709 msg = wpabuf_alloc(identity_len + 1000);
711 wpa_printf(MSG_ERROR,
712 "EAP-TTLS/MSCHAPV2: Failed to allocate memory");
715 pos = buf = wpabuf_mhead(msg);
718 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
719 identity, identity_len);
721 /* MS-CHAP-Challenge */
722 challenge = eap_ttls_implicit_challenge(
723 sm, data, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN + 1);
724 if (challenge == NULL) {
726 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
727 "implicit challenge");
730 peer_challenge = challenge + 1 + EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
732 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
733 RADIUS_VENDOR_ID_MICROSOFT, 1,
734 challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
736 /* MS-CHAP2-Response */
737 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP2_RESPONSE,
738 RADIUS_VENDOR_ID_MICROSOFT, 1,
739 EAP_TTLS_MSCHAPV2_RESPONSE_LEN);
740 data->ident = challenge[EAP_TTLS_MSCHAPV2_CHALLENGE_LEN];
741 *pos++ = data->ident;
742 *pos++ = 0; /* Flags */
743 os_memcpy(pos, peer_challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
744 pos += EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
745 os_memset(pos, 0, 8); /* Reserved, must be zero */
747 if (mschapv2_derive_response(identity, identity_len, password,
748 password_len, pwhash, challenge,
749 peer_challenge, pos, data->auth_response,
752 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
756 data->auth_response_valid = 1;
758 eap_ttlsv1_permute_inner(sm, data);
764 wpabuf_put(msg, pos - buf);
767 if (sm->workaround && data->ttls_version == 0) {
768 /* At least FreeRADIUS seems to be terminating
769 * EAP-TTLS/MSHCAPV2 without the expected MS-CHAP-v2 Success
771 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: EAP workaround - "
772 "allow success without tunneled response");
773 ret->methodState = METHOD_MAY_CONT;
774 ret->decision = DECISION_COND_SUCC;
781 static int eap_ttls_phase2_request_mschap(struct eap_sm *sm,
782 struct eap_ttls_data *data,
783 struct eap_method_ret *ret,
784 struct wpabuf **resp)
787 u8 *buf, *pos, *challenge;
788 const u8 *identity, *password;
789 size_t identity_len, password_len;
792 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAP Request");
794 identity = eap_get_config_identity(sm, &identity_len);
795 password = eap_get_config_password2(sm, &password_len, &pwhash);
796 if (identity == NULL || password == NULL)
799 msg = wpabuf_alloc(identity_len + 1000);
801 wpa_printf(MSG_ERROR,
802 "EAP-TTLS/MSCHAP: Failed to allocate memory");
805 pos = buf = wpabuf_mhead(msg);
808 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
809 identity, identity_len);
811 /* MS-CHAP-Challenge */
812 challenge = eap_ttls_implicit_challenge(
813 sm, data, EAP_TTLS_MSCHAP_CHALLENGE_LEN + 1);
814 if (challenge == NULL) {
816 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAP: Failed to derive "
817 "implicit challenge");
821 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
822 RADIUS_VENDOR_ID_MICROSOFT, 1,
823 challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
825 /* MS-CHAP-Response */
826 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP_RESPONSE,
827 RADIUS_VENDOR_ID_MICROSOFT, 1,
828 EAP_TTLS_MSCHAP_RESPONSE_LEN);
829 data->ident = challenge[EAP_TTLS_MSCHAP_CHALLENGE_LEN];
830 *pos++ = data->ident;
831 *pos++ = 1; /* Flags: Use NT style passwords */
832 os_memset(pos, 0, 24); /* LM-Response */
835 challenge_response(challenge, password, pos); /* NT-Response */
836 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password hash",
839 nt_challenge_response(challenge, password, password_len,
840 pos); /* NT-Response */
841 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password",
842 password, password_len);
844 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP implicit challenge",
845 challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
846 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP response", pos, 24);
851 wpabuf_put(msg, pos - buf);
854 if (data->ttls_version > 0) {
855 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
856 * so do not allow connection to be terminated yet. */
857 ret->methodState = METHOD_CONT;
858 ret->decision = DECISION_COND_SUCC;
860 /* EAP-TTLS/MSCHAP does not provide tunneled success
861 * notification, so assume that Phase2 succeeds. */
862 ret->methodState = METHOD_DONE;
863 ret->decision = DECISION_COND_SUCC;
870 static int eap_ttls_phase2_request_pap(struct eap_sm *sm,
871 struct eap_ttls_data *data,
872 struct eap_method_ret *ret,
873 struct wpabuf **resp)
878 const u8 *identity, *password;
879 size_t identity_len, password_len;
881 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 PAP Request");
883 identity = eap_get_config_identity(sm, &identity_len);
884 password = eap_get_config_password(sm, &password_len);
885 if (identity == NULL || password == NULL)
888 msg = wpabuf_alloc(identity_len + password_len + 100);
890 wpa_printf(MSG_ERROR,
891 "EAP-TTLS/PAP: Failed to allocate memory");
894 pos = buf = wpabuf_mhead(msg);
897 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
898 identity, identity_len);
900 /* User-Password; in RADIUS, this is encrypted, but EAP-TTLS encrypts
901 * the data, so no separate encryption is used in the AVP itself.
902 * However, the password is padded to obfuscate its length. */
903 pad = password_len == 0 ? 16 : (16 - (password_len & 15)) & 15;
904 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_USER_PASSWORD, 0, 1,
906 os_memcpy(pos, password, password_len);
908 os_memset(pos, 0, pad);
912 wpabuf_put(msg, pos - buf);
915 if (data->ttls_version > 0) {
916 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
917 * so do not allow connection to be terminated yet. */
918 ret->methodState = METHOD_CONT;
919 ret->decision = DECISION_COND_SUCC;
921 /* EAP-TTLS/PAP does not provide tunneled success notification,
922 * so assume that Phase2 succeeds. */
923 ret->methodState = METHOD_DONE;
924 ret->decision = DECISION_COND_SUCC;
931 static int eap_ttls_phase2_request_chap(struct eap_sm *sm,
932 struct eap_ttls_data *data,
933 struct eap_method_ret *ret,
934 struct wpabuf **resp)
937 u8 *buf, *pos, *challenge;
938 const u8 *identity, *password;
939 size_t identity_len, password_len;
941 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 CHAP Request");
943 identity = eap_get_config_identity(sm, &identity_len);
944 password = eap_get_config_password(sm, &password_len);
945 if (identity == NULL || password == NULL)
948 msg = wpabuf_alloc(identity_len + 1000);
950 wpa_printf(MSG_ERROR,
951 "EAP-TTLS/CHAP: Failed to allocate memory");
954 pos = buf = wpabuf_mhead(msg);
957 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
958 identity, identity_len);
961 challenge = eap_ttls_implicit_challenge(
962 sm, data, EAP_TTLS_CHAP_CHALLENGE_LEN + 1);
963 if (challenge == NULL) {
965 wpa_printf(MSG_ERROR, "EAP-TTLS/CHAP: Failed to derive "
966 "implicit challenge");
970 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_CHAP_CHALLENGE, 0, 1,
971 challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
974 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_CHAP_PASSWORD, 0, 1,
975 1 + EAP_TTLS_CHAP_PASSWORD_LEN);
976 data->ident = challenge[EAP_TTLS_CHAP_CHALLENGE_LEN];
977 *pos++ = data->ident;
979 /* MD5(Ident + Password + Challenge) */
980 chap_md5(data->ident, password, password_len, challenge,
981 EAP_TTLS_CHAP_CHALLENGE_LEN, pos);
983 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: CHAP username",
984 identity, identity_len);
985 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: CHAP password",
986 password, password_len);
987 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP implicit challenge",
988 challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
989 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP password",
990 pos, EAP_TTLS_CHAP_PASSWORD_LEN);
991 pos += EAP_TTLS_CHAP_PASSWORD_LEN;
995 wpabuf_put(msg, pos - buf);
998 if (data->ttls_version > 0) {
999 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
1000 * so do not allow connection to be terminated yet. */
1001 ret->methodState = METHOD_CONT;
1002 ret->decision = DECISION_COND_SUCC;
1004 /* EAP-TTLS/CHAP does not provide tunneled success
1005 * notification, so assume that Phase2 succeeds. */
1006 ret->methodState = METHOD_DONE;
1007 ret->decision = DECISION_COND_SUCC;
1014 static int eap_ttls_phase2_request(struct eap_sm *sm,
1015 struct eap_ttls_data *data,
1016 struct eap_method_ret *ret,
1017 struct eap_hdr *hdr,
1018 struct wpabuf **resp)
1022 enum phase2_types phase2_type = data->phase2_type;
1025 if (data->tnc_started) {
1026 wpa_printf(MSG_DEBUG, "EAP-TTLS: Processing TNC");
1027 phase2_type = EAP_TTLS_PHASE2_EAP;
1029 #endif /* EAP_TNC */
1031 if (phase2_type == EAP_TTLS_PHASE2_MSCHAPV2 ||
1032 phase2_type == EAP_TTLS_PHASE2_MSCHAP ||
1033 phase2_type == EAP_TTLS_PHASE2_PAP ||
1034 phase2_type == EAP_TTLS_PHASE2_CHAP) {
1035 if (eap_get_config_identity(sm, &len) == NULL) {
1036 wpa_printf(MSG_INFO,
1037 "EAP-TTLS: Identity not configured");
1038 eap_sm_request_identity(sm);
1039 if (eap_get_config_password(sm, &len) == NULL)
1040 eap_sm_request_password(sm);
1044 if (eap_get_config_password(sm, &len) == NULL) {
1045 wpa_printf(MSG_INFO,
1046 "EAP-TTLS: Password not configured");
1047 eap_sm_request_password(sm);
1052 switch (phase2_type) {
1053 case EAP_TTLS_PHASE2_EAP:
1054 res = eap_ttls_phase2_request_eap(sm, data, ret, hdr, resp);
1056 case EAP_TTLS_PHASE2_MSCHAPV2:
1057 res = eap_ttls_phase2_request_mschapv2(sm, data, ret, resp);
1059 case EAP_TTLS_PHASE2_MSCHAP:
1060 res = eap_ttls_phase2_request_mschap(sm, data, ret, resp);
1062 case EAP_TTLS_PHASE2_PAP:
1063 res = eap_ttls_phase2_request_pap(sm, data, ret, resp);
1065 case EAP_TTLS_PHASE2_CHAP:
1066 res = eap_ttls_phase2_request_chap(sm, data, ret, resp);
1069 wpa_printf(MSG_ERROR, "EAP-TTLS: Phase 2 - Unknown");
1075 ret->methodState = METHOD_DONE;
1076 ret->decision = DECISION_FAIL;
1083 #if EAP_TTLS_VERSION > 0
1084 static struct wpabuf * eap_ttls_build_phase_finished(
1085 struct eap_sm *sm, struct eap_ttls_data *data, int id, int final)
1087 struct wpabuf *req, *buf;
1089 buf = tls_connection_ia_send_phase_finished(sm->ssl_ctx,
1095 req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TTLS,
1096 1 + wpabuf_len(buf),
1097 EAP_CODE_RESPONSE, id);
1103 wpabuf_put_u8(req, data->ttls_version);
1104 wpabuf_put_buf(req, buf);
1106 eap_update_len(req);
1110 #endif /* EAP_TTLS_VERSION */
1113 struct ttls_parse_avp {
1123 static int eap_ttls_parse_attr_eap(const u8 *dpos, size_t dlen,
1124 struct ttls_parse_avp *parse)
1126 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - EAP Message");
1127 if (parse->eapdata == NULL) {
1128 parse->eapdata = os_malloc(dlen);
1129 if (parse->eapdata == NULL) {
1130 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
1131 "memory for Phase 2 EAP data");
1134 os_memcpy(parse->eapdata, dpos, dlen);
1135 parse->eap_len = dlen;
1137 u8 *neweap = os_realloc(parse->eapdata, parse->eap_len + dlen);
1138 if (neweap == NULL) {
1139 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
1140 "memory for Phase 2 EAP data");
1143 os_memcpy(neweap + parse->eap_len, dpos, dlen);
1144 parse->eapdata = neweap;
1145 parse->eap_len += dlen;
1152 static int eap_ttls_parse_attr_chbind(const u8 *dpos, size_t dlen,
1153 struct ttls_parse_avp *parse)
1155 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - Channel Binding Message");
1157 if (parse->chbind_data == NULL) {
1158 parse->chbind_data = os_malloc(dlen);
1159 if (parse->chbind_data == NULL) {
1160 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
1161 "memory for Phase 2 channel binding data");
1164 os_memcpy(parse->chbind_data, dpos, dlen);
1165 parse->chbind_len = dlen;
1167 /* TODO: can this really happen? maybe just make this an error? */
1168 u8 *newchbind = os_realloc(parse->chbind_data,
1169 parse->chbind_len + dlen);
1170 if (newchbind == NULL) {
1171 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
1172 "memory for Phase 2 channel binding data");
1175 os_memcpy(newchbind + parse->chbind_len, dpos, dlen);
1176 parse->chbind_data = newchbind;
1177 parse->chbind_len += dlen;
1184 static int eap_ttls_parse_avp(u8 *pos, size_t left,
1185 struct ttls_parse_avp *parse)
1187 struct ttls_avp *avp;
1188 u32 avp_code, avp_length, vendor_id = 0;
1189 u8 avp_flags, *dpos;
1192 avp = (struct ttls_avp *) pos;
1193 avp_code = be_to_host32(avp->avp_code);
1194 avp_length = be_to_host32(avp->avp_length);
1195 avp_flags = (avp_length >> 24) & 0xff;
1196 avp_length &= 0xffffff;
1197 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP: code=%d flags=0x%02x "
1198 "length=%d", (int) avp_code, avp_flags,
1201 if (avp_length > left) {
1202 wpa_printf(MSG_WARNING, "EAP-TTLS: AVP overflow "
1203 "(len=%d, left=%lu) - dropped",
1204 (int) avp_length, (unsigned long) left);
1208 if (avp_length < sizeof(*avp)) {
1209 wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid AVP length %d",
1214 dpos = (u8 *) (avp + 1);
1215 dlen = avp_length - sizeof(*avp);
1216 if (avp_flags & AVP_FLAGS_VENDOR) {
1218 wpa_printf(MSG_WARNING, "EAP-TTLS: Vendor AVP "
1222 vendor_id = WPA_GET_BE32(dpos);
1223 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP vendor_id %d",
1229 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP data", dpos, dlen);
1231 if (vendor_id == 0 && avp_code == RADIUS_ATTR_EAP_MESSAGE) {
1232 if (eap_ttls_parse_attr_eap(dpos, dlen, parse) < 0)
1234 } else if (vendor_id == RADIUS_VENDOR_ID_UKERNA &&
1235 avp_code == RADIUS_ATTR_UKERNA_CHBIND) {
1236 /* message containing channel binding data */
1237 if (eap_ttls_parse_attr_chbind(dpos, dlen, parse) < 0)
1239 } else if (vendor_id == 0 && avp_code == RADIUS_ATTR_REPLY_MESSAGE) {
1240 /* This is an optional message that can be displayed to
1242 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: AVP - Reply-Message",
1244 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
1245 avp_code == RADIUS_ATTR_MS_CHAP2_SUCCESS) {
1246 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP2-Success",
1249 wpa_printf(MSG_WARNING, "EAP-TTLS: Unexpected "
1250 "MS-CHAP2-Success length "
1251 "(len=%lu, expected 43)",
1252 (unsigned long) dlen);
1255 parse->mschapv2 = dpos;
1256 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
1257 avp_code == RADIUS_ATTR_MS_CHAP_ERROR) {
1258 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP-Error",
1260 parse->mschapv2_error = 1;
1261 } else if (avp_flags & AVP_FLAGS_MANDATORY) {
1262 wpa_printf(MSG_WARNING, "EAP-TTLS: Unsupported mandatory AVP "
1263 "code %d vendor_id %d - dropped",
1264 (int) avp_code, (int) vendor_id);
1267 wpa_printf(MSG_DEBUG, "EAP-TTLS: Ignoring unsupported AVP "
1268 "code %d vendor_id %d",
1269 (int) avp_code, (int) vendor_id);
1276 static int eap_ttls_parse_avps(struct wpabuf *in_decrypted,
1277 struct ttls_parse_avp *parse)
1283 pos = wpabuf_mhead(in_decrypted);
1284 left = wpabuf_len(in_decrypted);
1285 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Decrypted Phase 2 AVPs", pos, left);
1286 if (left < sizeof(struct ttls_avp)) {
1287 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 AVP frame"
1288 " len=%lu expected %lu or more - dropped",
1289 (unsigned long) left,
1290 (unsigned long) sizeof(struct ttls_avp));
1295 os_memset(parse, 0, sizeof(*parse));
1298 avp_length = eap_ttls_parse_avp(pos, left, parse);
1302 pad = (4 - (avp_length & 3)) & 3;
1303 pos += avp_length + pad;
1304 if (left < avp_length + pad)
1307 left -= avp_length + pad;
1314 static u8 * eap_ttls_fake_identity_request(void)
1316 struct eap_hdr *hdr;
1319 wpa_printf(MSG_DEBUG, "EAP-TTLS: empty data in beginning of "
1320 "Phase 2 - use fake EAP-Request Identity");
1321 buf = os_malloc(sizeof(*hdr) + 1);
1323 wpa_printf(MSG_WARNING, "EAP-TTLS: failed to allocate "
1324 "memory for fake EAP-Identity Request");
1328 hdr = (struct eap_hdr *) buf;
1329 hdr->code = EAP_CODE_REQUEST;
1330 hdr->identifier = 0;
1331 hdr->length = host_to_be16(sizeof(*hdr) + 1);
1332 buf[sizeof(*hdr)] = EAP_TYPE_IDENTITY;
1338 static int eap_ttls_encrypt_response(struct eap_sm *sm,
1339 struct eap_ttls_data *data,
1340 struct wpabuf *resp, u8 identifier,
1341 struct wpabuf **out_data)
1346 wpa_hexdump_buf_key(MSG_DEBUG, "EAP-TTLS: Encrypting Phase 2 data",
1348 if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
1349 data->ttls_version, identifier,
1351 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to encrypt a Phase 2 "
1360 static int eap_ttls_add_chbind_request(struct eap_sm *sm,
1361 struct eap_ttls_data *data,
1362 struct wpabuf **resp)
1364 struct wpabuf *chbind_req, *res;
1366 struct eap_peer_config *config = eap_get_config(sm);
1368 if (!config->chbind_config || config->chbind_config_len <= 0)
1371 for (i=0; i<config->chbind_config_len; i++) {
1372 length += 3 + config->chbind_config[i].req_data_len;
1375 chbind_req = wpabuf_alloc(length);
1379 wpabuf_put_u8(chbind_req, CHBIND_CODE_REQUEST);
1380 for (i=0; i<config->chbind_config_len; i++) {
1381 struct eap_peer_chbind_config *chbind_config =
1382 &config->chbind_config[i];
1383 wpabuf_put_be16(chbind_req, chbind_config->req_data_len);
1384 wpabuf_put_u8(chbind_req, chbind_config->nsid);
1385 wpabuf_put_data(chbind_req, chbind_config->req_data,
1386 chbind_config->req_data_len);
1388 if (eap_ttls_avp_vsa_encapsulate(&chbind_req,
1389 RADIUS_VENDOR_ID_UKERNA,
1390 RADIUS_ATTR_UKERNA_CHBIND, 0) < 0)
1393 /* bleh. This will free *resp regardless of whether combined buffer
1394 alloc succeeds, which is not consistent with the other error
1395 condition behavior in this function */
1396 *resp = wpabuf_concat(chbind_req, *resp);
1398 return (*resp) ? 0 : -1;
1402 static int eap_ttls_process_chbind(struct eap_sm *sm,
1403 struct eap_ttls_data *data,
1404 struct eap_method_ret *ret,
1405 struct ttls_parse_avp *parse,
1406 struct wpabuf **resp)
1411 struct chbind_hdr *hdr;
1412 struct eap_peer_config *config = eap_get_config(sm);
1414 if (parse->chbind_data == NULL) {
1415 wpa_printf(MSG_WARNING, "EAP-TTLS: No channel binding message "
1416 "in the packet - dropped");
1419 if (parse->chbind_len < 1 + sizeof(*hdr)) {
1420 wpa_printf(MSG_WARNING, "EAP-TTLS: bad channel binding response "
1421 "frame (len=%lu, expected %lu or more) - dropped",
1422 (unsigned long) parse->chbind_len,
1423 (unsigned long) sizeof(*hdr));
1426 code = parse->chbind_data[pos++];
1427 while (pos+sizeof(*hdr) < parse->chbind_len) {
1428 hdr = (struct chbind_hdr *)(&parse->chbind_data[pos]);
1429 pos += sizeof(*hdr);
1430 len = be_to_host16(hdr->len);
1431 if (pos + len <= parse->chbind_len) {
1433 for (i=0; i<config->chbind_config_len; i++) {
1434 struct eap_peer_chbind_config *chbind_config =
1435 &config->chbind_config[i];
1436 if (chbind_config->nsid == hdr->nsid)
1437 chbind_config->response_cb(
1440 &parse->chbind_data[pos], len);
1445 if (pos != parse->chbind_len) {
1446 wpa_printf(MSG_WARNING, "EAP-TTLS: bad channel binding response "
1447 "frame (parsed len=%lu, expected %lu) - dropped",
1448 (unsigned long) pos,
1449 (unsigned long) parse->chbind_len);
1455 static int eap_ttls_process_phase2_eap(struct eap_sm *sm,
1456 struct eap_ttls_data *data,
1457 struct eap_method_ret *ret,
1458 struct ttls_parse_avp *parse,
1459 struct wpabuf **resp)
1461 struct eap_hdr *hdr;
1464 if (parse->eapdata == NULL) {
1465 wpa_printf(MSG_WARNING, "EAP-TTLS: No EAP Message in the "
1466 "packet - dropped");
1470 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP",
1471 parse->eapdata, parse->eap_len);
1472 hdr = (struct eap_hdr *) parse->eapdata;
1474 if (parse->eap_len < sizeof(*hdr)) {
1475 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 EAP "
1476 "frame (len=%lu, expected %lu or more) - dropped",
1477 (unsigned long) parse->eap_len,
1478 (unsigned long) sizeof(*hdr));
1481 len = be_to_host16(hdr->length);
1482 if (len > parse->eap_len) {
1483 wpa_printf(MSG_INFO, "EAP-TTLS: Length mismatch in Phase 2 "
1484 "EAP frame (EAP hdr len=%lu, EAP data len in "
1486 (unsigned long) len,
1487 (unsigned long) parse->eap_len);
1490 wpa_printf(MSG_DEBUG, "EAP-TTLS: received Phase 2: code=%d "
1491 "identifier=%d length=%lu",
1492 hdr->code, hdr->identifier, (unsigned long) len);
1493 switch (hdr->code) {
1494 case EAP_CODE_REQUEST:
1495 if (eap_ttls_phase2_request(sm, data, ret, hdr, resp)) {
1496 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
1497 "processing failed");
1502 wpa_printf(MSG_INFO, "EAP-TTLS: Unexpected code=%d in "
1503 "Phase 2 EAP header", hdr->code);
1511 static int eap_ttls_process_phase2_mschapv2(struct eap_sm *sm,
1512 struct eap_ttls_data *data,
1513 struct eap_method_ret *ret,
1514 struct ttls_parse_avp *parse)
1516 if (parse->mschapv2_error) {
1517 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Received "
1518 "MS-CHAP-Error - failed");
1519 ret->methodState = METHOD_DONE;
1520 ret->decision = DECISION_FAIL;
1521 /* Reply with empty data to ACK error */
1525 if (parse->mschapv2 == NULL) {
1527 if (data->phase2_success && parse->eapdata) {
1529 * Allow EAP-TNC to be started after successfully
1530 * completed MSCHAPV2.
1534 #endif /* EAP_TNC */
1535 wpa_printf(MSG_WARNING, "EAP-TTLS: no MS-CHAP2-Success AVP "
1536 "received for Phase2 MSCHAPV2");
1539 if (parse->mschapv2[0] != data->ident) {
1540 wpa_printf(MSG_WARNING, "EAP-TTLS: Ident mismatch for Phase 2 "
1541 "MSCHAPV2 (received Ident 0x%02x, expected 0x%02x)",
1542 parse->mschapv2[0], data->ident);
1545 if (!data->auth_response_valid ||
1546 mschapv2_verify_auth_response(data->auth_response,
1547 parse->mschapv2 + 1, 42)) {
1548 wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid authenticator "
1549 "response in Phase 2 MSCHAPV2 success request");
1553 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 MSCHAPV2 "
1554 "authentication succeeded");
1555 if (data->ttls_version > 0) {
1557 * EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report
1558 * success, so do not allow connection to be terminated
1561 ret->methodState = METHOD_CONT;
1562 ret->decision = DECISION_COND_SUCC;
1564 ret->methodState = METHOD_DONE;
1565 ret->decision = DECISION_UNCOND_SUCC;
1566 data->phase2_success = 1;
1570 * Reply with empty data; authentication server will reply
1571 * with EAP-Success after this.
1578 static int eap_ttls_process_tnc_start(struct eap_sm *sm,
1579 struct eap_ttls_data *data,
1580 struct eap_method_ret *ret,
1581 struct ttls_parse_avp *parse,
1582 struct wpabuf **resp)
1584 /* TNC uses inner EAP method after non-EAP TTLS phase 2. */
1585 if (parse->eapdata == NULL) {
1586 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
1587 "unexpected tunneled data (no EAP)");
1591 if (!data->ready_for_tnc) {
1592 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
1593 "EAP after non-EAP, but not ready for TNC");
1597 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
1599 data->tnc_started = 1;
1601 if (eap_ttls_process_phase2_eap(sm, data, ret, parse, resp) < 0)
1606 #endif /* EAP_TNC */
1609 static int eap_ttls_process_decrypted(struct eap_sm *sm,
1610 struct eap_ttls_data *data,
1611 struct eap_method_ret *ret,
1613 struct ttls_parse_avp *parse,
1614 struct wpabuf *in_decrypted,
1615 struct wpabuf **out_data)
1617 struct wpabuf *resp = NULL;
1618 struct eap_peer_config *config = eap_get_config(sm);
1620 enum phase2_types phase2_type = data->phase2_type;
1623 if (data->tnc_started)
1624 phase2_type = EAP_TTLS_PHASE2_EAP;
1625 #endif /* EAP_TNC */
1627 switch (phase2_type) {
1628 case EAP_TTLS_PHASE2_EAP:
1629 if (eap_ttls_process_phase2_eap(sm, data, ret, parse, &resp) <
1633 case EAP_TTLS_PHASE2_MSCHAPV2:
1634 res = eap_ttls_process_phase2_mschapv2(sm, data, ret, parse);
1636 if (res == 1 && parse->eapdata && data->phase2_success) {
1638 * TNC may be required as the next
1639 * authentication method within the tunnel.
1641 ret->methodState = METHOD_MAY_CONT;
1642 data->ready_for_tnc = 1;
1643 if (eap_ttls_process_tnc_start(sm, data, ret, parse,
1647 #endif /* EAP_TNC */
1649 case EAP_TTLS_PHASE2_MSCHAP:
1650 case EAP_TTLS_PHASE2_PAP:
1651 case EAP_TTLS_PHASE2_CHAP:
1653 if (eap_ttls_process_tnc_start(sm, data, ret, parse, &resp) <
1658 /* EAP-TTLS/{MSCHAP,PAP,CHAP} should not send any TLS tunneled
1659 * requests to the supplicant */
1660 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received unexpected "
1663 #endif /* EAP_TNC */
1666 if (!resp && (config->pending_req_identity ||
1667 config->pending_req_password ||
1668 config->pending_req_otp ||
1669 config->pending_req_new_password)) {
1670 wpabuf_free(data->pending_phase2_req);
1671 data->pending_phase2_req = wpabuf_dup(in_decrypted);
1675 /* handle channel binding response here */
1676 if (parse->chbind_data) {
1677 /* received channel binding repsonse */
1678 if (eap_ttls_process_chbind(sm, data, ret, parse, &resp) < 0)
1681 /* issue channel binding request when appropriate */
1682 if (config->chbind_config && config->chbind_config_len > 0 &&
1683 !data->chbind_req_sent) {
1684 if (eap_ttls_add_chbind_request(sm, data, &resp) < 0)
1686 data->chbind_req_sent = 1;
1690 if (eap_ttls_encrypt_response(sm, data, resp, identifier,
1699 #if EAP_TTLS_VERSION > 0
1700 static void eap_ttls_final_phase_finished(struct eap_sm *sm,
1701 struct eap_ttls_data *data,
1702 struct eap_method_ret *ret,
1704 struct wpabuf **out_data)
1706 wpa_printf(MSG_DEBUG, "EAP-TTLS: FinalPhaseFinished received");
1707 wpa_printf(MSG_INFO, "EAP-TTLS: TLS/IA authentication succeeded");
1708 ret->methodState = METHOD_DONE;
1709 ret->decision = DECISION_UNCOND_SUCC;
1710 data->phase2_success = 1;
1711 *out_data = eap_ttls_build_phase_finished(sm, data, identifier, 1);
1712 eap_ttls_v1_derive_key(sm, data);
1714 #endif /* EAP_TTLS_VERSION */
1717 static int eap_ttls_implicit_identity_request(struct eap_sm *sm,
1718 struct eap_ttls_data *data,
1719 struct eap_method_ret *ret,
1721 struct wpabuf **out_data)
1724 struct eap_hdr *hdr;
1725 struct wpabuf *resp;
1727 hdr = (struct eap_hdr *) eap_ttls_fake_identity_request();
1729 ret->methodState = METHOD_DONE;
1730 ret->decision = DECISION_FAIL;
1735 if (eap_ttls_phase2_request(sm, data, ret, hdr, &resp)) {
1736 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
1737 "processing failed");
1740 retval = eap_ttls_encrypt_response(sm, data, resp, identifier,
1747 ret->methodState = METHOD_DONE;
1748 ret->decision = DECISION_FAIL;
1755 static int eap_ttls_phase2_start(struct eap_sm *sm, struct eap_ttls_data *data,
1756 struct eap_method_ret *ret, u8 identifier,
1757 struct wpabuf **out_data)
1759 data->phase2_start = 0;
1762 * EAP-TTLS does not use Phase2 on fast re-auth; this must be done only
1763 * if TLS part was indeed resuming a previous session. Most
1764 * Authentication Servers terminate EAP-TTLS before reaching this
1765 * point, but some do not. Make wpa_supplicant stop phase 2 here, if
1769 tls_connection_resumed(sm->ssl_ctx, data->ssl.conn)) {
1770 wpa_printf(MSG_DEBUG, "EAP-TTLS: Session resumption - "
1772 *out_data = eap_peer_tls_build_ack(identifier, EAP_TYPE_TTLS,
1773 data->ttls_version);
1774 ret->methodState = METHOD_DONE;
1775 ret->decision = DECISION_UNCOND_SUCC;
1776 data->phase2_success = 1;
1780 return eap_ttls_implicit_identity_request(sm, data, ret, identifier,
1785 static int eap_ttls_decrypt(struct eap_sm *sm, struct eap_ttls_data *data,
1786 struct eap_method_ret *ret, u8 identifier,
1787 const struct wpabuf *in_data,
1788 struct wpabuf **out_data)
1790 struct wpabuf *in_decrypted = NULL;
1792 struct ttls_parse_avp parse;
1794 os_memset(&parse, 0, sizeof(parse));
1796 wpa_printf(MSG_DEBUG, "EAP-TTLS: received %lu bytes encrypted data for"
1798 in_data ? (unsigned long) wpabuf_len(in_data) : 0);
1800 if (data->pending_phase2_req) {
1801 wpa_printf(MSG_DEBUG, "EAP-TTLS: Pending Phase 2 request - "
1802 "skip decryption and use old data");
1803 /* Clear TLS reassembly state. */
1804 eap_peer_tls_reset_input(&data->ssl);
1806 in_decrypted = data->pending_phase2_req;
1807 data->pending_phase2_req = NULL;
1808 if (wpabuf_len(in_decrypted) == 0) {
1809 wpabuf_free(in_decrypted);
1810 return eap_ttls_implicit_identity_request(
1811 sm, data, ret, identifier, out_data);
1816 if ((in_data == NULL || wpabuf_len(in_data) == 0) &&
1817 data->phase2_start) {
1818 return eap_ttls_phase2_start(sm, data, ret, identifier,
1822 if (in_data == NULL || wpabuf_len(in_data) == 0) {
1823 /* Received TLS ACK - requesting more fragments */
1824 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
1826 identifier, NULL, out_data);
1829 retval = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
1833 #if EAP_TTLS_VERSION > 0
1834 if (data->ttls_version > 0 &&
1835 (in_decrypted == NULL || wpabuf_len(in_decrypted) == 0) &&
1836 tls_connection_ia_final_phase_finished(sm->ssl_ctx,
1838 eap_ttls_final_phase_finished(sm, data, ret, identifier,
1842 #endif /* EAP_TTLS_VERSION */
1845 data->phase2_start = 0;
1847 if (eap_ttls_parse_avps(in_decrypted, &parse) < 0) {
1852 retval = eap_ttls_process_decrypted(sm, data, ret, identifier,
1853 &parse, in_decrypted, out_data);
1856 wpabuf_free(in_decrypted);
1857 os_free(parse.eapdata);
1860 ret->methodState = METHOD_DONE;
1861 ret->decision = DECISION_FAIL;
1868 static int eap_ttls_process_start(struct eap_sm *sm,
1869 struct eap_ttls_data *data, u8 flags,
1870 struct eap_method_ret *ret)
1872 struct eap_peer_config *config = eap_get_config(sm);
1874 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start (server ver=%d, own ver=%d)",
1875 flags & EAP_TLS_VERSION_MASK, data->ttls_version);
1876 #if EAP_TTLS_VERSION > 0
1877 if ((flags & EAP_TLS_VERSION_MASK) < data->ttls_version)
1878 data->ttls_version = flags & EAP_TLS_VERSION_MASK;
1879 if (data->force_ttls_version >= 0 &&
1880 data->force_ttls_version != data->ttls_version) {
1881 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to select "
1882 "forced TTLS version %d",
1883 data->force_ttls_version);
1884 ret->methodState = METHOD_DONE;
1885 ret->decision = DECISION_FAIL;
1886 ret->allowNotifications = FALSE;
1889 wpa_printf(MSG_DEBUG, "EAP-TTLS: Using TTLS version %d",
1890 data->ttls_version);
1892 if (data->ttls_version > 0)
1893 data->ssl.tls_ia = 1;
1894 #endif /* EAP_TTLS_VERSION */
1895 if (!data->ssl_initialized &&
1896 eap_peer_tls_ssl_init(sm, &data->ssl, config)) {
1897 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to initialize SSL.");
1900 data->ssl_initialized = 1;
1902 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start");
1908 static int eap_ttls_process_handshake(struct eap_sm *sm,
1909 struct eap_ttls_data *data,
1910 struct eap_method_ret *ret,
1912 const u8 *in_data, size_t in_len,
1913 struct wpabuf **out_data)
1917 res = eap_peer_tls_process_helper(sm, &data->ssl, EAP_TYPE_TTLS,
1918 data->ttls_version, identifier,
1919 in_data, in_len, out_data);
1921 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1922 wpa_printf(MSG_DEBUG, "EAP-TTLS: TLS done, proceed to "
1924 if (data->resuming) {
1925 wpa_printf(MSG_DEBUG, "EAP-TTLS: fast reauth - may "
1927 ret->decision = DECISION_COND_SUCC;
1928 ret->methodState = METHOD_MAY_CONT;
1930 data->phase2_start = 1;
1931 if (data->ttls_version == 0)
1932 eap_ttls_v0_derive_key(sm, data);
1934 if (*out_data == NULL || wpabuf_len(*out_data) == 0) {
1935 if (eap_ttls_decrypt(sm, data, ret, identifier,
1937 wpa_printf(MSG_WARNING, "EAP-TTLS: "
1938 "failed to process early "
1939 "start for Phase 2");
1949 * Application data included in the handshake message.
1951 wpabuf_free(data->pending_phase2_req);
1952 data->pending_phase2_req = *out_data;
1954 wpabuf_set(&msg, in_data, in_len);
1955 res = eap_ttls_decrypt(sm, data, ret, identifier, &msg,
1963 static void eap_ttls_check_auth_status(struct eap_sm *sm,
1964 struct eap_ttls_data *data,
1965 struct eap_method_ret *ret)
1967 if (data->ttls_version == 0 && ret->methodState == METHOD_DONE) {
1968 ret->allowNotifications = FALSE;
1969 if (ret->decision == DECISION_UNCOND_SUCC ||
1970 ret->decision == DECISION_COND_SUCC) {
1971 wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1972 "completed successfully");
1973 data->phase2_success = 1;
1975 if (!data->ready_for_tnc && !data->tnc_started) {
1977 * TNC may be required as the next
1978 * authentication method within the tunnel.
1980 ret->methodState = METHOD_MAY_CONT;
1981 data->ready_for_tnc = 1;
1983 #endif /* EAP_TNC */
1985 } else if (data->ttls_version == 0 &&
1986 ret->methodState == METHOD_MAY_CONT &&
1987 (ret->decision == DECISION_UNCOND_SUCC ||
1988 ret->decision == DECISION_COND_SUCC)) {
1989 wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1990 "completed successfully (MAY_CONT)");
1991 data->phase2_success = 1;
1996 static struct wpabuf * eap_ttls_process(struct eap_sm *sm, void *priv,
1997 struct eap_method_ret *ret,
1998 const struct wpabuf *reqData)
2003 struct wpabuf *resp;
2005 struct eap_ttls_data *data = priv;
2007 pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_TTLS, ret,
2008 reqData, &left, &flags);
2011 id = eap_get_id(reqData);
2013 if (flags & EAP_TLS_FLAGS_START) {
2014 if (eap_ttls_process_start(sm, data, flags, ret) < 0)
2017 /* RFC 5281, Ch. 9.2:
2018 * "This packet MAY contain additional information in the form
2019 * of AVPs, which may provide useful hints to the client"
2020 * For now, ignore any potential extra data.
2023 } else if (!data->ssl_initialized) {
2024 wpa_printf(MSG_DEBUG, "EAP-TTLS: First message did not "
2025 "include Start flag");
2026 ret->methodState = METHOD_DONE;
2027 ret->decision = DECISION_FAIL;
2028 ret->allowNotifications = FALSE;
2033 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
2036 wpabuf_set(&msg, pos, left);
2037 res = eap_ttls_decrypt(sm, data, ret, id, &msg, &resp);
2039 res = eap_ttls_process_handshake(sm, data, ret, id,
2043 eap_ttls_check_auth_status(sm, data, ret);
2045 /* FIX: what about res == -1? Could just move all error processing into
2046 * the other functions and get rid of this res==1 case here. */
2049 return eap_peer_tls_build_ack(id, EAP_TYPE_TTLS,
2050 data->ttls_version);
2056 static Boolean eap_ttls_has_reauth_data(struct eap_sm *sm, void *priv)
2058 struct eap_ttls_data *data = priv;
2059 return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
2060 data->phase2_success;
2064 static void eap_ttls_deinit_for_reauth(struct eap_sm *sm, void *priv)
2066 struct eap_ttls_data *data = priv;
2067 wpabuf_free(data->pending_phase2_req);
2068 data->pending_phase2_req = NULL;
2070 data->ready_for_tnc = 0;
2071 data->tnc_started = 0;
2072 #endif /* EAP_TNC */
2076 static void * eap_ttls_init_for_reauth(struct eap_sm *sm, void *priv)
2078 struct eap_ttls_data *data = priv;
2079 os_free(data->key_data);
2080 data->key_data = NULL;
2081 if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
2085 if (data->phase2_priv && data->phase2_method &&
2086 data->phase2_method->init_for_reauth)
2087 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
2088 data->phase2_start = 0;
2089 data->phase2_success = 0;
2096 static int eap_ttls_get_status(struct eap_sm *sm, void *priv, char *buf,
2097 size_t buflen, int verbose)
2099 struct eap_ttls_data *data = priv;
2102 len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
2103 ret = os_snprintf(buf + len, buflen - len,
2104 "EAP-TTLSv%d Phase2 method=",
2105 data->ttls_version);
2106 if (ret < 0 || (size_t) ret >= buflen - len)
2109 switch (data->phase2_type) {
2110 case EAP_TTLS_PHASE2_EAP:
2111 ret = os_snprintf(buf + len, buflen - len, "EAP-%s\n",
2112 data->phase2_method ?
2113 data->phase2_method->name : "?");
2115 case EAP_TTLS_PHASE2_MSCHAPV2:
2116 ret = os_snprintf(buf + len, buflen - len, "MSCHAPV2\n");
2118 case EAP_TTLS_PHASE2_MSCHAP:
2119 ret = os_snprintf(buf + len, buflen - len, "MSCHAP\n");
2121 case EAP_TTLS_PHASE2_PAP:
2122 ret = os_snprintf(buf + len, buflen - len, "PAP\n");
2124 case EAP_TTLS_PHASE2_CHAP:
2125 ret = os_snprintf(buf + len, buflen - len, "CHAP\n");
2131 if (ret < 0 || (size_t) ret >= buflen - len)
2139 static Boolean eap_ttls_isKeyAvailable(struct eap_sm *sm, void *priv)
2141 struct eap_ttls_data *data = priv;
2142 return data->key_data != NULL && data->phase2_success;
2146 static u8 * eap_ttls_getKey(struct eap_sm *sm, void *priv, size_t *len)
2148 struct eap_ttls_data *data = priv;
2151 if (data->key_data == NULL || !data->phase2_success)
2154 key = os_malloc(EAP_TLS_KEY_LEN);
2158 *len = EAP_TLS_KEY_LEN;
2159 os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
2165 int eap_peer_ttls_register(void)
2167 struct eap_method *eap;
2170 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
2171 EAP_VENDOR_IETF, EAP_TYPE_TTLS, "TTLS");
2175 eap->init = eap_ttls_init;
2176 eap->deinit = eap_ttls_deinit;
2177 eap->process = eap_ttls_process;
2178 eap->isKeyAvailable = eap_ttls_isKeyAvailable;
2179 eap->getKey = eap_ttls_getKey;
2180 eap->get_status = eap_ttls_get_status;
2181 eap->has_reauth_data = eap_ttls_has_reauth_data;
2182 eap->deinit_for_reauth = eap_ttls_deinit_for_reauth;
2183 eap->init_for_reauth = eap_ttls_init_for_reauth;
2185 ret = eap_peer_method_register(eap);
2187 eap_peer_method_free(eap);