eap_mschapv2.c revision 43cb578dfe2c492257636f6234a24178ed27789e
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 software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 *
8 * This file implements EAP peer part of EAP-MSCHAPV2 method (EAP type 26).
9 * draft-kamath-pppext-eap-mschapv2-00.txt defines the Microsoft EAP CHAP
10 * Extensions Protocol, Version 2, for mutual authentication and key
11 * derivation. This encapsulates MS-CHAP-v2 protocol which is defined in
12 * RFC 2759. Use of EAP-MSCHAPV2 derived keys with MPPE cipher is described in
13 * RFC 3079.
14 */
15
16#include "includes.h"
17
18#include "common.h"
19#include "crypto/ms_funcs.h"
20#include "crypto/random.h"
21#include "common/wpa_ctrl.h"
22#include "mschapv2.h"
23#include "eap_i.h"
24#include "eap_config.h"
25
26
27#ifdef _MSC_VER
28#pragma pack(push, 1)
29#endif /* _MSC_VER */
30
31struct eap_mschapv2_hdr {
32	u8 op_code; /* MSCHAPV2_OP_* */
33	u8 mschapv2_id; /* usually same as EAP identifier; must be changed
34			 * for challenges, but not for success/failure */
35	u8 ms_length[2]; /* Note: misaligned; length - 5 */
36	/* followed by data */
37} STRUCT_PACKED;
38
39/* Response Data field */
40struct ms_response {
41	u8 peer_challenge[MSCHAPV2_CHAL_LEN];
42	u8 reserved[8];
43	u8 nt_response[MSCHAPV2_NT_RESPONSE_LEN];
44	u8 flags;
45} STRUCT_PACKED;
46
47/* Change-Password Data field */
48struct ms_change_password {
49	u8 encr_password[516];
50	u8 encr_hash[16];
51	u8 peer_challenge[MSCHAPV2_CHAL_LEN];
52	u8 reserved[8];
53	u8 nt_response[MSCHAPV2_NT_RESPONSE_LEN];
54	u8 flags[2];
55} STRUCT_PACKED;
56
57#ifdef _MSC_VER
58#pragma pack(pop)
59#endif /* _MSC_VER */
60
61#define MSCHAPV2_OP_CHALLENGE 1
62#define MSCHAPV2_OP_RESPONSE 2
63#define MSCHAPV2_OP_SUCCESS 3
64#define MSCHAPV2_OP_FAILURE 4
65#define MSCHAPV2_OP_CHANGE_PASSWORD 7
66
67#define ERROR_RESTRICTED_LOGON_HOURS 646
68#define ERROR_ACCT_DISABLED 647
69#define ERROR_PASSWD_EXPIRED 648
70#define ERROR_NO_DIALIN_PERMISSION 649
71#define ERROR_AUTHENTICATION_FAILURE 691
72#define ERROR_CHANGING_PASSWORD 709
73
74#define PASSWD_CHANGE_CHAL_LEN 16
75#define MSCHAPV2_KEY_LEN 16
76
77
78struct eap_mschapv2_data {
79	u8 auth_response[MSCHAPV2_AUTH_RESPONSE_LEN];
80	int auth_response_valid;
81
82	int prev_error;
83	u8 passwd_change_challenge[PASSWD_CHANGE_CHAL_LEN];
84	int passwd_change_challenge_valid;
85	int passwd_change_version;
86
87	/* Optional challenge values generated in EAP-FAST Phase 1 negotiation
88	 */
89	u8 *peer_challenge;
90	u8 *auth_challenge;
91
92	int phase2;
93	u8 master_key[MSCHAPV2_MASTER_KEY_LEN];
94	int master_key_valid;
95	int success;
96
97	struct wpabuf *prev_challenge;
98};
99
100
101static void eap_mschapv2_deinit(struct eap_sm *sm, void *priv);
102
103
104static void * eap_mschapv2_init(struct eap_sm *sm)
105{
106	struct eap_mschapv2_data *data;
107	data = os_zalloc(sizeof(*data));
108	if (data == NULL)
109		return NULL;
110
111	if (sm->peer_challenge) {
112		data->peer_challenge = os_malloc(MSCHAPV2_CHAL_LEN);
113		if (data->peer_challenge == NULL) {
114			eap_mschapv2_deinit(sm, data);
115			return NULL;
116		}
117		os_memcpy(data->peer_challenge, sm->peer_challenge,
118			  MSCHAPV2_CHAL_LEN);
119	}
120
121	if (sm->auth_challenge) {
122		data->auth_challenge = os_malloc(MSCHAPV2_CHAL_LEN);
123		if (data->auth_challenge == NULL) {
124			eap_mschapv2_deinit(sm, data);
125			return NULL;
126		}
127		os_memcpy(data->auth_challenge, sm->auth_challenge,
128			  MSCHAPV2_CHAL_LEN);
129	}
130
131	data->phase2 = sm->init_phase2;
132
133	return data;
134}
135
136
137static void eap_mschapv2_deinit(struct eap_sm *sm, void *priv)
138{
139	struct eap_mschapv2_data *data = priv;
140	os_free(data->peer_challenge);
141	os_free(data->auth_challenge);
142	wpabuf_free(data->prev_challenge);
143	os_free(data);
144}
145
146
147static struct wpabuf * eap_mschapv2_challenge_reply(
148	struct eap_sm *sm, struct eap_mschapv2_data *data, u8 id,
149	u8 mschapv2_id, const u8 *auth_challenge)
150{
151	struct wpabuf *resp;
152	struct eap_mschapv2_hdr *ms;
153	u8 *peer_challenge;
154	int ms_len;
155	struct ms_response *r;
156	size_t identity_len, password_len;
157	const u8 *identity, *password;
158	int pwhash;
159
160	wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Generating Challenge Response");
161
162	identity = eap_get_config_identity(sm, &identity_len);
163	password = eap_get_config_password2(sm, &password_len, &pwhash);
164	if (identity == NULL || password == NULL)
165		return NULL;
166
167	ms_len = sizeof(*ms) + 1 + sizeof(*r) + identity_len;
168	resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, ms_len,
169			     EAP_CODE_RESPONSE, id);
170	if (resp == NULL)
171		return NULL;
172
173	ms = wpabuf_put(resp, sizeof(*ms));
174	ms->op_code = MSCHAPV2_OP_RESPONSE;
175	ms->mschapv2_id = mschapv2_id;
176	if (data->prev_error) {
177		/*
178		 * TODO: this does not seem to be enough when processing two
179		 * or more failure messages. IAS did not increment mschapv2_id
180		 * in its own packets, but it seemed to expect the peer to
181		 * increment this for all packets(?).
182		 */
183		ms->mschapv2_id++;
184	}
185	WPA_PUT_BE16(ms->ms_length, ms_len);
186
187	wpabuf_put_u8(resp, sizeof(*r)); /* Value-Size */
188
189	/* Response */
190	r = wpabuf_put(resp, sizeof(*r));
191	peer_challenge = r->peer_challenge;
192	if (data->peer_challenge) {
193		wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: peer_challenge generated "
194			   "in Phase 1");
195		peer_challenge = data->peer_challenge;
196		os_memset(r->peer_challenge, 0, MSCHAPV2_CHAL_LEN);
197	} else if (random_get_bytes(peer_challenge, MSCHAPV2_CHAL_LEN)) {
198		wpabuf_free(resp);
199		return NULL;
200	}
201	os_memset(r->reserved, 0, 8);
202	if (data->auth_challenge) {
203		wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: auth_challenge generated "
204			   "in Phase 1");
205		auth_challenge = data->auth_challenge;
206	}
207	if (mschapv2_derive_response(identity, identity_len, password,
208				     password_len, pwhash, auth_challenge,
209				     peer_challenge, r->nt_response,
210				     data->auth_response, data->master_key)) {
211		wpa_printf(MSG_ERROR, "EAP-MSCHAPV2: Failed to derive "
212			   "response");
213		wpabuf_free(resp);
214		return NULL;
215	}
216	data->auth_response_valid = 1;
217	data->master_key_valid = 1;
218
219	r->flags = 0; /* reserved, must be zero */
220
221	wpabuf_put_data(resp, identity, identity_len);
222	wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: TX identifier %d mschapv2_id %d "
223		   "(response)", id, ms->mschapv2_id);
224	return resp;
225}
226
227
228/**
229 * eap_mschapv2_process - Process an EAP-MSCHAPv2 challenge message
230 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
231 * @data: Pointer to private EAP method data from eap_mschapv2_init()
232 * @ret: Return values from EAP request validation and processing
233 * @req: Pointer to EAP-MSCHAPv2 header from the request
234 * @req_len: Length of the EAP-MSCHAPv2 data
235 * @id: EAP identifier used in the request
236 * Returns: Pointer to allocated EAP response packet (eapRespData) or %NULL if
237 * no reply available
238 */
239static struct wpabuf * eap_mschapv2_challenge(
240	struct eap_sm *sm, struct eap_mschapv2_data *data,
241	struct eap_method_ret *ret, const struct eap_mschapv2_hdr *req,
242	size_t req_len, u8 id)
243{
244	size_t len, challenge_len;
245	const u8 *pos, *challenge;
246
247	if (eap_get_config_identity(sm, &len) == NULL ||
248	    eap_get_config_password(sm, &len) == NULL)
249		return NULL;
250
251	wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Received challenge");
252	if (req_len < sizeof(*req) + 1) {
253		wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Too short challenge data "
254			   "(len %lu)", (unsigned long) req_len);
255		ret->ignore = TRUE;
256		return NULL;
257	}
258	pos = (const u8 *) (req + 1);
259	challenge_len = *pos++;
260	len = req_len - sizeof(*req) - 1;
261	if (challenge_len != MSCHAPV2_CHAL_LEN) {
262		wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Invalid challenge length "
263			   "%lu", (unsigned long) challenge_len);
264		ret->ignore = TRUE;
265		return NULL;
266	}
267
268	if (len < challenge_len) {
269		wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Too short challenge"
270			   " packet: len=%lu challenge_len=%lu",
271			   (unsigned long) len, (unsigned long) challenge_len);
272		ret->ignore = TRUE;
273		return NULL;
274	}
275
276	if (data->passwd_change_challenge_valid) {
277		wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Using challenge from the "
278			   "failure message");
279		challenge = data->passwd_change_challenge;
280	} else
281		challenge = pos;
282	pos += challenge_len;
283	len -= challenge_len;
284	wpa_hexdump_ascii(MSG_DEBUG, "EAP-MSCHAPV2: Authentication Servername",
285		    pos, len);
286
287	ret->ignore = FALSE;
288	ret->methodState = METHOD_MAY_CONT;
289	ret->decision = DECISION_FAIL;
290	ret->allowNotifications = TRUE;
291
292	return eap_mschapv2_challenge_reply(sm, data, id, req->mschapv2_id,
293					    challenge);
294}
295
296
297static void eap_mschapv2_password_changed(struct eap_sm *sm,
298					  struct eap_mschapv2_data *data)
299{
300	struct eap_peer_config *config = eap_get_config(sm);
301	if (config && config->new_password) {
302		wpa_msg(sm->msg_ctx, MSG_INFO,
303			WPA_EVENT_PASSWORD_CHANGED
304			"EAP-MSCHAPV2: Password changed successfully");
305		data->prev_error = 0;
306		os_free(config->password);
307		if (config->flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
308			/* TODO: update external storage */
309		} else if (config->flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH) {
310			config->password = os_malloc(16);
311			config->password_len = 16;
312			if (config->password &&
313			    nt_password_hash(config->new_password,
314					     config->new_password_len,
315					     config->password)) {
316				os_free(config->password);
317				config->password = NULL;
318				config->password_len = 0;
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 */
342static 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
398static 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
501static 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		if (nt_password_hash(new_password, new_password_len,
556				     new_password_hash))
557			goto fail;
558		nt_password_hash_encrypted_with_block(password,
559						      new_password_hash,
560						      cp->encr_hash);
561	} else {
562		if (old_nt_password_hash_encrypted_with_new_nt_password_hash(
563			    new_password, new_password_len,
564			    password, password_len, cp->encr_hash))
565			goto fail;
566	}
567
568	/* Peer-Challenge */
569	if (random_get_bytes(cp->peer_challenge, MSCHAPV2_CHAL_LEN))
570		goto fail;
571
572	/* Reserved, must be zero */
573	os_memset(cp->reserved, 0, 8);
574
575	/* NT-Response */
576	wpa_hexdump(MSG_DEBUG, "EAP-MSCHAPV2: auth_challenge",
577		    data->passwd_change_challenge, PASSWD_CHANGE_CHAL_LEN);
578	wpa_hexdump(MSG_DEBUG, "EAP-MSCHAPV2: peer_challenge",
579		    cp->peer_challenge, MSCHAPV2_CHAL_LEN);
580	wpa_hexdump_ascii(MSG_DEBUG, "EAP-MSCHAPV2: username",
581			  username, username_len);
582	wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-MSCHAPV2: new password",
583			      new_password, new_password_len);
584	generate_nt_response(data->passwd_change_challenge, cp->peer_challenge,
585			     username, username_len,
586			     new_password, new_password_len,
587			     cp->nt_response);
588	wpa_hexdump(MSG_DEBUG, "EAP-MSCHAPV2: NT-Response",
589		    cp->nt_response, MSCHAPV2_NT_RESPONSE_LEN);
590
591	/* Authenticator response is not really needed yet, but calculate it
592	 * here so that challenges need not be saved. */
593	generate_authenticator_response(new_password, new_password_len,
594					cp->peer_challenge,
595					data->passwd_change_challenge,
596					username, username_len,
597					cp->nt_response, data->auth_response);
598	data->auth_response_valid = 1;
599
600	/* Likewise, generate master_key here since we have the needed data
601	 * available. */
602	if (nt_password_hash(new_password, new_password_len, password_hash) ||
603	    hash_nt_password_hash(password_hash, password_hash_hash) ||
604	    get_master_key(password_hash_hash, cp->nt_response,
605			   data->master_key)) {
606		data->auth_response_valid = 0;
607		goto fail;
608	}
609	data->master_key_valid = 1;
610
611	/* Flags */
612	os_memset(cp->flags, 0, 2);
613
614	wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: TX identifier %d mschapv2_id %d "
615		   "(change pw)", id, ms->mschapv2_id);
616
617	return resp;
618
619fail:
620	wpabuf_free(resp);
621	return NULL;
622}
623
624
625/**
626 * eap_mschapv2_process - Process an EAP-MSCHAPv2 failure message
627 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
628 * @data: Pointer to private EAP method data from eap_mschapv2_init()
629 * @ret: Return values from EAP request validation and processing
630 * @req: Pointer to EAP-MSCHAPv2 header from the request
631 * @req_len: Length of the EAP-MSCHAPv2 data
632 * @id: EAP identifier used in th erequest
633 * Returns: Pointer to allocated EAP response packet (eapRespData) or %NULL if
634 * no reply available
635 */
636static struct wpabuf * eap_mschapv2_failure(struct eap_sm *sm,
637					    struct eap_mschapv2_data *data,
638					    struct eap_method_ret *ret,
639					    const struct eap_mschapv2_hdr *req,
640					    size_t req_len, u8 id)
641{
642	struct wpabuf *resp;
643	const u8 *msdata = (const u8 *) (req + 1);
644	char *buf;
645	size_t len = req_len - sizeof(*req);
646	int retry = 0;
647
648	wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Received failure");
649	wpa_hexdump_ascii(MSG_DEBUG, "EAP-MSCHAPV2: Failure data",
650			  msdata, len);
651	/*
652	 * eap_mschapv2_failure_txt() expects a nul terminated string, so we
653	 * must allocate a large enough temporary buffer to create that since
654	 * the received message does not include nul termination.
655	 */
656	buf = dup_binstr(msdata, len);
657	if (buf) {
658		retry = eap_mschapv2_failure_txt(sm, data, buf);
659		os_free(buf);
660	}
661
662	ret->ignore = FALSE;
663	ret->methodState = METHOD_DONE;
664	ret->decision = DECISION_FAIL;
665	ret->allowNotifications = FALSE;
666
667	if (data->prev_error == ERROR_PASSWD_EXPIRED &&
668	    data->passwd_change_version == 3) {
669		struct eap_peer_config *config = eap_get_config(sm);
670		if (config && config->new_password)
671			return eap_mschapv2_change_password(sm, data, ret, req,
672							    id);
673		if (config && config->pending_req_new_password)
674			return NULL;
675	} else if (retry && data->prev_error == ERROR_AUTHENTICATION_FAILURE) {
676		/* TODO: could try to retry authentication, e.g, after having
677		 * changed the username/password. In this case, EAP MS-CHAP-v2
678		 * Failure Response would not be sent here. */
679		return NULL;
680	}
681
682	/* Note: Only op_code of the EAP-MSCHAPV2 header is included in failure
683	 * message. */
684	resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, 1,
685			     EAP_CODE_RESPONSE, id);
686	if (resp == NULL)
687		return NULL;
688
689	wpabuf_put_u8(resp, MSCHAPV2_OP_FAILURE); /* op_code */
690
691	return resp;
692}
693
694
695static int eap_mschapv2_check_config(struct eap_sm *sm)
696{
697	size_t len;
698
699	if (eap_get_config_identity(sm, &len) == NULL) {
700		wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Identity not configured");
701		eap_sm_request_identity(sm);
702		return -1;
703	}
704
705	if (eap_get_config_password(sm, &len) == NULL) {
706		wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Password not configured");
707		eap_sm_request_password(sm);
708		return -1;
709	}
710
711	return 0;
712}
713
714
715static int eap_mschapv2_check_mslen(struct eap_sm *sm, size_t len,
716				    const struct eap_mschapv2_hdr *ms)
717{
718	size_t ms_len = WPA_GET_BE16(ms->ms_length);
719
720	if (ms_len == len)
721		return 0;
722
723	wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Invalid header: len=%lu "
724		   "ms_len=%lu", (unsigned long) len, (unsigned long) ms_len);
725	if (sm->workaround) {
726		/* Some authentication servers use invalid ms_len,
727		 * ignore it for interoperability. */
728		wpa_printf(MSG_INFO, "EAP-MSCHAPV2: workaround, ignore"
729			   " invalid ms_len %lu (len %lu)",
730			   (unsigned long) ms_len,
731			   (unsigned long) len);
732		return 0;
733	}
734
735	return -1;
736}
737
738
739static void eap_mschapv2_copy_challenge(struct eap_mschapv2_data *data,
740					const struct wpabuf *reqData)
741{
742	/*
743	 * Store a copy of the challenge message, so that it can be processed
744	 * again in case retry is allowed after a possible failure.
745	 */
746	wpabuf_free(data->prev_challenge);
747	data->prev_challenge = wpabuf_dup(reqData);
748}
749
750
751/**
752 * eap_mschapv2_process - Process an EAP-MSCHAPv2 request
753 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
754 * @priv: Pointer to private EAP method data from eap_mschapv2_init()
755 * @ret: Return values from EAP request validation and processing
756 * @reqData: EAP request to be processed (eapReqData)
757 * Returns: Pointer to allocated EAP response packet (eapRespData) or %NULL if
758 * no reply available
759 */
760static struct wpabuf * eap_mschapv2_process(struct eap_sm *sm, void *priv,
761					    struct eap_method_ret *ret,
762					    const struct wpabuf *reqData)
763{
764	struct eap_mschapv2_data *data = priv;
765	struct eap_peer_config *config = eap_get_config(sm);
766	const struct eap_mschapv2_hdr *ms;
767	int using_prev_challenge = 0;
768	const u8 *pos;
769	size_t len;
770	u8 id;
771
772	if (eap_mschapv2_check_config(sm)) {
773		ret->ignore = TRUE;
774		return NULL;
775	}
776
777	if (config->mschapv2_retry && data->prev_challenge &&
778	    data->prev_error == ERROR_AUTHENTICATION_FAILURE) {
779		wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Replacing pending packet "
780			   "with the previous challenge");
781
782		reqData = data->prev_challenge;
783		using_prev_challenge = 1;
784		config->mschapv2_retry = 0;
785	}
786
787	pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, reqData,
788			       &len);
789	if (pos == NULL || len < sizeof(*ms) + 1) {
790		ret->ignore = TRUE;
791		return NULL;
792	}
793
794	ms = (const struct eap_mschapv2_hdr *) pos;
795	if (eap_mschapv2_check_mslen(sm, len, ms)) {
796		ret->ignore = TRUE;
797		return NULL;
798	}
799
800	id = eap_get_id(reqData);
801	wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: RX identifier %d mschapv2_id %d",
802		   id, ms->mschapv2_id);
803
804	switch (ms->op_code) {
805	case MSCHAPV2_OP_CHALLENGE:
806		if (!using_prev_challenge)
807			eap_mschapv2_copy_challenge(data, reqData);
808		return eap_mschapv2_challenge(sm, data, ret, ms, len, id);
809	case MSCHAPV2_OP_SUCCESS:
810		return eap_mschapv2_success(sm, data, ret, ms, len, id);
811	case MSCHAPV2_OP_FAILURE:
812		return eap_mschapv2_failure(sm, data, ret, ms, len, id);
813	default:
814		wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Unknown op %d - ignored",
815			   ms->op_code);
816		ret->ignore = TRUE;
817		return NULL;
818	}
819}
820
821
822static Boolean eap_mschapv2_isKeyAvailable(struct eap_sm *sm, void *priv)
823{
824	struct eap_mschapv2_data *data = priv;
825	return data->success && data->master_key_valid;
826}
827
828
829static u8 * eap_mschapv2_getKey(struct eap_sm *sm, void *priv, size_t *len)
830{
831	struct eap_mschapv2_data *data = priv;
832	u8 *key;
833	int key_len;
834
835	if (!data->master_key_valid || !data->success)
836		return NULL;
837
838	key_len = 2 * MSCHAPV2_KEY_LEN;
839
840	key = os_malloc(key_len);
841	if (key == NULL)
842		return NULL;
843
844	/* MSK = server MS-MPPE-Recv-Key | MS-MPPE-Send-Key, i.e.,
845	 *	peer MS-MPPE-Send-Key | MS-MPPE-Recv-Key */
846	get_asymetric_start_key(data->master_key, key, MSCHAPV2_KEY_LEN, 1, 0);
847	get_asymetric_start_key(data->master_key, key + MSCHAPV2_KEY_LEN,
848				MSCHAPV2_KEY_LEN, 0, 0);
849
850	wpa_hexdump_key(MSG_DEBUG, "EAP-MSCHAPV2: Derived key",
851			key, key_len);
852
853	*len = key_len;
854	return key;
855}
856
857
858/**
859 * eap_peer_mschapv2_register - Register EAP-MSCHAPv2 peer method
860 * Returns: 0 on success, -1 on failure
861 *
862 * This function is used to register EAP-MSCHAPv2 peer method into the EAP
863 * method list.
864 */
865int eap_peer_mschapv2_register(void)
866{
867	struct eap_method *eap;
868	int ret;
869
870	eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
871				    EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2,
872				    "MSCHAPV2");
873	if (eap == NULL)
874		return -1;
875
876	eap->init = eap_mschapv2_init;
877	eap->deinit = eap_mschapv2_deinit;
878	eap->process = eap_mschapv2_process;
879	eap->isKeyAvailable = eap_mschapv2_isKeyAvailable;
880	eap->getKey = eap_mschapv2_getKey;
881
882	ret = eap_peer_method_register(eap);
883	if (ret)
884		eap_peer_method_free(eap);
885	return ret;
886}
887