xref: /freebsd/contrib/wpa/src/eap_server/eap_server.c (revision c1d255d3ffdbe447de3ab875bf4e7d7accc5bfc5)
1e28a4053SRui Paulo /*
2e28a4053SRui Paulo  * hostapd / EAP Full Authenticator state machine (RFC 4137)
35b9c547cSRui Paulo  * Copyright (c) 2004-2014, Jouni Malinen <j@w1.fi>
4e28a4053SRui Paulo  *
5f05cddf9SRui Paulo  * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo  * See README for more details.
7e28a4053SRui Paulo  *
8e28a4053SRui Paulo  * This state machine is based on the full authenticator state machine defined
9e28a4053SRui Paulo  * in RFC 4137. However, to support backend authentication in RADIUS
10e28a4053SRui Paulo  * authentication server functionality, parts of backend authenticator (also
11e28a4053SRui Paulo  * from RFC 4137) are mixed in. This functionality is enabled by setting
12*c1d255d3SCy Schubert  * backend_auth configuration variable to true.
13e28a4053SRui Paulo  */
14e28a4053SRui Paulo 
15e28a4053SRui Paulo #include "includes.h"
16e28a4053SRui Paulo 
17e28a4053SRui Paulo #include "common.h"
185b9c547cSRui Paulo #include "crypto/sha256.h"
19e28a4053SRui Paulo #include "eap_i.h"
20e28a4053SRui Paulo #include "state_machine.h"
21e28a4053SRui Paulo #include "common/wpa_ctrl.h"
22e28a4053SRui Paulo 
23e28a4053SRui Paulo #define STATE_MACHINE_DATA struct eap_sm
24e28a4053SRui Paulo #define STATE_MACHINE_DEBUG_PREFIX "EAP"
25e28a4053SRui Paulo 
26e28a4053SRui Paulo /* EAP state machines are described in RFC 4137 */
27e28a4053SRui Paulo 
28e28a4053SRui Paulo static int eap_sm_calculateTimeout(struct eap_sm *sm, int retransCount,
29e28a4053SRui Paulo 				   int eapSRTT, int eapRTTVAR,
30e28a4053SRui Paulo 				   int methodTimeout);
31e28a4053SRui Paulo static void eap_sm_parseEapResp(struct eap_sm *sm, const struct wpabuf *resp);
32e28a4053SRui Paulo static int eap_sm_getId(const struct wpabuf *data);
33e28a4053SRui Paulo static struct wpabuf * eap_sm_buildSuccess(struct eap_sm *sm, u8 id);
34e28a4053SRui Paulo static struct wpabuf * eap_sm_buildFailure(struct eap_sm *sm, u8 id);
35e28a4053SRui Paulo static int eap_sm_nextId(struct eap_sm *sm, int id);
36e28a4053SRui Paulo static void eap_sm_Policy_update(struct eap_sm *sm, const u8 *nak_list,
37e28a4053SRui Paulo 				 size_t len);
38*c1d255d3SCy Schubert static enum eap_type eap_sm_Policy_getNextMethod(struct eap_sm *sm,
39*c1d255d3SCy Schubert 						 int *vendor);
40e28a4053SRui Paulo static int eap_sm_Policy_getDecision(struct eap_sm *sm);
41*c1d255d3SCy Schubert static bool eap_sm_Policy_doPickUp(struct eap_sm *sm, enum eap_type method);
42e28a4053SRui Paulo 
43e28a4053SRui Paulo 
eap_get_erp_send_reauth_start(struct eap_sm * sm)445b9c547cSRui Paulo static int eap_get_erp_send_reauth_start(struct eap_sm *sm)
455b9c547cSRui Paulo {
465b9c547cSRui Paulo 	if (sm->eapol_cb->get_erp_send_reauth_start)
475b9c547cSRui Paulo 		return sm->eapol_cb->get_erp_send_reauth_start(sm->eapol_ctx);
485b9c547cSRui Paulo 	return 0;
495b9c547cSRui Paulo }
505b9c547cSRui Paulo 
515b9c547cSRui Paulo 
eap_get_erp_domain(struct eap_sm * sm)525b9c547cSRui Paulo static const char * eap_get_erp_domain(struct eap_sm *sm)
535b9c547cSRui Paulo {
545b9c547cSRui Paulo 	if (sm->eapol_cb->get_erp_domain)
555b9c547cSRui Paulo 		return sm->eapol_cb->get_erp_domain(sm->eapol_ctx);
565b9c547cSRui Paulo 	return NULL;
575b9c547cSRui Paulo }
585b9c547cSRui Paulo 
595b9c547cSRui Paulo 
605b9c547cSRui Paulo #ifdef CONFIG_ERP
615b9c547cSRui Paulo 
eap_erp_get_key(struct eap_sm * sm,const char * keyname)625b9c547cSRui Paulo static struct eap_server_erp_key * eap_erp_get_key(struct eap_sm *sm,
635b9c547cSRui Paulo 						   const char *keyname)
645b9c547cSRui Paulo {
655b9c547cSRui Paulo 	if (sm->eapol_cb->erp_get_key)
665b9c547cSRui Paulo 		return sm->eapol_cb->erp_get_key(sm->eapol_ctx, keyname);
675b9c547cSRui Paulo 	return NULL;
685b9c547cSRui Paulo }
695b9c547cSRui Paulo 
705b9c547cSRui Paulo 
eap_erp_add_key(struct eap_sm * sm,struct eap_server_erp_key * erp)715b9c547cSRui Paulo static int eap_erp_add_key(struct eap_sm *sm, struct eap_server_erp_key *erp)
725b9c547cSRui Paulo {
735b9c547cSRui Paulo 	if (sm->eapol_cb->erp_add_key)
745b9c547cSRui Paulo 		return sm->eapol_cb->erp_add_key(sm->eapol_ctx, erp);
755b9c547cSRui Paulo 	return -1;
765b9c547cSRui Paulo }
775b9c547cSRui Paulo 
785b9c547cSRui Paulo #endif /* CONFIG_ERP */
795b9c547cSRui Paulo 
805b9c547cSRui Paulo 
eap_sm_buildInitiateReauthStart(struct eap_sm * sm,u8 id)815b9c547cSRui Paulo static struct wpabuf * eap_sm_buildInitiateReauthStart(struct eap_sm *sm,
825b9c547cSRui Paulo 						       u8 id)
835b9c547cSRui Paulo {
845b9c547cSRui Paulo 	const char *domain;
855b9c547cSRui Paulo 	size_t plen = 1;
865b9c547cSRui Paulo 	struct wpabuf *msg;
875b9c547cSRui Paulo 	size_t domain_len = 0;
885b9c547cSRui Paulo 
895b9c547cSRui Paulo 	domain = eap_get_erp_domain(sm);
905b9c547cSRui Paulo 	if (domain) {
915b9c547cSRui Paulo 		domain_len = os_strlen(domain);
925b9c547cSRui Paulo 		plen += 2 + domain_len;
935b9c547cSRui Paulo 	}
945b9c547cSRui Paulo 
95325151a3SRui Paulo 	msg = eap_msg_alloc(EAP_VENDOR_IETF,
96*c1d255d3SCy Schubert 			    (enum eap_type) EAP_ERP_TYPE_REAUTH_START, plen,
975b9c547cSRui Paulo 			    EAP_CODE_INITIATE, id);
985b9c547cSRui Paulo 	if (msg == NULL)
995b9c547cSRui Paulo 		return NULL;
1005b9c547cSRui Paulo 	wpabuf_put_u8(msg, 0); /* Reserved */
1015b9c547cSRui Paulo 	if (domain) {
1025b9c547cSRui Paulo 		/* Domain name TLV */
1035b9c547cSRui Paulo 		wpabuf_put_u8(msg, EAP_ERP_TLV_DOMAIN_NAME);
1045b9c547cSRui Paulo 		wpabuf_put_u8(msg, domain_len);
1055b9c547cSRui Paulo 		wpabuf_put_data(msg, domain, domain_len);
1065b9c547cSRui Paulo 	}
1075b9c547cSRui Paulo 
1085b9c547cSRui Paulo 	return msg;
1095b9c547cSRui Paulo }
1105b9c547cSRui Paulo 
1115b9c547cSRui Paulo 
eap_copy_buf(struct wpabuf ** dst,const struct wpabuf * src)112e28a4053SRui Paulo static int eap_copy_buf(struct wpabuf **dst, const struct wpabuf *src)
113e28a4053SRui Paulo {
114e28a4053SRui Paulo 	if (src == NULL)
115e28a4053SRui Paulo 		return -1;
116e28a4053SRui Paulo 
117e28a4053SRui Paulo 	wpabuf_free(*dst);
118e28a4053SRui Paulo 	*dst = wpabuf_dup(src);
119e28a4053SRui Paulo 	return *dst ? 0 : -1;
120e28a4053SRui Paulo }
121e28a4053SRui Paulo 
122e28a4053SRui Paulo 
eap_copy_data(u8 ** dst,size_t * dst_len,const u8 * src,size_t src_len)123e28a4053SRui Paulo static int eap_copy_data(u8 **dst, size_t *dst_len,
124e28a4053SRui Paulo 			 const u8 *src, size_t src_len)
125e28a4053SRui Paulo {
126e28a4053SRui Paulo 	if (src == NULL)
127e28a4053SRui Paulo 		return -1;
128e28a4053SRui Paulo 
129e28a4053SRui Paulo 	os_free(*dst);
130e28a4053SRui Paulo 	*dst = os_malloc(src_len);
131e28a4053SRui Paulo 	if (*dst) {
132e28a4053SRui Paulo 		os_memcpy(*dst, src, src_len);
133e28a4053SRui Paulo 		*dst_len = src_len;
134e28a4053SRui Paulo 		return 0;
135e28a4053SRui Paulo 	} else {
136e28a4053SRui Paulo 		*dst_len = 0;
137e28a4053SRui Paulo 		return -1;
138e28a4053SRui Paulo 	}
139e28a4053SRui Paulo }
140e28a4053SRui Paulo 
141e28a4053SRui Paulo #define EAP_COPY(dst, src) \
142e28a4053SRui Paulo 	eap_copy_data((dst), (dst ## Len), (src), (src ## Len))
143e28a4053SRui Paulo 
144e28a4053SRui Paulo 
145e28a4053SRui Paulo /**
146e28a4053SRui Paulo  * eap_user_get - Fetch user information from the database
147e28a4053SRui Paulo  * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
148e28a4053SRui Paulo  * @identity: Identity (User-Name) of the user
149e28a4053SRui Paulo  * @identity_len: Length of identity in bytes
150e28a4053SRui Paulo  * @phase2: 0 = EAP phase1 user, 1 = EAP phase2 (tunneled) user
151e28a4053SRui Paulo  * Returns: 0 on success, or -1 on failure
152e28a4053SRui Paulo  *
153e28a4053SRui Paulo  * This function is used to fetch user information for EAP. The user will be
154e28a4053SRui Paulo  * selected based on the specified identity. sm->user and
155e28a4053SRui Paulo  * sm->user_eap_method_index are updated for the new user when a matching user
156e28a4053SRui Paulo  * is found. sm->user can be used to get user information (e.g., password).
157e28a4053SRui Paulo  */
eap_user_get(struct eap_sm * sm,const u8 * identity,size_t identity_len,int phase2)158e28a4053SRui Paulo int eap_user_get(struct eap_sm *sm, const u8 *identity, size_t identity_len,
159e28a4053SRui Paulo 		 int phase2)
160e28a4053SRui Paulo {
161e28a4053SRui Paulo 	struct eap_user *user;
162e28a4053SRui Paulo 
163e28a4053SRui Paulo 	if (sm == NULL || sm->eapol_cb == NULL ||
164e28a4053SRui Paulo 	    sm->eapol_cb->get_eap_user == NULL)
165e28a4053SRui Paulo 		return -1;
166e28a4053SRui Paulo 
167e28a4053SRui Paulo 	eap_user_free(sm->user);
168e28a4053SRui Paulo 	sm->user = NULL;
169e28a4053SRui Paulo 
170e28a4053SRui Paulo 	user = os_zalloc(sizeof(*user));
171e28a4053SRui Paulo 	if (user == NULL)
172e28a4053SRui Paulo 	    return -1;
173e28a4053SRui Paulo 
174e28a4053SRui Paulo 	if (sm->eapol_cb->get_eap_user(sm->eapol_ctx, identity,
175e28a4053SRui Paulo 				       identity_len, phase2, user) != 0) {
176e28a4053SRui Paulo 		eap_user_free(user);
177e28a4053SRui Paulo 		return -1;
178e28a4053SRui Paulo 	}
179e28a4053SRui Paulo 
180e28a4053SRui Paulo 	sm->user = user;
181e28a4053SRui Paulo 	sm->user_eap_method_index = 0;
182e28a4053SRui Paulo 
183e28a4053SRui Paulo 	return 0;
184e28a4053SRui Paulo }
185e28a4053SRui Paulo 
186e28a4053SRui Paulo 
eap_log_msg(struct eap_sm * sm,const char * fmt,...)1875b9c547cSRui Paulo void eap_log_msg(struct eap_sm *sm, const char *fmt, ...)
1885b9c547cSRui Paulo {
1895b9c547cSRui Paulo 	va_list ap;
1905b9c547cSRui Paulo 	char *buf;
1915b9c547cSRui Paulo 	int buflen;
1925b9c547cSRui Paulo 
1935b9c547cSRui Paulo 	if (sm == NULL || sm->eapol_cb == NULL || sm->eapol_cb->log_msg == NULL)
1945b9c547cSRui Paulo 		return;
1955b9c547cSRui Paulo 
1965b9c547cSRui Paulo 	va_start(ap, fmt);
1975b9c547cSRui Paulo 	buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
1985b9c547cSRui Paulo 	va_end(ap);
1995b9c547cSRui Paulo 
2005b9c547cSRui Paulo 	buf = os_malloc(buflen);
2015b9c547cSRui Paulo 	if (buf == NULL)
2025b9c547cSRui Paulo 		return;
2035b9c547cSRui Paulo 	va_start(ap, fmt);
2045b9c547cSRui Paulo 	vsnprintf(buf, buflen, fmt, ap);
2055b9c547cSRui Paulo 	va_end(ap);
2065b9c547cSRui Paulo 
2075b9c547cSRui Paulo 	sm->eapol_cb->log_msg(sm->eapol_ctx, buf);
2085b9c547cSRui Paulo 
2095b9c547cSRui Paulo 	os_free(buf);
2105b9c547cSRui Paulo }
2115b9c547cSRui Paulo 
2125b9c547cSRui Paulo 
SM_STATE(EAP,DISABLED)213e28a4053SRui Paulo SM_STATE(EAP, DISABLED)
214e28a4053SRui Paulo {
215e28a4053SRui Paulo 	SM_ENTRY(EAP, DISABLED);
216e28a4053SRui Paulo 	sm->num_rounds = 0;
217*c1d255d3SCy Schubert 	sm->num_rounds_short = 0;
218e28a4053SRui Paulo }
219e28a4053SRui Paulo 
220e28a4053SRui Paulo 
SM_STATE(EAP,INITIALIZE)221e28a4053SRui Paulo SM_STATE(EAP, INITIALIZE)
222e28a4053SRui Paulo {
223e28a4053SRui Paulo 	SM_ENTRY(EAP, INITIALIZE);
224e28a4053SRui Paulo 
225*c1d255d3SCy Schubert 	if (sm->eap_if.eapRestart && !sm->cfg->eap_server && sm->identity) {
226f05cddf9SRui Paulo 		/*
227f05cddf9SRui Paulo 		 * Need to allow internal Identity method to be used instead
228f05cddf9SRui Paulo 		 * of passthrough at the beginning of reauthentication.
229f05cddf9SRui Paulo 		 */
230f05cddf9SRui Paulo 		eap_server_clear_identity(sm);
231f05cddf9SRui Paulo 	}
232f05cddf9SRui Paulo 
233*c1d255d3SCy Schubert 	sm->try_initiate_reauth = false;
234e28a4053SRui Paulo 	sm->currentId = -1;
235*c1d255d3SCy Schubert 	sm->eap_if.eapSuccess = false;
236*c1d255d3SCy Schubert 	sm->eap_if.eapFail = false;
237*c1d255d3SCy Schubert 	sm->eap_if.eapTimeout = false;
2385b9c547cSRui Paulo 	bin_clear_free(sm->eap_if.eapKeyData, sm->eap_if.eapKeyDataLen);
239e28a4053SRui Paulo 	sm->eap_if.eapKeyData = NULL;
240e28a4053SRui Paulo 	sm->eap_if.eapKeyDataLen = 0;
2415b9c547cSRui Paulo 	os_free(sm->eap_if.eapSessionId);
2425b9c547cSRui Paulo 	sm->eap_if.eapSessionId = NULL;
2435b9c547cSRui Paulo 	sm->eap_if.eapSessionIdLen = 0;
244*c1d255d3SCy Schubert 	sm->eap_if.eapKeyAvailable = false;
245*c1d255d3SCy Schubert 	sm->eap_if.eapRestart = false;
246e28a4053SRui Paulo 
247e28a4053SRui Paulo 	/*
248e28a4053SRui Paulo 	 * This is not defined in RFC 4137, but method state needs to be
249e28a4053SRui Paulo 	 * reseted here so that it does not remain in success state when
250e28a4053SRui Paulo 	 * re-authentication starts.
251e28a4053SRui Paulo 	 */
252e28a4053SRui Paulo 	if (sm->m && sm->eap_method_priv) {
253e28a4053SRui Paulo 		sm->m->reset(sm, sm->eap_method_priv);
254e28a4053SRui Paulo 		sm->eap_method_priv = NULL;
255e28a4053SRui Paulo 	}
256e28a4053SRui Paulo 	sm->m = NULL;
257e28a4053SRui Paulo 	sm->user_eap_method_index = 0;
258e28a4053SRui Paulo 
259*c1d255d3SCy Schubert 	if (sm->cfg->backend_auth) {
260e28a4053SRui Paulo 		sm->currentMethod = EAP_TYPE_NONE;
261e28a4053SRui Paulo 		/* parse rxResp, respId, respMethod */
262e28a4053SRui Paulo 		eap_sm_parseEapResp(sm, sm->eap_if.eapRespData);
263e28a4053SRui Paulo 		if (sm->rxResp) {
264e28a4053SRui Paulo 			sm->currentId = sm->respId;
265e28a4053SRui Paulo 		}
266e28a4053SRui Paulo 	}
267e28a4053SRui Paulo 	sm->num_rounds = 0;
268*c1d255d3SCy Schubert 	sm->num_rounds_short = 0;
269e28a4053SRui Paulo 	sm->method_pending = METHOD_PENDING_NONE;
270e28a4053SRui Paulo 
271*c1d255d3SCy Schubert 	wpa_msg(sm->cfg->msg_ctx, MSG_INFO, WPA_EVENT_EAP_STARTED
272e28a4053SRui Paulo 		MACSTR, MAC2STR(sm->peer_addr));
273e28a4053SRui Paulo }
274e28a4053SRui Paulo 
275e28a4053SRui Paulo 
SM_STATE(EAP,PICK_UP_METHOD)276e28a4053SRui Paulo SM_STATE(EAP, PICK_UP_METHOD)
277e28a4053SRui Paulo {
278e28a4053SRui Paulo 	SM_ENTRY(EAP, PICK_UP_METHOD);
279e28a4053SRui Paulo 
280e28a4053SRui Paulo 	if (eap_sm_Policy_doPickUp(sm, sm->respMethod)) {
281e28a4053SRui Paulo 		sm->currentMethod = sm->respMethod;
282e28a4053SRui Paulo 		if (sm->m && sm->eap_method_priv) {
283e28a4053SRui Paulo 			sm->m->reset(sm, sm->eap_method_priv);
284e28a4053SRui Paulo 			sm->eap_method_priv = NULL;
285e28a4053SRui Paulo 		}
286e28a4053SRui Paulo 		sm->m = eap_server_get_eap_method(EAP_VENDOR_IETF,
287e28a4053SRui Paulo 						  sm->currentMethod);
288e28a4053SRui Paulo 		if (sm->m && sm->m->initPickUp) {
289e28a4053SRui Paulo 			sm->eap_method_priv = sm->m->initPickUp(sm);
290e28a4053SRui Paulo 			if (sm->eap_method_priv == NULL) {
291e28a4053SRui Paulo 				wpa_printf(MSG_DEBUG, "EAP: Failed to "
292e28a4053SRui Paulo 					   "initialize EAP method %d",
293e28a4053SRui Paulo 					   sm->currentMethod);
294e28a4053SRui Paulo 				sm->m = NULL;
295e28a4053SRui Paulo 				sm->currentMethod = EAP_TYPE_NONE;
296e28a4053SRui Paulo 			}
297e28a4053SRui Paulo 		} else {
298e28a4053SRui Paulo 			sm->m = NULL;
299e28a4053SRui Paulo 			sm->currentMethod = EAP_TYPE_NONE;
300e28a4053SRui Paulo 		}
301e28a4053SRui Paulo 	}
302e28a4053SRui Paulo 
303*c1d255d3SCy Schubert 	wpa_msg(sm->cfg->msg_ctx, MSG_INFO, WPA_EVENT_EAP_PROPOSED_METHOD
304e28a4053SRui Paulo 		"method=%u", sm->currentMethod);
305e28a4053SRui Paulo }
306e28a4053SRui Paulo 
307e28a4053SRui Paulo 
SM_STATE(EAP,IDLE)308e28a4053SRui Paulo SM_STATE(EAP, IDLE)
309e28a4053SRui Paulo {
310e28a4053SRui Paulo 	SM_ENTRY(EAP, IDLE);
311e28a4053SRui Paulo 
312e28a4053SRui Paulo 	sm->eap_if.retransWhile = eap_sm_calculateTimeout(
313e28a4053SRui Paulo 		sm, sm->retransCount, sm->eap_if.eapSRTT, sm->eap_if.eapRTTVAR,
314e28a4053SRui Paulo 		sm->methodTimeout);
315e28a4053SRui Paulo }
316e28a4053SRui Paulo 
317e28a4053SRui Paulo 
SM_STATE(EAP,RETRANSMIT)318e28a4053SRui Paulo SM_STATE(EAP, RETRANSMIT)
319e28a4053SRui Paulo {
320e28a4053SRui Paulo 	SM_ENTRY(EAP, RETRANSMIT);
321e28a4053SRui Paulo 
322e28a4053SRui Paulo 	sm->retransCount++;
323e28a4053SRui Paulo 	if (sm->retransCount <= sm->MaxRetrans && sm->lastReqData) {
324e28a4053SRui Paulo 		if (eap_copy_buf(&sm->eap_if.eapReqData, sm->lastReqData) == 0)
325*c1d255d3SCy Schubert 			sm->eap_if.eapReq = true;
326e28a4053SRui Paulo 	}
32785732ac8SCy Schubert 
328*c1d255d3SCy Schubert 	wpa_msg(sm->cfg->msg_ctx, MSG_INFO, WPA_EVENT_EAP_RETRANSMIT MACSTR,
32985732ac8SCy Schubert 		MAC2STR(sm->peer_addr));
330e28a4053SRui Paulo }
331e28a4053SRui Paulo 
332e28a4053SRui Paulo 
SM_STATE(EAP,RECEIVED)333e28a4053SRui Paulo SM_STATE(EAP, RECEIVED)
334e28a4053SRui Paulo {
335e28a4053SRui Paulo 	SM_ENTRY(EAP, RECEIVED);
336e28a4053SRui Paulo 
337e28a4053SRui Paulo 	/* parse rxResp, respId, respMethod */
338e28a4053SRui Paulo 	eap_sm_parseEapResp(sm, sm->eap_if.eapRespData);
339e28a4053SRui Paulo 	sm->num_rounds++;
340*c1d255d3SCy Schubert 	if (!sm->eap_if.eapRespData || wpabuf_len(sm->eap_if.eapRespData) < 20)
341*c1d255d3SCy Schubert 		sm->num_rounds_short++;
342*c1d255d3SCy Schubert 	else
343*c1d255d3SCy Schubert 		sm->num_rounds_short = 0;
344e28a4053SRui Paulo }
345e28a4053SRui Paulo 
346e28a4053SRui Paulo 
SM_STATE(EAP,DISCARD)347e28a4053SRui Paulo SM_STATE(EAP, DISCARD)
348e28a4053SRui Paulo {
349e28a4053SRui Paulo 	SM_ENTRY(EAP, DISCARD);
350*c1d255d3SCy Schubert 	sm->eap_if.eapResp = false;
351*c1d255d3SCy Schubert 	sm->eap_if.eapNoReq = true;
352e28a4053SRui Paulo }
353e28a4053SRui Paulo 
354e28a4053SRui Paulo 
SM_STATE(EAP,SEND_REQUEST)355e28a4053SRui Paulo SM_STATE(EAP, SEND_REQUEST)
356e28a4053SRui Paulo {
357e28a4053SRui Paulo 	SM_ENTRY(EAP, SEND_REQUEST);
358e28a4053SRui Paulo 
359e28a4053SRui Paulo 	sm->retransCount = 0;
360e28a4053SRui Paulo 	if (sm->eap_if.eapReqData) {
361*c1d255d3SCy Schubert 		if (wpabuf_len(sm->eap_if.eapReqData) >= 20)
362*c1d255d3SCy Schubert 			sm->num_rounds_short = 0;
363e28a4053SRui Paulo 		if (eap_copy_buf(&sm->lastReqData, sm->eap_if.eapReqData) == 0)
364e28a4053SRui Paulo 		{
365*c1d255d3SCy Schubert 			sm->eap_if.eapResp = false;
366*c1d255d3SCy Schubert 			sm->eap_if.eapReq = true;
367e28a4053SRui Paulo 		} else {
368*c1d255d3SCy Schubert 			sm->eap_if.eapResp = false;
369*c1d255d3SCy Schubert 			sm->eap_if.eapReq = false;
370e28a4053SRui Paulo 		}
371e28a4053SRui Paulo 	} else {
372e28a4053SRui Paulo 		wpa_printf(MSG_INFO, "EAP: SEND_REQUEST - no eapReqData");
373*c1d255d3SCy Schubert 		sm->eap_if.eapResp = false;
374*c1d255d3SCy Schubert 		sm->eap_if.eapReq = false;
375*c1d255d3SCy Schubert 		sm->eap_if.eapNoReq = true;
376e28a4053SRui Paulo 	}
377e28a4053SRui Paulo }
378e28a4053SRui Paulo 
379e28a4053SRui Paulo 
SM_STATE(EAP,INTEGRITY_CHECK)380e28a4053SRui Paulo SM_STATE(EAP, INTEGRITY_CHECK)
381e28a4053SRui Paulo {
382e28a4053SRui Paulo 	SM_ENTRY(EAP, INTEGRITY_CHECK);
383e28a4053SRui Paulo 
384f05cddf9SRui Paulo 	if (!eap_hdr_len_valid(sm->eap_if.eapRespData, 1)) {
385*c1d255d3SCy Schubert 		sm->ignore = true;
386f05cddf9SRui Paulo 		return;
387f05cddf9SRui Paulo 	}
388f05cddf9SRui Paulo 
389e28a4053SRui Paulo 	if (sm->m->check) {
390e28a4053SRui Paulo 		sm->ignore = sm->m->check(sm, sm->eap_method_priv,
391e28a4053SRui Paulo 					  sm->eap_if.eapRespData);
392e28a4053SRui Paulo 	}
393e28a4053SRui Paulo }
394e28a4053SRui Paulo 
395e28a4053SRui Paulo 
SM_STATE(EAP,METHOD_REQUEST)396e28a4053SRui Paulo SM_STATE(EAP, METHOD_REQUEST)
397e28a4053SRui Paulo {
398e28a4053SRui Paulo 	SM_ENTRY(EAP, METHOD_REQUEST);
399e28a4053SRui Paulo 
400e28a4053SRui Paulo 	if (sm->m == NULL) {
401e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP: method not initialized");
402e28a4053SRui Paulo 		return;
403e28a4053SRui Paulo 	}
404e28a4053SRui Paulo 
405e28a4053SRui Paulo 	sm->currentId = eap_sm_nextId(sm, sm->currentId);
406e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "EAP: building EAP-Request: Identifier %d",
407e28a4053SRui Paulo 		   sm->currentId);
408e28a4053SRui Paulo 	sm->lastId = sm->currentId;
409e28a4053SRui Paulo 	wpabuf_free(sm->eap_if.eapReqData);
410e28a4053SRui Paulo 	sm->eap_if.eapReqData = sm->m->buildReq(sm, sm->eap_method_priv,
411e28a4053SRui Paulo 						sm->currentId);
412e28a4053SRui Paulo 	if (sm->m->getTimeout)
413e28a4053SRui Paulo 		sm->methodTimeout = sm->m->getTimeout(sm, sm->eap_method_priv);
414e28a4053SRui Paulo 	else
415e28a4053SRui Paulo 		sm->methodTimeout = 0;
416e28a4053SRui Paulo }
417e28a4053SRui Paulo 
418e28a4053SRui Paulo 
eap_server_erp_init(struct eap_sm * sm)4195b9c547cSRui Paulo static void eap_server_erp_init(struct eap_sm *sm)
4205b9c547cSRui Paulo {
4215b9c547cSRui Paulo #ifdef CONFIG_ERP
4225b9c547cSRui Paulo 	u8 *emsk = NULL;
4235b9c547cSRui Paulo 	size_t emsk_len = 0;
4245b9c547cSRui Paulo 	u8 EMSKname[EAP_EMSK_NAME_LEN];
42585732ac8SCy Schubert 	u8 len[2], ctx[3];
4265b9c547cSRui Paulo 	const char *domain;
4275b9c547cSRui Paulo 	size_t domain_len, nai_buf_len;
4285b9c547cSRui Paulo 	struct eap_server_erp_key *erp = NULL;
4295b9c547cSRui Paulo 	int pos;
4305b9c547cSRui Paulo 
4315b9c547cSRui Paulo 	domain = eap_get_erp_domain(sm);
4325b9c547cSRui Paulo 	if (!domain)
4335b9c547cSRui Paulo 		return;
4345b9c547cSRui Paulo 
4355b9c547cSRui Paulo 	domain_len = os_strlen(domain);
4365b9c547cSRui Paulo 
4375b9c547cSRui Paulo 	nai_buf_len = 2 * EAP_EMSK_NAME_LEN + 1 + domain_len;
4385b9c547cSRui Paulo 	if (nai_buf_len > 253) {
4395b9c547cSRui Paulo 		/*
4405b9c547cSRui Paulo 		 * keyName-NAI has a maximum length of 253 octet to fit in
4415b9c547cSRui Paulo 		 * RADIUS attributes.
4425b9c547cSRui Paulo 		 */
4435b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
4445b9c547cSRui Paulo 			   "EAP: Too long realm for ERP keyName-NAI maximum length");
4455b9c547cSRui Paulo 		return;
4465b9c547cSRui Paulo 	}
4475b9c547cSRui Paulo 	nai_buf_len++; /* null termination */
4485b9c547cSRui Paulo 	erp = os_zalloc(sizeof(*erp) + nai_buf_len);
4495b9c547cSRui Paulo 	if (erp == NULL)
4505b9c547cSRui Paulo 		goto fail;
4515b9c547cSRui Paulo 	erp->recv_seq = (u32) -1;
4525b9c547cSRui Paulo 
4535b9c547cSRui Paulo 	emsk = sm->m->get_emsk(sm, sm->eap_method_priv, &emsk_len);
4545b9c547cSRui Paulo 	if (!emsk || emsk_len == 0 || emsk_len > ERP_MAX_KEY_LEN) {
4555b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
4565b9c547cSRui Paulo 			   "EAP: No suitable EMSK available for ERP");
4575b9c547cSRui Paulo 		goto fail;
4585b9c547cSRui Paulo 	}
4595b9c547cSRui Paulo 
4605b9c547cSRui Paulo 	wpa_hexdump_key(MSG_DEBUG, "EAP: EMSK", emsk, emsk_len);
4615b9c547cSRui Paulo 
46285732ac8SCy Schubert 	WPA_PUT_BE16(len, EAP_EMSK_NAME_LEN);
4635b9c547cSRui Paulo 	if (hmac_sha256_kdf(sm->eap_if.eapSessionId, sm->eap_if.eapSessionIdLen,
4645b9c547cSRui Paulo 			    "EMSK", len, sizeof(len),
4655b9c547cSRui Paulo 			    EMSKname, EAP_EMSK_NAME_LEN) < 0) {
4665b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "EAP: Could not derive EMSKname");
4675b9c547cSRui Paulo 		goto fail;
4685b9c547cSRui Paulo 	}
4695b9c547cSRui Paulo 	wpa_hexdump(MSG_DEBUG, "EAP: EMSKname", EMSKname, EAP_EMSK_NAME_LEN);
4705b9c547cSRui Paulo 
4715b9c547cSRui Paulo 	pos = wpa_snprintf_hex(erp->keyname_nai, nai_buf_len,
4725b9c547cSRui Paulo 			       EMSKname, EAP_EMSK_NAME_LEN);
4735b9c547cSRui Paulo 	erp->keyname_nai[pos] = '@';
4745b9c547cSRui Paulo 	os_memcpy(&erp->keyname_nai[pos + 1], domain, domain_len);
4755b9c547cSRui Paulo 
4765b9c547cSRui Paulo 	WPA_PUT_BE16(len, emsk_len);
4775b9c547cSRui Paulo 	if (hmac_sha256_kdf(emsk, emsk_len,
4785b9c547cSRui Paulo 			    "EAP Re-authentication Root Key@ietf.org",
4795b9c547cSRui Paulo 			    len, sizeof(len), erp->rRK, emsk_len) < 0) {
4805b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "EAP: Could not derive rRK for ERP");
4815b9c547cSRui Paulo 		goto fail;
4825b9c547cSRui Paulo 	}
4835b9c547cSRui Paulo 	erp->rRK_len = emsk_len;
4845b9c547cSRui Paulo 	wpa_hexdump_key(MSG_DEBUG, "EAP: ERP rRK", erp->rRK, erp->rRK_len);
4855b9c547cSRui Paulo 
48685732ac8SCy Schubert 	ctx[0] = EAP_ERP_CS_HMAC_SHA256_128;
48785732ac8SCy Schubert 	WPA_PUT_BE16(&ctx[1], erp->rRK_len);
4885b9c547cSRui Paulo 	if (hmac_sha256_kdf(erp->rRK, erp->rRK_len,
48985732ac8SCy Schubert 			    "Re-authentication Integrity Key@ietf.org",
49085732ac8SCy Schubert 			    ctx, sizeof(ctx), erp->rIK, erp->rRK_len) < 0) {
4915b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "EAP: Could not derive rIK for ERP");
4925b9c547cSRui Paulo 		goto fail;
4935b9c547cSRui Paulo 	}
4945b9c547cSRui Paulo 	erp->rIK_len = erp->rRK_len;
4955b9c547cSRui Paulo 	wpa_hexdump_key(MSG_DEBUG, "EAP: ERP rIK", erp->rIK, erp->rIK_len);
4965b9c547cSRui Paulo 
4975b9c547cSRui Paulo 	if (eap_erp_add_key(sm, erp) == 0) {
4985b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "EAP: Stored ERP keys %s",
4995b9c547cSRui Paulo 			   erp->keyname_nai);
5005b9c547cSRui Paulo 		erp = NULL;
5015b9c547cSRui Paulo 	}
5025b9c547cSRui Paulo 
5035b9c547cSRui Paulo fail:
5045b9c547cSRui Paulo 	bin_clear_free(emsk, emsk_len);
5055b9c547cSRui Paulo 	bin_clear_free(erp, sizeof(*erp));
5065b9c547cSRui Paulo #endif /* CONFIG_ERP */
5075b9c547cSRui Paulo }
5085b9c547cSRui Paulo 
5095b9c547cSRui Paulo 
SM_STATE(EAP,METHOD_RESPONSE)510e28a4053SRui Paulo SM_STATE(EAP, METHOD_RESPONSE)
511e28a4053SRui Paulo {
512e28a4053SRui Paulo 	SM_ENTRY(EAP, METHOD_RESPONSE);
513e28a4053SRui Paulo 
514f05cddf9SRui Paulo 	if (!eap_hdr_len_valid(sm->eap_if.eapRespData, 1))
515f05cddf9SRui Paulo 		return;
516f05cddf9SRui Paulo 
517e28a4053SRui Paulo 	sm->m->process(sm, sm->eap_method_priv, sm->eap_if.eapRespData);
518e28a4053SRui Paulo 	if (sm->m->isDone(sm, sm->eap_method_priv)) {
519e28a4053SRui Paulo 		eap_sm_Policy_update(sm, NULL, 0);
5205b9c547cSRui Paulo 		bin_clear_free(sm->eap_if.eapKeyData, sm->eap_if.eapKeyDataLen);
521e28a4053SRui Paulo 		if (sm->m->getKey) {
522e28a4053SRui Paulo 			sm->eap_if.eapKeyData = sm->m->getKey(
523e28a4053SRui Paulo 				sm, sm->eap_method_priv,
524e28a4053SRui Paulo 				&sm->eap_if.eapKeyDataLen);
525e28a4053SRui Paulo 		} else {
526e28a4053SRui Paulo 			sm->eap_if.eapKeyData = NULL;
527e28a4053SRui Paulo 			sm->eap_if.eapKeyDataLen = 0;
528e28a4053SRui Paulo 		}
5295b9c547cSRui Paulo 		os_free(sm->eap_if.eapSessionId);
5305b9c547cSRui Paulo 		sm->eap_if.eapSessionId = NULL;
5315b9c547cSRui Paulo 		if (sm->m->getSessionId) {
5325b9c547cSRui Paulo 			sm->eap_if.eapSessionId = sm->m->getSessionId(
5335b9c547cSRui Paulo 				sm, sm->eap_method_priv,
5345b9c547cSRui Paulo 				&sm->eap_if.eapSessionIdLen);
5355b9c547cSRui Paulo 			wpa_hexdump(MSG_DEBUG, "EAP: Session-Id",
5365b9c547cSRui Paulo 				    sm->eap_if.eapSessionId,
5375b9c547cSRui Paulo 				    sm->eap_if.eapSessionIdLen);
5385b9c547cSRui Paulo 		}
539*c1d255d3SCy Schubert 		if (sm->cfg->erp && sm->m->get_emsk && sm->eap_if.eapSessionId)
5405b9c547cSRui Paulo 			eap_server_erp_init(sm);
541e28a4053SRui Paulo 		sm->methodState = METHOD_END;
542e28a4053SRui Paulo 	} else {
543e28a4053SRui Paulo 		sm->methodState = METHOD_CONTINUE;
544e28a4053SRui Paulo 	}
545e28a4053SRui Paulo }
546e28a4053SRui Paulo 
547e28a4053SRui Paulo 
SM_STATE(EAP,PROPOSE_METHOD)548e28a4053SRui Paulo SM_STATE(EAP, PROPOSE_METHOD)
549e28a4053SRui Paulo {
550e28a4053SRui Paulo 	int vendor;
551*c1d255d3SCy Schubert 	enum eap_type type;
552e28a4053SRui Paulo 
553e28a4053SRui Paulo 	SM_ENTRY(EAP, PROPOSE_METHOD);
554e28a4053SRui Paulo 
555*c1d255d3SCy Schubert 	sm->try_initiate_reauth = false;
5565b9c547cSRui Paulo try_another_method:
557e28a4053SRui Paulo 	type = eap_sm_Policy_getNextMethod(sm, &vendor);
558e28a4053SRui Paulo 	if (vendor == EAP_VENDOR_IETF)
559e28a4053SRui Paulo 		sm->currentMethod = type;
560e28a4053SRui Paulo 	else
561e28a4053SRui Paulo 		sm->currentMethod = EAP_TYPE_EXPANDED;
562e28a4053SRui Paulo 	if (sm->m && sm->eap_method_priv) {
563e28a4053SRui Paulo 		sm->m->reset(sm, sm->eap_method_priv);
564e28a4053SRui Paulo 		sm->eap_method_priv = NULL;
565e28a4053SRui Paulo 	}
566e28a4053SRui Paulo 	sm->m = eap_server_get_eap_method(vendor, type);
567e28a4053SRui Paulo 	if (sm->m) {
568e28a4053SRui Paulo 		sm->eap_method_priv = sm->m->init(sm);
569e28a4053SRui Paulo 		if (sm->eap_method_priv == NULL) {
570e28a4053SRui Paulo 			wpa_printf(MSG_DEBUG, "EAP: Failed to initialize EAP "
571e28a4053SRui Paulo 				   "method %d", sm->currentMethod);
572e28a4053SRui Paulo 			sm->m = NULL;
573e28a4053SRui Paulo 			sm->currentMethod = EAP_TYPE_NONE;
5745b9c547cSRui Paulo 			goto try_another_method;
575e28a4053SRui Paulo 		}
576e28a4053SRui Paulo 	}
5775b9c547cSRui Paulo 	if (sm->m == NULL) {
5785b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "EAP: Could not find suitable EAP method");
5795b9c547cSRui Paulo 		eap_log_msg(sm, "Could not find suitable EAP method");
5805b9c547cSRui Paulo 		sm->decision = DECISION_FAILURE;
5815b9c547cSRui Paulo 		return;
5825b9c547cSRui Paulo 	}
583e28a4053SRui Paulo 	if (sm->currentMethod == EAP_TYPE_IDENTITY ||
584e28a4053SRui Paulo 	    sm->currentMethod == EAP_TYPE_NOTIFICATION)
585e28a4053SRui Paulo 		sm->methodState = METHOD_CONTINUE;
586e28a4053SRui Paulo 	else
587e28a4053SRui Paulo 		sm->methodState = METHOD_PROPOSED;
588e28a4053SRui Paulo 
589*c1d255d3SCy Schubert 	wpa_msg(sm->cfg->msg_ctx, MSG_INFO, WPA_EVENT_EAP_PROPOSED_METHOD
590e28a4053SRui Paulo 		"vendor=%u method=%u", vendor, sm->currentMethod);
5915b9c547cSRui Paulo 	eap_log_msg(sm, "Propose EAP method vendor=%u method=%u",
5925b9c547cSRui Paulo 		    vendor, sm->currentMethod);
593e28a4053SRui Paulo }
594e28a4053SRui Paulo 
595e28a4053SRui Paulo 
SM_STATE(EAP,NAK)596e28a4053SRui Paulo SM_STATE(EAP, NAK)
597e28a4053SRui Paulo {
598e28a4053SRui Paulo 	const struct eap_hdr *nak;
599e28a4053SRui Paulo 	size_t len = 0;
600e28a4053SRui Paulo 	const u8 *pos;
601e28a4053SRui Paulo 	const u8 *nak_list = NULL;
602e28a4053SRui Paulo 
603e28a4053SRui Paulo 	SM_ENTRY(EAP, NAK);
604e28a4053SRui Paulo 
605e28a4053SRui Paulo 	if (sm->eap_method_priv) {
606e28a4053SRui Paulo 		sm->m->reset(sm, sm->eap_method_priv);
607e28a4053SRui Paulo 		sm->eap_method_priv = NULL;
608e28a4053SRui Paulo 	}
609e28a4053SRui Paulo 	sm->m = NULL;
610e28a4053SRui Paulo 
611f05cddf9SRui Paulo 	if (!eap_hdr_len_valid(sm->eap_if.eapRespData, 1))
612f05cddf9SRui Paulo 		return;
613f05cddf9SRui Paulo 
614e28a4053SRui Paulo 	nak = wpabuf_head(sm->eap_if.eapRespData);
615e28a4053SRui Paulo 	if (nak && wpabuf_len(sm->eap_if.eapRespData) > sizeof(*nak)) {
616e28a4053SRui Paulo 		len = be_to_host16(nak->length);
617e28a4053SRui Paulo 		if (len > wpabuf_len(sm->eap_if.eapRespData))
618e28a4053SRui Paulo 			len = wpabuf_len(sm->eap_if.eapRespData);
619e28a4053SRui Paulo 		pos = (const u8 *) (nak + 1);
620e28a4053SRui Paulo 		len -= sizeof(*nak);
621e28a4053SRui Paulo 		if (*pos == EAP_TYPE_NAK) {
622e28a4053SRui Paulo 			pos++;
623e28a4053SRui Paulo 			len--;
624e28a4053SRui Paulo 			nak_list = pos;
625e28a4053SRui Paulo 		}
626e28a4053SRui Paulo 	}
627e28a4053SRui Paulo 	eap_sm_Policy_update(sm, nak_list, len);
628e28a4053SRui Paulo }
629e28a4053SRui Paulo 
630e28a4053SRui Paulo 
SM_STATE(EAP,SELECT_ACTION)631e28a4053SRui Paulo SM_STATE(EAP, SELECT_ACTION)
632e28a4053SRui Paulo {
633e28a4053SRui Paulo 	SM_ENTRY(EAP, SELECT_ACTION);
634e28a4053SRui Paulo 
635e28a4053SRui Paulo 	sm->decision = eap_sm_Policy_getDecision(sm);
636e28a4053SRui Paulo }
637e28a4053SRui Paulo 
638e28a4053SRui Paulo 
SM_STATE(EAP,TIMEOUT_FAILURE)639e28a4053SRui Paulo SM_STATE(EAP, TIMEOUT_FAILURE)
640e28a4053SRui Paulo {
641e28a4053SRui Paulo 	SM_ENTRY(EAP, TIMEOUT_FAILURE);
642e28a4053SRui Paulo 
643*c1d255d3SCy Schubert 	sm->eap_if.eapTimeout = true;
64485732ac8SCy Schubert 
645*c1d255d3SCy Schubert 	wpa_msg(sm->cfg->msg_ctx, MSG_INFO,
646*c1d255d3SCy Schubert 		WPA_EVENT_EAP_TIMEOUT_FAILURE MACSTR, MAC2STR(sm->peer_addr));
647e28a4053SRui Paulo }
648e28a4053SRui Paulo 
649e28a4053SRui Paulo 
SM_STATE(EAP,FAILURE)650e28a4053SRui Paulo SM_STATE(EAP, FAILURE)
651e28a4053SRui Paulo {
652e28a4053SRui Paulo 	SM_ENTRY(EAP, FAILURE);
653e28a4053SRui Paulo 
654e28a4053SRui Paulo 	wpabuf_free(sm->eap_if.eapReqData);
655e28a4053SRui Paulo 	sm->eap_if.eapReqData = eap_sm_buildFailure(sm, sm->currentId);
656e28a4053SRui Paulo 	wpabuf_free(sm->lastReqData);
657e28a4053SRui Paulo 	sm->lastReqData = NULL;
658*c1d255d3SCy Schubert 	sm->eap_if.eapFail = true;
659e28a4053SRui Paulo 
660*c1d255d3SCy Schubert 	wpa_msg(sm->cfg->msg_ctx, MSG_INFO, WPA_EVENT_EAP_FAILURE
661e28a4053SRui Paulo 		MACSTR, MAC2STR(sm->peer_addr));
662e28a4053SRui Paulo }
663e28a4053SRui Paulo 
664e28a4053SRui Paulo 
SM_STATE(EAP,SUCCESS)665e28a4053SRui Paulo SM_STATE(EAP, SUCCESS)
666e28a4053SRui Paulo {
667e28a4053SRui Paulo 	SM_ENTRY(EAP, SUCCESS);
668e28a4053SRui Paulo 
669e28a4053SRui Paulo 	wpabuf_free(sm->eap_if.eapReqData);
670e28a4053SRui Paulo 	sm->eap_if.eapReqData = eap_sm_buildSuccess(sm, sm->currentId);
671e28a4053SRui Paulo 	wpabuf_free(sm->lastReqData);
672e28a4053SRui Paulo 	sm->lastReqData = NULL;
673e28a4053SRui Paulo 	if (sm->eap_if.eapKeyData)
674*c1d255d3SCy Schubert 		sm->eap_if.eapKeyAvailable = true;
675*c1d255d3SCy Schubert 	sm->eap_if.eapSuccess = true;
676e28a4053SRui Paulo 
677*c1d255d3SCy Schubert 	wpa_msg(sm->cfg->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
678e28a4053SRui Paulo 		MACSTR, MAC2STR(sm->peer_addr));
679e28a4053SRui Paulo }
680e28a4053SRui Paulo 
681e28a4053SRui Paulo 
SM_STATE(EAP,INITIATE_REAUTH_START)6825b9c547cSRui Paulo SM_STATE(EAP, INITIATE_REAUTH_START)
6835b9c547cSRui Paulo {
6845b9c547cSRui Paulo 	SM_ENTRY(EAP, INITIATE_REAUTH_START);
6855b9c547cSRui Paulo 
686*c1d255d3SCy Schubert 	sm->initiate_reauth_start_sent = true;
687*c1d255d3SCy Schubert 	sm->try_initiate_reauth = true;
6885b9c547cSRui Paulo 	sm->currentId = eap_sm_nextId(sm, sm->currentId);
6895b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG,
6905b9c547cSRui Paulo 		   "EAP: building EAP-Initiate-Re-auth-Start: Identifier %d",
6915b9c547cSRui Paulo 		   sm->currentId);
6925b9c547cSRui Paulo 	sm->lastId = sm->currentId;
6935b9c547cSRui Paulo 	wpabuf_free(sm->eap_if.eapReqData);
6945b9c547cSRui Paulo 	sm->eap_if.eapReqData = eap_sm_buildInitiateReauthStart(sm,
6955b9c547cSRui Paulo 								sm->currentId);
6965b9c547cSRui Paulo 	wpabuf_free(sm->lastReqData);
6975b9c547cSRui Paulo 	sm->lastReqData = NULL;
6985b9c547cSRui Paulo }
6995b9c547cSRui Paulo 
7005b9c547cSRui Paulo 
7015b9c547cSRui Paulo #ifdef CONFIG_ERP
7025b9c547cSRui Paulo 
erp_send_finish_reauth(struct eap_sm * sm,struct eap_server_erp_key * erp,u8 id,u8 flags,u16 seq,const char * nai)7035b9c547cSRui Paulo static void erp_send_finish_reauth(struct eap_sm *sm,
7045b9c547cSRui Paulo 				   struct eap_server_erp_key *erp, u8 id,
7055b9c547cSRui Paulo 				   u8 flags, u16 seq, const char *nai)
7065b9c547cSRui Paulo {
7075b9c547cSRui Paulo 	size_t plen;
7085b9c547cSRui Paulo 	struct wpabuf *msg;
7095b9c547cSRui Paulo 	u8 hash[SHA256_MAC_LEN];
7105b9c547cSRui Paulo 	size_t hash_len;
7115b9c547cSRui Paulo 	u8 seed[4];
7125b9c547cSRui Paulo 
7135b9c547cSRui Paulo 	if (erp) {
7145b9c547cSRui Paulo 		switch (erp->cryptosuite) {
7155b9c547cSRui Paulo 		case EAP_ERP_CS_HMAC_SHA256_256:
7165b9c547cSRui Paulo 			hash_len = 32;
7175b9c547cSRui Paulo 			break;
7185b9c547cSRui Paulo 		case EAP_ERP_CS_HMAC_SHA256_128:
7195b9c547cSRui Paulo 			hash_len = 16;
7205b9c547cSRui Paulo 			break;
7215b9c547cSRui Paulo 		default:
7225b9c547cSRui Paulo 			return;
7235b9c547cSRui Paulo 		}
7245b9c547cSRui Paulo 	} else
7255b9c547cSRui Paulo 		hash_len = 0;
7265b9c547cSRui Paulo 
7275b9c547cSRui Paulo 	plen = 1 + 2 + 2 + os_strlen(nai);
7285b9c547cSRui Paulo 	if (hash_len)
7295b9c547cSRui Paulo 		plen += 1 + hash_len;
730*c1d255d3SCy Schubert 	msg = eap_msg_alloc(EAP_VENDOR_IETF,
731*c1d255d3SCy Schubert 			    (enum eap_type) EAP_ERP_TYPE_REAUTH,
732325151a3SRui Paulo 			    plen, EAP_CODE_FINISH, id);
7335b9c547cSRui Paulo 	if (msg == NULL)
7345b9c547cSRui Paulo 		return;
7355b9c547cSRui Paulo 	wpabuf_put_u8(msg, flags);
7365b9c547cSRui Paulo 	wpabuf_put_be16(msg, seq);
7375b9c547cSRui Paulo 
7385b9c547cSRui Paulo 	wpabuf_put_u8(msg, EAP_ERP_TLV_KEYNAME_NAI);
7395b9c547cSRui Paulo 	wpabuf_put_u8(msg, os_strlen(nai));
7405b9c547cSRui Paulo 	wpabuf_put_str(msg, nai);
7415b9c547cSRui Paulo 
7425b9c547cSRui Paulo 	if (erp) {
7435b9c547cSRui Paulo 		wpabuf_put_u8(msg, erp->cryptosuite);
7445b9c547cSRui Paulo 		if (hmac_sha256(erp->rIK, erp->rIK_len,
7455b9c547cSRui Paulo 				wpabuf_head(msg), wpabuf_len(msg), hash) < 0) {
7465b9c547cSRui Paulo 			wpabuf_free(msg);
7475b9c547cSRui Paulo 			return;
7485b9c547cSRui Paulo 		}
7495b9c547cSRui Paulo 		wpabuf_put_data(msg, hash, hash_len);
7505b9c547cSRui Paulo 	}
7515b9c547cSRui Paulo 
7525b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "EAP: Send EAP-Finish/Re-auth (%s)",
7535b9c547cSRui Paulo 		   flags & 0x80 ? "failure" : "success");
7545b9c547cSRui Paulo 
7555b9c547cSRui Paulo 	sm->lastId = sm->currentId;
7565b9c547cSRui Paulo 	sm->currentId = id;
7575b9c547cSRui Paulo 	wpabuf_free(sm->eap_if.eapReqData);
7585b9c547cSRui Paulo 	sm->eap_if.eapReqData = msg;
7595b9c547cSRui Paulo 	wpabuf_free(sm->lastReqData);
7605b9c547cSRui Paulo 	sm->lastReqData = NULL;
7615b9c547cSRui Paulo 
762325151a3SRui Paulo 	if ((flags & 0x80) || !erp) {
763*c1d255d3SCy Schubert 		sm->eap_if.eapFail = true;
764*c1d255d3SCy Schubert 		wpa_msg(sm->cfg->msg_ctx, MSG_INFO, WPA_EVENT_EAP_FAILURE
7655b9c547cSRui Paulo 			MACSTR, MAC2STR(sm->peer_addr));
7665b9c547cSRui Paulo 		return;
7675b9c547cSRui Paulo 	}
7685b9c547cSRui Paulo 
7695b9c547cSRui Paulo 	bin_clear_free(sm->eap_if.eapKeyData, sm->eap_if.eapKeyDataLen);
7705b9c547cSRui Paulo 	sm->eap_if.eapKeyDataLen = 0;
7715b9c547cSRui Paulo 	sm->eap_if.eapKeyData = os_malloc(erp->rRK_len);
7725b9c547cSRui Paulo 	if (!sm->eap_if.eapKeyData)
7735b9c547cSRui Paulo 		return;
7745b9c547cSRui Paulo 
7755b9c547cSRui Paulo 	WPA_PUT_BE16(seed, seq);
7765b9c547cSRui Paulo 	WPA_PUT_BE16(&seed[2], erp->rRK_len);
7775b9c547cSRui Paulo 	if (hmac_sha256_kdf(erp->rRK, erp->rRK_len,
7785b9c547cSRui Paulo 			    "Re-authentication Master Session Key@ietf.org",
7795b9c547cSRui Paulo 			    seed, sizeof(seed),
7805b9c547cSRui Paulo 			    sm->eap_if.eapKeyData, erp->rRK_len) < 0) {
7815b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "EAP: Could not derive rMSK for ERP");
7825b9c547cSRui Paulo 		bin_clear_free(sm->eap_if.eapKeyData, erp->rRK_len);
7835b9c547cSRui Paulo 		sm->eap_if.eapKeyData = NULL;
7845b9c547cSRui Paulo 		return;
7855b9c547cSRui Paulo 	}
7865b9c547cSRui Paulo 	sm->eap_if.eapKeyDataLen = erp->rRK_len;
787*c1d255d3SCy Schubert 	sm->eap_if.eapKeyAvailable = true;
7885b9c547cSRui Paulo 	wpa_hexdump_key(MSG_DEBUG, "EAP: ERP rMSK",
7895b9c547cSRui Paulo 			sm->eap_if.eapKeyData, sm->eap_if.eapKeyDataLen);
790*c1d255d3SCy Schubert 	sm->eap_if.eapSuccess = true;
7915b9c547cSRui Paulo 
792*c1d255d3SCy Schubert 	wpa_msg(sm->cfg->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
7935b9c547cSRui Paulo 		MACSTR, MAC2STR(sm->peer_addr));
7945b9c547cSRui Paulo }
7955b9c547cSRui Paulo 
7965b9c547cSRui Paulo 
SM_STATE(EAP,INITIATE_RECEIVED)7975b9c547cSRui Paulo SM_STATE(EAP, INITIATE_RECEIVED)
7985b9c547cSRui Paulo {
7995b9c547cSRui Paulo 	const u8 *pos, *end, *start, *tlvs, *hdr;
8005b9c547cSRui Paulo 	const struct eap_hdr *ehdr;
8015b9c547cSRui Paulo 	size_t len;
8025b9c547cSRui Paulo 	u8 flags;
8035b9c547cSRui Paulo 	u16 seq;
8045b9c547cSRui Paulo 	char nai[254];
8055b9c547cSRui Paulo 	struct eap_server_erp_key *erp;
8065b9c547cSRui Paulo 	int max_len;
8075b9c547cSRui Paulo 	u8 hash[SHA256_MAC_LEN];
8085b9c547cSRui Paulo 	size_t hash_len;
8095b9c547cSRui Paulo 	struct erp_tlvs parse;
8105b9c547cSRui Paulo 	u8 resp_flags = 0x80; /* default to failure; cleared on success */
8115b9c547cSRui Paulo 
8125b9c547cSRui Paulo 	SM_ENTRY(EAP, INITIATE_RECEIVED);
8135b9c547cSRui Paulo 
814*c1d255d3SCy Schubert 	sm->rxInitiate = false;
8155b9c547cSRui Paulo 
816*c1d255d3SCy Schubert 	pos = eap_hdr_validate(EAP_VENDOR_IETF,
817*c1d255d3SCy Schubert 			       (enum eap_type) EAP_ERP_TYPE_REAUTH,
8185b9c547cSRui Paulo 			       sm->eap_if.eapRespData, &len);
8195b9c547cSRui Paulo 	if (pos == NULL) {
8205b9c547cSRui Paulo 		wpa_printf(MSG_INFO, "EAP-Initiate: Invalid frame");
8215b9c547cSRui Paulo 		goto fail;
8225b9c547cSRui Paulo 	}
8235b9c547cSRui Paulo 	hdr = wpabuf_head(sm->eap_if.eapRespData);
8245b9c547cSRui Paulo 	ehdr = wpabuf_head(sm->eap_if.eapRespData);
8255b9c547cSRui Paulo 
8265b9c547cSRui Paulo 	wpa_hexdump(MSG_DEBUG, "EAP: EAP-Initiate/Re-Auth", pos, len);
8275b9c547cSRui Paulo 	if (len < 4) {
8285b9c547cSRui Paulo 		wpa_printf(MSG_INFO, "EAP: Too short EAP-Initiate/Re-auth");
8295b9c547cSRui Paulo 		goto fail;
8305b9c547cSRui Paulo 	}
8315b9c547cSRui Paulo 	end = pos + len;
8325b9c547cSRui Paulo 
8335b9c547cSRui Paulo 	flags = *pos++;
8345b9c547cSRui Paulo 	seq = WPA_GET_BE16(pos);
8355b9c547cSRui Paulo 	pos += 2;
8365b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "EAP: Flags=0x%x SEQ=%u", flags, seq);
8375b9c547cSRui Paulo 	tlvs = pos;
8385b9c547cSRui Paulo 
8395b9c547cSRui Paulo 	/*
8405b9c547cSRui Paulo 	 * Parse TVs/TLVs. Since we do not yet know the length of the
8415b9c547cSRui Paulo 	 * Authentication Tag, stop parsing if an unknown TV/TLV is seen and
8425b9c547cSRui Paulo 	 * just try to find the keyName-NAI first so that we can check the
8435b9c547cSRui Paulo 	 * Authentication Tag.
8445b9c547cSRui Paulo 	 */
8455b9c547cSRui Paulo 	if (erp_parse_tlvs(tlvs, end, &parse, 1) < 0)
8465b9c547cSRui Paulo 		goto fail;
8475b9c547cSRui Paulo 
8485b9c547cSRui Paulo 	if (!parse.keyname) {
8495b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
8505b9c547cSRui Paulo 			   "EAP: No keyName-NAI in EAP-Initiate/Re-auth Packet");
8515b9c547cSRui Paulo 		goto fail;
8525b9c547cSRui Paulo 	}
8535b9c547cSRui Paulo 
8545b9c547cSRui Paulo 	wpa_hexdump_ascii(MSG_DEBUG, "EAP: EAP-Initiate/Re-auth - keyName-NAI",
8555b9c547cSRui Paulo 			  parse.keyname, parse.keyname_len);
8565b9c547cSRui Paulo 	if (parse.keyname_len > 253) {
8575b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
8585b9c547cSRui Paulo 			   "EAP: Too long keyName-NAI in EAP-Initiate/Re-auth");
8595b9c547cSRui Paulo 		goto fail;
8605b9c547cSRui Paulo 	}
8615b9c547cSRui Paulo 	os_memcpy(nai, parse.keyname, parse.keyname_len);
8625b9c547cSRui Paulo 	nai[parse.keyname_len] = '\0';
8635b9c547cSRui Paulo 
864*c1d255d3SCy Schubert 	if (!sm->cfg->eap_server) {
8655b9c547cSRui Paulo 		/*
8665b9c547cSRui Paulo 		 * In passthrough case, EAP-Initiate/Re-auth replaces
8675b9c547cSRui Paulo 		 * EAP Identity exchange. Use keyName-NAI as the user identity
8685b9c547cSRui Paulo 		 * and forward EAP-Initiate/Re-auth to the backend
8695b9c547cSRui Paulo 		 * authentication server.
8705b9c547cSRui Paulo 		 */
8715b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
8725b9c547cSRui Paulo 			   "EAP: Use keyName-NAI as user identity for backend authentication");
8735b9c547cSRui Paulo 		eap_server_clear_identity(sm);
8745b9c547cSRui Paulo 		sm->identity = (u8 *) dup_binstr(parse.keyname,
8755b9c547cSRui Paulo 						 parse.keyname_len);
8765b9c547cSRui Paulo 		if (!sm->identity)
8775b9c547cSRui Paulo 			goto fail;
8785b9c547cSRui Paulo 		sm->identity_len = parse.keyname_len;
8795b9c547cSRui Paulo 		return;
8805b9c547cSRui Paulo 	}
8815b9c547cSRui Paulo 
8825b9c547cSRui Paulo 	erp = eap_erp_get_key(sm, nai);
8835b9c547cSRui Paulo 	if (!erp) {
8845b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "EAP: No matching ERP key found for %s",
8855b9c547cSRui Paulo 			   nai);
8865b9c547cSRui Paulo 		goto report_error;
8875b9c547cSRui Paulo 	}
8885b9c547cSRui Paulo 
8895b9c547cSRui Paulo 	if (erp->recv_seq != (u32) -1 && erp->recv_seq >= seq) {
8905b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
8915b9c547cSRui Paulo 			   "EAP: SEQ=%u replayed (already received SEQ=%u)",
8925b9c547cSRui Paulo 			   seq, erp->recv_seq);
8935b9c547cSRui Paulo 		goto fail;
8945b9c547cSRui Paulo 	}
8955b9c547cSRui Paulo 
8965b9c547cSRui Paulo 	/* Is there enough room for Cryptosuite and Authentication Tag? */
8975b9c547cSRui Paulo 	start = parse.keyname + parse.keyname_len;
8985b9c547cSRui Paulo 	max_len = end - start;
8995b9c547cSRui Paulo 	if (max_len <
9005b9c547cSRui Paulo 	    1 + (erp->cryptosuite == EAP_ERP_CS_HMAC_SHA256_256 ? 32 : 16)) {
9015b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
9025b9c547cSRui Paulo 			   "EAP: Not enough room for Authentication Tag");
9035b9c547cSRui Paulo 		goto fail;
9045b9c547cSRui Paulo 	}
9055b9c547cSRui Paulo 
9065b9c547cSRui Paulo 	switch (erp->cryptosuite) {
9075b9c547cSRui Paulo 	case EAP_ERP_CS_HMAC_SHA256_256:
9085b9c547cSRui Paulo 		if (end[-33] != erp->cryptosuite) {
9095b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
9105b9c547cSRui Paulo 				   "EAP: Different Cryptosuite used");
9115b9c547cSRui Paulo 			goto fail;
9125b9c547cSRui Paulo 		}
9135b9c547cSRui Paulo 		hash_len = 32;
9145b9c547cSRui Paulo 		break;
9155b9c547cSRui Paulo 	case EAP_ERP_CS_HMAC_SHA256_128:
9165b9c547cSRui Paulo 		if (end[-17] != erp->cryptosuite) {
9175b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
9185b9c547cSRui Paulo 				   "EAP: Different Cryptosuite used");
9195b9c547cSRui Paulo 			goto fail;
9205b9c547cSRui Paulo 		}
9215b9c547cSRui Paulo 		hash_len = 16;
9225b9c547cSRui Paulo 		break;
9235b9c547cSRui Paulo 	default:
9245b9c547cSRui Paulo 		hash_len = 0;
9255b9c547cSRui Paulo 		break;
9265b9c547cSRui Paulo 	}
9275b9c547cSRui Paulo 
9285b9c547cSRui Paulo 	if (hash_len) {
9295b9c547cSRui Paulo 		if (hmac_sha256(erp->rIK, erp->rIK_len, hdr,
9305b9c547cSRui Paulo 				end - hdr - hash_len, hash) < 0)
9315b9c547cSRui Paulo 			goto fail;
9325b9c547cSRui Paulo 		if (os_memcmp(end - hash_len, hash, hash_len) != 0) {
9335b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
9345b9c547cSRui Paulo 				   "EAP: Authentication Tag mismatch");
9355b9c547cSRui Paulo 			goto fail;
9365b9c547cSRui Paulo 		}
9375b9c547cSRui Paulo 	}
9385b9c547cSRui Paulo 
9395b9c547cSRui Paulo 	/* Check if any supported CS results in matching tag */
9405b9c547cSRui Paulo 	if (!hash_len && max_len >= 1 + 32 &&
9415b9c547cSRui Paulo 	    end[-33] == EAP_ERP_CS_HMAC_SHA256_256) {
9425b9c547cSRui Paulo 		if (hmac_sha256(erp->rIK, erp->rIK_len, hdr,
9435b9c547cSRui Paulo 				end - hdr - 32, hash) < 0)
9445b9c547cSRui Paulo 			goto fail;
9455b9c547cSRui Paulo 		if (os_memcmp(end - 32, hash, 32) == 0) {
9465b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
9475b9c547cSRui Paulo 				   "EAP: Authentication Tag match using HMAC-SHA256-256");
9485b9c547cSRui Paulo 			hash_len = 32;
9495b9c547cSRui Paulo 			erp->cryptosuite = EAP_ERP_CS_HMAC_SHA256_256;
9505b9c547cSRui Paulo 		}
9515b9c547cSRui Paulo 	}
9525b9c547cSRui Paulo 
9535b9c547cSRui Paulo 	if (!hash_len && end[-17] == EAP_ERP_CS_HMAC_SHA256_128) {
9545b9c547cSRui Paulo 		if (hmac_sha256(erp->rIK, erp->rIK_len, hdr,
9555b9c547cSRui Paulo 				end - hdr - 16, hash) < 0)
9565b9c547cSRui Paulo 			goto fail;
9575b9c547cSRui Paulo 		if (os_memcmp(end - 16, hash, 16) == 0) {
9585b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
9595b9c547cSRui Paulo 				   "EAP: Authentication Tag match using HMAC-SHA256-128");
9605b9c547cSRui Paulo 			hash_len = 16;
9615b9c547cSRui Paulo 			erp->cryptosuite = EAP_ERP_CS_HMAC_SHA256_128;
9625b9c547cSRui Paulo 		}
9635b9c547cSRui Paulo 	}
9645b9c547cSRui Paulo 
9655b9c547cSRui Paulo 	if (!hash_len) {
9665b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
9675b9c547cSRui Paulo 			   "EAP: No supported cryptosuite matched Authentication Tag");
9685b9c547cSRui Paulo 		goto fail;
9695b9c547cSRui Paulo 	}
9705b9c547cSRui Paulo 	end -= 1 + hash_len;
9715b9c547cSRui Paulo 
9725b9c547cSRui Paulo 	/*
9735b9c547cSRui Paulo 	 * Parse TVs/TLVs again now that we know the exact part of the buffer
9745b9c547cSRui Paulo 	 * that contains them.
9755b9c547cSRui Paulo 	 */
9765b9c547cSRui Paulo 	wpa_hexdump(MSG_DEBUG, "EAP: EAP-Initiate/Re-Auth TVs/TLVs",
9775b9c547cSRui Paulo 		    tlvs, end - tlvs);
9785b9c547cSRui Paulo 	if (erp_parse_tlvs(tlvs, end, &parse, 0) < 0)
9795b9c547cSRui Paulo 		goto fail;
9805b9c547cSRui Paulo 
9815b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "EAP: ERP key %s SEQ updated to %u",
9825b9c547cSRui Paulo 		   erp->keyname_nai, seq);
9835b9c547cSRui Paulo 	erp->recv_seq = seq;
9845b9c547cSRui Paulo 	resp_flags &= ~0x80; /* R=0 - success */
9855b9c547cSRui Paulo 
9865b9c547cSRui Paulo report_error:
9875b9c547cSRui Paulo 	erp_send_finish_reauth(sm, erp, ehdr->identifier, resp_flags, seq, nai);
9885b9c547cSRui Paulo 	return;
9895b9c547cSRui Paulo 
9905b9c547cSRui Paulo fail:
991*c1d255d3SCy Schubert 	sm->ignore = true;
9925b9c547cSRui Paulo }
9935b9c547cSRui Paulo 
9945b9c547cSRui Paulo #endif /* CONFIG_ERP */
9955b9c547cSRui Paulo 
9965b9c547cSRui Paulo 
SM_STATE(EAP,INITIALIZE_PASSTHROUGH)997e28a4053SRui Paulo SM_STATE(EAP, INITIALIZE_PASSTHROUGH)
998e28a4053SRui Paulo {
999e28a4053SRui Paulo 	SM_ENTRY(EAP, INITIALIZE_PASSTHROUGH);
1000e28a4053SRui Paulo 
1001e28a4053SRui Paulo 	wpabuf_free(sm->eap_if.aaaEapRespData);
1002e28a4053SRui Paulo 	sm->eap_if.aaaEapRespData = NULL;
1003*c1d255d3SCy Schubert 	sm->try_initiate_reauth = false;
1004e28a4053SRui Paulo }
1005e28a4053SRui Paulo 
1006e28a4053SRui Paulo 
SM_STATE(EAP,IDLE2)1007e28a4053SRui Paulo SM_STATE(EAP, IDLE2)
1008e28a4053SRui Paulo {
1009e28a4053SRui Paulo 	SM_ENTRY(EAP, IDLE2);
1010e28a4053SRui Paulo 
1011e28a4053SRui Paulo 	sm->eap_if.retransWhile = eap_sm_calculateTimeout(
1012e28a4053SRui Paulo 		sm, sm->retransCount, sm->eap_if.eapSRTT, sm->eap_if.eapRTTVAR,
1013e28a4053SRui Paulo 		sm->methodTimeout);
1014e28a4053SRui Paulo }
1015e28a4053SRui Paulo 
1016e28a4053SRui Paulo 
SM_STATE(EAP,RETRANSMIT2)1017e28a4053SRui Paulo SM_STATE(EAP, RETRANSMIT2)
1018e28a4053SRui Paulo {
1019e28a4053SRui Paulo 	SM_ENTRY(EAP, RETRANSMIT2);
1020e28a4053SRui Paulo 
1021e28a4053SRui Paulo 	sm->retransCount++;
1022e28a4053SRui Paulo 	if (sm->retransCount <= sm->MaxRetrans && sm->lastReqData) {
1023e28a4053SRui Paulo 		if (eap_copy_buf(&sm->eap_if.eapReqData, sm->lastReqData) == 0)
1024*c1d255d3SCy Schubert 			sm->eap_if.eapReq = true;
1025e28a4053SRui Paulo 	}
102685732ac8SCy Schubert 
1027*c1d255d3SCy Schubert 	wpa_msg(sm->cfg->msg_ctx, MSG_INFO, WPA_EVENT_EAP_RETRANSMIT2 MACSTR,
102885732ac8SCy Schubert 		MAC2STR(sm->peer_addr));
1029e28a4053SRui Paulo }
1030e28a4053SRui Paulo 
1031e28a4053SRui Paulo 
SM_STATE(EAP,RECEIVED2)1032e28a4053SRui Paulo SM_STATE(EAP, RECEIVED2)
1033e28a4053SRui Paulo {
1034e28a4053SRui Paulo 	SM_ENTRY(EAP, RECEIVED2);
1035e28a4053SRui Paulo 
1036e28a4053SRui Paulo 	/* parse rxResp, respId, respMethod */
1037e28a4053SRui Paulo 	eap_sm_parseEapResp(sm, sm->eap_if.eapRespData);
1038e28a4053SRui Paulo }
1039e28a4053SRui Paulo 
1040e28a4053SRui Paulo 
SM_STATE(EAP,DISCARD2)1041e28a4053SRui Paulo SM_STATE(EAP, DISCARD2)
1042e28a4053SRui Paulo {
1043e28a4053SRui Paulo 	SM_ENTRY(EAP, DISCARD2);
1044*c1d255d3SCy Schubert 	sm->eap_if.eapResp = false;
1045*c1d255d3SCy Schubert 	sm->eap_if.eapNoReq = true;
1046e28a4053SRui Paulo }
1047e28a4053SRui Paulo 
1048e28a4053SRui Paulo 
SM_STATE(EAP,SEND_REQUEST2)1049e28a4053SRui Paulo SM_STATE(EAP, SEND_REQUEST2)
1050e28a4053SRui Paulo {
1051e28a4053SRui Paulo 	SM_ENTRY(EAP, SEND_REQUEST2);
1052e28a4053SRui Paulo 
1053e28a4053SRui Paulo 	sm->retransCount = 0;
1054e28a4053SRui Paulo 	if (sm->eap_if.eapReqData) {
1055e28a4053SRui Paulo 		if (eap_copy_buf(&sm->lastReqData, sm->eap_if.eapReqData) == 0)
1056e28a4053SRui Paulo 		{
1057*c1d255d3SCy Schubert 			sm->eap_if.eapResp = false;
1058*c1d255d3SCy Schubert 			sm->eap_if.eapReq = true;
1059e28a4053SRui Paulo 		} else {
1060*c1d255d3SCy Schubert 			sm->eap_if.eapResp = false;
1061*c1d255d3SCy Schubert 			sm->eap_if.eapReq = false;
1062e28a4053SRui Paulo 		}
1063e28a4053SRui Paulo 	} else {
1064e28a4053SRui Paulo 		wpa_printf(MSG_INFO, "EAP: SEND_REQUEST2 - no eapReqData");
1065*c1d255d3SCy Schubert 		sm->eap_if.eapResp = false;
1066*c1d255d3SCy Schubert 		sm->eap_if.eapReq = false;
1067*c1d255d3SCy Schubert 		sm->eap_if.eapNoReq = true;
1068e28a4053SRui Paulo 	}
1069e28a4053SRui Paulo }
1070e28a4053SRui Paulo 
1071e28a4053SRui Paulo 
SM_STATE(EAP,AAA_REQUEST)1072e28a4053SRui Paulo SM_STATE(EAP, AAA_REQUEST)
1073e28a4053SRui Paulo {
1074e28a4053SRui Paulo 	SM_ENTRY(EAP, AAA_REQUEST);
1075e28a4053SRui Paulo 
1076e28a4053SRui Paulo 	if (sm->eap_if.eapRespData == NULL) {
1077e28a4053SRui Paulo 		wpa_printf(MSG_INFO, "EAP: AAA_REQUEST - no eapRespData");
1078e28a4053SRui Paulo 		return;
1079e28a4053SRui Paulo 	}
1080e28a4053SRui Paulo 
1081e28a4053SRui Paulo 	/*
1082e28a4053SRui Paulo 	 * if (respMethod == IDENTITY)
1083e28a4053SRui Paulo 	 *	aaaIdentity = eapRespData
1084e28a4053SRui Paulo 	 * This is already taken care of by the EAP-Identity method which
1085e28a4053SRui Paulo 	 * stores the identity into sm->identity.
1086e28a4053SRui Paulo 	 */
1087e28a4053SRui Paulo 
1088e28a4053SRui Paulo 	eap_copy_buf(&sm->eap_if.aaaEapRespData, sm->eap_if.eapRespData);
1089e28a4053SRui Paulo }
1090e28a4053SRui Paulo 
1091e28a4053SRui Paulo 
SM_STATE(EAP,AAA_RESPONSE)1092e28a4053SRui Paulo SM_STATE(EAP, AAA_RESPONSE)
1093e28a4053SRui Paulo {
1094e28a4053SRui Paulo 	SM_ENTRY(EAP, AAA_RESPONSE);
1095e28a4053SRui Paulo 
1096e28a4053SRui Paulo 	eap_copy_buf(&sm->eap_if.eapReqData, sm->eap_if.aaaEapReqData);
1097e28a4053SRui Paulo 	sm->currentId = eap_sm_getId(sm->eap_if.eapReqData);
1098e28a4053SRui Paulo 	sm->methodTimeout = sm->eap_if.aaaMethodTimeout;
1099e28a4053SRui Paulo }
1100e28a4053SRui Paulo 
1101e28a4053SRui Paulo 
SM_STATE(EAP,AAA_IDLE)1102e28a4053SRui Paulo SM_STATE(EAP, AAA_IDLE)
1103e28a4053SRui Paulo {
1104e28a4053SRui Paulo 	SM_ENTRY(EAP, AAA_IDLE);
1105e28a4053SRui Paulo 
1106*c1d255d3SCy Schubert 	sm->eap_if.aaaFail = false;
1107*c1d255d3SCy Schubert 	sm->eap_if.aaaSuccess = false;
1108*c1d255d3SCy Schubert 	sm->eap_if.aaaEapReq = false;
1109*c1d255d3SCy Schubert 	sm->eap_if.aaaEapNoReq = false;
1110*c1d255d3SCy Schubert 	sm->eap_if.aaaEapResp = true;
1111e28a4053SRui Paulo }
1112e28a4053SRui Paulo 
1113e28a4053SRui Paulo 
SM_STATE(EAP,TIMEOUT_FAILURE2)1114e28a4053SRui Paulo SM_STATE(EAP, TIMEOUT_FAILURE2)
1115e28a4053SRui Paulo {
1116e28a4053SRui Paulo 	SM_ENTRY(EAP, TIMEOUT_FAILURE2);
1117e28a4053SRui Paulo 
1118*c1d255d3SCy Schubert 	sm->eap_if.eapTimeout = true;
111985732ac8SCy Schubert 
1120*c1d255d3SCy Schubert 	wpa_msg(sm->cfg->msg_ctx, MSG_INFO,
1121*c1d255d3SCy Schubert 		WPA_EVENT_EAP_TIMEOUT_FAILURE2 MACSTR, MAC2STR(sm->peer_addr));
1122e28a4053SRui Paulo }
1123e28a4053SRui Paulo 
1124e28a4053SRui Paulo 
SM_STATE(EAP,FAILURE2)1125e28a4053SRui Paulo SM_STATE(EAP, FAILURE2)
1126e28a4053SRui Paulo {
1127e28a4053SRui Paulo 	SM_ENTRY(EAP, FAILURE2);
1128e28a4053SRui Paulo 
1129e28a4053SRui Paulo 	eap_copy_buf(&sm->eap_if.eapReqData, sm->eap_if.aaaEapReqData);
1130*c1d255d3SCy Schubert 	sm->eap_if.eapFail = true;
113185732ac8SCy Schubert 
1132*c1d255d3SCy Schubert 	wpa_msg(sm->cfg->msg_ctx, MSG_INFO, WPA_EVENT_EAP_FAILURE2 MACSTR,
113385732ac8SCy Schubert 		MAC2STR(sm->peer_addr));
1134e28a4053SRui Paulo }
1135e28a4053SRui Paulo 
1136e28a4053SRui Paulo 
SM_STATE(EAP,SUCCESS2)1137e28a4053SRui Paulo SM_STATE(EAP, SUCCESS2)
1138e28a4053SRui Paulo {
1139e28a4053SRui Paulo 	SM_ENTRY(EAP, SUCCESS2);
1140e28a4053SRui Paulo 
1141e28a4053SRui Paulo 	eap_copy_buf(&sm->eap_if.eapReqData, sm->eap_if.aaaEapReqData);
1142e28a4053SRui Paulo 
1143e28a4053SRui Paulo 	sm->eap_if.eapKeyAvailable = sm->eap_if.aaaEapKeyAvailable;
1144e28a4053SRui Paulo 	if (sm->eap_if.aaaEapKeyAvailable) {
1145e28a4053SRui Paulo 		EAP_COPY(&sm->eap_if.eapKeyData, sm->eap_if.aaaEapKeyData);
1146e28a4053SRui Paulo 	} else {
11475b9c547cSRui Paulo 		bin_clear_free(sm->eap_if.eapKeyData, sm->eap_if.eapKeyDataLen);
1148e28a4053SRui Paulo 		sm->eap_if.eapKeyData = NULL;
1149e28a4053SRui Paulo 		sm->eap_if.eapKeyDataLen = 0;
1150e28a4053SRui Paulo 	}
1151e28a4053SRui Paulo 
1152*c1d255d3SCy Schubert 	sm->eap_if.eapSuccess = true;
1153e28a4053SRui Paulo 
1154e28a4053SRui Paulo 	/*
1155e28a4053SRui Paulo 	 * Start reauthentication with identity request even though we know the
1156e28a4053SRui Paulo 	 * previously used identity. This is needed to get reauthentication
1157e28a4053SRui Paulo 	 * started properly.
1158e28a4053SRui Paulo 	 */
1159*c1d255d3SCy Schubert 	sm->start_reauth = true;
116085732ac8SCy Schubert 
1161*c1d255d3SCy Schubert 	wpa_msg(sm->cfg->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS2 MACSTR,
116285732ac8SCy Schubert 		MAC2STR(sm->peer_addr));
1163e28a4053SRui Paulo }
1164e28a4053SRui Paulo 
1165e28a4053SRui Paulo 
SM_STEP(EAP)1166e28a4053SRui Paulo SM_STEP(EAP)
1167e28a4053SRui Paulo {
1168e28a4053SRui Paulo 	if (sm->eap_if.eapRestart && sm->eap_if.portEnabled)
1169e28a4053SRui Paulo 		SM_ENTER_GLOBAL(EAP, INITIALIZE);
1170e28a4053SRui Paulo 	else if (!sm->eap_if.portEnabled)
1171e28a4053SRui Paulo 		SM_ENTER_GLOBAL(EAP, DISABLED);
1172*c1d255d3SCy Schubert 	else if (sm->num_rounds > sm->cfg->max_auth_rounds) {
1173*c1d255d3SCy Schubert 		if (sm->num_rounds == sm->cfg->max_auth_rounds + 1) {
1174e28a4053SRui Paulo 			wpa_printf(MSG_DEBUG, "EAP: more than %d "
1175e28a4053SRui Paulo 				   "authentication rounds - abort",
1176*c1d255d3SCy Schubert 				   sm->cfg->max_auth_rounds);
1177e28a4053SRui Paulo 			sm->num_rounds++;
1178e28a4053SRui Paulo 			SM_ENTER_GLOBAL(EAP, FAILURE);
1179e28a4053SRui Paulo 		}
1180*c1d255d3SCy Schubert 	} else if (sm->num_rounds_short > sm->cfg->max_auth_rounds_short) {
1181*c1d255d3SCy Schubert 		if (sm->num_rounds_short ==
1182*c1d255d3SCy Schubert 		    sm->cfg->max_auth_rounds_short + 1) {
1183*c1d255d3SCy Schubert 			wpa_printf(MSG_DEBUG,
1184*c1d255d3SCy Schubert 				   "EAP: more than %d authentication rounds (short) - abort",
1185*c1d255d3SCy Schubert 				   sm->cfg->max_auth_rounds_short);
1186*c1d255d3SCy Schubert 			sm->num_rounds_short++;
1187*c1d255d3SCy Schubert 			SM_ENTER_GLOBAL(EAP, FAILURE);
1188*c1d255d3SCy Schubert 		}
1189e28a4053SRui Paulo 	} else switch (sm->EAP_state) {
1190e28a4053SRui Paulo 	case EAP_INITIALIZE:
1191*c1d255d3SCy Schubert 		if (sm->cfg->backend_auth) {
1192e28a4053SRui Paulo 			if (!sm->rxResp)
1193e28a4053SRui Paulo 				SM_ENTER(EAP, SELECT_ACTION);
1194e28a4053SRui Paulo 			else if (sm->rxResp &&
1195e28a4053SRui Paulo 				 (sm->respMethod == EAP_TYPE_NAK ||
1196e28a4053SRui Paulo 				  (sm->respMethod == EAP_TYPE_EXPANDED &&
1197e28a4053SRui Paulo 				   sm->respVendor == EAP_VENDOR_IETF &&
1198e28a4053SRui Paulo 				   sm->respVendorMethod == EAP_TYPE_NAK)))
1199e28a4053SRui Paulo 				SM_ENTER(EAP, NAK);
1200e28a4053SRui Paulo 			else
1201e28a4053SRui Paulo 				SM_ENTER(EAP, PICK_UP_METHOD);
1202e28a4053SRui Paulo 		} else {
1203e28a4053SRui Paulo 			SM_ENTER(EAP, SELECT_ACTION);
1204e28a4053SRui Paulo 		}
1205e28a4053SRui Paulo 		break;
1206e28a4053SRui Paulo 	case EAP_PICK_UP_METHOD:
1207e28a4053SRui Paulo 		if (sm->currentMethod == EAP_TYPE_NONE) {
1208e28a4053SRui Paulo 			SM_ENTER(EAP, SELECT_ACTION);
1209e28a4053SRui Paulo 		} else {
1210e28a4053SRui Paulo 			SM_ENTER(EAP, METHOD_RESPONSE);
1211e28a4053SRui Paulo 		}
1212e28a4053SRui Paulo 		break;
1213e28a4053SRui Paulo 	case EAP_DISABLED:
1214e28a4053SRui Paulo 		if (sm->eap_if.portEnabled)
1215e28a4053SRui Paulo 			SM_ENTER(EAP, INITIALIZE);
1216e28a4053SRui Paulo 		break;
1217e28a4053SRui Paulo 	case EAP_IDLE:
12185b9c547cSRui Paulo 		if (sm->eap_if.retransWhile == 0) {
12195b9c547cSRui Paulo 			if (sm->try_initiate_reauth) {
1220*c1d255d3SCy Schubert 				sm->try_initiate_reauth = false;
12215b9c547cSRui Paulo 				SM_ENTER(EAP, SELECT_ACTION);
12225b9c547cSRui Paulo 			} else {
1223e28a4053SRui Paulo 				SM_ENTER(EAP, RETRANSMIT);
12245b9c547cSRui Paulo 			}
12255b9c547cSRui Paulo 		} else if (sm->eap_if.eapResp)
1226e28a4053SRui Paulo 			SM_ENTER(EAP, RECEIVED);
1227e28a4053SRui Paulo 		break;
1228e28a4053SRui Paulo 	case EAP_RETRANSMIT:
1229e28a4053SRui Paulo 		if (sm->retransCount > sm->MaxRetrans)
1230e28a4053SRui Paulo 			SM_ENTER(EAP, TIMEOUT_FAILURE);
1231e28a4053SRui Paulo 		else
1232e28a4053SRui Paulo 			SM_ENTER(EAP, IDLE);
1233e28a4053SRui Paulo 		break;
1234e28a4053SRui Paulo 	case EAP_RECEIVED:
1235e28a4053SRui Paulo 		if (sm->rxResp && (sm->respId == sm->currentId) &&
1236e28a4053SRui Paulo 		    (sm->respMethod == EAP_TYPE_NAK ||
1237e28a4053SRui Paulo 		     (sm->respMethod == EAP_TYPE_EXPANDED &&
1238e28a4053SRui Paulo 		      sm->respVendor == EAP_VENDOR_IETF &&
1239e28a4053SRui Paulo 		      sm->respVendorMethod == EAP_TYPE_NAK))
1240e28a4053SRui Paulo 		    && (sm->methodState == METHOD_PROPOSED))
1241e28a4053SRui Paulo 			SM_ENTER(EAP, NAK);
1242e28a4053SRui Paulo 		else if (sm->rxResp && (sm->respId == sm->currentId) &&
1243e28a4053SRui Paulo 			 ((sm->respMethod == sm->currentMethod) ||
1244e28a4053SRui Paulo 			  (sm->respMethod == EAP_TYPE_EXPANDED &&
1245e28a4053SRui Paulo 			   sm->respVendor == EAP_VENDOR_IETF &&
1246e28a4053SRui Paulo 			   sm->respVendorMethod == sm->currentMethod)))
1247e28a4053SRui Paulo 			SM_ENTER(EAP, INTEGRITY_CHECK);
12485b9c547cSRui Paulo #ifdef CONFIG_ERP
12495b9c547cSRui Paulo 		else if (sm->rxInitiate)
12505b9c547cSRui Paulo 			SM_ENTER(EAP, INITIATE_RECEIVED);
12515b9c547cSRui Paulo #endif /* CONFIG_ERP */
1252e28a4053SRui Paulo 		else {
1253e28a4053SRui Paulo 			wpa_printf(MSG_DEBUG, "EAP: RECEIVED->DISCARD: "
1254e28a4053SRui Paulo 				   "rxResp=%d respId=%d currentId=%d "
1255e28a4053SRui Paulo 				   "respMethod=%d currentMethod=%d",
1256e28a4053SRui Paulo 				   sm->rxResp, sm->respId, sm->currentId,
1257e28a4053SRui Paulo 				   sm->respMethod, sm->currentMethod);
12585b9c547cSRui Paulo 			eap_log_msg(sm, "Discard received EAP message");
1259e28a4053SRui Paulo 			SM_ENTER(EAP, DISCARD);
1260e28a4053SRui Paulo 		}
1261e28a4053SRui Paulo 		break;
1262e28a4053SRui Paulo 	case EAP_DISCARD:
1263e28a4053SRui Paulo 		SM_ENTER(EAP, IDLE);
1264e28a4053SRui Paulo 		break;
1265e28a4053SRui Paulo 	case EAP_SEND_REQUEST:
1266e28a4053SRui Paulo 		SM_ENTER(EAP, IDLE);
1267e28a4053SRui Paulo 		break;
1268e28a4053SRui Paulo 	case EAP_INTEGRITY_CHECK:
1269e28a4053SRui Paulo 		if (sm->ignore)
1270e28a4053SRui Paulo 			SM_ENTER(EAP, DISCARD);
1271e28a4053SRui Paulo 		else
1272e28a4053SRui Paulo 			SM_ENTER(EAP, METHOD_RESPONSE);
1273e28a4053SRui Paulo 		break;
1274e28a4053SRui Paulo 	case EAP_METHOD_REQUEST:
12755b9c547cSRui Paulo 		if (sm->m == NULL) {
12765b9c547cSRui Paulo 			/*
12775b9c547cSRui Paulo 			 * This transition is not mentioned in RFC 4137, but it
12785b9c547cSRui Paulo 			 * is needed to handle cleanly a case where EAP method
12795b9c547cSRui Paulo 			 * initialization fails.
12805b9c547cSRui Paulo 			 */
12815b9c547cSRui Paulo 			SM_ENTER(EAP, FAILURE);
12825b9c547cSRui Paulo 			break;
12835b9c547cSRui Paulo 		}
1284e28a4053SRui Paulo 		SM_ENTER(EAP, SEND_REQUEST);
1285325151a3SRui Paulo 		if (sm->eap_if.eapNoReq && !sm->eap_if.eapReq) {
1286325151a3SRui Paulo 			/*
1287325151a3SRui Paulo 			 * This transition is not mentioned in RFC 4137, but it
1288325151a3SRui Paulo 			 * is needed to handle cleanly a case where EAP method
1289325151a3SRui Paulo 			 * buildReq fails.
1290325151a3SRui Paulo 			 */
1291325151a3SRui Paulo 			wpa_printf(MSG_DEBUG,
1292325151a3SRui Paulo 				   "EAP: Method did not return a request");
1293325151a3SRui Paulo 			SM_ENTER(EAP, FAILURE);
1294325151a3SRui Paulo 			break;
1295325151a3SRui Paulo 		}
1296e28a4053SRui Paulo 		break;
1297e28a4053SRui Paulo 	case EAP_METHOD_RESPONSE:
1298e28a4053SRui Paulo 		/*
1299e28a4053SRui Paulo 		 * Note: Mechanism to allow EAP methods to wait while going
1300e28a4053SRui Paulo 		 * through pending processing is an extension to RFC 4137
1301e28a4053SRui Paulo 		 * which only defines the transits to SELECT_ACTION and
1302e28a4053SRui Paulo 		 * METHOD_REQUEST from this METHOD_RESPONSE state.
1303e28a4053SRui Paulo 		 */
1304e28a4053SRui Paulo 		if (sm->methodState == METHOD_END)
1305e28a4053SRui Paulo 			SM_ENTER(EAP, SELECT_ACTION);
1306e28a4053SRui Paulo 		else if (sm->method_pending == METHOD_PENDING_WAIT) {
1307e28a4053SRui Paulo 			wpa_printf(MSG_DEBUG, "EAP: Method has pending "
1308e28a4053SRui Paulo 				   "processing - wait before proceeding to "
1309e28a4053SRui Paulo 				   "METHOD_REQUEST state");
1310e28a4053SRui Paulo 		} else if (sm->method_pending == METHOD_PENDING_CONT) {
1311e28a4053SRui Paulo 			wpa_printf(MSG_DEBUG, "EAP: Method has completed "
1312e28a4053SRui Paulo 				   "pending processing - reprocess pending "
1313e28a4053SRui Paulo 				   "EAP message");
1314e28a4053SRui Paulo 			sm->method_pending = METHOD_PENDING_NONE;
1315e28a4053SRui Paulo 			SM_ENTER(EAP, METHOD_RESPONSE);
1316e28a4053SRui Paulo 		} else
1317e28a4053SRui Paulo 			SM_ENTER(EAP, METHOD_REQUEST);
1318e28a4053SRui Paulo 		break;
1319e28a4053SRui Paulo 	case EAP_PROPOSE_METHOD:
1320e28a4053SRui Paulo 		/*
1321e28a4053SRui Paulo 		 * Note: Mechanism to allow EAP methods to wait while going
1322e28a4053SRui Paulo 		 * through pending processing is an extension to RFC 4137
1323e28a4053SRui Paulo 		 * which only defines the transit to METHOD_REQUEST from this
1324e28a4053SRui Paulo 		 * PROPOSE_METHOD state.
1325e28a4053SRui Paulo 		 */
1326e28a4053SRui Paulo 		if (sm->method_pending == METHOD_PENDING_WAIT) {
1327e28a4053SRui Paulo 			wpa_printf(MSG_DEBUG, "EAP: Method has pending "
1328e28a4053SRui Paulo 				   "processing - wait before proceeding to "
1329e28a4053SRui Paulo 				   "METHOD_REQUEST state");
1330e28a4053SRui Paulo 			if (sm->user_eap_method_index > 0)
1331e28a4053SRui Paulo 				sm->user_eap_method_index--;
1332e28a4053SRui Paulo 		} else if (sm->method_pending == METHOD_PENDING_CONT) {
1333e28a4053SRui Paulo 			wpa_printf(MSG_DEBUG, "EAP: Method has completed "
1334e28a4053SRui Paulo 				   "pending processing - reprocess pending "
1335e28a4053SRui Paulo 				   "EAP message");
1336e28a4053SRui Paulo 			sm->method_pending = METHOD_PENDING_NONE;
1337e28a4053SRui Paulo 			SM_ENTER(EAP, PROPOSE_METHOD);
1338e28a4053SRui Paulo 		} else
1339e28a4053SRui Paulo 			SM_ENTER(EAP, METHOD_REQUEST);
1340e28a4053SRui Paulo 		break;
1341e28a4053SRui Paulo 	case EAP_NAK:
1342e28a4053SRui Paulo 		SM_ENTER(EAP, SELECT_ACTION);
1343e28a4053SRui Paulo 		break;
1344e28a4053SRui Paulo 	case EAP_SELECT_ACTION:
1345e28a4053SRui Paulo 		if (sm->decision == DECISION_FAILURE)
1346e28a4053SRui Paulo 			SM_ENTER(EAP, FAILURE);
1347e28a4053SRui Paulo 		else if (sm->decision == DECISION_SUCCESS)
1348e28a4053SRui Paulo 			SM_ENTER(EAP, SUCCESS);
1349e28a4053SRui Paulo 		else if (sm->decision == DECISION_PASSTHROUGH)
1350e28a4053SRui Paulo 			SM_ENTER(EAP, INITIALIZE_PASSTHROUGH);
13515b9c547cSRui Paulo 		else if (sm->decision == DECISION_INITIATE_REAUTH_START)
13525b9c547cSRui Paulo 			SM_ENTER(EAP, INITIATE_REAUTH_START);
13535b9c547cSRui Paulo #ifdef CONFIG_ERP
1354*c1d255d3SCy Schubert 		else if (sm->cfg->eap_server && sm->cfg->erp && sm->rxInitiate)
13555b9c547cSRui Paulo 			SM_ENTER(EAP, INITIATE_RECEIVED);
13565b9c547cSRui Paulo #endif /* CONFIG_ERP */
1357e28a4053SRui Paulo 		else
1358e28a4053SRui Paulo 			SM_ENTER(EAP, PROPOSE_METHOD);
1359e28a4053SRui Paulo 		break;
13605b9c547cSRui Paulo 	case EAP_INITIATE_REAUTH_START:
13615b9c547cSRui Paulo 		SM_ENTER(EAP, SEND_REQUEST);
13625b9c547cSRui Paulo 		break;
13635b9c547cSRui Paulo 	case EAP_INITIATE_RECEIVED:
1364*c1d255d3SCy Schubert 		if (!sm->cfg->eap_server)
13655b9c547cSRui Paulo 			SM_ENTER(EAP, SELECT_ACTION);
13665b9c547cSRui Paulo 		break;
1367e28a4053SRui Paulo 	case EAP_TIMEOUT_FAILURE:
1368e28a4053SRui Paulo 		break;
1369e28a4053SRui Paulo 	case EAP_FAILURE:
1370e28a4053SRui Paulo 		break;
1371e28a4053SRui Paulo 	case EAP_SUCCESS:
1372e28a4053SRui Paulo 		break;
1373e28a4053SRui Paulo 
1374e28a4053SRui Paulo 	case EAP_INITIALIZE_PASSTHROUGH:
1375e28a4053SRui Paulo 		if (sm->currentId == -1)
1376e28a4053SRui Paulo 			SM_ENTER(EAP, AAA_IDLE);
1377e28a4053SRui Paulo 		else
1378e28a4053SRui Paulo 			SM_ENTER(EAP, AAA_REQUEST);
1379e28a4053SRui Paulo 		break;
1380e28a4053SRui Paulo 	case EAP_IDLE2:
1381e28a4053SRui Paulo 		if (sm->eap_if.eapResp)
1382e28a4053SRui Paulo 			SM_ENTER(EAP, RECEIVED2);
1383e28a4053SRui Paulo 		else if (sm->eap_if.retransWhile == 0)
1384e28a4053SRui Paulo 			SM_ENTER(EAP, RETRANSMIT2);
1385e28a4053SRui Paulo 		break;
1386e28a4053SRui Paulo 	case EAP_RETRANSMIT2:
1387e28a4053SRui Paulo 		if (sm->retransCount > sm->MaxRetrans)
1388e28a4053SRui Paulo 			SM_ENTER(EAP, TIMEOUT_FAILURE2);
1389e28a4053SRui Paulo 		else
1390e28a4053SRui Paulo 			SM_ENTER(EAP, IDLE2);
1391e28a4053SRui Paulo 		break;
1392e28a4053SRui Paulo 	case EAP_RECEIVED2:
1393e28a4053SRui Paulo 		if (sm->rxResp && (sm->respId == sm->currentId))
1394e28a4053SRui Paulo 			SM_ENTER(EAP, AAA_REQUEST);
1395e28a4053SRui Paulo 		else
1396e28a4053SRui Paulo 			SM_ENTER(EAP, DISCARD2);
1397e28a4053SRui Paulo 		break;
1398e28a4053SRui Paulo 	case EAP_DISCARD2:
1399e28a4053SRui Paulo 		SM_ENTER(EAP, IDLE2);
1400e28a4053SRui Paulo 		break;
1401e28a4053SRui Paulo 	case EAP_SEND_REQUEST2:
1402e28a4053SRui Paulo 		SM_ENTER(EAP, IDLE2);
1403e28a4053SRui Paulo 		break;
1404e28a4053SRui Paulo 	case EAP_AAA_REQUEST:
1405e28a4053SRui Paulo 		SM_ENTER(EAP, AAA_IDLE);
1406e28a4053SRui Paulo 		break;
1407e28a4053SRui Paulo 	case EAP_AAA_RESPONSE:
1408e28a4053SRui Paulo 		SM_ENTER(EAP, SEND_REQUEST2);
1409e28a4053SRui Paulo 		break;
1410e28a4053SRui Paulo 	case EAP_AAA_IDLE:
1411e28a4053SRui Paulo 		if (sm->eap_if.aaaFail)
1412e28a4053SRui Paulo 			SM_ENTER(EAP, FAILURE2);
1413e28a4053SRui Paulo 		else if (sm->eap_if.aaaSuccess)
1414e28a4053SRui Paulo 			SM_ENTER(EAP, SUCCESS2);
1415e28a4053SRui Paulo 		else if (sm->eap_if.aaaEapReq)
1416e28a4053SRui Paulo 			SM_ENTER(EAP, AAA_RESPONSE);
1417e28a4053SRui Paulo 		else if (sm->eap_if.aaaTimeout)
1418e28a4053SRui Paulo 			SM_ENTER(EAP, TIMEOUT_FAILURE2);
1419e28a4053SRui Paulo 		break;
1420e28a4053SRui Paulo 	case EAP_TIMEOUT_FAILURE2:
1421e28a4053SRui Paulo 		break;
1422e28a4053SRui Paulo 	case EAP_FAILURE2:
1423e28a4053SRui Paulo 		break;
1424e28a4053SRui Paulo 	case EAP_SUCCESS2:
1425e28a4053SRui Paulo 		break;
1426e28a4053SRui Paulo 	}
1427e28a4053SRui Paulo }
1428e28a4053SRui Paulo 
1429e28a4053SRui Paulo 
eap_sm_calculateTimeout(struct eap_sm * sm,int retransCount,int eapSRTT,int eapRTTVAR,int methodTimeout)1430e28a4053SRui Paulo static int eap_sm_calculateTimeout(struct eap_sm *sm, int retransCount,
1431e28a4053SRui Paulo 				   int eapSRTT, int eapRTTVAR,
1432e28a4053SRui Paulo 				   int methodTimeout)
1433e28a4053SRui Paulo {
1434e28a4053SRui Paulo 	int rto, i;
1435e28a4053SRui Paulo 
14365b9c547cSRui Paulo 	if (sm->try_initiate_reauth) {
14375b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
14385b9c547cSRui Paulo 			   "EAP: retransmit timeout 1 second for EAP-Initiate-Re-auth-Start");
14395b9c547cSRui Paulo 		return 1;
14405b9c547cSRui Paulo 	}
14415b9c547cSRui Paulo 
1442e28a4053SRui Paulo 	if (methodTimeout) {
1443e28a4053SRui Paulo 		/*
1444e28a4053SRui Paulo 		 * EAP method (either internal or through AAA server, provided
1445e28a4053SRui Paulo 		 * timeout hint. Use that as-is as a timeout for retransmitting
1446e28a4053SRui Paulo 		 * the EAP request if no response is received.
1447e28a4053SRui Paulo 		 */
1448e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP: retransmit timeout %d seconds "
1449e28a4053SRui Paulo 			   "(from EAP method hint)", methodTimeout);
1450e28a4053SRui Paulo 		return methodTimeout;
1451e28a4053SRui Paulo 	}
1452e28a4053SRui Paulo 
1453e28a4053SRui Paulo 	/*
1454e28a4053SRui Paulo 	 * RFC 3748 recommends algorithms described in RFC 2988 for estimation
1455e28a4053SRui Paulo 	 * of the retransmission timeout. This should be implemented once
1456e28a4053SRui Paulo 	 * round-trip time measurements are available. For nowm a simple
1457e28a4053SRui Paulo 	 * backoff mechanism is used instead if there are no EAP method
1458e28a4053SRui Paulo 	 * specific hints.
1459e28a4053SRui Paulo 	 *
1460e28a4053SRui Paulo 	 * SRTT = smoothed round-trip time
1461e28a4053SRui Paulo 	 * RTTVAR = round-trip time variation
1462e28a4053SRui Paulo 	 * RTO = retransmission timeout
1463e28a4053SRui Paulo 	 */
1464e28a4053SRui Paulo 
1465e28a4053SRui Paulo 	/*
1466e28a4053SRui Paulo 	 * RFC 2988, 2.1: before RTT measurement, set RTO to 3 seconds for
1467e28a4053SRui Paulo 	 * initial retransmission and then double the RTO to provide back off
1468e28a4053SRui Paulo 	 * per 5.5. Limit the maximum RTO to 20 seconds per RFC 3748, 4.3
1469e28a4053SRui Paulo 	 * modified RTOmax.
1470e28a4053SRui Paulo 	 */
1471e28a4053SRui Paulo 	rto = 3;
1472e28a4053SRui Paulo 	for (i = 0; i < retransCount; i++) {
1473e28a4053SRui Paulo 		rto *= 2;
1474e28a4053SRui Paulo 		if (rto >= 20) {
1475e28a4053SRui Paulo 			rto = 20;
1476e28a4053SRui Paulo 			break;
1477e28a4053SRui Paulo 		}
1478e28a4053SRui Paulo 	}
1479e28a4053SRui Paulo 
1480e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "EAP: retransmit timeout %d seconds "
1481e28a4053SRui Paulo 		   "(from dynamic back off; retransCount=%d)",
1482e28a4053SRui Paulo 		   rto, retransCount);
1483e28a4053SRui Paulo 
1484e28a4053SRui Paulo 	return rto;
1485e28a4053SRui Paulo }
1486e28a4053SRui Paulo 
1487e28a4053SRui Paulo 
eap_sm_parseEapResp(struct eap_sm * sm,const struct wpabuf * resp)1488e28a4053SRui Paulo static void eap_sm_parseEapResp(struct eap_sm *sm, const struct wpabuf *resp)
1489e28a4053SRui Paulo {
1490e28a4053SRui Paulo 	const struct eap_hdr *hdr;
1491e28a4053SRui Paulo 	size_t plen;
1492e28a4053SRui Paulo 
1493e28a4053SRui Paulo 	/* parse rxResp, respId, respMethod */
1494*c1d255d3SCy Schubert 	sm->rxResp = false;
1495*c1d255d3SCy Schubert 	sm->rxInitiate = false;
1496e28a4053SRui Paulo 	sm->respId = -1;
1497e28a4053SRui Paulo 	sm->respMethod = EAP_TYPE_NONE;
1498e28a4053SRui Paulo 	sm->respVendor = EAP_VENDOR_IETF;
1499e28a4053SRui Paulo 	sm->respVendorMethod = EAP_TYPE_NONE;
1500e28a4053SRui Paulo 
1501e28a4053SRui Paulo 	if (resp == NULL || wpabuf_len(resp) < sizeof(*hdr)) {
1502e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP: parseEapResp: invalid resp=%p "
1503e28a4053SRui Paulo 			   "len=%lu", resp,
1504e28a4053SRui Paulo 			   resp ? (unsigned long) wpabuf_len(resp) : 0);
1505e28a4053SRui Paulo 		return;
1506e28a4053SRui Paulo 	}
1507e28a4053SRui Paulo 
1508e28a4053SRui Paulo 	hdr = wpabuf_head(resp);
1509e28a4053SRui Paulo 	plen = be_to_host16(hdr->length);
1510e28a4053SRui Paulo 	if (plen > wpabuf_len(resp)) {
1511e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP: Ignored truncated EAP-Packet "
1512e28a4053SRui Paulo 			   "(len=%lu plen=%lu)",
1513e28a4053SRui Paulo 			   (unsigned long) wpabuf_len(resp),
1514e28a4053SRui Paulo 			   (unsigned long) plen);
1515e28a4053SRui Paulo 		return;
1516e28a4053SRui Paulo 	}
1517e28a4053SRui Paulo 
1518e28a4053SRui Paulo 	sm->respId = hdr->identifier;
1519e28a4053SRui Paulo 
1520e28a4053SRui Paulo 	if (hdr->code == EAP_CODE_RESPONSE)
1521*c1d255d3SCy Schubert 		sm->rxResp = true;
15225b9c547cSRui Paulo 	else if (hdr->code == EAP_CODE_INITIATE)
1523*c1d255d3SCy Schubert 		sm->rxInitiate = true;
1524e28a4053SRui Paulo 
1525e28a4053SRui Paulo 	if (plen > sizeof(*hdr)) {
1526e28a4053SRui Paulo 		u8 *pos = (u8 *) (hdr + 1);
1527e28a4053SRui Paulo 		sm->respMethod = *pos++;
1528e28a4053SRui Paulo 		if (sm->respMethod == EAP_TYPE_EXPANDED) {
1529e28a4053SRui Paulo 			if (plen < sizeof(*hdr) + 8) {
1530e28a4053SRui Paulo 				wpa_printf(MSG_DEBUG, "EAP: Ignored truncated "
1531e28a4053SRui Paulo 					   "expanded EAP-Packet (plen=%lu)",
1532e28a4053SRui Paulo 					   (unsigned long) plen);
1533e28a4053SRui Paulo 				return;
1534e28a4053SRui Paulo 			}
1535e28a4053SRui Paulo 			sm->respVendor = WPA_GET_BE24(pos);
1536e28a4053SRui Paulo 			pos += 3;
1537e28a4053SRui Paulo 			sm->respVendorMethod = WPA_GET_BE32(pos);
1538e28a4053SRui Paulo 		}
1539e28a4053SRui Paulo 	}
1540e28a4053SRui Paulo 
15415b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG,
15425b9c547cSRui Paulo 		   "EAP: parseEapResp: rxResp=%d rxInitiate=%d respId=%d respMethod=%u respVendor=%u respVendorMethod=%u",
15435b9c547cSRui Paulo 		   sm->rxResp, sm->rxInitiate, sm->respId, sm->respMethod,
15445b9c547cSRui Paulo 		   sm->respVendor, sm->respVendorMethod);
1545e28a4053SRui Paulo }
1546e28a4053SRui Paulo 
1547e28a4053SRui Paulo 
eap_sm_getId(const struct wpabuf * data)1548e28a4053SRui Paulo static int eap_sm_getId(const struct wpabuf *data)
1549e28a4053SRui Paulo {
1550e28a4053SRui Paulo 	const struct eap_hdr *hdr;
1551e28a4053SRui Paulo 
1552e28a4053SRui Paulo 	if (data == NULL || wpabuf_len(data) < sizeof(*hdr))
1553e28a4053SRui Paulo 		return -1;
1554e28a4053SRui Paulo 
1555e28a4053SRui Paulo 	hdr = wpabuf_head(data);
1556e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "EAP: getId: id=%d", hdr->identifier);
1557e28a4053SRui Paulo 	return hdr->identifier;
1558e28a4053SRui Paulo }
1559e28a4053SRui Paulo 
1560e28a4053SRui Paulo 
eap_sm_buildSuccess(struct eap_sm * sm,u8 id)1561e28a4053SRui Paulo static struct wpabuf * eap_sm_buildSuccess(struct eap_sm *sm, u8 id)
1562e28a4053SRui Paulo {
1563e28a4053SRui Paulo 	struct wpabuf *msg;
1564e28a4053SRui Paulo 	struct eap_hdr *resp;
1565e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "EAP: Building EAP-Success (id=%d)", id);
1566e28a4053SRui Paulo 
1567e28a4053SRui Paulo 	msg = wpabuf_alloc(sizeof(*resp));
1568e28a4053SRui Paulo 	if (msg == NULL)
1569e28a4053SRui Paulo 		return NULL;
1570e28a4053SRui Paulo 	resp = wpabuf_put(msg, sizeof(*resp));
1571e28a4053SRui Paulo 	resp->code = EAP_CODE_SUCCESS;
1572e28a4053SRui Paulo 	resp->identifier = id;
1573e28a4053SRui Paulo 	resp->length = host_to_be16(sizeof(*resp));
1574e28a4053SRui Paulo 
1575e28a4053SRui Paulo 	return msg;
1576e28a4053SRui Paulo }
1577e28a4053SRui Paulo 
1578e28a4053SRui Paulo 
eap_sm_buildFailure(struct eap_sm * sm,u8 id)1579e28a4053SRui Paulo static struct wpabuf * eap_sm_buildFailure(struct eap_sm *sm, u8 id)
1580e28a4053SRui Paulo {
1581e28a4053SRui Paulo 	struct wpabuf *msg;
1582e28a4053SRui Paulo 	struct eap_hdr *resp;
1583e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "EAP: Building EAP-Failure (id=%d)", id);
1584e28a4053SRui Paulo 
1585e28a4053SRui Paulo 	msg = wpabuf_alloc(sizeof(*resp));
1586e28a4053SRui Paulo 	if (msg == NULL)
1587e28a4053SRui Paulo 		return NULL;
1588e28a4053SRui Paulo 	resp = wpabuf_put(msg, sizeof(*resp));
1589e28a4053SRui Paulo 	resp->code = EAP_CODE_FAILURE;
1590e28a4053SRui Paulo 	resp->identifier = id;
1591e28a4053SRui Paulo 	resp->length = host_to_be16(sizeof(*resp));
1592e28a4053SRui Paulo 
1593e28a4053SRui Paulo 	return msg;
1594e28a4053SRui Paulo }
1595e28a4053SRui Paulo 
1596e28a4053SRui Paulo 
eap_sm_nextId(struct eap_sm * sm,int id)1597e28a4053SRui Paulo static int eap_sm_nextId(struct eap_sm *sm, int id)
1598e28a4053SRui Paulo {
1599e28a4053SRui Paulo 	if (id < 0) {
1600e28a4053SRui Paulo 		/* RFC 3748 Ch 4.1: recommended to initialize Identifier with a
1601e28a4053SRui Paulo 		 * random number */
1602e28a4053SRui Paulo 		id = rand() & 0xff;
1603e28a4053SRui Paulo 		if (id != sm->lastId)
1604e28a4053SRui Paulo 			return id;
1605e28a4053SRui Paulo 	}
1606e28a4053SRui Paulo 	return (id + 1) & 0xff;
1607e28a4053SRui Paulo }
1608e28a4053SRui Paulo 
1609e28a4053SRui Paulo 
1610e28a4053SRui Paulo /**
1611e28a4053SRui Paulo  * eap_sm_process_nak - Process EAP-Response/Nak
1612e28a4053SRui Paulo  * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
1613e28a4053SRui Paulo  * @nak_list: Nak list (allowed methods) from the supplicant
1614e28a4053SRui Paulo  * @len: Length of nak_list in bytes
1615e28a4053SRui Paulo  *
1616e28a4053SRui Paulo  * This function is called when EAP-Response/Nak is received from the
1617e28a4053SRui Paulo  * supplicant. This can happen for both phase 1 and phase 2 authentications.
1618e28a4053SRui Paulo  */
eap_sm_process_nak(struct eap_sm * sm,const u8 * nak_list,size_t len)1619e28a4053SRui Paulo void eap_sm_process_nak(struct eap_sm *sm, const u8 *nak_list, size_t len)
1620e28a4053SRui Paulo {
1621e28a4053SRui Paulo 	int i;
1622e28a4053SRui Paulo 	size_t j;
1623e28a4053SRui Paulo 
1624e28a4053SRui Paulo 	if (sm->user == NULL)
1625e28a4053SRui Paulo 		return;
1626e28a4053SRui Paulo 
1627e28a4053SRui Paulo 	wpa_printf(MSG_MSGDUMP, "EAP: processing NAK (current EAP method "
1628e28a4053SRui Paulo 		   "index %d)", sm->user_eap_method_index);
1629e28a4053SRui Paulo 
1630e28a4053SRui Paulo 	wpa_hexdump(MSG_MSGDUMP, "EAP: configured methods",
1631e28a4053SRui Paulo 		    (u8 *) sm->user->methods,
1632e28a4053SRui Paulo 		    EAP_MAX_METHODS * sizeof(sm->user->methods[0]));
1633e28a4053SRui Paulo 	wpa_hexdump(MSG_MSGDUMP, "EAP: list of methods supported by the peer",
1634e28a4053SRui Paulo 		    nak_list, len);
1635e28a4053SRui Paulo 
1636e28a4053SRui Paulo 	i = sm->user_eap_method_index;
1637e28a4053SRui Paulo 	while (i < EAP_MAX_METHODS &&
1638e28a4053SRui Paulo 	       (sm->user->methods[i].vendor != EAP_VENDOR_IETF ||
1639e28a4053SRui Paulo 		sm->user->methods[i].method != EAP_TYPE_NONE)) {
1640e28a4053SRui Paulo 		if (sm->user->methods[i].vendor != EAP_VENDOR_IETF)
1641e28a4053SRui Paulo 			goto not_found;
1642e28a4053SRui Paulo 		for (j = 0; j < len; j++) {
1643e28a4053SRui Paulo 			if (nak_list[j] == sm->user->methods[i].method) {
1644e28a4053SRui Paulo 				break;
1645e28a4053SRui Paulo 			}
1646e28a4053SRui Paulo 		}
1647e28a4053SRui Paulo 
1648e28a4053SRui Paulo 		if (j < len) {
1649e28a4053SRui Paulo 			/* found */
1650e28a4053SRui Paulo 			i++;
1651e28a4053SRui Paulo 			continue;
1652e28a4053SRui Paulo 		}
1653e28a4053SRui Paulo 
1654e28a4053SRui Paulo 	not_found:
1655e28a4053SRui Paulo 		/* not found - remove from the list */
1656f05cddf9SRui Paulo 		if (i + 1 < EAP_MAX_METHODS) {
1657f05cddf9SRui Paulo 			os_memmove(&sm->user->methods[i],
1658f05cddf9SRui Paulo 				   &sm->user->methods[i + 1],
1659e28a4053SRui Paulo 				   (EAP_MAX_METHODS - i - 1) *
1660e28a4053SRui Paulo 				   sizeof(sm->user->methods[0]));
1661f05cddf9SRui Paulo 		}
1662e28a4053SRui Paulo 		sm->user->methods[EAP_MAX_METHODS - 1].vendor =
1663e28a4053SRui Paulo 			EAP_VENDOR_IETF;
1664e28a4053SRui Paulo 		sm->user->methods[EAP_MAX_METHODS - 1].method = EAP_TYPE_NONE;
1665e28a4053SRui Paulo 	}
1666e28a4053SRui Paulo 
1667e28a4053SRui Paulo 	wpa_hexdump(MSG_MSGDUMP, "EAP: new list of configured methods",
1668e28a4053SRui Paulo 		    (u8 *) sm->user->methods, EAP_MAX_METHODS *
1669e28a4053SRui Paulo 		    sizeof(sm->user->methods[0]));
1670e28a4053SRui Paulo }
1671e28a4053SRui Paulo 
1672e28a4053SRui Paulo 
eap_sm_Policy_update(struct eap_sm * sm,const u8 * nak_list,size_t len)1673e28a4053SRui Paulo static void eap_sm_Policy_update(struct eap_sm *sm, const u8 *nak_list,
1674e28a4053SRui Paulo 				 size_t len)
1675e28a4053SRui Paulo {
1676e28a4053SRui Paulo 	if (nak_list == NULL || sm == NULL || sm->user == NULL)
1677e28a4053SRui Paulo 		return;
1678e28a4053SRui Paulo 
1679e28a4053SRui Paulo 	if (sm->user->phase2) {
1680e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP: EAP-Nak received after Phase2 user"
1681e28a4053SRui Paulo 			   " info was selected - reject");
1682e28a4053SRui Paulo 		sm->decision = DECISION_FAILURE;
1683e28a4053SRui Paulo 		return;
1684e28a4053SRui Paulo 	}
1685e28a4053SRui Paulo 
1686e28a4053SRui Paulo 	eap_sm_process_nak(sm, nak_list, len);
1687e28a4053SRui Paulo }
1688e28a4053SRui Paulo 
1689e28a4053SRui Paulo 
eap_sm_Policy_getNextMethod(struct eap_sm * sm,int * vendor)1690*c1d255d3SCy Schubert static enum eap_type eap_sm_Policy_getNextMethod(struct eap_sm *sm, int *vendor)
1691e28a4053SRui Paulo {
1692*c1d255d3SCy Schubert 	enum eap_type next;
1693e28a4053SRui Paulo 	int idx = sm->user_eap_method_index;
1694e28a4053SRui Paulo 
1695e28a4053SRui Paulo 	/* In theory, there should be no problems with starting
1696e28a4053SRui Paulo 	 * re-authentication with something else than EAP-Request/Identity and
1697e28a4053SRui Paulo 	 * this does indeed work with wpa_supplicant. However, at least Funk
1698e28a4053SRui Paulo 	 * Supplicant seemed to ignore re-auth if it skipped
1699e28a4053SRui Paulo 	 * EAP-Request/Identity.
1700e28a4053SRui Paulo 	 * Re-auth sets currentId == -1, so that can be used here to select
1701e28a4053SRui Paulo 	 * whether Identity needs to be requested again. */
1702e28a4053SRui Paulo 	if (sm->identity == NULL || sm->currentId == -1) {
1703e28a4053SRui Paulo 		*vendor = EAP_VENDOR_IETF;
1704e28a4053SRui Paulo 		next = EAP_TYPE_IDENTITY;
1705*c1d255d3SCy Schubert 		sm->update_user = true;
1706e28a4053SRui Paulo 	} else if (sm->user && idx < EAP_MAX_METHODS &&
1707e28a4053SRui Paulo 		   (sm->user->methods[idx].vendor != EAP_VENDOR_IETF ||
1708e28a4053SRui Paulo 		    sm->user->methods[idx].method != EAP_TYPE_NONE)) {
1709e28a4053SRui Paulo 		*vendor = sm->user->methods[idx].vendor;
1710e28a4053SRui Paulo 		next = sm->user->methods[idx].method;
1711e28a4053SRui Paulo 		sm->user_eap_method_index++;
1712e28a4053SRui Paulo 	} else {
1713e28a4053SRui Paulo 		*vendor = EAP_VENDOR_IETF;
1714e28a4053SRui Paulo 		next = EAP_TYPE_NONE;
1715e28a4053SRui Paulo 	}
1716e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "EAP: getNextMethod: vendor %d type %d",
1717e28a4053SRui Paulo 		   *vendor, next);
1718e28a4053SRui Paulo 	return next;
1719e28a4053SRui Paulo }
1720e28a4053SRui Paulo 
1721e28a4053SRui Paulo 
eap_sm_Policy_getDecision(struct eap_sm * sm)1722e28a4053SRui Paulo static int eap_sm_Policy_getDecision(struct eap_sm *sm)
1723e28a4053SRui Paulo {
1724*c1d255d3SCy Schubert 	if (!sm->cfg->eap_server && sm->identity && !sm->start_reauth) {
1725e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP: getDecision: -> PASSTHROUGH");
1726e28a4053SRui Paulo 		return DECISION_PASSTHROUGH;
1727e28a4053SRui Paulo 	}
1728e28a4053SRui Paulo 
1729e28a4053SRui Paulo 	if (sm->m && sm->currentMethod != EAP_TYPE_IDENTITY &&
1730e28a4053SRui Paulo 	    sm->m->isSuccess(sm, sm->eap_method_priv)) {
1731e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP: getDecision: method succeeded -> "
1732e28a4053SRui Paulo 			   "SUCCESS");
1733*c1d255d3SCy Schubert 		sm->update_user = true;
1734e28a4053SRui Paulo 		return DECISION_SUCCESS;
1735e28a4053SRui Paulo 	}
1736e28a4053SRui Paulo 
1737e28a4053SRui Paulo 	if (sm->m && sm->m->isDone(sm, sm->eap_method_priv) &&
1738e28a4053SRui Paulo 	    !sm->m->isSuccess(sm, sm->eap_method_priv)) {
1739e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP: getDecision: method failed -> "
1740e28a4053SRui Paulo 			   "FAILURE");
1741*c1d255d3SCy Schubert 		sm->update_user = true;
1742e28a4053SRui Paulo 		return DECISION_FAILURE;
1743e28a4053SRui Paulo 	}
1744e28a4053SRui Paulo 
1745e28a4053SRui Paulo 	if ((sm->user == NULL || sm->update_user) && sm->identity &&
1746e28a4053SRui Paulo 	    !sm->start_reauth) {
1747e28a4053SRui Paulo 		/*
1748e28a4053SRui Paulo 		 * Allow Identity method to be started once to allow identity
1749e28a4053SRui Paulo 		 * selection hint to be sent from the authentication server,
1750e28a4053SRui Paulo 		 * but prevent a loop of Identity requests by only allowing
1751e28a4053SRui Paulo 		 * this to happen once.
1752e28a4053SRui Paulo 		 */
1753e28a4053SRui Paulo 		int id_req = 0;
1754e28a4053SRui Paulo 		if (sm->user && sm->currentMethod == EAP_TYPE_IDENTITY &&
1755e28a4053SRui Paulo 		    sm->user->methods[0].vendor == EAP_VENDOR_IETF &&
1756e28a4053SRui Paulo 		    sm->user->methods[0].method == EAP_TYPE_IDENTITY)
1757e28a4053SRui Paulo 			id_req = 1;
1758e28a4053SRui Paulo 		if (eap_user_get(sm, sm->identity, sm->identity_len, 0) != 0) {
1759e28a4053SRui Paulo 			wpa_printf(MSG_DEBUG, "EAP: getDecision: user not "
1760e28a4053SRui Paulo 				   "found from database -> FAILURE");
1761e28a4053SRui Paulo 			return DECISION_FAILURE;
1762e28a4053SRui Paulo 		}
1763e28a4053SRui Paulo 		if (id_req && sm->user &&
1764e28a4053SRui Paulo 		    sm->user->methods[0].vendor == EAP_VENDOR_IETF &&
1765e28a4053SRui Paulo 		    sm->user->methods[0].method == EAP_TYPE_IDENTITY) {
1766e28a4053SRui Paulo 			wpa_printf(MSG_DEBUG, "EAP: getDecision: stop "
1767e28a4053SRui Paulo 				   "identity request loop -> FAILURE");
1768*c1d255d3SCy Schubert 			sm->update_user = true;
1769e28a4053SRui Paulo 			return DECISION_FAILURE;
1770e28a4053SRui Paulo 		}
1771*c1d255d3SCy Schubert 		sm->update_user = false;
1772e28a4053SRui Paulo 	}
1773*c1d255d3SCy Schubert 	sm->start_reauth = false;
1774e28a4053SRui Paulo 
1775e28a4053SRui Paulo 	if (sm->user && sm->user_eap_method_index < EAP_MAX_METHODS &&
1776e28a4053SRui Paulo 	    (sm->user->methods[sm->user_eap_method_index].vendor !=
1777e28a4053SRui Paulo 	     EAP_VENDOR_IETF ||
1778e28a4053SRui Paulo 	     sm->user->methods[sm->user_eap_method_index].method !=
1779e28a4053SRui Paulo 	     EAP_TYPE_NONE)) {
1780e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP: getDecision: another method "
1781e28a4053SRui Paulo 			   "available -> CONTINUE");
1782e28a4053SRui Paulo 		return DECISION_CONTINUE;
1783e28a4053SRui Paulo 	}
1784e28a4053SRui Paulo 
17855b9c547cSRui Paulo 	if (!sm->identity && eap_get_erp_send_reauth_start(sm) &&
17865b9c547cSRui Paulo 	    !sm->initiate_reauth_start_sent) {
17875b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
17885b9c547cSRui Paulo 			   "EAP: getDecision: send EAP-Initiate/Re-auth-Start");
17895b9c547cSRui Paulo 		return DECISION_INITIATE_REAUTH_START;
17905b9c547cSRui Paulo 	}
17915b9c547cSRui Paulo 
1792e28a4053SRui Paulo 	if (sm->identity == NULL || sm->currentId == -1) {
1793e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP: getDecision: no identity known "
1794e28a4053SRui Paulo 			   "yet -> CONTINUE");
1795e28a4053SRui Paulo 		return DECISION_CONTINUE;
1796e28a4053SRui Paulo 	}
1797e28a4053SRui Paulo 
1798e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "EAP: getDecision: no more methods available -> "
1799e28a4053SRui Paulo 		   "FAILURE");
1800e28a4053SRui Paulo 	return DECISION_FAILURE;
1801e28a4053SRui Paulo }
1802e28a4053SRui Paulo 
1803e28a4053SRui Paulo 
eap_sm_Policy_doPickUp(struct eap_sm * sm,enum eap_type method)1804*c1d255d3SCy Schubert static bool eap_sm_Policy_doPickUp(struct eap_sm *sm, enum eap_type method)
1805e28a4053SRui Paulo {
1806*c1d255d3SCy Schubert 	return method == EAP_TYPE_IDENTITY;
1807e28a4053SRui Paulo }
1808e28a4053SRui Paulo 
1809e28a4053SRui Paulo 
1810e28a4053SRui Paulo /**
1811e28a4053SRui Paulo  * eap_server_sm_step - Step EAP server state machine
1812e28a4053SRui Paulo  * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
1813e28a4053SRui Paulo  * Returns: 1 if EAP state was changed or 0 if not
1814e28a4053SRui Paulo  *
1815e28a4053SRui Paulo  * This function advances EAP state machine to a new state to match with the
1816e28a4053SRui Paulo  * current variables. This should be called whenever variables used by the EAP
1817e28a4053SRui Paulo  * state machine have changed.
1818e28a4053SRui Paulo  */
eap_server_sm_step(struct eap_sm * sm)1819e28a4053SRui Paulo int eap_server_sm_step(struct eap_sm *sm)
1820e28a4053SRui Paulo {
1821e28a4053SRui Paulo 	int res = 0;
1822e28a4053SRui Paulo 	do {
1823*c1d255d3SCy Schubert 		sm->changed = false;
1824e28a4053SRui Paulo 		SM_STEP_RUN(EAP);
1825e28a4053SRui Paulo 		if (sm->changed)
1826e28a4053SRui Paulo 			res = 1;
1827e28a4053SRui Paulo 	} while (sm->changed);
1828e28a4053SRui Paulo 	return res;
1829e28a4053SRui Paulo }
1830e28a4053SRui Paulo 
1831e28a4053SRui Paulo 
eap_user_free(struct eap_user * user)18324bc52338SCy Schubert void eap_user_free(struct eap_user *user)
1833e28a4053SRui Paulo {
1834e28a4053SRui Paulo 	if (user == NULL)
1835e28a4053SRui Paulo 		return;
18365b9c547cSRui Paulo 	bin_clear_free(user->password, user->password_len);
1837e28a4053SRui Paulo 	user->password = NULL;
183885732ac8SCy Schubert 	bin_clear_free(user->salt, user->salt_len);
183985732ac8SCy Schubert 	user->salt = NULL;
1840e28a4053SRui Paulo 	os_free(user);
1841e28a4053SRui Paulo }
1842e28a4053SRui Paulo 
1843e28a4053SRui Paulo 
1844e28a4053SRui Paulo /**
1845e28a4053SRui Paulo  * eap_server_sm_init - Allocate and initialize EAP server state machine
1846e28a4053SRui Paulo  * @eapol_ctx: Context data to be used with eapol_cb calls
1847e28a4053SRui Paulo  * @eapol_cb: Pointer to EAPOL callback functions
1848e28a4053SRui Paulo  * @conf: EAP configuration
1849e28a4053SRui Paulo  * Returns: Pointer to the allocated EAP state machine or %NULL on failure
1850e28a4053SRui Paulo  *
1851e28a4053SRui Paulo  * This function allocates and initializes an EAP state machine.
1852e28a4053SRui Paulo  */
eap_server_sm_init(void * eapol_ctx,const struct eapol_callbacks * eapol_cb,const struct eap_config * conf,const struct eap_session_data * sess)1853e28a4053SRui Paulo struct eap_sm * eap_server_sm_init(void *eapol_ctx,
1854325151a3SRui Paulo 				   const struct eapol_callbacks *eapol_cb,
1855*c1d255d3SCy Schubert 				   const struct eap_config *conf,
1856*c1d255d3SCy Schubert 				   const struct eap_session_data *sess)
1857e28a4053SRui Paulo {
1858e28a4053SRui Paulo 	struct eap_sm *sm;
1859e28a4053SRui Paulo 
1860e28a4053SRui Paulo 	sm = os_zalloc(sizeof(*sm));
1861e28a4053SRui Paulo 	if (sm == NULL)
1862e28a4053SRui Paulo 		return NULL;
1863e28a4053SRui Paulo 	sm->eapol_ctx = eapol_ctx;
1864e28a4053SRui Paulo 	sm->eapol_cb = eapol_cb;
1865e28a4053SRui Paulo 	sm->MaxRetrans = 5; /* RFC 3748: max 3-5 retransmissions suggested */
1866*c1d255d3SCy Schubert 	sm->cfg = conf;
1867*c1d255d3SCy Schubert 	if (sess->assoc_wps_ie)
1868*c1d255d3SCy Schubert 		sm->assoc_wps_ie = wpabuf_dup(sess->assoc_wps_ie);
1869*c1d255d3SCy Schubert 	if (sess->assoc_p2p_ie)
1870*c1d255d3SCy Schubert 		sm->assoc_p2p_ie = wpabuf_dup(sess->assoc_p2p_ie);
1871*c1d255d3SCy Schubert 	if (sess->peer_addr)
1872*c1d255d3SCy Schubert 		os_memcpy(sm->peer_addr, sess->peer_addr, ETH_ALEN);
18735b9c547cSRui Paulo #ifdef CONFIG_TESTING_OPTIONS
1874*c1d255d3SCy Schubert 	sm->tls_test_flags = sess->tls_test_flags;
18755b9c547cSRui Paulo #endif /* CONFIG_TESTING_OPTIONS */
1876e28a4053SRui Paulo 
1877e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "EAP: Server state machine created");
1878e28a4053SRui Paulo 
1879e28a4053SRui Paulo 	return sm;
1880e28a4053SRui Paulo }
1881e28a4053SRui Paulo 
1882e28a4053SRui Paulo 
1883e28a4053SRui Paulo /**
1884e28a4053SRui Paulo  * eap_server_sm_deinit - Deinitialize and free an EAP server state machine
1885e28a4053SRui Paulo  * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
1886e28a4053SRui Paulo  *
1887e28a4053SRui Paulo  * This function deinitializes EAP state machine and frees all allocated
1888e28a4053SRui Paulo  * resources.
1889e28a4053SRui Paulo  */
eap_server_sm_deinit(struct eap_sm * sm)1890e28a4053SRui Paulo void eap_server_sm_deinit(struct eap_sm *sm)
1891e28a4053SRui Paulo {
1892e28a4053SRui Paulo 	if (sm == NULL)
1893e28a4053SRui Paulo 		return;
1894e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "EAP: Server state machine removed");
1895e28a4053SRui Paulo 	if (sm->m && sm->eap_method_priv)
1896e28a4053SRui Paulo 		sm->m->reset(sm, sm->eap_method_priv);
1897e28a4053SRui Paulo 	wpabuf_free(sm->eap_if.eapReqData);
18985b9c547cSRui Paulo 	bin_clear_free(sm->eap_if.eapKeyData, sm->eap_if.eapKeyDataLen);
18995b9c547cSRui Paulo 	os_free(sm->eap_if.eapSessionId);
1900e28a4053SRui Paulo 	wpabuf_free(sm->lastReqData);
1901e28a4053SRui Paulo 	wpabuf_free(sm->eap_if.eapRespData);
1902e28a4053SRui Paulo 	os_free(sm->identity);
190385732ac8SCy Schubert 	os_free(sm->serial_num);
1904e28a4053SRui Paulo 	wpabuf_free(sm->eap_if.aaaEapReqData);
1905e28a4053SRui Paulo 	wpabuf_free(sm->eap_if.aaaEapRespData);
19065b9c547cSRui Paulo 	bin_clear_free(sm->eap_if.aaaEapKeyData, sm->eap_if.aaaEapKeyDataLen);
1907e28a4053SRui Paulo 	eap_user_free(sm->user);
1908e28a4053SRui Paulo 	wpabuf_free(sm->assoc_wps_ie);
1909f05cddf9SRui Paulo 	wpabuf_free(sm->assoc_p2p_ie);
1910e28a4053SRui Paulo 	os_free(sm);
1911e28a4053SRui Paulo }
1912e28a4053SRui Paulo 
1913e28a4053SRui Paulo 
1914e28a4053SRui Paulo /**
1915e28a4053SRui Paulo  * eap_sm_notify_cached - Notify EAP state machine of cached PMK
1916e28a4053SRui Paulo  * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
1917e28a4053SRui Paulo  *
1918e28a4053SRui Paulo  * This function is called when PMKSA caching is used to skip EAP
1919e28a4053SRui Paulo  * authentication.
1920e28a4053SRui Paulo  */
eap_sm_notify_cached(struct eap_sm * sm)1921e28a4053SRui Paulo void eap_sm_notify_cached(struct eap_sm *sm)
1922e28a4053SRui Paulo {
1923e28a4053SRui Paulo 	if (sm == NULL)
1924e28a4053SRui Paulo 		return;
1925e28a4053SRui Paulo 
1926e28a4053SRui Paulo 	sm->EAP_state = EAP_SUCCESS;
1927e28a4053SRui Paulo }
1928e28a4053SRui Paulo 
1929e28a4053SRui Paulo 
1930e28a4053SRui Paulo /**
1931e28a4053SRui Paulo  * eap_sm_pending_cb - EAP state machine callback for a pending EAP request
1932e28a4053SRui Paulo  * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
1933e28a4053SRui Paulo  *
1934e28a4053SRui Paulo  * This function is called when data for a pending EAP-Request is received.
1935e28a4053SRui Paulo  */
eap_sm_pending_cb(struct eap_sm * sm)1936e28a4053SRui Paulo void eap_sm_pending_cb(struct eap_sm *sm)
1937e28a4053SRui Paulo {
1938e28a4053SRui Paulo 	if (sm == NULL)
1939e28a4053SRui Paulo 		return;
1940e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "EAP: Callback for pending request received");
1941e28a4053SRui Paulo 	if (sm->method_pending == METHOD_PENDING_WAIT)
1942e28a4053SRui Paulo 		sm->method_pending = METHOD_PENDING_CONT;
1943e28a4053SRui Paulo }
1944e28a4053SRui Paulo 
1945e28a4053SRui Paulo 
1946e28a4053SRui Paulo /**
1947e28a4053SRui Paulo  * eap_sm_method_pending - Query whether EAP method is waiting for pending data
1948e28a4053SRui Paulo  * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
1949e28a4053SRui Paulo  * Returns: 1 if method is waiting for pending data or 0 if not
1950e28a4053SRui Paulo  */
eap_sm_method_pending(struct eap_sm * sm)1951e28a4053SRui Paulo int eap_sm_method_pending(struct eap_sm *sm)
1952e28a4053SRui Paulo {
1953e28a4053SRui Paulo 	if (sm == NULL)
1954e28a4053SRui Paulo 		return 0;
1955e28a4053SRui Paulo 	return sm->method_pending == METHOD_PENDING_WAIT;
1956e28a4053SRui Paulo }
1957e28a4053SRui Paulo 
1958e28a4053SRui Paulo 
1959e28a4053SRui Paulo /**
1960e28a4053SRui Paulo  * eap_get_identity - Get the user identity (from EAP-Response/Identity)
1961e28a4053SRui Paulo  * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
1962e28a4053SRui Paulo  * @len: Buffer for returning identity length
1963e28a4053SRui Paulo  * Returns: Pointer to the user identity or %NULL if not available
1964e28a4053SRui Paulo  */
eap_get_identity(struct eap_sm * sm,size_t * len)1965e28a4053SRui Paulo const u8 * eap_get_identity(struct eap_sm *sm, size_t *len)
1966e28a4053SRui Paulo {
1967e28a4053SRui Paulo 	*len = sm->identity_len;
1968e28a4053SRui Paulo 	return sm->identity;
1969e28a4053SRui Paulo }
1970e28a4053SRui Paulo 
1971e28a4053SRui Paulo 
1972e28a4053SRui Paulo /**
197385732ac8SCy Schubert  * eap_get_serial_num - Get the serial number of user certificate
197485732ac8SCy Schubert  * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
197585732ac8SCy Schubert  * Returns: Pointer to the serial number or %NULL if not available
197685732ac8SCy Schubert  */
eap_get_serial_num(struct eap_sm * sm)197785732ac8SCy Schubert const char * eap_get_serial_num(struct eap_sm *sm)
197885732ac8SCy Schubert {
197985732ac8SCy Schubert 	return sm->serial_num;
198085732ac8SCy Schubert }
198185732ac8SCy Schubert 
198285732ac8SCy Schubert 
19834bc52338SCy Schubert /**
19844bc52338SCy Schubert  * eap_get_method - Get the used EAP method
19854bc52338SCy Schubert  * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
19864bc52338SCy Schubert  * Returns: Pointer to the method name or %NULL if not available
19874bc52338SCy Schubert  */
eap_get_method(struct eap_sm * sm)19884bc52338SCy Schubert const char * eap_get_method(struct eap_sm *sm)
19894bc52338SCy Schubert {
19904bc52338SCy Schubert 	if (!sm || !sm->m)
19914bc52338SCy Schubert 		return NULL;
19924bc52338SCy Schubert 	return sm->m->name;
19934bc52338SCy Schubert }
19944bc52338SCy Schubert 
19954bc52338SCy Schubert 
19964bc52338SCy Schubert /**
19974bc52338SCy Schubert  * eap_get_imsi - Get IMSI of the user
19984bc52338SCy Schubert  * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
19994bc52338SCy Schubert  * Returns: Pointer to IMSI or %NULL if not available
20004bc52338SCy Schubert  */
eap_get_imsi(struct eap_sm * sm)20014bc52338SCy Schubert const char * eap_get_imsi(struct eap_sm *sm)
20024bc52338SCy Schubert {
20034bc52338SCy Schubert 	if (!sm || sm->imsi[0] == '\0')
20044bc52338SCy Schubert 		return NULL;
20054bc52338SCy Schubert 	return sm->imsi;
20064bc52338SCy Schubert }
20074bc52338SCy Schubert 
20084bc52338SCy Schubert 
eap_erp_update_identity(struct eap_sm * sm,const u8 * eap,size_t len)200985732ac8SCy Schubert void eap_erp_update_identity(struct eap_sm *sm, const u8 *eap, size_t len)
201085732ac8SCy Schubert {
201185732ac8SCy Schubert #ifdef CONFIG_ERP
201285732ac8SCy Schubert 	const struct eap_hdr *hdr;
201385732ac8SCy Schubert 	const u8 *pos, *end;
201485732ac8SCy Schubert 	struct erp_tlvs parse;
201585732ac8SCy Schubert 
201685732ac8SCy Schubert 	if (len < sizeof(*hdr) + 1)
201785732ac8SCy Schubert 		return;
201885732ac8SCy Schubert 	hdr = (const struct eap_hdr *) eap;
201985732ac8SCy Schubert 	end = eap + len;
202085732ac8SCy Schubert 	pos = (const u8 *) (hdr + 1);
202185732ac8SCy Schubert 	if (hdr->code != EAP_CODE_INITIATE || *pos != EAP_ERP_TYPE_REAUTH)
202285732ac8SCy Schubert 		return;
202385732ac8SCy Schubert 	pos++;
202485732ac8SCy Schubert 	if (pos + 3 > end)
202585732ac8SCy Schubert 		return;
202685732ac8SCy Schubert 
202785732ac8SCy Schubert 	/* Skip Flags and SEQ */
202885732ac8SCy Schubert 	pos += 3;
202985732ac8SCy Schubert 
203085732ac8SCy Schubert 	if (erp_parse_tlvs(pos, end, &parse, 1) < 0 || !parse.keyname)
203185732ac8SCy Schubert 		return;
203285732ac8SCy Schubert 	wpa_hexdump_ascii(MSG_DEBUG,
203385732ac8SCy Schubert 			  "EAP: Update identity based on EAP-Initiate/Re-auth keyName-NAI",
203485732ac8SCy Schubert 			  parse.keyname, parse.keyname_len);
203585732ac8SCy Schubert 	os_free(sm->identity);
203685732ac8SCy Schubert 	sm->identity = os_malloc(parse.keyname_len);
203785732ac8SCy Schubert 	if (sm->identity) {
203885732ac8SCy Schubert 		os_memcpy(sm->identity, parse.keyname, parse.keyname_len);
203985732ac8SCy Schubert 		sm->identity_len = parse.keyname_len;
204085732ac8SCy Schubert 	} else {
204185732ac8SCy Schubert 		sm->identity_len = 0;
204285732ac8SCy Schubert 	}
204385732ac8SCy Schubert #endif /* CONFIG_ERP */
204485732ac8SCy Schubert }
204585732ac8SCy Schubert 
204685732ac8SCy Schubert 
204785732ac8SCy Schubert /**
2048e28a4053SRui Paulo  * eap_get_interface - Get pointer to EAP-EAPOL interface data
2049e28a4053SRui Paulo  * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
2050e28a4053SRui Paulo  * Returns: Pointer to the EAP-EAPOL interface data
2051e28a4053SRui Paulo  */
eap_get_interface(struct eap_sm * sm)2052e28a4053SRui Paulo struct eap_eapol_interface * eap_get_interface(struct eap_sm *sm)
2053e28a4053SRui Paulo {
2054e28a4053SRui Paulo 	return &sm->eap_if;
2055e28a4053SRui Paulo }
2056f05cddf9SRui Paulo 
2057f05cddf9SRui Paulo 
2058f05cddf9SRui Paulo /**
2059f05cddf9SRui Paulo  * eap_server_clear_identity - Clear EAP identity information
2060f05cddf9SRui Paulo  * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
2061f05cddf9SRui Paulo  *
2062f05cddf9SRui Paulo  * This function can be used to clear the EAP identity information in the EAP
2063f05cddf9SRui Paulo  * server context. This allows the EAP/Identity method to be used again after
2064f05cddf9SRui Paulo  * EAPOL-Start or EAPOL-Logoff.
2065f05cddf9SRui Paulo  */
eap_server_clear_identity(struct eap_sm * sm)2066f05cddf9SRui Paulo void eap_server_clear_identity(struct eap_sm *sm)
2067f05cddf9SRui Paulo {
2068f05cddf9SRui Paulo 	os_free(sm->identity);
2069f05cddf9SRui Paulo 	sm->identity = NULL;
2070f05cddf9SRui Paulo }
2071325151a3SRui Paulo 
2072325151a3SRui Paulo 
2073325151a3SRui Paulo #ifdef CONFIG_TESTING_OPTIONS
eap_server_mschap_rx_callback(struct eap_sm * sm,const char * source,const u8 * username,size_t username_len,const u8 * challenge,const u8 * response)2074325151a3SRui Paulo void eap_server_mschap_rx_callback(struct eap_sm *sm, const char *source,
2075325151a3SRui Paulo 				   const u8 *username, size_t username_len,
2076325151a3SRui Paulo 				   const u8 *challenge, const u8 *response)
2077325151a3SRui Paulo {
2078325151a3SRui Paulo 	char hex_challenge[30], hex_response[90], user[100];
2079325151a3SRui Paulo 
2080325151a3SRui Paulo 	/* Print out Challenge and Response in format supported by asleap. */
2081325151a3SRui Paulo 	if (username)
2082325151a3SRui Paulo 		printf_encode(user, sizeof(user), username, username_len);
2083325151a3SRui Paulo 	else
2084325151a3SRui Paulo 		user[0] = '\0';
2085325151a3SRui Paulo 	wpa_snprintf_hex_sep(hex_challenge, sizeof(hex_challenge),
2086325151a3SRui Paulo 			     challenge, sizeof(challenge), ':');
2087325151a3SRui Paulo 	wpa_snprintf_hex_sep(hex_response, sizeof(hex_response), response, 24,
2088325151a3SRui Paulo 			     ':');
2089325151a3SRui Paulo 	wpa_printf(MSG_DEBUG, "[%s/user=%s] asleap -C %s -R %s",
2090325151a3SRui Paulo 		   source, user, hex_challenge, hex_response);
2091325151a3SRui Paulo }
2092325151a3SRui Paulo #endif /* CONFIG_TESTING_OPTIONS */
2093*c1d255d3SCy Schubert 
2094*c1d255d3SCy Schubert 
eap_server_config_free(struct eap_config * cfg)2095*c1d255d3SCy Schubert void eap_server_config_free(struct eap_config *cfg)
2096*c1d255d3SCy Schubert {
2097*c1d255d3SCy Schubert 	if (!cfg)
2098*c1d255d3SCy Schubert 		return;
2099*c1d255d3SCy Schubert 	os_free(cfg->pac_opaque_encr_key);
2100*c1d255d3SCy Schubert 	os_free(cfg->eap_fast_a_id);
2101*c1d255d3SCy Schubert 	os_free(cfg->eap_fast_a_id_info);
2102*c1d255d3SCy Schubert 	os_free(cfg->server_id);
2103*c1d255d3SCy Schubert 	os_free(cfg);
2104*c1d255d3SCy Schubert }
2105