eap_mschapv2.c
Go to the documentation of this file.
00001 /*
00002  * EAP peer method: EAP-MSCHAPV2 (draft-kamath-pppext-eap-mschapv2-00.txt)
00003  * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 2 as
00007  * published by the Free Software Foundation.
00008  *
00009  * Alternatively, this software may be distributed under the terms of BSD
00010  * license.
00011  *
00012  * See README and COPYING for more details.
00013  *
00014  * This file implements EAP peer part of EAP-MSCHAPV2 method (EAP type 26).
00015  * draft-kamath-pppext-eap-mschapv2-00.txt defines the Microsoft EAP CHAP
00016  * Extensions Protocol, Version 2, for mutual authentication and key
00017  * derivation. This encapsulates MS-CHAP-v2 protocol which is defined in
00018  * RFC 2759. Use of EAP-MSCHAPV2 derived keys with MPPE cipher is described in
00019  * RFC 3079.
00020  */
00021 
00022 #include "includes.h"
00023 
00024 #include "common.h"
00025 #include "crypto/ms_funcs.h"
00026 #include "common/wpa_ctrl.h"
00027 #include "mschapv2.h"
00028 #include "eap_i.h"
00029 #include "eap_config.h"
00030 
00031 
00032 #ifdef _MSC_VER
00033 #pragma pack(push, 1)
00034 #endif /* _MSC_VER */
00035 
00036 struct eap_mschapv2_hdr {
00037         u8 op_code; /* MSCHAPV2_OP_* */
00038         u8 mschapv2_id; /* usually same as EAP identifier; must be changed
00039                          * for challenges, but not for success/failure */
00040         u8 ms_length[2]; /* Note: misaligned; length - 5 */
00041         /* followed by data */
00042 } STRUCT_PACKED;
00043 
00044 /* Response Data field */
00045 struct ms_response {
00046         u8 peer_challenge[MSCHAPV2_CHAL_LEN];
00047         u8 reserved[8];
00048         u8 nt_response[MSCHAPV2_NT_RESPONSE_LEN];
00049         u8 flags;
00050 } STRUCT_PACKED;
00051 
00052 /* Change-Password Data field */
00053 struct ms_change_password {
00054         u8 encr_password[516];
00055         u8 encr_hash[16];
00056         u8 peer_challenge[MSCHAPV2_CHAL_LEN];
00057         u8 reserved[8];
00058         u8 nt_response[MSCHAPV2_NT_RESPONSE_LEN];
00059         u8 flags[2];
00060 } STRUCT_PACKED;
00061 
00062 #ifdef _MSC_VER
00063 #pragma pack(pop)
00064 #endif /* _MSC_VER */
00065 
00066 #define MSCHAPV2_OP_CHALLENGE 1
00067 #define MSCHAPV2_OP_RESPONSE 2
00068 #define MSCHAPV2_OP_SUCCESS 3
00069 #define MSCHAPV2_OP_FAILURE 4
00070 #define MSCHAPV2_OP_CHANGE_PASSWORD 7
00071 
00072 #define ERROR_RESTRICTED_LOGON_HOURS 646
00073 #define ERROR_ACCT_DISABLED 647
00074 #define ERROR_PASSWD_EXPIRED 648
00075 #define ERROR_NO_DIALIN_PERMISSION 649
00076 #define ERROR_AUTHENTICATION_FAILURE 691
00077 #define ERROR_CHANGING_PASSWORD 709
00078 
00079 #define PASSWD_CHANGE_CHAL_LEN 16
00080 #define MSCHAPV2_KEY_LEN 16
00081 
00082 
00083 struct eap_mschapv2_data {
00084         u8 auth_response[MSCHAPV2_AUTH_RESPONSE_LEN];
00085         int auth_response_valid;
00086 
00087         int prev_error;
00088         u8 passwd_change_challenge[PASSWD_CHANGE_CHAL_LEN];
00089         int passwd_change_challenge_valid;
00090         int passwd_change_version;
00091 
00092         /* Optional challenge values generated in EAP-FAST Phase 1 negotiation
00093          */
00094         u8 *peer_challenge;
00095         u8 *auth_challenge;
00096 
00097         int phase2;
00098         u8 master_key[MSCHAPV2_MASTER_KEY_LEN];
00099         int master_key_valid;
00100         int success;
00101 
00102         struct wpabuf *prev_challenge;
00103 };
00104 
00105 
00106 static void eap_mschapv2_deinit(struct eap_sm *sm, void *priv);
00107 
00108 
00109 static void * eap_mschapv2_init(struct eap_sm *sm)
00110 {
00111         struct eap_mschapv2_data *data;
00112         data = os_zalloc(sizeof(*data));
00113         if (data == NULL)
00114                 return NULL;
00115 
00116         if (sm->peer_challenge) {
00117                 data->peer_challenge = os_malloc(MSCHAPV2_CHAL_LEN);
00118                 if (data->peer_challenge == NULL) {
00119                         eap_mschapv2_deinit(sm, data);
00120                         return NULL;
00121                 }
00122                 os_memcpy(data->peer_challenge, sm->peer_challenge,
00123                           MSCHAPV2_CHAL_LEN);
00124         }
00125 
00126         if (sm->auth_challenge) {
00127                 data->auth_challenge = os_malloc(MSCHAPV2_CHAL_LEN);
00128                 if (data->auth_challenge == NULL) {
00129                         eap_mschapv2_deinit(sm, data);
00130                         return NULL;
00131                 }
00132                 os_memcpy(data->auth_challenge, sm->auth_challenge,
00133                           MSCHAPV2_CHAL_LEN);
00134         }
00135 
00136         data->phase2 = sm->init_phase2;
00137 
00138         return data;
00139 }
00140 
00141 
00142 static void eap_mschapv2_deinit(struct eap_sm *sm, void *priv)
00143 {
00144         struct eap_mschapv2_data *data = priv;
00145         os_free(data->peer_challenge);
00146         os_free(data->auth_challenge);
00147         wpabuf_free(data->prev_challenge);
00148         os_free(data);
00149 }
00150 
00151 
00152 static struct wpabuf * eap_mschapv2_challenge_reply(
00153         struct eap_sm *sm, struct eap_mschapv2_data *data, u8 id,
00154         u8 mschapv2_id, const u8 *auth_challenge)
00155 {
00156         struct wpabuf *resp;
00157         struct eap_mschapv2_hdr *ms;
00158         u8 *peer_challenge;
00159         int ms_len;
00160         struct ms_response *r;
00161         size_t identity_len, password_len;
00162         const u8 *identity, *password;
00163         int pwhash;
00164 
00165         wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Generating Challenge Response");
00166 
00167         identity = eap_get_config_identity(sm, &identity_len);
00168         password = eap_get_config_password2(sm, &password_len, &pwhash);
00169         if (identity == NULL || password == NULL)
00170                 return NULL;
00171 
00172         ms_len = sizeof(*ms) + 1 + sizeof(*r) + identity_len;
00173         resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, ms_len,
00174                              EAP_CODE_RESPONSE, id);
00175         if (resp == NULL)
00176                 return NULL;
00177 
00178         ms = wpabuf_put(resp, sizeof(*ms));
00179         ms->op_code = MSCHAPV2_OP_RESPONSE;
00180         ms->mschapv2_id = mschapv2_id;
00181         if (data->prev_error) {
00182                 /*
00183                  * TODO: this does not seem to be enough when processing two
00184                  * or more failure messages. IAS did not increment mschapv2_id
00185                  * in its own packets, but it seemed to expect the peer to
00186                  * increment this for all packets(?).
00187                  */
00188                 ms->mschapv2_id++;
00189         }
00190         WPA_PUT_BE16(ms->ms_length, ms_len);
00191 
00192         wpabuf_put_u8(resp, sizeof(*r)); /* Value-Size */
00193 
00194         /* Response */
00195         r = wpabuf_put(resp, sizeof(*r));
00196         peer_challenge = r->peer_challenge;
00197         if (data->peer_challenge) {
00198                 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: peer_challenge generated "
00199                            "in Phase 1");
00200                 peer_challenge = data->peer_challenge;
00201                 os_memset(r->peer_challenge, 0, MSCHAPV2_CHAL_LEN);
00202         } else if (os_get_random(peer_challenge, MSCHAPV2_CHAL_LEN)) {
00203                 wpabuf_free(resp);
00204                 return NULL;
00205         }
00206         os_memset(r->reserved, 0, 8);
00207         if (data->auth_challenge) {
00208                 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: auth_challenge generated "
00209                            "in Phase 1");
00210                 auth_challenge = data->auth_challenge;
00211         }
00212         if (mschapv2_derive_response(identity, identity_len, password,
00213                                      password_len, pwhash, auth_challenge,
00214                                      peer_challenge, r->nt_response,
00215                                      data->auth_response, data->master_key)) {
00216                 wpa_printf(MSG_ERROR, "EAP-MSCHAPV2: Failed to derive "
00217                            "response");
00218                 wpabuf_free(resp);
00219                 return NULL;
00220         }
00221         data->auth_response_valid = 1;
00222         data->master_key_valid = 1;
00223 
00224         r->flags = 0; /* reserved, must be zero */
00225 
00226         wpabuf_put_data(resp, identity, identity_len);
00227         wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: TX identifier %d mschapv2_id %d "
00228                    "(response)", id, ms->mschapv2_id);
00229         return resp;
00230 }
00231 
00232 
00244 static struct wpabuf * eap_mschapv2_challenge(
00245         struct eap_sm *sm, struct eap_mschapv2_data *data,
00246         struct eap_method_ret *ret, const struct eap_mschapv2_hdr *req,
00247         size_t req_len, u8 id)
00248 {
00249         size_t len, challenge_len;
00250         const u8 *pos, *challenge;
00251 
00252         if (eap_get_config_identity(sm, &len) == NULL ||
00253             eap_get_config_password(sm, &len) == NULL)
00254                 return NULL;
00255 
00256         wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Received challenge");
00257         if (req_len < sizeof(*req) + 1) {
00258                 wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Too short challenge data "
00259                            "(len %lu)", (unsigned long) req_len);
00260                 ret->ignore = TRUE;
00261                 return NULL;
00262         }
00263         pos = (const u8 *) (req + 1);
00264         challenge_len = *pos++;
00265         len = req_len - sizeof(*req) - 1;
00266         if (challenge_len != MSCHAPV2_CHAL_LEN) {
00267                 wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Invalid challenge length "
00268                            "%lu", (unsigned long) challenge_len);
00269                 ret->ignore = TRUE;
00270                 return NULL;
00271         }
00272 
00273         if (len < challenge_len) {
00274                 wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Too short challenge"
00275                            " packet: len=%lu challenge_len=%lu",
00276                            (unsigned long) len, (unsigned long) challenge_len);
00277                 ret->ignore = TRUE;
00278                 return NULL;
00279         }
00280 
00281         if (data->passwd_change_challenge_valid) {
00282                 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Using challenge from the "
00283                            "failure message");
00284                 challenge = data->passwd_change_challenge;
00285         } else
00286                 challenge = pos;
00287         pos += challenge_len;
00288         len -= challenge_len;
00289         wpa_hexdump_ascii(MSG_DEBUG, "EAP-MSCHAPV2: Authentication Servername",
00290                     pos, len);
00291 
00292         ret->ignore = FALSE;
00293         ret->methodState = METHOD_MAY_CONT;
00294         ret->decision = DECISION_FAIL;
00295         ret->allowNotifications = TRUE;
00296 
00297         return eap_mschapv2_challenge_reply(sm, data, id, req->mschapv2_id,
00298                                             challenge);
00299 }
00300 
00301 
00302 static void eap_mschapv2_password_changed(struct eap_sm *sm,
00303                                           struct eap_mschapv2_data *data)
00304 {
00305         struct eap_peer_config *config = eap_get_config(sm);
00306         if (config && config->new_password) {
00307                 wpa_msg(sm->msg_ctx, MSG_INFO,
00308                         WPA_EVENT_PASSWORD_CHANGED
00309                         "EAP-MSCHAPV2: Password changed successfully");
00310                 data->prev_error = 0;
00311                 os_free(config->password);
00312                 if (config->flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH) {
00313                         config->password = os_malloc(16);
00314                         config->password_len = 16;
00315                         if (config->password) {
00316                                 nt_password_hash(config->new_password,
00317                                                  config->new_password_len,
00318                                                  config->password);
00319                         }
00320                         os_free(config->new_password);
00321                 } else {
00322                         config->password = config->new_password;
00323                         config->password_len = config->new_password_len;
00324                 }
00325                 config->new_password = NULL;
00326                 config->new_password_len = 0;
00327         }
00328 }
00329 
00330 
00342 static struct wpabuf * eap_mschapv2_success(struct eap_sm *sm,
00343                                             struct eap_mschapv2_data *data,
00344                                             struct eap_method_ret *ret,
00345                                             const struct eap_mschapv2_hdr *req,
00346                                             size_t req_len, u8 id)
00347 {
00348         struct wpabuf *resp;
00349         const u8 *pos;
00350         size_t len;
00351 
00352         wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Received success");
00353         len = req_len - sizeof(*req);
00354         pos = (const u8 *) (req + 1);
00355         if (!data->auth_response_valid ||
00356             mschapv2_verify_auth_response(data->auth_response, pos, len)) {
00357                 wpa_printf(MSG_WARNING, "EAP-MSCHAPV2: Invalid authenticator "
00358                            "response in success request");
00359                 ret->methodState = METHOD_DONE;
00360                 ret->decision = DECISION_FAIL;
00361                 return NULL;
00362         }
00363         pos += 2 + 2 * MSCHAPV2_AUTH_RESPONSE_LEN;
00364         len -= 2 + 2 * MSCHAPV2_AUTH_RESPONSE_LEN;
00365         while (len > 0 && *pos == ' ') {
00366                 pos++;
00367                 len--;
00368         }
00369         wpa_hexdump_ascii(MSG_DEBUG, "EAP-MSCHAPV2: Success message",
00370                           pos, len);
00371         wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Authentication succeeded");
00372 
00373         /* Note: Only op_code of the EAP-MSCHAPV2 header is included in success
00374          * message. */
00375         resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, 1,
00376                              EAP_CODE_RESPONSE, id);
00377         if (resp == NULL) {
00378                 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Failed to allocate "
00379                            "buffer for success response");
00380                 ret->ignore = TRUE;
00381                 return NULL;
00382         }
00383 
00384         wpabuf_put_u8(resp, MSCHAPV2_OP_SUCCESS); /* op_code */
00385 
00386         ret->methodState = METHOD_DONE;
00387         ret->decision = DECISION_UNCOND_SUCC;
00388         ret->allowNotifications = FALSE;
00389         data->success = 1;
00390 
00391         if (data->prev_error == ERROR_PASSWD_EXPIRED)
00392                 eap_mschapv2_password_changed(sm, data);
00393 
00394         return resp;
00395 }
00396 
00397 
00398 static int eap_mschapv2_failure_txt(struct eap_sm *sm,
00399                                     struct eap_mschapv2_data *data, char *txt)
00400 {
00401         char *pos, *msg = "";
00402         int retry = 1;
00403         struct eap_peer_config *config = eap_get_config(sm);
00404 
00405         /* For example:
00406          * E=691 R=1 C=<32 octets hex challenge> V=3 M=Authentication Failure
00407          */
00408 
00409         pos = txt;
00410 
00411         if (pos && os_strncmp(pos, "E=", 2) == 0) {
00412                 pos += 2;
00413                 data->prev_error = atoi(pos);
00414                 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: error %d",
00415                            data->prev_error);
00416                 pos = os_strchr(pos, ' ');
00417                 if (pos)
00418                         pos++;
00419         }
00420 
00421         if (pos && os_strncmp(pos, "R=", 2) == 0) {
00422                 pos += 2;
00423                 retry = atoi(pos);
00424                 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: retry is %sallowed",
00425                            retry == 1 ? "" : "not ");
00426                 pos = os_strchr(pos, ' ');
00427                 if (pos)
00428                         pos++;
00429         }
00430 
00431         if (pos && os_strncmp(pos, "C=", 2) == 0) {
00432                 int hex_len;
00433                 pos += 2;
00434                 hex_len = os_strchr(pos, ' ') - (char *) pos;
00435                 if (hex_len == PASSWD_CHANGE_CHAL_LEN * 2) {
00436                         if (hexstr2bin(pos, data->passwd_change_challenge,
00437                                        PASSWD_CHANGE_CHAL_LEN)) {
00438                                 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: invalid "
00439                                            "failure challenge");
00440                         } else {
00441                                 wpa_hexdump(MSG_DEBUG, "EAP-MSCHAPV2: failure "
00442                                             "challenge",
00443                                             data->passwd_change_challenge,
00444                                             PASSWD_CHANGE_CHAL_LEN);
00445                                 data->passwd_change_challenge_valid = 1;
00446                         }
00447                 } else {
00448                         wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: invalid failure "
00449                                    "challenge len %d", hex_len);
00450                 }
00451                 pos = os_strchr(pos, ' ');
00452                 if (pos)
00453                         pos++;
00454         } else {
00455                 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: required challenge field "
00456                            "was not present in failure message");
00457         }
00458 
00459         if (pos && os_strncmp(pos, "V=", 2) == 0) {
00460                 pos += 2;
00461                 data->passwd_change_version = atoi(pos);
00462                 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: password changing "
00463                            "protocol version %d", data->passwd_change_version);
00464                 pos = os_strchr(pos, ' ');
00465                 if (pos)
00466                         pos++;
00467         }
00468 
00469         if (pos && os_strncmp(pos, "M=", 2) == 0) {
00470                 pos += 2;
00471                 msg = pos;
00472         }
00473         wpa_msg(sm->msg_ctx, MSG_WARNING,
00474                 "EAP-MSCHAPV2: failure message: '%s' (retry %sallowed, error "
00475                 "%d)",
00476                 msg, retry == 1 ? "" : "not ", data->prev_error);
00477         if (data->prev_error == ERROR_PASSWD_EXPIRED &&
00478             data->passwd_change_version == 3 && config) {
00479                 if (config->new_password == NULL) {
00480                         wpa_msg(sm->msg_ctx, MSG_INFO,
00481                                 "EAP-MSCHAPV2: Password expired - password "
00482                                 "change required");
00483                         eap_sm_request_new_password(sm);
00484                 }
00485         } else if (retry == 1 && config) {
00486                 /* TODO: could prevent the current password from being used
00487                  * again at least for some period of time */
00488                 if (!config->mschapv2_retry)
00489                         eap_sm_request_identity(sm);
00490                 eap_sm_request_password(sm);
00491                 config->mschapv2_retry = 1;
00492         } else if (config) {
00493                 /* TODO: prevent retries using same username/password */
00494                 config->mschapv2_retry = 0;
00495         }
00496 
00497         return retry == 1;
00498 }
00499 
00500 
00501 static struct wpabuf * eap_mschapv2_change_password(
00502         struct eap_sm *sm, struct eap_mschapv2_data *data,
00503         struct eap_method_ret *ret, const struct eap_mschapv2_hdr *req, u8 id)
00504 {
00505         struct wpabuf *resp;
00506         int ms_len;
00507         const u8 *username, *password, *new_password;
00508         size_t username_len, password_len, new_password_len;
00509         struct eap_mschapv2_hdr *ms;
00510         struct ms_change_password *cp;
00511         u8 password_hash[16], password_hash_hash[16];
00512         int pwhash;
00513 
00514         username = eap_get_config_identity(sm, &username_len);
00515         password = eap_get_config_password2(sm, &password_len, &pwhash);
00516         new_password = eap_get_config_new_password(sm, &new_password_len);
00517         if (username == NULL || password == NULL || new_password == NULL)
00518                 return NULL;
00519 
00520         username = mschapv2_remove_domain(username, &username_len);
00521 
00522         ret->ignore = FALSE;
00523         ret->methodState = METHOD_MAY_CONT;
00524         ret->decision = DECISION_COND_SUCC;
00525         ret->allowNotifications = TRUE;
00526 
00527         ms_len = sizeof(*ms) + sizeof(*cp);
00528         resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, ms_len,
00529                              EAP_CODE_RESPONSE, id);
00530         if (resp == NULL)
00531                 return NULL;
00532 
00533         ms = wpabuf_put(resp, sizeof(*ms));
00534         ms->op_code = MSCHAPV2_OP_CHANGE_PASSWORD;
00535         ms->mschapv2_id = req->mschapv2_id + 1;
00536         WPA_PUT_BE16(ms->ms_length, ms_len);
00537         cp = wpabuf_put(resp, sizeof(*cp));
00538 
00539         /* Encrypted-Password */
00540         if (pwhash) {
00541                 if (encrypt_pw_block_with_password_hash(
00542                             new_password, new_password_len,
00543                             password, cp->encr_password))
00544                         goto fail;
00545         } else {
00546                 if (new_password_encrypted_with_old_nt_password_hash(
00547                             new_password, new_password_len,
00548                             password, password_len, cp->encr_password))
00549                         goto fail;
00550         }
00551 
00552         /* Encrypted-Hash */
00553         if (pwhash) {
00554                 u8 new_password_hash[16];
00555                 nt_password_hash(new_password, new_password_len,
00556                                  new_password_hash);
00557                 nt_password_hash_encrypted_with_block(password,
00558                                                       new_password_hash,
00559                                                       cp->encr_hash);
00560         } else {
00561                 old_nt_password_hash_encrypted_with_new_nt_password_hash(
00562                         new_password, new_password_len,
00563                         password, password_len, cp->encr_hash);
00564         }
00565 
00566         /* Peer-Challenge */
00567         if (os_get_random(cp->peer_challenge, MSCHAPV2_CHAL_LEN))
00568                 goto fail;
00569 
00570         /* Reserved, must be zero */
00571         os_memset(cp->reserved, 0, 8);
00572 
00573         /* NT-Response */
00574         wpa_hexdump(MSG_DEBUG, "EAP-MSCHAPV2: auth_challenge",
00575                     data->passwd_change_challenge, PASSWD_CHANGE_CHAL_LEN);
00576         wpa_hexdump(MSG_DEBUG, "EAP-MSCHAPV2: peer_challenge",
00577                     cp->peer_challenge, MSCHAPV2_CHAL_LEN);
00578         wpa_hexdump_ascii(MSG_DEBUG, "EAP-MSCHAPV2: username",
00579                           username, username_len);
00580         wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-MSCHAPV2: new password",
00581                               new_password, new_password_len);
00582         generate_nt_response(data->passwd_change_challenge, cp->peer_challenge,
00583                              username, username_len,
00584                              new_password, new_password_len,
00585                              cp->nt_response);
00586         wpa_hexdump(MSG_DEBUG, "EAP-MSCHAPV2: NT-Response",
00587                     cp->nt_response, MSCHAPV2_NT_RESPONSE_LEN);
00588 
00589         /* Authenticator response is not really needed yet, but calculate it
00590          * here so that challenges need not be saved. */
00591         generate_authenticator_response(new_password, new_password_len,
00592                                         cp->peer_challenge,
00593                                         data->passwd_change_challenge,
00594                                         username, username_len,
00595                                         cp->nt_response, data->auth_response);
00596         data->auth_response_valid = 1;
00597 
00598         /* Likewise, generate master_key here since we have the needed data
00599          * available. */
00600         nt_password_hash(new_password, new_password_len, password_hash);
00601         hash_nt_password_hash(password_hash, password_hash_hash);
00602         get_master_key(password_hash_hash, cp->nt_response, data->master_key);
00603         data->master_key_valid = 1;
00604 
00605         /* Flags */
00606         os_memset(cp->flags, 0, 2);
00607 
00608         wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: TX identifier %d mschapv2_id %d "
00609                    "(change pw)", id, ms->mschapv2_id);
00610 
00611         return resp;
00612 
00613 fail:
00614         wpabuf_free(resp);
00615         return NULL;
00616 }
00617 
00618 
00630 static struct wpabuf * eap_mschapv2_failure(struct eap_sm *sm,
00631                                             struct eap_mschapv2_data *data,
00632                                             struct eap_method_ret *ret,
00633                                             const struct eap_mschapv2_hdr *req,
00634                                             size_t req_len, u8 id)
00635 {
00636         struct wpabuf *resp;
00637         const u8 *msdata = (const u8 *) (req + 1);
00638         char *buf;
00639         size_t len = req_len - sizeof(*req);
00640         int retry = 0;
00641 
00642         wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Received failure");
00643         wpa_hexdump_ascii(MSG_DEBUG, "EAP-MSCHAPV2: Failure data",
00644                           msdata, len);
00645         /*
00646          * eap_mschapv2_failure_txt() expects a nul terminated string, so we
00647          * must allocate a large enough temporary buffer to create that since
00648          * the received message does not include nul termination.
00649          */
00650         buf = os_malloc(len + 1);
00651         if (buf) {
00652                 os_memcpy(buf, msdata, len);
00653                 buf[len] = '\0';
00654                 retry = eap_mschapv2_failure_txt(sm, data, buf);
00655                 os_free(buf);
00656         }
00657 
00658         ret->ignore = FALSE;
00659         ret->methodState = METHOD_DONE;
00660         ret->decision = DECISION_FAIL;
00661         ret->allowNotifications = FALSE;
00662 
00663         if (data->prev_error == ERROR_PASSWD_EXPIRED &&
00664             data->passwd_change_version == 3) {
00665                 struct eap_peer_config *config = eap_get_config(sm);
00666                 if (config && config->new_password)
00667                         return eap_mschapv2_change_password(sm, data, ret, req,
00668                                                             id);
00669                 if (config && config->pending_req_new_password)
00670                         return NULL;
00671         } else if (retry && data->prev_error == ERROR_AUTHENTICATION_FAILURE) {
00672                 /* TODO: could try to retry authentication, e.g, after having
00673                  * changed the username/password. In this case, EAP MS-CHAP-v2
00674                  * Failure Response would not be sent here. */
00675                 return NULL;
00676         }
00677 
00678         /* Note: Only op_code of the EAP-MSCHAPV2 header is included in failure
00679          * message. */
00680         resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, 1,
00681                              EAP_CODE_RESPONSE, id);
00682         if (resp == NULL)
00683                 return NULL;
00684 
00685         wpabuf_put_u8(resp, MSCHAPV2_OP_FAILURE); /* op_code */
00686 
00687         return resp;
00688 }
00689 
00690 
00691 static int eap_mschapv2_check_config(struct eap_sm *sm)
00692 {
00693         size_t len;
00694 
00695         if (eap_get_config_identity(sm, &len) == NULL) {
00696                 wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Identity not configured");
00697                 eap_sm_request_identity(sm);
00698                 return -1;
00699         }
00700 
00701         if (eap_get_config_password(sm, &len) == NULL) {
00702                 wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Password not configured");
00703                 eap_sm_request_password(sm);
00704                 return -1;
00705         }
00706 
00707         return 0;
00708 }
00709 
00710 
00711 static int eap_mschapv2_check_mslen(struct eap_sm *sm, size_t len,
00712                                     const struct eap_mschapv2_hdr *ms)
00713 {
00714         size_t ms_len = WPA_GET_BE16(ms->ms_length);
00715 
00716         if (ms_len == len)
00717                 return 0;
00718 
00719         wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Invalid header: len=%lu "
00720                    "ms_len=%lu", (unsigned long) len, (unsigned long) ms_len);
00721         if (sm->workaround) {
00722                 /* Some authentication servers use invalid ms_len,
00723                  * ignore it for interoperability. */
00724                 wpa_printf(MSG_INFO, "EAP-MSCHAPV2: workaround, ignore"
00725                            " invalid ms_len %lu (len %lu)",
00726                            (unsigned long) ms_len,
00727                            (unsigned long) len);
00728                 return 0;
00729         }
00730 
00731         return -1;
00732 }
00733 
00734 
00735 static void eap_mschapv2_copy_challenge(struct eap_mschapv2_data *data,
00736                                         const struct wpabuf *reqData)
00737 {
00738         /*
00739          * Store a copy of the challenge message, so that it can be processed
00740          * again in case retry is allowed after a possible failure.
00741          */
00742         wpabuf_free(data->prev_challenge);
00743         data->prev_challenge = wpabuf_dup(reqData);
00744 }
00745 
00746 
00756 static struct wpabuf * eap_mschapv2_process(struct eap_sm *sm, void *priv,
00757                                             struct eap_method_ret *ret,
00758                                             const struct wpabuf *reqData)
00759 {
00760         struct eap_mschapv2_data *data = priv;
00761         struct eap_peer_config *config = eap_get_config(sm);
00762         const struct eap_mschapv2_hdr *ms;
00763         int using_prev_challenge = 0;
00764         const u8 *pos;
00765         size_t len;
00766         u8 id;
00767 
00768         if (eap_mschapv2_check_config(sm)) {
00769                 ret->ignore = TRUE;
00770                 return NULL;
00771         }
00772 
00773         if (config->mschapv2_retry && data->prev_challenge &&
00774             data->prev_error == ERROR_AUTHENTICATION_FAILURE) {
00775                 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Replacing pending packet "
00776                            "with the previous challenge");
00777 
00778                 reqData = data->prev_challenge;
00779                 using_prev_challenge = 1;
00780                 config->mschapv2_retry = 0;
00781         }
00782 
00783         pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, reqData,
00784                                &len);
00785         if (pos == NULL || len < sizeof(*ms) + 1) {
00786                 ret->ignore = TRUE;
00787                 return NULL;
00788         }
00789 
00790         ms = (const struct eap_mschapv2_hdr *) pos;
00791         if (eap_mschapv2_check_mslen(sm, len, ms)) {
00792                 ret->ignore = TRUE;
00793                 return NULL;
00794         }
00795 
00796         id = eap_get_id(reqData);
00797         wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: RX identifier %d mschapv2_id %d",
00798                    id, ms->mschapv2_id);
00799 
00800         switch (ms->op_code) {
00801         case MSCHAPV2_OP_CHALLENGE:
00802                 if (!using_prev_challenge)
00803                         eap_mschapv2_copy_challenge(data, reqData);
00804                 return eap_mschapv2_challenge(sm, data, ret, ms, len, id);
00805         case MSCHAPV2_OP_SUCCESS:
00806                 return eap_mschapv2_success(sm, data, ret, ms, len, id);
00807         case MSCHAPV2_OP_FAILURE:
00808                 return eap_mschapv2_failure(sm, data, ret, ms, len, id);
00809         default:
00810                 wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Unknown op %d - ignored",
00811                            ms->op_code);
00812                 ret->ignore = TRUE;
00813                 return NULL;
00814         }
00815 }
00816 
00817 
00818 static Boolean eap_mschapv2_isKeyAvailable(struct eap_sm *sm, void *priv)
00819 {
00820         struct eap_mschapv2_data *data = priv;
00821         return data->success && data->master_key_valid;
00822 }
00823 
00824 
00825 static u8 * eap_mschapv2_getKey(struct eap_sm *sm, void *priv, size_t *len)
00826 {
00827         struct eap_mschapv2_data *data = priv;
00828         u8 *key;
00829         int key_len;
00830 
00831         if (!data->master_key_valid || !data->success)
00832                 return NULL;
00833 
00834         key_len = 2 * MSCHAPV2_KEY_LEN;
00835 
00836         key = os_malloc(key_len);
00837         if (key == NULL)
00838                 return NULL;
00839 
00840         /* MSK = server MS-MPPE-Recv-Key | MS-MPPE-Send-Key, i.e.,
00841          *      peer MS-MPPE-Send-Key | MS-MPPE-Recv-Key */
00842         get_asymetric_start_key(data->master_key, key, MSCHAPV2_KEY_LEN, 1, 0);
00843         get_asymetric_start_key(data->master_key, key + MSCHAPV2_KEY_LEN,
00844                                 MSCHAPV2_KEY_LEN, 0, 0);
00845 
00846         wpa_hexdump_key(MSG_DEBUG, "EAP-MSCHAPV2: Derived key",
00847                         key, key_len);
00848 
00849         *len = key_len;
00850         return key;
00851 }
00852 
00853 
00861 int eap_peer_mschapv2_register(void)
00862 {
00863         struct eap_method *eap;
00864         int ret;
00865 
00866         eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
00867                                     EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2,
00868                                     "MSCHAPV2");
00869         if (eap == NULL)
00870                 return -1;
00871 
00872         eap->init = eap_mschapv2_init;
00873         eap->deinit = eap_mschapv2_deinit;
00874         eap->process = eap_mschapv2_process;
00875         eap->isKeyAvailable = eap_mschapv2_isKeyAvailable;
00876         eap->getKey = eap_mschapv2_getKey;
00877 
00878         ret = eap_peer_method_register(eap);
00879         if (ret)
00880                 eap_peer_method_free(eap);
00881         return ret;
00882 }


wpa_supplicant
Author(s): Package maintained by Blaise Gassend
autogenerated on Thu Apr 24 2014 15:34:34