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