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/tls.h"
20 #include "eap_common/chap.h"
21 #include "eap_common/eap_ttls.h"
24 #include "eap_tls_common.h"
25 #include "eap_config.h"
28 /* Maximum supported TTLS version
30 * 1 = draft-funk-eap-ttls-v1-00.txt
32 #ifndef EAP_TTLS_VERSION
33 #define EAP_TTLS_VERSION 0 /* TTLSv1 implementation is not yet complete */
34 #endif /* EAP_TTLS_VERSION */
37 #define MSCHAPV2_KEY_LEN 16
38 #define MSCHAPV2_NT_RESPONSE_LEN 24
41 static void eap_ttls_deinit(struct eap_sm *sm, void *priv);
44 struct eap_ttls_data {
45 struct eap_ssl_data ssl;
48 int ttls_version, force_ttls_version;
50 const struct eap_method *phase2_method;
57 EAP_TTLS_PHASE2_MSCHAPV2,
58 EAP_TTLS_PHASE2_MSCHAP,
62 struct eap_method_type phase2_eap_type;
63 struct eap_method_type *phase2_eap_types;
64 size_t num_phase2_eap_types;
66 u8 auth_response[MSCHAPV2_AUTH_RESPONSE_LEN];
67 int auth_response_valid;
68 u8 master_key[MSCHAPV2_MASTER_KEY_LEN]; /* MSCHAPv2 master key */
70 int resuming; /* starting a resumed session */
71 int reauth; /* reauthentication */
74 struct wpabuf *pending_phase2_req;
83 static void * eap_ttls_init(struct eap_sm *sm)
85 struct eap_ttls_data *data;
86 struct eap_peer_config *config = eap_get_config(sm);
89 data = os_zalloc(sizeof(*data));
92 data->ttls_version = EAP_TTLS_VERSION;
93 data->force_ttls_version = -1;
95 data->phase2_type = EAP_TTLS_PHASE2_EAP;
97 #if EAP_TTLS_VERSION > 0
98 if (config && config->phase1) {
99 const char *pos = os_strstr(config->phase1, "ttlsver=");
101 data->force_ttls_version = atoi(pos + 8);
102 data->ttls_version = data->force_ttls_version;
103 wpa_printf(MSG_DEBUG, "EAP-TTLS: Forced TTLS version "
104 "%d", data->force_ttls_version);
107 #endif /* EAP_TTLS_VERSION */
109 if (config && config->phase2) {
110 if (os_strstr(config->phase2, "autheap=")) {
112 data->phase2_type = EAP_TTLS_PHASE2_EAP;
113 } else if (os_strstr(config->phase2, "auth=MSCHAPV2")) {
114 selected = "MSCHAPV2";
115 data->phase2_type = EAP_TTLS_PHASE2_MSCHAPV2;
116 } else if (os_strstr(config->phase2, "auth=MSCHAP")) {
118 data->phase2_type = EAP_TTLS_PHASE2_MSCHAP;
119 } else if (os_strstr(config->phase2, "auth=PAP")) {
121 data->phase2_type = EAP_TTLS_PHASE2_PAP;
122 } else if (os_strstr(config->phase2, "auth=CHAP")) {
124 data->phase2_type = EAP_TTLS_PHASE2_CHAP;
127 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase2 type: %s", selected);
129 if (data->phase2_type == EAP_TTLS_PHASE2_EAP) {
130 if (eap_peer_select_phase2_methods(config, "autheap=",
131 &data->phase2_eap_types,
132 &data->num_phase2_eap_types)
134 eap_ttls_deinit(sm, data);
138 data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
139 data->phase2_eap_type.method = EAP_TYPE_NONE;
142 #if EAP_TTLS_VERSION > 0
143 if (!(tls_capabilities(sm->ssl_ctx) & TLS_CAPABILITY_IA) &&
144 data->ttls_version > 0) {
145 if (data->force_ttls_version > 0) {
146 wpa_printf(MSG_INFO, "EAP-TTLS: Forced TTLSv%d and "
147 "TLS library does not support TLS/IA.",
148 data->force_ttls_version);
149 eap_ttls_deinit(sm, data);
152 data->ttls_version = 0;
154 #endif /* EAP_TTLS_VERSION */
160 static void eap_ttls_phase2_eap_deinit(struct eap_sm *sm,
161 struct eap_ttls_data *data)
163 if (data->phase2_priv && data->phase2_method) {
164 data->phase2_method->deinit(sm, data->phase2_priv);
165 data->phase2_method = NULL;
166 data->phase2_priv = NULL;
171 static void eap_ttls_deinit(struct eap_sm *sm, void *priv)
173 struct eap_ttls_data *data = priv;
176 eap_ttls_phase2_eap_deinit(sm, data);
177 os_free(data->phase2_eap_types);
178 if (data->ssl_initialized)
179 eap_peer_tls_ssl_deinit(sm, &data->ssl);
180 os_free(data->key_data);
181 wpabuf_free(data->pending_phase2_req);
186 static u8 * eap_ttls_avp_hdr(u8 *avphdr, u32 avp_code, u32 vendor_id,
187 int mandatory, size_t len)
189 struct ttls_avp_vendor *avp;
193 avp = (struct ttls_avp_vendor *) avphdr;
194 flags = mandatory ? AVP_FLAGS_MANDATORY : 0;
196 flags |= AVP_FLAGS_VENDOR;
197 hdrlen = sizeof(*avp);
198 avp->vendor_id = host_to_be32(vendor_id);
200 hdrlen = sizeof(struct ttls_avp);
203 avp->avp_code = host_to_be32(avp_code);
204 avp->avp_length = host_to_be32((flags << 24) | (hdrlen + len));
206 return avphdr + hdrlen;
210 static u8 * eap_ttls_avp_add(u8 *start, u8 *avphdr, u32 avp_code,
211 u32 vendor_id, int mandatory,
212 const u8 *data, size_t len)
215 pos = eap_ttls_avp_hdr(avphdr, avp_code, vendor_id, mandatory, len);
216 os_memcpy(pos, data, len);
223 static int eap_ttls_avp_encapsulate(struct wpabuf **resp, u32 avp_code,
229 msg = wpabuf_alloc(sizeof(struct ttls_avp) + wpabuf_len(*resp) + 4);
236 avp = wpabuf_mhead(msg);
237 pos = eap_ttls_avp_hdr(avp, avp_code, 0, mandatory, wpabuf_len(*resp));
238 os_memcpy(pos, wpabuf_head(*resp), wpabuf_len(*resp));
239 pos += wpabuf_len(*resp);
242 wpabuf_put(msg, pos - avp);
248 #if EAP_TTLS_VERSION > 0
249 static int eap_ttls_ia_permute_inner_secret(struct eap_sm *sm,
250 struct eap_ttls_data *data,
251 const u8 *key, size_t key_len)
258 buf_len = 2 + key_len;
259 buf = os_malloc(buf_len);
262 WPA_PUT_BE16(buf, key_len);
263 os_memcpy(buf + 2, key, key_len);
269 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Session keys for TLS/IA inner "
270 "secret permutation", buf, buf_len);
271 ret = tls_connection_ia_permute_inner_secret(sm->ssl_ctx,
278 #endif /* EAP_TTLS_VERSION */
281 static int eap_ttls_v0_derive_key(struct eap_sm *sm,
282 struct eap_ttls_data *data)
284 os_free(data->key_data);
285 data->key_data = eap_peer_tls_derive_key(sm, &data->ssl,
286 "ttls keying material",
288 if (!data->key_data) {
289 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to derive key");
293 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
294 data->key_data, EAP_TLS_KEY_LEN);
300 #if EAP_TTLS_VERSION > 0
301 static int eap_ttls_v1_derive_key(struct eap_sm *sm,
302 struct eap_ttls_data *data)
304 struct tls_keys keys;
307 os_free(data->key_data);
308 data->key_data = NULL;
310 os_memset(&keys, 0, sizeof(keys));
311 if (tls_connection_get_keys(sm->ssl_ctx, data->ssl.conn, &keys) ||
312 keys.client_random == NULL || keys.server_random == NULL ||
313 keys.inner_secret == NULL) {
314 wpa_printf(MSG_INFO, "EAP-TTLS: Could not get inner secret, "
315 "client random, or server random to derive keying "
320 rnd = os_malloc(keys.client_random_len + keys.server_random_len);
321 data->key_data = os_malloc(EAP_TLS_KEY_LEN);
322 if (rnd == NULL || data->key_data == NULL) {
323 wpa_printf(MSG_INFO, "EAP-TTLS: No memory for key derivation");
325 os_free(data->key_data);
326 data->key_data = NULL;
329 os_memcpy(rnd, keys.client_random, keys.client_random_len);
330 os_memcpy(rnd + keys.client_random_len, keys.server_random,
331 keys.server_random_len);
333 if (tls_prf(keys.inner_secret, keys.inner_secret_len,
334 "ttls v1 keying material", rnd, keys.client_random_len +
335 keys.server_random_len, data->key_data, EAP_TLS_KEY_LEN)) {
336 wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to derive key");
338 os_free(data->key_data);
339 data->key_data = NULL;
343 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: client/server random",
344 rnd, keys.client_random_len + keys.server_random_len);
345 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: TLS/IA inner secret",
346 keys.inner_secret, keys.inner_secret_len);
350 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
351 data->key_data, EAP_TLS_KEY_LEN);
355 #endif /* EAP_TTLS_VERSION */
358 static u8 * eap_ttls_implicit_challenge(struct eap_sm *sm,
359 struct eap_ttls_data *data, size_t len)
361 #if EAP_TTLS_VERSION > 0
362 struct tls_keys keys;
364 #endif /* EAP_TTLS_VERSION */
366 if (data->ttls_version == 0) {
367 return eap_peer_tls_derive_key(sm, &data->ssl,
368 "ttls challenge", len);
371 #if EAP_TTLS_VERSION > 0
373 os_memset(&keys, 0, sizeof(keys));
374 if (tls_connection_get_keys(sm->ssl_ctx, data->ssl.conn, &keys) ||
375 keys.client_random == NULL || keys.server_random == NULL ||
376 keys.inner_secret == NULL) {
377 wpa_printf(MSG_INFO, "EAP-TTLS: Could not get inner secret, "
378 "client random, or server random to derive "
379 "implicit challenge");
383 rnd = os_malloc(keys.client_random_len + keys.server_random_len);
384 challenge = os_malloc(len);
385 if (rnd == NULL || challenge == NULL) {
386 wpa_printf(MSG_INFO, "EAP-TTLS: No memory for implicit "
387 "challenge derivation");
392 os_memcpy(rnd, keys.server_random, keys.server_random_len);
393 os_memcpy(rnd + keys.server_random_len, keys.client_random,
394 keys.client_random_len);
396 if (tls_prf(keys.inner_secret, keys.inner_secret_len,
397 "inner application challenge", rnd,
398 keys.client_random_len + keys.server_random_len,
400 wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to derive implicit "
409 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived implicit challenge",
414 #else /* EAP_TTLS_VERSION */
418 #endif /* EAP_TTLS_VERSION */
422 static void eap_ttlsv1_phase2_eap_finish(struct eap_sm *sm,
423 struct eap_ttls_data *data,
424 struct eap_method_ret *ret)
426 #if EAP_TTLS_VERSION > 0
427 if (data->ttls_version > 0) {
428 const struct eap_method *m = data->phase2_method;
429 void *priv = data->phase2_priv;
431 /* TTLSv1 requires TLS/IA FinalPhaseFinished */
432 if (ret->decision == DECISION_UNCOND_SUCC)
433 ret->decision = DECISION_COND_SUCC;
434 ret->methodState = METHOD_CONT;
436 if (ret->decision == DECISION_COND_SUCC &&
437 m->isKeyAvailable && m->getKey &&
438 m->isKeyAvailable(sm, priv)) {
441 key = m->getKey(sm, priv, &key_len);
443 eap_ttls_ia_permute_inner_secret(
444 sm, data, key, key_len);
449 #endif /* EAP_TTLS_VERSION */
453 static void eap_ttls_phase2_select_eap_method(struct eap_ttls_data *data,
457 for (i = 0; i < data->num_phase2_eap_types; i++) {
458 if (data->phase2_eap_types[i].vendor != EAP_VENDOR_IETF ||
459 data->phase2_eap_types[i].method != method)
462 data->phase2_eap_type.vendor =
463 data->phase2_eap_types[i].vendor;
464 data->phase2_eap_type.method =
465 data->phase2_eap_types[i].method;
466 wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
467 "Phase 2 EAP vendor %d method %d",
468 data->phase2_eap_type.vendor,
469 data->phase2_eap_type.method);
475 static int eap_ttls_phase2_eap_process(struct eap_sm *sm,
476 struct eap_ttls_data *data,
477 struct eap_method_ret *ret,
478 struct eap_hdr *hdr, size_t len,
479 struct wpabuf **resp)
482 struct eap_method_ret iret;
484 os_memset(&iret, 0, sizeof(iret));
485 wpabuf_set(&msg, hdr, len);
486 *resp = data->phase2_method->process(sm, data->phase2_priv, &iret,
488 if ((iret.methodState == METHOD_DONE ||
489 iret.methodState == METHOD_MAY_CONT) &&
490 (iret.decision == DECISION_UNCOND_SUCC ||
491 iret.decision == DECISION_COND_SUCC ||
492 iret.decision == DECISION_FAIL)) {
493 ret->methodState = iret.methodState;
494 ret->decision = iret.decision;
496 eap_ttlsv1_phase2_eap_finish(sm, data, ret);
502 static int eap_ttls_phase2_request_eap_method(struct eap_sm *sm,
503 struct eap_ttls_data *data,
504 struct eap_method_ret *ret,
505 struct eap_hdr *hdr, size_t len,
506 u8 method, struct wpabuf **resp)
509 if (data->tnc_started && data->phase2_method &&
510 data->phase2_priv && method == EAP_TYPE_TNC &&
511 data->phase2_eap_type.method == EAP_TYPE_TNC)
512 return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len,
515 if (data->ready_for_tnc && !data->tnc_started &&
516 method == EAP_TYPE_TNC) {
517 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
519 data->tnc_started = 1;
522 if (data->tnc_started) {
523 if (data->phase2_eap_type.vendor != EAP_VENDOR_IETF ||
524 data->phase2_eap_type.method == EAP_TYPE_TNC) {
525 wpa_printf(MSG_DEBUG, "EAP-TTLS: Unexpected EAP "
526 "type %d for TNC", method);
530 data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
531 data->phase2_eap_type.method = method;
532 wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
533 "Phase 2 EAP vendor %d method %d (TNC)",
534 data->phase2_eap_type.vendor,
535 data->phase2_eap_type.method);
537 if (data->phase2_type == EAP_TTLS_PHASE2_EAP)
538 eap_ttls_phase2_eap_deinit(sm, data);
542 if (data->phase2_eap_type.vendor == EAP_VENDOR_IETF &&
543 data->phase2_eap_type.method == EAP_TYPE_NONE)
544 eap_ttls_phase2_select_eap_method(data, method);
546 if (method != data->phase2_eap_type.method || method == EAP_TYPE_NONE)
548 if (eap_peer_tls_phase2_nak(data->phase2_eap_types,
549 data->num_phase2_eap_types,
555 if (data->phase2_priv == NULL) {
556 data->phase2_method = eap_peer_get_eap_method(
557 EAP_VENDOR_IETF, method);
558 if (data->phase2_method) {
560 data->phase2_priv = data->phase2_method->init(sm);
564 if (data->phase2_priv == NULL || data->phase2_method == NULL) {
565 wpa_printf(MSG_INFO, "EAP-TTLS: failed to initialize "
566 "Phase 2 EAP method %d", method);
570 return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len, resp);
574 static int eap_ttls_phase2_request_eap(struct eap_sm *sm,
575 struct eap_ttls_data *data,
576 struct eap_method_ret *ret,
578 struct wpabuf **resp)
580 size_t len = be_to_host16(hdr->length);
582 struct eap_peer_config *config = eap_get_config(sm);
584 if (len <= sizeof(struct eap_hdr)) {
585 wpa_printf(MSG_INFO, "EAP-TTLS: too short "
586 "Phase 2 request (len=%lu)", (unsigned long) len);
589 pos = (u8 *) (hdr + 1);
590 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP Request: type=%d", *pos);
592 case EAP_TYPE_IDENTITY:
593 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
596 if (eap_ttls_phase2_request_eap_method(sm, data, ret, hdr, len,
603 (config->pending_req_identity || config->pending_req_password ||
604 config->pending_req_otp)) {
611 wpa_hexdump_buf(MSG_DEBUG, "EAP-TTLS: AVP encapsulate EAP Response",
613 return eap_ttls_avp_encapsulate(resp, RADIUS_ATTR_EAP_MESSAGE, 1);
617 static void eap_ttlsv1_permute_inner(struct eap_sm *sm,
618 struct eap_ttls_data *data)
620 #if EAP_TTLS_VERSION > 0
621 u8 session_key[2 * MSCHAPV2_KEY_LEN];
623 if (data->ttls_version == 0)
626 get_asymetric_start_key(data->master_key, session_key,
627 MSCHAPV2_KEY_LEN, 0, 0);
628 get_asymetric_start_key(data->master_key,
629 session_key + MSCHAPV2_KEY_LEN,
630 MSCHAPV2_KEY_LEN, 1, 0);
631 eap_ttls_ia_permute_inner_secret(sm, data, session_key,
632 sizeof(session_key));
633 #endif /* EAP_TTLS_VERSION */
637 static int eap_ttls_phase2_request_mschapv2(struct eap_sm *sm,
638 struct eap_ttls_data *data,
639 struct eap_method_ret *ret,
640 struct wpabuf **resp)
643 u8 *buf, *pos, *challenge, *peer_challenge;
644 const u8 *identity, *password;
645 size_t identity_len, password_len;
648 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAPV2 Request");
650 identity = eap_get_config_identity(sm, &identity_len);
651 password = eap_get_config_password2(sm, &password_len, &pwhash);
652 if (identity == NULL || password == NULL)
655 msg = wpabuf_alloc(identity_len + 1000);
657 wpa_printf(MSG_ERROR,
658 "EAP-TTLS/MSCHAPV2: Failed to allocate memory");
661 pos = buf = wpabuf_mhead(msg);
664 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
665 identity, identity_len);
667 /* MS-CHAP-Challenge */
668 challenge = eap_ttls_implicit_challenge(
669 sm, data, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN + 1);
670 if (challenge == NULL) {
672 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
673 "implicit challenge");
676 peer_challenge = challenge + 1 + EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
678 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
679 RADIUS_VENDOR_ID_MICROSOFT, 1,
680 challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
682 /* MS-CHAP2-Response */
683 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP2_RESPONSE,
684 RADIUS_VENDOR_ID_MICROSOFT, 1,
685 EAP_TTLS_MSCHAPV2_RESPONSE_LEN);
686 data->ident = challenge[EAP_TTLS_MSCHAPV2_CHALLENGE_LEN];
687 *pos++ = data->ident;
688 *pos++ = 0; /* Flags */
689 os_memcpy(pos, peer_challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
690 pos += EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
691 os_memset(pos, 0, 8); /* Reserved, must be zero */
693 if (mschapv2_derive_response(identity, identity_len, password,
694 password_len, pwhash, challenge,
695 peer_challenge, pos, data->auth_response,
698 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
702 data->auth_response_valid = 1;
704 eap_ttlsv1_permute_inner(sm, data);
710 wpabuf_put(msg, pos - buf);
713 if (sm->workaround && data->ttls_version == 0) {
714 /* At least FreeRADIUS seems to be terminating
715 * EAP-TTLS/MSHCAPV2 without the expected MS-CHAP-v2 Success
717 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: EAP workaround - "
718 "allow success without tunneled response");
719 ret->methodState = METHOD_MAY_CONT;
720 ret->decision = DECISION_COND_SUCC;
727 static int eap_ttls_phase2_request_mschap(struct eap_sm *sm,
728 struct eap_ttls_data *data,
729 struct eap_method_ret *ret,
730 struct wpabuf **resp)
733 u8 *buf, *pos, *challenge;
734 const u8 *identity, *password;
735 size_t identity_len, password_len;
738 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAP Request");
740 identity = eap_get_config_identity(sm, &identity_len);
741 password = eap_get_config_password2(sm, &password_len, &pwhash);
742 if (identity == NULL || password == NULL)
745 msg = wpabuf_alloc(identity_len + 1000);
747 wpa_printf(MSG_ERROR,
748 "EAP-TTLS/MSCHAP: Failed to allocate memory");
751 pos = buf = wpabuf_mhead(msg);
754 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
755 identity, identity_len);
757 /* MS-CHAP-Challenge */
758 challenge = eap_ttls_implicit_challenge(
759 sm, data, EAP_TTLS_MSCHAP_CHALLENGE_LEN + 1);
760 if (challenge == NULL) {
762 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAP: Failed to derive "
763 "implicit challenge");
767 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
768 RADIUS_VENDOR_ID_MICROSOFT, 1,
769 challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
771 /* MS-CHAP-Response */
772 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP_RESPONSE,
773 RADIUS_VENDOR_ID_MICROSOFT, 1,
774 EAP_TTLS_MSCHAP_RESPONSE_LEN);
775 data->ident = challenge[EAP_TTLS_MSCHAP_CHALLENGE_LEN];
776 *pos++ = data->ident;
777 *pos++ = 1; /* Flags: Use NT style passwords */
778 os_memset(pos, 0, 24); /* LM-Response */
781 challenge_response(challenge, password, pos); /* NT-Response */
782 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password hash",
785 nt_challenge_response(challenge, password, password_len,
786 pos); /* NT-Response */
787 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password",
788 password, password_len);
790 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP implicit challenge",
791 challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
792 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP response", pos, 24);
797 wpabuf_put(msg, pos - buf);
800 if (data->ttls_version > 0) {
801 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
802 * so do not allow connection to be terminated yet. */
803 ret->methodState = METHOD_CONT;
804 ret->decision = DECISION_COND_SUCC;
806 /* EAP-TTLS/MSCHAP does not provide tunneled success
807 * notification, so assume that Phase2 succeeds. */
808 ret->methodState = METHOD_DONE;
809 ret->decision = DECISION_COND_SUCC;
816 static int eap_ttls_phase2_request_pap(struct eap_sm *sm,
817 struct eap_ttls_data *data,
818 struct eap_method_ret *ret,
819 struct wpabuf **resp)
824 const u8 *identity, *password;
825 size_t identity_len, password_len;
827 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 PAP Request");
829 identity = eap_get_config_identity(sm, &identity_len);
830 password = eap_get_config_password(sm, &password_len);
831 if (identity == NULL || password == NULL)
834 msg = wpabuf_alloc(identity_len + password_len + 100);
836 wpa_printf(MSG_ERROR,
837 "EAP-TTLS/PAP: Failed to allocate memory");
840 pos = buf = wpabuf_mhead(msg);
843 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
844 identity, identity_len);
846 /* User-Password; in RADIUS, this is encrypted, but EAP-TTLS encrypts
847 * the data, so no separate encryption is used in the AVP itself.
848 * However, the password is padded to obfuscate its length. */
849 pad = (16 - (password_len & 15)) & 15;
850 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_USER_PASSWORD, 0, 1,
852 os_memcpy(pos, password, password_len);
854 os_memset(pos, 0, pad);
858 wpabuf_put(msg, pos - buf);
861 if (data->ttls_version > 0) {
862 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
863 * so do not allow connection to be terminated yet. */
864 ret->methodState = METHOD_CONT;
865 ret->decision = DECISION_COND_SUCC;
867 /* EAP-TTLS/PAP does not provide tunneled success notification,
868 * so assume that Phase2 succeeds. */
869 ret->methodState = METHOD_DONE;
870 ret->decision = DECISION_COND_SUCC;
877 static int eap_ttls_phase2_request_chap(struct eap_sm *sm,
878 struct eap_ttls_data *data,
879 struct eap_method_ret *ret,
880 struct wpabuf **resp)
883 u8 *buf, *pos, *challenge;
884 const u8 *identity, *password;
885 size_t identity_len, password_len;
887 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 CHAP Request");
889 identity = eap_get_config_identity(sm, &identity_len);
890 password = eap_get_config_password(sm, &password_len);
891 if (identity == NULL || password == NULL)
894 msg = wpabuf_alloc(identity_len + 1000);
896 wpa_printf(MSG_ERROR,
897 "EAP-TTLS/CHAP: Failed to allocate memory");
900 pos = buf = wpabuf_mhead(msg);
903 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
904 identity, identity_len);
907 challenge = eap_ttls_implicit_challenge(
908 sm, data, EAP_TTLS_CHAP_CHALLENGE_LEN + 1);
909 if (challenge == NULL) {
911 wpa_printf(MSG_ERROR, "EAP-TTLS/CHAP: Failed to derive "
912 "implicit challenge");
916 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_CHAP_CHALLENGE, 0, 1,
917 challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
920 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_CHAP_PASSWORD, 0, 1,
921 1 + EAP_TTLS_CHAP_PASSWORD_LEN);
922 data->ident = challenge[EAP_TTLS_CHAP_CHALLENGE_LEN];
923 *pos++ = data->ident;
925 /* MD5(Ident + Password + Challenge) */
926 chap_md5(data->ident, password, password_len, challenge,
927 EAP_TTLS_CHAP_CHALLENGE_LEN, pos);
929 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: CHAP username",
930 identity, identity_len);
931 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: CHAP password",
932 password, password_len);
933 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP implicit challenge",
934 challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
935 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP password",
936 pos, EAP_TTLS_CHAP_PASSWORD_LEN);
937 pos += EAP_TTLS_CHAP_PASSWORD_LEN;
941 wpabuf_put(msg, pos - buf);
944 if (data->ttls_version > 0) {
945 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
946 * so do not allow connection to be terminated yet. */
947 ret->methodState = METHOD_CONT;
948 ret->decision = DECISION_COND_SUCC;
950 /* EAP-TTLS/CHAP does not provide tunneled success
951 * notification, so assume that Phase2 succeeds. */
952 ret->methodState = METHOD_DONE;
953 ret->decision = DECISION_COND_SUCC;
960 static int eap_ttls_phase2_request(struct eap_sm *sm,
961 struct eap_ttls_data *data,
962 struct eap_method_ret *ret,
964 struct wpabuf **resp)
968 enum phase2_types phase2_type = data->phase2_type;
971 if (data->tnc_started) {
972 wpa_printf(MSG_DEBUG, "EAP-TTLS: Processing TNC");
973 phase2_type = EAP_TTLS_PHASE2_EAP;
977 if (phase2_type == EAP_TTLS_PHASE2_MSCHAPV2 ||
978 phase2_type == EAP_TTLS_PHASE2_MSCHAP ||
979 phase2_type == EAP_TTLS_PHASE2_PAP ||
980 phase2_type == EAP_TTLS_PHASE2_CHAP) {
981 if (eap_get_config_identity(sm, &len) == NULL) {
983 "EAP-TTLS: Identity not configured");
984 eap_sm_request_identity(sm);
985 if (eap_get_config_password(sm, &len) == NULL)
986 eap_sm_request_password(sm);
990 if (eap_get_config_password(sm, &len) == NULL) {
992 "EAP-TTLS: Password not configured");
993 eap_sm_request_password(sm);
998 switch (phase2_type) {
999 case EAP_TTLS_PHASE2_EAP:
1000 res = eap_ttls_phase2_request_eap(sm, data, ret, hdr, resp);
1002 case EAP_TTLS_PHASE2_MSCHAPV2:
1003 res = eap_ttls_phase2_request_mschapv2(sm, data, ret, resp);
1005 case EAP_TTLS_PHASE2_MSCHAP:
1006 res = eap_ttls_phase2_request_mschap(sm, data, ret, resp);
1008 case EAP_TTLS_PHASE2_PAP:
1009 res = eap_ttls_phase2_request_pap(sm, data, ret, resp);
1011 case EAP_TTLS_PHASE2_CHAP:
1012 res = eap_ttls_phase2_request_chap(sm, data, ret, resp);
1015 wpa_printf(MSG_ERROR, "EAP-TTLS: Phase 2 - Unknown");
1021 ret->methodState = METHOD_DONE;
1022 ret->decision = DECISION_FAIL;
1029 #if EAP_TTLS_VERSION > 0
1030 static struct wpabuf * eap_ttls_build_phase_finished(
1031 struct eap_sm *sm, struct eap_ttls_data *data, int id, int final)
1036 const int max_len = 300;
1038 req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TTLS, 1 + max_len,
1039 EAP_CODE_RESPONSE, id);
1043 wpabuf_put_u8(req, data->ttls_version);
1045 pos = wpabuf_put(req, 0);
1046 len = tls_connection_ia_send_phase_finished(sm->ssl_ctx,
1048 final, pos, max_len);
1053 wpabuf_put(req, len);
1054 eap_update_len(req);
1058 #endif /* EAP_TTLS_VERSION */
1061 struct ttls_parse_avp {
1069 static int eap_ttls_parse_attr_eap(const u8 *dpos, size_t dlen,
1070 struct ttls_parse_avp *parse)
1072 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - EAP Message");
1073 if (parse->eapdata == NULL) {
1074 parse->eapdata = os_malloc(dlen);
1075 if (parse->eapdata == NULL) {
1076 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
1077 "memory for Phase 2 EAP data");
1080 os_memcpy(parse->eapdata, dpos, dlen);
1081 parse->eap_len = dlen;
1083 u8 *neweap = os_realloc(parse->eapdata, parse->eap_len + dlen);
1084 if (neweap == NULL) {
1085 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
1086 "memory for Phase 2 EAP data");
1089 os_memcpy(neweap + parse->eap_len, dpos, dlen);
1090 parse->eapdata = neweap;
1091 parse->eap_len += dlen;
1098 static int eap_ttls_parse_avp(u8 *pos, size_t left,
1099 struct ttls_parse_avp *parse)
1101 struct ttls_avp *avp;
1102 u32 avp_code, avp_length, vendor_id = 0;
1103 u8 avp_flags, *dpos;
1106 avp = (struct ttls_avp *) pos;
1107 avp_code = be_to_host32(avp->avp_code);
1108 avp_length = be_to_host32(avp->avp_length);
1109 avp_flags = (avp_length >> 24) & 0xff;
1110 avp_length &= 0xffffff;
1111 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP: code=%d flags=0x%02x "
1112 "length=%d", (int) avp_code, avp_flags,
1115 if (avp_length > left) {
1116 wpa_printf(MSG_WARNING, "EAP-TTLS: AVP overflow "
1117 "(len=%d, left=%lu) - dropped",
1118 (int) avp_length, (unsigned long) left);
1122 if (avp_length < sizeof(*avp)) {
1123 wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid AVP length %d",
1128 dpos = (u8 *) (avp + 1);
1129 dlen = avp_length - sizeof(*avp);
1130 if (avp_flags & AVP_FLAGS_VENDOR) {
1132 wpa_printf(MSG_WARNING, "EAP-TTLS: Vendor AVP "
1136 vendor_id = WPA_GET_BE32(dpos);
1137 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP vendor_id %d",
1143 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP data", dpos, dlen);
1145 if (vendor_id == 0 && avp_code == RADIUS_ATTR_EAP_MESSAGE) {
1146 if (eap_ttls_parse_attr_eap(dpos, dlen, parse) < 0)
1148 } else if (vendor_id == 0 && avp_code == RADIUS_ATTR_REPLY_MESSAGE) {
1149 /* This is an optional message that can be displayed to
1151 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: AVP - Reply-Message",
1153 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
1154 avp_code == RADIUS_ATTR_MS_CHAP2_SUCCESS) {
1155 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP2-Success",
1158 wpa_printf(MSG_WARNING, "EAP-TTLS: Unexpected "
1159 "MS-CHAP2-Success length "
1160 "(len=%lu, expected 43)",
1161 (unsigned long) dlen);
1164 parse->mschapv2 = dpos;
1165 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
1166 avp_code == RADIUS_ATTR_MS_CHAP_ERROR) {
1167 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP-Error",
1169 parse->mschapv2_error = 1;
1170 } else if (avp_flags & AVP_FLAGS_MANDATORY) {
1171 wpa_printf(MSG_WARNING, "EAP-TTLS: Unsupported mandatory AVP "
1172 "code %d vendor_id %d - dropped",
1173 (int) avp_code, (int) vendor_id);
1176 wpa_printf(MSG_DEBUG, "EAP-TTLS: Ignoring unsupported AVP "
1177 "code %d vendor_id %d",
1178 (int) avp_code, (int) vendor_id);
1185 static int eap_ttls_parse_avps(struct wpabuf *in_decrypted,
1186 struct ttls_parse_avp *parse)
1192 pos = wpabuf_mhead(in_decrypted);
1193 left = wpabuf_len(in_decrypted);
1194 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Decrypted Phase 2 AVPs", pos, left);
1195 if (left < sizeof(struct ttls_avp)) {
1196 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 AVP frame"
1197 " len=%lu expected %lu or more - dropped",
1198 (unsigned long) left,
1199 (unsigned long) sizeof(struct ttls_avp));
1204 os_memset(parse, 0, sizeof(*parse));
1207 avp_length = eap_ttls_parse_avp(pos, left, parse);
1211 pad = (4 - (avp_length & 3)) & 3;
1212 pos += avp_length + pad;
1213 if (left < avp_length + pad)
1216 left -= avp_length + pad;
1223 static u8 * eap_ttls_fake_identity_request(void)
1225 struct eap_hdr *hdr;
1228 wpa_printf(MSG_DEBUG, "EAP-TTLS: empty data in beginning of "
1229 "Phase 2 - use fake EAP-Request Identity");
1230 buf = os_malloc(sizeof(*hdr) + 1);
1232 wpa_printf(MSG_WARNING, "EAP-TTLS: failed to allocate "
1233 "memory for fake EAP-Identity Request");
1237 hdr = (struct eap_hdr *) buf;
1238 hdr->code = EAP_CODE_REQUEST;
1239 hdr->identifier = 0;
1240 hdr->length = host_to_be16(sizeof(*hdr) + 1);
1241 buf[sizeof(*hdr)] = EAP_TYPE_IDENTITY;
1247 static int eap_ttls_encrypt_response(struct eap_sm *sm,
1248 struct eap_ttls_data *data,
1249 struct wpabuf *resp, u8 identifier,
1250 struct wpabuf **out_data)
1255 wpa_hexdump_buf_key(MSG_DEBUG, "EAP-TTLS: Encrypting Phase 2 data",
1257 if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
1258 data->ttls_version, identifier,
1260 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to encrypt a Phase 2 "
1270 static int eap_ttls_process_phase2_eap(struct eap_sm *sm,
1271 struct eap_ttls_data *data,
1272 struct eap_method_ret *ret,
1273 struct ttls_parse_avp *parse,
1274 struct wpabuf **resp)
1276 struct eap_hdr *hdr;
1279 if (parse->eapdata == NULL) {
1280 wpa_printf(MSG_WARNING, "EAP-TTLS: No EAP Message in the "
1281 "packet - dropped");
1285 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP",
1286 parse->eapdata, parse->eap_len);
1287 hdr = (struct eap_hdr *) parse->eapdata;
1289 if (parse->eap_len < sizeof(*hdr)) {
1290 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 EAP "
1291 "frame (len=%lu, expected %lu or more) - dropped",
1292 (unsigned long) parse->eap_len,
1293 (unsigned long) sizeof(*hdr));
1296 len = be_to_host16(hdr->length);
1297 if (len > parse->eap_len) {
1298 wpa_printf(MSG_INFO, "EAP-TTLS: Length mismatch in Phase 2 "
1299 "EAP frame (EAP hdr len=%lu, EAP data len in "
1301 (unsigned long) len,
1302 (unsigned long) parse->eap_len);
1305 wpa_printf(MSG_DEBUG, "EAP-TTLS: received Phase 2: code=%d "
1306 "identifier=%d length=%lu",
1307 hdr->code, hdr->identifier, (unsigned long) len);
1308 switch (hdr->code) {
1309 case EAP_CODE_REQUEST:
1310 if (eap_ttls_phase2_request(sm, data, ret, hdr, resp)) {
1311 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
1312 "processing failed");
1317 wpa_printf(MSG_INFO, "EAP-TTLS: Unexpected code=%d in "
1318 "Phase 2 EAP header", hdr->code);
1326 static int eap_ttls_process_phase2_mschapv2(struct eap_sm *sm,
1327 struct eap_ttls_data *data,
1328 struct eap_method_ret *ret,
1329 struct ttls_parse_avp *parse)
1331 if (parse->mschapv2_error) {
1332 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Received "
1333 "MS-CHAP-Error - failed");
1334 ret->methodState = METHOD_DONE;
1335 ret->decision = DECISION_FAIL;
1336 /* Reply with empty data to ACK error */
1340 if (parse->mschapv2 == NULL) {
1342 if (data->phase2_success && parse->eapdata) {
1344 * Allow EAP-TNC to be started after successfully
1345 * completed MSCHAPV2.
1349 #endif /* EAP_TNC */
1350 wpa_printf(MSG_WARNING, "EAP-TTLS: no MS-CHAP2-Success AVP "
1351 "received for Phase2 MSCHAPV2");
1354 if (parse->mschapv2[0] != data->ident) {
1355 wpa_printf(MSG_WARNING, "EAP-TTLS: Ident mismatch for Phase 2 "
1356 "MSCHAPV2 (received Ident 0x%02x, expected 0x%02x)",
1357 parse->mschapv2[0], data->ident);
1360 if (!data->auth_response_valid ||
1361 mschapv2_verify_auth_response(data->auth_response,
1362 parse->mschapv2 + 1, 42)) {
1363 wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid authenticator "
1364 "response in Phase 2 MSCHAPV2 success request");
1368 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 MSCHAPV2 "
1369 "authentication succeeded");
1370 if (data->ttls_version > 0) {
1372 * EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report
1373 * success, so do not allow connection to be terminated
1376 ret->methodState = METHOD_CONT;
1377 ret->decision = DECISION_COND_SUCC;
1379 ret->methodState = METHOD_DONE;
1380 ret->decision = DECISION_UNCOND_SUCC;
1381 data->phase2_success = 1;
1385 * Reply with empty data; authentication server will reply
1386 * with EAP-Success after this.
1393 static int eap_ttls_process_tnc_start(struct eap_sm *sm,
1394 struct eap_ttls_data *data,
1395 struct eap_method_ret *ret,
1396 struct ttls_parse_avp *parse,
1397 struct wpabuf **resp)
1399 /* TNC uses inner EAP method after non-EAP TTLS phase 2. */
1400 if (parse->eapdata == NULL) {
1401 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
1402 "unexpected tunneled data (no EAP)");
1406 if (!data->ready_for_tnc) {
1407 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
1408 "EAP after non-EAP, but not ready for TNC");
1412 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
1414 data->tnc_started = 1;
1416 if (eap_ttls_process_phase2_eap(sm, data, ret, parse, resp) < 0)
1421 #endif /* EAP_TNC */
1424 static int eap_ttls_process_decrypted(struct eap_sm *sm,
1425 struct eap_ttls_data *data,
1426 struct eap_method_ret *ret,
1428 struct ttls_parse_avp *parse,
1429 struct wpabuf *in_decrypted,
1430 struct wpabuf **out_data)
1432 struct wpabuf *resp = NULL;
1433 struct eap_peer_config *config = eap_get_config(sm);
1435 enum phase2_types phase2_type = data->phase2_type;
1438 if (data->tnc_started)
1439 phase2_type = EAP_TTLS_PHASE2_EAP;
1440 #endif /* EAP_TNC */
1442 switch (phase2_type) {
1443 case EAP_TTLS_PHASE2_EAP:
1444 if (eap_ttls_process_phase2_eap(sm, data, ret, parse, &resp) <
1448 case EAP_TTLS_PHASE2_MSCHAPV2:
1449 res = eap_ttls_process_phase2_mschapv2(sm, data, ret, parse);
1451 if (res == 1 && parse->eapdata && data->phase2_success) {
1453 * TNC may be required as the next
1454 * authentication method within the tunnel.
1456 ret->methodState = METHOD_MAY_CONT;
1457 data->ready_for_tnc = 1;
1458 if (eap_ttls_process_tnc_start(sm, data, ret, parse,
1462 #endif /* EAP_TNC */
1464 case EAP_TTLS_PHASE2_MSCHAP:
1465 case EAP_TTLS_PHASE2_PAP:
1466 case EAP_TTLS_PHASE2_CHAP:
1468 if (eap_ttls_process_tnc_start(sm, data, ret, parse, &resp) <
1473 /* EAP-TTLS/{MSCHAP,PAP,CHAP} should not send any TLS tunneled
1474 * requests to the supplicant */
1475 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received unexpected "
1478 #endif /* EAP_TNC */
1482 if (eap_ttls_encrypt_response(sm, data, resp, identifier,
1485 } else if (config->pending_req_identity ||
1486 config->pending_req_password ||
1487 config->pending_req_otp ||
1488 config->pending_req_new_password) {
1489 wpabuf_free(data->pending_phase2_req);
1490 data->pending_phase2_req = wpabuf_dup(in_decrypted);
1497 #if EAP_TTLS_VERSION > 0
1498 static void eap_ttls_final_phase_finished(struct eap_sm *sm,
1499 struct eap_ttls_data *data,
1500 struct eap_method_ret *ret,
1502 struct wpabuf **out_data)
1504 wpa_printf(MSG_DEBUG, "EAP-TTLS: FinalPhaseFinished received");
1505 wpa_printf(MSG_INFO, "EAP-TTLS: TLS/IA authentication succeeded");
1506 ret->methodState = METHOD_DONE;
1507 ret->decision = DECISION_UNCOND_SUCC;
1508 data->phase2_success = 1;
1509 *out_data = eap_ttls_build_phase_finished(sm, data, identifier, 1);
1510 eap_ttls_v1_derive_key(sm, data);
1512 #endif /* EAP_TTLS_VERSION */
1515 static int eap_ttls_implicit_identity_request(struct eap_sm *sm,
1516 struct eap_ttls_data *data,
1517 struct eap_method_ret *ret,
1519 struct wpabuf **out_data)
1522 struct eap_hdr *hdr;
1523 struct wpabuf *resp;
1525 hdr = (struct eap_hdr *) eap_ttls_fake_identity_request();
1527 ret->methodState = METHOD_DONE;
1528 ret->decision = DECISION_FAIL;
1533 if (eap_ttls_phase2_request(sm, data, ret, hdr, &resp)) {
1534 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
1535 "processing failed");
1538 retval = eap_ttls_encrypt_response(sm, data, resp, identifier,
1545 ret->methodState = METHOD_DONE;
1546 ret->decision = DECISION_FAIL;
1553 static int eap_ttls_phase2_start(struct eap_sm *sm, struct eap_ttls_data *data,
1554 struct eap_method_ret *ret, u8 identifier,
1555 struct wpabuf **out_data)
1557 data->phase2_start = 0;
1560 * EAP-TTLS does not use Phase2 on fast re-auth; this must be done only
1561 * if TLS part was indeed resuming a previous session. Most
1562 * Authentication Servers terminate EAP-TTLS before reaching this
1563 * point, but some do not. Make wpa_supplicant stop phase 2 here, if
1567 tls_connection_resumed(sm->ssl_ctx, data->ssl.conn)) {
1568 wpa_printf(MSG_DEBUG, "EAP-TTLS: Session resumption - "
1570 *out_data = eap_peer_tls_build_ack(identifier, EAP_TYPE_TTLS,
1571 data->ttls_version);
1572 ret->methodState = METHOD_DONE;
1573 ret->decision = DECISION_UNCOND_SUCC;
1574 data->phase2_success = 1;
1578 return eap_ttls_implicit_identity_request(sm, data, ret, identifier,
1583 static int eap_ttls_decrypt(struct eap_sm *sm, struct eap_ttls_data *data,
1584 struct eap_method_ret *ret, u8 identifier,
1585 const struct wpabuf *in_data,
1586 struct wpabuf **out_data)
1588 struct wpabuf *in_decrypted = NULL;
1590 struct ttls_parse_avp parse;
1592 os_memset(&parse, 0, sizeof(parse));
1594 wpa_printf(MSG_DEBUG, "EAP-TTLS: received %lu bytes encrypted data for"
1596 in_data ? (unsigned long) wpabuf_len(in_data) : 0);
1598 if (data->pending_phase2_req) {
1599 wpa_printf(MSG_DEBUG, "EAP-TTLS: Pending Phase 2 request - "
1600 "skip decryption and use old data");
1601 /* Clear TLS reassembly state. */
1602 eap_peer_tls_reset_input(&data->ssl);
1604 in_decrypted = data->pending_phase2_req;
1605 data->pending_phase2_req = NULL;
1606 if (wpabuf_len(in_decrypted) == 0) {
1607 wpabuf_free(in_decrypted);
1608 return eap_ttls_implicit_identity_request(
1609 sm, data, ret, identifier, out_data);
1614 if ((in_data == NULL || wpabuf_len(in_data) == 0) &&
1615 data->phase2_start) {
1616 return eap_ttls_phase2_start(sm, data, ret, identifier,
1620 if (in_data == NULL || wpabuf_len(in_data) == 0) {
1621 /* Received TLS ACK - requesting more fragments */
1622 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
1624 identifier, NULL, out_data);
1627 retval = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
1631 #if EAP_TTLS_VERSION > 0
1632 if (data->ttls_version > 0 &&
1633 (in_decrypted == NULL || wpabuf_len(in_decrypted) == 0) &&
1634 tls_connection_ia_final_phase_finished(sm->ssl_ctx,
1636 eap_ttls_final_phase_finished(sm, data, ret, identifier,
1640 #endif /* EAP_TTLS_VERSION */
1643 data->phase2_start = 0;
1645 if (eap_ttls_parse_avps(in_decrypted, &parse) < 0) {
1650 retval = eap_ttls_process_decrypted(sm, data, ret, identifier,
1651 &parse, in_decrypted, out_data);
1654 wpabuf_free(in_decrypted);
1655 os_free(parse.eapdata);
1658 ret->methodState = METHOD_DONE;
1659 ret->decision = DECISION_FAIL;
1666 static int eap_ttls_process_start(struct eap_sm *sm,
1667 struct eap_ttls_data *data, u8 flags,
1668 struct eap_method_ret *ret)
1670 struct eap_peer_config *config = eap_get_config(sm);
1672 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start (server ver=%d, own ver=%d)",
1673 flags & EAP_PEAP_VERSION_MASK, data->ttls_version);
1674 #if EAP_TTLS_VERSION > 0
1675 if ((flags & EAP_PEAP_VERSION_MASK) < data->ttls_version)
1676 data->ttls_version = flags & EAP_PEAP_VERSION_MASK;
1677 if (data->force_ttls_version >= 0 &&
1678 data->force_ttls_version != data->ttls_version) {
1679 wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to select "
1680 "forced TTLS version %d",
1681 data->force_ttls_version);
1682 ret->methodState = METHOD_DONE;
1683 ret->decision = DECISION_FAIL;
1684 ret->allowNotifications = FALSE;
1687 wpa_printf(MSG_DEBUG, "EAP-TTLS: Using TTLS version %d",
1688 data->ttls_version);
1690 if (data->ttls_version > 0)
1691 data->ssl.tls_ia = 1;
1692 #endif /* EAP_TTLS_VERSION */
1693 if (!data->ssl_initialized &&
1694 eap_peer_tls_ssl_init(sm, &data->ssl, config)) {
1695 wpa_printf(MSG_INFO, "EAP-TTLS: Failed to initialize SSL.");
1698 data->ssl_initialized = 1;
1700 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start");
1706 static int eap_ttls_process_handshake(struct eap_sm *sm,
1707 struct eap_ttls_data *data,
1708 struct eap_method_ret *ret,
1710 const u8 *in_data, size_t in_len,
1711 struct wpabuf **out_data)
1715 res = eap_peer_tls_process_helper(sm, &data->ssl, EAP_TYPE_TTLS,
1716 data->ttls_version, identifier,
1717 in_data, in_len, out_data);
1719 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1720 wpa_printf(MSG_DEBUG, "EAP-TTLS: TLS done, proceed to "
1722 if (data->resuming) {
1723 wpa_printf(MSG_DEBUG, "EAP-TTLS: fast reauth - may "
1725 ret->decision = DECISION_COND_SUCC;
1726 ret->methodState = METHOD_MAY_CONT;
1728 data->phase2_start = 1;
1729 if (data->ttls_version == 0)
1730 eap_ttls_v0_derive_key(sm, data);
1732 if (*out_data == NULL || wpabuf_len(*out_data) == 0) {
1733 if (eap_ttls_decrypt(sm, data, ret, identifier,
1735 wpa_printf(MSG_WARNING, "EAP-TTLS: "
1736 "failed to process early "
1737 "start for Phase 2");
1747 * Application data included in the handshake message.
1749 wpabuf_free(data->pending_phase2_req);
1750 data->pending_phase2_req = *out_data;
1752 wpabuf_set(&msg, in_data, in_len);
1753 res = eap_ttls_decrypt(sm, data, ret, identifier, &msg,
1761 static void eap_ttls_check_auth_status(struct eap_sm *sm,
1762 struct eap_ttls_data *data,
1763 struct eap_method_ret *ret)
1765 if (data->ttls_version == 0 && ret->methodState == METHOD_DONE) {
1766 ret->allowNotifications = FALSE;
1767 if (ret->decision == DECISION_UNCOND_SUCC ||
1768 ret->decision == DECISION_COND_SUCC) {
1769 wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1770 "completed successfully");
1771 data->phase2_success = 1;
1773 if (!data->ready_for_tnc && !data->tnc_started) {
1775 * TNC may be required as the next
1776 * authentication method within the tunnel.
1778 ret->methodState = METHOD_MAY_CONT;
1779 data->ready_for_tnc = 1;
1781 #endif /* EAP_TNC */
1783 } else if (data->ttls_version == 0 &&
1784 ret->methodState == METHOD_MAY_CONT &&
1785 (ret->decision == DECISION_UNCOND_SUCC ||
1786 ret->decision == DECISION_COND_SUCC)) {
1787 wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1788 "completed successfully (MAY_CONT)");
1789 data->phase2_success = 1;
1794 static struct wpabuf * eap_ttls_process(struct eap_sm *sm, void *priv,
1795 struct eap_method_ret *ret,
1796 const struct wpabuf *reqData)
1801 struct wpabuf *resp;
1803 struct eap_ttls_data *data = priv;
1805 pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_TTLS, ret,
1806 reqData, &left, &flags);
1809 id = eap_get_id(reqData);
1811 if (flags & EAP_TLS_FLAGS_START) {
1812 if (eap_ttls_process_start(sm, data, flags, ret) < 0)
1815 /* RFC 5281, Ch. 9.2:
1816 * "This packet MAY contain additional information in the form
1817 * of AVPs, which may provide useful hints to the client"
1818 * For now, ignore any potential extra data.
1821 } else if (!data->ssl_initialized) {
1822 wpa_printf(MSG_DEBUG, "EAP-TTLS: First message did not "
1823 "include Start flag");
1824 ret->methodState = METHOD_DONE;
1825 ret->decision = DECISION_FAIL;
1826 ret->allowNotifications = FALSE;
1831 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1834 wpabuf_set(&msg, pos, left);
1835 res = eap_ttls_decrypt(sm, data, ret, id, &msg, &resp);
1837 res = eap_ttls_process_handshake(sm, data, ret, id,
1841 eap_ttls_check_auth_status(sm, data, ret);
1843 /* FIX: what about res == -1? Could just move all error processing into
1844 * the other functions and get rid of this res==1 case here. */
1847 return eap_peer_tls_build_ack(id, EAP_TYPE_TTLS,
1848 data->ttls_version);
1854 static Boolean eap_ttls_has_reauth_data(struct eap_sm *sm, void *priv)
1856 struct eap_ttls_data *data = priv;
1857 return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1858 data->phase2_success;
1862 static void eap_ttls_deinit_for_reauth(struct eap_sm *sm, void *priv)
1864 struct eap_ttls_data *data = priv;
1865 wpabuf_free(data->pending_phase2_req);
1866 data->pending_phase2_req = NULL;
1868 data->ready_for_tnc = 0;
1869 data->tnc_started = 0;
1870 #endif /* EAP_TNC */
1874 static void * eap_ttls_init_for_reauth(struct eap_sm *sm, void *priv)
1876 struct eap_ttls_data *data = priv;
1877 os_free(data->key_data);
1878 data->key_data = NULL;
1879 if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
1883 if (data->phase2_priv && data->phase2_method &&
1884 data->phase2_method->init_for_reauth)
1885 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
1886 data->phase2_start = 0;
1887 data->phase2_success = 0;
1894 static int eap_ttls_get_status(struct eap_sm *sm, void *priv, char *buf,
1895 size_t buflen, int verbose)
1897 struct eap_ttls_data *data = priv;
1900 len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
1901 ret = os_snprintf(buf + len, buflen - len,
1902 "EAP-TTLSv%d Phase2 method=",
1903 data->ttls_version);
1904 if (ret < 0 || (size_t) ret >= buflen - len)
1907 switch (data->phase2_type) {
1908 case EAP_TTLS_PHASE2_EAP:
1909 ret = os_snprintf(buf + len, buflen - len, "EAP-%s\n",
1910 data->phase2_method ?
1911 data->phase2_method->name : "?");
1913 case EAP_TTLS_PHASE2_MSCHAPV2:
1914 ret = os_snprintf(buf + len, buflen - len, "MSCHAPV2\n");
1916 case EAP_TTLS_PHASE2_MSCHAP:
1917 ret = os_snprintf(buf + len, buflen - len, "MSCHAP\n");
1919 case EAP_TTLS_PHASE2_PAP:
1920 ret = os_snprintf(buf + len, buflen - len, "PAP\n");
1922 case EAP_TTLS_PHASE2_CHAP:
1923 ret = os_snprintf(buf + len, buflen - len, "CHAP\n");
1929 if (ret < 0 || (size_t) ret >= buflen - len)
1937 static Boolean eap_ttls_isKeyAvailable(struct eap_sm *sm, void *priv)
1939 struct eap_ttls_data *data = priv;
1940 return data->key_data != NULL && data->phase2_success;
1944 static u8 * eap_ttls_getKey(struct eap_sm *sm, void *priv, size_t *len)
1946 struct eap_ttls_data *data = priv;
1949 if (data->key_data == NULL || !data->phase2_success)
1952 key = os_malloc(EAP_TLS_KEY_LEN);
1956 *len = EAP_TLS_KEY_LEN;
1957 os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
1963 int eap_peer_ttls_register(void)
1965 struct eap_method *eap;
1968 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1969 EAP_VENDOR_IETF, EAP_TYPE_TTLS, "TTLS");
1973 eap->init = eap_ttls_init;
1974 eap->deinit = eap_ttls_deinit;
1975 eap->process = eap_ttls_process;
1976 eap->isKeyAvailable = eap_ttls_isKeyAvailable;
1977 eap->getKey = eap_ttls_getKey;
1978 eap->get_status = eap_ttls_get_status;
1979 eap->has_reauth_data = eap_ttls_has_reauth_data;
1980 eap->deinit_for_reauth = eap_ttls_deinit_for_reauth;
1981 eap->init_for_reauth = eap_ttls_init_for_reauth;
1983 ret = eap_peer_method_register(eap);
1985 eap_peer_method_free(eap);