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