1 /* 2 * hostapd / EAP-MSCHAPv2 (draft-kamath-pppext-eap-mschapv2-00.txt) server 3 * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi> 4 * 5 * This software may be distributed under the terms of the BSD license. 6 * See README for more details. 7 */ 8 9 #include "includes.h" 10 11 #include "common.h" 12 #include "crypto/ms_funcs.h" 13 #include "crypto/random.h" 14 #include "eap_i.h" 15 16 17 struct eap_mschapv2_hdr { 18 u8 op_code; /* MSCHAPV2_OP_* */ 19 u8 mschapv2_id; /* must be changed for challenges, but not for 20 * success/failure */ 21 u8 ms_length[2]; /* Note: misaligned; length - 5 */ 22 /* followed by data */ 23 } STRUCT_PACKED; 24 25 #define MSCHAPV2_OP_CHALLENGE 1 26 #define MSCHAPV2_OP_RESPONSE 2 27 #define MSCHAPV2_OP_SUCCESS 3 28 #define MSCHAPV2_OP_FAILURE 4 29 #define MSCHAPV2_OP_CHANGE_PASSWORD 7 30 31 #define MSCHAPV2_RESP_LEN 49 32 33 #define ERROR_RESTRICTED_LOGON_HOURS 646 34 #define ERROR_ACCT_DISABLED 647 35 #define ERROR_PASSWD_EXPIRED 648 36 #define ERROR_NO_DIALIN_PERMISSION 649 37 #define ERROR_AUTHENTICATION_FAILURE 691 38 #define ERROR_CHANGING_PASSWORD 709 39 40 #define PASSWD_CHANGE_CHAL_LEN 16 41 #define MSCHAPV2_KEY_LEN 16 42 43 44 #define CHALLENGE_LEN 16 45 46 struct eap_mschapv2_data { 47 u8 auth_challenge[CHALLENGE_LEN]; 48 int auth_challenge_from_tls; 49 u8 *peer_challenge; 50 u8 auth_response[20]; 51 enum { CHALLENGE, SUCCESS_REQ, FAILURE_REQ, SUCCESS, FAILURE } state; 52 u8 resp_mschapv2_id; 53 u8 master_key[16]; 54 int master_key_valid; 55 }; 56 57 58 static void * eap_mschapv2_init(struct eap_sm *sm) 59 { 60 struct eap_mschapv2_data *data; 61 62 data = os_zalloc(sizeof(*data)); 63 if (data == NULL) 64 return NULL; 65 data->state = CHALLENGE; 66 67 wpa_printf(MSG_DEBUG, "EAP-%sMSCHAPv2 init%s%s", 68 sm->eap_fast_mschapv2 ? "FAST-" : "", 69 sm->peer_challenge && sm->auth_challenge ? 70 " with preset challenges" : "", 71 sm->init_phase2 ? " for Phase 2" : ""); 72 73 if (sm->auth_challenge) { 74 os_memcpy(data->auth_challenge, sm->auth_challenge, 75 CHALLENGE_LEN); 76 data->auth_challenge_from_tls = 1; 77 } 78 79 if (sm->peer_challenge) { 80 data->peer_challenge = os_memdup(sm->peer_challenge, 81 CHALLENGE_LEN); 82 if (data->peer_challenge == NULL) { 83 os_free(data); 84 return NULL; 85 } 86 } 87 88 return data; 89 } 90 91 92 static void eap_mschapv2_reset(struct eap_sm *sm, void *priv) 93 { 94 struct eap_mschapv2_data *data = priv; 95 if (data == NULL) 96 return; 97 98 os_free(data->peer_challenge); 99 bin_clear_free(data, sizeof(*data)); 100 } 101 102 103 static struct wpabuf * eap_mschapv2_build_challenge( 104 struct eap_sm *sm, struct eap_mschapv2_data *data, u8 id) 105 { 106 struct wpabuf *req; 107 struct eap_mschapv2_hdr *ms; 108 size_t ms_len; 109 110 if (!data->auth_challenge_from_tls && 111 random_get_bytes(data->auth_challenge, CHALLENGE_LEN)) { 112 wpa_printf(MSG_ERROR, "EAP-MSCHAPV2: Failed to get random " 113 "data"); 114 data->state = FAILURE; 115 return NULL; 116 } 117 118 ms_len = sizeof(*ms) + 1 + CHALLENGE_LEN + sm->cfg->server_id_len; 119 req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, ms_len, 120 EAP_CODE_REQUEST, id); 121 if (req == NULL) { 122 wpa_printf(MSG_ERROR, "EAP-MSCHAPV2: Failed to allocate memory" 123 " for request"); 124 data->state = FAILURE; 125 return NULL; 126 } 127 128 ms = wpabuf_put(req, sizeof(*ms)); 129 ms->op_code = MSCHAPV2_OP_CHALLENGE; 130 ms->mschapv2_id = id; 131 WPA_PUT_BE16(ms->ms_length, ms_len); 132 133 wpabuf_put_u8(req, CHALLENGE_LEN); 134 if (!data->auth_challenge_from_tls) 135 wpabuf_put_data(req, data->auth_challenge, CHALLENGE_LEN); 136 else 137 wpabuf_put(req, CHALLENGE_LEN); 138 wpa_hexdump(MSG_MSGDUMP, "EAP-MSCHAPV2: Challenge", 139 data->auth_challenge, CHALLENGE_LEN); 140 wpabuf_put_data(req, sm->cfg->server_id, sm->cfg->server_id_len); 141 142 return req; 143 } 144 145 146 static struct wpabuf * eap_mschapv2_build_success_req( 147 struct eap_sm *sm, struct eap_mschapv2_data *data, u8 id) 148 { 149 struct wpabuf *req; 150 struct eap_mschapv2_hdr *ms; 151 u8 *msg; 152 char *message = "OK"; 153 size_t ms_len; 154 155 ms_len = sizeof(*ms) + 2 + 2 * sizeof(data->auth_response) + 1 + 2 + 156 os_strlen(message); 157 req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, ms_len, 158 EAP_CODE_REQUEST, id); 159 if (req == NULL) { 160 wpa_printf(MSG_ERROR, "EAP-MSCHAPV2: Failed to allocate memory" 161 " for request"); 162 data->state = FAILURE; 163 return NULL; 164 } 165 166 ms = wpabuf_put(req, sizeof(*ms)); 167 ms->op_code = MSCHAPV2_OP_SUCCESS; 168 ms->mschapv2_id = data->resp_mschapv2_id; 169 WPA_PUT_BE16(ms->ms_length, ms_len); 170 msg = (u8 *) (ms + 1); 171 172 wpabuf_put_u8(req, 'S'); 173 wpabuf_put_u8(req, '='); 174 wpa_snprintf_hex_uppercase( 175 wpabuf_put(req, sizeof(data->auth_response) * 2), 176 sizeof(data->auth_response) * 2 + 1, 177 data->auth_response, sizeof(data->auth_response)); 178 wpabuf_put_u8(req, ' '); 179 wpabuf_put_u8(req, 'M'); 180 wpabuf_put_u8(req, '='); 181 wpabuf_put_data(req, message, os_strlen(message)); 182 183 wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-MSCHAPV2: Success Request Message", 184 msg, ms_len - sizeof(*ms)); 185 186 return req; 187 } 188 189 190 static struct wpabuf * eap_mschapv2_build_failure_req( 191 struct eap_sm *sm, struct eap_mschapv2_data *data, u8 id) 192 { 193 struct wpabuf *req; 194 struct eap_mschapv2_hdr *ms; 195 char *message = "E=691 R=0 C=00000000000000000000000000000000 V=3 " 196 "M=FAILED"; 197 size_t ms_len; 198 199 ms_len = sizeof(*ms) + os_strlen(message); 200 req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, ms_len, 201 EAP_CODE_REQUEST, id); 202 if (req == NULL) { 203 wpa_printf(MSG_ERROR, "EAP-MSCHAPV2: Failed to allocate memory" 204 " for request"); 205 data->state = FAILURE; 206 return NULL; 207 } 208 209 ms = wpabuf_put(req, sizeof(*ms)); 210 ms->op_code = MSCHAPV2_OP_FAILURE; 211 ms->mschapv2_id = data->resp_mschapv2_id; 212 WPA_PUT_BE16(ms->ms_length, ms_len); 213 214 wpabuf_put_data(req, message, os_strlen(message)); 215 216 wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-MSCHAPV2: Failure Request Message", 217 (u8 *) message, os_strlen(message)); 218 219 return req; 220 } 221 222 223 static struct wpabuf * eap_mschapv2_buildReq(struct eap_sm *sm, void *priv, 224 u8 id) 225 { 226 struct eap_mschapv2_data *data = priv; 227 228 switch (data->state) { 229 case CHALLENGE: 230 return eap_mschapv2_build_challenge(sm, data, id); 231 case SUCCESS_REQ: 232 return eap_mschapv2_build_success_req(sm, data, id); 233 case FAILURE_REQ: 234 return eap_mschapv2_build_failure_req(sm, data, id); 235 default: 236 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Unknown state %d in " 237 "buildReq", data->state); 238 break; 239 } 240 return NULL; 241 } 242 243 244 static bool eap_mschapv2_check(struct eap_sm *sm, void *priv, 245 struct wpabuf *respData) 246 { 247 struct eap_mschapv2_data *data = priv; 248 struct eap_mschapv2_hdr *resp; 249 const u8 *pos; 250 size_t len; 251 252 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, respData, 253 &len); 254 if (pos == NULL || len < 1) { 255 wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Invalid frame"); 256 return true; 257 } 258 259 resp = (struct eap_mschapv2_hdr *) pos; 260 if (data->state == CHALLENGE && 261 resp->op_code != MSCHAPV2_OP_RESPONSE) { 262 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Expected Response - " 263 "ignore op %d", resp->op_code); 264 return true; 265 } 266 267 if (data->state == SUCCESS_REQ && 268 resp->op_code != MSCHAPV2_OP_SUCCESS && 269 resp->op_code != MSCHAPV2_OP_FAILURE) { 270 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Expected Success or " 271 "Failure - ignore op %d", resp->op_code); 272 return true; 273 } 274 275 if (data->state == FAILURE_REQ && 276 resp->op_code != MSCHAPV2_OP_FAILURE) { 277 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Expected Failure " 278 "- ignore op %d", resp->op_code); 279 return true; 280 } 281 282 return false; 283 } 284 285 286 static void eap_mschapv2_process_response(struct eap_sm *sm, 287 struct eap_mschapv2_data *data, 288 struct wpabuf *respData) 289 { 290 struct eap_mschapv2_hdr *resp; 291 const u8 *pos, *end, *peer_challenge, *nt_response, *name; 292 u8 flags; 293 size_t len, name_len, i; 294 u8 expected[24]; 295 const u8 *username, *user; 296 size_t username_len, user_len; 297 int res; 298 char *buf; 299 300 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, respData, 301 &len); 302 if (pos == NULL || len < 1) 303 return; /* Should not happen - frame already validated */ 304 305 end = pos + len; 306 resp = (struct eap_mschapv2_hdr *) pos; 307 pos = (u8 *) (resp + 1); 308 309 if (len < sizeof(*resp) + 1 + 49 || 310 resp->op_code != MSCHAPV2_OP_RESPONSE || 311 pos[0] != 49) { 312 wpa_hexdump_buf(MSG_DEBUG, "EAP-MSCHAPV2: Invalid response", 313 respData); 314 data->state = FAILURE; 315 return; 316 } 317 data->resp_mschapv2_id = resp->mschapv2_id; 318 pos++; 319 peer_challenge = pos; 320 pos += 16 + 8; 321 nt_response = pos; 322 pos += 24; 323 flags = *pos++; 324 name = pos; 325 name_len = end - name; 326 327 if (data->peer_challenge) { 328 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Using pre-configured " 329 "Peer-Challenge"); 330 peer_challenge = data->peer_challenge; 331 } 332 wpa_hexdump(MSG_MSGDUMP, "EAP-MSCHAPV2: Peer-Challenge", 333 peer_challenge, 16); 334 wpa_hexdump(MSG_MSGDUMP, "EAP-MSCHAPV2: NT-Response", nt_response, 24); 335 wpa_printf(MSG_MSGDUMP, "EAP-MSCHAPV2: Flags 0x%x", flags); 336 wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-MSCHAPV2: Name", name, name_len); 337 338 buf = os_malloc(name_len * 4 + 1); 339 if (buf) { 340 printf_encode(buf, name_len * 4 + 1, name, name_len); 341 eap_log_msg(sm, "EAP-MSCHAPV2 Name '%s'", buf); 342 os_free(buf); 343 } 344 345 /* MSCHAPv2 does not include optional domain name in the 346 * challenge-response calculation, so remove domain prefix 347 * (if present). */ 348 username = sm->identity; 349 username_len = sm->identity_len; 350 for (i = 0; i < username_len; i++) { 351 if (username[i] == '\\') { 352 username_len -= i + 1; 353 username += i + 1; 354 break; 355 } 356 } 357 358 user = name; 359 user_len = name_len; 360 for (i = 0; i < user_len; i++) { 361 if (user[i] == '\\') { 362 user_len -= i + 1; 363 user += i + 1; 364 break; 365 } 366 } 367 368 #ifdef CONFIG_TESTING_OPTIONS 369 { 370 u8 challenge[8]; 371 372 if (challenge_hash(peer_challenge, data->auth_challenge, 373 username, username_len, challenge) == 0) { 374 eap_server_mschap_rx_callback(sm, "EAP-MSCHAPV2", 375 username, username_len, 376 challenge, nt_response); 377 } 378 } 379 #endif /* CONFIG_TESTING_OPTIONS */ 380 381 if (username_len != user_len || 382 os_memcmp(username, user, username_len) != 0) { 383 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Mismatch in user names"); 384 wpa_hexdump_ascii(MSG_DEBUG, "EAP-MSCHAPV2: Expected user " 385 "name", username, username_len); 386 wpa_hexdump_ascii(MSG_DEBUG, "EAP-MSCHAPV2: Received user " 387 "name", user, user_len); 388 data->state = FAILURE; 389 return; 390 } 391 392 wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-MSCHAPV2: User name", 393 username, username_len); 394 395 if (sm->user->password_hash) { 396 res = generate_nt_response_pwhash(data->auth_challenge, 397 peer_challenge, 398 username, username_len, 399 sm->user->password, 400 expected); 401 } else { 402 res = generate_nt_response(data->auth_challenge, 403 peer_challenge, 404 username, username_len, 405 sm->user->password, 406 sm->user->password_len, 407 expected); 408 } 409 if (res) { 410 data->state = FAILURE; 411 return; 412 } 413 414 if (os_memcmp_const(nt_response, expected, 24) == 0) { 415 const u8 *pw_hash; 416 u8 pw_hash_buf[16], pw_hash_hash[16]; 417 418 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Correct NT-Response"); 419 data->state = SUCCESS_REQ; 420 421 /* Authenticator response is not really needed yet, but 422 * calculate it here so that peer_challenge and username need 423 * not be saved. */ 424 if (sm->user->password_hash) { 425 pw_hash = sm->user->password; 426 } else { 427 if (nt_password_hash(sm->user->password, 428 sm->user->password_len, 429 pw_hash_buf) < 0) { 430 data->state = FAILURE; 431 return; 432 } 433 pw_hash = pw_hash_buf; 434 } 435 if (generate_authenticator_response_pwhash( 436 pw_hash, peer_challenge, data->auth_challenge, 437 username, username_len, nt_response, 438 data->auth_response) < 0 || 439 hash_nt_password_hash(pw_hash, pw_hash_hash) < 0 || 440 get_master_key(pw_hash_hash, nt_response, 441 data->master_key)) { 442 data->state = FAILURE; 443 return; 444 } 445 data->master_key_valid = 1; 446 wpa_hexdump_key(MSG_DEBUG, "EAP-MSCHAPV2: Derived Master Key", 447 data->master_key, MSCHAPV2_KEY_LEN); 448 } else { 449 wpa_hexdump(MSG_MSGDUMP, "EAP-MSCHAPV2: Expected NT-Response", 450 expected, 24); 451 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Invalid NT-Response"); 452 data->state = FAILURE_REQ; 453 } 454 } 455 456 457 static void eap_mschapv2_process_success_resp(struct eap_sm *sm, 458 struct eap_mschapv2_data *data, 459 struct wpabuf *respData) 460 { 461 struct eap_mschapv2_hdr *resp; 462 const u8 *pos; 463 size_t len; 464 465 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, respData, 466 &len); 467 if (pos == NULL || len < 1) 468 return; /* Should not happen - frame already validated */ 469 470 resp = (struct eap_mschapv2_hdr *) pos; 471 472 if (resp->op_code == MSCHAPV2_OP_SUCCESS) { 473 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Received Success Response" 474 " - authentication completed successfully"); 475 data->state = SUCCESS; 476 } else { 477 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Did not receive Success " 478 "Response - peer rejected authentication"); 479 data->state = FAILURE; 480 } 481 } 482 483 484 static void eap_mschapv2_process_failure_resp(struct eap_sm *sm, 485 struct eap_mschapv2_data *data, 486 struct wpabuf *respData) 487 { 488 struct eap_mschapv2_hdr *resp; 489 const u8 *pos; 490 size_t len; 491 492 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, respData, 493 &len); 494 if (pos == NULL || len < 1) 495 return; /* Should not happen - frame already validated */ 496 497 resp = (struct eap_mschapv2_hdr *) pos; 498 499 if (resp->op_code == MSCHAPV2_OP_FAILURE) { 500 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Received Failure Response" 501 " - authentication failed"); 502 } else { 503 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Did not receive Failure " 504 "Response - authentication failed"); 505 } 506 507 data->state = FAILURE; 508 } 509 510 511 static void eap_mschapv2_process(struct eap_sm *sm, void *priv, 512 struct wpabuf *respData) 513 { 514 struct eap_mschapv2_data *data = priv; 515 516 if (sm->user == NULL || sm->user->password == NULL) { 517 wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Password not configured"); 518 data->state = FAILURE; 519 return; 520 } 521 522 switch (data->state) { 523 case CHALLENGE: 524 eap_mschapv2_process_response(sm, data, respData); 525 break; 526 case SUCCESS_REQ: 527 eap_mschapv2_process_success_resp(sm, data, respData); 528 break; 529 case FAILURE_REQ: 530 eap_mschapv2_process_failure_resp(sm, data, respData); 531 break; 532 default: 533 wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Unknown state %d in " 534 "process", data->state); 535 break; 536 } 537 } 538 539 540 static bool eap_mschapv2_isDone(struct eap_sm *sm, void *priv) 541 { 542 struct eap_mschapv2_data *data = priv; 543 return data->state == SUCCESS || data->state == FAILURE; 544 } 545 546 547 static u8 * eap_mschapv2_getKey(struct eap_sm *sm, void *priv, size_t *len) 548 { 549 struct eap_mschapv2_data *data = priv; 550 u8 *key; 551 bool first_is_send; 552 553 if (data->state != SUCCESS || !data->master_key_valid) 554 return NULL; 555 556 *len = 2 * MSCHAPV2_KEY_LEN; 557 key = os_malloc(*len); 558 if (key == NULL) 559 return NULL; 560 /* 561 * [MS-CHAP], 3.1.5.1 (Master Session Key (MSK) Derivation 562 * MSK = MasterReceiveKey + MasterSendKey + 32 bytes zeros (padding) 563 * On an Authenticator: 564 * MS-MPPE-Recv-Key = MasterReceiveKey 565 * MS-MPPE-Send-Key = MasterSendKey 566 * 567 * RFC 5422, 3.2.3 (Authenticating Using EAP-FAST-MSCHAPv2) 568 * MSK = MasterSendKey + MasterReceiveKey 569 * (i.e., reverse order and no padding) 570 * 571 * On Peer, EAP-MSCHAPv2 starts with Send key and EAP-FAST-MSCHAPv2 572 * starts with Receive key. 573 */ 574 first_is_send = sm->eap_fast_mschapv2; 575 /* MSK = server MS-MPPE-Recv-Key | MS-MPPE-Send-Key */ 576 if (get_asymetric_start_key(data->master_key, key, MSCHAPV2_KEY_LEN, 577 first_is_send, 1) < 0 || 578 get_asymetric_start_key(data->master_key, key + MSCHAPV2_KEY_LEN, 579 MSCHAPV2_KEY_LEN, !first_is_send, 1) < 0) { 580 os_free(key); 581 return NULL; 582 } 583 wpa_hexdump_key(MSG_DEBUG, "EAP-MSCHAPV2: Derived key", key, *len); 584 585 return key; 586 } 587 588 589 static bool eap_mschapv2_isSuccess(struct eap_sm *sm, void *priv) 590 { 591 struct eap_mschapv2_data *data = priv; 592 return data->state == SUCCESS; 593 } 594 595 596 int eap_server_mschapv2_register(void) 597 { 598 struct eap_method *eap; 599 600 eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION, 601 EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, 602 "MSCHAPV2"); 603 if (eap == NULL) 604 return -1; 605 606 eap->init = eap_mschapv2_init; 607 eap->reset = eap_mschapv2_reset; 608 eap->buildReq = eap_mschapv2_buildReq; 609 eap->check = eap_mschapv2_check; 610 eap->process = eap_mschapv2_process; 611 eap->isDone = eap_mschapv2_isDone; 612 eap->getKey = eap_mschapv2_getKey; 613 eap->isSuccess = eap_mschapv2_isSuccess; 614 615 return eap_server_method_register(eap); 616 } 617