2 * WPA Supplicant - WPA state machine and EAPOL-Key processing
3 * Copyright (c) 2003-2015, Jouni Malinen <j@w1.fi>
4 * Copyright(c) 2015 Intel Deutschland GmbH
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
13 #include "crypto/aes.h"
14 #include "crypto/aes_wrap.h"
15 #include "crypto/crypto.h"
16 #include "crypto/random.h"
17 #include "crypto/aes_siv.h"
18 #include "common/ieee802_11_defs.h"
19 #include "common/ieee802_11_common.h"
20 #include "eap_common/eap_defs.h"
21 #include "eapol_supp/eapol_supp_sm.h"
25 #include "pmksa_cache.h"
31 static const u8 null_rsc
[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
35 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
36 * @sm: Pointer to WPA state machine data from wpa_sm_init()
37 * @ptk: PTK for Key Confirmation/Encryption Key
38 * @ver: Version field from Key Info
39 * @dest: Destination address for the frame
40 * @proto: Ethertype (usually ETH_P_EAPOL)
41 * @msg: EAPOL-Key message
42 * @msg_len: Length of message
43 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
44 * Returns: >= 0 on success, < 0 on failure
46 int wpa_eapol_key_send(struct wpa_sm
*sm
, struct wpa_ptk
*ptk
,
47 int ver
, const u8
*dest
, u16 proto
,
48 u8
*msg
, size_t msg_len
, u8
*key_mic
)
51 size_t mic_len
= wpa_mic_len(sm
->key_mgmt
);
53 if (is_zero_ether_addr(dest
) && is_zero_ether_addr(sm
->bssid
)) {
55 * Association event was not yet received; try to fetch
56 * BSSID from the driver.
58 if (wpa_sm_get_bssid(sm
, sm
->bssid
) < 0) {
59 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
60 "WPA: Failed to read BSSID for "
61 "EAPOL-Key destination address");
64 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
65 "WPA: Use BSSID (" MACSTR
66 ") as the destination for EAPOL-Key",
72 if (key_mic
&& (!ptk
|| !ptk
->kck_len
))
76 wpa_eapol_key_mic(ptk
->kck
, ptk
->kck_len
, sm
->key_mgmt
, ver
,
77 msg
, msg_len
, key_mic
)) {
78 wpa_msg(sm
->ctx
->msg_ctx
, MSG_ERROR
,
79 "WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC",
84 wpa_hexdump_key(MSG_DEBUG
, "WPA: KCK",
85 ptk
->kck
, ptk
->kck_len
);
86 wpa_hexdump(MSG_DEBUG
, "WPA: Derived Key MIC",
90 /* AEAD cipher - Key MIC field not used */
91 struct ieee802_1x_hdr
*s_hdr
, *hdr
;
92 struct wpa_eapol_key
*s_key
, *key
;
93 u8
*buf
, *s_key_data
, *key_data
;
94 size_t buf_len
= msg_len
+ AES_BLOCK_SIZE
;
100 if (!ptk
|| !ptk
->kek_len
)
103 key_data_len
= msg_len
- sizeof(struct ieee802_1x_hdr
) -
104 sizeof(struct wpa_eapol_key
) - 2;
106 buf
= os_malloc(buf_len
);
110 os_memcpy(buf
, msg
, msg_len
);
111 hdr
= (struct ieee802_1x_hdr
*) buf
;
112 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
113 key_data
= ((u8
*) (key
+ 1)) + 2;
115 /* Update EAPOL header to include AES-SIV overhead */
116 eapol_len
= be_to_host16(hdr
->length
);
117 eapol_len
+= AES_BLOCK_SIZE
;
118 hdr
->length
= host_to_be16(eapol_len
);
120 /* Update Key Data Length field to include AES-SIV overhead */
121 WPA_PUT_BE16((u8
*) (key
+ 1), AES_BLOCK_SIZE
+ key_data_len
);
123 s_hdr
= (struct ieee802_1x_hdr
*) msg
;
124 s_key
= (struct wpa_eapol_key
*) (s_hdr
+ 1);
125 s_key_data
= ((u8
*) (s_key
+ 1)) + 2;
127 wpa_hexdump_key(MSG_DEBUG
, "WPA: Plaintext Key Data",
128 s_key_data
, key_data_len
);
130 wpa_hexdump_key(MSG_DEBUG
, "WPA: KEK", ptk
->kek
, ptk
->kek_len
);
131 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
132 * to Key Data (exclusive). */
134 aad_len
[0] = key_data
- buf
;
135 if (aes_siv_encrypt(ptk
->kek
, ptk
->kek_len
,
136 s_key_data
, key_data_len
,
137 1, aad
, aad_len
, key_data
) < 0) {
142 wpa_hexdump(MSG_DEBUG
, "WPA: Encrypted Key Data from SIV",
143 key_data
, AES_BLOCK_SIZE
+ key_data_len
);
148 #else /* CONFIG_FILS */
150 #endif /* CONFIG_FILS */
153 wpa_hexdump(MSG_MSGDUMP
, "WPA: TX EAPOL-Key", msg
, msg_len
);
154 ret
= wpa_sm_ether_send(sm
, dest
, proto
, msg
, msg_len
);
155 eapol_sm_notify_tx_eapol_key(sm
->eapol
);
163 * wpa_sm_key_request - Send EAPOL-Key Request
164 * @sm: Pointer to WPA state machine data from wpa_sm_init()
165 * @error: Indicate whether this is an Michael MIC error report
166 * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
168 * Send an EAPOL-Key Request to the current authenticator. This function is
169 * used to request rekeying and it is usually called when a local Michael MIC
170 * failure is detected.
172 void wpa_sm_key_request(struct wpa_sm
*sm
, int error
, int pairwise
)
174 size_t mic_len
, hdrlen
, rlen
;
175 struct wpa_eapol_key
*reply
;
177 u8 bssid
[ETH_ALEN
], *rbuf
, *key_mic
, *mic
;
179 if (sm
->key_mgmt
== WPA_KEY_MGMT_OSEN
||
180 wpa_key_mgmt_suite_b(sm
->key_mgmt
))
181 ver
= WPA_KEY_INFO_TYPE_AKM_DEFINED
;
182 else if (wpa_key_mgmt_ft(sm
->key_mgmt
) ||
183 wpa_key_mgmt_sha256(sm
->key_mgmt
))
184 ver
= WPA_KEY_INFO_TYPE_AES_128_CMAC
;
185 else if (sm
->pairwise_cipher
!= WPA_CIPHER_TKIP
)
186 ver
= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
;
188 ver
= WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
;
190 if (wpa_sm_get_bssid(sm
, bssid
) < 0) {
191 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
192 "Failed to read BSSID for EAPOL-Key request");
196 mic_len
= wpa_mic_len(sm
->key_mgmt
);
197 hdrlen
= sizeof(*reply
) + mic_len
+ 2;
198 rbuf
= wpa_sm_alloc_eapol(sm
, IEEE802_1X_TYPE_EAPOL_KEY
, NULL
,
199 hdrlen
, &rlen
, (void *) &reply
);
203 reply
->type
= (sm
->proto
== WPA_PROTO_RSN
||
204 sm
->proto
== WPA_PROTO_OSEN
) ?
205 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
206 key_info
= WPA_KEY_INFO_REQUEST
| ver
;
208 key_info
|= WPA_KEY_INFO_SECURE
;
209 if (sm
->ptk_set
&& mic_len
)
210 key_info
|= WPA_KEY_INFO_MIC
;
212 key_info
|= WPA_KEY_INFO_ERROR
;
214 key_info
|= WPA_KEY_INFO_KEY_TYPE
;
215 WPA_PUT_BE16(reply
->key_info
, key_info
);
216 WPA_PUT_BE16(reply
->key_length
, 0);
217 os_memcpy(reply
->replay_counter
, sm
->request_counter
,
218 WPA_REPLAY_COUNTER_LEN
);
219 inc_byte_array(sm
->request_counter
, WPA_REPLAY_COUNTER_LEN
);
221 mic
= (u8
*) (reply
+ 1);
222 WPA_PUT_BE16(mic
+ mic_len
, 0);
223 if (!(key_info
& WPA_KEY_INFO_MIC
))
228 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
229 "WPA: Sending EAPOL-Key Request (error=%d "
230 "pairwise=%d ptk_set=%d len=%lu)",
231 error
, pairwise
, sm
->ptk_set
, (unsigned long) rlen
);
232 wpa_eapol_key_send(sm
, &sm
->ptk
, ver
, bssid
, ETH_P_EAPOL
, rbuf
, rlen
,
237 static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm
*sm
)
239 #ifdef CONFIG_IEEE80211R
240 if (sm
->key_mgmt
== WPA_KEY_MGMT_FT_IEEE8021X
) {
241 if (wpa_sm_key_mgmt_set_pmk(sm
, sm
->xxkey
, sm
->xxkey_len
))
242 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
243 "RSN: Cannot set low order 256 bits of MSK for key management offload");
245 #endif /* CONFIG_IEEE80211R */
246 if (wpa_sm_key_mgmt_set_pmk(sm
, sm
->pmk
, sm
->pmk_len
))
247 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
248 "RSN: Cannot set PMK for key management offload");
249 #ifdef CONFIG_IEEE80211R
251 #endif /* CONFIG_IEEE80211R */
255 static int wpa_supplicant_get_pmk(struct wpa_sm
*sm
,
256 const unsigned char *src_addr
,
259 int abort_cached
= 0;
261 if (pmkid
&& !sm
->cur_pmksa
) {
262 /* When using drivers that generate RSN IE, wpa_supplicant may
263 * not have enough time to get the association information
264 * event before receiving this 1/4 message, so try to find a
265 * matching PMKSA cache entry here. */
266 sm
->cur_pmksa
= pmksa_cache_get(sm
->pmksa
, src_addr
, pmkid
,
269 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
270 "RSN: found matching PMKID from PMKSA cache");
272 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
273 "RSN: no matching PMKID found");
278 if (pmkid
&& sm
->cur_pmksa
&&
279 os_memcmp_const(pmkid
, sm
->cur_pmksa
->pmkid
, PMKID_LEN
) == 0) {
280 wpa_hexdump(MSG_DEBUG
, "RSN: matched PMKID", pmkid
, PMKID_LEN
);
281 wpa_sm_set_pmk_from_pmksa(sm
);
282 wpa_hexdump_key(MSG_DEBUG
, "RSN: PMK from PMKSA cache",
283 sm
->pmk
, sm
->pmk_len
);
284 eapol_sm_notify_cached(sm
->eapol
);
285 #ifdef CONFIG_IEEE80211R
287 #endif /* CONFIG_IEEE80211R */
288 } else if (wpa_key_mgmt_wpa_ieee8021x(sm
->key_mgmt
) && sm
->eapol
) {
291 if (sm
->key_mgmt
& WPA_KEY_MGMT_IEEE8021X_SUITE_B_192
)
292 pmk_len
= PMK_LEN_SUITE_B_192
;
295 res
= eapol_sm_get_key(sm
->eapol
, sm
->pmk
, pmk_len
);
297 if (pmk_len
== PMK_LEN
) {
299 * EAP-LEAP is an exception from other EAP
300 * methods: it uses only 16-byte PMK.
302 res
= eapol_sm_get_key(sm
->eapol
, sm
->pmk
, 16);
306 #ifdef CONFIG_IEEE80211R
308 if (eapol_sm_get_key(sm
->eapol
, buf
, 2 * PMK_LEN
) == 0)
310 os_memcpy(sm
->xxkey
, buf
+ PMK_LEN
, PMK_LEN
);
311 sm
->xxkey_len
= PMK_LEN
;
312 os_memset(buf
, 0, sizeof(buf
));
314 #endif /* CONFIG_IEEE80211R */
317 struct rsn_pmksa_cache_entry
*sa
= NULL
;
318 wpa_hexdump_key(MSG_DEBUG
, "WPA: PMK from EAPOL state "
319 "machines", sm
->pmk
, pmk_len
);
320 sm
->pmk_len
= pmk_len
;
321 wpa_supplicant_key_mgmt_set_pmk(sm
);
322 if (sm
->proto
== WPA_PROTO_RSN
&&
323 !wpa_key_mgmt_suite_b(sm
->key_mgmt
) &&
324 !wpa_key_mgmt_ft(sm
->key_mgmt
)) {
325 sa
= pmksa_cache_add(sm
->pmksa
,
326 sm
->pmk
, pmk_len
, NULL
,
328 src_addr
, sm
->own_addr
,
332 if (!sm
->cur_pmksa
&& pmkid
&&
333 pmksa_cache_get(sm
->pmksa
, src_addr
, pmkid
, NULL
))
335 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
336 "RSN: the new PMK matches with the "
339 } else if (sa
&& !sm
->cur_pmksa
&& pmkid
) {
341 * It looks like the authentication server
342 * derived mismatching MSK. This should not
343 * really happen, but bugs happen.. There is not
344 * much we can do here without knowing what
345 * exactly caused the server to misbehave.
347 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
348 "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
355 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
356 "WPA: Failed to get master session key from "
357 "EAPOL state machines - key handshake "
360 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
361 "RSN: Cancelled PMKSA caching "
363 sm
->cur_pmksa
= NULL
;
365 } else if (!abort_cached
) {
371 if (abort_cached
&& wpa_key_mgmt_wpa_ieee8021x(sm
->key_mgmt
) &&
372 !wpa_key_mgmt_suite_b(sm
->key_mgmt
) &&
373 !wpa_key_mgmt_ft(sm
->key_mgmt
) && sm
->key_mgmt
!= WPA_KEY_MGMT_OSEN
)
375 /* Send EAPOL-Start to trigger full EAP authentication. */
379 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
380 "RSN: no PMKSA entry found - trigger "
381 "full EAP authentication");
382 buf
= wpa_sm_alloc_eapol(sm
, IEEE802_1X_TYPE_EAPOL_START
,
383 NULL
, 0, &buflen
, NULL
);
385 wpa_sm_ether_send(sm
, sm
->bssid
, ETH_P_EAPOL
,
399 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
400 * @sm: Pointer to WPA state machine data from wpa_sm_init()
401 * @dst: Destination address for the frame
402 * @key: Pointer to the EAPOL-Key frame header
403 * @ver: Version bits from EAPOL-Key Key Info
404 * @nonce: Nonce value for the EAPOL-Key frame
405 * @wpa_ie: WPA/RSN IE
406 * @wpa_ie_len: Length of the WPA/RSN IE
407 * @ptk: PTK to use for keyed hash and encryption
408 * Returns: >= 0 on success, < 0 on failure
410 int wpa_supplicant_send_2_of_4(struct wpa_sm
*sm
, const unsigned char *dst
,
411 const struct wpa_eapol_key
*key
,
412 int ver
, const u8
*nonce
,
413 const u8
*wpa_ie
, size_t wpa_ie_len
,
416 size_t mic_len
, hdrlen
, rlen
;
417 struct wpa_eapol_key
*reply
;
419 u8
*rsn_ie_buf
= NULL
;
422 if (wpa_ie
== NULL
) {
423 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
, "WPA: No wpa_ie set - "
424 "cannot generate msg 2/4");
428 #ifdef CONFIG_IEEE80211R
429 if (wpa_key_mgmt_ft(sm
->key_mgmt
)) {
433 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
434 * FTIE from (Re)Association Response.
436 rsn_ie_buf
= os_malloc(wpa_ie_len
+ 2 + 2 + PMKID_LEN
+
437 sm
->assoc_resp_ies_len
);
438 if (rsn_ie_buf
== NULL
)
440 os_memcpy(rsn_ie_buf
, wpa_ie
, wpa_ie_len
);
441 res
= wpa_insert_pmkid(rsn_ie_buf
, &wpa_ie_len
,
448 if (sm
->assoc_resp_ies
) {
449 os_memcpy(rsn_ie_buf
+ wpa_ie_len
, sm
->assoc_resp_ies
,
450 sm
->assoc_resp_ies_len
);
451 wpa_ie_len
+= sm
->assoc_resp_ies_len
;
456 #endif /* CONFIG_IEEE80211R */
458 wpa_hexdump(MSG_DEBUG
, "WPA: WPA IE for msg 2/4", wpa_ie
, wpa_ie_len
);
460 mic_len
= wpa_mic_len(sm
->key_mgmt
);
461 hdrlen
= sizeof(*reply
) + mic_len
+ 2;
462 rbuf
= wpa_sm_alloc_eapol(sm
, IEEE802_1X_TYPE_EAPOL_KEY
,
463 NULL
, hdrlen
+ wpa_ie_len
,
464 &rlen
, (void *) &reply
);
470 reply
->type
= (sm
->proto
== WPA_PROTO_RSN
||
471 sm
->proto
== WPA_PROTO_OSEN
) ?
472 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
473 key_info
= ver
| WPA_KEY_INFO_KEY_TYPE
;
475 key_info
|= WPA_KEY_INFO_MIC
;
477 key_info
|= WPA_KEY_INFO_ENCR_KEY_DATA
;
478 WPA_PUT_BE16(reply
->key_info
, key_info
);
479 if (sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
)
480 WPA_PUT_BE16(reply
->key_length
, 0);
482 os_memcpy(reply
->key_length
, key
->key_length
, 2);
483 os_memcpy(reply
->replay_counter
, key
->replay_counter
,
484 WPA_REPLAY_COUNTER_LEN
);
485 wpa_hexdump(MSG_DEBUG
, "WPA: Replay Counter", reply
->replay_counter
,
486 WPA_REPLAY_COUNTER_LEN
);
488 key_mic
= (u8
*) (reply
+ 1);
489 WPA_PUT_BE16(key_mic
+ mic_len
, wpa_ie_len
); /* Key Data Length */
490 os_memcpy(key_mic
+ mic_len
+ 2, wpa_ie
, wpa_ie_len
); /* Key Data */
493 os_memcpy(reply
->key_nonce
, nonce
, WPA_NONCE_LEN
);
495 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Sending EAPOL-Key 2/4");
496 return wpa_eapol_key_send(sm
, ptk
, ver
, dst
, ETH_P_EAPOL
, rbuf
, rlen
,
501 static int wpa_derive_ptk(struct wpa_sm
*sm
, const unsigned char *src_addr
,
502 const struct wpa_eapol_key
*key
, struct wpa_ptk
*ptk
)
504 #ifdef CONFIG_IEEE80211R
505 if (wpa_key_mgmt_ft(sm
->key_mgmt
))
506 return wpa_derive_ptk_ft(sm
, src_addr
, key
, ptk
);
507 #endif /* CONFIG_IEEE80211R */
509 return wpa_pmk_to_ptk(sm
->pmk
, sm
->pmk_len
, "Pairwise key expansion",
510 sm
->own_addr
, sm
->bssid
, sm
->snonce
,
511 key
->key_nonce
, ptk
, sm
->key_mgmt
,
512 sm
->pairwise_cipher
);
516 static void wpa_supplicant_process_1_of_4(struct wpa_sm
*sm
,
517 const unsigned char *src_addr
,
518 const struct wpa_eapol_key
*key
,
519 u16 ver
, const u8
*key_data
,
522 struct wpa_eapol_ie_parse ie
;
525 u8
*kde
, *kde_buf
= NULL
;
528 if (wpa_sm_get_network_ctx(sm
) == NULL
) {
529 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
, "WPA: No SSID info "
530 "found (msg 1 of 4)");
534 wpa_sm_set_state(sm
, WPA_4WAY_HANDSHAKE
);
535 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: RX message 1 of 4-Way "
536 "Handshake from " MACSTR
" (ver=%d)", MAC2STR(src_addr
), ver
);
538 os_memset(&ie
, 0, sizeof(ie
));
540 if (sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
) {
541 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
542 wpa_hexdump(MSG_DEBUG
, "RSN: msg 1/4 key data",
543 key_data
, key_data_len
);
544 if (wpa_supplicant_parse_ies(key_data
, key_data_len
, &ie
) < 0)
547 wpa_hexdump(MSG_DEBUG
, "RSN: PMKID from "
548 "Authenticator", ie
.pmkid
, PMKID_LEN
);
552 res
= wpa_supplicant_get_pmk(sm
, src_addr
, ie
.pmkid
);
554 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "RSN: Do not reply to "
555 "msg 1/4 - requesting full EAP authentication");
561 if (sm
->renew_snonce
) {
562 if (random_get_bytes(sm
->snonce
, WPA_NONCE_LEN
)) {
563 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
564 "WPA: Failed to get random data for SNonce");
567 sm
->renew_snonce
= 0;
568 wpa_hexdump(MSG_DEBUG
, "WPA: Renewed SNonce",
569 sm
->snonce
, WPA_NONCE_LEN
);
572 /* Calculate PTK which will be stored as a temporary PTK until it has
573 * been verified when processing message 3/4. */
575 wpa_derive_ptk(sm
, src_addr
, key
, ptk
);
576 if (sm
->pairwise_cipher
== WPA_CIPHER_TKIP
) {
578 /* Supplicant: swap tx/rx Mic keys */
579 os_memcpy(buf
, &ptk
->tk
[16], 8);
580 os_memcpy(&ptk
->tk
[16], &ptk
->tk
[24], 8);
581 os_memcpy(&ptk
->tk
[24], buf
, 8);
582 os_memset(buf
, 0, sizeof(buf
));
587 kde
= sm
->assoc_wpa_ie
;
588 kde_len
= sm
->assoc_wpa_ie_len
;
592 kde_buf
= os_malloc(kde_len
+ 2 + RSN_SELECTOR_LEN
+ 1);
595 wpa_printf(MSG_DEBUG
, "P2P: Add IP Address Request KDE "
596 "into EAPOL-Key 2/4");
597 os_memcpy(kde_buf
, kde
, kde_len
);
600 *pos
++ = WLAN_EID_VENDOR_SPECIFIC
;
601 *pos
++ = RSN_SELECTOR_LEN
+ 1;
602 RSN_SELECTOR_PUT(pos
, WFA_KEY_DATA_IP_ADDR_REQ
);
603 pos
+= RSN_SELECTOR_LEN
;
608 #endif /* CONFIG_P2P */
610 if (wpa_supplicant_send_2_of_4(sm
, sm
->bssid
, key
, ver
, sm
->snonce
,
611 kde
, kde_len
, ptk
) < 0)
615 os_memcpy(sm
->anonce
, key
->key_nonce
, WPA_NONCE_LEN
);
620 wpa_sm_deauthenticate(sm
, WLAN_REASON_UNSPECIFIED
);
624 static void wpa_sm_start_preauth(void *eloop_ctx
, void *timeout_ctx
)
626 struct wpa_sm
*sm
= eloop_ctx
;
627 rsn_preauth_candidate_process(sm
);
631 static void wpa_supplicant_key_neg_complete(struct wpa_sm
*sm
,
632 const u8
*addr
, int secure
)
634 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
635 "WPA: Key negotiation completed with "
636 MACSTR
" [PTK=%s GTK=%s]", MAC2STR(addr
),
637 wpa_cipher_txt(sm
->pairwise_cipher
),
638 wpa_cipher_txt(sm
->group_cipher
));
639 wpa_sm_cancel_auth_timeout(sm
);
640 wpa_sm_set_state(sm
, WPA_COMPLETED
);
643 wpa_sm_mlme_setprotection(
644 sm
, addr
, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX
,
645 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE
);
646 eapol_sm_notify_portValid(sm
->eapol
, TRUE
);
647 if (wpa_key_mgmt_wpa_psk(sm
->key_mgmt
))
648 eapol_sm_notify_eap_success(sm
->eapol
, TRUE
);
650 * Start preauthentication after a short wait to avoid a
651 * possible race condition between the data receive and key
652 * configuration after the 4-Way Handshake. This increases the
653 * likelihood of the first preauth EAPOL-Start frame getting to
656 eloop_register_timeout(1, 0, wpa_sm_start_preauth
, sm
, NULL
);
659 if (sm
->cur_pmksa
&& sm
->cur_pmksa
->opportunistic
) {
660 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
661 "RSN: Authenticator accepted "
662 "opportunistic PMKSA entry - marking it valid");
663 sm
->cur_pmksa
->opportunistic
= 0;
666 #ifdef CONFIG_IEEE80211R
667 if (wpa_key_mgmt_ft(sm
->key_mgmt
)) {
668 /* Prepare for the next transition */
669 wpa_ft_prepare_auth_request(sm
, NULL
);
671 #endif /* CONFIG_IEEE80211R */
675 static void wpa_sm_rekey_ptk(void *eloop_ctx
, void *timeout_ctx
)
677 struct wpa_sm
*sm
= eloop_ctx
;
678 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Request PTK rekeying");
679 wpa_sm_key_request(sm
, 0, 1);
683 static int wpa_supplicant_install_ptk(struct wpa_sm
*sm
,
684 const struct wpa_eapol_key
*key
)
690 if (!sm
->tk_to_set
) {
691 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
692 "WPA: Do not re-install same PTK to the driver");
696 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
697 "WPA: Installing PTK to the driver");
699 if (sm
->pairwise_cipher
== WPA_CIPHER_NONE
) {
700 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Pairwise Cipher "
701 "Suite: NONE - do not use pairwise keys");
705 if (!wpa_cipher_valid_pairwise(sm
->pairwise_cipher
)) {
706 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
707 "WPA: Unsupported pairwise cipher %d",
708 sm
->pairwise_cipher
);
712 alg
= wpa_cipher_to_alg(sm
->pairwise_cipher
);
713 keylen
= wpa_cipher_key_len(sm
->pairwise_cipher
);
714 rsclen
= wpa_cipher_rsc_len(sm
->pairwise_cipher
);
716 if (sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
) {
719 key_rsc
= key
->key_rsc
;
720 wpa_hexdump(MSG_DEBUG
, "WPA: RSC", key_rsc
, rsclen
);
723 if (wpa_sm_set_key(sm
, alg
, sm
->bssid
, 0, 1, key_rsc
, rsclen
,
724 sm
->ptk
.tk
, keylen
) < 0) {
725 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
726 "WPA: Failed to set PTK to the "
727 "driver (alg=%d keylen=%d bssid=" MACSTR
")",
728 alg
, keylen
, MAC2STR(sm
->bssid
));
732 /* TK is not needed anymore in supplicant */
733 os_memset(sm
->ptk
.tk
, 0, WPA_TK_MAX_LEN
);
736 if (sm
->wpa_ptk_rekey
) {
737 eloop_cancel_timeout(wpa_sm_rekey_ptk
, sm
, NULL
);
738 eloop_register_timeout(sm
->wpa_ptk_rekey
, 0, wpa_sm_rekey_ptk
,
746 static int wpa_supplicant_check_group_cipher(struct wpa_sm
*sm
,
748 int keylen
, int maxkeylen
,
754 *alg
= wpa_cipher_to_alg(group_cipher
);
755 if (*alg
== WPA_ALG_NONE
) {
756 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
757 "WPA: Unsupported Group Cipher %d",
761 *key_rsc_len
= wpa_cipher_rsc_len(group_cipher
);
763 klen
= wpa_cipher_key_len(group_cipher
);
764 if (keylen
!= klen
|| maxkeylen
< klen
) {
765 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
766 "WPA: Unsupported %s Group Cipher key length %d (%d)",
767 wpa_cipher_txt(group_cipher
), keylen
, maxkeylen
);
774 struct wpa_gtk_data
{
776 int tx
, key_rsc_len
, keyidx
;
782 static int wpa_supplicant_install_gtk(struct wpa_sm
*sm
,
783 const struct wpa_gtk_data
*gd
,
786 const u8
*_gtk
= gd
->gtk
;
789 wpa_hexdump_key(MSG_DEBUG
, "WPA: Group Key", gd
->gtk
, gd
->gtk_len
);
790 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
791 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
792 gd
->keyidx
, gd
->tx
, gd
->gtk_len
);
793 wpa_hexdump(MSG_DEBUG
, "WPA: RSC", key_rsc
, gd
->key_rsc_len
);
794 if (sm
->group_cipher
== WPA_CIPHER_TKIP
) {
795 /* Swap Tx/Rx keys for Michael MIC */
796 os_memcpy(gtk_buf
, gd
->gtk
, 16);
797 os_memcpy(gtk_buf
+ 16, gd
->gtk
+ 24, 8);
798 os_memcpy(gtk_buf
+ 24, gd
->gtk
+ 16, 8);
801 if (sm
->pairwise_cipher
== WPA_CIPHER_NONE
) {
802 if (wpa_sm_set_key(sm
, gd
->alg
, NULL
,
803 gd
->keyidx
, 1, key_rsc
, gd
->key_rsc_len
,
804 _gtk
, gd
->gtk_len
) < 0) {
805 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
806 "WPA: Failed to set GTK to the driver "
808 os_memset(gtk_buf
, 0, sizeof(gtk_buf
));
811 } else if (wpa_sm_set_key(sm
, gd
->alg
, broadcast_ether_addr
,
812 gd
->keyidx
, gd
->tx
, key_rsc
, gd
->key_rsc_len
,
813 _gtk
, gd
->gtk_len
) < 0) {
814 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
815 "WPA: Failed to set GTK to "
816 "the driver (alg=%d keylen=%d keyidx=%d)",
817 gd
->alg
, gd
->gtk_len
, gd
->keyidx
);
818 os_memset(gtk_buf
, 0, sizeof(gtk_buf
));
821 os_memset(gtk_buf
, 0, sizeof(gtk_buf
));
827 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm
*sm
,
830 if (tx
&& sm
->pairwise_cipher
!= WPA_CIPHER_NONE
) {
831 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
832 * seemed to set this bit (incorrectly, since Tx is only when
833 * doing Group Key only APs) and without this workaround, the
834 * data connection does not work because wpa_supplicant
835 * configured non-zero keyidx to be used for unicast. */
836 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
837 "WPA: Tx bit set for GTK, but pairwise "
838 "keys are used - ignore Tx bit");
845 static int wpa_supplicant_rsc_relaxation(const struct wpa_sm
*sm
,
850 if (!sm
->wpa_rsc_relaxation
)
853 rsclen
= wpa_cipher_rsc_len(sm
->group_cipher
);
856 * Try to detect RSC (endian) corruption issue where the AP sends
857 * the RSC bytes in EAPOL-Key message in the wrong order, both if
858 * it's actually a 6-byte field (as it should be) and if it treats
859 * it as an 8-byte field.
860 * An AP model known to have this bug is the Sapido RB-1632.
862 if (rsclen
== 6 && ((rsc
[5] && !rsc
[0]) || rsc
[6] || rsc
[7])) {
863 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
864 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
865 rsc
[0], rsc
[1], rsc
[2], rsc
[3],
866 rsc
[4], rsc
[5], rsc
[6], rsc
[7]);
875 static int wpa_supplicant_pairwise_gtk(struct wpa_sm
*sm
,
876 const struct wpa_eapol_key
*key
,
877 const u8
*gtk
, size_t gtk_len
,
880 struct wpa_gtk_data gd
;
884 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
886 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
887 * Reserved [bits 0-7]
891 os_memset(&gd
, 0, sizeof(gd
));
892 wpa_hexdump_key(MSG_DEBUG
, "RSN: received GTK in pairwise handshake",
895 if (gtk_len
< 2 || gtk_len
- 2 > sizeof(gd
.gtk
))
898 gd
.keyidx
= gtk
[0] & 0x3;
899 gd
.tx
= wpa_supplicant_gtk_tx_bit_workaround(sm
,
900 !!(gtk
[0] & BIT(2)));
904 os_memcpy(gd
.gtk
, gtk
, gtk_len
);
905 gd
.gtk_len
= gtk_len
;
907 key_rsc
= key
->key_rsc
;
908 if (wpa_supplicant_rsc_relaxation(sm
, key
->key_rsc
))
911 if (sm
->group_cipher
!= WPA_CIPHER_GTK_NOT_USED
&&
912 (wpa_supplicant_check_group_cipher(sm
, sm
->group_cipher
,
914 &gd
.key_rsc_len
, &gd
.alg
) ||
915 wpa_supplicant_install_gtk(sm
, &gd
, key_rsc
))) {
916 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
917 "RSN: Failed to install GTK");
918 os_memset(&gd
, 0, sizeof(gd
));
921 os_memset(&gd
, 0, sizeof(gd
));
923 wpa_supplicant_key_neg_complete(sm
, sm
->bssid
,
924 key_info
& WPA_KEY_INFO_SECURE
);
929 static int ieee80211w_set_keys(struct wpa_sm
*sm
,
930 struct wpa_eapol_ie_parse
*ie
)
932 #ifdef CONFIG_IEEE80211W
933 if (!wpa_cipher_valid_mgmt_group(sm
->mgmt_group_cipher
))
938 const struct wpa_igtk_kde
*igtk
;
940 len
= wpa_cipher_key_len(sm
->mgmt_group_cipher
);
941 if (ie
->igtk_len
!= WPA_IGTK_KDE_PREFIX_LEN
+ len
)
943 igtk
= (const struct wpa_igtk_kde
*) ie
->igtk
;
944 keyidx
= WPA_GET_LE16(igtk
->keyid
);
945 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: IGTK keyid %d "
946 "pn %02x%02x%02x%02x%02x%02x",
947 keyidx
, MAC2STR(igtk
->pn
));
948 wpa_hexdump_key(MSG_DEBUG
, "WPA: IGTK",
951 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
952 "WPA: Invalid IGTK KeyID %d", keyidx
);
955 if (wpa_sm_set_key(sm
, wpa_cipher_to_alg(sm
->mgmt_group_cipher
),
956 broadcast_ether_addr
,
957 keyidx
, 0, igtk
->pn
, sizeof(igtk
->pn
),
958 igtk
->igtk
, len
) < 0) {
959 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
960 "WPA: Failed to configure IGTK to the driver");
966 #else /* CONFIG_IEEE80211W */
968 #endif /* CONFIG_IEEE80211W */
972 static void wpa_report_ie_mismatch(struct wpa_sm
*sm
,
973 const char *reason
, const u8
*src_addr
,
974 const u8
*wpa_ie
, size_t wpa_ie_len
,
975 const u8
*rsn_ie
, size_t rsn_ie_len
)
977 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
, "WPA: %s (src=" MACSTR
")",
978 reason
, MAC2STR(src_addr
));
981 wpa_hexdump(MSG_INFO
, "WPA: WPA IE in Beacon/ProbeResp",
982 sm
->ap_wpa_ie
, sm
->ap_wpa_ie_len
);
985 if (!sm
->ap_wpa_ie
) {
986 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
987 "WPA: No WPA IE in Beacon/ProbeResp");
989 wpa_hexdump(MSG_INFO
, "WPA: WPA IE in 3/4 msg",
994 wpa_hexdump(MSG_INFO
, "WPA: RSN IE in Beacon/ProbeResp",
995 sm
->ap_rsn_ie
, sm
->ap_rsn_ie_len
);
998 if (!sm
->ap_rsn_ie
) {
999 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1000 "WPA: No RSN IE in Beacon/ProbeResp");
1002 wpa_hexdump(MSG_INFO
, "WPA: RSN IE in 3/4 msg",
1003 rsn_ie
, rsn_ie_len
);
1006 wpa_sm_deauthenticate(sm
, WLAN_REASON_IE_IN_4WAY_DIFFERS
);
1010 #ifdef CONFIG_IEEE80211R
1012 static int ft_validate_mdie(struct wpa_sm
*sm
,
1013 const unsigned char *src_addr
,
1014 struct wpa_eapol_ie_parse
*ie
,
1015 const u8
*assoc_resp_mdie
)
1017 struct rsn_mdie
*mdie
;
1019 mdie
= (struct rsn_mdie
*) (ie
->mdie
+ 2);
1020 if (ie
->mdie
== NULL
|| ie
->mdie_len
< 2 + sizeof(*mdie
) ||
1021 os_memcmp(mdie
->mobility_domain
, sm
->mobility_domain
,
1022 MOBILITY_DOMAIN_ID_LEN
) != 0) {
1023 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "FT: MDIE in msg 3/4 did "
1024 "not match with the current mobility domain");
1028 if (assoc_resp_mdie
&&
1029 (assoc_resp_mdie
[1] != ie
->mdie
[1] ||
1030 os_memcmp(assoc_resp_mdie
, ie
->mdie
, 2 + ie
->mdie
[1]) != 0)) {
1031 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "FT: MDIE mismatch");
1032 wpa_hexdump(MSG_DEBUG
, "FT: MDIE in EAPOL-Key msg 3/4",
1033 ie
->mdie
, 2 + ie
->mdie
[1]);
1034 wpa_hexdump(MSG_DEBUG
, "FT: MDIE in (Re)Association Response",
1035 assoc_resp_mdie
, 2 + assoc_resp_mdie
[1]);
1043 static int ft_validate_ftie(struct wpa_sm
*sm
,
1044 const unsigned char *src_addr
,
1045 struct wpa_eapol_ie_parse
*ie
,
1046 const u8
*assoc_resp_ftie
)
1048 if (ie
->ftie
== NULL
) {
1049 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1050 "FT: No FTIE in EAPOL-Key msg 3/4");
1054 if (assoc_resp_ftie
== NULL
)
1057 if (assoc_resp_ftie
[1] != ie
->ftie
[1] ||
1058 os_memcmp(assoc_resp_ftie
, ie
->ftie
, 2 + ie
->ftie
[1]) != 0) {
1059 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "FT: FTIE mismatch");
1060 wpa_hexdump(MSG_DEBUG
, "FT: FTIE in EAPOL-Key msg 3/4",
1061 ie
->ftie
, 2 + ie
->ftie
[1]);
1062 wpa_hexdump(MSG_DEBUG
, "FT: FTIE in (Re)Association Response",
1063 assoc_resp_ftie
, 2 + assoc_resp_ftie
[1]);
1071 static int ft_validate_rsnie(struct wpa_sm
*sm
,
1072 const unsigned char *src_addr
,
1073 struct wpa_eapol_ie_parse
*ie
)
1075 struct wpa_ie_data rsn
;
1081 * Verify that PMKR1Name from EAPOL-Key message 3/4
1082 * matches with the value we derived.
1084 if (wpa_parse_wpa_ie_rsn(ie
->rsn_ie
, ie
->rsn_ie_len
, &rsn
) < 0 ||
1085 rsn
.num_pmkid
!= 1 || rsn
.pmkid
== NULL
) {
1086 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "FT: No PMKR1Name in "
1087 "FT 4-way handshake message 3/4");
1091 if (os_memcmp_const(rsn
.pmkid
, sm
->pmk_r1_name
, WPA_PMK_NAME_LEN
) != 0)
1093 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1094 "FT: PMKR1Name mismatch in "
1095 "FT 4-way handshake message 3/4");
1096 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name from Authenticator",
1097 rsn
.pmkid
, WPA_PMK_NAME_LEN
);
1098 wpa_hexdump(MSG_DEBUG
, "FT: Derived PMKR1Name",
1099 sm
->pmk_r1_name
, WPA_PMK_NAME_LEN
);
1107 static int wpa_supplicant_validate_ie_ft(struct wpa_sm
*sm
,
1108 const unsigned char *src_addr
,
1109 struct wpa_eapol_ie_parse
*ie
)
1111 const u8
*pos
, *end
, *mdie
= NULL
, *ftie
= NULL
;
1113 if (sm
->assoc_resp_ies
) {
1114 pos
= sm
->assoc_resp_ies
;
1115 end
= pos
+ sm
->assoc_resp_ies_len
;
1116 while (end
- pos
> 2) {
1117 if (2 + pos
[1] > end
- pos
)
1120 case WLAN_EID_MOBILITY_DOMAIN
:
1123 case WLAN_EID_FAST_BSS_TRANSITION
:
1131 if (ft_validate_mdie(sm
, src_addr
, ie
, mdie
) < 0 ||
1132 ft_validate_ftie(sm
, src_addr
, ie
, ftie
) < 0 ||
1133 ft_validate_rsnie(sm
, src_addr
, ie
) < 0)
1139 #endif /* CONFIG_IEEE80211R */
1142 static int wpa_supplicant_validate_ie(struct wpa_sm
*sm
,
1143 const unsigned char *src_addr
,
1144 struct wpa_eapol_ie_parse
*ie
)
1146 if (sm
->ap_wpa_ie
== NULL
&& sm
->ap_rsn_ie
== NULL
) {
1147 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1148 "WPA: No WPA/RSN IE for this AP known. "
1149 "Trying to get from scan results");
1150 if (wpa_sm_get_beacon_ie(sm
) < 0) {
1151 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1152 "WPA: Could not find AP from "
1153 "the scan results");
1155 wpa_msg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1156 "WPA: Found the current AP from "
1157 "updated scan results");
1161 if (ie
->wpa_ie
== NULL
&& ie
->rsn_ie
== NULL
&&
1162 (sm
->ap_wpa_ie
|| sm
->ap_rsn_ie
)) {
1163 wpa_report_ie_mismatch(sm
, "IE in 3/4 msg does not match "
1164 "with IE in Beacon/ProbeResp (no IE?)",
1165 src_addr
, ie
->wpa_ie
, ie
->wpa_ie_len
,
1166 ie
->rsn_ie
, ie
->rsn_ie_len
);
1170 if ((ie
->wpa_ie
&& sm
->ap_wpa_ie
&&
1171 (ie
->wpa_ie_len
!= sm
->ap_wpa_ie_len
||
1172 os_memcmp(ie
->wpa_ie
, sm
->ap_wpa_ie
, ie
->wpa_ie_len
) != 0)) ||
1173 (ie
->rsn_ie
&& sm
->ap_rsn_ie
&&
1174 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm
->key_mgmt
),
1175 sm
->ap_rsn_ie
, sm
->ap_rsn_ie_len
,
1176 ie
->rsn_ie
, ie
->rsn_ie_len
))) {
1177 wpa_report_ie_mismatch(sm
, "IE in 3/4 msg does not match "
1178 "with IE in Beacon/ProbeResp",
1179 src_addr
, ie
->wpa_ie
, ie
->wpa_ie_len
,
1180 ie
->rsn_ie
, ie
->rsn_ie_len
);
1184 if (sm
->proto
== WPA_PROTO_WPA
&&
1185 ie
->rsn_ie
&& sm
->ap_rsn_ie
== NULL
&& sm
->rsn_enabled
) {
1186 wpa_report_ie_mismatch(sm
, "Possible downgrade attack "
1187 "detected - RSN was enabled and RSN IE "
1188 "was in msg 3/4, but not in "
1190 src_addr
, ie
->wpa_ie
, ie
->wpa_ie_len
,
1191 ie
->rsn_ie
, ie
->rsn_ie_len
);
1195 #ifdef CONFIG_IEEE80211R
1196 if (wpa_key_mgmt_ft(sm
->key_mgmt
) &&
1197 wpa_supplicant_validate_ie_ft(sm
, src_addr
, ie
) < 0)
1199 #endif /* CONFIG_IEEE80211R */
1206 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1207 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1208 * @dst: Destination address for the frame
1209 * @key: Pointer to the EAPOL-Key frame header
1210 * @ver: Version bits from EAPOL-Key Key Info
1211 * @key_info: Key Info
1212 * @ptk: PTK to use for keyed hash and encryption
1213 * Returns: >= 0 on success, < 0 on failure
1215 int wpa_supplicant_send_4_of_4(struct wpa_sm
*sm
, const unsigned char *dst
,
1216 const struct wpa_eapol_key
*key
,
1217 u16 ver
, u16 key_info
,
1218 struct wpa_ptk
*ptk
)
1220 size_t mic_len
, hdrlen
, rlen
;
1221 struct wpa_eapol_key
*reply
;
1224 mic_len
= wpa_mic_len(sm
->key_mgmt
);
1225 hdrlen
= sizeof(*reply
) + mic_len
+ 2;
1226 rbuf
= wpa_sm_alloc_eapol(sm
, IEEE802_1X_TYPE_EAPOL_KEY
, NULL
,
1227 hdrlen
, &rlen
, (void *) &reply
);
1231 reply
->type
= (sm
->proto
== WPA_PROTO_RSN
||
1232 sm
->proto
== WPA_PROTO_OSEN
) ?
1233 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
1234 key_info
&= WPA_KEY_INFO_SECURE
;
1235 key_info
|= ver
| WPA_KEY_INFO_KEY_TYPE
;
1237 key_info
|= WPA_KEY_INFO_MIC
;
1239 key_info
|= WPA_KEY_INFO_ENCR_KEY_DATA
;
1240 WPA_PUT_BE16(reply
->key_info
, key_info
);
1241 if (sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
)
1242 WPA_PUT_BE16(reply
->key_length
, 0);
1244 os_memcpy(reply
->key_length
, key
->key_length
, 2);
1245 os_memcpy(reply
->replay_counter
, key
->replay_counter
,
1246 WPA_REPLAY_COUNTER_LEN
);
1248 key_mic
= (u8
*) (reply
+ 1);
1249 WPA_PUT_BE16(key_mic
+ mic_len
, 0);
1251 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Sending EAPOL-Key 4/4");
1252 return wpa_eapol_key_send(sm
, ptk
, ver
, dst
, ETH_P_EAPOL
, rbuf
, rlen
,
1257 static void wpa_supplicant_process_3_of_4(struct wpa_sm
*sm
,
1258 const struct wpa_eapol_key
*key
,
1259 u16 ver
, const u8
*key_data
,
1260 size_t key_data_len
)
1262 u16 key_info
, keylen
;
1263 struct wpa_eapol_ie_parse ie
;
1265 wpa_sm_set_state(sm
, WPA_4WAY_HANDSHAKE
);
1266 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: RX message 3 of 4-Way "
1267 "Handshake from " MACSTR
" (ver=%d)", MAC2STR(sm
->bssid
), ver
);
1269 key_info
= WPA_GET_BE16(key
->key_info
);
1271 wpa_hexdump(MSG_DEBUG
, "WPA: IE KeyData", key_data
, key_data_len
);
1272 if (wpa_supplicant_parse_ies(key_data
, key_data_len
, &ie
) < 0)
1274 if (ie
.gtk
&& !(key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
1275 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1276 "WPA: GTK IE in unencrypted key data");
1279 #ifdef CONFIG_IEEE80211W
1280 if (ie
.igtk
&& !(key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
1281 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1282 "WPA: IGTK KDE in unencrypted key data");
1287 wpa_cipher_valid_mgmt_group(sm
->mgmt_group_cipher
) &&
1288 ie
.igtk_len
!= WPA_IGTK_KDE_PREFIX_LEN
+
1289 (unsigned int) wpa_cipher_key_len(sm
->mgmt_group_cipher
)) {
1290 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1291 "WPA: Invalid IGTK KDE length %lu",
1292 (unsigned long) ie
.igtk_len
);
1295 #endif /* CONFIG_IEEE80211W */
1297 if (wpa_supplicant_validate_ie(sm
, sm
->bssid
, &ie
) < 0)
1300 if (os_memcmp(sm
->anonce
, key
->key_nonce
, WPA_NONCE_LEN
) != 0) {
1301 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1302 "WPA: ANonce from message 1 of 4-Way Handshake "
1303 "differs from 3 of 4-Way Handshake - drop packet (src="
1304 MACSTR
")", MAC2STR(sm
->bssid
));
1308 keylen
= WPA_GET_BE16(key
->key_length
);
1309 if (keylen
!= wpa_cipher_key_len(sm
->pairwise_cipher
)) {
1310 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1311 "WPA: Invalid %s key length %d (src=" MACSTR
1312 ")", wpa_cipher_txt(sm
->pairwise_cipher
), keylen
,
1313 MAC2STR(sm
->bssid
));
1318 if (ie
.ip_addr_alloc
) {
1319 os_memcpy(sm
->p2p_ip_addr
, ie
.ip_addr_alloc
, 3 * 4);
1320 wpa_hexdump(MSG_DEBUG
, "P2P: IP address info",
1321 sm
->p2p_ip_addr
, sizeof(sm
->p2p_ip_addr
));
1323 #endif /* CONFIG_P2P */
1325 if (wpa_supplicant_send_4_of_4(sm
, sm
->bssid
, key
, ver
, key_info
,
1330 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1331 * for the next 4-Way Handshake. If msg 3 is received again, the old
1332 * SNonce will still be used to avoid changing PTK. */
1333 sm
->renew_snonce
= 1;
1335 if (key_info
& WPA_KEY_INFO_INSTALL
) {
1336 if (wpa_supplicant_install_ptk(sm
, key
))
1340 if (key_info
& WPA_KEY_INFO_SECURE
) {
1341 wpa_sm_mlme_setprotection(
1342 sm
, sm
->bssid
, MLME_SETPROTECTION_PROTECT_TYPE_RX
,
1343 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE
);
1344 eapol_sm_notify_portValid(sm
->eapol
, TRUE
);
1346 wpa_sm_set_state(sm
, WPA_GROUP_HANDSHAKE
);
1348 if (sm
->group_cipher
== WPA_CIPHER_GTK_NOT_USED
) {
1349 wpa_supplicant_key_neg_complete(sm
, sm
->bssid
,
1350 key_info
& WPA_KEY_INFO_SECURE
);
1351 } else if (ie
.gtk
&&
1352 wpa_supplicant_pairwise_gtk(sm
, key
,
1353 ie
.gtk
, ie
.gtk_len
, key_info
) < 0) {
1354 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1355 "RSN: Failed to configure GTK");
1359 if (ieee80211w_set_keys(sm
, &ie
) < 0) {
1360 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1361 "RSN: Failed to configure IGTK");
1366 wpa_sm_set_rekey_offload(sm
);
1368 if (sm
->proto
== WPA_PROTO_RSN
&& wpa_key_mgmt_suite_b(sm
->key_mgmt
)) {
1369 struct rsn_pmksa_cache_entry
*sa
;
1371 sa
= pmksa_cache_add(sm
->pmksa
, sm
->pmk
, sm
->pmk_len
, NULL
,
1372 sm
->ptk
.kck
, sm
->ptk
.kck_len
,
1373 sm
->bssid
, sm
->own_addr
,
1374 sm
->network_ctx
, sm
->key_mgmt
);
1379 sm
->msg_3_of_4_ok
= 1;
1383 wpa_sm_deauthenticate(sm
, WLAN_REASON_UNSPECIFIED
);
1387 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm
*sm
,
1391 struct wpa_gtk_data
*gd
)
1394 struct wpa_eapol_ie_parse ie
;
1396 wpa_hexdump(MSG_DEBUG
, "RSN: msg 1/2 key data", keydata
, keydatalen
);
1397 if (wpa_supplicant_parse_ies(keydata
, keydatalen
, &ie
) < 0)
1399 if (ie
.gtk
&& !(key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
1400 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1401 "WPA: GTK IE in unencrypted key data");
1404 if (ie
.gtk
== NULL
) {
1405 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1406 "WPA: No GTK IE in Group Key msg 1/2");
1409 maxkeylen
= gd
->gtk_len
= ie
.gtk_len
- 2;
1411 if (wpa_supplicant_check_group_cipher(sm
, sm
->group_cipher
,
1412 gd
->gtk_len
, maxkeylen
,
1413 &gd
->key_rsc_len
, &gd
->alg
))
1416 wpa_hexdump_key(MSG_DEBUG
, "RSN: received GTK in group key handshake",
1417 ie
.gtk
, ie
.gtk_len
);
1418 gd
->keyidx
= ie
.gtk
[0] & 0x3;
1419 gd
->tx
= wpa_supplicant_gtk_tx_bit_workaround(sm
,
1420 !!(ie
.gtk
[0] & BIT(2)));
1421 if (ie
.gtk_len
- 2 > sizeof(gd
->gtk
)) {
1422 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1423 "RSN: Too long GTK in GTK IE (len=%lu)",
1424 (unsigned long) ie
.gtk_len
- 2);
1427 os_memcpy(gd
->gtk
, ie
.gtk
+ 2, ie
.gtk_len
- 2);
1429 if (ieee80211w_set_keys(sm
, &ie
) < 0)
1430 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1431 "RSN: Failed to configure IGTK");
1437 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm
*sm
,
1438 const struct wpa_eapol_key
*key
,
1440 size_t key_data_len
, u16 key_info
,
1441 u16 ver
, struct wpa_gtk_data
*gd
)
1446 gtk_len
= WPA_GET_BE16(key
->key_length
);
1447 maxkeylen
= key_data_len
;
1448 if (ver
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
1449 if (maxkeylen
< 8) {
1450 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1451 "WPA: Too short maxkeylen (%lu)",
1452 (unsigned long) maxkeylen
);
1458 if (gtk_len
> maxkeylen
||
1459 wpa_supplicant_check_group_cipher(sm
, sm
->group_cipher
,
1461 &gd
->key_rsc_len
, &gd
->alg
))
1464 gd
->gtk_len
= gtk_len
;
1465 gd
->keyidx
= (key_info
& WPA_KEY_INFO_KEY_INDEX_MASK
) >>
1466 WPA_KEY_INFO_KEY_INDEX_SHIFT
;
1467 if (ver
== WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
&& sm
->ptk
.kek_len
== 16) {
1468 #ifdef CONFIG_NO_RC4
1469 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1470 "WPA: RC4 not supported in the build");
1472 #else /* CONFIG_NO_RC4 */
1474 if (key_data_len
> sizeof(gd
->gtk
)) {
1475 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1476 "WPA: RC4 key data too long (%lu)",
1477 (unsigned long) key_data_len
);
1480 os_memcpy(ek
, key
->key_iv
, 16);
1481 os_memcpy(ek
+ 16, sm
->ptk
.kek
, sm
->ptk
.kek_len
);
1482 os_memcpy(gd
->gtk
, key_data
, key_data_len
);
1483 if (rc4_skip(ek
, 32, 256, gd
->gtk
, key_data_len
)) {
1484 os_memset(ek
, 0, sizeof(ek
));
1485 wpa_msg(sm
->ctx
->msg_ctx
, MSG_ERROR
,
1489 os_memset(ek
, 0, sizeof(ek
));
1490 #endif /* CONFIG_NO_RC4 */
1491 } else if (ver
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
1492 if (maxkeylen
% 8) {
1493 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1494 "WPA: Unsupported AES-WRAP len %lu",
1495 (unsigned long) maxkeylen
);
1498 if (maxkeylen
> sizeof(gd
->gtk
)) {
1499 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1500 "WPA: AES-WRAP key data "
1501 "too long (keydatalen=%lu maxkeylen=%lu)",
1502 (unsigned long) key_data_len
,
1503 (unsigned long) maxkeylen
);
1506 if (aes_unwrap(sm
->ptk
.kek
, sm
->ptk
.kek_len
, maxkeylen
/ 8,
1507 key_data
, gd
->gtk
)) {
1508 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1509 "WPA: AES unwrap failed - could not decrypt "
1514 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1515 "WPA: Unsupported key_info type %d", ver
);
1518 gd
->tx
= wpa_supplicant_gtk_tx_bit_workaround(
1519 sm
, !!(key_info
& WPA_KEY_INFO_TXRX
));
1524 static int wpa_supplicant_send_2_of_2(struct wpa_sm
*sm
,
1525 const struct wpa_eapol_key
*key
,
1526 int ver
, u16 key_info
)
1528 size_t mic_len
, hdrlen
, rlen
;
1529 struct wpa_eapol_key
*reply
;
1532 mic_len
= wpa_mic_len(sm
->key_mgmt
);
1533 hdrlen
= sizeof(*reply
) + mic_len
+ 2;
1534 rbuf
= wpa_sm_alloc_eapol(sm
, IEEE802_1X_TYPE_EAPOL_KEY
, NULL
,
1535 hdrlen
, &rlen
, (void *) &reply
);
1539 reply
->type
= (sm
->proto
== WPA_PROTO_RSN
||
1540 sm
->proto
== WPA_PROTO_OSEN
) ?
1541 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
1542 key_info
&= WPA_KEY_INFO_KEY_INDEX_MASK
;
1543 key_info
|= ver
| WPA_KEY_INFO_SECURE
;
1545 key_info
|= WPA_KEY_INFO_MIC
;
1546 WPA_PUT_BE16(reply
->key_info
, key_info
);
1547 if (sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
)
1548 WPA_PUT_BE16(reply
->key_length
, 0);
1550 os_memcpy(reply
->key_length
, key
->key_length
, 2);
1551 os_memcpy(reply
->replay_counter
, key
->replay_counter
,
1552 WPA_REPLAY_COUNTER_LEN
);
1554 key_mic
= (u8
*) (reply
+ 1);
1555 WPA_PUT_BE16(key_mic
+ mic_len
, 0);
1557 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Sending EAPOL-Key 2/2");
1558 return wpa_eapol_key_send(sm
, &sm
->ptk
, ver
, sm
->bssid
, ETH_P_EAPOL
,
1559 rbuf
, rlen
, key_mic
);
1563 static void wpa_supplicant_process_1_of_2(struct wpa_sm
*sm
,
1564 const unsigned char *src_addr
,
1565 const struct wpa_eapol_key
*key
,
1567 size_t key_data_len
, u16 ver
)
1571 struct wpa_gtk_data gd
;
1574 if (!sm
->msg_3_of_4_ok
) {
1575 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1576 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
1580 os_memset(&gd
, 0, sizeof(gd
));
1582 rekey
= wpa_sm_get_state(sm
) == WPA_COMPLETED
;
1583 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: RX message 1 of Group Key "
1584 "Handshake from " MACSTR
" (ver=%d)", MAC2STR(src_addr
), ver
);
1586 key_info
= WPA_GET_BE16(key
->key_info
);
1588 if (sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
) {
1589 ret
= wpa_supplicant_process_1_of_2_rsn(sm
, key_data
,
1590 key_data_len
, key_info
,
1593 ret
= wpa_supplicant_process_1_of_2_wpa(sm
, key
, key_data
,
1595 key_info
, ver
, &gd
);
1598 wpa_sm_set_state(sm
, WPA_GROUP_HANDSHAKE
);
1603 key_rsc
= key
->key_rsc
;
1604 if (wpa_supplicant_rsc_relaxation(sm
, key
->key_rsc
))
1607 if (wpa_supplicant_install_gtk(sm
, &gd
, key_rsc
) ||
1608 wpa_supplicant_send_2_of_2(sm
, key
, ver
, key_info
) < 0)
1610 os_memset(&gd
, 0, sizeof(gd
));
1613 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
, "WPA: Group rekeying "
1614 "completed with " MACSTR
" [GTK=%s]",
1615 MAC2STR(sm
->bssid
), wpa_cipher_txt(sm
->group_cipher
));
1616 wpa_sm_cancel_auth_timeout(sm
);
1617 wpa_sm_set_state(sm
, WPA_COMPLETED
);
1619 wpa_supplicant_key_neg_complete(sm
, sm
->bssid
,
1621 WPA_KEY_INFO_SECURE
);
1624 wpa_sm_set_rekey_offload(sm
);
1629 os_memset(&gd
, 0, sizeof(gd
));
1630 wpa_sm_deauthenticate(sm
, WLAN_REASON_UNSPECIFIED
);
1634 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm
*sm
,
1635 struct wpa_eapol_key
*key
,
1637 const u8
*buf
, size_t len
)
1639 u8 mic
[WPA_EAPOL_KEY_MIC_MAX_LEN
];
1641 size_t mic_len
= wpa_mic_len(sm
->key_mgmt
);
1643 os_memcpy(mic
, key
+ 1, mic_len
);
1645 os_memset(key
+ 1, 0, mic_len
);
1646 wpa_eapol_key_mic(sm
->tptk
.kck
, sm
->tptk
.kck_len
, sm
->key_mgmt
,
1647 ver
, buf
, len
, (u8
*) (key
+ 1));
1648 if (os_memcmp_const(mic
, key
+ 1, mic_len
) != 0) {
1649 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1650 "WPA: Invalid EAPOL-Key MIC "
1651 "when using TPTK - ignoring TPTK");
1656 os_memcpy(&sm
->ptk
, &sm
->tptk
, sizeof(sm
->ptk
));
1657 os_memset(&sm
->tptk
, 0, sizeof(sm
->tptk
));
1661 if (!ok
&& sm
->ptk_set
) {
1662 os_memset(key
+ 1, 0, mic_len
);
1663 wpa_eapol_key_mic(sm
->ptk
.kck
, sm
->ptk
.kck_len
, sm
->key_mgmt
,
1664 ver
, buf
, len
, (u8
*) (key
+ 1));
1665 if (os_memcmp_const(mic
, key
+ 1, mic_len
) != 0) {
1666 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1667 "WPA: Invalid EAPOL-Key MIC - "
1675 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1676 "WPA: Could not verify EAPOL-Key MIC - "
1681 os_memcpy(sm
->rx_replay_counter
, key
->replay_counter
,
1682 WPA_REPLAY_COUNTER_LEN
);
1683 sm
->rx_replay_counter_set
= 1;
1688 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1689 static int wpa_supplicant_decrypt_key_data(struct wpa_sm
*sm
,
1690 struct wpa_eapol_key
*key
,
1691 size_t mic_len
, u16 ver
,
1692 u8
*key_data
, size_t *key_data_len
)
1694 wpa_hexdump(MSG_DEBUG
, "RSN: encrypted key data",
1695 key_data
, *key_data_len
);
1697 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1698 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1703 /* Decrypt key data here so that this operation does not need
1704 * to be implemented separately for each message type. */
1705 if (ver
== WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
&& sm
->ptk
.kek_len
== 16) {
1706 #ifdef CONFIG_NO_RC4
1707 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1708 "WPA: RC4 not supported in the build");
1710 #else /* CONFIG_NO_RC4 */
1712 os_memcpy(ek
, key
->key_iv
, 16);
1713 os_memcpy(ek
+ 16, sm
->ptk
.kek
, sm
->ptk
.kek_len
);
1714 if (rc4_skip(ek
, 32, 256, key_data
, *key_data_len
)) {
1715 os_memset(ek
, 0, sizeof(ek
));
1716 wpa_msg(sm
->ctx
->msg_ctx
, MSG_ERROR
,
1720 os_memset(ek
, 0, sizeof(ek
));
1721 #endif /* CONFIG_NO_RC4 */
1722 } else if (ver
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
1723 ver
== WPA_KEY_INFO_TYPE_AES_128_CMAC
||
1724 sm
->key_mgmt
== WPA_KEY_MGMT_OSEN
||
1725 wpa_key_mgmt_suite_b(sm
->key_mgmt
)) {
1727 if (*key_data_len
< 8 || *key_data_len
% 8) {
1728 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1729 "WPA: Unsupported AES-WRAP len %u",
1730 (unsigned int) *key_data_len
);
1733 *key_data_len
-= 8; /* AES-WRAP adds 8 bytes */
1734 buf
= os_malloc(*key_data_len
);
1736 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1737 "WPA: No memory for AES-UNWRAP buffer");
1740 if (aes_unwrap(sm
->ptk
.kek
, sm
->ptk
.kek_len
, *key_data_len
/ 8,
1742 bin_clear_free(buf
, *key_data_len
);
1743 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1744 "WPA: AES unwrap failed - "
1745 "could not decrypt EAPOL-Key key data");
1748 os_memcpy(key_data
, buf
, *key_data_len
);
1749 bin_clear_free(buf
, *key_data_len
);
1750 WPA_PUT_BE16(((u8
*) (key
+ 1)) + mic_len
, *key_data_len
);
1752 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1753 "WPA: Unsupported key_info type %d", ver
);
1756 wpa_hexdump_key(MSG_DEBUG
, "WPA: decrypted EAPOL-Key key data",
1757 key_data
, *key_data_len
);
1763 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1764 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1766 void wpa_sm_aborted_cached(struct wpa_sm
*sm
)
1768 if (sm
&& sm
->cur_pmksa
) {
1769 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1770 "RSN: Cancelling PMKSA caching attempt");
1771 sm
->cur_pmksa
= NULL
;
1776 static void wpa_eapol_key_dump(struct wpa_sm
*sm
,
1777 const struct wpa_eapol_key
*key
,
1778 unsigned int key_data_len
,
1779 const u8
*mic
, unsigned int mic_len
)
1781 #ifndef CONFIG_NO_STDOUT_DEBUG
1782 u16 key_info
= WPA_GET_BE16(key
->key_info
);
1784 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, " EAPOL-Key type=%d", key
->type
);
1785 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1786 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
1787 key_info
, key_info
& WPA_KEY_INFO_TYPE_MASK
,
1788 (key_info
& WPA_KEY_INFO_KEY_INDEX_MASK
) >>
1789 WPA_KEY_INFO_KEY_INDEX_SHIFT
,
1790 (key_info
& (BIT(13) | BIT(14) | BIT(15))) >> 13,
1791 key_info
& WPA_KEY_INFO_KEY_TYPE
? "Pairwise" : "Group",
1792 key_info
& WPA_KEY_INFO_INSTALL
? " Install" : "",
1793 key_info
& WPA_KEY_INFO_ACK
? " Ack" : "",
1794 key_info
& WPA_KEY_INFO_MIC
? " MIC" : "",
1795 key_info
& WPA_KEY_INFO_SECURE
? " Secure" : "",
1796 key_info
& WPA_KEY_INFO_ERROR
? " Error" : "",
1797 key_info
& WPA_KEY_INFO_REQUEST
? " Request" : "",
1798 key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
? " Encr" : "");
1799 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1800 " key_length=%u key_data_length=%u",
1801 WPA_GET_BE16(key
->key_length
), key_data_len
);
1802 wpa_hexdump(MSG_DEBUG
, " replay_counter",
1803 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
1804 wpa_hexdump(MSG_DEBUG
, " key_nonce", key
->key_nonce
, WPA_NONCE_LEN
);
1805 wpa_hexdump(MSG_DEBUG
, " key_iv", key
->key_iv
, 16);
1806 wpa_hexdump(MSG_DEBUG
, " key_rsc", key
->key_rsc
, 8);
1807 wpa_hexdump(MSG_DEBUG
, " key_id (reserved)", key
->key_id
, 8);
1808 wpa_hexdump(MSG_DEBUG
, " key_mic", mic
, mic_len
);
1809 #endif /* CONFIG_NO_STDOUT_DEBUG */
1814 static int wpa_supp_aead_decrypt(struct wpa_sm
*sm
, u8
*buf
, size_t buf_len
,
1815 size_t *key_data_len
)
1817 struct wpa_ptk
*ptk
;
1818 struct ieee802_1x_hdr
*hdr
;
1819 struct wpa_eapol_key
*key
;
1824 if (*key_data_len
< AES_BLOCK_SIZE
) {
1825 wpa_printf(MSG_INFO
, "No room for AES-SIV data in the frame");
1831 else if (sm
->ptk_set
)
1836 hdr
= (struct ieee802_1x_hdr
*) buf
;
1837 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1838 pos
= (u8
*) (key
+ 1);
1839 pos
+= 2; /* Pointing at the Encrypted Key Data field */
1841 tmp
= os_malloc(*key_data_len
);
1845 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
1846 * to Key Data (exclusive). */
1848 aad_len
[0] = pos
- buf
;
1849 if (aes_siv_decrypt(ptk
->kek
, ptk
->kek_len
, pos
, *key_data_len
,
1850 1, aad
, aad_len
, tmp
) < 0) {
1851 wpa_printf(MSG_INFO
, "Invalid AES-SIV data in the frame");
1852 bin_clear_free(tmp
, *key_data_len
);
1856 /* AEAD decryption and validation completed successfully */
1857 (*key_data_len
) -= AES_BLOCK_SIZE
;
1858 wpa_hexdump_key(MSG_DEBUG
, "WPA: Decrypted Key Data",
1859 tmp
, *key_data_len
);
1861 /* Replace Key Data field with the decrypted version */
1862 os_memcpy(pos
, tmp
, *key_data_len
);
1863 pos
-= 2; /* Key Data Length field */
1864 WPA_PUT_BE16(pos
, *key_data_len
);
1865 bin_clear_free(tmp
, *key_data_len
);
1870 os_memcpy(&sm
->ptk
, &sm
->tptk
, sizeof(sm
->ptk
));
1871 os_memset(&sm
->tptk
, 0, sizeof(sm
->tptk
));
1874 os_memcpy(sm
->rx_replay_counter
, key
->replay_counter
,
1875 WPA_REPLAY_COUNTER_LEN
);
1876 sm
->rx_replay_counter_set
= 1;
1880 #endif /* CONFIG_FILS */
1884 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1885 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1886 * @src_addr: Source MAC address of the EAPOL packet
1887 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1888 * @len: Length of the EAPOL frame
1889 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1891 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1892 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1893 * only processing WPA and WPA2 EAPOL-Key frames.
1895 * The received EAPOL-Key packets are validated and valid packets are replied
1896 * to. In addition, key material (PTK, GTK) is configured at the end of a
1897 * successful key handshake.
1899 int wpa_sm_rx_eapol(struct wpa_sm
*sm
, const u8
*src_addr
,
1900 const u8
*buf
, size_t len
)
1902 size_t plen
, data_len
, key_data_len
;
1903 const struct ieee802_1x_hdr
*hdr
;
1904 struct wpa_eapol_key
*key
;
1908 struct wpa_peerkey
*peerkey
= NULL
;
1910 size_t mic_len
, keyhdrlen
;
1912 #ifdef CONFIG_IEEE80211R
1913 sm
->ft_completed
= 0;
1914 #endif /* CONFIG_IEEE80211R */
1916 mic_len
= wpa_mic_len(sm
->key_mgmt
);
1917 keyhdrlen
= sizeof(*key
) + mic_len
+ 2;
1919 if (len
< sizeof(*hdr
) + keyhdrlen
) {
1920 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1921 "WPA: EAPOL frame too short to be a WPA "
1922 "EAPOL-Key (len %lu, expecting at least %lu)",
1923 (unsigned long) len
,
1924 (unsigned long) sizeof(*hdr
) + keyhdrlen
);
1928 hdr
= (const struct ieee802_1x_hdr
*) buf
;
1929 plen
= be_to_host16(hdr
->length
);
1930 data_len
= plen
+ sizeof(*hdr
);
1931 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1932 "IEEE 802.1X RX: version=%d type=%d length=%lu",
1933 hdr
->version
, hdr
->type
, (unsigned long) plen
);
1935 if (hdr
->version
< EAPOL_VERSION
) {
1936 /* TODO: backwards compatibility */
1938 if (hdr
->type
!= IEEE802_1X_TYPE_EAPOL_KEY
) {
1939 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1940 "WPA: EAPOL frame (type %u) discarded, "
1941 "not a Key frame", hdr
->type
);
1945 wpa_hexdump(MSG_MSGDUMP
, "WPA: RX EAPOL-Key", buf
, len
);
1946 if (plen
> len
- sizeof(*hdr
) || plen
< keyhdrlen
) {
1947 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1948 "WPA: EAPOL frame payload size %lu "
1949 "invalid (frame size %lu)",
1950 (unsigned long) plen
, (unsigned long) len
);
1954 if (data_len
< len
) {
1955 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1956 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
1957 (unsigned long) len
- data_len
);
1961 * Make a copy of the frame since we need to modify the buffer during
1962 * MAC validation and Key Data decryption.
1964 tmp
= os_malloc(data_len
);
1967 os_memcpy(tmp
, buf
, data_len
);
1968 key
= (struct wpa_eapol_key
*) (tmp
+ sizeof(struct ieee802_1x_hdr
));
1969 mic
= (u8
*) (key
+ 1);
1970 key_data
= mic
+ mic_len
+ 2;
1972 if (key
->type
!= EAPOL_KEY_TYPE_WPA
&& key
->type
!= EAPOL_KEY_TYPE_RSN
)
1974 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1975 "WPA: EAPOL-Key type (%d) unknown, discarded",
1981 key_data_len
= WPA_GET_BE16(mic
+ mic_len
);
1982 wpa_eapol_key_dump(sm
, key
, key_data_len
, mic
, mic_len
);
1984 if (key_data_len
> plen
- keyhdrlen
) {
1985 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
, "WPA: Invalid EAPOL-Key "
1986 "frame - key_data overflow (%u > %u)",
1987 (unsigned int) key_data_len
,
1988 (unsigned int) (plen
- keyhdrlen
));
1992 eapol_sm_notify_lower_layer_success(sm
->eapol
, 0);
1993 key_info
= WPA_GET_BE16(key
->key_info
);
1994 ver
= key_info
& WPA_KEY_INFO_TYPE_MASK
;
1995 if (ver
!= WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
&&
1996 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
1997 ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
&&
1998 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
1999 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
&&
2000 !wpa_key_mgmt_suite_b(sm
->key_mgmt
) &&
2001 !wpa_key_mgmt_fils(sm
->key_mgmt
) &&
2002 sm
->key_mgmt
!= WPA_KEY_MGMT_OSEN
) {
2003 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2004 "WPA: Unsupported EAPOL-Key descriptor version %d",
2009 if (sm
->key_mgmt
== WPA_KEY_MGMT_OSEN
&&
2010 ver
!= WPA_KEY_INFO_TYPE_AKM_DEFINED
) {
2011 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2012 "OSEN: Unsupported EAPOL-Key descriptor version %d",
2017 if ((wpa_key_mgmt_suite_b(sm
->key_mgmt
) ||
2018 wpa_key_mgmt_fils(sm
->key_mgmt
)) &&
2019 ver
!= WPA_KEY_INFO_TYPE_AKM_DEFINED
) {
2020 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2021 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
2026 #ifdef CONFIG_IEEE80211R
2027 if (wpa_key_mgmt_ft(sm
->key_mgmt
)) {
2028 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
2029 if (ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
2030 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2031 "FT: AP did not use AES-128-CMAC");
2035 #endif /* CONFIG_IEEE80211R */
2036 #ifdef CONFIG_IEEE80211W
2037 if (wpa_key_mgmt_sha256(sm
->key_mgmt
)) {
2038 if (ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
&&
2039 sm
->key_mgmt
!= WPA_KEY_MGMT_OSEN
&&
2040 !wpa_key_mgmt_fils(sm
->key_mgmt
) &&
2041 !wpa_key_mgmt_suite_b(sm
->key_mgmt
)) {
2042 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2043 "WPA: AP did not use the "
2044 "negotiated AES-128-CMAC");
2048 #endif /* CONFIG_IEEE80211W */
2049 if (sm
->pairwise_cipher
== WPA_CIPHER_CCMP
&&
2050 !wpa_key_mgmt_suite_b(sm
->key_mgmt
) &&
2051 !wpa_key_mgmt_fils(sm
->key_mgmt
) &&
2052 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
2053 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2054 "WPA: CCMP is used, but EAPOL-Key "
2055 "descriptor version (%d) is not 2", ver
);
2056 if (sm
->group_cipher
!= WPA_CIPHER_CCMP
&&
2057 !(key_info
& WPA_KEY_INFO_KEY_TYPE
)) {
2058 /* Earlier versions of IEEE 802.11i did not explicitly
2059 * require version 2 descriptor for all EAPOL-Key
2060 * packets, so allow group keys to use version 1 if
2061 * CCMP is not used for them. */
2062 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2063 "WPA: Backwards compatibility: allow invalid "
2064 "version for non-CCMP group keys");
2065 } else if (ver
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
2066 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2067 "WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
2070 } else if (sm
->pairwise_cipher
== WPA_CIPHER_GCMP
&&
2071 !wpa_key_mgmt_suite_b(sm
->key_mgmt
) &&
2072 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
2073 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2074 "WPA: GCMP is used, but EAPOL-Key "
2075 "descriptor version (%d) is not 2", ver
);
2079 #ifdef CONFIG_PEERKEY
2080 for (peerkey
= sm
->peerkey
; peerkey
; peerkey
= peerkey
->next
) {
2081 if (os_memcmp(peerkey
->addr
, src_addr
, ETH_ALEN
) == 0)
2085 if (!(key_info
& WPA_KEY_INFO_SMK_MESSAGE
) && peerkey
) {
2086 if (!peerkey
->initiator
&& peerkey
->replay_counter_set
&&
2087 os_memcmp(key
->replay_counter
, peerkey
->replay_counter
,
2088 WPA_REPLAY_COUNTER_LEN
) <= 0) {
2089 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2090 "RSN: EAPOL-Key Replay Counter did not "
2091 "increase (STK) - dropping packet");
2093 } else if (peerkey
->initiator
) {
2094 u8 _tmp
[WPA_REPLAY_COUNTER_LEN
];
2095 os_memcpy(_tmp
, key
->replay_counter
,
2096 WPA_REPLAY_COUNTER_LEN
);
2097 inc_byte_array(_tmp
, WPA_REPLAY_COUNTER_LEN
);
2098 if (os_memcmp(_tmp
, peerkey
->replay_counter
,
2099 WPA_REPLAY_COUNTER_LEN
) != 0) {
2100 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2101 "RSN: EAPOL-Key Replay "
2102 "Counter did not match (STK) - "
2109 if (peerkey
&& peerkey
->initiator
&& (key_info
& WPA_KEY_INFO_ACK
)) {
2110 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2111 "RSN: Ack bit in key_info from STK peer");
2114 #endif /* CONFIG_PEERKEY */
2116 if (!peerkey
&& sm
->rx_replay_counter_set
&&
2117 os_memcmp(key
->replay_counter
, sm
->rx_replay_counter
,
2118 WPA_REPLAY_COUNTER_LEN
) <= 0) {
2119 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2120 "WPA: EAPOL-Key Replay Counter did not increase - "
2125 if (!(key_info
& (WPA_KEY_INFO_ACK
| WPA_KEY_INFO_SMK_MESSAGE
))
2126 #ifdef CONFIG_PEERKEY
2127 && (peerkey
== NULL
|| !peerkey
->initiator
)
2128 #endif /* CONFIG_PEERKEY */
2130 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2131 "WPA: No Ack bit in key_info");
2135 if (key_info
& WPA_KEY_INFO_REQUEST
) {
2136 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2137 "WPA: EAPOL-Key with Request bit - dropped");
2141 if ((key_info
& WPA_KEY_INFO_MIC
) && !peerkey
&&
2142 wpa_supplicant_verify_eapol_key_mic(sm
, key
, ver
, tmp
, data_len
))
2145 #ifdef CONFIG_PEERKEY
2146 if ((key_info
& WPA_KEY_INFO_MIC
) && peerkey
&&
2147 peerkey_verify_eapol_key_mic(sm
, peerkey
, key
, ver
, tmp
,
2150 #endif /* CONFIG_PEERKEY */
2153 if (!mic_len
&& (key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
2154 if (wpa_supp_aead_decrypt(sm
, tmp
, data_len
, &key_data_len
))
2157 #endif /* CONFIG_FILS */
2159 if ((sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
) &&
2160 (key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
) && mic_len
) {
2161 if (wpa_supplicant_decrypt_key_data(sm
, key
, mic_len
,
2167 if (key_info
& WPA_KEY_INFO_KEY_TYPE
) {
2168 if (key_info
& WPA_KEY_INFO_KEY_INDEX_MASK
) {
2169 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2170 "WPA: Ignored EAPOL-Key (Pairwise) with "
2171 "non-zero key index");
2175 /* PeerKey 4-Way Handshake */
2176 peerkey_rx_eapol_4way(sm
, peerkey
, key
, key_info
, ver
,
2177 key_data
, key_data_len
);
2178 } else if (key_info
& (WPA_KEY_INFO_MIC
|
2179 WPA_KEY_INFO_ENCR_KEY_DATA
)) {
2180 /* 3/4 4-Way Handshake */
2181 wpa_supplicant_process_3_of_4(sm
, key
, ver
, key_data
,
2184 /* 1/4 4-Way Handshake */
2185 wpa_supplicant_process_1_of_4(sm
, src_addr
, key
,
2189 } else if (key_info
& WPA_KEY_INFO_SMK_MESSAGE
) {
2190 /* PeerKey SMK Handshake */
2191 peerkey_rx_eapol_smk(sm
, src_addr
, key
, key_data
, key_data_len
,
2194 if ((mic_len
&& (key_info
& WPA_KEY_INFO_MIC
)) ||
2195 (!mic_len
&& (key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
))) {
2196 /* 1/2 Group Key Handshake */
2197 wpa_supplicant_process_1_of_2(sm
, src_addr
, key
,
2198 key_data
, key_data_len
,
2201 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2202 "WPA: EAPOL-Key (Group) without Mic/Encr bit - "
2210 bin_clear_free(tmp
, data_len
);
2215 #ifdef CONFIG_CTRL_IFACE
2216 static u32
wpa_key_mgmt_suite(struct wpa_sm
*sm
)
2218 switch (sm
->key_mgmt
) {
2219 case WPA_KEY_MGMT_IEEE8021X
:
2220 return ((sm
->proto
== WPA_PROTO_RSN
||
2221 sm
->proto
== WPA_PROTO_OSEN
) ?
2222 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X
:
2223 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X
);
2224 case WPA_KEY_MGMT_PSK
:
2225 return (sm
->proto
== WPA_PROTO_RSN
?
2226 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X
:
2227 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X
);
2228 #ifdef CONFIG_IEEE80211R
2229 case WPA_KEY_MGMT_FT_IEEE8021X
:
2230 return RSN_AUTH_KEY_MGMT_FT_802_1X
;
2231 case WPA_KEY_MGMT_FT_PSK
:
2232 return RSN_AUTH_KEY_MGMT_FT_PSK
;
2233 #endif /* CONFIG_IEEE80211R */
2234 #ifdef CONFIG_IEEE80211W
2235 case WPA_KEY_MGMT_IEEE8021X_SHA256
:
2236 return RSN_AUTH_KEY_MGMT_802_1X_SHA256
;
2237 case WPA_KEY_MGMT_PSK_SHA256
:
2238 return RSN_AUTH_KEY_MGMT_PSK_SHA256
;
2239 #endif /* CONFIG_IEEE80211W */
2240 case WPA_KEY_MGMT_CCKM
:
2241 return (sm
->proto
== WPA_PROTO_RSN
?
2242 RSN_AUTH_KEY_MGMT_CCKM
:
2243 WPA_AUTH_KEY_MGMT_CCKM
);
2244 case WPA_KEY_MGMT_WPA_NONE
:
2245 return WPA_AUTH_KEY_MGMT_NONE
;
2246 case WPA_KEY_MGMT_IEEE8021X_SUITE_B
:
2247 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B
;
2248 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192
:
2249 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192
;
2256 #define RSN_SUITE "%02x-%02x-%02x-%d"
2257 #define RSN_SUITE_ARG(s) \
2258 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2261 * wpa_sm_get_mib - Dump text list of MIB entries
2262 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2263 * @buf: Buffer for the list
2264 * @buflen: Length of the buffer
2265 * Returns: Number of bytes written to buffer
2267 * This function is used fetch dot11 MIB variables.
2269 int wpa_sm_get_mib(struct wpa_sm
*sm
, char *buf
, size_t buflen
)
2271 char pmkid_txt
[PMKID_LEN
* 2 + 1];
2275 if (sm
->cur_pmksa
) {
2276 wpa_snprintf_hex(pmkid_txt
, sizeof(pmkid_txt
),
2277 sm
->cur_pmksa
->pmkid
, PMKID_LEN
);
2279 pmkid_txt
[0] = '\0';
2281 if ((wpa_key_mgmt_wpa_psk(sm
->key_mgmt
) ||
2282 wpa_key_mgmt_wpa_ieee8021x(sm
->key_mgmt
)) &&
2283 sm
->proto
== WPA_PROTO_RSN
)
2288 ret
= os_snprintf(buf
, buflen
,
2289 "dot11RSNAOptionImplemented=TRUE\n"
2290 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2291 "dot11RSNAEnabled=%s\n"
2292 "dot11RSNAPreauthenticationEnabled=%s\n"
2293 "dot11RSNAConfigVersion=%d\n"
2294 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2295 "dot11RSNAConfigGroupCipherSize=%d\n"
2296 "dot11RSNAConfigPMKLifetime=%d\n"
2297 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2298 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2299 "dot11RSNAConfigSATimeout=%d\n",
2300 rsna
? "TRUE" : "FALSE",
2301 rsna
? "TRUE" : "FALSE",
2303 wpa_cipher_key_len(sm
->group_cipher
) * 8,
2304 sm
->dot11RSNAConfigPMKLifetime
,
2305 sm
->dot11RSNAConfigPMKReauthThreshold
,
2306 sm
->dot11RSNAConfigSATimeout
);
2307 if (os_snprintf_error(buflen
, ret
))
2312 buf
+ len
, buflen
- len
,
2313 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE
"\n"
2314 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE
"\n"
2315 "dot11RSNAGroupCipherSelected=" RSN_SUITE
"\n"
2316 "dot11RSNAPMKIDUsed=%s\n"
2317 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE
"\n"
2318 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE
"\n"
2319 "dot11RSNAGroupCipherRequested=" RSN_SUITE
"\n"
2320 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2321 "dot11RSNA4WayHandshakeFailures=%u\n",
2322 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm
)),
2323 RSN_SUITE_ARG(wpa_cipher_to_suite(sm
->proto
,
2324 sm
->pairwise_cipher
)),
2325 RSN_SUITE_ARG(wpa_cipher_to_suite(sm
->proto
,
2328 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm
)),
2329 RSN_SUITE_ARG(wpa_cipher_to_suite(sm
->proto
,
2330 sm
->pairwise_cipher
)),
2331 RSN_SUITE_ARG(wpa_cipher_to_suite(sm
->proto
,
2333 sm
->dot11RSNA4WayHandshakeFailures
);
2334 if (!os_snprintf_error(buflen
- len
, ret
))
2339 #endif /* CONFIG_CTRL_IFACE */
2342 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry
*entry
,
2343 void *ctx
, enum pmksa_free_reason reason
)
2345 struct wpa_sm
*sm
= ctx
;
2348 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "RSN: PMKSA cache entry free_cb: "
2349 MACSTR
" reason=%d", MAC2STR(entry
->aa
), reason
);
2351 if (sm
->cur_pmksa
== entry
) {
2352 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2353 "RSN: %s current PMKSA entry",
2354 reason
== PMKSA_REPLACE
? "replaced" : "removed");
2355 pmksa_cache_clear_current(sm
);
2358 * If an entry is simply being replaced, there's no need to
2359 * deauthenticate because it will be immediately re-added.
2360 * This happens when EAP authentication is completed again
2361 * (reauth or failed PMKSA caching attempt).
2363 if (reason
!= PMKSA_REPLACE
)
2367 if (reason
== PMKSA_EXPIRE
&&
2368 (sm
->pmk_len
== entry
->pmk_len
&&
2369 os_memcmp(sm
->pmk
, entry
->pmk
, sm
->pmk_len
) == 0)) {
2370 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2371 "RSN: deauthenticating due to expired PMK");
2372 pmksa_cache_clear_current(sm
);
2377 os_memset(sm
->pmk
, 0, sizeof(sm
->pmk
));
2378 wpa_sm_deauthenticate(sm
, WLAN_REASON_UNSPECIFIED
);
2384 * wpa_sm_init - Initialize WPA state machine
2385 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2386 * Returns: Pointer to the allocated WPA state machine data
2388 * This function is used to allocate a new WPA state machine and the returned
2389 * value is passed to all WPA state machine calls.
2391 struct wpa_sm
* wpa_sm_init(struct wpa_sm_ctx
*ctx
)
2395 sm
= os_zalloc(sizeof(*sm
));
2398 dl_list_init(&sm
->pmksa_candidates
);
2399 sm
->renew_snonce
= 1;
2402 sm
->dot11RSNAConfigPMKLifetime
= 43200;
2403 sm
->dot11RSNAConfigPMKReauthThreshold
= 70;
2404 sm
->dot11RSNAConfigSATimeout
= 60;
2406 sm
->pmksa
= pmksa_cache_init(wpa_sm_pmksa_free_cb
, sm
, sm
);
2407 if (sm
->pmksa
== NULL
) {
2408 wpa_msg(sm
->ctx
->msg_ctx
, MSG_ERROR
,
2409 "RSN: PMKSA cache initialization failed");
2419 * wpa_sm_deinit - Deinitialize WPA state machine
2420 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2422 void wpa_sm_deinit(struct wpa_sm
*sm
)
2426 pmksa_cache_deinit(sm
->pmksa
);
2427 eloop_cancel_timeout(wpa_sm_start_preauth
, sm
, NULL
);
2428 eloop_cancel_timeout(wpa_sm_rekey_ptk
, sm
, NULL
);
2429 os_free(sm
->assoc_wpa_ie
);
2430 os_free(sm
->ap_wpa_ie
);
2431 os_free(sm
->ap_rsn_ie
);
2435 #ifdef CONFIG_IEEE80211R
2436 os_free(sm
->assoc_resp_ies
);
2437 #endif /* CONFIG_IEEE80211R */
2438 #ifdef CONFIG_TESTING_OPTIONS
2439 wpabuf_free(sm
->test_assoc_ie
);
2440 #endif /* CONFIG_TESTING_OPTIONS */
2446 * wpa_sm_notify_assoc - Notify WPA state machine about association
2447 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2448 * @bssid: The BSSID of the new association
2450 * This function is called to let WPA state machine know that the connection
2453 void wpa_sm_notify_assoc(struct wpa_sm
*sm
, const u8
*bssid
)
2460 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2461 "WPA: Association event - clear replay counter");
2462 os_memcpy(sm
->bssid
, bssid
, ETH_ALEN
);
2463 os_memset(sm
->rx_replay_counter
, 0, WPA_REPLAY_COUNTER_LEN
);
2464 sm
->rx_replay_counter_set
= 0;
2465 sm
->renew_snonce
= 1;
2466 if (os_memcmp(sm
->preauth_bssid
, bssid
, ETH_ALEN
) == 0)
2467 rsn_preauth_deinit(sm
);
2469 #ifdef CONFIG_IEEE80211R
2470 if (wpa_ft_is_completed(sm
)) {
2472 * Clear portValid to kick EAPOL state machine to re-enter
2473 * AUTHENTICATED state to get the EAPOL port Authorized.
2475 eapol_sm_notify_portValid(sm
->eapol
, FALSE
);
2476 wpa_supplicant_key_neg_complete(sm
, sm
->bssid
, 1);
2478 /* Prepare for the next transition */
2479 wpa_ft_prepare_auth_request(sm
, NULL
);
2483 #endif /* CONFIG_IEEE80211R */
2485 if (sm
->fils_completed
) {
2487 * Clear portValid to kick EAPOL state machine to re-enter
2488 * AUTHENTICATED state to get the EAPOL port Authorized.
2490 wpa_supplicant_key_neg_complete(sm
, sm
->bssid
, 1);
2493 #endif /* CONFIG_FILS */
2497 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2498 * this is not part of a Fast BSS Transition.
2500 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Clear old PTK");
2502 os_memset(&sm
->ptk
, 0, sizeof(sm
->ptk
));
2504 os_memset(&sm
->tptk
, 0, sizeof(sm
->tptk
));
2509 #endif /* CONFIG_TDLS */
2512 os_memset(sm
->p2p_ip_addr
, 0, sizeof(sm
->p2p_ip_addr
));
2513 #endif /* CONFIG_P2P */
2518 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2519 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2521 * This function is called to let WPA state machine know that the connection
2522 * was lost. This will abort any existing pre-authentication session.
2524 void wpa_sm_notify_disassoc(struct wpa_sm
*sm
)
2526 eloop_cancel_timeout(wpa_sm_start_preauth
, sm
, NULL
);
2527 eloop_cancel_timeout(wpa_sm_rekey_ptk
, sm
, NULL
);
2529 rsn_preauth_deinit(sm
);
2530 pmksa_cache_clear_current(sm
);
2531 if (wpa_sm_get_state(sm
) == WPA_4WAY_HANDSHAKE
)
2532 sm
->dot11RSNA4WayHandshakeFailures
++;
2534 wpa_tdls_disassoc(sm
);
2535 #endif /* CONFIG_TDLS */
2537 sm
->fils_completed
= 0;
2538 #endif /* CONFIG_FILS */
2540 /* Keys are not needed in the WPA state machine anymore */
2543 sm
->msg_3_of_4_ok
= 0;
2548 * wpa_sm_set_pmk - Set PMK
2549 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2551 * @pmk_len: The length of the new PMK in bytes
2552 * @pmkid: Calculated PMKID
2553 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
2555 * Configure the PMK for WPA state machine.
2557 void wpa_sm_set_pmk(struct wpa_sm
*sm
, const u8
*pmk
, size_t pmk_len
,
2558 const u8
*pmkid
, const u8
*bssid
)
2563 sm
->pmk_len
= pmk_len
;
2564 os_memcpy(sm
->pmk
, pmk
, pmk_len
);
2566 #ifdef CONFIG_IEEE80211R
2567 /* Set XXKey to be PSK for FT key derivation */
2568 sm
->xxkey_len
= pmk_len
;
2569 os_memcpy(sm
->xxkey
, pmk
, pmk_len
);
2570 #endif /* CONFIG_IEEE80211R */
2573 pmksa_cache_add(sm
->pmksa
, pmk
, pmk_len
, pmkid
, NULL
, 0,
2574 bssid
, sm
->own_addr
,
2575 sm
->network_ctx
, sm
->key_mgmt
);
2581 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2582 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2584 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2587 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm
*sm
)
2592 if (sm
->cur_pmksa
) {
2593 sm
->pmk_len
= sm
->cur_pmksa
->pmk_len
;
2594 os_memcpy(sm
->pmk
, sm
->cur_pmksa
->pmk
, sm
->pmk_len
);
2596 sm
->pmk_len
= PMK_LEN
;
2597 os_memset(sm
->pmk
, 0, PMK_LEN
);
2603 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2604 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2605 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2607 void wpa_sm_set_fast_reauth(struct wpa_sm
*sm
, int fast_reauth
)
2610 sm
->fast_reauth
= fast_reauth
;
2615 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2616 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2617 * @scard_ctx: Context pointer for smartcard related callback functions
2619 void wpa_sm_set_scard_ctx(struct wpa_sm
*sm
, void *scard_ctx
)
2623 sm
->scard_ctx
= scard_ctx
;
2624 if (sm
->preauth_eapol
)
2625 eapol_sm_register_scard_ctx(sm
->preauth_eapol
, scard_ctx
);
2630 * wpa_sm_set_config - Notification of current configration change
2631 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2632 * @config: Pointer to current network configuration
2634 * Notify WPA state machine that configuration has changed. config will be
2635 * stored as a backpointer to network configuration. This can be %NULL to clear
2636 * the stored pointed.
2638 void wpa_sm_set_config(struct wpa_sm
*sm
, struct rsn_supp_config
*config
)
2644 sm
->network_ctx
= config
->network_ctx
;
2645 sm
->peerkey_enabled
= config
->peerkey_enabled
;
2646 sm
->allowed_pairwise_cipher
= config
->allowed_pairwise_cipher
;
2647 sm
->proactive_key_caching
= config
->proactive_key_caching
;
2648 sm
->eap_workaround
= config
->eap_workaround
;
2649 sm
->eap_conf_ctx
= config
->eap_conf_ctx
;
2651 os_memcpy(sm
->ssid
, config
->ssid
, config
->ssid_len
);
2652 sm
->ssid_len
= config
->ssid_len
;
2655 sm
->wpa_ptk_rekey
= config
->wpa_ptk_rekey
;
2656 sm
->p2p
= config
->p2p
;
2657 sm
->wpa_rsc_relaxation
= config
->wpa_rsc_relaxation
;
2659 sm
->network_ctx
= NULL
;
2660 sm
->peerkey_enabled
= 0;
2661 sm
->allowed_pairwise_cipher
= 0;
2662 sm
->proactive_key_caching
= 0;
2663 sm
->eap_workaround
= 0;
2664 sm
->eap_conf_ctx
= NULL
;
2666 sm
->wpa_ptk_rekey
= 0;
2668 sm
->wpa_rsc_relaxation
= 0;
2674 * wpa_sm_set_own_addr - Set own MAC address
2675 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2676 * @addr: Own MAC address
2678 void wpa_sm_set_own_addr(struct wpa_sm
*sm
, const u8
*addr
)
2681 os_memcpy(sm
->own_addr
, addr
, ETH_ALEN
);
2686 * wpa_sm_set_ifname - Set network interface name
2687 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2688 * @ifname: Interface name
2689 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2691 void wpa_sm_set_ifname(struct wpa_sm
*sm
, const char *ifname
,
2692 const char *bridge_ifname
)
2695 sm
->ifname
= ifname
;
2696 sm
->bridge_ifname
= bridge_ifname
;
2702 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2703 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2704 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2706 void wpa_sm_set_eapol(struct wpa_sm
*sm
, struct eapol_sm
*eapol
)
2714 * wpa_sm_set_param - Set WPA state machine parameters
2715 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2716 * @param: Parameter field
2717 * @value: Parameter value
2718 * Returns: 0 on success, -1 on failure
2720 int wpa_sm_set_param(struct wpa_sm
*sm
, enum wpa_sm_conf_params param
,
2729 case RSNA_PMK_LIFETIME
:
2731 sm
->dot11RSNAConfigPMKLifetime
= value
;
2735 case RSNA_PMK_REAUTH_THRESHOLD
:
2736 if (value
> 0 && value
<= 100)
2737 sm
->dot11RSNAConfigPMKReauthThreshold
= value
;
2741 case RSNA_SA_TIMEOUT
:
2743 sm
->dot11RSNAConfigSATimeout
= value
;
2747 case WPA_PARAM_PROTO
:
2750 case WPA_PARAM_PAIRWISE
:
2751 sm
->pairwise_cipher
= value
;
2753 case WPA_PARAM_GROUP
:
2754 sm
->group_cipher
= value
;
2756 case WPA_PARAM_KEY_MGMT
:
2757 sm
->key_mgmt
= value
;
2759 #ifdef CONFIG_IEEE80211W
2760 case WPA_PARAM_MGMT_GROUP
:
2761 sm
->mgmt_group_cipher
= value
;
2763 #endif /* CONFIG_IEEE80211W */
2764 case WPA_PARAM_RSN_ENABLED
:
2765 sm
->rsn_enabled
= value
;
2779 * wpa_sm_get_status - Get WPA state machine
2780 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2781 * @buf: Buffer for status information
2782 * @buflen: Maximum buffer length
2783 * @verbose: Whether to include verbose status information
2784 * Returns: Number of bytes written to buf.
2786 * Query WPA state machine for status information. This function fills in
2787 * a text area with current status information. If the buffer (buf) is not
2788 * large enough, status information will be truncated to fit the buffer.
2790 int wpa_sm_get_status(struct wpa_sm
*sm
, char *buf
, size_t buflen
,
2793 char *pos
= buf
, *end
= buf
+ buflen
;
2796 ret
= os_snprintf(pos
, end
- pos
,
2797 "pairwise_cipher=%s\n"
2800 wpa_cipher_txt(sm
->pairwise_cipher
),
2801 wpa_cipher_txt(sm
->group_cipher
),
2802 wpa_key_mgmt_txt(sm
->key_mgmt
, sm
->proto
));
2803 if (os_snprintf_error(end
- pos
, ret
))
2807 if (sm
->mfp
!= NO_MGMT_FRAME_PROTECTION
&& sm
->ap_rsn_ie
) {
2808 struct wpa_ie_data rsn
;
2809 if (wpa_parse_wpa_ie_rsn(sm
->ap_rsn_ie
, sm
->ap_rsn_ie_len
, &rsn
)
2811 rsn
.capabilities
& (WPA_CAPABILITY_MFPR
|
2812 WPA_CAPABILITY_MFPC
)) {
2813 ret
= os_snprintf(pos
, end
- pos
, "pmf=%d\n",
2815 WPA_CAPABILITY_MFPR
) ? 2 : 1);
2816 if (os_snprintf_error(end
- pos
, ret
))
2826 int wpa_sm_pmf_enabled(struct wpa_sm
*sm
)
2828 struct wpa_ie_data rsn
;
2830 if (sm
->mfp
== NO_MGMT_FRAME_PROTECTION
|| !sm
->ap_rsn_ie
)
2833 if (wpa_parse_wpa_ie_rsn(sm
->ap_rsn_ie
, sm
->ap_rsn_ie_len
, &rsn
) >= 0 &&
2834 rsn
.capabilities
& (WPA_CAPABILITY_MFPR
| WPA_CAPABILITY_MFPC
))
2842 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2843 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2844 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2845 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2846 * Returns: 0 on success, -1 on failure
2848 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm
*sm
, u8
*wpa_ie
,
2856 #ifdef CONFIG_TESTING_OPTIONS
2857 if (sm
->test_assoc_ie
) {
2858 wpa_printf(MSG_DEBUG
,
2859 "TESTING: Replace association WPA/RSN IE");
2860 if (*wpa_ie_len
< wpabuf_len(sm
->test_assoc_ie
))
2862 os_memcpy(wpa_ie
, wpabuf_head(sm
->test_assoc_ie
),
2863 wpabuf_len(sm
->test_assoc_ie
));
2864 res
= wpabuf_len(sm
->test_assoc_ie
);
2866 #endif /* CONFIG_TESTING_OPTIONS */
2867 res
= wpa_gen_wpa_ie(sm
, wpa_ie
, *wpa_ie_len
);
2872 wpa_hexdump(MSG_DEBUG
, "WPA: Set own WPA IE default",
2873 wpa_ie
, *wpa_ie_len
);
2875 if (sm
->assoc_wpa_ie
== NULL
) {
2877 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2878 * the correct version of the IE even if PMKSA caching is
2879 * aborted (which would remove PMKID from IE generation).
2881 sm
->assoc_wpa_ie
= os_malloc(*wpa_ie_len
);
2882 if (sm
->assoc_wpa_ie
== NULL
)
2885 os_memcpy(sm
->assoc_wpa_ie
, wpa_ie
, *wpa_ie_len
);
2886 sm
->assoc_wpa_ie_len
= *wpa_ie_len
;
2888 wpa_hexdump(MSG_DEBUG
,
2889 "WPA: Leave previously set WPA IE default",
2890 sm
->assoc_wpa_ie
, sm
->assoc_wpa_ie_len
);
2898 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2899 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2900 * @ie: Pointer to IE data (starting from id)
2902 * Returns: 0 on success, -1 on failure
2904 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2905 * Request frame. The IE will be used to override the default value generated
2906 * with wpa_sm_set_assoc_wpa_ie_default().
2908 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm
*sm
, const u8
*ie
, size_t len
)
2913 os_free(sm
->assoc_wpa_ie
);
2914 if (ie
== NULL
|| len
== 0) {
2915 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2916 "WPA: clearing own WPA/RSN IE");
2917 sm
->assoc_wpa_ie
= NULL
;
2918 sm
->assoc_wpa_ie_len
= 0;
2920 wpa_hexdump(MSG_DEBUG
, "WPA: set own WPA/RSN IE", ie
, len
);
2921 sm
->assoc_wpa_ie
= os_malloc(len
);
2922 if (sm
->assoc_wpa_ie
== NULL
)
2925 os_memcpy(sm
->assoc_wpa_ie
, ie
, len
);
2926 sm
->assoc_wpa_ie_len
= len
;
2934 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2935 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2936 * @ie: Pointer to IE data (starting from id)
2938 * Returns: 0 on success, -1 on failure
2940 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2943 int wpa_sm_set_ap_wpa_ie(struct wpa_sm
*sm
, const u8
*ie
, size_t len
)
2948 os_free(sm
->ap_wpa_ie
);
2949 if (ie
== NULL
|| len
== 0) {
2950 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2951 "WPA: clearing AP WPA IE");
2952 sm
->ap_wpa_ie
= NULL
;
2953 sm
->ap_wpa_ie_len
= 0;
2955 wpa_hexdump(MSG_DEBUG
, "WPA: set AP WPA IE", ie
, len
);
2956 sm
->ap_wpa_ie
= os_malloc(len
);
2957 if (sm
->ap_wpa_ie
== NULL
)
2960 os_memcpy(sm
->ap_wpa_ie
, ie
, len
);
2961 sm
->ap_wpa_ie_len
= len
;
2969 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2970 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2971 * @ie: Pointer to IE data (starting from id)
2973 * Returns: 0 on success, -1 on failure
2975 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2978 int wpa_sm_set_ap_rsn_ie(struct wpa_sm
*sm
, const u8
*ie
, size_t len
)
2983 os_free(sm
->ap_rsn_ie
);
2984 if (ie
== NULL
|| len
== 0) {
2985 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2986 "WPA: clearing AP RSN IE");
2987 sm
->ap_rsn_ie
= NULL
;
2988 sm
->ap_rsn_ie_len
= 0;
2990 wpa_hexdump(MSG_DEBUG
, "WPA: set AP RSN IE", ie
, len
);
2991 sm
->ap_rsn_ie
= os_malloc(len
);
2992 if (sm
->ap_rsn_ie
== NULL
)
2995 os_memcpy(sm
->ap_rsn_ie
, ie
, len
);
2996 sm
->ap_rsn_ie_len
= len
;
3004 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
3005 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3006 * @data: Pointer to data area for parsing results
3007 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
3009 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
3010 * parsed data into data.
3012 int wpa_sm_parse_own_wpa_ie(struct wpa_sm
*sm
, struct wpa_ie_data
*data
)
3017 if (sm
->assoc_wpa_ie
== NULL
) {
3018 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
3019 "WPA: No WPA/RSN IE available from association info");
3022 if (wpa_parse_wpa_ie(sm
->assoc_wpa_ie
, sm
->assoc_wpa_ie_len
, data
))
3028 int wpa_sm_pmksa_cache_list(struct wpa_sm
*sm
, char *buf
, size_t len
)
3030 return pmksa_cache_list(sm
->pmksa
, buf
, len
);
3034 struct rsn_pmksa_cache_entry
* wpa_sm_pmksa_cache_head(struct wpa_sm
*sm
)
3036 return pmksa_cache_head(sm
->pmksa
);
3040 struct rsn_pmksa_cache_entry
*
3041 wpa_sm_pmksa_cache_add_entry(struct wpa_sm
*sm
,
3042 struct rsn_pmksa_cache_entry
* entry
)
3044 return pmksa_cache_add_entry(sm
->pmksa
, entry
);
3048 void wpa_sm_drop_sa(struct wpa_sm
*sm
)
3050 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Clear old PMK and PTK");
3053 os_memset(sm
->pmk
, 0, sizeof(sm
->pmk
));
3054 os_memset(&sm
->ptk
, 0, sizeof(sm
->ptk
));
3055 os_memset(&sm
->tptk
, 0, sizeof(sm
->tptk
));
3056 #ifdef CONFIG_IEEE80211R
3057 os_memset(sm
->xxkey
, 0, sizeof(sm
->xxkey
));
3058 os_memset(sm
->pmk_r0
, 0, sizeof(sm
->pmk_r0
));
3059 os_memset(sm
->pmk_r1
, 0, sizeof(sm
->pmk_r1
));
3060 #endif /* CONFIG_IEEE80211R */
3064 int wpa_sm_has_ptk(struct wpa_sm
*sm
)
3072 void wpa_sm_update_replay_ctr(struct wpa_sm
*sm
, const u8
*replay_ctr
)
3074 os_memcpy(sm
->rx_replay_counter
, replay_ctr
, WPA_REPLAY_COUNTER_LEN
);
3078 void wpa_sm_pmksa_cache_flush(struct wpa_sm
*sm
, void *network_ctx
)
3080 pmksa_cache_flush(sm
->pmksa
, network_ctx
, NULL
, 0);
3085 int wpa_wnmsleep_install_key(struct wpa_sm
*sm
, u8 subelem_id
, u8
*buf
)
3088 u8 keylen
; /* plaintext key len */
3091 if (subelem_id
== WNM_SLEEP_SUBELEM_GTK
) {
3092 struct wpa_gtk_data gd
;
3094 os_memset(&gd
, 0, sizeof(gd
));
3095 keylen
= wpa_cipher_key_len(sm
->group_cipher
);
3096 gd
.key_rsc_len
= wpa_cipher_rsc_len(sm
->group_cipher
);
3097 gd
.alg
= wpa_cipher_to_alg(sm
->group_cipher
);
3098 if (gd
.alg
== WPA_ALG_NONE
) {
3099 wpa_printf(MSG_DEBUG
, "Unsupported group cipher suite");
3104 keyinfo
= WPA_GET_LE16(buf
+ 2);
3105 gd
.gtk_len
= keylen
;
3106 if (gd
.gtk_len
!= buf
[4]) {
3107 wpa_printf(MSG_DEBUG
, "GTK len mismatch len %d vs %d",
3108 gd
.gtk_len
, buf
[4]);
3111 gd
.keyidx
= keyinfo
& 0x03; /* B0 - B1 */
3112 gd
.tx
= wpa_supplicant_gtk_tx_bit_workaround(
3113 sm
, !!(keyinfo
& WPA_KEY_INFO_TXRX
));
3115 os_memcpy(gd
.gtk
, buf
+ 13, gd
.gtk_len
);
3117 wpa_hexdump_key(MSG_DEBUG
, "Install GTK (WNM SLEEP)",
3118 gd
.gtk
, gd
.gtk_len
);
3119 if (wpa_supplicant_install_gtk(sm
, &gd
, key_rsc
)) {
3120 os_memset(&gd
, 0, sizeof(gd
));
3121 wpa_printf(MSG_DEBUG
, "Failed to install the GTK in "
3125 os_memset(&gd
, 0, sizeof(gd
));
3126 #ifdef CONFIG_IEEE80211W
3127 } else if (subelem_id
== WNM_SLEEP_SUBELEM_IGTK
) {
3128 struct wpa_igtk_kde igd
;
3131 os_memset(&igd
, 0, sizeof(igd
));
3132 keylen
= wpa_cipher_key_len(sm
->mgmt_group_cipher
);
3133 os_memcpy(igd
.keyid
, buf
+ 2, 2);
3134 os_memcpy(igd
.pn
, buf
+ 4, 6);
3136 keyidx
= WPA_GET_LE16(igd
.keyid
);
3137 os_memcpy(igd
.igtk
, buf
+ 10, keylen
);
3139 wpa_hexdump_key(MSG_DEBUG
, "Install IGTK (WNM SLEEP)",
3141 if (wpa_sm_set_key(sm
, wpa_cipher_to_alg(sm
->mgmt_group_cipher
),
3142 broadcast_ether_addr
,
3143 keyidx
, 0, igd
.pn
, sizeof(igd
.pn
),
3144 igd
.igtk
, keylen
) < 0) {
3145 wpa_printf(MSG_DEBUG
, "Failed to install the IGTK in "
3147 os_memset(&igd
, 0, sizeof(igd
));
3150 os_memset(&igd
, 0, sizeof(igd
));
3151 #endif /* CONFIG_IEEE80211W */
3153 wpa_printf(MSG_DEBUG
, "Unknown element id");
3159 #endif /* CONFIG_WNM */
3162 #ifdef CONFIG_PEERKEY
3163 int wpa_sm_rx_eapol_peerkey(struct wpa_sm
*sm
, const u8
*src_addr
,
3164 const u8
*buf
, size_t len
)
3166 struct wpa_peerkey
*peerkey
;
3168 for (peerkey
= sm
->peerkey
; peerkey
; peerkey
= peerkey
->next
) {
3169 if (os_memcmp(peerkey
->addr
, src_addr
, ETH_ALEN
) == 0)
3176 wpa_sm_rx_eapol(sm
, src_addr
, buf
, len
);
3180 #endif /* CONFIG_PEERKEY */
3185 int wpa_sm_get_p2p_ip_addr(struct wpa_sm
*sm
, u8
*buf
)
3187 if (sm
== NULL
|| WPA_GET_BE32(sm
->p2p_ip_addr
) == 0)
3189 os_memcpy(buf
, sm
->p2p_ip_addr
, 3 * 4);
3193 #endif /* CONFIG_P2P */
3196 void wpa_sm_set_rx_replay_ctr(struct wpa_sm
*sm
, const u8
*rx_replay_counter
)
3198 if (rx_replay_counter
== NULL
)
3201 os_memcpy(sm
->rx_replay_counter
, rx_replay_counter
,
3202 WPA_REPLAY_COUNTER_LEN
);
3203 sm
->rx_replay_counter_set
= 1;
3204 wpa_printf(MSG_DEBUG
, "Updated key replay counter");
3208 void wpa_sm_set_ptk_kck_kek(struct wpa_sm
*sm
,
3209 const u8
*ptk_kck
, size_t ptk_kck_len
,
3210 const u8
*ptk_kek
, size_t ptk_kek_len
)
3212 if (ptk_kck
&& ptk_kck_len
<= WPA_KCK_MAX_LEN
) {
3213 os_memcpy(sm
->ptk
.kck
, ptk_kck
, ptk_kck_len
);
3214 sm
->ptk
.kck_len
= ptk_kck_len
;
3215 wpa_printf(MSG_DEBUG
, "Updated PTK KCK");
3217 if (ptk_kek
&& ptk_kek_len
<= WPA_KEK_MAX_LEN
) {
3218 os_memcpy(sm
->ptk
.kek
, ptk_kek
, ptk_kek_len
);
3219 sm
->ptk
.kek_len
= ptk_kek_len
;
3220 wpa_printf(MSG_DEBUG
, "Updated PTK KEK");
3226 #ifdef CONFIG_TESTING_OPTIONS
3227 void wpa_sm_set_test_assoc_ie(struct wpa_sm
*sm
, struct wpabuf
*buf
)
3229 wpabuf_free(sm
->test_assoc_ie
);
3230 sm
->test_assoc_ie
= buf
;
3232 #endif /* CONFIG_TESTING_OPTIONS */
3237 struct wpabuf
* fils_build_auth(struct wpa_sm
*sm
)
3239 struct wpabuf
*buf
= NULL
;
3240 struct wpabuf
*erp_msg
;
3242 erp_msg
= eapol_sm_build_erp_reauth_start(sm
->eapol
);
3243 if (!erp_msg
&& !sm
->cur_pmksa
) {
3244 wpa_printf(MSG_DEBUG
,
3245 "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS");
3249 wpa_printf(MSG_DEBUG
, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)",
3250 erp_msg
!= NULL
, sm
->cur_pmksa
!= NULL
);
3252 sm
->fils_completed
= 0;
3254 if (!sm
->assoc_wpa_ie
) {
3255 wpa_printf(MSG_INFO
, "FILS: No own RSN IE set for FILS");
3259 if (random_get_bytes(sm
->fils_nonce
, FILS_NONCE_LEN
) < 0 ||
3260 random_get_bytes(sm
->fils_session
, FILS_SESSION_LEN
) < 0)
3263 wpa_hexdump(MSG_DEBUG
, "FILS: Generated FILS Nonce",
3264 sm
->fils_nonce
, FILS_NONCE_LEN
);
3265 wpa_hexdump(MSG_DEBUG
, "FILS: Generated FILS Session",
3266 sm
->fils_session
, FILS_SESSION_LEN
);
3268 buf
= wpabuf_alloc(1000 + sm
->assoc_wpa_ie_len
);
3272 /* Fields following the Authentication algorithm number field */
3274 /* Authentication Transaction seq# */
3275 wpabuf_put_le16(buf
, 1);
3278 wpabuf_put_le16(buf
, WLAN_STATUS_SUCCESS
);
3280 /* TODO: Finite Cyclic Group when using PK or PFS */
3281 /* TODO: Element when using PK or PFS */
3284 wpa_hexdump(MSG_DEBUG
, "FILS: RSNE in FILS Authentication frame",
3285 sm
->assoc_wpa_ie
, sm
->assoc_wpa_ie_len
);
3286 wpabuf_put_data(buf
, sm
->assoc_wpa_ie
, sm
->assoc_wpa_ie_len
);
3288 /* TODO: MDE when using FILS for FT initial association */
3289 /* TODO: FTE when using FILS for FT initial association */
3292 wpabuf_put_u8(buf
, WLAN_EID_EXTENSION
); /* Element ID */
3293 wpabuf_put_u8(buf
, 1 + FILS_NONCE_LEN
); /* Length */
3294 /* Element ID Extension */
3295 wpabuf_put_u8(buf
, WLAN_EID_EXT_FILS_NONCE
);
3296 wpabuf_put_data(buf
, sm
->fils_nonce
, FILS_NONCE_LEN
);
3299 wpabuf_put_u8(buf
, WLAN_EID_EXTENSION
); /* Element ID */
3300 wpabuf_put_u8(buf
, 1 + FILS_SESSION_LEN
); /* Length */
3301 /* Element ID Extension */
3302 wpabuf_put_u8(buf
, WLAN_EID_EXT_FILS_SESSION
);
3303 wpabuf_put_data(buf
, sm
->fils_session
, FILS_SESSION_LEN
);
3305 /* FILS Wrapped Data */
3306 sm
->fils_erp_pmkid_set
= 0;
3308 wpabuf_put_u8(buf
, WLAN_EID_EXTENSION
); /* Element ID */
3309 wpabuf_put_u8(buf
, 1 + wpabuf_len(erp_msg
)); /* Length */
3310 /* Element ID Extension */
3311 wpabuf_put_u8(buf
, WLAN_EID_EXT_FILS_WRAPPED_DATA
);
3312 wpabuf_put_buf(buf
, erp_msg
);
3313 /* Calculate pending PMKID here so that we do not need to
3314 * maintain a copy of the EAP-Initiate/Reauth message. */
3315 if (fils_pmkid_erp(sm
->key_mgmt
, wpabuf_head(erp_msg
),
3316 wpabuf_len(erp_msg
),
3317 sm
->fils_erp_pmkid
) == 0)
3318 sm
->fils_erp_pmkid_set
= 1;
3321 wpa_hexdump_buf(MSG_DEBUG
, "RSN: FILS fields for Authentication frame",
3325 wpabuf_free(erp_msg
);
3330 int fils_process_auth(struct wpa_sm
*sm
, const u8
*data
, size_t len
)
3332 const u8
*pos
, *end
;
3333 struct ieee802_11_elems elems
;
3334 struct wpa_ie_data rsn
;
3335 int pmkid_match
= 0;
3336 u8 ick
[FILS_ICK_MAX_LEN
];
3340 wpa_hexdump(MSG_DEBUG
, "FILS: Authentication frame fields",
3345 /* TODO: Finite Cyclic Group when using PK or PFS */
3346 /* TODO: Element when using PK or PFS */
3348 wpa_hexdump(MSG_DEBUG
, "FILS: Remaining IEs", pos
, end
- pos
);
3349 if (ieee802_11_parse_elems(pos
, end
- pos
, &elems
, 1) == ParseFailed
) {
3350 wpa_printf(MSG_DEBUG
, "FILS: Could not parse elements");
3355 wpa_hexdump(MSG_DEBUG
, "FILS: RSN element", elems
.rsn_ie
,
3357 if (!elems
.rsn_ie
||
3358 wpa_parse_wpa_ie_rsn(elems
.rsn_ie
- 2, elems
.rsn_ie_len
+ 2,
3360 wpa_printf(MSG_DEBUG
, "FILS: No RSN element");
3364 if (!elems
.fils_nonce
) {
3365 wpa_printf(MSG_DEBUG
, "FILS: No FILS Nonce field");
3368 os_memcpy(sm
->fils_anonce
, elems
.fils_nonce
, FILS_NONCE_LEN
);
3369 wpa_hexdump(MSG_DEBUG
, "FILS: ANonce", sm
->fils_anonce
, FILS_NONCE_LEN
);
3371 /* TODO: MDE when using FILS+FT */
3372 /* TODO: FTE when using FILS+FT */
3375 if (rsn
.pmkid
&& rsn
.num_pmkid
> 0) {
3376 wpa_hexdump(MSG_DEBUG
, "FILS: PMKID List",
3377 rsn
.pmkid
, rsn
.num_pmkid
* PMKID_LEN
);
3379 if (rsn
.num_pmkid
!= 1) {
3380 wpa_printf(MSG_DEBUG
, "FILS: Invalid PMKID selection");
3383 wpa_hexdump(MSG_DEBUG
, "FILS: PMKID", rsn
.pmkid
, PMKID_LEN
);
3384 if (os_memcmp(sm
->cur_pmksa
->pmkid
, rsn
.pmkid
, PMKID_LEN
) != 0)
3386 wpa_printf(MSG_DEBUG
, "FILS: PMKID mismatch");
3387 wpa_hexdump(MSG_DEBUG
, "FILS: Expected PMKID",
3388 sm
->cur_pmksa
->pmkid
, PMKID_LEN
);
3391 wpa_printf(MSG_DEBUG
,
3392 "FILS: Matching PMKID - continue using PMKSA caching");
3395 if (!pmkid_match
&& sm
->cur_pmksa
) {
3396 wpa_printf(MSG_DEBUG
,
3397 "FILS: No PMKID match - cannot use cached PMKSA entry");
3398 sm
->cur_pmksa
= NULL
;
3402 if (!elems
.fils_session
) {
3403 wpa_printf(MSG_DEBUG
, "FILS: No FILS Session element");
3406 wpa_hexdump(MSG_DEBUG
, "FILS: FILS Session", elems
.fils_session
,
3408 if (os_memcmp(sm
->fils_session
, elems
.fils_session
, FILS_SESSION_LEN
)
3410 wpa_printf(MSG_DEBUG
, "FILS: Session mismatch");
3411 wpa_hexdump(MSG_DEBUG
, "FILS: Expected FILS Session",
3412 sm
->fils_session
, FILS_SESSION_LEN
);
3416 /* FILS Wrapped Data */
3417 if (!sm
->cur_pmksa
&& elems
.fils_wrapped_data
) {
3418 u8 rmsk
[ERP_MAX_KEY_LEN
];
3421 wpa_hexdump(MSG_DEBUG
, "FILS: Wrapped Data",
3422 elems
.fils_wrapped_data
,
3423 elems
.fils_wrapped_data_len
);
3424 eapol_sm_process_erp_finish(sm
->eapol
, elems
.fils_wrapped_data
,
3425 elems
.fils_wrapped_data_len
);
3426 if (eapol_sm_failed(sm
->eapol
))
3429 rmsk_len
= ERP_MAX_KEY_LEN
;
3430 res
= eapol_sm_get_key(sm
->eapol
, rmsk
, rmsk_len
);
3431 if (res
== PMK_LEN
) {
3433 res
= eapol_sm_get_key(sm
->eapol
, rmsk
, rmsk_len
);
3438 res
= fils_rmsk_to_pmk(sm
->key_mgmt
, rmsk
, rmsk_len
,
3439 sm
->fils_nonce
, sm
->fils_anonce
, NULL
, 0,
3440 sm
->pmk
, &sm
->pmk_len
);
3441 os_memset(rmsk
, 0, sizeof(rmsk
));
3443 if (!sm
->fils_erp_pmkid_set
) {
3444 wpa_printf(MSG_DEBUG
, "FILS: PMKID not available");
3447 wpa_hexdump(MSG_DEBUG
, "FILS: PMKID", sm
->fils_erp_pmkid
,
3449 wpa_printf(MSG_DEBUG
, "FILS: ERP processing succeeded - add PMKSA cache entry for the result");
3450 sm
->cur_pmksa
= pmksa_cache_add(sm
->pmksa
, sm
->pmk
, sm
->pmk_len
,
3451 sm
->fils_erp_pmkid
, NULL
, 0,
3452 sm
->bssid
, sm
->own_addr
,
3453 sm
->network_ctx
, sm
->key_mgmt
);
3456 if (!sm
->cur_pmksa
) {
3457 wpa_printf(MSG_DEBUG
,
3458 "FILS: No remaining options to continue FILS authentication");
3462 if (fils_pmk_to_ptk(sm
->pmk
, sm
->pmk_len
, sm
->own_addr
, sm
->bssid
,
3463 sm
->fils_nonce
, sm
->fils_anonce
, &sm
->ptk
,
3464 ick
, &ick_len
, sm
->key_mgmt
, sm
->pairwise_cipher
) <
3466 wpa_printf(MSG_DEBUG
, "FILS: Failed to derive PTK");
3471 os_memset(&sm
->tptk
, 0, sizeof(sm
->tptk
));
3473 res
= fils_key_auth_sk(ick
, ick_len
, sm
->fils_nonce
,
3474 sm
->fils_anonce
, sm
->own_addr
, sm
->bssid
,
3475 NULL
, 0, NULL
, 0, /* TODO: SK+PFS */
3476 sm
->key_mgmt
, sm
->fils_key_auth_sta
,
3477 sm
->fils_key_auth_ap
,
3478 &sm
->fils_key_auth_len
);
3479 os_memset(ick
, 0, sizeof(ick
));
3484 struct wpabuf
* fils_build_assoc_req(struct wpa_sm
*sm
, const u8
**kek
,
3485 size_t *kek_len
, const u8
**snonce
,
3487 const struct wpabuf
**hlp
,
3488 unsigned int num_hlp
)
3495 for (i
= 0; hlp
&& i
< num_hlp
; i
++)
3496 len
+= 10 + wpabuf_len(hlp
[i
]);
3497 buf
= wpabuf_alloc(len
);
3502 wpabuf_put_u8(buf
, WLAN_EID_EXTENSION
); /* Element ID */
3503 wpabuf_put_u8(buf
, 1 + FILS_SESSION_LEN
); /* Length */
3504 /* Element ID Extension */
3505 wpabuf_put_u8(buf
, WLAN_EID_EXT_FILS_SESSION
);
3506 wpabuf_put_data(buf
, sm
->fils_session
, FILS_SESSION_LEN
);
3508 /* Everything after FILS Session element gets encrypted in the driver
3509 * with KEK. The buffer returned from here is the plaintext version. */
3511 /* TODO: FILS Public Key */
3513 /* FILS Key Confirm */
3514 wpabuf_put_u8(buf
, WLAN_EID_EXTENSION
); /* Element ID */
3515 wpabuf_put_u8(buf
, 1 + sm
->fils_key_auth_len
); /* Length */
3516 /* Element ID Extension */
3517 wpabuf_put_u8(buf
, WLAN_EID_EXT_FILS_KEY_CONFIRM
);
3518 wpabuf_put_data(buf
, sm
->fils_key_auth_sta
, sm
->fils_key_auth_len
);
3520 /* FILS HLP Container */
3521 for (i
= 0; hlp
&& i
< num_hlp
; i
++) {
3522 const u8
*pos
= wpabuf_head(hlp
[i
]);
3523 size_t left
= wpabuf_len(hlp
[i
]);
3525 wpabuf_put_u8(buf
, WLAN_EID_EXTENSION
); /* Element ID */
3530 wpabuf_put_u8(buf
, len
); /* Length */
3531 /* Element ID Extension */
3532 wpabuf_put_u8(buf
, WLAN_EID_EXT_FILS_HLP_CONTAINER
);
3533 /* Destination MAC Address, Source MAC Address, HLP Packet.
3534 * HLP Packet is in MSDU format (i.e., included the LLC/SNAP
3535 * header when LPD is used). */
3536 wpabuf_put_data(buf
, pos
, len
- 1);
3540 wpabuf_put_u8(buf
, WLAN_EID_FRAGMENT
);
3541 len
= left
> 255 ? 255 : left
;
3542 wpabuf_put_u8(buf
, len
);
3543 wpabuf_put_data(buf
, pos
, len
);
3549 /* TODO: FILS IP Address Assignment */
3551 wpa_hexdump_buf(MSG_DEBUG
, "FILS: Association Request plaintext", buf
);
3554 *kek_len
= sm
->ptk
.kek_len
;
3555 wpa_hexdump_key(MSG_DEBUG
, "FILS: KEK for AEAD", *kek
, *kek_len
);
3556 *snonce
= sm
->fils_nonce
;
3557 wpa_hexdump(MSG_DEBUG
, "FILS: SNonce for AEAD AAD",
3558 *snonce
, FILS_NONCE_LEN
);
3559 *anonce
= sm
->fils_anonce
;
3560 wpa_hexdump(MSG_DEBUG
, "FILS: ANonce for AEAD AAD",
3561 *anonce
, FILS_NONCE_LEN
);
3567 int fils_process_assoc_resp(struct wpa_sm
*sm
, const u8
*resp
, size_t len
)
3569 const struct ieee80211_mgmt
*mgmt
;
3570 const u8
*end
, *ie_start
;
3571 struct ieee802_11_elems elems
;
3574 struct wpa_gtk_data gd
;
3576 struct wpa_eapol_ie_parse kde
;
3578 if (!sm
|| !sm
->ptk_set
) {
3579 wpa_printf(MSG_DEBUG
, "FILS: No KEK available");
3583 if (!wpa_key_mgmt_fils(sm
->key_mgmt
)) {
3584 wpa_printf(MSG_DEBUG
, "FILS: Not a FILS AKM");
3588 wpa_hexdump(MSG_DEBUG
, "FILS: (Re)Association Response frame",
3591 mgmt
= (const struct ieee80211_mgmt
*) resp
;
3592 if (len
< IEEE80211_HDRLEN
+ sizeof(mgmt
->u
.assoc_resp
))
3596 /* Same offset for Association Response and Reassociation Response */
3597 ie_start
= mgmt
->u
.assoc_resp
.variable
;
3599 if (ieee802_11_parse_elems(ie_start
, end
- ie_start
, &elems
, 1) ==
3601 wpa_printf(MSG_DEBUG
,
3602 "FILS: Failed to parse decrypted elements");
3606 if (!elems
.fils_session
) {
3607 wpa_printf(MSG_DEBUG
, "FILS: No FILS Session element");
3610 if (os_memcmp(elems
.fils_session
, sm
->fils_session
,
3611 FILS_SESSION_LEN
) != 0) {
3612 wpa_printf(MSG_DEBUG
, "FILS: FILS Session mismatch");
3613 wpa_hexdump(MSG_DEBUG
, "FILS: Received FILS Session",
3614 elems
.fils_session
, FILS_SESSION_LEN
);
3615 wpa_hexdump(MSG_DEBUG
, "FILS: Expected FILS Session",
3616 sm
->fils_session
, FILS_SESSION_LEN
);
3619 /* TODO: FILS Public Key */
3621 if (!elems
.fils_key_confirm
) {
3622 wpa_printf(MSG_DEBUG
, "FILS: No FILS Key Confirm element");
3625 if (elems
.fils_key_confirm_len
!= sm
->fils_key_auth_len
) {
3626 wpa_printf(MSG_DEBUG
,
3627 "FILS: Unexpected Key-Auth length %d (expected %d)",
3628 elems
.fils_key_confirm_len
,
3629 (int) sm
->fils_key_auth_len
);
3632 if (os_memcmp(elems
.fils_key_confirm
, sm
->fils_key_auth_ap
,
3633 sm
->fils_key_auth_len
) != 0) {
3634 wpa_printf(MSG_DEBUG
, "FILS: Key-Auth mismatch");
3635 wpa_hexdump(MSG_DEBUG
, "FILS: Received Key-Auth",
3636 elems
.fils_key_confirm
,
3637 elems
.fils_key_confirm_len
);
3638 wpa_hexdump(MSG_DEBUG
, "FILS: Expected Key-Auth",
3639 sm
->fils_key_auth_ap
, sm
->fils_key_auth_len
);
3644 if (!elems
.key_delivery
) {
3645 wpa_printf(MSG_DEBUG
, "FILS: No Key Delivery element");
3649 /* Parse GTK and set the key to the driver */
3650 os_memset(&gd
, 0, sizeof(gd
));
3651 if (wpa_supplicant_parse_ies(elems
.key_delivery
+ WPA_KEY_RSC_LEN
,
3652 elems
.key_delivery_len
- WPA_KEY_RSC_LEN
,
3654 wpa_printf(MSG_DEBUG
, "FILS: Failed to parse KDEs");
3658 wpa_printf(MSG_DEBUG
, "FILS: No GTK KDE");
3661 maxkeylen
= gd
.gtk_len
= kde
.gtk_len
- 2;
3662 if (wpa_supplicant_check_group_cipher(sm
, sm
->group_cipher
,
3663 gd
.gtk_len
, maxkeylen
,
3664 &gd
.key_rsc_len
, &gd
.alg
))
3667 wpa_hexdump_key(MSG_DEBUG
, "FILS: Received GTK", kde
.gtk
, kde
.gtk_len
);
3668 gd
.keyidx
= kde
.gtk
[0] & 0x3;
3669 gd
.tx
= wpa_supplicant_gtk_tx_bit_workaround(sm
,
3670 !!(kde
.gtk
[0] & BIT(2)));
3671 if (kde
.gtk_len
- 2 > sizeof(gd
.gtk
)) {
3672 wpa_printf(MSG_DEBUG
, "FILS: Too long GTK in GTK KDE (len=%lu)",
3673 (unsigned long) kde
.gtk_len
- 2);
3676 os_memcpy(gd
.gtk
, kde
.gtk
+ 2, kde
.gtk_len
- 2);
3678 wpa_printf(MSG_DEBUG
, "FILS: Set GTK to driver");
3679 if (wpa_supplicant_install_gtk(sm
, &gd
, elems
.key_delivery
) < 0) {
3680 wpa_printf(MSG_DEBUG
, "FILS: Failed to set GTK");
3684 if (ieee80211w_set_keys(sm
, &kde
) < 0) {
3685 wpa_printf(MSG_DEBUG
, "FILS: Failed to set IGTK");
3689 alg
= wpa_cipher_to_alg(sm
->pairwise_cipher
);
3690 keylen
= wpa_cipher_key_len(sm
->pairwise_cipher
);
3691 rsclen
= wpa_cipher_rsc_len(sm
->pairwise_cipher
);
3692 wpa_hexdump_key(MSG_DEBUG
, "FILS: Set TK to driver",
3693 sm
->ptk
.tk
, keylen
);
3694 if (wpa_sm_set_key(sm
, alg
, sm
->bssid
, 0, 1, null_rsc
, rsclen
,
3695 sm
->ptk
.tk
, keylen
) < 0) {
3696 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
3697 "FILS: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
3699 alg
, keylen
, MAC2STR(sm
->bssid
));
3703 /* TODO: TK could be cleared after auth frame exchange now that driver
3704 * takes care of association frame encryption/decryption. */
3705 /* TK is not needed anymore in supplicant */
3706 os_memset(sm
->ptk
.tk
, 0, WPA_TK_MAX_LEN
);
3708 /* TODO: FILS HLP Container */
3710 /* TODO: FILS IP Address Assignment */
3712 wpa_printf(MSG_DEBUG
, "FILS: Auth+Assoc completed successfully");
3713 sm
->fils_completed
= 1;
3720 #endif /* CONFIG_FILS */
3723 int wpa_fils_is_completed(struct wpa_sm
*sm
)
3726 return sm
&& sm
->fils_completed
;
3727 #else /* CONFIG_FILS */
3729 #endif /* CONFIG_FILS */