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