2 * EAP peer state machines (RFC 4137)
3 * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
14 * This file implements the Peer State Machine as defined in RFC 4137. The used
15 * states and state transitions match mostly with the RFC. However, there are
16 * couple of additional transitions for working around small issues noticed
17 * during testing. These exceptions are explained in comments within the
18 * functions in this file. The method functions, m.func(), are similar to the
19 * ones used in RFC 4137, but some small changes have used here to optimize
20 * operations and to add functionality needed for fast re-authentication
21 * (session resumption).
28 #include "eap_config.h"
31 #include "pcsc_funcs.h"
33 #include "state_machine.h"
34 #include "eap_common/eap_wsc_common.h"
36 #define STATE_MACHINE_DATA struct eap_sm
37 #define STATE_MACHINE_DEBUG_PREFIX "EAP"
39 #define EAP_MAX_AUTH_ROUNDS 50
42 static Boolean eap_sm_allowMethod(struct eap_sm *sm, int vendor,
44 static struct wpabuf * eap_sm_buildNak(struct eap_sm *sm, int id);
45 static void eap_sm_processIdentity(struct eap_sm *sm,
46 const struct wpabuf *req);
47 static void eap_sm_processNotify(struct eap_sm *sm, const struct wpabuf *req);
48 static struct wpabuf * eap_sm_buildNotify(int id);
49 static void eap_sm_parseEapReq(struct eap_sm *sm, const struct wpabuf *req);
50 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
51 static const char * eap_sm_method_state_txt(EapMethodState state);
52 static const char * eap_sm_decision_txt(EapDecision decision);
53 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
57 static Boolean eapol_get_bool(struct eap_sm *sm, enum eapol_bool_var var)
59 return sm->eapol_cb->get_bool(sm->eapol_ctx, var);
63 static void eapol_set_bool(struct eap_sm *sm, enum eapol_bool_var var,
66 sm->eapol_cb->set_bool(sm->eapol_ctx, var, value);
70 static unsigned int eapol_get_int(struct eap_sm *sm, enum eapol_int_var var)
72 return sm->eapol_cb->get_int(sm->eapol_ctx, var);
76 static void eapol_set_int(struct eap_sm *sm, enum eapol_int_var var,
79 sm->eapol_cb->set_int(sm->eapol_ctx, var, value);
83 static struct wpabuf * eapol_get_eapReqData(struct eap_sm *sm)
85 return sm->eapol_cb->get_eapReqData(sm->eapol_ctx);
89 static void eap_deinit_prev_method(struct eap_sm *sm, const char *txt)
91 if (sm->m == NULL || sm->eap_method_priv == NULL)
94 wpa_printf(MSG_DEBUG, "EAP: deinitialize previously used EAP method "
95 "(%d, %s) at %s", sm->selectedMethod, sm->m->name, txt);
96 sm->m->deinit(sm, sm->eap_method_priv);
97 sm->eap_method_priv = NULL;
103 * eap_allowed_method - Check whether EAP method is allowed
104 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
105 * @vendor: Vendor-Id for expanded types or 0 = IETF for legacy types
107 * Returns: 1 = allowed EAP method, 0 = not allowed
109 int eap_allowed_method(struct eap_sm *sm, int vendor, u32 method)
111 struct eap_peer_config *config = eap_get_config(sm);
113 struct eap_method_type *m;
115 if (config == NULL || config->eap_methods == NULL)
118 m = config->eap_methods;
119 for (i = 0; m[i].vendor != EAP_VENDOR_IETF ||
120 m[i].method != EAP_TYPE_NONE; i++) {
121 if (m[i].vendor == vendor && m[i].method == method)
129 * This state initializes state machine variables when the machine is
130 * activated (portEnabled = TRUE). This is also used when re-starting
131 * authentication (eapRestart == TRUE).
133 SM_STATE(EAP, INITIALIZE)
135 SM_ENTRY(EAP, INITIALIZE);
136 if (sm->fast_reauth && sm->m && sm->m->has_reauth_data &&
137 sm->m->has_reauth_data(sm, sm->eap_method_priv) &&
139 wpa_printf(MSG_DEBUG, "EAP: maintaining EAP method data for "
140 "fast reauthentication");
141 sm->m->deinit_for_reauth(sm, sm->eap_method_priv);
143 eap_deinit_prev_method(sm, "INITIALIZE");
145 sm->selectedMethod = EAP_TYPE_NONE;
146 sm->methodState = METHOD_NONE;
147 sm->allowNotifications = TRUE;
148 sm->decision = DECISION_FAIL;
149 eapol_set_int(sm, EAPOL_idleWhile, sm->ClientTimeout);
150 eapol_set_bool(sm, EAPOL_eapSuccess, FALSE);
151 eapol_set_bool(sm, EAPOL_eapFail, FALSE);
152 os_free(sm->eapKeyData);
153 sm->eapKeyData = NULL;
154 sm->eapKeyAvailable = FALSE;
155 eapol_set_bool(sm, EAPOL_eapRestart, FALSE);
156 sm->lastId = -1; /* new session - make sure this does not match with
157 * the first EAP-Packet */
159 * RFC 4137 does not reset eapResp and eapNoResp here. However, this
160 * seemed to be able to trigger cases where both were set and if EAPOL
161 * state machine uses eapNoResp first, it may end up not sending a real
162 * reply correctly. This occurred when the workaround in FAIL state set
163 * eapNoResp = TRUE.. Maybe that workaround needs to be fixed to do
166 eapol_set_bool(sm, EAPOL_eapResp, FALSE);
167 eapol_set_bool(sm, EAPOL_eapNoResp, FALSE);
169 sm->prev_failure = 0;
174 * This state is reached whenever service from the lower layer is interrupted
175 * or unavailable (portEnabled == FALSE). Immediate transition to INITIALIZE
176 * occurs when the port becomes enabled.
178 SM_STATE(EAP, DISABLED)
180 SM_ENTRY(EAP, DISABLED);
186 * The state machine spends most of its time here, waiting for something to
187 * happen. This state is entered unconditionally from INITIALIZE, DISCARD, and
188 * SEND_RESPONSE states.
197 * This state is entered when an EAP packet is received (eapReq == TRUE) to
198 * parse the packet header.
200 SM_STATE(EAP, RECEIVED)
202 const struct wpabuf *eapReqData;
204 SM_ENTRY(EAP, RECEIVED);
205 eapReqData = eapol_get_eapReqData(sm);
206 /* parse rxReq, rxSuccess, rxFailure, reqId, reqMethod */
207 eap_sm_parseEapReq(sm, eapReqData);
213 * This state is entered when a request for a new type comes in. Either the
214 * correct method is started, or a Nak response is built.
216 SM_STATE(EAP, GET_METHOD)
221 SM_ENTRY(EAP, GET_METHOD);
223 if (sm->reqMethod == EAP_TYPE_EXPANDED)
224 method = sm->reqVendorMethod;
226 method = sm->reqMethod;
228 if (!eap_sm_allowMethod(sm, sm->reqVendor, method)) {
229 wpa_printf(MSG_DEBUG, "EAP: vendor %u method %u not allowed",
230 sm->reqVendor, method);
235 * RFC 4137 does not define specific operation for fast
236 * re-authentication (session resumption). The design here is to allow
237 * the previously used method data to be maintained for
238 * re-authentication if the method support session resumption.
239 * Otherwise, the previously used method data is freed and a new method
242 if (sm->fast_reauth &&
243 sm->m && sm->m->vendor == sm->reqVendor &&
244 sm->m->method == method &&
245 sm->m->has_reauth_data &&
246 sm->m->has_reauth_data(sm, sm->eap_method_priv)) {
247 wpa_printf(MSG_DEBUG, "EAP: Using previous method data"
248 " for fast re-authentication");
251 eap_deinit_prev_method(sm, "GET_METHOD");
255 sm->selectedMethod = sm->reqMethod;
257 sm->m = eap_peer_get_eap_method(sm->reqVendor, method);
259 wpa_printf(MSG_DEBUG, "EAP: Could not find selected method: "
260 "vendor %d method %d",
261 sm->reqVendor, method);
265 wpa_printf(MSG_DEBUG, "EAP: Initialize selected EAP method: "
266 "vendor %u method %u (%s)",
267 sm->reqVendor, method, sm->m->name);
269 sm->eap_method_priv = sm->m->init_for_reauth(
270 sm, sm->eap_method_priv);
272 sm->eap_method_priv = sm->m->init(sm);
274 if (sm->eap_method_priv == NULL) {
275 struct eap_peer_config *config = eap_get_config(sm);
276 wpa_msg(sm->msg_ctx, MSG_INFO,
277 "EAP: Failed to initialize EAP method: vendor %u "
279 sm->reqVendor, method, sm->m->name);
281 sm->methodState = METHOD_NONE;
282 sm->selectedMethod = EAP_TYPE_NONE;
283 if (sm->reqMethod == EAP_TYPE_TLS && config &&
284 (config->pending_req_pin ||
285 config->pending_req_passphrase)) {
287 * Return without generating Nak in order to allow
288 * entering of PIN code or passphrase to retry the
289 * current EAP packet.
291 wpa_printf(MSG_DEBUG, "EAP: Pending PIN/passphrase "
292 "request - skip Nak");
299 sm->methodState = METHOD_INIT;
300 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_METHOD
301 "EAP vendor %u method %u (%s) selected",
302 sm->reqVendor, method, sm->m->name);
306 wpabuf_free(sm->eapRespData);
307 sm->eapRespData = NULL;
308 sm->eapRespData = eap_sm_buildNak(sm, sm->reqId);
313 * The method processing happens here. The request from the authenticator is
314 * processed, and an appropriate response packet is built.
316 SM_STATE(EAP, METHOD)
318 struct wpabuf *eapReqData;
319 struct eap_method_ret ret;
321 SM_ENTRY(EAP, METHOD);
323 wpa_printf(MSG_WARNING, "EAP::METHOD - method not selected");
327 eapReqData = eapol_get_eapReqData(sm);
330 * Get ignore, methodState, decision, allowNotifications, and
331 * eapRespData. RFC 4137 uses three separate method procedure (check,
332 * process, and buildResp) in this state. These have been combined into
333 * a single function call to m->process() in order to optimize EAP
334 * method implementation interface a bit. These procedures are only
335 * used from within this METHOD state, so there is no need to keep
336 * these as separate C functions.
338 * The RFC 4137 procedures return values as follows:
339 * ignore = m.check(eapReqData)
340 * (methodState, decision, allowNotifications) = m.process(eapReqData)
341 * eapRespData = m.buildResp(reqId)
343 os_memset(&ret, 0, sizeof(ret));
344 ret.ignore = sm->ignore;
345 ret.methodState = sm->methodState;
346 ret.decision = sm->decision;
347 ret.allowNotifications = sm->allowNotifications;
348 wpabuf_free(sm->eapRespData);
349 sm->eapRespData = NULL;
350 sm->eapRespData = sm->m->process(sm, sm->eap_method_priv, &ret,
352 wpa_printf(MSG_DEBUG, "EAP: method process -> ignore=%s "
353 "methodState=%s decision=%s",
354 ret.ignore ? "TRUE" : "FALSE",
355 eap_sm_method_state_txt(ret.methodState),
356 eap_sm_decision_txt(ret.decision));
358 sm->ignore = ret.ignore;
361 sm->methodState = ret.methodState;
362 sm->decision = ret.decision;
363 sm->allowNotifications = ret.allowNotifications;
365 if (sm->m->isKeyAvailable && sm->m->getKey &&
366 sm->m->isKeyAvailable(sm, sm->eap_method_priv)) {
367 os_free(sm->eapKeyData);
368 sm->eapKeyData = sm->m->getKey(sm, sm->eap_method_priv,
375 * This state signals the lower layer that a response packet is ready to be
378 SM_STATE(EAP, SEND_RESPONSE)
380 SM_ENTRY(EAP, SEND_RESPONSE);
381 wpabuf_free(sm->lastRespData);
382 if (sm->eapRespData) {
384 os_memcpy(sm->last_md5, sm->req_md5, 16);
385 sm->lastId = sm->reqId;
386 sm->lastRespData = wpabuf_dup(sm->eapRespData);
387 eapol_set_bool(sm, EAPOL_eapResp, TRUE);
389 sm->lastRespData = NULL;
390 eapol_set_bool(sm, EAPOL_eapReq, FALSE);
391 eapol_set_int(sm, EAPOL_idleWhile, sm->ClientTimeout);
396 * This state signals the lower layer that the request was discarded, and no
397 * response packet will be sent at this time.
399 SM_STATE(EAP, DISCARD)
401 SM_ENTRY(EAP, DISCARD);
402 eapol_set_bool(sm, EAPOL_eapReq, FALSE);
403 eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
408 * Handles requests for Identity method and builds a response.
410 SM_STATE(EAP, IDENTITY)
412 const struct wpabuf *eapReqData;
414 SM_ENTRY(EAP, IDENTITY);
415 eapReqData = eapol_get_eapReqData(sm);
416 eap_sm_processIdentity(sm, eapReqData);
417 wpabuf_free(sm->eapRespData);
418 sm->eapRespData = NULL;
419 sm->eapRespData = eap_sm_buildIdentity(sm, sm->reqId, 0);
424 * Handles requests for Notification method and builds a response.
426 SM_STATE(EAP, NOTIFICATION)
428 const struct wpabuf *eapReqData;
430 SM_ENTRY(EAP, NOTIFICATION);
431 eapReqData = eapol_get_eapReqData(sm);
432 eap_sm_processNotify(sm, eapReqData);
433 wpabuf_free(sm->eapRespData);
434 sm->eapRespData = NULL;
435 sm->eapRespData = eap_sm_buildNotify(sm->reqId);
440 * This state retransmits the previous response packet.
442 SM_STATE(EAP, RETRANSMIT)
444 SM_ENTRY(EAP, RETRANSMIT);
445 wpabuf_free(sm->eapRespData);
446 if (sm->lastRespData)
447 sm->eapRespData = wpabuf_dup(sm->lastRespData);
449 sm->eapRespData = NULL;
454 * This state is entered in case of a successful completion of authentication
455 * and state machine waits here until port is disabled or EAP authentication is
458 SM_STATE(EAP, SUCCESS)
460 SM_ENTRY(EAP, SUCCESS);
461 if (sm->eapKeyData != NULL)
462 sm->eapKeyAvailable = TRUE;
463 eapol_set_bool(sm, EAPOL_eapSuccess, TRUE);
466 * RFC 4137 does not clear eapReq here, but this seems to be required
467 * to avoid processing the same request twice when state machine is
470 eapol_set_bool(sm, EAPOL_eapReq, FALSE);
473 * RFC 4137 does not set eapNoResp here, but this seems to be required
474 * to get EAPOL Supplicant backend state machine into SUCCESS state. In
475 * addition, either eapResp or eapNoResp is required to be set after
476 * processing the received EAP frame.
478 eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
480 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
481 "EAP authentication completed successfully");
486 * This state is entered in case of a failure and state machine waits here
487 * until port is disabled or EAP authentication is restarted.
489 SM_STATE(EAP, FAILURE)
491 SM_ENTRY(EAP, FAILURE);
492 eapol_set_bool(sm, EAPOL_eapFail, TRUE);
495 * RFC 4137 does not clear eapReq here, but this seems to be required
496 * to avoid processing the same request twice when state machine is
499 eapol_set_bool(sm, EAPOL_eapReq, FALSE);
502 * RFC 4137 does not set eapNoResp here. However, either eapResp or
503 * eapNoResp is required to be set after processing the received EAP
506 eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
508 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_FAILURE
509 "EAP authentication failed");
511 sm->prev_failure = 1;
515 static int eap_success_workaround(struct eap_sm *sm, int reqId, int lastId)
518 * At least Microsoft IAS and Meetinghouse Aegis seem to be sending
519 * EAP-Success/Failure with lastId + 1 even though RFC 3748 and
520 * RFC 4137 require that reqId == lastId. In addition, it looks like
521 * Ringmaster v2.1.2.0 would be using lastId + 2 in EAP-Success.
523 * Accept this kind of Id if EAP workarounds are enabled. These are
524 * unauthenticated plaintext messages, so this should have minimal
525 * security implications (bit easier to fake EAP-Success/Failure).
527 if (sm->workaround && (reqId == ((lastId + 1) & 0xff) ||
528 reqId == ((lastId + 2) & 0xff))) {
529 wpa_printf(MSG_DEBUG, "EAP: Workaround for unexpected "
530 "identifier field in EAP Success: "
531 "reqId=%d lastId=%d (these are supposed to be "
532 "same)", reqId, lastId);
535 wpa_printf(MSG_DEBUG, "EAP: EAP-Success Id mismatch - reqId=%d "
536 "lastId=%d", reqId, lastId);
542 * RFC 4137 - Appendix A.1: EAP Peer State Machine - State transitions
545 static void eap_peer_sm_step_idle(struct eap_sm *sm)
548 * The first three transitions are from RFC 4137. The last two are
549 * local additions to handle special cases with LEAP and PEAP server
550 * not sending EAP-Success in some cases.
552 if (eapol_get_bool(sm, EAPOL_eapReq))
553 SM_ENTER(EAP, RECEIVED);
554 else if ((eapol_get_bool(sm, EAPOL_altAccept) &&
555 sm->decision != DECISION_FAIL) ||
556 (eapol_get_int(sm, EAPOL_idleWhile) == 0 &&
557 sm->decision == DECISION_UNCOND_SUCC))
558 SM_ENTER(EAP, SUCCESS);
559 else if (eapol_get_bool(sm, EAPOL_altReject) ||
560 (eapol_get_int(sm, EAPOL_idleWhile) == 0 &&
561 sm->decision != DECISION_UNCOND_SUCC) ||
562 (eapol_get_bool(sm, EAPOL_altAccept) &&
563 sm->methodState != METHOD_CONT &&
564 sm->decision == DECISION_FAIL))
565 SM_ENTER(EAP, FAILURE);
566 else if (sm->selectedMethod == EAP_TYPE_LEAP &&
567 sm->leap_done && sm->decision != DECISION_FAIL &&
568 sm->methodState == METHOD_DONE)
569 SM_ENTER(EAP, SUCCESS);
570 else if (sm->selectedMethod == EAP_TYPE_PEAP &&
571 sm->peap_done && sm->decision != DECISION_FAIL &&
572 sm->methodState == METHOD_DONE)
573 SM_ENTER(EAP, SUCCESS);
577 static int eap_peer_req_is_duplicate(struct eap_sm *sm)
581 duplicate = (sm->reqId == sm->lastId) && sm->rxReq;
582 if (sm->workaround && duplicate &&
583 os_memcmp(sm->req_md5, sm->last_md5, 16) != 0) {
585 * RFC 4137 uses (reqId == lastId) as the only verification for
586 * duplicate EAP requests. However, this misses cases where the
587 * AS is incorrectly using the same id again; and
588 * unfortunately, such implementations exist. Use MD5 hash as
589 * an extra verification for the packets being duplicate to
590 * workaround these issues.
592 wpa_printf(MSG_DEBUG, "EAP: AS used the same Id again, but "
593 "EAP packets were not identical");
594 wpa_printf(MSG_DEBUG, "EAP: workaround - assume this is not a "
603 static void eap_peer_sm_step_received(struct eap_sm *sm)
605 int duplicate = eap_peer_req_is_duplicate(sm);
608 * Two special cases below for LEAP are local additions to work around
609 * odd LEAP behavior (EAP-Success in the middle of authentication and
610 * then swapped roles). Other transitions are based on RFC 4137.
612 if (sm->rxSuccess && sm->decision != DECISION_FAIL &&
613 (sm->reqId == sm->lastId ||
614 eap_success_workaround(sm, sm->reqId, sm->lastId)))
615 SM_ENTER(EAP, SUCCESS);
616 else if (sm->methodState != METHOD_CONT &&
618 sm->decision != DECISION_UNCOND_SUCC) ||
619 (sm->rxSuccess && sm->decision == DECISION_FAIL &&
620 (sm->selectedMethod != EAP_TYPE_LEAP ||
621 sm->methodState != METHOD_MAY_CONT))) &&
622 (sm->reqId == sm->lastId ||
623 eap_success_workaround(sm, sm->reqId, sm->lastId)))
624 SM_ENTER(EAP, FAILURE);
625 else if (sm->rxReq && duplicate)
626 SM_ENTER(EAP, RETRANSMIT);
627 else if (sm->rxReq && !duplicate &&
628 sm->reqMethod == EAP_TYPE_NOTIFICATION &&
629 sm->allowNotifications)
630 SM_ENTER(EAP, NOTIFICATION);
631 else if (sm->rxReq && !duplicate &&
632 sm->selectedMethod == EAP_TYPE_NONE &&
633 sm->reqMethod == EAP_TYPE_IDENTITY)
634 SM_ENTER(EAP, IDENTITY);
635 else if (sm->rxReq && !duplicate &&
636 sm->selectedMethod == EAP_TYPE_NONE &&
637 sm->reqMethod != EAP_TYPE_IDENTITY &&
638 sm->reqMethod != EAP_TYPE_NOTIFICATION)
639 SM_ENTER(EAP, GET_METHOD);
640 else if (sm->rxReq && !duplicate &&
641 sm->reqMethod == sm->selectedMethod &&
642 sm->methodState != METHOD_DONE)
643 SM_ENTER(EAP, METHOD);
644 else if (sm->selectedMethod == EAP_TYPE_LEAP &&
645 (sm->rxSuccess || sm->rxResp))
646 SM_ENTER(EAP, METHOD);
648 SM_ENTER(EAP, DISCARD);
652 static void eap_peer_sm_step_local(struct eap_sm *sm)
654 switch (sm->EAP_state) {
659 if (eapol_get_bool(sm, EAPOL_portEnabled) &&
661 SM_ENTER(EAP, INITIALIZE);
664 eap_peer_sm_step_idle(sm);
667 eap_peer_sm_step_received(sm);
670 if (sm->selectedMethod == sm->reqMethod)
671 SM_ENTER(EAP, METHOD);
673 SM_ENTER(EAP, SEND_RESPONSE);
677 SM_ENTER(EAP, DISCARD);
679 SM_ENTER(EAP, SEND_RESPONSE);
681 case EAP_SEND_RESPONSE:
688 SM_ENTER(EAP, SEND_RESPONSE);
690 case EAP_NOTIFICATION:
691 SM_ENTER(EAP, SEND_RESPONSE);
694 SM_ENTER(EAP, SEND_RESPONSE);
706 /* Global transitions */
707 if (eapol_get_bool(sm, EAPOL_eapRestart) &&
708 eapol_get_bool(sm, EAPOL_portEnabled))
709 SM_ENTER_GLOBAL(EAP, INITIALIZE);
710 else if (!eapol_get_bool(sm, EAPOL_portEnabled) || sm->force_disabled)
711 SM_ENTER_GLOBAL(EAP, DISABLED);
712 else if (sm->num_rounds > EAP_MAX_AUTH_ROUNDS) {
713 /* RFC 4137 does not place any limit on number of EAP messages
714 * in an authentication session. However, some error cases have
715 * ended up in a state were EAP messages were sent between the
716 * peer and server in a loop (e.g., TLS ACK frame in both
717 * direction). Since this is quite undesired outcome, limit the
718 * total number of EAP round-trips and abort authentication if
719 * this limit is exceeded.
721 if (sm->num_rounds == EAP_MAX_AUTH_ROUNDS + 1) {
722 wpa_msg(sm->msg_ctx, MSG_INFO, "EAP: more than %d "
723 "authentication rounds - abort",
724 EAP_MAX_AUTH_ROUNDS);
726 SM_ENTER_GLOBAL(EAP, FAILURE);
729 /* Local transitions */
730 eap_peer_sm_step_local(sm);
735 static Boolean eap_sm_allowMethod(struct eap_sm *sm, int vendor,
738 if (!eap_allowed_method(sm, vendor, method)) {
739 wpa_printf(MSG_DEBUG, "EAP: configuration does not allow: "
740 "vendor %u method %u", vendor, method);
743 if (eap_peer_get_eap_method(vendor, method))
745 wpa_printf(MSG_DEBUG, "EAP: not included in build: "
746 "vendor %u method %u", vendor, method);
751 static struct wpabuf * eap_sm_build_expanded_nak(
752 struct eap_sm *sm, int id, const struct eap_method *methods,
757 const struct eap_method *m;
759 wpa_printf(MSG_DEBUG, "EAP: Building expanded EAP-Nak");
761 /* RFC 3748 - 5.3.2: Expanded Nak */
762 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_EXPANDED,
763 8 + 8 * (count + 1), EAP_CODE_RESPONSE, id);
767 wpabuf_put_be24(resp, EAP_VENDOR_IETF);
768 wpabuf_put_be32(resp, EAP_TYPE_NAK);
770 for (m = methods; m; m = m->next) {
771 if (sm->reqVendor == m->vendor &&
772 sm->reqVendorMethod == m->method)
773 continue; /* do not allow the current method again */
774 if (eap_allowed_method(sm, m->vendor, m->method)) {
775 wpa_printf(MSG_DEBUG, "EAP: allowed type: "
776 "vendor=%u method=%u",
777 m->vendor, m->method);
778 wpabuf_put_u8(resp, EAP_TYPE_EXPANDED);
779 wpabuf_put_be24(resp, m->vendor);
780 wpabuf_put_be32(resp, m->method);
786 wpa_printf(MSG_DEBUG, "EAP: no more allowed methods");
787 wpabuf_put_u8(resp, EAP_TYPE_EXPANDED);
788 wpabuf_put_be24(resp, EAP_VENDOR_IETF);
789 wpabuf_put_be32(resp, EAP_TYPE_NONE);
792 eap_update_len(resp);
798 static struct wpabuf * eap_sm_buildNak(struct eap_sm *sm, int id)
802 int found = 0, expanded_found = 0;
804 const struct eap_method *methods, *m;
806 wpa_printf(MSG_DEBUG, "EAP: Building EAP-Nak (requested type %u "
807 "vendor=%u method=%u not allowed)", sm->reqMethod,
808 sm->reqVendor, sm->reqVendorMethod);
809 methods = eap_peer_get_methods(&count);
812 if (sm->reqMethod == EAP_TYPE_EXPANDED)
813 return eap_sm_build_expanded_nak(sm, id, methods, count);
815 /* RFC 3748 - 5.3.1: Legacy Nak */
816 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NAK,
817 sizeof(struct eap_hdr) + 1 + count + 1,
818 EAP_CODE_RESPONSE, id);
822 start = wpabuf_put(resp, 0);
823 for (m = methods; m; m = m->next) {
824 if (m->vendor == EAP_VENDOR_IETF && m->method == sm->reqMethod)
825 continue; /* do not allow the current method again */
826 if (eap_allowed_method(sm, m->vendor, m->method)) {
827 if (m->vendor != EAP_VENDOR_IETF) {
831 wpabuf_put_u8(resp, EAP_TYPE_EXPANDED);
833 wpabuf_put_u8(resp, m->method);
838 wpabuf_put_u8(resp, EAP_TYPE_NONE);
839 wpa_hexdump(MSG_DEBUG, "EAP: allowed methods", start, found);
841 eap_update_len(resp);
847 static void eap_sm_processIdentity(struct eap_sm *sm, const struct wpabuf *req)
849 const struct eap_hdr *hdr = wpabuf_head(req);
850 const u8 *pos = (const u8 *) (hdr + 1);
853 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_STARTED
854 "EAP authentication started");
857 * RFC 3748 - 5.1: Identity
858 * Data field may contain a displayable message in UTF-8. If this
859 * includes NUL-character, only the data before that should be
860 * displayed. Some EAP implementasitons may piggy-back additional
861 * options after the NUL.
863 /* TODO: could save displayable message so that it can be shown to the
864 * user in case of interaction is required */
865 wpa_hexdump_ascii(MSG_DEBUG, "EAP: EAP-Request Identity data",
866 pos, be_to_host16(hdr->length) - 5);
871 static int eap_sm_imsi_identity(struct eap_sm *sm,
872 struct eap_peer_config *conf)
877 struct eap_method_type *m = conf->eap_methods;
880 imsi_len = sizeof(imsi);
881 if (scard_get_imsi(sm->scard_ctx, imsi, &imsi_len)) {
882 wpa_printf(MSG_WARNING, "Failed to get IMSI from SIM");
886 wpa_hexdump_ascii(MSG_DEBUG, "IMSI", (u8 *) imsi, imsi_len);
888 for (i = 0; m && (m[i].vendor != EAP_VENDOR_IETF ||
889 m[i].method != EAP_TYPE_NONE); i++) {
890 if (m[i].vendor == EAP_VENDOR_IETF &&
891 m[i].method == EAP_TYPE_AKA) {
897 os_free(conf->identity);
898 conf->identity = os_malloc(1 + imsi_len);
899 if (conf->identity == NULL) {
900 wpa_printf(MSG_WARNING, "Failed to allocate buffer for "
901 "IMSI-based identity");
905 conf->identity[0] = aka ? '0' : '1';
906 os_memcpy(conf->identity + 1, imsi, imsi_len);
907 conf->identity_len = 1 + imsi_len;
911 #endif /* PCSC_FUNCS */
914 static int eap_sm_set_scard_pin(struct eap_sm *sm,
915 struct eap_peer_config *conf)
918 if (scard_set_pin(sm->scard_ctx, conf->pin)) {
920 * Make sure the same PIN is not tried again in order to avoid
926 wpa_printf(MSG_WARNING, "PIN validation failed");
927 eap_sm_request_pin(sm);
931 #else /* PCSC_FUNCS */
933 #endif /* PCSC_FUNCS */
936 static int eap_sm_get_scard_identity(struct eap_sm *sm,
937 struct eap_peer_config *conf)
940 if (eap_sm_set_scard_pin(sm, conf))
943 return eap_sm_imsi_identity(sm, conf);
944 #else /* PCSC_FUNCS */
946 #endif /* PCSC_FUNCS */
951 * eap_sm_buildIdentity - Build EAP-Identity/Response for the current network
952 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
953 * @id: EAP identifier for the packet
954 * @encrypted: Whether the packet is for encrypted tunnel (EAP phase 2)
955 * Returns: Pointer to the allocated EAP-Identity/Response packet or %NULL on
958 * This function allocates and builds an EAP-Identity/Response packet for the
959 * current network. The caller is responsible for freeing the returned data.
961 struct wpabuf * eap_sm_buildIdentity(struct eap_sm *sm, int id, int encrypted)
963 struct eap_peer_config *config = eap_get_config(sm);
968 if (config == NULL) {
969 wpa_printf(MSG_WARNING, "EAP: buildIdentity: configuration "
970 "was not available");
974 if (sm->m && sm->m->get_identity &&
975 (identity = sm->m->get_identity(sm, sm->eap_method_priv,
976 &identity_len)) != NULL) {
977 wpa_hexdump_ascii(MSG_DEBUG, "EAP: using method re-auth "
978 "identity", identity, identity_len);
979 } else if (!encrypted && config->anonymous_identity) {
980 identity = config->anonymous_identity;
981 identity_len = config->anonymous_identity_len;
982 wpa_hexdump_ascii(MSG_DEBUG, "EAP: using anonymous identity",
983 identity, identity_len);
985 identity = config->identity;
986 identity_len = config->identity_len;
987 wpa_hexdump_ascii(MSG_DEBUG, "EAP: using real identity",
988 identity, identity_len);
991 if (identity == NULL) {
992 wpa_printf(MSG_WARNING, "EAP: buildIdentity: identity "
993 "configuration was not available");
995 if (eap_sm_get_scard_identity(sm, config) < 0)
997 identity = config->identity;
998 identity_len = config->identity_len;
999 wpa_hexdump_ascii(MSG_DEBUG, "permanent identity from "
1000 "IMSI", identity, identity_len);
1002 eap_sm_request_identity(sm);
1005 } else if (config->pcsc) {
1006 if (eap_sm_set_scard_pin(sm, config) < 0)
1010 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY, identity_len,
1011 EAP_CODE_RESPONSE, id);
1015 wpabuf_put_data(resp, identity, identity_len);
1021 static void eap_sm_processNotify(struct eap_sm *sm, const struct wpabuf *req)
1027 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_NOTIFICATION, req,
1031 wpa_hexdump_ascii(MSG_DEBUG, "EAP: EAP-Request Notification data",
1034 msg = os_malloc(msg_len + 1);
1037 for (i = 0; i < msg_len; i++)
1038 msg[i] = isprint(pos[i]) ? (char) pos[i] : '_';
1039 msg[msg_len] = '\0';
1040 wpa_msg(sm->msg_ctx, MSG_INFO, "%s%s",
1041 WPA_EVENT_EAP_NOTIFICATION, msg);
1046 static struct wpabuf * eap_sm_buildNotify(int id)
1048 struct wpabuf *resp;
1050 wpa_printf(MSG_DEBUG, "EAP: Generating EAP-Response Notification");
1051 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_NOTIFICATION, 0,
1052 EAP_CODE_RESPONSE, id);
1060 static void eap_sm_parseEapReq(struct eap_sm *sm, const struct wpabuf *req)
1062 const struct eap_hdr *hdr;
1066 sm->rxReq = sm->rxResp = sm->rxSuccess = sm->rxFailure = FALSE;
1068 sm->reqMethod = EAP_TYPE_NONE;
1069 sm->reqVendor = EAP_VENDOR_IETF;
1070 sm->reqVendorMethod = EAP_TYPE_NONE;
1072 if (req == NULL || wpabuf_len(req) < sizeof(*hdr))
1075 hdr = wpabuf_head(req);
1076 plen = be_to_host16(hdr->length);
1077 if (plen > wpabuf_len(req)) {
1078 wpa_printf(MSG_DEBUG, "EAP: Ignored truncated EAP-Packet "
1079 "(len=%lu plen=%lu)",
1080 (unsigned long) wpabuf_len(req),
1081 (unsigned long) plen);
1085 sm->reqId = hdr->identifier;
1087 if (sm->workaround) {
1089 addr[0] = wpabuf_head(req);
1090 md5_vector(1, addr, &plen, sm->req_md5);
1093 switch (hdr->code) {
1094 case EAP_CODE_REQUEST:
1095 if (plen < sizeof(*hdr) + 1) {
1096 wpa_printf(MSG_DEBUG, "EAP: Too short EAP-Request - "
1101 pos = (const u8 *) (hdr + 1);
1102 sm->reqMethod = *pos++;
1103 if (sm->reqMethod == EAP_TYPE_EXPANDED) {
1104 if (plen < sizeof(*hdr) + 8) {
1105 wpa_printf(MSG_DEBUG, "EAP: Ignored truncated "
1106 "expanded EAP-Packet (plen=%lu)",
1107 (unsigned long) plen);
1110 sm->reqVendor = WPA_GET_BE24(pos);
1112 sm->reqVendorMethod = WPA_GET_BE32(pos);
1114 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Request id=%d "
1115 "method=%u vendor=%u vendorMethod=%u",
1116 sm->reqId, sm->reqMethod, sm->reqVendor,
1117 sm->reqVendorMethod);
1119 case EAP_CODE_RESPONSE:
1120 if (sm->selectedMethod == EAP_TYPE_LEAP) {
1122 * LEAP differs from RFC 4137 by using reversed roles
1123 * for mutual authentication and because of this, we
1124 * need to accept EAP-Response frames if LEAP is used.
1126 if (plen < sizeof(*hdr) + 1) {
1127 wpa_printf(MSG_DEBUG, "EAP: Too short "
1128 "EAP-Response - no Type field");
1132 pos = (const u8 *) (hdr + 1);
1133 sm->reqMethod = *pos;
1134 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Response for "
1135 "LEAP method=%d id=%d",
1136 sm->reqMethod, sm->reqId);
1139 wpa_printf(MSG_DEBUG, "EAP: Ignored EAP-Response");
1141 case EAP_CODE_SUCCESS:
1142 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Success");
1143 sm->rxSuccess = TRUE;
1145 case EAP_CODE_FAILURE:
1146 wpa_printf(MSG_DEBUG, "EAP: Received EAP-Failure");
1147 sm->rxFailure = TRUE;
1150 wpa_printf(MSG_DEBUG, "EAP: Ignored EAP-Packet with unknown "
1151 "code %d", hdr->code);
1158 * eap_peer_sm_init - Allocate and initialize EAP peer state machine
1159 * @eapol_ctx: Context data to be used with eapol_cb calls
1160 * @eapol_cb: Pointer to EAPOL callback functions
1161 * @msg_ctx: Context data for wpa_msg() calls
1162 * @conf: EAP configuration
1163 * Returns: Pointer to the allocated EAP state machine or %NULL on failure
1165 * This function allocates and initializes an EAP state machine. In addition,
1166 * this initializes TLS library for the new EAP state machine. eapol_cb pointer
1167 * will be in use until eap_peer_sm_deinit() is used to deinitialize this EAP
1168 * state machine. Consequently, the caller must make sure that this data
1169 * structure remains alive while the EAP state machine is active.
1171 struct eap_sm * eap_peer_sm_init(void *eapol_ctx,
1172 struct eapol_callbacks *eapol_cb,
1173 void *msg_ctx, struct eap_config *conf)
1176 struct tls_config tlsconf;
1178 sm = os_zalloc(sizeof(*sm));
1181 sm->eapol_ctx = eapol_ctx;
1182 sm->eapol_cb = eapol_cb;
1183 sm->msg_ctx = msg_ctx;
1184 sm->ClientTimeout = 60;
1185 sm->wps = conf->wps;
1187 os_memset(&tlsconf, 0, sizeof(tlsconf));
1188 tlsconf.opensc_engine_path = conf->opensc_engine_path;
1189 tlsconf.pkcs11_engine_path = conf->pkcs11_engine_path;
1190 tlsconf.pkcs11_module_path = conf->pkcs11_module_path;
1191 sm->ssl_ctx = tls_init(&tlsconf);
1192 if (sm->ssl_ctx == NULL) {
1193 wpa_printf(MSG_WARNING, "SSL: Failed to initialize TLS "
1204 * eap_peer_sm_deinit - Deinitialize and free an EAP peer state machine
1205 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1207 * This function deinitializes EAP state machine and frees all allocated
1210 void eap_peer_sm_deinit(struct eap_sm *sm)
1214 eap_deinit_prev_method(sm, "EAP deinit");
1216 tls_deinit(sm->ssl_ctx);
1222 * eap_peer_sm_step - Step EAP peer state machine
1223 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1224 * Returns: 1 if EAP state was changed or 0 if not
1226 * This function advances EAP state machine to a new state to match with the
1227 * current variables. This should be called whenever variables used by the EAP
1228 * state machine have changed.
1230 int eap_peer_sm_step(struct eap_sm *sm)
1234 sm->changed = FALSE;
1238 } while (sm->changed);
1244 * eap_sm_abort - Abort EAP authentication
1245 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1247 * Release system resources that have been allocated for the authentication
1248 * session without fully deinitializing the EAP state machine.
1250 void eap_sm_abort(struct eap_sm *sm)
1252 wpabuf_free(sm->lastRespData);
1253 sm->lastRespData = NULL;
1254 wpabuf_free(sm->eapRespData);
1255 sm->eapRespData = NULL;
1256 os_free(sm->eapKeyData);
1257 sm->eapKeyData = NULL;
1259 /* This is not clearly specified in the EAP statemachines draft, but
1260 * it seems necessary to make sure that some of the EAPOL variables get
1261 * cleared for the next authentication. */
1262 eapol_set_bool(sm, EAPOL_eapSuccess, FALSE);
1266 #ifdef CONFIG_CTRL_IFACE
1267 static const char * eap_sm_state_txt(int state)
1270 case EAP_INITIALIZE:
1271 return "INITIALIZE";
1278 case EAP_GET_METHOD:
1279 return "GET_METHOD";
1282 case EAP_SEND_RESPONSE:
1283 return "SEND_RESPONSE";
1288 case EAP_NOTIFICATION:
1289 return "NOTIFICATION";
1290 case EAP_RETRANSMIT:
1291 return "RETRANSMIT";
1300 #endif /* CONFIG_CTRL_IFACE */
1303 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
1304 static const char * eap_sm_method_state_txt(EapMethodState state)
1313 case METHOD_MAY_CONT:
1323 static const char * eap_sm_decision_txt(EapDecision decision)
1328 case DECISION_COND_SUCC:
1330 case DECISION_UNCOND_SUCC:
1331 return "UNCOND_SUCC";
1336 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1339 #ifdef CONFIG_CTRL_IFACE
1342 * eap_sm_get_status - Get EAP state machine status
1343 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1344 * @buf: Buffer for status information
1345 * @buflen: Maximum buffer length
1346 * @verbose: Whether to include verbose status information
1347 * Returns: Number of bytes written to buf.
1349 * Query EAP state machine for status information. This function fills in a
1350 * text area with current status information from the EAPOL state machine. If
1351 * the buffer (buf) is not large enough, status information will be truncated
1352 * to fit the buffer.
1354 int eap_sm_get_status(struct eap_sm *sm, char *buf, size_t buflen, int verbose)
1361 len = os_snprintf(buf, buflen,
1363 eap_sm_state_txt(sm->EAP_state));
1364 if (len < 0 || (size_t) len >= buflen)
1367 if (sm->selectedMethod != EAP_TYPE_NONE) {
1372 const struct eap_method *m =
1373 eap_peer_get_eap_method(EAP_VENDOR_IETF,
1374 sm->selectedMethod);
1380 ret = os_snprintf(buf + len, buflen - len,
1381 "selectedMethod=%d (EAP-%s)\n",
1382 sm->selectedMethod, name);
1383 if (ret < 0 || (size_t) ret >= buflen - len)
1387 if (sm->m && sm->m->get_status) {
1388 len += sm->m->get_status(sm, sm->eap_method_priv,
1389 buf + len, buflen - len,
1395 ret = os_snprintf(buf + len, buflen - len,
1399 "ClientTimeout=%d\n",
1401 eap_sm_method_state_txt(sm->methodState),
1402 eap_sm_decision_txt(sm->decision),
1404 if (ret < 0 || (size_t) ret >= buflen - len)
1411 #endif /* CONFIG_CTRL_IFACE */
1414 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
1416 TYPE_IDENTITY, TYPE_PASSWORD, TYPE_OTP, TYPE_PIN, TYPE_NEW_PASSWORD,
1418 } eap_ctrl_req_type;
1420 static void eap_sm_request(struct eap_sm *sm, eap_ctrl_req_type type,
1421 const char *msg, size_t msglen)
1423 struct eap_peer_config *config;
1424 char *field, *txt, *tmp;
1428 config = eap_get_config(sm);
1436 config->pending_req_identity++;
1441 config->pending_req_password++;
1443 case TYPE_NEW_PASSWORD:
1444 field = "NEW_PASSWORD";
1445 txt = "New Password";
1446 config->pending_req_new_password++;
1451 config->pending_req_pin++;
1456 tmp = os_malloc(msglen + 3);
1460 os_memcpy(tmp + 1, msg, msglen);
1461 tmp[msglen + 1] = ']';
1462 tmp[msglen + 2] = '\0';
1464 os_free(config->pending_req_otp);
1465 config->pending_req_otp = tmp;
1466 config->pending_req_otp_len = msglen + 3;
1468 if (config->pending_req_otp == NULL)
1470 txt = config->pending_req_otp;
1473 case TYPE_PASSPHRASE:
1474 field = "PASSPHRASE";
1475 txt = "Private key passphrase";
1476 config->pending_req_passphrase++;
1482 if (sm->eapol_cb->eap_param_needed)
1483 sm->eapol_cb->eap_param_needed(sm->eapol_ctx, field, txt);
1485 #else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1486 #define eap_sm_request(sm, type, msg, msglen) do { } while (0)
1487 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1491 * eap_sm_request_identity - Request identity from user (ctrl_iface)
1492 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1494 * EAP methods can call this function to request identity information for the
1495 * current network. This is normally called when the identity is not included
1496 * in the network configuration. The request will be sent to monitor programs
1497 * through the control interface.
1499 void eap_sm_request_identity(struct eap_sm *sm)
1501 eap_sm_request(sm, TYPE_IDENTITY, NULL, 0);
1506 * eap_sm_request_password - Request password from user (ctrl_iface)
1507 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1509 * EAP methods can call this function to request password information for the
1510 * current network. This is normally called when the password is not included
1511 * in the network configuration. The request will be sent to monitor programs
1512 * through the control interface.
1514 void eap_sm_request_password(struct eap_sm *sm)
1516 eap_sm_request(sm, TYPE_PASSWORD, NULL, 0);
1521 * eap_sm_request_new_password - Request new password from user (ctrl_iface)
1522 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1524 * EAP methods can call this function to request new password information for
1525 * the current network. This is normally called when the EAP method indicates
1526 * that the current password has expired and password change is required. The
1527 * request will be sent to monitor programs through the control interface.
1529 void eap_sm_request_new_password(struct eap_sm *sm)
1531 eap_sm_request(sm, TYPE_NEW_PASSWORD, NULL, 0);
1536 * eap_sm_request_pin - Request SIM or smart card PIN from user (ctrl_iface)
1537 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1539 * EAP methods can call this function to request SIM or smart card PIN
1540 * information for the current network. This is normally called when the PIN is
1541 * not included in the network configuration. The request will be sent to
1542 * monitor programs through the control interface.
1544 void eap_sm_request_pin(struct eap_sm *sm)
1546 eap_sm_request(sm, TYPE_PIN, NULL, 0);
1551 * eap_sm_request_otp - Request one time password from user (ctrl_iface)
1552 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1553 * @msg: Message to be displayed to the user when asking for OTP
1554 * @msg_len: Length of the user displayable message
1556 * EAP methods can call this function to request open time password (OTP) for
1557 * the current network. The request will be sent to monitor programs through
1558 * the control interface.
1560 void eap_sm_request_otp(struct eap_sm *sm, const char *msg, size_t msg_len)
1562 eap_sm_request(sm, TYPE_OTP, msg, msg_len);
1567 * eap_sm_request_passphrase - Request passphrase from user (ctrl_iface)
1568 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1570 * EAP methods can call this function to request passphrase for a private key
1571 * for the current network. This is normally called when the passphrase is not
1572 * included in the network configuration. The request will be sent to monitor
1573 * programs through the control interface.
1575 void eap_sm_request_passphrase(struct eap_sm *sm)
1577 eap_sm_request(sm, TYPE_PASSPHRASE, NULL, 0);
1582 * eap_sm_notify_ctrl_attached - Notification of attached monitor
1583 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1585 * Notify EAP state machines that a monitor was attached to the control
1586 * interface to trigger re-sending of pending requests for user input.
1588 void eap_sm_notify_ctrl_attached(struct eap_sm *sm)
1590 struct eap_peer_config *config = eap_get_config(sm);
1595 /* Re-send any pending requests for user data since a new control
1596 * interface was added. This handles cases where the EAP authentication
1597 * starts immediately after system startup when the user interface is
1598 * not yet running. */
1599 if (config->pending_req_identity)
1600 eap_sm_request_identity(sm);
1601 if (config->pending_req_password)
1602 eap_sm_request_password(sm);
1603 if (config->pending_req_new_password)
1604 eap_sm_request_new_password(sm);
1605 if (config->pending_req_otp)
1606 eap_sm_request_otp(sm, NULL, 0);
1607 if (config->pending_req_pin)
1608 eap_sm_request_pin(sm);
1609 if (config->pending_req_passphrase)
1610 eap_sm_request_passphrase(sm);
1614 static int eap_allowed_phase2_type(int vendor, int type)
1616 if (vendor != EAP_VENDOR_IETF)
1618 return type != EAP_TYPE_PEAP && type != EAP_TYPE_TTLS &&
1619 type != EAP_TYPE_FAST;
1624 * eap_get_phase2_type - Get EAP type for the given EAP phase 2 method name
1625 * @name: EAP method name, e.g., MD5
1626 * @vendor: Buffer for returning EAP Vendor-Id
1627 * Returns: EAP method type or %EAP_TYPE_NONE if not found
1629 * This function maps EAP type names into EAP type numbers that are allowed for
1630 * Phase 2, i.e., for tunneled authentication. Phase 2 is used, e.g., with
1631 * EAP-PEAP, EAP-TTLS, and EAP-FAST.
1633 u32 eap_get_phase2_type(const char *name, int *vendor)
1636 u8 type = eap_peer_get_type(name, &v);
1637 if (eap_allowed_phase2_type(v, type)) {
1641 *vendor = EAP_VENDOR_IETF;
1642 return EAP_TYPE_NONE;
1647 * eap_get_phase2_types - Get list of allowed EAP phase 2 types
1648 * @config: Pointer to a network configuration
1649 * @count: Pointer to a variable to be filled with number of returned EAP types
1650 * Returns: Pointer to allocated type list or %NULL on failure
1652 * This function generates an array of allowed EAP phase 2 (tunneled) types for
1653 * the given network configuration.
1655 struct eap_method_type * eap_get_phase2_types(struct eap_peer_config *config,
1658 struct eap_method_type *buf;
1662 const struct eap_method *methods, *m;
1664 methods = eap_peer_get_methods(&mcount);
1665 if (methods == NULL)
1668 buf = os_malloc(mcount * sizeof(struct eap_method_type));
1672 for (m = methods; m; m = m->next) {
1675 if (eap_allowed_phase2_type(vendor, method)) {
1676 if (vendor == EAP_VENDOR_IETF &&
1677 method == EAP_TYPE_TLS && config &&
1678 config->private_key2 == NULL)
1680 buf[*count].vendor = vendor;
1681 buf[*count].method = method;
1691 * eap_set_fast_reauth - Update fast_reauth setting
1692 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1693 * @enabled: 1 = Fast reauthentication is enabled, 0 = Disabled
1695 void eap_set_fast_reauth(struct eap_sm *sm, int enabled)
1697 sm->fast_reauth = enabled;
1702 * eap_set_workaround - Update EAP workarounds setting
1703 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1704 * @workaround: 1 = Enable EAP workarounds, 0 = Disable EAP workarounds
1706 void eap_set_workaround(struct eap_sm *sm, unsigned int workaround)
1708 sm->workaround = workaround;
1713 * eap_get_config - Get current network configuration
1714 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1715 * Returns: Pointer to the current network configuration or %NULL if not found
1717 * EAP peer methods should avoid using this function if they can use other
1718 * access functions, like eap_get_config_identity() and
1719 * eap_get_config_password(), that do not require direct access to
1720 * struct eap_peer_config.
1722 struct eap_peer_config * eap_get_config(struct eap_sm *sm)
1724 return sm->eapol_cb->get_config(sm->eapol_ctx);
1729 * eap_get_config_identity - Get identity from the network configuration
1730 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1731 * @len: Buffer for the length of the identity
1732 * Returns: Pointer to the identity or %NULL if not found
1734 const u8 * eap_get_config_identity(struct eap_sm *sm, size_t *len)
1736 struct eap_peer_config *config = eap_get_config(sm);
1739 *len = config->identity_len;
1740 return config->identity;
1745 * eap_get_config_password - Get password from the network configuration
1746 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1747 * @len: Buffer for the length of the password
1748 * Returns: Pointer to the password or %NULL if not found
1750 const u8 * eap_get_config_password(struct eap_sm *sm, size_t *len)
1752 struct eap_peer_config *config = eap_get_config(sm);
1755 *len = config->password_len;
1756 return config->password;
1761 * eap_get_config_password2 - Get password from the network configuration
1762 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1763 * @len: Buffer for the length of the password
1764 * @hash: Buffer for returning whether the password is stored as a
1765 * NtPasswordHash instead of plaintext password; can be %NULL if this
1766 * information is not needed
1767 * Returns: Pointer to the password or %NULL if not found
1769 const u8 * eap_get_config_password2(struct eap_sm *sm, size_t *len, int *hash)
1771 struct eap_peer_config *config = eap_get_config(sm);
1774 *len = config->password_len;
1776 *hash = !!(config->flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH);
1777 return config->password;
1782 * eap_get_config_new_password - Get new password from network configuration
1783 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1784 * @len: Buffer for the length of the new password
1785 * Returns: Pointer to the new password or %NULL if not found
1787 const u8 * eap_get_config_new_password(struct eap_sm *sm, size_t *len)
1789 struct eap_peer_config *config = eap_get_config(sm);
1792 *len = config->new_password_len;
1793 return config->new_password;
1798 * eap_get_config_otp - Get one-time password from the network configuration
1799 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1800 * @len: Buffer for the length of the one-time password
1801 * Returns: Pointer to the one-time password or %NULL if not found
1803 const u8 * eap_get_config_otp(struct eap_sm *sm, size_t *len)
1805 struct eap_peer_config *config = eap_get_config(sm);
1808 *len = config->otp_len;
1814 * eap_clear_config_otp - Clear used one-time password
1815 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1817 * This function clears a used one-time password (OTP) from the current network
1818 * configuration. This should be called when the OTP has been used and is not
1821 void eap_clear_config_otp(struct eap_sm *sm)
1823 struct eap_peer_config *config = eap_get_config(sm);
1826 os_memset(config->otp, 0, config->otp_len);
1827 os_free(config->otp);
1829 config->otp_len = 0;
1834 * eap_get_config_phase1 - Get phase1 data from the network configuration
1835 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1836 * Returns: Pointer to the phase1 data or %NULL if not found
1838 const char * eap_get_config_phase1(struct eap_sm *sm)
1840 struct eap_peer_config *config = eap_get_config(sm);
1843 return config->phase1;
1848 * eap_get_config_phase2 - Get phase2 data from the network configuration
1849 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1850 * Returns: Pointer to the phase1 data or %NULL if not found
1852 const char * eap_get_config_phase2(struct eap_sm *sm)
1854 struct eap_peer_config *config = eap_get_config(sm);
1857 return config->phase2;
1862 * eap_key_available - Get key availability (eapKeyAvailable variable)
1863 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1864 * Returns: 1 if EAP keying material is available, 0 if not
1866 int eap_key_available(struct eap_sm *sm)
1868 return sm ? sm->eapKeyAvailable : 0;
1873 * eap_notify_success - Notify EAP state machine about external success trigger
1874 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1876 * This function is called when external event, e.g., successful completion of
1877 * WPA-PSK key handshake, is indicating that EAP state machine should move to
1878 * success state. This is mainly used with security modes that do not use EAP
1879 * state machine (e.g., WPA-PSK).
1881 void eap_notify_success(struct eap_sm *sm)
1884 sm->decision = DECISION_COND_SUCC;
1885 sm->EAP_state = EAP_SUCCESS;
1891 * eap_notify_lower_layer_success - Notification of lower layer success
1892 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1894 * Notify EAP state machines that a lower layer has detected a successful
1895 * authentication. This is used to recover from dropped EAP-Success messages.
1897 void eap_notify_lower_layer_success(struct eap_sm *sm)
1902 if (eapol_get_bool(sm, EAPOL_eapSuccess) ||
1903 sm->decision == DECISION_FAIL ||
1904 (sm->methodState != METHOD_MAY_CONT &&
1905 sm->methodState != METHOD_DONE))
1908 if (sm->eapKeyData != NULL)
1909 sm->eapKeyAvailable = TRUE;
1910 eapol_set_bool(sm, EAPOL_eapSuccess, TRUE);
1911 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
1912 "EAP authentication completed successfully (based on lower "
1918 * eap_get_eapKeyData - Get master session key (MSK) from EAP state machine
1919 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1920 * @len: Pointer to variable that will be set to number of bytes in the key
1921 * Returns: Pointer to the EAP keying data or %NULL on failure
1923 * Fetch EAP keying material (MSK, eapKeyData) from the EAP state machine. The
1924 * key is available only after a successful authentication. EAP state machine
1925 * continues to manage the key data and the caller must not change or free the
1928 const u8 * eap_get_eapKeyData(struct eap_sm *sm, size_t *len)
1930 if (sm == NULL || sm->eapKeyData == NULL) {
1935 *len = sm->eapKeyDataLen;
1936 return sm->eapKeyData;
1941 * eap_get_eapKeyData - Get EAP response data
1942 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1943 * Returns: Pointer to the EAP response (eapRespData) or %NULL on failure
1945 * Fetch EAP response (eapRespData) from the EAP state machine. This data is
1946 * available when EAP state machine has processed an incoming EAP request. The
1947 * EAP state machine does not maintain a reference to the response after this
1948 * function is called and the caller is responsible for freeing the data.
1950 struct wpabuf * eap_get_eapRespData(struct eap_sm *sm)
1952 struct wpabuf *resp;
1954 if (sm == NULL || sm->eapRespData == NULL)
1957 resp = sm->eapRespData;
1958 sm->eapRespData = NULL;
1965 * eap_sm_register_scard_ctx - Notification of smart card context
1966 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1967 * @ctx: Context data for smart card operations
1969 * Notify EAP state machines of context data for smart card operations. This
1970 * context data will be used as a parameter for scard_*() functions.
1972 void eap_register_scard_ctx(struct eap_sm *sm, void *ctx)
1975 sm->scard_ctx = ctx;
1980 * eap_set_config_blob - Set or add a named configuration blob
1981 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1982 * @blob: New value for the blob
1984 * Adds a new configuration blob or replaces the current value of an existing
1987 void eap_set_config_blob(struct eap_sm *sm, struct wpa_config_blob *blob)
1989 #ifndef CONFIG_NO_CONFIG_BLOBS
1990 sm->eapol_cb->set_config_blob(sm->eapol_ctx, blob);
1991 #endif /* CONFIG_NO_CONFIG_BLOBS */
1996 * eap_get_config_blob - Get a named configuration blob
1997 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1998 * @name: Name of the blob
1999 * Returns: Pointer to blob data or %NULL if not found
2001 const struct wpa_config_blob * eap_get_config_blob(struct eap_sm *sm,
2004 #ifndef CONFIG_NO_CONFIG_BLOBS
2005 return sm->eapol_cb->get_config_blob(sm->eapol_ctx, name);
2006 #else /* CONFIG_NO_CONFIG_BLOBS */
2008 #endif /* CONFIG_NO_CONFIG_BLOBS */
2013 * eap_set_force_disabled - Set force_disabled flag
2014 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2015 * @disabled: 1 = EAP disabled, 0 = EAP enabled
2017 * This function is used to force EAP state machine to be disabled when it is
2018 * not in use (e.g., with WPA-PSK or plaintext connections).
2020 void eap_set_force_disabled(struct eap_sm *sm, int disabled)
2022 sm->force_disabled = disabled;
2027 * eap_notify_pending - Notify that EAP method is ready to re-process a request
2028 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2030 * An EAP method can perform a pending operation (e.g., to get a response from
2031 * an external process). Once the response is available, this function can be
2032 * used to request EAPOL state machine to retry delivering the previously
2033 * received (and still unanswered) EAP request to EAP state machine.
2035 void eap_notify_pending(struct eap_sm *sm)
2037 sm->eapol_cb->notify_pending(sm->eapol_ctx);
2042 * eap_invalidate_cached_session - Mark cached session data invalid
2043 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2045 void eap_invalidate_cached_session(struct eap_sm *sm)
2048 eap_deinit_prev_method(sm, "invalidate");
2052 int eap_is_wps_pbc_enrollee(struct eap_peer_config *conf)
2054 if (conf->identity_len != WSC_ID_ENROLLEE_LEN ||
2055 os_memcmp(conf->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN))
2056 return 0; /* Not a WPS Enrollee */
2058 if (conf->phase1 == NULL || os_strstr(conf->phase1, "pbc=1") == NULL)
2059 return 0; /* Not using PBC */
2065 int eap_is_wps_pin_enrollee(struct eap_peer_config *conf)
2067 if (conf->identity_len != WSC_ID_ENROLLEE_LEN ||
2068 os_memcmp(conf->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN))
2069 return 0; /* Not a WPS Enrollee */
2071 if (conf->phase1 == NULL || os_strstr(conf->phase1, "pin=") == NULL)
2072 return 0; /* Not using PIN */