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