2 * EAP peer state machines (RFC 4137)
3 * Copyright (c) 2004-2012, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
8 * This file implements the Peer State Machine as defined in RFC 4137. The used
9 * states and state transitions match mostly with the RFC. However, there are
10 * couple of additional transitions for working around small issues noticed
11 * during testing. These exceptions are explained in comments within the
12 * functions in this file. The method functions, m.func(), are similar to the
13 * ones used in RFC 4137, but some small changes have used here to optimize
14 * operations and to add functionality needed for fast re-authentication
15 * (session resumption).
21 #include "pcsc_funcs.h"
22 #include "state_machine.h"
23 #include "ext_password.h"
24 #include "crypto/crypto.h"
25 #include "crypto/tls.h"
26 #include "common/wpa_ctrl.h"
27 #include "eap_common/eap_wsc_common.h"
29 #include "eap_config.h"
31 #define STATE_MACHINE_DATA struct eap_sm
32 #define STATE_MACHINE_DEBUG_PREFIX "EAP"
34 #define EAP_MAX_AUTH_ROUNDS 50
35 #define EAP_CLIENT_TIMEOUT_DEFAULT 60
38 static Boolean eap_sm_allowMethod(struct eap_sm *sm, int vendor,
40 static struct wpabuf * eap_sm_buildNak(struct eap_sm *sm, int id);
41 static void eap_sm_processIdentity(struct eap_sm *sm,
42 const struct wpabuf *req);
43 static void eap_sm_processNotify(struct eap_sm *sm, const struct wpabuf *req);
44 static struct wpabuf * eap_sm_buildNotify(int id);
45 static void eap_sm_parseEapReq(struct eap_sm *sm, const struct wpabuf *req);
46 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
47 static const char * eap_sm_method_state_txt(EapMethodState state);
48 static const char * eap_sm_decision_txt(EapDecision decision);
49 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
53 static Boolean eapol_get_bool(struct eap_sm *sm, enum eapol_bool_var var)
55 return sm->eapol_cb->get_bool(sm->eapol_ctx, var);
59 static void eapol_set_bool(struct eap_sm *sm, enum eapol_bool_var var,
62 sm->eapol_cb->set_bool(sm->eapol_ctx, var, value);
66 static unsigned int eapol_get_int(struct eap_sm *sm, enum eapol_int_var var)
68 return sm->eapol_cb->get_int(sm->eapol_ctx, var);
72 static void eapol_set_int(struct eap_sm *sm, enum eapol_int_var var,
75 sm->eapol_cb->set_int(sm->eapol_ctx, var, value);
79 static struct wpabuf * eapol_get_eapReqData(struct eap_sm *sm)
81 return sm->eapol_cb->get_eapReqData(sm->eapol_ctx);
85 static void eap_notify_status(struct eap_sm *sm, const char *status,
86 const char *parameter)
88 wpa_printf(MSG_DEBUG, "EAP: Status notification: %s (param=%s)",
90 if (sm->eapol_cb->notify_status)
91 sm->eapol_cb->notify_status(sm->eapol_ctx, status, parameter);
95 static void eap_deinit_prev_method(struct eap_sm *sm, const char *txt)
97 ext_password_free(sm->ext_pw_buf);
98 sm->ext_pw_buf = NULL;
100 if (sm->m == NULL || sm->eap_method_priv == NULL)
103 wpa_printf(MSG_DEBUG, "EAP: deinitialize previously used EAP method "
104 "(%d, %s) at %s", sm->selectedMethod, sm->m->name, txt);
105 sm->m->deinit(sm, sm->eap_method_priv);
106 sm->eap_method_priv = NULL;
112 * eap_allowed_method - Check whether EAP method is allowed
113 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
114 * @vendor: Vendor-Id for expanded types or 0 = IETF for legacy types
116 * Returns: 1 = allowed EAP method, 0 = not allowed
118 int eap_allowed_method(struct eap_sm *sm, int vendor, u32 method)
120 struct eap_peer_config *config = eap_get_config(sm);
122 struct eap_method_type *m;
124 if (config == NULL || config->eap_methods == NULL)
127 m = config->eap_methods;
128 for (i = 0; m[i].vendor != EAP_VENDOR_IETF ||
129 m[i].method != EAP_TYPE_NONE; i++) {
130 if (m[i].vendor == vendor && m[i].method == method)
138 * This state initializes state machine variables when the machine is
139 * activated (portEnabled = TRUE). This is also used when re-starting
140 * authentication (eapRestart == TRUE).
142 SM_STATE(EAP, INITIALIZE)
144 SM_ENTRY(EAP, INITIALIZE);
145 if (sm->fast_reauth && sm->m && sm->m->has_reauth_data &&
146 sm->m->has_reauth_data(sm, sm->eap_method_priv) &&
148 wpa_printf(MSG_DEBUG, "EAP: maintaining EAP method data for "
149 "fast reauthentication");
150 sm->m->deinit_for_reauth(sm, sm->eap_method_priv);
152 eap_deinit_prev_method(sm, "INITIALIZE");
154 sm->selectedMethod = EAP_TYPE_NONE;
155 sm->methodState = METHOD_NONE;
156 sm->allowNotifications = TRUE;
157 sm->decision = DECISION_FAIL;
158 sm->ClientTimeout = EAP_CLIENT_TIMEOUT_DEFAULT;
159 eapol_set_int(sm, EAPOL_idleWhile, sm->ClientTimeout);
160 eapol_set_bool(sm, EAPOL_eapSuccess, FALSE);
161 eapol_set_bool(sm, EAPOL_eapFail, FALSE);
162 os_free(sm->eapKeyData);
163 sm->eapKeyData = NULL;
164 sm->eapKeyAvailable = FALSE;
165 eapol_set_bool(sm, EAPOL_eapRestart, FALSE);
166 sm->lastId = -1; /* new session - make sure this does not match with
167 * the first EAP-Packet */
169 * RFC 4137 does not reset eapResp and eapNoResp here. However, this
170 * seemed to be able to trigger cases where both were set and if EAPOL
171 * state machine uses eapNoResp first, it may end up not sending a real
172 * reply correctly. This occurred when the workaround in FAIL state set
173 * eapNoResp = TRUE.. Maybe that workaround needs to be fixed to do
176 eapol_set_bool(sm, EAPOL_eapResp, FALSE);
177 eapol_set_bool(sm, EAPOL_eapNoResp, FALSE);
179 sm->prev_failure = 0;
184 * This state is reached whenever service from the lower layer is interrupted
185 * or unavailable (portEnabled == FALSE). Immediate transition to INITIALIZE
186 * occurs when the port becomes enabled.
188 SM_STATE(EAP, DISABLED)
190 SM_ENTRY(EAP, DISABLED);
193 * RFC 4137 does not describe clearing of idleWhile here, but doing so
194 * allows the timer tick to be stopped more quickly when EAP is not in
197 eapol_set_int(sm, EAPOL_idleWhile, 0);
202 * The state machine spends most of its time here, waiting for something to
203 * happen. This state is entered unconditionally from INITIALIZE, DISCARD, and
204 * SEND_RESPONSE states.
213 * This state is entered when an EAP packet is received (eapReq == TRUE) to
214 * parse the packet header.
216 SM_STATE(EAP, RECEIVED)
218 const struct wpabuf *eapReqData;
220 SM_ENTRY(EAP, RECEIVED);
221 eapReqData = eapol_get_eapReqData(sm);
222 /* parse rxReq, rxSuccess, rxFailure, reqId, reqMethod */
223 eap_sm_parseEapReq(sm, eapReqData);
229 * This state is entered when a request for a new type comes in. Either the
230 * correct method is started, or a Nak response is built.
232 SM_STATE(EAP, GET_METHOD)
236 const struct eap_method *eap_method;
238 SM_ENTRY(EAP, GET_METHOD);
240 if (sm->reqMethod == EAP_TYPE_EXPANDED)
241 method = sm->reqVendorMethod;
243 method = sm->reqMethod;
245 eap_method = eap_peer_get_eap_method(sm->reqVendor, method);
247 if (!eap_sm_allowMethod(sm, sm->reqVendor, method)) {
248 wpa_printf(MSG_DEBUG, "EAP: vendor %u method %u not allowed",
249 sm->reqVendor, method);
250 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_PROPOSED_METHOD
251 "vendor=%u method=%u -> NAK",
252 sm->reqVendor, method);
253 eap_notify_status(sm, "refuse proposed method",
254 eap_method ? eap_method->name : "unknown");
258 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_PROPOSED_METHOD
259 "vendor=%u method=%u", sm->reqVendor, method);
261 eap_notify_status(sm, "accept proposed method",
262 eap_method ? eap_method->name : "unknown");
264 * RFC 4137 does not define specific operation for fast
265 * re-authentication (session resumption). The design here is to allow
266 * the previously used method data to be maintained for
267 * re-authentication if the method support session resumption.
268 * Otherwise, the previously used method data is freed and a new method
271 if (sm->fast_reauth &&
272 sm->m && sm->m->vendor == sm->reqVendor &&
273 sm->m->method == method &&
274 sm->m->has_reauth_data &&
275 sm->m->has_reauth_data(sm, sm->eap_method_priv)) {
276 wpa_printf(MSG_DEBUG, "EAP: Using previous method data"
277 " for fast re-authentication");
280 eap_deinit_prev_method(sm, "GET_METHOD");
284 sm->selectedMethod = sm->reqMethod;
288 wpa_printf(MSG_DEBUG, "EAP: Could not find selected method: "
289 "vendor %d method %d",
290 sm->reqVendor, method);
294 sm->ClientTimeout = EAP_CLIENT_TIMEOUT_DEFAULT;
296 wpa_printf(MSG_DEBUG, "EAP: Initialize selected EAP method: "
297 "vendor %u method %u (%s)",
298 sm->reqVendor, method, sm->m->name);
300 sm->eap_method_priv = sm->m->init_for_reauth(
301 sm, sm->eap_method_priv);
303 sm->eap_method_priv = sm->m->init(sm);
305 if (sm->eap_method_priv == NULL) {
306 struct eap_peer_config *config = eap_get_config(sm);
307 wpa_msg(sm->msg_ctx, MSG_INFO,
308 "EAP: Failed to initialize EAP method: vendor %u "
310 sm->reqVendor, method, sm->m->name);
312 sm->methodState = METHOD_NONE;
313 sm->selectedMethod = EAP_TYPE_NONE;
314 if (sm->reqMethod == EAP_TYPE_TLS && config &&
315 (config->pending_req_pin ||
316 config->pending_req_passphrase)) {
318 * Return without generating Nak in order to allow
319 * entering of PIN code or passphrase to retry the
320 * current EAP packet.
322 wpa_printf(MSG_DEBUG, "EAP: Pending PIN/passphrase "
323 "request - skip Nak");
330 sm->methodState = METHOD_INIT;
331 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_METHOD
332 "EAP vendor %u method %u (%s) selected",
333 sm->reqVendor, method, sm->m->name);
337 wpabuf_free(sm->eapRespData);
338 sm->eapRespData = NULL;
339 sm->eapRespData = eap_sm_buildNak(sm, sm->reqId);
344 * The method processing happens here. The request from the authenticator is
345 * processed, and an appropriate response packet is built.
347 SM_STATE(EAP, METHOD)
349 struct wpabuf *eapReqData;
350 struct eap_method_ret ret;
352 SM_ENTRY(EAP, METHOD);
354 wpa_printf(MSG_WARNING, "EAP::METHOD - method not selected");
358 eapReqData = eapol_get_eapReqData(sm);
359 if (!eap_hdr_len_valid(eapReqData, 1))
363 * Get ignore, methodState, decision, allowNotifications, and
364 * eapRespData. RFC 4137 uses three separate method procedure (check,
365 * process, and buildResp) in this state. These have been combined into
366 * a single function call to m->process() in order to optimize EAP
367 * method implementation interface a bit. These procedures are only
368 * used from within this METHOD state, so there is no need to keep
369 * these as separate C functions.
371 * The RFC 4137 procedures return values as follows:
372 * ignore = m.check(eapReqData)
373 * (methodState, decision, allowNotifications) = m.process(eapReqData)
374 * eapRespData = m.buildResp(reqId)
376 os_memset(&ret, 0, sizeof(ret));
377 ret.ignore = sm->ignore;
378 ret.methodState = sm->methodState;
379 ret.decision = sm->decision;
380 ret.allowNotifications = sm->allowNotifications;
381 wpabuf_free(sm->eapRespData);
382 sm->eapRespData = NULL;
383 sm->eapRespData = sm->m->process(sm, sm->eap_method_priv, &ret,
385 wpa_printf(MSG_DEBUG, "EAP: method process -> ignore=%s "
386 "methodState=%s decision=%s",
387 ret.ignore ? "TRUE" : "FALSE",
388 eap_sm_method_state_txt(ret.methodState),
389 eap_sm_decision_txt(ret.decision));
391 sm->ignore = ret.ignore;
394 sm->methodState = ret.methodState;
395 sm->decision = ret.decision;
396 sm->allowNotifications = ret.allowNotifications;
398 if (sm->m->isKeyAvailable && sm->m->getKey &&
399 sm->m->isKeyAvailable(sm, sm->eap_method_priv)) {
400 os_free(sm->eapKeyData);
401 sm->eapKeyData = sm->m->getKey(sm, sm->eap_method_priv,
408 * This state signals the lower layer that a response packet is ready to be
411 SM_STATE(EAP, SEND_RESPONSE)
413 SM_ENTRY(EAP, SEND_RESPONSE);
414 wpabuf_free(sm->lastRespData);
415 if (sm->eapRespData) {
417 os_memcpy(sm->last_md5, sm->req_md5, 16);
418 sm->lastId = sm->reqId;
419 sm->lastRespData = wpabuf_dup(sm->eapRespData);
420 eapol_set_bool(sm, EAPOL_eapResp, TRUE);
422 sm->lastRespData = NULL;
423 eapol_set_bool(sm, EAPOL_eapReq, FALSE);
424 eapol_set_int(sm, EAPOL_idleWhile, sm->ClientTimeout);
429 * This state signals the lower layer that the request was discarded, and no
430 * response packet will be sent at this time.
432 SM_STATE(EAP, DISCARD)
434 SM_ENTRY(EAP, DISCARD);
435 eapol_set_bool(sm, EAPOL_eapReq, FALSE);
436 eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
441 * Handles requests for Identity method and builds a response.
443 SM_STATE(EAP, IDENTITY)
445 const struct wpabuf *eapReqData;
447 SM_ENTRY(EAP, IDENTITY);
448 eapReqData = eapol_get_eapReqData(sm);
449 if (!eap_hdr_len_valid(eapReqData, 1))
451 eap_sm_processIdentity(sm, eapReqData);
452 wpabuf_free(sm->eapRespData);
453 sm->eapRespData = NULL;
454 sm->eapRespData = eap_sm_buildIdentity(sm, sm->reqId, 0);
459 * Handles requests for Notification method and builds a response.
461 SM_STATE(EAP, NOTIFICATION)
463 const struct wpabuf *eapReqData;
465 SM_ENTRY(EAP, NOTIFICATION);
466 eapReqData = eapol_get_eapReqData(sm);
467 if (!eap_hdr_len_valid(eapReqData, 1))
469 eap_sm_processNotify(sm, eapReqData);
470 wpabuf_free(sm->eapRespData);
471 sm->eapRespData = NULL;
472 sm->eapRespData = eap_sm_buildNotify(sm->reqId);
477 * This state retransmits the previous response packet.
479 SM_STATE(EAP, RETRANSMIT)
481 SM_ENTRY(EAP, RETRANSMIT);
482 wpabuf_free(sm->eapRespData);
483 if (sm->lastRespData)
484 sm->eapRespData = wpabuf_dup(sm->lastRespData);
486 sm->eapRespData = NULL;
491 * This state is entered in case of a successful completion of authentication
492 * and state machine waits here until port is disabled or EAP authentication is
495 SM_STATE(EAP, SUCCESS)
497 SM_ENTRY(EAP, SUCCESS);
498 if (sm->eapKeyData != NULL)
499 sm->eapKeyAvailable = TRUE;
500 eapol_set_bool(sm, EAPOL_eapSuccess, TRUE);
503 * RFC 4137 does not clear eapReq here, but this seems to be required
504 * to avoid processing the same request twice when state machine is
507 eapol_set_bool(sm, EAPOL_eapReq, FALSE);
510 * RFC 4137 does not set eapNoResp here, but this seems to be required
511 * to get EAPOL Supplicant backend state machine into SUCCESS state. In
512 * addition, either eapResp or eapNoResp is required to be set after
513 * processing the received EAP frame.
515 eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
517 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
518 "EAP authentication completed successfully");
523 * This state is entered in case of a failure and state machine waits here
524 * until port is disabled or EAP authentication is restarted.
526 SM_STATE(EAP, FAILURE)
528 SM_ENTRY(EAP, FAILURE);
529 eapol_set_bool(sm, EAPOL_eapFail, TRUE);
532 * RFC 4137 does not clear eapReq here, but this seems to be required
533 * to avoid processing the same request twice when state machine is
536 eapol_set_bool(sm, EAPOL_eapReq, FALSE);
539 * RFC 4137 does not set eapNoResp here. However, either eapResp or
540 * eapNoResp is required to be set after processing the received EAP
543 eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
545 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_FAILURE
546 "EAP authentication failed");
548 sm->prev_failure = 1;
552 static int eap_success_workaround(struct eap_sm *sm, int reqId, int lastId)
555 * At least Microsoft IAS and Meetinghouse Aegis seem to be sending
556 * EAP-Success/Failure with lastId + 1 even though RFC 3748 and
557 * RFC 4137 require that reqId == lastId. In addition, it looks like
558 * Ringmaster v2.1.2.0 would be using lastId + 2 in EAP-Success.
560 * Accept this kind of Id if EAP workarounds are enabled. These are
561 * unauthenticated plaintext messages, so this should have minimal
562 * security implications (bit easier to fake EAP-Success/Failure).
564 if (sm->workaround && (reqId == ((lastId + 1) & 0xff) ||
565 reqId == ((lastId + 2) & 0xff))) {
566 wpa_printf(MSG_DEBUG, "EAP: Workaround for unexpected "
567 "identifier field in EAP Success: "
568 "reqId=%d lastId=%d (these are supposed to be "
569 "same)", reqId, lastId);
572 wpa_printf(MSG_DEBUG, "EAP: EAP-Success Id mismatch - reqId=%d "
573 "lastId=%d", reqId, lastId);
579 * RFC 4137 - Appendix A.1: EAP Peer State Machine - State transitions
582 static void eap_peer_sm_step_idle(struct eap_sm *sm)
585 * The first three transitions are from RFC 4137. The last two are
586 * local additions to handle special cases with LEAP and PEAP server
587 * not sending EAP-Success in some cases.
589 if (eapol_get_bool(sm, EAPOL_eapReq))
590 SM_ENTER(EAP, RECEIVED);
591 else if ((eapol_get_bool(sm, EAPOL_altAccept) &&
592 sm->decision != DECISION_FAIL) ||
593 (eapol_get_int(sm, EAPOL_idleWhile) == 0 &&
594 sm->decision == DECISION_UNCOND_SUCC))
595 SM_ENTER(EAP, SUCCESS);
596 else if (eapol_get_bool(sm, EAPOL_altReject) ||
597 (eapol_get_int(sm, EAPOL_idleWhile) == 0 &&
598 sm->decision != DECISION_UNCOND_SUCC) ||
599 (eapol_get_bool(sm, EAPOL_altAccept) &&
600 sm->methodState != METHOD_CONT &&
601 sm->decision == DECISION_FAIL))
602 SM_ENTER(EAP, FAILURE);
603 else if (sm->selectedMethod == EAP_TYPE_LEAP &&
604 sm->leap_done && sm->decision != DECISION_FAIL &&
605 sm->methodState == METHOD_DONE)
606 SM_ENTER(EAP, SUCCESS);
607 else if (sm->selectedMethod == EAP_TYPE_PEAP &&
608 sm->peap_done && sm->decision != DECISION_FAIL &&
609 sm->methodState == METHOD_DONE)
610 SM_ENTER(EAP, SUCCESS);
614 static int eap_peer_req_is_duplicate(struct eap_sm *sm)
618 duplicate = (sm->reqId == sm->lastId) && sm->rxReq;
619 if (sm->workaround && duplicate &&
620 os_memcmp(sm->req_md5, sm->last_md5, 16) != 0) {
622 * RFC 4137 uses (reqId == lastId) as the only verification for
623 * duplicate EAP requests. However, this misses cases where the
624 * AS is incorrectly using the same id again; and
625 * unfortunately, such implementations exist. Use MD5 hash as
626 * an extra verification for the packets being duplicate to
627 * workaround these issues.
629 wpa_printf(MSG_DEBUG, "EAP: AS used the same Id again, but "
630 "EAP packets were not identical");
631 wpa_printf(MSG_DEBUG, "EAP: workaround - assume this is not a "
640 static void eap_peer_sm_step_received(struct eap_sm *sm)
642 int duplicate = eap_peer_req_is_duplicate(sm);
645 * Two special cases below for LEAP are local additions to work around
646 * odd LEAP behavior (EAP-Success in the middle of authentication and
647 * then swapped roles). Other transitions are based on RFC 4137.
649 if (sm->rxSuccess && sm->decision != DECISION_FAIL &&
650 (sm->reqId == sm->lastId ||
651 eap_success_workaround(sm, sm->reqId, sm->lastId)))
652 SM_ENTER(EAP, SUCCESS);
653 else if (sm->methodState != METHOD_CONT &&
655 sm->decision != DECISION_UNCOND_SUCC) ||
656 (sm->rxSuccess && sm->decision == DECISION_FAIL &&
657 (sm->selectedMethod != EAP_TYPE_LEAP ||
658 sm->methodState != METHOD_MAY_CONT))) &&
659 (sm->reqId == sm->lastId ||
660 eap_success_workaround(sm, sm->reqId, sm->lastId)))
661 SM_ENTER(EAP, FAILURE);
662 else if (sm->rxReq && duplicate)
663 SM_ENTER(EAP, RETRANSMIT);
664 else if (sm->rxReq && !duplicate &&
665 sm->reqMethod == EAP_TYPE_NOTIFICATION &&
666 sm->allowNotifications)
667 SM_ENTER(EAP, NOTIFICATION);
668 else if (sm->rxReq && !duplicate &&
669 sm->selectedMethod == EAP_TYPE_NONE &&
670 sm->reqMethod == EAP_TYPE_IDENTITY)
671 SM_ENTER(EAP, IDENTITY);
672 else if (sm->rxReq && !duplicate &&
673 sm->selectedMethod == EAP_TYPE_NONE &&
674 sm->reqMethod != EAP_TYPE_IDENTITY &&
675 sm->reqMethod != EAP_TYPE_NOTIFICATION)
676 SM_ENTER(EAP, GET_METHOD);
677 else if (sm->rxReq && !duplicate &&
678 sm->reqMethod == sm->selectedMethod &&
679 sm->methodState != METHOD_DONE)
680 SM_ENTER(EAP, METHOD);
681 else if (sm->selectedMethod == EAP_TYPE_LEAP &&
682 (sm->rxSuccess || sm->rxResp))
683 SM_ENTER(EAP, METHOD);
685 SM_ENTER(EAP, DISCARD);
689 static void eap_peer_sm_step_local(struct eap_sm *sm)
691 switch (sm->EAP_state) {
696 if (eapol_get_bool(sm, EAPOL_portEnabled) &&
698 SM_ENTER(EAP, INITIALIZE);
701 eap_peer_sm_step_idle(sm);
704 eap_peer_sm_step_received(sm);
707 if (sm->selectedMethod == sm->reqMethod)
708 SM_ENTER(EAP, METHOD);
710 SM_ENTER(EAP, SEND_RESPONSE);
714 SM_ENTER(EAP, DISCARD);
716 SM_ENTER(EAP, SEND_RESPONSE);
718 case EAP_SEND_RESPONSE:
725 SM_ENTER(EAP, SEND_RESPONSE);
727 case EAP_NOTIFICATION:
728 SM_ENTER(EAP, SEND_RESPONSE);
731 SM_ENTER(EAP, SEND_RESPONSE);
743 /* Global transitions */
744 if (eapol_get_bool(sm, EAPOL_eapRestart) &&
745 eapol_get_bool(sm, EAPOL_portEnabled))
746 SM_ENTER_GLOBAL(EAP, INITIALIZE);
747 else if (!eapol_get_bool(sm, EAPOL_portEnabled) || sm->force_disabled)
748 SM_ENTER_GLOBAL(EAP, DISABLED);
749 else if (sm->num_rounds > EAP_MAX_AUTH_ROUNDS) {
750 /* RFC 4137 does not place any limit on number of EAP messages
751 * in an authentication session. However, some error cases have
752 * ended up in a state were EAP messages were sent between the
753 * peer and server in a loop (e.g., TLS ACK frame in both
754 * direction). Since this is quite undesired outcome, limit the
755 * total number of EAP round-trips and abort authentication if
756 * this limit is exceeded.
758 if (sm->num_rounds == EAP_MAX_AUTH_ROUNDS + 1) {
759 wpa_msg(sm->msg_ctx, MSG_INFO, "EAP: more than %d "
760 "authentication rounds - abort",
761 EAP_MAX_AUTH_ROUNDS);
763 SM_ENTER_GLOBAL(EAP, FAILURE);
766 /* Local transitions */
767 eap_peer_sm_step_local(sm);
772 static Boolean eap_sm_allowMethod(struct eap_sm *sm, int vendor,
775 if (!eap_allowed_method(sm, vendor, method)) {
776 wpa_printf(MSG_DEBUG, "EAP: configuration does not allow: "
777 "vendor %u method %u", vendor, method);
780 if (eap_peer_get_eap_method(vendor, method))
782 wpa_printf(MSG_DEBUG, "EAP: not included in build: "
783 "vendor %u method %u", vendor, method);
788 static struct wpabuf * eap_sm_build_expanded_nak(
789 struct eap_sm *sm, int id, const struct eap_method *methods,
794 const struct eap_method *m;
796 wpa_printf(MSG_DEBUG, "EAP: Building expanded EAP-Nak");
798 /* RFC 3748 - 5.3.2: Expanded Nak */
799 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_EXPANDED,
800 8 + 8 * (count + 1), EAP_CODE_RESPONSE, id);
804 wpabuf_put_be24(resp, EAP_VENDOR_IETF);
805 wpabuf_put_be32(resp, EAP_TYPE_NAK);
807 for (m = methods; m; m = m->next) {
808 if (sm->reqVendor == m->vendor &&
809 sm->reqVendorMethod == m->method)
810 continue; /* do not allow the current method again */
811 if (eap_allowed_method(sm, m->vendor, m->method)) {
812 wpa_printf(MSG_DEBUG, "EAP: allowed type: "
813 "vendor=%u method=%u",
814 m->vendor, m->method);
815 wpabuf_put_u8(resp, EAP_TYPE_EXPANDED);
816 wpabuf_put_be24(resp, m->vendor);
817 wpabuf_put_be32(resp, m->method);
823 wpa_printf(MSG_DEBUG, "EAP: no more allowed methods");
824 wpabuf_put_u8(resp, EAP_TYPE_EXPANDED);
825 wpabuf_put_be24(resp, EAP_VENDOR_IETF);
826 wpabuf_put_be32(resp, EAP_TYPE_NONE);
829 eap_update_len(resp);
835 static struct wpabuf * eap_sm_buildNak(struct eap_sm *sm, int id)
839 int found = 0, expanded_found = 0;
841 const struct eap_method *methods, *m;
843 wpa_printf(MSG_DEBUG, "EAP: Building EAP-Nak (requested type %u "
844 "vendor=%u method=%u not allowed)", sm->reqMethod,
845 sm->reqVendor, sm->reqVendorMethod);
846 methods = eap_peer_get_methods(&count);
849 if (sm->reqMethod == EAP_TYPE_EXPANDED)
850 return eap_sm_build_expanded_nak(sm, id, methods, count);
852 /* RFC 3748 - 5.3.1: Legacy Nak */
853 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NAK,
854 sizeof(struct eap_hdr) + 1 + count + 1,
855 EAP_CODE_RESPONSE, id);
859 start = wpabuf_put(resp, 0);
860 for (m = methods; m; m = m->next) {
861 if (m->vendor == EAP_VENDOR_IETF && m->method == sm->reqMethod)
862 continue; /* do not allow the current method again */
863 if (eap_allowed_method(sm, m->vendor, m->method)) {
864 if (m->vendor != EAP_VENDOR_IETF) {
868 wpabuf_put_u8(resp, EAP_TYPE_EXPANDED);
870 wpabuf_put_u8(resp, m->method);
875 wpabuf_put_u8(resp, EAP_TYPE_NONE);
876 wpa_hexdump(MSG_DEBUG, "EAP: allowed methods", start, found);
878 eap_update_len(resp);
884 static void eap_sm_processIdentity(struct eap_sm *sm, const struct wpabuf *req)
889 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_STARTED
890 "EAP authentication started");
892 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY, req,
898 * RFC 3748 - 5.1: Identity
899 * Data field may contain a displayable message in UTF-8. If this
900 * includes NUL-character, only the data before that should be
901 * displayed. Some EAP implementasitons may piggy-back additional
902 * options after the NUL.
904 /* TODO: could save displayable message so that it can be shown to the
905 * user in case of interaction is required */
906 wpa_hexdump_ascii(MSG_DEBUG, "EAP: EAP-Request Identity data",
914 * Rules for figuring out MNC length based on IMSI for SIM cards that do not
915 * include MNC length field.
917 static int mnc_len_from_imsi(const char *imsi)
922 os_memcpy(mcc_str, imsi, 3);
927 return 2; /* Networks in Finland use 2-digit MNC */
933 static int eap_sm_append_3gpp_realm(struct eap_sm *sm, char *imsi,
934 size_t max_len, size_t *imsi_len)
939 if (*imsi_len + 36 > max_len) {
940 wpa_printf(MSG_WARNING, "No room for realm in IMSI buffer");
944 /* MNC (2 or 3 digits) */
945 mnc_len = scard_get_mnc_len(sm->scard_ctx);
947 mnc_len = mnc_len_from_imsi(imsi);
949 wpa_printf(MSG_INFO, "Failed to get MNC length from (U)SIM "
958 } else if (mnc_len == 3) {
965 pos = imsi + *imsi_len;
966 pos += os_snprintf(pos, imsi + max_len - pos,
967 "@wlan.mnc%s.mcc%c%c%c.3gppnetwork.org",
968 mnc, imsi[0], imsi[1], imsi[2]);
969 *imsi_len = pos - imsi;
975 static int eap_sm_imsi_identity(struct eap_sm *sm,
976 struct eap_peer_config *conf)
978 enum { EAP_SM_SIM, EAP_SM_AKA, EAP_SM_AKA_PRIME } method = EAP_SM_SIM;
981 struct eap_method_type *m = conf->eap_methods;
984 imsi_len = sizeof(imsi);
985 if (scard_get_imsi(sm->scard_ctx, imsi, &imsi_len)) {
986 wpa_printf(MSG_WARNING, "Failed to get IMSI from SIM");
990 wpa_hexdump_ascii(MSG_DEBUG, "IMSI", (u8 *) imsi, imsi_len);
993 wpa_printf(MSG_WARNING, "Too short IMSI for SIM identity");
997 if (eap_sm_append_3gpp_realm(sm, imsi, sizeof(imsi), &imsi_len) < 0) {
998 wpa_printf(MSG_WARNING, "Could not add realm to SIM identity");
1001 wpa_hexdump_ascii(MSG_DEBUG, "IMSI + realm", (u8 *) imsi, imsi_len);
1003 for (i = 0; m && (m[i].vendor != EAP_VENDOR_IETF ||
1004 m[i].method != EAP_TYPE_NONE); i++) {
1005 if (m[i].vendor == EAP_VENDOR_IETF &&
1006 m[i].method == EAP_TYPE_AKA_PRIME) {
1007 method = EAP_SM_AKA_PRIME;
1011 if (m[i].vendor == EAP_VENDOR_IETF &&
1012 m[i].method == EAP_TYPE_AKA) {
1013 method = EAP_SM_AKA;
1018 os_free(conf->identity);
1019 conf->identity = os_malloc(1 + imsi_len);
1020 if (conf->identity == NULL) {
1021 wpa_printf(MSG_WARNING, "Failed to allocate buffer for "
1022 "IMSI-based identity");
1028 conf->identity[0] = '1';
1031 conf->identity[0] = '0';
1033 case EAP_SM_AKA_PRIME:
1034 conf->identity[0] = '6';
1037 os_memcpy(conf->identity + 1, imsi, imsi_len);
1038 conf->identity_len = 1 + imsi_len;
1043 #endif /* PCSC_FUNCS */
1046 static int eap_sm_set_scard_pin(struct eap_sm *sm,
1047 struct eap_peer_config *conf)
1050 if (scard_set_pin(sm->scard_ctx, conf->pin)) {
1052 * Make sure the same PIN is not tried again in order to avoid
1058 wpa_printf(MSG_WARNING, "PIN validation failed");
1059 eap_sm_request_pin(sm);
1063 #else /* PCSC_FUNCS */
1065 #endif /* PCSC_FUNCS */
1068 static int eap_sm_get_scard_identity(struct eap_sm *sm,
1069 struct eap_peer_config *conf)
1072 if (eap_sm_set_scard_pin(sm, conf))
1075 return eap_sm_imsi_identity(sm, conf);
1076 #else /* PCSC_FUNCS */
1078 #endif /* PCSC_FUNCS */
1083 * eap_sm_buildIdentity - Build EAP-Identity/Response for the current network
1084 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1085 * @id: EAP identifier for the packet
1086 * @encrypted: Whether the packet is for encrypted tunnel (EAP phase 2)
1087 * Returns: Pointer to the allocated EAP-Identity/Response packet or %NULL on
1090 * This function allocates and builds an EAP-Identity/Response packet for the
1091 * current network. The caller is responsible for freeing the returned data.
1093 struct wpabuf * eap_sm_buildIdentity(struct eap_sm *sm, int id, int encrypted)
1095 struct eap_peer_config *config = eap_get_config(sm);
1096 struct wpabuf *resp;
1098 size_t identity_len;
1100 if (config == NULL) {
1101 wpa_printf(MSG_WARNING, "EAP: buildIdentity: configuration "
1102 "was not available");
1106 if (sm->m && sm->m->get_identity &&
1107 (identity = sm->m->get_identity(sm, sm->eap_method_priv,
1108 &identity_len)) != NULL) {
1109 wpa_hexdump_ascii(MSG_DEBUG, "EAP: using method re-auth "
1110 "identity", identity, identity_len);
1111 } else if (!encrypted && config->anonymous_identity) {
1112 identity = config->anonymous_identity;
1113 identity_len = config->anonymous_identity_len;
1114 wpa_hexdump_ascii(MSG_DEBUG, "EAP: using anonymous identity",
1115 identity, identity_len);
1117 identity = config->identity;
1118 identity_len = config->identity_len;
1119 wpa_hexdump_ascii(MSG_DEBUG, "EAP: using real identity",
1120 identity, identity_len);
1123 if (identity == NULL) {
1124 wpa_printf(MSG_WARNING, "EAP: buildIdentity: identity "
1125 "configuration was not available");
1127 if (eap_sm_get_scard_identity(sm, config) < 0)
1129 identity = config->identity;
1130 identity_len = config->identity_len;
1131 wpa_hexdump_ascii(MSG_DEBUG, "permanent identity from "
1132 "IMSI", identity, identity_len);
1134 eap_sm_request_identity(sm);
1137 } else if (config->pcsc) {
1138 if (eap_sm_set_scard_pin(sm, config) < 0)
1142 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY, identity_len,
1143 EAP_CODE_RESPONSE, id);
1147 wpabuf_put_data(resp, identity, identity_len);
1153 static void eap_sm_processNotify(struct eap_sm *sm, const struct wpabuf *req)
1159 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_NOTIFICATION, req,
1163 wpa_hexdump_ascii(MSG_DEBUG, "EAP: EAP-Request Notification data",
1166 msg = os_malloc(msg_len + 1);
1169 for (i = 0; i < msg_len; i++)
1170 msg[i] = isprint(pos[i]) ? (char) pos[i] : '_';
1171 msg[msg_len] = '\0';
1172 wpa_msg(sm->msg_ctx, MSG_INFO, "%s%s",
1173 WPA_EVENT_EAP_NOTIFICATION, msg);
1178 static struct wpabuf * eap_sm_buildNotify(int id)
1180 struct wpabuf *resp;
1182 wpa_printf(MSG_DEBUG, "EAP: Generating EAP-Response Notification");
1183 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NOTIFICATION, 0,
1184 EAP_CODE_RESPONSE, id);
1192 static void eap_sm_parseEapReq(struct eap_sm *sm, const struct wpabuf *req)
1194 const struct eap_hdr *hdr;
1198 sm->rxReq = sm->rxResp = sm->rxSuccess = sm->rxFailure = FALSE;
1200 sm->reqMethod = EAP_TYPE_NONE;
1201 sm->reqVendor = EAP_VENDOR_IETF;
1202 sm->reqVendorMethod = EAP_TYPE_NONE;
1204 if (req == NULL || wpabuf_len(req) < sizeof(*hdr))
1207 hdr = wpabuf_head(req);
1208 plen = be_to_host16(hdr->length);
1209 if (plen > wpabuf_len(req)) {
1210 wpa_printf(MSG_DEBUG, "EAP: Ignored truncated EAP-Packet "
1211 "(len=%lu plen=%lu)",
1212 (unsigned long) wpabuf_len(req),
1213 (unsigned long) plen);
1217 sm->reqId = hdr->identifier;
1219 if (sm->workaround) {
1221 addr[0] = wpabuf_head(req);
1222 md5_vector(1, addr, &plen, sm->req_md5);
1225 switch (hdr->code) {
1226 case EAP_CODE_REQUEST:
1227 if (plen < sizeof(*hdr) + 1) {
1228 wpa_printf(MSG_DEBUG, "EAP: Too short EAP-Request - "
1233 pos = (const u8 *) (hdr + 1);
1234 sm->reqMethod = *pos++;
1235 if (sm->reqMethod == EAP_TYPE_EXPANDED) {
1236 if (plen < sizeof(*hdr) + 8) {
1237 wpa_printf(MSG_DEBUG, "EAP: Ignored truncated "
1238 "expanded EAP-Packet (plen=%lu)",
1239 (unsigned long) plen);
1242 sm->reqVendor = WPA_GET_BE24(pos);
1244 sm->reqVendorMethod = WPA_GET_BE32(pos);
1246 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Request id=%d "
1247 "method=%u vendor=%u vendorMethod=%u",
1248 sm->reqId, sm->reqMethod, sm->reqVendor,
1249 sm->reqVendorMethod);
1251 case EAP_CODE_RESPONSE:
1252 if (sm->selectedMethod == EAP_TYPE_LEAP) {
1254 * LEAP differs from RFC 4137 by using reversed roles
1255 * for mutual authentication and because of this, we
1256 * need to accept EAP-Response frames if LEAP is used.
1258 if (plen < sizeof(*hdr) + 1) {
1259 wpa_printf(MSG_DEBUG, "EAP: Too short "
1260 "EAP-Response - no Type field");
1264 pos = (const u8 *) (hdr + 1);
1265 sm->reqMethod = *pos;
1266 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Response for "
1267 "LEAP method=%d id=%d",
1268 sm->reqMethod, sm->reqId);
1271 wpa_printf(MSG_DEBUG, "EAP: Ignored EAP-Response");
1273 case EAP_CODE_SUCCESS:
1274 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Success");
1275 eap_notify_status(sm, "completion", "success");
1276 sm->rxSuccess = TRUE;
1278 case EAP_CODE_FAILURE:
1279 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Failure");
1280 eap_notify_status(sm, "completion", "failure");
1281 sm->rxFailure = TRUE;
1284 wpa_printf(MSG_DEBUG, "EAP: Ignored EAP-Packet with unknown "
1285 "code %d", hdr->code);
1291 static void eap_peer_sm_tls_event(void *ctx, enum tls_event ev,
1292 union tls_event_data *data)
1294 struct eap_sm *sm = ctx;
1295 char *hash_hex = NULL;
1298 case TLS_CERT_CHAIN_SUCCESS:
1299 eap_notify_status(sm, "remote certificate verification",
1302 case TLS_CERT_CHAIN_FAILURE:
1303 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_TLS_CERT_ERROR
1304 "reason=%d depth=%d subject='%s' err='%s'",
1305 data->cert_fail.reason,
1306 data->cert_fail.depth,
1307 data->cert_fail.subject,
1308 data->cert_fail.reason_txt);
1309 eap_notify_status(sm, "remote certificate verification",
1310 data->cert_fail.reason_txt);
1312 case TLS_PEER_CERTIFICATE:
1313 if (!sm->eapol_cb->notify_cert)
1316 if (data->peer_cert.hash) {
1317 size_t len = data->peer_cert.hash_len * 2 + 1;
1318 hash_hex = os_malloc(len);
1320 wpa_snprintf_hex(hash_hex, len,
1321 data->peer_cert.hash,
1322 data->peer_cert.hash_len);
1326 sm->eapol_cb->notify_cert(sm->eapol_ctx,
1327 data->peer_cert.depth,
1328 data->peer_cert.subject,
1329 hash_hex, data->peer_cert.cert);
1332 if (data->alert.is_local)
1333 eap_notify_status(sm, "local TLS alert",
1334 data->alert.description);
1336 eap_notify_status(sm, "remote TLS alert",
1337 data->alert.description);
1346 * eap_peer_sm_init - Allocate and initialize EAP peer state machine
1347 * @eapol_ctx: Context data to be used with eapol_cb calls
1348 * @eapol_cb: Pointer to EAPOL callback functions
1349 * @msg_ctx: Context data for wpa_msg() calls
1350 * @conf: EAP configuration
1351 * Returns: Pointer to the allocated EAP state machine or %NULL on failure
1353 * This function allocates and initializes an EAP state machine. In addition,
1354 * this initializes TLS library for the new EAP state machine. eapol_cb pointer
1355 * will be in use until eap_peer_sm_deinit() is used to deinitialize this EAP
1356 * state machine. Consequently, the caller must make sure that this data
1357 * structure remains alive while the EAP state machine is active.
1359 struct eap_sm * eap_peer_sm_init(void *eapol_ctx,
1360 struct eapol_callbacks *eapol_cb,
1361 void *msg_ctx, struct eap_config *conf)
1364 struct tls_config tlsconf;
1366 sm = os_zalloc(sizeof(*sm));
1369 sm->eapol_ctx = eapol_ctx;
1370 sm->eapol_cb = eapol_cb;
1371 sm->msg_ctx = msg_ctx;
1372 sm->ClientTimeout = EAP_CLIENT_TIMEOUT_DEFAULT;
1373 sm->wps = conf->wps;
1375 os_memset(&tlsconf, 0, sizeof(tlsconf));
1376 tlsconf.opensc_engine_path = conf->opensc_engine_path;
1377 tlsconf.pkcs11_engine_path = conf->pkcs11_engine_path;
1378 tlsconf.pkcs11_module_path = conf->pkcs11_module_path;
1380 tlsconf.fips_mode = 1;
1381 #endif /* CONFIG_FIPS */
1382 tlsconf.event_cb = eap_peer_sm_tls_event;
1383 tlsconf.cb_ctx = sm;
1384 tlsconf.cert_in_cb = conf->cert_in_cb;
1385 sm->ssl_ctx = tls_init(&tlsconf);
1386 if (sm->ssl_ctx == NULL) {
1387 wpa_printf(MSG_WARNING, "SSL: Failed to initialize TLS "
1393 sm->ssl_ctx2 = tls_init(&tlsconf);
1394 if (sm->ssl_ctx2 == NULL) {
1395 wpa_printf(MSG_INFO, "SSL: Failed to initialize TLS "
1397 /* Run without separate TLS context within TLS tunnel */
1405 * eap_peer_sm_deinit - Deinitialize and free an EAP peer state machine
1406 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1408 * This function deinitializes EAP state machine and frees all allocated
1411 void eap_peer_sm_deinit(struct eap_sm *sm)
1415 eap_deinit_prev_method(sm, "EAP deinit");
1418 tls_deinit(sm->ssl_ctx2);
1419 tls_deinit(sm->ssl_ctx);
1425 * eap_peer_sm_step - Step EAP peer state machine
1426 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1427 * Returns: 1 if EAP state was changed or 0 if not
1429 * This function advances EAP state machine to a new state to match with the
1430 * current variables. This should be called whenever variables used by the EAP
1431 * state machine have changed.
1433 int eap_peer_sm_step(struct eap_sm *sm)
1437 sm->changed = FALSE;
1441 } while (sm->changed);
1447 * eap_sm_abort - Abort EAP authentication
1448 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1450 * Release system resources that have been allocated for the authentication
1451 * session without fully deinitializing the EAP state machine.
1453 void eap_sm_abort(struct eap_sm *sm)
1455 wpabuf_free(sm->lastRespData);
1456 sm->lastRespData = NULL;
1457 wpabuf_free(sm->eapRespData);
1458 sm->eapRespData = NULL;
1459 os_free(sm->eapKeyData);
1460 sm->eapKeyData = NULL;
1462 /* This is not clearly specified in the EAP statemachines draft, but
1463 * it seems necessary to make sure that some of the EAPOL variables get
1464 * cleared for the next authentication. */
1465 eapol_set_bool(sm, EAPOL_eapSuccess, FALSE);
1469 #ifdef CONFIG_CTRL_IFACE
1470 static const char * eap_sm_state_txt(int state)
1473 case EAP_INITIALIZE:
1474 return "INITIALIZE";
1481 case EAP_GET_METHOD:
1482 return "GET_METHOD";
1485 case EAP_SEND_RESPONSE:
1486 return "SEND_RESPONSE";
1491 case EAP_NOTIFICATION:
1492 return "NOTIFICATION";
1493 case EAP_RETRANSMIT:
1494 return "RETRANSMIT";
1503 #endif /* CONFIG_CTRL_IFACE */
1506 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
1507 static const char * eap_sm_method_state_txt(EapMethodState state)
1516 case METHOD_MAY_CONT:
1526 static const char * eap_sm_decision_txt(EapDecision decision)
1531 case DECISION_COND_SUCC:
1533 case DECISION_UNCOND_SUCC:
1534 return "UNCOND_SUCC";
1539 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1542 #ifdef CONFIG_CTRL_IFACE
1545 * eap_sm_get_status - Get EAP state machine status
1546 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1547 * @buf: Buffer for status information
1548 * @buflen: Maximum buffer length
1549 * @verbose: Whether to include verbose status information
1550 * Returns: Number of bytes written to buf.
1552 * Query EAP state machine for status information. This function fills in a
1553 * text area with current status information from the EAPOL state machine. If
1554 * the buffer (buf) is not large enough, status information will be truncated
1555 * to fit the buffer.
1557 int eap_sm_get_status(struct eap_sm *sm, char *buf, size_t buflen, int verbose)
1564 len = os_snprintf(buf, buflen,
1566 eap_sm_state_txt(sm->EAP_state));
1567 if (len < 0 || (size_t) len >= buflen)
1570 if (sm->selectedMethod != EAP_TYPE_NONE) {
1575 const struct eap_method *m =
1576 eap_peer_get_eap_method(EAP_VENDOR_IETF,
1577 sm->selectedMethod);
1583 ret = os_snprintf(buf + len, buflen - len,
1584 "selectedMethod=%d (EAP-%s)\n",
1585 sm->selectedMethod, name);
1586 if (ret < 0 || (size_t) ret >= buflen - len)
1590 if (sm->m && sm->m->get_status) {
1591 len += sm->m->get_status(sm, sm->eap_method_priv,
1592 buf + len, buflen - len,
1598 ret = os_snprintf(buf + len, buflen - len,
1602 "ClientTimeout=%d\n",
1604 eap_sm_method_state_txt(sm->methodState),
1605 eap_sm_decision_txt(sm->decision),
1607 if (ret < 0 || (size_t) ret >= buflen - len)
1614 #endif /* CONFIG_CTRL_IFACE */
1617 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
1618 static void eap_sm_request(struct eap_sm *sm, enum wpa_ctrl_req_type field,
1619 const char *msg, size_t msglen)
1621 struct eap_peer_config *config;
1622 char *txt = NULL, *tmp;
1626 config = eap_get_config(sm);
1631 case WPA_CTRL_REQ_EAP_IDENTITY:
1632 config->pending_req_identity++;
1634 case WPA_CTRL_REQ_EAP_PASSWORD:
1635 config->pending_req_password++;
1637 case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
1638 config->pending_req_new_password++;
1640 case WPA_CTRL_REQ_EAP_PIN:
1641 config->pending_req_pin++;
1643 case WPA_CTRL_REQ_EAP_OTP:
1645 tmp = os_malloc(msglen + 3);
1649 os_memcpy(tmp + 1, msg, msglen);
1650 tmp[msglen + 1] = ']';
1651 tmp[msglen + 2] = '\0';
1653 os_free(config->pending_req_otp);
1654 config->pending_req_otp = tmp;
1655 config->pending_req_otp_len = msglen + 3;
1657 if (config->pending_req_otp == NULL)
1659 txt = config->pending_req_otp;
1662 case WPA_CTRL_REQ_EAP_PASSPHRASE:
1663 config->pending_req_passphrase++;
1669 if (sm->eapol_cb->eap_param_needed)
1670 sm->eapol_cb->eap_param_needed(sm->eapol_ctx, field, txt);
1672 #else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1673 #define eap_sm_request(sm, type, msg, msglen) do { } while (0)
1674 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1676 const char * eap_sm_get_method_name(struct eap_sm *sm)
1685 * eap_sm_request_identity - Request identity from user (ctrl_iface)
1686 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1688 * EAP methods can call this function to request identity information for the
1689 * current network. This is normally called when the identity is not included
1690 * in the network configuration. The request will be sent to monitor programs
1691 * through the control interface.
1693 void eap_sm_request_identity(struct eap_sm *sm)
1695 eap_sm_request(sm, WPA_CTRL_REQ_EAP_IDENTITY, NULL, 0);
1700 * eap_sm_request_password - Request password from user (ctrl_iface)
1701 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1703 * EAP methods can call this function to request password information for the
1704 * current network. This is normally called when the password is not included
1705 * in the network configuration. The request will be sent to monitor programs
1706 * through the control interface.
1708 void eap_sm_request_password(struct eap_sm *sm)
1710 eap_sm_request(sm, WPA_CTRL_REQ_EAP_PASSWORD, NULL, 0);
1715 * eap_sm_request_new_password - Request new password from user (ctrl_iface)
1716 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1718 * EAP methods can call this function to request new password information for
1719 * the current network. This is normally called when the EAP method indicates
1720 * that the current password has expired and password change is required. The
1721 * request will be sent to monitor programs through the control interface.
1723 void eap_sm_request_new_password(struct eap_sm *sm)
1725 eap_sm_request(sm, WPA_CTRL_REQ_EAP_NEW_PASSWORD, NULL, 0);
1730 * eap_sm_request_pin - Request SIM or smart card PIN from user (ctrl_iface)
1731 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1733 * EAP methods can call this function to request SIM or smart card PIN
1734 * information for the current network. This is normally called when the PIN is
1735 * not included in the network configuration. The request will be sent to
1736 * monitor programs through the control interface.
1738 void eap_sm_request_pin(struct eap_sm *sm)
1740 eap_sm_request(sm, WPA_CTRL_REQ_EAP_PIN, NULL, 0);
1745 * eap_sm_request_otp - Request one time password from user (ctrl_iface)
1746 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1747 * @msg: Message to be displayed to the user when asking for OTP
1748 * @msg_len: Length of the user displayable message
1750 * EAP methods can call this function to request open time password (OTP) for
1751 * the current network. The request will be sent to monitor programs through
1752 * the control interface.
1754 void eap_sm_request_otp(struct eap_sm *sm, const char *msg, size_t msg_len)
1756 eap_sm_request(sm, WPA_CTRL_REQ_EAP_OTP, msg, msg_len);
1761 * eap_sm_request_passphrase - Request passphrase from user (ctrl_iface)
1762 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1764 * EAP methods can call this function to request passphrase for a private key
1765 * for the current network. This is normally called when the passphrase is not
1766 * included in the network configuration. The request will be sent to monitor
1767 * programs through the control interface.
1769 void eap_sm_request_passphrase(struct eap_sm *sm)
1771 eap_sm_request(sm, WPA_CTRL_REQ_EAP_PASSPHRASE, NULL, 0);
1776 * eap_sm_notify_ctrl_attached - Notification of attached monitor
1777 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1779 * Notify EAP state machines that a monitor was attached to the control
1780 * interface to trigger re-sending of pending requests for user input.
1782 void eap_sm_notify_ctrl_attached(struct eap_sm *sm)
1784 struct eap_peer_config *config = eap_get_config(sm);
1789 /* Re-send any pending requests for user data since a new control
1790 * interface was added. This handles cases where the EAP authentication
1791 * starts immediately after system startup when the user interface is
1792 * not yet running. */
1793 if (config->pending_req_identity)
1794 eap_sm_request_identity(sm);
1795 if (config->pending_req_password)
1796 eap_sm_request_password(sm);
1797 if (config->pending_req_new_password)
1798 eap_sm_request_new_password(sm);
1799 if (config->pending_req_otp)
1800 eap_sm_request_otp(sm, NULL, 0);
1801 if (config->pending_req_pin)
1802 eap_sm_request_pin(sm);
1803 if (config->pending_req_passphrase)
1804 eap_sm_request_passphrase(sm);
1808 static int eap_allowed_phase2_type(int vendor, int type)
1810 if (vendor != EAP_VENDOR_IETF)
1812 return type != EAP_TYPE_PEAP && type != EAP_TYPE_TTLS &&
1813 type != EAP_TYPE_FAST;
1818 * eap_get_phase2_type - Get EAP type for the given EAP phase 2 method name
1819 * @name: EAP method name, e.g., MD5
1820 * @vendor: Buffer for returning EAP Vendor-Id
1821 * Returns: EAP method type or %EAP_TYPE_NONE if not found
1823 * This function maps EAP type names into EAP type numbers that are allowed for
1824 * Phase 2, i.e., for tunneled authentication. Phase 2 is used, e.g., with
1825 * EAP-PEAP, EAP-TTLS, and EAP-FAST.
1827 u32 eap_get_phase2_type(const char *name, int *vendor)
1830 u8 type = eap_peer_get_type(name, &v);
1831 if (eap_allowed_phase2_type(v, type)) {
1835 *vendor = EAP_VENDOR_IETF;
1836 return EAP_TYPE_NONE;
1841 * eap_get_phase2_types - Get list of allowed EAP phase 2 types
1842 * @config: Pointer to a network configuration
1843 * @count: Pointer to a variable to be filled with number of returned EAP types
1844 * Returns: Pointer to allocated type list or %NULL on failure
1846 * This function generates an array of allowed EAP phase 2 (tunneled) types for
1847 * the given network configuration.
1849 struct eap_method_type * eap_get_phase2_types(struct eap_peer_config *config,
1852 struct eap_method_type *buf;
1856 const struct eap_method *methods, *m;
1858 methods = eap_peer_get_methods(&mcount);
1859 if (methods == NULL)
1862 buf = os_malloc(mcount * sizeof(struct eap_method_type));
1866 for (m = methods; m; m = m->next) {
1869 if (eap_allowed_phase2_type(vendor, method)) {
1870 if (vendor == EAP_VENDOR_IETF &&
1871 method == EAP_TYPE_TLS && config &&
1872 config->private_key2 == NULL)
1874 buf[*count].vendor = vendor;
1875 buf[*count].method = method;
1885 * eap_set_fast_reauth - Update fast_reauth setting
1886 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1887 * @enabled: 1 = Fast reauthentication is enabled, 0 = Disabled
1889 void eap_set_fast_reauth(struct eap_sm *sm, int enabled)
1891 sm->fast_reauth = enabled;
1896 * eap_set_workaround - Update EAP workarounds setting
1897 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1898 * @workaround: 1 = Enable EAP workarounds, 0 = Disable EAP workarounds
1900 void eap_set_workaround(struct eap_sm *sm, unsigned int workaround)
1902 sm->workaround = workaround;
1907 * eap_get_config - Get current network configuration
1908 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1909 * Returns: Pointer to the current network configuration or %NULL if not found
1911 * EAP peer methods should avoid using this function if they can use other
1912 * access functions, like eap_get_config_identity() and
1913 * eap_get_config_password(), that do not require direct access to
1914 * struct eap_peer_config.
1916 struct eap_peer_config * eap_get_config(struct eap_sm *sm)
1918 return sm->eapol_cb->get_config(sm->eapol_ctx);
1923 * eap_get_config_identity - Get identity from the network configuration
1924 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1925 * @len: Buffer for the length of the identity
1926 * Returns: Pointer to the identity or %NULL if not found
1928 const u8 * eap_get_config_identity(struct eap_sm *sm, size_t *len)
1930 struct eap_peer_config *config = eap_get_config(sm);
1933 *len = config->identity_len;
1934 return config->identity;
1938 static int eap_get_ext_password(struct eap_sm *sm,
1939 struct eap_peer_config *config)
1943 if (config->password == NULL)
1946 name = os_zalloc(config->password_len + 1);
1949 os_memcpy(name, config->password, config->password_len);
1951 ext_password_free(sm->ext_pw_buf);
1952 sm->ext_pw_buf = ext_password_get(sm->ext_pw, name);
1955 return sm->ext_pw_buf == NULL ? -1 : 0;
1960 * eap_get_config_password - Get password from the network configuration
1961 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1962 * @len: Buffer for the length of the password
1963 * Returns: Pointer to the password or %NULL if not found
1965 const u8 * eap_get_config_password(struct eap_sm *sm, size_t *len)
1967 struct eap_peer_config *config = eap_get_config(sm);
1971 if (config->flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1972 if (eap_get_ext_password(sm, config) < 0)
1974 *len = wpabuf_len(sm->ext_pw_buf);
1975 return wpabuf_head(sm->ext_pw_buf);
1978 *len = config->password_len;
1979 return config->password;
1984 * eap_get_config_password2 - Get password from the network configuration
1985 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1986 * @len: Buffer for the length of the password
1987 * @hash: Buffer for returning whether the password is stored as a
1988 * NtPasswordHash instead of plaintext password; can be %NULL if this
1989 * information is not needed
1990 * Returns: Pointer to the password or %NULL if not found
1992 const u8 * eap_get_config_password2(struct eap_sm *sm, size_t *len, int *hash)
1994 struct eap_peer_config *config = eap_get_config(sm);
1998 if (config->flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1999 if (eap_get_ext_password(sm, config) < 0)
2001 *len = wpabuf_len(sm->ext_pw_buf);
2002 return wpabuf_head(sm->ext_pw_buf);
2005 *len = config->password_len;
2007 *hash = !!(config->flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH);
2008 return config->password;
2013 * eap_get_config_new_password - Get new password from network configuration
2014 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2015 * @len: Buffer for the length of the new password
2016 * Returns: Pointer to the new password or %NULL if not found
2018 const u8 * eap_get_config_new_password(struct eap_sm *sm, size_t *len)
2020 struct eap_peer_config *config = eap_get_config(sm);
2023 *len = config->new_password_len;
2024 return config->new_password;
2029 * eap_get_config_otp - Get one-time password from the network configuration
2030 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2031 * @len: Buffer for the length of the one-time password
2032 * Returns: Pointer to the one-time password or %NULL if not found
2034 const u8 * eap_get_config_otp(struct eap_sm *sm, size_t *len)
2036 struct eap_peer_config *config = eap_get_config(sm);
2039 *len = config->otp_len;
2045 * eap_clear_config_otp - Clear used one-time password
2046 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2048 * This function clears a used one-time password (OTP) from the current network
2049 * configuration. This should be called when the OTP has been used and is not
2052 void eap_clear_config_otp(struct eap_sm *sm)
2054 struct eap_peer_config *config = eap_get_config(sm);
2057 os_memset(config->otp, 0, config->otp_len);
2058 os_free(config->otp);
2060 config->otp_len = 0;
2065 * eap_get_config_phase1 - Get phase1 data from the network configuration
2066 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2067 * Returns: Pointer to the phase1 data or %NULL if not found
2069 const char * eap_get_config_phase1(struct eap_sm *sm)
2071 struct eap_peer_config *config = eap_get_config(sm);
2074 return config->phase1;
2079 * eap_get_config_phase2 - Get phase2 data from the network configuration
2080 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2081 * Returns: Pointer to the phase1 data or %NULL if not found
2083 const char * eap_get_config_phase2(struct eap_sm *sm)
2085 struct eap_peer_config *config = eap_get_config(sm);
2088 return config->phase2;
2092 int eap_get_config_fragment_size(struct eap_sm *sm)
2094 struct eap_peer_config *config = eap_get_config(sm);
2097 return config->fragment_size;
2102 * eap_key_available - Get key availability (eapKeyAvailable variable)
2103 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2104 * Returns: 1 if EAP keying material is available, 0 if not
2106 int eap_key_available(struct eap_sm *sm)
2108 return sm ? sm->eapKeyAvailable : 0;
2113 * eap_notify_success - Notify EAP state machine about external success trigger
2114 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2116 * This function is called when external event, e.g., successful completion of
2117 * WPA-PSK key handshake, is indicating that EAP state machine should move to
2118 * success state. This is mainly used with security modes that do not use EAP
2119 * state machine (e.g., WPA-PSK).
2121 void eap_notify_success(struct eap_sm *sm)
2124 sm->decision = DECISION_COND_SUCC;
2125 sm->EAP_state = EAP_SUCCESS;
2131 * eap_notify_lower_layer_success - Notification of lower layer success
2132 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2134 * Notify EAP state machines that a lower layer has detected a successful
2135 * authentication. This is used to recover from dropped EAP-Success messages.
2137 void eap_notify_lower_layer_success(struct eap_sm *sm)
2142 if (eapol_get_bool(sm, EAPOL_eapSuccess) ||
2143 sm->decision == DECISION_FAIL ||
2144 (sm->methodState != METHOD_MAY_CONT &&
2145 sm->methodState != METHOD_DONE))
2148 if (sm->eapKeyData != NULL)
2149 sm->eapKeyAvailable = TRUE;
2150 eapol_set_bool(sm, EAPOL_eapSuccess, TRUE);
2151 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
2152 "EAP authentication completed successfully (based on lower "
2158 * eap_get_eapKeyData - Get master session key (MSK) from EAP state machine
2159 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2160 * @len: Pointer to variable that will be set to number of bytes in the key
2161 * Returns: Pointer to the EAP keying data or %NULL on failure
2163 * Fetch EAP keying material (MSK, eapKeyData) from the EAP state machine. The
2164 * key is available only after a successful authentication. EAP state machine
2165 * continues to manage the key data and the caller must not change or free the
2168 const u8 * eap_get_eapKeyData(struct eap_sm *sm, size_t *len)
2170 if (sm == NULL || sm->eapKeyData == NULL) {
2175 *len = sm->eapKeyDataLen;
2176 return sm->eapKeyData;
2181 * eap_get_eapKeyData - Get EAP response data
2182 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2183 * Returns: Pointer to the EAP response (eapRespData) or %NULL on failure
2185 * Fetch EAP response (eapRespData) from the EAP state machine. This data is
2186 * available when EAP state machine has processed an incoming EAP request. The
2187 * EAP state machine does not maintain a reference to the response after this
2188 * function is called and the caller is responsible for freeing the data.
2190 struct wpabuf * eap_get_eapRespData(struct eap_sm *sm)
2192 struct wpabuf *resp;
2194 if (sm == NULL || sm->eapRespData == NULL)
2197 resp = sm->eapRespData;
2198 sm->eapRespData = NULL;
2205 * eap_sm_register_scard_ctx - Notification of smart card context
2206 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2207 * @ctx: Context data for smart card operations
2209 * Notify EAP state machines of context data for smart card operations. This
2210 * context data will be used as a parameter for scard_*() functions.
2212 void eap_register_scard_ctx(struct eap_sm *sm, void *ctx)
2215 sm->scard_ctx = ctx;
2220 * eap_set_config_blob - Set or add a named configuration blob
2221 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2222 * @blob: New value for the blob
2224 * Adds a new configuration blob or replaces the current value of an existing
2227 void eap_set_config_blob(struct eap_sm *sm, struct wpa_config_blob *blob)
2229 #ifndef CONFIG_NO_CONFIG_BLOBS
2230 sm->eapol_cb->set_config_blob(sm->eapol_ctx, blob);
2231 #endif /* CONFIG_NO_CONFIG_BLOBS */
2236 * eap_get_config_blob - Get a named configuration blob
2237 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2238 * @name: Name of the blob
2239 * Returns: Pointer to blob data or %NULL if not found
2241 const struct wpa_config_blob * eap_get_config_blob(struct eap_sm *sm,
2244 #ifndef CONFIG_NO_CONFIG_BLOBS
2245 return sm->eapol_cb->get_config_blob(sm->eapol_ctx, name);
2246 #else /* CONFIG_NO_CONFIG_BLOBS */
2248 #endif /* CONFIG_NO_CONFIG_BLOBS */
2253 * eap_set_force_disabled - Set force_disabled flag
2254 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2255 * @disabled: 1 = EAP disabled, 0 = EAP enabled
2257 * This function is used to force EAP state machine to be disabled when it is
2258 * not in use (e.g., with WPA-PSK or plaintext connections).
2260 void eap_set_force_disabled(struct eap_sm *sm, int disabled)
2262 sm->force_disabled = disabled;
2267 * eap_notify_pending - Notify that EAP method is ready to re-process a request
2268 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2270 * An EAP method can perform a pending operation (e.g., to get a response from
2271 * an external process). Once the response is available, this function can be
2272 * used to request EAPOL state machine to retry delivering the previously
2273 * received (and still unanswered) EAP request to EAP state machine.
2275 void eap_notify_pending(struct eap_sm *sm)
2277 sm->eapol_cb->notify_pending(sm->eapol_ctx);
2282 * eap_invalidate_cached_session - Mark cached session data invalid
2283 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2285 void eap_invalidate_cached_session(struct eap_sm *sm)
2288 eap_deinit_prev_method(sm, "invalidate");
2292 int eap_is_wps_pbc_enrollee(struct eap_peer_config *conf)
2294 if (conf->identity_len != WSC_ID_ENROLLEE_LEN ||
2295 os_memcmp(conf->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN))
2296 return 0; /* Not a WPS Enrollee */
2298 if (conf->phase1 == NULL || os_strstr(conf->phase1, "pbc=1") == NULL)
2299 return 0; /* Not using PBC */
2305 int eap_is_wps_pin_enrollee(struct eap_peer_config *conf)
2307 if (conf->identity_len != WSC_ID_ENROLLEE_LEN ||
2308 os_memcmp(conf->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN))
2309 return 0; /* Not a WPS Enrollee */
2311 if (conf->phase1 == NULL || os_strstr(conf->phase1, "pin=") == NULL)
2312 return 0; /* Not using PIN */
2318 void eap_sm_set_ext_pw_ctx(struct eap_sm *sm, struct ext_password_data *ext)
2320 ext_password_free(sm->ext_pw_buf);
2321 sm->ext_pw_buf = NULL;
2327 * eap_set_anon_id - Set or add anonymous identity
2328 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2329 * @id: Anonymous identity (e.g., EAP-SIM pseudonym) or %NULL to clear
2330 * @len: Length of anonymous identity in octets
2332 void eap_set_anon_id(struct eap_sm *sm, const u8 *id, size_t len)
2334 if (sm->eapol_cb->set_anon_id)
2335 sm->eapol_cb->set_anon_id(sm->eapol_ctx, id, len);