2 * EAP peer method: EAP-TTLS (RFC 5281)
3 * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
18 #include "eap_peer/eap_i.h"
19 #include "eap_peer/eap_tls_common.h"
20 #include "eap_peer/eap_config.h"
23 #include "eap_common/chap.h"
26 #include "eap_common/eap_ttls.h"
29 /* Maximum supported TTLS version
31 * 1 = draft-funk-eap-ttls-v1-00.txt
33 #ifndef EAP_TTLS_VERSION
34 #define EAP_TTLS_VERSION 0 /* TTLSv1 implementation is not yet complete */
35 #endif /* EAP_TTLS_VERSION */
38 #define MSCHAPV2_KEY_LEN 16
39 #define MSCHAPV2_NT_RESPONSE_LEN 24
42 static void eap_ttls_deinit(struct eap_sm *sm, void *priv);
45 struct eap_ttls_data {
46 struct eap_ssl_data ssl;
49 int ttls_version, force_ttls_version;
51 const struct eap_method *phase2_method;
58 EAP_TTLS_PHASE2_MSCHAPV2,
59 EAP_TTLS_PHASE2_MSCHAP,
63 struct eap_method_type phase2_eap_type;
64 struct eap_method_type *phase2_eap_types;
65 size_t num_phase2_eap_types;
67 u8 auth_response[MSCHAPV2_AUTH_RESPONSE_LEN];
68 int auth_response_valid;
69 u8 master_key[MSCHAPV2_MASTER_KEY_LEN]; /* MSCHAPv2 master key */
71 int resuming; /* starting a resumed session */
72 int reauth; /* reauthentication */
75 struct wpabuf *pending_phase2_req;
84 static void * eap_ttls_init(struct eap_sm *sm)
86 struct eap_ttls_data *data;
87 struct eap_peer_config *config = eap_get_config(sm);
90 data = os_zalloc(sizeof(*data));
93 data->ttls_version = EAP_TTLS_VERSION;
94 data->force_ttls_version = -1;
96 data->phase2_type = EAP_TTLS_PHASE2_EAP;
98 #if EAP_TTLS_VERSION > 0
99 if (config && config->phase1) {
100 const char *pos = os_strstr(config->phase1, "ttlsver=");
102 data->force_ttls_version = atoi(pos + 8);
103 data->ttls_version = data->force_ttls_version;
104 wpa_printf(MSG_DEBUG, "EAP-TTLS: Forced TTLS version "
105 "%d", data->force_ttls_version);
108 #endif /* EAP_TTLS_VERSION */
110 if (config && config->phase2) {
111 if (os_strstr(config->phase2, "autheap=")) {
113 data->phase2_type = EAP_TTLS_PHASE2_EAP;
114 } else if (os_strstr(config->phase2, "auth=MSCHAPV2")) {
115 selected = "MSCHAPV2";
116 data->phase2_type = EAP_TTLS_PHASE2_MSCHAPV2;
117 } else if (os_strstr(config->phase2, "auth=MSCHAP")) {
119 data->phase2_type = EAP_TTLS_PHASE2_MSCHAP;
120 } else if (os_strstr(config->phase2, "auth=PAP")) {
122 data->phase2_type = EAP_TTLS_PHASE2_PAP;
123 } else if (os_strstr(config->phase2, "auth=CHAP")) {
125 data->phase2_type = EAP_TTLS_PHASE2_CHAP;
128 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase2 type: %s", selected);
130 if (data->phase2_type == EAP_TTLS_PHASE2_EAP) {
131 if (eap_peer_select_phase2_methods(config, "autheap=",
132 &data->phase2_eap_types,
133 &data->num_phase2_eap_types)
135 eap_ttls_deinit(sm, data);
139 data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
140 data->phase2_eap_type.method = EAP_TYPE_NONE;
143 #if EAP_TTLS_VERSION > 0
144 if (!(tls_capabilities(sm->ssl_ctx) & TLS_CAPABILITY_IA) &&
145 data->ttls_version > 0) {
146 if (data->force_ttls_version > 0) {
147 wpa_printf(MSG_INFO, "EAP-TTLS: Forced TTLSv%d and "
148 "TLS library does not support TLS/IA.",
149 data->force_ttls_version);
150 eap_ttls_deinit(sm, data);
153 data->ttls_version = 0;
155 #endif /* EAP_TTLS_VERSION */
161 static void eap_ttls_phase2_eap_deinit(struct eap_sm *sm,
162 struct eap_ttls_data *data)
164 if (data->phase2_priv && data->phase2_method) {
165 data->phase2_method->deinit(sm, data->phase2_priv);
166 data->phase2_method = NULL;
167 data->phase2_priv = NULL;
172 static void eap_ttls_deinit(struct eap_sm *sm, void *priv)
174 struct eap_ttls_data *data = priv;
177 eap_ttls_phase2_eap_deinit(sm, data);
178 os_free(data->phase2_eap_types);
179 if (data->ssl_initialized)
180 eap_peer_tls_ssl_deinit(sm, &data->ssl);
181 os_free(data->key_data);
182 wpabuf_free(data->pending_phase2_req);
187 static u8 * eap_ttls_avp_hdr(u8 *avphdr, u32 avp_code, u32 vendor_id,
188 int mandatory, size_t len)
190 struct ttls_avp_vendor *avp;
194 avp = (struct ttls_avp_vendor *) avphdr;
195 flags = mandatory ? AVP_FLAGS_MANDATORY : 0;
197 flags |= AVP_FLAGS_VENDOR;
198 hdrlen = sizeof(*avp);
199 avp->vendor_id = host_to_be32(vendor_id);
201 hdrlen = sizeof(struct ttls_avp);
204 avp->avp_code = host_to_be32(avp_code);
205 avp->avp_length = host_to_be32((flags << 24) | (hdrlen + len));
207 return avphdr + hdrlen;
211 static u8 * eap_ttls_avp_add(u8 *start, u8 *avphdr, u32 avp_code,
212 u32 vendor_id, int mandatory,
213 const u8 *data, size_t len)
216 pos = eap_ttls_avp_hdr(avphdr, avp_code, vendor_id, mandatory, len);
217 os_memcpy(pos, data, len);
224 static int eap_ttls_avp_encapsulate(struct wpabuf **resp, u32 avp_code,
230 msg = wpabuf_alloc(sizeof(struct ttls_avp) + wpabuf_len(*resp) + 4);
237 avp = wpabuf_mhead(msg);
238 pos = eap_ttls_avp_hdr(avp, avp_code, 0, mandatory, wpabuf_len(*resp));
239 os_memcpy(pos, wpabuf_head(*resp), wpabuf_len(*resp));
240 pos += wpabuf_len(*resp);
243 wpabuf_put(msg, pos - avp);
249 #if EAP_TTLS_VERSION > 0
250 static int eap_ttls_ia_permute_inner_secret(struct eap_sm *sm,
251 struct eap_ttls_data *data,
252 const u8 *key, size_t key_len)
259 buf_len = 2 + key_len;
260 buf = os_malloc(buf_len);
263 WPA_PUT_BE16(buf, key_len);
264 os_memcpy(buf + 2, key, key_len);
270 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Session keys for TLS/IA inner "
271 "secret permutation", buf, buf_len);
272 ret = tls_connection_ia_permute_inner_secret(sm->ssl_ctx,
279 #endif /* EAP_TTLS_VERSION */
282 static int eap_ttls_v0_derive_key(struct eap_sm *sm,
283 struct eap_ttls_data *data)
285 os_free(data->key_data);
286 data->key_data = eap_peer_tls_derive_key(sm, &data->ssl,
287 "ttls keying material",
289 if (!data->key_data) {
290 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to derive key");
294 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
295 data->key_data, EAP_TLS_KEY_LEN);
301 #if EAP_TTLS_VERSION > 0
302 static int eap_ttls_v1_derive_key(struct eap_sm *sm,
303 struct eap_ttls_data *data)
305 struct tls_keys keys;
308 os_free(data->key_data);
309 data->key_data = NULL;
311 os_memset(&keys, 0, sizeof(keys));
312 if (tls_connection_get_keys(sm->ssl_ctx, data->ssl.conn, &keys) ||
313 keys.client_random == NULL || keys.server_random == NULL ||
314 keys.inner_secret == NULL) {
315 wpa_printf(MSG_INFO, "EAP-TTLS: Could not get inner secret, "
316 "client random, or server random to derive keying "
321 rnd = os_malloc(keys.client_random_len + keys.server_random_len);
322 data->key_data = os_malloc(EAP_TLS_KEY_LEN);
323 if (rnd == NULL || data->key_data == NULL) {
324 wpa_printf(MSG_INFO, "EAP-TTLS: No memory for key derivation");
326 os_free(data->key_data);
327 data->key_data = NULL;
330 os_memcpy(rnd, keys.client_random, keys.client_random_len);
331 os_memcpy(rnd + keys.client_random_len, keys.server_random,
332 keys.server_random_len);
334 if (tls_prf(keys.inner_secret, keys.inner_secret_len,
335 "ttls v1 keying material", rnd, keys.client_random_len +
336 keys.server_random_len, data->key_data, EAP_TLS_KEY_LEN)) {
337 wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to derive key");
339 os_free(data->key_data);
340 data->key_data = NULL;
344 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: client/server random",
345 rnd, keys.client_random_len + keys.server_random_len);
346 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: TLS/IA inner secret",
347 keys.inner_secret, keys.inner_secret_len);
351 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
352 data->key_data, EAP_TLS_KEY_LEN);
356 #endif /* EAP_TTLS_VERSION */
359 static u8 * eap_ttls_implicit_challenge(struct eap_sm *sm,
360 struct eap_ttls_data *data, size_t len)
362 #if EAP_TTLS_VERSION > 0
363 struct tls_keys keys;
365 #endif /* EAP_TTLS_VERSION */
367 if (data->ttls_version == 0) {
368 return eap_peer_tls_derive_key(sm, &data->ssl,
369 "ttls challenge", len);
372 #if EAP_TTLS_VERSION > 0
374 os_memset(&keys, 0, sizeof(keys));
375 if (tls_connection_get_keys(sm->ssl_ctx, data->ssl.conn, &keys) ||
376 keys.client_random == NULL || keys.server_random == NULL ||
377 keys.inner_secret == NULL) {
378 wpa_printf(MSG_INFO, "EAP-TTLS: Could not get inner secret, "
379 "client random, or server random to derive "
380 "implicit challenge");
384 rnd = os_malloc(keys.client_random_len + keys.server_random_len);
385 challenge = os_malloc(len);
386 if (rnd == NULL || challenge == NULL) {
387 wpa_printf(MSG_INFO, "EAP-TTLS: No memory for implicit "
388 "challenge derivation");
393 os_memcpy(rnd, keys.server_random, keys.server_random_len);
394 os_memcpy(rnd + keys.server_random_len, keys.client_random,
395 keys.client_random_len);
397 if (tls_prf(keys.inner_secret, keys.inner_secret_len,
398 "inner application challenge", rnd,
399 keys.client_random_len + keys.server_random_len,
401 wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to derive implicit "
410 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived implicit challenge",
415 #else /* EAP_TTLS_VERSION */
419 #endif /* EAP_TTLS_VERSION */
423 static void eap_ttlsv1_phase2_eap_finish(struct eap_sm *sm,
424 struct eap_ttls_data *data,
425 struct eap_method_ret *ret)
427 #if EAP_TTLS_VERSION > 0
428 if (data->ttls_version > 0) {
429 const struct eap_method *m = data->phase2_method;
430 void *priv = data->phase2_priv;
432 /* TTLSv1 requires TLS/IA FinalPhaseFinished */
433 if (ret->decision == DECISION_UNCOND_SUCC)
434 ret->decision = DECISION_COND_SUCC;
435 ret->methodState = METHOD_CONT;
437 if (ret->decision == DECISION_COND_SUCC &&
438 m->isKeyAvailable && m->getKey &&
439 m->isKeyAvailable(sm, priv)) {
442 key = m->getKey(sm, priv, &key_len);
444 eap_ttls_ia_permute_inner_secret(
445 sm, data, key, key_len);
450 #endif /* EAP_TTLS_VERSION */
454 static void eap_ttls_phase2_select_eap_method(struct eap_ttls_data *data,
458 for (i = 0; i < data->num_phase2_eap_types; i++) {
459 if (data->phase2_eap_types[i].vendor != EAP_VENDOR_IETF ||
460 data->phase2_eap_types[i].method != method)
463 data->phase2_eap_type.vendor =
464 data->phase2_eap_types[i].vendor;
465 data->phase2_eap_type.method =
466 data->phase2_eap_types[i].method;
467 wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
468 "Phase 2 EAP vendor %d method %d",
469 data->phase2_eap_type.vendor,
470 data->phase2_eap_type.method);
476 static int eap_ttls_phase2_eap_process(struct eap_sm *sm,
477 struct eap_ttls_data *data,
478 struct eap_method_ret *ret,
479 struct eap_hdr *hdr, size_t len,
480 struct wpabuf **resp)
483 struct eap_method_ret iret;
485 os_memset(&iret, 0, sizeof(iret));
486 wpabuf_set(&msg, hdr, len);
487 *resp = data->phase2_method->process(sm, data->phase2_priv, &iret,
489 if ((iret.methodState == METHOD_DONE ||
490 iret.methodState == METHOD_MAY_CONT) &&
491 (iret.decision == DECISION_UNCOND_SUCC ||
492 iret.decision == DECISION_COND_SUCC ||
493 iret.decision == DECISION_FAIL)) {
494 ret->methodState = iret.methodState;
495 ret->decision = iret.decision;
497 eap_ttlsv1_phase2_eap_finish(sm, data, ret);
503 static int eap_ttls_phase2_request_eap_method(struct eap_sm *sm,
504 struct eap_ttls_data *data,
505 struct eap_method_ret *ret,
506 struct eap_hdr *hdr, size_t len,
507 u8 method, struct wpabuf **resp)
510 if (data->tnc_started && data->phase2_method &&
511 data->phase2_priv && method == EAP_TYPE_TNC &&
512 data->phase2_eap_type.method == EAP_TYPE_TNC)
513 return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len,
516 if (data->ready_for_tnc && !data->tnc_started &&
517 method == EAP_TYPE_TNC) {
518 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
520 data->tnc_started = 1;
523 if (data->tnc_started) {
524 if (data->phase2_eap_type.vendor != EAP_VENDOR_IETF ||
525 data->phase2_eap_type.method == EAP_TYPE_TNC) {
526 wpa_printf(MSG_DEBUG, "EAP-TTLS: Unexpected EAP "
527 "type %d for TNC", method);
531 data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
532 data->phase2_eap_type.method = method;
533 wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
534 "Phase 2 EAP vendor %d method %d (TNC)",
535 data->phase2_eap_type.vendor,
536 data->phase2_eap_type.method);
538 if (data->phase2_type == EAP_TTLS_PHASE2_EAP)
539 eap_ttls_phase2_eap_deinit(sm, data);
543 if (data->phase2_eap_type.vendor == EAP_VENDOR_IETF &&
544 data->phase2_eap_type.method == EAP_TYPE_NONE)
545 eap_ttls_phase2_select_eap_method(data, method);
547 if (method != data->phase2_eap_type.method || method == EAP_TYPE_NONE)
549 if (eap_peer_tls_phase2_nak(data->phase2_eap_types,
550 data->num_phase2_eap_types,
556 if (data->phase2_priv == NULL) {
557 data->phase2_method = eap_peer_get_eap_method(
558 EAP_VENDOR_IETF, method);
559 if (data->phase2_method) {
561 data->phase2_priv = data->phase2_method->init(sm);
565 if (data->phase2_priv == NULL || data->phase2_method == NULL) {
566 wpa_printf(MSG_INFO, "EAP-TTLS: failed to initialize "
567 "Phase 2 EAP method %d", method);
571 return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len, resp);
575 static int eap_ttls_phase2_request_eap(struct eap_sm *sm,
576 struct eap_ttls_data *data,
577 struct eap_method_ret *ret,
579 struct wpabuf **resp)
581 size_t len = be_to_host16(hdr->length);
583 struct eap_peer_config *config = eap_get_config(sm);
585 if (len <= sizeof(struct eap_hdr)) {
586 wpa_printf(MSG_INFO, "EAP-TTLS: too short "
587 "Phase 2 request (len=%lu)", (unsigned long) len);
590 pos = (u8 *) (hdr + 1);
591 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP Request: type=%d", *pos);
593 case EAP_TYPE_IDENTITY:
594 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
597 if (eap_ttls_phase2_request_eap_method(sm, data, ret, hdr, len,
604 (config->pending_req_identity || config->pending_req_password ||
605 config->pending_req_otp)) {
612 wpa_hexdump_buf(MSG_DEBUG, "EAP-TTLS: AVP encapsulate EAP Response",
614 return eap_ttls_avp_encapsulate(resp, RADIUS_ATTR_EAP_MESSAGE, 1);
618 static void eap_ttlsv1_permute_inner(struct eap_sm *sm,
619 struct eap_ttls_data *data)
621 #if EAP_TTLS_VERSION > 0
622 u8 session_key[2 * MSCHAPV2_KEY_LEN];
624 if (data->ttls_version == 0)
627 get_asymetric_start_key(data->master_key, session_key,
628 MSCHAPV2_KEY_LEN, 0, 0);
629 get_asymetric_start_key(data->master_key,
630 session_key + MSCHAPV2_KEY_LEN,
631 MSCHAPV2_KEY_LEN, 1, 0);
632 eap_ttls_ia_permute_inner_secret(sm, data, session_key,
633 sizeof(session_key));
634 #endif /* EAP_TTLS_VERSION */
638 static int eap_ttls_phase2_request_mschapv2(struct eap_sm *sm,
639 struct eap_ttls_data *data,
640 struct eap_method_ret *ret,
641 struct wpabuf **resp)
644 u8 *buf, *pos, *challenge, *peer_challenge;
645 const u8 *identity, *password;
646 size_t identity_len, password_len;
649 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAPV2 Request");
651 identity = eap_get_config_identity(sm, &identity_len);
652 password = eap_get_config_password2(sm, &password_len, &pwhash);
653 if (identity == NULL || password == NULL)
656 msg = wpabuf_alloc(identity_len + 1000);
658 wpa_printf(MSG_ERROR,
659 "EAP-TTLS/MSCHAPV2: Failed to allocate memory");
662 pos = buf = wpabuf_mhead(msg);
665 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
666 identity, identity_len);
668 /* MS-CHAP-Challenge */
669 challenge = eap_ttls_implicit_challenge(
670 sm, data, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN + 1);
671 if (challenge == NULL) {
673 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
674 "implicit challenge");
677 peer_challenge = challenge + 1 + EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
679 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
680 RADIUS_VENDOR_ID_MICROSOFT, 1,
681 challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
683 /* MS-CHAP2-Response */
684 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP2_RESPONSE,
685 RADIUS_VENDOR_ID_MICROSOFT, 1,
686 EAP_TTLS_MSCHAPV2_RESPONSE_LEN);
687 data->ident = challenge[EAP_TTLS_MSCHAPV2_CHALLENGE_LEN];
688 *pos++ = data->ident;
689 *pos++ = 0; /* Flags */
690 os_memcpy(pos, peer_challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
691 pos += EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
692 os_memset(pos, 0, 8); /* Reserved, must be zero */
694 mschapv2_derive_response(identity, identity_len, password,
695 password_len, pwhash, challenge,
696 peer_challenge, pos, data->auth_response,
698 data->auth_response_valid = 1;
700 eap_ttlsv1_permute_inner(sm, data);
706 wpabuf_put(msg, pos - buf);
709 if (sm->workaround && data->ttls_version == 0) {
710 /* At least FreeRADIUS seems to be terminating
711 * EAP-TTLS/MSHCAPV2 without the expected MS-CHAP-v2 Success
713 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: EAP workaround - "
714 "allow success without tunneled response");
715 ret->methodState = METHOD_MAY_CONT;
716 ret->decision = DECISION_COND_SUCC;
723 static int eap_ttls_phase2_request_mschap(struct eap_sm *sm,
724 struct eap_ttls_data *data,
725 struct eap_method_ret *ret,
726 struct wpabuf **resp)
729 u8 *buf, *pos, *challenge;
730 const u8 *identity, *password;
731 size_t identity_len, password_len;
734 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAP Request");
736 identity = eap_get_config_identity(sm, &identity_len);
737 password = eap_get_config_password2(sm, &password_len, &pwhash);
738 if (identity == NULL || password == NULL)
741 msg = wpabuf_alloc(identity_len + 1000);
743 wpa_printf(MSG_ERROR,
744 "EAP-TTLS/MSCHAP: Failed to allocate memory");
747 pos = buf = wpabuf_mhead(msg);
750 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
751 identity, identity_len);
753 /* MS-CHAP-Challenge */
754 challenge = eap_ttls_implicit_challenge(
755 sm, data, EAP_TTLS_MSCHAP_CHALLENGE_LEN + 1);
756 if (challenge == NULL) {
758 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAP: Failed to derive "
759 "implicit challenge");
763 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
764 RADIUS_VENDOR_ID_MICROSOFT, 1,
765 challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
767 /* MS-CHAP-Response */
768 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP_RESPONSE,
769 RADIUS_VENDOR_ID_MICROSOFT, 1,
770 EAP_TTLS_MSCHAP_RESPONSE_LEN);
771 data->ident = challenge[EAP_TTLS_MSCHAP_CHALLENGE_LEN];
772 *pos++ = data->ident;
773 *pos++ = 1; /* Flags: Use NT style passwords */
774 os_memset(pos, 0, 24); /* LM-Response */
777 challenge_response(challenge, password, pos); /* NT-Response */
778 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password hash",
781 nt_challenge_response(challenge, password, password_len,
782 pos); /* NT-Response */
783 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password",
784 password, password_len);
786 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP implicit challenge",
787 challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
788 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP response", pos, 24);
793 wpabuf_put(msg, pos - buf);
796 if (data->ttls_version > 0) {
797 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
798 * so do not allow connection to be terminated yet. */
799 ret->methodState = METHOD_CONT;
800 ret->decision = DECISION_COND_SUCC;
802 /* EAP-TTLS/MSCHAP does not provide tunneled success
803 * notification, so assume that Phase2 succeeds. */
804 ret->methodState = METHOD_DONE;
805 ret->decision = DECISION_COND_SUCC;
812 static int eap_ttls_phase2_request_pap(struct eap_sm *sm,
813 struct eap_ttls_data *data,
814 struct eap_method_ret *ret,
815 struct wpabuf **resp)
820 const u8 *identity, *password;
821 size_t identity_len, password_len;
823 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 PAP Request");
825 identity = eap_get_config_identity(sm, &identity_len);
826 password = eap_get_config_password(sm, &password_len);
827 if (identity == NULL || password == NULL)
830 msg = wpabuf_alloc(identity_len + password_len + 100);
832 wpa_printf(MSG_ERROR,
833 "EAP-TTLS/PAP: Failed to allocate memory");
836 pos = buf = wpabuf_mhead(msg);
839 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
840 identity, identity_len);
842 /* User-Password; in RADIUS, this is encrypted, but EAP-TTLS encrypts
843 * the data, so no separate encryption is used in the AVP itself.
844 * However, the password is padded to obfuscate its length. */
845 pad = (16 - (password_len & 15)) & 15;
846 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_USER_PASSWORD, 0, 1,
848 os_memcpy(pos, password, password_len);
850 os_memset(pos, 0, pad);
854 wpabuf_put(msg, pos - buf);
857 if (data->ttls_version > 0) {
858 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
859 * so do not allow connection to be terminated yet. */
860 ret->methodState = METHOD_CONT;
861 ret->decision = DECISION_COND_SUCC;
863 /* EAP-TTLS/PAP does not provide tunneled success notification,
864 * so assume that Phase2 succeeds. */
865 ret->methodState = METHOD_DONE;
866 ret->decision = DECISION_COND_SUCC;
873 static int eap_ttls_phase2_request_chap(struct eap_sm *sm,
874 struct eap_ttls_data *data,
875 struct eap_method_ret *ret,
876 struct wpabuf **resp)
879 u8 *buf, *pos, *challenge;
880 const u8 *identity, *password;
881 size_t identity_len, password_len;
883 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 CHAP Request");
885 identity = eap_get_config_identity(sm, &identity_len);
886 password = eap_get_config_password(sm, &password_len);
887 if (identity == NULL || password == NULL)
890 msg = wpabuf_alloc(identity_len + 1000);
892 wpa_printf(MSG_ERROR,
893 "EAP-TTLS/CHAP: Failed to allocate memory");
896 pos = buf = wpabuf_mhead(msg);
899 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
900 identity, identity_len);
903 challenge = eap_ttls_implicit_challenge(
904 sm, data, EAP_TTLS_CHAP_CHALLENGE_LEN + 1);
905 if (challenge == NULL) {
907 wpa_printf(MSG_ERROR, "EAP-TTLS/CHAP: Failed to derive "
908 "implicit challenge");
912 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_CHAP_CHALLENGE, 0, 1,
913 challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
916 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_CHAP_PASSWORD, 0, 1,
917 1 + EAP_TTLS_CHAP_PASSWORD_LEN);
918 data->ident = challenge[EAP_TTLS_CHAP_CHALLENGE_LEN];
919 *pos++ = data->ident;
921 /* MD5(Ident + Password + Challenge) */
922 chap_md5(data->ident, password, password_len, challenge,
923 EAP_TTLS_CHAP_CHALLENGE_LEN, pos);
925 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: CHAP username",
926 identity, identity_len);
927 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: CHAP password",
928 password, password_len);
929 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP implicit challenge",
930 challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
931 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP password",
932 pos, EAP_TTLS_CHAP_PASSWORD_LEN);
933 pos += EAP_TTLS_CHAP_PASSWORD_LEN;
937 wpabuf_put(msg, pos - buf);
940 if (data->ttls_version > 0) {
941 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
942 * so do not allow connection to be terminated yet. */
943 ret->methodState = METHOD_CONT;
944 ret->decision = DECISION_COND_SUCC;
946 /* EAP-TTLS/CHAP does not provide tunneled success
947 * notification, so assume that Phase2 succeeds. */
948 ret->methodState = METHOD_DONE;
949 ret->decision = DECISION_COND_SUCC;
956 static int eap_ttls_phase2_request(struct eap_sm *sm,
957 struct eap_ttls_data *data,
958 struct eap_method_ret *ret,
960 struct wpabuf **resp)
964 enum phase2_types phase2_type = data->phase2_type;
967 if (data->tnc_started) {
968 wpa_printf(MSG_DEBUG, "EAP-TTLS: Processing TNC");
969 phase2_type = EAP_TTLS_PHASE2_EAP;
973 if (phase2_type == EAP_TTLS_PHASE2_MSCHAPV2 ||
974 phase2_type == EAP_TTLS_PHASE2_MSCHAP ||
975 phase2_type == EAP_TTLS_PHASE2_PAP ||
976 phase2_type == EAP_TTLS_PHASE2_CHAP) {
977 if (eap_get_config_identity(sm, &len) == NULL) {
979 "EAP-TTLS: Identity not configured");
980 eap_sm_request_identity(sm);
981 if (eap_get_config_password(sm, &len) == NULL)
982 eap_sm_request_password(sm);
986 if (eap_get_config_password(sm, &len) == NULL) {
988 "EAP-TTLS: Password not configured");
989 eap_sm_request_password(sm);
994 switch (phase2_type) {
995 case EAP_TTLS_PHASE2_EAP:
996 res = eap_ttls_phase2_request_eap(sm, data, ret, hdr, resp);
998 case EAP_TTLS_PHASE2_MSCHAPV2:
999 res = eap_ttls_phase2_request_mschapv2(sm, data, ret, resp);
1001 case EAP_TTLS_PHASE2_MSCHAP:
1002 res = eap_ttls_phase2_request_mschap(sm, data, ret, resp);
1004 case EAP_TTLS_PHASE2_PAP:
1005 res = eap_ttls_phase2_request_pap(sm, data, ret, resp);
1007 case EAP_TTLS_PHASE2_CHAP:
1008 res = eap_ttls_phase2_request_chap(sm, data, ret, resp);
1011 wpa_printf(MSG_ERROR, "EAP-TTLS: Phase 2 - Unknown");
1017 ret->methodState = METHOD_DONE;
1018 ret->decision = DECISION_FAIL;
1025 #if EAP_TTLS_VERSION > 0
1026 static struct wpabuf * eap_ttls_build_phase_finished(
1027 struct eap_sm *sm, struct eap_ttls_data *data, int id, int final)
1032 const int max_len = 300;
1034 req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TTLS, 1 + max_len,
1035 EAP_CODE_RESPONSE, id);
1039 wpabuf_put_u8(req, data->ttls_version);
1041 pos = wpabuf_put(req, 0);
1042 len = tls_connection_ia_send_phase_finished(sm->ssl_ctx,
1044 final, pos, max_len);
1049 wpabuf_put(req, len);
1050 eap_update_len(req);
1054 #endif /* EAP_TTLS_VERSION */
1057 struct ttls_parse_avp {
1065 static int eap_ttls_parse_attr_eap(const u8 *dpos, size_t dlen,
1066 struct ttls_parse_avp *parse)
1068 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - EAP Message");
1069 if (parse->eapdata == NULL) {
1070 parse->eapdata = os_malloc(dlen);
1071 if (parse->eapdata == NULL) {
1072 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
1073 "memory for Phase 2 EAP data");
1076 os_memcpy(parse->eapdata, dpos, dlen);
1077 parse->eap_len = dlen;
1079 u8 *neweap = os_realloc(parse->eapdata, parse->eap_len + dlen);
1080 if (neweap == NULL) {
1081 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
1082 "memory for Phase 2 EAP data");
1085 os_memcpy(neweap + parse->eap_len, dpos, dlen);
1086 parse->eapdata = neweap;
1087 parse->eap_len += dlen;
1094 static int eap_ttls_parse_avp(u8 *pos, size_t left,
1095 struct ttls_parse_avp *parse)
1097 struct ttls_avp *avp;
1098 u32 avp_code, avp_length, vendor_id = 0;
1099 u8 avp_flags, *dpos;
1102 avp = (struct ttls_avp *) pos;
1103 avp_code = be_to_host32(avp->avp_code);
1104 avp_length = be_to_host32(avp->avp_length);
1105 avp_flags = (avp_length >> 24) & 0xff;
1106 avp_length &= 0xffffff;
1107 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP: code=%d flags=0x%02x "
1108 "length=%d", (int) avp_code, avp_flags,
1111 if (avp_length > left) {
1112 wpa_printf(MSG_WARNING, "EAP-TTLS: AVP overflow "
1113 "(len=%d, left=%lu) - dropped",
1114 (int) avp_length, (unsigned long) left);
1118 if (avp_length < sizeof(*avp)) {
1119 wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid AVP length %d",
1124 dpos = (u8 *) (avp + 1);
1125 dlen = avp_length - sizeof(*avp);
1126 if (avp_flags & AVP_FLAGS_VENDOR) {
1128 wpa_printf(MSG_WARNING, "EAP-TTLS: Vendor AVP "
1132 vendor_id = WPA_GET_BE32(dpos);
1133 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP vendor_id %d",
1139 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP data", dpos, dlen);
1141 if (vendor_id == 0 && avp_code == RADIUS_ATTR_EAP_MESSAGE) {
1142 if (eap_ttls_parse_attr_eap(dpos, dlen, parse) < 0)
1144 } else if (vendor_id == 0 && avp_code == RADIUS_ATTR_REPLY_MESSAGE) {
1145 /* This is an optional message that can be displayed to
1147 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: AVP - Reply-Message",
1149 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
1150 avp_code == RADIUS_ATTR_MS_CHAP2_SUCCESS) {
1151 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP2-Success",
1154 wpa_printf(MSG_WARNING, "EAP-TTLS: Unexpected "
1155 "MS-CHAP2-Success length "
1156 "(len=%lu, expected 43)",
1157 (unsigned long) dlen);
1160 parse->mschapv2 = dpos;
1161 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
1162 avp_code == RADIUS_ATTR_MS_CHAP_ERROR) {
1163 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP-Error",
1165 parse->mschapv2_error = 1;
1166 } else if (avp_flags & AVP_FLAGS_MANDATORY) {
1167 wpa_printf(MSG_WARNING, "EAP-TTLS: Unsupported mandatory AVP "
1168 "code %d vendor_id %d - dropped",
1169 (int) avp_code, (int) vendor_id);
1172 wpa_printf(MSG_DEBUG, "EAP-TTLS: Ignoring unsupported AVP "
1173 "code %d vendor_id %d",
1174 (int) avp_code, (int) vendor_id);
1181 static int eap_ttls_parse_avps(struct wpabuf *in_decrypted,
1182 struct ttls_parse_avp *parse)
1188 pos = wpabuf_mhead(in_decrypted);
1189 left = wpabuf_len(in_decrypted);
1190 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Decrypted Phase 2 AVPs", pos, left);
1191 if (left < sizeof(struct ttls_avp)) {
1192 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 AVP frame"
1193 " len=%lu expected %lu or more - dropped",
1194 (unsigned long) left,
1195 (unsigned long) sizeof(struct ttls_avp));
1200 os_memset(parse, 0, sizeof(*parse));
1203 avp_length = eap_ttls_parse_avp(pos, left, parse);
1207 pad = (4 - (avp_length & 3)) & 3;
1208 pos += avp_length + pad;
1209 if (left < avp_length + pad)
1212 left -= avp_length + pad;
1219 static u8 * eap_ttls_fake_identity_request(void)
1221 struct eap_hdr *hdr;
1224 wpa_printf(MSG_DEBUG, "EAP-TTLS: empty data in beginning of "
1225 "Phase 2 - use fake EAP-Request Identity");
1226 buf = os_malloc(sizeof(*hdr) + 1);
1228 wpa_printf(MSG_WARNING, "EAP-TTLS: failed to allocate "
1229 "memory for fake EAP-Identity Request");
1233 hdr = (struct eap_hdr *) buf;
1234 hdr->code = EAP_CODE_REQUEST;
1235 hdr->identifier = 0;
1236 hdr->length = host_to_be16(sizeof(*hdr) + 1);
1237 buf[sizeof(*hdr)] = EAP_TYPE_IDENTITY;
1243 static int eap_ttls_encrypt_response(struct eap_sm *sm,
1244 struct eap_ttls_data *data,
1245 struct wpabuf *resp, u8 identifier,
1246 struct wpabuf **out_data)
1251 wpa_hexdump_buf_key(MSG_DEBUG, "EAP-TTLS: Encrypting Phase 2 data",
1253 if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
1254 data->ttls_version, identifier,
1256 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to encrypt a Phase 2 "
1266 static int eap_ttls_process_phase2_eap(struct eap_sm *sm,
1267 struct eap_ttls_data *data,
1268 struct eap_method_ret *ret,
1269 struct ttls_parse_avp *parse,
1270 struct wpabuf **resp)
1272 struct eap_hdr *hdr;
1275 if (parse->eapdata == NULL) {
1276 wpa_printf(MSG_WARNING, "EAP-TTLS: No EAP Message in the "
1277 "packet - dropped");
1281 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP",
1282 parse->eapdata, parse->eap_len);
1283 hdr = (struct eap_hdr *) parse->eapdata;
1285 if (parse->eap_len < sizeof(*hdr)) {
1286 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 EAP "
1287 "frame (len=%lu, expected %lu or more) - dropped",
1288 (unsigned long) parse->eap_len,
1289 (unsigned long) sizeof(*hdr));
1292 len = be_to_host16(hdr->length);
1293 if (len > parse->eap_len) {
1294 wpa_printf(MSG_INFO, "EAP-TTLS: Length mismatch in Phase 2 "
1295 "EAP frame (EAP hdr len=%lu, EAP data len in "
1297 (unsigned long) len,
1298 (unsigned long) parse->eap_len);
1301 wpa_printf(MSG_DEBUG, "EAP-TTLS: received Phase 2: code=%d "
1302 "identifier=%d length=%lu",
1303 hdr->code, hdr->identifier, (unsigned long) len);
1304 switch (hdr->code) {
1305 case EAP_CODE_REQUEST:
1306 if (eap_ttls_phase2_request(sm, data, ret, hdr, resp)) {
1307 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
1308 "processing failed");
1313 wpa_printf(MSG_INFO, "EAP-TTLS: Unexpected code=%d in "
1314 "Phase 2 EAP header", hdr->code);
1322 static int eap_ttls_process_phase2_mschapv2(struct eap_sm *sm,
1323 struct eap_ttls_data *data,
1324 struct eap_method_ret *ret,
1325 struct ttls_parse_avp *parse)
1327 if (parse->mschapv2_error) {
1328 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Received "
1329 "MS-CHAP-Error - failed");
1330 ret->methodState = METHOD_DONE;
1331 ret->decision = DECISION_FAIL;
1332 /* Reply with empty data to ACK error */
1336 if (parse->mschapv2 == NULL) {
1338 if (data->phase2_success && parse->eapdata) {
1340 * Allow EAP-TNC to be started after successfully
1341 * completed MSCHAPV2.
1345 #endif /* EAP_TNC */
1346 wpa_printf(MSG_WARNING, "EAP-TTLS: no MS-CHAP2-Success AVP "
1347 "received for Phase2 MSCHAPV2");
1350 if (parse->mschapv2[0] != data->ident) {
1351 wpa_printf(MSG_WARNING, "EAP-TTLS: Ident mismatch for Phase 2 "
1352 "MSCHAPV2 (received Ident 0x%02x, expected 0x%02x)",
1353 parse->mschapv2[0], data->ident);
1356 if (!data->auth_response_valid ||
1357 mschapv2_verify_auth_response(data->auth_response,
1358 parse->mschapv2 + 1, 42)) {
1359 wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid authenticator "
1360 "response in Phase 2 MSCHAPV2 success request");
1364 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 MSCHAPV2 "
1365 "authentication succeeded");
1366 if (data->ttls_version > 0) {
1368 * EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report
1369 * success, so do not allow connection to be terminated
1372 ret->methodState = METHOD_CONT;
1373 ret->decision = DECISION_COND_SUCC;
1375 ret->methodState = METHOD_DONE;
1376 ret->decision = DECISION_UNCOND_SUCC;
1377 data->phase2_success = 1;
1381 * Reply with empty data; authentication server will reply
1382 * with EAP-Success after this.
1389 static int eap_ttls_process_tnc_start(struct eap_sm *sm,
1390 struct eap_ttls_data *data,
1391 struct eap_method_ret *ret,
1392 struct ttls_parse_avp *parse,
1393 struct wpabuf **resp)
1395 /* TNC uses inner EAP method after non-EAP TTLS phase 2. */
1396 if (parse->eapdata == NULL) {
1397 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
1398 "unexpected tunneled data (no EAP)");
1402 if (!data->ready_for_tnc) {
1403 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
1404 "EAP after non-EAP, but not ready for TNC");
1408 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
1410 data->tnc_started = 1;
1412 if (eap_ttls_process_phase2_eap(sm, data, ret, parse, resp) < 0)
1417 #endif /* EAP_TNC */
1420 static int eap_ttls_process_decrypted(struct eap_sm *sm,
1421 struct eap_ttls_data *data,
1422 struct eap_method_ret *ret,
1424 struct ttls_parse_avp *parse,
1425 struct wpabuf *in_decrypted,
1426 struct wpabuf **out_data)
1428 struct wpabuf *resp = NULL;
1429 struct eap_peer_config *config = eap_get_config(sm);
1431 enum phase2_types phase2_type = data->phase2_type;
1434 if (data->tnc_started)
1435 phase2_type = EAP_TTLS_PHASE2_EAP;
1436 #endif /* EAP_TNC */
1438 switch (phase2_type) {
1439 case EAP_TTLS_PHASE2_EAP:
1440 if (eap_ttls_process_phase2_eap(sm, data, ret, parse, &resp) <
1444 case EAP_TTLS_PHASE2_MSCHAPV2:
1445 res = eap_ttls_process_phase2_mschapv2(sm, data, ret, parse);
1447 if (res == 1 && parse->eapdata && data->phase2_success) {
1449 * TNC may be required as the next
1450 * authentication method within the tunnel.
1452 ret->methodState = METHOD_MAY_CONT;
1453 data->ready_for_tnc = 1;
1454 if (eap_ttls_process_tnc_start(sm, data, ret, parse,
1458 #endif /* EAP_TNC */
1460 case EAP_TTLS_PHASE2_MSCHAP:
1461 case EAP_TTLS_PHASE2_PAP:
1462 case EAP_TTLS_PHASE2_CHAP:
1464 if (eap_ttls_process_tnc_start(sm, data, ret, parse, &resp) <
1469 /* EAP-TTLS/{MSCHAP,PAP,CHAP} should not send any TLS tunneled
1470 * requests to the supplicant */
1471 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received unexpected "
1474 #endif /* EAP_TNC */
1478 if (eap_ttls_encrypt_response(sm, data, resp, identifier,
1481 } else if (config->pending_req_identity ||
1482 config->pending_req_password ||
1483 config->pending_req_otp ||
1484 config->pending_req_new_password) {
1485 wpabuf_free(data->pending_phase2_req);
1486 data->pending_phase2_req = wpabuf_dup(in_decrypted);
1493 #if EAP_TTLS_VERSION > 0
1494 static void eap_ttls_final_phase_finished(struct eap_sm *sm,
1495 struct eap_ttls_data *data,
1496 struct eap_method_ret *ret,
1498 struct wpabuf **out_data)
1500 wpa_printf(MSG_DEBUG, "EAP-TTLS: FinalPhaseFinished received");
1501 wpa_printf(MSG_INFO, "EAP-TTLS: TLS/IA authentication succeeded");
1502 ret->methodState = METHOD_DONE;
1503 ret->decision = DECISION_UNCOND_SUCC;
1504 data->phase2_success = 1;
1505 *out_data = eap_ttls_build_phase_finished(sm, data, identifier, 1);
1506 eap_ttls_v1_derive_key(sm, data);
1508 #endif /* EAP_TTLS_VERSION */
1511 static int eap_ttls_implicit_identity_request(struct eap_sm *sm,
1512 struct eap_ttls_data *data,
1513 struct eap_method_ret *ret,
1515 struct wpabuf **out_data)
1518 struct eap_hdr *hdr;
1519 struct wpabuf *resp;
1521 hdr = (struct eap_hdr *) eap_ttls_fake_identity_request();
1523 ret->methodState = METHOD_DONE;
1524 ret->decision = DECISION_FAIL;
1529 if (eap_ttls_phase2_request(sm, data, ret, hdr, &resp)) {
1530 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
1531 "processing failed");
1534 retval = eap_ttls_encrypt_response(sm, data, resp, identifier,
1541 ret->methodState = METHOD_DONE;
1542 ret->decision = DECISION_FAIL;
1549 static int eap_ttls_phase2_start(struct eap_sm *sm, struct eap_ttls_data *data,
1550 struct eap_method_ret *ret, u8 identifier,
1551 struct wpabuf **out_data)
1553 data->phase2_start = 0;
1556 * EAP-TTLS does not use Phase2 on fast re-auth; this must be done only
1557 * if TLS part was indeed resuming a previous session. Most
1558 * Authentication Servers terminate EAP-TTLS before reaching this
1559 * point, but some do not. Make wpa_supplicant stop phase 2 here, if
1563 tls_connection_resumed(sm->ssl_ctx, data->ssl.conn)) {
1564 wpa_printf(MSG_DEBUG, "EAP-TTLS: Session resumption - "
1566 *out_data = eap_peer_tls_build_ack(identifier, EAP_TYPE_TTLS,
1567 data->ttls_version);
1568 ret->methodState = METHOD_DONE;
1569 ret->decision = DECISION_UNCOND_SUCC;
1570 data->phase2_success = 1;
1574 return eap_ttls_implicit_identity_request(sm, data, ret, identifier,
1579 static int eap_ttls_decrypt(struct eap_sm *sm, struct eap_ttls_data *data,
1580 struct eap_method_ret *ret, u8 identifier,
1581 const struct wpabuf *in_data,
1582 struct wpabuf **out_data)
1584 struct wpabuf *in_decrypted = NULL;
1586 struct ttls_parse_avp parse;
1588 os_memset(&parse, 0, sizeof(parse));
1590 wpa_printf(MSG_DEBUG, "EAP-TTLS: received %lu bytes encrypted data for"
1592 in_data ? (unsigned long) wpabuf_len(in_data) : 0);
1594 if (data->pending_phase2_req) {
1595 wpa_printf(MSG_DEBUG, "EAP-TTLS: Pending Phase 2 request - "
1596 "skip decryption and use old data");
1597 /* Clear TLS reassembly state. */
1598 eap_peer_tls_reset_input(&data->ssl);
1600 in_decrypted = data->pending_phase2_req;
1601 data->pending_phase2_req = NULL;
1602 if (wpabuf_len(in_decrypted) == 0) {
1603 wpabuf_free(in_decrypted);
1604 return eap_ttls_implicit_identity_request(
1605 sm, data, ret, identifier, out_data);
1610 if ((in_data == NULL || wpabuf_len(in_data) == 0) &&
1611 data->phase2_start) {
1612 return eap_ttls_phase2_start(sm, data, ret, identifier,
1616 if (in_data == NULL || wpabuf_len(in_data) == 0) {
1617 /* Received TLS ACK - requesting more fragments */
1618 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
1620 identifier, NULL, out_data);
1623 retval = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
1627 #if EAP_TTLS_VERSION > 0
1628 if (data->ttls_version > 0 &&
1629 (in_decrypted == NULL || wpabuf_len(in_decrypted) == 0) &&
1630 tls_connection_ia_final_phase_finished(sm->ssl_ctx,
1632 eap_ttls_final_phase_finished(sm, data, ret, identifier,
1636 #endif /* EAP_TTLS_VERSION */
1639 data->phase2_start = 0;
1641 if (eap_ttls_parse_avps(in_decrypted, &parse) < 0) {
1646 retval = eap_ttls_process_decrypted(sm, data, ret, identifier,
1647 &parse, in_decrypted, out_data);
1650 wpabuf_free(in_decrypted);
1651 os_free(parse.eapdata);
1654 ret->methodState = METHOD_DONE;
1655 ret->decision = DECISION_FAIL;
1662 static int eap_ttls_process_start(struct eap_sm *sm,
1663 struct eap_ttls_data *data, u8 flags,
1664 struct eap_method_ret *ret)
1666 struct eap_peer_config *config = eap_get_config(sm);
1668 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start (server ver=%d, own ver=%d)",
1669 flags & EAP_PEAP_VERSION_MASK, data->ttls_version);
1670 #if EAP_TTLS_VERSION > 0
1671 if ((flags & EAP_PEAP_VERSION_MASK) < data->ttls_version)
1672 data->ttls_version = flags & EAP_PEAP_VERSION_MASK;
1673 if (data->force_ttls_version >= 0 &&
1674 data->force_ttls_version != data->ttls_version) {
1675 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to select "
1676 "forced TTLS version %d",
1677 data->force_ttls_version);
1678 ret->methodState = METHOD_DONE;
1679 ret->decision = DECISION_FAIL;
1680 ret->allowNotifications = FALSE;
1683 wpa_printf(MSG_DEBUG, "EAP-TTLS: Using TTLS version %d",
1684 data->ttls_version);
1686 if (data->ttls_version > 0)
1687 data->ssl.tls_ia = 1;
1688 #endif /* EAP_TTLS_VERSION */
1689 if (!data->ssl_initialized &&
1690 eap_peer_tls_ssl_init(sm, &data->ssl, config)) {
1691 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to initialize SSL.");
1694 data->ssl_initialized = 1;
1696 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start");
1702 static int eap_ttls_process_handshake(struct eap_sm *sm,
1703 struct eap_ttls_data *data,
1704 struct eap_method_ret *ret,
1706 const u8 *in_data, size_t in_len,
1707 struct wpabuf **out_data)
1711 res = eap_peer_tls_process_helper(sm, &data->ssl, EAP_TYPE_TTLS,
1712 data->ttls_version, identifier,
1713 in_data, in_len, out_data);
1715 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1716 wpa_printf(MSG_DEBUG, "EAP-TTLS: TLS done, proceed to "
1718 if (data->resuming) {
1719 wpa_printf(MSG_DEBUG, "EAP-TTLS: fast reauth - may "
1721 ret->decision = DECISION_COND_SUCC;
1722 ret->methodState = METHOD_MAY_CONT;
1724 data->phase2_start = 1;
1725 if (data->ttls_version == 0)
1726 eap_ttls_v0_derive_key(sm, data);
1728 if (*out_data == NULL || wpabuf_len(*out_data) == 0) {
1729 if (eap_ttls_decrypt(sm, data, ret, identifier,
1731 wpa_printf(MSG_WARNING, "EAP-TTLS: "
1732 "failed to process early "
1733 "start for Phase 2");
1743 * Application data included in the handshake message.
1745 wpabuf_free(data->pending_phase2_req);
1746 data->pending_phase2_req = *out_data;
1748 wpabuf_set(&msg, in_data, in_len);
1749 res = eap_ttls_decrypt(sm, data, ret, identifier, &msg,
1757 static void eap_ttls_check_auth_status(struct eap_sm *sm,
1758 struct eap_ttls_data *data,
1759 struct eap_method_ret *ret)
1761 if (data->ttls_version == 0 && ret->methodState == METHOD_DONE) {
1762 ret->allowNotifications = FALSE;
1763 if (ret->decision == DECISION_UNCOND_SUCC ||
1764 ret->decision == DECISION_COND_SUCC) {
1765 wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1766 "completed successfully");
1767 data->phase2_success = 1;
1769 if (!data->ready_for_tnc && !data->tnc_started) {
1771 * TNC may be required as the next
1772 * authentication method within the tunnel.
1774 ret->methodState = METHOD_MAY_CONT;
1775 data->ready_for_tnc = 1;
1777 #endif /* EAP_TNC */
1779 } else if (data->ttls_version == 0 &&
1780 ret->methodState == METHOD_MAY_CONT &&
1781 (ret->decision == DECISION_UNCOND_SUCC ||
1782 ret->decision == DECISION_COND_SUCC)) {
1783 wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1784 "completed successfully (MAY_CONT)");
1785 data->phase2_success = 1;
1790 static struct wpabuf * eap_ttls_process(struct eap_sm *sm, void *priv,
1791 struct eap_method_ret *ret,
1792 const struct wpabuf *reqData)
1797 struct wpabuf *resp;
1799 struct eap_ttls_data *data = priv;
1801 pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_TTLS, ret,
1802 reqData, &left, &flags);
1805 id = eap_get_id(reqData);
1807 if (flags & EAP_TLS_FLAGS_START) {
1808 if (eap_ttls_process_start(sm, data, flags, ret) < 0)
1811 /* RFC 5281, Ch. 9.2:
1812 * "This packet MAY contain additional information in the form
1813 * of AVPs, which may provide useful hints to the client"
1814 * For now, ignore any potential extra data.
1817 } else if (!data->ssl_initialized) {
1818 wpa_printf(MSG_DEBUG, "EAP-TTLS: First message did not "
1819 "include Start flag");
1820 ret->methodState = METHOD_DONE;
1821 ret->decision = DECISION_FAIL;
1822 ret->allowNotifications = FALSE;
1827 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1830 wpabuf_set(&msg, pos, left);
1831 res = eap_ttls_decrypt(sm, data, ret, id, &msg, &resp);
1833 res = eap_ttls_process_handshake(sm, data, ret, id,
1837 eap_ttls_check_auth_status(sm, data, ret);
1839 /* FIX: what about res == -1? Could just move all error processing into
1840 * the other functions and get rid of this res==1 case here. */
1843 return eap_peer_tls_build_ack(id, EAP_TYPE_TTLS,
1844 data->ttls_version);
1850 static Boolean eap_ttls_has_reauth_data(struct eap_sm *sm, void *priv)
1852 struct eap_ttls_data *data = priv;
1853 return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1854 data->phase2_success;
1858 static void eap_ttls_deinit_for_reauth(struct eap_sm *sm, void *priv)
1860 struct eap_ttls_data *data = priv;
1861 wpabuf_free(data->pending_phase2_req);
1862 data->pending_phase2_req = NULL;
1864 data->ready_for_tnc = 0;
1865 data->tnc_started = 0;
1866 #endif /* EAP_TNC */
1870 static void * eap_ttls_init_for_reauth(struct eap_sm *sm, void *priv)
1872 struct eap_ttls_data *data = priv;
1873 os_free(data->key_data);
1874 data->key_data = NULL;
1875 if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
1879 if (data->phase2_priv && data->phase2_method &&
1880 data->phase2_method->init_for_reauth)
1881 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
1882 data->phase2_start = 0;
1883 data->phase2_success = 0;
1890 static int eap_ttls_get_status(struct eap_sm *sm, void *priv, char *buf,
1891 size_t buflen, int verbose)
1893 struct eap_ttls_data *data = priv;
1896 len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
1897 ret = os_snprintf(buf + len, buflen - len,
1898 "EAP-TTLSv%d Phase2 method=",
1899 data->ttls_version);
1900 if (ret < 0 || (size_t) ret >= buflen - len)
1903 switch (data->phase2_type) {
1904 case EAP_TTLS_PHASE2_EAP:
1905 ret = os_snprintf(buf + len, buflen - len, "EAP-%s\n",
1906 data->phase2_method ?
1907 data->phase2_method->name : "?");
1909 case EAP_TTLS_PHASE2_MSCHAPV2:
1910 ret = os_snprintf(buf + len, buflen - len, "MSCHAPV2\n");
1912 case EAP_TTLS_PHASE2_MSCHAP:
1913 ret = os_snprintf(buf + len, buflen - len, "MSCHAP\n");
1915 case EAP_TTLS_PHASE2_PAP:
1916 ret = os_snprintf(buf + len, buflen - len, "PAP\n");
1918 case EAP_TTLS_PHASE2_CHAP:
1919 ret = os_snprintf(buf + len, buflen - len, "CHAP\n");
1925 if (ret < 0 || (size_t) ret >= buflen - len)
1933 static Boolean eap_ttls_isKeyAvailable(struct eap_sm *sm, void *priv)
1935 struct eap_ttls_data *data = priv;
1936 return data->key_data != NULL && data->phase2_success;
1940 static u8 * eap_ttls_getKey(struct eap_sm *sm, void *priv, size_t *len)
1942 struct eap_ttls_data *data = priv;
1945 if (data->key_data == NULL || !data->phase2_success)
1948 key = os_malloc(EAP_TLS_KEY_LEN);
1952 *len = EAP_TLS_KEY_LEN;
1953 os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
1959 int eap_peer_ttls_register(void)
1961 struct eap_method *eap;
1964 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1965 EAP_VENDOR_IETF, EAP_TYPE_TTLS, "TTLS");
1969 eap->init = eap_ttls_init;
1970 eap->deinit = eap_ttls_deinit;
1971 eap->process = eap_ttls_process;
1972 eap->isKeyAvailable = eap_ttls_isKeyAvailable;
1973 eap->getKey = eap_ttls_getKey;
1974 eap->get_status = eap_ttls_get_status;
1975 eap->has_reauth_data = eap_ttls_has_reauth_data;
1976 eap->deinit_for_reauth = eap_ttls_deinit_for_reauth;
1977 eap->init_for_reauth = eap_ttls_init_for_reauth;
1979 ret = eap_peer_method_register(eap);
1981 eap_peer_method_free(eap);