1 /*
2 * EAP peer method: EAP-TTLS (RFC 5281)
3 * Copyright (c) 2004-2011, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "utils/includes.h"
10
11 #ifdef EAP_TTLS
12 #include "utils/common.h"
13 #include "crypto/ms_funcs.h"
14 #include "crypto/sha1.h"
15 #include "tls/tls.h"
16 #include "eap_peer/chap.h"
17 #include "eap_peer/eap.h"
18 #include "eap_peer/eap_ttls.h"
19 #include "eap_peer/mschapv2.h"
20 #include "eap_peer/eap_i.h"
21 #include "eap_peer/eap_tls_common.h"
22 #include "eap_peer/eap_config.h"
23 #include "eap_peer/eap_methods.h"
24
25
26 #define EAP_TTLS_VERSION 0
27
28 static void eap_ttls_deinit(struct eap_sm *sm, void *priv);
29
30 struct eap_ttls_data {
31 struct eap_ssl_data ssl;
32
33 int ttls_version;
34
35 const struct eap_method *phase2_method;
36 void *phase2_priv;
37 int phase2_success;
38 int phase2_start;
39
40 enum phase2_types {
41 EAP_TTLS_PHASE2_EAP,
42 EAP_TTLS_PHASE2_MSCHAPV2,
43 EAP_TTLS_PHASE2_MSCHAP,
44 EAP_TTLS_PHASE2_PAP,
45 EAP_TTLS_PHASE2_CHAP
46 } phase2_type;
47 struct eap_method_type phase2_eap_type;
48 struct eap_method_type *phase2_eap_types;
49 size_t num_phase2_eap_types;
50
51 u8 auth_response[MSCHAPV2_AUTH_RESPONSE_LEN];
52 int auth_response_valid;
53 u8 master_key[MSCHAPV2_MASTER_KEY_LEN]; /* MSCHAPv2 master key */
54 u8 ident;
55 int resuming; /* starting a resumed session */
56 int reauth; /* reauthentication */
57 u8 *key_data;
58 u8 *session_id;
59 size_t id_len;
60
61 struct wpabuf *pending_phase2_req;
62
63 #ifdef EAP_TNC
64 int ready_for_tnc;
65 int tnc_started;
66 #endif /* EAP_TNC */
67 };
68
69
eap_ttls_init(struct eap_sm * sm)70 static void * eap_ttls_init(struct eap_sm *sm)
71 {
72 struct eap_ttls_data *data;
73 struct eap_peer_config *config = eap_get_config(sm);
74
75 data = (struct eap_ttls_data *)os_zalloc(sizeof(*data));
76 if (data == NULL)
77 return NULL;
78 data->ttls_version = EAP_TTLS_VERSION;
79 data->phase2_type = EAP_TTLS_PHASE2_EAP;
80
81 if (config && config->phase2) {
82 if (os_strstr(config->phase2, "autheap=")) {
83 data->phase2_type = EAP_TTLS_PHASE2_EAP;
84 } else if (os_strstr(config->phase2, "auth=MSCHAPV2")) {
85 data->phase2_type = EAP_TTLS_PHASE2_MSCHAPV2;
86 } else if (os_strstr(config->phase2, "auth=MSCHAP")) {
87 data->phase2_type = EAP_TTLS_PHASE2_MSCHAP;
88 } else if (os_strstr(config->phase2, "auth=PAP")) {
89 data->phase2_type = EAP_TTLS_PHASE2_PAP;
90 } else if (os_strstr(config->phase2, "auth=CHAP")) {
91 data->phase2_type = EAP_TTLS_PHASE2_CHAP;
92 }
93 }
94
95 if (data->phase2_type == EAP_TTLS_PHASE2_EAP) {
96 if (eap_peer_select_phase2_methods(config, "autheap=",
97 &data->phase2_eap_types,
98 &data->num_phase2_eap_types)
99 < 0) {
100 eap_ttls_deinit(sm, data);
101 return NULL;
102 }
103
104 data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
105 data->phase2_eap_type.method = EAP_TYPE_NONE;
106 }
107
108 if (eap_peer_tls_ssl_init(sm, &data->ssl, config, EAP_TYPE_TTLS)) {
109 wpa_printf(MSG_ERROR, "EAP-TTLS: Failed to initialize SSL.\n");
110 eap_ttls_deinit(sm, data);
111 return NULL;
112 }
113
114 return data;
115 }
116
117
eap_ttls_phase2_eap_deinit(struct eap_sm * sm,struct eap_ttls_data * data)118 static void eap_ttls_phase2_eap_deinit(struct eap_sm *sm,
119 struct eap_ttls_data *data)
120 {
121 if (data->phase2_priv && data->phase2_method) {
122 data->phase2_method->deinit(sm, data->phase2_priv);
123 data->phase2_method = NULL;
124 data->phase2_priv = NULL;
125 }
126 }
127
128
eap_ttls_deinit(struct eap_sm * sm,void * priv)129 static void eap_ttls_deinit(struct eap_sm *sm, void *priv)
130 {
131 struct eap_ttls_data *data = priv;
132 if (data == NULL)
133 return;
134 eap_ttls_phase2_eap_deinit(sm, data);
135 os_free(data->phase2_eap_types);
136 eap_peer_tls_ssl_deinit(sm, &data->ssl);
137 os_free(data->key_data);
138 os_free(data->session_id);
139 wpabuf_free(data->pending_phase2_req);
140 os_free(data);
141 }
142
143
eap_ttls_avp_hdr(u8 * avphdr,u32 avp_code,u32 vendor_id,int mandatory,size_t len)144 static u8 * eap_ttls_avp_hdr(u8 *avphdr, u32 avp_code, u32 vendor_id,
145 int mandatory, size_t len)
146 {
147 struct ttls_avp_vendor *avp;
148 u8 flags;
149 size_t hdrlen;
150
151 avp = (struct ttls_avp_vendor *) avphdr;
152 flags = mandatory ? AVP_FLAGS_MANDATORY : 0;
153 if (vendor_id) {
154 flags |= AVP_FLAGS_VENDOR;
155 hdrlen = sizeof(*avp);
156 avp->vendor_id = host_to_be32(vendor_id);
157 } else {
158 hdrlen = sizeof(struct ttls_avp);
159 }
160
161 avp->avp_code = host_to_be32(avp_code);
162 avp->avp_length = host_to_be32((flags << 24) | (u32) (hdrlen + len));
163
164 return avphdr + hdrlen;
165 }
166
167
eap_ttls_avp_add(u8 * start,u8 * avphdr,u32 avp_code,u32 vendor_id,int mandatory,const u8 * data,size_t len)168 static u8 * eap_ttls_avp_add(u8 *start, u8 *avphdr, u32 avp_code,
169 u32 vendor_id, int mandatory,
170 const u8 *data, size_t len)
171 {
172 u8 *pos;
173 pos = eap_ttls_avp_hdr(avphdr, avp_code, vendor_id, mandatory, len);
174 os_memcpy(pos, data, len);
175 pos += len;
176 AVP_PAD(start, pos);
177 return pos;
178 }
179
180
eap_ttls_avp_encapsulate(struct wpabuf ** resp,u32 avp_code,int mandatory)181 static int eap_ttls_avp_encapsulate(struct wpabuf **resp, u32 avp_code,
182 int mandatory)
183 {
184 struct wpabuf *msg;
185 u8 *avp, *pos;
186
187 msg = wpabuf_alloc(sizeof(struct ttls_avp) + wpabuf_len(*resp) + 4);
188 if (msg == NULL) {
189 wpabuf_free(*resp);
190 *resp = NULL;
191 return -1;
192 }
193
194 avp = wpabuf_mhead(msg);
195 pos = eap_ttls_avp_hdr(avp, avp_code, 0, mandatory, wpabuf_len(*resp));
196 os_memcpy(pos, wpabuf_head(*resp), wpabuf_len(*resp));
197 pos += wpabuf_len(*resp);
198 AVP_PAD(avp, pos);
199 wpabuf_free(*resp);
200 wpabuf_put(msg, pos - avp);
201 *resp = msg;
202 return 0;
203 }
204
205
eap_ttls_v0_derive_key(struct eap_sm * sm,struct eap_ttls_data * data)206 static int eap_ttls_v0_derive_key(struct eap_sm *sm,
207 struct eap_ttls_data *data)
208 {
209 os_free(data->key_data);
210 data->key_data = eap_peer_tls_derive_key(sm, &data->ssl,
211 "ttls keying material",
212 EAP_TLS_KEY_LEN);
213 if (!data->key_data) {
214 wpa_printf(MSG_ERROR, "EAP-TTLS: Failed to derive key\n");
215 return -1;
216 }
217
218 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
219 data->key_data, EAP_TLS_KEY_LEN);
220 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived EMSK",
221 data->key_data + EAP_TLS_KEY_LEN,
222 EAP_EMSK_LEN);
223
224 os_free(data->session_id);
225 data->session_id = eap_peer_tls_derive_session_id(sm, &data->ssl,
226 EAP_TYPE_TTLS,
227 &data->id_len);
228 if (data->session_id) {
229 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Derived Session-Id",
230 data->session_id, data->id_len);
231 } else {
232 wpa_printf(MSG_ERROR, "EAP-TTLS: Failed to derive Session-Id\n");
233 }
234
235 return 0;
236 }
237
238
239 #ifndef CONFIG_FIPS
eap_ttls_implicit_challenge(struct eap_sm * sm,struct eap_ttls_data * data,size_t len)240 static u8 * eap_ttls_implicit_challenge(struct eap_sm *sm,
241 struct eap_ttls_data *data, size_t len)
242 {
243 return eap_peer_tls_derive_key(sm, &data->ssl, "ttls challenge", len);
244 }
245 #endif /* CONFIG_FIPS */
246
247
eap_ttls_phase2_select_eap_method(struct eap_ttls_data * data,u8 method)248 static void eap_ttls_phase2_select_eap_method(struct eap_ttls_data *data,
249 u8 method)
250 {
251 size_t i;
252 for (i = 0; i < data->num_phase2_eap_types; i++) {
253 if (data->phase2_eap_types[i].vendor != EAP_VENDOR_IETF ||
254 data->phase2_eap_types[i].method != method)
255 continue;
256
257 data->phase2_eap_type.vendor =
258 data->phase2_eap_types[i].vendor;
259 data->phase2_eap_type.method =
260 data->phase2_eap_types[i].method;
261 wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
262 "Phase 2 EAP vendor %d method %d",
263 data->phase2_eap_type.vendor,
264 data->phase2_eap_type.method);
265 break;
266 }
267 }
268
269
eap_ttls_phase2_eap_process(struct eap_sm * sm,struct eap_ttls_data * data,struct eap_method_ret * ret,struct eap_hdr * hdr,size_t len,struct wpabuf ** resp)270 static int eap_ttls_phase2_eap_process(struct eap_sm *sm,
271 struct eap_ttls_data *data,
272 struct eap_method_ret *ret,
273 struct eap_hdr *hdr, size_t len,
274 struct wpabuf **resp)
275 {
276 struct wpabuf msg;
277 struct eap_method_ret iret;
278
279 os_memset(&iret, 0, sizeof(iret));
280 wpabuf_set(&msg, hdr, len);
281 *resp = data->phase2_method->process(sm, data->phase2_priv, &iret,
282 &msg);
283 if ((iret.methodState == METHOD_DONE ||
284 iret.methodState == METHOD_MAY_CONT) &&
285 (iret.decision == DECISION_UNCOND_SUCC ||
286 iret.decision == DECISION_COND_SUCC ||
287 iret.decision == DECISION_FAIL)) {
288 ret->methodState = iret.methodState;
289 ret->decision = iret.decision;
290 }
291
292 return 0;
293 }
294
295
eap_ttls_phase2_request_eap_method(struct eap_sm * sm,struct eap_ttls_data * data,struct eap_method_ret * ret,struct eap_hdr * hdr,size_t len,u8 method,struct wpabuf ** resp)296 static int eap_ttls_phase2_request_eap_method(struct eap_sm *sm,
297 struct eap_ttls_data *data,
298 struct eap_method_ret *ret,
299 struct eap_hdr *hdr, size_t len,
300 u8 method, struct wpabuf **resp)
301 {
302 #ifdef EAP_TNC
303 if (data->tnc_started && data->phase2_method &&
304 data->phase2_priv && method == EAP_TYPE_TNC &&
305 data->phase2_eap_type.method == EAP_TYPE_TNC)
306 return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len,
307 resp);
308
309 if (data->ready_for_tnc && !data->tnc_started &&
310 method == EAP_TYPE_TNC) {
311 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
312 "EAP method");
313 data->tnc_started = 1;
314 }
315
316 if (data->tnc_started) {
317 if (data->phase2_eap_type.vendor != EAP_VENDOR_IETF ||
318 data->phase2_eap_type.method == EAP_TYPE_TNC) {
319 wpa_printf(MSG_DEBUG, "EAP-TTLS: Unexpected EAP "
320 "type %d for TNC", method);
321 return -1;
322 }
323
324 data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
325 data->phase2_eap_type.method = method;
326 wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
327 "Phase 2 EAP vendor %d method %d (TNC)",
328 data->phase2_eap_type.vendor,
329 data->phase2_eap_type.method);
330
331 if (data->phase2_type == EAP_TTLS_PHASE2_EAP)
332 eap_ttls_phase2_eap_deinit(sm, data);
333 }
334 #endif /* EAP_TNC */
335
336 if (data->phase2_eap_type.vendor == EAP_VENDOR_IETF &&
337 data->phase2_eap_type.method == EAP_TYPE_NONE)
338 eap_ttls_phase2_select_eap_method(data, method);
339
340 if (method != data->phase2_eap_type.method || method == EAP_TYPE_NONE)
341 {
342 if (eap_peer_tls_phase2_nak(data->phase2_eap_types,
343 data->num_phase2_eap_types,
344 hdr, resp))
345 return -1;
346 return 0;
347 }
348
349 if (data->phase2_priv == NULL) {
350 data->phase2_method = eap_peer_get_eap_method(
351 EAP_VENDOR_IETF, method);
352 if (data->phase2_method) {
353 sm->init_phase2 = 1;
354 data->phase2_priv = data->phase2_method->init(sm);
355 sm->init_phase2 = 0;
356 }
357 }
358 if (data->phase2_priv == NULL || data->phase2_method == NULL) {
359 wpa_printf(MSG_INFO, "EAP-TTLS: failed to initialize "
360 "Phase 2 EAP method %d", method);
361 return -1;
362 }
363
364 return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len, resp);
365 }
366
367
eap_ttls_phase2_request_eap(struct eap_sm * sm,struct eap_ttls_data * data,struct eap_method_ret * ret,struct eap_hdr * hdr,struct wpabuf ** resp)368 static int eap_ttls_phase2_request_eap(struct eap_sm *sm,
369 struct eap_ttls_data *data,
370 struct eap_method_ret *ret,
371 struct eap_hdr *hdr,
372 struct wpabuf **resp)
373 {
374 size_t len = be_to_host16(hdr->length);
375 u8 *pos;
376 struct eap_peer_config *config = eap_get_config(sm);
377
378 if (len <= sizeof(struct eap_hdr)) {
379 wpa_printf(MSG_INFO, "EAP-TTLS: too short "
380 "Phase 2 request (len=%lu)", (unsigned long) len);
381 return -1;
382 }
383 pos = (u8 *) (hdr + 1);
384 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP Request: type=%d", *pos);
385 switch (*pos) {
386 case EAP_TYPE_IDENTITY:
387 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
388 break;
389 default:
390 if (eap_ttls_phase2_request_eap_method(sm, data, ret, hdr, len,
391 *pos, resp) < 0)
392 return -1;
393 break;
394 }
395
396 if (*resp == NULL &&
397 (config->pending_req_identity || config->pending_req_password ||
398 config->pending_req_otp)) {
399 return 0;
400 }
401
402 if (*resp == NULL)
403 return -1;
404
405 wpa_hexdump_buf(MSG_DEBUG, "EAP-TTLS: AVP encapsulate EAP Response",
406 *resp);
407 return eap_ttls_avp_encapsulate(resp, RADIUS_ATTR_EAP_MESSAGE, 1);
408 }
409
410
eap_ttls_phase2_request_mschapv2(struct eap_sm * sm,struct eap_ttls_data * data,struct eap_method_ret * ret,struct wpabuf ** resp)411 static int eap_ttls_phase2_request_mschapv2(struct eap_sm *sm,
412 struct eap_ttls_data *data,
413 struct eap_method_ret *ret,
414 struct wpabuf **resp)
415 {
416 #ifdef CONFIG_FIPS
417 wpa_printf(MSG_ERROR, "EAP-TTLS: MSCHAPV2 not supported in FIPS build");
418 return -1;
419 #else /* CONFIG_FIPS */
420 #ifdef EAP_MSCHAPv2
421 struct wpabuf *msg;
422 u8 *buf, *pos, *challenge, *peer_challenge;
423 const u8 *identity, *password;
424 size_t identity_len, password_len;
425 int pwhash;
426
427 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAPV2 Request\n");
428
429 identity = eap_get_config_identity(sm, &identity_len);
430 password = eap_get_config_password2(sm, &password_len, &pwhash);
431 if (identity == NULL || password == NULL)
432 return -1;
433
434 msg = wpabuf_alloc(identity_len + 1000);
435 if (msg == NULL) {
436 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to allocate memory\n");
437 return -1;
438 }
439 pos = buf = wpabuf_mhead(msg);
440
441 /* User-Name */
442 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
443 identity, identity_len);
444
445 /* MS-CHAP-Challenge */
446 challenge = eap_ttls_implicit_challenge(
447 sm, data, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN + 1);
448 if (challenge == NULL) {
449 wpabuf_free(msg);
450 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
451 "implicit challenge\n");
452 return -1;
453 }
454
455 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
456 RADIUS_VENDOR_ID_MICROSOFT, 1,
457 challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
458
459 /* MS-CHAP2-Response */
460 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP2_RESPONSE,
461 RADIUS_VENDOR_ID_MICROSOFT, 1,
462 EAP_TTLS_MSCHAPV2_RESPONSE_LEN);
463 data->ident = challenge[EAP_TTLS_MSCHAPV2_CHALLENGE_LEN];
464 *pos++ = data->ident;
465 *pos++ = 0; /* Flags */
466 if (os_get_random(pos, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN) < 0) {
467 os_free(challenge);
468 wpabuf_free(msg);
469 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to get "
470 "random data for peer challenge\n");
471 return -1;
472 }
473 peer_challenge = pos;
474 pos += EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
475 os_memset(pos, 0, 8); /* Reserved, must be zero */
476 pos += 8;
477 if (mschapv2_derive_response(identity, identity_len, password,
478 password_len, pwhash, challenge,
479 peer_challenge, pos, data->auth_response,
480 data->master_key)) {
481 os_free(challenge);
482 wpabuf_free(msg);
483 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
484 "response\n");
485 return -1;
486 }
487 data->auth_response_valid = 1;
488
489 pos += 24;
490 os_free(challenge);
491 AVP_PAD(buf, pos);
492
493 wpabuf_put(msg, pos - buf);
494 *resp = msg;
495
496 if (sm->workaround) {
497 /* At least FreeRADIUS seems to be terminating
498 * EAP-TTLS/MSHCAPV2 without the expected MS-CHAP-v2 Success
499 * packet. */
500 wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: EAP workaround - "
501 "allow success without tunneled response\n");
502 ret->methodState = METHOD_MAY_CONT;
503 ret->decision = DECISION_COND_SUCC;
504 }
505
506 return 0;
507 #else /* EAP_MSCHAPv2 */
508 printf("[Debug] Set EEEEE \n");
509 wpa_printf(MSG_ERROR, "EAP-TTLS: MSCHAPv2 not included in the build\n");
510 return -1;
511 #endif /* EAP_MSCHAPv2 */
512 #endif /* CONFIG_FIPS */
513 }
514
515
eap_ttls_phase2_request_mschap(struct eap_sm * sm,struct eap_ttls_data * data,struct eap_method_ret * ret,struct wpabuf ** resp)516 static int eap_ttls_phase2_request_mschap(struct eap_sm *sm,
517 struct eap_ttls_data *data,
518 struct eap_method_ret *ret,
519 struct wpabuf **resp)
520 {
521 #ifdef CONFIG_FIPS
522 wpa_printf(MSG_ERROR, "EAP-TTLS: MSCHAP not supported in FIPS build");
523 return -1;
524 #else /* CONFIG_FIPS */
525 struct wpabuf *msg;
526 u8 *buf, *pos, *challenge;
527 const u8 *identity, *password;
528 size_t identity_len, password_len;
529 int pwhash;
530
531 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAP Request");
532
533 identity = eap_get_config_identity(sm, &identity_len);
534 password = eap_get_config_password2(sm, &password_len, &pwhash);
535 if (identity == NULL || password == NULL)
536 return -1;
537
538 msg = wpabuf_alloc(identity_len + 1000);
539 if (msg == NULL) {
540 wpa_printf(MSG_ERROR,
541 "EAP-TTLS/MSCHAP: Failed to allocate memory");
542 return -1;
543 }
544 pos = buf = wpabuf_mhead(msg);
545
546 /* User-Name */
547 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
548 identity, identity_len);
549
550 /* MS-CHAP-Challenge */
551 challenge = eap_ttls_implicit_challenge(
552 sm, data, EAP_TTLS_MSCHAP_CHALLENGE_LEN + 1);
553 if (challenge == NULL) {
554 wpabuf_free(msg);
555 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAP: Failed to derive "
556 "implicit challenge");
557 return -1;
558 }
559
560 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
561 RADIUS_VENDOR_ID_MICROSOFT, 1,
562 challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
563
564 /* MS-CHAP-Response */
565 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP_RESPONSE,
566 RADIUS_VENDOR_ID_MICROSOFT, 1,
567 EAP_TTLS_MSCHAP_RESPONSE_LEN);
568 data->ident = challenge[EAP_TTLS_MSCHAP_CHALLENGE_LEN];
569 *pos++ = data->ident;
570 *pos++ = 1; /* Flags: Use NT style passwords */
571 os_memset(pos, 0, 24); /* LM-Response */
572 pos += 24;
573 if (pwhash) {
574 challenge_response(challenge, password, pos); /* NT-Response */
575 wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password hash",
576 password, 16);
577 } else {
578 nt_challenge_response(challenge, password, password_len,
579 pos); /* NT-Response */
580 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password",
581 password, password_len);
582 }
583 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP implicit challenge",
584 challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
585 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP response", pos, 24);
586 pos += 24;
587 os_free(challenge);
588 AVP_PAD(buf, pos);
589
590 wpabuf_put(msg, pos - buf);
591 *resp = msg;
592
593 /* EAP-TTLS/MSCHAP does not provide tunneled success
594 * notification, so assume that Phase2 succeeds. */
595 ret->methodState = METHOD_DONE;
596 ret->decision = DECISION_COND_SUCC;
597
598 return 0;
599 #endif /* CONFIG_FIPS */
600 }
601
602
eap_ttls_phase2_request_pap(struct eap_sm * sm,struct eap_ttls_data * data,struct eap_method_ret * ret,struct wpabuf ** resp)603 static int eap_ttls_phase2_request_pap(struct eap_sm *sm,
604 struct eap_ttls_data *data,
605 struct eap_method_ret *ret,
606 struct wpabuf **resp)
607 {
608 struct wpabuf *msg;
609 u8 *buf, *pos;
610 size_t pad;
611 const u8 *identity, *password;
612 size_t identity_len, password_len;
613
614 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 PAP Request");
615
616 identity = eap_get_config_identity(sm, &identity_len);
617 password = eap_get_config_password(sm, &password_len);
618 if (identity == NULL || password == NULL)
619 return -1;
620
621 msg = wpabuf_alloc(identity_len + password_len + 100);
622 if (msg == NULL) {
623 wpa_printf(MSG_ERROR,
624 "EAP-TTLS/PAP: Failed to allocate memory");
625 return -1;
626 }
627 pos = buf = wpabuf_mhead(msg);
628
629 /* User-Name */
630 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
631 identity, identity_len);
632
633 /* User-Password; in RADIUS, this is encrypted, but EAP-TTLS encrypts
634 * the data, so no separate encryption is used in the AVP itself.
635 * However, the password is padded to obfuscate its length. */
636 pad = password_len == 0 ? 16 : (16 - (password_len & 15)) & 15;
637 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_USER_PASSWORD, 0, 1,
638 password_len + pad);
639 os_memcpy(pos, password, password_len);
640 pos += password_len;
641 os_memset(pos, 0, pad);
642 pos += pad;
643 AVP_PAD(buf, pos);
644
645 wpabuf_put(msg, pos - buf);
646 *resp = msg;
647
648 /* EAP-TTLS/PAP does not provide tunneled success notification,
649 * so assume that Phase2 succeeds. */
650 ret->methodState = METHOD_DONE;
651 ret->decision = DECISION_COND_SUCC;
652
653 return 0;
654 }
655
656
eap_ttls_phase2_request_chap(struct eap_sm * sm,struct eap_ttls_data * data,struct eap_method_ret * ret,struct wpabuf ** resp)657 static int eap_ttls_phase2_request_chap(struct eap_sm *sm,
658 struct eap_ttls_data *data,
659 struct eap_method_ret *ret,
660 struct wpabuf **resp)
661 {
662 #ifdef CONFIG_FIPS
663 wpa_printf(MSG_ERROR, "EAP-TTLS: CHAP not supported in FIPS build");
664 return -1;
665 #else /* CONFIG_FIPS */
666 struct wpabuf *msg;
667 u8 *buf, *pos, *challenge;
668 const u8 *identity, *password;
669 size_t identity_len, password_len;
670
671 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 CHAP Request");
672
673 identity = eap_get_config_identity(sm, &identity_len);
674 password = eap_get_config_password(sm, &password_len);
675 if (identity == NULL || password == NULL)
676 return -1;
677
678 msg = wpabuf_alloc(identity_len + 1000);
679 if (msg == NULL) {
680 wpa_printf(MSG_ERROR,
681 "EAP-TTLS/CHAP: Failed to allocate memory");
682 return -1;
683 }
684 pos = buf = wpabuf_mhead(msg);
685
686 /* User-Name */
687 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
688 identity, identity_len);
689
690 /* CHAP-Challenge */
691 challenge = eap_ttls_implicit_challenge(
692 sm, data, EAP_TTLS_CHAP_CHALLENGE_LEN + 1);
693 if (challenge == NULL) {
694 wpabuf_free(msg);
695 wpa_printf(MSG_ERROR, "EAP-TTLS/CHAP: Failed to derive "
696 "implicit challenge");
697 return -1;
698 }
699
700 pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_CHAP_CHALLENGE, 0, 1,
701 challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
702
703 /* CHAP-Password */
704 pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_CHAP_PASSWORD, 0, 1,
705 1 + EAP_TTLS_CHAP_PASSWORD_LEN);
706 data->ident = challenge[EAP_TTLS_CHAP_CHALLENGE_LEN];
707 *pos++ = data->ident;
708
709 /* MD5(Ident + Password + Challenge) */
710 chap_md5(data->ident, password, password_len, challenge,
711 EAP_TTLS_CHAP_CHALLENGE_LEN, pos);
712
713 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: CHAP username",
714 identity, identity_len);
715 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: CHAP password",
716 password, password_len);
717 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP implicit challenge",
718 challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
719 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP password",
720 pos, EAP_TTLS_CHAP_PASSWORD_LEN);
721 pos += EAP_TTLS_CHAP_PASSWORD_LEN;
722 os_free(challenge);
723 AVP_PAD(buf, pos);
724
725 wpabuf_put(msg, pos - buf);
726 *resp = msg;
727
728 /* EAP-TTLS/CHAP does not provide tunneled success
729 * notification, so assume that Phase2 succeeds. */
730 ret->methodState = METHOD_DONE;
731 ret->decision = DECISION_COND_SUCC;
732
733 return 0;
734 #endif /* CONFIG_FIPS */
735 }
736
737
eap_ttls_phase2_request(struct eap_sm * sm,struct eap_ttls_data * data,struct eap_method_ret * ret,struct eap_hdr * hdr,struct wpabuf ** resp)738 static int eap_ttls_phase2_request(struct eap_sm *sm,
739 struct eap_ttls_data *data,
740 struct eap_method_ret *ret,
741 struct eap_hdr *hdr,
742 struct wpabuf **resp)
743 {
744 int res = 0;
745 size_t len;
746 enum phase2_types phase2_type = data->phase2_type;
747
748 #ifdef EAP_TNC
749 if (data->tnc_started) {
750 printf("[debug] set phase2_type \n");
751 wpa_printf(MSG_DEBUG, "EAP-TTLS: Processing TNC\n");
752 phase2_type = EAP_TTLS_PHASE2_EAP;
753 }
754 #endif /* EAP_TNC */
755
756 if (phase2_type == EAP_TTLS_PHASE2_MSCHAPV2 ||
757 phase2_type == EAP_TTLS_PHASE2_MSCHAP ||
758 phase2_type == EAP_TTLS_PHASE2_PAP ||
759 phase2_type == EAP_TTLS_PHASE2_CHAP) {
760 if (eap_get_config_identity(sm, &len) == NULL) {
761 wpa_printf(MSG_ERROR, "EAP-TTLS: Identity not configured\n");
762 if (eap_get_config_password(sm, &len) == NULL)
763 printf("[Debug] Return because no identity EAP_TTLS_PHASE2_MSCHAPV2 EAP_TTLS_PHASE2_MSCHAP\n");
764 return 0;
765 }
766
767 if (eap_get_config_password(sm, &len) == NULL) {
768 wpa_printf(MSG_ERROR, "EAP-TTLS: Password not configured\n");
769 printf("[Debug] Return because no password EAP_TTLS_PHASE2_MSCHAPV2 EAP_TTLS_PHASE2_MSCHAP\n");
770 return 0;
771 }
772 }
773
774 switch (phase2_type) {
775 case EAP_TTLS_PHASE2_EAP:
776 res = eap_ttls_phase2_request_eap(sm, data, ret, hdr, resp);
777 break;
778 case EAP_TTLS_PHASE2_MSCHAPV2:
779 res = eap_ttls_phase2_request_mschapv2(sm, data, ret, resp);
780 break;
781 case EAP_TTLS_PHASE2_MSCHAP:
782 res = eap_ttls_phase2_request_mschap(sm, data, ret, resp);
783 break;
784 case EAP_TTLS_PHASE2_PAP:
785 res = eap_ttls_phase2_request_pap(sm, data, ret, resp);
786 break;
787 case EAP_TTLS_PHASE2_CHAP:
788 res = eap_ttls_phase2_request_chap(sm, data, ret, resp);
789 break;
790 default:
791 wpa_printf(MSG_ERROR, "EAP-TTLS: Phase 2 - Unknown\n");
792 res = -1;
793 break;
794 }
795
796 if (res < 0) {
797 ret->methodState = METHOD_DONE;
798 ret->decision = DECISION_FAIL;
799 }
800
801 return res;
802 }
803
804
805 struct ttls_parse_avp {
806 u8 *mschapv2;
807 u8 *eapdata;
808 size_t eap_len;
809 int mschapv2_error;
810 };
811
812
eap_ttls_parse_attr_eap(const u8 * dpos,size_t dlen,struct ttls_parse_avp * parse)813 static int eap_ttls_parse_attr_eap(const u8 *dpos, size_t dlen,
814 struct ttls_parse_avp *parse)
815 {
816 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - EAP Message\n");
817 if (parse->eapdata == NULL) {
818 parse->eapdata = os_malloc(dlen);
819 if (parse->eapdata == NULL) {
820 wpa_printf(MSG_ERROR, "EAP-TTLS: Failed to allocate "
821 "memory for Phase 2 EAP data\n");
822 return -1;
823 }
824 os_memcpy(parse->eapdata, dpos, dlen);
825 parse->eap_len = dlen;
826 } else {
827 u8 *neweap = (u8 *)os_realloc(parse->eapdata, parse->eap_len + dlen);
828 if (neweap == NULL) {
829 wpa_printf(MSG_ERROR, "EAP-TTLS: Failed to allocate "
830 "memory for Phase 2 EAP data\n");
831 return -1;
832 }
833 os_memcpy(neweap + parse->eap_len, dpos, dlen);
834 parse->eapdata = neweap;
835 parse->eap_len += dlen;
836 }
837
838 return 0;
839 }
840
841
eap_ttls_parse_avp(u8 * pos,size_t left,struct ttls_parse_avp * parse)842 static int eap_ttls_parse_avp(u8 *pos, size_t left,
843 struct ttls_parse_avp *parse)
844 {
845 struct ttls_avp *avp;
846 u32 avp_code, avp_length, vendor_id = 0;
847 u8 avp_flags, *dpos;
848 size_t dlen;
849
850 avp = (struct ttls_avp *) pos;
851 avp_code = be_to_host32(avp->avp_code);
852 avp_length = be_to_host32(avp->avp_length);
853 avp_flags = (avp_length >> 24) & 0xff;
854 avp_length &= 0xffffff;
855 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP: code=%d flags=0x%02x "
856 "length=%d\n", (int) avp_code, avp_flags,
857 (int) avp_length);
858
859 if (avp_length > left) {
860 wpa_printf(MSG_ERROR, "EAP-TTLS: AVP overflow "
861 "(len=%d, left=%lu) - dropped\n",
862 (int) avp_length, (unsigned long) left);
863 return -1;
864 }
865
866 if (avp_length < sizeof(*avp)) {
867 wpa_printf(MSG_ERROR, "EAP-TTLS: Invalid AVP length %d\n",
868 avp_length);
869 return -1;
870 }
871
872 dpos = (u8 *) (avp + 1);
873 dlen = avp_length - sizeof(*avp);
874 if (avp_flags & AVP_FLAGS_VENDOR) {
875 if (dlen < 4) {
876 wpa_printf(MSG_ERROR, "EAP-TTLS: Vendor AVP underflow\n");
877 return -1;
878 }
879 vendor_id = WPA_GET_BE32(dpos);
880 wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP vendor_id %d\n",
881 (int) vendor_id);
882 dpos += 4;
883 dlen -= 4;
884 }
885
886 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP data", dpos, dlen);
887
888 if (vendor_id == 0 && avp_code == RADIUS_ATTR_EAP_MESSAGE) {
889 if (eap_ttls_parse_attr_eap(dpos, dlen, parse) < 0)
890 return -1;
891 } else if (vendor_id == 0 && avp_code == RADIUS_ATTR_REPLY_MESSAGE) {
892 /* This is an optional message that can be displayed to
893 * the user. */
894 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: AVP - Reply-Message",
895 dpos, dlen);
896 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
897 avp_code == RADIUS_ATTR_MS_CHAP2_SUCCESS) {
898 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP2-Success",
899 dpos, dlen);
900 if (dlen != 43) {
901 wpa_printf(MSG_ERROR, "EAP-TTLS: Unexpected "
902 "MS-CHAP2-Success length "
903 "(len=%lu, expected 43)\n",
904 (unsigned long) dlen);
905 return -1;
906 }
907 parse->mschapv2 = dpos;
908 } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
909 avp_code == RADIUS_ATTR_MS_CHAP_ERROR) {
910 wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP-Error",
911 dpos, dlen);
912 parse->mschapv2_error = 1;
913 } else if (avp_flags & AVP_FLAGS_MANDATORY) {
914 wpa_printf(MSG_ERROR, "EAP-TTLS: Unsupported mandatory AVP "
915 "code %d vendor_id %d - dropped\n",
916 (int) avp_code, (int) vendor_id);
917 return -1;
918 } else {
919 wpa_printf(MSG_INFO, "EAP-TTLS: Ignoring unsupported AVP "
920 "code %d vendor_id %d\n",
921 (int) avp_code, (int) vendor_id);
922 }
923
924 return avp_length;
925 }
926
927
eap_ttls_parse_avps(struct wpabuf * in_decrypted,struct ttls_parse_avp * parse)928 static int eap_ttls_parse_avps(struct wpabuf *in_decrypted,
929 struct ttls_parse_avp *parse)
930 {
931 u8 *pos;
932 size_t left, pad;
933 int avp_length;
934
935 pos = wpabuf_mhead(in_decrypted);
936 left = wpabuf_len(in_decrypted);
937 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Decrypted Phase 2 AVPs", pos, left);
938 if (left < sizeof(struct ttls_avp)) {
939 wpa_printf(MSG_ERROR, "EAP-TTLS: Too short Phase 2 AVP frame"
940 " len=%lu expected %lu or more - dropped\n",
941 (unsigned long) left,
942 (unsigned long) sizeof(struct ttls_avp));
943 return -1;
944 }
945
946 /* Parse AVPs */
947 os_memset(parse, 0, sizeof(*parse));
948
949 while (left > 0) {
950 avp_length = eap_ttls_parse_avp(pos, left, parse);
951 if (avp_length < 0)
952 return -1;
953
954 pad = (4 - (avp_length & 3)) & 3;
955 pos += avp_length + pad;
956 if (left < avp_length + pad)
957 left = 0;
958 else
959 left -= avp_length + pad;
960 }
961
962 return 0;
963 }
964
965
eap_ttls_fake_identity_request(void)966 static u8 * eap_ttls_fake_identity_request(void)
967 {
968 struct eap_hdr *hdr;
969 u8 *buf;
970
971 wpa_printf(MSG_DEBUG, "EAP-TTLS: empty data in beginning of "
972 "Phase 2 - use fake EAP-Request Identity\n");
973 buf = os_malloc(sizeof(*hdr) + 1);
974 if (buf == NULL) {
975 wpa_printf(MSG_ERROR, "EAP-TTLS: failed to allocate "
976 "memory for fake EAP-Identity Request\n");
977 return NULL;
978 }
979
980 hdr = (struct eap_hdr *) buf;
981 hdr->code = EAP_CODE_REQUEST;
982 hdr->identifier = 0;
983 hdr->length = host_to_be16(sizeof(*hdr) + 1);
984 buf[sizeof(*hdr)] = EAP_TYPE_IDENTITY;
985
986 return buf;
987 }
988
989
eap_ttls_encrypt_response(struct eap_sm * sm,struct eap_ttls_data * data,struct wpabuf * resp,u8 identifier,struct wpabuf ** out_data)990 static int eap_ttls_encrypt_response(struct eap_sm *sm,
991 struct eap_ttls_data *data,
992 struct wpabuf *resp, u8 identifier,
993 struct wpabuf **out_data)
994 {
995 if (resp == NULL)
996 return 0;
997
998 if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
999 data->ttls_version, identifier,
1000 resp, out_data)) {
1001 wpa_printf(MSG_ERROR, "EAP-TTLS: Failed to encrypt a Phase 2 frame\n");
1002 return -1;
1003 }
1004 wpabuf_free(resp);
1005
1006 return 0;
1007 }
1008
1009
eap_ttls_process_phase2_eap(struct eap_sm * sm,struct eap_ttls_data * data,struct eap_method_ret * ret,struct ttls_parse_avp * parse,struct wpabuf ** resp)1010 static int eap_ttls_process_phase2_eap(struct eap_sm *sm,
1011 struct eap_ttls_data *data,
1012 struct eap_method_ret *ret,
1013 struct ttls_parse_avp *parse,
1014 struct wpabuf **resp)
1015 {
1016 struct eap_hdr *hdr;
1017 size_t len;
1018
1019 if (parse->eapdata == NULL) {
1020 wpa_printf(MSG_WARNING, "EAP-TTLS: No EAP Message in the "
1021 "packet - dropped");
1022 return -1;
1023 }
1024
1025 wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP",
1026 parse->eapdata, parse->eap_len);
1027 hdr = (struct eap_hdr *) parse->eapdata;
1028
1029 if (parse->eap_len < sizeof(*hdr)) {
1030 wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 EAP "
1031 "frame (len=%lu, expected %lu or more) - dropped",
1032 (unsigned long) parse->eap_len,
1033 (unsigned long) sizeof(*hdr));
1034 return -1;
1035 }
1036 len = be_to_host16(hdr->length);
1037 if (len > parse->eap_len) {
1038 wpa_printf(MSG_INFO, "EAP-TTLS: Length mismatch in Phase 2 "
1039 "EAP frame (EAP hdr len=%lu, EAP data len in "
1040 "AVP=%lu)",
1041 (unsigned long) len,
1042 (unsigned long) parse->eap_len);
1043 return -1;
1044 }
1045 wpa_printf(MSG_DEBUG, "EAP-TTLS: received Phase 2: code=%d "
1046 "identifier=%d length=%lu",
1047 hdr->code, hdr->identifier, (unsigned long) len);
1048 switch (hdr->code) {
1049 case EAP_CODE_REQUEST:
1050 if (eap_ttls_phase2_request(sm, data, ret, hdr, resp)) {
1051 wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
1052 "processing failed");
1053 return -1;
1054 }
1055 break;
1056 default:
1057 wpa_printf(MSG_INFO, "EAP-TTLS: Unexpected code=%d in "
1058 "Phase 2 EAP header", hdr->code);
1059 return -1;
1060 }
1061
1062 return 0;
1063 }
1064
1065
eap_ttls_process_phase2_mschapv2(struct eap_sm * sm,struct eap_ttls_data * data,struct eap_method_ret * ret,struct ttls_parse_avp * parse)1066 static int eap_ttls_process_phase2_mschapv2(struct eap_sm *sm,
1067 struct eap_ttls_data *data,
1068 struct eap_method_ret *ret,
1069 struct ttls_parse_avp *parse)
1070 {
1071 #ifdef EAP_MSCHAPv2
1072 if (parse->mschapv2_error) {
1073 wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Received "
1074 "MS-CHAP-Error - failed\n");
1075 ret->methodState = METHOD_DONE;
1076 ret->decision = DECISION_FAIL;
1077 /* Reply with empty data to ACK error */
1078 return 1;
1079 }
1080
1081 if (parse->mschapv2 == NULL) {
1082 #ifdef EAP_TNC
1083 if (data->phase2_success && parse->eapdata) {
1084 /*
1085 * Allow EAP-TNC to be started after successfully
1086 * completed MSCHAPV2.
1087 */
1088 return 1;
1089 }
1090 #endif /* EAP_TNC */
1091 wpa_printf(MSG_ERROR, "EAP-TTLS: no MS-CHAP2-Success AVP "
1092 "received for Phase2 MSCHAPV2\n");
1093 return -1;
1094 }
1095 if (parse->mschapv2[0] != data->ident) {
1096 wpa_printf(MSG_ERROR, "EAP-TTLS: Ident mismatch for Phase 2 "
1097 "MSCHAPV2 (received Ident 0x%02x, expected 0x%02x)\n",
1098 parse->mschapv2[0], data->ident);
1099 return -1;
1100 }
1101 if (!data->auth_response_valid ||
1102 mschapv2_verify_auth_response(data->auth_response,
1103 parse->mschapv2 + 1, 42)) {
1104 wpa_printf(MSG_ERROR, "EAP-TTLS: Invalid authenticator "
1105 "response in Phase 2 MSCHAPV2 success request\n");
1106 return -1;
1107 }
1108
1109 wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAPV2 "
1110 "authentication succeeded\n");
1111 ret->methodState = METHOD_DONE;
1112 ret->decision = DECISION_UNCOND_SUCC;
1113 data->phase2_success = 1;
1114
1115 /*
1116 * Reply with empty data; authentication server will reply
1117 * with EAP-Success after this.
1118 */
1119 return 1;
1120 #else /* EAP_MSCHAPv2 */
1121 wpa_printf(MSG_ERROR, "EAP-TTLS: MSCHAPv2 not included in the build\n");
1122 return -1;
1123 #endif /* EAP_MSCHAPv2 */
1124 }
1125
1126
1127 #ifdef EAP_TNC
eap_ttls_process_tnc_start(struct eap_sm * sm,struct eap_ttls_data * data,struct eap_method_ret * ret,struct ttls_parse_avp * parse,struct wpabuf ** resp)1128 static int eap_ttls_process_tnc_start(struct eap_sm *sm,
1129 struct eap_ttls_data *data,
1130 struct eap_method_ret *ret,
1131 struct ttls_parse_avp *parse,
1132 struct wpabuf **resp)
1133 {
1134 /* TNC uses inner EAP method after non-EAP TTLS phase 2. */
1135 if (parse->eapdata == NULL) {
1136 wpa_printf(MSG_ERROR, "EAP-TTLS: Phase 2 received "
1137 "unexpected tunneled data (no EAP)\n");
1138 return -1;
1139 }
1140
1141 if (!data->ready_for_tnc) {
1142 wpa_printf(MSG_ERROR, "EAP-TTLS: Phase 2 received "
1143 "EAP after non-EAP, but not ready for TNC\n");
1144 return -1;
1145 }
1146
1147 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
1148 "non-EAP method\n");
1149 data->tnc_started = 1;
1150
1151 if (eap_ttls_process_phase2_eap(sm, data, ret, parse, resp) < 0)
1152 return -1;
1153
1154 return 0;
1155 }
1156 #endif /* EAP_TNC */
1157
1158
eap_ttls_process_decrypted(struct eap_sm * sm,struct eap_ttls_data * data,struct eap_method_ret * ret,u8 identifier,struct ttls_parse_avp * parse,struct wpabuf * in_decrypted,struct wpabuf ** out_data)1159 static int eap_ttls_process_decrypted(struct eap_sm *sm,
1160 struct eap_ttls_data *data,
1161 struct eap_method_ret *ret,
1162 u8 identifier,
1163 struct ttls_parse_avp *parse,
1164 struct wpabuf *in_decrypted,
1165 struct wpabuf **out_data)
1166 {
1167 struct wpabuf *resp = NULL;
1168 int res;
1169 enum phase2_types phase2_type = data->phase2_type;
1170 struct eap_peer_config *config = eap_get_config(sm);
1171
1172 #ifdef EAP_TNC
1173 if (data->tnc_started)
1174 phase2_type = EAP_TTLS_PHASE2_EAP;
1175 #endif /* EAP_TNC */
1176
1177 switch (phase2_type) {
1178 case EAP_TTLS_PHASE2_EAP:
1179 if (eap_ttls_process_phase2_eap(sm, data, ret, parse, &resp) <
1180 0)
1181 return -1;
1182 break;
1183 case EAP_TTLS_PHASE2_MSCHAPV2:
1184 res = eap_ttls_process_phase2_mschapv2(sm, data, ret, parse);
1185 #ifdef EAP_TNC
1186 if (res == 1 && parse->eapdata && data->phase2_success) {
1187 /*
1188 * TNC may be required as the next
1189 * authentication method within the tunnel.
1190 */
1191 ret->methodState = METHOD_MAY_CONT;
1192 data->ready_for_tnc = 1;
1193 if (eap_ttls_process_tnc_start(sm, data, ret, parse,
1194 &resp) == 0)
1195 break;
1196 }
1197 #endif /* EAP_TNC */
1198 return res;
1199 case EAP_TTLS_PHASE2_MSCHAP:
1200 case EAP_TTLS_PHASE2_PAP:
1201 case EAP_TTLS_PHASE2_CHAP:
1202 #ifdef EAP_TNC
1203 if (eap_ttls_process_tnc_start(sm, data, ret, parse, &resp) <
1204 0)
1205 return -1;
1206 break;
1207 #else /* EAP_TNC */
1208 /* EAP-TTLS/{MSCHAP,PAP,CHAP} should not send any TLS tunneled
1209 * requests to the supplicant */
1210 wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received unexpected "
1211 "tunneled data");
1212 return -1;
1213 #endif /* EAP_TNC */
1214 }
1215
1216 if (resp) {
1217 if (eap_ttls_encrypt_response(sm, data, resp, identifier,
1218 out_data) < 0)
1219 return -1;
1220 } else if (config->pending_req_identity ||
1221 config->pending_req_password ||
1222 config->pending_req_otp ||
1223 config->pending_req_new_password) {
1224 wpabuf_free(data->pending_phase2_req);
1225 data->pending_phase2_req = wpabuf_dup(in_decrypted);
1226 }
1227
1228 return 0;
1229 }
1230
1231
eap_ttls_implicit_identity_request(struct eap_sm * sm,struct eap_ttls_data * data,struct eap_method_ret * ret,u8 identifier,struct wpabuf ** out_data)1232 static int eap_ttls_implicit_identity_request(struct eap_sm *sm,
1233 struct eap_ttls_data *data,
1234 struct eap_method_ret *ret,
1235 u8 identifier,
1236 struct wpabuf **out_data)
1237 {
1238 int retval = 0;
1239 struct eap_hdr *hdr;
1240 struct wpabuf *resp;
1241
1242 hdr = (struct eap_hdr *) eap_ttls_fake_identity_request();
1243 if (hdr == NULL) {
1244 ret->methodState = METHOD_DONE;
1245 ret->decision = DECISION_FAIL;
1246 return -1;
1247 }
1248
1249 resp = NULL;
1250 if (eap_ttls_phase2_request(sm, data, ret, hdr, &resp)) {
1251 wpa_printf(MSG_ERROR, "EAP-TTLS: Phase2 Request "
1252 "processing failed\n");
1253 retval = -1;
1254 } else {
1255 struct eap_peer_config *config = eap_get_config(sm);
1256 if (resp == NULL &&
1257 (config->pending_req_identity ||
1258 config->pending_req_password ||
1259 config->pending_req_otp ||
1260 config->pending_req_new_password)) {
1261 /*
1262 * Use empty buffer to force implicit request
1263 * processing when EAP request is re-processed after
1264 * user input.
1265 */
1266 wpabuf_free(data->pending_phase2_req);
1267 data->pending_phase2_req = wpabuf_alloc(0);
1268 }
1269
1270 retval = eap_ttls_encrypt_response(sm, data, resp, identifier,
1271 out_data);
1272 }
1273
1274 os_free(hdr);
1275
1276 if (retval < 0) {
1277 ret->methodState = METHOD_DONE;
1278 ret->decision = DECISION_FAIL;
1279 }
1280
1281 return retval;
1282 }
1283
1284
eap_ttls_phase2_start(struct eap_sm * sm,struct eap_ttls_data * data,struct eap_method_ret * ret,u8 identifier,struct wpabuf ** out_data)1285 static int eap_ttls_phase2_start(struct eap_sm *sm, struct eap_ttls_data *data,
1286 struct eap_method_ret *ret, u8 identifier,
1287 struct wpabuf **out_data)
1288 {
1289 data->phase2_start = 0;
1290
1291 /*
1292 * EAP-TTLS does not use Phase2 on fast re-auth; this must be done only
1293 * if TLS part was indeed resuming a previous session. Most
1294 * Authentication Servers terminate EAP-TTLS before reaching this
1295 * point, but some do not. Make wpa_supplicant stop phase 2 here, if
1296 * needed.
1297 */
1298 if (data->reauth &&
1299 tls_connection_resumed(sm->ssl_ctx, data->ssl.conn)) {
1300 wpa_printf(MSG_DEBUG, "EAP-TTLS: Session resumption - "
1301 "skip phase 2\n");
1302 *out_data = eap_peer_tls_build_ack(identifier, EAP_TYPE_TTLS,
1303 data->ttls_version);
1304 ret->methodState = METHOD_DONE;
1305 ret->decision = DECISION_UNCOND_SUCC;
1306 data->phase2_success = 1;
1307 return 0;
1308 }
1309
1310 return eap_ttls_implicit_identity_request(sm, data, ret, identifier,
1311 out_data);
1312 }
1313
1314
eap_ttls_decrypt(struct eap_sm * sm,struct eap_ttls_data * data,struct eap_method_ret * ret,u8 identifier,const struct wpabuf * in_data,struct wpabuf ** out_data)1315 static int eap_ttls_decrypt(struct eap_sm *sm, struct eap_ttls_data *data,
1316 struct eap_method_ret *ret, u8 identifier,
1317 const struct wpabuf *in_data,
1318 struct wpabuf **out_data)
1319 {
1320 struct wpabuf *in_decrypted = NULL;
1321 int retval = 0;
1322 struct ttls_parse_avp parse;
1323
1324 os_memset(&parse, 0, sizeof(parse));
1325
1326 wpa_printf(MSG_DEBUG, "EAP-TTLS: received %lu bytes encrypted data for"
1327 " Phase 2\n",
1328 in_data ? (unsigned long) wpabuf_len(in_data) : 0);
1329
1330 if (data->pending_phase2_req) {
1331 wpa_printf(MSG_DEBUG, "EAP-TTLS: Pending Phase 2 request - "
1332 "skip decryption and use old data\n");
1333 /* Clear TLS reassembly state. */
1334 eap_peer_tls_reset_input(&data->ssl);
1335
1336 in_decrypted = data->pending_phase2_req;
1337 data->pending_phase2_req = NULL;
1338 if (wpabuf_len(in_decrypted) == 0) {
1339 wpabuf_free(in_decrypted);
1340 return eap_ttls_implicit_identity_request(
1341 sm, data, ret, identifier, out_data);
1342 }
1343 goto continue_req;
1344 }
1345
1346 if ((in_data == NULL || wpabuf_len(in_data) == 0) &&
1347 data->phase2_start) {
1348 return eap_ttls_phase2_start(sm, data, ret, identifier,
1349 out_data);
1350 }
1351
1352 if (in_data == NULL || wpabuf_len(in_data) == 0) {
1353 /* Received TLS ACK - requesting more fragments */
1354 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
1355 data->ttls_version,
1356 identifier, NULL, out_data);
1357 }
1358
1359 retval = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
1360 if (retval)
1361 goto done;
1362
1363 continue_req:
1364 data->phase2_start = 0;
1365
1366 if (eap_ttls_parse_avps(in_decrypted, &parse) < 0) {
1367 retval = -1;
1368 goto done;
1369 }
1370
1371 retval = eap_ttls_process_decrypted(sm, data, ret, identifier,
1372 &parse, in_decrypted, out_data);
1373
1374 done:
1375 wpabuf_free(in_decrypted);
1376 os_free(parse.eapdata);
1377
1378 if (retval < 0) {
1379 ret->methodState = METHOD_DONE;
1380 ret->decision = DECISION_FAIL;
1381 }
1382
1383 return retval;
1384 }
1385
1386
eap_ttls_process_handshake(struct eap_sm * sm,struct eap_ttls_data * data,struct eap_method_ret * ret,u8 identifier,const u8 * in_data,size_t in_len,struct wpabuf ** out_data)1387 static int eap_ttls_process_handshake(struct eap_sm *sm,
1388 struct eap_ttls_data *data,
1389 struct eap_method_ret *ret,
1390 u8 identifier,
1391 const u8 *in_data, size_t in_len,
1392 struct wpabuf **out_data)
1393 {
1394 int res;
1395
1396 res = eap_peer_tls_process_helper(sm, &data->ssl, EAP_TYPE_TTLS,
1397 data->ttls_version, identifier,
1398 in_data, in_len, out_data);
1399
1400 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1401 wpa_printf(MSG_DEBUG, "EAP-TTLS: TLS done, proceed to "
1402 "Phase 2\n");
1403 if (data->resuming) {
1404 wpa_printf(MSG_DEBUG, "EAP-TTLS: fast reauth - may "
1405 "skip Phase 2\n");
1406 ret->decision = DECISION_COND_SUCC;
1407 ret->methodState = METHOD_MAY_CONT;
1408 }
1409 data->phase2_start = 1;
1410 eap_ttls_v0_derive_key(sm, data);
1411
1412 if (*out_data == NULL || wpabuf_len(*out_data) == 0) {
1413 if (eap_ttls_decrypt(sm, data, ret, identifier,
1414 NULL, out_data)) {
1415 wpa_printf(MSG_ERROR, "EAP-TTLS: "
1416 "failed to process early "
1417 "start for Phase 2\n");
1418 }
1419 res = 0;
1420 }
1421 data->resuming = 0;
1422 }
1423
1424 if (res == 2) {
1425 struct wpabuf msg;
1426 /*
1427 * Application data included in the handshake message.
1428 */
1429 wpabuf_free(data->pending_phase2_req);
1430 data->pending_phase2_req = *out_data;
1431 *out_data = NULL;
1432 wpabuf_set(&msg, in_data, in_len);
1433 res = eap_ttls_decrypt(sm, data, ret, identifier, &msg,
1434 out_data);
1435 }
1436
1437 return res;
1438 }
1439
1440
eap_ttls_check_auth_status(struct eap_sm * sm,struct eap_ttls_data * data,struct eap_method_ret * ret)1441 static void eap_ttls_check_auth_status(struct eap_sm *sm,
1442 struct eap_ttls_data *data,
1443 struct eap_method_ret *ret)
1444 {
1445 if (ret->methodState == METHOD_DONE) {
1446 ret->allowNotifications = FALSE;
1447 if (ret->decision == DECISION_UNCOND_SUCC ||
1448 ret->decision == DECISION_COND_SUCC) {
1449 wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1450 "completed successfully");
1451 data->phase2_success = 1;
1452 #ifdef EAP_TNC
1453 if (!data->ready_for_tnc && !data->tnc_started) {
1454 /*
1455 * TNC may be required as the next
1456 * authentication method within the tunnel.
1457 */
1458 ret->methodState = METHOD_MAY_CONT;
1459 data->ready_for_tnc = 1;
1460 }
1461 #endif /* EAP_TNC */
1462 }
1463 } else if (ret->methodState == METHOD_MAY_CONT &&
1464 (ret->decision == DECISION_UNCOND_SUCC ||
1465 ret->decision == DECISION_COND_SUCC)) {
1466 wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
1467 "completed successfully (MAY_CONT)\n");
1468 data->phase2_success = 1;
1469 }
1470 }
1471
1472
eap_ttls_process(struct eap_sm * sm,void * priv,struct eap_method_ret * ret,const struct wpabuf * reqData)1473 static struct wpabuf * eap_ttls_process(struct eap_sm *sm, void *priv,
1474 struct eap_method_ret *ret,
1475 const struct wpabuf *reqData)
1476 {
1477 size_t left;
1478 int res;
1479 u8 flags, id;
1480 struct wpabuf *resp;
1481 const u8 *pos;
1482 struct eap_ttls_data *data = priv;
1483
1484 pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_TTLS, ret,
1485 reqData, &left, &flags);
1486 if (pos == NULL)
1487 return NULL;
1488 id = eap_get_id(reqData);
1489
1490 if (flags & EAP_TLS_FLAGS_START) {
1491 wpa_printf(MSG_DEBUG, "EAP-TTLS: Start (server ver=%d, own "
1492 "ver=%d)\n", flags & EAP_TLS_VERSION_MASK,
1493 data->ttls_version);
1494
1495 /* RFC 5281, Ch. 9.2:
1496 * "This packet MAY contain additional information in the form
1497 * of AVPs, which may provide useful hints to the client"
1498 * For now, ignore any potential extra data.
1499 */
1500 left = 0;
1501 }
1502
1503 resp = NULL;
1504 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1505 !data->resuming) {
1506 struct wpabuf msg;
1507 wpabuf_set(&msg, pos, left);
1508 res = eap_ttls_decrypt(sm, data, ret, id, &msg, &resp);
1509 } else {
1510 res = eap_ttls_process_handshake(sm, data, ret, id,
1511 pos, left, &resp);
1512 }
1513
1514 eap_ttls_check_auth_status(sm, data, ret);
1515 /* FIX: what about res == -1? Could just move all error processing into
1516 * the other functions and get rid of this res==1 case here. */
1517 if (res == 1) {
1518 wpabuf_free(resp);
1519 return eap_peer_tls_build_ack(id, EAP_TYPE_TTLS,
1520 data->ttls_version);
1521 }
1522
1523 return resp;
1524 }
1525
1526
eap_ttls_has_reauth_data(struct eap_sm * sm,void * priv)1527 static bool eap_ttls_has_reauth_data(struct eap_sm *sm, void *priv)
1528 {
1529 struct eap_ttls_data *data = priv;
1530 return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1531 data->phase2_success;
1532 }
1533
1534
eap_ttls_deinit_for_reauth(struct eap_sm * sm,void * priv)1535 static void eap_ttls_deinit_for_reauth(struct eap_sm *sm, void *priv)
1536 {
1537 struct eap_ttls_data *data = priv;
1538 wpabuf_free(data->pending_phase2_req);
1539 data->pending_phase2_req = NULL;
1540 #ifdef EAP_TNC
1541 data->ready_for_tnc = 0;
1542 data->tnc_started = 0;
1543 #endif /* EAP_TNC */
1544 }
1545
1546
eap_ttls_init_for_reauth(struct eap_sm * sm,void * priv)1547 static void * eap_ttls_init_for_reauth(struct eap_sm *sm, void *priv)
1548 {
1549 struct eap_ttls_data *data = priv;
1550 os_free(data->key_data);
1551 data->key_data = NULL;
1552 os_free(data->session_id);
1553 data->session_id = NULL;
1554 if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
1555 os_free(data);
1556 return NULL;
1557 }
1558 if (data->phase2_priv && data->phase2_method &&
1559 data->phase2_method->init_for_reauth)
1560 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
1561 data->phase2_start = 0;
1562 data->phase2_success = 0;
1563 data->resuming = 1;
1564 data->reauth = 1;
1565 return priv;
1566 }
1567
1568
eap_ttls_get_status(struct eap_sm * sm,void * priv,char * buf,size_t buflen,int verbose)1569 static int eap_ttls_get_status(struct eap_sm *sm, void *priv, char *buf,
1570 size_t buflen, int verbose)
1571 {
1572 struct eap_ttls_data *data = priv;
1573 int len, ret;
1574
1575 len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
1576 ret = snprintf(buf + len, buflen - len,
1577 "EAP-TTLSv%d Phase2 method=",
1578 data->ttls_version);
1579 if (ret < 0 || (size_t) ret >= buflen - len)
1580 return len;
1581 len += ret;
1582 switch (data->phase2_type) {
1583 case EAP_TTLS_PHASE2_EAP:
1584 ret = os_snprintf(buf + len, buflen - len, "EAP-%s\n",
1585 data->phase2_method ?
1586 data->phase2_method->name : "?");
1587 break;
1588 case EAP_TTLS_PHASE2_MSCHAPV2:
1589 ret = snprintf(buf + len, buflen - len, "MSCHAPV2\n");
1590 break;
1591 case EAP_TTLS_PHASE2_MSCHAP:
1592 ret = os_snprintf(buf + len, buflen - len, "MSCHAP\n");
1593 break;
1594 case EAP_TTLS_PHASE2_PAP:
1595 ret = os_snprintf(buf + len, buflen - len, "PAP\n");
1596 break;
1597 case EAP_TTLS_PHASE2_CHAP:
1598 ret = os_snprintf(buf + len, buflen - len, "CHAP\n");
1599 break;
1600 default:
1601 ret = 0;
1602 break;
1603 }
1604 if (ret < 0 || (size_t) ret >= buflen - len)
1605 return len;
1606 len += ret;
1607
1608 return len;
1609 }
1610
1611
eap_ttls_isKeyAvailable(struct eap_sm * sm,void * priv)1612 static bool eap_ttls_isKeyAvailable(struct eap_sm *sm, void *priv)
1613 {
1614 struct eap_ttls_data *data = priv;
1615 return data->key_data != NULL && data->phase2_success;
1616 }
1617
1618
eap_ttls_getKey(struct eap_sm * sm,void * priv,size_t * len)1619 static u8 * eap_ttls_getKey(struct eap_sm *sm, void *priv, size_t *len)
1620 {
1621 struct eap_ttls_data *data = priv;
1622 u8 *key;
1623
1624 if (data->key_data == NULL || !data->phase2_success)
1625 return NULL;
1626
1627 key = os_malloc(EAP_TLS_KEY_LEN);
1628 if (key == NULL)
1629 return NULL;
1630
1631 *len = EAP_TLS_KEY_LEN;
1632 os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
1633
1634 return key;
1635 }
1636
1637
eap_ttls_get_session_id(struct eap_sm * sm,void * priv,size_t * len)1638 static u8 * eap_ttls_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
1639 {
1640 struct eap_ttls_data *data = priv;
1641 u8 *id;
1642
1643 if (data->session_id == NULL || !data->phase2_success)
1644 return NULL;
1645
1646 id = os_malloc(data->id_len);
1647 if (id == NULL)
1648 return NULL;
1649
1650 *len = data->id_len;
1651 os_memcpy(id, data->session_id, data->id_len);
1652
1653 return id;
1654 }
1655
1656
eap_ttls_get_emsk(struct eap_sm * sm,void * priv,size_t * len)1657 static u8 * eap_ttls_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
1658 {
1659 struct eap_ttls_data *data = priv;
1660 u8 *key;
1661
1662 if (data->key_data == NULL)
1663 return NULL;
1664
1665 key = os_malloc(EAP_EMSK_LEN);
1666 if (key == NULL)
1667 return NULL;
1668
1669 *len = EAP_EMSK_LEN;
1670 os_memcpy(key, data->key_data + EAP_TLS_KEY_LEN, EAP_EMSK_LEN);
1671
1672 return key;
1673 }
1674
1675
eap_peer_ttls_register(void)1676 int eap_peer_ttls_register(void)
1677 {
1678 struct eap_method *eap;
1679 int ret;
1680
1681 eap = eap_peer_method_alloc(EAP_VENDOR_IETF, EAP_TYPE_TTLS,
1682 "TTLS");
1683 if (eap == NULL)
1684 return -1;
1685
1686 eap->init = eap_ttls_init;
1687 eap->deinit = eap_ttls_deinit;
1688 eap->process = eap_ttls_process;
1689 eap->isKeyAvailable = eap_ttls_isKeyAvailable;
1690 eap->getKey = eap_ttls_getKey;
1691 eap->getSessionId = eap_ttls_get_session_id;
1692 eap->get_status = eap_ttls_get_status;
1693 eap->has_reauth_data = eap_ttls_has_reauth_data;
1694 eap->deinit_for_reauth = eap_ttls_deinit_for_reauth;
1695 eap->init_for_reauth = eap_ttls_init_for_reauth;
1696 eap->get_emsk = eap_ttls_get_emsk;
1697
1698 ret = eap_peer_method_register(eap);
1699 if (ret)
1700 eap_peer_method_free(eap);
1701 return ret;
1702 }
1703
1704 #endif /* EAP_TTLS */
1705