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 "radius/radius.h"
19 #include "crypto/ms_funcs.h"
20 #include "crypto/sha1.h"
21 #include "crypto/tls.h"
22 #include "eap_common/chap.h"
23 #include "eap_common/eap_ttls.h"
26 #include "eap_tls_common.h"
27 #include "eap_config.h"
30 /* Maximum supported TTLS version
32 * 1 = draft-funk-eap-ttls-v1-00.txt
34 #ifndef EAP_TTLS_VERSION
35 #define EAP_TTLS_VERSION 0 /* TTLSv1 implementation is not yet complete */
36 #endif /* EAP_TTLS_VERSION */
39 #define MSCHAPV2_KEY_LEN 16
40 #define MSCHAPV2_NT_RESPONSE_LEN 24
43 static void eap_ttls_deinit(struct eap_sm *sm, void *priv);
46 struct eap_ttls_data {
47 struct eap_ssl_data ssl;
50 int ttls_version, force_ttls_version;
52 const struct eap_method *phase2_method;
59 EAP_TTLS_PHASE2_MSCHAPV2,
60 EAP_TTLS_PHASE2_MSCHAP,
64 struct eap_method_type phase2_eap_type;
65 struct eap_method_type *phase2_eap_types;
66 size_t num_phase2_eap_types;
68 u8 auth_response[MSCHAPV2_AUTH_RESPONSE_LEN];
69 int auth_response_valid;
70 u8 master_key[MSCHAPV2_MASTER_KEY_LEN]; /* MSCHAPv2 master key */
72 int resuming; /* starting a resumed session */
73 int reauth; /* reauthentication */
76 struct wpabuf *pending_phase2_req;
77 int chbind_req_sent; /* channel binding request was sent */
85 /* draft-ietf-emu-chbind-13 section 5.3 */
102 static void * eap_ttls_init(struct eap_sm *sm)
104 struct eap_ttls_data *data;
105 struct eap_peer_config *config = eap_get_config(sm);
108 data = os_zalloc(sizeof(*data));
111 data->ttls_version = EAP_TTLS_VERSION;
112 data->force_ttls_version = -1;
114 data->phase2_type = EAP_TTLS_PHASE2_EAP;
116 #if EAP_TTLS_VERSION > 0
117 if (config && config->phase1) {
118 const char *pos = os_strstr(config->phase1, "ttlsver=");
120 data->force_ttls_version = atoi(pos + 8);
121 data->ttls_version = data->force_ttls_version;
122 wpa_printf(MSG_DEBUG, "EAP-TTLS: Forced TTLS version "
123 "%d", data->force_ttls_version);
126 #endif /* EAP_TTLS_VERSION */
128 if (config && config->phase2) {
129 if (os_strstr(config->phase2, "autheap=")) {
131 data->phase2_type = EAP_TTLS_PHASE2_EAP;
132 } else if (os_strstr(config->phase2, "auth=MSCHAPV2")) {
133 selected = "MSCHAPV2";
134 data->phase2_type = EAP_TTLS_PHASE2_MSCHAPV2;
135 } else if (os_strstr(config->phase2, "auth=MSCHAP")) {
137 data->phase2_type = EAP_TTLS_PHASE2_MSCHAP;
138 } else if (os_strstr(config->phase2, "auth=PAP")) {
140 data->phase2_type = EAP_TTLS_PHASE2_PAP;
141 } else if (os_strstr(config->phase2, "auth=CHAP")) {
143 data->phase2_type = EAP_TTLS_PHASE2_CHAP;
146 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase2 type: %s", selected);
148 if (data->phase2_type == EAP_TTLS_PHASE2_EAP) {
149 if (eap_peer_select_phase2_methods(config, "autheap=",
150 &data->phase2_eap_types,
151 &data->num_phase2_eap_types)
153 eap_ttls_deinit(sm, data);
157 data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
158 data->phase2_eap_type.method = EAP_TYPE_NONE;
161 #if EAP_TTLS_VERSION > 0
162 if (!(tls_capabilities(sm->ssl_ctx) & TLS_CAPABILITY_IA) &&
163 data->ttls_version > 0) {
164 if (data->force_ttls_version > 0) {
165 wpa_printf(MSG_INFO, "EAP-TTLS: Forced TTLSv%d and "
166 "TLS library does not support TLS/IA.",
167 data->force_ttls_version);
168 eap_ttls_deinit(sm, data);
171 data->ttls_version = 0;
173 #endif /* EAP_TTLS_VERSION */
179 static void eap_ttls_phase2_eap_deinit(struct eap_sm *sm,
180 struct eap_ttls_data *data)
182 if (data->phase2_priv && data->phase2_method) {
183 data->phase2_method->deinit(sm, data->phase2_priv);
184 data->phase2_method = NULL;
185 data->phase2_priv = NULL;
190 static void eap_ttls_deinit(struct eap_sm *sm, void *priv)
192 struct eap_ttls_data *data = priv;
195 eap_ttls_phase2_eap_deinit(sm, data);
196 os_free(data->phase2_eap_types);
197 if (data->ssl_initialized)
198 eap_peer_tls_ssl_deinit(sm, &data->ssl);
199 os_free(data->key_data);
200 wpabuf_free(data->pending_phase2_req);
205 static u8 * eap_ttls_avp_hdr(u8 *avphdr, u32 avp_code, u32 vendor_id,
206 int mandatory, size_t len)
208 struct ttls_avp_vendor *avp;
212 avp = (struct ttls_avp_vendor *) avphdr;
213 flags = mandatory ? AVP_FLAGS_MANDATORY : 0;
215 flags |= AVP_FLAGS_VENDOR;
216 hdrlen = sizeof(*avp);
217 avp->vendor_id = host_to_be32(vendor_id);
219 hdrlen = sizeof(struct ttls_avp);
222 avp->avp_code = host_to_be32(avp_code);
223 avp->avp_length = host_to_be32((flags << 24) | (hdrlen + len));
225 return avphdr + hdrlen;
229 static u8 * eap_ttls_avp_add(u8 *start, u8 *avphdr, u32 avp_code,
230 u32 vendor_id, int mandatory,
231 const u8 *data, size_t len)
234 pos = eap_ttls_avp_hdr(avphdr, avp_code, vendor_id, mandatory, len);
235 os_memcpy(pos, data, len);
242 static int eap_ttls_avp_encapsulate(struct wpabuf **resp, u32 avp_code,
248 msg = wpabuf_alloc(sizeof(struct ttls_avp) + wpabuf_len(*resp) + 4);
255 avp = wpabuf_mhead(msg);
256 pos = eap_ttls_avp_hdr(avp, avp_code, 0, mandatory, wpabuf_len(*resp));
257 os_memcpy(pos, wpabuf_head(*resp), wpabuf_len(*resp));
258 pos += wpabuf_len(*resp);
261 wpabuf_put(msg, pos - avp);
266 /* chop up resp into multiple vsa's as necessary*/
267 static int eap_ttls_avp_radius_vsa_encapsulate(struct wpabuf **resp, u32 vendor,
268 u8 attr, int mandatory)
271 u8 *avp, *pos, *src, *final;
272 size_t size = wpabuf_len(*resp);
273 size_t num_msgs = 1 + (size / 248);
274 size_t msg_wrapper_size = sizeof(struct ttls_avp_vendor) + 6;
275 size_t allocated_total = num_msgs * (4 + msg_wrapper_size) + size;
277 msg = wpabuf_alloc(allocated_total);
283 src = wpabuf_mhead(*resp);
284 avp = wpabuf_mhead(msg);
286 int avp_size = size > 248 ? 248 : size;
288 pos = eap_ttls_avp_hdr(avp, RADIUS_ATTR_VENDOR_SPECIFIC, 0, mandatory,
290 wpabuf_put(msg, pos-avp);
291 wpabuf_put_be32(msg, vendor);
292 wpabuf_put_u8(msg, (u8) attr);
293 wpabuf_put_u8(msg, (u8) avp_size+2);
294 wpabuf_put_data(msg, src, avp_size);
296 pos = wpabuf_mhead_u8(msg) + wpabuf_len(msg);
297 final = pos; /*keep pos so we know how much padding is added*/
298 AVP_PAD(avp, final); /*final modified*/
300 wpabuf_put(msg, final-pos);
303 /* check avp-wpabuf_mhead(msg) < allocated_total */
309 #if EAP_TTLS_VERSION > 0
310 static int eap_ttls_ia_permute_inner_secret(struct eap_sm *sm,
311 struct eap_ttls_data *data,
312 const u8 *key, size_t key_len)
319 buf_len = 2 + key_len;
320 buf = os_malloc(buf_len);
323 WPA_PUT_BE16(buf, key_len);
324 os_memcpy(buf + 2, key, key_len);
330 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Session keys for TLS/IA inner "
331 "secret permutation", buf, buf_len);
332 ret = tls_connection_ia_permute_inner_secret(sm->ssl_ctx,
339 #endif /* EAP_TTLS_VERSION */
342 static int eap_ttls_v0_derive_key(struct eap_sm *sm,
343 struct eap_ttls_data *data)
345 os_free(data->key_data);
346 data->key_data = eap_peer_tls_derive_key(sm, &data->ssl,
347 "ttls keying material",
349 if (!data->key_data) {
350 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to derive key");
354 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
355 data->key_data, EAP_TLS_KEY_LEN);
361 #if EAP_TTLS_VERSION > 0
362 static int eap_ttls_v1_derive_key(struct eap_sm *sm,
363 struct eap_ttls_data *data)
365 struct tls_keys keys;
368 os_free(data->key_data);
369 data->key_data = NULL;
371 os_memset(&keys, 0, sizeof(keys));
372 if (tls_connection_get_keys(sm->ssl_ctx, data->ssl.conn, &keys) ||
373 keys.client_random == NULL || keys.server_random == NULL ||
374 keys.inner_secret == NULL) {
375 wpa_printf(MSG_INFO, "EAP-TTLS: Could not get inner secret, "
376 "client random, or server random to derive keying "
381 rnd = os_malloc(keys.client_random_len + keys.server_random_len);
382 data->key_data = os_malloc(EAP_TLS_KEY_LEN);
383 if (rnd == NULL || data->key_data == NULL) {
384 wpa_printf(MSG_INFO, "EAP-TTLS: No memory for key derivation");
386 os_free(data->key_data);
387 data->key_data = NULL;
390 os_memcpy(rnd, keys.client_random, keys.client_random_len);
391 os_memcpy(rnd + keys.client_random_len, keys.server_random,
392 keys.server_random_len);
394 if (tls_prf(keys.inner_secret, keys.inner_secret_len,
395 "ttls v1 keying material", rnd, keys.client_random_len +
396 keys.server_random_len, data->key_data, EAP_TLS_KEY_LEN)) {
397 wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to derive key");
399 os_free(data->key_data);
400 data->key_data = NULL;
404 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: client/server random",
405 rnd, keys.client_random_len + keys.server_random_len);
406 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: TLS/IA inner secret",
407 keys.inner_secret, keys.inner_secret_len);
411 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
412 data->key_data, EAP_TLS_KEY_LEN);
416 #endif /* EAP_TTLS_VERSION */
419 static u8 * eap_ttls_implicit_challenge(struct eap_sm *sm,
420 struct eap_ttls_data *data, size_t len)
422 #if EAP_TTLS_VERSION > 0
423 struct tls_keys keys;
425 #endif /* EAP_TTLS_VERSION */
427 if (data->ttls_version == 0) {
428 return eap_peer_tls_derive_key(sm, &data->ssl,
429 "ttls challenge", len);
432 #if EAP_TTLS_VERSION > 0
434 os_memset(&keys, 0, sizeof(keys));
435 if (tls_connection_get_keys(sm->ssl_ctx, data->ssl.conn, &keys) ||
436 keys.client_random == NULL || keys.server_random == NULL ||
437 keys.inner_secret == NULL) {
438 wpa_printf(MSG_INFO, "EAP-TTLS: Could not get inner secret, "
439 "client random, or server random to derive "
440 "implicit challenge");
444 rnd = os_malloc(keys.client_random_len + keys.server_random_len);
445 challenge = os_malloc(len);
446 if (rnd == NULL || challenge == NULL) {
447 wpa_printf(MSG_INFO, "EAP-TTLS: No memory for implicit "
448 "challenge derivation");
453 os_memcpy(rnd, keys.server_random, keys.server_random_len);
454 os_memcpy(rnd + keys.server_random_len, keys.client_random,
455 keys.client_random_len);
457 if (tls_prf(keys.inner_secret, keys.inner_secret_len,
458 "inner application challenge", rnd,
459 keys.client_random_len + keys.server_random_len,
461 wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to derive implicit "
470 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived implicit challenge",
475 #else /* EAP_TTLS_VERSION */
479 #endif /* EAP_TTLS_VERSION */
483 static void eap_ttlsv1_phase2_eap_finish(struct eap_sm *sm,
484 struct eap_ttls_data *data,
485 struct eap_method_ret *ret)
487 #if EAP_TTLS_VERSION > 0
488 if (data->ttls_version > 0) {
489 const struct eap_method *m = data->phase2_method;
490 void *priv = data->phase2_priv;
492 /* TTLSv1 requires TLS/IA FinalPhaseFinished */
493 if (ret->decision == DECISION_UNCOND_SUCC)
494 ret->decision = DECISION_COND_SUCC;
495 ret->methodState = METHOD_CONT;
497 if (ret->decision == DECISION_COND_SUCC &&
498 m->isKeyAvailable && m->getKey &&
499 m->isKeyAvailable(sm, priv)) {
502 key = m->getKey(sm, priv, &key_len);
504 eap_ttls_ia_permute_inner_secret(
505 sm, data, key, key_len);
510 #endif /* EAP_TTLS_VERSION */
514 static void eap_ttls_phase2_select_eap_method(struct eap_ttls_data *data,
518 for (i = 0; i < data->num_phase2_eap_types; i++) {
519 if (data->phase2_eap_types[i].vendor != EAP_VENDOR_IETF ||
520 data->phase2_eap_types[i].method != method)
523 data->phase2_eap_type.vendor =
524 data->phase2_eap_types[i].vendor;
525 data->phase2_eap_type.method =
526 data->phase2_eap_types[i].method;
527 wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
528 "Phase 2 EAP vendor %d method %d",
529 data->phase2_eap_type.vendor,
530 data->phase2_eap_type.method);
536 static int eap_ttls_phase2_eap_process(struct eap_sm *sm,
537 struct eap_ttls_data *data,
538 struct eap_method_ret *ret,
539 struct eap_hdr *hdr, size_t len,
540 struct wpabuf **resp)
543 struct eap_method_ret iret;
545 os_memset(&iret, 0, sizeof(iret));
546 wpabuf_set(&msg, hdr, len);
547 *resp = data->phase2_method->process(sm, data->phase2_priv, &iret,
549 if ((iret.methodState == METHOD_DONE ||
550 iret.methodState == METHOD_MAY_CONT) &&
551 (iret.decision == DECISION_UNCOND_SUCC ||
552 iret.decision == DECISION_COND_SUCC ||
553 iret.decision == DECISION_FAIL)) {
554 ret->methodState = iret.methodState;
555 ret->decision = iret.decision;
557 eap_ttlsv1_phase2_eap_finish(sm, data, ret);
563 static int eap_ttls_phase2_request_eap_method(struct eap_sm *sm,
564 struct eap_ttls_data *data,
565 struct eap_method_ret *ret,
566 struct eap_hdr *hdr, size_t len,
567 u8 method, struct wpabuf **resp)
570 if (data->tnc_started && data->phase2_method &&
571 data->phase2_priv && method == EAP_TYPE_TNC &&
572 data->phase2_eap_type.method == EAP_TYPE_TNC)
573 return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len,
576 if (data->ready_for_tnc && !data->tnc_started &&
577 method == EAP_TYPE_TNC) {
578 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
580 data->tnc_started = 1;
583 if (data->tnc_started) {
584 if (data->phase2_eap_type.vendor != EAP_VENDOR_IETF ||
585 data->phase2_eap_type.method == EAP_TYPE_TNC) {
586 wpa_printf(MSG_DEBUG, "EAP-TTLS: Unexpected EAP "
587 "type %d for TNC", method);
591 data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
592 data->phase2_eap_type.method = method;
593 wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
594 "Phase 2 EAP vendor %d method %d (TNC)",
595 data->phase2_eap_type.vendor,
596 data->phase2_eap_type.method);
598 if (data->phase2_type == EAP_TTLS_PHASE2_EAP)
599 eap_ttls_phase2_eap_deinit(sm, data);
603 if (data->phase2_eap_type.vendor == EAP_VENDOR_IETF &&
604 data->phase2_eap_type.method == EAP_TYPE_NONE)
605 eap_ttls_phase2_select_eap_method(data, method);
607 if (method != data->phase2_eap_type.method || method == EAP_TYPE_NONE)
609 if (eap_peer_tls_phase2_nak(data->phase2_eap_types,
610 data->num_phase2_eap_types,
616 if (data->phase2_priv == NULL) {
617 data->phase2_method = eap_peer_get_eap_method(
618 EAP_VENDOR_IETF, method);
619 if (data->phase2_method) {
621 data->phase2_priv = data->phase2_method->init(sm);
625 if (data->phase2_priv == NULL || data->phase2_method == NULL) {
626 wpa_printf(MSG_INFO, "EAP-TTLS: failed to initialize "
627 "Phase 2 EAP method %d", method);
631 return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len, resp);
635 static int eap_ttls_phase2_request_eap(struct eap_sm *sm,
636 struct eap_ttls_data *data,
637 struct eap_method_ret *ret,
639 struct wpabuf **resp)
641 size_t len = be_to_host16(hdr->length);
643 struct eap_peer_config *config = eap_get_config(sm);
645 if (len <= sizeof(struct eap_hdr)) {
646 wpa_printf(MSG_INFO, "EAP-TTLS: too short "
647 "Phase 2 request (len=%lu)", (unsigned long) len);
650 pos = (u8 *) (hdr + 1);
651 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP Request: type=%d", *pos);
653 case EAP_TYPE_IDENTITY:
654 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
657 if (eap_ttls_phase2_request_eap_method(sm, data, ret, hdr, len,
664 (config->pending_req_identity || config->pending_req_password ||
665 config->pending_req_otp)) {
672 wpa_hexdump_buf(MSG_DEBUG, "EAP-TTLS: AVP encapsulate EAP Response",
674 return eap_ttls_avp_encapsulate(resp, RADIUS_ATTR_EAP_MESSAGE, 1);
678 static void eap_ttlsv1_permute_inner(struct eap_sm *sm,
679 struct eap_ttls_data *data)
681 #if EAP_TTLS_VERSION > 0
682 u8 session_key[2 * MSCHAPV2_KEY_LEN];
684 if (data->ttls_version == 0)
687 get_asymetric_start_key(data->master_key, session_key,
688 MSCHAPV2_KEY_LEN, 0, 0);
689 get_asymetric_start_key(data->master_key,
690 session_key + MSCHAPV2_KEY_LEN,
691 MSCHAPV2_KEY_LEN, 1, 0);
692 eap_ttls_ia_permute_inner_secret(sm, data, session_key,
693 sizeof(session_key));
694 #endif /* EAP_TTLS_VERSION */
698 static int eap_ttls_phase2_request_mschapv2(struct eap_sm *sm,
699 struct eap_ttls_data *data,
700 struct eap_method_ret *ret,
701 struct wpabuf **resp)
704 u8 *buf, *pos, *challenge, *peer_challenge;
705 const u8 *identity, *password;
706 size_t identity_len, password_len;
709 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAPV2 Request");
711 identity = eap_get_config_identity(sm, &identity_len);
712 password = eap_get_config_password2(sm, &password_len, &pwhash);
713 if (identity == NULL || password == NULL)
716 msg = wpabuf_alloc(identity_len + 1000);
718 wpa_printf(MSG_ERROR,
719 "EAP-TTLS/MSCHAPV2: Failed to allocate memory");
722 pos = buf = wpabuf_mhead(msg);
725 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
726 identity, identity_len);
728 /* MS-CHAP-Challenge */
729 challenge = eap_ttls_implicit_challenge(
730 sm, data, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN + 1);
731 if (challenge == NULL) {
733 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
734 "implicit challenge");
737 peer_challenge = challenge + 1 + EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
739 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
740 RADIUS_VENDOR_ID_MICROSOFT, 1,
741 challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
743 /* MS-CHAP2-Response */
744 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP2_RESPONSE,
745 RADIUS_VENDOR_ID_MICROSOFT, 1,
746 EAP_TTLS_MSCHAPV2_RESPONSE_LEN);
747 data->ident = challenge[EAP_TTLS_MSCHAPV2_CHALLENGE_LEN];
748 *pos++ = data->ident;
749 *pos++ = 0; /* Flags */
750 os_memcpy(pos, peer_challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
751 pos += EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
752 os_memset(pos, 0, 8); /* Reserved, must be zero */
754 if (mschapv2_derive_response(identity, identity_len, password,
755 password_len, pwhash, challenge,
756 peer_challenge, pos, data->auth_response,
759 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
763 data->auth_response_valid = 1;
765 eap_ttlsv1_permute_inner(sm, data);
771 wpabuf_put(msg, pos - buf);
774 if (sm->workaround && data->ttls_version == 0) {
775 /* At least FreeRADIUS seems to be terminating
776 * EAP-TTLS/MSHCAPV2 without the expected MS-CHAP-v2 Success
778 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: EAP workaround - "
779 "allow success without tunneled response");
780 ret->methodState = METHOD_MAY_CONT;
781 ret->decision = DECISION_COND_SUCC;
788 static int eap_ttls_phase2_request_mschap(struct eap_sm *sm,
789 struct eap_ttls_data *data,
790 struct eap_method_ret *ret,
791 struct wpabuf **resp)
794 u8 *buf, *pos, *challenge;
795 const u8 *identity, *password;
796 size_t identity_len, password_len;
799 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAP Request");
801 identity = eap_get_config_identity(sm, &identity_len);
802 password = eap_get_config_password2(sm, &password_len, &pwhash);
803 if (identity == NULL || password == NULL)
806 msg = wpabuf_alloc(identity_len + 1000);
808 wpa_printf(MSG_ERROR,
809 "EAP-TTLS/MSCHAP: Failed to allocate memory");
812 pos = buf = wpabuf_mhead(msg);
815 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
816 identity, identity_len);
818 /* MS-CHAP-Challenge */
819 challenge = eap_ttls_implicit_challenge(
820 sm, data, EAP_TTLS_MSCHAP_CHALLENGE_LEN + 1);
821 if (challenge == NULL) {
823 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAP: Failed to derive "
824 "implicit challenge");
828 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
829 RADIUS_VENDOR_ID_MICROSOFT, 1,
830 challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
832 /* MS-CHAP-Response */
833 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP_RESPONSE,
834 RADIUS_VENDOR_ID_MICROSOFT, 1,
835 EAP_TTLS_MSCHAP_RESPONSE_LEN);
836 data->ident = challenge[EAP_TTLS_MSCHAP_CHALLENGE_LEN];
837 *pos++ = data->ident;
838 *pos++ = 1; /* Flags: Use NT style passwords */
839 os_memset(pos, 0, 24); /* LM-Response */
842 challenge_response(challenge, password, pos); /* NT-Response */
843 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password hash",
846 nt_challenge_response(challenge, password, password_len,
847 pos); /* NT-Response */
848 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password",
849 password, password_len);
851 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP implicit challenge",
852 challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
853 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP response", pos, 24);
858 wpabuf_put(msg, pos - buf);
861 if (data->ttls_version > 0) {
862 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
863 * so do not allow connection to be terminated yet. */
864 ret->methodState = METHOD_CONT;
865 ret->decision = DECISION_COND_SUCC;
867 /* EAP-TTLS/MSCHAP does not provide tunneled success
868 * notification, so assume that Phase2 succeeds. */
869 ret->methodState = METHOD_DONE;
870 ret->decision = DECISION_COND_SUCC;
877 static int eap_ttls_phase2_request_pap(struct eap_sm *sm,
878 struct eap_ttls_data *data,
879 struct eap_method_ret *ret,
880 struct wpabuf **resp)
885 const u8 *identity, *password;
886 size_t identity_len, password_len;
888 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 PAP 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 + password_len + 100);
897 wpa_printf(MSG_ERROR,
898 "EAP-TTLS/PAP: 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);
907 /* User-Password; in RADIUS, this is encrypted, but EAP-TTLS encrypts
908 * the data, so no separate encryption is used in the AVP itself.
909 * However, the password is padded to obfuscate its length. */
910 pad = password_len == 0 ? 16 : (16 - (password_len & 15)) & 15;
911 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_USER_PASSWORD, 0, 1,
913 os_memcpy(pos, password, password_len);
915 os_memset(pos, 0, pad);
919 wpabuf_put(msg, pos - buf);
922 if (data->ttls_version > 0) {
923 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
924 * so do not allow connection to be terminated yet. */
925 ret->methodState = METHOD_CONT;
926 ret->decision = DECISION_COND_SUCC;
928 /* EAP-TTLS/PAP does not provide tunneled success notification,
929 * so assume that Phase2 succeeds. */
930 ret->methodState = METHOD_DONE;
931 ret->decision = DECISION_COND_SUCC;
938 static int eap_ttls_phase2_request_chap(struct eap_sm *sm,
939 struct eap_ttls_data *data,
940 struct eap_method_ret *ret,
941 struct wpabuf **resp)
944 u8 *buf, *pos, *challenge;
945 const u8 *identity, *password;
946 size_t identity_len, password_len;
948 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 CHAP Request");
950 identity = eap_get_config_identity(sm, &identity_len);
951 password = eap_get_config_password(sm, &password_len);
952 if (identity == NULL || password == NULL)
955 msg = wpabuf_alloc(identity_len + 1000);
957 wpa_printf(MSG_ERROR,
958 "EAP-TTLS/CHAP: Failed to allocate memory");
961 pos = buf = wpabuf_mhead(msg);
964 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
965 identity, identity_len);
968 challenge = eap_ttls_implicit_challenge(
969 sm, data, EAP_TTLS_CHAP_CHALLENGE_LEN + 1);
970 if (challenge == NULL) {
972 wpa_printf(MSG_ERROR, "EAP-TTLS/CHAP: Failed to derive "
973 "implicit challenge");
977 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_CHAP_CHALLENGE, 0, 1,
978 challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
981 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_CHAP_PASSWORD, 0, 1,
982 1 + EAP_TTLS_CHAP_PASSWORD_LEN);
983 data->ident = challenge[EAP_TTLS_CHAP_CHALLENGE_LEN];
984 *pos++ = data->ident;
986 /* MD5(Ident + Password + Challenge) */
987 chap_md5(data->ident, password, password_len, challenge,
988 EAP_TTLS_CHAP_CHALLENGE_LEN, pos);
990 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: CHAP username",
991 identity, identity_len);
992 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: CHAP password",
993 password, password_len);
994 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP implicit challenge",
995 challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
996 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP password",
997 pos, EAP_TTLS_CHAP_PASSWORD_LEN);
998 pos += EAP_TTLS_CHAP_PASSWORD_LEN;
1002 wpabuf_put(msg, pos - buf);
1005 if (data->ttls_version > 0) {
1006 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
1007 * so do not allow connection to be terminated yet. */
1008 ret->methodState = METHOD_CONT;
1009 ret->decision = DECISION_COND_SUCC;
1011 /* EAP-TTLS/CHAP does not provide tunneled success
1012 * notification, so assume that Phase2 succeeds. */
1013 ret->methodState = METHOD_DONE;
1014 ret->decision = DECISION_COND_SUCC;
1021 static int eap_ttls_phase2_request(struct eap_sm *sm,
1022 struct eap_ttls_data *data,
1023 struct eap_method_ret *ret,
1024 struct eap_hdr *hdr,
1025 struct wpabuf **resp)
1029 enum phase2_types phase2_type = data->phase2_type;
1032 if (data->tnc_started) {
1033 wpa_printf(MSG_DEBUG, "EAP-TTLS: Processing TNC");
1034 phase2_type = EAP_TTLS_PHASE2_EAP;
1036 #endif /* EAP_TNC */
1038 if (phase2_type == EAP_TTLS_PHASE2_MSCHAPV2 ||
1039 phase2_type == EAP_TTLS_PHASE2_MSCHAP ||
1040 phase2_type == EAP_TTLS_PHASE2_PAP ||
1041 phase2_type == EAP_TTLS_PHASE2_CHAP) {
1042 if (eap_get_config_identity(sm, &len) == NULL) {
1043 wpa_printf(MSG_INFO,
1044 "EAP-TTLS: Identity not configured");
1045 eap_sm_request_identity(sm);
1046 if (eap_get_config_password(sm, &len) == NULL)
1047 eap_sm_request_password(sm);
1051 if (eap_get_config_password(sm, &len) == NULL) {
1052 wpa_printf(MSG_INFO,
1053 "EAP-TTLS: Password not configured");
1054 eap_sm_request_password(sm);
1059 switch (phase2_type) {
1060 case EAP_TTLS_PHASE2_EAP:
1061 res = eap_ttls_phase2_request_eap(sm, data, ret, hdr, resp);
1063 case EAP_TTLS_PHASE2_MSCHAPV2:
1064 res = eap_ttls_phase2_request_mschapv2(sm, data, ret, resp);
1066 case EAP_TTLS_PHASE2_MSCHAP:
1067 res = eap_ttls_phase2_request_mschap(sm, data, ret, resp);
1069 case EAP_TTLS_PHASE2_PAP:
1070 res = eap_ttls_phase2_request_pap(sm, data, ret, resp);
1072 case EAP_TTLS_PHASE2_CHAP:
1073 res = eap_ttls_phase2_request_chap(sm, data, ret, resp);
1076 wpa_printf(MSG_ERROR, "EAP-TTLS: Phase 2 - Unknown");
1082 ret->methodState = METHOD_DONE;
1083 ret->decision = DECISION_FAIL;
1090 #if EAP_TTLS_VERSION > 0
1091 static struct wpabuf * eap_ttls_build_phase_finished(
1092 struct eap_sm *sm, struct eap_ttls_data *data, int id, int final)
1094 struct wpabuf *req, *buf;
1096 buf = tls_connection_ia_send_phase_finished(sm->ssl_ctx,
1102 req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TTLS,
1103 1 + wpabuf_len(buf),
1104 EAP_CODE_RESPONSE, id);
1110 wpabuf_put_u8(req, data->ttls_version);
1111 wpabuf_put_buf(req, buf);
1113 eap_update_len(req);
1117 #endif /* EAP_TTLS_VERSION */
1120 struct ttls_parse_avp {
1130 static int eap_ttls_parse_attr_eap(const u8 *dpos, size_t dlen,
1131 struct ttls_parse_avp *parse)
1133 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - EAP Message");
1134 if (parse->eapdata == NULL) {
1135 parse->eapdata = os_malloc(dlen);
1136 if (parse->eapdata == NULL) {
1137 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
1138 "memory for Phase 2 EAP data");
1141 os_memcpy(parse->eapdata, dpos, dlen);
1142 parse->eap_len = dlen;
1144 u8 *neweap = os_realloc(parse->eapdata, parse->eap_len + dlen);
1145 if (neweap == NULL) {
1146 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
1147 "memory for Phase 2 EAP data");
1150 os_memcpy(neweap + parse->eap_len, dpos, dlen);
1151 parse->eapdata = neweap;
1152 parse->eap_len += dlen;
1159 static int eap_ttls_parse_attr_chbind(const u8 *dpos, size_t dlen,
1160 struct ttls_parse_avp *parse)
1162 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - Channel Binding Message");
1164 if (parse->chbind_data == NULL) {
1165 parse->chbind_data = os_malloc(dlen);
1166 if (parse->chbind_data == NULL) {
1167 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
1168 "memory for Phase 2 channel binding data");
1171 os_memcpy(parse->chbind_data, dpos, dlen);
1172 parse->chbind_len = dlen;
1174 /* TODO: can this really happen? maybe just make this an error? */
1175 u8 *newchbind = os_realloc(parse->chbind_data,
1176 parse->chbind_len + dlen);
1177 if (newchbind == NULL) {
1178 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
1179 "memory for Phase 2 channel binding data");
1182 os_memcpy(newchbind + parse->chbind_len, dpos, dlen);
1183 parse->chbind_data = newchbind;
1184 parse->chbind_len += dlen;
1191 static int eap_ttls_parse_avp(u8 *pos, size_t left,
1192 struct ttls_parse_avp *parse)
1194 struct ttls_avp *avp;
1195 u32 avp_code, avp_length, vendor_id = 0;
1196 u8 avp_flags, *dpos;
1199 avp = (struct ttls_avp *) pos;
1200 avp_code = be_to_host32(avp->avp_code);
1201 avp_length = be_to_host32(avp->avp_length);
1202 avp_flags = (avp_length >> 24) & 0xff;
1203 avp_length &= 0xffffff;
1204 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP: code=%d flags=0x%02x "
1205 "length=%d", (int) avp_code, avp_flags,
1208 if (avp_length > left) {
1209 wpa_printf(MSG_WARNING, "EAP-TTLS: AVP overflow "
1210 "(len=%d, left=%lu) - dropped",
1211 (int) avp_length, (unsigned long) left);
1215 if (avp_length < sizeof(*avp)) {
1216 wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid AVP length %d",
1221 dpos = (u8 *) (avp + 1);
1222 dlen = avp_length - sizeof(*avp);
1223 if (avp_flags & AVP_FLAGS_VENDOR) {
1225 wpa_printf(MSG_WARNING, "EAP-TTLS: Vendor AVP "
1229 vendor_id = WPA_GET_BE32(dpos);
1230 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP vendor_id %d",
1236 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP data", dpos, dlen);
1238 if (vendor_id == 0 && avp_code == RADIUS_ATTR_EAP_MESSAGE) {
1239 if (eap_ttls_parse_attr_eap(dpos, dlen, parse) < 0)
1241 } else if (vendor_id == RADIUS_VENDOR_ID_UKERNA &&
1242 avp_code == RADIUS_ATTR_UKERNA_CHBIND) {
1243 /* message containing channel binding data */
1244 if (eap_ttls_parse_attr_chbind(dpos, dlen, parse) < 0)
1246 } else if (vendor_id == 0 && avp_code == RADIUS_ATTR_REPLY_MESSAGE) {
1247 /* This is an optional message that can be displayed to
1249 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: AVP - Reply-Message",
1251 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
1252 avp_code == RADIUS_ATTR_MS_CHAP2_SUCCESS) {
1253 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP2-Success",
1256 wpa_printf(MSG_WARNING, "EAP-TTLS: Unexpected "
1257 "MS-CHAP2-Success length "
1258 "(len=%lu, expected 43)",
1259 (unsigned long) dlen);
1262 parse->mschapv2 = dpos;
1263 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
1264 avp_code == RADIUS_ATTR_MS_CHAP_ERROR) {
1265 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP-Error",
1267 parse->mschapv2_error = 1;
1268 } else if (avp_flags & AVP_FLAGS_MANDATORY) {
1269 wpa_printf(MSG_WARNING, "EAP-TTLS: Unsupported mandatory AVP "
1270 "code %d vendor_id %d - dropped",
1271 (int) avp_code, (int) vendor_id);
1274 wpa_printf(MSG_DEBUG, "EAP-TTLS: Ignoring unsupported AVP "
1275 "code %d vendor_id %d",
1276 (int) avp_code, (int) vendor_id);
1283 static int eap_ttls_parse_avps(struct wpabuf *in_decrypted,
1284 struct ttls_parse_avp *parse)
1290 pos = wpabuf_mhead(in_decrypted);
1291 left = wpabuf_len(in_decrypted);
1292 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Decrypted Phase 2 AVPs", pos, left);
1293 if (left < sizeof(struct ttls_avp)) {
1294 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 AVP frame"
1295 " len=%lu expected %lu or more - dropped",
1296 (unsigned long) left,
1297 (unsigned long) sizeof(struct ttls_avp));
1302 os_memset(parse, 0, sizeof(*parse));
1305 avp_length = eap_ttls_parse_avp(pos, left, parse);
1309 pad = (4 - (avp_length & 3)) & 3;
1310 pos += avp_length + pad;
1311 if (left < avp_length + pad)
1314 left -= avp_length + pad;
1321 static u8 * eap_ttls_fake_identity_request(void)
1323 struct eap_hdr *hdr;
1326 wpa_printf(MSG_DEBUG, "EAP-TTLS: empty data in beginning of "
1327 "Phase 2 - use fake EAP-Request Identity");
1328 buf = os_malloc(sizeof(*hdr) + 1);
1330 wpa_printf(MSG_WARNING, "EAP-TTLS: failed to allocate "
1331 "memory for fake EAP-Identity Request");
1335 hdr = (struct eap_hdr *) buf;
1336 hdr->code = EAP_CODE_REQUEST;
1337 hdr->identifier = 0;
1338 hdr->length = host_to_be16(sizeof(*hdr) + 1);
1339 buf[sizeof(*hdr)] = EAP_TYPE_IDENTITY;
1345 static int eap_ttls_encrypt_response(struct eap_sm *sm,
1346 struct eap_ttls_data *data,
1347 struct wpabuf *resp, u8 identifier,
1348 struct wpabuf **out_data)
1353 wpa_hexdump_buf_key(MSG_DEBUG, "EAP-TTLS: Encrypting Phase 2 data",
1355 if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
1356 data->ttls_version, identifier,
1358 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to encrypt a Phase 2 "
1367 static int eap_ttls_add_chbind_request(struct eap_sm *sm,
1368 struct eap_ttls_data *data,
1369 struct wpabuf **resp)
1371 struct wpabuf *chbind_req, *res;
1373 struct eap_peer_config *config = eap_get_config(sm);
1375 if (!config->chbind_config || config->chbind_config_len <= 0)
1378 for (i=0; i<config->chbind_config_len; i++) {
1379 length += 3 + config->chbind_config[i].req_data_len;
1382 chbind_req = wpabuf_alloc(length);
1386 wpabuf_put_u8(chbind_req, CHBIND_CODE_REQUEST);
1387 for (i=0; i<config->chbind_config_len; i++) {
1388 struct eap_peer_chbind_config *chbind_config =
1389 &config->chbind_config[i];
1390 wpabuf_put_be16(chbind_req, chbind_config->req_data_len);
1391 wpabuf_put_u8(chbind_req, chbind_config->nsid);
1392 wpabuf_put_data(chbind_req, chbind_config->req_data,
1393 chbind_config->req_data_len);
1395 if (eap_ttls_avp_radius_vsa_encapsulate(&chbind_req,
1396 RADIUS_VENDOR_ID_UKERNA,
1397 RADIUS_ATTR_UKERNA_CHBIND, 0) < 0)
1400 /* bleh. This will free *resp regardless of whether combined buffer
1401 alloc succeeds, which is not consistent with the other error
1402 condition behavior in this function */
1403 *resp = wpabuf_concat(chbind_req, *resp);
1405 return (*resp) ? 0 : -1;
1409 static int eap_ttls_process_chbind(struct eap_sm *sm,
1410 struct eap_ttls_data *data,
1411 struct eap_method_ret *ret,
1412 struct ttls_parse_avp *parse,
1413 struct wpabuf **resp)
1418 struct chbind_hdr *hdr;
1419 struct eap_peer_config *config = eap_get_config(sm);
1421 if (parse->chbind_data == NULL) {
1422 wpa_printf(MSG_WARNING, "EAP-TTLS: No channel binding message "
1423 "in the packet - dropped");
1426 if (parse->chbind_len < 1 + sizeof(*hdr)) {
1427 wpa_printf(MSG_WARNING, "EAP-TTLS: bad channel binding response "
1428 "frame (len=%lu, expected %lu or more) - dropped",
1429 (unsigned long) parse->chbind_len,
1430 (unsigned long) sizeof(*hdr));
1433 code = parse->chbind_data[pos++];
1434 while (pos+sizeof(*hdr) < parse->chbind_len) {
1435 hdr = (struct chbind_hdr *)(&parse->chbind_data[pos]);
1436 pos += sizeof(*hdr);
1437 len = be_to_host16(hdr->len);
1438 if (pos + len <= parse->chbind_len) {
1440 for (i=0; i<config->chbind_config_len; i++) {
1441 struct eap_peer_chbind_config *chbind_config =
1442 &config->chbind_config[i];
1443 if (chbind_config->nsid == hdr->nsid)
1444 chbind_config->response_cb(
1447 &parse->chbind_data[pos], len);
1452 if (pos != parse->chbind_len) {
1453 wpa_printf(MSG_WARNING, "EAP-TTLS: bad channel binding response "
1454 "frame (parsed len=%lu, expected %lu) - dropped",
1455 (unsigned long) pos,
1456 (unsigned long) parse->chbind_len);
1462 static int eap_ttls_process_phase2_eap(struct eap_sm *sm,
1463 struct eap_ttls_data *data,
1464 struct eap_method_ret *ret,
1465 struct ttls_parse_avp *parse,
1466 struct wpabuf **resp)
1468 struct eap_hdr *hdr;
1471 if (parse->eapdata == NULL) {
1472 wpa_printf(MSG_WARNING, "EAP-TTLS: No EAP Message in the "
1473 "packet - dropped");
1477 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP",
1478 parse->eapdata, parse->eap_len);
1479 hdr = (struct eap_hdr *) parse->eapdata;
1481 if (parse->eap_len < sizeof(*hdr)) {
1482 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 EAP "
1483 "frame (len=%lu, expected %lu or more) - dropped",
1484 (unsigned long) parse->eap_len,
1485 (unsigned long) sizeof(*hdr));
1488 len = be_to_host16(hdr->length);
1489 if (len > parse->eap_len) {
1490 wpa_printf(MSG_INFO, "EAP-TTLS: Length mismatch in Phase 2 "
1491 "EAP frame (EAP hdr len=%lu, EAP data len in "
1493 (unsigned long) len,
1494 (unsigned long) parse->eap_len);
1497 wpa_printf(MSG_DEBUG, "EAP-TTLS: received Phase 2: code=%d "
1498 "identifier=%d length=%lu",
1499 hdr->code, hdr->identifier, (unsigned long) len);
1500 switch (hdr->code) {
1501 case EAP_CODE_REQUEST:
1502 if (eap_ttls_phase2_request(sm, data, ret, hdr, resp)) {
1503 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
1504 "processing failed");
1509 wpa_printf(MSG_INFO, "EAP-TTLS: Unexpected code=%d in "
1510 "Phase 2 EAP header", hdr->code);
1518 static int eap_ttls_process_phase2_mschapv2(struct eap_sm *sm,
1519 struct eap_ttls_data *data,
1520 struct eap_method_ret *ret,
1521 struct ttls_parse_avp *parse)
1523 if (parse->mschapv2_error) {
1524 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Received "
1525 "MS-CHAP-Error - failed");
1526 ret->methodState = METHOD_DONE;
1527 ret->decision = DECISION_FAIL;
1528 /* Reply with empty data to ACK error */
1532 if (parse->mschapv2 == NULL) {
1534 if (data->phase2_success && parse->eapdata) {
1536 * Allow EAP-TNC to be started after successfully
1537 * completed MSCHAPV2.
1541 #endif /* EAP_TNC */
1542 wpa_printf(MSG_WARNING, "EAP-TTLS: no MS-CHAP2-Success AVP "
1543 "received for Phase2 MSCHAPV2");
1546 if (parse->mschapv2[0] != data->ident) {
1547 wpa_printf(MSG_WARNING, "EAP-TTLS: Ident mismatch for Phase 2 "
1548 "MSCHAPV2 (received Ident 0x%02x, expected 0x%02x)",
1549 parse->mschapv2[0], data->ident);
1552 if (!data->auth_response_valid ||
1553 mschapv2_verify_auth_response(data->auth_response,
1554 parse->mschapv2 + 1, 42)) {
1555 wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid authenticator "
1556 "response in Phase 2 MSCHAPV2 success request");
1560 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 MSCHAPV2 "
1561 "authentication succeeded");
1562 if (data->ttls_version > 0) {
1564 * EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report
1565 * success, so do not allow connection to be terminated
1568 ret->methodState = METHOD_CONT;
1569 ret->decision = DECISION_COND_SUCC;
1571 ret->methodState = METHOD_DONE;
1572 ret->decision = DECISION_UNCOND_SUCC;
1573 data->phase2_success = 1;
1577 * Reply with empty data; authentication server will reply
1578 * with EAP-Success after this.
1585 static int eap_ttls_process_tnc_start(struct eap_sm *sm,
1586 struct eap_ttls_data *data,
1587 struct eap_method_ret *ret,
1588 struct ttls_parse_avp *parse,
1589 struct wpabuf **resp)
1591 /* TNC uses inner EAP method after non-EAP TTLS phase 2. */
1592 if (parse->eapdata == NULL) {
1593 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
1594 "unexpected tunneled data (no EAP)");
1598 if (!data->ready_for_tnc) {
1599 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
1600 "EAP after non-EAP, but not ready for TNC");
1604 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
1606 data->tnc_started = 1;
1608 if (eap_ttls_process_phase2_eap(sm, data, ret, parse, resp) < 0)
1613 #endif /* EAP_TNC */
1616 static int eap_ttls_process_decrypted(struct eap_sm *sm,
1617 struct eap_ttls_data *data,
1618 struct eap_method_ret *ret,
1620 struct ttls_parse_avp *parse,
1621 struct wpabuf *in_decrypted,
1622 struct wpabuf **out_data)
1624 struct wpabuf *resp = NULL;
1625 struct eap_peer_config *config = eap_get_config(sm);
1627 enum phase2_types phase2_type = data->phase2_type;
1630 if (data->tnc_started)
1631 phase2_type = EAP_TTLS_PHASE2_EAP;
1632 #endif /* EAP_TNC */
1634 switch (phase2_type) {
1635 case EAP_TTLS_PHASE2_EAP:
1636 if (eap_ttls_process_phase2_eap(sm, data, ret, parse, &resp) <
1640 case EAP_TTLS_PHASE2_MSCHAPV2:
1641 res = eap_ttls_process_phase2_mschapv2(sm, data, ret, parse);
1643 if (res == 1 && parse->eapdata && data->phase2_success) {
1645 * TNC may be required as the next
1646 * authentication method within the tunnel.
1648 ret->methodState = METHOD_MAY_CONT;
1649 data->ready_for_tnc = 1;
1650 if (eap_ttls_process_tnc_start(sm, data, ret, parse,
1654 #endif /* EAP_TNC */
1656 case EAP_TTLS_PHASE2_MSCHAP:
1657 case EAP_TTLS_PHASE2_PAP:
1658 case EAP_TTLS_PHASE2_CHAP:
1660 if (eap_ttls_process_tnc_start(sm, data, ret, parse, &resp) <
1665 /* EAP-TTLS/{MSCHAP,PAP,CHAP} should not send any TLS tunneled
1666 * requests to the supplicant */
1667 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received unexpected "
1670 #endif /* EAP_TNC */
1673 if (!resp && (config->pending_req_identity ||
1674 config->pending_req_password ||
1675 config->pending_req_otp ||
1676 config->pending_req_new_password)) {
1677 wpabuf_free(data->pending_phase2_req);
1678 data->pending_phase2_req = wpabuf_dup(in_decrypted);
1682 /* handle channel binding response here */
1683 if (parse->chbind_data) {
1684 /* received channel binding repsonse */
1685 if (eap_ttls_process_chbind(sm, data, ret, parse, &resp) < 0)
1688 /* issue channel binding request when appropriate */
1689 if (config->chbind_config && config->chbind_config_len > 0 &&
1690 !data->chbind_req_sent) {
1691 if (eap_ttls_add_chbind_request(sm, data, &resp) < 0)
1693 data->chbind_req_sent = 1;
1697 if (eap_ttls_encrypt_response(sm, data, resp, identifier,
1706 #if EAP_TTLS_VERSION > 0
1707 static void eap_ttls_final_phase_finished(struct eap_sm *sm,
1708 struct eap_ttls_data *data,
1709 struct eap_method_ret *ret,
1711 struct wpabuf **out_data)
1713 wpa_printf(MSG_DEBUG, "EAP-TTLS: FinalPhaseFinished received");
1714 wpa_printf(MSG_INFO, "EAP-TTLS: TLS/IA authentication succeeded");
1715 ret->methodState = METHOD_DONE;
1716 ret->decision = DECISION_UNCOND_SUCC;
1717 data->phase2_success = 1;
1718 *out_data = eap_ttls_build_phase_finished(sm, data, identifier, 1);
1719 eap_ttls_v1_derive_key(sm, data);
1721 #endif /* EAP_TTLS_VERSION */
1724 static int eap_ttls_implicit_identity_request(struct eap_sm *sm,
1725 struct eap_ttls_data *data,
1726 struct eap_method_ret *ret,
1728 struct wpabuf **out_data)
1731 struct eap_hdr *hdr;
1732 struct wpabuf *resp;
1734 hdr = (struct eap_hdr *) eap_ttls_fake_identity_request();
1736 ret->methodState = METHOD_DONE;
1737 ret->decision = DECISION_FAIL;
1742 if (eap_ttls_phase2_request(sm, data, ret, hdr, &resp)) {
1743 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
1744 "processing failed");
1747 retval = eap_ttls_encrypt_response(sm, data, resp, identifier,
1754 ret->methodState = METHOD_DONE;
1755 ret->decision = DECISION_FAIL;
1762 static int eap_ttls_phase2_start(struct eap_sm *sm, struct eap_ttls_data *data,
1763 struct eap_method_ret *ret, u8 identifier,
1764 struct wpabuf **out_data)
1766 data->phase2_start = 0;
1769 * EAP-TTLS does not use Phase2 on fast re-auth; this must be done only
1770 * if TLS part was indeed resuming a previous session. Most
1771 * Authentication Servers terminate EAP-TTLS before reaching this
1772 * point, but some do not. Make wpa_supplicant stop phase 2 here, if
1776 tls_connection_resumed(sm->ssl_ctx, data->ssl.conn)) {
1777 wpa_printf(MSG_DEBUG, "EAP-TTLS: Session resumption - "
1779 *out_data = eap_peer_tls_build_ack(identifier, EAP_TYPE_TTLS,
1780 data->ttls_version);
1781 ret->methodState = METHOD_DONE;
1782 ret->decision = DECISION_UNCOND_SUCC;
1783 data->phase2_success = 1;
1787 return eap_ttls_implicit_identity_request(sm, data, ret, identifier,
1792 static int eap_ttls_decrypt(struct eap_sm *sm, struct eap_ttls_data *data,
1793 struct eap_method_ret *ret, u8 identifier,
1794 const struct wpabuf *in_data,
1795 struct wpabuf **out_data)
1797 struct wpabuf *in_decrypted = NULL;
1799 struct ttls_parse_avp parse;
1801 os_memset(&parse, 0, sizeof(parse));
1803 wpa_printf(MSG_DEBUG, "EAP-TTLS: received %lu bytes encrypted data for"
1805 in_data ? (unsigned long) wpabuf_len(in_data) : 0);
1807 if (data->pending_phase2_req) {
1808 wpa_printf(MSG_DEBUG, "EAP-TTLS: Pending Phase 2 request - "
1809 "skip decryption and use old data");
1810 /* Clear TLS reassembly state. */
1811 eap_peer_tls_reset_input(&data->ssl);
1813 in_decrypted = data->pending_phase2_req;
1814 data->pending_phase2_req = NULL;
1815 if (wpabuf_len(in_decrypted) == 0) {
1816 wpabuf_free(in_decrypted);
1817 return eap_ttls_implicit_identity_request(
1818 sm, data, ret, identifier, out_data);
1823 if ((in_data == NULL || wpabuf_len(in_data) == 0) &&
1824 data->phase2_start) {
1825 return eap_ttls_phase2_start(sm, data, ret, identifier,
1829 if (in_data == NULL || wpabuf_len(in_data) == 0) {
1830 /* Received TLS ACK - requesting more fragments */
1831 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
1833 identifier, NULL, out_data);
1836 retval = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
1840 #if EAP_TTLS_VERSION > 0
1841 if (data->ttls_version > 0 &&
1842 (in_decrypted == NULL || wpabuf_len(in_decrypted) == 0) &&
1843 tls_connection_ia_final_phase_finished(sm->ssl_ctx,
1845 eap_ttls_final_phase_finished(sm, data, ret, identifier,
1849 #endif /* EAP_TTLS_VERSION */
1852 data->phase2_start = 0;
1854 if (eap_ttls_parse_avps(in_decrypted, &parse) < 0) {
1859 retval = eap_ttls_process_decrypted(sm, data, ret, identifier,
1860 &parse, in_decrypted, out_data);
1863 wpabuf_free(in_decrypted);
1864 os_free(parse.eapdata);
1867 ret->methodState = METHOD_DONE;
1868 ret->decision = DECISION_FAIL;
1875 static int eap_ttls_process_start(struct eap_sm *sm,
1876 struct eap_ttls_data *data, u8 flags,
1877 struct eap_method_ret *ret)
1879 struct eap_peer_config *config = eap_get_config(sm);
1881 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start (server ver=%d, own ver=%d)",
1882 flags & EAP_TLS_VERSION_MASK, data->ttls_version);
1883 #if EAP_TTLS_VERSION > 0
1884 if ((flags & EAP_TLS_VERSION_MASK) < data->ttls_version)
1885 data->ttls_version = flags & EAP_TLS_VERSION_MASK;
1886 if (data->force_ttls_version >= 0 &&
1887 data->force_ttls_version != data->ttls_version) {
1888 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to select "
1889 "forced TTLS version %d",
1890 data->force_ttls_version);
1891 ret->methodState = METHOD_DONE;
1892 ret->decision = DECISION_FAIL;
1893 ret->allowNotifications = FALSE;
1896 wpa_printf(MSG_DEBUG, "EAP-TTLS: Using TTLS version %d",
1897 data->ttls_version);
1899 if (data->ttls_version > 0)
1900 data->ssl.tls_ia = 1;
1901 #endif /* EAP_TTLS_VERSION */
1902 if (!data->ssl_initialized &&
1903 eap_peer_tls_ssl_init(sm, &data->ssl, config)) {
1904 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to initialize SSL.");
1907 data->ssl_initialized = 1;
1909 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start");
1915 static int eap_ttls_process_handshake(struct eap_sm *sm,
1916 struct eap_ttls_data *data,
1917 struct eap_method_ret *ret,
1919 const u8 *in_data, size_t in_len,
1920 struct wpabuf **out_data)
1924 res = eap_peer_tls_process_helper(sm, &data->ssl, EAP_TYPE_TTLS,
1925 data->ttls_version, identifier,
1926 in_data, in_len, out_data);
1928 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1929 wpa_printf(MSG_DEBUG, "EAP-TTLS: TLS done, proceed to "
1931 if (data->resuming) {
1932 wpa_printf(MSG_DEBUG, "EAP-TTLS: fast reauth - may "
1934 ret->decision = DECISION_COND_SUCC;
1935 ret->methodState = METHOD_MAY_CONT;
1937 data->phase2_start = 1;
1938 if (data->ttls_version == 0)
1939 eap_ttls_v0_derive_key(sm, data);
1941 if (*out_data == NULL || wpabuf_len(*out_data) == 0) {
1942 if (eap_ttls_decrypt(sm, data, ret, identifier,
1944 wpa_printf(MSG_WARNING, "EAP-TTLS: "
1945 "failed to process early "
1946 "start for Phase 2");
1956 * Application data included in the handshake message.
1958 wpabuf_free(data->pending_phase2_req);
1959 data->pending_phase2_req = *out_data;
1961 wpabuf_set(&msg, in_data, in_len);
1962 res = eap_ttls_decrypt(sm, data, ret, identifier, &msg,
1970 static void eap_ttls_check_auth_status(struct eap_sm *sm,
1971 struct eap_ttls_data *data,
1972 struct eap_method_ret *ret)
1974 if (data->ttls_version == 0 && ret->methodState == METHOD_DONE) {
1975 ret->allowNotifications = FALSE;
1976 if (ret->decision == DECISION_UNCOND_SUCC ||
1977 ret->decision == DECISION_COND_SUCC) {
1978 wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1979 "completed successfully");
1980 data->phase2_success = 1;
1982 if (!data->ready_for_tnc && !data->tnc_started) {
1984 * TNC may be required as the next
1985 * authentication method within the tunnel.
1987 ret->methodState = METHOD_MAY_CONT;
1988 data->ready_for_tnc = 1;
1990 #endif /* EAP_TNC */
1992 } else if (data->ttls_version == 0 &&
1993 ret->methodState == METHOD_MAY_CONT &&
1994 (ret->decision == DECISION_UNCOND_SUCC ||
1995 ret->decision == DECISION_COND_SUCC)) {
1996 wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1997 "completed successfully (MAY_CONT)");
1998 data->phase2_success = 1;
2003 static struct wpabuf * eap_ttls_process(struct eap_sm *sm, void *priv,
2004 struct eap_method_ret *ret,
2005 const struct wpabuf *reqData)
2010 struct wpabuf *resp;
2012 struct eap_ttls_data *data = priv;
2014 pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_TTLS, ret,
2015 reqData, &left, &flags);
2018 id = eap_get_id(reqData);
2020 if (flags & EAP_TLS_FLAGS_START) {
2021 if (eap_ttls_process_start(sm, data, flags, ret) < 0)
2024 /* RFC 5281, Ch. 9.2:
2025 * "This packet MAY contain additional information in the form
2026 * of AVPs, which may provide useful hints to the client"
2027 * For now, ignore any potential extra data.
2030 } else if (!data->ssl_initialized) {
2031 wpa_printf(MSG_DEBUG, "EAP-TTLS: First message did not "
2032 "include Start flag");
2033 ret->methodState = METHOD_DONE;
2034 ret->decision = DECISION_FAIL;
2035 ret->allowNotifications = FALSE;
2040 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
2043 wpabuf_set(&msg, pos, left);
2044 res = eap_ttls_decrypt(sm, data, ret, id, &msg, &resp);
2046 res = eap_ttls_process_handshake(sm, data, ret, id,
2050 eap_ttls_check_auth_status(sm, data, ret);
2052 /* FIX: what about res == -1? Could just move all error processing into
2053 * the other functions and get rid of this res==1 case here. */
2056 return eap_peer_tls_build_ack(id, EAP_TYPE_TTLS,
2057 data->ttls_version);
2063 static Boolean eap_ttls_has_reauth_data(struct eap_sm *sm, void *priv)
2065 struct eap_ttls_data *data = priv;
2066 return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
2067 data->phase2_success;
2071 static void eap_ttls_deinit_for_reauth(struct eap_sm *sm, void *priv)
2073 struct eap_ttls_data *data = priv;
2074 wpabuf_free(data->pending_phase2_req);
2075 data->pending_phase2_req = NULL;
2077 data->ready_for_tnc = 0;
2078 data->tnc_started = 0;
2079 #endif /* EAP_TNC */
2083 static void * eap_ttls_init_for_reauth(struct eap_sm *sm, void *priv)
2085 struct eap_ttls_data *data = priv;
2086 os_free(data->key_data);
2087 data->key_data = NULL;
2088 if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
2092 if (data->phase2_priv && data->phase2_method &&
2093 data->phase2_method->init_for_reauth)
2094 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
2095 data->phase2_start = 0;
2096 data->phase2_success = 0;
2103 static int eap_ttls_get_status(struct eap_sm *sm, void *priv, char *buf,
2104 size_t buflen, int verbose)
2106 struct eap_ttls_data *data = priv;
2109 len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
2110 ret = os_snprintf(buf + len, buflen - len,
2111 "EAP-TTLSv%d Phase2 method=",
2112 data->ttls_version);
2113 if (ret < 0 || (size_t) ret >= buflen - len)
2116 switch (data->phase2_type) {
2117 case EAP_TTLS_PHASE2_EAP:
2118 ret = os_snprintf(buf + len, buflen - len, "EAP-%s\n",
2119 data->phase2_method ?
2120 data->phase2_method->name : "?");
2122 case EAP_TTLS_PHASE2_MSCHAPV2:
2123 ret = os_snprintf(buf + len, buflen - len, "MSCHAPV2\n");
2125 case EAP_TTLS_PHASE2_MSCHAP:
2126 ret = os_snprintf(buf + len, buflen - len, "MSCHAP\n");
2128 case EAP_TTLS_PHASE2_PAP:
2129 ret = os_snprintf(buf + len, buflen - len, "PAP\n");
2131 case EAP_TTLS_PHASE2_CHAP:
2132 ret = os_snprintf(buf + len, buflen - len, "CHAP\n");
2138 if (ret < 0 || (size_t) ret >= buflen - len)
2146 static Boolean eap_ttls_isKeyAvailable(struct eap_sm *sm, void *priv)
2148 struct eap_ttls_data *data = priv;
2149 return data->key_data != NULL && data->phase2_success;
2153 static u8 * eap_ttls_getKey(struct eap_sm *sm, void *priv, size_t *len)
2155 struct eap_ttls_data *data = priv;
2158 if (data->key_data == NULL || !data->phase2_success)
2161 key = os_malloc(EAP_TLS_KEY_LEN);
2165 *len = EAP_TLS_KEY_LEN;
2166 os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
2172 int eap_peer_ttls_register(void)
2174 struct eap_method *eap;
2177 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
2178 EAP_VENDOR_IETF, EAP_TYPE_TTLS, "TTLS");
2182 eap->init = eap_ttls_init;
2183 eap->deinit = eap_ttls_deinit;
2184 eap->process = eap_ttls_process;
2185 eap->isKeyAvailable = eap_ttls_isKeyAvailable;
2186 eap->getKey = eap_ttls_getKey;
2187 eap->get_status = eap_ttls_get_status;
2188 eap->has_reauth_data = eap_ttls_has_reauth_data;
2189 eap->deinit_for_reauth = eap_ttls_deinit_for_reauth;
2190 eap->init_for_reauth = eap_ttls_init_for_reauth;
2192 ret = eap_peer_method_register(eap);
2194 eap_peer_method_free(eap);