2 * EAP peer state machines (RFC 4137)
3 * Copyright (c) 2004-2014, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
8 * This file implements the Peer State Machine as defined in RFC 4137. The used
9 * states and state transitions match mostly with the RFC. However, there are
10 * couple of additional transitions for working around small issues noticed
11 * during testing. These exceptions are explained in comments within the
12 * functions in this file. The method functions, m.func(), are similar to the
13 * ones used in RFC 4137, but some small changes have used here to optimize
14 * operations and to add functionality needed for fast re-authentication
15 * (session resumption).
21 #include "pcsc_funcs.h"
22 #include "state_machine.h"
23 #include "ext_password.h"
24 #include "crypto/crypto.h"
25 #include "crypto/tls.h"
26 #include "crypto/sha256.h"
27 #include "common/wpa_ctrl.h"
28 #include "eap_common/eap_wsc_common.h"
30 #include "eap_config.h"
32 #define STATE_MACHINE_DATA struct eap_sm
33 #define STATE_MACHINE_DEBUG_PREFIX "EAP"
35 #define EAP_MAX_AUTH_ROUNDS 100
36 #define EAP_MAX_AUTH_ROUNDS_SHORT 50
37 #define EAP_CLIENT_TIMEOUT_DEFAULT 60
40 static Boolean
eap_sm_allowMethod(struct eap_sm
*sm
, int vendor
,
41 enum eap_type method
);
42 static struct wpabuf
* eap_sm_buildNak(struct eap_sm
*sm
, int id
);
43 static void eap_sm_processIdentity(struct eap_sm
*sm
,
44 const struct wpabuf
*req
);
45 static void eap_sm_processNotify(struct eap_sm
*sm
, const struct wpabuf
*req
);
46 static struct wpabuf
* eap_sm_buildNotify(int id
);
47 static void eap_sm_parseEapReq(struct eap_sm
*sm
, const struct wpabuf
*req
);
48 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
49 static const char * eap_sm_method_state_txt(EapMethodState state
);
50 static const char * eap_sm_decision_txt(EapDecision decision
);
51 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
52 static void eap_sm_request(struct eap_sm
*sm
, enum wpa_ctrl_req_type field
,
53 const char *msg
, size_t msglen
);
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_notify_status(struct eap_sm
*sm
, const char *status
,
90 const char *parameter
)
92 wpa_printf(MSG_DEBUG
, "EAP: Status notification: %s (param=%s)",
94 if (sm
->eapol_cb
->notify_status
)
95 sm
->eapol_cb
->notify_status(sm
->eapol_ctx
, status
, parameter
);
99 static void eap_report_error(struct eap_sm
*sm
, int error_code
)
101 wpa_printf(MSG_DEBUG
, "EAP: Error notification: %d", error_code
);
102 if (sm
->eapol_cb
->notify_eap_error
)
103 sm
->eapol_cb
->notify_eap_error(sm
->eapol_ctx
, error_code
);
107 static void eap_sm_free_key(struct eap_sm
*sm
)
109 if (sm
->eapKeyData
) {
110 bin_clear_free(sm
->eapKeyData
, sm
->eapKeyDataLen
);
111 sm
->eapKeyData
= NULL
;
116 static void eap_deinit_prev_method(struct eap_sm
*sm
, const char *txt
)
118 ext_password_free(sm
->ext_pw_buf
);
119 sm
->ext_pw_buf
= NULL
;
121 if (sm
->m
== NULL
|| sm
->eap_method_priv
== NULL
)
124 wpa_printf(MSG_DEBUG
, "EAP: deinitialize previously used EAP method "
125 "(%d, %s) at %s", sm
->selectedMethod
, sm
->m
->name
, txt
);
126 sm
->m
->deinit(sm
, sm
->eap_method_priv
);
127 sm
->eap_method_priv
= NULL
;
133 * eap_config_allowed_method - Check whether EAP method is allowed
134 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
135 * @config: EAP configuration
136 * @vendor: Vendor-Id for expanded types or 0 = IETF for legacy types
138 * Returns: 1 = allowed EAP method, 0 = not allowed
140 static int eap_config_allowed_method(struct eap_sm
*sm
,
141 struct eap_peer_config
*config
,
142 int vendor
, u32 method
)
145 struct eap_method_type
*m
;
147 if (config
== NULL
|| config
->eap_methods
== NULL
)
150 m
= config
->eap_methods
;
151 for (i
= 0; m
[i
].vendor
!= EAP_VENDOR_IETF
||
152 m
[i
].method
!= EAP_TYPE_NONE
; i
++) {
153 if (m
[i
].vendor
== vendor
&& m
[i
].method
== method
)
161 * eap_allowed_method - Check whether EAP method is allowed
162 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
163 * @vendor: Vendor-Id for expanded types or 0 = IETF for legacy types
165 * Returns: 1 = allowed EAP method, 0 = not allowed
167 int eap_allowed_method(struct eap_sm
*sm
, int vendor
, u32 method
)
169 return eap_config_allowed_method(sm
, eap_get_config(sm
), vendor
,
174 #if defined(PCSC_FUNCS) || defined(CONFIG_EAP_PROXY)
175 static int eap_sm_append_3gpp_realm(struct eap_sm
*sm
, char *imsi
,
176 size_t max_len
, size_t *imsi_len
,
181 if (*imsi_len
+ 36 > max_len
) {
182 wpa_printf(MSG_WARNING
, "No room for realm in IMSI buffer");
186 if (mnc_len
!= 2 && mnc_len
!= 3)
193 } else if (mnc_len
== 3) {
200 pos
= imsi
+ *imsi_len
;
201 pos
+= os_snprintf(pos
, imsi
+ max_len
- pos
,
202 "@wlan.mnc%s.mcc%c%c%c.3gppnetwork.org",
203 mnc
, imsi
[0], imsi
[1], imsi
[2]);
204 *imsi_len
= pos
- imsi
;
208 #endif /* PCSC_FUNCS || CONFIG_EAP_PROXY */
212 * This state initializes state machine variables when the machine is
213 * activated (portEnabled = TRUE). This is also used when re-starting
214 * authentication (eapRestart == TRUE).
216 SM_STATE(EAP
, INITIALIZE
)
218 SM_ENTRY(EAP
, INITIALIZE
);
219 if (sm
->fast_reauth
&& sm
->m
&& sm
->m
->has_reauth_data
&&
220 sm
->m
->has_reauth_data(sm
, sm
->eap_method_priv
) &&
222 sm
->last_config
== eap_get_config(sm
)) {
223 wpa_printf(MSG_DEBUG
, "EAP: maintaining EAP method data for "
224 "fast reauthentication");
225 sm
->m
->deinit_for_reauth(sm
, sm
->eap_method_priv
);
227 sm
->last_config
= eap_get_config(sm
);
228 eap_deinit_prev_method(sm
, "INITIALIZE");
230 sm
->selectedMethod
= EAP_TYPE_NONE
;
231 sm
->methodState
= METHOD_NONE
;
232 sm
->allowNotifications
= TRUE
;
233 sm
->decision
= DECISION_FAIL
;
234 sm
->ClientTimeout
= EAP_CLIENT_TIMEOUT_DEFAULT
;
235 eapol_set_int(sm
, EAPOL_idleWhile
, sm
->ClientTimeout
);
236 eapol_set_bool(sm
, EAPOL_eapSuccess
, FALSE
);
237 eapol_set_bool(sm
, EAPOL_eapFail
, FALSE
);
239 os_free(sm
->eapSessionId
);
240 sm
->eapSessionId
= NULL
;
241 sm
->eapKeyAvailable
= FALSE
;
242 eapol_set_bool(sm
, EAPOL_eapRestart
, FALSE
);
243 sm
->lastId
= -1; /* new session - make sure this does not match with
244 * the first EAP-Packet */
246 * RFC 4137 does not reset eapResp and eapNoResp here. However, this
247 * seemed to be able to trigger cases where both were set and if EAPOL
248 * state machine uses eapNoResp first, it may end up not sending a real
249 * reply correctly. This occurred when the workaround in FAIL state set
250 * eapNoResp = TRUE.. Maybe that workaround needs to be fixed to do
253 eapol_set_bool(sm
, EAPOL_eapResp
, FALSE
);
254 eapol_set_bool(sm
, EAPOL_eapNoResp
, FALSE
);
256 * RFC 4137 does not reset ignore here, but since it is possible for
257 * some method code paths to end up not setting ignore=FALSE, clear the
258 * value here to avoid issues if a previous authentication attempt
259 * failed with ignore=TRUE being left behind in the last
260 * m.check(eapReqData) operation.
264 sm
->num_rounds_short
= 0;
265 sm
->prev_failure
= 0;
266 sm
->expected_failure
= 0;
267 sm
->reauthInit
= FALSE
;
268 sm
->erp_seq
= (u32
) -1;
273 * This state is reached whenever service from the lower layer is interrupted
274 * or unavailable (portEnabled == FALSE). Immediate transition to INITIALIZE
275 * occurs when the port becomes enabled.
277 SM_STATE(EAP
, DISABLED
)
279 SM_ENTRY(EAP
, DISABLED
);
281 sm
->num_rounds_short
= 0;
283 * RFC 4137 does not describe clearing of idleWhile here, but doing so
284 * allows the timer tick to be stopped more quickly when EAP is not in
287 eapol_set_int(sm
, EAPOL_idleWhile
, 0);
292 * The state machine spends most of its time here, waiting for something to
293 * happen. This state is entered unconditionally from INITIALIZE, DISCARD, and
294 * SEND_RESPONSE states.
303 * This state is entered when an EAP packet is received (eapReq == TRUE) to
304 * parse the packet header.
306 SM_STATE(EAP
, RECEIVED
)
308 const struct wpabuf
*eapReqData
;
310 SM_ENTRY(EAP
, RECEIVED
);
311 eapReqData
= eapol_get_eapReqData(sm
);
312 /* parse rxReq, rxSuccess, rxFailure, reqId, reqMethod */
313 eap_sm_parseEapReq(sm
, eapReqData
);
315 if (!eapReqData
|| wpabuf_len(eapReqData
) < 20)
316 sm
->num_rounds_short
++;
318 sm
->num_rounds_short
= 0;
323 * This state is entered when a request for a new type comes in. Either the
324 * correct method is started, or a Nak response is built.
326 SM_STATE(EAP
, GET_METHOD
)
329 enum eap_type method
;
330 const struct eap_method
*eap_method
;
332 SM_ENTRY(EAP
, GET_METHOD
);
334 if (sm
->reqMethod
== EAP_TYPE_EXPANDED
)
335 method
= sm
->reqVendorMethod
;
337 method
= sm
->reqMethod
;
339 eap_method
= eap_peer_get_eap_method(sm
->reqVendor
, method
);
341 if (!eap_sm_allowMethod(sm
, sm
->reqVendor
, method
)) {
342 wpa_printf(MSG_DEBUG
, "EAP: vendor %u method %u not allowed",
343 sm
->reqVendor
, method
);
344 wpa_msg(sm
->msg_ctx
, MSG_INFO
, WPA_EVENT_EAP_PROPOSED_METHOD
345 "vendor=%u method=%u -> NAK",
346 sm
->reqVendor
, method
);
347 eap_notify_status(sm
, "refuse proposed method",
348 eap_method
? eap_method
->name
: "unknown");
352 wpa_msg(sm
->msg_ctx
, MSG_INFO
, WPA_EVENT_EAP_PROPOSED_METHOD
353 "vendor=%u method=%u", sm
->reqVendor
, method
);
355 eap_notify_status(sm
, "accept proposed method",
356 eap_method
? eap_method
->name
: "unknown");
358 * RFC 4137 does not define specific operation for fast
359 * re-authentication (session resumption). The design here is to allow
360 * the previously used method data to be maintained for
361 * re-authentication if the method support session resumption.
362 * Otherwise, the previously used method data is freed and a new method
365 if (sm
->fast_reauth
&&
366 sm
->m
&& sm
->m
->vendor
== sm
->reqVendor
&&
367 sm
->m
->method
== method
&&
368 sm
->m
->has_reauth_data
&&
369 sm
->m
->has_reauth_data(sm
, sm
->eap_method_priv
)) {
370 wpa_printf(MSG_DEBUG
, "EAP: Using previous method data"
371 " for fast re-authentication");
374 eap_deinit_prev_method(sm
, "GET_METHOD");
378 sm
->selectedMethod
= sm
->reqMethod
;
382 wpa_printf(MSG_DEBUG
, "EAP: Could not find selected method: "
383 "vendor %d method %d",
384 sm
->reqVendor
, method
);
388 sm
->ClientTimeout
= EAP_CLIENT_TIMEOUT_DEFAULT
;
390 wpa_printf(MSG_DEBUG
, "EAP: Initialize selected EAP method: "
391 "vendor %u method %u (%s)",
392 sm
->reqVendor
, method
, sm
->m
->name
);
394 sm
->eap_method_priv
= sm
->m
->init_for_reauth(
395 sm
, sm
->eap_method_priv
);
397 sm
->waiting_ext_cert_check
= 0;
398 sm
->ext_cert_check
= 0;
399 sm
->eap_method_priv
= sm
->m
->init(sm
);
402 if (sm
->eap_method_priv
== NULL
) {
403 struct eap_peer_config
*config
= eap_get_config(sm
);
404 wpa_msg(sm
->msg_ctx
, MSG_INFO
,
405 "EAP: Failed to initialize EAP method: vendor %u "
407 sm
->reqVendor
, method
, sm
->m
->name
);
409 sm
->methodState
= METHOD_NONE
;
410 sm
->selectedMethod
= EAP_TYPE_NONE
;
411 if (sm
->reqMethod
== EAP_TYPE_TLS
&& config
&&
412 (config
->pending_req_pin
||
413 config
->pending_req_passphrase
)) {
415 * Return without generating Nak in order to allow
416 * entering of PIN code or passphrase to retry the
417 * current EAP packet.
419 wpa_printf(MSG_DEBUG
, "EAP: Pending PIN/passphrase "
420 "request - skip Nak");
427 sm
->methodState
= METHOD_INIT
;
428 wpa_msg(sm
->msg_ctx
, MSG_INFO
, WPA_EVENT_EAP_METHOD
429 "EAP vendor %u method %u (%s) selected",
430 sm
->reqVendor
, method
, sm
->m
->name
);
434 wpabuf_free(sm
->eapRespData
);
435 sm
->eapRespData
= NULL
;
436 sm
->eapRespData
= eap_sm_buildNak(sm
, sm
->reqId
);
442 static char * eap_get_realm(struct eap_sm
*sm
, struct eap_peer_config
*config
)
450 if (config
->identity
) {
451 for (i
= 0; i
< config
->identity_len
; i
++) {
452 if (config
->identity
[i
] == '@')
455 if (i
< config
->identity_len
) {
456 realm_len
= config
->identity_len
- i
- 1;
457 realm
= os_malloc(realm_len
+ 1);
460 os_memcpy(realm
, &config
->identity
[i
+ 1], realm_len
);
461 realm
[realm_len
] = '\0';
466 if (config
->anonymous_identity
) {
467 for (i
= 0; i
< config
->anonymous_identity_len
; i
++) {
468 if (config
->anonymous_identity
[i
] == '@')
471 if (i
< config
->anonymous_identity_len
) {
472 realm_len
= config
->anonymous_identity_len
- i
- 1;
473 realm
= os_malloc(realm_len
+ 1);
476 os_memcpy(realm
, &config
->anonymous_identity
[i
+ 1],
478 realm
[realm_len
] = '\0';
483 #ifdef CONFIG_EAP_PROXY
484 /* When identity is not provided in the config, build the realm from
485 * IMSI for eap_proxy based methods.
487 if (!config
->identity
&& !config
->anonymous_identity
&&
488 sm
->eapol_cb
->get_imsi
&&
489 (eap_config_allowed_method(sm
, config
, EAP_VENDOR_IETF
,
491 eap_config_allowed_method(sm
, config
, EAP_VENDOR_IETF
,
493 eap_config_allowed_method(sm
, config
, EAP_VENDOR_IETF
,
494 EAP_TYPE_AKA_PRIME
))) {
499 wpa_printf(MSG_DEBUG
, "EAP: Build realm from IMSI (eap_proxy)");
500 mnc_len
= sm
->eapol_cb
->get_imsi(sm
->eapol_ctx
, config
->sim_num
,
505 pos
= imsi_len
+ 1; /* points to the beginning of the realm */
506 if (eap_sm_append_3gpp_realm(sm
, imsi
, sizeof(imsi
), &imsi_len
,
508 wpa_printf(MSG_WARNING
, "Could not append realm");
512 realm
= os_strdup(&imsi
[pos
]);
516 wpa_printf(MSG_DEBUG
, "EAP: Generated realm '%s'", realm
);
519 #endif /* CONFIG_EAP_PROXY */
525 static char * eap_home_realm(struct eap_sm
*sm
)
527 return eap_get_realm(sm
, eap_get_config(sm
));
531 static struct eap_erp_key
*
532 eap_erp_get_key(struct eap_sm
*sm
, const char *realm
)
534 struct eap_erp_key
*erp
;
536 dl_list_for_each(erp
, &sm
->erp_keys
, struct eap_erp_key
, list
) {
539 pos
= os_strchr(erp
->keyname_nai
, '@');
543 if (os_strcmp(pos
, realm
) == 0)
551 static struct eap_erp_key
*
552 eap_erp_get_key_nai(struct eap_sm
*sm
, const char *nai
)
554 struct eap_erp_key
*erp
;
556 dl_list_for_each(erp
, &sm
->erp_keys
, struct eap_erp_key
, list
) {
557 if (os_strcmp(erp
->keyname_nai
, nai
) == 0)
565 static void eap_peer_erp_free_key(struct eap_erp_key
*erp
)
567 dl_list_del(&erp
->list
);
568 bin_clear_free(erp
, sizeof(*erp
));
572 static void eap_erp_remove_keys_realm(struct eap_sm
*sm
, const char *realm
)
574 struct eap_erp_key
*erp
;
576 while ((erp
= eap_erp_get_key(sm
, realm
)) != NULL
) {
577 wpa_printf(MSG_DEBUG
, "EAP: Delete old ERP key %s",
579 eap_peer_erp_free_key(erp
);
584 int eap_peer_update_erp_next_seq_num(struct eap_sm
*sm
, u16 next_seq_num
)
586 struct eap_erp_key
*erp
;
589 home_realm
= eap_home_realm(sm
);
590 if (!home_realm
|| os_strlen(home_realm
) == 0) {
595 erp
= eap_erp_get_key(sm
, home_realm
);
597 wpa_printf(MSG_DEBUG
,
598 "EAP: Failed to find ERP key for realm: %s",
604 if ((u32
) next_seq_num
< erp
->next_seq
) {
605 /* Sequence number has wrapped around, clear this ERP
606 * info and do a full auth next time.
608 eap_peer_erp_free_key(erp
);
610 erp
->next_seq
= (u32
) next_seq_num
;
618 int eap_peer_get_erp_info(struct eap_sm
*sm
, struct eap_peer_config
*config
,
619 const u8
**username
, size_t *username_len
,
620 const u8
**realm
, size_t *realm_len
,
621 u16
*erp_next_seq_num
, const u8
**rrk
,
624 struct eap_erp_key
*erp
;
629 home_realm
= eap_get_realm(sm
, config
);
631 home_realm
= eap_home_realm(sm
);
632 if (!home_realm
|| os_strlen(home_realm
) == 0) {
637 erp
= eap_erp_get_key(sm
, home_realm
);
642 if (erp
->next_seq
>= 65536)
643 return -1; /* SEQ has range of 0..65535 */
645 pos
= os_strchr(erp
->keyname_nai
, '@');
647 return -1; /* this cannot really happen */
648 *username_len
= pos
- erp
->keyname_nai
;
649 *username
= (u8
*) erp
->keyname_nai
;
652 *realm_len
= os_strlen(pos
);
655 *erp_next_seq_num
= (u16
) erp
->next_seq
;
657 *rrk_len
= erp
->rRK_len
;
660 if (*username_len
== 0 || *realm_len
== 0 || *rrk_len
== 0)
666 #endif /* CONFIG_ERP */
669 void eap_peer_erp_free_keys(struct eap_sm
*sm
)
672 struct eap_erp_key
*erp
, *tmp
;
674 dl_list_for_each_safe(erp
, tmp
, &sm
->erp_keys
, struct eap_erp_key
, list
)
675 eap_peer_erp_free_key(erp
);
676 #endif /* CONFIG_ERP */
680 /* Note: If ext_session and/or ext_emsk are passed to this function, they are
681 * expected to point to allocated memory and those allocations will be freed
682 * unconditionally. */
683 void eap_peer_erp_init(struct eap_sm
*sm
, u8
*ext_session_id
,
684 size_t ext_session_id_len
, u8
*ext_emsk
,
690 u8
*session_id
= NULL
;
691 size_t session_id_len
= 0;
692 u8 EMSKname
[EAP_EMSK_NAME_LEN
];
695 size_t realm_len
, nai_buf_len
;
696 struct eap_erp_key
*erp
= NULL
;
699 realm
= eap_home_realm(sm
);
702 realm_len
= os_strlen(realm
);
703 wpa_printf(MSG_DEBUG
, "EAP: Realm for ERP keyName-NAI: %s", realm
);
704 eap_erp_remove_keys_realm(sm
, realm
);
706 nai_buf_len
= 2 * EAP_EMSK_NAME_LEN
+ 1 + realm_len
;
707 if (nai_buf_len
> 253) {
709 * keyName-NAI has a maximum length of 253 octet to fit in
712 wpa_printf(MSG_DEBUG
,
713 "EAP: Too long realm for ERP keyName-NAI maximum length");
716 nai_buf_len
++; /* null termination */
717 erp
= os_zalloc(sizeof(*erp
) + nai_buf_len
);
723 emsk_len
= ext_emsk_len
;
725 emsk
= sm
->m
->get_emsk(sm
, sm
->eap_method_priv
, &emsk_len
);
728 if (!emsk
|| emsk_len
== 0 || emsk_len
> ERP_MAX_KEY_LEN
) {
729 wpa_printf(MSG_DEBUG
,
730 "EAP: No suitable EMSK available for ERP");
734 wpa_hexdump_key(MSG_DEBUG
, "EAP: EMSK", emsk
, emsk_len
);
736 if (ext_session_id
) {
737 session_id
= ext_session_id
;
738 session_id_len
= ext_session_id_len
;
740 session_id
= sm
->eapSessionId
;
741 session_id_len
= sm
->eapSessionIdLen
;
744 if (!session_id
|| session_id_len
== 0) {
745 wpa_printf(MSG_DEBUG
,
746 "EAP: No suitable session id available for ERP");
750 WPA_PUT_BE16(len
, EAP_EMSK_NAME_LEN
);
751 if (hmac_sha256_kdf(session_id
, session_id_len
, "EMSK", len
,
752 sizeof(len
), EMSKname
, EAP_EMSK_NAME_LEN
) < 0) {
753 wpa_printf(MSG_DEBUG
, "EAP: Could not derive EMSKname");
756 wpa_hexdump(MSG_DEBUG
, "EAP: EMSKname", EMSKname
, EAP_EMSK_NAME_LEN
);
758 pos
= wpa_snprintf_hex(erp
->keyname_nai
, nai_buf_len
,
759 EMSKname
, EAP_EMSK_NAME_LEN
);
760 erp
->keyname_nai
[pos
] = '@';
761 os_memcpy(&erp
->keyname_nai
[pos
+ 1], realm
, realm_len
);
763 WPA_PUT_BE16(len
, emsk_len
);
764 if (hmac_sha256_kdf(emsk
, emsk_len
,
765 "EAP Re-authentication Root Key@ietf.org",
766 len
, sizeof(len
), erp
->rRK
, emsk_len
) < 0) {
767 wpa_printf(MSG_DEBUG
, "EAP: Could not derive rRK for ERP");
770 erp
->rRK_len
= emsk_len
;
771 wpa_hexdump_key(MSG_DEBUG
, "EAP: ERP rRK", erp
->rRK
, erp
->rRK_len
);
773 ctx
[0] = EAP_ERP_CS_HMAC_SHA256_128
;
774 WPA_PUT_BE16(&ctx
[1], erp
->rRK_len
);
775 if (hmac_sha256_kdf(erp
->rRK
, erp
->rRK_len
,
776 "Re-authentication Integrity Key@ietf.org",
777 ctx
, sizeof(ctx
), erp
->rIK
, erp
->rRK_len
) < 0) {
778 wpa_printf(MSG_DEBUG
, "EAP: Could not derive rIK for ERP");
781 erp
->rIK_len
= erp
->rRK_len
;
782 wpa_hexdump_key(MSG_DEBUG
, "EAP: ERP rIK", erp
->rIK
, erp
->rIK_len
);
784 wpa_printf(MSG_DEBUG
, "EAP: Stored ERP keys %s", erp
->keyname_nai
);
785 dl_list_add(&sm
->erp_keys
, &erp
->list
);
789 bin_clear_free(ext_emsk
, ext_emsk_len
);
791 bin_clear_free(emsk
, emsk_len
);
792 bin_clear_free(ext_session_id
, ext_session_id_len
);
793 bin_clear_free(erp
, sizeof(*erp
));
795 #endif /* CONFIG_ERP */
800 struct wpabuf
* eap_peer_build_erp_reauth_start(struct eap_sm
*sm
, u8 eap_id
)
803 struct eap_erp_key
*erp
;
805 u8 hash
[SHA256_MAC_LEN
];
807 realm
= eap_home_realm(sm
);
811 erp
= eap_erp_get_key(sm
, realm
);
817 if (erp
->next_seq
>= 65536)
818 return NULL
; /* SEQ has range of 0..65535 */
820 /* TODO: check rRK lifetime expiration */
822 wpa_printf(MSG_DEBUG
, "EAP: Valid ERP key found %s (SEQ=%u)",
823 erp
->keyname_nai
, erp
->next_seq
);
825 msg
= eap_msg_alloc(EAP_VENDOR_IETF
,
826 (enum eap_type
) EAP_ERP_TYPE_REAUTH
,
827 1 + 2 + 2 + os_strlen(erp
->keyname_nai
) + 1 + 16,
828 EAP_CODE_INITIATE
, eap_id
);
832 wpabuf_put_u8(msg
, 0x20); /* Flags: R=0 B=0 L=1 */
833 wpabuf_put_be16(msg
, erp
->next_seq
);
835 wpabuf_put_u8(msg
, EAP_ERP_TLV_KEYNAME_NAI
);
836 wpabuf_put_u8(msg
, os_strlen(erp
->keyname_nai
));
837 wpabuf_put_str(msg
, erp
->keyname_nai
);
839 wpabuf_put_u8(msg
, EAP_ERP_CS_HMAC_SHA256_128
); /* Cryptosuite */
841 if (hmac_sha256(erp
->rIK
, erp
->rIK_len
,
842 wpabuf_head(msg
), wpabuf_len(msg
), hash
) < 0) {
846 wpabuf_put_data(msg
, hash
, 16);
848 sm
->erp_seq
= erp
->next_seq
;
851 wpa_hexdump_buf(MSG_DEBUG
, "ERP: EAP-Initiate/Re-auth", msg
);
857 static int eap_peer_erp_reauth_start(struct eap_sm
*sm
, u8 eap_id
)
861 msg
= eap_peer_build_erp_reauth_start(sm
, eap_id
);
865 wpa_printf(MSG_DEBUG
, "EAP: Sending EAP-Initiate/Re-auth");
866 wpabuf_free(sm
->eapRespData
);
867 sm
->eapRespData
= msg
;
868 sm
->reauthInit
= TRUE
;
871 #endif /* CONFIG_ERP */
875 * The method processing happens here. The request from the authenticator is
876 * processed, and an appropriate response packet is built.
878 SM_STATE(EAP
, METHOD
)
880 struct wpabuf
*eapReqData
;
881 struct eap_method_ret ret
;
884 SM_ENTRY(EAP
, METHOD
);
886 wpa_printf(MSG_WARNING
, "EAP::METHOD - method not selected");
890 eapReqData
= eapol_get_eapReqData(sm
);
891 if (sm
->m
->vendor
== EAP_VENDOR_IETF
&& sm
->m
->method
== EAP_TYPE_LEAP
)
892 min_len
= 0; /* LEAP uses EAP-Success without payload */
893 if (!eap_hdr_len_valid(eapReqData
, min_len
))
897 * Get ignore, methodState, decision, allowNotifications, and
898 * eapRespData. RFC 4137 uses three separate method procedure (check,
899 * process, and buildResp) in this state. These have been combined into
900 * a single function call to m->process() in order to optimize EAP
901 * method implementation interface a bit. These procedures are only
902 * used from within this METHOD state, so there is no need to keep
903 * these as separate C functions.
905 * The RFC 4137 procedures return values as follows:
906 * ignore = m.check(eapReqData)
907 * (methodState, decision, allowNotifications) = m.process(eapReqData)
908 * eapRespData = m.buildResp(reqId)
910 os_memset(&ret
, 0, sizeof(ret
));
911 ret
.ignore
= sm
->ignore
;
912 ret
.methodState
= sm
->methodState
;
913 ret
.decision
= sm
->decision
;
914 ret
.allowNotifications
= sm
->allowNotifications
;
915 wpabuf_free(sm
->eapRespData
);
916 sm
->eapRespData
= NULL
;
917 sm
->eapRespData
= sm
->m
->process(sm
, sm
->eap_method_priv
, &ret
,
919 wpa_printf(MSG_DEBUG
, "EAP: method process -> ignore=%s "
920 "methodState=%s decision=%s eapRespData=%p",
921 ret
.ignore
? "TRUE" : "FALSE",
922 eap_sm_method_state_txt(ret
.methodState
),
923 eap_sm_decision_txt(ret
.decision
),
926 sm
->ignore
= ret
.ignore
;
929 sm
->methodState
= ret
.methodState
;
930 sm
->decision
= ret
.decision
;
931 sm
->allowNotifications
= ret
.allowNotifications
;
933 if (sm
->m
->isKeyAvailable
&& sm
->m
->getKey
&&
934 sm
->m
->isKeyAvailable(sm
, sm
->eap_method_priv
)) {
936 sm
->eapKeyData
= sm
->m
->getKey(sm
, sm
->eap_method_priv
,
938 os_free(sm
->eapSessionId
);
939 sm
->eapSessionId
= NULL
;
940 if (sm
->m
->getSessionId
) {
941 sm
->eapSessionId
= sm
->m
->getSessionId(
942 sm
, sm
->eap_method_priv
,
943 &sm
->eapSessionIdLen
);
944 wpa_hexdump(MSG_DEBUG
, "EAP: Session-Id",
945 sm
->eapSessionId
, sm
->eapSessionIdLen
);
952 * This state signals the lower layer that a response packet is ready to be
955 SM_STATE(EAP
, SEND_RESPONSE
)
957 SM_ENTRY(EAP
, SEND_RESPONSE
);
958 wpabuf_free(sm
->lastRespData
);
959 if (sm
->eapRespData
) {
960 if (wpabuf_len(sm
->eapRespData
) >= 20)
961 sm
->num_rounds_short
= 0;
963 os_memcpy(sm
->last_sha1
, sm
->req_sha1
, 20);
964 sm
->lastId
= sm
->reqId
;
965 sm
->lastRespData
= wpabuf_dup(sm
->eapRespData
);
966 eapol_set_bool(sm
, EAPOL_eapResp
, TRUE
);
968 wpa_printf(MSG_DEBUG
, "EAP: No eapRespData available");
969 sm
->lastRespData
= NULL
;
971 eapol_set_bool(sm
, EAPOL_eapReq
, FALSE
);
972 eapol_set_int(sm
, EAPOL_idleWhile
, sm
->ClientTimeout
);
973 sm
->reauthInit
= FALSE
;
978 * This state signals the lower layer that the request was discarded, and no
979 * response packet will be sent at this time.
981 SM_STATE(EAP
, DISCARD
)
983 SM_ENTRY(EAP
, DISCARD
);
984 eapol_set_bool(sm
, EAPOL_eapReq
, FALSE
);
985 eapol_set_bool(sm
, EAPOL_eapNoResp
, TRUE
);
990 * Handles requests for Identity method and builds a response.
992 SM_STATE(EAP
, IDENTITY
)
994 const struct wpabuf
*eapReqData
;
996 SM_ENTRY(EAP
, IDENTITY
);
997 eapReqData
= eapol_get_eapReqData(sm
);
998 if (!eap_hdr_len_valid(eapReqData
, 1))
1000 eap_sm_processIdentity(sm
, eapReqData
);
1001 wpabuf_free(sm
->eapRespData
);
1002 sm
->eapRespData
= NULL
;
1003 sm
->eapRespData
= eap_sm_buildIdentity(sm
, sm
->reqId
, 0);
1008 * Handles requests for Notification method and builds a response.
1010 SM_STATE(EAP
, NOTIFICATION
)
1012 const struct wpabuf
*eapReqData
;
1014 SM_ENTRY(EAP
, NOTIFICATION
);
1015 eapReqData
= eapol_get_eapReqData(sm
);
1016 if (!eap_hdr_len_valid(eapReqData
, 1))
1018 eap_sm_processNotify(sm
, eapReqData
);
1019 wpabuf_free(sm
->eapRespData
);
1020 sm
->eapRespData
= NULL
;
1021 sm
->eapRespData
= eap_sm_buildNotify(sm
->reqId
);
1026 * This state retransmits the previous response packet.
1028 SM_STATE(EAP
, RETRANSMIT
)
1030 SM_ENTRY(EAP
, RETRANSMIT
);
1031 wpabuf_free(sm
->eapRespData
);
1032 if (sm
->lastRespData
)
1033 sm
->eapRespData
= wpabuf_dup(sm
->lastRespData
);
1035 sm
->eapRespData
= NULL
;
1040 * This state is entered in case of a successful completion of authentication
1041 * and state machine waits here until port is disabled or EAP authentication is
1044 SM_STATE(EAP
, SUCCESS
)
1046 struct eap_peer_config
*config
= eap_get_config(sm
);
1048 SM_ENTRY(EAP
, SUCCESS
);
1049 if (sm
->eapKeyData
!= NULL
)
1050 sm
->eapKeyAvailable
= TRUE
;
1051 eapol_set_bool(sm
, EAPOL_eapSuccess
, TRUE
);
1054 * RFC 4137 does not clear eapReq here, but this seems to be required
1055 * to avoid processing the same request twice when state machine is
1058 eapol_set_bool(sm
, EAPOL_eapReq
, FALSE
);
1061 * RFC 4137 does not set eapNoResp here, but this seems to be required
1062 * to get EAPOL Supplicant backend state machine into SUCCESS state. In
1063 * addition, either eapResp or eapNoResp is required to be set after
1064 * processing the received EAP frame.
1066 eapol_set_bool(sm
, EAPOL_eapNoResp
, TRUE
);
1068 wpa_msg(sm
->msg_ctx
, MSG_INFO
, WPA_EVENT_EAP_SUCCESS
1069 "EAP authentication completed successfully");
1071 if (config
->erp
&& sm
->m
->get_emsk
&& sm
->eapSessionId
&&
1072 sm
->m
->isKeyAvailable
&&
1073 sm
->m
->isKeyAvailable(sm
, sm
->eap_method_priv
))
1074 eap_peer_erp_init(sm
, NULL
, 0, NULL
, 0);
1079 * This state is entered in case of a failure and state machine waits here
1080 * until port is disabled or EAP authentication is restarted.
1082 SM_STATE(EAP
, FAILURE
)
1084 SM_ENTRY(EAP
, FAILURE
);
1085 eapol_set_bool(sm
, EAPOL_eapFail
, TRUE
);
1088 * RFC 4137 does not clear eapReq here, but this seems to be required
1089 * to avoid processing the same request twice when state machine is
1092 eapol_set_bool(sm
, EAPOL_eapReq
, FALSE
);
1095 * RFC 4137 does not set eapNoResp here. However, either eapResp or
1096 * eapNoResp is required to be set after processing the received EAP
1099 eapol_set_bool(sm
, EAPOL_eapNoResp
, TRUE
);
1101 wpa_msg(sm
->msg_ctx
, MSG_INFO
, WPA_EVENT_EAP_FAILURE
1102 "EAP authentication failed");
1104 sm
->prev_failure
= 1;
1108 static int eap_success_workaround(struct eap_sm
*sm
, int reqId
, int lastId
)
1111 * At least Microsoft IAS and Meetinghouse Aegis seem to be sending
1112 * EAP-Success/Failure with lastId + 1 even though RFC 3748 and
1113 * RFC 4137 require that reqId == lastId. In addition, it looks like
1114 * Ringmaster v2.1.2.0 would be using lastId + 2 in EAP-Success.
1116 * Accept this kind of Id if EAP workarounds are enabled. These are
1117 * unauthenticated plaintext messages, so this should have minimal
1118 * security implications (bit easier to fake EAP-Success/Failure).
1120 if (sm
->workaround
&& (reqId
== ((lastId
+ 1) & 0xff) ||
1121 reqId
== ((lastId
+ 2) & 0xff))) {
1122 wpa_printf(MSG_DEBUG
, "EAP: Workaround for unexpected "
1123 "identifier field in EAP Success: "
1124 "reqId=%d lastId=%d (these are supposed to be "
1125 "same)", reqId
, lastId
);
1128 wpa_printf(MSG_DEBUG
, "EAP: EAP-Success Id mismatch - reqId=%d "
1129 "lastId=%d", reqId
, lastId
);
1135 * RFC 4137 - Appendix A.1: EAP Peer State Machine - State transitions
1138 static void eap_peer_sm_step_idle(struct eap_sm
*sm
)
1141 * The first three transitions are from RFC 4137. The last two are
1142 * local additions to handle special cases with LEAP and PEAP server
1143 * not sending EAP-Success in some cases.
1145 if (eapol_get_bool(sm
, EAPOL_eapReq
))
1146 SM_ENTER(EAP
, RECEIVED
);
1147 else if ((eapol_get_bool(sm
, EAPOL_altAccept
) &&
1148 sm
->decision
!= DECISION_FAIL
) ||
1149 (eapol_get_int(sm
, EAPOL_idleWhile
) == 0 &&
1150 sm
->decision
== DECISION_UNCOND_SUCC
))
1151 SM_ENTER(EAP
, SUCCESS
);
1152 else if (eapol_get_bool(sm
, EAPOL_altReject
) ||
1153 (eapol_get_int(sm
, EAPOL_idleWhile
) == 0 &&
1154 sm
->decision
!= DECISION_UNCOND_SUCC
) ||
1155 (eapol_get_bool(sm
, EAPOL_altAccept
) &&
1156 sm
->methodState
!= METHOD_CONT
&&
1157 sm
->decision
== DECISION_FAIL
))
1158 SM_ENTER(EAP
, FAILURE
);
1159 else if (sm
->selectedMethod
== EAP_TYPE_LEAP
&&
1160 sm
->leap_done
&& sm
->decision
!= DECISION_FAIL
&&
1161 sm
->methodState
== METHOD_DONE
)
1162 SM_ENTER(EAP
, SUCCESS
);
1163 else if (sm
->selectedMethod
== EAP_TYPE_PEAP
&&
1164 sm
->peap_done
&& sm
->decision
!= DECISION_FAIL
&&
1165 sm
->methodState
== METHOD_DONE
)
1166 SM_ENTER(EAP
, SUCCESS
);
1170 static int eap_peer_req_is_duplicate(struct eap_sm
*sm
)
1174 duplicate
= (sm
->reqId
== sm
->lastId
) && sm
->rxReq
;
1175 if (sm
->workaround
&& duplicate
&&
1176 os_memcmp(sm
->req_sha1
, sm
->last_sha1
, 20) != 0) {
1178 * RFC 4137 uses (reqId == lastId) as the only verification for
1179 * duplicate EAP requests. However, this misses cases where the
1180 * AS is incorrectly using the same id again; and
1181 * unfortunately, such implementations exist. Use SHA1 hash as
1182 * an extra verification for the packets being duplicate to
1183 * workaround these issues.
1185 wpa_printf(MSG_DEBUG
, "EAP: AS used the same Id again, but "
1186 "EAP packets were not identical");
1187 wpa_printf(MSG_DEBUG
, "EAP: workaround - assume this is not a "
1188 "duplicate packet");
1196 static int eap_peer_sm_allow_canned(struct eap_sm
*sm
)
1198 struct eap_peer_config
*config
= eap_get_config(sm
);
1200 return config
&& config
->phase1
&&
1201 os_strstr(config
->phase1
, "allow_canned_success=1");
1205 static void eap_peer_sm_step_received(struct eap_sm
*sm
)
1207 int duplicate
= eap_peer_req_is_duplicate(sm
);
1210 * Two special cases below for LEAP are local additions to work around
1211 * odd LEAP behavior (EAP-Success in the middle of authentication and
1212 * then swapped roles). Other transitions are based on RFC 4137.
1214 if (sm
->rxSuccess
&& sm
->decision
!= DECISION_FAIL
&&
1215 (sm
->reqId
== sm
->lastId
||
1216 eap_success_workaround(sm
, sm
->reqId
, sm
->lastId
)))
1217 SM_ENTER(EAP
, SUCCESS
);
1218 else if (sm
->workaround
&& sm
->lastId
== -1 && sm
->rxSuccess
&&
1219 !sm
->rxFailure
&& !sm
->rxReq
&& eap_peer_sm_allow_canned(sm
))
1220 SM_ENTER(EAP
, SUCCESS
); /* EAP-Success prior any EAP method */
1221 else if (sm
->workaround
&& sm
->lastId
== -1 && sm
->rxFailure
&&
1222 !sm
->rxReq
&& sm
->methodState
!= METHOD_CONT
&&
1223 eap_peer_sm_allow_canned(sm
))
1224 SM_ENTER(EAP
, FAILURE
); /* EAP-Failure prior any EAP method */
1225 else if (sm
->workaround
&& sm
->rxSuccess
&& !sm
->rxFailure
&&
1226 !sm
->rxReq
&& sm
->methodState
!= METHOD_CONT
&&
1227 eap_peer_sm_allow_canned(sm
))
1228 SM_ENTER(EAP
, SUCCESS
); /* EAP-Success after Identity */
1229 else if (sm
->methodState
!= METHOD_CONT
&&
1231 sm
->decision
!= DECISION_UNCOND_SUCC
) ||
1232 (sm
->rxSuccess
&& sm
->decision
== DECISION_FAIL
&&
1233 (sm
->selectedMethod
!= EAP_TYPE_LEAP
||
1234 sm
->methodState
!= METHOD_MAY_CONT
))) &&
1235 (sm
->reqId
== sm
->lastId
||
1236 eap_success_workaround(sm
, sm
->reqId
, sm
->lastId
)))
1237 SM_ENTER(EAP
, FAILURE
);
1238 else if (sm
->rxReq
&& duplicate
)
1239 SM_ENTER(EAP
, RETRANSMIT
);
1240 else if (sm
->rxReq
&& !duplicate
&&
1241 sm
->reqMethod
== EAP_TYPE_NOTIFICATION
&&
1242 sm
->allowNotifications
)
1243 SM_ENTER(EAP
, NOTIFICATION
);
1244 else if (sm
->rxReq
&& !duplicate
&&
1245 sm
->selectedMethod
== EAP_TYPE_NONE
&&
1246 sm
->reqMethod
== EAP_TYPE_IDENTITY
)
1247 SM_ENTER(EAP
, IDENTITY
);
1248 else if (sm
->rxReq
&& !duplicate
&&
1249 sm
->selectedMethod
== EAP_TYPE_NONE
&&
1250 sm
->reqMethod
!= EAP_TYPE_IDENTITY
&&
1251 sm
->reqMethod
!= EAP_TYPE_NOTIFICATION
)
1252 SM_ENTER(EAP
, GET_METHOD
);
1253 else if (sm
->rxReq
&& !duplicate
&&
1254 sm
->reqMethod
== sm
->selectedMethod
&&
1255 sm
->methodState
!= METHOD_DONE
)
1256 SM_ENTER(EAP
, METHOD
);
1257 else if (sm
->selectedMethod
== EAP_TYPE_LEAP
&&
1258 (sm
->rxSuccess
|| sm
->rxResp
))
1259 SM_ENTER(EAP
, METHOD
);
1260 else if (sm
->reauthInit
)
1261 SM_ENTER(EAP
, SEND_RESPONSE
);
1263 SM_ENTER(EAP
, DISCARD
);
1267 static void eap_peer_sm_step_local(struct eap_sm
*sm
)
1269 switch (sm
->EAP_state
) {
1270 case EAP_INITIALIZE
:
1271 SM_ENTER(EAP
, IDLE
);
1274 if (eapol_get_bool(sm
, EAPOL_portEnabled
) &&
1275 !sm
->force_disabled
)
1276 SM_ENTER(EAP
, INITIALIZE
);
1279 eap_peer_sm_step_idle(sm
);
1282 eap_peer_sm_step_received(sm
);
1284 case EAP_GET_METHOD
:
1285 if (sm
->selectedMethod
== sm
->reqMethod
)
1286 SM_ENTER(EAP
, METHOD
);
1288 SM_ENTER(EAP
, SEND_RESPONSE
);
1292 * Note: RFC 4137 uses methodState == DONE && decision == FAIL
1293 * as the condition. eapRespData == NULL here is used to allow
1294 * final EAP method response to be sent without having to change
1295 * all methods to either use methodState MAY_CONT or leaving
1296 * decision to something else than FAIL in cases where the only
1297 * expected response is EAP-Failure.
1300 SM_ENTER(EAP
, DISCARD
);
1301 else if (sm
->methodState
== METHOD_DONE
&&
1302 sm
->decision
== DECISION_FAIL
&& !sm
->eapRespData
)
1303 SM_ENTER(EAP
, FAILURE
);
1305 SM_ENTER(EAP
, SEND_RESPONSE
);
1307 case EAP_SEND_RESPONSE
:
1308 SM_ENTER(EAP
, IDLE
);
1311 SM_ENTER(EAP
, IDLE
);
1314 SM_ENTER(EAP
, SEND_RESPONSE
);
1316 case EAP_NOTIFICATION
:
1317 SM_ENTER(EAP
, SEND_RESPONSE
);
1319 case EAP_RETRANSMIT
:
1320 SM_ENTER(EAP
, SEND_RESPONSE
);
1332 /* Global transitions */
1333 if (eapol_get_bool(sm
, EAPOL_eapRestart
) &&
1334 eapol_get_bool(sm
, EAPOL_portEnabled
))
1335 SM_ENTER_GLOBAL(EAP
, INITIALIZE
);
1336 else if (!eapol_get_bool(sm
, EAPOL_portEnabled
) || sm
->force_disabled
)
1337 SM_ENTER_GLOBAL(EAP
, DISABLED
);
1338 else if (sm
->num_rounds
> EAP_MAX_AUTH_ROUNDS
) {
1339 /* RFC 4137 does not place any limit on number of EAP messages
1340 * in an authentication session. However, some error cases have
1341 * ended up in a state were EAP messages were sent between the
1342 * peer and server in a loop (e.g., TLS ACK frame in both
1343 * direction). Since this is quite undesired outcome, limit the
1344 * total number of EAP round-trips and abort authentication if
1345 * this limit is exceeded.
1347 if (sm
->num_rounds
== EAP_MAX_AUTH_ROUNDS
+ 1) {
1348 wpa_msg(sm
->msg_ctx
, MSG_INFO
, "EAP: more than %d "
1349 "authentication rounds - abort",
1350 EAP_MAX_AUTH_ROUNDS
);
1352 SM_ENTER_GLOBAL(EAP
, FAILURE
);
1354 } else if (sm
->num_rounds_short
> EAP_MAX_AUTH_ROUNDS_SHORT
) {
1355 if (sm
->num_rounds_short
== EAP_MAX_AUTH_ROUNDS_SHORT
+ 1) {
1356 wpa_msg(sm
->msg_ctx
, MSG_INFO
,
1357 "EAP: more than %d authentication rounds (short) - abort",
1358 EAP_MAX_AUTH_ROUNDS_SHORT
);
1359 sm
->num_rounds_short
++;
1360 SM_ENTER_GLOBAL(EAP
, FAILURE
);
1363 /* Local transitions */
1364 eap_peer_sm_step_local(sm
);
1369 static Boolean
eap_sm_allowMethod(struct eap_sm
*sm
, int vendor
,
1370 enum eap_type method
)
1372 if (!eap_allowed_method(sm
, vendor
, method
)) {
1373 wpa_printf(MSG_DEBUG
, "EAP: configuration does not allow: "
1374 "vendor %u method %u", vendor
, method
);
1377 if (eap_peer_get_eap_method(vendor
, method
))
1379 wpa_printf(MSG_DEBUG
, "EAP: not included in build: "
1380 "vendor %u method %u", vendor
, method
);
1385 static struct wpabuf
* eap_sm_build_expanded_nak(
1386 struct eap_sm
*sm
, int id
, const struct eap_method
*methods
,
1389 struct wpabuf
*resp
;
1391 const struct eap_method
*m
;
1393 wpa_printf(MSG_DEBUG
, "EAP: Building expanded EAP-Nak");
1395 /* RFC 3748 - 5.3.2: Expanded Nak */
1396 resp
= eap_msg_alloc(EAP_VENDOR_IETF
, EAP_TYPE_EXPANDED
,
1397 8 + 8 * (count
+ 1), EAP_CODE_RESPONSE
, id
);
1401 wpabuf_put_be24(resp
, EAP_VENDOR_IETF
);
1402 wpabuf_put_be32(resp
, EAP_TYPE_NAK
);
1404 for (m
= methods
; m
; m
= m
->next
) {
1405 if (sm
->reqVendor
== m
->vendor
&&
1406 sm
->reqVendorMethod
== m
->method
)
1407 continue; /* do not allow the current method again */
1408 if (eap_allowed_method(sm
, m
->vendor
, m
->method
)) {
1409 wpa_printf(MSG_DEBUG
, "EAP: allowed type: "
1410 "vendor=%u method=%u",
1411 m
->vendor
, m
->method
);
1412 wpabuf_put_u8(resp
, EAP_TYPE_EXPANDED
);
1413 wpabuf_put_be24(resp
, m
->vendor
);
1414 wpabuf_put_be32(resp
, m
->method
);
1420 wpa_printf(MSG_DEBUG
, "EAP: no more allowed methods");
1421 wpabuf_put_u8(resp
, EAP_TYPE_EXPANDED
);
1422 wpabuf_put_be24(resp
, EAP_VENDOR_IETF
);
1423 wpabuf_put_be32(resp
, EAP_TYPE_NONE
);
1426 eap_update_len(resp
);
1432 static struct wpabuf
* eap_sm_buildNak(struct eap_sm
*sm
, int id
)
1434 struct wpabuf
*resp
;
1436 int found
= 0, expanded_found
= 0;
1438 const struct eap_method
*methods
, *m
;
1440 wpa_printf(MSG_DEBUG
, "EAP: Building EAP-Nak (requested type %u "
1441 "vendor=%u method=%u not allowed)", sm
->reqMethod
,
1442 sm
->reqVendor
, sm
->reqVendorMethod
);
1443 methods
= eap_peer_get_methods(&count
);
1444 if (methods
== NULL
)
1446 if (sm
->reqMethod
== EAP_TYPE_EXPANDED
)
1447 return eap_sm_build_expanded_nak(sm
, id
, methods
, count
);
1449 /* RFC 3748 - 5.3.1: Legacy Nak */
1450 resp
= eap_msg_alloc(EAP_VENDOR_IETF
, EAP_TYPE_NAK
,
1451 sizeof(struct eap_hdr
) + 1 + count
+ 1,
1452 EAP_CODE_RESPONSE
, id
);
1456 start
= wpabuf_put(resp
, 0);
1457 for (m
= methods
; m
; m
= m
->next
) {
1458 if (m
->vendor
== EAP_VENDOR_IETF
&& m
->method
== sm
->reqMethod
)
1459 continue; /* do not allow the current method again */
1460 if (eap_allowed_method(sm
, m
->vendor
, m
->method
)) {
1461 if (m
->vendor
!= EAP_VENDOR_IETF
) {
1465 wpabuf_put_u8(resp
, EAP_TYPE_EXPANDED
);
1467 wpabuf_put_u8(resp
, m
->method
);
1472 wpabuf_put_u8(resp
, EAP_TYPE_NONE
);
1473 wpa_hexdump(MSG_DEBUG
, "EAP: allowed methods", start
, found
);
1475 eap_update_len(resp
);
1481 static void eap_sm_processIdentity(struct eap_sm
*sm
, const struct wpabuf
*req
)
1486 wpa_msg(sm
->msg_ctx
, MSG_INFO
, WPA_EVENT_EAP_STARTED
1487 "EAP authentication started");
1488 eap_notify_status(sm
, "started", "");
1490 pos
= eap_hdr_validate(EAP_VENDOR_IETF
, EAP_TYPE_IDENTITY
, req
,
1496 * RFC 3748 - 5.1: Identity
1497 * Data field may contain a displayable message in UTF-8. If this
1498 * includes NUL-character, only the data before that should be
1499 * displayed. Some EAP implementasitons may piggy-back additional
1500 * options after the NUL.
1502 /* TODO: could save displayable message so that it can be shown to the
1503 * user in case of interaction is required */
1504 wpa_hexdump_ascii(MSG_DEBUG
, "EAP: EAP-Request Identity data",
1512 * Rules for figuring out MNC length based on IMSI for SIM cards that do not
1513 * include MNC length field.
1515 static int mnc_len_from_imsi(const char *imsi
)
1520 os_memcpy(mcc_str
, imsi
, 3);
1522 mcc
= atoi(mcc_str
);
1525 return 2; /* Networks in Switzerland use 2-digit MNC */
1527 return 2; /* Networks in Finland use 2-digit MNC */
1533 static int eap_sm_imsi_identity(struct eap_sm
*sm
,
1534 struct eap_peer_config
*conf
)
1536 enum { EAP_SM_SIM
, EAP_SM_AKA
, EAP_SM_AKA_PRIME
} method
= EAP_SM_SIM
;
1539 struct eap_method_type
*m
= conf
->eap_methods
;
1542 imsi_len
= sizeof(imsi
);
1543 if (scard_get_imsi(sm
->scard_ctx
, imsi
, &imsi_len
)) {
1544 wpa_printf(MSG_WARNING
, "Failed to get IMSI from SIM");
1548 wpa_hexdump_ascii(MSG_DEBUG
, "IMSI", (u8
*) imsi
, imsi_len
);
1551 wpa_printf(MSG_WARNING
, "Too short IMSI for SIM identity");
1555 /* MNC (2 or 3 digits) */
1556 mnc_len
= scard_get_mnc_len(sm
->scard_ctx
);
1558 mnc_len
= mnc_len_from_imsi(imsi
);
1560 wpa_printf(MSG_INFO
, "Failed to get MNC length from (U)SIM "
1565 if (eap_sm_append_3gpp_realm(sm
, imsi
, sizeof(imsi
), &imsi_len
,
1567 wpa_printf(MSG_WARNING
, "Could not add realm to SIM identity");
1570 wpa_hexdump_ascii(MSG_DEBUG
, "IMSI + realm", (u8
*) imsi
, imsi_len
);
1572 for (i
= 0; m
&& (m
[i
].vendor
!= EAP_VENDOR_IETF
||
1573 m
[i
].method
!= EAP_TYPE_NONE
); i
++) {
1574 if (m
[i
].vendor
== EAP_VENDOR_IETF
&&
1575 m
[i
].method
== EAP_TYPE_AKA_PRIME
) {
1576 method
= EAP_SM_AKA_PRIME
;
1580 if (m
[i
].vendor
== EAP_VENDOR_IETF
&&
1581 m
[i
].method
== EAP_TYPE_AKA
) {
1582 method
= EAP_SM_AKA
;
1587 os_free(conf
->identity
);
1588 conf
->identity
= os_malloc(1 + imsi_len
);
1589 if (conf
->identity
== NULL
) {
1590 wpa_printf(MSG_WARNING
, "Failed to allocate buffer for "
1591 "IMSI-based identity");
1597 conf
->identity
[0] = '1';
1600 conf
->identity
[0] = '0';
1602 case EAP_SM_AKA_PRIME
:
1603 conf
->identity
[0] = '6';
1606 os_memcpy(conf
->identity
+ 1, imsi
, imsi_len
);
1607 conf
->identity_len
= 1 + imsi_len
;
1613 static int eap_sm_set_scard_pin(struct eap_sm
*sm
,
1614 struct eap_peer_config
*conf
)
1616 if (scard_set_pin(sm
->scard_ctx
, conf
->pin
)) {
1618 * Make sure the same PIN is not tried again in order to avoid
1624 wpa_printf(MSG_WARNING
, "PIN validation failed");
1625 eap_sm_request_pin(sm
);
1632 static int eap_sm_get_scard_identity(struct eap_sm
*sm
,
1633 struct eap_peer_config
*conf
)
1635 if (eap_sm_set_scard_pin(sm
, conf
))
1638 return eap_sm_imsi_identity(sm
, conf
);
1641 #endif /* PCSC_FUNCS */
1645 * eap_sm_buildIdentity - Build EAP-Identity/Response for the current network
1646 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
1647 * @id: EAP identifier for the packet
1648 * @encrypted: Whether the packet is for encrypted tunnel (EAP phase 2)
1649 * Returns: Pointer to the allocated EAP-Identity/Response packet or %NULL on
1652 * This function allocates and builds an EAP-Identity/Response packet for the
1653 * current network. The caller is responsible for freeing the returned data.
1655 struct wpabuf
* eap_sm_buildIdentity(struct eap_sm
*sm
, int id
, int encrypted
)
1657 struct eap_peer_config
*config
= eap_get_config(sm
);
1658 struct wpabuf
*resp
;
1660 size_t identity_len
;
1662 if (config
== NULL
) {
1663 wpa_printf(MSG_WARNING
, "EAP: buildIdentity: configuration "
1664 "was not available");
1668 if (sm
->m
&& sm
->m
->get_identity
&&
1669 (identity
= sm
->m
->get_identity(sm
, sm
->eap_method_priv
,
1670 &identity_len
)) != NULL
) {
1671 wpa_hexdump_ascii(MSG_DEBUG
, "EAP: using method re-auth "
1672 "identity", identity
, identity_len
);
1673 } else if (!encrypted
&& config
->anonymous_identity
) {
1674 identity
= config
->anonymous_identity
;
1675 identity_len
= config
->anonymous_identity_len
;
1676 wpa_hexdump_ascii(MSG_DEBUG
, "EAP: using anonymous identity",
1677 identity
, identity_len
);
1679 identity
= config
->identity
;
1680 identity_len
= config
->identity_len
;
1681 wpa_hexdump_ascii(MSG_DEBUG
, "EAP: using real identity",
1682 identity
, identity_len
);
1688 if (eap_sm_get_scard_identity(sm
, config
) < 0)
1690 identity
= config
->identity
;
1691 identity_len
= config
->identity_len
;
1692 wpa_hexdump_ascii(MSG_DEBUG
,
1693 "permanent identity from IMSI",
1694 identity
, identity_len
);
1695 } else if (eap_sm_set_scard_pin(sm
, config
) < 0) {
1698 #else /* PCSC_FUNCS */
1700 #endif /* PCSC_FUNCS */
1701 } else if (!identity
) {
1702 wpa_printf(MSG_WARNING
,
1703 "EAP: buildIdentity: identity configuration was not available");
1704 eap_sm_request_identity(sm
);
1708 resp
= eap_msg_alloc(EAP_VENDOR_IETF
, EAP_TYPE_IDENTITY
, identity_len
,
1709 EAP_CODE_RESPONSE
, id
);
1713 wpabuf_put_data(resp
, identity
, identity_len
);
1719 static void eap_sm_processNotify(struct eap_sm
*sm
, const struct wpabuf
*req
)
1725 pos
= eap_hdr_validate(EAP_VENDOR_IETF
, EAP_TYPE_NOTIFICATION
, req
,
1729 wpa_hexdump_ascii(MSG_DEBUG
, "EAP: EAP-Request Notification data",
1732 msg
= os_malloc(msg_len
+ 1);
1735 for (i
= 0; i
< msg_len
; i
++)
1736 msg
[i
] = isprint(pos
[i
]) ? (char) pos
[i
] : '_';
1737 msg
[msg_len
] = '\0';
1738 wpa_msg(sm
->msg_ctx
, MSG_INFO
, "%s%s",
1739 WPA_EVENT_EAP_NOTIFICATION
, msg
);
1744 static struct wpabuf
* eap_sm_buildNotify(int id
)
1746 wpa_printf(MSG_DEBUG
, "EAP: Generating EAP-Response Notification");
1747 return eap_msg_alloc(EAP_VENDOR_IETF
, EAP_TYPE_NOTIFICATION
, 0,
1748 EAP_CODE_RESPONSE
, id
);
1752 static void eap_peer_initiate(struct eap_sm
*sm
, const struct eap_hdr
*hdr
,
1756 const u8
*pos
= (const u8
*) (hdr
+ 1);
1757 const u8
*end
= ((const u8
*) hdr
) + len
;
1758 struct erp_tlvs parse
;
1760 if (len
< sizeof(*hdr
) + 1) {
1761 wpa_printf(MSG_DEBUG
, "EAP: Ignored too short EAP-Initiate");
1765 if (*pos
!= EAP_ERP_TYPE_REAUTH_START
) {
1766 wpa_printf(MSG_DEBUG
,
1767 "EAP: Ignored unexpected EAP-Initiate Type=%u",
1774 wpa_printf(MSG_DEBUG
,
1775 "EAP: Too short EAP-Initiate/Re-auth-Start");
1778 pos
++; /* Reserved */
1779 wpa_hexdump(MSG_DEBUG
, "EAP: EAP-Initiate/Re-auth-Start TVs/TLVs",
1782 if (erp_parse_tlvs(pos
, end
, &parse
, 0) < 0)
1786 wpa_hexdump_ascii(MSG_DEBUG
,
1787 "EAP: EAP-Initiate/Re-auth-Start - Domain name",
1788 parse
.domain
, parse
.domain_len
);
1789 /* TODO: Derivation of domain specific keys for local ER */
1792 if (eap_peer_erp_reauth_start(sm
, hdr
->identifier
) == 0)
1796 #endif /* CONFIG_ERP */
1797 wpa_printf(MSG_DEBUG
,
1798 "EAP: EAP-Initiate/Re-auth-Start - No suitable ERP keys available - try to start full EAP authentication");
1799 eapol_set_bool(sm
, EAPOL_eapTriggerStart
, TRUE
);
1803 void eap_peer_finish(struct eap_sm
*sm
, const struct eap_hdr
*hdr
, size_t len
)
1806 const u8
*pos
= (const u8
*) (hdr
+ 1);
1807 const u8
*end
= ((const u8
*) hdr
) + len
;
1809 struct erp_tlvs parse
;
1812 u8 hash
[SHA256_MAC_LEN
];
1814 struct eap_erp_key
*erp
;
1818 int auth_tag_ok
= 0;
1820 if (len
< sizeof(*hdr
) + 1) {
1821 wpa_printf(MSG_DEBUG
, "EAP: Ignored too short EAP-Finish");
1825 if (*pos
!= EAP_ERP_TYPE_REAUTH
) {
1826 wpa_printf(MSG_DEBUG
,
1827 "EAP: Ignored unexpected EAP-Finish Type=%u", *pos
);
1831 if (len
< sizeof(*hdr
) + 4) {
1832 wpa_printf(MSG_DEBUG
,
1833 "EAP: Ignored too short EAP-Finish/Re-auth");
1839 seq
= WPA_GET_BE16(pos
);
1841 wpa_printf(MSG_DEBUG
, "EAP: Flags=0x%x SEQ=%u", flags
, seq
);
1843 if (seq
!= sm
->erp_seq
) {
1844 wpa_printf(MSG_DEBUG
,
1845 "EAP: Unexpected EAP-Finish/Re-auth SEQ=%u", seq
);
1850 * Parse TVs/TLVs. Since we do not yet know the length of the
1851 * Authentication Tag, stop parsing if an unknown TV/TLV is seen and
1852 * just try to find the keyName-NAI first so that we can check the
1853 * Authentication Tag.
1855 if (erp_parse_tlvs(pos
, end
, &parse
, 1) < 0)
1858 if (!parse
.keyname
) {
1859 wpa_printf(MSG_DEBUG
,
1860 "EAP: No keyName-NAI in EAP-Finish/Re-auth Packet");
1864 wpa_hexdump_ascii(MSG_DEBUG
, "EAP: EAP-Finish/Re-auth - keyName-NAI",
1865 parse
.keyname
, parse
.keyname_len
);
1866 if (parse
.keyname_len
> 253) {
1867 wpa_printf(MSG_DEBUG
,
1868 "EAP: Too long keyName-NAI in EAP-Finish/Re-auth");
1871 os_memcpy(nai
, parse
.keyname
, parse
.keyname_len
);
1872 nai
[parse
.keyname_len
] = '\0';
1874 erp
= eap_erp_get_key_nai(sm
, nai
);
1876 wpa_printf(MSG_DEBUG
, "EAP: No matching ERP key found for %s",
1881 /* Is there enough room for Cryptosuite and Authentication Tag? */
1882 start
= parse
.keyname
+ parse
.keyname_len
;
1883 max_len
= end
- start
;
1885 if (max_len
< 1 + (int) hash_len
) {
1886 wpa_printf(MSG_DEBUG
,
1887 "EAP: Not enough room for Authentication Tag");
1892 if (end
[-17] != EAP_ERP_CS_HMAC_SHA256_128
) {
1893 wpa_printf(MSG_DEBUG
, "EAP: Different Cryptosuite used");
1899 if (hmac_sha256(erp
->rIK
, erp
->rIK_len
, (const u8
*) hdr
,
1900 end
- ((const u8
*) hdr
) - hash_len
, hash
) < 0)
1902 if (os_memcmp(end
- hash_len
, hash
, hash_len
) != 0) {
1903 wpa_printf(MSG_DEBUG
,
1904 "EAP: Authentication Tag mismatch");
1908 end
-= 1 + hash_len
;
1912 * Parse TVs/TLVs again now that we know the exact part of the buffer
1913 * that contains them.
1915 wpa_hexdump(MSG_DEBUG
, "EAP: EAP-Finish/Re-Auth TVs/TLVs",
1917 if (erp_parse_tlvs(pos
, end
, &parse
, 0) < 0)
1920 if (flags
& 0x80 || !auth_tag_ok
) {
1921 wpa_printf(MSG_DEBUG
,
1922 "EAP: EAP-Finish/Re-auth indicated failure");
1923 eapol_set_bool(sm
, EAPOL_eapFail
, TRUE
);
1924 eapol_set_bool(sm
, EAPOL_eapReq
, FALSE
);
1925 eapol_set_bool(sm
, EAPOL_eapNoResp
, TRUE
);
1926 wpa_msg(sm
->msg_ctx
, MSG_INFO
, WPA_EVENT_EAP_FAILURE
1927 "EAP authentication failed");
1928 sm
->prev_failure
= 1;
1929 wpa_printf(MSG_DEBUG
,
1930 "EAP: Drop ERP key to try full authentication on next attempt");
1931 eap_peer_erp_free_key(erp
);
1935 eap_sm_free_key(sm
);
1936 sm
->eapKeyDataLen
= 0;
1937 sm
->eapKeyData
= os_malloc(erp
->rRK_len
);
1938 if (!sm
->eapKeyData
)
1940 sm
->eapKeyDataLen
= erp
->rRK_len
;
1942 WPA_PUT_BE16(seed
, seq
);
1943 WPA_PUT_BE16(&seed
[2], erp
->rRK_len
);
1944 if (hmac_sha256_kdf(erp
->rRK
, erp
->rRK_len
,
1945 "Re-authentication Master Session Key@ietf.org",
1947 sm
->eapKeyData
, erp
->rRK_len
) < 0) {
1948 wpa_printf(MSG_DEBUG
, "EAP: Could not derive rMSK for ERP");
1949 eap_sm_free_key(sm
);
1952 wpa_hexdump_key(MSG_DEBUG
, "EAP: ERP rMSK",
1953 sm
->eapKeyData
, sm
->eapKeyDataLen
);
1954 sm
->eapKeyAvailable
= TRUE
;
1955 eapol_set_bool(sm
, EAPOL_eapSuccess
, TRUE
);
1956 eapol_set_bool(sm
, EAPOL_eapReq
, FALSE
);
1957 eapol_set_bool(sm
, EAPOL_eapNoResp
, TRUE
);
1958 wpa_msg(sm
->msg_ctx
, MSG_INFO
, WPA_EVENT_EAP_SUCCESS
1959 "EAP re-authentication completed successfully");
1960 #endif /* CONFIG_ERP */
1964 static void eap_sm_parseEapReq(struct eap_sm
*sm
, const struct wpabuf
*req
)
1966 const struct eap_hdr
*hdr
;
1970 sm
->rxReq
= sm
->rxResp
= sm
->rxSuccess
= sm
->rxFailure
= FALSE
;
1972 sm
->reqMethod
= EAP_TYPE_NONE
;
1973 sm
->reqVendor
= EAP_VENDOR_IETF
;
1974 sm
->reqVendorMethod
= EAP_TYPE_NONE
;
1976 if (req
== NULL
|| wpabuf_len(req
) < sizeof(*hdr
))
1979 hdr
= wpabuf_head(req
);
1980 plen
= be_to_host16(hdr
->length
);
1981 if (plen
> wpabuf_len(req
)) {
1982 wpa_printf(MSG_DEBUG
, "EAP: Ignored truncated EAP-Packet "
1983 "(len=%lu plen=%lu)",
1984 (unsigned long) wpabuf_len(req
),
1985 (unsigned long) plen
);
1989 sm
->reqId
= hdr
->identifier
;
1991 if (sm
->workaround
) {
1993 addr
[0] = wpabuf_head(req
);
1994 sha1_vector(1, addr
, &plen
, sm
->req_sha1
);
1997 switch (hdr
->code
) {
1998 case EAP_CODE_REQUEST
:
1999 if (plen
< sizeof(*hdr
) + 1) {
2000 wpa_printf(MSG_DEBUG
, "EAP: Too short EAP-Request - "
2005 pos
= (const u8
*) (hdr
+ 1);
2006 sm
->reqMethod
= *pos
++;
2007 if (sm
->reqMethod
== EAP_TYPE_EXPANDED
) {
2008 if (plen
< sizeof(*hdr
) + 8) {
2009 wpa_printf(MSG_DEBUG
, "EAP: Ignored truncated "
2010 "expanded EAP-Packet (plen=%lu)",
2011 (unsigned long) plen
);
2014 sm
->reqVendor
= WPA_GET_BE24(pos
);
2016 sm
->reqVendorMethod
= WPA_GET_BE32(pos
);
2018 wpa_printf(MSG_DEBUG
, "EAP: Received EAP-Request id=%d "
2019 "method=%u vendor=%u vendorMethod=%u",
2020 sm
->reqId
, sm
->reqMethod
, sm
->reqVendor
,
2021 sm
->reqVendorMethod
);
2023 case EAP_CODE_RESPONSE
:
2024 if (sm
->selectedMethod
== EAP_TYPE_LEAP
) {
2026 * LEAP differs from RFC 4137 by using reversed roles
2027 * for mutual authentication and because of this, we
2028 * need to accept EAP-Response frames if LEAP is used.
2030 if (plen
< sizeof(*hdr
) + 1) {
2031 wpa_printf(MSG_DEBUG
, "EAP: Too short "
2032 "EAP-Response - no Type field");
2036 pos
= (const u8
*) (hdr
+ 1);
2037 sm
->reqMethod
= *pos
;
2038 wpa_printf(MSG_DEBUG
, "EAP: Received EAP-Response for "
2039 "LEAP method=%d id=%d",
2040 sm
->reqMethod
, sm
->reqId
);
2043 wpa_printf(MSG_DEBUG
, "EAP: Ignored EAP-Response");
2045 case EAP_CODE_SUCCESS
:
2046 wpa_printf(MSG_DEBUG
, "EAP: Received EAP-Success");
2047 eap_notify_status(sm
, "completion", "success");
2048 sm
->rxSuccess
= TRUE
;
2050 case EAP_CODE_FAILURE
:
2051 wpa_printf(MSG_DEBUG
, "EAP: Received EAP-Failure");
2052 eap_notify_status(sm
, "completion", "failure");
2054 /* Get the error code from method */
2055 if (sm
->m
&& sm
->m
->get_error_code
) {
2058 error_code
= sm
->m
->get_error_code(sm
->eap_method_priv
);
2059 if (error_code
!= NO_EAP_METHOD_ERROR
)
2060 eap_report_error(sm
, error_code
);
2062 sm
->rxFailure
= TRUE
;
2064 case EAP_CODE_INITIATE
:
2065 eap_peer_initiate(sm
, hdr
, plen
);
2067 case EAP_CODE_FINISH
:
2068 eap_peer_finish(sm
, hdr
, plen
);
2071 wpa_printf(MSG_DEBUG
, "EAP: Ignored EAP-Packet with unknown "
2072 "code %d", hdr
->code
);
2078 static void eap_peer_sm_tls_event(void *ctx
, enum tls_event ev
,
2079 union tls_event_data
*data
)
2081 struct eap_sm
*sm
= ctx
;
2082 char *hash_hex
= NULL
;
2085 case TLS_CERT_CHAIN_SUCCESS
:
2086 eap_notify_status(sm
, "remote certificate verification",
2088 if (sm
->ext_cert_check
) {
2089 sm
->waiting_ext_cert_check
= 1;
2090 eap_sm_request(sm
, WPA_CTRL_REQ_EXT_CERT_CHECK
,
2094 case TLS_CERT_CHAIN_FAILURE
:
2095 wpa_msg(sm
->msg_ctx
, MSG_INFO
, WPA_EVENT_EAP_TLS_CERT_ERROR
2096 "reason=%d depth=%d subject='%s' err='%s'",
2097 data
->cert_fail
.reason
,
2098 data
->cert_fail
.depth
,
2099 data
->cert_fail
.subject
,
2100 data
->cert_fail
.reason_txt
);
2101 eap_notify_status(sm
, "remote certificate verification",
2102 data
->cert_fail
.reason_txt
);
2104 case TLS_PEER_CERTIFICATE
:
2105 if (!sm
->eapol_cb
->notify_cert
)
2108 if (data
->peer_cert
.hash
) {
2109 size_t len
= data
->peer_cert
.hash_len
* 2 + 1;
2110 hash_hex
= os_malloc(len
);
2112 wpa_snprintf_hex(hash_hex
, len
,
2113 data
->peer_cert
.hash
,
2114 data
->peer_cert
.hash_len
);
2118 sm
->eapol_cb
->notify_cert(sm
->eapol_ctx
, &data
->peer_cert
,
2122 if (data
->alert
.is_local
)
2123 eap_notify_status(sm
, "local TLS alert",
2124 data
->alert
.description
);
2126 eap_notify_status(sm
, "remote TLS alert",
2127 data
->alert
.description
);
2136 * eap_peer_sm_init - Allocate and initialize EAP peer state machine
2137 * @eapol_ctx: Context data to be used with eapol_cb calls
2138 * @eapol_cb: Pointer to EAPOL callback functions
2139 * @msg_ctx: Context data for wpa_msg() calls
2140 * @conf: EAP configuration
2141 * Returns: Pointer to the allocated EAP state machine or %NULL on failure
2143 * This function allocates and initializes an EAP state machine. In addition,
2144 * this initializes TLS library for the new EAP state machine. eapol_cb pointer
2145 * will be in use until eap_peer_sm_deinit() is used to deinitialize this EAP
2146 * state machine. Consequently, the caller must make sure that this data
2147 * structure remains alive while the EAP state machine is active.
2149 struct eap_sm
* eap_peer_sm_init(void *eapol_ctx
,
2150 const struct eapol_callbacks
*eapol_cb
,
2151 void *msg_ctx
, struct eap_config
*conf
)
2154 struct tls_config tlsconf
;
2156 sm
= os_zalloc(sizeof(*sm
));
2159 sm
->eapol_ctx
= eapol_ctx
;
2160 sm
->eapol_cb
= eapol_cb
;
2161 sm
->msg_ctx
= msg_ctx
;
2162 sm
->ClientTimeout
= EAP_CLIENT_TIMEOUT_DEFAULT
;
2163 sm
->wps
= conf
->wps
;
2164 dl_list_init(&sm
->erp_keys
);
2166 os_memset(&tlsconf
, 0, sizeof(tlsconf
));
2167 tlsconf
.opensc_engine_path
= conf
->opensc_engine_path
;
2168 tlsconf
.pkcs11_engine_path
= conf
->pkcs11_engine_path
;
2169 tlsconf
.pkcs11_module_path
= conf
->pkcs11_module_path
;
2170 tlsconf
.openssl_ciphers
= conf
->openssl_ciphers
;
2172 tlsconf
.fips_mode
= 1;
2173 #endif /* CONFIG_FIPS */
2174 tlsconf
.event_cb
= eap_peer_sm_tls_event
;
2175 tlsconf
.cb_ctx
= sm
;
2176 tlsconf
.cert_in_cb
= conf
->cert_in_cb
;
2177 sm
->ssl_ctx
= tls_init(&tlsconf
);
2178 if (sm
->ssl_ctx
== NULL
) {
2179 wpa_printf(MSG_WARNING
, "SSL: Failed to initialize TLS "
2185 sm
->ssl_ctx2
= tls_init(&tlsconf
);
2186 if (sm
->ssl_ctx2
== NULL
) {
2187 wpa_printf(MSG_INFO
, "SSL: Failed to initialize TLS "
2189 /* Run without separate TLS context within TLS tunnel */
2197 * eap_peer_sm_deinit - Deinitialize and free an EAP peer state machine
2198 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2200 * This function deinitializes EAP state machine and frees all allocated
2203 void eap_peer_sm_deinit(struct eap_sm
*sm
)
2207 eap_deinit_prev_method(sm
, "EAP deinit");
2210 tls_deinit(sm
->ssl_ctx2
);
2211 tls_deinit(sm
->ssl_ctx
);
2212 eap_peer_erp_free_keys(sm
);
2218 * eap_peer_sm_step - Step EAP peer state machine
2219 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2220 * Returns: 1 if EAP state was changed or 0 if not
2222 * This function advances EAP state machine to a new state to match with the
2223 * current variables. This should be called whenever variables used by the EAP
2224 * state machine have changed.
2226 int eap_peer_sm_step(struct eap_sm
*sm
)
2230 sm
->changed
= FALSE
;
2234 } while (sm
->changed
);
2240 * eap_sm_abort - Abort EAP authentication
2241 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2243 * Release system resources that have been allocated for the authentication
2244 * session without fully deinitializing the EAP state machine.
2246 void eap_sm_abort(struct eap_sm
*sm
)
2248 wpabuf_free(sm
->lastRespData
);
2249 sm
->lastRespData
= NULL
;
2250 wpabuf_free(sm
->eapRespData
);
2251 sm
->eapRespData
= NULL
;
2252 eap_sm_free_key(sm
);
2253 os_free(sm
->eapSessionId
);
2254 sm
->eapSessionId
= NULL
;
2256 /* This is not clearly specified in the EAP statemachines draft, but
2257 * it seems necessary to make sure that some of the EAPOL variables get
2258 * cleared for the next authentication. */
2259 eapol_set_bool(sm
, EAPOL_eapSuccess
, FALSE
);
2263 #ifdef CONFIG_CTRL_IFACE
2264 static const char * eap_sm_state_txt(int state
)
2267 case EAP_INITIALIZE
:
2268 return "INITIALIZE";
2275 case EAP_GET_METHOD
:
2276 return "GET_METHOD";
2279 case EAP_SEND_RESPONSE
:
2280 return "SEND_RESPONSE";
2285 case EAP_NOTIFICATION
:
2286 return "NOTIFICATION";
2287 case EAP_RETRANSMIT
:
2288 return "RETRANSMIT";
2297 #endif /* CONFIG_CTRL_IFACE */
2300 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
2301 static const char * eap_sm_method_state_txt(EapMethodState state
)
2310 case METHOD_MAY_CONT
:
2320 static const char * eap_sm_decision_txt(EapDecision decision
)
2325 case DECISION_COND_SUCC
:
2327 case DECISION_UNCOND_SUCC
:
2328 return "UNCOND_SUCC";
2333 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
2336 #ifdef CONFIG_CTRL_IFACE
2339 * eap_sm_get_status - Get EAP state machine status
2340 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2341 * @buf: Buffer for status information
2342 * @buflen: Maximum buffer length
2343 * @verbose: Whether to include verbose status information
2344 * Returns: Number of bytes written to buf.
2346 * Query EAP state machine for status information. This function fills in a
2347 * text area with current status information from the EAPOL state machine. If
2348 * the buffer (buf) is not large enough, status information will be truncated
2349 * to fit the buffer.
2351 int eap_sm_get_status(struct eap_sm
*sm
, char *buf
, size_t buflen
, int verbose
)
2358 len
= os_snprintf(buf
, buflen
,
2360 eap_sm_state_txt(sm
->EAP_state
));
2361 if (os_snprintf_error(buflen
, len
))
2364 if (sm
->selectedMethod
!= EAP_TYPE_NONE
) {
2369 const struct eap_method
*m
=
2370 eap_peer_get_eap_method(EAP_VENDOR_IETF
,
2371 sm
->selectedMethod
);
2377 ret
= os_snprintf(buf
+ len
, buflen
- len
,
2378 "selectedMethod=%d (EAP-%s)\n",
2379 sm
->selectedMethod
, name
);
2380 if (os_snprintf_error(buflen
- len
, ret
))
2384 if (sm
->m
&& sm
->m
->get_status
) {
2385 len
+= sm
->m
->get_status(sm
, sm
->eap_method_priv
,
2386 buf
+ len
, buflen
- len
,
2392 ret
= os_snprintf(buf
+ len
, buflen
- len
,
2396 "ClientTimeout=%d\n",
2398 eap_sm_method_state_txt(sm
->methodState
),
2399 eap_sm_decision_txt(sm
->decision
),
2401 if (os_snprintf_error(buflen
- len
, ret
))
2408 #endif /* CONFIG_CTRL_IFACE */
2411 static void eap_sm_request(struct eap_sm
*sm
, enum wpa_ctrl_req_type field
,
2412 const char *msg
, size_t msglen
)
2414 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
2415 struct eap_peer_config
*config
;
2416 const char *txt
= NULL
;
2421 config
= eap_get_config(sm
);
2426 case WPA_CTRL_REQ_EAP_IDENTITY
:
2427 config
->pending_req_identity
++;
2429 case WPA_CTRL_REQ_EAP_PASSWORD
:
2430 config
->pending_req_password
++;
2432 case WPA_CTRL_REQ_EAP_NEW_PASSWORD
:
2433 config
->pending_req_new_password
++;
2435 case WPA_CTRL_REQ_EAP_PIN
:
2436 config
->pending_req_pin
++;
2438 case WPA_CTRL_REQ_EAP_OTP
:
2440 tmp
= os_malloc(msglen
+ 3);
2444 os_memcpy(tmp
+ 1, msg
, msglen
);
2445 tmp
[msglen
+ 1] = ']';
2446 tmp
[msglen
+ 2] = '\0';
2448 os_free(config
->pending_req_otp
);
2449 config
->pending_req_otp
= tmp
;
2450 config
->pending_req_otp_len
= msglen
+ 3;
2452 if (config
->pending_req_otp
== NULL
)
2454 txt
= config
->pending_req_otp
;
2457 case WPA_CTRL_REQ_EAP_PASSPHRASE
:
2458 config
->pending_req_passphrase
++;
2460 case WPA_CTRL_REQ_SIM
:
2461 config
->pending_req_sim
++;
2464 case WPA_CTRL_REQ_EXT_CERT_CHECK
:
2470 if (sm
->eapol_cb
->eap_param_needed
)
2471 sm
->eapol_cb
->eap_param_needed(sm
->eapol_ctx
, field
, txt
);
2472 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
2476 const char * eap_sm_get_method_name(struct eap_sm
*sm
)
2485 * eap_sm_request_identity - Request identity from user (ctrl_iface)
2486 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2488 * EAP methods can call this function to request identity information for the
2489 * current network. This is normally called when the identity is not included
2490 * in the network configuration. The request will be sent to monitor programs
2491 * through the control interface.
2493 void eap_sm_request_identity(struct eap_sm
*sm
)
2495 eap_sm_request(sm
, WPA_CTRL_REQ_EAP_IDENTITY
, NULL
, 0);
2500 * eap_sm_request_password - Request password from user (ctrl_iface)
2501 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2503 * EAP methods can call this function to request password information for the
2504 * current network. This is normally called when the password is not included
2505 * in the network configuration. The request will be sent to monitor programs
2506 * through the control interface.
2508 void eap_sm_request_password(struct eap_sm
*sm
)
2510 eap_sm_request(sm
, WPA_CTRL_REQ_EAP_PASSWORD
, NULL
, 0);
2515 * eap_sm_request_new_password - Request new password from user (ctrl_iface)
2516 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2518 * EAP methods can call this function to request new password information for
2519 * the current network. This is normally called when the EAP method indicates
2520 * that the current password has expired and password change is required. The
2521 * request will be sent to monitor programs through the control interface.
2523 void eap_sm_request_new_password(struct eap_sm
*sm
)
2525 eap_sm_request(sm
, WPA_CTRL_REQ_EAP_NEW_PASSWORD
, NULL
, 0);
2530 * eap_sm_request_pin - Request SIM or smart card PIN from user (ctrl_iface)
2531 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2533 * EAP methods can call this function to request SIM or smart card PIN
2534 * information for the current network. This is normally called when the PIN is
2535 * not included in the network configuration. The request will be sent to
2536 * monitor programs through the control interface.
2538 void eap_sm_request_pin(struct eap_sm
*sm
)
2540 eap_sm_request(sm
, WPA_CTRL_REQ_EAP_PIN
, NULL
, 0);
2545 * eap_sm_request_otp - Request one time password from user (ctrl_iface)
2546 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2547 * @msg: Message to be displayed to the user when asking for OTP
2548 * @msg_len: Length of the user displayable message
2550 * EAP methods can call this function to request open time password (OTP) for
2551 * the current network. The request will be sent to monitor programs through
2552 * the control interface.
2554 void eap_sm_request_otp(struct eap_sm
*sm
, const char *msg
, size_t msg_len
)
2556 eap_sm_request(sm
, WPA_CTRL_REQ_EAP_OTP
, msg
, msg_len
);
2561 * eap_sm_request_passphrase - Request passphrase from user (ctrl_iface)
2562 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2564 * EAP methods can call this function to request passphrase for a private key
2565 * for the current network. This is normally called when the passphrase is not
2566 * included in the network configuration. The request will be sent to monitor
2567 * programs through the control interface.
2569 void eap_sm_request_passphrase(struct eap_sm
*sm
)
2571 eap_sm_request(sm
, WPA_CTRL_REQ_EAP_PASSPHRASE
, NULL
, 0);
2576 * eap_sm_request_sim - Request external SIM processing
2577 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2578 * @req: EAP method specific request
2580 void eap_sm_request_sim(struct eap_sm
*sm
, const char *req
)
2582 eap_sm_request(sm
, WPA_CTRL_REQ_SIM
, req
, os_strlen(req
));
2587 * eap_sm_notify_ctrl_attached - Notification of attached monitor
2588 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2590 * Notify EAP state machines that a monitor was attached to the control
2591 * interface to trigger re-sending of pending requests for user input.
2593 void eap_sm_notify_ctrl_attached(struct eap_sm
*sm
)
2595 struct eap_peer_config
*config
= eap_get_config(sm
);
2600 /* Re-send any pending requests for user data since a new control
2601 * interface was added. This handles cases where the EAP authentication
2602 * starts immediately after system startup when the user interface is
2603 * not yet running. */
2604 if (config
->pending_req_identity
)
2605 eap_sm_request_identity(sm
);
2606 if (config
->pending_req_password
)
2607 eap_sm_request_password(sm
);
2608 if (config
->pending_req_new_password
)
2609 eap_sm_request_new_password(sm
);
2610 if (config
->pending_req_otp
)
2611 eap_sm_request_otp(sm
, NULL
, 0);
2612 if (config
->pending_req_pin
)
2613 eap_sm_request_pin(sm
);
2614 if (config
->pending_req_passphrase
)
2615 eap_sm_request_passphrase(sm
);
2619 static int eap_allowed_phase2_type(int vendor
, int type
)
2621 if (vendor
== EAP_VENDOR_HOSTAP
)
2623 if (vendor
!= EAP_VENDOR_IETF
)
2625 return type
!= EAP_TYPE_PEAP
&& type
!= EAP_TYPE_TTLS
&&
2626 type
!= EAP_TYPE_FAST
&& type
!= EAP_TYPE_TEAP
;
2631 * eap_get_phase2_type - Get EAP type for the given EAP phase 2 method name
2632 * @name: EAP method name, e.g., MD5
2633 * @vendor: Buffer for returning EAP Vendor-Id
2634 * Returns: EAP method type or %EAP_TYPE_NONE if not found
2636 * This function maps EAP type names into EAP type numbers that are allowed for
2637 * Phase 2, i.e., for tunneled authentication. Phase 2 is used, e.g., with
2638 * EAP-PEAP, EAP-TTLS, and EAP-FAST.
2640 u32
eap_get_phase2_type(const char *name
, int *vendor
)
2643 u32 type
= eap_peer_get_type(name
, &v
);
2644 if (eap_allowed_phase2_type(v
, type
)) {
2648 *vendor
= EAP_VENDOR_IETF
;
2649 return EAP_TYPE_NONE
;
2654 * eap_get_phase2_types - Get list of allowed EAP phase 2 types
2655 * @config: Pointer to a network configuration
2656 * @count: Pointer to a variable to be filled with number of returned EAP types
2657 * Returns: Pointer to allocated type list or %NULL on failure
2659 * This function generates an array of allowed EAP phase 2 (tunneled) types for
2660 * the given network configuration.
2662 struct eap_method_type
* eap_get_phase2_types(struct eap_peer_config
*config
,
2665 struct eap_method_type
*buf
;
2669 const struct eap_method
*methods
, *m
;
2671 methods
= eap_peer_get_methods(&mcount
);
2672 if (methods
== NULL
)
2675 buf
= os_malloc(mcount
* sizeof(struct eap_method_type
));
2679 for (m
= methods
; m
; m
= m
->next
) {
2682 if (eap_allowed_phase2_type(vendor
, method
)) {
2683 if (vendor
== EAP_VENDOR_IETF
&&
2684 method
== EAP_TYPE_TLS
&& config
&&
2685 config
->private_key2
== NULL
)
2687 buf
[*count
].vendor
= vendor
;
2688 buf
[*count
].method
= method
;
2698 * eap_set_fast_reauth - Update fast_reauth setting
2699 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2700 * @enabled: 1 = Fast reauthentication is enabled, 0 = Disabled
2702 void eap_set_fast_reauth(struct eap_sm
*sm
, int enabled
)
2704 sm
->fast_reauth
= enabled
;
2709 * eap_set_workaround - Update EAP workarounds setting
2710 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2711 * @workaround: 1 = Enable EAP workarounds, 0 = Disable EAP workarounds
2713 void eap_set_workaround(struct eap_sm
*sm
, unsigned int workaround
)
2715 sm
->workaround
= workaround
;
2720 * eap_get_config - Get current network configuration
2721 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2722 * Returns: Pointer to the current network configuration or %NULL if not found
2724 * EAP peer methods should avoid using this function if they can use other
2725 * access functions, like eap_get_config_identity() and
2726 * eap_get_config_password(), that do not require direct access to
2727 * struct eap_peer_config.
2729 struct eap_peer_config
* eap_get_config(struct eap_sm
*sm
)
2731 return sm
->eapol_cb
->get_config(sm
->eapol_ctx
);
2736 * eap_get_config_identity - Get identity from the network configuration
2737 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2738 * @len: Buffer for the length of the identity
2739 * Returns: Pointer to the identity or %NULL if not found
2741 const u8
* eap_get_config_identity(struct eap_sm
*sm
, size_t *len
)
2743 struct eap_peer_config
*config
= eap_get_config(sm
);
2746 *len
= config
->identity_len
;
2747 return config
->identity
;
2751 static int eap_get_ext_password(struct eap_sm
*sm
,
2752 struct eap_peer_config
*config
)
2756 if (config
->password
== NULL
)
2759 name
= os_zalloc(config
->password_len
+ 1);
2762 os_memcpy(name
, config
->password
, config
->password_len
);
2764 ext_password_free(sm
->ext_pw_buf
);
2765 sm
->ext_pw_buf
= ext_password_get(sm
->ext_pw
, name
);
2768 return sm
->ext_pw_buf
== NULL
? -1 : 0;
2773 * eap_get_config_password - Get password from the network configuration
2774 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2775 * @len: Buffer for the length of the password
2776 * Returns: Pointer to the password or %NULL if not found
2778 const u8
* eap_get_config_password(struct eap_sm
*sm
, size_t *len
)
2780 struct eap_peer_config
*config
= eap_get_config(sm
);
2784 if (config
->flags
& EAP_CONFIG_FLAGS_EXT_PASSWORD
) {
2785 if (eap_get_ext_password(sm
, config
) < 0)
2787 *len
= wpabuf_len(sm
->ext_pw_buf
);
2788 return wpabuf_head(sm
->ext_pw_buf
);
2791 *len
= config
->password_len
;
2792 return config
->password
;
2797 * eap_get_config_password2 - Get password from the network configuration
2798 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2799 * @len: Buffer for the length of the password
2800 * @hash: Buffer for returning whether the password is stored as a
2801 * NtPasswordHash instead of plaintext password; can be %NULL if this
2802 * information is not needed
2803 * Returns: Pointer to the password or %NULL if not found
2805 const u8
* eap_get_config_password2(struct eap_sm
*sm
, size_t *len
, int *hash
)
2807 struct eap_peer_config
*config
= eap_get_config(sm
);
2811 if (config
->flags
& EAP_CONFIG_FLAGS_EXT_PASSWORD
) {
2812 if (eap_get_ext_password(sm
, config
) < 0)
2816 *len
= wpabuf_len(sm
->ext_pw_buf
);
2817 return wpabuf_head(sm
->ext_pw_buf
);
2820 *len
= config
->password_len
;
2822 *hash
= !!(config
->flags
& EAP_CONFIG_FLAGS_PASSWORD_NTHASH
);
2823 return config
->password
;
2828 * eap_get_config_new_password - Get new password from network configuration
2829 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2830 * @len: Buffer for the length of the new password
2831 * Returns: Pointer to the new password or %NULL if not found
2833 const u8
* eap_get_config_new_password(struct eap_sm
*sm
, size_t *len
)
2835 struct eap_peer_config
*config
= eap_get_config(sm
);
2838 *len
= config
->new_password_len
;
2839 return config
->new_password
;
2844 * eap_get_config_otp - Get one-time password from the network configuration
2845 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2846 * @len: Buffer for the length of the one-time password
2847 * Returns: Pointer to the one-time password or %NULL if not found
2849 const u8
* eap_get_config_otp(struct eap_sm
*sm
, size_t *len
)
2851 struct eap_peer_config
*config
= eap_get_config(sm
);
2854 *len
= config
->otp_len
;
2860 * eap_clear_config_otp - Clear used one-time password
2861 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2863 * This function clears a used one-time password (OTP) from the current network
2864 * configuration. This should be called when the OTP has been used and is not
2867 void eap_clear_config_otp(struct eap_sm
*sm
)
2869 struct eap_peer_config
*config
= eap_get_config(sm
);
2872 os_memset(config
->otp
, 0, config
->otp_len
);
2873 os_free(config
->otp
);
2875 config
->otp_len
= 0;
2880 * eap_get_config_phase1 - Get phase1 data from the network configuration
2881 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2882 * Returns: Pointer to the phase1 data or %NULL if not found
2884 const char * eap_get_config_phase1(struct eap_sm
*sm
)
2886 struct eap_peer_config
*config
= eap_get_config(sm
);
2889 return config
->phase1
;
2894 * eap_get_config_phase2 - Get phase2 data from the network configuration
2895 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2896 * Returns: Pointer to the phase1 data or %NULL if not found
2898 const char * eap_get_config_phase2(struct eap_sm
*sm
)
2900 struct eap_peer_config
*config
= eap_get_config(sm
);
2903 return config
->phase2
;
2907 int eap_get_config_fragment_size(struct eap_sm
*sm
)
2909 struct eap_peer_config
*config
= eap_get_config(sm
);
2912 return config
->fragment_size
;
2917 * eap_key_available - Get key availability (eapKeyAvailable variable)
2918 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2919 * Returns: 1 if EAP keying material is available, 0 if not
2921 int eap_key_available(struct eap_sm
*sm
)
2923 return sm
? sm
->eapKeyAvailable
: 0;
2928 * eap_notify_success - Notify EAP state machine about external success trigger
2929 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2931 * This function is called when external event, e.g., successful completion of
2932 * WPA-PSK key handshake, is indicating that EAP state machine should move to
2933 * success state. This is mainly used with security modes that do not use EAP
2934 * state machine (e.g., WPA-PSK).
2936 void eap_notify_success(struct eap_sm
*sm
)
2939 sm
->decision
= DECISION_COND_SUCC
;
2940 sm
->EAP_state
= EAP_SUCCESS
;
2946 * eap_notify_lower_layer_success - Notification of lower layer success
2947 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2949 * Notify EAP state machines that a lower layer has detected a successful
2950 * authentication. This is used to recover from dropped EAP-Success messages.
2952 void eap_notify_lower_layer_success(struct eap_sm
*sm
)
2957 if (eapol_get_bool(sm
, EAPOL_eapSuccess
) ||
2958 sm
->decision
== DECISION_FAIL
||
2959 (sm
->methodState
!= METHOD_MAY_CONT
&&
2960 sm
->methodState
!= METHOD_DONE
))
2963 if (sm
->eapKeyData
!= NULL
)
2964 sm
->eapKeyAvailable
= TRUE
;
2965 eapol_set_bool(sm
, EAPOL_eapSuccess
, TRUE
);
2966 wpa_msg(sm
->msg_ctx
, MSG_INFO
, WPA_EVENT_EAP_SUCCESS
2967 "EAP authentication completed successfully (based on lower "
2973 * eap_get_eapSessionId - Get Session-Id from EAP state machine
2974 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2975 * @len: Pointer to variable that will be set to number of bytes in the session
2976 * Returns: Pointer to the EAP Session-Id or %NULL on failure
2978 * Fetch EAP Session-Id from the EAP state machine. The Session-Id is available
2979 * only after a successful authentication. EAP state machine continues to manage
2980 * the Session-Id and the caller must not change or free the returned data.
2982 const u8
* eap_get_eapSessionId(struct eap_sm
*sm
, size_t *len
)
2984 if (sm
== NULL
|| sm
->eapSessionId
== NULL
) {
2989 *len
= sm
->eapSessionIdLen
;
2990 return sm
->eapSessionId
;
2995 * eap_get_eapKeyData - Get master session key (MSK) from EAP state machine
2996 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
2997 * @len: Pointer to variable that will be set to number of bytes in the key
2998 * Returns: Pointer to the EAP keying data or %NULL on failure
3000 * Fetch EAP keying material (MSK, eapKeyData) from the EAP state machine. The
3001 * key is available only after a successful authentication. EAP state machine
3002 * continues to manage the key data and the caller must not change or free the
3005 const u8
* eap_get_eapKeyData(struct eap_sm
*sm
, size_t *len
)
3007 if (sm
== NULL
|| sm
->eapKeyData
== NULL
) {
3012 *len
= sm
->eapKeyDataLen
;
3013 return sm
->eapKeyData
;
3018 * eap_get_eapKeyData - Get EAP response data
3019 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3020 * Returns: Pointer to the EAP response (eapRespData) or %NULL on failure
3022 * Fetch EAP response (eapRespData) from the EAP state machine. This data is
3023 * available when EAP state machine has processed an incoming EAP request. The
3024 * EAP state machine does not maintain a reference to the response after this
3025 * function is called and the caller is responsible for freeing the data.
3027 struct wpabuf
* eap_get_eapRespData(struct eap_sm
*sm
)
3029 struct wpabuf
*resp
;
3031 if (sm
== NULL
|| sm
->eapRespData
== NULL
)
3034 resp
= sm
->eapRespData
;
3035 sm
->eapRespData
= NULL
;
3042 * eap_sm_register_scard_ctx - Notification of smart card context
3043 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3044 * @ctx: Context data for smart card operations
3046 * Notify EAP state machines of context data for smart card operations. This
3047 * context data will be used as a parameter for scard_*() functions.
3049 void eap_register_scard_ctx(struct eap_sm
*sm
, void *ctx
)
3052 sm
->scard_ctx
= ctx
;
3057 * eap_set_config_blob - Set or add a named configuration blob
3058 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3059 * @blob: New value for the blob
3061 * Adds a new configuration blob or replaces the current value of an existing
3064 void eap_set_config_blob(struct eap_sm
*sm
, struct wpa_config_blob
*blob
)
3066 #ifndef CONFIG_NO_CONFIG_BLOBS
3067 sm
->eapol_cb
->set_config_blob(sm
->eapol_ctx
, blob
);
3068 #endif /* CONFIG_NO_CONFIG_BLOBS */
3073 * eap_get_config_blob - Get a named configuration blob
3074 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3075 * @name: Name of the blob
3076 * Returns: Pointer to blob data or %NULL if not found
3078 const struct wpa_config_blob
* eap_get_config_blob(struct eap_sm
*sm
,
3081 #ifndef CONFIG_NO_CONFIG_BLOBS
3082 return sm
->eapol_cb
->get_config_blob(sm
->eapol_ctx
, name
);
3083 #else /* CONFIG_NO_CONFIG_BLOBS */
3085 #endif /* CONFIG_NO_CONFIG_BLOBS */
3090 * eap_set_force_disabled - Set force_disabled flag
3091 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3092 * @disabled: 1 = EAP disabled, 0 = EAP enabled
3094 * This function is used to force EAP state machine to be disabled when it is
3095 * not in use (e.g., with WPA-PSK or plaintext connections).
3097 void eap_set_force_disabled(struct eap_sm
*sm
, int disabled
)
3099 sm
->force_disabled
= disabled
;
3104 * eap_set_external_sim - Set external_sim flag
3105 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3106 * @external_sim: Whether external SIM/USIM processing is used
3108 void eap_set_external_sim(struct eap_sm
*sm
, int external_sim
)
3110 sm
->external_sim
= external_sim
;
3115 * eap_notify_pending - Notify that EAP method is ready to re-process a request
3116 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3118 * An EAP method can perform a pending operation (e.g., to get a response from
3119 * an external process). Once the response is available, this function can be
3120 * used to request EAPOL state machine to retry delivering the previously
3121 * received (and still unanswered) EAP request to EAP state machine.
3123 void eap_notify_pending(struct eap_sm
*sm
)
3125 sm
->eapol_cb
->notify_pending(sm
->eapol_ctx
);
3130 * eap_invalidate_cached_session - Mark cached session data invalid
3131 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3133 void eap_invalidate_cached_session(struct eap_sm
*sm
)
3136 eap_deinit_prev_method(sm
, "invalidate");
3140 int eap_is_wps_pbc_enrollee(struct eap_peer_config
*conf
)
3142 if (conf
->identity_len
!= WSC_ID_ENROLLEE_LEN
||
3143 os_memcmp(conf
->identity
, WSC_ID_ENROLLEE
, WSC_ID_ENROLLEE_LEN
))
3144 return 0; /* Not a WPS Enrollee */
3146 if (conf
->phase1
== NULL
|| os_strstr(conf
->phase1
, "pbc=1") == NULL
)
3147 return 0; /* Not using PBC */
3153 int eap_is_wps_pin_enrollee(struct eap_peer_config
*conf
)
3155 if (conf
->identity_len
!= WSC_ID_ENROLLEE_LEN
||
3156 os_memcmp(conf
->identity
, WSC_ID_ENROLLEE
, WSC_ID_ENROLLEE_LEN
))
3157 return 0; /* Not a WPS Enrollee */
3159 if (conf
->phase1
== NULL
|| os_strstr(conf
->phase1
, "pin=") == NULL
)
3160 return 0; /* Not using PIN */
3166 void eap_sm_set_ext_pw_ctx(struct eap_sm
*sm
, struct ext_password_data
*ext
)
3168 ext_password_free(sm
->ext_pw_buf
);
3169 sm
->ext_pw_buf
= NULL
;
3175 * eap_set_anon_id - Set or add anonymous identity
3176 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
3177 * @id: Anonymous identity (e.g., EAP-SIM pseudonym) or %NULL to clear
3178 * @len: Length of anonymous identity in octets
3180 void eap_set_anon_id(struct eap_sm
*sm
, const u8
*id
, size_t len
)
3182 if (sm
->eapol_cb
->set_anon_id
)
3183 sm
->eapol_cb
->set_anon_id(sm
->eapol_ctx
, id
, len
);
3187 int eap_peer_was_failure_expected(struct eap_sm
*sm
)
3189 return sm
->expected_failure
;