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