⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eap.c

📁 WPA在Linux下实现的原代码 WPA在Linux下实现的原代码
💻 C
📖 第 1 页 / 共 4 页
字号:
/* * WPA Supplicant / EAP state machines (RFC 4137) * Copyright (c) 2004-2005, 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 <stdlib.h>#include <stdio.h>#include <string.h>#include <ctype.h>#include "common.h"#include "eap_i.h"#include "wpa_supplicant.h"#include "config_ssid.h"#include "tls.h"#include "crypto.h"#include "pcsc_funcs.h"#include "wpa_ctrl.h"#define EAP_MAX_AUTH_ROUNDS 50#ifdef EAP_MD5extern const struct eap_method eap_method_md5;#endif#ifdef EAP_TLSextern const struct eap_method eap_method_tls;#endif#ifdef EAP_MSCHAPv2extern const struct eap_method eap_method_mschapv2;#endif#ifdef EAP_PEAPextern const struct eap_method eap_method_peap;#endif#ifdef EAP_TTLSextern const struct eap_method eap_method_ttls;#endif#ifdef EAP_GTCextern const struct eap_method eap_method_gtc;#endif#ifdef EAP_OTPextern const struct eap_method eap_method_otp;#endif#ifdef EAP_SIMextern const struct eap_method eap_method_sim;#endif#ifdef EAP_LEAPextern const struct eap_method eap_method_leap;#endif#ifdef EAP_PSKextern const struct eap_method eap_method_psk;#endif#ifdef EAP_AKAextern const struct eap_method eap_method_aka;#endif#ifdef EAP_FASTextern const struct eap_method eap_method_fast;#endif#ifdef EAP_PAXextern const struct eap_method eap_method_pax;#endifstatic const struct eap_method *eap_methods[] ={#ifdef EAP_MD5	&eap_method_md5,#endif#ifdef EAP_TLS	&eap_method_tls,#endif#ifdef EAP_MSCHAPv2	&eap_method_mschapv2,#endif#ifdef EAP_PEAP	&eap_method_peap,#endif#ifdef EAP_TTLS	&eap_method_ttls,#endif#ifdef EAP_GTC	&eap_method_gtc,#endif#ifdef EAP_OTP	&eap_method_otp,#endif#ifdef EAP_SIM	&eap_method_sim,#endif#ifdef EAP_LEAP	&eap_method_leap,#endif#ifdef EAP_PSK	&eap_method_psk,#endif#ifdef EAP_AKA	&eap_method_aka,#endif#ifdef EAP_FAST	&eap_method_fast,#endif#ifdef EAP_PAX	&eap_method_pax,#endif};#define NUM_EAP_METHODS (sizeof(eap_methods) / sizeof(eap_methods[0]))/** * eap_sm_get_eap_methods - Get EAP method based on type number * @method: EAP type number * Returns: Pointer to EAP method of %NULL if not found */const struct eap_method * eap_sm_get_eap_methods(int method){	int i;	for (i = 0; i < NUM_EAP_METHODS; i++) {		if (eap_methods[i]->method == method)			return eap_methods[i];	}	return NULL;}static Boolean eap_sm_allowMethod(struct eap_sm *sm, 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,				   size_t len);static void eap_sm_processNotify(struct eap_sm *sm, const u8 *req, size_t len);static u8 * eap_sm_buildNotify(struct eap_sm *sm, int id, size_t *len);static void eap_sm_parseEapReq(struct eap_sm *sm, const u8 *req, size_t len);static const char * eap_sm_method_state_txt(EapMethodState state);static const char * eap_sm_decision_txt(EapDecision decision);/* Definitions for clarifying state machine implementation */#define SM_STATE(machine, state) \static void sm_ ## machine ## _ ## state ## _Enter(struct eap_sm *sm, \	int global)#define SM_ENTRY(machine, state) \if (!global || sm->machine ## _state != machine ## _ ## state) { \	sm->changed = TRUE; \	wpa_printf(MSG_DEBUG, "EAP: " #machine " entering state " #state); \} \sm->machine ## _state = machine ## _ ## state;#define SM_ENTER(machine, state) \sm_ ## machine ## _ ## state ## _Enter(sm, 0)#define SM_ENTER_GLOBAL(machine, state) \sm_ ## machine ## _ ## state ## _Enter(sm, 1)#define SM_STEP(machine) \static void sm_ ## machine ## _Step(struct eap_sm *sm)#define SM_STEP_RUN(machine) sm_ ## machine ## _Step(sm)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);	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){	SM_ENTRY(EAP, GET_METHOD);	if (eap_sm_allowMethod(sm, sm->reqMethod)) {		int reinit = 0;		/*		 * 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->method == sm->reqMethod &&		    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");		sm->selectedMethod = sm->reqMethod;		if (sm->m == NULL)			sm->m = eap_sm_get_eap_methods(sm->selectedMethod);		if (sm->m) {			wpa_printf(MSG_DEBUG, "EAP: Initialize selected EAP "				   "method (%d, %s)",				   sm->selectedMethod, 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 "					"%d (%s)",					sm->selectedMethod, 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;				}			} else {				sm->methodState = METHOD_INIT;				wpa_msg(sm->msg_ctx, MSG_INFO,					WPA_EVENT_EAP_METHOD					"EAP method %d (%s) selected",					sm->selectedMethod, sm->m->name);				return;			}		}	}	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)	 */	memset(&ret, 0, sizeof(ret));	ret.ignore = sm->ignore;	ret.methodState = sm->methodState;	ret.decision = sm->decision;	ret.allowNotifications = sm->allowNotifications;	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)) {		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);	free(sm->lastRespData);	if (sm->eapRespData) {		if (sm->workaround)			memcpy(sm->last_md5, sm->req_md5, 16);		sm->lastId = sm->reqId;		sm->lastRespData = malloc(sm->eapRespDataLen);		if (sm->lastRespData) {			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);}/*

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -