📄 eap.c
字号:
/* * EAP peer state machines (RFC 4137) * Copyright (c) 2004-2006, Jouni Malinen <j@w1.fi> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * Alternatively, this software may be distributed under the terms of BSD * license. * * See README and COPYING for more details. * * This file implements the Peer State Machine as defined in RFC 4137. The used * states and state transitions match mostly with the RFC. However, there are * couple of additional transitions for working around small issues noticed * during testing. These exceptions are explained in comments within the * functions in this file. The method functions, m.func(), are similar to the * ones used in RFC 4137, but some small changes have used here to optimize * operations and to add functionality needed for fast re-authentication * (session resumption). */#include "includes.h"#include "common.h"#include "eap_i.h"#include "config_ssid.h"#include "tls.h"#include "crypto.h"#include "pcsc_funcs.h"#include "wpa_ctrl.h"#include "state_machine.h"#define STATE_MACHINE_DATA struct eap_sm#define STATE_MACHINE_DEBUG_PREFIX "EAP"#define EAP_MAX_AUTH_ROUNDS 50static Boolean eap_sm_allowMethod(struct eap_sm *sm, int vendor, EapType method);static u8 * eap_sm_buildNak(struct eap_sm *sm, int id, size_t *len);static void eap_sm_processIdentity(struct eap_sm *sm, const u8 *req);static void eap_sm_processNotify(struct eap_sm *sm, const u8 *req);static u8 * eap_sm_buildNotify(int id, size_t *len);static void eap_sm_parseEapReq(struct eap_sm *sm, const u8 *req, size_t len);#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)static const char * eap_sm_method_state_txt(EapMethodState state);static const char * eap_sm_decision_txt(EapDecision decision);#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */static Boolean eapol_get_bool(struct eap_sm *sm, enum eapol_bool_var var){ return sm->eapol_cb->get_bool(sm->eapol_ctx, var);}static void eapol_set_bool(struct eap_sm *sm, enum eapol_bool_var var, Boolean value){ sm->eapol_cb->set_bool(sm->eapol_ctx, var, value);}static unsigned int eapol_get_int(struct eap_sm *sm, enum eapol_int_var var){ return sm->eapol_cb->get_int(sm->eapol_ctx, var);}static void eapol_set_int(struct eap_sm *sm, enum eapol_int_var var, unsigned int value){ sm->eapol_cb->set_int(sm->eapol_ctx, var, value);}static u8 * eapol_get_eapReqData(struct eap_sm *sm, size_t *len){ return sm->eapol_cb->get_eapReqData(sm->eapol_ctx, len);}static void eap_deinit_prev_method(struct eap_sm *sm, const char *txt){ if (sm->m == NULL || sm->eap_method_priv == NULL) return; wpa_printf(MSG_DEBUG, "EAP: deinitialize previously used EAP method " "(%d, %s) at %s", sm->selectedMethod, sm->m->name, txt); sm->m->deinit(sm, sm->eap_method_priv); sm->eap_method_priv = NULL; sm->m = NULL;}/* * This state initializes state machine variables when the machine is * activated (portEnabled = TRUE). This is also used when re-starting * authentication (eapRestart == TRUE). */SM_STATE(EAP, INITIALIZE){ SM_ENTRY(EAP, INITIALIZE); if (sm->fast_reauth && sm->m && sm->m->has_reauth_data && sm->m->has_reauth_data(sm, sm->eap_method_priv)) { wpa_printf(MSG_DEBUG, "EAP: maintaining EAP method data for " "fast reauthentication"); sm->m->deinit_for_reauth(sm, sm->eap_method_priv); } else { eap_deinit_prev_method(sm, "INITIALIZE"); } sm->selectedMethod = EAP_TYPE_NONE; sm->methodState = METHOD_NONE; sm->allowNotifications = TRUE; sm->decision = DECISION_FAIL; eapol_set_int(sm, EAPOL_idleWhile, sm->ClientTimeout); eapol_set_bool(sm, EAPOL_eapSuccess, FALSE); eapol_set_bool(sm, EAPOL_eapFail, FALSE); os_free(sm->eapKeyData); sm->eapKeyData = NULL; sm->eapKeyAvailable = FALSE; eapol_set_bool(sm, EAPOL_eapRestart, FALSE); sm->lastId = -1; /* new session - make sure this does not match with * the first EAP-Packet */ /* * RFC 4137 does not reset eapResp and eapNoResp here. However, this * seemed to be able to trigger cases where both were set and if EAPOL * state machine uses eapNoResp first, it may end up not sending a real * reply correctly. This occurred when the workaround in FAIL state set * eapNoResp = TRUE.. Maybe that workaround needs to be fixed to do * something else(?) */ eapol_set_bool(sm, EAPOL_eapResp, FALSE); eapol_set_bool(sm, EAPOL_eapNoResp, FALSE); sm->num_rounds = 0;}/* * This state is reached whenever service from the lower layer is interrupted * or unavailable (portEnabled == FALSE). Immediate transition to INITIALIZE * occurs when the port becomes enabled. */SM_STATE(EAP, DISABLED){ SM_ENTRY(EAP, DISABLED); sm->num_rounds = 0;}/* * The state machine spends most of its time here, waiting for something to * happen. This state is entered unconditionally from INITIALIZE, DISCARD, and * SEND_RESPONSE states. */SM_STATE(EAP, IDLE){ SM_ENTRY(EAP, IDLE);}/* * This state is entered when an EAP packet is received (eapReq == TRUE) to * parse the packet header. */SM_STATE(EAP, RECEIVED){ const u8 *eapReqData; size_t eapReqDataLen; SM_ENTRY(EAP, RECEIVED); eapReqData = eapol_get_eapReqData(sm, &eapReqDataLen); /* parse rxReq, rxSuccess, rxFailure, reqId, reqMethod */ eap_sm_parseEapReq(sm, eapReqData, eapReqDataLen); sm->num_rounds++;}/* * This state is entered when a request for a new type comes in. Either the * correct method is started, or a Nak response is built. */SM_STATE(EAP, GET_METHOD){ int reinit; EapType method; SM_ENTRY(EAP, GET_METHOD); if (sm->reqMethod == EAP_TYPE_EXPANDED) method = sm->reqVendorMethod; else method = sm->reqMethod; if (!eap_sm_allowMethod(sm, sm->reqVendor, method)) { wpa_printf(MSG_DEBUG, "EAP: vendor %u method %u not allowed", sm->reqVendor, method); goto nak; } /* * RFC 4137 does not define specific operation for fast * re-authentication (session resumption). The design here is to allow * the previously used method data to be maintained for * re-authentication if the method support session resumption. * Otherwise, the previously used method data is freed and a new method * is allocated here. */ if (sm->fast_reauth && sm->m && sm->m->vendor == sm->reqVendor && sm->m->method == method && sm->m->has_reauth_data && sm->m->has_reauth_data(sm, sm->eap_method_priv)) { wpa_printf(MSG_DEBUG, "EAP: Using previous method data" " for fast re-authentication"); reinit = 1; } else { eap_deinit_prev_method(sm, "GET_METHOD"); reinit = 0; } sm->selectedMethod = sm->reqMethod; if (sm->m == NULL) sm->m = eap_sm_get_eap_methods(sm->reqVendor, method); if (!sm->m) { wpa_printf(MSG_DEBUG, "EAP: Could not find selected method: " "vendor %d method %d", sm->reqVendor, method); goto nak; } wpa_printf(MSG_DEBUG, "EAP: Initialize selected EAP method: " "vendor %u method %u (%s)", sm->reqVendor, method, sm->m->name); if (reinit) sm->eap_method_priv = sm->m->init_for_reauth( sm, sm->eap_method_priv); else sm->eap_method_priv = sm->m->init(sm); if (sm->eap_method_priv == NULL) { struct wpa_ssid *config = eap_get_config(sm); wpa_msg(sm->msg_ctx, MSG_INFO, "EAP: Failed to initialize EAP method: vendor %u " "method %u (%s)", sm->reqVendor, method, sm->m->name); sm->m = NULL; sm->methodState = METHOD_NONE; sm->selectedMethod = EAP_TYPE_NONE; if (sm->reqMethod == EAP_TYPE_TLS && config && (config->pending_req_pin || config->pending_req_passphrase)) { /* * Return without generating Nak in order to allow * entering of PIN code or passphrase to retry the * current EAP packet. */ wpa_printf(MSG_DEBUG, "EAP: Pending PIN/passphrase " "request - skip Nak"); return; } goto nak; } sm->methodState = METHOD_INIT; wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_METHOD "EAP vendor %u method %u (%s) selected", sm->reqVendor, method, sm->m->name); return;nak: os_free(sm->eapRespData); sm->eapRespData = NULL; sm->eapRespData = eap_sm_buildNak(sm, sm->reqId, &sm->eapRespDataLen);}/* * The method processing happens here. The request from the authenticator is * processed, and an appropriate response packet is built. */SM_STATE(EAP, METHOD){ u8 *eapReqData; size_t eapReqDataLen; struct eap_method_ret ret; SM_ENTRY(EAP, METHOD); if (sm->m == NULL) { wpa_printf(MSG_WARNING, "EAP::METHOD - method not selected"); return; } eapReqData = eapol_get_eapReqData(sm, &eapReqDataLen); /* * Get ignore, methodState, decision, allowNotifications, and * eapRespData. RFC 4137 uses three separate method procedure (check, * process, and buildResp) in this state. These have been combined into * a single function call to m->process() in order to optimize EAP * method implementation interface a bit. These procedures are only * used from within this METHOD state, so there is no need to keep * these as separate C functions. * * The RFC 4137 procedures return values as follows: * ignore = m.check(eapReqData) * (methodState, decision, allowNotifications) = m.process(eapReqData) * eapRespData = m.buildResp(reqId) */ os_memset(&ret, 0, sizeof(ret)); ret.ignore = sm->ignore; ret.methodState = sm->methodState; ret.decision = sm->decision; ret.allowNotifications = sm->allowNotifications; os_free(sm->eapRespData); sm->eapRespData = NULL; sm->eapRespData = sm->m->process(sm, sm->eap_method_priv, &ret, eapReqData, eapReqDataLen, &sm->eapRespDataLen); wpa_printf(MSG_DEBUG, "EAP: method process -> ignore=%s " "methodState=%s decision=%s", ret.ignore ? "TRUE" : "FALSE", eap_sm_method_state_txt(ret.methodState), eap_sm_decision_txt(ret.decision)); sm->ignore = ret.ignore; if (sm->ignore) return; sm->methodState = ret.methodState; sm->decision = ret.decision; sm->allowNotifications = ret.allowNotifications; if (sm->m->isKeyAvailable && sm->m->getKey && sm->m->isKeyAvailable(sm, sm->eap_method_priv)) { os_free(sm->eapKeyData); sm->eapKeyData = sm->m->getKey(sm, sm->eap_method_priv, &sm->eapKeyDataLen); }}/* * This state signals the lower layer that a response packet is ready to be * sent. */SM_STATE(EAP, SEND_RESPONSE){ SM_ENTRY(EAP, SEND_RESPONSE); os_free(sm->lastRespData); if (sm->eapRespData) { if (sm->workaround) os_memcpy(sm->last_md5, sm->req_md5, 16); sm->lastId = sm->reqId; sm->lastRespData = os_malloc(sm->eapRespDataLen); if (sm->lastRespData) { os_memcpy(sm->lastRespData, sm->eapRespData, sm->eapRespDataLen); sm->lastRespDataLen = sm->eapRespDataLen; } eapol_set_bool(sm, EAPOL_eapResp, TRUE); } else sm->lastRespData = NULL; eapol_set_bool(sm, EAPOL_eapReq, FALSE); eapol_set_int(sm, EAPOL_idleWhile, sm->ClientTimeout);}/* * This state signals the lower layer that the request was discarded, and no * response packet will be sent at this time. */SM_STATE(EAP, DISCARD){ SM_ENTRY(EAP, DISCARD); eapol_set_bool(sm, EAPOL_eapReq, FALSE); eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);}/* * Handles requests for Identity method and builds a response. */SM_STATE(EAP, IDENTITY){ const u8 *eapReqData; size_t eapReqDataLen; SM_ENTRY(EAP, IDENTITY); eapReqData = eapol_get_eapReqData(sm, &eapReqDataLen); eap_sm_processIdentity(sm, eapReqData); os_free(sm->eapRespData); sm->eapRespData = NULL; sm->eapRespData = eap_sm_buildIdentity(sm, sm->reqId, &sm->eapRespDataLen, 0);}/* * Handles requests for Notification method and builds a response. */SM_STATE(EAP, NOTIFICATION){ const u8 *eapReqData; size_t eapReqDataLen; SM_ENTRY(EAP, NOTIFICATION); eapReqData = eapol_get_eapReqData(sm, &eapReqDataLen); eap_sm_processNotify(sm, eapReqData); os_free(sm->eapRespData); sm->eapRespData = NULL; sm->eapRespData = eap_sm_buildNotify(sm->reqId, &sm->eapRespDataLen);}/* * This state retransmits the previous response packet. */SM_STATE(EAP, RETRANSMIT){ SM_ENTRY(EAP, RETRANSMIT); os_free(sm->eapRespData); if (sm->lastRespData) { sm->eapRespData = os_malloc(sm->lastRespDataLen); if (sm->eapRespData) { os_memcpy(sm->eapRespData, sm->lastRespData, sm->lastRespDataLen); sm->eapRespDataLen = sm->lastRespDataLen; } } else sm->eapRespData = NULL;}/* * This state is entered in case of a successful completion of authentication * and state machine waits here until port is disabled or EAP authentication is * restarted. */SM_STATE(EAP, SUCCESS){ SM_ENTRY(EAP, SUCCESS); if (sm->eapKeyData != NULL) sm->eapKeyAvailable = TRUE; eapol_set_bool(sm, EAPOL_eapSuccess, TRUE); /* * RFC 4137 does not clear eapReq here, but this seems to be required * to avoid processing the same request twice when state machine is * initialized. */ eapol_set_bool(sm, EAPOL_eapReq, FALSE); /* * RFC 4137 does not set eapNoResp here, but this seems to be required * to get EAPOL Supplicant backend state machine into SUCCESS state. In * addition, either eapResp or eapNoResp is required to be set after * processing the received EAP frame. */ eapol_set_bool(sm, EAPOL_eapNoResp, TRUE); wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS "EAP authentication completed successfully");}/* * This state is entered in case of a failure and state machine waits here * until port is disabled or EAP authentication is restarted. */SM_STATE(EAP, FAILURE){ SM_ENTRY(EAP, FAILURE); eapol_set_bool(sm, EAPOL_eapFail, TRUE); /* * RFC 4137 does not clear eapReq here, but this seems to be required * to avoid processing the same request twice when state machine is * initialized. */ eapol_set_bool(sm, EAPOL_eapReq, FALSE); /* * RFC 4137 does not set eapNoResp here. However, either eapResp or * eapNoResp is required to be set after processing the received EAP * frame. */ eapol_set_bool(sm, EAPOL_eapNoResp, TRUE); wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_FAILURE "EAP authentication failed");}static int eap_success_workaround(struct eap_sm *sm, int reqId, int lastId){ /* * At least Microsoft IAS and Meetinghouse Aegis seem to be sending * EAP-Success/Failure with lastId + 1 even though RFC 3748 and * RFC 4137 require that reqId == lastId. In addition, it looks like * Ringmaster v2.1.2.0 would be using lastId + 2 in EAP-Success. * * Accept this kind of Id if EAP workarounds are enabled. These are * unauthenticated plaintext messages, so this should have minimal * security implications (bit easier to fake EAP-Success/Failure). */ if (sm->workaround && (reqId == ((lastId + 1) & 0xff) || reqId == ((lastId + 2) & 0xff))) { wpa_printf(MSG_DEBUG, "EAP: Workaround for unexpected " "identifier field in EAP Success: " "reqId=%d lastId=%d (these are supposed to be " "same)", reqId, lastId); return 1; } wpa_printf(MSG_DEBUG, "EAP: EAP-Success Id mismatch - reqId=%d " "lastId=%d", reqId, lastId); return 0;}/* * RFC 4137 - Appendix A.1: EAP Peer State Machine - State transitions */SM_STEP(EAP){ int duplicate;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -