eap.c revision 4b06059785b935dd1f4f09314e4e12c417d2c6a4
1/* 2 * EAP peer state machines (RFC 4137) 3 * Copyright (c) 2004-2012, 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 * This file implements the Peer State Machine as defined in RFC 4137. The used 9 * states and state transitions match mostly with the RFC. However, there are 10 * couple of additional transitions for working around small issues noticed 11 * during testing. These exceptions are explained in comments within the 12 * functions in this file. The method functions, m.func(), are similar to the 13 * ones used in RFC 4137, but some small changes have used here to optimize 14 * operations and to add functionality needed for fast re-authentication 15 * (session resumption). 16 */ 17 18#include "includes.h" 19 20#include "common.h" 21#include "pcsc_funcs.h" 22#include "state_machine.h" 23#include "ext_password.h" 24#include "crypto/crypto.h" 25#include "crypto/tls.h" 26#include "common/wpa_ctrl.h" 27#include "eap_common/eap_wsc_common.h" 28#include "eap_i.h" 29#include "eap_config.h" 30 31#define STATE_MACHINE_DATA struct eap_sm 32#define STATE_MACHINE_DEBUG_PREFIX "EAP" 33 34#define EAP_MAX_AUTH_ROUNDS 50 35#define EAP_CLIENT_TIMEOUT_DEFAULT 60 36 37 38static Boolean eap_sm_allowMethod(struct eap_sm *sm, int vendor, 39 EapType method); 40static struct wpabuf * eap_sm_buildNak(struct eap_sm *sm, int id); 41static void eap_sm_processIdentity(struct eap_sm *sm, 42 const struct wpabuf *req); 43static void eap_sm_processNotify(struct eap_sm *sm, const struct wpabuf *req); 44static struct wpabuf * eap_sm_buildNotify(int id); 45static void eap_sm_parseEapReq(struct eap_sm *sm, const struct wpabuf *req); 46#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG) 47static const char * eap_sm_method_state_txt(EapMethodState state); 48static const char * eap_sm_decision_txt(EapDecision decision); 49#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */ 50 51 52 53static Boolean eapol_get_bool(struct eap_sm *sm, enum eapol_bool_var var) 54{ 55 return sm->eapol_cb->get_bool(sm->eapol_ctx, var); 56} 57 58 59static void eapol_set_bool(struct eap_sm *sm, enum eapol_bool_var var, 60 Boolean value) 61{ 62 sm->eapol_cb->set_bool(sm->eapol_ctx, var, value); 63} 64 65 66static unsigned int eapol_get_int(struct eap_sm *sm, enum eapol_int_var var) 67{ 68 return sm->eapol_cb->get_int(sm->eapol_ctx, var); 69} 70 71 72static void eapol_set_int(struct eap_sm *sm, enum eapol_int_var var, 73 unsigned int value) 74{ 75 sm->eapol_cb->set_int(sm->eapol_ctx, var, value); 76} 77 78 79static struct wpabuf * eapol_get_eapReqData(struct eap_sm *sm) 80{ 81 return sm->eapol_cb->get_eapReqData(sm->eapol_ctx); 82} 83 84 85static void eap_notify_status(struct eap_sm *sm, const char *status, 86 const char *parameter) 87{ 88 wpa_printf(MSG_DEBUG, "EAP: Status notification: %s (param=%s)", 89 status, parameter); 90 if (sm->eapol_cb->notify_status) 91 sm->eapol_cb->notify_status(sm->eapol_ctx, status, parameter); 92} 93 94 95static void eap_deinit_prev_method(struct eap_sm *sm, const char *txt) 96{ 97 ext_password_free(sm->ext_pw_buf); 98 sm->ext_pw_buf = NULL; 99 100 if (sm->m == NULL || sm->eap_method_priv == NULL) 101 return; 102 103 wpa_printf(MSG_DEBUG, "EAP: deinitialize previously used EAP method " 104 "(%d, %s) at %s", sm->selectedMethod, sm->m->name, txt); 105 sm->m->deinit(sm, sm->eap_method_priv); 106 sm->eap_method_priv = NULL; 107 sm->m = NULL; 108} 109 110 111/** 112 * eap_allowed_method - Check whether EAP method is allowed 113 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 114 * @vendor: Vendor-Id for expanded types or 0 = IETF for legacy types 115 * @method: EAP type 116 * Returns: 1 = allowed EAP method, 0 = not allowed 117 */ 118int eap_allowed_method(struct eap_sm *sm, int vendor, u32 method) 119{ 120 struct eap_peer_config *config = eap_get_config(sm); 121 int i; 122 struct eap_method_type *m; 123 124 if (config == NULL || config->eap_methods == NULL) 125 return 1; 126 127 m = config->eap_methods; 128 for (i = 0; m[i].vendor != EAP_VENDOR_IETF || 129 m[i].method != EAP_TYPE_NONE; i++) { 130 if (m[i].vendor == vendor && m[i].method == method) 131 return 1; 132 } 133 return 0; 134} 135 136 137/* 138 * This state initializes state machine variables when the machine is 139 * activated (portEnabled = TRUE). This is also used when re-starting 140 * authentication (eapRestart == TRUE). 141 */ 142SM_STATE(EAP, INITIALIZE) 143{ 144 SM_ENTRY(EAP, INITIALIZE); 145 if (sm->fast_reauth && sm->m && sm->m->has_reauth_data && 146 sm->m->has_reauth_data(sm, sm->eap_method_priv) && 147 !sm->prev_failure) { 148 wpa_printf(MSG_DEBUG, "EAP: maintaining EAP method data for " 149 "fast reauthentication"); 150 sm->m->deinit_for_reauth(sm, sm->eap_method_priv); 151 } else { 152 eap_deinit_prev_method(sm, "INITIALIZE"); 153 } 154 sm->selectedMethod = EAP_TYPE_NONE; 155 sm->methodState = METHOD_NONE; 156 sm->allowNotifications = TRUE; 157 sm->decision = DECISION_FAIL; 158 sm->ClientTimeout = EAP_CLIENT_TIMEOUT_DEFAULT; 159 eapol_set_int(sm, EAPOL_idleWhile, sm->ClientTimeout); 160 eapol_set_bool(sm, EAPOL_eapSuccess, FALSE); 161 eapol_set_bool(sm, EAPOL_eapFail, FALSE); 162 os_free(sm->eapKeyData); 163 sm->eapKeyData = NULL; 164 os_free(sm->eapSessionId); 165 sm->eapSessionId = NULL; 166 sm->eapKeyAvailable = FALSE; 167 eapol_set_bool(sm, EAPOL_eapRestart, FALSE); 168 sm->lastId = -1; /* new session - make sure this does not match with 169 * the first EAP-Packet */ 170 /* 171 * RFC 4137 does not reset eapResp and eapNoResp here. However, this 172 * seemed to be able to trigger cases where both were set and if EAPOL 173 * state machine uses eapNoResp first, it may end up not sending a real 174 * reply correctly. This occurred when the workaround in FAIL state set 175 * eapNoResp = TRUE.. Maybe that workaround needs to be fixed to do 176 * something else(?) 177 */ 178 eapol_set_bool(sm, EAPOL_eapResp, FALSE); 179 eapol_set_bool(sm, EAPOL_eapNoResp, FALSE); 180 sm->num_rounds = 0; 181 sm->prev_failure = 0; 182} 183 184 185/* 186 * This state is reached whenever service from the lower layer is interrupted 187 * or unavailable (portEnabled == FALSE). Immediate transition to INITIALIZE 188 * occurs when the port becomes enabled. 189 */ 190SM_STATE(EAP, DISABLED) 191{ 192 SM_ENTRY(EAP, DISABLED); 193 sm->num_rounds = 0; 194 /* 195 * RFC 4137 does not describe clearing of idleWhile here, but doing so 196 * allows the timer tick to be stopped more quickly when EAP is not in 197 * use. 198 */ 199 eapol_set_int(sm, EAPOL_idleWhile, 0); 200} 201 202 203/* 204 * The state machine spends most of its time here, waiting for something to 205 * happen. This state is entered unconditionally from INITIALIZE, DISCARD, and 206 * SEND_RESPONSE states. 207 */ 208SM_STATE(EAP, IDLE) 209{ 210 SM_ENTRY(EAP, IDLE); 211} 212 213 214/* 215 * This state is entered when an EAP packet is received (eapReq == TRUE) to 216 * parse the packet header. 217 */ 218SM_STATE(EAP, RECEIVED) 219{ 220 const struct wpabuf *eapReqData; 221 222 SM_ENTRY(EAP, RECEIVED); 223 eapReqData = eapol_get_eapReqData(sm); 224 /* parse rxReq, rxSuccess, rxFailure, reqId, reqMethod */ 225 eap_sm_parseEapReq(sm, eapReqData); 226 sm->num_rounds++; 227} 228 229 230/* 231 * This state is entered when a request for a new type comes in. Either the 232 * correct method is started, or a Nak response is built. 233 */ 234SM_STATE(EAP, GET_METHOD) 235{ 236 int reinit; 237 EapType method; 238 const struct eap_method *eap_method; 239 240 SM_ENTRY(EAP, GET_METHOD); 241 242 if (sm->reqMethod == EAP_TYPE_EXPANDED) 243 method = sm->reqVendorMethod; 244 else 245 method = sm->reqMethod; 246 247 eap_method = eap_peer_get_eap_method(sm->reqVendor, method); 248 249 if (!eap_sm_allowMethod(sm, sm->reqVendor, method)) { 250 wpa_printf(MSG_DEBUG, "EAP: vendor %u method %u not allowed", 251 sm->reqVendor, method); 252 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_PROPOSED_METHOD 253 "vendor=%u method=%u -> NAK", 254 sm->reqVendor, method); 255 eap_notify_status(sm, "refuse proposed method", 256 eap_method ? eap_method->name : "unknown"); 257 goto nak; 258 } 259 260 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_PROPOSED_METHOD 261 "vendor=%u method=%u", sm->reqVendor, method); 262 263 eap_notify_status(sm, "accept proposed method", 264 eap_method ? eap_method->name : "unknown"); 265 /* 266 * RFC 4137 does not define specific operation for fast 267 * re-authentication (session resumption). The design here is to allow 268 * the previously used method data to be maintained for 269 * re-authentication if the method support session resumption. 270 * Otherwise, the previously used method data is freed and a new method 271 * is allocated here. 272 */ 273 if (sm->fast_reauth && 274 sm->m && sm->m->vendor == sm->reqVendor && 275 sm->m->method == method && 276 sm->m->has_reauth_data && 277 sm->m->has_reauth_data(sm, sm->eap_method_priv)) { 278 wpa_printf(MSG_DEBUG, "EAP: Using previous method data" 279 " for fast re-authentication"); 280 reinit = 1; 281 } else { 282 eap_deinit_prev_method(sm, "GET_METHOD"); 283 reinit = 0; 284 } 285 286 sm->selectedMethod = sm->reqMethod; 287 if (sm->m == NULL) 288 sm->m = eap_method; 289 if (!sm->m) { 290 wpa_printf(MSG_DEBUG, "EAP: Could not find selected method: " 291 "vendor %d method %d", 292 sm->reqVendor, method); 293 goto nak; 294 } 295 296 sm->ClientTimeout = EAP_CLIENT_TIMEOUT_DEFAULT; 297 298 wpa_printf(MSG_DEBUG, "EAP: Initialize selected EAP method: " 299 "vendor %u method %u (%s)", 300 sm->reqVendor, method, sm->m->name); 301 if (reinit) 302 sm->eap_method_priv = sm->m->init_for_reauth( 303 sm, sm->eap_method_priv); 304 else 305 sm->eap_method_priv = sm->m->init(sm); 306 307 if (sm->eap_method_priv == NULL) { 308 struct eap_peer_config *config = eap_get_config(sm); 309 wpa_msg(sm->msg_ctx, MSG_INFO, 310 "EAP: Failed to initialize EAP method: vendor %u " 311 "method %u (%s)", 312 sm->reqVendor, method, sm->m->name); 313 sm->m = NULL; 314 sm->methodState = METHOD_NONE; 315 sm->selectedMethod = EAP_TYPE_NONE; 316 if (sm->reqMethod == EAP_TYPE_TLS && config && 317 (config->pending_req_pin || 318 config->pending_req_passphrase)) { 319 /* 320 * Return without generating Nak in order to allow 321 * entering of PIN code or passphrase to retry the 322 * current EAP packet. 323 */ 324 wpa_printf(MSG_DEBUG, "EAP: Pending PIN/passphrase " 325 "request - skip Nak"); 326 return; 327 } 328 329 goto nak; 330 } 331 332 sm->methodState = METHOD_INIT; 333 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_METHOD 334 "EAP vendor %u method %u (%s) selected", 335 sm->reqVendor, method, sm->m->name); 336 return; 337 338nak: 339 wpabuf_free(sm->eapRespData); 340 sm->eapRespData = NULL; 341 sm->eapRespData = eap_sm_buildNak(sm, sm->reqId); 342} 343 344 345/* 346 * The method processing happens here. The request from the authenticator is 347 * processed, and an appropriate response packet is built. 348 */ 349SM_STATE(EAP, METHOD) 350{ 351 struct wpabuf *eapReqData; 352 struct eap_method_ret ret; 353 int min_len = 1; 354 355 SM_ENTRY(EAP, METHOD); 356 if (sm->m == NULL) { 357 wpa_printf(MSG_WARNING, "EAP::METHOD - method not selected"); 358 return; 359 } 360 361 eapReqData = eapol_get_eapReqData(sm); 362 if (sm->m->vendor == EAP_VENDOR_IETF && sm->m->method == EAP_TYPE_LEAP) 363 min_len = 0; /* LEAP uses EAP-Success without payload */ 364 if (!eap_hdr_len_valid(eapReqData, min_len)) 365 return; 366 367 /* 368 * Get ignore, methodState, decision, allowNotifications, and 369 * eapRespData. RFC 4137 uses three separate method procedure (check, 370 * process, and buildResp) in this state. These have been combined into 371 * a single function call to m->process() in order to optimize EAP 372 * method implementation interface a bit. These procedures are only 373 * used from within this METHOD state, so there is no need to keep 374 * these as separate C functions. 375 * 376 * The RFC 4137 procedures return values as follows: 377 * ignore = m.check(eapReqData) 378 * (methodState, decision, allowNotifications) = m.process(eapReqData) 379 * eapRespData = m.buildResp(reqId) 380 */ 381 os_memset(&ret, 0, sizeof(ret)); 382 ret.ignore = sm->ignore; 383 ret.methodState = sm->methodState; 384 ret.decision = sm->decision; 385 ret.allowNotifications = sm->allowNotifications; 386 wpabuf_free(sm->eapRespData); 387 sm->eapRespData = NULL; 388 sm->eapRespData = sm->m->process(sm, sm->eap_method_priv, &ret, 389 eapReqData); 390 wpa_printf(MSG_DEBUG, "EAP: method process -> ignore=%s " 391 "methodState=%s decision=%s", 392 ret.ignore ? "TRUE" : "FALSE", 393 eap_sm_method_state_txt(ret.methodState), 394 eap_sm_decision_txt(ret.decision)); 395 396 sm->ignore = ret.ignore; 397 if (sm->ignore) 398 return; 399 sm->methodState = ret.methodState; 400 sm->decision = ret.decision; 401 sm->allowNotifications = ret.allowNotifications; 402 403 if (sm->m->isKeyAvailable && sm->m->getKey && 404 sm->m->isKeyAvailable(sm, sm->eap_method_priv)) { 405 os_free(sm->eapKeyData); 406 sm->eapKeyData = sm->m->getKey(sm, sm->eap_method_priv, 407 &sm->eapKeyDataLen); 408 os_free(sm->eapSessionId); 409 sm->eapSessionId = NULL; 410 if (sm->m->getSessionId) { 411 sm->eapSessionId = sm->m->getSessionId( 412 sm, sm->eap_method_priv, 413 &sm->eapSessionIdLen); 414 wpa_hexdump(MSG_DEBUG, "EAP: Session-Id", 415 sm->eapSessionId, sm->eapSessionIdLen); 416 } 417 } 418} 419 420 421/* 422 * This state signals the lower layer that a response packet is ready to be 423 * sent. 424 */ 425SM_STATE(EAP, SEND_RESPONSE) 426{ 427 SM_ENTRY(EAP, SEND_RESPONSE); 428 wpabuf_free(sm->lastRespData); 429 if (sm->eapRespData) { 430 if (sm->workaround) 431 os_memcpy(sm->last_md5, sm->req_md5, 16); 432 sm->lastId = sm->reqId; 433 sm->lastRespData = wpabuf_dup(sm->eapRespData); 434 eapol_set_bool(sm, EAPOL_eapResp, TRUE); 435 } else 436 sm->lastRespData = NULL; 437 eapol_set_bool(sm, EAPOL_eapReq, FALSE); 438 eapol_set_int(sm, EAPOL_idleWhile, sm->ClientTimeout); 439} 440 441 442/* 443 * This state signals the lower layer that the request was discarded, and no 444 * response packet will be sent at this time. 445 */ 446SM_STATE(EAP, DISCARD) 447{ 448 SM_ENTRY(EAP, DISCARD); 449 eapol_set_bool(sm, EAPOL_eapReq, FALSE); 450 eapol_set_bool(sm, EAPOL_eapNoResp, TRUE); 451} 452 453 454/* 455 * Handles requests for Identity method and builds a response. 456 */ 457SM_STATE(EAP, IDENTITY) 458{ 459 const struct wpabuf *eapReqData; 460 461 SM_ENTRY(EAP, IDENTITY); 462 eapReqData = eapol_get_eapReqData(sm); 463 if (!eap_hdr_len_valid(eapReqData, 1)) 464 return; 465 eap_sm_processIdentity(sm, eapReqData); 466 wpabuf_free(sm->eapRespData); 467 sm->eapRespData = NULL; 468 sm->eapRespData = eap_sm_buildIdentity(sm, sm->reqId, 0); 469} 470 471 472/* 473 * Handles requests for Notification method and builds a response. 474 */ 475SM_STATE(EAP, NOTIFICATION) 476{ 477 const struct wpabuf *eapReqData; 478 479 SM_ENTRY(EAP, NOTIFICATION); 480 eapReqData = eapol_get_eapReqData(sm); 481 if (!eap_hdr_len_valid(eapReqData, 1)) 482 return; 483 eap_sm_processNotify(sm, eapReqData); 484 wpabuf_free(sm->eapRespData); 485 sm->eapRespData = NULL; 486 sm->eapRespData = eap_sm_buildNotify(sm->reqId); 487} 488 489 490/* 491 * This state retransmits the previous response packet. 492 */ 493SM_STATE(EAP, RETRANSMIT) 494{ 495 SM_ENTRY(EAP, RETRANSMIT); 496 wpabuf_free(sm->eapRespData); 497 if (sm->lastRespData) 498 sm->eapRespData = wpabuf_dup(sm->lastRespData); 499 else 500 sm->eapRespData = NULL; 501} 502 503 504/* 505 * This state is entered in case of a successful completion of authentication 506 * and state machine waits here until port is disabled or EAP authentication is 507 * restarted. 508 */ 509SM_STATE(EAP, SUCCESS) 510{ 511 SM_ENTRY(EAP, SUCCESS); 512 if (sm->eapKeyData != NULL) 513 sm->eapKeyAvailable = TRUE; 514 eapol_set_bool(sm, EAPOL_eapSuccess, TRUE); 515 516 /* 517 * RFC 4137 does not clear eapReq here, but this seems to be required 518 * to avoid processing the same request twice when state machine is 519 * initialized. 520 */ 521 eapol_set_bool(sm, EAPOL_eapReq, FALSE); 522 523 /* 524 * RFC 4137 does not set eapNoResp here, but this seems to be required 525 * to get EAPOL Supplicant backend state machine into SUCCESS state. In 526 * addition, either eapResp or eapNoResp is required to be set after 527 * processing the received EAP frame. 528 */ 529 eapol_set_bool(sm, EAPOL_eapNoResp, TRUE); 530 531 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS 532 "EAP authentication completed successfully"); 533} 534 535 536/* 537 * This state is entered in case of a failure and state machine waits here 538 * until port is disabled or EAP authentication is restarted. 539 */ 540SM_STATE(EAP, FAILURE) 541{ 542 SM_ENTRY(EAP, FAILURE); 543 eapol_set_bool(sm, EAPOL_eapFail, TRUE); 544 545 /* 546 * RFC 4137 does not clear eapReq here, but this seems to be required 547 * to avoid processing the same request twice when state machine is 548 * initialized. 549 */ 550 eapol_set_bool(sm, EAPOL_eapReq, FALSE); 551 552 /* 553 * RFC 4137 does not set eapNoResp here. However, either eapResp or 554 * eapNoResp is required to be set after processing the received EAP 555 * frame. 556 */ 557 eapol_set_bool(sm, EAPOL_eapNoResp, TRUE); 558 559 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_FAILURE 560 "EAP authentication failed"); 561 562 sm->prev_failure = 1; 563} 564 565 566static int eap_success_workaround(struct eap_sm *sm, int reqId, int lastId) 567{ 568 /* 569 * At least Microsoft IAS and Meetinghouse Aegis seem to be sending 570 * EAP-Success/Failure with lastId + 1 even though RFC 3748 and 571 * RFC 4137 require that reqId == lastId. In addition, it looks like 572 * Ringmaster v2.1.2.0 would be using lastId + 2 in EAP-Success. 573 * 574 * Accept this kind of Id if EAP workarounds are enabled. These are 575 * unauthenticated plaintext messages, so this should have minimal 576 * security implications (bit easier to fake EAP-Success/Failure). 577 */ 578 if (sm->workaround && (reqId == ((lastId + 1) & 0xff) || 579 reqId == ((lastId + 2) & 0xff))) { 580 wpa_printf(MSG_DEBUG, "EAP: Workaround for unexpected " 581 "identifier field in EAP Success: " 582 "reqId=%d lastId=%d (these are supposed to be " 583 "same)", reqId, lastId); 584 return 1; 585 } 586 wpa_printf(MSG_DEBUG, "EAP: EAP-Success Id mismatch - reqId=%d " 587 "lastId=%d", reqId, lastId); 588 return 0; 589} 590 591 592/* 593 * RFC 4137 - Appendix A.1: EAP Peer State Machine - State transitions 594 */ 595 596static void eap_peer_sm_step_idle(struct eap_sm *sm) 597{ 598 /* 599 * The first three transitions are from RFC 4137. The last two are 600 * local additions to handle special cases with LEAP and PEAP server 601 * not sending EAP-Success in some cases. 602 */ 603 if (eapol_get_bool(sm, EAPOL_eapReq)) 604 SM_ENTER(EAP, RECEIVED); 605 else if ((eapol_get_bool(sm, EAPOL_altAccept) && 606 sm->decision != DECISION_FAIL) || 607 (eapol_get_int(sm, EAPOL_idleWhile) == 0 && 608 sm->decision == DECISION_UNCOND_SUCC)) 609 SM_ENTER(EAP, SUCCESS); 610 else if (eapol_get_bool(sm, EAPOL_altReject) || 611 (eapol_get_int(sm, EAPOL_idleWhile) == 0 && 612 sm->decision != DECISION_UNCOND_SUCC) || 613 (eapol_get_bool(sm, EAPOL_altAccept) && 614 sm->methodState != METHOD_CONT && 615 sm->decision == DECISION_FAIL)) 616 SM_ENTER(EAP, FAILURE); 617 else if (sm->selectedMethod == EAP_TYPE_LEAP && 618 sm->leap_done && sm->decision != DECISION_FAIL && 619 sm->methodState == METHOD_DONE) 620 SM_ENTER(EAP, SUCCESS); 621 else if (sm->selectedMethod == EAP_TYPE_PEAP && 622 sm->peap_done && sm->decision != DECISION_FAIL && 623 sm->methodState == METHOD_DONE) 624 SM_ENTER(EAP, SUCCESS); 625} 626 627 628static int eap_peer_req_is_duplicate(struct eap_sm *sm) 629{ 630 int duplicate; 631 632 duplicate = (sm->reqId == sm->lastId) && sm->rxReq; 633 if (sm->workaround && duplicate && 634 os_memcmp(sm->req_md5, sm->last_md5, 16) != 0) { 635 /* 636 * RFC 4137 uses (reqId == lastId) as the only verification for 637 * duplicate EAP requests. However, this misses cases where the 638 * AS is incorrectly using the same id again; and 639 * unfortunately, such implementations exist. Use MD5 hash as 640 * an extra verification for the packets being duplicate to 641 * workaround these issues. 642 */ 643 wpa_printf(MSG_DEBUG, "EAP: AS used the same Id again, but " 644 "EAP packets were not identical"); 645 wpa_printf(MSG_DEBUG, "EAP: workaround - assume this is not a " 646 "duplicate packet"); 647 duplicate = 0; 648 } 649 650 return duplicate; 651} 652 653 654static void eap_peer_sm_step_received(struct eap_sm *sm) 655{ 656 int duplicate = eap_peer_req_is_duplicate(sm); 657 658 /* 659 * Two special cases below for LEAP are local additions to work around 660 * odd LEAP behavior (EAP-Success in the middle of authentication and 661 * then swapped roles). Other transitions are based on RFC 4137. 662 */ 663 if (sm->rxSuccess && sm->decision != DECISION_FAIL && 664 (sm->reqId == sm->lastId || 665 eap_success_workaround(sm, sm->reqId, sm->lastId))) 666 SM_ENTER(EAP, SUCCESS); 667 else if (sm->methodState != METHOD_CONT && 668 ((sm->rxFailure && 669 sm->decision != DECISION_UNCOND_SUCC) || 670 (sm->rxSuccess && sm->decision == DECISION_FAIL && 671 (sm->selectedMethod != EAP_TYPE_LEAP || 672 sm->methodState != METHOD_MAY_CONT))) && 673 (sm->reqId == sm->lastId || 674 eap_success_workaround(sm, sm->reqId, sm->lastId))) 675 SM_ENTER(EAP, FAILURE); 676 else if (sm->rxReq && duplicate) 677 SM_ENTER(EAP, RETRANSMIT); 678 else if (sm->rxReq && !duplicate && 679 sm->reqMethod == EAP_TYPE_NOTIFICATION && 680 sm->allowNotifications) 681 SM_ENTER(EAP, NOTIFICATION); 682 else if (sm->rxReq && !duplicate && 683 sm->selectedMethod == EAP_TYPE_NONE && 684 sm->reqMethod == EAP_TYPE_IDENTITY) 685 SM_ENTER(EAP, IDENTITY); 686 else if (sm->rxReq && !duplicate && 687 sm->selectedMethod == EAP_TYPE_NONE && 688 sm->reqMethod != EAP_TYPE_IDENTITY && 689 sm->reqMethod != EAP_TYPE_NOTIFICATION) 690 SM_ENTER(EAP, GET_METHOD); 691 else if (sm->rxReq && !duplicate && 692 sm->reqMethod == sm->selectedMethod && 693 sm->methodState != METHOD_DONE) 694 SM_ENTER(EAP, METHOD); 695 else if (sm->selectedMethod == EAP_TYPE_LEAP && 696 (sm->rxSuccess || sm->rxResp)) 697 SM_ENTER(EAP, METHOD); 698 else 699 SM_ENTER(EAP, DISCARD); 700} 701 702 703static void eap_peer_sm_step_local(struct eap_sm *sm) 704{ 705 switch (sm->EAP_state) { 706 case EAP_INITIALIZE: 707 SM_ENTER(EAP, IDLE); 708 break; 709 case EAP_DISABLED: 710 if (eapol_get_bool(sm, EAPOL_portEnabled) && 711 !sm->force_disabled) 712 SM_ENTER(EAP, INITIALIZE); 713 break; 714 case EAP_IDLE: 715 eap_peer_sm_step_idle(sm); 716 break; 717 case EAP_RECEIVED: 718 eap_peer_sm_step_received(sm); 719 break; 720 case EAP_GET_METHOD: 721 if (sm->selectedMethod == sm->reqMethod) 722 SM_ENTER(EAP, METHOD); 723 else 724 SM_ENTER(EAP, SEND_RESPONSE); 725 break; 726 case EAP_METHOD: 727 if (sm->ignore) 728 SM_ENTER(EAP, DISCARD); 729 else 730 SM_ENTER(EAP, SEND_RESPONSE); 731 break; 732 case EAP_SEND_RESPONSE: 733 SM_ENTER(EAP, IDLE); 734 break; 735 case EAP_DISCARD: 736 SM_ENTER(EAP, IDLE); 737 break; 738 case EAP_IDENTITY: 739 SM_ENTER(EAP, SEND_RESPONSE); 740 break; 741 case EAP_NOTIFICATION: 742 SM_ENTER(EAP, SEND_RESPONSE); 743 break; 744 case EAP_RETRANSMIT: 745 SM_ENTER(EAP, SEND_RESPONSE); 746 break; 747 case EAP_SUCCESS: 748 break; 749 case EAP_FAILURE: 750 break; 751 } 752} 753 754 755SM_STEP(EAP) 756{ 757 /* Global transitions */ 758 if (eapol_get_bool(sm, EAPOL_eapRestart) && 759 eapol_get_bool(sm, EAPOL_portEnabled)) 760 SM_ENTER_GLOBAL(EAP, INITIALIZE); 761 else if (!eapol_get_bool(sm, EAPOL_portEnabled) || sm->force_disabled) 762 SM_ENTER_GLOBAL(EAP, DISABLED); 763 else if (sm->num_rounds > EAP_MAX_AUTH_ROUNDS) { 764 /* RFC 4137 does not place any limit on number of EAP messages 765 * in an authentication session. However, some error cases have 766 * ended up in a state were EAP messages were sent between the 767 * peer and server in a loop (e.g., TLS ACK frame in both 768 * direction). Since this is quite undesired outcome, limit the 769 * total number of EAP round-trips and abort authentication if 770 * this limit is exceeded. 771 */ 772 if (sm->num_rounds == EAP_MAX_AUTH_ROUNDS + 1) { 773 wpa_msg(sm->msg_ctx, MSG_INFO, "EAP: more than %d " 774 "authentication rounds - abort", 775 EAP_MAX_AUTH_ROUNDS); 776 sm->num_rounds++; 777 SM_ENTER_GLOBAL(EAP, FAILURE); 778 } 779 } else { 780 /* Local transitions */ 781 eap_peer_sm_step_local(sm); 782 } 783} 784 785 786static Boolean eap_sm_allowMethod(struct eap_sm *sm, int vendor, 787 EapType method) 788{ 789 if (!eap_allowed_method(sm, vendor, method)) { 790 wpa_printf(MSG_DEBUG, "EAP: configuration does not allow: " 791 "vendor %u method %u", vendor, method); 792 return FALSE; 793 } 794 if (eap_peer_get_eap_method(vendor, method)) 795 return TRUE; 796 wpa_printf(MSG_DEBUG, "EAP: not included in build: " 797 "vendor %u method %u", vendor, method); 798 return FALSE; 799} 800 801 802static struct wpabuf * eap_sm_build_expanded_nak( 803 struct eap_sm *sm, int id, const struct eap_method *methods, 804 size_t count) 805{ 806 struct wpabuf *resp; 807 int found = 0; 808 const struct eap_method *m; 809 810 wpa_printf(MSG_DEBUG, "EAP: Building expanded EAP-Nak"); 811 812 /* RFC 3748 - 5.3.2: Expanded Nak */ 813 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_EXPANDED, 814 8 + 8 * (count + 1), EAP_CODE_RESPONSE, id); 815 if (resp == NULL) 816 return NULL; 817 818 wpabuf_put_be24(resp, EAP_VENDOR_IETF); 819 wpabuf_put_be32(resp, EAP_TYPE_NAK); 820 821 for (m = methods; m; m = m->next) { 822 if (sm->reqVendor == m->vendor && 823 sm->reqVendorMethod == m->method) 824 continue; /* do not allow the current method again */ 825 if (eap_allowed_method(sm, m->vendor, m->method)) { 826 wpa_printf(MSG_DEBUG, "EAP: allowed type: " 827 "vendor=%u method=%u", 828 m->vendor, m->method); 829 wpabuf_put_u8(resp, EAP_TYPE_EXPANDED); 830 wpabuf_put_be24(resp, m->vendor); 831 wpabuf_put_be32(resp, m->method); 832 833 found++; 834 } 835 } 836 if (!found) { 837 wpa_printf(MSG_DEBUG, "EAP: no more allowed methods"); 838 wpabuf_put_u8(resp, EAP_TYPE_EXPANDED); 839 wpabuf_put_be24(resp, EAP_VENDOR_IETF); 840 wpabuf_put_be32(resp, EAP_TYPE_NONE); 841 } 842 843 eap_update_len(resp); 844 845 return resp; 846} 847 848 849static struct wpabuf * eap_sm_buildNak(struct eap_sm *sm, int id) 850{ 851 struct wpabuf *resp; 852 u8 *start; 853 int found = 0, expanded_found = 0; 854 size_t count; 855 const struct eap_method *methods, *m; 856 857 wpa_printf(MSG_DEBUG, "EAP: Building EAP-Nak (requested type %u " 858 "vendor=%u method=%u not allowed)", sm->reqMethod, 859 sm->reqVendor, sm->reqVendorMethod); 860 methods = eap_peer_get_methods(&count); 861 if (methods == NULL) 862 return NULL; 863 if (sm->reqMethod == EAP_TYPE_EXPANDED) 864 return eap_sm_build_expanded_nak(sm, id, methods, count); 865 866 /* RFC 3748 - 5.3.1: Legacy Nak */ 867 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NAK, 868 sizeof(struct eap_hdr) + 1 + count + 1, 869 EAP_CODE_RESPONSE, id); 870 if (resp == NULL) 871 return NULL; 872 873 start = wpabuf_put(resp, 0); 874 for (m = methods; m; m = m->next) { 875 if (m->vendor == EAP_VENDOR_IETF && m->method == sm->reqMethod) 876 continue; /* do not allow the current method again */ 877 if (eap_allowed_method(sm, m->vendor, m->method)) { 878 if (m->vendor != EAP_VENDOR_IETF) { 879 if (expanded_found) 880 continue; 881 expanded_found = 1; 882 wpabuf_put_u8(resp, EAP_TYPE_EXPANDED); 883 } else 884 wpabuf_put_u8(resp, m->method); 885 found++; 886 } 887 } 888 if (!found) 889 wpabuf_put_u8(resp, EAP_TYPE_NONE); 890 wpa_hexdump(MSG_DEBUG, "EAP: allowed methods", start, found); 891 892 eap_update_len(resp); 893 894 return resp; 895} 896 897 898static void eap_sm_processIdentity(struct eap_sm *sm, const struct wpabuf *req) 899{ 900 const u8 *pos; 901 size_t msg_len; 902 903 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_STARTED 904 "EAP authentication started"); 905 eap_notify_status(sm, "started", ""); 906 907 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY, req, 908 &msg_len); 909 if (pos == NULL) 910 return; 911 912 /* 913 * RFC 3748 - 5.1: Identity 914 * Data field may contain a displayable message in UTF-8. If this 915 * includes NUL-character, only the data before that should be 916 * displayed. Some EAP implementasitons may piggy-back additional 917 * options after the NUL. 918 */ 919 /* TODO: could save displayable message so that it can be shown to the 920 * user in case of interaction is required */ 921 wpa_hexdump_ascii(MSG_DEBUG, "EAP: EAP-Request Identity data", 922 pos, msg_len); 923} 924 925 926#ifdef PCSC_FUNCS 927 928/* 929 * Rules for figuring out MNC length based on IMSI for SIM cards that do not 930 * include MNC length field. 931 */ 932static int mnc_len_from_imsi(const char *imsi) 933{ 934 char mcc_str[4]; 935 unsigned int mcc; 936 937 os_memcpy(mcc_str, imsi, 3); 938 mcc_str[3] = '\0'; 939 mcc = atoi(mcc_str); 940 941 if (mcc == 244) 942 return 2; /* Networks in Finland use 2-digit MNC */ 943 944 return -1; 945} 946 947 948static int eap_sm_append_3gpp_realm(struct eap_sm *sm, char *imsi, 949 size_t max_len, size_t *imsi_len) 950{ 951 int mnc_len; 952 char *pos, mnc[4]; 953 954 if (*imsi_len + 36 > max_len) { 955 wpa_printf(MSG_WARNING, "No room for realm in IMSI buffer"); 956 return -1; 957 } 958 959 /* MNC (2 or 3 digits) */ 960 mnc_len = scard_get_mnc_len(sm->scard_ctx); 961 if (mnc_len < 0) 962 mnc_len = mnc_len_from_imsi(imsi); 963 if (mnc_len < 0) { 964 wpa_printf(MSG_INFO, "Failed to get MNC length from (U)SIM " 965 "assuming 3"); 966 mnc_len = 3; 967 } 968 969 if (mnc_len == 2) { 970 mnc[0] = '0'; 971 mnc[1] = imsi[3]; 972 mnc[2] = imsi[4]; 973 } else if (mnc_len == 3) { 974 mnc[0] = imsi[3]; 975 mnc[1] = imsi[4]; 976 mnc[2] = imsi[5]; 977 } 978 mnc[3] = '\0'; 979 980 pos = imsi + *imsi_len; 981 pos += os_snprintf(pos, imsi + max_len - pos, 982 "@wlan.mnc%s.mcc%c%c%c.3gppnetwork.org", 983 mnc, imsi[0], imsi[1], imsi[2]); 984 *imsi_len = pos - imsi; 985 986 return 0; 987} 988 989 990static int eap_sm_imsi_identity(struct eap_sm *sm, 991 struct eap_peer_config *conf) 992{ 993 enum { EAP_SM_SIM, EAP_SM_AKA, EAP_SM_AKA_PRIME } method = EAP_SM_SIM; 994 char imsi[100]; 995 size_t imsi_len; 996 struct eap_method_type *m = conf->eap_methods; 997 int i; 998 999 imsi_len = sizeof(imsi); 1000 if (scard_get_imsi(sm->scard_ctx, imsi, &imsi_len)) { 1001 wpa_printf(MSG_WARNING, "Failed to get IMSI from SIM"); 1002 return -1; 1003 } 1004 1005 wpa_hexdump_ascii(MSG_DEBUG, "IMSI", (u8 *) imsi, imsi_len); 1006 1007 if (imsi_len < 7) { 1008 wpa_printf(MSG_WARNING, "Too short IMSI for SIM identity"); 1009 return -1; 1010 } 1011 1012 if (eap_sm_append_3gpp_realm(sm, imsi, sizeof(imsi), &imsi_len) < 0) { 1013 wpa_printf(MSG_WARNING, "Could not add realm to SIM identity"); 1014 return -1; 1015 } 1016 wpa_hexdump_ascii(MSG_DEBUG, "IMSI + realm", (u8 *) imsi, imsi_len); 1017 1018 for (i = 0; m && (m[i].vendor != EAP_VENDOR_IETF || 1019 m[i].method != EAP_TYPE_NONE); i++) { 1020 if (m[i].vendor == EAP_VENDOR_IETF && 1021 m[i].method == EAP_TYPE_AKA_PRIME) { 1022 method = EAP_SM_AKA_PRIME; 1023 break; 1024 } 1025 1026 if (m[i].vendor == EAP_VENDOR_IETF && 1027 m[i].method == EAP_TYPE_AKA) { 1028 method = EAP_SM_AKA; 1029 break; 1030 } 1031 } 1032 1033 os_free(conf->identity); 1034 conf->identity = os_malloc(1 + imsi_len); 1035 if (conf->identity == NULL) { 1036 wpa_printf(MSG_WARNING, "Failed to allocate buffer for " 1037 "IMSI-based identity"); 1038 return -1; 1039 } 1040 1041 switch (method) { 1042 case EAP_SM_SIM: 1043 conf->identity[0] = '1'; 1044 break; 1045 case EAP_SM_AKA: 1046 conf->identity[0] = '0'; 1047 break; 1048 case EAP_SM_AKA_PRIME: 1049 conf->identity[0] = '6'; 1050 break; 1051 } 1052 os_memcpy(conf->identity + 1, imsi, imsi_len); 1053 conf->identity_len = 1 + imsi_len; 1054 1055 return 0; 1056} 1057 1058#endif /* PCSC_FUNCS */ 1059 1060 1061static int eap_sm_set_scard_pin(struct eap_sm *sm, 1062 struct eap_peer_config *conf) 1063{ 1064#ifdef PCSC_FUNCS 1065 if (scard_set_pin(sm->scard_ctx, conf->pin)) { 1066 /* 1067 * Make sure the same PIN is not tried again in order to avoid 1068 * blocking SIM. 1069 */ 1070 os_free(conf->pin); 1071 conf->pin = NULL; 1072 1073 wpa_printf(MSG_WARNING, "PIN validation failed"); 1074 eap_sm_request_pin(sm); 1075 return -1; 1076 } 1077 return 0; 1078#else /* PCSC_FUNCS */ 1079 return -1; 1080#endif /* PCSC_FUNCS */ 1081} 1082 1083static int eap_sm_get_scard_identity(struct eap_sm *sm, 1084 struct eap_peer_config *conf) 1085{ 1086#ifdef PCSC_FUNCS 1087 if (eap_sm_set_scard_pin(sm, conf)) 1088 return -1; 1089 1090 return eap_sm_imsi_identity(sm, conf); 1091#else /* PCSC_FUNCS */ 1092 return -1; 1093#endif /* PCSC_FUNCS */ 1094} 1095 1096 1097/** 1098 * eap_sm_buildIdentity - Build EAP-Identity/Response for the current network 1099 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 1100 * @id: EAP identifier for the packet 1101 * @encrypted: Whether the packet is for encrypted tunnel (EAP phase 2) 1102 * Returns: Pointer to the allocated EAP-Identity/Response packet or %NULL on 1103 * failure 1104 * 1105 * This function allocates and builds an EAP-Identity/Response packet for the 1106 * current network. The caller is responsible for freeing the returned data. 1107 */ 1108struct wpabuf * eap_sm_buildIdentity(struct eap_sm *sm, int id, int encrypted) 1109{ 1110 struct eap_peer_config *config = eap_get_config(sm); 1111 struct wpabuf *resp; 1112 const u8 *identity; 1113 size_t identity_len; 1114 1115 if (config == NULL) { 1116 wpa_printf(MSG_WARNING, "EAP: buildIdentity: configuration " 1117 "was not available"); 1118 return NULL; 1119 } 1120 1121 if (sm->m && sm->m->get_identity && 1122 (identity = sm->m->get_identity(sm, sm->eap_method_priv, 1123 &identity_len)) != NULL) { 1124 wpa_hexdump_ascii(MSG_DEBUG, "EAP: using method re-auth " 1125 "identity", identity, identity_len); 1126 } else if (!encrypted && config->anonymous_identity) { 1127 identity = config->anonymous_identity; 1128 identity_len = config->anonymous_identity_len; 1129 wpa_hexdump_ascii(MSG_DEBUG, "EAP: using anonymous identity", 1130 identity, identity_len); 1131 } else { 1132 identity = config->identity; 1133 identity_len = config->identity_len; 1134 wpa_hexdump_ascii(MSG_DEBUG, "EAP: using real identity", 1135 identity, identity_len); 1136 } 1137 1138 if (identity == NULL) { 1139 wpa_printf(MSG_WARNING, "EAP: buildIdentity: identity " 1140 "configuration was not available"); 1141 if (config->pcsc) { 1142 if (eap_sm_get_scard_identity(sm, config) < 0) 1143 return NULL; 1144 identity = config->identity; 1145 identity_len = config->identity_len; 1146 wpa_hexdump_ascii(MSG_DEBUG, "permanent identity from " 1147 "IMSI", identity, identity_len); 1148 } else { 1149 eap_sm_request_identity(sm); 1150 return NULL; 1151 } 1152 } else if (config->pcsc) { 1153 if (eap_sm_set_scard_pin(sm, config) < 0) 1154 return NULL; 1155 } 1156 1157 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY, identity_len, 1158 EAP_CODE_RESPONSE, id); 1159 if (resp == NULL) 1160 return NULL; 1161 1162 wpabuf_put_data(resp, identity, identity_len); 1163 1164 return resp; 1165} 1166 1167 1168static void eap_sm_processNotify(struct eap_sm *sm, const struct wpabuf *req) 1169{ 1170 const u8 *pos; 1171 char *msg; 1172 size_t i, msg_len; 1173 1174 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_NOTIFICATION, req, 1175 &msg_len); 1176 if (pos == NULL) 1177 return; 1178 wpa_hexdump_ascii(MSG_DEBUG, "EAP: EAP-Request Notification data", 1179 pos, msg_len); 1180 1181 msg = os_malloc(msg_len + 1); 1182 if (msg == NULL) 1183 return; 1184 for (i = 0; i < msg_len; i++) 1185 msg[i] = isprint(pos[i]) ? (char) pos[i] : '_'; 1186 msg[msg_len] = '\0'; 1187 wpa_msg(sm->msg_ctx, MSG_INFO, "%s%s", 1188 WPA_EVENT_EAP_NOTIFICATION, msg); 1189 os_free(msg); 1190} 1191 1192 1193static struct wpabuf * eap_sm_buildNotify(int id) 1194{ 1195 struct wpabuf *resp; 1196 1197 wpa_printf(MSG_DEBUG, "EAP: Generating EAP-Response Notification"); 1198 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NOTIFICATION, 0, 1199 EAP_CODE_RESPONSE, id); 1200 if (resp == NULL) 1201 return NULL; 1202 1203 return resp; 1204} 1205 1206 1207static void eap_sm_parseEapReq(struct eap_sm *sm, const struct wpabuf *req) 1208{ 1209 const struct eap_hdr *hdr; 1210 size_t plen; 1211 const u8 *pos; 1212 1213 sm->rxReq = sm->rxResp = sm->rxSuccess = sm->rxFailure = FALSE; 1214 sm->reqId = 0; 1215 sm->reqMethod = EAP_TYPE_NONE; 1216 sm->reqVendor = EAP_VENDOR_IETF; 1217 sm->reqVendorMethod = EAP_TYPE_NONE; 1218 1219 if (req == NULL || wpabuf_len(req) < sizeof(*hdr)) 1220 return; 1221 1222 hdr = wpabuf_head(req); 1223 plen = be_to_host16(hdr->length); 1224 if (plen > wpabuf_len(req)) { 1225 wpa_printf(MSG_DEBUG, "EAP: Ignored truncated EAP-Packet " 1226 "(len=%lu plen=%lu)", 1227 (unsigned long) wpabuf_len(req), 1228 (unsigned long) plen); 1229 return; 1230 } 1231 1232 sm->reqId = hdr->identifier; 1233 1234 if (sm->workaround) { 1235 const u8 *addr[1]; 1236 addr[0] = wpabuf_head(req); 1237 md5_vector(1, addr, &plen, sm->req_md5); 1238 } 1239 1240 switch (hdr->code) { 1241 case EAP_CODE_REQUEST: 1242 if (plen < sizeof(*hdr) + 1) { 1243 wpa_printf(MSG_DEBUG, "EAP: Too short EAP-Request - " 1244 "no Type field"); 1245 return; 1246 } 1247 sm->rxReq = TRUE; 1248 pos = (const u8 *) (hdr + 1); 1249 sm->reqMethod = *pos++; 1250 if (sm->reqMethod == EAP_TYPE_EXPANDED) { 1251 if (plen < sizeof(*hdr) + 8) { 1252 wpa_printf(MSG_DEBUG, "EAP: Ignored truncated " 1253 "expanded EAP-Packet (plen=%lu)", 1254 (unsigned long) plen); 1255 return; 1256 } 1257 sm->reqVendor = WPA_GET_BE24(pos); 1258 pos += 3; 1259 sm->reqVendorMethod = WPA_GET_BE32(pos); 1260 } 1261 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Request id=%d " 1262 "method=%u vendor=%u vendorMethod=%u", 1263 sm->reqId, sm->reqMethod, sm->reqVendor, 1264 sm->reqVendorMethod); 1265 break; 1266 case EAP_CODE_RESPONSE: 1267 if (sm->selectedMethod == EAP_TYPE_LEAP) { 1268 /* 1269 * LEAP differs from RFC 4137 by using reversed roles 1270 * for mutual authentication and because of this, we 1271 * need to accept EAP-Response frames if LEAP is used. 1272 */ 1273 if (plen < sizeof(*hdr) + 1) { 1274 wpa_printf(MSG_DEBUG, "EAP: Too short " 1275 "EAP-Response - no Type field"); 1276 return; 1277 } 1278 sm->rxResp = TRUE; 1279 pos = (const u8 *) (hdr + 1); 1280 sm->reqMethod = *pos; 1281 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Response for " 1282 "LEAP method=%d id=%d", 1283 sm->reqMethod, sm->reqId); 1284 break; 1285 } 1286 wpa_printf(MSG_DEBUG, "EAP: Ignored EAP-Response"); 1287 break; 1288 case EAP_CODE_SUCCESS: 1289 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Success"); 1290 eap_notify_status(sm, "completion", "success"); 1291 sm->rxSuccess = TRUE; 1292 break; 1293 case EAP_CODE_FAILURE: 1294 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Failure"); 1295 eap_notify_status(sm, "completion", "failure"); 1296 sm->rxFailure = TRUE; 1297 break; 1298 default: 1299 wpa_printf(MSG_DEBUG, "EAP: Ignored EAP-Packet with unknown " 1300 "code %d", hdr->code); 1301 break; 1302 } 1303} 1304 1305 1306static void eap_peer_sm_tls_event(void *ctx, enum tls_event ev, 1307 union tls_event_data *data) 1308{ 1309 struct eap_sm *sm = ctx; 1310 char *hash_hex = NULL; 1311 1312 switch (ev) { 1313 case TLS_CERT_CHAIN_SUCCESS: 1314 eap_notify_status(sm, "remote certificate verification", 1315 "success"); 1316 break; 1317 case TLS_CERT_CHAIN_FAILURE: 1318 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_TLS_CERT_ERROR 1319 "reason=%d depth=%d subject='%s' err='%s'", 1320 data->cert_fail.reason, 1321 data->cert_fail.depth, 1322 data->cert_fail.subject, 1323 data->cert_fail.reason_txt); 1324 eap_notify_status(sm, "remote certificate verification", 1325 data->cert_fail.reason_txt); 1326 break; 1327 case TLS_PEER_CERTIFICATE: 1328 if (!sm->eapol_cb->notify_cert) 1329 break; 1330 1331 if (data->peer_cert.hash) { 1332 size_t len = data->peer_cert.hash_len * 2 + 1; 1333 hash_hex = os_malloc(len); 1334 if (hash_hex) { 1335 wpa_snprintf_hex(hash_hex, len, 1336 data->peer_cert.hash, 1337 data->peer_cert.hash_len); 1338 } 1339 } 1340 1341 sm->eapol_cb->notify_cert(sm->eapol_ctx, 1342 data->peer_cert.depth, 1343 data->peer_cert.subject, 1344 hash_hex, data->peer_cert.cert); 1345 break; 1346 case TLS_ALERT: 1347 if (data->alert.is_local) 1348 eap_notify_status(sm, "local TLS alert", 1349 data->alert.description); 1350 else 1351 eap_notify_status(sm, "remote TLS alert", 1352 data->alert.description); 1353 break; 1354 } 1355 1356 os_free(hash_hex); 1357} 1358 1359 1360/** 1361 * eap_peer_sm_init - Allocate and initialize EAP peer state machine 1362 * @eapol_ctx: Context data to be used with eapol_cb calls 1363 * @eapol_cb: Pointer to EAPOL callback functions 1364 * @msg_ctx: Context data for wpa_msg() calls 1365 * @conf: EAP configuration 1366 * Returns: Pointer to the allocated EAP state machine or %NULL on failure 1367 * 1368 * This function allocates and initializes an EAP state machine. In addition, 1369 * this initializes TLS library for the new EAP state machine. eapol_cb pointer 1370 * will be in use until eap_peer_sm_deinit() is used to deinitialize this EAP 1371 * state machine. Consequently, the caller must make sure that this data 1372 * structure remains alive while the EAP state machine is active. 1373 */ 1374struct eap_sm * eap_peer_sm_init(void *eapol_ctx, 1375 struct eapol_callbacks *eapol_cb, 1376 void *msg_ctx, struct eap_config *conf) 1377{ 1378 struct eap_sm *sm; 1379 struct tls_config tlsconf; 1380 1381 sm = os_zalloc(sizeof(*sm)); 1382 if (sm == NULL) 1383 return NULL; 1384 sm->eapol_ctx = eapol_ctx; 1385 sm->eapol_cb = eapol_cb; 1386 sm->msg_ctx = msg_ctx; 1387 sm->ClientTimeout = EAP_CLIENT_TIMEOUT_DEFAULT; 1388 sm->wps = conf->wps; 1389 1390 os_memset(&tlsconf, 0, sizeof(tlsconf)); 1391 tlsconf.opensc_engine_path = conf->opensc_engine_path; 1392 tlsconf.pkcs11_engine_path = conf->pkcs11_engine_path; 1393 tlsconf.pkcs11_module_path = conf->pkcs11_module_path; 1394#ifdef CONFIG_FIPS 1395 tlsconf.fips_mode = 1; 1396#endif /* CONFIG_FIPS */ 1397 tlsconf.event_cb = eap_peer_sm_tls_event; 1398 tlsconf.cb_ctx = sm; 1399 tlsconf.cert_in_cb = conf->cert_in_cb; 1400 sm->ssl_ctx = tls_init(&tlsconf); 1401 if (sm->ssl_ctx == NULL) { 1402 wpa_printf(MSG_WARNING, "SSL: Failed to initialize TLS " 1403 "context."); 1404 os_free(sm); 1405 return NULL; 1406 } 1407 1408 sm->ssl_ctx2 = tls_init(&tlsconf); 1409 if (sm->ssl_ctx2 == NULL) { 1410 wpa_printf(MSG_INFO, "SSL: Failed to initialize TLS " 1411 "context (2)."); 1412 /* Run without separate TLS context within TLS tunnel */ 1413 } 1414 1415 return sm; 1416} 1417 1418 1419/** 1420 * eap_peer_sm_deinit - Deinitialize and free an EAP peer state machine 1421 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 1422 * 1423 * This function deinitializes EAP state machine and frees all allocated 1424 * resources. 1425 */ 1426void eap_peer_sm_deinit(struct eap_sm *sm) 1427{ 1428 if (sm == NULL) 1429 return; 1430 eap_deinit_prev_method(sm, "EAP deinit"); 1431 eap_sm_abort(sm); 1432 if (sm->ssl_ctx2) 1433 tls_deinit(sm->ssl_ctx2); 1434 tls_deinit(sm->ssl_ctx); 1435 os_free(sm); 1436} 1437 1438 1439/** 1440 * eap_peer_sm_step - Step EAP peer state machine 1441 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 1442 * Returns: 1 if EAP state was changed or 0 if not 1443 * 1444 * This function advances EAP state machine to a new state to match with the 1445 * current variables. This should be called whenever variables used by the EAP 1446 * state machine have changed. 1447 */ 1448int eap_peer_sm_step(struct eap_sm *sm) 1449{ 1450 int res = 0; 1451 do { 1452 sm->changed = FALSE; 1453 SM_STEP_RUN(EAP); 1454 if (sm->changed) 1455 res = 1; 1456 } while (sm->changed); 1457 return res; 1458} 1459 1460 1461/** 1462 * eap_sm_abort - Abort EAP authentication 1463 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 1464 * 1465 * Release system resources that have been allocated for the authentication 1466 * session without fully deinitializing the EAP state machine. 1467 */ 1468void eap_sm_abort(struct eap_sm *sm) 1469{ 1470 wpabuf_free(sm->lastRespData); 1471 sm->lastRespData = NULL; 1472 wpabuf_free(sm->eapRespData); 1473 sm->eapRespData = NULL; 1474 os_free(sm->eapKeyData); 1475 sm->eapKeyData = NULL; 1476 os_free(sm->eapSessionId); 1477 sm->eapSessionId = NULL; 1478 1479 /* This is not clearly specified in the EAP statemachines draft, but 1480 * it seems necessary to make sure that some of the EAPOL variables get 1481 * cleared for the next authentication. */ 1482 eapol_set_bool(sm, EAPOL_eapSuccess, FALSE); 1483} 1484 1485 1486#ifdef CONFIG_CTRL_IFACE 1487static const char * eap_sm_state_txt(int state) 1488{ 1489 switch (state) { 1490 case EAP_INITIALIZE: 1491 return "INITIALIZE"; 1492 case EAP_DISABLED: 1493 return "DISABLED"; 1494 case EAP_IDLE: 1495 return "IDLE"; 1496 case EAP_RECEIVED: 1497 return "RECEIVED"; 1498 case EAP_GET_METHOD: 1499 return "GET_METHOD"; 1500 case EAP_METHOD: 1501 return "METHOD"; 1502 case EAP_SEND_RESPONSE: 1503 return "SEND_RESPONSE"; 1504 case EAP_DISCARD: 1505 return "DISCARD"; 1506 case EAP_IDENTITY: 1507 return "IDENTITY"; 1508 case EAP_NOTIFICATION: 1509 return "NOTIFICATION"; 1510 case EAP_RETRANSMIT: 1511 return "RETRANSMIT"; 1512 case EAP_SUCCESS: 1513 return "SUCCESS"; 1514 case EAP_FAILURE: 1515 return "FAILURE"; 1516 default: 1517 return "UNKNOWN"; 1518 } 1519} 1520#endif /* CONFIG_CTRL_IFACE */ 1521 1522 1523#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG) 1524static const char * eap_sm_method_state_txt(EapMethodState state) 1525{ 1526 switch (state) { 1527 case METHOD_NONE: 1528 return "NONE"; 1529 case METHOD_INIT: 1530 return "INIT"; 1531 case METHOD_CONT: 1532 return "CONT"; 1533 case METHOD_MAY_CONT: 1534 return "MAY_CONT"; 1535 case METHOD_DONE: 1536 return "DONE"; 1537 default: 1538 return "UNKNOWN"; 1539 } 1540} 1541 1542 1543static const char * eap_sm_decision_txt(EapDecision decision) 1544{ 1545 switch (decision) { 1546 case DECISION_FAIL: 1547 return "FAIL"; 1548 case DECISION_COND_SUCC: 1549 return "COND_SUCC"; 1550 case DECISION_UNCOND_SUCC: 1551 return "UNCOND_SUCC"; 1552 default: 1553 return "UNKNOWN"; 1554 } 1555} 1556#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */ 1557 1558 1559#ifdef CONFIG_CTRL_IFACE 1560 1561/** 1562 * eap_sm_get_status - Get EAP state machine status 1563 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 1564 * @buf: Buffer for status information 1565 * @buflen: Maximum buffer length 1566 * @verbose: Whether to include verbose status information 1567 * Returns: Number of bytes written to buf. 1568 * 1569 * Query EAP state machine for status information. This function fills in a 1570 * text area with current status information from the EAPOL state machine. If 1571 * the buffer (buf) is not large enough, status information will be truncated 1572 * to fit the buffer. 1573 */ 1574int eap_sm_get_status(struct eap_sm *sm, char *buf, size_t buflen, int verbose) 1575{ 1576 int len, ret; 1577 1578 if (sm == NULL) 1579 return 0; 1580 1581 len = os_snprintf(buf, buflen, 1582 "EAP state=%s\n", 1583 eap_sm_state_txt(sm->EAP_state)); 1584 if (len < 0 || (size_t) len >= buflen) 1585 return 0; 1586 1587 if (sm->selectedMethod != EAP_TYPE_NONE) { 1588 const char *name; 1589 if (sm->m) { 1590 name = sm->m->name; 1591 } else { 1592 const struct eap_method *m = 1593 eap_peer_get_eap_method(EAP_VENDOR_IETF, 1594 sm->selectedMethod); 1595 if (m) 1596 name = m->name; 1597 else 1598 name = "?"; 1599 } 1600 ret = os_snprintf(buf + len, buflen - len, 1601 "selectedMethod=%d (EAP-%s)\n", 1602 sm->selectedMethod, name); 1603 if (ret < 0 || (size_t) ret >= buflen - len) 1604 return len; 1605 len += ret; 1606 1607 if (sm->m && sm->m->get_status) { 1608 len += sm->m->get_status(sm, sm->eap_method_priv, 1609 buf + len, buflen - len, 1610 verbose); 1611 } 1612 } 1613 1614 if (verbose) { 1615 ret = os_snprintf(buf + len, buflen - len, 1616 "reqMethod=%d\n" 1617 "methodState=%s\n" 1618 "decision=%s\n" 1619 "ClientTimeout=%d\n", 1620 sm->reqMethod, 1621 eap_sm_method_state_txt(sm->methodState), 1622 eap_sm_decision_txt(sm->decision), 1623 sm->ClientTimeout); 1624 if (ret < 0 || (size_t) ret >= buflen - len) 1625 return len; 1626 len += ret; 1627 } 1628 1629 return len; 1630} 1631#endif /* CONFIG_CTRL_IFACE */ 1632 1633 1634#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG) 1635static void eap_sm_request(struct eap_sm *sm, enum wpa_ctrl_req_type field, 1636 const char *msg, size_t msglen) 1637{ 1638 struct eap_peer_config *config; 1639 char *txt = NULL, *tmp; 1640 1641 if (sm == NULL) 1642 return; 1643 config = eap_get_config(sm); 1644 if (config == NULL) 1645 return; 1646 1647 switch (field) { 1648 case WPA_CTRL_REQ_EAP_IDENTITY: 1649 config->pending_req_identity++; 1650 break; 1651 case WPA_CTRL_REQ_EAP_PASSWORD: 1652 config->pending_req_password++; 1653 break; 1654 case WPA_CTRL_REQ_EAP_NEW_PASSWORD: 1655 config->pending_req_new_password++; 1656 break; 1657 case WPA_CTRL_REQ_EAP_PIN: 1658 config->pending_req_pin++; 1659 break; 1660 case WPA_CTRL_REQ_EAP_OTP: 1661 if (msg) { 1662 tmp = os_malloc(msglen + 3); 1663 if (tmp == NULL) 1664 return; 1665 tmp[0] = '['; 1666 os_memcpy(tmp + 1, msg, msglen); 1667 tmp[msglen + 1] = ']'; 1668 tmp[msglen + 2] = '\0'; 1669 txt = tmp; 1670 os_free(config->pending_req_otp); 1671 config->pending_req_otp = tmp; 1672 config->pending_req_otp_len = msglen + 3; 1673 } else { 1674 if (config->pending_req_otp == NULL) 1675 return; 1676 txt = config->pending_req_otp; 1677 } 1678 break; 1679 case WPA_CTRL_REQ_EAP_PASSPHRASE: 1680 config->pending_req_passphrase++; 1681 break; 1682 default: 1683 return; 1684 } 1685 1686 if (sm->eapol_cb->eap_param_needed) 1687 sm->eapol_cb->eap_param_needed(sm->eapol_ctx, field, txt); 1688} 1689#else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */ 1690#define eap_sm_request(sm, type, msg, msglen) do { } while (0) 1691#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */ 1692 1693const char * eap_sm_get_method_name(struct eap_sm *sm) 1694{ 1695 if (sm->m == NULL) 1696 return "UNKNOWN"; 1697 return sm->m->name; 1698} 1699 1700 1701/** 1702 * eap_sm_request_identity - Request identity from user (ctrl_iface) 1703 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 1704 * 1705 * EAP methods can call this function to request identity information for the 1706 * current network. This is normally called when the identity is not included 1707 * in the network configuration. The request will be sent to monitor programs 1708 * through the control interface. 1709 */ 1710void eap_sm_request_identity(struct eap_sm *sm) 1711{ 1712 eap_sm_request(sm, WPA_CTRL_REQ_EAP_IDENTITY, NULL, 0); 1713} 1714 1715 1716/** 1717 * eap_sm_request_password - Request password from user (ctrl_iface) 1718 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 1719 * 1720 * EAP methods can call this function to request password information for the 1721 * current network. This is normally called when the password is not included 1722 * in the network configuration. The request will be sent to monitor programs 1723 * through the control interface. 1724 */ 1725void eap_sm_request_password(struct eap_sm *sm) 1726{ 1727 eap_sm_request(sm, WPA_CTRL_REQ_EAP_PASSWORD, NULL, 0); 1728} 1729 1730 1731/** 1732 * eap_sm_request_new_password - Request new password from user (ctrl_iface) 1733 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 1734 * 1735 * EAP methods can call this function to request new password information for 1736 * the current network. This is normally called when the EAP method indicates 1737 * that the current password has expired and password change is required. The 1738 * request will be sent to monitor programs through the control interface. 1739 */ 1740void eap_sm_request_new_password(struct eap_sm *sm) 1741{ 1742 eap_sm_request(sm, WPA_CTRL_REQ_EAP_NEW_PASSWORD, NULL, 0); 1743} 1744 1745 1746/** 1747 * eap_sm_request_pin - Request SIM or smart card PIN from user (ctrl_iface) 1748 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 1749 * 1750 * EAP methods can call this function to request SIM or smart card PIN 1751 * information for the current network. This is normally called when the PIN is 1752 * not included in the network configuration. The request will be sent to 1753 * monitor programs through the control interface. 1754 */ 1755void eap_sm_request_pin(struct eap_sm *sm) 1756{ 1757 eap_sm_request(sm, WPA_CTRL_REQ_EAP_PIN, NULL, 0); 1758} 1759 1760 1761/** 1762 * eap_sm_request_otp - Request one time password from user (ctrl_iface) 1763 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 1764 * @msg: Message to be displayed to the user when asking for OTP 1765 * @msg_len: Length of the user displayable message 1766 * 1767 * EAP methods can call this function to request open time password (OTP) for 1768 * the current network. The request will be sent to monitor programs through 1769 * the control interface. 1770 */ 1771void eap_sm_request_otp(struct eap_sm *sm, const char *msg, size_t msg_len) 1772{ 1773 eap_sm_request(sm, WPA_CTRL_REQ_EAP_OTP, msg, msg_len); 1774} 1775 1776 1777/** 1778 * eap_sm_request_passphrase - Request passphrase from user (ctrl_iface) 1779 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 1780 * 1781 * EAP methods can call this function to request passphrase for a private key 1782 * for the current network. This is normally called when the passphrase is not 1783 * included in the network configuration. The request will be sent to monitor 1784 * programs through the control interface. 1785 */ 1786void eap_sm_request_passphrase(struct eap_sm *sm) 1787{ 1788 eap_sm_request(sm, WPA_CTRL_REQ_EAP_PASSPHRASE, NULL, 0); 1789} 1790 1791 1792/** 1793 * eap_sm_notify_ctrl_attached - Notification of attached monitor 1794 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 1795 * 1796 * Notify EAP state machines that a monitor was attached to the control 1797 * interface to trigger re-sending of pending requests for user input. 1798 */ 1799void eap_sm_notify_ctrl_attached(struct eap_sm *sm) 1800{ 1801 struct eap_peer_config *config = eap_get_config(sm); 1802 1803 if (config == NULL) 1804 return; 1805 1806 /* Re-send any pending requests for user data since a new control 1807 * interface was added. This handles cases where the EAP authentication 1808 * starts immediately after system startup when the user interface is 1809 * not yet running. */ 1810 if (config->pending_req_identity) 1811 eap_sm_request_identity(sm); 1812 if (config->pending_req_password) 1813 eap_sm_request_password(sm); 1814 if (config->pending_req_new_password) 1815 eap_sm_request_new_password(sm); 1816 if (config->pending_req_otp) 1817 eap_sm_request_otp(sm, NULL, 0); 1818 if (config->pending_req_pin) 1819 eap_sm_request_pin(sm); 1820 if (config->pending_req_passphrase) 1821 eap_sm_request_passphrase(sm); 1822} 1823 1824 1825static int eap_allowed_phase2_type(int vendor, int type) 1826{ 1827 if (vendor != EAP_VENDOR_IETF) 1828 return 0; 1829 return type != EAP_TYPE_PEAP && type != EAP_TYPE_TTLS && 1830 type != EAP_TYPE_FAST; 1831} 1832 1833 1834/** 1835 * eap_get_phase2_type - Get EAP type for the given EAP phase 2 method name 1836 * @name: EAP method name, e.g., MD5 1837 * @vendor: Buffer for returning EAP Vendor-Id 1838 * Returns: EAP method type or %EAP_TYPE_NONE if not found 1839 * 1840 * This function maps EAP type names into EAP type numbers that are allowed for 1841 * Phase 2, i.e., for tunneled authentication. Phase 2 is used, e.g., with 1842 * EAP-PEAP, EAP-TTLS, and EAP-FAST. 1843 */ 1844u32 eap_get_phase2_type(const char *name, int *vendor) 1845{ 1846 int v; 1847 u8 type = eap_peer_get_type(name, &v); 1848 if (eap_allowed_phase2_type(v, type)) { 1849 *vendor = v; 1850 return type; 1851 } 1852 *vendor = EAP_VENDOR_IETF; 1853 return EAP_TYPE_NONE; 1854} 1855 1856 1857/** 1858 * eap_get_phase2_types - Get list of allowed EAP phase 2 types 1859 * @config: Pointer to a network configuration 1860 * @count: Pointer to a variable to be filled with number of returned EAP types 1861 * Returns: Pointer to allocated type list or %NULL on failure 1862 * 1863 * This function generates an array of allowed EAP phase 2 (tunneled) types for 1864 * the given network configuration. 1865 */ 1866struct eap_method_type * eap_get_phase2_types(struct eap_peer_config *config, 1867 size_t *count) 1868{ 1869 struct eap_method_type *buf; 1870 u32 method; 1871 int vendor; 1872 size_t mcount; 1873 const struct eap_method *methods, *m; 1874 1875 methods = eap_peer_get_methods(&mcount); 1876 if (methods == NULL) 1877 return NULL; 1878 *count = 0; 1879 buf = os_malloc(mcount * sizeof(struct eap_method_type)); 1880 if (buf == NULL) 1881 return NULL; 1882 1883 for (m = methods; m; m = m->next) { 1884 vendor = m->vendor; 1885 method = m->method; 1886 if (eap_allowed_phase2_type(vendor, method)) { 1887 if (vendor == EAP_VENDOR_IETF && 1888 method == EAP_TYPE_TLS && config && 1889 config->private_key2 == NULL) 1890 continue; 1891 buf[*count].vendor = vendor; 1892 buf[*count].method = method; 1893 (*count)++; 1894 } 1895 } 1896 1897 return buf; 1898} 1899 1900 1901/** 1902 * eap_set_fast_reauth - Update fast_reauth setting 1903 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 1904 * @enabled: 1 = Fast reauthentication is enabled, 0 = Disabled 1905 */ 1906void eap_set_fast_reauth(struct eap_sm *sm, int enabled) 1907{ 1908 sm->fast_reauth = enabled; 1909} 1910 1911 1912/** 1913 * eap_set_workaround - Update EAP workarounds setting 1914 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 1915 * @workaround: 1 = Enable EAP workarounds, 0 = Disable EAP workarounds 1916 */ 1917void eap_set_workaround(struct eap_sm *sm, unsigned int workaround) 1918{ 1919 sm->workaround = workaround; 1920} 1921 1922 1923/** 1924 * eap_get_config - Get current network configuration 1925 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 1926 * Returns: Pointer to the current network configuration or %NULL if not found 1927 * 1928 * EAP peer methods should avoid using this function if they can use other 1929 * access functions, like eap_get_config_identity() and 1930 * eap_get_config_password(), that do not require direct access to 1931 * struct eap_peer_config. 1932 */ 1933struct eap_peer_config * eap_get_config(struct eap_sm *sm) 1934{ 1935 return sm->eapol_cb->get_config(sm->eapol_ctx); 1936} 1937 1938 1939/** 1940 * eap_get_config_identity - Get identity from the network configuration 1941 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 1942 * @len: Buffer for the length of the identity 1943 * Returns: Pointer to the identity or %NULL if not found 1944 */ 1945const u8 * eap_get_config_identity(struct eap_sm *sm, size_t *len) 1946{ 1947 struct eap_peer_config *config = eap_get_config(sm); 1948 if (config == NULL) 1949 return NULL; 1950 *len = config->identity_len; 1951 return config->identity; 1952} 1953 1954 1955static int eap_get_ext_password(struct eap_sm *sm, 1956 struct eap_peer_config *config) 1957{ 1958 char *name; 1959 1960 if (config->password == NULL) 1961 return -1; 1962 1963 name = os_zalloc(config->password_len + 1); 1964 if (name == NULL) 1965 return -1; 1966 os_memcpy(name, config->password, config->password_len); 1967 1968 ext_password_free(sm->ext_pw_buf); 1969 sm->ext_pw_buf = ext_password_get(sm->ext_pw, name); 1970 os_free(name); 1971 1972 return sm->ext_pw_buf == NULL ? -1 : 0; 1973} 1974 1975 1976/** 1977 * eap_get_config_password - Get password from the network configuration 1978 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 1979 * @len: Buffer for the length of the password 1980 * Returns: Pointer to the password or %NULL if not found 1981 */ 1982const u8 * eap_get_config_password(struct eap_sm *sm, size_t *len) 1983{ 1984 struct eap_peer_config *config = eap_get_config(sm); 1985 if (config == NULL) 1986 return NULL; 1987 1988 if (config->flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) { 1989 if (eap_get_ext_password(sm, config) < 0) 1990 return NULL; 1991 *len = wpabuf_len(sm->ext_pw_buf); 1992 return wpabuf_head(sm->ext_pw_buf); 1993 } 1994 1995 *len = config->password_len; 1996 return config->password; 1997} 1998 1999 2000/** 2001 * eap_get_config_password2 - Get password from the network configuration 2002 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 2003 * @len: Buffer for the length of the password 2004 * @hash: Buffer for returning whether the password is stored as a 2005 * NtPasswordHash instead of plaintext password; can be %NULL if this 2006 * information is not needed 2007 * Returns: Pointer to the password or %NULL if not found 2008 */ 2009const u8 * eap_get_config_password2(struct eap_sm *sm, size_t *len, int *hash) 2010{ 2011 struct eap_peer_config *config = eap_get_config(sm); 2012 if (config == NULL) 2013 return NULL; 2014 2015 if (config->flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) { 2016 if (eap_get_ext_password(sm, config) < 0) 2017 return NULL; 2018 *len = wpabuf_len(sm->ext_pw_buf); 2019 return wpabuf_head(sm->ext_pw_buf); 2020 } 2021 2022 *len = config->password_len; 2023 if (hash) 2024 *hash = !!(config->flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH); 2025 return config->password; 2026} 2027 2028 2029/** 2030 * eap_get_config_new_password - Get new password from network configuration 2031 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 2032 * @len: Buffer for the length of the new password 2033 * Returns: Pointer to the new password or %NULL if not found 2034 */ 2035const u8 * eap_get_config_new_password(struct eap_sm *sm, size_t *len) 2036{ 2037 struct eap_peer_config *config = eap_get_config(sm); 2038 if (config == NULL) 2039 return NULL; 2040 *len = config->new_password_len; 2041 return config->new_password; 2042} 2043 2044 2045/** 2046 * eap_get_config_otp - Get one-time password from the network configuration 2047 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 2048 * @len: Buffer for the length of the one-time password 2049 * Returns: Pointer to the one-time password or %NULL if not found 2050 */ 2051const u8 * eap_get_config_otp(struct eap_sm *sm, size_t *len) 2052{ 2053 struct eap_peer_config *config = eap_get_config(sm); 2054 if (config == NULL) 2055 return NULL; 2056 *len = config->otp_len; 2057 return config->otp; 2058} 2059 2060 2061/** 2062 * eap_clear_config_otp - Clear used one-time password 2063 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 2064 * 2065 * This function clears a used one-time password (OTP) from the current network 2066 * configuration. This should be called when the OTP has been used and is not 2067 * needed anymore. 2068 */ 2069void eap_clear_config_otp(struct eap_sm *sm) 2070{ 2071 struct eap_peer_config *config = eap_get_config(sm); 2072 if (config == NULL) 2073 return; 2074 os_memset(config->otp, 0, config->otp_len); 2075 os_free(config->otp); 2076 config->otp = NULL; 2077 config->otp_len = 0; 2078} 2079 2080 2081/** 2082 * eap_get_config_phase1 - Get phase1 data from the network configuration 2083 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 2084 * Returns: Pointer to the phase1 data or %NULL if not found 2085 */ 2086const char * eap_get_config_phase1(struct eap_sm *sm) 2087{ 2088 struct eap_peer_config *config = eap_get_config(sm); 2089 if (config == NULL) 2090 return NULL; 2091 return config->phase1; 2092} 2093 2094 2095/** 2096 * eap_get_config_phase2 - Get phase2 data from the network configuration 2097 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 2098 * Returns: Pointer to the phase1 data or %NULL if not found 2099 */ 2100const char * eap_get_config_phase2(struct eap_sm *sm) 2101{ 2102 struct eap_peer_config *config = eap_get_config(sm); 2103 if (config == NULL) 2104 return NULL; 2105 return config->phase2; 2106} 2107 2108 2109int eap_get_config_fragment_size(struct eap_sm *sm) 2110{ 2111 struct eap_peer_config *config = eap_get_config(sm); 2112 if (config == NULL) 2113 return -1; 2114 return config->fragment_size; 2115} 2116 2117 2118/** 2119 * eap_key_available - Get key availability (eapKeyAvailable variable) 2120 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 2121 * Returns: 1 if EAP keying material is available, 0 if not 2122 */ 2123int eap_key_available(struct eap_sm *sm) 2124{ 2125 return sm ? sm->eapKeyAvailable : 0; 2126} 2127 2128 2129/** 2130 * eap_notify_success - Notify EAP state machine about external success trigger 2131 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 2132 * 2133 * This function is called when external event, e.g., successful completion of 2134 * WPA-PSK key handshake, is indicating that EAP state machine should move to 2135 * success state. This is mainly used with security modes that do not use EAP 2136 * state machine (e.g., WPA-PSK). 2137 */ 2138void eap_notify_success(struct eap_sm *sm) 2139{ 2140 if (sm) { 2141 sm->decision = DECISION_COND_SUCC; 2142 sm->EAP_state = EAP_SUCCESS; 2143 } 2144} 2145 2146 2147/** 2148 * eap_notify_lower_layer_success - Notification of lower layer success 2149 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 2150 * 2151 * Notify EAP state machines that a lower layer has detected a successful 2152 * authentication. This is used to recover from dropped EAP-Success messages. 2153 */ 2154void eap_notify_lower_layer_success(struct eap_sm *sm) 2155{ 2156 if (sm == NULL) 2157 return; 2158 2159 if (eapol_get_bool(sm, EAPOL_eapSuccess) || 2160 sm->decision == DECISION_FAIL || 2161 (sm->methodState != METHOD_MAY_CONT && 2162 sm->methodState != METHOD_DONE)) 2163 return; 2164 2165 if (sm->eapKeyData != NULL) 2166 sm->eapKeyAvailable = TRUE; 2167 eapol_set_bool(sm, EAPOL_eapSuccess, TRUE); 2168 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS 2169 "EAP authentication completed successfully (based on lower " 2170 "layer success)"); 2171} 2172 2173 2174/** 2175 * eap_get_eapSessionId - Get Session-Id from EAP state machine 2176 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 2177 * @len: Pointer to variable that will be set to number of bytes in the session 2178 * Returns: Pointer to the EAP Session-Id or %NULL on failure 2179 * 2180 * Fetch EAP Session-Id from the EAP state machine. The Session-Id is available 2181 * only after a successful authentication. EAP state machine continues to manage 2182 * the Session-Id and the caller must not change or free the returned data. 2183 */ 2184const u8 * eap_get_eapSessionId(struct eap_sm *sm, size_t *len) 2185{ 2186 if (sm == NULL || sm->eapSessionId == NULL) { 2187 *len = 0; 2188 return NULL; 2189 } 2190 2191 *len = sm->eapSessionIdLen; 2192 return sm->eapSessionId; 2193} 2194 2195 2196/** 2197 * eap_get_eapKeyData - Get master session key (MSK) from EAP state machine 2198 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 2199 * @len: Pointer to variable that will be set to number of bytes in the key 2200 * Returns: Pointer to the EAP keying data or %NULL on failure 2201 * 2202 * Fetch EAP keying material (MSK, eapKeyData) from the EAP state machine. The 2203 * key is available only after a successful authentication. EAP state machine 2204 * continues to manage the key data and the caller must not change or free the 2205 * returned data. 2206 */ 2207const u8 * eap_get_eapKeyData(struct eap_sm *sm, size_t *len) 2208{ 2209 if (sm == NULL || sm->eapKeyData == NULL) { 2210 *len = 0; 2211 return NULL; 2212 } 2213 2214 *len = sm->eapKeyDataLen; 2215 return sm->eapKeyData; 2216} 2217 2218 2219/** 2220 * eap_get_eapKeyData - Get EAP response data 2221 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 2222 * Returns: Pointer to the EAP response (eapRespData) or %NULL on failure 2223 * 2224 * Fetch EAP response (eapRespData) from the EAP state machine. This data is 2225 * available when EAP state machine has processed an incoming EAP request. The 2226 * EAP state machine does not maintain a reference to the response after this 2227 * function is called and the caller is responsible for freeing the data. 2228 */ 2229struct wpabuf * eap_get_eapRespData(struct eap_sm *sm) 2230{ 2231 struct wpabuf *resp; 2232 2233 if (sm == NULL || sm->eapRespData == NULL) 2234 return NULL; 2235 2236 resp = sm->eapRespData; 2237 sm->eapRespData = NULL; 2238 2239 return resp; 2240} 2241 2242 2243/** 2244 * eap_sm_register_scard_ctx - Notification of smart card context 2245 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 2246 * @ctx: Context data for smart card operations 2247 * 2248 * Notify EAP state machines of context data for smart card operations. This 2249 * context data will be used as a parameter for scard_*() functions. 2250 */ 2251void eap_register_scard_ctx(struct eap_sm *sm, void *ctx) 2252{ 2253 if (sm) 2254 sm->scard_ctx = ctx; 2255} 2256 2257 2258/** 2259 * eap_set_config_blob - Set or add a named configuration blob 2260 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 2261 * @blob: New value for the blob 2262 * 2263 * Adds a new configuration blob or replaces the current value of an existing 2264 * blob. 2265 */ 2266void eap_set_config_blob(struct eap_sm *sm, struct wpa_config_blob *blob) 2267{ 2268#ifndef CONFIG_NO_CONFIG_BLOBS 2269 sm->eapol_cb->set_config_blob(sm->eapol_ctx, blob); 2270#endif /* CONFIG_NO_CONFIG_BLOBS */ 2271} 2272 2273 2274/** 2275 * eap_get_config_blob - Get a named configuration blob 2276 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 2277 * @name: Name of the blob 2278 * Returns: Pointer to blob data or %NULL if not found 2279 */ 2280const struct wpa_config_blob * eap_get_config_blob(struct eap_sm *sm, 2281 const char *name) 2282{ 2283#ifndef CONFIG_NO_CONFIG_BLOBS 2284 return sm->eapol_cb->get_config_blob(sm->eapol_ctx, name); 2285#else /* CONFIG_NO_CONFIG_BLOBS */ 2286 return NULL; 2287#endif /* CONFIG_NO_CONFIG_BLOBS */ 2288} 2289 2290 2291/** 2292 * eap_set_force_disabled - Set force_disabled flag 2293 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 2294 * @disabled: 1 = EAP disabled, 0 = EAP enabled 2295 * 2296 * This function is used to force EAP state machine to be disabled when it is 2297 * not in use (e.g., with WPA-PSK or plaintext connections). 2298 */ 2299void eap_set_force_disabled(struct eap_sm *sm, int disabled) 2300{ 2301 sm->force_disabled = disabled; 2302} 2303 2304 2305 /** 2306 * eap_notify_pending - Notify that EAP method is ready to re-process a request 2307 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 2308 * 2309 * An EAP method can perform a pending operation (e.g., to get a response from 2310 * an external process). Once the response is available, this function can be 2311 * used to request EAPOL state machine to retry delivering the previously 2312 * received (and still unanswered) EAP request to EAP state machine. 2313 */ 2314void eap_notify_pending(struct eap_sm *sm) 2315{ 2316 sm->eapol_cb->notify_pending(sm->eapol_ctx); 2317} 2318 2319 2320/** 2321 * eap_invalidate_cached_session - Mark cached session data invalid 2322 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 2323 */ 2324void eap_invalidate_cached_session(struct eap_sm *sm) 2325{ 2326 if (sm) 2327 eap_deinit_prev_method(sm, "invalidate"); 2328} 2329 2330 2331int eap_is_wps_pbc_enrollee(struct eap_peer_config *conf) 2332{ 2333 if (conf->identity_len != WSC_ID_ENROLLEE_LEN || 2334 os_memcmp(conf->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN)) 2335 return 0; /* Not a WPS Enrollee */ 2336 2337 if (conf->phase1 == NULL || os_strstr(conf->phase1, "pbc=1") == NULL) 2338 return 0; /* Not using PBC */ 2339 2340 return 1; 2341} 2342 2343 2344int eap_is_wps_pin_enrollee(struct eap_peer_config *conf) 2345{ 2346 if (conf->identity_len != WSC_ID_ENROLLEE_LEN || 2347 os_memcmp(conf->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN)) 2348 return 0; /* Not a WPS Enrollee */ 2349 2350 if (conf->phase1 == NULL || os_strstr(conf->phase1, "pin=") == NULL) 2351 return 0; /* Not using PIN */ 2352 2353 return 1; 2354} 2355 2356 2357void eap_sm_set_ext_pw_ctx(struct eap_sm *sm, struct ext_password_data *ext) 2358{ 2359 ext_password_free(sm->ext_pw_buf); 2360 sm->ext_pw_buf = NULL; 2361 sm->ext_pw = ext; 2362} 2363 2364 2365/** 2366 * eap_set_anon_id - Set or add anonymous identity 2367 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init() 2368 * @id: Anonymous identity (e.g., EAP-SIM pseudonym) or %NULL to clear 2369 * @len: Length of anonymous identity in octets 2370 */ 2371void eap_set_anon_id(struct eap_sm *sm, const u8 *id, size_t len) 2372{ 2373 if (sm->eapol_cb->set_anon_id) 2374 sm->eapol_cb->set_anon_id(sm->eapol_ctx, id, len); 2375} 2376