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 */
78 int done_butfor_cb; /*we turned METHOD_DONE into METHOD_MAY_CONT to receive cb*/
79 EapDecision cbDecision;
87 /* draft-ietf-emu-chbind-13 section 5.3 */
100 #endif /* _MSC_VER */
104 static void * eap_ttls_init(struct eap_sm *sm)
106 struct eap_ttls_data *data;
107 struct eap_peer_config *config = eap_get_config(sm);
110 data = os_zalloc(sizeof(*data));
113 data->ttls_version = EAP_TTLS_VERSION;
114 data->force_ttls_version = -1;
116 data->phase2_type = EAP_TTLS_PHASE2_EAP;
118 #if EAP_TTLS_VERSION > 0
119 if (config && config->phase1) {
120 const char *pos = os_strstr(config->phase1, "ttlsver=");
122 data->force_ttls_version = atoi(pos + 8);
123 data->ttls_version = data->force_ttls_version;
124 wpa_printf(MSG_DEBUG, "EAP-TTLS: Forced TTLS version "
125 "%d", data->force_ttls_version);
128 #endif /* EAP_TTLS_VERSION */
130 if (config && config->phase2) {
131 if (os_strstr(config->phase2, "autheap=")) {
133 data->phase2_type = EAP_TTLS_PHASE2_EAP;
134 } else if (os_strstr(config->phase2, "auth=MSCHAPV2")) {
135 selected = "MSCHAPV2";
136 data->phase2_type = EAP_TTLS_PHASE2_MSCHAPV2;
137 } else if (os_strstr(config->phase2, "auth=MSCHAP")) {
139 data->phase2_type = EAP_TTLS_PHASE2_MSCHAP;
140 } else if (os_strstr(config->phase2, "auth=PAP")) {
142 data->phase2_type = EAP_TTLS_PHASE2_PAP;
143 } else if (os_strstr(config->phase2, "auth=CHAP")) {
145 data->phase2_type = EAP_TTLS_PHASE2_CHAP;
148 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase2 type: %s", selected);
150 if (data->phase2_type == EAP_TTLS_PHASE2_EAP) {
151 if (eap_peer_select_phase2_methods(config, "autheap=",
152 &data->phase2_eap_types,
153 &data->num_phase2_eap_types)
155 eap_ttls_deinit(sm, data);
159 data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
160 data->phase2_eap_type.method = EAP_TYPE_NONE;
163 #if EAP_TTLS_VERSION > 0
164 if (!(tls_capabilities(sm->ssl_ctx) & TLS_CAPABILITY_IA) &&
165 data->ttls_version > 0) {
166 if (data->force_ttls_version > 0) {
167 wpa_printf(MSG_INFO, "EAP-TTLS: Forced TTLSv%d and "
168 "TLS library does not support TLS/IA.",
169 data->force_ttls_version);
170 eap_ttls_deinit(sm, data);
173 data->ttls_version = 0;
175 #endif /* EAP_TTLS_VERSION */
181 static void eap_ttls_phase2_eap_deinit(struct eap_sm *sm,
182 struct eap_ttls_data *data)
184 if (data->phase2_priv && data->phase2_method) {
185 data->phase2_method->deinit(sm, data->phase2_priv);
186 data->phase2_method = NULL;
187 data->phase2_priv = NULL;
192 static void eap_ttls_deinit(struct eap_sm *sm, void *priv)
194 struct eap_ttls_data *data = priv;
197 eap_ttls_phase2_eap_deinit(sm, data);
198 os_free(data->phase2_eap_types);
199 if (data->ssl_initialized)
200 eap_peer_tls_ssl_deinit(sm, &data->ssl);
201 os_free(data->key_data);
202 wpabuf_free(data->pending_phase2_req);
207 static u8 * eap_ttls_avp_hdr(u8 *avphdr, u32 avp_code, u32 vendor_id,
208 int mandatory, size_t len)
210 struct ttls_avp_vendor *avp;
214 avp = (struct ttls_avp_vendor *) avphdr;
215 flags = mandatory ? AVP_FLAGS_MANDATORY : 0;
217 flags |= AVP_FLAGS_VENDOR;
218 hdrlen = sizeof(*avp);
219 avp->vendor_id = host_to_be32(vendor_id);
221 hdrlen = sizeof(struct ttls_avp);
224 avp->avp_code = host_to_be32(avp_code);
225 avp->avp_length = host_to_be32((flags << 24) | (hdrlen + len));
227 return avphdr + hdrlen;
231 static u8 * eap_ttls_avp_add(u8 *start, u8 *avphdr, u32 avp_code,
232 u32 vendor_id, int mandatory,
233 const u8 *data, size_t len)
236 pos = eap_ttls_avp_hdr(avphdr, avp_code, vendor_id, mandatory, len);
237 os_memcpy(pos, data, len);
244 static int eap_ttls_avp_encapsulate(struct wpabuf **resp, u32 avp_code,
250 msg = wpabuf_alloc(sizeof(struct ttls_avp) + wpabuf_len(*resp) + 4);
257 avp = wpabuf_mhead(msg);
258 pos = eap_ttls_avp_hdr(avp, avp_code, 0, mandatory, wpabuf_len(*resp));
259 os_memcpy(pos, wpabuf_head(*resp), wpabuf_len(*resp));
260 pos += wpabuf_len(*resp);
263 wpabuf_put(msg, pos - avp);
268 /* chop up resp into multiple vsa's as necessary*/
269 static int eap_ttls_avp_radius_vsa_encapsulate(struct wpabuf **resp, u32 vendor,
270 u8 attr, int mandatory)
273 u8 *avp, *pos, *src, *final;
274 size_t size = wpabuf_len(*resp);
275 size_t num_msgs = 1 + (size / 248);
276 size_t msg_wrapper_size = sizeof(struct ttls_avp_vendor) + 6;
277 size_t allocated_total = num_msgs * (4 + msg_wrapper_size) + size;
279 msg = wpabuf_alloc(allocated_total);
285 src = wpabuf_mhead(*resp);
286 avp = wpabuf_mhead(msg);
288 int avp_size = size > 248 ? 248 : size;
290 pos = eap_ttls_avp_hdr(avp, RADIUS_ATTR_VENDOR_SPECIFIC, 0, mandatory,
292 wpabuf_put(msg, pos-avp);
293 wpabuf_put_be32(msg, vendor);
294 wpabuf_put_u8(msg, (u8) attr);
295 wpabuf_put_u8(msg, (u8) avp_size+2);
296 wpabuf_put_data(msg, src, avp_size);
298 pos = wpabuf_mhead_u8(msg) + wpabuf_len(msg);
299 final = pos; /*keep pos so we know how much padding is added*/
300 AVP_PAD(avp, final); /*final modified*/
302 wpabuf_put(msg, final-pos);
305 /* check avp-wpabuf_mhead(msg) < allocated_total */
311 #if EAP_TTLS_VERSION > 0
312 static int eap_ttls_ia_permute_inner_secret(struct eap_sm *sm,
313 struct eap_ttls_data *data,
314 const u8 *key, size_t key_len)
321 buf_len = 2 + key_len;
322 buf = os_malloc(buf_len);
325 WPA_PUT_BE16(buf, key_len);
326 os_memcpy(buf + 2, key, key_len);
332 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Session keys for TLS/IA inner "
333 "secret permutation", buf, buf_len);
334 ret = tls_connection_ia_permute_inner_secret(sm->ssl_ctx,
341 #endif /* EAP_TTLS_VERSION */
344 static int eap_ttls_v0_derive_key(struct eap_sm *sm,
345 struct eap_ttls_data *data)
347 os_free(data->key_data);
348 data->key_data = eap_peer_tls_derive_key(sm, &data->ssl,
349 "ttls keying material",
351 if (!data->key_data) {
352 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to derive key");
356 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
357 data->key_data, EAP_TLS_KEY_LEN);
363 #if EAP_TTLS_VERSION > 0
364 static int eap_ttls_v1_derive_key(struct eap_sm *sm,
365 struct eap_ttls_data *data)
367 struct tls_keys keys;
370 os_free(data->key_data);
371 data->key_data = NULL;
373 os_memset(&keys, 0, sizeof(keys));
374 if (tls_connection_get_keys(sm->ssl_ctx, data->ssl.conn, &keys) ||
375 keys.client_random == NULL || keys.server_random == NULL ||
376 keys.inner_secret == NULL) {
377 wpa_printf(MSG_INFO, "EAP-TTLS: Could not get inner secret, "
378 "client random, or server random to derive keying "
383 rnd = os_malloc(keys.client_random_len + keys.server_random_len);
384 data->key_data = os_malloc(EAP_TLS_KEY_LEN);
385 if (rnd == NULL || data->key_data == NULL) {
386 wpa_printf(MSG_INFO, "EAP-TTLS: No memory for key derivation");
388 os_free(data->key_data);
389 data->key_data = NULL;
392 os_memcpy(rnd, keys.client_random, keys.client_random_len);
393 os_memcpy(rnd + keys.client_random_len, keys.server_random,
394 keys.server_random_len);
396 if (tls_prf(keys.inner_secret, keys.inner_secret_len,
397 "ttls v1 keying material", rnd, keys.client_random_len +
398 keys.server_random_len, data->key_data, EAP_TLS_KEY_LEN)) {
399 wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to derive key");
401 os_free(data->key_data);
402 data->key_data = NULL;
406 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: client/server random",
407 rnd, keys.client_random_len + keys.server_random_len);
408 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: TLS/IA inner secret",
409 keys.inner_secret, keys.inner_secret_len);
413 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
414 data->key_data, EAP_TLS_KEY_LEN);
418 #endif /* EAP_TTLS_VERSION */
421 static u8 * eap_ttls_implicit_challenge(struct eap_sm *sm,
422 struct eap_ttls_data *data, size_t len)
424 #if EAP_TTLS_VERSION > 0
425 struct tls_keys keys;
427 #endif /* EAP_TTLS_VERSION */
429 if (data->ttls_version == 0) {
430 return eap_peer_tls_derive_key(sm, &data->ssl,
431 "ttls challenge", len);
434 #if EAP_TTLS_VERSION > 0
436 os_memset(&keys, 0, sizeof(keys));
437 if (tls_connection_get_keys(sm->ssl_ctx, data->ssl.conn, &keys) ||
438 keys.client_random == NULL || keys.server_random == NULL ||
439 keys.inner_secret == NULL) {
440 wpa_printf(MSG_INFO, "EAP-TTLS: Could not get inner secret, "
441 "client random, or server random to derive "
442 "implicit challenge");
446 rnd = os_malloc(keys.client_random_len + keys.server_random_len);
447 challenge = os_malloc(len);
448 if (rnd == NULL || challenge == NULL) {
449 wpa_printf(MSG_INFO, "EAP-TTLS: No memory for implicit "
450 "challenge derivation");
455 os_memcpy(rnd, keys.server_random, keys.server_random_len);
456 os_memcpy(rnd + keys.server_random_len, keys.client_random,
457 keys.client_random_len);
459 if (tls_prf(keys.inner_secret, keys.inner_secret_len,
460 "inner application challenge", rnd,
461 keys.client_random_len + keys.server_random_len,
463 wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to derive implicit "
472 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived implicit challenge",
477 #else /* EAP_TTLS_VERSION */
481 #endif /* EAP_TTLS_VERSION */
485 static void eap_ttlsv1_phase2_eap_finish(struct eap_sm *sm,
486 struct eap_ttls_data *data,
487 struct eap_method_ret *ret)
489 #if EAP_TTLS_VERSION > 0
490 if (data->ttls_version > 0) {
491 const struct eap_method *m = data->phase2_method;
492 void *priv = data->phase2_priv;
494 /* TTLSv1 requires TLS/IA FinalPhaseFinished */
495 if (ret->decision == DECISION_UNCOND_SUCC)
496 ret->decision = DECISION_COND_SUCC;
497 ret->methodState = METHOD_CONT;
499 if (ret->decision == DECISION_COND_SUCC &&
500 m->isKeyAvailable && m->getKey &&
501 m->isKeyAvailable(sm, priv)) {
504 key = m->getKey(sm, priv, &key_len);
506 eap_ttls_ia_permute_inner_secret(
507 sm, data, key, key_len);
512 #endif /* EAP_TTLS_VERSION */
516 static void eap_ttls_phase2_select_eap_method(struct eap_ttls_data *data,
520 for (i = 0; i < data->num_phase2_eap_types; i++) {
521 if (data->phase2_eap_types[i].vendor != EAP_VENDOR_IETF ||
522 data->phase2_eap_types[i].method != method)
525 data->phase2_eap_type.vendor =
526 data->phase2_eap_types[i].vendor;
527 data->phase2_eap_type.method =
528 data->phase2_eap_types[i].method;
529 wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
530 "Phase 2 EAP vendor %d method %d",
531 data->phase2_eap_type.vendor,
532 data->phase2_eap_type.method);
538 static int eap_ttls_phase2_eap_process(struct eap_sm *sm,
539 struct eap_ttls_data *data,
540 struct eap_method_ret *ret,
541 struct eap_hdr *hdr, size_t len,
542 struct wpabuf **resp)
545 struct eap_method_ret iret;
547 os_memset(&iret, 0, sizeof(iret));
548 wpabuf_set(&msg, hdr, len);
549 *resp = data->phase2_method->process(sm, data->phase2_priv, &iret,
551 if ((iret.methodState == METHOD_DONE ||
552 iret.methodState == METHOD_MAY_CONT) &&
553 (iret.decision == DECISION_UNCOND_SUCC ||
554 iret.decision == DECISION_COND_SUCC ||
555 iret.decision == DECISION_FAIL)) {
556 ret->methodState = iret.methodState;
557 ret->decision = iret.decision;
559 eap_ttlsv1_phase2_eap_finish(sm, data, ret);
565 static int eap_ttls_phase2_request_eap_method(struct eap_sm *sm,
566 struct eap_ttls_data *data,
567 struct eap_method_ret *ret,
568 struct eap_hdr *hdr, size_t len,
569 u8 method, struct wpabuf **resp)
572 if (data->tnc_started && data->phase2_method &&
573 data->phase2_priv && method == EAP_TYPE_TNC &&
574 data->phase2_eap_type.method == EAP_TYPE_TNC)
575 return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len,
578 if (data->ready_for_tnc && !data->tnc_started &&
579 method == EAP_TYPE_TNC) {
580 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
582 data->tnc_started = 1;
585 if (data->tnc_started) {
586 if (data->phase2_eap_type.vendor != EAP_VENDOR_IETF ||
587 data->phase2_eap_type.method == EAP_TYPE_TNC) {
588 wpa_printf(MSG_DEBUG, "EAP-TTLS: Unexpected EAP "
589 "type %d for TNC", method);
593 data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
594 data->phase2_eap_type.method = method;
595 wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
596 "Phase 2 EAP vendor %d method %d (TNC)",
597 data->phase2_eap_type.vendor,
598 data->phase2_eap_type.method);
600 if (data->phase2_type == EAP_TTLS_PHASE2_EAP)
601 eap_ttls_phase2_eap_deinit(sm, data);
605 if (data->phase2_eap_type.vendor == EAP_VENDOR_IETF &&
606 data->phase2_eap_type.method == EAP_TYPE_NONE)
607 eap_ttls_phase2_select_eap_method(data, method);
609 if (method != data->phase2_eap_type.method || method == EAP_TYPE_NONE)
611 if (eap_peer_tls_phase2_nak(data->phase2_eap_types,
612 data->num_phase2_eap_types,
618 if (data->phase2_priv == NULL) {
619 data->phase2_method = eap_peer_get_eap_method(
620 EAP_VENDOR_IETF, method);
621 if (data->phase2_method) {
623 data->phase2_priv = data->phase2_method->init(sm);
627 if (data->phase2_priv == NULL || data->phase2_method == NULL) {
628 wpa_printf(MSG_INFO, "EAP-TTLS: failed to initialize "
629 "Phase 2 EAP method %d", method);
633 return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len, resp);
637 static int eap_ttls_phase2_request_eap(struct eap_sm *sm,
638 struct eap_ttls_data *data,
639 struct eap_method_ret *ret,
641 struct wpabuf **resp)
643 size_t len = be_to_host16(hdr->length);
645 struct eap_peer_config *config = eap_get_config(sm);
647 if (len <= sizeof(struct eap_hdr)) {
648 wpa_printf(MSG_INFO, "EAP-TTLS: too short "
649 "Phase 2 request (len=%lu)", (unsigned long) len);
652 pos = (u8 *) (hdr + 1);
653 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP Request: type=%d", *pos);
655 case EAP_TYPE_IDENTITY:
656 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
659 if (eap_ttls_phase2_request_eap_method(sm, data, ret, hdr, len,
666 (config->pending_req_identity || config->pending_req_password ||
667 config->pending_req_otp)) {
674 wpa_hexdump_buf(MSG_DEBUG, "EAP-TTLS: AVP encapsulate EAP Response",
676 return eap_ttls_avp_encapsulate(resp, RADIUS_ATTR_EAP_MESSAGE, 1);
680 static void eap_ttlsv1_permute_inner(struct eap_sm *sm,
681 struct eap_ttls_data *data)
683 #if EAP_TTLS_VERSION > 0
684 u8 session_key[2 * MSCHAPV2_KEY_LEN];
686 if (data->ttls_version == 0)
689 get_asymetric_start_key(data->master_key, session_key,
690 MSCHAPV2_KEY_LEN, 0, 0);
691 get_asymetric_start_key(data->master_key,
692 session_key + MSCHAPV2_KEY_LEN,
693 MSCHAPV2_KEY_LEN, 1, 0);
694 eap_ttls_ia_permute_inner_secret(sm, data, session_key,
695 sizeof(session_key));
696 #endif /* EAP_TTLS_VERSION */
700 static int eap_ttls_phase2_request_mschapv2(struct eap_sm *sm,
701 struct eap_ttls_data *data,
702 struct eap_method_ret *ret,
703 struct wpabuf **resp)
706 u8 *buf, *pos, *challenge, *peer_challenge;
707 const u8 *identity, *password;
708 size_t identity_len, password_len;
711 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAPV2 Request");
713 identity = eap_get_config_identity(sm, &identity_len);
714 password = eap_get_config_password2(sm, &password_len, &pwhash);
715 if (identity == NULL || password == NULL)
718 msg = wpabuf_alloc(identity_len + 1000);
720 wpa_printf(MSG_ERROR,
721 "EAP-TTLS/MSCHAPV2: Failed to allocate memory");
724 pos = buf = wpabuf_mhead(msg);
727 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
728 identity, identity_len);
730 /* MS-CHAP-Challenge */
731 challenge = eap_ttls_implicit_challenge(
732 sm, data, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN + 1);
733 if (challenge == NULL) {
735 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
736 "implicit challenge");
739 peer_challenge = challenge + 1 + EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
741 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
742 RADIUS_VENDOR_ID_MICROSOFT, 1,
743 challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
745 /* MS-CHAP2-Response */
746 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP2_RESPONSE,
747 RADIUS_VENDOR_ID_MICROSOFT, 1,
748 EAP_TTLS_MSCHAPV2_RESPONSE_LEN);
749 data->ident = challenge[EAP_TTLS_MSCHAPV2_CHALLENGE_LEN];
750 *pos++ = data->ident;
751 *pos++ = 0; /* Flags */
752 os_memcpy(pos, peer_challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
753 pos += EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
754 os_memset(pos, 0, 8); /* Reserved, must be zero */
756 if (mschapv2_derive_response(identity, identity_len, password,
757 password_len, pwhash, challenge,
758 peer_challenge, pos, data->auth_response,
761 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
765 data->auth_response_valid = 1;
767 eap_ttlsv1_permute_inner(sm, data);
773 wpabuf_put(msg, pos - buf);
776 if (sm->workaround && data->ttls_version == 0) {
777 /* At least FreeRADIUS seems to be terminating
778 * EAP-TTLS/MSHCAPV2 without the expected MS-CHAP-v2 Success
780 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: EAP workaround - "
781 "allow success without tunneled response");
782 ret->methodState = METHOD_MAY_CONT;
783 ret->decision = DECISION_COND_SUCC;
790 static int eap_ttls_phase2_request_mschap(struct eap_sm *sm,
791 struct eap_ttls_data *data,
792 struct eap_method_ret *ret,
793 struct wpabuf **resp)
796 u8 *buf, *pos, *challenge;
797 const u8 *identity, *password;
798 size_t identity_len, password_len;
801 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAP Request");
803 identity = eap_get_config_identity(sm, &identity_len);
804 password = eap_get_config_password2(sm, &password_len, &pwhash);
805 if (identity == NULL || password == NULL)
808 msg = wpabuf_alloc(identity_len + 1000);
810 wpa_printf(MSG_ERROR,
811 "EAP-TTLS/MSCHAP: Failed to allocate memory");
814 pos = buf = wpabuf_mhead(msg);
817 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
818 identity, identity_len);
820 /* MS-CHAP-Challenge */
821 challenge = eap_ttls_implicit_challenge(
822 sm, data, EAP_TTLS_MSCHAP_CHALLENGE_LEN + 1);
823 if (challenge == NULL) {
825 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAP: Failed to derive "
826 "implicit challenge");
830 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
831 RADIUS_VENDOR_ID_MICROSOFT, 1,
832 challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
834 /* MS-CHAP-Response */
835 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP_RESPONSE,
836 RADIUS_VENDOR_ID_MICROSOFT, 1,
837 EAP_TTLS_MSCHAP_RESPONSE_LEN);
838 data->ident = challenge[EAP_TTLS_MSCHAP_CHALLENGE_LEN];
839 *pos++ = data->ident;
840 *pos++ = 1; /* Flags: Use NT style passwords */
841 os_memset(pos, 0, 24); /* LM-Response */
844 challenge_response(challenge, password, pos); /* NT-Response */
845 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password hash",
848 nt_challenge_response(challenge, password, password_len,
849 pos); /* NT-Response */
850 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password",
851 password, password_len);
853 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP implicit challenge",
854 challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
855 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP response", pos, 24);
860 wpabuf_put(msg, pos - buf);
863 if (data->ttls_version > 0) {
864 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
865 * so do not allow connection to be terminated yet. */
866 ret->methodState = METHOD_CONT;
867 ret->decision = DECISION_COND_SUCC;
869 /* EAP-TTLS/MSCHAP does not provide tunneled success
870 * notification, so assume that Phase2 succeeds. */
871 ret->methodState = METHOD_DONE;
872 ret->decision = DECISION_COND_SUCC;
879 static int eap_ttls_phase2_request_pap(struct eap_sm *sm,
880 struct eap_ttls_data *data,
881 struct eap_method_ret *ret,
882 struct wpabuf **resp)
887 const u8 *identity, *password;
888 size_t identity_len, password_len;
890 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 PAP Request");
892 identity = eap_get_config_identity(sm, &identity_len);
893 password = eap_get_config_password(sm, &password_len);
894 if (identity == NULL || password == NULL)
897 msg = wpabuf_alloc(identity_len + password_len + 100);
899 wpa_printf(MSG_ERROR,
900 "EAP-TTLS/PAP: Failed to allocate memory");
903 pos = buf = wpabuf_mhead(msg);
906 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
907 identity, identity_len);
909 /* User-Password; in RADIUS, this is encrypted, but EAP-TTLS encrypts
910 * the data, so no separate encryption is used in the AVP itself.
911 * However, the password is padded to obfuscate its length. */
912 pad = password_len == 0 ? 16 : (16 - (password_len & 15)) & 15;
913 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_USER_PASSWORD, 0, 1,
915 os_memcpy(pos, password, password_len);
917 os_memset(pos, 0, pad);
921 wpabuf_put(msg, pos - buf);
924 if (data->ttls_version > 0) {
925 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
926 * so do not allow connection to be terminated yet. */
927 ret->methodState = METHOD_CONT;
928 ret->decision = DECISION_COND_SUCC;
930 /* EAP-TTLS/PAP does not provide tunneled success notification,
931 * so assume that Phase2 succeeds. */
932 ret->methodState = METHOD_DONE;
933 ret->decision = DECISION_COND_SUCC;
940 static int eap_ttls_phase2_request_chap(struct eap_sm *sm,
941 struct eap_ttls_data *data,
942 struct eap_method_ret *ret,
943 struct wpabuf **resp)
946 u8 *buf, *pos, *challenge;
947 const u8 *identity, *password;
948 size_t identity_len, password_len;
950 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 CHAP Request");
952 identity = eap_get_config_identity(sm, &identity_len);
953 password = eap_get_config_password(sm, &password_len);
954 if (identity == NULL || password == NULL)
957 msg = wpabuf_alloc(identity_len + 1000);
959 wpa_printf(MSG_ERROR,
960 "EAP-TTLS/CHAP: Failed to allocate memory");
963 pos = buf = wpabuf_mhead(msg);
966 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
967 identity, identity_len);
970 challenge = eap_ttls_implicit_challenge(
971 sm, data, EAP_TTLS_CHAP_CHALLENGE_LEN + 1);
972 if (challenge == NULL) {
974 wpa_printf(MSG_ERROR, "EAP-TTLS/CHAP: Failed to derive "
975 "implicit challenge");
979 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_CHAP_CHALLENGE, 0, 1,
980 challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
983 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_CHAP_PASSWORD, 0, 1,
984 1 + EAP_TTLS_CHAP_PASSWORD_LEN);
985 data->ident = challenge[EAP_TTLS_CHAP_CHALLENGE_LEN];
986 *pos++ = data->ident;
988 /* MD5(Ident + Password + Challenge) */
989 chap_md5(data->ident, password, password_len, challenge,
990 EAP_TTLS_CHAP_CHALLENGE_LEN, pos);
992 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: CHAP username",
993 identity, identity_len);
994 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: CHAP password",
995 password, password_len);
996 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP implicit challenge",
997 challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
998 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP password",
999 pos, EAP_TTLS_CHAP_PASSWORD_LEN);
1000 pos += EAP_TTLS_CHAP_PASSWORD_LEN;
1004 wpabuf_put(msg, pos - buf);
1007 if (data->ttls_version > 0) {
1008 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
1009 * so do not allow connection to be terminated yet. */
1010 ret->methodState = METHOD_CONT;
1011 ret->decision = DECISION_COND_SUCC;
1013 /* EAP-TTLS/CHAP does not provide tunneled success
1014 * notification, so assume that Phase2 succeeds. */
1015 ret->methodState = METHOD_DONE;
1016 ret->decision = DECISION_COND_SUCC;
1023 static int eap_ttls_phase2_request(struct eap_sm *sm,
1024 struct eap_ttls_data *data,
1025 struct eap_method_ret *ret,
1026 struct eap_hdr *hdr,
1027 struct wpabuf **resp)
1031 enum phase2_types phase2_type = data->phase2_type;
1034 if (data->tnc_started) {
1035 wpa_printf(MSG_DEBUG, "EAP-TTLS: Processing TNC");
1036 phase2_type = EAP_TTLS_PHASE2_EAP;
1038 #endif /* EAP_TNC */
1040 if (phase2_type == EAP_TTLS_PHASE2_MSCHAPV2 ||
1041 phase2_type == EAP_TTLS_PHASE2_MSCHAP ||
1042 phase2_type == EAP_TTLS_PHASE2_PAP ||
1043 phase2_type == EAP_TTLS_PHASE2_CHAP) {
1044 if (eap_get_config_identity(sm, &len) == NULL) {
1045 wpa_printf(MSG_INFO,
1046 "EAP-TTLS: Identity not configured");
1047 eap_sm_request_identity(sm);
1048 if (eap_get_config_password(sm, &len) == NULL)
1049 eap_sm_request_password(sm);
1053 if (eap_get_config_password(sm, &len) == NULL) {
1054 wpa_printf(MSG_INFO,
1055 "EAP-TTLS: Password not configured");
1056 eap_sm_request_password(sm);
1061 switch (phase2_type) {
1062 case EAP_TTLS_PHASE2_EAP:
1063 res = eap_ttls_phase2_request_eap(sm, data, ret, hdr, resp);
1065 case EAP_TTLS_PHASE2_MSCHAPV2:
1066 res = eap_ttls_phase2_request_mschapv2(sm, data, ret, resp);
1068 case EAP_TTLS_PHASE2_MSCHAP:
1069 res = eap_ttls_phase2_request_mschap(sm, data, ret, resp);
1071 case EAP_TTLS_PHASE2_PAP:
1072 res = eap_ttls_phase2_request_pap(sm, data, ret, resp);
1074 case EAP_TTLS_PHASE2_CHAP:
1075 res = eap_ttls_phase2_request_chap(sm, data, ret, resp);
1078 wpa_printf(MSG_ERROR, "EAP-TTLS: Phase 2 - Unknown");
1084 ret->methodState = METHOD_DONE;
1085 ret->decision = DECISION_FAIL;
1092 #if EAP_TTLS_VERSION > 0
1093 static struct wpabuf * eap_ttls_build_phase_finished(
1094 struct eap_sm *sm, struct eap_ttls_data *data, int id, int final)
1096 struct wpabuf *req, *buf;
1098 buf = tls_connection_ia_send_phase_finished(sm->ssl_ctx,
1104 req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TTLS,
1105 1 + wpabuf_len(buf),
1106 EAP_CODE_RESPONSE, id);
1112 wpabuf_put_u8(req, data->ttls_version);
1113 wpabuf_put_buf(req, buf);
1115 eap_update_len(req);
1119 #endif /* EAP_TTLS_VERSION */
1122 struct ttls_parse_avp {
1132 static int eap_ttls_parse_attr_eap(const u8 *dpos, size_t dlen,
1133 struct ttls_parse_avp *parse)
1135 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - EAP Message");
1136 if (parse->eapdata == NULL) {
1137 parse->eapdata = os_malloc(dlen);
1138 if (parse->eapdata == NULL) {
1139 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
1140 "memory for Phase 2 EAP data");
1143 os_memcpy(parse->eapdata, dpos, dlen);
1144 parse->eap_len = dlen;
1146 u8 *neweap = os_realloc(parse->eapdata, parse->eap_len + dlen);
1147 if (neweap == NULL) {
1148 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
1149 "memory for Phase 2 EAP data");
1152 os_memcpy(neweap + parse->eap_len, dpos, dlen);
1153 parse->eapdata = neweap;
1154 parse->eap_len += dlen;
1161 static int eap_ttls_parse_attr_chbind(const u8 *dpos, size_t dlen,
1162 struct ttls_parse_avp *parse)
1164 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - Channel Binding Message");
1166 if (parse->chbind_data == NULL) {
1167 parse->chbind_data = os_malloc(dlen);
1168 if (parse->chbind_data == NULL) {
1169 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
1170 "memory for Phase 2 channel binding data");
1173 os_memcpy(parse->chbind_data, dpos, dlen);
1174 parse->chbind_len = dlen;
1176 /* TODO: can this really happen? maybe just make this an error? */
1177 u8 *newchbind = os_realloc(parse->chbind_data,
1178 parse->chbind_len + dlen);
1179 if (newchbind == NULL) {
1180 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
1181 "memory for Phase 2 channel binding data");
1184 os_memcpy(newchbind + parse->chbind_len, dpos, dlen);
1185 parse->chbind_data = newchbind;
1186 parse->chbind_len += dlen;
1193 static int eap_ttls_parse_avp(u8 *pos, size_t left,
1194 struct ttls_parse_avp *parse)
1196 struct ttls_avp *avp;
1197 u32 avp_code, avp_length, vendor_id = 0;
1198 u8 avp_flags, *dpos;
1201 avp = (struct ttls_avp *) pos;
1202 avp_code = be_to_host32(avp->avp_code);
1203 avp_length = be_to_host32(avp->avp_length);
1204 avp_flags = (avp_length >> 24) & 0xff;
1205 avp_length &= 0xffffff;
1206 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP: code=%d flags=0x%02x "
1207 "length=%d", (int) avp_code, avp_flags,
1210 if (avp_length > left) {
1211 wpa_printf(MSG_WARNING, "EAP-TTLS: AVP overflow "
1212 "(len=%d, left=%lu) - dropped",
1213 (int) avp_length, (unsigned long) left);
1217 if (avp_length < sizeof(*avp)) {
1218 wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid AVP length %d",
1223 dpos = (u8 *) (avp + 1);
1224 dlen = avp_length - sizeof(*avp);
1225 if (avp_flags & AVP_FLAGS_VENDOR) {
1227 wpa_printf(MSG_WARNING, "EAP-TTLS: Vendor AVP "
1231 vendor_id = WPA_GET_BE32(dpos);
1232 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP vendor_id %d",
1238 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP data", dpos, dlen);
1240 if (vendor_id == 0 && avp_code == RADIUS_ATTR_EAP_MESSAGE) {
1241 if (eap_ttls_parse_attr_eap(dpos, dlen, parse) < 0)
1243 } else if (vendor_id == RADIUS_VENDOR_ID_UKERNA &&
1244 avp_code == RADIUS_ATTR_UKERNA_CHBIND) {
1245 /* message containing channel binding data */
1246 if (eap_ttls_parse_attr_chbind(dpos, dlen, parse) < 0)
1248 } else if (vendor_id == 0 && avp_code == RADIUS_ATTR_REPLY_MESSAGE) {
1249 /* This is an optional message that can be displayed to
1251 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: AVP - Reply-Message",
1253 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
1254 avp_code == RADIUS_ATTR_MS_CHAP2_SUCCESS) {
1255 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP2-Success",
1258 wpa_printf(MSG_WARNING, "EAP-TTLS: Unexpected "
1259 "MS-CHAP2-Success length "
1260 "(len=%lu, expected 43)",
1261 (unsigned long) dlen);
1264 parse->mschapv2 = dpos;
1265 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
1266 avp_code == RADIUS_ATTR_MS_CHAP_ERROR) {
1267 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP-Error",
1269 parse->mschapv2_error = 1;
1270 } else if (avp_flags & AVP_FLAGS_MANDATORY) {
1271 wpa_printf(MSG_WARNING, "EAP-TTLS: Unsupported mandatory AVP "
1272 "code %d vendor_id %d - dropped",
1273 (int) avp_code, (int) vendor_id);
1276 wpa_printf(MSG_DEBUG, "EAP-TTLS: Ignoring unsupported AVP "
1277 "code %d vendor_id %d",
1278 (int) avp_code, (int) vendor_id);
1285 static int eap_ttls_parse_avps(struct wpabuf *in_decrypted,
1286 struct ttls_parse_avp *parse)
1292 pos = wpabuf_mhead(in_decrypted);
1293 left = wpabuf_len(in_decrypted);
1294 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Decrypted Phase 2 AVPs", pos, left);
1295 if (left < sizeof(struct ttls_avp)) {
1296 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 AVP frame"
1297 " len=%lu expected %lu or more - dropped",
1298 (unsigned long) left,
1299 (unsigned long) sizeof(struct ttls_avp));
1304 os_memset(parse, 0, sizeof(*parse));
1307 avp_length = eap_ttls_parse_avp(pos, left, parse);
1311 pad = (4 - (avp_length & 3)) & 3;
1312 pos += avp_length + pad;
1313 if (left < avp_length + pad)
1316 left -= avp_length + pad;
1323 static u8 * eap_ttls_fake_identity_request(void)
1325 struct eap_hdr *hdr;
1328 wpa_printf(MSG_DEBUG, "EAP-TTLS: empty data in beginning of "
1329 "Phase 2 - use fake EAP-Request Identity");
1330 buf = os_malloc(sizeof(*hdr) + 1);
1332 wpa_printf(MSG_WARNING, "EAP-TTLS: failed to allocate "
1333 "memory for fake EAP-Identity Request");
1337 hdr = (struct eap_hdr *) buf;
1338 hdr->code = EAP_CODE_REQUEST;
1339 hdr->identifier = 0;
1340 hdr->length = host_to_be16(sizeof(*hdr) + 1);
1341 buf[sizeof(*hdr)] = EAP_TYPE_IDENTITY;
1347 static int eap_ttls_encrypt_response(struct eap_sm *sm,
1348 struct eap_ttls_data *data,
1349 struct wpabuf *resp, u8 identifier,
1350 struct wpabuf **out_data)
1355 wpa_hexdump_buf_key(MSG_DEBUG, "EAP-TTLS: Encrypting Phase 2 data",
1357 if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
1358 data->ttls_version, identifier,
1360 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to encrypt a Phase 2 "
1369 static int eap_ttls_add_chbind_request(struct eap_sm *sm,
1370 struct eap_ttls_data *data,
1371 struct wpabuf **resp)
1373 struct wpabuf *chbind_req, *res;
1375 struct eap_peer_config *config = eap_get_config(sm);
1377 if (!config->chbind_config || config->chbind_config_len <= 0)
1380 for (i=0; i<config->chbind_config_len; i++) {
1381 length += 3 + config->chbind_config[i].req_data_len;
1384 chbind_req = wpabuf_alloc(length);
1388 wpabuf_put_u8(chbind_req, CHBIND_CODE_REQUEST);
1389 for (i=0; i<config->chbind_config_len; i++) {
1390 struct eap_peer_chbind_config *chbind_config =
1391 &config->chbind_config[i];
1392 wpabuf_put_be16(chbind_req, chbind_config->req_data_len);
1393 wpabuf_put_u8(chbind_req, chbind_config->nsid);
1394 wpabuf_put_data(chbind_req, chbind_config->req_data,
1395 chbind_config->req_data_len);
1397 if (eap_ttls_avp_radius_vsa_encapsulate(&chbind_req,
1398 RADIUS_VENDOR_ID_UKERNA,
1399 RADIUS_ATTR_UKERNA_CHBIND, 0) < 0)
1402 /* bleh. This will free *resp regardless of whether combined buffer
1403 alloc succeeds, which is not consistent with the other error
1404 condition behavior in this function */
1405 *resp = wpabuf_concat(chbind_req, *resp);
1407 return (*resp) ? 0 : -1;
1411 static int eap_ttls_process_chbind(struct eap_sm *sm,
1412 struct eap_ttls_data *data,
1413 struct eap_method_ret *ret,
1414 struct ttls_parse_avp *parse,
1415 struct wpabuf **resp)
1420 struct chbind_hdr *hdr;
1421 struct eap_peer_config *config = eap_get_config(sm);
1423 if (parse->chbind_data == NULL) {
1424 wpa_printf(MSG_WARNING, "EAP-TTLS: No channel binding message "
1425 "in the packet - dropped");
1428 if (parse->chbind_len < 1 + sizeof(*hdr)) {
1429 wpa_printf(MSG_WARNING, "EAP-TTLS: bad channel binding response "
1430 "frame (len=%lu, expected %lu or more) - dropped",
1431 (unsigned long) parse->chbind_len,
1432 (unsigned long) sizeof(*hdr));
1435 code = parse->chbind_data[pos++];
1436 while (pos+sizeof(*hdr) < parse->chbind_len) {
1437 hdr = (struct chbind_hdr *)(&parse->chbind_data[pos]);
1438 pos += sizeof(*hdr);
1439 len = be_to_host16(hdr->len);
1440 if (pos + len <= parse->chbind_len) {
1442 for (i=0; i<config->chbind_config_len; i++) {
1443 struct eap_peer_chbind_config *chbind_config =
1444 &config->chbind_config[i];
1445 if (chbind_config->nsid == hdr->nsid)
1446 chbind_config->response_cb(
1449 &parse->chbind_data[pos], len);
1454 if (pos != parse->chbind_len) {
1455 wpa_printf(MSG_WARNING, "EAP-TTLS: bad channel binding response "
1456 "frame (parsed len=%lu, expected %lu) - dropped",
1457 (unsigned long) pos,
1458 (unsigned long) parse->chbind_len);
1464 static int eap_ttls_process_phase2_eap(struct eap_sm *sm,
1465 struct eap_ttls_data *data,
1466 struct eap_method_ret *ret,
1467 struct ttls_parse_avp *parse,
1468 struct wpabuf **resp)
1470 struct eap_hdr *hdr;
1473 if (parse->eapdata == NULL) {
1474 wpa_printf(MSG_WARNING, "EAP-TTLS: No EAP Message in the "
1475 "packet - dropped");
1479 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP",
1480 parse->eapdata, parse->eap_len);
1481 hdr = (struct eap_hdr *) parse->eapdata;
1483 if (parse->eap_len < sizeof(*hdr)) {
1484 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 EAP "
1485 "frame (len=%lu, expected %lu or more) - dropped",
1486 (unsigned long) parse->eap_len,
1487 (unsigned long) sizeof(*hdr));
1490 len = be_to_host16(hdr->length);
1491 if (len > parse->eap_len) {
1492 wpa_printf(MSG_INFO, "EAP-TTLS: Length mismatch in Phase 2 "
1493 "EAP frame (EAP hdr len=%lu, EAP data len in "
1495 (unsigned long) len,
1496 (unsigned long) parse->eap_len);
1499 wpa_printf(MSG_DEBUG, "EAP-TTLS: received Phase 2: code=%d "
1500 "identifier=%d length=%lu",
1501 hdr->code, hdr->identifier, (unsigned long) len);
1502 switch (hdr->code) {
1503 case EAP_CODE_REQUEST:
1504 if (eap_ttls_phase2_request(sm, data, ret, hdr, resp)) {
1505 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
1506 "processing failed");
1511 wpa_printf(MSG_INFO, "EAP-TTLS: Unexpected code=%d in "
1512 "Phase 2 EAP header", hdr->code);
1520 static int eap_ttls_process_phase2_mschapv2(struct eap_sm *sm,
1521 struct eap_ttls_data *data,
1522 struct eap_method_ret *ret,
1523 struct ttls_parse_avp *parse)
1525 if (parse->mschapv2_error) {
1526 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Received "
1527 "MS-CHAP-Error - failed");
1528 ret->methodState = METHOD_DONE;
1529 ret->decision = DECISION_FAIL;
1530 /* Reply with empty data to ACK error */
1534 if (parse->mschapv2 == NULL) {
1536 if (data->phase2_success && parse->eapdata) {
1538 * Allow EAP-TNC to be started after successfully
1539 * completed MSCHAPV2.
1543 #endif /* EAP_TNC */
1544 wpa_printf(MSG_WARNING, "EAP-TTLS: no MS-CHAP2-Success AVP "
1545 "received for Phase2 MSCHAPV2");
1548 if (parse->mschapv2[0] != data->ident) {
1549 wpa_printf(MSG_WARNING, "EAP-TTLS: Ident mismatch for Phase 2 "
1550 "MSCHAPV2 (received Ident 0x%02x, expected 0x%02x)",
1551 parse->mschapv2[0], data->ident);
1554 if (!data->auth_response_valid ||
1555 mschapv2_verify_auth_response(data->auth_response,
1556 parse->mschapv2 + 1, 42)) {
1557 wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid authenticator "
1558 "response in Phase 2 MSCHAPV2 success request");
1562 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 MSCHAPV2 "
1563 "authentication succeeded");
1564 if (data->ttls_version > 0) {
1566 * EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report
1567 * success, so do not allow connection to be terminated
1570 ret->methodState = METHOD_CONT;
1571 ret->decision = DECISION_COND_SUCC;
1573 ret->methodState = METHOD_DONE;
1574 ret->decision = DECISION_UNCOND_SUCC;
1575 data->phase2_success = 1;
1579 * Reply with empty data; authentication server will reply
1580 * with EAP-Success after this.
1587 static int eap_ttls_process_tnc_start(struct eap_sm *sm,
1588 struct eap_ttls_data *data,
1589 struct eap_method_ret *ret,
1590 struct ttls_parse_avp *parse,
1591 struct wpabuf **resp)
1593 /* TNC uses inner EAP method after non-EAP TTLS phase 2. */
1594 if (parse->eapdata == NULL) {
1595 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
1596 "unexpected tunneled data (no EAP)");
1600 if (!data->ready_for_tnc) {
1601 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
1602 "EAP after non-EAP, but not ready for TNC");
1606 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
1608 data->tnc_started = 1;
1610 if (eap_ttls_process_phase2_eap(sm, data, ret, parse, resp) < 0)
1615 #endif /* EAP_TNC */
1618 static int eap_ttls_process_decrypted(struct eap_sm *sm,
1619 struct eap_ttls_data *data,
1620 struct eap_method_ret *ret,
1622 struct ttls_parse_avp *parse,
1623 struct wpabuf *in_decrypted,
1624 struct wpabuf **out_data)
1626 struct wpabuf *resp = NULL;
1627 struct eap_peer_config *config = eap_get_config(sm);
1629 enum phase2_types phase2_type = data->phase2_type;
1632 if (data->tnc_started)
1633 phase2_type = EAP_TTLS_PHASE2_EAP;
1634 #endif /* EAP_TNC */
1636 /* handle channel binding response here */
1637 if (parse->chbind_data) {
1638 /* received channel binding repsonse */
1639 if (eap_ttls_process_chbind(sm, data, ret, parse, &resp) < 0)
1641 if (data->done_butfor_cb) {
1642 ret->methodState = METHOD_DONE;
1643 ret->decision = data->cbDecision;
1644 data->phase2_success = 1;
1645 return 1; /*request ack*/
1649 switch (phase2_type) {
1650 case EAP_TTLS_PHASE2_EAP:
1651 if (eap_ttls_process_phase2_eap(sm, data, ret, parse, &resp) <
1655 case EAP_TTLS_PHASE2_MSCHAPV2:
1656 res = eap_ttls_process_phase2_mschapv2(sm, data, ret, parse);
1658 if (res == 1 && parse->eapdata && data->phase2_success) {
1660 * TNC may be required as the next
1661 * authentication method within the tunnel.
1663 ret->methodState = METHOD_MAY_CONT;
1664 data->ready_for_tnc = 1;
1665 if (eap_ttls_process_tnc_start(sm, data, ret, parse,
1669 #endif /* EAP_TNC */
1671 case EAP_TTLS_PHASE2_MSCHAP:
1672 case EAP_TTLS_PHASE2_PAP:
1673 case EAP_TTLS_PHASE2_CHAP:
1675 if (eap_ttls_process_tnc_start(sm, data, ret, parse, &resp) <
1680 /* EAP-TTLS/{MSCHAP,PAP,CHAP} should not send any TLS tunneled
1681 * requests to the supplicant */
1682 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received unexpected "
1685 #endif /* EAP_TNC */
1688 if (!resp && (config->pending_req_identity ||
1689 config->pending_req_password ||
1690 config->pending_req_otp ||
1691 config->pending_req_new_password)) {
1692 wpabuf_free(data->pending_phase2_req);
1693 data->pending_phase2_req = wpabuf_dup(in_decrypted);
1697 /* issue channel binding request when appropriate */
1698 if (config->chbind_config && config->chbind_config_len > 0 &&
1699 !data->chbind_req_sent) {
1700 if (eap_ttls_add_chbind_request(sm, data, &resp) < 0)
1702 data->chbind_req_sent = 1;
1703 if (ret->methodState == METHOD_DONE) {
1704 data->done_butfor_cb = 1;
1705 data->cbDecision = ret->decision;
1706 ret->methodState = METHOD_MAY_CONT;
1711 if (eap_ttls_encrypt_response(sm, data, resp, identifier,
1720 #if EAP_TTLS_VERSION > 0
1721 static void eap_ttls_final_phase_finished(struct eap_sm *sm,
1722 struct eap_ttls_data *data,
1723 struct eap_method_ret *ret,
1725 struct wpabuf **out_data)
1727 wpa_printf(MSG_DEBUG, "EAP-TTLS: FinalPhaseFinished received");
1728 wpa_printf(MSG_INFO, "EAP-TTLS: TLS/IA authentication succeeded");
1729 ret->methodState = METHOD_DONE;
1730 ret->decision = DECISION_UNCOND_SUCC;
1731 data->phase2_success = 1;
1732 *out_data = eap_ttls_build_phase_finished(sm, data, identifier, 1);
1733 eap_ttls_v1_derive_key(sm, data);
1735 #endif /* EAP_TTLS_VERSION */
1738 static int eap_ttls_implicit_identity_request(struct eap_sm *sm,
1739 struct eap_ttls_data *data,
1740 struct eap_method_ret *ret,
1742 struct wpabuf **out_data)
1745 struct eap_hdr *hdr;
1746 struct wpabuf *resp;
1748 hdr = (struct eap_hdr *) eap_ttls_fake_identity_request();
1750 ret->methodState = METHOD_DONE;
1751 ret->decision = DECISION_FAIL;
1756 if (eap_ttls_phase2_request(sm, data, ret, hdr, &resp)) {
1757 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
1758 "processing failed");
1761 retval = eap_ttls_encrypt_response(sm, data, resp, identifier,
1768 ret->methodState = METHOD_DONE;
1769 ret->decision = DECISION_FAIL;
1776 static int eap_ttls_phase2_start(struct eap_sm *sm, struct eap_ttls_data *data,
1777 struct eap_method_ret *ret, u8 identifier,
1778 struct wpabuf **out_data)
1780 data->phase2_start = 0;
1783 * EAP-TTLS does not use Phase2 on fast re-auth; this must be done only
1784 * if TLS part was indeed resuming a previous session. Most
1785 * Authentication Servers terminate EAP-TTLS before reaching this
1786 * point, but some do not. Make wpa_supplicant stop phase 2 here, if
1790 tls_connection_resumed(sm->ssl_ctx, data->ssl.conn)) {
1791 wpa_printf(MSG_DEBUG, "EAP-TTLS: Session resumption - "
1793 *out_data = eap_peer_tls_build_ack(identifier, EAP_TYPE_TTLS,
1794 data->ttls_version);
1795 ret->methodState = METHOD_DONE;
1796 ret->decision = DECISION_UNCOND_SUCC;
1797 data->phase2_success = 1;
1801 return eap_ttls_implicit_identity_request(sm, data, ret, identifier,
1806 static int eap_ttls_decrypt(struct eap_sm *sm, struct eap_ttls_data *data,
1807 struct eap_method_ret *ret, u8 identifier,
1808 const struct wpabuf *in_data,
1809 struct wpabuf **out_data)
1811 struct wpabuf *in_decrypted = NULL;
1813 struct ttls_parse_avp parse;
1815 os_memset(&parse, 0, sizeof(parse));
1817 wpa_printf(MSG_DEBUG, "EAP-TTLS: received %lu bytes encrypted data for"
1819 in_data ? (unsigned long) wpabuf_len(in_data) : 0);
1821 if (data->pending_phase2_req) {
1822 wpa_printf(MSG_DEBUG, "EAP-TTLS: Pending Phase 2 request - "
1823 "skip decryption and use old data");
1824 /* Clear TLS reassembly state. */
1825 eap_peer_tls_reset_input(&data->ssl);
1827 in_decrypted = data->pending_phase2_req;
1828 data->pending_phase2_req = NULL;
1829 if (wpabuf_len(in_decrypted) == 0) {
1830 wpabuf_free(in_decrypted);
1831 return eap_ttls_implicit_identity_request(
1832 sm, data, ret, identifier, out_data);
1837 if ((in_data == NULL || wpabuf_len(in_data) == 0) &&
1838 data->phase2_start) {
1839 return eap_ttls_phase2_start(sm, data, ret, identifier,
1843 if (in_data == NULL || wpabuf_len(in_data) == 0) {
1844 /* Received TLS ACK - requesting more fragments */
1845 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
1847 identifier, NULL, out_data);
1850 retval = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
1854 #if EAP_TTLS_VERSION > 0
1855 if (data->ttls_version > 0 &&
1856 (in_decrypted == NULL || wpabuf_len(in_decrypted) == 0) &&
1857 tls_connection_ia_final_phase_finished(sm->ssl_ctx,
1859 eap_ttls_final_phase_finished(sm, data, ret, identifier,
1863 #endif /* EAP_TTLS_VERSION */
1866 data->phase2_start = 0;
1868 if (eap_ttls_parse_avps(in_decrypted, &parse) < 0) {
1873 retval = eap_ttls_process_decrypted(sm, data, ret, identifier,
1874 &parse, in_decrypted, out_data);
1877 wpabuf_free(in_decrypted);
1878 os_free(parse.eapdata);
1881 ret->methodState = METHOD_DONE;
1882 ret->decision = DECISION_FAIL;
1889 static int eap_ttls_process_start(struct eap_sm *sm,
1890 struct eap_ttls_data *data, u8 flags,
1891 struct eap_method_ret *ret)
1893 struct eap_peer_config *config = eap_get_config(sm);
1895 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start (server ver=%d, own ver=%d)",
1896 flags & EAP_TLS_VERSION_MASK, data->ttls_version);
1897 #if EAP_TTLS_VERSION > 0
1898 if ((flags & EAP_TLS_VERSION_MASK) < data->ttls_version)
1899 data->ttls_version = flags & EAP_TLS_VERSION_MASK;
1900 if (data->force_ttls_version >= 0 &&
1901 data->force_ttls_version != data->ttls_version) {
1902 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to select "
1903 "forced TTLS version %d",
1904 data->force_ttls_version);
1905 ret->methodState = METHOD_DONE;
1906 ret->decision = DECISION_FAIL;
1907 ret->allowNotifications = FALSE;
1910 wpa_printf(MSG_DEBUG, "EAP-TTLS: Using TTLS version %d",
1911 data->ttls_version);
1913 if (data->ttls_version > 0)
1914 data->ssl.tls_ia = 1;
1915 #endif /* EAP_TTLS_VERSION */
1916 if (!data->ssl_initialized &&
1917 eap_peer_tls_ssl_init(sm, &data->ssl, config)) {
1918 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to initialize SSL.");
1921 data->ssl_initialized = 1;
1923 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start");
1929 static int eap_ttls_process_handshake(struct eap_sm *sm,
1930 struct eap_ttls_data *data,
1931 struct eap_method_ret *ret,
1933 const u8 *in_data, size_t in_len,
1934 struct wpabuf **out_data)
1938 res = eap_peer_tls_process_helper(sm, &data->ssl, EAP_TYPE_TTLS,
1939 data->ttls_version, identifier,
1940 in_data, in_len, out_data);
1942 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1943 wpa_printf(MSG_DEBUG, "EAP-TTLS: TLS done, proceed to "
1945 if (data->resuming) {
1946 wpa_printf(MSG_DEBUG, "EAP-TTLS: fast reauth - may "
1948 ret->decision = DECISION_COND_SUCC;
1949 ret->methodState = METHOD_MAY_CONT;
1951 data->phase2_start = 1;
1952 if (data->ttls_version == 0)
1953 eap_ttls_v0_derive_key(sm, data);
1955 if (*out_data == NULL || wpabuf_len(*out_data) == 0) {
1956 if (eap_ttls_decrypt(sm, data, ret, identifier,
1958 wpa_printf(MSG_WARNING, "EAP-TTLS: "
1959 "failed to process early "
1960 "start for Phase 2");
1970 * Application data included in the handshake message.
1972 wpabuf_free(data->pending_phase2_req);
1973 data->pending_phase2_req = *out_data;
1975 wpabuf_set(&msg, in_data, in_len);
1976 res = eap_ttls_decrypt(sm, data, ret, identifier, &msg,
1984 static void eap_ttls_check_auth_status(struct eap_sm *sm,
1985 struct eap_ttls_data *data,
1986 struct eap_method_ret *ret)
1988 if (data->ttls_version == 0 && ret->methodState == METHOD_DONE) {
1989 ret->allowNotifications = FALSE;
1990 if (ret->decision == DECISION_UNCOND_SUCC ||
1991 ret->decision == DECISION_COND_SUCC) {
1992 wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1993 "completed successfully");
1994 data->phase2_success = 1;
1996 if (!data->ready_for_tnc && !data->tnc_started) {
1998 * TNC may be required as the next
1999 * authentication method within the tunnel.
2001 ret->methodState = METHOD_MAY_CONT;
2002 data->ready_for_tnc = 1;
2004 #endif /* EAP_TNC */
2006 } else if (data->ttls_version == 0 &&
2007 ret->methodState == METHOD_MAY_CONT &&
2008 (ret->decision == DECISION_UNCOND_SUCC ||
2009 ret->decision == DECISION_COND_SUCC)) {
2010 wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
2011 "completed successfully (MAY_CONT)");
2012 data->phase2_success = 1;
2017 static struct wpabuf * eap_ttls_process(struct eap_sm *sm, void *priv,
2018 struct eap_method_ret *ret,
2019 const struct wpabuf *reqData)
2024 struct wpabuf *resp;
2026 struct eap_ttls_data *data = priv;
2028 pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_TTLS, ret,
2029 reqData, &left, &flags);
2032 id = eap_get_id(reqData);
2034 if (flags & EAP_TLS_FLAGS_START) {
2035 if (eap_ttls_process_start(sm, data, flags, ret) < 0)
2038 /* RFC 5281, Ch. 9.2:
2039 * "This packet MAY contain additional information in the form
2040 * of AVPs, which may provide useful hints to the client"
2041 * For now, ignore any potential extra data.
2044 } else if (!data->ssl_initialized) {
2045 wpa_printf(MSG_DEBUG, "EAP-TTLS: First message did not "
2046 "include Start flag");
2047 ret->methodState = METHOD_DONE;
2048 ret->decision = DECISION_FAIL;
2049 ret->allowNotifications = FALSE;
2054 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
2057 wpabuf_set(&msg, pos, left);
2058 res = eap_ttls_decrypt(sm, data, ret, id, &msg, &resp);
2060 res = eap_ttls_process_handshake(sm, data, ret, id,
2064 eap_ttls_check_auth_status(sm, data, ret);
2066 /* FIX: what about res == -1? Could just move all error processing into
2067 * the other functions and get rid of this res==1 case here. */
2070 return eap_peer_tls_build_ack(id, EAP_TYPE_TTLS,
2071 data->ttls_version);
2077 static Boolean eap_ttls_has_reauth_data(struct eap_sm *sm, void *priv)
2079 struct eap_ttls_data *data = priv;
2080 return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
2081 data->phase2_success;
2085 static void eap_ttls_deinit_for_reauth(struct eap_sm *sm, void *priv)
2087 struct eap_ttls_data *data = priv;
2088 wpabuf_free(data->pending_phase2_req);
2089 data->pending_phase2_req = NULL;
2091 data->ready_for_tnc = 0;
2092 data->tnc_started = 0;
2093 #endif /* EAP_TNC */
2097 static void * eap_ttls_init_for_reauth(struct eap_sm *sm, void *priv)
2099 struct eap_ttls_data *data = priv;
2100 os_free(data->key_data);
2101 data->key_data = NULL;
2102 if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
2106 if (data->phase2_priv && data->phase2_method &&
2107 data->phase2_method->init_for_reauth)
2108 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
2109 data->phase2_start = 0;
2110 data->phase2_success = 0;
2117 static int eap_ttls_get_status(struct eap_sm *sm, void *priv, char *buf,
2118 size_t buflen, int verbose)
2120 struct eap_ttls_data *data = priv;
2123 len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
2124 ret = os_snprintf(buf + len, buflen - len,
2125 "EAP-TTLSv%d Phase2 method=",
2126 data->ttls_version);
2127 if (ret < 0 || (size_t) ret >= buflen - len)
2130 switch (data->phase2_type) {
2131 case EAP_TTLS_PHASE2_EAP:
2132 ret = os_snprintf(buf + len, buflen - len, "EAP-%s\n",
2133 data->phase2_method ?
2134 data->phase2_method->name : "?");
2136 case EAP_TTLS_PHASE2_MSCHAPV2:
2137 ret = os_snprintf(buf + len, buflen - len, "MSCHAPV2\n");
2139 case EAP_TTLS_PHASE2_MSCHAP:
2140 ret = os_snprintf(buf + len, buflen - len, "MSCHAP\n");
2142 case EAP_TTLS_PHASE2_PAP:
2143 ret = os_snprintf(buf + len, buflen - len, "PAP\n");
2145 case EAP_TTLS_PHASE2_CHAP:
2146 ret = os_snprintf(buf + len, buflen - len, "CHAP\n");
2152 if (ret < 0 || (size_t) ret >= buflen - len)
2160 static Boolean eap_ttls_isKeyAvailable(struct eap_sm *sm, void *priv)
2162 struct eap_ttls_data *data = priv;
2163 return data->key_data != NULL && data->phase2_success;
2167 static u8 * eap_ttls_getKey(struct eap_sm *sm, void *priv, size_t *len)
2169 struct eap_ttls_data *data = priv;
2172 if (data->key_data == NULL || !data->phase2_success)
2175 key = os_malloc(EAP_TLS_KEY_LEN);
2179 *len = EAP_TLS_KEY_LEN;
2180 os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
2186 int eap_peer_ttls_register(void)
2188 struct eap_method *eap;
2191 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
2192 EAP_VENDOR_IETF, EAP_TYPE_TTLS, "TTLS");
2196 eap->init = eap_ttls_init;
2197 eap->deinit = eap_ttls_deinit;
2198 eap->process = eap_ttls_process;
2199 eap->isKeyAvailable = eap_ttls_isKeyAvailable;
2200 eap->getKey = eap_ttls_getKey;
2201 eap->get_status = eap_ttls_get_status;
2202 eap->has_reauth_data = eap_ttls_has_reauth_data;
2203 eap->deinit_for_reauth = eap_ttls_deinit_for_reauth;
2204 eap->init_for_reauth = eap_ttls_init_for_reauth;
2206 ret = eap_peer_method_register(eap);
2208 eap_peer_method_free(eap);