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;
353 SM_ENTRY(EAP, METHOD);
355 wpa_printf(MSG_WARNING, "EAP::METHOD - method not selected");
359 eapReqData = eapol_get_eapReqData(sm);
360 if (sm->m->vendor == EAP_VENDOR_IETF && sm->m->method == EAP_TYPE_LEAP)
361 min_len = 0; /* LEAP uses EAP-Success without payload */
362 if (!eap_hdr_len_valid(eapReqData, min_len))
366 * Get ignore, methodState, decision, allowNotifications, and
367 * eapRespData. RFC 4137 uses three separate method procedure (check,
368 * process, and buildResp) in this state. These have been combined into
369 * a single function call to m->process() in order to optimize EAP
370 * method implementation interface a bit. These procedures are only
371 * used from within this METHOD state, so there is no need to keep
372 * these as separate C functions.
374 * The RFC 4137 procedures return values as follows:
375 * ignore = m.check(eapReqData)
376 * (methodState, decision, allowNotifications) = m.process(eapReqData)
377 * eapRespData = m.buildResp(reqId)
379 os_memset(&ret, 0, sizeof(ret));
380 ret.ignore = sm->ignore;
381 ret.methodState = sm->methodState;
382 ret.decision = sm->decision;
383 ret.allowNotifications = sm->allowNotifications;
384 wpabuf_free(sm->eapRespData);
385 sm->eapRespData = NULL;
386 sm->eapRespData = sm->m->process(sm, sm->eap_method_priv, &ret,
388 wpa_printf(MSG_DEBUG, "EAP: method process -> ignore=%s "
389 "methodState=%s decision=%s",
390 ret.ignore ? "TRUE" : "FALSE",
391 eap_sm_method_state_txt(ret.methodState),
392 eap_sm_decision_txt(ret.decision));
394 sm->ignore = ret.ignore;
397 sm->methodState = ret.methodState;
398 sm->decision = ret.decision;
399 sm->allowNotifications = ret.allowNotifications;
401 if (sm->m->isKeyAvailable && sm->m->getKey &&
402 sm->m->isKeyAvailable(sm, sm->eap_method_priv)) {
403 os_free(sm->eapKeyData);
404 sm->eapKeyData = sm->m->getKey(sm, sm->eap_method_priv,
411 * This state signals the lower layer that a response packet is ready to be
414 SM_STATE(EAP, SEND_RESPONSE)
416 SM_ENTRY(EAP, SEND_RESPONSE);
417 wpabuf_free(sm->lastRespData);
418 if (sm->eapRespData) {
420 os_memcpy(sm->last_md5, sm->req_md5, 16);
421 sm->lastId = sm->reqId;
422 sm->lastRespData = wpabuf_dup(sm->eapRespData);
423 eapol_set_bool(sm, EAPOL_eapResp, TRUE);
425 sm->lastRespData = NULL;
426 eapol_set_bool(sm, EAPOL_eapReq, FALSE);
427 eapol_set_int(sm, EAPOL_idleWhile, sm->ClientTimeout);
432 * This state signals the lower layer that the request was discarded, and no
433 * response packet will be sent at this time.
435 SM_STATE(EAP, DISCARD)
437 SM_ENTRY(EAP, DISCARD);
438 eapol_set_bool(sm, EAPOL_eapReq, FALSE);
439 eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
444 * Handles requests for Identity method and builds a response.
446 SM_STATE(EAP, IDENTITY)
448 const struct wpabuf *eapReqData;
450 SM_ENTRY(EAP, IDENTITY);
451 eapReqData = eapol_get_eapReqData(sm);
452 if (!eap_hdr_len_valid(eapReqData, 1))
454 eap_sm_processIdentity(sm, eapReqData);
455 wpabuf_free(sm->eapRespData);
456 sm->eapRespData = NULL;
457 sm->eapRespData = eap_sm_buildIdentity(sm, sm->reqId, 0);
462 * Handles requests for Notification method and builds a response.
464 SM_STATE(EAP, NOTIFICATION)
466 const struct wpabuf *eapReqData;
468 SM_ENTRY(EAP, NOTIFICATION);
469 eapReqData = eapol_get_eapReqData(sm);
470 if (!eap_hdr_len_valid(eapReqData, 1))
472 eap_sm_processNotify(sm, eapReqData);
473 wpabuf_free(sm->eapRespData);
474 sm->eapRespData = NULL;
475 sm->eapRespData = eap_sm_buildNotify(sm->reqId);
480 * This state retransmits the previous response packet.
482 SM_STATE(EAP, RETRANSMIT)
484 SM_ENTRY(EAP, RETRANSMIT);
485 wpabuf_free(sm->eapRespData);
486 if (sm->lastRespData)
487 sm->eapRespData = wpabuf_dup(sm->lastRespData);
489 sm->eapRespData = NULL;
494 * This state is entered in case of a successful completion of authentication
495 * and state machine waits here until port is disabled or EAP authentication is
498 SM_STATE(EAP, SUCCESS)
500 SM_ENTRY(EAP, SUCCESS);
501 if (sm->eapKeyData != NULL)
502 sm->eapKeyAvailable = TRUE;
503 eapol_set_bool(sm, EAPOL_eapSuccess, TRUE);
506 * RFC 4137 does not clear eapReq here, but this seems to be required
507 * to avoid processing the same request twice when state machine is
510 eapol_set_bool(sm, EAPOL_eapReq, FALSE);
513 * RFC 4137 does not set eapNoResp here, but this seems to be required
514 * to get EAPOL Supplicant backend state machine into SUCCESS state. In
515 * addition, either eapResp or eapNoResp is required to be set after
516 * processing the received EAP frame.
518 eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
520 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
521 "EAP authentication completed successfully");
526 * This state is entered in case of a failure and state machine waits here
527 * until port is disabled or EAP authentication is restarted.
529 SM_STATE(EAP, FAILURE)
531 SM_ENTRY(EAP, FAILURE);
532 eapol_set_bool(sm, EAPOL_eapFail, TRUE);
535 * RFC 4137 does not clear eapReq here, but this seems to be required
536 * to avoid processing the same request twice when state machine is
539 eapol_set_bool(sm, EAPOL_eapReq, FALSE);
542 * RFC 4137 does not set eapNoResp here. However, either eapResp or
543 * eapNoResp is required to be set after processing the received EAP
546 eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
548 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_FAILURE
549 "EAP authentication failed");
551 sm->prev_failure = 1;
555 static int eap_success_workaround(struct eap_sm *sm, int reqId, int lastId)
558 * At least Microsoft IAS and Meetinghouse Aegis seem to be sending
559 * EAP-Success/Failure with lastId + 1 even though RFC 3748 and
560 * RFC 4137 require that reqId == lastId. In addition, it looks like
561 * Ringmaster v2.1.2.0 would be using lastId + 2 in EAP-Success.
563 * Accept this kind of Id if EAP workarounds are enabled. These are
564 * unauthenticated plaintext messages, so this should have minimal
565 * security implications (bit easier to fake EAP-Success/Failure).
567 if (sm->workaround && (reqId == ((lastId + 1) & 0xff) ||
568 reqId == ((lastId + 2) & 0xff))) {
569 wpa_printf(MSG_DEBUG, "EAP: Workaround for unexpected "
570 "identifier field in EAP Success: "
571 "reqId=%d lastId=%d (these are supposed to be "
572 "same)", reqId, lastId);
575 wpa_printf(MSG_DEBUG, "EAP: EAP-Success Id mismatch - reqId=%d "
576 "lastId=%d", reqId, lastId);
582 * RFC 4137 - Appendix A.1: EAP Peer State Machine - State transitions
585 static void eap_peer_sm_step_idle(struct eap_sm *sm)
588 * The first three transitions are from RFC 4137. The last two are
589 * local additions to handle special cases with LEAP and PEAP server
590 * not sending EAP-Success in some cases.
592 if (eapol_get_bool(sm, EAPOL_eapReq))
593 SM_ENTER(EAP, RECEIVED);
594 else if ((eapol_get_bool(sm, EAPOL_altAccept) &&
595 sm->decision != DECISION_FAIL) ||
596 (eapol_get_int(sm, EAPOL_idleWhile) == 0 &&
597 sm->decision == DECISION_UNCOND_SUCC))
598 SM_ENTER(EAP, SUCCESS);
599 else if (eapol_get_bool(sm, EAPOL_altReject) ||
600 (eapol_get_int(sm, EAPOL_idleWhile) == 0 &&
601 sm->decision != DECISION_UNCOND_SUCC) ||
602 (eapol_get_bool(sm, EAPOL_altAccept) &&
603 sm->methodState != METHOD_CONT &&
604 sm->decision == DECISION_FAIL))
605 SM_ENTER(EAP, FAILURE);
606 else if (sm->selectedMethod == EAP_TYPE_LEAP &&
607 sm->leap_done && sm->decision != DECISION_FAIL &&
608 sm->methodState == METHOD_DONE)
609 SM_ENTER(EAP, SUCCESS);
610 else if (sm->selectedMethod == EAP_TYPE_PEAP &&
611 sm->peap_done && sm->decision != DECISION_FAIL &&
612 sm->methodState == METHOD_DONE)
613 SM_ENTER(EAP, SUCCESS);
617 static int eap_peer_req_is_duplicate(struct eap_sm *sm)
621 duplicate = (sm->reqId == sm->lastId) && sm->rxReq;
622 if (sm->workaround && duplicate &&
623 os_memcmp(sm->req_md5, sm->last_md5, 16) != 0) {
625 * RFC 4137 uses (reqId == lastId) as the only verification for
626 * duplicate EAP requests. However, this misses cases where the
627 * AS is incorrectly using the same id again; and
628 * unfortunately, such implementations exist. Use MD5 hash as
629 * an extra verification for the packets being duplicate to
630 * workaround these issues.
632 wpa_printf(MSG_DEBUG, "EAP: AS used the same Id again, but "
633 "EAP packets were not identical");
634 wpa_printf(MSG_DEBUG, "EAP: workaround - assume this is not a "
643 static void eap_peer_sm_step_received(struct eap_sm *sm)
645 int duplicate = eap_peer_req_is_duplicate(sm);
648 * Two special cases below for LEAP are local additions to work around
649 * odd LEAP behavior (EAP-Success in the middle of authentication and
650 * then swapped roles). Other transitions are based on RFC 4137.
652 if (sm->rxSuccess && sm->decision != DECISION_FAIL &&
653 (sm->reqId == sm->lastId ||
654 eap_success_workaround(sm, sm->reqId, sm->lastId)))
655 SM_ENTER(EAP, SUCCESS);
656 else if (sm->methodState != METHOD_CONT &&
658 sm->decision != DECISION_UNCOND_SUCC) ||
659 (sm->rxSuccess && sm->decision == DECISION_FAIL &&
660 (sm->selectedMethod != EAP_TYPE_LEAP ||
661 sm->methodState != METHOD_MAY_CONT))) &&
662 (sm->reqId == sm->lastId ||
663 eap_success_workaround(sm, sm->reqId, sm->lastId)))
664 SM_ENTER(EAP, FAILURE);
665 else if (sm->rxReq && duplicate)
666 SM_ENTER(EAP, RETRANSMIT);
667 else if (sm->rxReq && !duplicate &&
668 sm->reqMethod == EAP_TYPE_NOTIFICATION &&
669 sm->allowNotifications)
670 SM_ENTER(EAP, NOTIFICATION);
671 else if (sm->rxReq && !duplicate &&
672 sm->selectedMethod == EAP_TYPE_NONE &&
673 sm->reqMethod == EAP_TYPE_IDENTITY)
674 SM_ENTER(EAP, IDENTITY);
675 else if (sm->rxReq && !duplicate &&
676 sm->selectedMethod == EAP_TYPE_NONE &&
677 sm->reqMethod != EAP_TYPE_IDENTITY &&
678 sm->reqMethod != EAP_TYPE_NOTIFICATION)
679 SM_ENTER(EAP, GET_METHOD);
680 else if (sm->rxReq && !duplicate &&
681 sm->reqMethod == sm->selectedMethod &&
682 sm->methodState != METHOD_DONE)
683 SM_ENTER(EAP, METHOD);
684 else if (sm->selectedMethod == EAP_TYPE_LEAP &&
685 (sm->rxSuccess || sm->rxResp))
686 SM_ENTER(EAP, METHOD);
688 SM_ENTER(EAP, DISCARD);
692 static void eap_peer_sm_step_local(struct eap_sm *sm)
694 switch (sm->EAP_state) {
699 if (eapol_get_bool(sm, EAPOL_portEnabled) &&
701 SM_ENTER(EAP, INITIALIZE);
704 eap_peer_sm_step_idle(sm);
707 eap_peer_sm_step_received(sm);
710 if (sm->selectedMethod == sm->reqMethod)
711 SM_ENTER(EAP, METHOD);
713 SM_ENTER(EAP, SEND_RESPONSE);
717 SM_ENTER(EAP, DISCARD);
719 SM_ENTER(EAP, SEND_RESPONSE);
721 case EAP_SEND_RESPONSE:
728 SM_ENTER(EAP, SEND_RESPONSE);
730 case EAP_NOTIFICATION:
731 SM_ENTER(EAP, SEND_RESPONSE);
734 SM_ENTER(EAP, SEND_RESPONSE);
746 /* Global transitions */
747 if (eapol_get_bool(sm, EAPOL_eapRestart) &&
748 eapol_get_bool(sm, EAPOL_portEnabled))
749 SM_ENTER_GLOBAL(EAP, INITIALIZE);
750 else if (!eapol_get_bool(sm, EAPOL_portEnabled) || sm->force_disabled)
751 SM_ENTER_GLOBAL(EAP, DISABLED);
752 else if (sm->num_rounds > EAP_MAX_AUTH_ROUNDS) {
753 /* RFC 4137 does not place any limit on number of EAP messages
754 * in an authentication session. However, some error cases have
755 * ended up in a state were EAP messages were sent between the
756 * peer and server in a loop (e.g., TLS ACK frame in both
757 * direction). Since this is quite undesired outcome, limit the
758 * total number of EAP round-trips and abort authentication if
759 * this limit is exceeded.
761 if (sm->num_rounds == EAP_MAX_AUTH_ROUNDS + 1) {
762 wpa_msg(sm->msg_ctx, MSG_INFO, "EAP: more than %d "
763 "authentication rounds - abort",
764 EAP_MAX_AUTH_ROUNDS);
766 SM_ENTER_GLOBAL(EAP, FAILURE);
769 /* Local transitions */
770 eap_peer_sm_step_local(sm);
775 static Boolean eap_sm_allowMethod(struct eap_sm *sm, int vendor,
778 if (!eap_allowed_method(sm, vendor, method)) {
779 wpa_printf(MSG_DEBUG, "EAP: configuration does not allow: "
780 "vendor %u method %u", vendor, method);
783 if (eap_peer_get_eap_method(vendor, method))
785 wpa_printf(MSG_DEBUG, "EAP: not included in build: "
786 "vendor %u method %u", vendor, method);
791 static struct wpabuf * eap_sm_build_expanded_nak(
792 struct eap_sm *sm, int id, const struct eap_method *methods,
797 const struct eap_method *m;
799 wpa_printf(MSG_DEBUG, "EAP: Building expanded EAP-Nak");
801 /* RFC 3748 - 5.3.2: Expanded Nak */
802 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_EXPANDED,
803 8 + 8 * (count + 1), EAP_CODE_RESPONSE, id);
807 wpabuf_put_be24(resp, EAP_VENDOR_IETF);
808 wpabuf_put_be32(resp, EAP_TYPE_NAK);
810 for (m = methods; m; m = m->next) {
811 if (sm->reqVendor == m->vendor &&
812 sm->reqVendorMethod == m->method)
813 continue; /* do not allow the current method again */
814 if (eap_allowed_method(sm, m->vendor, m->method)) {
815 wpa_printf(MSG_DEBUG, "EAP: allowed type: "
816 "vendor=%u method=%u",
817 m->vendor, m->method);
818 wpabuf_put_u8(resp, EAP_TYPE_EXPANDED);
819 wpabuf_put_be24(resp, m->vendor);
820 wpabuf_put_be32(resp, m->method);
826 wpa_printf(MSG_DEBUG, "EAP: no more allowed methods");
827 wpabuf_put_u8(resp, EAP_TYPE_EXPANDED);
828 wpabuf_put_be24(resp, EAP_VENDOR_IETF);
829 wpabuf_put_be32(resp, EAP_TYPE_NONE);
832 eap_update_len(resp);
838 static struct wpabuf * eap_sm_buildNak(struct eap_sm *sm, int id)
842 int found = 0, expanded_found = 0;
844 const struct eap_method *methods, *m;
846 wpa_printf(MSG_DEBUG, "EAP: Building EAP-Nak (requested type %u "
847 "vendor=%u method=%u not allowed)", sm->reqMethod,
848 sm->reqVendor, sm->reqVendorMethod);
849 methods = eap_peer_get_methods(&count);
852 if (sm->reqMethod == EAP_TYPE_EXPANDED)
853 return eap_sm_build_expanded_nak(sm, id, methods, count);
855 /* RFC 3748 - 5.3.1: Legacy Nak */
856 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NAK,
857 sizeof(struct eap_hdr) + 1 + count + 1,
858 EAP_CODE_RESPONSE, id);
862 start = wpabuf_put(resp, 0);
863 for (m = methods; m; m = m->next) {
864 if (m->vendor == EAP_VENDOR_IETF && m->method == sm->reqMethod)
865 continue; /* do not allow the current method again */
866 if (eap_allowed_method(sm, m->vendor, m->method)) {
867 if (m->vendor != EAP_VENDOR_IETF) {
871 wpabuf_put_u8(resp, EAP_TYPE_EXPANDED);
873 wpabuf_put_u8(resp, m->method);
878 wpabuf_put_u8(resp, EAP_TYPE_NONE);
879 wpa_hexdump(MSG_DEBUG, "EAP: allowed methods", start, found);
881 eap_update_len(resp);
887 static void eap_sm_processIdentity(struct eap_sm *sm, const struct wpabuf *req)
892 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_STARTED
893 "EAP authentication started");
895 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY, req,
901 * RFC 3748 - 5.1: Identity
902 * Data field may contain a displayable message in UTF-8. If this
903 * includes NUL-character, only the data before that should be
904 * displayed. Some EAP implementasitons may piggy-back additional
905 * options after the NUL.
907 /* TODO: could save displayable message so that it can be shown to the
908 * user in case of interaction is required */
909 wpa_hexdump_ascii(MSG_DEBUG, "EAP: EAP-Request Identity data",
917 * Rules for figuring out MNC length based on IMSI for SIM cards that do not
918 * include MNC length field.
920 static int mnc_len_from_imsi(const char *imsi)
925 os_memcpy(mcc_str, imsi, 3);
930 return 2; /* Networks in Finland use 2-digit MNC */
936 static int eap_sm_append_3gpp_realm(struct eap_sm *sm, char *imsi,
937 size_t max_len, size_t *imsi_len)
942 if (*imsi_len + 36 > max_len) {
943 wpa_printf(MSG_WARNING, "No room for realm in IMSI buffer");
947 /* MNC (2 or 3 digits) */
948 mnc_len = scard_get_mnc_len(sm->scard_ctx);
950 mnc_len = mnc_len_from_imsi(imsi);
952 wpa_printf(MSG_INFO, "Failed to get MNC length from (U)SIM "
961 } else if (mnc_len == 3) {
968 pos = imsi + *imsi_len;
969 pos += os_snprintf(pos, imsi + max_len - pos,
970 "@wlan.mnc%s.mcc%c%c%c.3gppnetwork.org",
971 mnc, imsi[0], imsi[1], imsi[2]);
972 *imsi_len = pos - imsi;
978 static int eap_sm_imsi_identity(struct eap_sm *sm,
979 struct eap_peer_config *conf)
981 enum { EAP_SM_SIM, EAP_SM_AKA, EAP_SM_AKA_PRIME } method = EAP_SM_SIM;
984 struct eap_method_type *m = conf->eap_methods;
987 imsi_len = sizeof(imsi);
988 if (scard_get_imsi(sm->scard_ctx, imsi, &imsi_len)) {
989 wpa_printf(MSG_WARNING, "Failed to get IMSI from SIM");
993 wpa_hexdump_ascii(MSG_DEBUG, "IMSI", (u8 *) imsi, imsi_len);
996 wpa_printf(MSG_WARNING, "Too short IMSI for SIM identity");
1000 if (eap_sm_append_3gpp_realm(sm, imsi, sizeof(imsi), &imsi_len) < 0) {
1001 wpa_printf(MSG_WARNING, "Could not add realm to SIM identity");
1004 wpa_hexdump_ascii(MSG_DEBUG, "IMSI + realm", (u8 *) imsi, imsi_len);
1006 for (i = 0; m && (m[i].vendor != EAP_VENDOR_IETF ||
1007 m[i].method != EAP_TYPE_NONE); i++) {
1008 if (m[i].vendor == EAP_VENDOR_IETF &&
1009 m[i].method == EAP_TYPE_AKA_PRIME) {
1010 method = EAP_SM_AKA_PRIME;
1014 if (m[i].vendor == EAP_VENDOR_IETF &&
1015 m[i].method == EAP_TYPE_AKA) {
1016 method = EAP_SM_AKA;
1021 os_free(conf->identity);
1022 conf->identity = os_malloc(1 + imsi_len);
1023 if (conf->identity == NULL) {
1024 wpa_printf(MSG_WARNING, "Failed to allocate buffer for "
1025 "IMSI-based identity");
1031 conf->identity[0] = '1';
1034 conf->identity[0] = '0';
1036 case EAP_SM_AKA_PRIME:
1037 conf->identity[0] = '6';
1040 os_memcpy(conf->identity + 1, imsi, imsi_len);
1041 conf->identity_len = 1 + imsi_len;
1046 #endif /* PCSC_FUNCS */
1049 static int eap_sm_set_scard_pin(struct eap_sm *sm,
1050 struct eap_peer_config *conf)
1053 if (scard_set_pin(sm->scard_ctx, conf->pin)) {
1055 * Make sure the same PIN is not tried again in order to avoid
1061 wpa_printf(MSG_WARNING, "PIN validation failed");
1062 eap_sm_request_pin(sm);
1066 #else /* PCSC_FUNCS */
1068 #endif /* PCSC_FUNCS */
1071 static int eap_sm_get_scard_identity(struct eap_sm *sm,
1072 struct eap_peer_config *conf)
1075 if (eap_sm_set_scard_pin(sm, conf))
1078 return eap_sm_imsi_identity(sm, conf);
1079 #else /* PCSC_FUNCS */
1081 #endif /* PCSC_FUNCS */
1086 * eap_sm_buildIdentity - Build EAP-Identity/Response for the current network
1087 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1088 * @id: EAP identifier for the packet
1089 * @encrypted: Whether the packet is for encrypted tunnel (EAP phase 2)
1090 * Returns: Pointer to the allocated EAP-Identity/Response packet or %NULL on
1093 * This function allocates and builds an EAP-Identity/Response packet for the
1094 * current network. The caller is responsible for freeing the returned data.
1096 struct wpabuf * eap_sm_buildIdentity(struct eap_sm *sm, int id, int encrypted)
1098 struct eap_peer_config *config = eap_get_config(sm);
1099 struct wpabuf *resp;
1101 size_t identity_len;
1103 if (config == NULL) {
1104 wpa_printf(MSG_WARNING, "EAP: buildIdentity: configuration "
1105 "was not available");
1109 if (sm->m && sm->m->get_identity &&
1110 (identity = sm->m->get_identity(sm, sm->eap_method_priv,
1111 &identity_len)) != NULL) {
1112 wpa_hexdump_ascii(MSG_DEBUG, "EAP: using method re-auth "
1113 "identity", identity, identity_len);
1114 } else if (!encrypted && config->anonymous_identity) {
1115 identity = config->anonymous_identity;
1116 identity_len = config->anonymous_identity_len;
1117 wpa_hexdump_ascii(MSG_DEBUG, "EAP: using anonymous identity",
1118 identity, identity_len);
1120 identity = config->identity;
1121 identity_len = config->identity_len;
1122 wpa_hexdump_ascii(MSG_DEBUG, "EAP: using real identity",
1123 identity, identity_len);
1126 if (identity == NULL) {
1127 wpa_printf(MSG_WARNING, "EAP: buildIdentity: identity "
1128 "configuration was not available");
1130 if (eap_sm_get_scard_identity(sm, config) < 0)
1132 identity = config->identity;
1133 identity_len = config->identity_len;
1134 wpa_hexdump_ascii(MSG_DEBUG, "permanent identity from "
1135 "IMSI", identity, identity_len);
1137 eap_sm_request_identity(sm);
1140 } else if (config->pcsc) {
1141 if (eap_sm_set_scard_pin(sm, config) < 0)
1145 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY, identity_len,
1146 EAP_CODE_RESPONSE, id);
1150 wpabuf_put_data(resp, identity, identity_len);
1156 static void eap_sm_processNotify(struct eap_sm *sm, const struct wpabuf *req)
1162 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_NOTIFICATION, req,
1166 wpa_hexdump_ascii(MSG_DEBUG, "EAP: EAP-Request Notification data",
1169 msg = os_malloc(msg_len + 1);
1172 for (i = 0; i < msg_len; i++)
1173 msg[i] = isprint(pos[i]) ? (char) pos[i] : '_';
1174 msg[msg_len] = '\0';
1175 wpa_msg(sm->msg_ctx, MSG_INFO, "%s%s",
1176 WPA_EVENT_EAP_NOTIFICATION, msg);
1181 static struct wpabuf * eap_sm_buildNotify(int id)
1183 struct wpabuf *resp;
1185 wpa_printf(MSG_DEBUG, "EAP: Generating EAP-Response Notification");
1186 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NOTIFICATION, 0,
1187 EAP_CODE_RESPONSE, id);
1195 static void eap_sm_parseEapReq(struct eap_sm *sm, const struct wpabuf *req)
1197 const struct eap_hdr *hdr;
1201 sm->rxReq = sm->rxResp = sm->rxSuccess = sm->rxFailure = FALSE;
1203 sm->reqMethod = EAP_TYPE_NONE;
1204 sm->reqVendor = EAP_VENDOR_IETF;
1205 sm->reqVendorMethod = EAP_TYPE_NONE;
1207 if (req == NULL || wpabuf_len(req) < sizeof(*hdr))
1210 hdr = wpabuf_head(req);
1211 plen = be_to_host16(hdr->length);
1212 if (plen > wpabuf_len(req)) {
1213 wpa_printf(MSG_DEBUG, "EAP: Ignored truncated EAP-Packet "
1214 "(len=%lu plen=%lu)",
1215 (unsigned long) wpabuf_len(req),
1216 (unsigned long) plen);
1220 sm->reqId = hdr->identifier;
1222 if (sm->workaround) {
1224 addr[0] = wpabuf_head(req);
1225 md5_vector(1, addr, &plen, sm->req_md5);
1228 switch (hdr->code) {
1229 case EAP_CODE_REQUEST:
1230 if (plen < sizeof(*hdr) + 1) {
1231 wpa_printf(MSG_DEBUG, "EAP: Too short EAP-Request - "
1236 pos = (const u8 *) (hdr + 1);
1237 sm->reqMethod = *pos++;
1238 if (sm->reqMethod == EAP_TYPE_EXPANDED) {
1239 if (plen < sizeof(*hdr) + 8) {
1240 wpa_printf(MSG_DEBUG, "EAP: Ignored truncated "
1241 "expanded EAP-Packet (plen=%lu)",
1242 (unsigned long) plen);
1245 sm->reqVendor = WPA_GET_BE24(pos);
1247 sm->reqVendorMethod = WPA_GET_BE32(pos);
1249 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Request id=%d "
1250 "method=%u vendor=%u vendorMethod=%u",
1251 sm->reqId, sm->reqMethod, sm->reqVendor,
1252 sm->reqVendorMethod);
1254 case EAP_CODE_RESPONSE:
1255 if (sm->selectedMethod == EAP_TYPE_LEAP) {
1257 * LEAP differs from RFC 4137 by using reversed roles
1258 * for mutual authentication and because of this, we
1259 * need to accept EAP-Response frames if LEAP is used.
1261 if (plen < sizeof(*hdr) + 1) {
1262 wpa_printf(MSG_DEBUG, "EAP: Too short "
1263 "EAP-Response - no Type field");
1267 pos = (const u8 *) (hdr + 1);
1268 sm->reqMethod = *pos;
1269 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Response for "
1270 "LEAP method=%d id=%d",
1271 sm->reqMethod, sm->reqId);
1274 wpa_printf(MSG_DEBUG, "EAP: Ignored EAP-Response");
1276 case EAP_CODE_SUCCESS:
1277 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Success");
1278 eap_notify_status(sm, "completion", "success");
1279 sm->rxSuccess = TRUE;
1281 case EAP_CODE_FAILURE:
1282 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Failure");
1283 eap_notify_status(sm, "completion", "failure");
1284 sm->rxFailure = TRUE;
1287 wpa_printf(MSG_DEBUG, "EAP: Ignored EAP-Packet with unknown "
1288 "code %d", hdr->code);
1294 static void eap_peer_sm_tls_event(void *ctx, enum tls_event ev,
1295 union tls_event_data *data)
1297 struct eap_sm *sm = ctx;
1298 char *hash_hex = NULL;
1301 case TLS_CERT_CHAIN_SUCCESS:
1302 eap_notify_status(sm, "remote certificate verification",
1305 case TLS_CERT_CHAIN_FAILURE:
1306 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_TLS_CERT_ERROR
1307 "reason=%d depth=%d subject='%s' err='%s'",
1308 data->cert_fail.reason,
1309 data->cert_fail.depth,
1310 data->cert_fail.subject,
1311 data->cert_fail.reason_txt);
1312 eap_notify_status(sm, "remote certificate verification",
1313 data->cert_fail.reason_txt);
1315 case TLS_PEER_CERTIFICATE:
1316 if (!sm->eapol_cb->notify_cert)
1319 if (data->peer_cert.hash) {
1320 size_t len = data->peer_cert.hash_len * 2 + 1;
1321 hash_hex = os_malloc(len);
1323 wpa_snprintf_hex(hash_hex, len,
1324 data->peer_cert.hash,
1325 data->peer_cert.hash_len);
1329 sm->eapol_cb->notify_cert(sm->eapol_ctx,
1330 data->peer_cert.depth,
1331 data->peer_cert.subject,
1332 hash_hex, data->peer_cert.cert);
1335 if (data->alert.is_local)
1336 eap_notify_status(sm, "local TLS alert",
1337 data->alert.description);
1339 eap_notify_status(sm, "remote TLS alert",
1340 data->alert.description);
1349 * eap_peer_sm_init - Allocate and initialize EAP peer state machine
1350 * @eapol_ctx: Context data to be used with eapol_cb calls
1351 * @eapol_cb: Pointer to EAPOL callback functions
1352 * @msg_ctx: Context data for wpa_msg() calls
1353 * @conf: EAP configuration
1354 * Returns: Pointer to the allocated EAP state machine or %NULL on failure
1356 * This function allocates and initializes an EAP state machine. In addition,
1357 * this initializes TLS library for the new EAP state machine. eapol_cb pointer
1358 * will be in use until eap_peer_sm_deinit() is used to deinitialize this EAP
1359 * state machine. Consequently, the caller must make sure that this data
1360 * structure remains alive while the EAP state machine is active.
1362 struct eap_sm * eap_peer_sm_init(void *eapol_ctx,
1363 struct eapol_callbacks *eapol_cb,
1364 void *msg_ctx, struct eap_config *conf)
1367 struct tls_config tlsconf;
1369 sm = os_zalloc(sizeof(*sm));
1372 sm->eapol_ctx = eapol_ctx;
1373 sm->eapol_cb = eapol_cb;
1374 sm->msg_ctx = msg_ctx;
1375 sm->ClientTimeout = EAP_CLIENT_TIMEOUT_DEFAULT;
1376 sm->wps = conf->wps;
1378 os_memset(&tlsconf, 0, sizeof(tlsconf));
1379 tlsconf.opensc_engine_path = conf->opensc_engine_path;
1380 tlsconf.pkcs11_engine_path = conf->pkcs11_engine_path;
1381 tlsconf.pkcs11_module_path = conf->pkcs11_module_path;
1383 tlsconf.fips_mode = 1;
1384 #endif /* CONFIG_FIPS */
1385 tlsconf.event_cb = eap_peer_sm_tls_event;
1386 tlsconf.cb_ctx = sm;
1387 tlsconf.cert_in_cb = conf->cert_in_cb;
1388 sm->ssl_ctx = tls_init(&tlsconf);
1389 if (sm->ssl_ctx == NULL) {
1390 wpa_printf(MSG_WARNING, "SSL: Failed to initialize TLS "
1396 sm->ssl_ctx2 = tls_init(&tlsconf);
1397 if (sm->ssl_ctx2 == NULL) {
1398 wpa_printf(MSG_INFO, "SSL: Failed to initialize TLS "
1400 /* Run without separate TLS context within TLS tunnel */
1408 * eap_peer_sm_deinit - Deinitialize and free an EAP peer state machine
1409 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1411 * This function deinitializes EAP state machine and frees all allocated
1414 void eap_peer_sm_deinit(struct eap_sm *sm)
1418 eap_deinit_prev_method(sm, "EAP deinit");
1421 tls_deinit(sm->ssl_ctx2);
1422 tls_deinit(sm->ssl_ctx);
1428 * eap_peer_sm_step - Step EAP peer state machine
1429 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1430 * Returns: 1 if EAP state was changed or 0 if not
1432 * This function advances EAP state machine to a new state to match with the
1433 * current variables. This should be called whenever variables used by the EAP
1434 * state machine have changed.
1436 int eap_peer_sm_step(struct eap_sm *sm)
1440 sm->changed = FALSE;
1444 } while (sm->changed);
1450 * eap_sm_abort - Abort EAP authentication
1451 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1453 * Release system resources that have been allocated for the authentication
1454 * session without fully deinitializing the EAP state machine.
1456 void eap_sm_abort(struct eap_sm *sm)
1458 wpabuf_free(sm->lastRespData);
1459 sm->lastRespData = NULL;
1460 wpabuf_free(sm->eapRespData);
1461 sm->eapRespData = NULL;
1462 os_free(sm->eapKeyData);
1463 sm->eapKeyData = NULL;
1465 /* This is not clearly specified in the EAP statemachines draft, but
1466 * it seems necessary to make sure that some of the EAPOL variables get
1467 * cleared for the next authentication. */
1468 eapol_set_bool(sm, EAPOL_eapSuccess, FALSE);
1472 #ifdef CONFIG_CTRL_IFACE
1473 static const char * eap_sm_state_txt(int state)
1476 case EAP_INITIALIZE:
1477 return "INITIALIZE";
1484 case EAP_GET_METHOD:
1485 return "GET_METHOD";
1488 case EAP_SEND_RESPONSE:
1489 return "SEND_RESPONSE";
1494 case EAP_NOTIFICATION:
1495 return "NOTIFICATION";
1496 case EAP_RETRANSMIT:
1497 return "RETRANSMIT";
1506 #endif /* CONFIG_CTRL_IFACE */
1509 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
1510 static const char * eap_sm_method_state_txt(EapMethodState state)
1519 case METHOD_MAY_CONT:
1529 static const char * eap_sm_decision_txt(EapDecision decision)
1534 case DECISION_COND_SUCC:
1536 case DECISION_UNCOND_SUCC:
1537 return "UNCOND_SUCC";
1542 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1545 #ifdef CONFIG_CTRL_IFACE
1548 * eap_sm_get_status - Get EAP state machine status
1549 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1550 * @buf: Buffer for status information
1551 * @buflen: Maximum buffer length
1552 * @verbose: Whether to include verbose status information
1553 * Returns: Number of bytes written to buf.
1555 * Query EAP state machine for status information. This function fills in a
1556 * text area with current status information from the EAPOL state machine. If
1557 * the buffer (buf) is not large enough, status information will be truncated
1558 * to fit the buffer.
1560 int eap_sm_get_status(struct eap_sm *sm, char *buf, size_t buflen, int verbose)
1567 len = os_snprintf(buf, buflen,
1569 eap_sm_state_txt(sm->EAP_state));
1570 if (len < 0 || (size_t) len >= buflen)
1573 if (sm->selectedMethod != EAP_TYPE_NONE) {
1578 const struct eap_method *m =
1579 eap_peer_get_eap_method(EAP_VENDOR_IETF,
1580 sm->selectedMethod);
1586 ret = os_snprintf(buf + len, buflen - len,
1587 "selectedMethod=%d (EAP-%s)\n",
1588 sm->selectedMethod, name);
1589 if (ret < 0 || (size_t) ret >= buflen - len)
1593 if (sm->m && sm->m->get_status) {
1594 len += sm->m->get_status(sm, sm->eap_method_priv,
1595 buf + len, buflen - len,
1601 ret = os_snprintf(buf + len, buflen - len,
1605 "ClientTimeout=%d\n",
1607 eap_sm_method_state_txt(sm->methodState),
1608 eap_sm_decision_txt(sm->decision),
1610 if (ret < 0 || (size_t) ret >= buflen - len)
1617 #endif /* CONFIG_CTRL_IFACE */
1620 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
1621 static void eap_sm_request(struct eap_sm *sm, enum wpa_ctrl_req_type field,
1622 const char *msg, size_t msglen)
1624 struct eap_peer_config *config;
1625 char *txt = NULL, *tmp;
1629 config = eap_get_config(sm);
1634 case WPA_CTRL_REQ_EAP_IDENTITY:
1635 config->pending_req_identity++;
1637 case WPA_CTRL_REQ_EAP_PASSWORD:
1638 config->pending_req_password++;
1640 case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
1641 config->pending_req_new_password++;
1643 case WPA_CTRL_REQ_EAP_PIN:
1644 config->pending_req_pin++;
1646 case WPA_CTRL_REQ_EAP_OTP:
1648 tmp = os_malloc(msglen + 3);
1652 os_memcpy(tmp + 1, msg, msglen);
1653 tmp[msglen + 1] = ']';
1654 tmp[msglen + 2] = '\0';
1656 os_free(config->pending_req_otp);
1657 config->pending_req_otp = tmp;
1658 config->pending_req_otp_len = msglen + 3;
1660 if (config->pending_req_otp == NULL)
1662 txt = config->pending_req_otp;
1665 case WPA_CTRL_REQ_EAP_PASSPHRASE:
1666 config->pending_req_passphrase++;
1672 if (sm->eapol_cb->eap_param_needed)
1673 sm->eapol_cb->eap_param_needed(sm->eapol_ctx, field, txt);
1675 #else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1676 #define eap_sm_request(sm, type, msg, msglen) do { } while (0)
1677 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1679 const char * eap_sm_get_method_name(struct eap_sm *sm)
1688 * eap_sm_request_identity - Request identity from user (ctrl_iface)
1689 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1691 * EAP methods can call this function to request identity information for the
1692 * current network. This is normally called when the identity is not included
1693 * in the network configuration. The request will be sent to monitor programs
1694 * through the control interface.
1696 void eap_sm_request_identity(struct eap_sm *sm)
1698 eap_sm_request(sm, WPA_CTRL_REQ_EAP_IDENTITY, NULL, 0);
1703 * eap_sm_request_password - Request password from user (ctrl_iface)
1704 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1706 * EAP methods can call this function to request password information for the
1707 * current network. This is normally called when the password is not included
1708 * in the network configuration. The request will be sent to monitor programs
1709 * through the control interface.
1711 void eap_sm_request_password(struct eap_sm *sm)
1713 eap_sm_request(sm, WPA_CTRL_REQ_EAP_PASSWORD, NULL, 0);
1718 * eap_sm_request_new_password - Request new password from user (ctrl_iface)
1719 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1721 * EAP methods can call this function to request new password information for
1722 * the current network. This is normally called when the EAP method indicates
1723 * that the current password has expired and password change is required. The
1724 * request will be sent to monitor programs through the control interface.
1726 void eap_sm_request_new_password(struct eap_sm *sm)
1728 eap_sm_request(sm, WPA_CTRL_REQ_EAP_NEW_PASSWORD, NULL, 0);
1733 * eap_sm_request_pin - Request SIM or smart card PIN from user (ctrl_iface)
1734 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1736 * EAP methods can call this function to request SIM or smart card PIN
1737 * information for the current network. This is normally called when the PIN is
1738 * not included in the network configuration. The request will be sent to
1739 * monitor programs through the control interface.
1741 void eap_sm_request_pin(struct eap_sm *sm)
1743 eap_sm_request(sm, WPA_CTRL_REQ_EAP_PIN, NULL, 0);
1748 * eap_sm_request_otp - Request one time password from user (ctrl_iface)
1749 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1750 * @msg: Message to be displayed to the user when asking for OTP
1751 * @msg_len: Length of the user displayable message
1753 * EAP methods can call this function to request open time password (OTP) for
1754 * the current network. The request will be sent to monitor programs through
1755 * the control interface.
1757 void eap_sm_request_otp(struct eap_sm *sm, const char *msg, size_t msg_len)
1759 eap_sm_request(sm, WPA_CTRL_REQ_EAP_OTP, msg, msg_len);
1764 * eap_sm_request_passphrase - Request passphrase from user (ctrl_iface)
1765 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1767 * EAP methods can call this function to request passphrase for a private key
1768 * for the current network. This is normally called when the passphrase is not
1769 * included in the network configuration. The request will be sent to monitor
1770 * programs through the control interface.
1772 void eap_sm_request_passphrase(struct eap_sm *sm)
1774 eap_sm_request(sm, WPA_CTRL_REQ_EAP_PASSPHRASE, NULL, 0);
1779 * eap_sm_notify_ctrl_attached - Notification of attached monitor
1780 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1782 * Notify EAP state machines that a monitor was attached to the control
1783 * interface to trigger re-sending of pending requests for user input.
1785 void eap_sm_notify_ctrl_attached(struct eap_sm *sm)
1787 struct eap_peer_config *config = eap_get_config(sm);
1792 /* Re-send any pending requests for user data since a new control
1793 * interface was added. This handles cases where the EAP authentication
1794 * starts immediately after system startup when the user interface is
1795 * not yet running. */
1796 if (config->pending_req_identity)
1797 eap_sm_request_identity(sm);
1798 if (config->pending_req_password)
1799 eap_sm_request_password(sm);
1800 if (config->pending_req_new_password)
1801 eap_sm_request_new_password(sm);
1802 if (config->pending_req_otp)
1803 eap_sm_request_otp(sm, NULL, 0);
1804 if (config->pending_req_pin)
1805 eap_sm_request_pin(sm);
1806 if (config->pending_req_passphrase)
1807 eap_sm_request_passphrase(sm);
1811 static int eap_allowed_phase2_type(int vendor, int type)
1813 if (vendor != EAP_VENDOR_IETF)
1815 return type != EAP_TYPE_PEAP && type != EAP_TYPE_TTLS &&
1816 type != EAP_TYPE_FAST;
1821 * eap_get_phase2_type - Get EAP type for the given EAP phase 2 method name
1822 * @name: EAP method name, e.g., MD5
1823 * @vendor: Buffer for returning EAP Vendor-Id
1824 * Returns: EAP method type or %EAP_TYPE_NONE if not found
1826 * This function maps EAP type names into EAP type numbers that are allowed for
1827 * Phase 2, i.e., for tunneled authentication. Phase 2 is used, e.g., with
1828 * EAP-PEAP, EAP-TTLS, and EAP-FAST.
1830 u32 eap_get_phase2_type(const char *name, int *vendor)
1833 u8 type = eap_peer_get_type(name, &v);
1834 if (eap_allowed_phase2_type(v, type)) {
1838 *vendor = EAP_VENDOR_IETF;
1839 return EAP_TYPE_NONE;
1844 * eap_get_phase2_types - Get list of allowed EAP phase 2 types
1845 * @config: Pointer to a network configuration
1846 * @count: Pointer to a variable to be filled with number of returned EAP types
1847 * Returns: Pointer to allocated type list or %NULL on failure
1849 * This function generates an array of allowed EAP phase 2 (tunneled) types for
1850 * the given network configuration.
1852 struct eap_method_type * eap_get_phase2_types(struct eap_peer_config *config,
1855 struct eap_method_type *buf;
1859 const struct eap_method *methods, *m;
1861 methods = eap_peer_get_methods(&mcount);
1862 if (methods == NULL)
1865 buf = os_malloc(mcount * sizeof(struct eap_method_type));
1869 for (m = methods; m; m = m->next) {
1872 if (eap_allowed_phase2_type(vendor, method)) {
1873 if (vendor == EAP_VENDOR_IETF &&
1874 method == EAP_TYPE_TLS && config &&
1875 config->private_key2 == NULL)
1877 buf[*count].vendor = vendor;
1878 buf[*count].method = method;
1888 * eap_set_fast_reauth - Update fast_reauth setting
1889 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1890 * @enabled: 1 = Fast reauthentication is enabled, 0 = Disabled
1892 void eap_set_fast_reauth(struct eap_sm *sm, int enabled)
1894 sm->fast_reauth = enabled;
1899 * eap_set_workaround - Update EAP workarounds setting
1900 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1901 * @workaround: 1 = Enable EAP workarounds, 0 = Disable EAP workarounds
1903 void eap_set_workaround(struct eap_sm *sm, unsigned int workaround)
1905 sm->workaround = workaround;
1910 * eap_get_config - Get current network configuration
1911 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1912 * Returns: Pointer to the current network configuration or %NULL if not found
1914 * EAP peer methods should avoid using this function if they can use other
1915 * access functions, like eap_get_config_identity() and
1916 * eap_get_config_password(), that do not require direct access to
1917 * struct eap_peer_config.
1919 struct eap_peer_config * eap_get_config(struct eap_sm *sm)
1921 return sm->eapol_cb->get_config(sm->eapol_ctx);
1926 * eap_get_config_identity - Get identity from the network configuration
1927 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1928 * @len: Buffer for the length of the identity
1929 * Returns: Pointer to the identity or %NULL if not found
1931 const u8 * eap_get_config_identity(struct eap_sm *sm, size_t *len)
1933 struct eap_peer_config *config = eap_get_config(sm);
1936 *len = config->identity_len;
1937 return config->identity;
1941 static int eap_get_ext_password(struct eap_sm *sm,
1942 struct eap_peer_config *config)
1946 if (config->password == NULL)
1949 name = os_zalloc(config->password_len + 1);
1952 os_memcpy(name, config->password, config->password_len);
1954 ext_password_free(sm->ext_pw_buf);
1955 sm->ext_pw_buf = ext_password_get(sm->ext_pw, name);
1958 return sm->ext_pw_buf == NULL ? -1 : 0;
1963 * eap_get_config_password - Get password from the network configuration
1964 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1965 * @len: Buffer for the length of the password
1966 * Returns: Pointer to the password or %NULL if not found
1968 const u8 * eap_get_config_password(struct eap_sm *sm, size_t *len)
1970 struct eap_peer_config *config = eap_get_config(sm);
1974 if (config->flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1975 if (eap_get_ext_password(sm, config) < 0)
1977 *len = wpabuf_len(sm->ext_pw_buf);
1978 return wpabuf_head(sm->ext_pw_buf);
1981 *len = config->password_len;
1982 return config->password;
1987 * eap_get_config_password2 - Get password from the network configuration
1988 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1989 * @len: Buffer for the length of the password
1990 * @hash: Buffer for returning whether the password is stored as a
1991 * NtPasswordHash instead of plaintext password; can be %NULL if this
1992 * information is not needed
1993 * Returns: Pointer to the password or %NULL if not found
1995 const u8 * eap_get_config_password2(struct eap_sm *sm, size_t *len, int *hash)
1997 struct eap_peer_config *config = eap_get_config(sm);
2001 if (config->flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
2002 if (eap_get_ext_password(sm, config) < 0)
2004 *len = wpabuf_len(sm->ext_pw_buf);
2005 return wpabuf_head(sm->ext_pw_buf);
2008 *len = config->password_len;
2010 *hash = !!(config->flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH);
2011 return config->password;
2016 * eap_get_config_new_password - Get new password from network configuration
2017 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2018 * @len: Buffer for the length of the new password
2019 * Returns: Pointer to the new password or %NULL if not found
2021 const u8 * eap_get_config_new_password(struct eap_sm *sm, size_t *len)
2023 struct eap_peer_config *config = eap_get_config(sm);
2026 *len = config->new_password_len;
2027 return config->new_password;
2032 * eap_get_config_otp - Get one-time password from the network configuration
2033 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2034 * @len: Buffer for the length of the one-time password
2035 * Returns: Pointer to the one-time password or %NULL if not found
2037 const u8 * eap_get_config_otp(struct eap_sm *sm, size_t *len)
2039 struct eap_peer_config *config = eap_get_config(sm);
2042 *len = config->otp_len;
2048 * eap_clear_config_otp - Clear used one-time password
2049 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2051 * This function clears a used one-time password (OTP) from the current network
2052 * configuration. This should be called when the OTP has been used and is not
2055 void eap_clear_config_otp(struct eap_sm *sm)
2057 struct eap_peer_config *config = eap_get_config(sm);
2060 os_memset(config->otp, 0, config->otp_len);
2061 os_free(config->otp);
2063 config->otp_len = 0;
2068 * eap_get_config_phase1 - Get phase1 data from the network configuration
2069 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2070 * Returns: Pointer to the phase1 data or %NULL if not found
2072 const char * eap_get_config_phase1(struct eap_sm *sm)
2074 struct eap_peer_config *config = eap_get_config(sm);
2077 return config->phase1;
2082 * eap_get_config_phase2 - Get phase2 data from the network configuration
2083 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2084 * Returns: Pointer to the phase1 data or %NULL if not found
2086 const char * eap_get_config_phase2(struct eap_sm *sm)
2088 struct eap_peer_config *config = eap_get_config(sm);
2091 return config->phase2;
2095 int eap_get_config_fragment_size(struct eap_sm *sm)
2097 struct eap_peer_config *config = eap_get_config(sm);
2100 return config->fragment_size;
2105 * eap_key_available - Get key availability (eapKeyAvailable variable)
2106 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2107 * Returns: 1 if EAP keying material is available, 0 if not
2109 int eap_key_available(struct eap_sm *sm)
2111 return sm ? sm->eapKeyAvailable : 0;
2116 * eap_notify_success - Notify EAP state machine about external success trigger
2117 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2119 * This function is called when external event, e.g., successful completion of
2120 * WPA-PSK key handshake, is indicating that EAP state machine should move to
2121 * success state. This is mainly used with security modes that do not use EAP
2122 * state machine (e.g., WPA-PSK).
2124 void eap_notify_success(struct eap_sm *sm)
2127 sm->decision = DECISION_COND_SUCC;
2128 sm->EAP_state = EAP_SUCCESS;
2134 * eap_notify_lower_layer_success - Notification of lower layer success
2135 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2137 * Notify EAP state machines that a lower layer has detected a successful
2138 * authentication. This is used to recover from dropped EAP-Success messages.
2140 void eap_notify_lower_layer_success(struct eap_sm *sm)
2145 if (eapol_get_bool(sm, EAPOL_eapSuccess) ||
2146 sm->decision == DECISION_FAIL ||
2147 (sm->methodState != METHOD_MAY_CONT &&
2148 sm->methodState != METHOD_DONE))
2151 if (sm->eapKeyData != NULL)
2152 sm->eapKeyAvailable = TRUE;
2153 eapol_set_bool(sm, EAPOL_eapSuccess, TRUE);
2154 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
2155 "EAP authentication completed successfully (based on lower "
2161 * eap_get_eapKeyData - Get master session key (MSK) from EAP state machine
2162 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2163 * @len: Pointer to variable that will be set to number of bytes in the key
2164 * Returns: Pointer to the EAP keying data or %NULL on failure
2166 * Fetch EAP keying material (MSK, eapKeyData) from the EAP state machine. The
2167 * key is available only after a successful authentication. EAP state machine
2168 * continues to manage the key data and the caller must not change or free the
2171 const u8 * eap_get_eapKeyData(struct eap_sm *sm, size_t *len)
2173 if (sm == NULL || sm->eapKeyData == NULL) {
2178 *len = sm->eapKeyDataLen;
2179 return sm->eapKeyData;
2184 * eap_get_eapKeyData - Get EAP response data
2185 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2186 * Returns: Pointer to the EAP response (eapRespData) or %NULL on failure
2188 * Fetch EAP response (eapRespData) from the EAP state machine. This data is
2189 * available when EAP state machine has processed an incoming EAP request. The
2190 * EAP state machine does not maintain a reference to the response after this
2191 * function is called and the caller is responsible for freeing the data.
2193 struct wpabuf * eap_get_eapRespData(struct eap_sm *sm)
2195 struct wpabuf *resp;
2197 if (sm == NULL || sm->eapRespData == NULL)
2200 resp = sm->eapRespData;
2201 sm->eapRespData = NULL;
2208 * eap_sm_register_scard_ctx - Notification of smart card context
2209 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2210 * @ctx: Context data for smart card operations
2212 * Notify EAP state machines of context data for smart card operations. This
2213 * context data will be used as a parameter for scard_*() functions.
2215 void eap_register_scard_ctx(struct eap_sm *sm, void *ctx)
2218 sm->scard_ctx = ctx;
2223 * eap_set_config_blob - Set or add a named configuration blob
2224 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2225 * @blob: New value for the blob
2227 * Adds a new configuration blob or replaces the current value of an existing
2230 void eap_set_config_blob(struct eap_sm *sm, struct wpa_config_blob *blob)
2232 #ifndef CONFIG_NO_CONFIG_BLOBS
2233 sm->eapol_cb->set_config_blob(sm->eapol_ctx, blob);
2234 #endif /* CONFIG_NO_CONFIG_BLOBS */
2239 * eap_get_config_blob - Get a named configuration blob
2240 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2241 * @name: Name of the blob
2242 * Returns: Pointer to blob data or %NULL if not found
2244 const struct wpa_config_blob * eap_get_config_blob(struct eap_sm *sm,
2247 #ifndef CONFIG_NO_CONFIG_BLOBS
2248 return sm->eapol_cb->get_config_blob(sm->eapol_ctx, name);
2249 #else /* CONFIG_NO_CONFIG_BLOBS */
2251 #endif /* CONFIG_NO_CONFIG_BLOBS */
2256 * eap_set_force_disabled - Set force_disabled flag
2257 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2258 * @disabled: 1 = EAP disabled, 0 = EAP enabled
2260 * This function is used to force EAP state machine to be disabled when it is
2261 * not in use (e.g., with WPA-PSK or plaintext connections).
2263 void eap_set_force_disabled(struct eap_sm *sm, int disabled)
2265 sm->force_disabled = disabled;
2270 * eap_notify_pending - Notify that EAP method is ready to re-process a request
2271 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2273 * An EAP method can perform a pending operation (e.g., to get a response from
2274 * an external process). Once the response is available, this function can be
2275 * used to request EAPOL state machine to retry delivering the previously
2276 * received (and still unanswered) EAP request to EAP state machine.
2278 void eap_notify_pending(struct eap_sm *sm)
2280 sm->eapol_cb->notify_pending(sm->eapol_ctx);
2285 * eap_invalidate_cached_session - Mark cached session data invalid
2286 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2288 void eap_invalidate_cached_session(struct eap_sm *sm)
2291 eap_deinit_prev_method(sm, "invalidate");
2295 int eap_is_wps_pbc_enrollee(struct eap_peer_config *conf)
2297 if (conf->identity_len != WSC_ID_ENROLLEE_LEN ||
2298 os_memcmp(conf->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN))
2299 return 0; /* Not a WPS Enrollee */
2301 if (conf->phase1 == NULL || os_strstr(conf->phase1, "pbc=1") == NULL)
2302 return 0; /* Not using PBC */
2308 int eap_is_wps_pin_enrollee(struct eap_peer_config *conf)
2310 if (conf->identity_len != WSC_ID_ENROLLEE_LEN ||
2311 os_memcmp(conf->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN))
2312 return 0; /* Not a WPS Enrollee */
2314 if (conf->phase1 == NULL || os_strstr(conf->phase1, "pin=") == NULL)
2315 return 0; /* Not using PIN */
2321 void eap_sm_set_ext_pw_ctx(struct eap_sm *sm, struct ext_password_data *ext)
2323 ext_password_free(sm->ext_pw_buf);
2324 sm->ext_pw_buf = NULL;
2330 * eap_set_anon_id - Set or add anonymous identity
2331 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2332 * @id: Anonymous identity (e.g., EAP-SIM pseudonym) or %NULL to clear
2333 * @len: Length of anonymous identity in octets
2335 void eap_set_anon_id(struct eap_sm *sm, const u8 *id, size_t len)
2337 if (sm->eapol_cb->set_anon_id)
2338 sm->eapol_cb->set_anon_id(sm->eapol_ctx, id, len);