2 * EAP peer state machines (RFC 4137)
3 * Copyright (c) 2004-2010, 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).
27 #include "pcsc_funcs.h"
28 #include "state_machine.h"
29 #include "crypto/crypto.h"
30 #include "crypto/tls.h"
31 #include "common/wpa_ctrl.h"
32 #include "eap_common/eap_wsc_common.h"
34 #include "eap_config.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
);
231 wpa_msg(sm
->msg_ctx
, MSG_INFO
, WPA_EVENT_EAP_PROPOSED_METHOD
232 "vendor=%u method=%u -> NAK",
233 sm
->reqVendor
, method
);
237 wpa_msg(sm
->msg_ctx
, MSG_INFO
, WPA_EVENT_EAP_PROPOSED_METHOD
238 "vendor=%u method=%u", sm
->reqVendor
, method
);
241 * RFC 4137 does not define specific operation for fast
242 * re-authentication (session resumption). The design here is to allow
243 * the previously used method data to be maintained for
244 * re-authentication if the method support session resumption.
245 * Otherwise, the previously used method data is freed and a new method
248 if (sm
->fast_reauth
&&
249 sm
->m
&& sm
->m
->vendor
== sm
->reqVendor
&&
250 sm
->m
->method
== method
&&
251 sm
->m
->has_reauth_data
&&
252 sm
->m
->has_reauth_data(sm
, sm
->eap_method_priv
)) {
253 wpa_printf(MSG_DEBUG
, "EAP: Using previous method data"
254 " for fast re-authentication");
257 eap_deinit_prev_method(sm
, "GET_METHOD");
261 sm
->selectedMethod
= sm
->reqMethod
;
263 sm
->m
= eap_peer_get_eap_method(sm
->reqVendor
, method
);
265 wpa_printf(MSG_DEBUG
, "EAP: Could not find selected method: "
266 "vendor %d method %d",
267 sm
->reqVendor
, method
);
271 wpa_printf(MSG_DEBUG
, "EAP: Initialize selected EAP method: "
272 "vendor %u method %u (%s)",
273 sm
->reqVendor
, method
, sm
->m
->name
);
275 sm
->eap_method_priv
= sm
->m
->init_for_reauth(
276 sm
, sm
->eap_method_priv
);
278 sm
->eap_method_priv
= sm
->m
->init(sm
);
280 if (sm
->eap_method_priv
== NULL
) {
281 struct eap_peer_config
*config
= eap_get_config(sm
);
282 wpa_msg(sm
->msg_ctx
, MSG_INFO
,
283 "EAP: Failed to initialize EAP method: vendor %u "
285 sm
->reqVendor
, method
, sm
->m
->name
);
287 sm
->methodState
= METHOD_NONE
;
288 sm
->selectedMethod
= EAP_TYPE_NONE
;
289 if (sm
->reqMethod
== EAP_TYPE_TLS
&& config
&&
290 (config
->pending_req_pin
||
291 config
->pending_req_passphrase
)) {
293 * Return without generating Nak in order to allow
294 * entering of PIN code or passphrase to retry the
295 * current EAP packet.
297 wpa_printf(MSG_DEBUG
, "EAP: Pending PIN/passphrase "
298 "request - skip Nak");
305 sm
->methodState
= METHOD_INIT
;
306 wpa_msg(sm
->msg_ctx
, MSG_INFO
, WPA_EVENT_EAP_METHOD
307 "EAP vendor %u method %u (%s) selected",
308 sm
->reqVendor
, method
, sm
->m
->name
);
312 wpabuf_free(sm
->eapRespData
);
313 sm
->eapRespData
= NULL
;
314 sm
->eapRespData
= eap_sm_buildNak(sm
, sm
->reqId
);
319 * The method processing happens here. The request from the authenticator is
320 * processed, and an appropriate response packet is built.
322 SM_STATE(EAP
, METHOD
)
324 struct wpabuf
*eapReqData
;
325 struct eap_method_ret ret
;
327 SM_ENTRY(EAP
, METHOD
);
329 wpa_printf(MSG_WARNING
, "EAP::METHOD - method not selected");
333 eapReqData
= eapol_get_eapReqData(sm
);
336 * Get ignore, methodState, decision, allowNotifications, and
337 * eapRespData. RFC 4137 uses three separate method procedure (check,
338 * process, and buildResp) in this state. These have been combined into
339 * a single function call to m->process() in order to optimize EAP
340 * method implementation interface a bit. These procedures are only
341 * used from within this METHOD state, so there is no need to keep
342 * these as separate C functions.
344 * The RFC 4137 procedures return values as follows:
345 * ignore = m.check(eapReqData)
346 * (methodState, decision, allowNotifications) = m.process(eapReqData)
347 * eapRespData = m.buildResp(reqId)
349 os_memset(&ret
, 0, sizeof(ret
));
350 ret
.ignore
= sm
->ignore
;
351 ret
.methodState
= sm
->methodState
;
352 ret
.decision
= sm
->decision
;
353 ret
.allowNotifications
= sm
->allowNotifications
;
354 wpabuf_free(sm
->eapRespData
);
355 sm
->eapRespData
= NULL
;
356 sm
->eapRespData
= sm
->m
->process(sm
, sm
->eap_method_priv
, &ret
,
358 wpa_printf(MSG_DEBUG
, "EAP: method process -> ignore=%s "
359 "methodState=%s decision=%s",
360 ret
.ignore
? "TRUE" : "FALSE",
361 eap_sm_method_state_txt(ret
.methodState
),
362 eap_sm_decision_txt(ret
.decision
));
364 sm
->ignore
= ret
.ignore
;
367 sm
->methodState
= ret
.methodState
;
368 sm
->decision
= ret
.decision
;
369 sm
->allowNotifications
= ret
.allowNotifications
;
371 if (sm
->m
->isKeyAvailable
&& sm
->m
->getKey
&&
372 sm
->m
->isKeyAvailable(sm
, sm
->eap_method_priv
)) {
373 os_free(sm
->eapKeyData
);
374 sm
->eapKeyData
= sm
->m
->getKey(sm
, sm
->eap_method_priv
,
381 * This state signals the lower layer that a response packet is ready to be
384 SM_STATE(EAP
, SEND_RESPONSE
)
386 SM_ENTRY(EAP
, SEND_RESPONSE
);
387 wpabuf_free(sm
->lastRespData
);
388 if (sm
->eapRespData
) {
390 os_memcpy(sm
->last_md5
, sm
->req_md5
, 16);
391 sm
->lastId
= sm
->reqId
;
392 sm
->lastRespData
= wpabuf_dup(sm
->eapRespData
);
393 eapol_set_bool(sm
, EAPOL_eapResp
, TRUE
);
395 sm
->lastRespData
= NULL
;
396 eapol_set_bool(sm
, EAPOL_eapReq
, FALSE
);
397 eapol_set_int(sm
, EAPOL_idleWhile
, sm
->ClientTimeout
);
402 * This state signals the lower layer that the request was discarded, and no
403 * response packet will be sent at this time.
405 SM_STATE(EAP
, DISCARD
)
407 SM_ENTRY(EAP
, DISCARD
);
408 eapol_set_bool(sm
, EAPOL_eapReq
, FALSE
);
409 eapol_set_bool(sm
, EAPOL_eapNoResp
, TRUE
);
414 * Handles requests for Identity method and builds a response.
416 SM_STATE(EAP
, IDENTITY
)
418 const struct wpabuf
*eapReqData
;
420 SM_ENTRY(EAP
, IDENTITY
);
421 eapReqData
= eapol_get_eapReqData(sm
);
422 eap_sm_processIdentity(sm
, eapReqData
);
423 wpabuf_free(sm
->eapRespData
);
424 sm
->eapRespData
= NULL
;
425 sm
->eapRespData
= eap_sm_buildIdentity(sm
, sm
->reqId
, 0);
430 * Handles requests for Notification method and builds a response.
432 SM_STATE(EAP
, NOTIFICATION
)
434 const struct wpabuf
*eapReqData
;
436 SM_ENTRY(EAP
, NOTIFICATION
);
437 eapReqData
= eapol_get_eapReqData(sm
);
438 eap_sm_processNotify(sm
, eapReqData
);
439 wpabuf_free(sm
->eapRespData
);
440 sm
->eapRespData
= NULL
;
441 sm
->eapRespData
= eap_sm_buildNotify(sm
->reqId
);
446 * This state retransmits the previous response packet.
448 SM_STATE(EAP
, RETRANSMIT
)
450 SM_ENTRY(EAP
, RETRANSMIT
);
451 wpabuf_free(sm
->eapRespData
);
452 if (sm
->lastRespData
)
453 sm
->eapRespData
= wpabuf_dup(sm
->lastRespData
);
455 sm
->eapRespData
= NULL
;
460 * This state is entered in case of a successful completion of authentication
461 * and state machine waits here until port is disabled or EAP authentication is
464 SM_STATE(EAP
, SUCCESS
)
466 SM_ENTRY(EAP
, SUCCESS
);
467 if (sm
->eapKeyData
!= NULL
)
468 sm
->eapKeyAvailable
= TRUE
;
469 eapol_set_bool(sm
, EAPOL_eapSuccess
, TRUE
);
472 * RFC 4137 does not clear eapReq here, but this seems to be required
473 * to avoid processing the same request twice when state machine is
476 eapol_set_bool(sm
, EAPOL_eapReq
, FALSE
);
479 * RFC 4137 does not set eapNoResp here, but this seems to be required
480 * to get EAPOL Supplicant backend state machine into SUCCESS state. In
481 * addition, either eapResp or eapNoResp is required to be set after
482 * processing the received EAP frame.
484 eapol_set_bool(sm
, EAPOL_eapNoResp
, TRUE
);
486 wpa_msg(sm
->msg_ctx
, MSG_INFO
, WPA_EVENT_EAP_SUCCESS
487 "EAP authentication completed successfully");
492 * This state is entered in case of a failure and state machine waits here
493 * until port is disabled or EAP authentication is restarted.
495 SM_STATE(EAP
, FAILURE
)
497 SM_ENTRY(EAP
, FAILURE
);
498 eapol_set_bool(sm
, EAPOL_eapFail
, TRUE
);
501 * RFC 4137 does not clear eapReq here, but this seems to be required
502 * to avoid processing the same request twice when state machine is
505 eapol_set_bool(sm
, EAPOL_eapReq
, FALSE
);
508 * RFC 4137 does not set eapNoResp here. However, either eapResp or
509 * eapNoResp is required to be set after processing the received EAP
512 eapol_set_bool(sm
, EAPOL_eapNoResp
, TRUE
);
514 wpa_msg(sm
->msg_ctx
, MSG_INFO
, WPA_EVENT_EAP_FAILURE
515 "EAP authentication failed");
517 sm
->prev_failure
= 1;
521 static int eap_success_workaround(struct eap_sm
*sm
, int reqId
, int lastId
)
524 * At least Microsoft IAS and Meetinghouse Aegis seem to be sending
525 * EAP-Success/Failure with lastId + 1 even though RFC 3748 and
526 * RFC 4137 require that reqId == lastId. In addition, it looks like
527 * Ringmaster v2.1.2.0 would be using lastId + 2 in EAP-Success.
529 * Accept this kind of Id if EAP workarounds are enabled. These are
530 * unauthenticated plaintext messages, so this should have minimal
531 * security implications (bit easier to fake EAP-Success/Failure).
533 if (sm
->workaround
&& (reqId
== ((lastId
+ 1) & 0xff) ||
534 reqId
== ((lastId
+ 2) & 0xff))) {
535 wpa_printf(MSG_DEBUG
, "EAP: Workaround for unexpected "
536 "identifier field in EAP Success: "
537 "reqId=%d lastId=%d (these are supposed to be "
538 "same)", reqId
, lastId
);
541 wpa_printf(MSG_DEBUG
, "EAP: EAP-Success Id mismatch - reqId=%d "
542 "lastId=%d", reqId
, lastId
);
548 * RFC 4137 - Appendix A.1: EAP Peer State Machine - State transitions
551 static void eap_peer_sm_step_idle(struct eap_sm
*sm
)
554 * The first three transitions are from RFC 4137. The last two are
555 * local additions to handle special cases with LEAP and PEAP server
556 * not sending EAP-Success in some cases.
558 if (eapol_get_bool(sm
, EAPOL_eapReq
))
559 SM_ENTER(EAP
, RECEIVED
);
560 else if ((eapol_get_bool(sm
, EAPOL_altAccept
) &&
561 sm
->decision
!= DECISION_FAIL
) ||
562 (eapol_get_int(sm
, EAPOL_idleWhile
) == 0 &&
563 sm
->decision
== DECISION_UNCOND_SUCC
))
564 SM_ENTER(EAP
, SUCCESS
);
565 else if (eapol_get_bool(sm
, EAPOL_altReject
) ||
566 (eapol_get_int(sm
, EAPOL_idleWhile
) == 0 &&
567 sm
->decision
!= DECISION_UNCOND_SUCC
) ||
568 (eapol_get_bool(sm
, EAPOL_altAccept
) &&
569 sm
->methodState
!= METHOD_CONT
&&
570 sm
->decision
== DECISION_FAIL
))
571 SM_ENTER(EAP
, FAILURE
);
572 else if (sm
->selectedMethod
== EAP_TYPE_LEAP
&&
573 sm
->leap_done
&& sm
->decision
!= DECISION_FAIL
&&
574 sm
->methodState
== METHOD_DONE
)
575 SM_ENTER(EAP
, SUCCESS
);
576 else if (sm
->selectedMethod
== EAP_TYPE_PEAP
&&
577 sm
->peap_done
&& sm
->decision
!= DECISION_FAIL
&&
578 sm
->methodState
== METHOD_DONE
)
579 SM_ENTER(EAP
, SUCCESS
);
583 static int eap_peer_req_is_duplicate(struct eap_sm
*sm
)
587 duplicate
= (sm
->reqId
== sm
->lastId
) && sm
->rxReq
;
588 if (sm
->workaround
&& duplicate
&&
589 os_memcmp(sm
->req_md5
, sm
->last_md5
, 16) != 0) {
591 * RFC 4137 uses (reqId == lastId) as the only verification for
592 * duplicate EAP requests. However, this misses cases where the
593 * AS is incorrectly using the same id again; and
594 * unfortunately, such implementations exist. Use MD5 hash as
595 * an extra verification for the packets being duplicate to
596 * workaround these issues.
598 wpa_printf(MSG_DEBUG
, "EAP: AS used the same Id again, but "
599 "EAP packets were not identical");
600 wpa_printf(MSG_DEBUG
, "EAP: workaround - assume this is not a "
609 static void eap_peer_sm_step_received(struct eap_sm
*sm
)
611 int duplicate
= eap_peer_req_is_duplicate(sm
);
614 * Two special cases below for LEAP are local additions to work around
615 * odd LEAP behavior (EAP-Success in the middle of authentication and
616 * then swapped roles). Other transitions are based on RFC 4137.
618 if (sm
->rxSuccess
&& sm
->decision
!= DECISION_FAIL
&&
619 (sm
->reqId
== sm
->lastId
||
620 eap_success_workaround(sm
, sm
->reqId
, sm
->lastId
)))
621 SM_ENTER(EAP
, SUCCESS
);
622 else if (sm
->methodState
!= METHOD_CONT
&&
624 sm
->decision
!= DECISION_UNCOND_SUCC
) ||
625 (sm
->rxSuccess
&& sm
->decision
== DECISION_FAIL
&&
626 (sm
->selectedMethod
!= EAP_TYPE_LEAP
||
627 sm
->methodState
!= METHOD_MAY_CONT
))) &&
628 (sm
->reqId
== sm
->lastId
||
629 eap_success_workaround(sm
, sm
->reqId
, sm
->lastId
)))
630 SM_ENTER(EAP
, FAILURE
);
631 else if (sm
->rxReq
&& duplicate
)
632 SM_ENTER(EAP
, RETRANSMIT
);
633 else if (sm
->rxReq
&& !duplicate
&&
634 sm
->reqMethod
== EAP_TYPE_NOTIFICATION
&&
635 sm
->allowNotifications
)
636 SM_ENTER(EAP
, NOTIFICATION
);
637 else if (sm
->rxReq
&& !duplicate
&&
638 sm
->selectedMethod
== EAP_TYPE_NONE
&&
639 sm
->reqMethod
== EAP_TYPE_IDENTITY
)
640 SM_ENTER(EAP
, IDENTITY
);
641 else if (sm
->rxReq
&& !duplicate
&&
642 sm
->selectedMethod
== EAP_TYPE_NONE
&&
643 sm
->reqMethod
!= EAP_TYPE_IDENTITY
&&
644 sm
->reqMethod
!= EAP_TYPE_NOTIFICATION
)
645 SM_ENTER(EAP
, GET_METHOD
);
646 else if (sm
->rxReq
&& !duplicate
&&
647 sm
->reqMethod
== sm
->selectedMethod
&&
648 sm
->methodState
!= METHOD_DONE
)
649 SM_ENTER(EAP
, METHOD
);
650 else if (sm
->selectedMethod
== EAP_TYPE_LEAP
&&
651 (sm
->rxSuccess
|| sm
->rxResp
))
652 SM_ENTER(EAP
, METHOD
);
654 SM_ENTER(EAP
, DISCARD
);
658 static void eap_peer_sm_step_local(struct eap_sm
*sm
)
660 switch (sm
->EAP_state
) {
665 if (eapol_get_bool(sm
, EAPOL_portEnabled
) &&
667 SM_ENTER(EAP
, INITIALIZE
);
670 eap_peer_sm_step_idle(sm
);
673 eap_peer_sm_step_received(sm
);
676 if (sm
->selectedMethod
== sm
->reqMethod
)
677 SM_ENTER(EAP
, METHOD
);
679 SM_ENTER(EAP
, SEND_RESPONSE
);
683 SM_ENTER(EAP
, DISCARD
);
685 SM_ENTER(EAP
, SEND_RESPONSE
);
687 case EAP_SEND_RESPONSE
:
694 SM_ENTER(EAP
, SEND_RESPONSE
);
696 case EAP_NOTIFICATION
:
697 SM_ENTER(EAP
, SEND_RESPONSE
);
700 SM_ENTER(EAP
, SEND_RESPONSE
);
712 /* Global transitions */
713 if (eapol_get_bool(sm
, EAPOL_eapRestart
) &&
714 eapol_get_bool(sm
, EAPOL_portEnabled
))
715 SM_ENTER_GLOBAL(EAP
, INITIALIZE
);
716 else if (!eapol_get_bool(sm
, EAPOL_portEnabled
) || sm
->force_disabled
)
717 SM_ENTER_GLOBAL(EAP
, DISABLED
);
718 else if (sm
->num_rounds
> EAP_MAX_AUTH_ROUNDS
) {
719 /* RFC 4137 does not place any limit on number of EAP messages
720 * in an authentication session. However, some error cases have
721 * ended up in a state were EAP messages were sent between the
722 * peer and server in a loop (e.g., TLS ACK frame in both
723 * direction). Since this is quite undesired outcome, limit the
724 * total number of EAP round-trips and abort authentication if
725 * this limit is exceeded.
727 if (sm
->num_rounds
== EAP_MAX_AUTH_ROUNDS
+ 1) {
728 wpa_msg(sm
->msg_ctx
, MSG_INFO
, "EAP: more than %d "
729 "authentication rounds - abort",
730 EAP_MAX_AUTH_ROUNDS
);
732 SM_ENTER_GLOBAL(EAP
, FAILURE
);
735 /* Local transitions */
736 eap_peer_sm_step_local(sm
);
741 static Boolean
eap_sm_allowMethod(struct eap_sm
*sm
, int vendor
,
744 if (!eap_allowed_method(sm
, vendor
, method
)) {
745 wpa_printf(MSG_DEBUG
, "EAP: configuration does not allow: "
746 "vendor %u method %u", vendor
, method
);
749 if (eap_peer_get_eap_method(vendor
, method
))
751 wpa_printf(MSG_DEBUG
, "EAP: not included in build: "
752 "vendor %u method %u", vendor
, method
);
757 static struct wpabuf
* eap_sm_build_expanded_nak(
758 struct eap_sm
*sm
, int id
, const struct eap_method
*methods
,
763 const struct eap_method
*m
;
765 wpa_printf(MSG_DEBUG
, "EAP: Building expanded EAP-Nak");
767 /* RFC 3748 - 5.3.2: Expanded Nak */
768 resp
= eap_msg_alloc(EAP_VENDOR_IETF
, EAP_TYPE_EXPANDED
,
769 8 + 8 * (count
+ 1), EAP_CODE_RESPONSE
, id
);
773 wpabuf_put_be24(resp
, EAP_VENDOR_IETF
);
774 wpabuf_put_be32(resp
, EAP_TYPE_NAK
);
776 for (m
= methods
; m
; m
= m
->next
) {
777 if (sm
->reqVendor
== m
->vendor
&&
778 sm
->reqVendorMethod
== m
->method
)
779 continue; /* do not allow the current method again */
780 if (eap_allowed_method(sm
, m
->vendor
, m
->method
)) {
781 wpa_printf(MSG_DEBUG
, "EAP: allowed type: "
782 "vendor=%u method=%u",
783 m
->vendor
, m
->method
);
784 wpabuf_put_u8(resp
, EAP_TYPE_EXPANDED
);
785 wpabuf_put_be24(resp
, m
->vendor
);
786 wpabuf_put_be32(resp
, m
->method
);
792 wpa_printf(MSG_DEBUG
, "EAP: no more allowed methods");
793 wpabuf_put_u8(resp
, EAP_TYPE_EXPANDED
);
794 wpabuf_put_be24(resp
, EAP_VENDOR_IETF
);
795 wpabuf_put_be32(resp
, EAP_TYPE_NONE
);
798 eap_update_len(resp
);
804 static struct wpabuf
* eap_sm_buildNak(struct eap_sm
*sm
, int id
)
808 int found
= 0, expanded_found
= 0;
810 const struct eap_method
*methods
, *m
;
812 wpa_printf(MSG_DEBUG
, "EAP: Building EAP-Nak (requested type %u "
813 "vendor=%u method=%u not allowed)", sm
->reqMethod
,
814 sm
->reqVendor
, sm
->reqVendorMethod
);
815 methods
= eap_peer_get_methods(&count
);
818 if (sm
->reqMethod
== EAP_TYPE_EXPANDED
)
819 return eap_sm_build_expanded_nak(sm
, id
, methods
, count
);
821 /* RFC 3748 - 5.3.1: Legacy Nak */
822 resp
= eap_msg_alloc(EAP_VENDOR_IETF
, EAP_TYPE_NAK
,
823 sizeof(struct eap_hdr
) + 1 + count
+ 1,
824 EAP_CODE_RESPONSE
, id
);
828 start
= wpabuf_put(resp
, 0);
829 for (m
= methods
; m
; m
= m
->next
) {
830 if (m
->vendor
== EAP_VENDOR_IETF
&& m
->method
== sm
->reqMethod
)
831 continue; /* do not allow the current method again */
832 if (eap_allowed_method(sm
, m
->vendor
, m
->method
)) {
833 if (m
->vendor
!= EAP_VENDOR_IETF
) {
837 wpabuf_put_u8(resp
, EAP_TYPE_EXPANDED
);
839 wpabuf_put_u8(resp
, m
->method
);
844 wpabuf_put_u8(resp
, EAP_TYPE_NONE
);
845 wpa_hexdump(MSG_DEBUG
, "EAP: allowed methods", start
, found
);
847 eap_update_len(resp
);
853 static void eap_sm_processIdentity(struct eap_sm
*sm
, const struct wpabuf
*req
)
855 const struct eap_hdr
*hdr
= wpabuf_head(req
);
856 const u8
*pos
= (const u8
*) (hdr
+ 1);
859 wpa_msg(sm
->msg_ctx
, MSG_INFO
, WPA_EVENT_EAP_STARTED
860 "EAP authentication started");
863 * RFC 3748 - 5.1: Identity
864 * Data field may contain a displayable message in UTF-8. If this
865 * includes NUL-character, only the data before that should be
866 * displayed. Some EAP implementasitons may piggy-back additional
867 * options after the NUL.
869 /* TODO: could save displayable message so that it can be shown to the
870 * user in case of interaction is required */
871 wpa_hexdump_ascii(MSG_DEBUG
, "EAP: EAP-Request Identity data",
872 pos
, be_to_host16(hdr
->length
) - 5);
877 static int eap_sm_imsi_identity(struct eap_sm
*sm
,
878 struct eap_peer_config
*conf
)
883 struct eap_method_type
*m
= conf
->eap_methods
;
886 imsi_len
= sizeof(imsi
);
887 if (scard_get_imsi(sm
->scard_ctx
, imsi
, &imsi_len
)) {
888 wpa_printf(MSG_WARNING
, "Failed to get IMSI from SIM");
892 wpa_hexdump_ascii(MSG_DEBUG
, "IMSI", (u8
*) imsi
, imsi_len
);
894 for (i
= 0; m
&& (m
[i
].vendor
!= EAP_VENDOR_IETF
||
895 m
[i
].method
!= EAP_TYPE_NONE
); i
++) {
896 if (m
[i
].vendor
== EAP_VENDOR_IETF
&&
897 m
[i
].method
== EAP_TYPE_AKA
) {
903 os_free(conf
->identity
);
904 conf
->identity
= os_malloc(1 + imsi_len
);
905 if (conf
->identity
== NULL
) {
906 wpa_printf(MSG_WARNING
, "Failed to allocate buffer for "
907 "IMSI-based identity");
911 conf
->identity
[0] = aka
? '0' : '1';
912 os_memcpy(conf
->identity
+ 1, imsi
, imsi_len
);
913 conf
->identity_len
= 1 + imsi_len
;
917 #endif /* PCSC_FUNCS */
920 static int eap_sm_set_scard_pin(struct eap_sm
*sm
,
921 struct eap_peer_config
*conf
)
924 if (scard_set_pin(sm
->scard_ctx
, conf
->pin
)) {
926 * Make sure the same PIN is not tried again in order to avoid
932 wpa_printf(MSG_WARNING
, "PIN validation failed");
933 eap_sm_request_pin(sm
);
937 #else /* PCSC_FUNCS */
939 #endif /* PCSC_FUNCS */
942 static int eap_sm_get_scard_identity(struct eap_sm
*sm
,
943 struct eap_peer_config
*conf
)
946 if (eap_sm_set_scard_pin(sm
, conf
))
949 return eap_sm_imsi_identity(sm
, conf
);
950 #else /* PCSC_FUNCS */
952 #endif /* PCSC_FUNCS */
957 * eap_sm_buildIdentity - Build EAP-Identity/Response for the current network
958 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
959 * @id: EAP identifier for the packet
960 * @encrypted: Whether the packet is for encrypted tunnel (EAP phase 2)
961 * Returns: Pointer to the allocated EAP-Identity/Response packet or %NULL on
964 * This function allocates and builds an EAP-Identity/Response packet for the
965 * current network. The caller is responsible for freeing the returned data.
967 struct wpabuf
* eap_sm_buildIdentity(struct eap_sm
*sm
, int id
, int encrypted
)
969 struct eap_peer_config
*config
= eap_get_config(sm
);
974 if (config
== NULL
) {
975 wpa_printf(MSG_WARNING
, "EAP: buildIdentity: configuration "
976 "was not available");
980 if (sm
->m
&& sm
->m
->get_identity
&&
981 (identity
= sm
->m
->get_identity(sm
, sm
->eap_method_priv
,
982 &identity_len
)) != NULL
) {
983 wpa_hexdump_ascii(MSG_DEBUG
, "EAP: using method re-auth "
984 "identity", identity
, identity_len
);
985 } else if (!encrypted
&& config
->anonymous_identity
) {
986 identity
= config
->anonymous_identity
;
987 identity_len
= config
->anonymous_identity_len
;
988 wpa_hexdump_ascii(MSG_DEBUG
, "EAP: using anonymous identity",
989 identity
, identity_len
);
991 identity
= config
->identity
;
992 identity_len
= config
->identity_len
;
993 wpa_hexdump_ascii(MSG_DEBUG
, "EAP: using real identity",
994 identity
, identity_len
);
997 if (identity
== NULL
) {
998 wpa_printf(MSG_WARNING
, "EAP: buildIdentity: identity "
999 "configuration was not available");
1001 if (eap_sm_get_scard_identity(sm
, config
) < 0)
1003 identity
= config
->identity
;
1004 identity_len
= config
->identity_len
;
1005 wpa_hexdump_ascii(MSG_DEBUG
, "permanent identity from "
1006 "IMSI", identity
, identity_len
);
1008 eap_sm_request_identity(sm
);
1011 } else if (config
->pcsc
) {
1012 if (eap_sm_set_scard_pin(sm
, config
) < 0)
1016 resp
= eap_msg_alloc(EAP_VENDOR_IETF
, EAP_TYPE_IDENTITY
, identity_len
,
1017 EAP_CODE_RESPONSE
, id
);
1021 wpabuf_put_data(resp
, identity
, identity_len
);
1027 static void eap_sm_processNotify(struct eap_sm
*sm
, const struct wpabuf
*req
)
1033 pos
= eap_hdr_validate(EAP_VENDOR_IETF
, EAP_TYPE_NOTIFICATION
, req
,
1037 wpa_hexdump_ascii(MSG_DEBUG
, "EAP: EAP-Request Notification data",
1040 msg
= os_malloc(msg_len
+ 1);
1043 for (i
= 0; i
< msg_len
; i
++)
1044 msg
[i
] = isprint(pos
[i
]) ? (char) pos
[i
] : '_';
1045 msg
[msg_len
] = '\0';
1046 wpa_msg(sm
->msg_ctx
, MSG_INFO
, "%s%s",
1047 WPA_EVENT_EAP_NOTIFICATION
, msg
);
1052 static struct wpabuf
* eap_sm_buildNotify(int id
)
1054 struct wpabuf
*resp
;
1056 wpa_printf(MSG_DEBUG
, "EAP: Generating EAP-Response Notification");
1057 resp
= eap_msg_alloc(EAP_VENDOR_IETF
, EAP_TYPE_NOTIFICATION
, 0,
1058 EAP_CODE_RESPONSE
, id
);
1066 static void eap_sm_parseEapReq(struct eap_sm
*sm
, const struct wpabuf
*req
)
1068 const struct eap_hdr
*hdr
;
1072 sm
->rxReq
= sm
->rxResp
= sm
->rxSuccess
= sm
->rxFailure
= FALSE
;
1074 sm
->reqMethod
= EAP_TYPE_NONE
;
1075 sm
->reqVendor
= EAP_VENDOR_IETF
;
1076 sm
->reqVendorMethod
= EAP_TYPE_NONE
;
1078 if (req
== NULL
|| wpabuf_len(req
) < sizeof(*hdr
))
1081 hdr
= wpabuf_head(req
);
1082 plen
= be_to_host16(hdr
->length
);
1083 if (plen
> wpabuf_len(req
)) {
1084 wpa_printf(MSG_DEBUG
, "EAP: Ignored truncated EAP-Packet "
1085 "(len=%lu plen=%lu)",
1086 (unsigned long) wpabuf_len(req
),
1087 (unsigned long) plen
);
1091 sm
->reqId
= hdr
->identifier
;
1093 if (sm
->workaround
) {
1095 addr
[0] = wpabuf_head(req
);
1096 md5_vector(1, addr
, &plen
, sm
->req_md5
);
1099 switch (hdr
->code
) {
1100 case EAP_CODE_REQUEST
:
1101 if (plen
< sizeof(*hdr
) + 1) {
1102 wpa_printf(MSG_DEBUG
, "EAP: Too short EAP-Request - "
1107 pos
= (const u8
*) (hdr
+ 1);
1108 sm
->reqMethod
= *pos
++;
1109 if (sm
->reqMethod
== EAP_TYPE_EXPANDED
) {
1110 if (plen
< sizeof(*hdr
) + 8) {
1111 wpa_printf(MSG_DEBUG
, "EAP: Ignored truncated "
1112 "expanded EAP-Packet (plen=%lu)",
1113 (unsigned long) plen
);
1116 sm
->reqVendor
= WPA_GET_BE24(pos
);
1118 sm
->reqVendorMethod
= WPA_GET_BE32(pos
);
1120 wpa_printf(MSG_DEBUG
, "EAP: Received EAP-Request id=%d "
1121 "method=%u vendor=%u vendorMethod=%u",
1122 sm
->reqId
, sm
->reqMethod
, sm
->reqVendor
,
1123 sm
->reqVendorMethod
);
1125 case EAP_CODE_RESPONSE
:
1126 if (sm
->selectedMethod
== EAP_TYPE_LEAP
) {
1128 * LEAP differs from RFC 4137 by using reversed roles
1129 * for mutual authentication and because of this, we
1130 * need to accept EAP-Response frames if LEAP is used.
1132 if (plen
< sizeof(*hdr
) + 1) {
1133 wpa_printf(MSG_DEBUG
, "EAP: Too short "
1134 "EAP-Response - no Type field");
1138 pos
= (const u8
*) (hdr
+ 1);
1139 sm
->reqMethod
= *pos
;
1140 wpa_printf(MSG_DEBUG
, "EAP: Received EAP-Response for "
1141 "LEAP method=%d id=%d",
1142 sm
->reqMethod
, sm
->reqId
);
1145 wpa_printf(MSG_DEBUG
, "EAP: Ignored EAP-Response");
1147 case EAP_CODE_SUCCESS
:
1148 wpa_printf(MSG_DEBUG
, "EAP: Received EAP-Success");
1149 sm
->rxSuccess
= TRUE
;
1151 case EAP_CODE_FAILURE
:
1152 wpa_printf(MSG_DEBUG
, "EAP: Received EAP-Failure");
1153 sm
->rxFailure
= TRUE
;
1156 wpa_printf(MSG_DEBUG
, "EAP: Ignored EAP-Packet with unknown "
1157 "code %d", hdr
->code
);
1163 static void eap_peer_sm_tls_event(void *ctx
, enum tls_event ev
,
1164 union tls_event_data
*data
)
1166 struct eap_sm
*sm
= ctx
;
1167 char *hash_hex
= NULL
;
1168 char *cert_hex
= NULL
;
1171 case TLS_CERT_CHAIN_FAILURE
:
1172 wpa_msg(sm
->msg_ctx
, MSG_INFO
, WPA_EVENT_EAP_TLS_CERT_ERROR
1173 "reason=%d depth=%d subject='%s' err='%s'",
1174 data
->cert_fail
.reason
,
1175 data
->cert_fail
.depth
,
1176 data
->cert_fail
.subject
,
1177 data
->cert_fail
.reason_txt
);
1179 case TLS_PEER_CERTIFICATE
:
1180 if (data
->peer_cert
.hash
) {
1181 size_t len
= data
->peer_cert
.hash_len
* 2 + 1;
1182 hash_hex
= os_malloc(len
);
1184 wpa_snprintf_hex(hash_hex
, len
,
1185 data
->peer_cert
.hash
,
1186 data
->peer_cert
.hash_len
);
1189 wpa_msg(sm
->msg_ctx
, MSG_INFO
, WPA_EVENT_EAP_PEER_CERT
1190 "depth=%d subject='%s'%s%s",
1191 data
->peer_cert
.depth
, data
->peer_cert
.subject
,
1192 hash_hex
? " hash=" : "", hash_hex
? hash_hex
: "");
1194 if (data
->peer_cert
.cert
) {
1195 size_t len
= wpabuf_len(data
->peer_cert
.cert
) * 2 + 1;
1196 cert_hex
= os_malloc(len
);
1197 if (cert_hex
== NULL
)
1199 wpa_snprintf_hex(cert_hex
, len
,
1200 wpabuf_head(data
->peer_cert
.cert
),
1201 wpabuf_len(data
->peer_cert
.cert
));
1202 wpa_msg_ctrl(sm
->msg_ctx
, MSG_INFO
,
1203 WPA_EVENT_EAP_PEER_CERT
1204 "depth=%d subject='%s' cert=%s",
1205 data
->peer_cert
.depth
,
1206 data
->peer_cert
.subject
,
1218 * eap_peer_sm_init - Allocate and initialize EAP peer state machine
1219 * @eapol_ctx: Context data to be used with eapol_cb calls
1220 * @eapol_cb: Pointer to EAPOL callback functions
1221 * @msg_ctx: Context data for wpa_msg() calls
1222 * @conf: EAP configuration
1223 * Returns: Pointer to the allocated EAP state machine or %NULL on failure
1225 * This function allocates and initializes an EAP state machine. In addition,
1226 * this initializes TLS library for the new EAP state machine. eapol_cb pointer
1227 * will be in use until eap_peer_sm_deinit() is used to deinitialize this EAP
1228 * state machine. Consequently, the caller must make sure that this data
1229 * structure remains alive while the EAP state machine is active.
1231 struct eap_sm
* eap_peer_sm_init(void *eapol_ctx
,
1232 struct eapol_callbacks
*eapol_cb
,
1233 void *msg_ctx
, struct eap_config
*conf
)
1236 struct tls_config tlsconf
;
1238 sm
= os_zalloc(sizeof(*sm
));
1241 sm
->eapol_ctx
= eapol_ctx
;
1242 sm
->eapol_cb
= eapol_cb
;
1243 sm
->msg_ctx
= msg_ctx
;
1244 sm
->ClientTimeout
= 60;
1245 sm
->wps
= conf
->wps
;
1247 os_memset(&tlsconf
, 0, sizeof(tlsconf
));
1248 tlsconf
.opensc_engine_path
= conf
->opensc_engine_path
;
1249 tlsconf
.pkcs11_engine_path
= conf
->pkcs11_engine_path
;
1250 tlsconf
.pkcs11_module_path
= conf
->pkcs11_module_path
;
1252 tlsconf
.fips_mode
= 1;
1253 #endif /* CONFIG_FIPS */
1254 tlsconf
.event_cb
= eap_peer_sm_tls_event
;
1255 tlsconf
.cb_ctx
= sm
;
1256 sm
->ssl_ctx
= tls_init(&tlsconf
);
1257 if (sm
->ssl_ctx
== NULL
) {
1258 wpa_printf(MSG_WARNING
, "SSL: Failed to initialize TLS "
1269 * eap_peer_sm_deinit - Deinitialize and free an EAP peer state machine
1270 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1272 * This function deinitializes EAP state machine and frees all allocated
1275 void eap_peer_sm_deinit(struct eap_sm
*sm
)
1279 eap_deinit_prev_method(sm
, "EAP deinit");
1281 tls_deinit(sm
->ssl_ctx
);
1287 * eap_peer_sm_step - Step EAP peer state machine
1288 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1289 * Returns: 1 if EAP state was changed or 0 if not
1291 * This function advances EAP state machine to a new state to match with the
1292 * current variables. This should be called whenever variables used by the EAP
1293 * state machine have changed.
1295 int eap_peer_sm_step(struct eap_sm
*sm
)
1299 sm
->changed
= FALSE
;
1303 } while (sm
->changed
);
1309 * eap_sm_abort - Abort EAP authentication
1310 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1312 * Release system resources that have been allocated for the authentication
1313 * session without fully deinitializing the EAP state machine.
1315 void eap_sm_abort(struct eap_sm
*sm
)
1317 wpabuf_free(sm
->lastRespData
);
1318 sm
->lastRespData
= NULL
;
1319 wpabuf_free(sm
->eapRespData
);
1320 sm
->eapRespData
= NULL
;
1321 os_free(sm
->eapKeyData
);
1322 sm
->eapKeyData
= NULL
;
1324 /* This is not clearly specified in the EAP statemachines draft, but
1325 * it seems necessary to make sure that some of the EAPOL variables get
1326 * cleared for the next authentication. */
1327 eapol_set_bool(sm
, EAPOL_eapSuccess
, FALSE
);
1331 #ifdef CONFIG_CTRL_IFACE
1332 static const char * eap_sm_state_txt(int state
)
1335 case EAP_INITIALIZE
:
1336 return "INITIALIZE";
1343 case EAP_GET_METHOD
:
1344 return "GET_METHOD";
1347 case EAP_SEND_RESPONSE
:
1348 return "SEND_RESPONSE";
1353 case EAP_NOTIFICATION
:
1354 return "NOTIFICATION";
1355 case EAP_RETRANSMIT
:
1356 return "RETRANSMIT";
1365 #endif /* CONFIG_CTRL_IFACE */
1368 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
1369 static const char * eap_sm_method_state_txt(EapMethodState state
)
1378 case METHOD_MAY_CONT
:
1388 static const char * eap_sm_decision_txt(EapDecision decision
)
1393 case DECISION_COND_SUCC
:
1395 case DECISION_UNCOND_SUCC
:
1396 return "UNCOND_SUCC";
1401 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1404 #ifdef CONFIG_CTRL_IFACE
1407 * eap_sm_get_status - Get EAP state machine status
1408 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1409 * @buf: Buffer for status information
1410 * @buflen: Maximum buffer length
1411 * @verbose: Whether to include verbose status information
1412 * Returns: Number of bytes written to buf.
1414 * Query EAP state machine for status information. This function fills in a
1415 * text area with current status information from the EAPOL state machine. If
1416 * the buffer (buf) is not large enough, status information will be truncated
1417 * to fit the buffer.
1419 int eap_sm_get_status(struct eap_sm
*sm
, char *buf
, size_t buflen
, int verbose
)
1426 len
= os_snprintf(buf
, buflen
,
1428 eap_sm_state_txt(sm
->EAP_state
));
1429 if (len
< 0 || (size_t) len
>= buflen
)
1432 if (sm
->selectedMethod
!= EAP_TYPE_NONE
) {
1437 const struct eap_method
*m
=
1438 eap_peer_get_eap_method(EAP_VENDOR_IETF
,
1439 sm
->selectedMethod
);
1445 ret
= os_snprintf(buf
+ len
, buflen
- len
,
1446 "selectedMethod=%d (EAP-%s)\n",
1447 sm
->selectedMethod
, name
);
1448 if (ret
< 0 || (size_t) ret
>= buflen
- len
)
1452 if (sm
->m
&& sm
->m
->get_status
) {
1453 len
+= sm
->m
->get_status(sm
, sm
->eap_method_priv
,
1454 buf
+ len
, buflen
- len
,
1460 ret
= os_snprintf(buf
+ len
, buflen
- len
,
1464 "ClientTimeout=%d\n",
1466 eap_sm_method_state_txt(sm
->methodState
),
1467 eap_sm_decision_txt(sm
->decision
),
1469 if (ret
< 0 || (size_t) ret
>= buflen
- len
)
1476 #endif /* CONFIG_CTRL_IFACE */
1479 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
1481 TYPE_IDENTITY
, TYPE_PASSWORD
, TYPE_OTP
, TYPE_PIN
, TYPE_NEW_PASSWORD
,
1483 } eap_ctrl_req_type
;
1485 static void eap_sm_request(struct eap_sm
*sm
, eap_ctrl_req_type type
,
1486 const char *msg
, size_t msglen
)
1488 struct eap_peer_config
*config
;
1489 char *field
, *txt
, *tmp
;
1493 config
= eap_get_config(sm
);
1501 config
->pending_req_identity
++;
1506 config
->pending_req_password
++;
1508 case TYPE_NEW_PASSWORD
:
1509 field
= "NEW_PASSWORD";
1510 txt
= "New Password";
1511 config
->pending_req_new_password
++;
1516 config
->pending_req_pin
++;
1521 tmp
= os_malloc(msglen
+ 3);
1525 os_memcpy(tmp
+ 1, msg
, msglen
);
1526 tmp
[msglen
+ 1] = ']';
1527 tmp
[msglen
+ 2] = '\0';
1529 os_free(config
->pending_req_otp
);
1530 config
->pending_req_otp
= tmp
;
1531 config
->pending_req_otp_len
= msglen
+ 3;
1533 if (config
->pending_req_otp
== NULL
)
1535 txt
= config
->pending_req_otp
;
1538 case TYPE_PASSPHRASE
:
1539 field
= "PASSPHRASE";
1540 txt
= "Private key passphrase";
1541 config
->pending_req_passphrase
++;
1547 if (sm
->eapol_cb
->eap_param_needed
)
1548 sm
->eapol_cb
->eap_param_needed(sm
->eapol_ctx
, field
, txt
);
1550 #else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1551 #define eap_sm_request(sm, type, msg, msglen) do { } while (0)
1552 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1556 * eap_sm_request_identity - Request identity from user (ctrl_iface)
1557 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1559 * EAP methods can call this function to request identity information for the
1560 * current network. This is normally called when the identity is not included
1561 * in the network configuration. The request will be sent to monitor programs
1562 * through the control interface.
1564 void eap_sm_request_identity(struct eap_sm
*sm
)
1566 eap_sm_request(sm
, TYPE_IDENTITY
, NULL
, 0);
1571 * eap_sm_request_password - Request password from user (ctrl_iface)
1572 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1574 * EAP methods can call this function to request password information for the
1575 * current network. This is normally called when the password is not included
1576 * in the network configuration. The request will be sent to monitor programs
1577 * through the control interface.
1579 void eap_sm_request_password(struct eap_sm
*sm
)
1581 eap_sm_request(sm
, TYPE_PASSWORD
, NULL
, 0);
1586 * eap_sm_request_new_password - Request new password from user (ctrl_iface)
1587 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1589 * EAP methods can call this function to request new password information for
1590 * the current network. This is normally called when the EAP method indicates
1591 * that the current password has expired and password change is required. The
1592 * request will be sent to monitor programs through the control interface.
1594 void eap_sm_request_new_password(struct eap_sm
*sm
)
1596 eap_sm_request(sm
, TYPE_NEW_PASSWORD
, NULL
, 0);
1601 * eap_sm_request_pin - Request SIM or smart card PIN from user (ctrl_iface)
1602 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1604 * EAP methods can call this function to request SIM or smart card PIN
1605 * information for the current network. This is normally called when the PIN is
1606 * not included in the network configuration. The request will be sent to
1607 * monitor programs through the control interface.
1609 void eap_sm_request_pin(struct eap_sm
*sm
)
1611 eap_sm_request(sm
, TYPE_PIN
, NULL
, 0);
1616 * eap_sm_request_otp - Request one time password from user (ctrl_iface)
1617 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1618 * @msg: Message to be displayed to the user when asking for OTP
1619 * @msg_len: Length of the user displayable message
1621 * EAP methods can call this function to request open time password (OTP) for
1622 * the current network. The request will be sent to monitor programs through
1623 * the control interface.
1625 void eap_sm_request_otp(struct eap_sm
*sm
, const char *msg
, size_t msg_len
)
1627 eap_sm_request(sm
, TYPE_OTP
, msg
, msg_len
);
1632 * eap_sm_request_passphrase - Request passphrase from user (ctrl_iface)
1633 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1635 * EAP methods can call this function to request passphrase for a private key
1636 * for the current network. This is normally called when the passphrase is not
1637 * included in the network configuration. The request will be sent to monitor
1638 * programs through the control interface.
1640 void eap_sm_request_passphrase(struct eap_sm
*sm
)
1642 eap_sm_request(sm
, TYPE_PASSPHRASE
, NULL
, 0);
1647 * eap_sm_notify_ctrl_attached - Notification of attached monitor
1648 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1650 * Notify EAP state machines that a monitor was attached to the control
1651 * interface to trigger re-sending of pending requests for user input.
1653 void eap_sm_notify_ctrl_attached(struct eap_sm
*sm
)
1655 struct eap_peer_config
*config
= eap_get_config(sm
);
1660 /* Re-send any pending requests for user data since a new control
1661 * interface was added. This handles cases where the EAP authentication
1662 * starts immediately after system startup when the user interface is
1663 * not yet running. */
1664 if (config
->pending_req_identity
)
1665 eap_sm_request_identity(sm
);
1666 if (config
->pending_req_password
)
1667 eap_sm_request_password(sm
);
1668 if (config
->pending_req_new_password
)
1669 eap_sm_request_new_password(sm
);
1670 if (config
->pending_req_otp
)
1671 eap_sm_request_otp(sm
, NULL
, 0);
1672 if (config
->pending_req_pin
)
1673 eap_sm_request_pin(sm
);
1674 if (config
->pending_req_passphrase
)
1675 eap_sm_request_passphrase(sm
);
1679 static int eap_allowed_phase2_type(int vendor
, int type
)
1681 if (vendor
!= EAP_VENDOR_IETF
)
1683 return type
!= EAP_TYPE_PEAP
&& type
!= EAP_TYPE_TTLS
&&
1684 type
!= EAP_TYPE_FAST
;
1689 * eap_get_phase2_type - Get EAP type for the given EAP phase 2 method name
1690 * @name: EAP method name, e.g., MD5
1691 * @vendor: Buffer for returning EAP Vendor-Id
1692 * Returns: EAP method type or %EAP_TYPE_NONE if not found
1694 * This function maps EAP type names into EAP type numbers that are allowed for
1695 * Phase 2, i.e., for tunneled authentication. Phase 2 is used, e.g., with
1696 * EAP-PEAP, EAP-TTLS, and EAP-FAST.
1698 u32
eap_get_phase2_type(const char *name
, int *vendor
)
1701 u8 type
= eap_peer_get_type(name
, &v
);
1702 if (eap_allowed_phase2_type(v
, type
)) {
1706 *vendor
= EAP_VENDOR_IETF
;
1707 return EAP_TYPE_NONE
;
1712 * eap_get_phase2_types - Get list of allowed EAP phase 2 types
1713 * @config: Pointer to a network configuration
1714 * @count: Pointer to a variable to be filled with number of returned EAP types
1715 * Returns: Pointer to allocated type list or %NULL on failure
1717 * This function generates an array of allowed EAP phase 2 (tunneled) types for
1718 * the given network configuration.
1720 struct eap_method_type
* eap_get_phase2_types(struct eap_peer_config
*config
,
1723 struct eap_method_type
*buf
;
1727 const struct eap_method
*methods
, *m
;
1729 methods
= eap_peer_get_methods(&mcount
);
1730 if (methods
== NULL
)
1733 buf
= os_malloc(mcount
* sizeof(struct eap_method_type
));
1737 for (m
= methods
; m
; m
= m
->next
) {
1740 if (eap_allowed_phase2_type(vendor
, method
)) {
1741 if (vendor
== EAP_VENDOR_IETF
&&
1742 method
== EAP_TYPE_TLS
&& config
&&
1743 config
->private_key2
== NULL
)
1745 buf
[*count
].vendor
= vendor
;
1746 buf
[*count
].method
= method
;
1756 * eap_set_fast_reauth - Update fast_reauth setting
1757 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1758 * @enabled: 1 = Fast reauthentication is enabled, 0 = Disabled
1760 void eap_set_fast_reauth(struct eap_sm
*sm
, int enabled
)
1762 sm
->fast_reauth
= enabled
;
1767 * eap_set_workaround - Update EAP workarounds setting
1768 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1769 * @workaround: 1 = Enable EAP workarounds, 0 = Disable EAP workarounds
1771 void eap_set_workaround(struct eap_sm
*sm
, unsigned int workaround
)
1773 sm
->workaround
= workaround
;
1778 * eap_get_config - Get current network configuration
1779 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1780 * Returns: Pointer to the current network configuration or %NULL if not found
1782 * EAP peer methods should avoid using this function if they can use other
1783 * access functions, like eap_get_config_identity() and
1784 * eap_get_config_password(), that do not require direct access to
1785 * struct eap_peer_config.
1787 struct eap_peer_config
* eap_get_config(struct eap_sm
*sm
)
1789 return sm
->eapol_cb
->get_config(sm
->eapol_ctx
);
1794 * eap_get_config_identity - Get identity from the network configuration
1795 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1796 * @len: Buffer for the length of the identity
1797 * Returns: Pointer to the identity or %NULL if not found
1799 const u8
* eap_get_config_identity(struct eap_sm
*sm
, size_t *len
)
1801 struct eap_peer_config
*config
= eap_get_config(sm
);
1804 *len
= config
->identity_len
;
1805 return config
->identity
;
1810 * eap_get_config_password - Get password from the network configuration
1811 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1812 * @len: Buffer for the length of the password
1813 * Returns: Pointer to the password or %NULL if not found
1815 const u8
* eap_get_config_password(struct eap_sm
*sm
, size_t *len
)
1817 struct eap_peer_config
*config
= eap_get_config(sm
);
1820 *len
= config
->password_len
;
1821 return config
->password
;
1826 * eap_get_config_password2 - Get password from the network configuration
1827 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1828 * @len: Buffer for the length of the password
1829 * @hash: Buffer for returning whether the password is stored as a
1830 * NtPasswordHash instead of plaintext password; can be %NULL if this
1831 * information is not needed
1832 * Returns: Pointer to the password or %NULL if not found
1834 const u8
* eap_get_config_password2(struct eap_sm
*sm
, size_t *len
, int *hash
)
1836 struct eap_peer_config
*config
= eap_get_config(sm
);
1839 *len
= config
->password_len
;
1841 *hash
= !!(config
->flags
& EAP_CONFIG_FLAGS_PASSWORD_NTHASH
);
1842 return config
->password
;
1847 * eap_get_config_new_password - Get new password from network configuration
1848 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1849 * @len: Buffer for the length of the new password
1850 * Returns: Pointer to the new password or %NULL if not found
1852 const u8
* eap_get_config_new_password(struct eap_sm
*sm
, size_t *len
)
1854 struct eap_peer_config
*config
= eap_get_config(sm
);
1857 *len
= config
->new_password_len
;
1858 return config
->new_password
;
1863 * eap_get_config_otp - Get one-time password from the network configuration
1864 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1865 * @len: Buffer for the length of the one-time password
1866 * Returns: Pointer to the one-time password or %NULL if not found
1868 const u8
* eap_get_config_otp(struct eap_sm
*sm
, size_t *len
)
1870 struct eap_peer_config
*config
= eap_get_config(sm
);
1873 *len
= config
->otp_len
;
1879 * eap_clear_config_otp - Clear used one-time password
1880 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1882 * This function clears a used one-time password (OTP) from the current network
1883 * configuration. This should be called when the OTP has been used and is not
1886 void eap_clear_config_otp(struct eap_sm
*sm
)
1888 struct eap_peer_config
*config
= eap_get_config(sm
);
1891 os_memset(config
->otp
, 0, config
->otp_len
);
1892 os_free(config
->otp
);
1894 config
->otp_len
= 0;
1899 * eap_get_config_phase1 - Get phase1 data from the network configuration
1900 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1901 * Returns: Pointer to the phase1 data or %NULL if not found
1903 const char * eap_get_config_phase1(struct eap_sm
*sm
)
1905 struct eap_peer_config
*config
= eap_get_config(sm
);
1908 return config
->phase1
;
1913 * eap_get_config_phase2 - Get phase2 data from the network configuration
1914 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1915 * Returns: Pointer to the phase1 data or %NULL if not found
1917 const char * eap_get_config_phase2(struct eap_sm
*sm
)
1919 struct eap_peer_config
*config
= eap_get_config(sm
);
1922 return config
->phase2
;
1927 * eap_key_available - Get key availability (eapKeyAvailable variable)
1928 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1929 * Returns: 1 if EAP keying material is available, 0 if not
1931 int eap_key_available(struct eap_sm
*sm
)
1933 return sm
? sm
->eapKeyAvailable
: 0;
1938 * eap_notify_success - Notify EAP state machine about external success trigger
1939 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1941 * This function is called when external event, e.g., successful completion of
1942 * WPA-PSK key handshake, is indicating that EAP state machine should move to
1943 * success state. This is mainly used with security modes that do not use EAP
1944 * state machine (e.g., WPA-PSK).
1946 void eap_notify_success(struct eap_sm
*sm
)
1949 sm
->decision
= DECISION_COND_SUCC
;
1950 sm
->EAP_state
= EAP_SUCCESS
;
1956 * eap_notify_lower_layer_success - Notification of lower layer success
1957 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1959 * Notify EAP state machines that a lower layer has detected a successful
1960 * authentication. This is used to recover from dropped EAP-Success messages.
1962 void eap_notify_lower_layer_success(struct eap_sm
*sm
)
1967 if (eapol_get_bool(sm
, EAPOL_eapSuccess
) ||
1968 sm
->decision
== DECISION_FAIL
||
1969 (sm
->methodState
!= METHOD_MAY_CONT
&&
1970 sm
->methodState
!= METHOD_DONE
))
1973 if (sm
->eapKeyData
!= NULL
)
1974 sm
->eapKeyAvailable
= TRUE
;
1975 eapol_set_bool(sm
, EAPOL_eapSuccess
, TRUE
);
1976 wpa_msg(sm
->msg_ctx
, MSG_INFO
, WPA_EVENT_EAP_SUCCESS
1977 "EAP authentication completed successfully (based on lower "
1983 * eap_get_eapKeyData - Get master session key (MSK) from EAP state machine
1984 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1985 * @len: Pointer to variable that will be set to number of bytes in the key
1986 * Returns: Pointer to the EAP keying data or %NULL on failure
1988 * Fetch EAP keying material (MSK, eapKeyData) from the EAP state machine. The
1989 * key is available only after a successful authentication. EAP state machine
1990 * continues to manage the key data and the caller must not change or free the
1993 const u8
* eap_get_eapKeyData(struct eap_sm
*sm
, size_t *len
)
1995 if (sm
== NULL
|| sm
->eapKeyData
== NULL
) {
2000 *len
= sm
->eapKeyDataLen
;
2001 return sm
->eapKeyData
;
2006 * eap_get_eapKeyData - Get EAP response data
2007 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2008 * Returns: Pointer to the EAP response (eapRespData) or %NULL on failure
2010 * Fetch EAP response (eapRespData) from the EAP state machine. This data is
2011 * available when EAP state machine has processed an incoming EAP request. The
2012 * EAP state machine does not maintain a reference to the response after this
2013 * function is called and the caller is responsible for freeing the data.
2015 struct wpabuf
* eap_get_eapRespData(struct eap_sm
*sm
)
2017 struct wpabuf
*resp
;
2019 if (sm
== NULL
|| sm
->eapRespData
== NULL
)
2022 resp
= sm
->eapRespData
;
2023 sm
->eapRespData
= NULL
;
2030 * eap_sm_register_scard_ctx - Notification of smart card context
2031 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2032 * @ctx: Context data for smart card operations
2034 * Notify EAP state machines of context data for smart card operations. This
2035 * context data will be used as a parameter for scard_*() functions.
2037 void eap_register_scard_ctx(struct eap_sm
*sm
, void *ctx
)
2040 sm
->scard_ctx
= ctx
;
2045 * eap_set_config_blob - Set or add a named configuration blob
2046 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2047 * @blob: New value for the blob
2049 * Adds a new configuration blob or replaces the current value of an existing
2052 void eap_set_config_blob(struct eap_sm
*sm
, struct wpa_config_blob
*blob
)
2054 #ifndef CONFIG_NO_CONFIG_BLOBS
2055 sm
->eapol_cb
->set_config_blob(sm
->eapol_ctx
, blob
);
2056 #endif /* CONFIG_NO_CONFIG_BLOBS */
2061 * eap_get_config_blob - Get a named configuration blob
2062 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2063 * @name: Name of the blob
2064 * Returns: Pointer to blob data or %NULL if not found
2066 const struct wpa_config_blob
* eap_get_config_blob(struct eap_sm
*sm
,
2069 #ifndef CONFIG_NO_CONFIG_BLOBS
2070 return sm
->eapol_cb
->get_config_blob(sm
->eapol_ctx
, name
);
2071 #else /* CONFIG_NO_CONFIG_BLOBS */
2073 #endif /* CONFIG_NO_CONFIG_BLOBS */
2078 * eap_set_force_disabled - Set force_disabled flag
2079 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2080 * @disabled: 1 = EAP disabled, 0 = EAP enabled
2082 * This function is used to force EAP state machine to be disabled when it is
2083 * not in use (e.g., with WPA-PSK or plaintext connections).
2085 void eap_set_force_disabled(struct eap_sm
*sm
, int disabled
)
2087 sm
->force_disabled
= disabled
;
2092 * eap_notify_pending - Notify that EAP method is ready to re-process a request
2093 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2095 * An EAP method can perform a pending operation (e.g., to get a response from
2096 * an external process). Once the response is available, this function can be
2097 * used to request EAPOL state machine to retry delivering the previously
2098 * received (and still unanswered) EAP request to EAP state machine.
2100 void eap_notify_pending(struct eap_sm
*sm
)
2102 sm
->eapol_cb
->notify_pending(sm
->eapol_ctx
);
2107 * eap_invalidate_cached_session - Mark cached session data invalid
2108 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2110 void eap_invalidate_cached_session(struct eap_sm
*sm
)
2113 eap_deinit_prev_method(sm
, "invalidate");
2117 int eap_is_wps_pbc_enrollee(struct eap_peer_config
*conf
)
2119 if (conf
->identity_len
!= WSC_ID_ENROLLEE_LEN
||
2120 os_memcmp(conf
->identity
, WSC_ID_ENROLLEE
, WSC_ID_ENROLLEE_LEN
))
2121 return 0; /* Not a WPS Enrollee */
2123 if (conf
->phase1
== NULL
|| os_strstr(conf
->phase1
, "pbc=1") == NULL
)
2124 return 0; /* Not using PBC */
2130 int eap_is_wps_pin_enrollee(struct eap_peer_config
*conf
)
2132 if (conf
->identity_len
!= WSC_ID_ENROLLEE_LEN
||
2133 os_memcmp(conf
->identity
, WSC_ID_ENROLLEE
, WSC_ID_ENROLLEE_LEN
))
2134 return 0; /* Not a WPS Enrollee */
2136 if (conf
->phase1
== NULL
|| os_strstr(conf
->phase1
, "pin=") == NULL
)
2137 return 0; /* Not using PIN */