eap.c revision c55524ad84d13014e8019491c2b17e5dcf13545a
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
1172	switch (ev) {
1173	case TLS_CERT_CHAIN_FAILURE:
1174		wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_TLS_CERT_ERROR
1175			"reason=%d depth=%d subject='%s' err='%s'",
1176			data->cert_fail.reason,
1177			data->cert_fail.depth,
1178			data->cert_fail.subject,
1179			data->cert_fail.reason_txt);
1180		break;
1181	case TLS_PEER_CERTIFICATE:
1182		if (!sm->eapol_cb->notify_cert)
1183			break;
1184
1185		if (data->peer_cert.hash) {
1186			size_t len = data->peer_cert.hash_len * 2 + 1;
1187			hash_hex = os_malloc(len);
1188			if (hash_hex) {
1189				wpa_snprintf_hex(hash_hex, len,
1190						 data->peer_cert.hash,
1191						 data->peer_cert.hash_len);
1192			}
1193		}
1194
1195		sm->eapol_cb->notify_cert(sm->eapol_ctx,
1196					  data->peer_cert.depth,
1197					  data->peer_cert.subject,
1198					  hash_hex, data->peer_cert.cert);
1199		break;
1200	}
1201
1202	os_free(hash_hex);
1203}
1204
1205
1206/**
1207 * eap_peer_sm_init - Allocate and initialize EAP peer state machine
1208 * @eapol_ctx: Context data to be used with eapol_cb calls
1209 * @eapol_cb: Pointer to EAPOL callback functions
1210 * @msg_ctx: Context data for wpa_msg() calls
1211 * @conf: EAP configuration
1212 * Returns: Pointer to the allocated EAP state machine or %NULL on failure
1213 *
1214 * This function allocates and initializes an EAP state machine. In addition,
1215 * this initializes TLS library for the new EAP state machine. eapol_cb pointer
1216 * will be in use until eap_peer_sm_deinit() is used to deinitialize this EAP
1217 * state machine. Consequently, the caller must make sure that this data
1218 * structure remains alive while the EAP state machine is active.
1219 */
1220struct eap_sm * eap_peer_sm_init(void *eapol_ctx,
1221				 struct eapol_callbacks *eapol_cb,
1222				 void *msg_ctx, struct eap_config *conf)
1223{
1224	struct eap_sm *sm;
1225	struct tls_config tlsconf;
1226
1227	sm = os_zalloc(sizeof(*sm));
1228	if (sm == NULL)
1229		return NULL;
1230	sm->eapol_ctx = eapol_ctx;
1231	sm->eapol_cb = eapol_cb;
1232	sm->msg_ctx = msg_ctx;
1233	sm->ClientTimeout = EAP_CLIENT_TIMEOUT_DEFAULT;
1234	sm->wps = conf->wps;
1235
1236	os_memset(&tlsconf, 0, sizeof(tlsconf));
1237	tlsconf.opensc_engine_path = conf->opensc_engine_path;
1238	tlsconf.pkcs11_engine_path = conf->pkcs11_engine_path;
1239	tlsconf.pkcs11_module_path = conf->pkcs11_module_path;
1240#ifdef CONFIG_FIPS
1241	tlsconf.fips_mode = 1;
1242#endif /* CONFIG_FIPS */
1243	tlsconf.event_cb = eap_peer_sm_tls_event;
1244	tlsconf.cb_ctx = sm;
1245	sm->ssl_ctx = tls_init(&tlsconf);
1246	if (sm->ssl_ctx == NULL) {
1247		wpa_printf(MSG_WARNING, "SSL: Failed to initialize TLS "
1248			   "context.");
1249		os_free(sm);
1250		return NULL;
1251	}
1252
1253	return sm;
1254}
1255
1256
1257/**
1258 * eap_peer_sm_deinit - Deinitialize and free an EAP peer state machine
1259 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1260 *
1261 * This function deinitializes EAP state machine and frees all allocated
1262 * resources.
1263 */
1264void eap_peer_sm_deinit(struct eap_sm *sm)
1265{
1266	if (sm == NULL)
1267		return;
1268	eap_deinit_prev_method(sm, "EAP deinit");
1269	eap_sm_abort(sm);
1270	tls_deinit(sm->ssl_ctx);
1271	os_free(sm);
1272}
1273
1274
1275/**
1276 * eap_peer_sm_step - Step EAP peer state machine
1277 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1278 * Returns: 1 if EAP state was changed or 0 if not
1279 *
1280 * This function advances EAP state machine to a new state to match with the
1281 * current variables. This should be called whenever variables used by the EAP
1282 * state machine have changed.
1283 */
1284int eap_peer_sm_step(struct eap_sm *sm)
1285{
1286	int res = 0;
1287	do {
1288		sm->changed = FALSE;
1289		SM_STEP_RUN(EAP);
1290		if (sm->changed)
1291			res = 1;
1292	} while (sm->changed);
1293	return res;
1294}
1295
1296
1297/**
1298 * eap_sm_abort - Abort EAP authentication
1299 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1300 *
1301 * Release system resources that have been allocated for the authentication
1302 * session without fully deinitializing the EAP state machine.
1303 */
1304void eap_sm_abort(struct eap_sm *sm)
1305{
1306	wpabuf_free(sm->lastRespData);
1307	sm->lastRespData = NULL;
1308	wpabuf_free(sm->eapRespData);
1309	sm->eapRespData = NULL;
1310	os_free(sm->eapKeyData);
1311	sm->eapKeyData = NULL;
1312
1313	/* This is not clearly specified in the EAP statemachines draft, but
1314	 * it seems necessary to make sure that some of the EAPOL variables get
1315	 * cleared for the next authentication. */
1316	eapol_set_bool(sm, EAPOL_eapSuccess, FALSE);
1317}
1318
1319
1320#ifdef CONFIG_CTRL_IFACE
1321static const char * eap_sm_state_txt(int state)
1322{
1323	switch (state) {
1324	case EAP_INITIALIZE:
1325		return "INITIALIZE";
1326	case EAP_DISABLED:
1327		return "DISABLED";
1328	case EAP_IDLE:
1329		return "IDLE";
1330	case EAP_RECEIVED:
1331		return "RECEIVED";
1332	case EAP_GET_METHOD:
1333		return "GET_METHOD";
1334	case EAP_METHOD:
1335		return "METHOD";
1336	case EAP_SEND_RESPONSE:
1337		return "SEND_RESPONSE";
1338	case EAP_DISCARD:
1339		return "DISCARD";
1340	case EAP_IDENTITY:
1341		return "IDENTITY";
1342	case EAP_NOTIFICATION:
1343		return "NOTIFICATION";
1344	case EAP_RETRANSMIT:
1345		return "RETRANSMIT";
1346	case EAP_SUCCESS:
1347		return "SUCCESS";
1348	case EAP_FAILURE:
1349		return "FAILURE";
1350	default:
1351		return "UNKNOWN";
1352	}
1353}
1354#endif /* CONFIG_CTRL_IFACE */
1355
1356
1357#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
1358static const char * eap_sm_method_state_txt(EapMethodState state)
1359{
1360	switch (state) {
1361	case METHOD_NONE:
1362		return "NONE";
1363	case METHOD_INIT:
1364		return "INIT";
1365	case METHOD_CONT:
1366		return "CONT";
1367	case METHOD_MAY_CONT:
1368		return "MAY_CONT";
1369	case METHOD_DONE:
1370		return "DONE";
1371	default:
1372		return "UNKNOWN";
1373	}
1374}
1375
1376
1377static const char * eap_sm_decision_txt(EapDecision decision)
1378{
1379	switch (decision) {
1380	case DECISION_FAIL:
1381		return "FAIL";
1382	case DECISION_COND_SUCC:
1383		return "COND_SUCC";
1384	case DECISION_UNCOND_SUCC:
1385		return "UNCOND_SUCC";
1386	default:
1387		return "UNKNOWN";
1388	}
1389}
1390#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1391
1392
1393#ifdef CONFIG_CTRL_IFACE
1394
1395/**
1396 * eap_sm_get_status - Get EAP state machine status
1397 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1398 * @buf: Buffer for status information
1399 * @buflen: Maximum buffer length
1400 * @verbose: Whether to include verbose status information
1401 * Returns: Number of bytes written to buf.
1402 *
1403 * Query EAP state machine for status information. This function fills in a
1404 * text area with current status information from the EAPOL state machine. If
1405 * the buffer (buf) is not large enough, status information will be truncated
1406 * to fit the buffer.
1407 */
1408int eap_sm_get_status(struct eap_sm *sm, char *buf, size_t buflen, int verbose)
1409{
1410	int len, ret;
1411
1412	if (sm == NULL)
1413		return 0;
1414
1415	len = os_snprintf(buf, buflen,
1416			  "EAP state=%s\n",
1417			  eap_sm_state_txt(sm->EAP_state));
1418	if (len < 0 || (size_t) len >= buflen)
1419		return 0;
1420
1421	if (sm->selectedMethod != EAP_TYPE_NONE) {
1422		const char *name;
1423		if (sm->m) {
1424			name = sm->m->name;
1425		} else {
1426			const struct eap_method *m =
1427				eap_peer_get_eap_method(EAP_VENDOR_IETF,
1428							sm->selectedMethod);
1429			if (m)
1430				name = m->name;
1431			else
1432				name = "?";
1433		}
1434		ret = os_snprintf(buf + len, buflen - len,
1435				  "selectedMethod=%d (EAP-%s)\n",
1436				  sm->selectedMethod, name);
1437		if (ret < 0 || (size_t) ret >= buflen - len)
1438			return len;
1439		len += ret;
1440
1441		if (sm->m && sm->m->get_status) {
1442			len += sm->m->get_status(sm, sm->eap_method_priv,
1443						 buf + len, buflen - len,
1444						 verbose);
1445		}
1446	}
1447
1448	if (verbose) {
1449		ret = os_snprintf(buf + len, buflen - len,
1450				  "reqMethod=%d\n"
1451				  "methodState=%s\n"
1452				  "decision=%s\n"
1453				  "ClientTimeout=%d\n",
1454				  sm->reqMethod,
1455				  eap_sm_method_state_txt(sm->methodState),
1456				  eap_sm_decision_txt(sm->decision),
1457				  sm->ClientTimeout);
1458		if (ret < 0 || (size_t) ret >= buflen - len)
1459			return len;
1460		len += ret;
1461	}
1462
1463	return len;
1464}
1465#endif /* CONFIG_CTRL_IFACE */
1466
1467
1468#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
1469typedef enum {
1470	TYPE_IDENTITY, TYPE_PASSWORD, TYPE_OTP, TYPE_PIN, TYPE_NEW_PASSWORD,
1471	TYPE_PASSPHRASE
1472} eap_ctrl_req_type;
1473
1474static void eap_sm_request(struct eap_sm *sm, eap_ctrl_req_type type,
1475			   const char *msg, size_t msglen)
1476{
1477	struct eap_peer_config *config;
1478	char *field, *txt, *tmp;
1479
1480	if (sm == NULL)
1481		return;
1482	config = eap_get_config(sm);
1483	if (config == NULL)
1484		return;
1485
1486	switch (type) {
1487	case TYPE_IDENTITY:
1488		field = "IDENTITY";
1489		txt = "Identity";
1490		config->pending_req_identity++;
1491		break;
1492	case TYPE_PASSWORD:
1493		field = "PASSWORD";
1494		txt = "Password";
1495		config->pending_req_password++;
1496		break;
1497	case TYPE_NEW_PASSWORD:
1498		field = "NEW_PASSWORD";
1499		txt = "New Password";
1500		config->pending_req_new_password++;
1501		break;
1502	case TYPE_PIN:
1503		field = "PIN";
1504		txt = "PIN";
1505		config->pending_req_pin++;
1506		break;
1507	case TYPE_OTP:
1508		field = "OTP";
1509		if (msg) {
1510			tmp = os_malloc(msglen + 3);
1511			if (tmp == NULL)
1512				return;
1513			tmp[0] = '[';
1514			os_memcpy(tmp + 1, msg, msglen);
1515			tmp[msglen + 1] = ']';
1516			tmp[msglen + 2] = '\0';
1517			txt = tmp;
1518			os_free(config->pending_req_otp);
1519			config->pending_req_otp = tmp;
1520			config->pending_req_otp_len = msglen + 3;
1521		} else {
1522			if (config->pending_req_otp == NULL)
1523				return;
1524			txt = config->pending_req_otp;
1525		}
1526		break;
1527	case TYPE_PASSPHRASE:
1528		field = "PASSPHRASE";
1529		txt = "Private key passphrase";
1530		config->pending_req_passphrase++;
1531		break;
1532	default:
1533		return;
1534	}
1535
1536	if (sm->eapol_cb->eap_param_needed)
1537		sm->eapol_cb->eap_param_needed(sm->eapol_ctx, field, txt);
1538}
1539#else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1540#define eap_sm_request(sm, type, msg, msglen) do { } while (0)
1541#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1542
1543const char * eap_sm_get_method_name(struct eap_sm *sm)
1544{
1545	if (sm->m == NULL)
1546		return "UNKNOWN";
1547	return sm->m->name;
1548}
1549
1550
1551/**
1552 * eap_sm_request_identity - Request identity from user (ctrl_iface)
1553 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1554 *
1555 * EAP methods can call this function to request identity information for the
1556 * current network. This is normally called when the identity is not included
1557 * in the network configuration. The request will be sent to monitor programs
1558 * through the control interface.
1559 */
1560void eap_sm_request_identity(struct eap_sm *sm)
1561{
1562	eap_sm_request(sm, TYPE_IDENTITY, NULL, 0);
1563}
1564
1565
1566/**
1567 * eap_sm_request_password - Request password from user (ctrl_iface)
1568 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1569 *
1570 * EAP methods can call this function to request password information for the
1571 * current network. This is normally called when the password is not included
1572 * in the network configuration. The request will be sent to monitor programs
1573 * through the control interface.
1574 */
1575void eap_sm_request_password(struct eap_sm *sm)
1576{
1577	eap_sm_request(sm, TYPE_PASSWORD, NULL, 0);
1578}
1579
1580
1581/**
1582 * eap_sm_request_new_password - Request new password from user (ctrl_iface)
1583 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1584 *
1585 * EAP methods can call this function to request new password information for
1586 * the current network. This is normally called when the EAP method indicates
1587 * that the current password has expired and password change is required. The
1588 * request will be sent to monitor programs through the control interface.
1589 */
1590void eap_sm_request_new_password(struct eap_sm *sm)
1591{
1592	eap_sm_request(sm, TYPE_NEW_PASSWORD, NULL, 0);
1593}
1594
1595
1596/**
1597 * eap_sm_request_pin - Request SIM or smart card PIN from user (ctrl_iface)
1598 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1599 *
1600 * EAP methods can call this function to request SIM or smart card PIN
1601 * information for the current network. This is normally called when the PIN is
1602 * not included in the network configuration. The request will be sent to
1603 * monitor programs through the control interface.
1604 */
1605void eap_sm_request_pin(struct eap_sm *sm)
1606{
1607	eap_sm_request(sm, TYPE_PIN, NULL, 0);
1608}
1609
1610
1611/**
1612 * eap_sm_request_otp - Request one time password from user (ctrl_iface)
1613 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1614 * @msg: Message to be displayed to the user when asking for OTP
1615 * @msg_len: Length of the user displayable message
1616 *
1617 * EAP methods can call this function to request open time password (OTP) for
1618 * the current network. The request will be sent to monitor programs through
1619 * the control interface.
1620 */
1621void eap_sm_request_otp(struct eap_sm *sm, const char *msg, size_t msg_len)
1622{
1623	eap_sm_request(sm, TYPE_OTP, msg, msg_len);
1624}
1625
1626
1627/**
1628 * eap_sm_request_passphrase - Request passphrase from user (ctrl_iface)
1629 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1630 *
1631 * EAP methods can call this function to request passphrase for a private key
1632 * for the current network. This is normally called when the passphrase is not
1633 * included in the network configuration. The request will be sent to monitor
1634 * programs through the control interface.
1635 */
1636void eap_sm_request_passphrase(struct eap_sm *sm)
1637{
1638	eap_sm_request(sm, TYPE_PASSPHRASE, NULL, 0);
1639}
1640
1641
1642/**
1643 * eap_sm_notify_ctrl_attached - Notification of attached monitor
1644 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1645 *
1646 * Notify EAP state machines that a monitor was attached to the control
1647 * interface to trigger re-sending of pending requests for user input.
1648 */
1649void eap_sm_notify_ctrl_attached(struct eap_sm *sm)
1650{
1651	struct eap_peer_config *config = eap_get_config(sm);
1652
1653	if (config == NULL)
1654		return;
1655
1656	/* Re-send any pending requests for user data since a new control
1657	 * interface was added. This handles cases where the EAP authentication
1658	 * starts immediately after system startup when the user interface is
1659	 * not yet running. */
1660	if (config->pending_req_identity)
1661		eap_sm_request_identity(sm);
1662	if (config->pending_req_password)
1663		eap_sm_request_password(sm);
1664	if (config->pending_req_new_password)
1665		eap_sm_request_new_password(sm);
1666	if (config->pending_req_otp)
1667		eap_sm_request_otp(sm, NULL, 0);
1668	if (config->pending_req_pin)
1669		eap_sm_request_pin(sm);
1670	if (config->pending_req_passphrase)
1671		eap_sm_request_passphrase(sm);
1672}
1673
1674
1675static int eap_allowed_phase2_type(int vendor, int type)
1676{
1677	if (vendor != EAP_VENDOR_IETF)
1678		return 0;
1679	return type != EAP_TYPE_PEAP && type != EAP_TYPE_TTLS &&
1680		type != EAP_TYPE_FAST;
1681}
1682
1683
1684/**
1685 * eap_get_phase2_type - Get EAP type for the given EAP phase 2 method name
1686 * @name: EAP method name, e.g., MD5
1687 * @vendor: Buffer for returning EAP Vendor-Id
1688 * Returns: EAP method type or %EAP_TYPE_NONE if not found
1689 *
1690 * This function maps EAP type names into EAP type numbers that are allowed for
1691 * Phase 2, i.e., for tunneled authentication. Phase 2 is used, e.g., with
1692 * EAP-PEAP, EAP-TTLS, and EAP-FAST.
1693 */
1694u32 eap_get_phase2_type(const char *name, int *vendor)
1695{
1696	int v;
1697	u8 type = eap_peer_get_type(name, &v);
1698	if (eap_allowed_phase2_type(v, type)) {
1699		*vendor = v;
1700		return type;
1701	}
1702	*vendor = EAP_VENDOR_IETF;
1703	return EAP_TYPE_NONE;
1704}
1705
1706
1707/**
1708 * eap_get_phase2_types - Get list of allowed EAP phase 2 types
1709 * @config: Pointer to a network configuration
1710 * @count: Pointer to a variable to be filled with number of returned EAP types
1711 * Returns: Pointer to allocated type list or %NULL on failure
1712 *
1713 * This function generates an array of allowed EAP phase 2 (tunneled) types for
1714 * the given network configuration.
1715 */
1716struct eap_method_type * eap_get_phase2_types(struct eap_peer_config *config,
1717					      size_t *count)
1718{
1719	struct eap_method_type *buf;
1720	u32 method;
1721	int vendor;
1722	size_t mcount;
1723	const struct eap_method *methods, *m;
1724
1725	methods = eap_peer_get_methods(&mcount);
1726	if (methods == NULL)
1727		return NULL;
1728	*count = 0;
1729	buf = os_malloc(mcount * sizeof(struct eap_method_type));
1730	if (buf == NULL)
1731		return NULL;
1732
1733	for (m = methods; m; m = m->next) {
1734		vendor = m->vendor;
1735		method = m->method;
1736		if (eap_allowed_phase2_type(vendor, method)) {
1737			if (vendor == EAP_VENDOR_IETF &&
1738			    method == EAP_TYPE_TLS && config &&
1739			    config->private_key2 == NULL)
1740				continue;
1741			buf[*count].vendor = vendor;
1742			buf[*count].method = method;
1743			(*count)++;
1744		}
1745	}
1746
1747	return buf;
1748}
1749
1750
1751/**
1752 * eap_set_fast_reauth - Update fast_reauth setting
1753 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1754 * @enabled: 1 = Fast reauthentication is enabled, 0 = Disabled
1755 */
1756void eap_set_fast_reauth(struct eap_sm *sm, int enabled)
1757{
1758	sm->fast_reauth = enabled;
1759}
1760
1761
1762/**
1763 * eap_set_workaround - Update EAP workarounds setting
1764 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1765 * @workaround: 1 = Enable EAP workarounds, 0 = Disable EAP workarounds
1766 */
1767void eap_set_workaround(struct eap_sm *sm, unsigned int workaround)
1768{
1769	sm->workaround = workaround;
1770}
1771
1772
1773/**
1774 * eap_get_config - Get current network configuration
1775 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1776 * Returns: Pointer to the current network configuration or %NULL if not found
1777 *
1778 * EAP peer methods should avoid using this function if they can use other
1779 * access functions, like eap_get_config_identity() and
1780 * eap_get_config_password(), that do not require direct access to
1781 * struct eap_peer_config.
1782 */
1783struct eap_peer_config * eap_get_config(struct eap_sm *sm)
1784{
1785	return sm->eapol_cb->get_config(sm->eapol_ctx);
1786}
1787
1788
1789/**
1790 * eap_get_config_identity - Get identity from the network configuration
1791 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1792 * @len: Buffer for the length of the identity
1793 * Returns: Pointer to the identity or %NULL if not found
1794 */
1795const u8 * eap_get_config_identity(struct eap_sm *sm, size_t *len)
1796{
1797	struct eap_peer_config *config = eap_get_config(sm);
1798	if (config == NULL)
1799		return NULL;
1800	*len = config->identity_len;
1801	return config->identity;
1802}
1803
1804
1805/**
1806 * eap_get_config_password - Get password from the network configuration
1807 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1808 * @len: Buffer for the length of the password
1809 * Returns: Pointer to the password or %NULL if not found
1810 */
1811const u8 * eap_get_config_password(struct eap_sm *sm, size_t *len)
1812{
1813	struct eap_peer_config *config = eap_get_config(sm);
1814	if (config == NULL)
1815		return NULL;
1816	*len = config->password_len;
1817	return config->password;
1818}
1819
1820
1821/**
1822 * eap_get_config_password2 - Get password from the network configuration
1823 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1824 * @len: Buffer for the length of the password
1825 * @hash: Buffer for returning whether the password is stored as a
1826 * NtPasswordHash instead of plaintext password; can be %NULL if this
1827 * information is not needed
1828 * Returns: Pointer to the password or %NULL if not found
1829 */
1830const u8 * eap_get_config_password2(struct eap_sm *sm, size_t *len, int *hash)
1831{
1832	struct eap_peer_config *config = eap_get_config(sm);
1833	if (config == NULL)
1834		return NULL;
1835	*len = config->password_len;
1836	if (hash)
1837		*hash = !!(config->flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH);
1838	return config->password;
1839}
1840
1841
1842/**
1843 * eap_get_config_new_password - Get new password from network configuration
1844 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1845 * @len: Buffer for the length of the new password
1846 * Returns: Pointer to the new password or %NULL if not found
1847 */
1848const u8 * eap_get_config_new_password(struct eap_sm *sm, size_t *len)
1849{
1850	struct eap_peer_config *config = eap_get_config(sm);
1851	if (config == NULL)
1852		return NULL;
1853	*len = config->new_password_len;
1854	return config->new_password;
1855}
1856
1857
1858/**
1859 * eap_get_config_otp - Get one-time password from the network configuration
1860 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1861 * @len: Buffer for the length of the one-time password
1862 * Returns: Pointer to the one-time password or %NULL if not found
1863 */
1864const u8 * eap_get_config_otp(struct eap_sm *sm, size_t *len)
1865{
1866	struct eap_peer_config *config = eap_get_config(sm);
1867	if (config == NULL)
1868		return NULL;
1869	*len = config->otp_len;
1870	return config->otp;
1871}
1872
1873
1874/**
1875 * eap_clear_config_otp - Clear used one-time password
1876 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1877 *
1878 * This function clears a used one-time password (OTP) from the current network
1879 * configuration. This should be called when the OTP has been used and is not
1880 * needed anymore.
1881 */
1882void eap_clear_config_otp(struct eap_sm *sm)
1883{
1884	struct eap_peer_config *config = eap_get_config(sm);
1885	if (config == NULL)
1886		return;
1887	os_memset(config->otp, 0, config->otp_len);
1888	os_free(config->otp);
1889	config->otp = NULL;
1890	config->otp_len = 0;
1891}
1892
1893
1894/**
1895 * eap_get_config_phase1 - Get phase1 data from the network configuration
1896 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1897 * Returns: Pointer to the phase1 data or %NULL if not found
1898 */
1899const char * eap_get_config_phase1(struct eap_sm *sm)
1900{
1901	struct eap_peer_config *config = eap_get_config(sm);
1902	if (config == NULL)
1903		return NULL;
1904	return config->phase1;
1905}
1906
1907
1908/**
1909 * eap_get_config_phase2 - Get phase2 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_phase2(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->phase2;
1919}
1920
1921
1922int eap_get_config_fragment_size(struct eap_sm *sm)
1923{
1924	struct eap_peer_config *config = eap_get_config(sm);
1925	if (config == NULL)
1926		return -1;
1927	return config->fragment_size;
1928}
1929
1930
1931/**
1932 * eap_key_available - Get key availability (eapKeyAvailable variable)
1933 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1934 * Returns: 1 if EAP keying material is available, 0 if not
1935 */
1936int eap_key_available(struct eap_sm *sm)
1937{
1938	return sm ? sm->eapKeyAvailable : 0;
1939}
1940
1941
1942/**
1943 * eap_notify_success - Notify EAP state machine about external success trigger
1944 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1945 *
1946 * This function is called when external event, e.g., successful completion of
1947 * WPA-PSK key handshake, is indicating that EAP state machine should move to
1948 * success state. This is mainly used with security modes that do not use EAP
1949 * state machine (e.g., WPA-PSK).
1950 */
1951void eap_notify_success(struct eap_sm *sm)
1952{
1953	if (sm) {
1954		sm->decision = DECISION_COND_SUCC;
1955		sm->EAP_state = EAP_SUCCESS;
1956	}
1957}
1958
1959
1960/**
1961 * eap_notify_lower_layer_success - Notification of lower layer success
1962 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1963 *
1964 * Notify EAP state machines that a lower layer has detected a successful
1965 * authentication. This is used to recover from dropped EAP-Success messages.
1966 */
1967void eap_notify_lower_layer_success(struct eap_sm *sm)
1968{
1969	if (sm == NULL)
1970		return;
1971
1972	if (eapol_get_bool(sm, EAPOL_eapSuccess) ||
1973	    sm->decision == DECISION_FAIL ||
1974	    (sm->methodState != METHOD_MAY_CONT &&
1975	     sm->methodState != METHOD_DONE))
1976		return;
1977
1978	if (sm->eapKeyData != NULL)
1979		sm->eapKeyAvailable = TRUE;
1980	eapol_set_bool(sm, EAPOL_eapSuccess, TRUE);
1981	wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
1982		"EAP authentication completed successfully (based on lower "
1983		"layer success)");
1984}
1985
1986
1987/**
1988 * eap_get_eapKeyData - Get master session key (MSK) from EAP state machine
1989 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1990 * @len: Pointer to variable that will be set to number of bytes in the key
1991 * Returns: Pointer to the EAP keying data or %NULL on failure
1992 *
1993 * Fetch EAP keying material (MSK, eapKeyData) from the EAP state machine. The
1994 * key is available only after a successful authentication. EAP state machine
1995 * continues to manage the key data and the caller must not change or free the
1996 * returned data.
1997 */
1998const u8 * eap_get_eapKeyData(struct eap_sm *sm, size_t *len)
1999{
2000	if (sm == NULL || sm->eapKeyData == NULL) {
2001		*len = 0;
2002		return NULL;
2003	}
2004
2005	*len = sm->eapKeyDataLen;
2006	return sm->eapKeyData;
2007}
2008
2009
2010/**
2011 * eap_get_eapKeyData - Get EAP response data
2012 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2013 * Returns: Pointer to the EAP response (eapRespData) or %NULL on failure
2014 *
2015 * Fetch EAP response (eapRespData) from the EAP state machine. This data is
2016 * available when EAP state machine has processed an incoming EAP request. The
2017 * EAP state machine does not maintain a reference to the response after this
2018 * function is called and the caller is responsible for freeing the data.
2019 */
2020struct wpabuf * eap_get_eapRespData(struct eap_sm *sm)
2021{
2022	struct wpabuf *resp;
2023
2024	if (sm == NULL || sm->eapRespData == NULL)
2025		return NULL;
2026
2027	resp = sm->eapRespData;
2028	sm->eapRespData = NULL;
2029
2030	return resp;
2031}
2032
2033
2034/**
2035 * eap_sm_register_scard_ctx - Notification of smart card context
2036 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2037 * @ctx: Context data for smart card operations
2038 *
2039 * Notify EAP state machines of context data for smart card operations. This
2040 * context data will be used as a parameter for scard_*() functions.
2041 */
2042void eap_register_scard_ctx(struct eap_sm *sm, void *ctx)
2043{
2044	if (sm)
2045		sm->scard_ctx = ctx;
2046}
2047
2048
2049/**
2050 * eap_set_config_blob - Set or add a named configuration blob
2051 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2052 * @blob: New value for the blob
2053 *
2054 * Adds a new configuration blob or replaces the current value of an existing
2055 * blob.
2056 */
2057void eap_set_config_blob(struct eap_sm *sm, struct wpa_config_blob *blob)
2058{
2059#ifndef CONFIG_NO_CONFIG_BLOBS
2060	sm->eapol_cb->set_config_blob(sm->eapol_ctx, blob);
2061#endif /* CONFIG_NO_CONFIG_BLOBS */
2062}
2063
2064
2065/**
2066 * eap_get_config_blob - Get a named configuration blob
2067 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2068 * @name: Name of the blob
2069 * Returns: Pointer to blob data or %NULL if not found
2070 */
2071const struct wpa_config_blob * eap_get_config_blob(struct eap_sm *sm,
2072						   const char *name)
2073{
2074#ifndef CONFIG_NO_CONFIG_BLOBS
2075	return sm->eapol_cb->get_config_blob(sm->eapol_ctx, name);
2076#else /* CONFIG_NO_CONFIG_BLOBS */
2077	return NULL;
2078#endif /* CONFIG_NO_CONFIG_BLOBS */
2079}
2080
2081
2082/**
2083 * eap_set_force_disabled - Set force_disabled flag
2084 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2085 * @disabled: 1 = EAP disabled, 0 = EAP enabled
2086 *
2087 * This function is used to force EAP state machine to be disabled when it is
2088 * not in use (e.g., with WPA-PSK or plaintext connections).
2089 */
2090void eap_set_force_disabled(struct eap_sm *sm, int disabled)
2091{
2092	sm->force_disabled = disabled;
2093}
2094
2095
2096 /**
2097 * eap_notify_pending - Notify that EAP method is ready to re-process a request
2098 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2099 *
2100 * An EAP method can perform a pending operation (e.g., to get a response from
2101 * an external process). Once the response is available, this function can be
2102 * used to request EAPOL state machine to retry delivering the previously
2103 * received (and still unanswered) EAP request to EAP state machine.
2104 */
2105void eap_notify_pending(struct eap_sm *sm)
2106{
2107	sm->eapol_cb->notify_pending(sm->eapol_ctx);
2108}
2109
2110
2111/**
2112 * eap_invalidate_cached_session - Mark cached session data invalid
2113 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2114 */
2115void eap_invalidate_cached_session(struct eap_sm *sm)
2116{
2117	if (sm)
2118		eap_deinit_prev_method(sm, "invalidate");
2119}
2120
2121
2122int eap_is_wps_pbc_enrollee(struct eap_peer_config *conf)
2123{
2124	if (conf->identity_len != WSC_ID_ENROLLEE_LEN ||
2125	    os_memcmp(conf->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN))
2126		return 0; /* Not a WPS Enrollee */
2127
2128	if (conf->phase1 == NULL || os_strstr(conf->phase1, "pbc=1") == NULL)
2129		return 0; /* Not using PBC */
2130
2131	return 1;
2132}
2133
2134
2135int eap_is_wps_pin_enrollee(struct eap_peer_config *conf)
2136{
2137	if (conf->identity_len != WSC_ID_ENROLLEE_LEN ||
2138	    os_memcmp(conf->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN))
2139		return 0; /* Not a WPS Enrollee */
2140
2141	if (conf->phase1 == NULL || os_strstr(conf->phase1, "pin=") == NULL)
2142		return 0; /* Not using PIN */
2143
2144	return 1;
2145}
2146