2 * EAP peer method: EAP-TTLS (RFC 5281)
3 * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
18 #include "crypto/ms_funcs.h"
19 #include "crypto/sha1.h"
20 #include "crypto/tls.h"
21 #include "eap_common/chap.h"
22 #include "eap_common/eap_ttls.h"
25 #include "eap_tls_common.h"
26 #include "eap_config.h"
29 /* Maximum supported TTLS version
31 * 1 = draft-funk-eap-ttls-v1-00.txt
33 #ifndef EAP_TTLS_VERSION
34 #define EAP_TTLS_VERSION 0 /* TTLSv1 implementation is not yet complete */
35 #endif /* EAP_TTLS_VERSION */
38 #define MSCHAPV2_KEY_LEN 16
39 #define MSCHAPV2_NT_RESPONSE_LEN 24
42 static void eap_ttls_deinit(struct eap_sm *sm, void *priv);
45 struct eap_ttls_data {
46 struct eap_ssl_data ssl;
49 int ttls_version, force_ttls_version;
51 const struct eap_method *phase2_method;
58 EAP_TTLS_PHASE2_MSCHAPV2,
59 EAP_TTLS_PHASE2_MSCHAP,
63 struct eap_method_type phase2_eap_type;
64 struct eap_method_type *phase2_eap_types;
65 size_t num_phase2_eap_types;
67 u8 auth_response[MSCHAPV2_AUTH_RESPONSE_LEN];
68 int auth_response_valid;
69 u8 master_key[MSCHAPV2_MASTER_KEY_LEN]; /* MSCHAPv2 master key */
71 int resuming; /* starting a resumed session */
72 int reauth; /* reauthentication */
75 struct wpabuf *pending_phase2_req;
76 int chbind_req_sent; /* channel binding request was sent */
84 /* draft-ietf-emu-chbind-13 section 5.3 */
101 static void * eap_ttls_init(struct eap_sm *sm)
103 struct eap_ttls_data *data;
104 struct eap_peer_config *config = eap_get_config(sm);
107 data = os_zalloc(sizeof(*data));
110 data->ttls_version = EAP_TTLS_VERSION;
111 data->force_ttls_version = -1;
113 data->phase2_type = EAP_TTLS_PHASE2_EAP;
115 #if EAP_TTLS_VERSION > 0
116 if (config && config->phase1) {
117 const char *pos = os_strstr(config->phase1, "ttlsver=");
119 data->force_ttls_version = atoi(pos + 8);
120 data->ttls_version = data->force_ttls_version;
121 wpa_printf(MSG_DEBUG, "EAP-TTLS: Forced TTLS version "
122 "%d", data->force_ttls_version);
125 #endif /* EAP_TTLS_VERSION */
127 if (config && config->phase2) {
128 if (os_strstr(config->phase2, "autheap=")) {
130 data->phase2_type = EAP_TTLS_PHASE2_EAP;
131 } else if (os_strstr(config->phase2, "auth=MSCHAPV2")) {
132 selected = "MSCHAPV2";
133 data->phase2_type = EAP_TTLS_PHASE2_MSCHAPV2;
134 } else if (os_strstr(config->phase2, "auth=MSCHAP")) {
136 data->phase2_type = EAP_TTLS_PHASE2_MSCHAP;
137 } else if (os_strstr(config->phase2, "auth=PAP")) {
139 data->phase2_type = EAP_TTLS_PHASE2_PAP;
140 } else if (os_strstr(config->phase2, "auth=CHAP")) {
142 data->phase2_type = EAP_TTLS_PHASE2_CHAP;
145 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase2 type: %s", selected);
147 if (data->phase2_type == EAP_TTLS_PHASE2_EAP) {
148 if (eap_peer_select_phase2_methods(config, "autheap=",
149 &data->phase2_eap_types,
150 &data->num_phase2_eap_types)
152 eap_ttls_deinit(sm, data);
156 data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
157 data->phase2_eap_type.method = EAP_TYPE_NONE;
160 #if EAP_TTLS_VERSION > 0
161 if (!(tls_capabilities(sm->ssl_ctx) & TLS_CAPABILITY_IA) &&
162 data->ttls_version > 0) {
163 if (data->force_ttls_version > 0) {
164 wpa_printf(MSG_INFO, "EAP-TTLS: Forced TTLSv%d and "
165 "TLS library does not support TLS/IA.",
166 data->force_ttls_version);
167 eap_ttls_deinit(sm, data);
170 data->ttls_version = 0;
172 #endif /* EAP_TTLS_VERSION */
178 static void eap_ttls_phase2_eap_deinit(struct eap_sm *sm,
179 struct eap_ttls_data *data)
181 if (data->phase2_priv && data->phase2_method) {
182 data->phase2_method->deinit(sm, data->phase2_priv);
183 data->phase2_method = NULL;
184 data->phase2_priv = NULL;
189 static void eap_ttls_deinit(struct eap_sm *sm, void *priv)
191 struct eap_ttls_data *data = priv;
194 eap_ttls_phase2_eap_deinit(sm, data);
195 os_free(data->phase2_eap_types);
196 if (data->ssl_initialized)
197 eap_peer_tls_ssl_deinit(sm, &data->ssl);
198 os_free(data->key_data);
199 wpabuf_free(data->pending_phase2_req);
204 static u8 * eap_ttls_avp_hdr(u8 *avphdr, u32 avp_code, u32 vendor_id,
205 int mandatory, size_t len)
207 struct ttls_avp_vendor *avp;
211 avp = (struct ttls_avp_vendor *) avphdr;
212 flags = mandatory ? AVP_FLAGS_MANDATORY : 0;
214 flags |= AVP_FLAGS_VENDOR;
215 hdrlen = sizeof(*avp);
216 avp->vendor_id = host_to_be32(vendor_id);
218 hdrlen = sizeof(struct ttls_avp);
221 avp->avp_code = host_to_be32(avp_code);
222 avp->avp_length = host_to_be32((flags << 24) | (hdrlen + len));
224 return avphdr + hdrlen;
228 static u8 * eap_ttls_avp_add(u8 *start, u8 *avphdr, u32 avp_code,
229 u32 vendor_id, int mandatory,
230 const u8 *data, size_t len)
233 pos = eap_ttls_avp_hdr(avphdr, avp_code, vendor_id, mandatory, len);
234 os_memcpy(pos, data, len);
241 static int eap_ttls_avp_encapsulate(struct wpabuf **resp, u32 avp_code,
247 msg = wpabuf_alloc(sizeof(struct ttls_avp) + wpabuf_len(*resp) + 4);
254 avp = wpabuf_mhead(msg);
255 pos = eap_ttls_avp_hdr(avp, avp_code, 0, mandatory, wpabuf_len(*resp));
256 os_memcpy(pos, wpabuf_head(*resp), wpabuf_len(*resp));
257 pos += wpabuf_len(*resp);
260 wpabuf_put(msg, pos - avp);
266 #if EAP_TTLS_VERSION > 0
267 static int eap_ttls_ia_permute_inner_secret(struct eap_sm *sm,
268 struct eap_ttls_data *data,
269 const u8 *key, size_t key_len)
276 buf_len = 2 + key_len;
277 buf = os_malloc(buf_len);
280 WPA_PUT_BE16(buf, key_len);
281 os_memcpy(buf + 2, key, key_len);
287 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Session keys for TLS/IA inner "
288 "secret permutation", buf, buf_len);
289 ret = tls_connection_ia_permute_inner_secret(sm->ssl_ctx,
296 #endif /* EAP_TTLS_VERSION */
299 static int eap_ttls_v0_derive_key(struct eap_sm *sm,
300 struct eap_ttls_data *data)
302 os_free(data->key_data);
303 data->key_data = eap_peer_tls_derive_key(sm, &data->ssl,
304 "ttls keying material",
306 if (!data->key_data) {
307 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to derive key");
311 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
312 data->key_data, EAP_TLS_KEY_LEN);
318 #if EAP_TTLS_VERSION > 0
319 static int eap_ttls_v1_derive_key(struct eap_sm *sm,
320 struct eap_ttls_data *data)
322 struct tls_keys keys;
325 os_free(data->key_data);
326 data->key_data = NULL;
328 os_memset(&keys, 0, sizeof(keys));
329 if (tls_connection_get_keys(sm->ssl_ctx, data->ssl.conn, &keys) ||
330 keys.client_random == NULL || keys.server_random == NULL ||
331 keys.inner_secret == NULL) {
332 wpa_printf(MSG_INFO, "EAP-TTLS: Could not get inner secret, "
333 "client random, or server random to derive keying "
338 rnd = os_malloc(keys.client_random_len + keys.server_random_len);
339 data->key_data = os_malloc(EAP_TLS_KEY_LEN);
340 if (rnd == NULL || data->key_data == NULL) {
341 wpa_printf(MSG_INFO, "EAP-TTLS: No memory for key derivation");
343 os_free(data->key_data);
344 data->key_data = NULL;
347 os_memcpy(rnd, keys.client_random, keys.client_random_len);
348 os_memcpy(rnd + keys.client_random_len, keys.server_random,
349 keys.server_random_len);
351 if (tls_prf(keys.inner_secret, keys.inner_secret_len,
352 "ttls v1 keying material", rnd, keys.client_random_len +
353 keys.server_random_len, data->key_data, EAP_TLS_KEY_LEN)) {
354 wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to derive key");
356 os_free(data->key_data);
357 data->key_data = NULL;
361 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: client/server random",
362 rnd, keys.client_random_len + keys.server_random_len);
363 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: TLS/IA inner secret",
364 keys.inner_secret, keys.inner_secret_len);
368 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
369 data->key_data, EAP_TLS_KEY_LEN);
373 #endif /* EAP_TTLS_VERSION */
376 static u8 * eap_ttls_implicit_challenge(struct eap_sm *sm,
377 struct eap_ttls_data *data, size_t len)
379 #if EAP_TTLS_VERSION > 0
380 struct tls_keys keys;
382 #endif /* EAP_TTLS_VERSION */
384 if (data->ttls_version == 0) {
385 return eap_peer_tls_derive_key(sm, &data->ssl,
386 "ttls challenge", len);
389 #if EAP_TTLS_VERSION > 0
391 os_memset(&keys, 0, sizeof(keys));
392 if (tls_connection_get_keys(sm->ssl_ctx, data->ssl.conn, &keys) ||
393 keys.client_random == NULL || keys.server_random == NULL ||
394 keys.inner_secret == NULL) {
395 wpa_printf(MSG_INFO, "EAP-TTLS: Could not get inner secret, "
396 "client random, or server random to derive "
397 "implicit challenge");
401 rnd = os_malloc(keys.client_random_len + keys.server_random_len);
402 challenge = os_malloc(len);
403 if (rnd == NULL || challenge == NULL) {
404 wpa_printf(MSG_INFO, "EAP-TTLS: No memory for implicit "
405 "challenge derivation");
410 os_memcpy(rnd, keys.server_random, keys.server_random_len);
411 os_memcpy(rnd + keys.server_random_len, keys.client_random,
412 keys.client_random_len);
414 if (tls_prf(keys.inner_secret, keys.inner_secret_len,
415 "inner application challenge", rnd,
416 keys.client_random_len + keys.server_random_len,
418 wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to derive implicit "
427 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived implicit challenge",
432 #else /* EAP_TTLS_VERSION */
436 #endif /* EAP_TTLS_VERSION */
440 static void eap_ttlsv1_phase2_eap_finish(struct eap_sm *sm,
441 struct eap_ttls_data *data,
442 struct eap_method_ret *ret)
444 #if EAP_TTLS_VERSION > 0
445 if (data->ttls_version > 0) {
446 const struct eap_method *m = data->phase2_method;
447 void *priv = data->phase2_priv;
449 /* TTLSv1 requires TLS/IA FinalPhaseFinished */
450 if (ret->decision == DECISION_UNCOND_SUCC)
451 ret->decision = DECISION_COND_SUCC;
452 ret->methodState = METHOD_CONT;
454 if (ret->decision == DECISION_COND_SUCC &&
455 m->isKeyAvailable && m->getKey &&
456 m->isKeyAvailable(sm, priv)) {
459 key = m->getKey(sm, priv, &key_len);
461 eap_ttls_ia_permute_inner_secret(
462 sm, data, key, key_len);
467 #endif /* EAP_TTLS_VERSION */
471 static void eap_ttls_phase2_select_eap_method(struct eap_ttls_data *data,
475 for (i = 0; i < data->num_phase2_eap_types; i++) {
476 if (data->phase2_eap_types[i].vendor != EAP_VENDOR_IETF ||
477 data->phase2_eap_types[i].method != method)
480 data->phase2_eap_type.vendor =
481 data->phase2_eap_types[i].vendor;
482 data->phase2_eap_type.method =
483 data->phase2_eap_types[i].method;
484 wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
485 "Phase 2 EAP vendor %d method %d",
486 data->phase2_eap_type.vendor,
487 data->phase2_eap_type.method);
493 static int eap_ttls_phase2_eap_process(struct eap_sm *sm,
494 struct eap_ttls_data *data,
495 struct eap_method_ret *ret,
496 struct eap_hdr *hdr, size_t len,
497 struct wpabuf **resp)
500 struct eap_method_ret iret;
502 os_memset(&iret, 0, sizeof(iret));
503 wpabuf_set(&msg, hdr, len);
504 *resp = data->phase2_method->process(sm, data->phase2_priv, &iret,
506 if ((iret.methodState == METHOD_DONE ||
507 iret.methodState == METHOD_MAY_CONT) &&
508 (iret.decision == DECISION_UNCOND_SUCC ||
509 iret.decision == DECISION_COND_SUCC ||
510 iret.decision == DECISION_FAIL)) {
511 ret->methodState = iret.methodState;
512 ret->decision = iret.decision;
514 eap_ttlsv1_phase2_eap_finish(sm, data, ret);
520 static int eap_ttls_phase2_request_eap_method(struct eap_sm *sm,
521 struct eap_ttls_data *data,
522 struct eap_method_ret *ret,
523 struct eap_hdr *hdr, size_t len,
524 u8 method, struct wpabuf **resp)
527 if (data->tnc_started && data->phase2_method &&
528 data->phase2_priv && method == EAP_TYPE_TNC &&
529 data->phase2_eap_type.method == EAP_TYPE_TNC)
530 return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len,
533 if (data->ready_for_tnc && !data->tnc_started &&
534 method == EAP_TYPE_TNC) {
535 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
537 data->tnc_started = 1;
540 if (data->tnc_started) {
541 if (data->phase2_eap_type.vendor != EAP_VENDOR_IETF ||
542 data->phase2_eap_type.method == EAP_TYPE_TNC) {
543 wpa_printf(MSG_DEBUG, "EAP-TTLS: Unexpected EAP "
544 "type %d for TNC", method);
548 data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
549 data->phase2_eap_type.method = method;
550 wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
551 "Phase 2 EAP vendor %d method %d (TNC)",
552 data->phase2_eap_type.vendor,
553 data->phase2_eap_type.method);
555 if (data->phase2_type == EAP_TTLS_PHASE2_EAP)
556 eap_ttls_phase2_eap_deinit(sm, data);
560 if (data->phase2_eap_type.vendor == EAP_VENDOR_IETF &&
561 data->phase2_eap_type.method == EAP_TYPE_NONE)
562 eap_ttls_phase2_select_eap_method(data, method);
564 if (method != data->phase2_eap_type.method || method == EAP_TYPE_NONE)
566 if (eap_peer_tls_phase2_nak(data->phase2_eap_types,
567 data->num_phase2_eap_types,
573 if (data->phase2_priv == NULL) {
574 data->phase2_method = eap_peer_get_eap_method(
575 EAP_VENDOR_IETF, method);
576 if (data->phase2_method) {
578 data->phase2_priv = data->phase2_method->init(sm);
582 if (data->phase2_priv == NULL || data->phase2_method == NULL) {
583 wpa_printf(MSG_INFO, "EAP-TTLS: failed to initialize "
584 "Phase 2 EAP method %d", method);
588 return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len, resp);
592 static int eap_ttls_phase2_request_eap(struct eap_sm *sm,
593 struct eap_ttls_data *data,
594 struct eap_method_ret *ret,
596 struct wpabuf **resp)
598 size_t len = be_to_host16(hdr->length);
600 struct eap_peer_config *config = eap_get_config(sm);
602 if (len <= sizeof(struct eap_hdr)) {
603 wpa_printf(MSG_INFO, "EAP-TTLS: too short "
604 "Phase 2 request (len=%lu)", (unsigned long) len);
607 pos = (u8 *) (hdr + 1);
608 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP Request: type=%d", *pos);
610 case EAP_TYPE_IDENTITY:
611 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
614 if (eap_ttls_phase2_request_eap_method(sm, data, ret, hdr, len,
621 (config->pending_req_identity || config->pending_req_password ||
622 config->pending_req_otp)) {
629 wpa_hexdump_buf(MSG_DEBUG, "EAP-TTLS: AVP encapsulate EAP Response",
631 return eap_ttls_avp_encapsulate(resp, RADIUS_ATTR_EAP_MESSAGE, 1);
635 static void eap_ttlsv1_permute_inner(struct eap_sm *sm,
636 struct eap_ttls_data *data)
638 #if EAP_TTLS_VERSION > 0
639 u8 session_key[2 * MSCHAPV2_KEY_LEN];
641 if (data->ttls_version == 0)
644 get_asymetric_start_key(data->master_key, session_key,
645 MSCHAPV2_KEY_LEN, 0, 0);
646 get_asymetric_start_key(data->master_key,
647 session_key + MSCHAPV2_KEY_LEN,
648 MSCHAPV2_KEY_LEN, 1, 0);
649 eap_ttls_ia_permute_inner_secret(sm, data, session_key,
650 sizeof(session_key));
651 #endif /* EAP_TTLS_VERSION */
655 static int eap_ttls_phase2_request_mschapv2(struct eap_sm *sm,
656 struct eap_ttls_data *data,
657 struct eap_method_ret *ret,
658 struct wpabuf **resp)
661 u8 *buf, *pos, *challenge, *peer_challenge;
662 const u8 *identity, *password;
663 size_t identity_len, password_len;
666 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAPV2 Request");
668 identity = eap_get_config_identity(sm, &identity_len);
669 password = eap_get_config_password2(sm, &password_len, &pwhash);
670 if (identity == NULL || password == NULL)
673 msg = wpabuf_alloc(identity_len + 1000);
675 wpa_printf(MSG_ERROR,
676 "EAP-TTLS/MSCHAPV2: Failed to allocate memory");
679 pos = buf = wpabuf_mhead(msg);
682 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
683 identity, identity_len);
685 /* MS-CHAP-Challenge */
686 challenge = eap_ttls_implicit_challenge(
687 sm, data, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN + 1);
688 if (challenge == NULL) {
690 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
691 "implicit challenge");
694 peer_challenge = challenge + 1 + EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
696 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
697 RADIUS_VENDOR_ID_MICROSOFT, 1,
698 challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
700 /* MS-CHAP2-Response */
701 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP2_RESPONSE,
702 RADIUS_VENDOR_ID_MICROSOFT, 1,
703 EAP_TTLS_MSCHAPV2_RESPONSE_LEN);
704 data->ident = challenge[EAP_TTLS_MSCHAPV2_CHALLENGE_LEN];
705 *pos++ = data->ident;
706 *pos++ = 0; /* Flags */
707 os_memcpy(pos, peer_challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
708 pos += EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
709 os_memset(pos, 0, 8); /* Reserved, must be zero */
711 if (mschapv2_derive_response(identity, identity_len, password,
712 password_len, pwhash, challenge,
713 peer_challenge, pos, data->auth_response,
716 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
720 data->auth_response_valid = 1;
722 eap_ttlsv1_permute_inner(sm, data);
728 wpabuf_put(msg, pos - buf);
731 if (sm->workaround && data->ttls_version == 0) {
732 /* At least FreeRADIUS seems to be terminating
733 * EAP-TTLS/MSHCAPV2 without the expected MS-CHAP-v2 Success
735 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: EAP workaround - "
736 "allow success without tunneled response");
737 ret->methodState = METHOD_MAY_CONT;
738 ret->decision = DECISION_COND_SUCC;
745 static int eap_ttls_phase2_request_mschap(struct eap_sm *sm,
746 struct eap_ttls_data *data,
747 struct eap_method_ret *ret,
748 struct wpabuf **resp)
751 u8 *buf, *pos, *challenge;
752 const u8 *identity, *password;
753 size_t identity_len, password_len;
756 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAP Request");
758 identity = eap_get_config_identity(sm, &identity_len);
759 password = eap_get_config_password2(sm, &password_len, &pwhash);
760 if (identity == NULL || password == NULL)
763 msg = wpabuf_alloc(identity_len + 1000);
765 wpa_printf(MSG_ERROR,
766 "EAP-TTLS/MSCHAP: Failed to allocate memory");
769 pos = buf = wpabuf_mhead(msg);
772 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
773 identity, identity_len);
775 /* MS-CHAP-Challenge */
776 challenge = eap_ttls_implicit_challenge(
777 sm, data, EAP_TTLS_MSCHAP_CHALLENGE_LEN + 1);
778 if (challenge == NULL) {
780 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAP: Failed to derive "
781 "implicit challenge");
785 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
786 RADIUS_VENDOR_ID_MICROSOFT, 1,
787 challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
789 /* MS-CHAP-Response */
790 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP_RESPONSE,
791 RADIUS_VENDOR_ID_MICROSOFT, 1,
792 EAP_TTLS_MSCHAP_RESPONSE_LEN);
793 data->ident = challenge[EAP_TTLS_MSCHAP_CHALLENGE_LEN];
794 *pos++ = data->ident;
795 *pos++ = 1; /* Flags: Use NT style passwords */
796 os_memset(pos, 0, 24); /* LM-Response */
799 challenge_response(challenge, password, pos); /* NT-Response */
800 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password hash",
803 nt_challenge_response(challenge, password, password_len,
804 pos); /* NT-Response */
805 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password",
806 password, password_len);
808 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP implicit challenge",
809 challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
810 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP response", pos, 24);
815 wpabuf_put(msg, pos - buf);
818 if (data->ttls_version > 0) {
819 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
820 * so do not allow connection to be terminated yet. */
821 ret->methodState = METHOD_CONT;
822 ret->decision = DECISION_COND_SUCC;
824 /* EAP-TTLS/MSCHAP does not provide tunneled success
825 * notification, so assume that Phase2 succeeds. */
826 ret->methodState = METHOD_DONE;
827 ret->decision = DECISION_COND_SUCC;
834 static int eap_ttls_phase2_request_pap(struct eap_sm *sm,
835 struct eap_ttls_data *data,
836 struct eap_method_ret *ret,
837 struct wpabuf **resp)
842 const u8 *identity, *password;
843 size_t identity_len, password_len;
845 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 PAP Request");
847 identity = eap_get_config_identity(sm, &identity_len);
848 password = eap_get_config_password(sm, &password_len);
849 if (identity == NULL || password == NULL)
852 msg = wpabuf_alloc(identity_len + password_len + 100);
854 wpa_printf(MSG_ERROR,
855 "EAP-TTLS/PAP: Failed to allocate memory");
858 pos = buf = wpabuf_mhead(msg);
861 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
862 identity, identity_len);
864 /* User-Password; in RADIUS, this is encrypted, but EAP-TTLS encrypts
865 * the data, so no separate encryption is used in the AVP itself.
866 * However, the password is padded to obfuscate its length. */
867 pad = password_len == 0 ? 16 : (16 - (password_len & 15)) & 15;
868 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_USER_PASSWORD, 0, 1,
870 os_memcpy(pos, password, password_len);
872 os_memset(pos, 0, pad);
876 wpabuf_put(msg, pos - buf);
879 if (data->ttls_version > 0) {
880 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
881 * so do not allow connection to be terminated yet. */
882 ret->methodState = METHOD_CONT;
883 ret->decision = DECISION_COND_SUCC;
885 /* EAP-TTLS/PAP does not provide tunneled success notification,
886 * so assume that Phase2 succeeds. */
887 ret->methodState = METHOD_DONE;
888 ret->decision = DECISION_COND_SUCC;
895 static int eap_ttls_phase2_request_chap(struct eap_sm *sm,
896 struct eap_ttls_data *data,
897 struct eap_method_ret *ret,
898 struct wpabuf **resp)
901 u8 *buf, *pos, *challenge;
902 const u8 *identity, *password;
903 size_t identity_len, password_len;
905 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 CHAP Request");
907 identity = eap_get_config_identity(sm, &identity_len);
908 password = eap_get_config_password(sm, &password_len);
909 if (identity == NULL || password == NULL)
912 msg = wpabuf_alloc(identity_len + 1000);
914 wpa_printf(MSG_ERROR,
915 "EAP-TTLS/CHAP: Failed to allocate memory");
918 pos = buf = wpabuf_mhead(msg);
921 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
922 identity, identity_len);
925 challenge = eap_ttls_implicit_challenge(
926 sm, data, EAP_TTLS_CHAP_CHALLENGE_LEN + 1);
927 if (challenge == NULL) {
929 wpa_printf(MSG_ERROR, "EAP-TTLS/CHAP: Failed to derive "
930 "implicit challenge");
934 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_CHAP_CHALLENGE, 0, 1,
935 challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
938 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_CHAP_PASSWORD, 0, 1,
939 1 + EAP_TTLS_CHAP_PASSWORD_LEN);
940 data->ident = challenge[EAP_TTLS_CHAP_CHALLENGE_LEN];
941 *pos++ = data->ident;
943 /* MD5(Ident + Password + Challenge) */
944 chap_md5(data->ident, password, password_len, challenge,
945 EAP_TTLS_CHAP_CHALLENGE_LEN, pos);
947 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: CHAP username",
948 identity, identity_len);
949 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: CHAP password",
950 password, password_len);
951 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP implicit challenge",
952 challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
953 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP password",
954 pos, EAP_TTLS_CHAP_PASSWORD_LEN);
955 pos += EAP_TTLS_CHAP_PASSWORD_LEN;
959 wpabuf_put(msg, pos - buf);
962 if (data->ttls_version > 0) {
963 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
964 * so do not allow connection to be terminated yet. */
965 ret->methodState = METHOD_CONT;
966 ret->decision = DECISION_COND_SUCC;
968 /* EAP-TTLS/CHAP does not provide tunneled success
969 * notification, so assume that Phase2 succeeds. */
970 ret->methodState = METHOD_DONE;
971 ret->decision = DECISION_COND_SUCC;
978 static int eap_ttls_phase2_request(struct eap_sm *sm,
979 struct eap_ttls_data *data,
980 struct eap_method_ret *ret,
982 struct wpabuf **resp)
986 enum phase2_types phase2_type = data->phase2_type;
989 if (data->tnc_started) {
990 wpa_printf(MSG_DEBUG, "EAP-TTLS: Processing TNC");
991 phase2_type = EAP_TTLS_PHASE2_EAP;
995 if (phase2_type == EAP_TTLS_PHASE2_MSCHAPV2 ||
996 phase2_type == EAP_TTLS_PHASE2_MSCHAP ||
997 phase2_type == EAP_TTLS_PHASE2_PAP ||
998 phase2_type == EAP_TTLS_PHASE2_CHAP) {
999 if (eap_get_config_identity(sm, &len) == NULL) {
1000 wpa_printf(MSG_INFO,
1001 "EAP-TTLS: Identity not configured");
1002 eap_sm_request_identity(sm);
1003 if (eap_get_config_password(sm, &len) == NULL)
1004 eap_sm_request_password(sm);
1008 if (eap_get_config_password(sm, &len) == NULL) {
1009 wpa_printf(MSG_INFO,
1010 "EAP-TTLS: Password not configured");
1011 eap_sm_request_password(sm);
1016 switch (phase2_type) {
1017 case EAP_TTLS_PHASE2_EAP:
1018 res = eap_ttls_phase2_request_eap(sm, data, ret, hdr, resp);
1020 case EAP_TTLS_PHASE2_MSCHAPV2:
1021 res = eap_ttls_phase2_request_mschapv2(sm, data, ret, resp);
1023 case EAP_TTLS_PHASE2_MSCHAP:
1024 res = eap_ttls_phase2_request_mschap(sm, data, ret, resp);
1026 case EAP_TTLS_PHASE2_PAP:
1027 res = eap_ttls_phase2_request_pap(sm, data, ret, resp);
1029 case EAP_TTLS_PHASE2_CHAP:
1030 res = eap_ttls_phase2_request_chap(sm, data, ret, resp);
1033 wpa_printf(MSG_ERROR, "EAP-TTLS: Phase 2 - Unknown");
1039 ret->methodState = METHOD_DONE;
1040 ret->decision = DECISION_FAIL;
1047 #if EAP_TTLS_VERSION > 0
1048 static struct wpabuf * eap_ttls_build_phase_finished(
1049 struct eap_sm *sm, struct eap_ttls_data *data, int id, int final)
1051 struct wpabuf *req, *buf;
1053 buf = tls_connection_ia_send_phase_finished(sm->ssl_ctx,
1059 req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TTLS,
1060 1 + wpabuf_len(buf),
1061 EAP_CODE_RESPONSE, id);
1067 wpabuf_put_u8(req, data->ttls_version);
1068 wpabuf_put_buf(req, buf);
1070 eap_update_len(req);
1074 #endif /* EAP_TTLS_VERSION */
1077 struct ttls_parse_avp {
1087 static int eap_ttls_parse_attr_eap(const u8 *dpos, size_t dlen,
1088 struct ttls_parse_avp *parse)
1090 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - EAP Message");
1091 if (parse->eapdata == NULL) {
1092 parse->eapdata = os_malloc(dlen);
1093 if (parse->eapdata == NULL) {
1094 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
1095 "memory for Phase 2 EAP data");
1098 os_memcpy(parse->eapdata, dpos, dlen);
1099 parse->eap_len = dlen;
1101 u8 *neweap = os_realloc(parse->eapdata, parse->eap_len + dlen);
1102 if (neweap == NULL) {
1103 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
1104 "memory for Phase 2 EAP data");
1107 os_memcpy(neweap + parse->eap_len, dpos, dlen);
1108 parse->eapdata = neweap;
1109 parse->eap_len += dlen;
1116 static int eap_ttls_parse_attr_chbind(const u8 *dpos, size_t dlen,
1117 struct ttls_parse_avp *parse)
1119 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - Channel Binding Message");
1121 if (parse->chbind_data == NULL) {
1122 parse->chbind_data = os_malloc(dlen);
1123 if (parse->chbind_data == NULL) {
1124 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
1125 "memory for Phase 2 channel binding data");
1128 os_memcpy(parse->chbind_data, dpos, dlen);
1129 parse->chbind_len = dlen;
1131 /* TODO: can this really happen? maybe just make this an error? */
1132 u8 *newchbind = os_realloc(parse->chbind_data,
1133 parse->chbind_len + dlen);
1134 if (newchbind == NULL) {
1135 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
1136 "memory for Phase 2 channel binding data");
1139 os_memcpy(newchbind + parse->chbind_len, dpos, dlen);
1140 parse->chbind_data = newchbind;
1141 parse->chbind_len += dlen;
1148 static int eap_ttls_parse_avp(u8 *pos, size_t left,
1149 struct ttls_parse_avp *parse)
1151 struct ttls_avp *avp;
1152 u32 avp_code, avp_length, vendor_id = 0;
1153 u8 avp_flags, *dpos;
1156 avp = (struct ttls_avp *) pos;
1157 avp_code = be_to_host32(avp->avp_code);
1158 avp_length = be_to_host32(avp->avp_length);
1159 avp_flags = (avp_length >> 24) & 0xff;
1160 avp_length &= 0xffffff;
1161 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP: code=%d flags=0x%02x "
1162 "length=%d", (int) avp_code, avp_flags,
1165 if (avp_length > left) {
1166 wpa_printf(MSG_WARNING, "EAP-TTLS: AVP overflow "
1167 "(len=%d, left=%lu) - dropped",
1168 (int) avp_length, (unsigned long) left);
1172 if (avp_length < sizeof(*avp)) {
1173 wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid AVP length %d",
1178 dpos = (u8 *) (avp + 1);
1179 dlen = avp_length - sizeof(*avp);
1180 if (avp_flags & AVP_FLAGS_VENDOR) {
1182 wpa_printf(MSG_WARNING, "EAP-TTLS: Vendor AVP "
1186 vendor_id = WPA_GET_BE32(dpos);
1187 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP vendor_id %d",
1193 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP data", dpos, dlen);
1195 if (vendor_id == 0 && avp_code == RADIUS_ATTR_EAP_MESSAGE) {
1196 if (eap_ttls_parse_attr_eap(dpos, dlen, parse) < 0)
1198 } else if (vendor_id == 0 &&
1199 avp_code == DIAMETER_ATTR_CHBIND_MESSAGE) {
1200 /* message containing channel binding data */
1201 if (eap_ttls_parse_attr_chbind(dpos, dlen, parse) < 0)
1203 } else if (vendor_id == 0 && avp_code == RADIUS_ATTR_REPLY_MESSAGE) {
1204 /* This is an optional message that can be displayed to
1206 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: AVP - Reply-Message",
1208 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
1209 avp_code == RADIUS_ATTR_MS_CHAP2_SUCCESS) {
1210 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP2-Success",
1213 wpa_printf(MSG_WARNING, "EAP-TTLS: Unexpected "
1214 "MS-CHAP2-Success length "
1215 "(len=%lu, expected 43)",
1216 (unsigned long) dlen);
1219 parse->mschapv2 = dpos;
1220 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
1221 avp_code == RADIUS_ATTR_MS_CHAP_ERROR) {
1222 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP-Error",
1224 parse->mschapv2_error = 1;
1225 } else if (avp_flags & AVP_FLAGS_MANDATORY) {
1226 wpa_printf(MSG_WARNING, "EAP-TTLS: Unsupported mandatory AVP "
1227 "code %d vendor_id %d - dropped",
1228 (int) avp_code, (int) vendor_id);
1231 wpa_printf(MSG_DEBUG, "EAP-TTLS: Ignoring unsupported AVP "
1232 "code %d vendor_id %d",
1233 (int) avp_code, (int) vendor_id);
1240 static int eap_ttls_parse_avps(struct wpabuf *in_decrypted,
1241 struct ttls_parse_avp *parse)
1247 pos = wpabuf_mhead(in_decrypted);
1248 left = wpabuf_len(in_decrypted);
1249 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Decrypted Phase 2 AVPs", pos, left);
1250 if (left < sizeof(struct ttls_avp)) {
1251 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 AVP frame"
1252 " len=%lu expected %lu or more - dropped",
1253 (unsigned long) left,
1254 (unsigned long) sizeof(struct ttls_avp));
1259 os_memset(parse, 0, sizeof(*parse));
1262 avp_length = eap_ttls_parse_avp(pos, left, parse);
1266 pad = (4 - (avp_length & 3)) & 3;
1267 pos += avp_length + pad;
1268 if (left < avp_length + pad)
1271 left -= avp_length + pad;
1278 static u8 * eap_ttls_fake_identity_request(void)
1280 struct eap_hdr *hdr;
1283 wpa_printf(MSG_DEBUG, "EAP-TTLS: empty data in beginning of "
1284 "Phase 2 - use fake EAP-Request Identity");
1285 buf = os_malloc(sizeof(*hdr) + 1);
1287 wpa_printf(MSG_WARNING, "EAP-TTLS: failed to allocate "
1288 "memory for fake EAP-Identity Request");
1292 hdr = (struct eap_hdr *) buf;
1293 hdr->code = EAP_CODE_REQUEST;
1294 hdr->identifier = 0;
1295 hdr->length = host_to_be16(sizeof(*hdr) + 1);
1296 buf[sizeof(*hdr)] = EAP_TYPE_IDENTITY;
1302 static int eap_ttls_encrypt_response(struct eap_sm *sm,
1303 struct eap_ttls_data *data,
1304 struct wpabuf *resp, u8 identifier,
1305 struct wpabuf **out_data)
1310 wpa_hexdump_buf_key(MSG_DEBUG, "EAP-TTLS: Encrypting Phase 2 data",
1312 if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
1313 data->ttls_version, identifier,
1315 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to encrypt a Phase 2 "
1324 static int eap_ttls_add_chbind_request(struct eap_sm *sm,
1325 struct eap_ttls_data *data,
1326 struct wpabuf **resp)
1328 struct wpabuf *chbind_req, *res;
1330 struct eap_peer_config *config = eap_get_config(sm);
1332 if (!config->chbind_config || config->chbind_config_len <= 0)
1335 for (i=0; i<config->chbind_config_len; i++) {
1336 length += 3 + config->chbind_config[i].req_data_len;
1339 chbind_req = wpabuf_alloc(length);
1343 wpabuf_put_u8(chbind_req, CHBIND_CODE_REQUEST);
1344 for (i=0; i<config->chbind_config_len; i++) {
1345 struct eap_peer_chbind_config *chbind_config =
1346 &config->chbind_config[i];
1347 wpabuf_put_be16(chbind_req, chbind_config->req_data_len);
1348 wpabuf_put_u8(chbind_req, chbind_config->nsid);
1349 wpabuf_put_data(chbind_req, chbind_config->req_data,
1350 chbind_config->req_data_len);
1352 if (eap_ttls_avp_encapsulate(&chbind_req,
1353 DIAMETER_ATTR_CHBIND_MESSAGE, 0) < 0)
1356 /* bleh. This will free *resp regardless of whether combined buffer
1357 alloc succeeds, which is not consistent with the other error
1358 condition behavior in this function */
1359 *resp = wpabuf_concat(chbind_req, *resp);
1361 return (*resp) ? 0 : -1;
1365 static int eap_ttls_process_chbind(struct eap_sm *sm,
1366 struct eap_ttls_data *data,
1367 struct eap_method_ret *ret,
1368 struct ttls_parse_avp *parse,
1369 struct wpabuf **resp)
1374 struct chbind_hdr *hdr;
1375 struct eap_peer_config *config = eap_get_config(sm);
1377 if (parse->chbind_data == NULL) {
1378 wpa_printf(MSG_WARNING, "EAP-TTLS: No channel binding message "
1379 "in the packet - dropped");
1382 if (parse->chbind_len < 1 + sizeof(*hdr)) {
1383 wpa_printf(MSG_WARNING, "EAP-TTLS: bad channel binding response "
1384 "frame (len=%lu, expected %lu or more) - dropped",
1385 (unsigned long) parse->chbind_len,
1386 (unsigned long) sizeof(*hdr));
1389 code = parse->chbind_data[pos++];
1390 while (pos+sizeof(*hdr) < parse->chbind_len) {
1391 hdr = (struct chbind_hdr *)(&parse->chbind_data[pos]);
1392 pos += sizeof(*hdr);
1393 len = be_to_host16(hdr->len);
1394 if (pos + len <= parse->chbind_len) {
1396 for (i=0; i<config->chbind_config_len; i++) {
1397 struct eap_peer_chbind_config *chbind_config =
1398 &config->chbind_config[i];
1399 if (chbind_config->nsid == hdr->nsid)
1400 chbind_config->response_cb(
1403 &parse->chbind_data[pos], len);
1408 if (pos != parse->chbind_len) {
1409 wpa_printf(MSG_WARNING, "EAP-TTLS: bad channel binding response "
1410 "frame (parsed len=%lu, expected %lu) - dropped",
1411 (unsigned long) pos,
1412 (unsigned long) parse->chbind_len);
1418 static int eap_ttls_process_phase2_eap(struct eap_sm *sm,
1419 struct eap_ttls_data *data,
1420 struct eap_method_ret *ret,
1421 struct ttls_parse_avp *parse,
1422 struct wpabuf **resp)
1424 struct eap_hdr *hdr;
1427 if (parse->eapdata == NULL) {
1428 wpa_printf(MSG_WARNING, "EAP-TTLS: No EAP Message in the "
1429 "packet - dropped");
1433 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP",
1434 parse->eapdata, parse->eap_len);
1435 hdr = (struct eap_hdr *) parse->eapdata;
1437 if (parse->eap_len < sizeof(*hdr)) {
1438 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 EAP "
1439 "frame (len=%lu, expected %lu or more) - dropped",
1440 (unsigned long) parse->eap_len,
1441 (unsigned long) sizeof(*hdr));
1444 len = be_to_host16(hdr->length);
1445 if (len > parse->eap_len) {
1446 wpa_printf(MSG_INFO, "EAP-TTLS: Length mismatch in Phase 2 "
1447 "EAP frame (EAP hdr len=%lu, EAP data len in "
1449 (unsigned long) len,
1450 (unsigned long) parse->eap_len);
1453 wpa_printf(MSG_DEBUG, "EAP-TTLS: received Phase 2: code=%d "
1454 "identifier=%d length=%lu",
1455 hdr->code, hdr->identifier, (unsigned long) len);
1456 switch (hdr->code) {
1457 case EAP_CODE_REQUEST:
1458 if (eap_ttls_phase2_request(sm, data, ret, hdr, resp)) {
1459 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
1460 "processing failed");
1465 wpa_printf(MSG_INFO, "EAP-TTLS: Unexpected code=%d in "
1466 "Phase 2 EAP header", hdr->code);
1474 static int eap_ttls_process_phase2_mschapv2(struct eap_sm *sm,
1475 struct eap_ttls_data *data,
1476 struct eap_method_ret *ret,
1477 struct ttls_parse_avp *parse)
1479 if (parse->mschapv2_error) {
1480 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Received "
1481 "MS-CHAP-Error - failed");
1482 ret->methodState = METHOD_DONE;
1483 ret->decision = DECISION_FAIL;
1484 /* Reply with empty data to ACK error */
1488 if (parse->mschapv2 == NULL) {
1490 if (data->phase2_success && parse->eapdata) {
1492 * Allow EAP-TNC to be started after successfully
1493 * completed MSCHAPV2.
1497 #endif /* EAP_TNC */
1498 wpa_printf(MSG_WARNING, "EAP-TTLS: no MS-CHAP2-Success AVP "
1499 "received for Phase2 MSCHAPV2");
1502 if (parse->mschapv2[0] != data->ident) {
1503 wpa_printf(MSG_WARNING, "EAP-TTLS: Ident mismatch for Phase 2 "
1504 "MSCHAPV2 (received Ident 0x%02x, expected 0x%02x)",
1505 parse->mschapv2[0], data->ident);
1508 if (!data->auth_response_valid ||
1509 mschapv2_verify_auth_response(data->auth_response,
1510 parse->mschapv2 + 1, 42)) {
1511 wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid authenticator "
1512 "response in Phase 2 MSCHAPV2 success request");
1516 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 MSCHAPV2 "
1517 "authentication succeeded");
1518 if (data->ttls_version > 0) {
1520 * EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report
1521 * success, so do not allow connection to be terminated
1524 ret->methodState = METHOD_CONT;
1525 ret->decision = DECISION_COND_SUCC;
1527 ret->methodState = METHOD_DONE;
1528 ret->decision = DECISION_UNCOND_SUCC;
1529 data->phase2_success = 1;
1533 * Reply with empty data; authentication server will reply
1534 * with EAP-Success after this.
1541 static int eap_ttls_process_tnc_start(struct eap_sm *sm,
1542 struct eap_ttls_data *data,
1543 struct eap_method_ret *ret,
1544 struct ttls_parse_avp *parse,
1545 struct wpabuf **resp)
1547 /* TNC uses inner EAP method after non-EAP TTLS phase 2. */
1548 if (parse->eapdata == NULL) {
1549 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
1550 "unexpected tunneled data (no EAP)");
1554 if (!data->ready_for_tnc) {
1555 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
1556 "EAP after non-EAP, but not ready for TNC");
1560 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
1562 data->tnc_started = 1;
1564 if (eap_ttls_process_phase2_eap(sm, data, ret, parse, resp) < 0)
1569 #endif /* EAP_TNC */
1572 static int eap_ttls_process_decrypted(struct eap_sm *sm,
1573 struct eap_ttls_data *data,
1574 struct eap_method_ret *ret,
1576 struct ttls_parse_avp *parse,
1577 struct wpabuf *in_decrypted,
1578 struct wpabuf **out_data)
1580 struct wpabuf *resp = NULL;
1581 struct eap_peer_config *config = eap_get_config(sm);
1583 enum phase2_types phase2_type = data->phase2_type;
1586 if (data->tnc_started)
1587 phase2_type = EAP_TTLS_PHASE2_EAP;
1588 #endif /* EAP_TNC */
1590 switch (phase2_type) {
1591 case EAP_TTLS_PHASE2_EAP:
1592 if (eap_ttls_process_phase2_eap(sm, data, ret, parse, &resp) <
1596 case EAP_TTLS_PHASE2_MSCHAPV2:
1597 res = eap_ttls_process_phase2_mschapv2(sm, data, ret, parse);
1599 if (res == 1 && parse->eapdata && data->phase2_success) {
1601 * TNC may be required as the next
1602 * authentication method within the tunnel.
1604 ret->methodState = METHOD_MAY_CONT;
1605 data->ready_for_tnc = 1;
1606 if (eap_ttls_process_tnc_start(sm, data, ret, parse,
1610 #endif /* EAP_TNC */
1612 case EAP_TTLS_PHASE2_MSCHAP:
1613 case EAP_TTLS_PHASE2_PAP:
1614 case EAP_TTLS_PHASE2_CHAP:
1616 if (eap_ttls_process_tnc_start(sm, data, ret, parse, &resp) <
1621 /* EAP-TTLS/{MSCHAP,PAP,CHAP} should not send any TLS tunneled
1622 * requests to the supplicant */
1623 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received unexpected "
1626 #endif /* EAP_TNC */
1629 if (!resp && (config->pending_req_identity ||
1630 config->pending_req_password ||
1631 config->pending_req_otp ||
1632 config->pending_req_new_password)) {
1633 wpabuf_free(data->pending_phase2_req);
1634 data->pending_phase2_req = wpabuf_dup(in_decrypted);
1638 /* handle channel binding response here */
1639 if (parse->chbind_data) {
1640 /* received channel binding repsonse */
1641 if (eap_ttls_process_chbind(sm, data, ret, parse, &resp) < 0)
1644 /* issue channel binding request when appropriate */
1645 if (config->chbind_config && config->chbind_config_len > 0 &&
1646 !data->chbind_req_sent) {
1647 if (eap_ttls_add_chbind_request(sm, data, &resp) < 0)
1649 data->chbind_req_sent = 1;
1653 if (eap_ttls_encrypt_response(sm, data, resp, identifier,
1662 #if EAP_TTLS_VERSION > 0
1663 static void eap_ttls_final_phase_finished(struct eap_sm *sm,
1664 struct eap_ttls_data *data,
1665 struct eap_method_ret *ret,
1667 struct wpabuf **out_data)
1669 wpa_printf(MSG_DEBUG, "EAP-TTLS: FinalPhaseFinished received");
1670 wpa_printf(MSG_INFO, "EAP-TTLS: TLS/IA authentication succeeded");
1671 ret->methodState = METHOD_DONE;
1672 ret->decision = DECISION_UNCOND_SUCC;
1673 data->phase2_success = 1;
1674 *out_data = eap_ttls_build_phase_finished(sm, data, identifier, 1);
1675 eap_ttls_v1_derive_key(sm, data);
1677 #endif /* EAP_TTLS_VERSION */
1680 static int eap_ttls_implicit_identity_request(struct eap_sm *sm,
1681 struct eap_ttls_data *data,
1682 struct eap_method_ret *ret,
1684 struct wpabuf **out_data)
1687 struct eap_hdr *hdr;
1688 struct wpabuf *resp;
1690 hdr = (struct eap_hdr *) eap_ttls_fake_identity_request();
1692 ret->methodState = METHOD_DONE;
1693 ret->decision = DECISION_FAIL;
1698 if (eap_ttls_phase2_request(sm, data, ret, hdr, &resp)) {
1699 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
1700 "processing failed");
1703 retval = eap_ttls_encrypt_response(sm, data, resp, identifier,
1710 ret->methodState = METHOD_DONE;
1711 ret->decision = DECISION_FAIL;
1718 static int eap_ttls_phase2_start(struct eap_sm *sm, struct eap_ttls_data *data,
1719 struct eap_method_ret *ret, u8 identifier,
1720 struct wpabuf **out_data)
1722 data->phase2_start = 0;
1725 * EAP-TTLS does not use Phase2 on fast re-auth; this must be done only
1726 * if TLS part was indeed resuming a previous session. Most
1727 * Authentication Servers terminate EAP-TTLS before reaching this
1728 * point, but some do not. Make wpa_supplicant stop phase 2 here, if
1732 tls_connection_resumed(sm->ssl_ctx, data->ssl.conn)) {
1733 wpa_printf(MSG_DEBUG, "EAP-TTLS: Session resumption - "
1735 *out_data = eap_peer_tls_build_ack(identifier, EAP_TYPE_TTLS,
1736 data->ttls_version);
1737 ret->methodState = METHOD_DONE;
1738 ret->decision = DECISION_UNCOND_SUCC;
1739 data->phase2_success = 1;
1743 return eap_ttls_implicit_identity_request(sm, data, ret, identifier,
1748 static int eap_ttls_decrypt(struct eap_sm *sm, struct eap_ttls_data *data,
1749 struct eap_method_ret *ret, u8 identifier,
1750 const struct wpabuf *in_data,
1751 struct wpabuf **out_data)
1753 struct wpabuf *in_decrypted = NULL;
1755 struct ttls_parse_avp parse;
1757 os_memset(&parse, 0, sizeof(parse));
1759 wpa_printf(MSG_DEBUG, "EAP-TTLS: received %lu bytes encrypted data for"
1761 in_data ? (unsigned long) wpabuf_len(in_data) : 0);
1763 if (data->pending_phase2_req) {
1764 wpa_printf(MSG_DEBUG, "EAP-TTLS: Pending Phase 2 request - "
1765 "skip decryption and use old data");
1766 /* Clear TLS reassembly state. */
1767 eap_peer_tls_reset_input(&data->ssl);
1769 in_decrypted = data->pending_phase2_req;
1770 data->pending_phase2_req = NULL;
1771 if (wpabuf_len(in_decrypted) == 0) {
1772 wpabuf_free(in_decrypted);
1773 return eap_ttls_implicit_identity_request(
1774 sm, data, ret, identifier, out_data);
1779 if ((in_data == NULL || wpabuf_len(in_data) == 0) &&
1780 data->phase2_start) {
1781 return eap_ttls_phase2_start(sm, data, ret, identifier,
1785 if (in_data == NULL || wpabuf_len(in_data) == 0) {
1786 /* Received TLS ACK - requesting more fragments */
1787 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
1789 identifier, NULL, out_data);
1792 retval = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
1796 #if EAP_TTLS_VERSION > 0
1797 if (data->ttls_version > 0 &&
1798 (in_decrypted == NULL || wpabuf_len(in_decrypted) == 0) &&
1799 tls_connection_ia_final_phase_finished(sm->ssl_ctx,
1801 eap_ttls_final_phase_finished(sm, data, ret, identifier,
1805 #endif /* EAP_TTLS_VERSION */
1808 data->phase2_start = 0;
1810 if (eap_ttls_parse_avps(in_decrypted, &parse) < 0) {
1815 retval = eap_ttls_process_decrypted(sm, data, ret, identifier,
1816 &parse, in_decrypted, out_data);
1819 wpabuf_free(in_decrypted);
1820 os_free(parse.eapdata);
1823 ret->methodState = METHOD_DONE;
1824 ret->decision = DECISION_FAIL;
1831 static int eap_ttls_process_start(struct eap_sm *sm,
1832 struct eap_ttls_data *data, u8 flags,
1833 struct eap_method_ret *ret)
1835 struct eap_peer_config *config = eap_get_config(sm);
1837 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start (server ver=%d, own ver=%d)",
1838 flags & EAP_TLS_VERSION_MASK, data->ttls_version);
1839 #if EAP_TTLS_VERSION > 0
1840 if ((flags & EAP_TLS_VERSION_MASK) < data->ttls_version)
1841 data->ttls_version = flags & EAP_TLS_VERSION_MASK;
1842 if (data->force_ttls_version >= 0 &&
1843 data->force_ttls_version != data->ttls_version) {
1844 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to select "
1845 "forced TTLS version %d",
1846 data->force_ttls_version);
1847 ret->methodState = METHOD_DONE;
1848 ret->decision = DECISION_FAIL;
1849 ret->allowNotifications = FALSE;
1852 wpa_printf(MSG_DEBUG, "EAP-TTLS: Using TTLS version %d",
1853 data->ttls_version);
1855 if (data->ttls_version > 0)
1856 data->ssl.tls_ia = 1;
1857 #endif /* EAP_TTLS_VERSION */
1858 if (!data->ssl_initialized &&
1859 eap_peer_tls_ssl_init(sm, &data->ssl, config)) {
1860 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to initialize SSL.");
1863 data->ssl_initialized = 1;
1865 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start");
1871 static int eap_ttls_process_handshake(struct eap_sm *sm,
1872 struct eap_ttls_data *data,
1873 struct eap_method_ret *ret,
1875 const u8 *in_data, size_t in_len,
1876 struct wpabuf **out_data)
1880 res = eap_peer_tls_process_helper(sm, &data->ssl, EAP_TYPE_TTLS,
1881 data->ttls_version, identifier,
1882 in_data, in_len, out_data);
1884 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1885 wpa_printf(MSG_DEBUG, "EAP-TTLS: TLS done, proceed to "
1887 if (data->resuming) {
1888 wpa_printf(MSG_DEBUG, "EAP-TTLS: fast reauth - may "
1890 ret->decision = DECISION_COND_SUCC;
1891 ret->methodState = METHOD_MAY_CONT;
1893 data->phase2_start = 1;
1894 if (data->ttls_version == 0)
1895 eap_ttls_v0_derive_key(sm, data);
1897 if (*out_data == NULL || wpabuf_len(*out_data) == 0) {
1898 if (eap_ttls_decrypt(sm, data, ret, identifier,
1900 wpa_printf(MSG_WARNING, "EAP-TTLS: "
1901 "failed to process early "
1902 "start for Phase 2");
1912 * Application data included in the handshake message.
1914 wpabuf_free(data->pending_phase2_req);
1915 data->pending_phase2_req = *out_data;
1917 wpabuf_set(&msg, in_data, in_len);
1918 res = eap_ttls_decrypt(sm, data, ret, identifier, &msg,
1926 static void eap_ttls_check_auth_status(struct eap_sm *sm,
1927 struct eap_ttls_data *data,
1928 struct eap_method_ret *ret)
1930 if (data->ttls_version == 0 && ret->methodState == METHOD_DONE) {
1931 ret->allowNotifications = FALSE;
1932 if (ret->decision == DECISION_UNCOND_SUCC ||
1933 ret->decision == DECISION_COND_SUCC) {
1934 wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1935 "completed successfully");
1936 data->phase2_success = 1;
1938 if (!data->ready_for_tnc && !data->tnc_started) {
1940 * TNC may be required as the next
1941 * authentication method within the tunnel.
1943 ret->methodState = METHOD_MAY_CONT;
1944 data->ready_for_tnc = 1;
1946 #endif /* EAP_TNC */
1948 } else if (data->ttls_version == 0 &&
1949 ret->methodState == METHOD_MAY_CONT &&
1950 (ret->decision == DECISION_UNCOND_SUCC ||
1951 ret->decision == DECISION_COND_SUCC)) {
1952 wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1953 "completed successfully (MAY_CONT)");
1954 data->phase2_success = 1;
1959 static struct wpabuf * eap_ttls_process(struct eap_sm *sm, void *priv,
1960 struct eap_method_ret *ret,
1961 const struct wpabuf *reqData)
1966 struct wpabuf *resp;
1968 struct eap_ttls_data *data = priv;
1970 pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_TTLS, ret,
1971 reqData, &left, &flags);
1974 id = eap_get_id(reqData);
1976 if (flags & EAP_TLS_FLAGS_START) {
1977 if (eap_ttls_process_start(sm, data, flags, ret) < 0)
1980 /* RFC 5281, Ch. 9.2:
1981 * "This packet MAY contain additional information in the form
1982 * of AVPs, which may provide useful hints to the client"
1983 * For now, ignore any potential extra data.
1986 } else if (!data->ssl_initialized) {
1987 wpa_printf(MSG_DEBUG, "EAP-TTLS: First message did not "
1988 "include Start flag");
1989 ret->methodState = METHOD_DONE;
1990 ret->decision = DECISION_FAIL;
1991 ret->allowNotifications = FALSE;
1996 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1999 wpabuf_set(&msg, pos, left);
2000 res = eap_ttls_decrypt(sm, data, ret, id, &msg, &resp);
2002 res = eap_ttls_process_handshake(sm, data, ret, id,
2006 eap_ttls_check_auth_status(sm, data, ret);
2008 /* FIX: what about res == -1? Could just move all error processing into
2009 * the other functions and get rid of this res==1 case here. */
2012 return eap_peer_tls_build_ack(id, EAP_TYPE_TTLS,
2013 data->ttls_version);
2019 static Boolean eap_ttls_has_reauth_data(struct eap_sm *sm, void *priv)
2021 struct eap_ttls_data *data = priv;
2022 return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
2023 data->phase2_success;
2027 static void eap_ttls_deinit_for_reauth(struct eap_sm *sm, void *priv)
2029 struct eap_ttls_data *data = priv;
2030 wpabuf_free(data->pending_phase2_req);
2031 data->pending_phase2_req = NULL;
2033 data->ready_for_tnc = 0;
2034 data->tnc_started = 0;
2035 #endif /* EAP_TNC */
2039 static void * eap_ttls_init_for_reauth(struct eap_sm *sm, void *priv)
2041 struct eap_ttls_data *data = priv;
2042 os_free(data->key_data);
2043 data->key_data = NULL;
2044 if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
2048 if (data->phase2_priv && data->phase2_method &&
2049 data->phase2_method->init_for_reauth)
2050 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
2051 data->phase2_start = 0;
2052 data->phase2_success = 0;
2059 static int eap_ttls_get_status(struct eap_sm *sm, void *priv, char *buf,
2060 size_t buflen, int verbose)
2062 struct eap_ttls_data *data = priv;
2065 len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
2066 ret = os_snprintf(buf + len, buflen - len,
2067 "EAP-TTLSv%d Phase2 method=",
2068 data->ttls_version);
2069 if (ret < 0 || (size_t) ret >= buflen - len)
2072 switch (data->phase2_type) {
2073 case EAP_TTLS_PHASE2_EAP:
2074 ret = os_snprintf(buf + len, buflen - len, "EAP-%s\n",
2075 data->phase2_method ?
2076 data->phase2_method->name : "?");
2078 case EAP_TTLS_PHASE2_MSCHAPV2:
2079 ret = os_snprintf(buf + len, buflen - len, "MSCHAPV2\n");
2081 case EAP_TTLS_PHASE2_MSCHAP:
2082 ret = os_snprintf(buf + len, buflen - len, "MSCHAP\n");
2084 case EAP_TTLS_PHASE2_PAP:
2085 ret = os_snprintf(buf + len, buflen - len, "PAP\n");
2087 case EAP_TTLS_PHASE2_CHAP:
2088 ret = os_snprintf(buf + len, buflen - len, "CHAP\n");
2094 if (ret < 0 || (size_t) ret >= buflen - len)
2102 static Boolean eap_ttls_isKeyAvailable(struct eap_sm *sm, void *priv)
2104 struct eap_ttls_data *data = priv;
2105 return data->key_data != NULL && data->phase2_success;
2109 static u8 * eap_ttls_getKey(struct eap_sm *sm, void *priv, size_t *len)
2111 struct eap_ttls_data *data = priv;
2114 if (data->key_data == NULL || !data->phase2_success)
2117 key = os_malloc(EAP_TLS_KEY_LEN);
2121 *len = EAP_TLS_KEY_LEN;
2122 os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
2128 int eap_peer_ttls_register(void)
2130 struct eap_method *eap;
2133 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
2134 EAP_VENDOR_IETF, EAP_TYPE_TTLS, "TTLS");
2138 eap->init = eap_ttls_init;
2139 eap->deinit = eap_ttls_deinit;
2140 eap->process = eap_ttls_process;
2141 eap->isKeyAvailable = eap_ttls_isKeyAvailable;
2142 eap->getKey = eap_ttls_getKey;
2143 eap->get_status = eap_ttls_get_status;
2144 eap->has_reauth_data = eap_ttls_has_reauth_data;
2145 eap->deinit_for_reauth = eap_ttls_deinit_for_reauth;
2146 eap->init_for_reauth = eap_ttls_init_for_reauth;
2148 ret = eap_peer_method_register(eap);
2150 eap_peer_method_free(eap);