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 "eap_i.h"
26 #include "eap_config.h"
27 #include "crypto/ms_funcs.h"
28 #include "wpa_ctrl.h"
29 #include "mschapv2.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
eap_mschapv2_init(struct eap_sm * sm)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
eap_mschapv2_deinit(struct eap_sm * sm,void * priv)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
eap_mschapv2_challenge_reply(struct eap_sm * sm,struct eap_mschapv2_data * data,u8 id,u8 mschapv2_id,const u8 * auth_challenge)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 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 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 */
eap_mschapv2_challenge(struct eap_sm * sm,struct eap_mschapv2_data * data,struct eap_method_ret * ret,const struct eap_mschapv2_hdr * req,size_t req_len,u8 id)239 static 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
eap_mschapv2_password_changed(struct eap_sm * sm,struct eap_mschapv2_data * data)297 static 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_PASSWORD_NTHASH) {
308 config->password = os_malloc(16);
309 config->password_len = 16;
310 if (config->password) {
311 nt_password_hash(config->new_password,
312 config->new_password_len,
313 config->password);
314 }
315 os_free(config->new_password);
316 } else {
317 config->password = config->new_password;
318 config->password_len = config->new_password_len;
319 }
320 config->new_password = NULL;
321 config->new_password_len = 0;
322 }
323 }
324
325
326 /**
327 * eap_mschapv2_process - Process an EAP-MSCHAPv2 success message
328 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
329 * @data: Pointer to private EAP method data from eap_mschapv2_init()
330 * @ret: Return values from EAP request validation and processing
331 * @req: Pointer to EAP-MSCHAPv2 header from the request
332 * @req_len: Length of the EAP-MSCHAPv2 data
333 * @id: EAP identifier used in th erequest
334 * Returns: Pointer to allocated EAP response packet (eapRespData) or %NULL if
335 * no reply available
336 */
eap_mschapv2_success(struct eap_sm * sm,struct eap_mschapv2_data * data,struct eap_method_ret * ret,const struct eap_mschapv2_hdr * req,size_t req_len,u8 id)337 static struct wpabuf * eap_mschapv2_success(struct eap_sm *sm,
338 struct eap_mschapv2_data *data,
339 struct eap_method_ret *ret,
340 const struct eap_mschapv2_hdr *req,
341 size_t req_len, u8 id)
342 {
343 struct wpabuf *resp;
344 const u8 *pos;
345 size_t len;
346
347 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Received success");
348 len = req_len - sizeof(*req);
349 pos = (const u8 *) (req + 1);
350 if (!data->auth_response_valid ||
351 mschapv2_verify_auth_response(data->auth_response, pos, len)) {
352 wpa_printf(MSG_WARNING, "EAP-MSCHAPV2: Invalid authenticator "
353 "response in success request");
354 ret->methodState = METHOD_DONE;
355 ret->decision = DECISION_FAIL;
356 return NULL;
357 }
358 pos += 2 + 2 * MSCHAPV2_AUTH_RESPONSE_LEN;
359 len -= 2 + 2 * MSCHAPV2_AUTH_RESPONSE_LEN;
360 while (len > 0 && *pos == ' ') {
361 pos++;
362 len--;
363 }
364 wpa_hexdump_ascii(MSG_DEBUG, "EAP-MSCHAPV2: Success message",
365 pos, len);
366 wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Authentication succeeded");
367
368 /* Note: Only op_code of the EAP-MSCHAPV2 header is included in success
369 * message. */
370 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, 1,
371 EAP_CODE_RESPONSE, id);
372 if (resp == NULL) {
373 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Failed to allocate "
374 "buffer for success response");
375 ret->ignore = TRUE;
376 return NULL;
377 }
378
379 wpabuf_put_u8(resp, MSCHAPV2_OP_SUCCESS); /* op_code */
380
381 ret->methodState = METHOD_DONE;
382 ret->decision = DECISION_UNCOND_SUCC;
383 ret->allowNotifications = FALSE;
384 data->success = 1;
385
386 if (data->prev_error == ERROR_PASSWD_EXPIRED)
387 eap_mschapv2_password_changed(sm, data);
388
389 return resp;
390 }
391
392
eap_mschapv2_failure_txt(struct eap_sm * sm,struct eap_mschapv2_data * data,char * txt)393 static int eap_mschapv2_failure_txt(struct eap_sm *sm,
394 struct eap_mschapv2_data *data, char *txt)
395 {
396 char *pos, *msg = "";
397 int retry = 1;
398 struct eap_peer_config *config = eap_get_config(sm);
399
400 /* For example:
401 * E=691 R=1 C=<32 octets hex challenge> V=3 M=Authentication Failure
402 */
403
404 pos = txt;
405
406 if (pos && os_strncmp(pos, "E=", 2) == 0) {
407 pos += 2;
408 data->prev_error = atoi(pos);
409 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: error %d",
410 data->prev_error);
411 pos = os_strchr(pos, ' ');
412 if (pos)
413 pos++;
414 }
415
416 if (pos && os_strncmp(pos, "R=", 2) == 0) {
417 pos += 2;
418 retry = atoi(pos);
419 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: retry is %sallowed",
420 retry == 1 ? "" : "not ");
421 pos = os_strchr(pos, ' ');
422 if (pos)
423 pos++;
424 }
425
426 if (pos && os_strncmp(pos, "C=", 2) == 0) {
427 int hex_len;
428 pos += 2;
429 hex_len = os_strchr(pos, ' ') - (char *) pos;
430 if (hex_len == PASSWD_CHANGE_CHAL_LEN * 2) {
431 if (hexstr2bin(pos, data->passwd_change_challenge,
432 PASSWD_CHANGE_CHAL_LEN)) {
433 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: invalid "
434 "failure challenge");
435 } else {
436 wpa_hexdump(MSG_DEBUG, "EAP-MSCHAPV2: failure "
437 "challenge",
438 data->passwd_change_challenge,
439 PASSWD_CHANGE_CHAL_LEN);
440 data->passwd_change_challenge_valid = 1;
441 }
442 } else {
443 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: invalid failure "
444 "challenge len %d", hex_len);
445 }
446 pos = os_strchr(pos, ' ');
447 if (pos)
448 pos++;
449 } else {
450 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: required challenge field "
451 "was not present in failure message");
452 }
453
454 if (pos && os_strncmp(pos, "V=", 2) == 0) {
455 pos += 2;
456 data->passwd_change_version = atoi(pos);
457 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: password changing "
458 "protocol version %d", data->passwd_change_version);
459 pos = os_strchr(pos, ' ');
460 if (pos)
461 pos++;
462 }
463
464 if (pos && os_strncmp(pos, "M=", 2) == 0) {
465 pos += 2;
466 msg = pos;
467 }
468 wpa_msg(sm->msg_ctx, MSG_WARNING,
469 "EAP-MSCHAPV2: failure message: '%s' (retry %sallowed, error "
470 "%d)",
471 msg, retry == 1 ? "" : "not ", data->prev_error);
472 if (data->prev_error == ERROR_PASSWD_EXPIRED &&
473 data->passwd_change_version == 3 && config) {
474 if (config->new_password == NULL) {
475 wpa_msg(sm->msg_ctx, MSG_INFO,
476 "EAP-MSCHAPV2: Password expired - password "
477 "change required");
478 eap_sm_request_new_password(sm);
479 }
480 } else if (retry == 1 && config) {
481 /* TODO: could prevent the current password from being used
482 * again at least for some period of time */
483 if (!config->mschapv2_retry)
484 eap_sm_request_identity(sm);
485 eap_sm_request_password(sm);
486 config->mschapv2_retry = 1;
487 } else if (config) {
488 /* TODO: prevent retries using same username/password */
489 config->mschapv2_retry = 0;
490 }
491
492 return retry == 1;
493 }
494
495
eap_mschapv2_change_password(struct eap_sm * sm,struct eap_mschapv2_data * data,struct eap_method_ret * ret,const struct eap_mschapv2_hdr * req,u8 id)496 static struct wpabuf * eap_mschapv2_change_password(
497 struct eap_sm *sm, struct eap_mschapv2_data *data,
498 struct eap_method_ret *ret, const struct eap_mschapv2_hdr *req, u8 id)
499 {
500 struct wpabuf *resp;
501 int ms_len;
502 const u8 *username, *password, *new_password;
503 size_t username_len, password_len, new_password_len;
504 struct eap_mschapv2_hdr *ms;
505 struct ms_change_password *cp;
506 u8 password_hash[16], password_hash_hash[16];
507 int pwhash;
508
509 username = eap_get_config_identity(sm, &username_len);
510 password = eap_get_config_password2(sm, &password_len, &pwhash);
511 new_password = eap_get_config_new_password(sm, &new_password_len);
512 if (username == NULL || password == NULL || new_password == NULL)
513 return NULL;
514
515 username = mschapv2_remove_domain(username, &username_len);
516
517 ret->ignore = FALSE;
518 ret->methodState = METHOD_MAY_CONT;
519 ret->decision = DECISION_COND_SUCC;
520 ret->allowNotifications = TRUE;
521
522 ms_len = sizeof(*ms) + sizeof(*cp);
523 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, ms_len,
524 EAP_CODE_RESPONSE, id);
525 if (resp == NULL)
526 return NULL;
527
528 ms = wpabuf_put(resp, sizeof(*ms));
529 ms->op_code = MSCHAPV2_OP_CHANGE_PASSWORD;
530 ms->mschapv2_id = req->mschapv2_id + 1;
531 WPA_PUT_BE16(ms->ms_length, ms_len);
532 cp = wpabuf_put(resp, sizeof(*cp));
533
534 /* Encrypted-Password */
535 if (pwhash) {
536 if (encrypt_pw_block_with_password_hash(
537 new_password, new_password_len,
538 password, cp->encr_password))
539 goto fail;
540 } else {
541 if (new_password_encrypted_with_old_nt_password_hash(
542 new_password, new_password_len,
543 password, password_len, cp->encr_password))
544 goto fail;
545 }
546
547 /* Encrypted-Hash */
548 if (pwhash) {
549 u8 new_password_hash[16];
550 nt_password_hash(new_password, new_password_len,
551 new_password_hash);
552 nt_password_hash_encrypted_with_block(password,
553 new_password_hash,
554 cp->encr_hash);
555 } else {
556 old_nt_password_hash_encrypted_with_new_nt_password_hash(
557 new_password, new_password_len,
558 password, password_len, cp->encr_hash);
559 }
560
561 /* Peer-Challenge */
562 if (os_get_random(cp->peer_challenge, MSCHAPV2_CHAL_LEN))
563 goto fail;
564
565 /* Reserved, must be zero */
566 os_memset(cp->reserved, 0, 8);
567
568 /* NT-Response */
569 wpa_hexdump(MSG_DEBUG, "EAP-MSCHAPV2: auth_challenge",
570 data->passwd_change_challenge, PASSWD_CHANGE_CHAL_LEN);
571 wpa_hexdump(MSG_DEBUG, "EAP-MSCHAPV2: peer_challenge",
572 cp->peer_challenge, MSCHAPV2_CHAL_LEN);
573 wpa_hexdump_ascii(MSG_DEBUG, "EAP-MSCHAPV2: username",
574 username, username_len);
575 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-MSCHAPV2: new password",
576 new_password, new_password_len);
577 generate_nt_response(data->passwd_change_challenge, cp->peer_challenge,
578 username, username_len,
579 new_password, new_password_len,
580 cp->nt_response);
581 wpa_hexdump(MSG_DEBUG, "EAP-MSCHAPV2: NT-Response",
582 cp->nt_response, MSCHAPV2_NT_RESPONSE_LEN);
583
584 /* Authenticator response is not really needed yet, but calculate it
585 * here so that challenges need not be saved. */
586 generate_authenticator_response(new_password, new_password_len,
587 cp->peer_challenge,
588 data->passwd_change_challenge,
589 username, username_len,
590 cp->nt_response, data->auth_response);
591 data->auth_response_valid = 1;
592
593 /* Likewise, generate master_key here since we have the needed data
594 * available. */
595 nt_password_hash(new_password, new_password_len, password_hash);
596 hash_nt_password_hash(password_hash, password_hash_hash);
597 get_master_key(password_hash_hash, cp->nt_response, data->master_key);
598 data->master_key_valid = 1;
599
600 /* Flags */
601 os_memset(cp->flags, 0, 2);
602
603 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: TX identifier %d mschapv2_id %d "
604 "(change pw)", id, ms->mschapv2_id);
605
606 return resp;
607
608 fail:
609 wpabuf_free(resp);
610 return NULL;
611 }
612
613
614 /**
615 * eap_mschapv2_process - Process an EAP-MSCHAPv2 failure message
616 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
617 * @data: Pointer to private EAP method data from eap_mschapv2_init()
618 * @ret: Return values from EAP request validation and processing
619 * @req: Pointer to EAP-MSCHAPv2 header from the request
620 * @req_len: Length of the EAP-MSCHAPv2 data
621 * @id: EAP identifier used in th erequest
622 * Returns: Pointer to allocated EAP response packet (eapRespData) or %NULL if
623 * no reply available
624 */
eap_mschapv2_failure(struct eap_sm * sm,struct eap_mschapv2_data * data,struct eap_method_ret * ret,const struct eap_mschapv2_hdr * req,size_t req_len,u8 id)625 static struct wpabuf * eap_mschapv2_failure(struct eap_sm *sm,
626 struct eap_mschapv2_data *data,
627 struct eap_method_ret *ret,
628 const struct eap_mschapv2_hdr *req,
629 size_t req_len, u8 id)
630 {
631 struct wpabuf *resp;
632 const u8 *msdata = (const u8 *) (req + 1);
633 char *buf;
634 size_t len = req_len - sizeof(*req);
635 int retry = 0;
636
637 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Received failure");
638 wpa_hexdump_ascii(MSG_DEBUG, "EAP-MSCHAPV2: Failure data",
639 msdata, len);
640 /*
641 * eap_mschapv2_failure_txt() expects a nul terminated string, so we
642 * must allocate a large enough temporary buffer to create that since
643 * the received message does not include nul termination.
644 */
645 buf = os_malloc(len + 1);
646 if (buf) {
647 os_memcpy(buf, msdata, len);
648 buf[len] = '\0';
649 retry = eap_mschapv2_failure_txt(sm, data, buf);
650 os_free(buf);
651 }
652
653 ret->ignore = FALSE;
654 ret->methodState = METHOD_DONE;
655 ret->decision = DECISION_FAIL;
656 ret->allowNotifications = FALSE;
657
658 if (data->prev_error == ERROR_PASSWD_EXPIRED &&
659 data->passwd_change_version == 3) {
660 struct eap_peer_config *config = eap_get_config(sm);
661 if (config && config->new_password)
662 return eap_mschapv2_change_password(sm, data, ret, req,
663 id);
664 if (config && config->pending_req_new_password)
665 return NULL;
666 } else if (retry && data->prev_error == ERROR_AUTHENTICATION_FAILURE) {
667 /* TODO: could try to retry authentication, e.g, after having
668 * changed the username/password. In this case, EAP MS-CHAP-v2
669 * Failure Response would not be sent here. */
670 return NULL;
671 }
672
673 /* Note: Only op_code of the EAP-MSCHAPV2 header is included in failure
674 * message. */
675 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, 1,
676 EAP_CODE_RESPONSE, id);
677 if (resp == NULL)
678 return NULL;
679
680 wpabuf_put_u8(resp, MSCHAPV2_OP_FAILURE); /* op_code */
681
682 return resp;
683 }
684
685
eap_mschapv2_check_config(struct eap_sm * sm)686 static int eap_mschapv2_check_config(struct eap_sm *sm)
687 {
688 size_t len;
689
690 if (eap_get_config_identity(sm, &len) == NULL) {
691 wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Identity not configured");
692 eap_sm_request_identity(sm);
693 return -1;
694 }
695
696 if (eap_get_config_password(sm, &len) == NULL) {
697 wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Password not configured");
698 eap_sm_request_password(sm);
699 return -1;
700 }
701
702 return 0;
703 }
704
705
eap_mschapv2_check_mslen(struct eap_sm * sm,size_t len,const struct eap_mschapv2_hdr * ms)706 static int eap_mschapv2_check_mslen(struct eap_sm *sm, size_t len,
707 const struct eap_mschapv2_hdr *ms)
708 {
709 size_t ms_len = WPA_GET_BE16(ms->ms_length);
710
711 if (ms_len == len)
712 return 0;
713
714 wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Invalid header: len=%lu "
715 "ms_len=%lu", (unsigned long) len, (unsigned long) ms_len);
716 if (sm->workaround) {
717 /* Some authentication servers use invalid ms_len,
718 * ignore it for interoperability. */
719 wpa_printf(MSG_INFO, "EAP-MSCHAPV2: workaround, ignore"
720 " invalid ms_len %lu (len %lu)",
721 (unsigned long) ms_len,
722 (unsigned long) len);
723 return 0;
724 }
725
726 return -1;
727 }
728
729
eap_mschapv2_copy_challenge(struct eap_mschapv2_data * data,const struct wpabuf * reqData)730 static void eap_mschapv2_copy_challenge(struct eap_mschapv2_data *data,
731 const struct wpabuf *reqData)
732 {
733 /*
734 * Store a copy of the challenge message, so that it can be processed
735 * again in case retry is allowed after a possible failure.
736 */
737 wpabuf_free(data->prev_challenge);
738 data->prev_challenge = wpabuf_dup(reqData);
739 }
740
741
742 /**
743 * eap_mschapv2_process - Process an EAP-MSCHAPv2 request
744 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
745 * @priv: Pointer to private EAP method data from eap_mschapv2_init()
746 * @ret: Return values from EAP request validation and processing
747 * @reqData: EAP request to be processed (eapReqData)
748 * Returns: Pointer to allocated EAP response packet (eapRespData) or %NULL if
749 * no reply available
750 */
eap_mschapv2_process(struct eap_sm * sm,void * priv,struct eap_method_ret * ret,const struct wpabuf * reqData)751 static struct wpabuf * eap_mschapv2_process(struct eap_sm *sm, void *priv,
752 struct eap_method_ret *ret,
753 const struct wpabuf *reqData)
754 {
755 struct eap_mschapv2_data *data = priv;
756 struct eap_peer_config *config = eap_get_config(sm);
757 const struct eap_mschapv2_hdr *ms;
758 int using_prev_challenge = 0;
759 const u8 *pos;
760 size_t len;
761 u8 id;
762
763 if (eap_mschapv2_check_config(sm)) {
764 ret->ignore = TRUE;
765 return NULL;
766 }
767
768 if (config->mschapv2_retry && data->prev_challenge &&
769 data->prev_error == ERROR_AUTHENTICATION_FAILURE) {
770 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Replacing pending packet "
771 "with the previous challenge");
772
773 reqData = data->prev_challenge;
774 using_prev_challenge = 1;
775 config->mschapv2_retry = 0;
776 }
777
778 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, reqData,
779 &len);
780 if (pos == NULL || len < sizeof(*ms) + 1) {
781 ret->ignore = TRUE;
782 return NULL;
783 }
784
785 ms = (const struct eap_mschapv2_hdr *) pos;
786 if (eap_mschapv2_check_mslen(sm, len, ms)) {
787 ret->ignore = TRUE;
788 return NULL;
789 }
790
791 id = eap_get_id(reqData);
792 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: RX identifier %d mschapv2_id %d",
793 id, ms->mschapv2_id);
794
795 switch (ms->op_code) {
796 case MSCHAPV2_OP_CHALLENGE:
797 if (!using_prev_challenge)
798 eap_mschapv2_copy_challenge(data, reqData);
799 return eap_mschapv2_challenge(sm, data, ret, ms, len, id);
800 case MSCHAPV2_OP_SUCCESS:
801 return eap_mschapv2_success(sm, data, ret, ms, len, id);
802 case MSCHAPV2_OP_FAILURE:
803 return eap_mschapv2_failure(sm, data, ret, ms, len, id);
804 default:
805 wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Unknown op %d - ignored",
806 ms->op_code);
807 ret->ignore = TRUE;
808 return NULL;
809 }
810 }
811
812
eap_mschapv2_isKeyAvailable(struct eap_sm * sm,void * priv)813 static Boolean eap_mschapv2_isKeyAvailable(struct eap_sm *sm, void *priv)
814 {
815 struct eap_mschapv2_data *data = priv;
816 return data->success && data->master_key_valid;
817 }
818
819
eap_mschapv2_getKey(struct eap_sm * sm,void * priv,size_t * len)820 static u8 * eap_mschapv2_getKey(struct eap_sm *sm, void *priv, size_t *len)
821 {
822 struct eap_mschapv2_data *data = priv;
823 u8 *key;
824 int key_len;
825
826 if (!data->master_key_valid || !data->success)
827 return NULL;
828
829 key_len = 2 * MSCHAPV2_KEY_LEN;
830
831 key = os_malloc(key_len);
832 if (key == NULL)
833 return NULL;
834
835 /* MSK = server MS-MPPE-Recv-Key | MS-MPPE-Send-Key, i.e.,
836 * peer MS-MPPE-Send-Key | MS-MPPE-Recv-Key */
837 get_asymetric_start_key(data->master_key, key, MSCHAPV2_KEY_LEN, 1, 0);
838 get_asymetric_start_key(data->master_key, key + MSCHAPV2_KEY_LEN,
839 MSCHAPV2_KEY_LEN, 0, 0);
840
841 wpa_hexdump_key(MSG_DEBUG, "EAP-MSCHAPV2: Derived key",
842 key, key_len);
843
844 *len = key_len;
845 return key;
846 }
847
848
849 /**
850 * eap_peer_mschapv2_register - Register EAP-MSCHAPv2 peer method
851 * Returns: 0 on success, -1 on failure
852 *
853 * This function is used to register EAP-MSCHAPv2 peer method into the EAP
854 * method list.
855 */
eap_peer_mschapv2_register(void)856 int eap_peer_mschapv2_register(void)
857 {
858 struct eap_method *eap;
859 int ret;
860
861 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
862 EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2,
863 "MSCHAPV2");
864 if (eap == NULL)
865 return -1;
866
867 eap->init = eap_mschapv2_init;
868 eap->deinit = eap_mschapv2_deinit;
869 eap->process = eap_mschapv2_process;
870 eap->isKeyAvailable = eap_mschapv2_isKeyAvailable;
871 eap->getKey = eap_mschapv2_getKey;
872
873 ret = eap_peer_method_register(eap);
874 if (ret)
875 eap_peer_method_free(eap);
876 return ret;
877 }
878