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 software may be distributed under the terms of the BSD license.
6 * See README for more details.
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
19 #include "crypto/ms_funcs.h"
20 #include "crypto/random.h"
21 #include "common/wpa_ctrl.h"
24 #include "eap_config.h"
31 struct 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 */
39 /* Response Data field */
41 u8 peer_challenge[MSCHAPV2_CHAL_LEN];
43 u8 nt_response[MSCHAPV2_NT_RESPONSE_LEN];
47 /* Change-Password Data field */
48 struct ms_change_password {
49 u8 encr_password[516];
51 u8 peer_challenge[MSCHAPV2_CHAL_LEN];
53 u8 nt_response[MSCHAPV2_NT_RESPONSE_LEN];
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
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
74 #define PASSWD_CHANGE_CHAL_LEN 16
75 #define MSCHAPV2_KEY_LEN 16
78 struct eap_mschapv2_data {
79 u8 auth_response[MSCHAPV2_AUTH_RESPONSE_LEN];
80 int auth_response_valid;
83 u8 passwd_change_challenge[PASSWD_CHANGE_CHAL_LEN];
84 int passwd_change_challenge_valid;
85 int passwd_change_version;
87 /* Optional challenge values generated in EAP-FAST Phase 1 negotiation
93 u8 master_key[MSCHAPV2_MASTER_KEY_LEN];
97 struct wpabuf *prev_challenge;
101 static void eap_mschapv2_deinit(struct eap_sm *sm, void *priv);
104 static void * eap_mschapv2_init(struct eap_sm *sm)
106 struct eap_mschapv2_data *data;
107 data = os_zalloc(sizeof(*data));
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);
117 os_memcpy(data->peer_challenge, sm->peer_challenge,
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);
127 os_memcpy(data->auth_challenge, sm->auth_challenge,
131 data->phase2 = sm->init_phase2;
137 static void eap_mschapv2_deinit(struct eap_sm *sm, void *priv)
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);
147 static 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)
152 struct eap_mschapv2_hdr *ms;
155 struct ms_response *r;
156 size_t identity_len, password_len;
157 const u8 *identity, *password;
160 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Generating Challenge Response");
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)
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);
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) {
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(?).
185 WPA_PUT_BE16(ms->ms_length, ms_len);
187 wpabuf_put_u8(resp, sizeof(*r)); /* Value-Size */
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 "
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)) {
201 os_memset(r->reserved, 0, 8);
202 if (data->auth_challenge) {
203 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: auth_challenge generated "
205 auth_challenge = data->auth_challenge;
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 "
216 data->auth_response_valid = 1;
217 data->master_key_valid = 1;
219 r->flags = 0; /* reserved, must be zero */
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);
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
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)
244 size_t len, challenge_len;
245 const u8 *pos, *challenge;
247 if (eap_get_config_identity(sm, &len) == NULL ||
248 eap_get_config_password(sm, &len) == NULL)
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);
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);
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);
276 if (data->passwd_change_challenge_valid) {
277 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Using challenge from the "
279 challenge = data->passwd_change_challenge;
282 pos += challenge_len;
283 len -= challenge_len;
284 wpa_hexdump_ascii(MSG_DEBUG, "EAP-MSCHAPV2: Authentication Servername",
288 ret->methodState = METHOD_MAY_CONT;
289 ret->decision = DECISION_FAIL;
290 ret->allowNotifications = TRUE;
292 return eap_mschapv2_challenge_reply(sm, data, id, req->mschapv2_id,
297 static void eap_mschapv2_password_changed(struct eap_sm *sm,
298 struct eap_mschapv2_data *data)
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,
317 os_free(config->new_password);
319 config->password = config->new_password;
320 config->password_len = config->new_password_len;
322 config->new_password = NULL;
323 config->new_password_len = 0;
329 * eap_mschapv2_process - Process an EAP-MSCHAPv2 success message
330 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
331 * @data: Pointer to private EAP method data from eap_mschapv2_init()
332 * @ret: Return values from EAP request validation and processing
333 * @req: Pointer to EAP-MSCHAPv2 header from the request
334 * @req_len: Length of the EAP-MSCHAPv2 data
335 * @id: EAP identifier used in th erequest
336 * Returns: Pointer to allocated EAP response packet (eapRespData) or %NULL if
339 static struct wpabuf * eap_mschapv2_success(struct eap_sm *sm,
340 struct eap_mschapv2_data *data,
341 struct eap_method_ret *ret,
342 const struct eap_mschapv2_hdr *req,
343 size_t req_len, u8 id)
349 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Received success");
350 len = req_len - sizeof(*req);
351 pos = (const u8 *) (req + 1);
352 if (!data->auth_response_valid ||
353 mschapv2_verify_auth_response(data->auth_response, pos, len)) {
354 wpa_printf(MSG_WARNING, "EAP-MSCHAPV2: Invalid authenticator "
355 "response in success request");
356 ret->methodState = METHOD_DONE;
357 ret->decision = DECISION_FAIL;
360 pos += 2 + 2 * MSCHAPV2_AUTH_RESPONSE_LEN;
361 len -= 2 + 2 * MSCHAPV2_AUTH_RESPONSE_LEN;
362 while (len > 0 && *pos == ' ') {
366 wpa_hexdump_ascii(MSG_DEBUG, "EAP-MSCHAPV2: Success message",
368 wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Authentication succeeded");
370 /* Note: Only op_code of the EAP-MSCHAPV2 header is included in success
372 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, 1,
373 EAP_CODE_RESPONSE, id);
375 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Failed to allocate "
376 "buffer for success response");
381 wpabuf_put_u8(resp, MSCHAPV2_OP_SUCCESS); /* op_code */
383 ret->methodState = METHOD_DONE;
384 ret->decision = DECISION_UNCOND_SUCC;
385 ret->allowNotifications = FALSE;
388 if (data->prev_error == ERROR_PASSWD_EXPIRED)
389 eap_mschapv2_password_changed(sm, data);
395 static int eap_mschapv2_failure_txt(struct eap_sm *sm,
396 struct eap_mschapv2_data *data, char *txt)
398 char *pos, *msg = "";
400 struct eap_peer_config *config = eap_get_config(sm);
403 * E=691 R=1 C=<32 octets hex challenge> V=3 M=Authentication Failure
408 if (pos && os_strncmp(pos, "E=", 2) == 0) {
410 data->prev_error = atoi(pos);
411 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: error %d",
413 pos = os_strchr(pos, ' ');
418 if (pos && os_strncmp(pos, "R=", 2) == 0) {
421 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: retry is %sallowed",
422 retry == 1 ? "" : "not ");
423 pos = os_strchr(pos, ' ');
428 if (pos && os_strncmp(pos, "C=", 2) == 0) {
431 hex_len = os_strchr(pos, ' ') - (char *) pos;
432 if (hex_len == PASSWD_CHANGE_CHAL_LEN * 2) {
433 if (hexstr2bin(pos, data->passwd_change_challenge,
434 PASSWD_CHANGE_CHAL_LEN)) {
435 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: invalid "
436 "failure challenge");
438 wpa_hexdump(MSG_DEBUG, "EAP-MSCHAPV2: failure "
440 data->passwd_change_challenge,
441 PASSWD_CHANGE_CHAL_LEN);
442 data->passwd_change_challenge_valid = 1;
445 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: invalid failure "
446 "challenge len %d", hex_len);
448 pos = os_strchr(pos, ' ');
452 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: required challenge field "
453 "was not present in failure message");
456 if (pos && os_strncmp(pos, "V=", 2) == 0) {
458 data->passwd_change_version = atoi(pos);
459 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: password changing "
460 "protocol version %d", data->passwd_change_version);
461 pos = os_strchr(pos, ' ');
466 if (pos && os_strncmp(pos, "M=", 2) == 0) {
470 wpa_msg(sm->msg_ctx, MSG_WARNING,
471 "EAP-MSCHAPV2: failure message: '%s' (retry %sallowed, error "
473 msg, retry == 1 ? "" : "not ", data->prev_error);
474 if (data->prev_error == ERROR_PASSWD_EXPIRED &&
475 data->passwd_change_version == 3 && config) {
476 if (config->new_password == NULL) {
477 wpa_msg(sm->msg_ctx, MSG_INFO,
478 "EAP-MSCHAPV2: Password expired - password "
480 eap_sm_request_new_password(sm);
482 } else if (retry == 1 && config) {
483 /* TODO: could prevent the current password from being used
484 * again at least for some period of time */
485 if (!config->mschapv2_retry)
486 eap_sm_request_identity(sm);
487 eap_sm_request_password(sm);
488 config->mschapv2_retry = 1;
490 /* TODO: prevent retries using same username/password */
491 config->mschapv2_retry = 0;
498 static struct wpabuf * eap_mschapv2_change_password(
499 struct eap_sm *sm, struct eap_mschapv2_data *data,
500 struct eap_method_ret *ret, const struct eap_mschapv2_hdr *req, u8 id)
504 const u8 *username, *password, *new_password;
505 size_t username_len, password_len, new_password_len;
506 struct eap_mschapv2_hdr *ms;
507 struct ms_change_password *cp;
508 u8 password_hash[16], password_hash_hash[16];
511 username = eap_get_config_identity(sm, &username_len);
512 password = eap_get_config_password2(sm, &password_len, &pwhash);
513 new_password = eap_get_config_new_password(sm, &new_password_len);
514 if (username == NULL || password == NULL || new_password == NULL)
517 username = mschapv2_remove_domain(username, &username_len);
520 ret->methodState = METHOD_MAY_CONT;
521 ret->decision = DECISION_COND_SUCC;
522 ret->allowNotifications = TRUE;
524 ms_len = sizeof(*ms) + sizeof(*cp);
525 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, ms_len,
526 EAP_CODE_RESPONSE, id);
530 ms = wpabuf_put(resp, sizeof(*ms));
531 ms->op_code = MSCHAPV2_OP_CHANGE_PASSWORD;
532 ms->mschapv2_id = req->mschapv2_id + 1;
533 WPA_PUT_BE16(ms->ms_length, ms_len);
534 cp = wpabuf_put(resp, sizeof(*cp));
536 /* Encrypted-Password */
538 if (encrypt_pw_block_with_password_hash(
539 new_password, new_password_len,
540 password, cp->encr_password))
543 if (new_password_encrypted_with_old_nt_password_hash(
544 new_password, new_password_len,
545 password, password_len, cp->encr_password))
551 u8 new_password_hash[16];
552 nt_password_hash(new_password, new_password_len,
554 nt_password_hash_encrypted_with_block(password,
558 old_nt_password_hash_encrypted_with_new_nt_password_hash(
559 new_password, new_password_len,
560 password, password_len, cp->encr_hash);
564 if (random_get_bytes(cp->peer_challenge, MSCHAPV2_CHAL_LEN))
567 /* Reserved, must be zero */
568 os_memset(cp->reserved, 0, 8);
571 wpa_hexdump(MSG_DEBUG, "EAP-MSCHAPV2: auth_challenge",
572 data->passwd_change_challenge, PASSWD_CHANGE_CHAL_LEN);
573 wpa_hexdump(MSG_DEBUG, "EAP-MSCHAPV2: peer_challenge",
574 cp->peer_challenge, MSCHAPV2_CHAL_LEN);
575 wpa_hexdump_ascii(MSG_DEBUG, "EAP-MSCHAPV2: username",
576 username, username_len);
577 wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-MSCHAPV2: new password",
578 new_password, new_password_len);
579 generate_nt_response(data->passwd_change_challenge, cp->peer_challenge,
580 username, username_len,
581 new_password, new_password_len,
583 wpa_hexdump(MSG_DEBUG, "EAP-MSCHAPV2: NT-Response",
584 cp->nt_response, MSCHAPV2_NT_RESPONSE_LEN);
586 /* Authenticator response is not really needed yet, but calculate it
587 * here so that challenges need not be saved. */
588 generate_authenticator_response(new_password, new_password_len,
590 data->passwd_change_challenge,
591 username, username_len,
592 cp->nt_response, data->auth_response);
593 data->auth_response_valid = 1;
595 /* Likewise, generate master_key here since we have the needed data
597 nt_password_hash(new_password, new_password_len, password_hash);
598 hash_nt_password_hash(password_hash, password_hash_hash);
599 get_master_key(password_hash_hash, cp->nt_response, data->master_key);
600 data->master_key_valid = 1;
603 os_memset(cp->flags, 0, 2);
605 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: TX identifier %d mschapv2_id %d "
606 "(change pw)", id, ms->mschapv2_id);
617 * eap_mschapv2_process - Process an EAP-MSCHAPv2 failure message
618 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
619 * @data: Pointer to private EAP method data from eap_mschapv2_init()
620 * @ret: Return values from EAP request validation and processing
621 * @req: Pointer to EAP-MSCHAPv2 header from the request
622 * @req_len: Length of the EAP-MSCHAPv2 data
623 * @id: EAP identifier used in th erequest
624 * Returns: Pointer to allocated EAP response packet (eapRespData) or %NULL if
627 static struct wpabuf * eap_mschapv2_failure(struct eap_sm *sm,
628 struct eap_mschapv2_data *data,
629 struct eap_method_ret *ret,
630 const struct eap_mschapv2_hdr *req,
631 size_t req_len, u8 id)
634 const u8 *msdata = (const u8 *) (req + 1);
636 size_t len = req_len - sizeof(*req);
639 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Received failure");
640 wpa_hexdump_ascii(MSG_DEBUG, "EAP-MSCHAPV2: Failure data",
643 * eap_mschapv2_failure_txt() expects a nul terminated string, so we
644 * must allocate a large enough temporary buffer to create that since
645 * the received message does not include nul termination.
647 buf = dup_binstr(msdata, len);
649 retry = eap_mschapv2_failure_txt(sm, data, buf);
654 ret->methodState = METHOD_DONE;
655 ret->decision = DECISION_FAIL;
656 ret->allowNotifications = FALSE;
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,
664 if (config && config->pending_req_new_password)
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. */
673 /* Note: Only op_code of the EAP-MSCHAPV2 header is included in failure
675 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, 1,
676 EAP_CODE_RESPONSE, id);
680 wpabuf_put_u8(resp, MSCHAPV2_OP_FAILURE); /* op_code */
686 static int eap_mschapv2_check_config(struct eap_sm *sm)
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);
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);
706 static int eap_mschapv2_check_mslen(struct eap_sm *sm, size_t len,
707 const struct eap_mschapv2_hdr *ms)
709 size_t ms_len = WPA_GET_BE16(ms->ms_length);
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);
730 static void eap_mschapv2_copy_challenge(struct eap_mschapv2_data *data,
731 const struct wpabuf *reqData)
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.
737 wpabuf_free(data->prev_challenge);
738 data->prev_challenge = wpabuf_dup(reqData);
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
751 static struct wpabuf * eap_mschapv2_process(struct eap_sm *sm, void *priv,
752 struct eap_method_ret *ret,
753 const struct wpabuf *reqData)
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;
763 if (eap_mschapv2_check_config(sm)) {
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");
773 reqData = data->prev_challenge;
774 using_prev_challenge = 1;
775 config->mschapv2_retry = 0;
778 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, reqData,
780 if (pos == NULL || len < sizeof(*ms) + 1) {
785 ms = (const struct eap_mschapv2_hdr *) pos;
786 if (eap_mschapv2_check_mslen(sm, len, ms)) {
791 id = eap_get_id(reqData);
792 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: RX identifier %d mschapv2_id %d",
793 id, ms->mschapv2_id);
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);
805 wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Unknown op %d - ignored",
813 static Boolean eap_mschapv2_isKeyAvailable(struct eap_sm *sm, void *priv)
815 struct eap_mschapv2_data *data = priv;
816 return data->success && data->master_key_valid;
820 static u8 * eap_mschapv2_getKey(struct eap_sm *sm, void *priv, size_t *len)
822 struct eap_mschapv2_data *data = priv;
826 if (!data->master_key_valid || !data->success)
829 key_len = 2 * MSCHAPV2_KEY_LEN;
831 key = os_malloc(key_len);
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);
841 wpa_hexdump_key(MSG_DEBUG, "EAP-MSCHAPV2: Derived key",
850 * eap_peer_mschapv2_register - Register EAP-MSCHAPv2 peer method
851 * Returns: 0 on success, -1 on failure
853 * This function is used to register EAP-MSCHAPv2 peer method into the EAP
856 int eap_peer_mschapv2_register(void)
858 struct eap_method *eap;
861 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
862 EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2,
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;
873 ret = eap_peer_method_register(eap);
875 eap_peer_method_free(eap);