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