2 * EAP peer method: EAP-MSCHAPV2 (draft-kamath-pppext-eap-mschapv2-00.txt)
3 * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
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
25 #include "crypto/ms_funcs.h"
26 #include "crypto/random.h"
27 #include "common/wpa_ctrl.h"
30 #include "eap_config.h"
37 struct eap_mschapv2_hdr
{
38 u8 op_code
; /* MSCHAPV2_OP_* */
39 u8 mschapv2_id
; /* usually same as EAP identifier; must be changed
40 * for challenges, but not for success/failure */
41 u8 ms_length
[2]; /* Note: misaligned; length - 5 */
42 /* followed by data */
45 /* Response Data field */
47 u8 peer_challenge
[MSCHAPV2_CHAL_LEN
];
49 u8 nt_response
[MSCHAPV2_NT_RESPONSE_LEN
];
53 /* Change-Password Data field */
54 struct ms_change_password
{
55 u8 encr_password
[516];
57 u8 peer_challenge
[MSCHAPV2_CHAL_LEN
];
59 u8 nt_response
[MSCHAPV2_NT_RESPONSE_LEN
];
67 #define MSCHAPV2_OP_CHALLENGE 1
68 #define MSCHAPV2_OP_RESPONSE 2
69 #define MSCHAPV2_OP_SUCCESS 3
70 #define MSCHAPV2_OP_FAILURE 4
71 #define MSCHAPV2_OP_CHANGE_PASSWORD 7
73 #define ERROR_RESTRICTED_LOGON_HOURS 646
74 #define ERROR_ACCT_DISABLED 647
75 #define ERROR_PASSWD_EXPIRED 648
76 #define ERROR_NO_DIALIN_PERMISSION 649
77 #define ERROR_AUTHENTICATION_FAILURE 691
78 #define ERROR_CHANGING_PASSWORD 709
80 #define PASSWD_CHANGE_CHAL_LEN 16
81 #define MSCHAPV2_KEY_LEN 16
84 struct eap_mschapv2_data
{
85 u8 auth_response
[MSCHAPV2_AUTH_RESPONSE_LEN
];
86 int auth_response_valid
;
89 u8 passwd_change_challenge
[PASSWD_CHANGE_CHAL_LEN
];
90 int passwd_change_challenge_valid
;
91 int passwd_change_version
;
93 /* Optional challenge values generated in EAP-FAST Phase 1 negotiation
99 u8 master_key
[MSCHAPV2_MASTER_KEY_LEN
];
100 int master_key_valid
;
103 struct wpabuf
*prev_challenge
;
107 static void eap_mschapv2_deinit(struct eap_sm
*sm
, void *priv
);
110 static void * eap_mschapv2_init(struct eap_sm
*sm
)
112 struct eap_mschapv2_data
*data
;
113 data
= os_zalloc(sizeof(*data
));
117 if (sm
->peer_challenge
) {
118 data
->peer_challenge
= os_malloc(MSCHAPV2_CHAL_LEN
);
119 if (data
->peer_challenge
== NULL
) {
120 eap_mschapv2_deinit(sm
, data
);
123 os_memcpy(data
->peer_challenge
, sm
->peer_challenge
,
127 if (sm
->auth_challenge
) {
128 data
->auth_challenge
= os_malloc(MSCHAPV2_CHAL_LEN
);
129 if (data
->auth_challenge
== NULL
) {
130 eap_mschapv2_deinit(sm
, data
);
133 os_memcpy(data
->auth_challenge
, sm
->auth_challenge
,
137 data
->phase2
= sm
->init_phase2
;
143 static void eap_mschapv2_deinit(struct eap_sm
*sm
, void *priv
)
145 struct eap_mschapv2_data
*data
= priv
;
146 os_free(data
->peer_challenge
);
147 os_free(data
->auth_challenge
);
148 wpabuf_free(data
->prev_challenge
);
153 static struct wpabuf
* eap_mschapv2_challenge_reply(
154 struct eap_sm
*sm
, struct eap_mschapv2_data
*data
, u8 id
,
155 u8 mschapv2_id
, const u8
*auth_challenge
)
158 struct eap_mschapv2_hdr
*ms
;
161 struct ms_response
*r
;
162 size_t identity_len
, password_len
;
163 const u8
*identity
, *password
;
166 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: Generating Challenge Response");
168 identity
= eap_get_config_identity(sm
, &identity_len
);
169 password
= eap_get_config_password2(sm
, &password_len
, &pwhash
);
170 if (identity
== NULL
|| password
== NULL
)
173 ms_len
= sizeof(*ms
) + 1 + sizeof(*r
) + identity_len
;
174 resp
= eap_msg_alloc(EAP_VENDOR_IETF
, EAP_TYPE_MSCHAPV2
, ms_len
,
175 EAP_CODE_RESPONSE
, id
);
179 ms
= wpabuf_put(resp
, sizeof(*ms
));
180 ms
->op_code
= MSCHAPV2_OP_RESPONSE
;
181 ms
->mschapv2_id
= mschapv2_id
;
182 if (data
->prev_error
) {
184 * TODO: this does not seem to be enough when processing two
185 * or more failure messages. IAS did not increment mschapv2_id
186 * in its own packets, but it seemed to expect the peer to
187 * increment this for all packets(?).
191 WPA_PUT_BE16(ms
->ms_length
, ms_len
);
193 wpabuf_put_u8(resp
, sizeof(*r
)); /* Value-Size */
196 r
= wpabuf_put(resp
, sizeof(*r
));
197 peer_challenge
= r
->peer_challenge
;
198 if (data
->peer_challenge
) {
199 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: peer_challenge generated "
201 peer_challenge
= data
->peer_challenge
;
202 os_memset(r
->peer_challenge
, 0, MSCHAPV2_CHAL_LEN
);
203 } else if (random_get_bytes(peer_challenge
, MSCHAPV2_CHAL_LEN
)) {
207 os_memset(r
->reserved
, 0, 8);
208 if (data
->auth_challenge
) {
209 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: auth_challenge generated "
211 auth_challenge
= data
->auth_challenge
;
213 if (mschapv2_derive_response(identity
, identity_len
, password
,
214 password_len
, pwhash
, auth_challenge
,
215 peer_challenge
, r
->nt_response
,
216 data
->auth_response
, data
->master_key
)) {
217 wpa_printf(MSG_ERROR
, "EAP-MSCHAPV2: Failed to derive "
222 data
->auth_response_valid
= 1;
223 data
->master_key_valid
= 1;
225 r
->flags
= 0; /* reserved, must be zero */
227 wpabuf_put_data(resp
, identity
, identity_len
);
228 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: TX identifier %d mschapv2_id %d "
229 "(response)", id
, ms
->mschapv2_id
);
235 * eap_mschapv2_process - Process an EAP-MSCHAPv2 challenge message
236 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
237 * @data: Pointer to private EAP method data from eap_mschapv2_init()
238 * @ret: Return values from EAP request validation and processing
239 * @req: Pointer to EAP-MSCHAPv2 header from the request
240 * @req_len: Length of the EAP-MSCHAPv2 data
241 * @id: EAP identifier used in the request
242 * Returns: Pointer to allocated EAP response packet (eapRespData) or %NULL if
245 static struct wpabuf
* eap_mschapv2_challenge(
246 struct eap_sm
*sm
, struct eap_mschapv2_data
*data
,
247 struct eap_method_ret
*ret
, const struct eap_mschapv2_hdr
*req
,
248 size_t req_len
, u8 id
)
250 size_t len
, challenge_len
;
251 const u8
*pos
, *challenge
;
253 if (eap_get_config_identity(sm
, &len
) == NULL
||
254 eap_get_config_password(sm
, &len
) == NULL
)
257 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: Received challenge");
258 if (req_len
< sizeof(*req
) + 1) {
259 wpa_printf(MSG_INFO
, "EAP-MSCHAPV2: Too short challenge data "
260 "(len %lu)", (unsigned long) req_len
);
264 pos
= (const u8
*) (req
+ 1);
265 challenge_len
= *pos
++;
266 len
= req_len
- sizeof(*req
) - 1;
267 if (challenge_len
!= MSCHAPV2_CHAL_LEN
) {
268 wpa_printf(MSG_INFO
, "EAP-MSCHAPV2: Invalid challenge length "
269 "%lu", (unsigned long) challenge_len
);
274 if (len
< challenge_len
) {
275 wpa_printf(MSG_INFO
, "EAP-MSCHAPV2: Too short challenge"
276 " packet: len=%lu challenge_len=%lu",
277 (unsigned long) len
, (unsigned long) challenge_len
);
282 if (data
->passwd_change_challenge_valid
) {
283 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: Using challenge from the "
285 challenge
= data
->passwd_change_challenge
;
288 pos
+= challenge_len
;
289 len
-= challenge_len
;
290 wpa_hexdump_ascii(MSG_DEBUG
, "EAP-MSCHAPV2: Authentication Servername",
294 ret
->methodState
= METHOD_MAY_CONT
;
295 ret
->decision
= DECISION_FAIL
;
296 ret
->allowNotifications
= TRUE
;
298 return eap_mschapv2_challenge_reply(sm
, data
, id
, req
->mschapv2_id
,
303 static void eap_mschapv2_password_changed(struct eap_sm
*sm
,
304 struct eap_mschapv2_data
*data
)
306 struct eap_peer_config
*config
= eap_get_config(sm
);
307 if (config
&& config
->new_password
) {
308 wpa_msg(sm
->msg_ctx
, MSG_INFO
,
309 WPA_EVENT_PASSWORD_CHANGED
310 "EAP-MSCHAPV2: Password changed successfully");
311 data
->prev_error
= 0;
312 os_free(config
->password
);
313 if (config
->flags
& EAP_CONFIG_FLAGS_PASSWORD_NTHASH
) {
314 config
->password
= os_malloc(16);
315 config
->password_len
= 16;
316 if (config
->password
) {
317 nt_password_hash(config
->new_password
,
318 config
->new_password_len
,
321 os_free(config
->new_password
);
323 config
->password
= config
->new_password
;
324 config
->password_len
= config
->new_password_len
;
326 config
->new_password
= NULL
;
327 config
->new_password_len
= 0;
333 * eap_mschapv2_process - Process an EAP-MSCHAPv2 success message
334 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
335 * @data: Pointer to private EAP method data from eap_mschapv2_init()
336 * @ret: Return values from EAP request validation and processing
337 * @req: Pointer to EAP-MSCHAPv2 header from the request
338 * @req_len: Length of the EAP-MSCHAPv2 data
339 * @id: EAP identifier used in th erequest
340 * Returns: Pointer to allocated EAP response packet (eapRespData) or %NULL if
343 static struct wpabuf
* eap_mschapv2_success(struct eap_sm
*sm
,
344 struct eap_mschapv2_data
*data
,
345 struct eap_method_ret
*ret
,
346 const struct eap_mschapv2_hdr
*req
,
347 size_t req_len
, u8 id
)
353 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: Received success");
354 len
= req_len
- sizeof(*req
);
355 pos
= (const u8
*) (req
+ 1);
356 if (!data
->auth_response_valid
||
357 mschapv2_verify_auth_response(data
->auth_response
, pos
, len
)) {
358 wpa_printf(MSG_WARNING
, "EAP-MSCHAPV2: Invalid authenticator "
359 "response in success request");
360 ret
->methodState
= METHOD_DONE
;
361 ret
->decision
= DECISION_FAIL
;
364 pos
+= 2 + 2 * MSCHAPV2_AUTH_RESPONSE_LEN
;
365 len
-= 2 + 2 * MSCHAPV2_AUTH_RESPONSE_LEN
;
366 while (len
> 0 && *pos
== ' ') {
370 wpa_hexdump_ascii(MSG_DEBUG
, "EAP-MSCHAPV2: Success message",
372 wpa_printf(MSG_INFO
, "EAP-MSCHAPV2: Authentication succeeded");
374 /* Note: Only op_code of the EAP-MSCHAPV2 header is included in success
376 resp
= eap_msg_alloc(EAP_VENDOR_IETF
, EAP_TYPE_MSCHAPV2
, 1,
377 EAP_CODE_RESPONSE
, id
);
379 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: Failed to allocate "
380 "buffer for success response");
385 wpabuf_put_u8(resp
, MSCHAPV2_OP_SUCCESS
); /* op_code */
387 ret
->methodState
= METHOD_DONE
;
388 ret
->decision
= DECISION_UNCOND_SUCC
;
389 ret
->allowNotifications
= FALSE
;
392 if (data
->prev_error
== ERROR_PASSWD_EXPIRED
)
393 eap_mschapv2_password_changed(sm
, data
);
399 static int eap_mschapv2_failure_txt(struct eap_sm
*sm
,
400 struct eap_mschapv2_data
*data
, char *txt
)
402 char *pos
, *msg
= "";
404 struct eap_peer_config
*config
= eap_get_config(sm
);
407 * E=691 R=1 C=<32 octets hex challenge> V=3 M=Authentication Failure
412 if (pos
&& os_strncmp(pos
, "E=", 2) == 0) {
414 data
->prev_error
= atoi(pos
);
415 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: error %d",
417 pos
= os_strchr(pos
, ' ');
422 if (pos
&& os_strncmp(pos
, "R=", 2) == 0) {
425 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: retry is %sallowed",
426 retry
== 1 ? "" : "not ");
427 pos
= os_strchr(pos
, ' ');
432 if (pos
&& os_strncmp(pos
, "C=", 2) == 0) {
435 hex_len
= os_strchr(pos
, ' ') - (char *) pos
;
436 if (hex_len
== PASSWD_CHANGE_CHAL_LEN
* 2) {
437 if (hexstr2bin(pos
, data
->passwd_change_challenge
,
438 PASSWD_CHANGE_CHAL_LEN
)) {
439 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: invalid "
440 "failure challenge");
442 wpa_hexdump(MSG_DEBUG
, "EAP-MSCHAPV2: failure "
444 data
->passwd_change_challenge
,
445 PASSWD_CHANGE_CHAL_LEN
);
446 data
->passwd_change_challenge_valid
= 1;
449 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: invalid failure "
450 "challenge len %d", hex_len
);
452 pos
= os_strchr(pos
, ' ');
456 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: required challenge field "
457 "was not present in failure message");
460 if (pos
&& os_strncmp(pos
, "V=", 2) == 0) {
462 data
->passwd_change_version
= atoi(pos
);
463 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: password changing "
464 "protocol version %d", data
->passwd_change_version
);
465 pos
= os_strchr(pos
, ' ');
470 if (pos
&& os_strncmp(pos
, "M=", 2) == 0) {
474 wpa_msg(sm
->msg_ctx
, MSG_WARNING
,
475 "EAP-MSCHAPV2: failure message: '%s' (retry %sallowed, error "
477 msg
, retry
== 1 ? "" : "not ", data
->prev_error
);
478 if (data
->prev_error
== ERROR_PASSWD_EXPIRED
&&
479 data
->passwd_change_version
== 3 && config
) {
480 if (config
->new_password
== NULL
) {
481 wpa_msg(sm
->msg_ctx
, MSG_INFO
,
482 "EAP-MSCHAPV2: Password expired - password "
484 eap_sm_request_new_password(sm
);
486 } else if (retry
== 1 && config
) {
487 /* TODO: could prevent the current password from being used
488 * again at least for some period of time */
489 if (!config
->mschapv2_retry
)
490 eap_sm_request_identity(sm
);
491 eap_sm_request_password(sm
);
492 config
->mschapv2_retry
= 1;
494 /* TODO: prevent retries using same username/password */
495 config
->mschapv2_retry
= 0;
502 static struct wpabuf
* eap_mschapv2_change_password(
503 struct eap_sm
*sm
, struct eap_mschapv2_data
*data
,
504 struct eap_method_ret
*ret
, const struct eap_mschapv2_hdr
*req
, u8 id
)
508 const u8
*username
, *password
, *new_password
;
509 size_t username_len
, password_len
, new_password_len
;
510 struct eap_mschapv2_hdr
*ms
;
511 struct ms_change_password
*cp
;
512 u8 password_hash
[16], password_hash_hash
[16];
515 username
= eap_get_config_identity(sm
, &username_len
);
516 password
= eap_get_config_password2(sm
, &password_len
, &pwhash
);
517 new_password
= eap_get_config_new_password(sm
, &new_password_len
);
518 if (username
== NULL
|| password
== NULL
|| new_password
== NULL
)
521 username
= mschapv2_remove_domain(username
, &username_len
);
524 ret
->methodState
= METHOD_MAY_CONT
;
525 ret
->decision
= DECISION_COND_SUCC
;
526 ret
->allowNotifications
= TRUE
;
528 ms_len
= sizeof(*ms
) + sizeof(*cp
);
529 resp
= eap_msg_alloc(EAP_VENDOR_IETF
, EAP_TYPE_MSCHAPV2
, ms_len
,
530 EAP_CODE_RESPONSE
, id
);
534 ms
= wpabuf_put(resp
, sizeof(*ms
));
535 ms
->op_code
= MSCHAPV2_OP_CHANGE_PASSWORD
;
536 ms
->mschapv2_id
= req
->mschapv2_id
+ 1;
537 WPA_PUT_BE16(ms
->ms_length
, ms_len
);
538 cp
= wpabuf_put(resp
, sizeof(*cp
));
540 /* Encrypted-Password */
542 if (encrypt_pw_block_with_password_hash(
543 new_password
, new_password_len
,
544 password
, cp
->encr_password
))
547 if (new_password_encrypted_with_old_nt_password_hash(
548 new_password
, new_password_len
,
549 password
, password_len
, cp
->encr_password
))
555 u8 new_password_hash
[16];
556 nt_password_hash(new_password
, new_password_len
,
558 nt_password_hash_encrypted_with_block(password
,
562 old_nt_password_hash_encrypted_with_new_nt_password_hash(
563 new_password
, new_password_len
,
564 password
, password_len
, cp
->encr_hash
);
568 if (random_get_bytes(cp
->peer_challenge
, MSCHAPV2_CHAL_LEN
))
571 /* Reserved, must be zero */
572 os_memset(cp
->reserved
, 0, 8);
575 wpa_hexdump(MSG_DEBUG
, "EAP-MSCHAPV2: auth_challenge",
576 data
->passwd_change_challenge
, PASSWD_CHANGE_CHAL_LEN
);
577 wpa_hexdump(MSG_DEBUG
, "EAP-MSCHAPV2: peer_challenge",
578 cp
->peer_challenge
, MSCHAPV2_CHAL_LEN
);
579 wpa_hexdump_ascii(MSG_DEBUG
, "EAP-MSCHAPV2: username",
580 username
, username_len
);
581 wpa_hexdump_ascii_key(MSG_DEBUG
, "EAP-MSCHAPV2: new password",
582 new_password
, new_password_len
);
583 generate_nt_response(data
->passwd_change_challenge
, cp
->peer_challenge
,
584 username
, username_len
,
585 new_password
, new_password_len
,
587 wpa_hexdump(MSG_DEBUG
, "EAP-MSCHAPV2: NT-Response",
588 cp
->nt_response
, MSCHAPV2_NT_RESPONSE_LEN
);
590 /* Authenticator response is not really needed yet, but calculate it
591 * here so that challenges need not be saved. */
592 generate_authenticator_response(new_password
, new_password_len
,
594 data
->passwd_change_challenge
,
595 username
, username_len
,
596 cp
->nt_response
, data
->auth_response
);
597 data
->auth_response_valid
= 1;
599 /* Likewise, generate master_key here since we have the needed data
601 nt_password_hash(new_password
, new_password_len
, password_hash
);
602 hash_nt_password_hash(password_hash
, password_hash_hash
);
603 get_master_key(password_hash_hash
, cp
->nt_response
, data
->master_key
);
604 data
->master_key_valid
= 1;
607 os_memset(cp
->flags
, 0, 2);
609 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: TX identifier %d mschapv2_id %d "
610 "(change pw)", id
, ms
->mschapv2_id
);
621 * eap_mschapv2_process - Process an EAP-MSCHAPv2 failure message
622 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
623 * @data: Pointer to private EAP method data from eap_mschapv2_init()
624 * @ret: Return values from EAP request validation and processing
625 * @req: Pointer to EAP-MSCHAPv2 header from the request
626 * @req_len: Length of the EAP-MSCHAPv2 data
627 * @id: EAP identifier used in th erequest
628 * Returns: Pointer to allocated EAP response packet (eapRespData) or %NULL if
631 static struct wpabuf
* eap_mschapv2_failure(struct eap_sm
*sm
,
632 struct eap_mschapv2_data
*data
,
633 struct eap_method_ret
*ret
,
634 const struct eap_mschapv2_hdr
*req
,
635 size_t req_len
, u8 id
)
638 const u8
*msdata
= (const u8
*) (req
+ 1);
640 size_t len
= req_len
- sizeof(*req
);
643 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: Received failure");
644 wpa_hexdump_ascii(MSG_DEBUG
, "EAP-MSCHAPV2: Failure data",
647 * eap_mschapv2_failure_txt() expects a nul terminated string, so we
648 * must allocate a large enough temporary buffer to create that since
649 * the received message does not include nul termination.
651 buf
= os_malloc(len
+ 1);
653 os_memcpy(buf
, msdata
, len
);
655 retry
= eap_mschapv2_failure_txt(sm
, data
, buf
);
660 ret
->methodState
= METHOD_DONE
;
661 ret
->decision
= DECISION_FAIL
;
662 ret
->allowNotifications
= FALSE
;
664 if (data
->prev_error
== ERROR_PASSWD_EXPIRED
&&
665 data
->passwd_change_version
== 3) {
666 struct eap_peer_config
*config
= eap_get_config(sm
);
667 if (config
&& config
->new_password
)
668 return eap_mschapv2_change_password(sm
, data
, ret
, req
,
670 if (config
&& config
->pending_req_new_password
)
672 } else if (retry
&& data
->prev_error
== ERROR_AUTHENTICATION_FAILURE
) {
673 /* TODO: could try to retry authentication, e.g, after having
674 * changed the username/password. In this case, EAP MS-CHAP-v2
675 * Failure Response would not be sent here. */
679 /* Note: Only op_code of the EAP-MSCHAPV2 header is included in failure
681 resp
= eap_msg_alloc(EAP_VENDOR_IETF
, EAP_TYPE_MSCHAPV2
, 1,
682 EAP_CODE_RESPONSE
, id
);
686 wpabuf_put_u8(resp
, MSCHAPV2_OP_FAILURE
); /* op_code */
692 static int eap_mschapv2_check_config(struct eap_sm
*sm
)
696 if (eap_get_config_identity(sm
, &len
) == NULL
) {
697 wpa_printf(MSG_INFO
, "EAP-MSCHAPV2: Identity not configured");
698 eap_sm_request_identity(sm
);
702 if (eap_get_config_password(sm
, &len
) == NULL
) {
703 wpa_printf(MSG_INFO
, "EAP-MSCHAPV2: Password not configured");
704 eap_sm_request_password(sm
);
712 static int eap_mschapv2_check_mslen(struct eap_sm
*sm
, size_t len
,
713 const struct eap_mschapv2_hdr
*ms
)
715 size_t ms_len
= WPA_GET_BE16(ms
->ms_length
);
720 wpa_printf(MSG_INFO
, "EAP-MSCHAPV2: Invalid header: len=%lu "
721 "ms_len=%lu", (unsigned long) len
, (unsigned long) ms_len
);
722 if (sm
->workaround
) {
723 /* Some authentication servers use invalid ms_len,
724 * ignore it for interoperability. */
725 wpa_printf(MSG_INFO
, "EAP-MSCHAPV2: workaround, ignore"
726 " invalid ms_len %lu (len %lu)",
727 (unsigned long) ms_len
,
728 (unsigned long) len
);
736 static void eap_mschapv2_copy_challenge(struct eap_mschapv2_data
*data
,
737 const struct wpabuf
*reqData
)
740 * Store a copy of the challenge message, so that it can be processed
741 * again in case retry is allowed after a possible failure.
743 wpabuf_free(data
->prev_challenge
);
744 data
->prev_challenge
= wpabuf_dup(reqData
);
749 * eap_mschapv2_process - Process an EAP-MSCHAPv2 request
750 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
751 * @priv: Pointer to private EAP method data from eap_mschapv2_init()
752 * @ret: Return values from EAP request validation and processing
753 * @reqData: EAP request to be processed (eapReqData)
754 * Returns: Pointer to allocated EAP response packet (eapRespData) or %NULL if
757 static struct wpabuf
* eap_mschapv2_process(struct eap_sm
*sm
, void *priv
,
758 struct eap_method_ret
*ret
,
759 const struct wpabuf
*reqData
)
761 struct eap_mschapv2_data
*data
= priv
;
762 struct eap_peer_config
*config
= eap_get_config(sm
);
763 const struct eap_mschapv2_hdr
*ms
;
764 int using_prev_challenge
= 0;
769 if (eap_mschapv2_check_config(sm
)) {
774 if (config
->mschapv2_retry
&& data
->prev_challenge
&&
775 data
->prev_error
== ERROR_AUTHENTICATION_FAILURE
) {
776 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: Replacing pending packet "
777 "with the previous challenge");
779 reqData
= data
->prev_challenge
;
780 using_prev_challenge
= 1;
781 config
->mschapv2_retry
= 0;
784 pos
= eap_hdr_validate(EAP_VENDOR_IETF
, EAP_TYPE_MSCHAPV2
, reqData
,
786 if (pos
== NULL
|| len
< sizeof(*ms
) + 1) {
791 ms
= (const struct eap_mschapv2_hdr
*) pos
;
792 if (eap_mschapv2_check_mslen(sm
, len
, ms
)) {
797 id
= eap_get_id(reqData
);
798 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: RX identifier %d mschapv2_id %d",
799 id
, ms
->mschapv2_id
);
801 switch (ms
->op_code
) {
802 case MSCHAPV2_OP_CHALLENGE
:
803 if (!using_prev_challenge
)
804 eap_mschapv2_copy_challenge(data
, reqData
);
805 return eap_mschapv2_challenge(sm
, data
, ret
, ms
, len
, id
);
806 case MSCHAPV2_OP_SUCCESS
:
807 return eap_mschapv2_success(sm
, data
, ret
, ms
, len
, id
);
808 case MSCHAPV2_OP_FAILURE
:
809 return eap_mschapv2_failure(sm
, data
, ret
, ms
, len
, id
);
811 wpa_printf(MSG_INFO
, "EAP-MSCHAPV2: Unknown op %d - ignored",
819 static Boolean
eap_mschapv2_isKeyAvailable(struct eap_sm
*sm
, void *priv
)
821 struct eap_mschapv2_data
*data
= priv
;
822 return data
->success
&& data
->master_key_valid
;
826 static u8
* eap_mschapv2_getKey(struct eap_sm
*sm
, void *priv
, size_t *len
)
828 struct eap_mschapv2_data
*data
= priv
;
832 if (!data
->master_key_valid
|| !data
->success
)
835 key_len
= 2 * MSCHAPV2_KEY_LEN
;
837 key
= os_malloc(key_len
);
841 /* MSK = server MS-MPPE-Recv-Key | MS-MPPE-Send-Key, i.e.,
842 * peer MS-MPPE-Send-Key | MS-MPPE-Recv-Key */
843 get_asymetric_start_key(data
->master_key
, key
, MSCHAPV2_KEY_LEN
, 1, 0);
844 get_asymetric_start_key(data
->master_key
, key
+ MSCHAPV2_KEY_LEN
,
845 MSCHAPV2_KEY_LEN
, 0, 0);
847 wpa_hexdump_key(MSG_DEBUG
, "EAP-MSCHAPV2: Derived key",
856 * eap_peer_mschapv2_register - Register EAP-MSCHAPv2 peer method
857 * Returns: 0 on success, -1 on failure
859 * This function is used to register EAP-MSCHAPv2 peer method into the EAP
862 int eap_peer_mschapv2_register(void)
864 struct eap_method
*eap
;
867 eap
= eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION
,
868 EAP_VENDOR_IETF
, EAP_TYPE_MSCHAPV2
,
873 eap
->init
= eap_mschapv2_init
;
874 eap
->deinit
= eap_mschapv2_deinit
;
875 eap
->process
= eap_mschapv2_process
;
876 eap
->isKeyAvailable
= eap_mschapv2_isKeyAvailable
;
877 eap
->getKey
= eap_mschapv2_getKey
;
879 ret
= eap_peer_method_register(eap
);
881 eap_peer_method_free(eap
);