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