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