2 * EAP peer method: EAP-TTLS (RFC 5281)
3 * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
18 #include "crypto/ms_funcs.h"
19 #include "crypto/sha1.h"
20 #include "crypto/tls.h"
21 #include "eap_common/chap.h"
22 #include "eap_common/eap_ttls.h"
25 #include "eap_tls_common.h"
26 #include "eap_config.h"
29 /* Maximum supported TTLS version
31 * 1 = draft-funk-eap-ttls-v1-00.txt
33 #ifndef EAP_TTLS_VERSION
34 #define EAP_TTLS_VERSION 0 /* TTLSv1 implementation is not yet complete */
35 #endif /* EAP_TTLS_VERSION */
38 #define MSCHAPV2_KEY_LEN 16
39 #define MSCHAPV2_NT_RESPONSE_LEN 24
42 static void eap_ttls_deinit(struct eap_sm
*sm
, void *priv
);
45 struct eap_ttls_data
{
46 struct eap_ssl_data ssl
;
49 int ttls_version
, force_ttls_version
;
51 const struct eap_method
*phase2_method
;
58 EAP_TTLS_PHASE2_MSCHAPV2
,
59 EAP_TTLS_PHASE2_MSCHAP
,
63 struct eap_method_type phase2_eap_type
;
64 struct eap_method_type
*phase2_eap_types
;
65 size_t num_phase2_eap_types
;
67 u8 auth_response
[MSCHAPV2_AUTH_RESPONSE_LEN
];
68 int auth_response_valid
;
69 u8 master_key
[MSCHAPV2_MASTER_KEY_LEN
]; /* MSCHAPv2 master key */
71 int resuming
; /* starting a resumed session */
72 int reauth
; /* reauthentication */
75 struct wpabuf
*pending_phase2_req
;
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 if (mschapv2_derive_response(identity
, identity_len
, password
,
695 password_len
, pwhash
, challenge
,
696 peer_challenge
, pos
, data
->auth_response
,
699 wpa_printf(MSG_ERROR
, "EAP-TTLS/MSCHAPV2: Failed to derive "
703 data
->auth_response_valid
= 1;
705 eap_ttlsv1_permute_inner(sm
, data
);
711 wpabuf_put(msg
, pos
- buf
);
714 if (sm
->workaround
&& data
->ttls_version
== 0) {
715 /* At least FreeRADIUS seems to be terminating
716 * EAP-TTLS/MSHCAPV2 without the expected MS-CHAP-v2 Success
718 wpa_printf(MSG_DEBUG
, "EAP-TTLS/MSCHAPV2: EAP workaround - "
719 "allow success without tunneled response");
720 ret
->methodState
= METHOD_MAY_CONT
;
721 ret
->decision
= DECISION_COND_SUCC
;
728 static int eap_ttls_phase2_request_mschap(struct eap_sm
*sm
,
729 struct eap_ttls_data
*data
,
730 struct eap_method_ret
*ret
,
731 struct wpabuf
**resp
)
734 u8
*buf
, *pos
, *challenge
;
735 const u8
*identity
, *password
;
736 size_t identity_len
, password_len
;
739 wpa_printf(MSG_DEBUG
, "EAP-TTLS: Phase 2 MSCHAP Request");
741 identity
= eap_get_config_identity(sm
, &identity_len
);
742 password
= eap_get_config_password2(sm
, &password_len
, &pwhash
);
743 if (identity
== NULL
|| password
== NULL
)
746 msg
= wpabuf_alloc(identity_len
+ 1000);
748 wpa_printf(MSG_ERROR
,
749 "EAP-TTLS/MSCHAP: Failed to allocate memory");
752 pos
= buf
= wpabuf_mhead(msg
);
755 pos
= eap_ttls_avp_add(buf
, pos
, RADIUS_ATTR_USER_NAME
, 0, 1,
756 identity
, identity_len
);
758 /* MS-CHAP-Challenge */
759 challenge
= eap_ttls_implicit_challenge(
760 sm
, data
, EAP_TTLS_MSCHAP_CHALLENGE_LEN
+ 1);
761 if (challenge
== NULL
) {
763 wpa_printf(MSG_ERROR
, "EAP-TTLS/MSCHAP: Failed to derive "
764 "implicit challenge");
768 pos
= eap_ttls_avp_add(buf
, pos
, RADIUS_ATTR_MS_CHAP_CHALLENGE
,
769 RADIUS_VENDOR_ID_MICROSOFT
, 1,
770 challenge
, EAP_TTLS_MSCHAP_CHALLENGE_LEN
);
772 /* MS-CHAP-Response */
773 pos
= eap_ttls_avp_hdr(pos
, RADIUS_ATTR_MS_CHAP_RESPONSE
,
774 RADIUS_VENDOR_ID_MICROSOFT
, 1,
775 EAP_TTLS_MSCHAP_RESPONSE_LEN
);
776 data
->ident
= challenge
[EAP_TTLS_MSCHAP_CHALLENGE_LEN
];
777 *pos
++ = data
->ident
;
778 *pos
++ = 1; /* Flags: Use NT style passwords */
779 os_memset(pos
, 0, 24); /* LM-Response */
782 challenge_response(challenge
, password
, pos
); /* NT-Response */
783 wpa_hexdump_key(MSG_DEBUG
, "EAP-TTLS: MSCHAP password hash",
786 nt_challenge_response(challenge
, password
, password_len
,
787 pos
); /* NT-Response */
788 wpa_hexdump_ascii_key(MSG_DEBUG
, "EAP-TTLS: MSCHAP password",
789 password
, password_len
);
791 wpa_hexdump(MSG_DEBUG
, "EAP-TTLS: MSCHAP implicit challenge",
792 challenge
, EAP_TTLS_MSCHAP_CHALLENGE_LEN
);
793 wpa_hexdump(MSG_DEBUG
, "EAP-TTLS: MSCHAP response", pos
, 24);
798 wpabuf_put(msg
, pos
- buf
);
801 if (data
->ttls_version
> 0) {
802 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
803 * so do not allow connection to be terminated yet. */
804 ret
->methodState
= METHOD_CONT
;
805 ret
->decision
= DECISION_COND_SUCC
;
807 /* EAP-TTLS/MSCHAP does not provide tunneled success
808 * notification, so assume that Phase2 succeeds. */
809 ret
->methodState
= METHOD_DONE
;
810 ret
->decision
= DECISION_COND_SUCC
;
817 static int eap_ttls_phase2_request_pap(struct eap_sm
*sm
,
818 struct eap_ttls_data
*data
,
819 struct eap_method_ret
*ret
,
820 struct wpabuf
**resp
)
825 const u8
*identity
, *password
;
826 size_t identity_len
, password_len
;
828 wpa_printf(MSG_DEBUG
, "EAP-TTLS: Phase 2 PAP Request");
830 identity
= eap_get_config_identity(sm
, &identity_len
);
831 password
= eap_get_config_password(sm
, &password_len
);
832 if (identity
== NULL
|| password
== NULL
)
835 msg
= wpabuf_alloc(identity_len
+ password_len
+ 100);
837 wpa_printf(MSG_ERROR
,
838 "EAP-TTLS/PAP: Failed to allocate memory");
841 pos
= buf
= wpabuf_mhead(msg
);
844 pos
= eap_ttls_avp_add(buf
, pos
, RADIUS_ATTR_USER_NAME
, 0, 1,
845 identity
, identity_len
);
847 /* User-Password; in RADIUS, this is encrypted, but EAP-TTLS encrypts
848 * the data, so no separate encryption is used in the AVP itself.
849 * However, the password is padded to obfuscate its length. */
850 pad
= password_len
== 0 ? 16 : (16 - (password_len
& 15)) & 15;
851 pos
= eap_ttls_avp_hdr(pos
, RADIUS_ATTR_USER_PASSWORD
, 0, 1,
853 os_memcpy(pos
, password
, password_len
);
855 os_memset(pos
, 0, pad
);
859 wpabuf_put(msg
, pos
- buf
);
862 if (data
->ttls_version
> 0) {
863 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
864 * so do not allow connection to be terminated yet. */
865 ret
->methodState
= METHOD_CONT
;
866 ret
->decision
= DECISION_COND_SUCC
;
868 /* EAP-TTLS/PAP does not provide tunneled success notification,
869 * so assume that Phase2 succeeds. */
870 ret
->methodState
= METHOD_DONE
;
871 ret
->decision
= DECISION_COND_SUCC
;
878 static int eap_ttls_phase2_request_chap(struct eap_sm
*sm
,
879 struct eap_ttls_data
*data
,
880 struct eap_method_ret
*ret
,
881 struct wpabuf
**resp
)
884 u8
*buf
, *pos
, *challenge
;
885 const u8
*identity
, *password
;
886 size_t identity_len
, password_len
;
888 wpa_printf(MSG_DEBUG
, "EAP-TTLS: Phase 2 CHAP Request");
890 identity
= eap_get_config_identity(sm
, &identity_len
);
891 password
= eap_get_config_password(sm
, &password_len
);
892 if (identity
== NULL
|| password
== NULL
)
895 msg
= wpabuf_alloc(identity_len
+ 1000);
897 wpa_printf(MSG_ERROR
,
898 "EAP-TTLS/CHAP: Failed to allocate memory");
901 pos
= buf
= wpabuf_mhead(msg
);
904 pos
= eap_ttls_avp_add(buf
, pos
, RADIUS_ATTR_USER_NAME
, 0, 1,
905 identity
, identity_len
);
908 challenge
= eap_ttls_implicit_challenge(
909 sm
, data
, EAP_TTLS_CHAP_CHALLENGE_LEN
+ 1);
910 if (challenge
== NULL
) {
912 wpa_printf(MSG_ERROR
, "EAP-TTLS/CHAP: Failed to derive "
913 "implicit challenge");
917 pos
= eap_ttls_avp_add(buf
, pos
, RADIUS_ATTR_CHAP_CHALLENGE
, 0, 1,
918 challenge
, EAP_TTLS_CHAP_CHALLENGE_LEN
);
921 pos
= eap_ttls_avp_hdr(pos
, RADIUS_ATTR_CHAP_PASSWORD
, 0, 1,
922 1 + EAP_TTLS_CHAP_PASSWORD_LEN
);
923 data
->ident
= challenge
[EAP_TTLS_CHAP_CHALLENGE_LEN
];
924 *pos
++ = data
->ident
;
926 /* MD5(Ident + Password + Challenge) */
927 chap_md5(data
->ident
, password
, password_len
, challenge
,
928 EAP_TTLS_CHAP_CHALLENGE_LEN
, pos
);
930 wpa_hexdump_ascii(MSG_DEBUG
, "EAP-TTLS: CHAP username",
931 identity
, identity_len
);
932 wpa_hexdump_ascii_key(MSG_DEBUG
, "EAP-TTLS: CHAP password",
933 password
, password_len
);
934 wpa_hexdump(MSG_DEBUG
, "EAP-TTLS: CHAP implicit challenge",
935 challenge
, EAP_TTLS_CHAP_CHALLENGE_LEN
);
936 wpa_hexdump(MSG_DEBUG
, "EAP-TTLS: CHAP password",
937 pos
, EAP_TTLS_CHAP_PASSWORD_LEN
);
938 pos
+= EAP_TTLS_CHAP_PASSWORD_LEN
;
942 wpabuf_put(msg
, pos
- buf
);
945 if (data
->ttls_version
> 0) {
946 /* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
947 * so do not allow connection to be terminated yet. */
948 ret
->methodState
= METHOD_CONT
;
949 ret
->decision
= DECISION_COND_SUCC
;
951 /* EAP-TTLS/CHAP does not provide tunneled success
952 * notification, so assume that Phase2 succeeds. */
953 ret
->methodState
= METHOD_DONE
;
954 ret
->decision
= DECISION_COND_SUCC
;
961 static int eap_ttls_phase2_request(struct eap_sm
*sm
,
962 struct eap_ttls_data
*data
,
963 struct eap_method_ret
*ret
,
965 struct wpabuf
**resp
)
969 enum phase2_types phase2_type
= data
->phase2_type
;
972 if (data
->tnc_started
) {
973 wpa_printf(MSG_DEBUG
, "EAP-TTLS: Processing TNC");
974 phase2_type
= EAP_TTLS_PHASE2_EAP
;
978 if (phase2_type
== EAP_TTLS_PHASE2_MSCHAPV2
||
979 phase2_type
== EAP_TTLS_PHASE2_MSCHAP
||
980 phase2_type
== EAP_TTLS_PHASE2_PAP
||
981 phase2_type
== EAP_TTLS_PHASE2_CHAP
) {
982 if (eap_get_config_identity(sm
, &len
) == NULL
) {
984 "EAP-TTLS: Identity not configured");
985 eap_sm_request_identity(sm
);
986 if (eap_get_config_password(sm
, &len
) == NULL
)
987 eap_sm_request_password(sm
);
991 if (eap_get_config_password(sm
, &len
) == NULL
) {
993 "EAP-TTLS: Password not configured");
994 eap_sm_request_password(sm
);
999 switch (phase2_type
) {
1000 case EAP_TTLS_PHASE2_EAP
:
1001 res
= eap_ttls_phase2_request_eap(sm
, data
, ret
, hdr
, resp
);
1003 case EAP_TTLS_PHASE2_MSCHAPV2
:
1004 res
= eap_ttls_phase2_request_mschapv2(sm
, data
, ret
, resp
);
1006 case EAP_TTLS_PHASE2_MSCHAP
:
1007 res
= eap_ttls_phase2_request_mschap(sm
, data
, ret
, resp
);
1009 case EAP_TTLS_PHASE2_PAP
:
1010 res
= eap_ttls_phase2_request_pap(sm
, data
, ret
, resp
);
1012 case EAP_TTLS_PHASE2_CHAP
:
1013 res
= eap_ttls_phase2_request_chap(sm
, data
, ret
, resp
);
1016 wpa_printf(MSG_ERROR
, "EAP-TTLS: Phase 2 - Unknown");
1022 ret
->methodState
= METHOD_DONE
;
1023 ret
->decision
= DECISION_FAIL
;
1030 #if EAP_TTLS_VERSION > 0
1031 static struct wpabuf
* eap_ttls_build_phase_finished(
1032 struct eap_sm
*sm
, struct eap_ttls_data
*data
, int id
, int final
)
1034 struct wpabuf
*req
, *buf
;
1036 buf
= tls_connection_ia_send_phase_finished(sm
->ssl_ctx
,
1042 req
= eap_msg_alloc(EAP_VENDOR_IETF
, EAP_TYPE_TTLS
,
1043 1 + wpabuf_len(buf
),
1044 EAP_CODE_RESPONSE
, id
);
1050 wpabuf_put_u8(req
, data
->ttls_version
);
1051 wpabuf_put_buf(req
, buf
);
1053 eap_update_len(req
);
1057 #endif /* EAP_TTLS_VERSION */
1060 struct ttls_parse_avp
{
1068 static int eap_ttls_parse_attr_eap(const u8
*dpos
, size_t dlen
,
1069 struct ttls_parse_avp
*parse
)
1071 wpa_printf(MSG_DEBUG
, "EAP-TTLS: AVP - EAP Message");
1072 if (parse
->eapdata
== NULL
) {
1073 parse
->eapdata
= os_malloc(dlen
);
1074 if (parse
->eapdata
== NULL
) {
1075 wpa_printf(MSG_WARNING
, "EAP-TTLS: Failed to allocate "
1076 "memory for Phase 2 EAP data");
1079 os_memcpy(parse
->eapdata
, dpos
, dlen
);
1080 parse
->eap_len
= dlen
;
1082 u8
*neweap
= os_realloc(parse
->eapdata
, parse
->eap_len
+ dlen
);
1083 if (neweap
== NULL
) {
1084 wpa_printf(MSG_WARNING
, "EAP-TTLS: Failed to allocate "
1085 "memory for Phase 2 EAP data");
1088 os_memcpy(neweap
+ parse
->eap_len
, dpos
, dlen
);
1089 parse
->eapdata
= neweap
;
1090 parse
->eap_len
+= dlen
;
1097 static int eap_ttls_parse_avp(u8
*pos
, size_t left
,
1098 struct ttls_parse_avp
*parse
)
1100 struct ttls_avp
*avp
;
1101 u32 avp_code
, avp_length
, vendor_id
= 0;
1102 u8 avp_flags
, *dpos
;
1105 avp
= (struct ttls_avp
*) pos
;
1106 avp_code
= be_to_host32(avp
->avp_code
);
1107 avp_length
= be_to_host32(avp
->avp_length
);
1108 avp_flags
= (avp_length
>> 24) & 0xff;
1109 avp_length
&= 0xffffff;
1110 wpa_printf(MSG_DEBUG
, "EAP-TTLS: AVP: code=%d flags=0x%02x "
1111 "length=%d", (int) avp_code
, avp_flags
,
1114 if (avp_length
> left
) {
1115 wpa_printf(MSG_WARNING
, "EAP-TTLS: AVP overflow "
1116 "(len=%d, left=%lu) - dropped",
1117 (int) avp_length
, (unsigned long) left
);
1121 if (avp_length
< sizeof(*avp
)) {
1122 wpa_printf(MSG_WARNING
, "EAP-TTLS: Invalid AVP length %d",
1127 dpos
= (u8
*) (avp
+ 1);
1128 dlen
= avp_length
- sizeof(*avp
);
1129 if (avp_flags
& AVP_FLAGS_VENDOR
) {
1131 wpa_printf(MSG_WARNING
, "EAP-TTLS: Vendor AVP "
1135 vendor_id
= WPA_GET_BE32(dpos
);
1136 wpa_printf(MSG_DEBUG
, "EAP-TTLS: AVP vendor_id %d",
1142 wpa_hexdump(MSG_DEBUG
, "EAP-TTLS: AVP data", dpos
, dlen
);
1144 if (vendor_id
== 0 && avp_code
== RADIUS_ATTR_EAP_MESSAGE
) {
1145 if (eap_ttls_parse_attr_eap(dpos
, dlen
, parse
) < 0)
1147 } else if (vendor_id
== 0 && avp_code
== RADIUS_ATTR_REPLY_MESSAGE
) {
1148 /* This is an optional message that can be displayed to
1150 wpa_hexdump_ascii(MSG_DEBUG
, "EAP-TTLS: AVP - Reply-Message",
1152 } else if (vendor_id
== RADIUS_VENDOR_ID_MICROSOFT
&&
1153 avp_code
== RADIUS_ATTR_MS_CHAP2_SUCCESS
) {
1154 wpa_hexdump_ascii(MSG_DEBUG
, "EAP-TTLS: MS-CHAP2-Success",
1157 wpa_printf(MSG_WARNING
, "EAP-TTLS: Unexpected "
1158 "MS-CHAP2-Success length "
1159 "(len=%lu, expected 43)",
1160 (unsigned long) dlen
);
1163 parse
->mschapv2
= dpos
;
1164 } else if (vendor_id
== RADIUS_VENDOR_ID_MICROSOFT
&&
1165 avp_code
== RADIUS_ATTR_MS_CHAP_ERROR
) {
1166 wpa_hexdump_ascii(MSG_DEBUG
, "EAP-TTLS: MS-CHAP-Error",
1168 parse
->mschapv2_error
= 1;
1169 } else if (avp_flags
& AVP_FLAGS_MANDATORY
) {
1170 wpa_printf(MSG_WARNING
, "EAP-TTLS: Unsupported mandatory AVP "
1171 "code %d vendor_id %d - dropped",
1172 (int) avp_code
, (int) vendor_id
);
1175 wpa_printf(MSG_DEBUG
, "EAP-TTLS: Ignoring unsupported AVP "
1176 "code %d vendor_id %d",
1177 (int) avp_code
, (int) vendor_id
);
1184 static int eap_ttls_parse_avps(struct wpabuf
*in_decrypted
,
1185 struct ttls_parse_avp
*parse
)
1191 pos
= wpabuf_mhead(in_decrypted
);
1192 left
= wpabuf_len(in_decrypted
);
1193 wpa_hexdump(MSG_DEBUG
, "EAP-TTLS: Decrypted Phase 2 AVPs", pos
, left
);
1194 if (left
< sizeof(struct ttls_avp
)) {
1195 wpa_printf(MSG_WARNING
, "EAP-TTLS: Too short Phase 2 AVP frame"
1196 " len=%lu expected %lu or more - dropped",
1197 (unsigned long) left
,
1198 (unsigned long) sizeof(struct ttls_avp
));
1203 os_memset(parse
, 0, sizeof(*parse
));
1206 avp_length
= eap_ttls_parse_avp(pos
, left
, parse
);
1210 pad
= (4 - (avp_length
& 3)) & 3;
1211 pos
+= avp_length
+ pad
;
1212 if (left
< avp_length
+ pad
)
1215 left
-= avp_length
+ pad
;
1222 static u8
* eap_ttls_fake_identity_request(void)
1224 struct eap_hdr
*hdr
;
1227 wpa_printf(MSG_DEBUG
, "EAP-TTLS: empty data in beginning of "
1228 "Phase 2 - use fake EAP-Request Identity");
1229 buf
= os_malloc(sizeof(*hdr
) + 1);
1231 wpa_printf(MSG_WARNING
, "EAP-TTLS: failed to allocate "
1232 "memory for fake EAP-Identity Request");
1236 hdr
= (struct eap_hdr
*) buf
;
1237 hdr
->code
= EAP_CODE_REQUEST
;
1238 hdr
->identifier
= 0;
1239 hdr
->length
= host_to_be16(sizeof(*hdr
) + 1);
1240 buf
[sizeof(*hdr
)] = EAP_TYPE_IDENTITY
;
1246 static int eap_ttls_encrypt_response(struct eap_sm
*sm
,
1247 struct eap_ttls_data
*data
,
1248 struct wpabuf
*resp
, u8 identifier
,
1249 struct wpabuf
**out_data
)
1254 wpa_hexdump_buf_key(MSG_DEBUG
, "EAP-TTLS: Encrypting Phase 2 data",
1256 if (eap_peer_tls_encrypt(sm
, &data
->ssl
, EAP_TYPE_TTLS
,
1257 data
->ttls_version
, identifier
,
1259 wpa_printf(MSG_INFO
, "EAP-TTLS: Failed to encrypt a Phase 2 "
1269 static int eap_ttls_process_phase2_eap(struct eap_sm
*sm
,
1270 struct eap_ttls_data
*data
,
1271 struct eap_method_ret
*ret
,
1272 struct ttls_parse_avp
*parse
,
1273 struct wpabuf
**resp
)
1275 struct eap_hdr
*hdr
;
1278 if (parse
->eapdata
== NULL
) {
1279 wpa_printf(MSG_WARNING
, "EAP-TTLS: No EAP Message in the "
1280 "packet - dropped");
1284 wpa_hexdump(MSG_DEBUG
, "EAP-TTLS: Phase 2 EAP",
1285 parse
->eapdata
, parse
->eap_len
);
1286 hdr
= (struct eap_hdr
*) parse
->eapdata
;
1288 if (parse
->eap_len
< sizeof(*hdr
)) {
1289 wpa_printf(MSG_WARNING
, "EAP-TTLS: Too short Phase 2 EAP "
1290 "frame (len=%lu, expected %lu or more) - dropped",
1291 (unsigned long) parse
->eap_len
,
1292 (unsigned long) sizeof(*hdr
));
1295 len
= be_to_host16(hdr
->length
);
1296 if (len
> parse
->eap_len
) {
1297 wpa_printf(MSG_INFO
, "EAP-TTLS: Length mismatch in Phase 2 "
1298 "EAP frame (EAP hdr len=%lu, EAP data len in "
1300 (unsigned long) len
,
1301 (unsigned long) parse
->eap_len
);
1304 wpa_printf(MSG_DEBUG
, "EAP-TTLS: received Phase 2: code=%d "
1305 "identifier=%d length=%lu",
1306 hdr
->code
, hdr
->identifier
, (unsigned long) len
);
1307 switch (hdr
->code
) {
1308 case EAP_CODE_REQUEST
:
1309 if (eap_ttls_phase2_request(sm
, data
, ret
, hdr
, resp
)) {
1310 wpa_printf(MSG_INFO
, "EAP-TTLS: Phase2 Request "
1311 "processing failed");
1316 wpa_printf(MSG_INFO
, "EAP-TTLS: Unexpected code=%d in "
1317 "Phase 2 EAP header", hdr
->code
);
1325 static int eap_ttls_process_phase2_mschapv2(struct eap_sm
*sm
,
1326 struct eap_ttls_data
*data
,
1327 struct eap_method_ret
*ret
,
1328 struct ttls_parse_avp
*parse
)
1330 if (parse
->mschapv2_error
) {
1331 wpa_printf(MSG_DEBUG
, "EAP-TTLS/MSCHAPV2: Received "
1332 "MS-CHAP-Error - failed");
1333 ret
->methodState
= METHOD_DONE
;
1334 ret
->decision
= DECISION_FAIL
;
1335 /* Reply with empty data to ACK error */
1339 if (parse
->mschapv2
== NULL
) {
1341 if (data
->phase2_success
&& parse
->eapdata
) {
1343 * Allow EAP-TNC to be started after successfully
1344 * completed MSCHAPV2.
1348 #endif /* EAP_TNC */
1349 wpa_printf(MSG_WARNING
, "EAP-TTLS: no MS-CHAP2-Success AVP "
1350 "received for Phase2 MSCHAPV2");
1353 if (parse
->mschapv2
[0] != data
->ident
) {
1354 wpa_printf(MSG_WARNING
, "EAP-TTLS: Ident mismatch for Phase 2 "
1355 "MSCHAPV2 (received Ident 0x%02x, expected 0x%02x)",
1356 parse
->mschapv2
[0], data
->ident
);
1359 if (!data
->auth_response_valid
||
1360 mschapv2_verify_auth_response(data
->auth_response
,
1361 parse
->mschapv2
+ 1, 42)) {
1362 wpa_printf(MSG_WARNING
, "EAP-TTLS: Invalid authenticator "
1363 "response in Phase 2 MSCHAPV2 success request");
1367 wpa_printf(MSG_INFO
, "EAP-TTLS: Phase 2 MSCHAPV2 "
1368 "authentication succeeded");
1369 if (data
->ttls_version
> 0) {
1371 * EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report
1372 * success, so do not allow connection to be terminated
1375 ret
->methodState
= METHOD_CONT
;
1376 ret
->decision
= DECISION_COND_SUCC
;
1378 ret
->methodState
= METHOD_DONE
;
1379 ret
->decision
= DECISION_UNCOND_SUCC
;
1380 data
->phase2_success
= 1;
1384 * Reply with empty data; authentication server will reply
1385 * with EAP-Success after this.
1392 static int eap_ttls_process_tnc_start(struct eap_sm
*sm
,
1393 struct eap_ttls_data
*data
,
1394 struct eap_method_ret
*ret
,
1395 struct ttls_parse_avp
*parse
,
1396 struct wpabuf
**resp
)
1398 /* TNC uses inner EAP method after non-EAP TTLS phase 2. */
1399 if (parse
->eapdata
== NULL
) {
1400 wpa_printf(MSG_INFO
, "EAP-TTLS: Phase 2 received "
1401 "unexpected tunneled data (no EAP)");
1405 if (!data
->ready_for_tnc
) {
1406 wpa_printf(MSG_INFO
, "EAP-TTLS: Phase 2 received "
1407 "EAP after non-EAP, but not ready for TNC");
1411 wpa_printf(MSG_DEBUG
, "EAP-TTLS: Start TNC after completed "
1413 data
->tnc_started
= 1;
1415 if (eap_ttls_process_phase2_eap(sm
, data
, ret
, parse
, resp
) < 0)
1420 #endif /* EAP_TNC */
1423 static int eap_ttls_process_decrypted(struct eap_sm
*sm
,
1424 struct eap_ttls_data
*data
,
1425 struct eap_method_ret
*ret
,
1427 struct ttls_parse_avp
*parse
,
1428 struct wpabuf
*in_decrypted
,
1429 struct wpabuf
**out_data
)
1431 struct wpabuf
*resp
= NULL
;
1432 struct eap_peer_config
*config
= eap_get_config(sm
);
1434 enum phase2_types phase2_type
= data
->phase2_type
;
1437 if (data
->tnc_started
)
1438 phase2_type
= EAP_TTLS_PHASE2_EAP
;
1439 #endif /* EAP_TNC */
1441 switch (phase2_type
) {
1442 case EAP_TTLS_PHASE2_EAP
:
1443 if (eap_ttls_process_phase2_eap(sm
, data
, ret
, parse
, &resp
) <
1447 case EAP_TTLS_PHASE2_MSCHAPV2
:
1448 res
= eap_ttls_process_phase2_mschapv2(sm
, data
, ret
, parse
);
1450 if (res
== 1 && parse
->eapdata
&& data
->phase2_success
) {
1452 * TNC may be required as the next
1453 * authentication method within the tunnel.
1455 ret
->methodState
= METHOD_MAY_CONT
;
1456 data
->ready_for_tnc
= 1;
1457 if (eap_ttls_process_tnc_start(sm
, data
, ret
, parse
,
1461 #endif /* EAP_TNC */
1463 case EAP_TTLS_PHASE2_MSCHAP
:
1464 case EAP_TTLS_PHASE2_PAP
:
1465 case EAP_TTLS_PHASE2_CHAP
:
1467 if (eap_ttls_process_tnc_start(sm
, data
, ret
, parse
, &resp
) <
1472 /* EAP-TTLS/{MSCHAP,PAP,CHAP} should not send any TLS tunneled
1473 * requests to the supplicant */
1474 wpa_printf(MSG_INFO
, "EAP-TTLS: Phase 2 received unexpected "
1477 #endif /* EAP_TNC */
1481 if (eap_ttls_encrypt_response(sm
, data
, resp
, identifier
,
1484 } else if (config
->pending_req_identity
||
1485 config
->pending_req_password
||
1486 config
->pending_req_otp
||
1487 config
->pending_req_new_password
) {
1488 wpabuf_free(data
->pending_phase2_req
);
1489 data
->pending_phase2_req
= wpabuf_dup(in_decrypted
);
1496 #if EAP_TTLS_VERSION > 0
1497 static void eap_ttls_final_phase_finished(struct eap_sm
*sm
,
1498 struct eap_ttls_data
*data
,
1499 struct eap_method_ret
*ret
,
1501 struct wpabuf
**out_data
)
1503 wpa_printf(MSG_DEBUG
, "EAP-TTLS: FinalPhaseFinished received");
1504 wpa_printf(MSG_INFO
, "EAP-TTLS: TLS/IA authentication succeeded");
1505 ret
->methodState
= METHOD_DONE
;
1506 ret
->decision
= DECISION_UNCOND_SUCC
;
1507 data
->phase2_success
= 1;
1508 *out_data
= eap_ttls_build_phase_finished(sm
, data
, identifier
, 1);
1509 eap_ttls_v1_derive_key(sm
, data
);
1511 #endif /* EAP_TTLS_VERSION */
1514 static int eap_ttls_implicit_identity_request(struct eap_sm
*sm
,
1515 struct eap_ttls_data
*data
,
1516 struct eap_method_ret
*ret
,
1518 struct wpabuf
**out_data
)
1521 struct eap_hdr
*hdr
;
1522 struct wpabuf
*resp
;
1524 hdr
= (struct eap_hdr
*) eap_ttls_fake_identity_request();
1526 ret
->methodState
= METHOD_DONE
;
1527 ret
->decision
= DECISION_FAIL
;
1532 if (eap_ttls_phase2_request(sm
, data
, ret
, hdr
, &resp
)) {
1533 wpa_printf(MSG_INFO
, "EAP-TTLS: Phase2 Request "
1534 "processing failed");
1537 retval
= eap_ttls_encrypt_response(sm
, data
, resp
, identifier
,
1544 ret
->methodState
= METHOD_DONE
;
1545 ret
->decision
= DECISION_FAIL
;
1552 static int eap_ttls_phase2_start(struct eap_sm
*sm
, struct eap_ttls_data
*data
,
1553 struct eap_method_ret
*ret
, u8 identifier
,
1554 struct wpabuf
**out_data
)
1556 data
->phase2_start
= 0;
1559 * EAP-TTLS does not use Phase2 on fast re-auth; this must be done only
1560 * if TLS part was indeed resuming a previous session. Most
1561 * Authentication Servers terminate EAP-TTLS before reaching this
1562 * point, but some do not. Make wpa_supplicant stop phase 2 here, if
1566 tls_connection_resumed(sm
->ssl_ctx
, data
->ssl
.conn
)) {
1567 wpa_printf(MSG_DEBUG
, "EAP-TTLS: Session resumption - "
1569 *out_data
= eap_peer_tls_build_ack(identifier
, EAP_TYPE_TTLS
,
1570 data
->ttls_version
);
1571 ret
->methodState
= METHOD_DONE
;
1572 ret
->decision
= DECISION_UNCOND_SUCC
;
1573 data
->phase2_success
= 1;
1577 return eap_ttls_implicit_identity_request(sm
, data
, ret
, identifier
,
1582 static int eap_ttls_decrypt(struct eap_sm
*sm
, struct eap_ttls_data
*data
,
1583 struct eap_method_ret
*ret
, u8 identifier
,
1584 const struct wpabuf
*in_data
,
1585 struct wpabuf
**out_data
)
1587 struct wpabuf
*in_decrypted
= NULL
;
1589 struct ttls_parse_avp parse
;
1591 os_memset(&parse
, 0, sizeof(parse
));
1593 wpa_printf(MSG_DEBUG
, "EAP-TTLS: received %lu bytes encrypted data for"
1595 in_data
? (unsigned long) wpabuf_len(in_data
) : 0);
1597 if (data
->pending_phase2_req
) {
1598 wpa_printf(MSG_DEBUG
, "EAP-TTLS: Pending Phase 2 request - "
1599 "skip decryption and use old data");
1600 /* Clear TLS reassembly state. */
1601 eap_peer_tls_reset_input(&data
->ssl
);
1603 in_decrypted
= data
->pending_phase2_req
;
1604 data
->pending_phase2_req
= NULL
;
1605 if (wpabuf_len(in_decrypted
) == 0) {
1606 wpabuf_free(in_decrypted
);
1607 return eap_ttls_implicit_identity_request(
1608 sm
, data
, ret
, identifier
, out_data
);
1613 if ((in_data
== NULL
|| wpabuf_len(in_data
) == 0) &&
1614 data
->phase2_start
) {
1615 return eap_ttls_phase2_start(sm
, data
, ret
, identifier
,
1619 if (in_data
== NULL
|| wpabuf_len(in_data
) == 0) {
1620 /* Received TLS ACK - requesting more fragments */
1621 return eap_peer_tls_encrypt(sm
, &data
->ssl
, EAP_TYPE_TTLS
,
1623 identifier
, NULL
, out_data
);
1626 retval
= eap_peer_tls_decrypt(sm
, &data
->ssl
, in_data
, &in_decrypted
);
1630 #if EAP_TTLS_VERSION > 0
1631 if (data
->ttls_version
> 0 &&
1632 (in_decrypted
== NULL
|| wpabuf_len(in_decrypted
) == 0) &&
1633 tls_connection_ia_final_phase_finished(sm
->ssl_ctx
,
1635 eap_ttls_final_phase_finished(sm
, data
, ret
, identifier
,
1639 #endif /* EAP_TTLS_VERSION */
1642 data
->phase2_start
= 0;
1644 if (eap_ttls_parse_avps(in_decrypted
, &parse
) < 0) {
1649 retval
= eap_ttls_process_decrypted(sm
, data
, ret
, identifier
,
1650 &parse
, in_decrypted
, out_data
);
1653 wpabuf_free(in_decrypted
);
1654 os_free(parse
.eapdata
);
1657 ret
->methodState
= METHOD_DONE
;
1658 ret
->decision
= DECISION_FAIL
;
1665 static int eap_ttls_process_start(struct eap_sm
*sm
,
1666 struct eap_ttls_data
*data
, u8 flags
,
1667 struct eap_method_ret
*ret
)
1669 struct eap_peer_config
*config
= eap_get_config(sm
);
1671 wpa_printf(MSG_DEBUG
, "EAP-TTLS: Start (server ver=%d, own ver=%d)",
1672 flags
& EAP_TLS_VERSION_MASK
, data
->ttls_version
);
1673 #if EAP_TTLS_VERSION > 0
1674 if ((flags
& EAP_TLS_VERSION_MASK
) < data
->ttls_version
)
1675 data
->ttls_version
= flags
& EAP_TLS_VERSION_MASK
;
1676 if (data
->force_ttls_version
>= 0 &&
1677 data
->force_ttls_version
!= data
->ttls_version
) {
1678 wpa_printf(MSG_WARNING
, "EAP-TTLS: Failed to select "
1679 "forced TTLS version %d",
1680 data
->force_ttls_version
);
1681 ret
->methodState
= METHOD_DONE
;
1682 ret
->decision
= DECISION_FAIL
;
1683 ret
->allowNotifications
= FALSE
;
1686 wpa_printf(MSG_DEBUG
, "EAP-TTLS: Using TTLS version %d",
1687 data
->ttls_version
);
1689 if (data
->ttls_version
> 0)
1690 data
->ssl
.tls_ia
= 1;
1691 #endif /* EAP_TTLS_VERSION */
1692 if (!data
->ssl_initialized
&&
1693 eap_peer_tls_ssl_init(sm
, &data
->ssl
, config
)) {
1694 wpa_printf(MSG_INFO
, "EAP-TTLS: Failed to initialize SSL.");
1697 data
->ssl_initialized
= 1;
1699 wpa_printf(MSG_DEBUG
, "EAP-TTLS: Start");
1705 static int eap_ttls_process_handshake(struct eap_sm
*sm
,
1706 struct eap_ttls_data
*data
,
1707 struct eap_method_ret
*ret
,
1709 const u8
*in_data
, size_t in_len
,
1710 struct wpabuf
**out_data
)
1714 res
= eap_peer_tls_process_helper(sm
, &data
->ssl
, EAP_TYPE_TTLS
,
1715 data
->ttls_version
, identifier
,
1716 in_data
, in_len
, out_data
);
1718 if (tls_connection_established(sm
->ssl_ctx
, data
->ssl
.conn
)) {
1719 wpa_printf(MSG_DEBUG
, "EAP-TTLS: TLS done, proceed to "
1721 if (data
->resuming
) {
1722 wpa_printf(MSG_DEBUG
, "EAP-TTLS: fast reauth - may "
1724 ret
->decision
= DECISION_COND_SUCC
;
1725 ret
->methodState
= METHOD_MAY_CONT
;
1727 data
->phase2_start
= 1;
1728 if (data
->ttls_version
== 0)
1729 eap_ttls_v0_derive_key(sm
, data
);
1731 if (*out_data
== NULL
|| wpabuf_len(*out_data
) == 0) {
1732 if (eap_ttls_decrypt(sm
, data
, ret
, identifier
,
1734 wpa_printf(MSG_WARNING
, "EAP-TTLS: "
1735 "failed to process early "
1736 "start for Phase 2");
1746 * Application data included in the handshake message.
1748 wpabuf_free(data
->pending_phase2_req
);
1749 data
->pending_phase2_req
= *out_data
;
1751 wpabuf_set(&msg
, in_data
, in_len
);
1752 res
= eap_ttls_decrypt(sm
, data
, ret
, identifier
, &msg
,
1760 static void eap_ttls_check_auth_status(struct eap_sm
*sm
,
1761 struct eap_ttls_data
*data
,
1762 struct eap_method_ret
*ret
)
1764 if (data
->ttls_version
== 0 && ret
->methodState
== METHOD_DONE
) {
1765 ret
->allowNotifications
= FALSE
;
1766 if (ret
->decision
== DECISION_UNCOND_SUCC
||
1767 ret
->decision
== DECISION_COND_SUCC
) {
1768 wpa_printf(MSG_DEBUG
, "EAP-TTLS: Authentication "
1769 "completed successfully");
1770 data
->phase2_success
= 1;
1772 if (!data
->ready_for_tnc
&& !data
->tnc_started
) {
1774 * TNC may be required as the next
1775 * authentication method within the tunnel.
1777 ret
->methodState
= METHOD_MAY_CONT
;
1778 data
->ready_for_tnc
= 1;
1780 #endif /* EAP_TNC */
1782 } else if (data
->ttls_version
== 0 &&
1783 ret
->methodState
== METHOD_MAY_CONT
&&
1784 (ret
->decision
== DECISION_UNCOND_SUCC
||
1785 ret
->decision
== DECISION_COND_SUCC
)) {
1786 wpa_printf(MSG_DEBUG
, "EAP-TTLS: Authentication "
1787 "completed successfully (MAY_CONT)");
1788 data
->phase2_success
= 1;
1793 static struct wpabuf
* eap_ttls_process(struct eap_sm
*sm
, void *priv
,
1794 struct eap_method_ret
*ret
,
1795 const struct wpabuf
*reqData
)
1800 struct wpabuf
*resp
;
1802 struct eap_ttls_data
*data
= priv
;
1804 pos
= eap_peer_tls_process_init(sm
, &data
->ssl
, EAP_TYPE_TTLS
, ret
,
1805 reqData
, &left
, &flags
);
1808 id
= eap_get_id(reqData
);
1810 if (flags
& EAP_TLS_FLAGS_START
) {
1811 if (eap_ttls_process_start(sm
, data
, flags
, ret
) < 0)
1814 /* RFC 5281, Ch. 9.2:
1815 * "This packet MAY contain additional information in the form
1816 * of AVPs, which may provide useful hints to the client"
1817 * For now, ignore any potential extra data.
1820 } else if (!data
->ssl_initialized
) {
1821 wpa_printf(MSG_DEBUG
, "EAP-TTLS: First message did not "
1822 "include Start flag");
1823 ret
->methodState
= METHOD_DONE
;
1824 ret
->decision
= DECISION_FAIL
;
1825 ret
->allowNotifications
= FALSE
;
1830 if (tls_connection_established(sm
->ssl_ctx
, data
->ssl
.conn
) &&
1833 wpabuf_set(&msg
, pos
, left
);
1834 res
= eap_ttls_decrypt(sm
, data
, ret
, id
, &msg
, &resp
);
1836 res
= eap_ttls_process_handshake(sm
, data
, ret
, id
,
1840 eap_ttls_check_auth_status(sm
, data
, ret
);
1842 /* FIX: what about res == -1? Could just move all error processing into
1843 * the other functions and get rid of this res==1 case here. */
1846 return eap_peer_tls_build_ack(id
, EAP_TYPE_TTLS
,
1847 data
->ttls_version
);
1853 static Boolean
eap_ttls_has_reauth_data(struct eap_sm
*sm
, void *priv
)
1855 struct eap_ttls_data
*data
= priv
;
1856 return tls_connection_established(sm
->ssl_ctx
, data
->ssl
.conn
) &&
1857 data
->phase2_success
;
1861 static void eap_ttls_deinit_for_reauth(struct eap_sm
*sm
, void *priv
)
1863 struct eap_ttls_data
*data
= priv
;
1864 wpabuf_free(data
->pending_phase2_req
);
1865 data
->pending_phase2_req
= NULL
;
1867 data
->ready_for_tnc
= 0;
1868 data
->tnc_started
= 0;
1869 #endif /* EAP_TNC */
1873 static void * eap_ttls_init_for_reauth(struct eap_sm
*sm
, void *priv
)
1875 struct eap_ttls_data
*data
= priv
;
1876 os_free(data
->key_data
);
1877 data
->key_data
= NULL
;
1878 if (eap_peer_tls_reauth_init(sm
, &data
->ssl
)) {
1882 if (data
->phase2_priv
&& data
->phase2_method
&&
1883 data
->phase2_method
->init_for_reauth
)
1884 data
->phase2_method
->init_for_reauth(sm
, data
->phase2_priv
);
1885 data
->phase2_start
= 0;
1886 data
->phase2_success
= 0;
1893 static int eap_ttls_get_status(struct eap_sm
*sm
, void *priv
, char *buf
,
1894 size_t buflen
, int verbose
)
1896 struct eap_ttls_data
*data
= priv
;
1899 len
= eap_peer_tls_status(sm
, &data
->ssl
, buf
, buflen
, verbose
);
1900 ret
= os_snprintf(buf
+ len
, buflen
- len
,
1901 "EAP-TTLSv%d Phase2 method=",
1902 data
->ttls_version
);
1903 if (ret
< 0 || (size_t) ret
>= buflen
- len
)
1906 switch (data
->phase2_type
) {
1907 case EAP_TTLS_PHASE2_EAP
:
1908 ret
= os_snprintf(buf
+ len
, buflen
- len
, "EAP-%s\n",
1909 data
->phase2_method
?
1910 data
->phase2_method
->name
: "?");
1912 case EAP_TTLS_PHASE2_MSCHAPV2
:
1913 ret
= os_snprintf(buf
+ len
, buflen
- len
, "MSCHAPV2\n");
1915 case EAP_TTLS_PHASE2_MSCHAP
:
1916 ret
= os_snprintf(buf
+ len
, buflen
- len
, "MSCHAP\n");
1918 case EAP_TTLS_PHASE2_PAP
:
1919 ret
= os_snprintf(buf
+ len
, buflen
- len
, "PAP\n");
1921 case EAP_TTLS_PHASE2_CHAP
:
1922 ret
= os_snprintf(buf
+ len
, buflen
- len
, "CHAP\n");
1928 if (ret
< 0 || (size_t) ret
>= buflen
- len
)
1936 static Boolean
eap_ttls_isKeyAvailable(struct eap_sm
*sm
, void *priv
)
1938 struct eap_ttls_data
*data
= priv
;
1939 return data
->key_data
!= NULL
&& data
->phase2_success
;
1943 static u8
* eap_ttls_getKey(struct eap_sm
*sm
, void *priv
, size_t *len
)
1945 struct eap_ttls_data
*data
= priv
;
1948 if (data
->key_data
== NULL
|| !data
->phase2_success
)
1951 key
= os_malloc(EAP_TLS_KEY_LEN
);
1955 *len
= EAP_TLS_KEY_LEN
;
1956 os_memcpy(key
, data
->key_data
, EAP_TLS_KEY_LEN
);
1962 int eap_peer_ttls_register(void)
1964 struct eap_method
*eap
;
1967 eap
= eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION
,
1968 EAP_VENDOR_IETF
, EAP_TYPE_TTLS
, "TTLS");
1972 eap
->init
= eap_ttls_init
;
1973 eap
->deinit
= eap_ttls_deinit
;
1974 eap
->process
= eap_ttls_process
;
1975 eap
->isKeyAvailable
= eap_ttls_isKeyAvailable
;
1976 eap
->getKey
= eap_ttls_getKey
;
1977 eap
->get_status
= eap_ttls_get_status
;
1978 eap
->has_reauth_data
= eap_ttls_has_reauth_data
;
1979 eap
->deinit_for_reauth
= eap_ttls_deinit_for_reauth
;
1980 eap
->init_for_reauth
= eap_ttls_init_for_reauth
;
1982 ret
= eap_peer_method_register(eap
);
1984 eap_peer_method_free(eap
);