2
* EAP peer method: EAP-TTLS (draft-ietf-pppext-eap-ttls-03.txt)
3
* Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 2 as
7
* published by the Free Software Foundation.
9
* Alternatively, this software may be distributed under the terms of BSD
12
* See README and COPYING for more details.
18
#include "eap_peer/eap_i.h"
19
#include "eap_peer/eap_tls_common.h"
20
#include "config_ssid.h"
25
#include "eap_common/eap_ttls.h"
28
/* Maximum supported TTLS version
29
* 0 = draft-ietf-pppext-eap-ttls-03.txt / draft-funk-eap-ttls-v0-00.txt
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
40
static void eap_ttls_deinit(struct eap_sm *sm, void *priv);
43
struct eap_ttls_data {
44
struct eap_ssl_data ssl;
47
int ttls_version, force_ttls_version;
49
const struct eap_method *phase2_method;
56
EAP_TTLS_PHASE2_MSCHAPV2,
57
EAP_TTLS_PHASE2_MSCHAP,
61
struct eap_method_type phase2_eap_type;
62
struct eap_method_type *phase2_eap_types;
63
size_t num_phase2_eap_types;
66
int auth_response_valid;
68
int resuming; /* starting a resumed session */
69
int reauth; /* reauthentication */
72
u8 *pending_phase2_req;
73
size_t pending_phase2_req_len;
82
static void * eap_ttls_init(struct eap_sm *sm)
84
struct eap_ttls_data *data;
85
struct wpa_ssid *config = eap_get_config(sm);
88
data = os_zalloc(sizeof(*data));
91
data->ttls_version = EAP_TTLS_VERSION;
92
data->force_ttls_version = -1;
94
data->phase2_type = EAP_TTLS_PHASE2_EAP;
96
#if EAP_TTLS_VERSION > 0
97
if (config && config->phase1) {
98
const char *pos = os_strstr(config->phase1, "ttlsver=");
100
data->force_ttls_version = atoi(pos + 8);
101
data->ttls_version = data->force_ttls_version;
102
wpa_printf(MSG_DEBUG, "EAP-TTLS: Forced TTLS version "
103
"%d", data->force_ttls_version);
106
#endif /* EAP_TTLS_VERSION */
108
if (config && config->phase2) {
109
if (os_strstr(config->phase2, "autheap=")) {
111
data->phase2_type = EAP_TTLS_PHASE2_EAP;
112
} else if (os_strstr(config->phase2, "auth=MSCHAPV2")) {
113
selected = "MSCHAPV2";
114
data->phase2_type = EAP_TTLS_PHASE2_MSCHAPV2;
115
} else if (os_strstr(config->phase2, "auth=MSCHAP")) {
117
data->phase2_type = EAP_TTLS_PHASE2_MSCHAP;
118
} else if (os_strstr(config->phase2, "auth=PAP")) {
120
data->phase2_type = EAP_TTLS_PHASE2_PAP;
121
} else if (os_strstr(config->phase2, "auth=CHAP")) {
123
data->phase2_type = EAP_TTLS_PHASE2_CHAP;
126
wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase2 type: %s", selected);
128
if (data->phase2_type == EAP_TTLS_PHASE2_EAP) {
129
if (eap_peer_select_phase2_methods(config, "autheap=",
130
&data->phase2_eap_types,
131
&data->num_phase2_eap_types)
133
eap_ttls_deinit(sm, data);
137
data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
138
data->phase2_eap_type.method = EAP_TYPE_NONE;
141
#if EAP_TTLS_VERSION > 0
142
if (!(tls_capabilities(sm->ssl_ctx) & TLS_CAPABILITY_IA) &&
143
data->ttls_version > 0) {
144
if (data->force_ttls_version > 0) {
145
wpa_printf(MSG_INFO, "EAP-TTLS: Forced TTLSv%d and "
146
"TLS library does not support TLS/IA.",
147
data->force_ttls_version);
148
eap_ttls_deinit(sm, data);
151
data->ttls_version = 0;
153
#endif /* EAP_TTLS_VERSION */
159
static void eap_ttls_deinit(struct eap_sm *sm, void *priv)
161
struct eap_ttls_data *data = priv;
164
if (data->phase2_priv && data->phase2_method)
165
data->phase2_method->deinit(sm, data->phase2_priv);
166
os_free(data->phase2_eap_types);
167
if (data->ssl_initialized)
168
eap_peer_tls_ssl_deinit(sm, &data->ssl);
169
os_free(data->key_data);
170
os_free(data->pending_phase2_req);
175
static u8 * eap_ttls_avp_hdr(u8 *avphdr, u32 avp_code, u32 vendor_id,
176
int mandatory, size_t len)
178
struct ttls_avp_vendor *avp;
182
avp = (struct ttls_avp_vendor *) avphdr;
183
flags = mandatory ? AVP_FLAGS_MANDATORY : 0;
185
flags |= AVP_FLAGS_VENDOR;
186
hdrlen = sizeof(*avp);
187
avp->vendor_id = host_to_be32(vendor_id);
189
hdrlen = sizeof(struct ttls_avp);
192
avp->avp_code = host_to_be32(avp_code);
193
avp->avp_length = host_to_be32((flags << 24) | (hdrlen + len));
195
return avphdr + hdrlen;
199
static u8 * eap_ttls_avp_add(u8 *start, u8 *avphdr, u32 avp_code,
200
u32 vendor_id, int mandatory,
201
u8 *data, size_t len)
204
pos = eap_ttls_avp_hdr(avphdr, avp_code, vendor_id, mandatory, len);
205
os_memcpy(pos, data, len);
212
static int eap_ttls_avp_encapsulate(u8 **resp, size_t *resp_len, u32 avp_code,
217
avp = os_malloc(sizeof(struct ttls_avp) + *resp_len + 4);
225
pos = eap_ttls_avp_hdr(avp, avp_code, 0, mandatory, *resp_len);
226
os_memcpy(pos, *resp, *resp_len);
231
*resp_len = pos - avp;
236
#if EAP_TTLS_VERSION > 0
237
static int eap_ttls_ia_permute_inner_secret(struct eap_sm *sm,
238
struct eap_ttls_data *data,
239
const u8 *key, size_t key_len)
246
buf_len = 2 + key_len;
247
buf = os_malloc(buf_len);
250
WPA_PUT_BE16(buf, key_len);
251
os_memcpy(buf + 2, key, key_len);
257
wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Session keys for TLS/IA inner "
258
"secret permutation", buf, buf_len);
259
ret = tls_connection_ia_permute_inner_secret(sm->ssl_ctx,
266
#endif /* EAP_TTLS_VERSION */
269
static int eap_ttls_v0_derive_key(struct eap_sm *sm,
270
struct eap_ttls_data *data)
272
os_free(data->key_data);
273
data->key_data = eap_peer_tls_derive_key(sm, &data->ssl,
274
"ttls keying material",
276
if (!data->key_data) {
277
wpa_printf(MSG_INFO, "EAP-TTLS: Failed to derive key");
281
wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
282
data->key_data, EAP_TLS_KEY_LEN);
288
#if EAP_TTLS_VERSION > 0
289
static int eap_ttls_v1_derive_key(struct eap_sm *sm,
290
struct eap_ttls_data *data)
292
struct tls_keys keys;
295
os_free(data->key_data);
296
data->key_data = NULL;
298
os_memset(&keys, 0, sizeof(keys));
299
if (tls_connection_get_keys(sm->ssl_ctx, data->ssl.conn, &keys) ||
300
keys.client_random == NULL || keys.server_random == NULL ||
301
keys.inner_secret == NULL) {
302
wpa_printf(MSG_INFO, "EAP-TTLS: Could not get inner secret, "
303
"client random, or server random to derive keying "
308
rnd = os_malloc(keys.client_random_len + keys.server_random_len);
309
data->key_data = os_malloc(EAP_TLS_KEY_LEN);
310
if (rnd == NULL || data->key_data == NULL) {
311
wpa_printf(MSG_INFO, "EAP-TTLS: No memory for key derivation");
313
os_free(data->key_data);
314
data->key_data = NULL;
317
os_memcpy(rnd, keys.client_random, keys.client_random_len);
318
os_memcpy(rnd + keys.client_random_len, keys.server_random,
319
keys.server_random_len);
321
if (tls_prf(keys.inner_secret, keys.inner_secret_len,
322
"ttls v1 keying material", rnd, keys.client_random_len +
323
keys.server_random_len, data->key_data, EAP_TLS_KEY_LEN)) {
324
wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to derive key");
326
os_free(data->key_data);
327
data->key_data = NULL;
331
wpa_hexdump(MSG_DEBUG, "EAP-TTLS: client/server random",
332
rnd, keys.client_random_len + keys.server_random_len);
333
wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: TLS/IA inner secret",
334
keys.inner_secret, keys.inner_secret_len);
338
wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
339
data->key_data, EAP_TLS_KEY_LEN);
343
#endif /* EAP_TTLS_VERSION */
346
static u8 * eap_ttls_implicit_challenge(struct eap_sm *sm,
347
struct eap_ttls_data *data, size_t len)
349
#if EAP_TTLS_VERSION > 0
350
struct tls_keys keys;
352
#endif /* EAP_TTLS_VERSION */
354
if (data->ttls_version == 0) {
355
return eap_peer_tls_derive_key(sm, &data->ssl,
356
"ttls challenge", len);
359
#if EAP_TTLS_VERSION > 0
361
os_memset(&keys, 0, sizeof(keys));
362
if (tls_connection_get_keys(sm->ssl_ctx, data->ssl.conn, &keys) ||
363
keys.client_random == NULL || keys.server_random == NULL ||
364
keys.inner_secret == NULL) {
365
wpa_printf(MSG_INFO, "EAP-TTLS: Could not get inner secret, "
366
"client random, or server random to derive "
367
"implicit challenge");
371
rnd = os_malloc(keys.client_random_len + keys.server_random_len);
372
challenge = os_malloc(len);
373
if (rnd == NULL || challenge == NULL) {
374
wpa_printf(MSG_INFO, "EAP-TTLS: No memory for implicit "
375
"challenge derivation");
380
os_memcpy(rnd, keys.server_random, keys.server_random_len);
381
os_memcpy(rnd + keys.server_random_len, keys.client_random,
382
keys.client_random_len);
384
if (tls_prf(keys.inner_secret, keys.inner_secret_len,
385
"inner application challenge", rnd,
386
keys.client_random_len + keys.server_random_len,
388
wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to derive implicit "
397
wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived implicit challenge",
402
#else /* EAP_TTLS_VERSION */
406
#endif /* EAP_TTLS_VERSION */
410
static void eap_ttlsv1_phase2_eap_finish(struct eap_sm *sm,
411
struct eap_ttls_data *data,
412
struct eap_method_ret *ret)
414
#if EAP_TTLS_VERSION > 0
415
if (data->ttls_version > 0) {
416
const struct eap_method *m = data->phase2_method;
417
void *priv = data->phase2_priv;
419
/* TTLSv1 requires TLS/IA FinalPhaseFinished */
420
if (ret->decision == DECISION_UNCOND_SUCC)
421
ret->decision = DECISION_COND_SUCC;
422
ret->methodState = METHOD_CONT;
424
if (ret->decision == DECISION_COND_SUCC &&
425
m->isKeyAvailable && m->getKey &&
426
m->isKeyAvailable(sm, priv)) {
429
key = m->getKey(sm, priv, &key_len);
431
eap_ttls_ia_permute_inner_secret(
432
sm, data, key, key_len);
437
#endif /* EAP_TTLS_VERSION */
441
static void eap_ttls_phase2_select_eap_method(struct eap_ttls_data *data,
445
for (i = 0; i < data->num_phase2_eap_types; i++) {
446
if (data->phase2_eap_types[i].vendor != EAP_VENDOR_IETF ||
447
data->phase2_eap_types[i].method != method)
450
data->phase2_eap_type.vendor =
451
data->phase2_eap_types[i].vendor;
452
data->phase2_eap_type.method =
453
data->phase2_eap_types[i].method;
454
wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
455
"Phase 2 EAP vendor %d method %d",
456
data->phase2_eap_type.vendor,
457
data->phase2_eap_type.method);
463
static int eap_ttls_phase2_request_eap_method(struct eap_sm *sm,
464
struct eap_ttls_data *data,
465
struct eap_method_ret *ret,
466
struct eap_hdr *hdr, size_t len,
468
u8 **resp, size_t *resp_len)
470
struct eap_method_ret iret;
473
if (data->tnc_started && data->phase2_method &&
474
data->phase2_priv && method == EAP_TYPE_TNC &&
475
data->phase2_eap_type.method == EAP_TYPE_TNC)
478
if (data->ready_for_tnc && !data->tnc_started &&
479
method == EAP_TYPE_TNC) {
480
wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
482
data->tnc_started = 1;
485
if (data->tnc_started) {
486
if (data->phase2_eap_type.vendor != EAP_VENDOR_IETF ||
487
data->phase2_eap_type.method == EAP_TYPE_TNC) {
488
wpa_printf(MSG_DEBUG, "EAP-TTLS: Unexpected EAP "
489
"type %d for TNC", method);
493
data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
494
data->phase2_eap_type.method = method;
495
wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
496
"Phase 2 EAP vendor %d method %d (TNC)",
497
data->phase2_eap_type.vendor,
498
data->phase2_eap_type.method);
500
if (data->phase2_type == EAP_TTLS_PHASE2_EAP &&
501
data->phase2_priv && data->phase2_method) {
502
data->phase2_method->deinit(sm, data->phase2_priv);
503
data->phase2_method = NULL;
504
data->phase2_priv = NULL;
511
if (data->phase2_eap_type.vendor == EAP_VENDOR_IETF &&
512
data->phase2_eap_type.method == EAP_TYPE_NONE)
513
eap_ttls_phase2_select_eap_method(data, method);
515
if (method != data->phase2_eap_type.method || method == EAP_TYPE_NONE)
517
if (eap_peer_tls_phase2_nak(data->phase2_eap_types,
518
data->num_phase2_eap_types,
519
hdr, resp, resp_len))
528
if (data->phase2_priv == NULL) {
529
data->phase2_method = eap_peer_get_eap_method(
530
EAP_VENDOR_IETF, method);
531
if (data->phase2_method) {
533
sm->mschapv2_full_key = 1;
534
data->phase2_priv = data->phase2_method->init(sm);
536
sm->mschapv2_full_key = 0;
539
if (data->phase2_priv == NULL || data->phase2_method == NULL) {
540
wpa_printf(MSG_INFO, "EAP-TTLS: failed to initialize "
541
"Phase 2 EAP method %d", method);
548
os_memset(&iret, 0, sizeof(iret));
549
*resp = data->phase2_method->process(sm, data->phase2_priv,
550
&iret, (u8 *) hdr, len,
552
if ((iret.methodState == METHOD_DONE ||
553
iret.methodState == METHOD_MAY_CONT) &&
554
(iret.decision == DECISION_UNCOND_SUCC ||
555
iret.decision == DECISION_COND_SUCC ||
556
iret.decision == DECISION_FAIL)) {
557
ret->methodState = iret.methodState;
558
ret->decision = iret.decision;
560
eap_ttlsv1_phase2_eap_finish(sm, data, ret);
566
static int eap_ttls_phase2_request_eap(struct eap_sm *sm,
567
struct eap_ttls_data *data,
568
struct eap_method_ret *ret,
570
u8 **resp, size_t *resp_len)
572
size_t len = be_to_host16(hdr->length);
574
struct wpa_ssid *config = eap_get_config(sm);
576
if (len <= sizeof(struct eap_hdr)) {
577
wpa_printf(MSG_INFO, "EAP-TTLS: too short "
578
"Phase 2 request (len=%lu)", (unsigned long) len);
581
pos = (u8 *) (hdr + 1);
582
wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP Request: type=%d", *pos);
584
case EAP_TYPE_IDENTITY:
585
*resp = eap_sm_buildIdentity(sm, hdr->identifier, resp_len, 1);
588
if (eap_ttls_phase2_request_eap_method(sm, data, ret, hdr, len,
589
*pos, resp, resp_len) <
596
(config->pending_req_identity || config->pending_req_password ||
597
config->pending_req_otp)) {
604
wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP encapsulate EAP Response",
606
return eap_ttls_avp_encapsulate(resp, resp_len,
607
RADIUS_ATTR_EAP_MESSAGE, 1);
611
static void eap_ttlsv1_permute_inner(struct eap_sm *sm,
612
struct eap_ttls_data *data,
613
const u8 *nt_response)
615
#if EAP_TTLS_VERSION > 0
616
u8 pw_hash[16], pw_hash_hash[16], master_key[16];
617
u8 session_key[2 * MSCHAPV2_KEY_LEN];
618
struct wpa_ssid *config = eap_get_config(sm);
620
if (data->ttls_version == 0)
623
nt_password_hash(config->password, config->password_len, pw_hash);
624
hash_nt_password_hash(pw_hash, pw_hash_hash);
625
get_master_key(pw_hash_hash, nt_response, master_key);
626
get_asymetric_start_key(master_key, session_key,
627
MSCHAPV2_KEY_LEN, 0, 0);
628
get_asymetric_start_key(master_key, session_key + MSCHAPV2_KEY_LEN,
629
MSCHAPV2_KEY_LEN, 1, 0);
630
eap_ttls_ia_permute_inner_secret(sm, data, session_key,
631
sizeof(session_key));
632
#endif /* EAP_TTLS_VERSION */
636
static int eap_ttls_phase2_request_mschapv2(struct eap_sm *sm,
637
struct eap_ttls_data *data,
638
struct eap_method_ret *ret,
639
u8 **resp, size_t *resp_len)
641
struct wpa_ssid *config = eap_get_config(sm);
642
u8 *buf, *pos, *challenge, *username, *peer_challenge;
643
size_t username_len, i;
645
wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAPV2 Request");
647
/* MSCHAPv2 does not include optional domain name in the
648
* challenge-response calculation, so remove domain prefix
650
username = config->identity;
651
username_len = config->identity_len;
653
for (i = 0; i < username_len; i++) {
654
if (username[i] == '\\') {
655
username_len -= i + 1;
661
pos = buf = os_malloc(config->identity_len + 1000);
663
wpa_printf(MSG_ERROR,
664
"EAP-TTLS/MSCHAPV2: Failed to allocate memory");
669
pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
670
config->identity, config->identity_len);
672
/* MS-CHAP-Challenge */
673
challenge = eap_ttls_implicit_challenge(
674
sm, data, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN * 2 + 1);
675
if (challenge == NULL) {
677
wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
678
"implicit challenge");
681
peer_challenge = challenge + 1 + EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
683
pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
684
RADIUS_VENDOR_ID_MICROSOFT, 1,
685
challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
687
/* MS-CHAP2-Response */
688
pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP2_RESPONSE,
689
RADIUS_VENDOR_ID_MICROSOFT, 1,
690
EAP_TTLS_MSCHAPV2_RESPONSE_LEN);
691
data->ident = challenge[EAP_TTLS_MSCHAPV2_CHALLENGE_LEN];
692
*pos++ = data->ident;
693
*pos++ = 0; /* Flags */
694
os_memcpy(pos, peer_challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
695
pos += EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
696
os_memset(pos, 0, 8); /* Reserved, must be zero */
698
wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAPV2: implicit auth_challenge",
699
challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
700
wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAPV2: peer_challenge",
701
peer_challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
702
wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MSCHAPV2 username",
703
username, username_len);
704
wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: MSCHAPV2 password",
705
config->password, config->password_len);
706
generate_nt_response(challenge, peer_challenge,
707
username, username_len,
708
config->password, config->password_len,
710
wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAPV2 response", pos, 24);
711
generate_authenticator_response(config->password, config->password_len,
712
peer_challenge, challenge,
713
username, username_len,
714
pos, data->auth_response);
715
data->auth_response_valid = 1;
717
eap_ttlsv1_permute_inner(sm, data, pos);
724
*resp_len = pos - buf;
726
if (sm->workaround && data->ttls_version == 0) {
727
/* At least FreeRADIUS seems to be terminating
728
* EAP-TTLS/MSHCAPV2 without the expected MS-CHAP-v2 Success
730
wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: EAP workaround - "
731
"allow success without tunneled response");
732
ret->methodState = METHOD_MAY_CONT;
733
ret->decision = DECISION_COND_SUCC;
740
static int eap_ttls_phase2_request_mschap(struct eap_sm *sm,
741
struct eap_ttls_data *data,
742
struct eap_method_ret *ret,
743
u8 **resp, size_t *resp_len)
745
struct wpa_ssid *config = eap_get_config(sm);
746
u8 *buf, *pos, *challenge;
748
wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAP Request");
750
pos = buf = os_malloc(config->identity_len + 1000);
752
wpa_printf(MSG_ERROR,
753
"EAP-TTLS/MSCHAP: Failed to allocate memory");
758
pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
759
config->identity, config->identity_len);
761
/* MS-CHAP-Challenge */
762
challenge = eap_ttls_implicit_challenge(sm, data, EAP_TLS_KEY_LEN);
763
if (challenge == NULL) {
765
wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAP: Failed to derive "
766
"implicit challenge");
770
pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
771
RADIUS_VENDOR_ID_MICROSOFT, 1,
772
challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
774
/* MS-CHAP-Response */
775
pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP_RESPONSE,
776
RADIUS_VENDOR_ID_MICROSOFT, 1,
777
EAP_TTLS_MSCHAP_RESPONSE_LEN);
778
data->ident = challenge[EAP_TTLS_MSCHAP_CHALLENGE_LEN];
779
*pos++ = data->ident;
780
*pos++ = 1; /* Flags: Use NT style passwords */
781
os_memset(pos, 0, 24); /* LM-Response */
783
nt_challenge_response(challenge,
784
config->password, config->password_len,
785
pos); /* NT-Response */
786
wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password",
787
config->password, config->password_len);
788
wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP implicit challenge",
789
challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
790
wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP response", pos, 24);
796
*resp_len = pos - buf;
798
if (data->ttls_version > 0) {
799
/* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
800
* so do not allow connection to be terminated yet. */
801
ret->methodState = METHOD_CONT;
802
ret->decision = DECISION_COND_SUCC;
804
/* EAP-TTLS/MSCHAP does not provide tunneled success
805
* notification, so assume that Phase2 succeeds. */
806
ret->methodState = METHOD_DONE;
807
ret->decision = DECISION_COND_SUCC;
814
static int eap_ttls_phase2_request_pap(struct eap_sm *sm,
815
struct eap_ttls_data *data,
816
struct eap_method_ret *ret,
817
u8 **resp, size_t *resp_len)
819
struct wpa_ssid *config = eap_get_config(sm);
823
wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 PAP Request");
825
pos = buf = os_malloc(config->identity_len + config->password_len +
828
wpa_printf(MSG_ERROR,
829
"EAP-TTLS/PAP: Failed to allocate memory");
834
pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
835
config->identity, config->identity_len);
837
/* User-Password; in RADIUS, this is encrypted, but EAP-TTLS encrypts
838
* the data, so no separate encryption is used in the AVP itself.
839
* However, the password is padded to obfuscate its length. */
840
pad = (16 - (config->password_len & 15)) & 15;
841
pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_USER_PASSWORD, 0, 1,
842
config->password_len + pad);
843
os_memcpy(pos, config->password, config->password_len);
844
pos += config->password_len;
845
os_memset(pos, 0, pad);
850
*resp_len = pos - buf;
852
if (data->ttls_version > 0) {
853
/* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
854
* so do not allow connection to be terminated yet. */
855
ret->methodState = METHOD_CONT;
856
ret->decision = DECISION_COND_SUCC;
858
/* EAP-TTLS/PAP does not provide tunneled success notification,
859
* so assume that Phase2 succeeds. */
860
ret->methodState = METHOD_DONE;
861
ret->decision = DECISION_COND_SUCC;
868
static int eap_ttls_phase2_request_chap(struct eap_sm *sm,
869
struct eap_ttls_data *data,
870
struct eap_method_ret *ret,
871
u8 **resp, size_t *resp_len)
873
struct wpa_ssid *config = eap_get_config(sm);
874
u8 *buf, *pos, *challenge;
878
wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 CHAP Request");
880
pos = buf = os_malloc(config->identity_len + 1000);
882
wpa_printf(MSG_ERROR,
883
"EAP-TTLS/CHAP: Failed to allocate memory");
888
pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
889
config->identity, config->identity_len);
892
challenge = eap_ttls_implicit_challenge(sm, data, EAP_TLS_KEY_LEN);
893
if (challenge == NULL) {
895
wpa_printf(MSG_ERROR, "EAP-TTLS/CHAP: Failed to derive "
896
"implicit challenge");
900
pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_CHAP_CHALLENGE, 0, 1,
901
challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
904
pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_CHAP_PASSWORD, 0, 1,
905
1 + EAP_TTLS_CHAP_PASSWORD_LEN);
906
data->ident = challenge[EAP_TTLS_CHAP_CHALLENGE_LEN];
907
*pos++ = data->ident;
909
/* MD5(Ident + Password + Challenge) */
910
addr[0] = &data->ident;
912
addr[1] = config->password;
913
len[1] = config->password_len;
915
len[2] = EAP_TTLS_CHAP_CHALLENGE_LEN;
916
md5_vector(3, addr, len, pos);
918
wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: CHAP username",
919
config->identity, config->identity_len);
920
wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: CHAP password",
921
config->password, config->password_len);
922
wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP implicit challenge",
923
challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
924
wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP password",
925
pos, EAP_TTLS_CHAP_PASSWORD_LEN);
926
pos += EAP_TTLS_CHAP_PASSWORD_LEN;
931
*resp_len = pos - buf;
933
if (data->ttls_version > 0) {
934
/* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report success,
935
* so do not allow connection to be terminated yet. */
936
ret->methodState = METHOD_CONT;
937
ret->decision = DECISION_COND_SUCC;
939
/* EAP-TTLS/CHAP does not provide tunneled success
940
* notification, so assume that Phase2 succeeds. */
941
ret->methodState = METHOD_DONE;
942
ret->decision = DECISION_COND_SUCC;
949
static int eap_ttls_phase2_request(struct eap_sm *sm,
950
struct eap_ttls_data *data,
951
struct eap_method_ret *ret,
953
u8 **resp, size_t *resp_len)
958
if (data->phase2_type == EAP_TTLS_PHASE2_MSCHAPV2 ||
959
data->phase2_type == EAP_TTLS_PHASE2_MSCHAP ||
960
data->phase2_type == EAP_TTLS_PHASE2_PAP ||
961
data->phase2_type == EAP_TTLS_PHASE2_CHAP) {
962
if (eap_get_config_identity(sm, &len) == NULL) {
964
"EAP-TTLS: Identity not configured");
965
eap_sm_request_identity(sm);
966
if (eap_get_config_password(sm, &len) == NULL)
967
eap_sm_request_password(sm);
971
if (eap_get_config_password(sm, &len) == NULL) {
973
"EAP-TTLS: Password not configured");
974
eap_sm_request_password(sm);
980
if (data->tnc_started) {
981
wpa_printf(MSG_DEBUG, "EAP-TTLS: Processing TNC");
982
res = eap_ttls_phase2_request_eap(sm, data, ret, hdr,
988
switch (data->phase2_type) {
989
case EAP_TTLS_PHASE2_EAP:
990
res = eap_ttls_phase2_request_eap(sm, data, ret, hdr,
993
case EAP_TTLS_PHASE2_MSCHAPV2:
994
res = eap_ttls_phase2_request_mschapv2(sm, data, ret,
997
case EAP_TTLS_PHASE2_MSCHAP:
998
res = eap_ttls_phase2_request_mschap(sm, data, ret,
1001
case EAP_TTLS_PHASE2_PAP:
1002
res = eap_ttls_phase2_request_pap(sm, data, ret,
1005
case EAP_TTLS_PHASE2_CHAP:
1006
res = eap_ttls_phase2_request_chap(sm, data, ret,
1010
wpa_printf(MSG_ERROR, "EAP-TTLS: Phase 2 - Unknown");
1017
#endif /* EAP_TNC */
1020
ret->methodState = METHOD_DONE;
1021
ret->decision = DECISION_FAIL;
1028
#if EAP_TTLS_VERSION > 0
1029
static u8 * eap_ttls_build_phase_finished(struct eap_sm *sm,
1030
struct eap_ttls_data *data,
1035
struct eap_hdr *req;
1037
const int max_len = 300;
1039
len = sizeof(struct eap_hdr) + 2 + max_len;
1040
req = os_malloc(len);
1044
req->code = EAP_CODE_RESPONSE;
1045
req->identifier = id;
1047
pos = (u8 *) (req + 1);
1048
*pos++ = EAP_TYPE_TTLS;
1049
*pos++ = data->ttls_version;
1051
len = tls_connection_ia_send_phase_finished(sm->ssl_ctx,
1053
final, pos, max_len);
1059
*reqDataLen = sizeof(struct eap_hdr) + 2 + len;
1060
req->length = host_to_be16(*reqDataLen);
1064
#endif /* EAP_TTLS_VERSION */
1067
struct ttls_parse_avp {
1075
static int eap_ttls_parse_attr_eap(const u8 *dpos, size_t dlen,
1076
struct ttls_parse_avp *parse)
1078
wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - EAP Message");
1079
if (parse->eapdata == NULL) {
1080
parse->eapdata = os_malloc(dlen);
1081
if (parse->eapdata == NULL) {
1082
wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
1083
"memory for Phase 2 EAP data");
1086
os_memcpy(parse->eapdata, dpos, dlen);
1087
parse->eap_len = dlen;
1089
u8 *neweap = os_realloc(parse->eapdata, parse->eap_len + dlen);
1090
if (neweap == NULL) {
1091
wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
1092
"memory for Phase 2 EAP data");
1095
os_memcpy(neweap + parse->eap_len, dpos, dlen);
1096
parse->eapdata = neweap;
1097
parse->eap_len += dlen;
1104
static int eap_ttls_parse_avp(u8 *pos, size_t left,
1105
struct ttls_parse_avp *parse)
1107
struct ttls_avp *avp;
1108
u32 avp_code, avp_length, vendor_id = 0;
1109
u8 avp_flags, *dpos;
1112
avp = (struct ttls_avp *) pos;
1113
avp_code = be_to_host32(avp->avp_code);
1114
avp_length = be_to_host32(avp->avp_length);
1115
avp_flags = (avp_length >> 24) & 0xff;
1116
avp_length &= 0xffffff;
1117
wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP: code=%d flags=0x%02x "
1118
"length=%d", (int) avp_code, avp_flags,
1121
if (avp_length > left) {
1122
wpa_printf(MSG_WARNING, "EAP-TTLS: AVP overflow "
1123
"(len=%d, left=%lu) - dropped",
1124
(int) avp_length, (unsigned long) left);
1128
if (avp_length < sizeof(*avp)) {
1129
wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid AVP length %d",
1134
dpos = (u8 *) (avp + 1);
1135
dlen = avp_length - sizeof(*avp);
1136
if (avp_flags & AVP_FLAGS_VENDOR) {
1138
wpa_printf(MSG_WARNING, "EAP-TTLS: Vendor AVP "
1142
vendor_id = WPA_GET_BE32(dpos);
1143
wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP vendor_id %d",
1149
wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP data", dpos, dlen);
1151
if (vendor_id == 0 && avp_code == RADIUS_ATTR_EAP_MESSAGE) {
1152
if (eap_ttls_parse_attr_eap(dpos, dlen, parse) < 0)
1154
} else if (vendor_id == 0 && avp_code == RADIUS_ATTR_REPLY_MESSAGE) {
1155
/* This is an optional message that can be displayed to
1157
wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: AVP - Reply-Message",
1159
} else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
1160
avp_code == RADIUS_ATTR_MS_CHAP2_SUCCESS) {
1161
wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP2-Success",
1164
wpa_printf(MSG_WARNING, "EAP-TTLS: Unexpected "
1165
"MS-CHAP2-Success length "
1166
"(len=%lu, expected 43)",
1167
(unsigned long) dlen);
1170
parse->mschapv2 = dpos;
1171
} else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
1172
avp_code == RADIUS_ATTR_MS_CHAP_ERROR) {
1173
wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP-Error",
1175
parse->mschapv2_error = 1;
1176
} else if (avp_flags & AVP_FLAGS_MANDATORY) {
1177
wpa_printf(MSG_WARNING, "EAP-TTLS: Unsupported mandatory AVP "
1178
"code %d vendor_id %d - dropped",
1179
(int) avp_code, (int) vendor_id);
1182
wpa_printf(MSG_DEBUG, "EAP-TTLS: Ignoring unsupported AVP "
1183
"code %d vendor_id %d",
1184
(int) avp_code, (int) vendor_id);
1191
static int eap_ttls_parse_avps(u8 *in_decrypted, size_t len_decrypted,
1192
struct ttls_parse_avp *parse)
1198
wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Decrypted Phase 2 AVPs",
1199
in_decrypted, len_decrypted);
1200
if (len_decrypted < sizeof(struct ttls_avp)) {
1201
wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 AVP frame"
1202
" len=%lu expected %lu or more - dropped",
1203
(unsigned long) len_decrypted,
1204
(unsigned long) sizeof(struct ttls_avp));
1210
left = len_decrypted;
1212
os_memset(parse, 0, sizeof(*parse));
1215
avp_length = eap_ttls_parse_avp(pos, left, parse);
1219
pad = (4 - (avp_length & 3)) & 3;
1220
pos += avp_length + pad;
1221
if (left < avp_length + pad)
1224
left -= avp_length + pad;
1231
static u8 * eap_ttls_fake_identity_request(void)
1233
struct eap_hdr *hdr;
1236
wpa_printf(MSG_DEBUG, "EAP-TTLS: empty data in beginning of "
1237
"Phase 2 - use fake EAP-Request Identity");
1238
buf = os_malloc(sizeof(*hdr) + 1);
1240
wpa_printf(MSG_WARNING, "EAP-TTLS: failed to allocate "
1241
"memory for fake EAP-Identity Request");
1245
hdr = (struct eap_hdr *) buf;
1246
hdr->code = EAP_CODE_REQUEST;
1247
hdr->identifier = 0;
1248
hdr->length = host_to_be16(sizeof(*hdr) + 1);
1249
buf[sizeof(*hdr)] = EAP_TYPE_IDENTITY;
1255
static int eap_ttls_encrypt_response(struct eap_sm *sm,
1256
struct eap_ttls_data *data,
1257
u8 *resp, size_t resp_len,
1259
u8 **out_data, size_t *out_len)
1264
wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Encrypting Phase 2 data",
1266
if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
1267
data->ttls_version, identifier,
1268
resp, resp_len, out_data, out_len)) {
1269
wpa_printf(MSG_INFO, "EAP-TTLS: Failed to encrypt a Phase 2 "
1279
static int eap_ttls_process_phase2_eap(struct eap_sm *sm,
1280
struct eap_ttls_data *data,
1281
struct eap_method_ret *ret,
1282
struct ttls_parse_avp *parse,
1283
u8 **resp, size_t *resp_len)
1285
struct eap_hdr *hdr;
1288
if (parse->eapdata == NULL) {
1289
wpa_printf(MSG_WARNING, "EAP-TTLS: No EAP Message in the "
1290
"packet - dropped");
1294
wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP",
1295
parse->eapdata, parse->eap_len);
1296
hdr = (struct eap_hdr *) parse->eapdata;
1298
if (parse->eap_len < sizeof(*hdr)) {
1299
wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 EAP "
1300
"frame (len=%lu, expected %lu or more) - dropped",
1301
(unsigned long) parse->eap_len,
1302
(unsigned long) sizeof(*hdr));
1305
len = be_to_host16(hdr->length);
1306
if (len > parse->eap_len) {
1307
wpa_printf(MSG_INFO, "EAP-TTLS: Length mismatch in Phase 2 "
1308
"EAP frame (EAP hdr len=%lu, EAP data len in "
1310
(unsigned long) len,
1311
(unsigned long) parse->eap_len);
1314
wpa_printf(MSG_DEBUG, "EAP-TTLS: received Phase 2: code=%d "
1315
"identifier=%d length=%lu",
1316
hdr->code, hdr->identifier, (unsigned long) len);
1317
switch (hdr->code) {
1318
case EAP_CODE_REQUEST:
1319
if (eap_ttls_phase2_request(sm, data, ret, hdr, resp,
1321
wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
1322
"processing failed");
1327
wpa_printf(MSG_INFO, "EAP-TTLS: Unexpected code=%d in "
1328
"Phase 2 EAP header", hdr->code);
1336
static int eap_ttls_process_phase2_mschapv2(struct eap_sm *sm,
1337
struct eap_ttls_data *data,
1338
struct eap_method_ret *ret,
1339
struct ttls_parse_avp *parse)
1341
u8 recv_response[20];
1343
if (parse->mschapv2_error) {
1344
wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Received "
1345
"MS-CHAP-Error - failed");
1346
ret->methodState = METHOD_DONE;
1347
ret->decision = DECISION_FAIL;
1348
/* Reply with empty data to ACK error */
1352
if (parse->mschapv2 == NULL) {
1353
wpa_printf(MSG_WARNING, "EAP-TTLS: no MS-CHAP2-Success AVP "
1354
"received for Phase2 MSCHAPV2");
1357
if (parse->mschapv2[0] != data->ident) {
1358
wpa_printf(MSG_WARNING, "EAP-TTLS: Ident mismatch for Phase 2 "
1359
"MSCHAPV2 (received Ident 0x%02x, expected 0x%02x)",
1360
parse->mschapv2[0], data->ident);
1363
if (!data->auth_response_valid ||
1364
parse->mschapv2[1] != 'S' || parse->mschapv2[2] != '=' ||
1365
hexstr2bin((char *) (parse->mschapv2 + 3), recv_response, 20) ||
1366
os_memcmp(data->auth_response, recv_response, 20) != 0) {
1367
wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid authenticator "
1368
"response in Phase 2 MSCHAPV2 success request");
1372
wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 MSCHAPV2 "
1373
"authentication succeeded");
1374
if (data->ttls_version > 0) {
1376
* EAP-TTLSv1 uses TLS/IA FinalPhaseFinished to report
1377
* success, so do not allow connection to be terminated
1380
ret->methodState = METHOD_CONT;
1381
ret->decision = DECISION_COND_SUCC;
1383
ret->methodState = METHOD_DONE;
1384
ret->decision = DECISION_UNCOND_SUCC;
1385
data->phase2_success = 1;
1389
* Reply with empty data; authentication server will reply
1390
* with EAP-Success after this.
1397
static int eap_ttls_process_tnc_start(struct eap_sm *sm,
1398
struct eap_ttls_data *data,
1399
struct eap_method_ret *ret,
1400
struct ttls_parse_avp *parse,
1401
u8 **resp, size_t *resp_len)
1403
/* TNC uses inner EAP method after non-EAP TTLS phase 2. */
1404
if (parse->eapdata == NULL) {
1405
wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
1406
"unexpected tunneled data (no EAP)");
1410
if (!data->ready_for_tnc) {
1411
wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
1412
"EAP after non-EAP, but not ready for TNC");
1416
wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
1418
data->tnc_started = 1;
1420
if (eap_ttls_process_phase2_eap(sm, data, ret, parse, resp,
1426
#endif /* EAP_TNC */
1429
static int eap_ttls_process_decrypted(struct eap_sm *sm,
1430
struct eap_ttls_data *data,
1431
struct eap_method_ret *ret,
1433
struct ttls_parse_avp *parse,
1434
u8 *in_decrypted, size_t len_decrypted,
1435
u8 **out_data, size_t *out_len)
1439
struct wpa_ssid *config = eap_get_config(sm);
1442
switch (data->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 &&
1452
ret->methodState == METHOD_DONE &&
1453
ret->decision == DECISION_UNCOND_SUCC) {
1455
* TNC may be required as the next
1456
* authentication method within the tunnel.
1458
ret->methodState = METHOD_MAY_CONT;
1459
data->ready_for_tnc = 1;
1460
if (eap_ttls_process_tnc_start(sm, data, ret, parse,
1461
&resp, &resp_len) == 0)
1464
#endif /* EAP_TNC */
1466
case EAP_TTLS_PHASE2_MSCHAP:
1467
case EAP_TTLS_PHASE2_PAP:
1468
case EAP_TTLS_PHASE2_CHAP:
1470
if (eap_ttls_process_tnc_start(sm, data, ret, parse,
1471
&resp, &resp_len) < 0)
1475
/* EAP-TTLS/{MSCHAP,PAP,CHAP} should not send any TLS tunneled
1476
* requests to the supplicant */
1477
wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received unexpected "
1480
#endif /* EAP_TNC */
1484
if (eap_ttls_encrypt_response(sm, data, resp, resp_len,
1486
out_data, out_len) < 0)
1488
} else if (config->pending_req_identity ||
1489
config->pending_req_password ||
1490
config->pending_req_otp ||
1491
config->pending_req_new_password) {
1492
os_free(data->pending_phase2_req);
1493
data->pending_phase2_req = os_malloc(len_decrypted);
1494
if (data->pending_phase2_req) {
1495
os_memcpy(data->pending_phase2_req, in_decrypted,
1497
data->pending_phase2_req_len = len_decrypted;
1505
#if EAP_TTLS_VERSION > 0
1506
static void eap_ttls_final_phase_finished(struct eap_sm *sm,
1507
struct eap_ttls_data *data,
1508
struct eap_method_ret *ret,
1510
u8 **out_data, size_t *out_len)
1512
wpa_printf(MSG_DEBUG, "EAP-TTLS: FinalPhaseFinished received");
1513
wpa_printf(MSG_INFO, "EAP-TTLS: TLS/IA authentication succeeded");
1514
ret->methodState = METHOD_DONE;
1515
ret->decision = DECISION_UNCOND_SUCC;
1516
data->phase2_success = 1;
1517
*out_data = eap_ttls_build_phase_finished(sm, data, identifier, 1,
1519
eap_ttls_v1_derive_key(sm, data);
1521
#endif /* EAP_TTLS_VERSION */
1524
static int eap_ttls_implicit_identity_request(struct eap_sm *sm,
1525
struct eap_ttls_data *data,
1526
struct eap_method_ret *ret,
1528
u8 **out_data, size_t *out_len)
1531
struct eap_hdr *hdr;
1535
hdr = (struct eap_hdr *) eap_ttls_fake_identity_request();
1537
ret->methodState = METHOD_DONE;
1538
ret->decision = DECISION_FAIL;
1543
if (eap_ttls_phase2_request(sm, data, ret, hdr, &resp, &resp_len)) {
1544
wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
1545
"processing failed");
1548
retval = eap_ttls_encrypt_response(sm, data, resp, resp_len,
1549
identifier, out_data,
1556
ret->methodState = METHOD_DONE;
1557
ret->decision = DECISION_FAIL;
1564
static int eap_ttls_phase2_start(struct eap_sm *sm, struct eap_ttls_data *data,
1565
struct eap_method_ret *ret, u8 identifier,
1566
u8 **out_data, size_t *out_len)
1568
data->phase2_start = 0;
1571
* EAP-TTLS does not use Phase2 on fast re-auth; this must be done only
1572
* if TLS part was indeed resuming a previous session. Most
1573
* Authentication Servers terminate EAP-TTLS before reaching this
1574
* point, but some do not. Make wpa_supplicant stop phase 2 here, if
1578
tls_connection_resumed(sm->ssl_ctx, data->ssl.conn)) {
1579
wpa_printf(MSG_DEBUG, "EAP-TTLS: Session resumption - "
1581
*out_data = eap_peer_tls_build_ack(&data->ssl, out_len,
1582
identifier, EAP_TYPE_TTLS,
1583
data->ttls_version);
1584
ret->methodState = METHOD_DONE;
1585
ret->decision = DECISION_UNCOND_SUCC;
1586
data->phase2_success = 1;
1590
return eap_ttls_implicit_identity_request(sm, data, ret, identifier,
1595
static int eap_ttls_decrypt(struct eap_sm *sm, struct eap_ttls_data *data,
1596
struct eap_method_ret *ret,
1598
const u8 *in_data, size_t in_len,
1599
u8 **out_data, size_t *out_len)
1601
u8 *in_decrypted = NULL;
1603
size_t len_decrypted = 0;
1604
struct ttls_parse_avp parse;
1606
os_memset(&parse, 0, sizeof(parse));
1608
wpa_printf(MSG_DEBUG, "EAP-TTLS: received %lu bytes encrypted data for"
1609
" Phase 2", (unsigned long) in_len);
1611
if (data->pending_phase2_req) {
1612
wpa_printf(MSG_DEBUG, "EAP-TTLS: Pending Phase 2 request - "
1613
"skip decryption and use old data");
1614
/* Clear TLS reassembly state. */
1615
eap_peer_tls_reset_input(&data->ssl);
1617
in_decrypted = data->pending_phase2_req;
1618
data->pending_phase2_req = NULL;
1619
len_decrypted = data->pending_phase2_req_len;
1620
if (data->pending_phase2_req_len == 0) {
1621
os_free(in_decrypted);
1622
return eap_ttls_implicit_identity_request(
1623
sm, data, ret, identifier, out_data,
1629
if (in_len == 0 && data->phase2_start) {
1630
return eap_ttls_phase2_start(sm, data, ret, identifier,
1635
/* Received TLS ACK - requesting more fragments */
1636
return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
1638
identifier, NULL, 0,
1642
retval = eap_peer_tls_decrypt(sm, &data->ssl, in_data, in_len,
1643
&in_decrypted, &len_decrypted);
1647
#if EAP_TTLS_VERSION > 0
1648
if (data->ttls_version > 0 && len_decrypted == 0 &&
1649
tls_connection_ia_final_phase_finished(sm->ssl_ctx,
1651
eap_ttls_final_phase_finished(sm, data, ret, identifier,
1655
#endif /* EAP_TTLS_VERSION */
1658
data->phase2_start = 0;
1660
if (eap_ttls_parse_avps(in_decrypted, len_decrypted, &parse) < 0) {
1665
retval = eap_ttls_process_decrypted(sm, data, ret, identifier,
1667
in_decrypted, len_decrypted,
1671
os_free(in_decrypted);
1672
os_free(parse.eapdata);
1675
ret->methodState = METHOD_DONE;
1676
ret->decision = DECISION_FAIL;
1683
static int eap_ttls_process_start(struct eap_sm *sm,
1684
struct eap_ttls_data *data, u8 flags,
1685
struct eap_method_ret *ret)
1687
struct wpa_ssid *config = eap_get_config(sm);
1689
wpa_printf(MSG_DEBUG, "EAP-TTLS: Start (server ver=%d, own ver=%d)",
1690
flags & EAP_PEAP_VERSION_MASK, data->ttls_version);
1691
#if EAP_TTLS_VERSION > 0
1692
if ((flags & EAP_PEAP_VERSION_MASK) < data->ttls_version)
1693
data->ttls_version = flags & EAP_PEAP_VERSION_MASK;
1694
if (data->force_ttls_version >= 0 &&
1695
data->force_ttls_version != data->ttls_version) {
1696
wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to select "
1697
"forced TTLS version %d",
1698
data->force_ttls_version);
1699
ret->methodState = METHOD_DONE;
1700
ret->decision = DECISION_FAIL;
1701
ret->allowNotifications = FALSE;
1704
wpa_printf(MSG_DEBUG, "EAP-TTLS: Using TTLS version %d",
1705
data->ttls_version);
1707
if (data->ttls_version > 0)
1708
data->ssl.tls_ia = 1;
1709
#endif /* EAP_TTLS_VERSION */
1710
if (!data->ssl_initialized &&
1711
eap_peer_tls_ssl_init(sm, &data->ssl, config)) {
1712
wpa_printf(MSG_INFO, "EAP-TTLS: Failed to initialize SSL.");
1715
data->ssl_initialized = 1;
1717
wpa_printf(MSG_DEBUG, "EAP-TTLS: Start");
1723
static int eap_ttls_process_handshake(struct eap_sm *sm,
1724
struct eap_ttls_data *data,
1725
struct eap_method_ret *ret,
1727
const u8 *in_data, size_t in_len,
1728
u8 **out_data, size_t *out_len)
1732
res = eap_peer_tls_process_helper(sm, &data->ssl, EAP_TYPE_TTLS,
1733
data->ttls_version, identifier,
1734
in_data, in_len, out_data, out_len);
1736
if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1737
wpa_printf(MSG_DEBUG, "EAP-TTLS: TLS done, proceed to "
1739
if (data->resuming) {
1740
wpa_printf(MSG_DEBUG, "EAP-TTLS: fast reauth - may "
1742
ret->decision = DECISION_COND_SUCC;
1743
ret->methodState = METHOD_MAY_CONT;
1745
data->phase2_start = 1;
1746
if (data->ttls_version == 0)
1747
eap_ttls_v0_derive_key(sm, data);
1749
if (*out_len == 0) {
1750
if (eap_ttls_decrypt(sm, data, ret, identifier,
1751
NULL, 0, out_data, out_len)) {
1752
wpa_printf(MSG_WARNING, "EAP-TTLS: "
1753
"failed to process early "
1754
"start for Phase 2");
1763
* Application data included in the handshake message.
1765
os_free(data->pending_phase2_req);
1766
data->pending_phase2_req = *out_data;
1767
data->pending_phase2_req_len = *out_len;
1770
res = eap_ttls_decrypt(sm, data, ret, identifier,
1771
in_data, in_len, out_data, out_len);
1778
static void eap_ttls_check_auth_status(struct eap_sm *sm,
1779
struct eap_ttls_data *data,
1780
struct eap_method_ret *ret)
1782
if (data->ttls_version == 0 && ret->methodState == METHOD_DONE) {
1783
ret->allowNotifications = FALSE;
1784
if (ret->decision == DECISION_UNCOND_SUCC ||
1785
ret->decision == DECISION_COND_SUCC) {
1786
wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1787
"completed successfully");
1788
data->phase2_success = 1;
1790
if (!data->ready_for_tnc && !data->tnc_started) {
1792
* TNC may be required as the next
1793
* authentication method within the tunnel.
1795
ret->methodState = METHOD_MAY_CONT;
1796
data->ready_for_tnc = 1;
1798
#endif /* EAP_TNC */
1800
} else if (data->ttls_version == 0 && sm->workaround &&
1801
ret->methodState == METHOD_MAY_CONT &&
1802
(ret->decision == DECISION_UNCOND_SUCC ||
1803
ret->decision == DECISION_COND_SUCC)) {
1804
wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1805
"completed successfully (EAP workaround)");
1806
data->phase2_success = 1;
1811
static u8 * eap_ttls_process(struct eap_sm *sm, void *priv,
1812
struct eap_method_ret *ret,
1813
const u8 *reqData, size_t reqDataLen,
1814
size_t *respDataLen)
1816
const struct eap_hdr *req;
1821
struct eap_ttls_data *data = priv;
1823
pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_TTLS, ret,
1824
reqData, reqDataLen, &left, &flags);
1827
req = (const struct eap_hdr *) reqData;
1829
if (flags & EAP_TLS_FLAGS_START) {
1830
if (eap_ttls_process_start(sm, data, flags, ret) < 0)
1833
/* draft-ietf-pppext-eap-ttls-03.txt, Ch. 8.1:
1834
* EAP-TTLS Start packet may, in a future specification, be
1835
* allowed to contain data. Client based on this draft version
1836
* must ignore such data but must not reject the Start packet.
1839
} else if (!data->ssl_initialized) {
1840
wpa_printf(MSG_DEBUG, "EAP-TTLS: First message did not "
1841
"include Start flag");
1842
ret->methodState = METHOD_DONE;
1843
ret->decision = DECISION_FAIL;
1844
ret->allowNotifications = FALSE;
1849
if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1851
res = eap_ttls_decrypt(sm, data, ret, req->identifier,
1852
pos, left, &resp, respDataLen);
1854
res = eap_ttls_process_handshake(sm, data, ret,
1855
req->identifier, pos, left,
1856
&resp, respDataLen);
1859
eap_ttls_check_auth_status(sm, data, ret);
1861
/* FIX: what about res == -1? Could just move all error processing into
1862
* the other functions and get rid of this res==1 case here. */
1865
return eap_peer_tls_build_ack(&data->ssl, respDataLen,
1866
req->identifier, EAP_TYPE_TTLS,
1867
data->ttls_version);
1873
static Boolean eap_ttls_has_reauth_data(struct eap_sm *sm, void *priv)
1875
struct eap_ttls_data *data = priv;
1876
return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1877
data->phase2_success;
1881
static void eap_ttls_deinit_for_reauth(struct eap_sm *sm, void *priv)
1883
struct eap_ttls_data *data = priv;
1884
os_free(data->pending_phase2_req);
1885
data->pending_phase2_req = NULL;
1887
data->ready_for_tnc = 0;
1888
data->tnc_started = 0;
1889
#endif /* EAP_TNC */
1893
static void * eap_ttls_init_for_reauth(struct eap_sm *sm, void *priv)
1895
struct eap_ttls_data *data = priv;
1896
os_free(data->key_data);
1897
data->key_data = NULL;
1898
if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
1902
if (data->phase2_priv && data->phase2_method &&
1903
data->phase2_method->init_for_reauth)
1904
data->phase2_method->init_for_reauth(sm, data->phase2_priv);
1905
data->phase2_start = 0;
1906
data->phase2_success = 0;
1913
static int eap_ttls_get_status(struct eap_sm *sm, void *priv, char *buf,
1914
size_t buflen, int verbose)
1916
struct eap_ttls_data *data = priv;
1919
len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
1920
ret = os_snprintf(buf + len, buflen - len,
1921
"EAP-TTLSv%d Phase2 method=",
1922
data->ttls_version);
1923
if (ret < 0 || (size_t) ret >= buflen - len)
1926
switch (data->phase2_type) {
1927
case EAP_TTLS_PHASE2_EAP:
1928
ret = os_snprintf(buf + len, buflen - len, "EAP-%s\n",
1929
data->phase2_method ?
1930
data->phase2_method->name : "?");
1932
case EAP_TTLS_PHASE2_MSCHAPV2:
1933
ret = os_snprintf(buf + len, buflen - len, "MSCHAPV2\n");
1935
case EAP_TTLS_PHASE2_MSCHAP:
1936
ret = os_snprintf(buf + len, buflen - len, "MSCHAP\n");
1938
case EAP_TTLS_PHASE2_PAP:
1939
ret = os_snprintf(buf + len, buflen - len, "PAP\n");
1941
case EAP_TTLS_PHASE2_CHAP:
1942
ret = os_snprintf(buf + len, buflen - len, "CHAP\n");
1948
if (ret < 0 || (size_t) ret >= buflen - len)
1956
static Boolean eap_ttls_isKeyAvailable(struct eap_sm *sm, void *priv)
1958
struct eap_ttls_data *data = priv;
1959
return data->key_data != NULL && data->phase2_success;
1963
static u8 * eap_ttls_getKey(struct eap_sm *sm, void *priv, size_t *len)
1965
struct eap_ttls_data *data = priv;
1968
if (data->key_data == NULL || !data->phase2_success)
1971
key = os_malloc(EAP_TLS_KEY_LEN);
1975
*len = EAP_TLS_KEY_LEN;
1976
os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
1982
int eap_peer_ttls_register(void)
1984
struct eap_method *eap;
1987
eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1988
EAP_VENDOR_IETF, EAP_TYPE_TTLS, "TTLS");
1992
eap->init = eap_ttls_init;
1993
eap->deinit = eap_ttls_deinit;
1994
eap->process = eap_ttls_process;
1995
eap->isKeyAvailable = eap_ttls_isKeyAvailable;
1996
eap->getKey = eap_ttls_getKey;
1997
eap->get_status = eap_ttls_get_status;
1998
eap->has_reauth_data = eap_ttls_has_reauth_data;
1999
eap->deinit_for_reauth = eap_ttls_deinit_for_reauth;
2000
eap->init_for_reauth = eap_ttls_init_for_reauth;
2002
ret = eap_peer_method_register(eap);
2004
eap_peer_method_free(eap);