1 /*
2  * EAP peer method: EAP-MSCHAPV2 (draft-kamath-pppext-eap-mschapv2-00.txt)
3  * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  *
14  * This file implements EAP peer part of EAP-MSCHAPV2 method (EAP type 26).
15  * draft-kamath-pppext-eap-mschapv2-00.txt defines the Microsoft EAP CHAP
16  * Extensions Protocol, Version 2, for mutual authentication and key
17  * derivation. This encapsulates MS-CHAP-v2 protocol which is defined in
18  * RFC 2759. Use of EAP-MSCHAPV2 derived keys with MPPE cipher is described in
19  * RFC 3079.
20  */
21 
22 #include "includes.h"
23 
24 #include "common.h"
25 #include "crypto/ms_funcs.h"
26 #include "common/wpa_ctrl.h"
27 #include "mschapv2.h"
28 #include "eap_i.h"
29 #include "eap_config.h"
30 
31 
32 #ifdef _MSC_VER
33 #pragma pack(push, 1)
34 #endif /* _MSC_VER */
35 
36 struct eap_mschapv2_hdr {
37 	u8 op_code; /* MSCHAPV2_OP_* */
38 	u8 mschapv2_id; /* usually same as EAP identifier; must be changed
39 			 * for challenges, but not for success/failure */
40 	u8 ms_length[2]; /* Note: misaligned; length - 5 */
41 	/* followed by data */
42 } STRUCT_PACKED;
43 
44 /* Response Data field */
45 struct ms_response {
46 	u8 peer_challenge[MSCHAPV2_CHAL_LEN];
47 	u8 reserved[8];
48 	u8 nt_response[MSCHAPV2_NT_RESPONSE_LEN];
49 	u8 flags;
50 } STRUCT_PACKED;
51 
52 /* Change-Password Data field */
53 struct ms_change_password {
54 	u8 encr_password[516];
55 	u8 encr_hash[16];
56 	u8 peer_challenge[MSCHAPV2_CHAL_LEN];
57 	u8 reserved[8];
58 	u8 nt_response[MSCHAPV2_NT_RESPONSE_LEN];
59 	u8 flags[2];
60 } STRUCT_PACKED;
61 
62 #ifdef _MSC_VER
63 #pragma pack(pop)
64 #endif /* _MSC_VER */
65 
66 #define MSCHAPV2_OP_CHALLENGE 1
67 #define MSCHAPV2_OP_RESPONSE 2
68 #define MSCHAPV2_OP_SUCCESS 3
69 #define MSCHAPV2_OP_FAILURE 4
70 #define MSCHAPV2_OP_CHANGE_PASSWORD 7
71 
72 #define ERROR_RESTRICTED_LOGON_HOURS 646
73 #define ERROR_ACCT_DISABLED 647
74 #define ERROR_PASSWD_EXPIRED 648
75 #define ERROR_NO_DIALIN_PERMISSION 649
76 #define ERROR_AUTHENTICATION_FAILURE 691
77 #define ERROR_CHANGING_PASSWORD 709
78 
79 #define PASSWD_CHANGE_CHAL_LEN 16
80 #define MSCHAPV2_KEY_LEN 16
81 
82 
83 struct eap_mschapv2_data {
84 	u8 auth_response[MSCHAPV2_AUTH_RESPONSE_LEN];
85 	int auth_response_valid;
86 
87 	int prev_error;
88 	u8 passwd_change_challenge[PASSWD_CHANGE_CHAL_LEN];
89 	int passwd_change_challenge_valid;
90 	int passwd_change_version;
91 
92 	/* Optional challenge values generated in EAP-FAST Phase 1 negotiation
93 	 */
94 	u8 *peer_challenge;
95 	u8 *auth_challenge;
96 
97 	int phase2;
98 	u8 master_key[MSCHAPV2_MASTER_KEY_LEN];
99 	int master_key_valid;
100 	int success;
101 
102 	struct wpabuf *prev_challenge;
103 };
104 
105 
106 static void eap_mschapv2_deinit(struct eap_sm *sm, void *priv);
107 
108 
109 static void * eap_mschapv2_init(struct eap_sm *sm)
110 {
111 	struct eap_mschapv2_data *data;
112 	data = os_zalloc(sizeof(*data));
113 	if (data == NULL)
114 		return NULL;
115 
116 	if (sm->peer_challenge) {
117 		data->peer_challenge = os_malloc(MSCHAPV2_CHAL_LEN);
118 		if (data->peer_challenge == NULL) {
119 			eap_mschapv2_deinit(sm, data);
120 			return NULL;
121 		}
122 		os_memcpy(data->peer_challenge, sm->peer_challenge,
123 			  MSCHAPV2_CHAL_LEN);
124 	}
125 
126 	if (sm->auth_challenge) {
127 		data->auth_challenge = os_malloc(MSCHAPV2_CHAL_LEN);
128 		if (data->auth_challenge == NULL) {
129 			eap_mschapv2_deinit(sm, data);
130 			return NULL;
131 		}
132 		os_memcpy(data->auth_challenge, sm->auth_challenge,
133 			  MSCHAPV2_CHAL_LEN);
134 	}
135 
136 	data->phase2 = sm->init_phase2;
137 
138 	return data;
139 }
140 
141 
142 static void eap_mschapv2_deinit(struct eap_sm *sm, void *priv)
143 {
144 	struct eap_mschapv2_data *data = priv;
145 	os_free(data->peer_challenge);
146 	os_free(data->auth_challenge);
147 	wpabuf_free(data->prev_challenge);
148 	os_free(data);
149 }
150 
151 
152 static struct wpabuf * eap_mschapv2_challenge_reply(
153 	struct eap_sm *sm, struct eap_mschapv2_data *data, u8 id,
154 	u8 mschapv2_id, const u8 *auth_challenge)
155 {
156 	struct wpabuf *resp;
157 	struct eap_mschapv2_hdr *ms;
158 	u8 *peer_challenge;
159 	int ms_len;
160 	struct ms_response *r;
161 	size_t identity_len, password_len;
162 	const u8 *identity, *password;
163 	int pwhash;
164 
165 	wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Generating Challenge Response");
166 
167 	identity = eap_get_config_identity(sm, &identity_len);
168 	password = eap_get_config_password2(sm, &password_len, &pwhash);
169 	if (identity == NULL || password == NULL)
170 		return NULL;
171 
172 	ms_len = sizeof(*ms) + 1 + sizeof(*r) + identity_len;
173 	resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, ms_len,
174 			     EAP_CODE_RESPONSE, id);
175 	if (resp == NULL)
176 		return NULL;
177 
178 	ms = wpabuf_put(resp, sizeof(*ms));
179 	ms->op_code = MSCHAPV2_OP_RESPONSE;
180 	ms->mschapv2_id = mschapv2_id;
181 	if (data->prev_error) {
182 		/*
183 		 * TODO: this does not seem to be enough when processing two
184 		 * or more failure messages. IAS did not increment mschapv2_id
185 		 * in its own packets, but it seemed to expect the peer to
186 		 * increment this for all packets(?).
187 		 */
188 		ms->mschapv2_id++;
189 	}
190 	WPA_PUT_BE16(ms->ms_length, ms_len);
191 
192 	wpabuf_put_u8(resp, sizeof(*r)); /* Value-Size */
193 
194 	/* Response */
195 	r = wpabuf_put(resp, sizeof(*r));
196 	peer_challenge = r->peer_challenge;
197 	if (data->peer_challenge) {
198 		wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: peer_challenge generated "
199 			   "in Phase 1");
200 		peer_challenge = data->peer_challenge;
201 		os_memset(r->peer_challenge, 0, MSCHAPV2_CHAL_LEN);
202 	} else if (os_get_random(peer_challenge, MSCHAPV2_CHAL_LEN)) {
203 		wpabuf_free(resp);
204 		return NULL;
205 	}
206 	os_memset(r->reserved, 0, 8);
207 	if (data->auth_challenge) {
208 		wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: auth_challenge generated "
209 			   "in Phase 1");
210 		auth_challenge = data->auth_challenge;
211 	}
212 	if (mschapv2_derive_response(identity, identity_len, password,
213 				     password_len, pwhash, auth_challenge,
214 				     peer_challenge, r->nt_response,
215 				     data->auth_response, data->master_key)) {
216 		wpa_printf(MSG_ERROR, "EAP-MSCHAPV2: Failed to derive "
217 			   "response");
218 		wpabuf_free(resp);
219 		return NULL;
220 	}
221 	data->auth_response_valid = 1;
222 	data->master_key_valid = 1;
223 
224 	r->flags = 0; /* reserved, must be zero */
225 
226 	wpabuf_put_data(resp, identity, identity_len);
227 	wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: TX identifier %d mschapv2_id %d "
228 		   "(response)", id, ms->mschapv2_id);
229 	return resp;
230 }
231 
232 
233 /**
234  * eap_mschapv2_process - Process an EAP-MSCHAPv2 challenge message
235  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
236  * @data: Pointer to private EAP method data from eap_mschapv2_init()
237  * @ret: Return values from EAP request validation and processing
238  * @req: Pointer to EAP-MSCHAPv2 header from the request
239  * @req_len: Length of the EAP-MSCHAPv2 data
240  * @id: EAP identifier used in the request
241  * Returns: Pointer to allocated EAP response packet (eapRespData) or %NULL if
242  * no reply available
243  */
244 static struct wpabuf * eap_mschapv2_challenge(
245 	struct eap_sm *sm, struct eap_mschapv2_data *data,
246 	struct eap_method_ret *ret, const struct eap_mschapv2_hdr *req,
247 	size_t req_len, u8 id)
248 {
249 	size_t len, challenge_len;
250 	const u8 *pos, *challenge;
251 
252 	if (eap_get_config_identity(sm, &len) == NULL ||
253 	    eap_get_config_password(sm, &len) == NULL)
254 		return NULL;
255 
256 	wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Received challenge");
257 	if (req_len < sizeof(*req) + 1) {
258 		wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Too short challenge data "
259 			   "(len %lu)", (unsigned long) req_len);
260 		ret->ignore = TRUE;
261 		return NULL;
262 	}
263 	pos = (const u8 *) (req + 1);
264 	challenge_len = *pos++;
265 	len = req_len - sizeof(*req) - 1;
266 	if (challenge_len != MSCHAPV2_CHAL_LEN) {
267 		wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Invalid challenge length "
268 			   "%lu", (unsigned long) challenge_len);
269 		ret->ignore = TRUE;
270 		return NULL;
271 	}
272 
273 	if (len < challenge_len) {
274 		wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Too short challenge"
275 			   " packet: len=%lu challenge_len=%lu",
276 			   (unsigned long) len, (unsigned long) challenge_len);
277 		ret->ignore = TRUE;
278 		return NULL;
279 	}
280 
281 	if (data->passwd_change_challenge_valid) {
282 		wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Using challenge from the "
283 			   "failure message");
284 		challenge = data->passwd_change_challenge;
285 	} else
286 		challenge = pos;
287 	pos += challenge_len;
288 	len -= challenge_len;
289 	wpa_hexdump_ascii(MSG_DEBUG, "EAP-MSCHAPV2: Authentication Servername",
290 		    pos, len);
291 
292 	ret->ignore = FALSE;
293 	ret->methodState = METHOD_MAY_CONT;
294 	ret->decision = DECISION_FAIL;
295 	ret->allowNotifications = TRUE;
296 
297 	return eap_mschapv2_challenge_reply(sm, data, id, req->mschapv2_id,
298 					    challenge);
299 }
300 
301 
302 static void eap_mschapv2_password_changed(struct eap_sm *sm,
303 					  struct eap_mschapv2_data *data)
304 {
305 	struct eap_peer_config *config = eap_get_config(sm);
306 	if (config && config->new_password) {
307 		wpa_msg(sm->msg_ctx, MSG_INFO,
308 			WPA_EVENT_PASSWORD_CHANGED
309 			"EAP-MSCHAPV2: Password changed successfully");
310 		data->prev_error = 0;
311 		os_free(config->password);
312 		if (config->flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH) {
313 			config->password = os_malloc(16);
314 			config->password_len = 16;
315 			if (config->password) {
316 				nt_password_hash(config->new_password,
317 						 config->new_password_len,
318 						 config->password);
319 			}
320 			os_free(config->new_password);
321 		} else {
322 			config->password = config->new_password;
323 			config->password_len = config->new_password_len;
324 		}
325 		config->new_password = NULL;
326 		config->new_password_len = 0;
327 	}
328 }
329 
330 
331 /**
332  * eap_mschapv2_process - Process an EAP-MSCHAPv2 success message
333  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
334  * @data: Pointer to private EAP method data from eap_mschapv2_init()
335  * @ret: Return values from EAP request validation and processing
336  * @req: Pointer to EAP-MSCHAPv2 header from the request
337  * @req_len: Length of the EAP-MSCHAPv2 data
338  * @id: EAP identifier used in th erequest
339  * Returns: Pointer to allocated EAP response packet (eapRespData) or %NULL if
340  * no reply available
341  */
342 static struct wpabuf * eap_mschapv2_success(struct eap_sm *sm,
343 					    struct eap_mschapv2_data *data,
344 					    struct eap_method_ret *ret,
345 					    const struct eap_mschapv2_hdr *req,
346 					    size_t req_len, u8 id)
347 {
348 	struct wpabuf *resp;
349 	const u8 *pos;
350 	size_t len;
351 
352 	wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Received success");
353 	len = req_len - sizeof(*req);
354 	pos = (const u8 *) (req + 1);
355 	if (!data->auth_response_valid ||
356 	    mschapv2_verify_auth_response(data->auth_response, pos, len)) {
357 		wpa_printf(MSG_WARNING, "EAP-MSCHAPV2: Invalid authenticator "
358 			   "response in success request");
359 		ret->methodState = METHOD_DONE;
360 		ret->decision = DECISION_FAIL;
361 		return NULL;
362 	}
363 	pos += 2 + 2 * MSCHAPV2_AUTH_RESPONSE_LEN;
364 	len -= 2 + 2 * MSCHAPV2_AUTH_RESPONSE_LEN;
365 	while (len > 0 && *pos == ' ') {
366 		pos++;
367 		len--;
368 	}
369 	wpa_hexdump_ascii(MSG_DEBUG, "EAP-MSCHAPV2: Success message",
370 			  pos, len);
371 	wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Authentication succeeded");
372 
373 	/* Note: Only op_code of the EAP-MSCHAPV2 header is included in success
374 	 * message. */
375 	resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, 1,
376 			     EAP_CODE_RESPONSE, id);
377 	if (resp == NULL) {
378 		wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Failed to allocate "
379 			   "buffer for success response");
380 		ret->ignore = TRUE;
381 		return NULL;
382 	}
383 
384 	wpabuf_put_u8(resp, MSCHAPV2_OP_SUCCESS); /* op_code */
385 
386 	ret->methodState = METHOD_DONE;
387 	ret->decision = DECISION_UNCOND_SUCC;
388 	ret->allowNotifications = FALSE;
389 	data->success = 1;
390 
391 	if (data->prev_error == ERROR_PASSWD_EXPIRED)
392 		eap_mschapv2_password_changed(sm, data);
393 
394 	return resp;
395 }
396 
397 
398 static int eap_mschapv2_failure_txt(struct eap_sm *sm,
399 				    struct eap_mschapv2_data *data, char *txt)
400 {
401 	char *pos, *msg = "";
402 	int retry = 1;
403 	struct eap_peer_config *config = eap_get_config(sm);
404 
405 	/* For example:
406 	 * E=691 R=1 C=<32 octets hex challenge> V=3 M=Authentication Failure
407 	 */
408 
409 	pos = txt;
410 
411 	if (pos && os_strncmp(pos, "E=", 2) == 0) {
412 		pos += 2;
413 		data->prev_error = atoi(pos);
414 		wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: error %d",
415 			   data->prev_error);
416 		pos = os_strchr(pos, ' ');
417 		if (pos)
418 			pos++;
419 	}
420 
421 	if (pos && os_strncmp(pos, "R=", 2) == 0) {
422 		pos += 2;
423 		retry = atoi(pos);
424 		wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: retry is %sallowed",
425 			   retry == 1 ? "" : "not ");
426 		pos = os_strchr(pos, ' ');
427 		if (pos)
428 			pos++;
429 	}
430 
431 	if (pos && os_strncmp(pos, "C=", 2) == 0) {
432 		int hex_len;
433 		pos += 2;
434 		hex_len = os_strchr(pos, ' ') - (char *) pos;
435 		if (hex_len == PASSWD_CHANGE_CHAL_LEN * 2) {
436 			if (hexstr2bin(pos, data->passwd_change_challenge,
437 				       PASSWD_CHANGE_CHAL_LEN)) {
438 				wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: invalid "
439 					   "failure challenge");
440 			} else {
441 				wpa_hexdump(MSG_DEBUG, "EAP-MSCHAPV2: failure "
442 					    "challenge",
443 					    data->passwd_change_challenge,
444 					    PASSWD_CHANGE_CHAL_LEN);
445 				data->passwd_change_challenge_valid = 1;
446 			}
447 		} else {
448 			wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: invalid failure "
449 				   "challenge len %d", hex_len);
450 		}
451 		pos = os_strchr(pos, ' ');
452 		if (pos)
453 			pos++;
454 	} else {
455 		wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: required challenge field "
456 			   "was not present in failure message");
457 	}
458 
459 	if (pos && os_strncmp(pos, "V=", 2) == 0) {
460 		pos += 2;
461 		data->passwd_change_version = atoi(pos);
462 		wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: password changing "
463 			   "protocol version %d", data->passwd_change_version);
464 		pos = os_strchr(pos, ' ');
465 		if (pos)
466 			pos++;
467 	}
468 
469 	if (pos && os_strncmp(pos, "M=", 2) == 0) {
470 		pos += 2;
471 		msg = pos;
472 	}
473 	wpa_msg(sm->msg_ctx, MSG_WARNING,
474 		"EAP-MSCHAPV2: failure message: '%s' (retry %sallowed, error "
475 		"%d)",
476 		msg, retry == 1 ? "" : "not ", data->prev_error);
477 	if (data->prev_error == ERROR_PASSWD_EXPIRED &&
478 	    data->passwd_change_version == 3 && config) {
479 		if (config->new_password == NULL) {
480 			wpa_msg(sm->msg_ctx, MSG_INFO,
481 				"EAP-MSCHAPV2: Password expired - password "
482 				"change required");
483 			eap_sm_request_new_password(sm);
484 		}
485 	} else if (retry == 1 && config) {
486 		/* TODO: could prevent the current password from being used
487 		 * again at least for some period of time */
488 		if (!config->mschapv2_retry)
489 			eap_sm_request_identity(sm);
490 		eap_sm_request_password(sm);
491 		config->mschapv2_retry = 1;
492 	} else if (config) {
493 		/* TODO: prevent retries using same username/password */
494 		config->mschapv2_retry = 0;
495 	}
496 
497 	return retry == 1;
498 }
499 
500 
501 static struct wpabuf * eap_mschapv2_change_password(
502 	struct eap_sm *sm, struct eap_mschapv2_data *data,
503 	struct eap_method_ret *ret, const struct eap_mschapv2_hdr *req, u8 id)
504 {
505 	struct wpabuf *resp;
506 	int ms_len;
507 	const u8 *username, *password, *new_password;
508 	size_t username_len, password_len, new_password_len;
509 	struct eap_mschapv2_hdr *ms;
510 	struct ms_change_password *cp;
511 	u8 password_hash[16], password_hash_hash[16];
512 	int pwhash;
513 
514 	username = eap_get_config_identity(sm, &username_len);
515 	password = eap_get_config_password2(sm, &password_len, &pwhash);
516 	new_password = eap_get_config_new_password(sm, &new_password_len);
517 	if (username == NULL || password == NULL || new_password == NULL)
518 		return NULL;
519 
520 	username = mschapv2_remove_domain(username, &username_len);
521 
522 	ret->ignore = FALSE;
523 	ret->methodState = METHOD_MAY_CONT;
524 	ret->decision = DECISION_COND_SUCC;
525 	ret->allowNotifications = TRUE;
526 
527 	ms_len = sizeof(*ms) + sizeof(*cp);
528 	resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, ms_len,
529 			     EAP_CODE_RESPONSE, id);
530 	if (resp == NULL)
531 		return NULL;
532 
533 	ms = wpabuf_put(resp, sizeof(*ms));
534 	ms->op_code = MSCHAPV2_OP_CHANGE_PASSWORD;
535 	ms->mschapv2_id = req->mschapv2_id + 1;
536 	WPA_PUT_BE16(ms->ms_length, ms_len);
537 	cp = wpabuf_put(resp, sizeof(*cp));
538 
539 	/* Encrypted-Password */
540 	if (pwhash) {
541 		if (encrypt_pw_block_with_password_hash(
542 			    new_password, new_password_len,
543 			    password, cp->encr_password))
544 			goto fail;
545 	} else {
546 		if (new_password_encrypted_with_old_nt_password_hash(
547 			    new_password, new_password_len,
548 			    password, password_len, cp->encr_password))
549 			goto fail;
550 	}
551 
552 	/* Encrypted-Hash */
553 	if (pwhash) {
554 		u8 new_password_hash[16];
555 		nt_password_hash(new_password, new_password_len,
556 				 new_password_hash);
557 		nt_password_hash_encrypted_with_block(password,
558 						      new_password_hash,
559 						      cp->encr_hash);
560 	} else {
561 		old_nt_password_hash_encrypted_with_new_nt_password_hash(
562 			new_password, new_password_len,
563 			password, password_len, cp->encr_hash);
564 	}
565 
566 	/* Peer-Challenge */
567 	if (os_get_random(cp->peer_challenge, MSCHAPV2_CHAL_LEN))
568 		goto fail;
569 
570 	/* Reserved, must be zero */
571 	os_memset(cp->reserved, 0, 8);
572 
573 	/* NT-Response */
574 	wpa_hexdump(MSG_DEBUG, "EAP-MSCHAPV2: auth_challenge",
575 		    data->passwd_change_challenge, PASSWD_CHANGE_CHAL_LEN);
576 	wpa_hexdump(MSG_DEBUG, "EAP-MSCHAPV2: peer_challenge",
577 		    cp->peer_challenge, MSCHAPV2_CHAL_LEN);
578 	wpa_hexdump_ascii(MSG_DEBUG, "EAP-MSCHAPV2: username",
579 			  username, username_len);
580 	wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-MSCHAPV2: new password",
581 			      new_password, new_password_len);
582 	generate_nt_response(data->passwd_change_challenge, cp->peer_challenge,
583 			     username, username_len,
584 			     new_password, new_password_len,
585 			     cp->nt_response);
586 	wpa_hexdump(MSG_DEBUG, "EAP-MSCHAPV2: NT-Response",
587 		    cp->nt_response, MSCHAPV2_NT_RESPONSE_LEN);
588 
589 	/* Authenticator response is not really needed yet, but calculate it
590 	 * here so that challenges need not be saved. */
591 	generate_authenticator_response(new_password, new_password_len,
592 					cp->peer_challenge,
593 					data->passwd_change_challenge,
594 					username, username_len,
595 					cp->nt_response, data->auth_response);
596 	data->auth_response_valid = 1;
597 
598 	/* Likewise, generate master_key here since we have the needed data
599 	 * available. */
600 	nt_password_hash(new_password, new_password_len, password_hash);
601 	hash_nt_password_hash(password_hash, password_hash_hash);
602 	get_master_key(password_hash_hash, cp->nt_response, data->master_key);
603 	data->master_key_valid = 1;
604 
605 	/* Flags */
606 	os_memset(cp->flags, 0, 2);
607 
608 	wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: TX identifier %d mschapv2_id %d "
609 		   "(change pw)", id, ms->mschapv2_id);
610 
611 	return resp;
612 
613 fail:
614 	wpabuf_free(resp);
615 	return NULL;
616 }
617 
618 
619 /**
620  * eap_mschapv2_process - Process an EAP-MSCHAPv2 failure message
621  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
622  * @data: Pointer to private EAP method data from eap_mschapv2_init()
623  * @ret: Return values from EAP request validation and processing
624  * @req: Pointer to EAP-MSCHAPv2 header from the request
625  * @req_len: Length of the EAP-MSCHAPv2 data
626  * @id: EAP identifier used in th erequest
627  * Returns: Pointer to allocated EAP response packet (eapRespData) or %NULL if
628  * no reply available
629  */
630 static struct wpabuf * eap_mschapv2_failure(struct eap_sm *sm,
631 					    struct eap_mschapv2_data *data,
632 					    struct eap_method_ret *ret,
633 					    const struct eap_mschapv2_hdr *req,
634 					    size_t req_len, u8 id)
635 {
636 	struct wpabuf *resp;
637 	const u8 *msdata = (const u8 *) (req + 1);
638 	char *buf;
639 	size_t len = req_len - sizeof(*req);
640 	int retry = 0;
641 
642 	wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Received failure");
643 	wpa_hexdump_ascii(MSG_DEBUG, "EAP-MSCHAPV2: Failure data",
644 			  msdata, len);
645 	/*
646 	 * eap_mschapv2_failure_txt() expects a nul terminated string, so we
647 	 * must allocate a large enough temporary buffer to create that since
648 	 * the received message does not include nul termination.
649 	 */
650 	buf = os_malloc(len + 1);
651 	if (buf) {
652 		os_memcpy(buf, msdata, len);
653 		buf[len] = '\0';
654 		retry = eap_mschapv2_failure_txt(sm, data, buf);
655 		os_free(buf);
656 	}
657 
658 	ret->ignore = FALSE;
659 	ret->methodState = METHOD_DONE;
660 	ret->decision = DECISION_FAIL;
661 	ret->allowNotifications = FALSE;
662 
663 	if (data->prev_error == ERROR_PASSWD_EXPIRED &&
664 	    data->passwd_change_version == 3) {
665 		struct eap_peer_config *config = eap_get_config(sm);
666 		if (config && config->new_password)
667 			return eap_mschapv2_change_password(sm, data, ret, req,
668 							    id);
669 		if (config && config->pending_req_new_password)
670 			return NULL;
671 	} else if (retry && data->prev_error == ERROR_AUTHENTICATION_FAILURE) {
672 		/* TODO: could try to retry authentication, e.g, after having
673 		 * changed the username/password. In this case, EAP MS-CHAP-v2
674 		 * Failure Response would not be sent here. */
675 		return NULL;
676 	}
677 
678 	/* Note: Only op_code of the EAP-MSCHAPV2 header is included in failure
679 	 * message. */
680 	resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, 1,
681 			     EAP_CODE_RESPONSE, id);
682 	if (resp == NULL)
683 		return NULL;
684 
685 	wpabuf_put_u8(resp, MSCHAPV2_OP_FAILURE); /* op_code */
686 
687 	return resp;
688 }
689 
690 
691 static int eap_mschapv2_check_config(struct eap_sm *sm)
692 {
693 	size_t len;
694 
695 	if (eap_get_config_identity(sm, &len) == NULL) {
696 		wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Identity not configured");
697 		eap_sm_request_identity(sm);
698 		return -1;
699 	}
700 
701 	if (eap_get_config_password(sm, &len) == NULL) {
702 		wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Password not configured");
703 		eap_sm_request_password(sm);
704 		return -1;
705 	}
706 
707 	return 0;
708 }
709 
710 
711 static int eap_mschapv2_check_mslen(struct eap_sm *sm, size_t len,
712 				    const struct eap_mschapv2_hdr *ms)
713 {
714 	size_t ms_len = WPA_GET_BE16(ms->ms_length);
715 
716 	if (ms_len == len)
717 		return 0;
718 
719 	wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Invalid header: len=%lu "
720 		   "ms_len=%lu", (unsigned long) len, (unsigned long) ms_len);
721 	if (sm->workaround) {
722 		/* Some authentication servers use invalid ms_len,
723 		 * ignore it for interoperability. */
724 		wpa_printf(MSG_INFO, "EAP-MSCHAPV2: workaround, ignore"
725 			   " invalid ms_len %lu (len %lu)",
726 			   (unsigned long) ms_len,
727 			   (unsigned long) len);
728 		return 0;
729 	}
730 
731 	return -1;
732 }
733 
734 
735 static void eap_mschapv2_copy_challenge(struct eap_mschapv2_data *data,
736 					const struct wpabuf *reqData)
737 {
738 	/*
739 	 * Store a copy of the challenge message, so that it can be processed
740 	 * again in case retry is allowed after a possible failure.
741 	 */
742 	wpabuf_free(data->prev_challenge);
743 	data->prev_challenge = wpabuf_dup(reqData);
744 }
745 
746 
747 /**
748  * eap_mschapv2_process - Process an EAP-MSCHAPv2 request
749  * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
750  * @priv: Pointer to private EAP method data from eap_mschapv2_init()
751  * @ret: Return values from EAP request validation and processing
752  * @reqData: EAP request to be processed (eapReqData)
753  * Returns: Pointer to allocated EAP response packet (eapRespData) or %NULL if
754  * no reply available
755  */
756 static struct wpabuf * eap_mschapv2_process(struct eap_sm *sm, void *priv,
757 					    struct eap_method_ret *ret,
758 					    const struct wpabuf *reqData)
759 {
760 	struct eap_mschapv2_data *data = priv;
761 	struct eap_peer_config *config = eap_get_config(sm);
762 	const struct eap_mschapv2_hdr *ms;
763 	int using_prev_challenge = 0;
764 	const u8 *pos;
765 	size_t len;
766 	u8 id;
767 
768 	if (eap_mschapv2_check_config(sm)) {
769 		ret->ignore = TRUE;
770 		return NULL;
771 	}
772 
773 	if (config->mschapv2_retry && data->prev_challenge &&
774 	    data->prev_error == ERROR_AUTHENTICATION_FAILURE) {
775 		wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Replacing pending packet "
776 			   "with the previous challenge");
777 
778 		reqData = data->prev_challenge;
779 		using_prev_challenge = 1;
780 		config->mschapv2_retry = 0;
781 	}
782 
783 	pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, reqData,
784 			       &len);
785 	if (pos == NULL || len < sizeof(*ms) + 1) {
786 		ret->ignore = TRUE;
787 		return NULL;
788 	}
789 
790 	ms = (const struct eap_mschapv2_hdr *) pos;
791 	if (eap_mschapv2_check_mslen(sm, len, ms)) {
792 		ret->ignore = TRUE;
793 		return NULL;
794 	}
795 
796 	id = eap_get_id(reqData);
797 	wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: RX identifier %d mschapv2_id %d",
798 		   id, ms->mschapv2_id);
799 
800 	switch (ms->op_code) {
801 	case MSCHAPV2_OP_CHALLENGE:
802 		if (!using_prev_challenge)
803 			eap_mschapv2_copy_challenge(data, reqData);
804 		return eap_mschapv2_challenge(sm, data, ret, ms, len, id);
805 	case MSCHAPV2_OP_SUCCESS:
806 		return eap_mschapv2_success(sm, data, ret, ms, len, id);
807 	case MSCHAPV2_OP_FAILURE:
808 		return eap_mschapv2_failure(sm, data, ret, ms, len, id);
809 	default:
810 		wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Unknown op %d - ignored",
811 			   ms->op_code);
812 		ret->ignore = TRUE;
813 		return NULL;
814 	}
815 }
816 
817 
818 static Boolean eap_mschapv2_isKeyAvailable(struct eap_sm *sm, void *priv)
819 {
820 	struct eap_mschapv2_data *data = priv;
821 	return data->success && data->master_key_valid;
822 }
823 
824 
825 static u8 * eap_mschapv2_getKey(struct eap_sm *sm, void *priv, size_t *len)
826 {
827 	struct eap_mschapv2_data *data = priv;
828 	u8 *key;
829 	int key_len;
830 
831 	if (!data->master_key_valid || !data->success)
832 		return NULL;
833 
834 	key_len = 2 * MSCHAPV2_KEY_LEN;
835 
836 	key = os_malloc(key_len);
837 	if (key == NULL)
838 		return NULL;
839 
840 	/* MSK = server MS-MPPE-Recv-Key | MS-MPPE-Send-Key, i.e.,
841 	 *	peer MS-MPPE-Send-Key | MS-MPPE-Recv-Key */
842 	get_asymetric_start_key(data->master_key, key, MSCHAPV2_KEY_LEN, 1, 0);
843 	get_asymetric_start_key(data->master_key, key + MSCHAPV2_KEY_LEN,
844 				MSCHAPV2_KEY_LEN, 0, 0);
845 
846 	wpa_hexdump_key(MSG_DEBUG, "EAP-MSCHAPV2: Derived key",
847 			key, key_len);
848 
849 	*len = key_len;
850 	return key;
851 }
852 
853 
854 /**
855  * eap_peer_mschapv2_register - Register EAP-MSCHAPv2 peer method
856  * Returns: 0 on success, -1 on failure
857  *
858  * This function is used to register EAP-MSCHAPv2 peer method into the EAP
859  * method list.
860  */
861 int eap_peer_mschapv2_register(void)
862 {
863 	struct eap_method *eap;
864 	int ret;
865 
866 	eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
867 				    EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2,
868 				    "MSCHAPV2");
869 	if (eap == NULL)
870 		return -1;
871 
872 	eap->init = eap_mschapv2_init;
873 	eap->deinit = eap_mschapv2_deinit;
874 	eap->process = eap_mschapv2_process;
875 	eap->isKeyAvailable = eap_mschapv2_isKeyAvailable;
876 	eap->getKey = eap_mschapv2_getKey;
877 
878 	ret = eap_peer_method_register(eap);
879 	if (ret)
880 		eap_peer_method_free(eap);
881 	return ret;
882 }
883