2 * WPA Supplicant - WPA state machine and EAPOL-Key processing
3 * Copyright (c) 2003-2017, 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 "crypto/sha256.h"
19 #include "crypto/sha384.h"
20 #include "crypto/sha512.h"
21 #include "common/ieee802_11_defs.h"
22 #include "common/ieee802_11_common.h"
23 #include "eap_common/eap_defs.h"
24 #include "eapol_supp/eapol_supp_sm.h"
28 #include "pmksa_cache.h"
33 static const u8 null_rsc
[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
37 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
38 * @sm: Pointer to WPA state machine data from wpa_sm_init()
39 * @ptk: PTK for Key Confirmation/Encryption Key
40 * @ver: Version field from Key Info
41 * @dest: Destination address for the frame
42 * @proto: Ethertype (usually ETH_P_EAPOL)
43 * @msg: EAPOL-Key message
44 * @msg_len: Length of message
45 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
46 * Returns: >= 0 on success, < 0 on failure
48 int wpa_eapol_key_send(struct wpa_sm
*sm
, struct wpa_ptk
*ptk
,
49 int ver
, const u8
*dest
, u16 proto
,
50 u8
*msg
, size_t msg_len
, u8
*key_mic
)
53 size_t mic_len
= wpa_mic_len(sm
->key_mgmt
, sm
->pmk_len
);
55 wpa_printf(MSG_DEBUG
, "WPA: Send EAPOL-Key frame to " MACSTR
56 " ver=%d mic_len=%d key_mgmt=0x%x",
57 MAC2STR(dest
), ver
, (int) mic_len
, sm
->key_mgmt
);
58 if (is_zero_ether_addr(dest
) && is_zero_ether_addr(sm
->bssid
)) {
60 * Association event was not yet received; try to fetch
61 * BSSID from the driver.
63 if (wpa_sm_get_bssid(sm
, sm
->bssid
) < 0) {
64 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
65 "WPA: Failed to read BSSID for "
66 "EAPOL-Key destination address");
69 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
70 "WPA: Use BSSID (" MACSTR
71 ") as the destination for EAPOL-Key",
77 if (key_mic
&& (!ptk
|| !ptk
->kck_len
))
81 wpa_eapol_key_mic(ptk
->kck
, ptk
->kck_len
, sm
->key_mgmt
, ver
,
82 msg
, msg_len
, key_mic
)) {
83 wpa_msg(sm
->ctx
->msg_ctx
, MSG_ERROR
,
84 "WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC",
89 wpa_hexdump_key(MSG_DEBUG
, "WPA: KCK",
90 ptk
->kck
, ptk
->kck_len
);
91 wpa_hexdump(MSG_DEBUG
, "WPA: Derived Key MIC",
95 /* AEAD cipher - Key MIC field not used */
96 struct ieee802_1x_hdr
*s_hdr
, *hdr
;
97 struct wpa_eapol_key
*s_key
, *key
;
98 u8
*buf
, *s_key_data
, *key_data
;
99 size_t buf_len
= msg_len
+ AES_BLOCK_SIZE
;
105 if (!ptk
|| !ptk
->kek_len
)
108 key_data_len
= msg_len
- sizeof(struct ieee802_1x_hdr
) -
109 sizeof(struct wpa_eapol_key
) - 2;
111 buf
= os_malloc(buf_len
);
115 os_memcpy(buf
, msg
, msg_len
);
116 hdr
= (struct ieee802_1x_hdr
*) buf
;
117 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
118 key_data
= ((u8
*) (key
+ 1)) + 2;
120 /* Update EAPOL header to include AES-SIV overhead */
121 eapol_len
= be_to_host16(hdr
->length
);
122 eapol_len
+= AES_BLOCK_SIZE
;
123 hdr
->length
= host_to_be16(eapol_len
);
125 /* Update Key Data Length field to include AES-SIV overhead */
126 WPA_PUT_BE16((u8
*) (key
+ 1), AES_BLOCK_SIZE
+ key_data_len
);
128 s_hdr
= (struct ieee802_1x_hdr
*) msg
;
129 s_key
= (struct wpa_eapol_key
*) (s_hdr
+ 1);
130 s_key_data
= ((u8
*) (s_key
+ 1)) + 2;
132 wpa_hexdump_key(MSG_DEBUG
, "WPA: Plaintext Key Data",
133 s_key_data
, key_data_len
);
135 wpa_hexdump_key(MSG_DEBUG
, "WPA: KEK", ptk
->kek
, ptk
->kek_len
);
136 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
137 * to Key Data (exclusive). */
139 aad_len
[0] = key_data
- buf
;
140 if (aes_siv_encrypt(ptk
->kek
, ptk
->kek_len
,
141 s_key_data
, key_data_len
,
142 1, aad
, aad_len
, key_data
) < 0) {
147 wpa_hexdump(MSG_DEBUG
, "WPA: Encrypted Key Data from SIV",
148 key_data
, AES_BLOCK_SIZE
+ key_data_len
);
153 #else /* CONFIG_FILS */
155 #endif /* CONFIG_FILS */
158 wpa_hexdump(MSG_MSGDUMP
, "WPA: TX EAPOL-Key", msg
, msg_len
);
159 ret
= wpa_sm_ether_send(sm
, dest
, proto
, msg
, msg_len
);
160 eapol_sm_notify_tx_eapol_key(sm
->eapol
);
168 * wpa_sm_key_request - Send EAPOL-Key Request
169 * @sm: Pointer to WPA state machine data from wpa_sm_init()
170 * @error: Indicate whether this is an Michael MIC error report
171 * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
173 * Send an EAPOL-Key Request to the current authenticator. This function is
174 * used to request rekeying and it is usually called when a local Michael MIC
175 * failure is detected.
177 void wpa_sm_key_request(struct wpa_sm
*sm
, int error
, int pairwise
)
179 size_t mic_len
, hdrlen
, rlen
;
180 struct wpa_eapol_key
*reply
;
182 u8 bssid
[ETH_ALEN
], *rbuf
, *key_mic
, *mic
;
184 if (sm
->key_mgmt
== WPA_KEY_MGMT_OSEN
||
185 wpa_key_mgmt_suite_b(sm
->key_mgmt
))
186 ver
= WPA_KEY_INFO_TYPE_AKM_DEFINED
;
187 else if (wpa_key_mgmt_ft(sm
->key_mgmt
) ||
188 wpa_key_mgmt_sha256(sm
->key_mgmt
))
189 ver
= WPA_KEY_INFO_TYPE_AES_128_CMAC
;
190 else if (sm
->pairwise_cipher
!= WPA_CIPHER_TKIP
)
191 ver
= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
;
193 ver
= WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
;
195 if (wpa_sm_get_bssid(sm
, bssid
) < 0) {
196 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
197 "Failed to read BSSID for EAPOL-Key request");
201 mic_len
= wpa_mic_len(sm
->key_mgmt
, sm
->pmk_len
);
202 hdrlen
= sizeof(*reply
) + mic_len
+ 2;
203 rbuf
= wpa_sm_alloc_eapol(sm
, IEEE802_1X_TYPE_EAPOL_KEY
, NULL
,
204 hdrlen
, &rlen
, (void *) &reply
);
208 reply
->type
= (sm
->proto
== WPA_PROTO_RSN
||
209 sm
->proto
== WPA_PROTO_OSEN
) ?
210 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
211 key_info
= WPA_KEY_INFO_REQUEST
| ver
;
213 key_info
|= WPA_KEY_INFO_SECURE
;
214 if (sm
->ptk_set
&& mic_len
)
215 key_info
|= WPA_KEY_INFO_MIC
;
217 key_info
|= WPA_KEY_INFO_ERROR
;
219 key_info
|= WPA_KEY_INFO_KEY_TYPE
;
220 WPA_PUT_BE16(reply
->key_info
, key_info
);
221 WPA_PUT_BE16(reply
->key_length
, 0);
222 os_memcpy(reply
->replay_counter
, sm
->request_counter
,
223 WPA_REPLAY_COUNTER_LEN
);
224 inc_byte_array(sm
->request_counter
, WPA_REPLAY_COUNTER_LEN
);
226 mic
= (u8
*) (reply
+ 1);
227 WPA_PUT_BE16(mic
+ mic_len
, 0);
228 if (!(key_info
& WPA_KEY_INFO_MIC
))
233 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
234 "WPA: Sending EAPOL-Key Request (error=%d "
235 "pairwise=%d ptk_set=%d len=%lu)",
236 error
, pairwise
, sm
->ptk_set
, (unsigned long) rlen
);
237 wpa_eapol_key_send(sm
, &sm
->ptk
, ver
, bssid
, ETH_P_EAPOL
, rbuf
, rlen
,
242 static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm
*sm
)
244 #ifdef CONFIG_IEEE80211R
245 if (sm
->key_mgmt
== WPA_KEY_MGMT_FT_IEEE8021X
) {
246 if (wpa_sm_key_mgmt_set_pmk(sm
, sm
->xxkey
, sm
->xxkey_len
))
247 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
248 "RSN: Cannot set low order 256 bits of MSK for key management offload");
250 #endif /* CONFIG_IEEE80211R */
251 if (wpa_sm_key_mgmt_set_pmk(sm
, sm
->pmk
, sm
->pmk_len
))
252 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
253 "RSN: Cannot set PMK for key management offload");
254 #ifdef CONFIG_IEEE80211R
256 #endif /* CONFIG_IEEE80211R */
260 static int wpa_supplicant_get_pmk(struct wpa_sm
*sm
,
261 const unsigned char *src_addr
,
264 int abort_cached
= 0;
266 if (pmkid
&& !sm
->cur_pmksa
) {
267 /* When using drivers that generate RSN IE, wpa_supplicant may
268 * not have enough time to get the association information
269 * event before receiving this 1/4 message, so try to find a
270 * matching PMKSA cache entry here. */
271 sm
->cur_pmksa
= pmksa_cache_get(sm
->pmksa
, src_addr
, pmkid
,
274 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
275 "RSN: found matching PMKID from PMKSA cache");
277 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
278 "RSN: no matching PMKID found");
283 if (pmkid
&& sm
->cur_pmksa
&&
284 os_memcmp_const(pmkid
, sm
->cur_pmksa
->pmkid
, PMKID_LEN
) == 0) {
285 wpa_hexdump(MSG_DEBUG
, "RSN: matched PMKID", pmkid
, PMKID_LEN
);
286 wpa_sm_set_pmk_from_pmksa(sm
);
287 wpa_hexdump_key(MSG_DEBUG
, "RSN: PMK from PMKSA cache",
288 sm
->pmk
, sm
->pmk_len
);
289 eapol_sm_notify_cached(sm
->eapol
);
290 #ifdef CONFIG_IEEE80211R
292 #endif /* CONFIG_IEEE80211R */
293 } else if (wpa_key_mgmt_wpa_ieee8021x(sm
->key_mgmt
) && sm
->eapol
) {
296 if (wpa_key_mgmt_sha384(sm
->key_mgmt
))
297 pmk_len
= PMK_LEN_SUITE_B_192
;
300 res
= eapol_sm_get_key(sm
->eapol
, sm
->pmk
, pmk_len
);
302 if (pmk_len
== PMK_LEN
) {
304 * EAP-LEAP is an exception from other EAP
305 * methods: it uses only 16-byte PMK.
307 res
= eapol_sm_get_key(sm
->eapol
, sm
->pmk
, 16);
311 #ifdef CONFIG_IEEE80211R
313 if (eapol_sm_get_key(sm
->eapol
, buf
, 2 * PMK_LEN
) == 0)
315 os_memcpy(sm
->xxkey
, buf
+ PMK_LEN
, PMK_LEN
);
316 sm
->xxkey_len
= PMK_LEN
;
317 os_memset(buf
, 0, sizeof(buf
));
319 #endif /* CONFIG_IEEE80211R */
322 struct rsn_pmksa_cache_entry
*sa
= NULL
;
323 const u8
*fils_cache_id
= NULL
;
326 if (sm
->fils_cache_id_set
)
327 fils_cache_id
= sm
->fils_cache_id
;
328 #endif /* CONFIG_FILS */
330 wpa_hexdump_key(MSG_DEBUG
, "WPA: PMK from EAPOL state "
331 "machines", sm
->pmk
, pmk_len
);
332 sm
->pmk_len
= pmk_len
;
333 wpa_supplicant_key_mgmt_set_pmk(sm
);
334 if (sm
->proto
== WPA_PROTO_RSN
&&
335 !wpa_key_mgmt_suite_b(sm
->key_mgmt
) &&
336 !wpa_key_mgmt_ft(sm
->key_mgmt
)) {
337 sa
= pmksa_cache_add(sm
->pmksa
,
338 sm
->pmk
, pmk_len
, NULL
,
340 src_addr
, sm
->own_addr
,
345 if (!sm
->cur_pmksa
&& pmkid
&&
346 pmksa_cache_get(sm
->pmksa
, src_addr
, pmkid
, NULL
))
348 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
349 "RSN: the new PMK matches with the "
352 } else if (sa
&& !sm
->cur_pmksa
&& pmkid
) {
354 * It looks like the authentication server
355 * derived mismatching MSK. This should not
356 * really happen, but bugs happen.. There is not
357 * much we can do here without knowing what
358 * exactly caused the server to misbehave.
360 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
361 "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
368 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
369 "WPA: Failed to get master session key from "
370 "EAPOL state machines - key handshake "
373 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
374 "RSN: Cancelled PMKSA caching "
376 sm
->cur_pmksa
= NULL
;
378 } else if (!abort_cached
) {
384 if (abort_cached
&& wpa_key_mgmt_wpa_ieee8021x(sm
->key_mgmt
) &&
385 !wpa_key_mgmt_suite_b(sm
->key_mgmt
) &&
386 !wpa_key_mgmt_ft(sm
->key_mgmt
) && sm
->key_mgmt
!= WPA_KEY_MGMT_OSEN
)
388 /* Send EAPOL-Start to trigger full EAP authentication. */
392 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
393 "RSN: no PMKSA entry found - trigger "
394 "full EAP authentication");
395 buf
= wpa_sm_alloc_eapol(sm
, IEEE802_1X_TYPE_EAPOL_START
,
396 NULL
, 0, &buflen
, NULL
);
398 wpa_sm_ether_send(sm
, sm
->bssid
, ETH_P_EAPOL
,
412 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
413 * @sm: Pointer to WPA state machine data from wpa_sm_init()
414 * @dst: Destination address for the frame
415 * @key: Pointer to the EAPOL-Key frame header
416 * @ver: Version bits from EAPOL-Key Key Info
417 * @nonce: Nonce value for the EAPOL-Key frame
418 * @wpa_ie: WPA/RSN IE
419 * @wpa_ie_len: Length of the WPA/RSN IE
420 * @ptk: PTK to use for keyed hash and encryption
421 * Returns: >= 0 on success, < 0 on failure
423 int wpa_supplicant_send_2_of_4(struct wpa_sm
*sm
, const unsigned char *dst
,
424 const struct wpa_eapol_key
*key
,
425 int ver
, const u8
*nonce
,
426 const u8
*wpa_ie
, size_t wpa_ie_len
,
429 size_t mic_len
, hdrlen
, rlen
;
430 struct wpa_eapol_key
*reply
;
432 u8
*rsn_ie_buf
= NULL
;
435 if (wpa_ie
== NULL
) {
436 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
, "WPA: No wpa_ie set - "
437 "cannot generate msg 2/4");
441 #ifdef CONFIG_IEEE80211R
442 if (wpa_key_mgmt_ft(sm
->key_mgmt
)) {
446 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
447 * FTIE from (Re)Association Response.
449 rsn_ie_buf
= os_malloc(wpa_ie_len
+ 2 + 2 + PMKID_LEN
+
450 sm
->assoc_resp_ies_len
);
451 if (rsn_ie_buf
== NULL
)
453 os_memcpy(rsn_ie_buf
, wpa_ie
, wpa_ie_len
);
454 res
= wpa_insert_pmkid(rsn_ie_buf
, &wpa_ie_len
,
461 if (sm
->assoc_resp_ies
) {
462 os_memcpy(rsn_ie_buf
+ wpa_ie_len
, sm
->assoc_resp_ies
,
463 sm
->assoc_resp_ies_len
);
464 wpa_ie_len
+= sm
->assoc_resp_ies_len
;
469 #endif /* CONFIG_IEEE80211R */
471 wpa_hexdump(MSG_DEBUG
, "WPA: WPA IE for msg 2/4", wpa_ie
, wpa_ie_len
);
473 mic_len
= wpa_mic_len(sm
->key_mgmt
, sm
->pmk_len
);
474 hdrlen
= sizeof(*reply
) + mic_len
+ 2;
475 rbuf
= wpa_sm_alloc_eapol(sm
, IEEE802_1X_TYPE_EAPOL_KEY
,
476 NULL
, hdrlen
+ wpa_ie_len
,
477 &rlen
, (void *) &reply
);
483 reply
->type
= (sm
->proto
== WPA_PROTO_RSN
||
484 sm
->proto
== WPA_PROTO_OSEN
) ?
485 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
486 key_info
= ver
| WPA_KEY_INFO_KEY_TYPE
;
488 key_info
|= WPA_KEY_INFO_MIC
;
490 key_info
|= WPA_KEY_INFO_ENCR_KEY_DATA
;
491 WPA_PUT_BE16(reply
->key_info
, key_info
);
492 if (sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
)
493 WPA_PUT_BE16(reply
->key_length
, 0);
495 os_memcpy(reply
->key_length
, key
->key_length
, 2);
496 os_memcpy(reply
->replay_counter
, key
->replay_counter
,
497 WPA_REPLAY_COUNTER_LEN
);
498 wpa_hexdump(MSG_DEBUG
, "WPA: Replay Counter", reply
->replay_counter
,
499 WPA_REPLAY_COUNTER_LEN
);
501 key_mic
= (u8
*) (reply
+ 1);
502 WPA_PUT_BE16(key_mic
+ mic_len
, wpa_ie_len
); /* Key Data Length */
503 os_memcpy(key_mic
+ mic_len
+ 2, wpa_ie
, wpa_ie_len
); /* Key Data */
506 os_memcpy(reply
->key_nonce
, nonce
, WPA_NONCE_LEN
);
508 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Sending EAPOL-Key 2/4");
509 return wpa_eapol_key_send(sm
, ptk
, ver
, dst
, ETH_P_EAPOL
, rbuf
, rlen
,
514 static int wpa_derive_ptk(struct wpa_sm
*sm
, const unsigned char *src_addr
,
515 const struct wpa_eapol_key
*key
, struct wpa_ptk
*ptk
)
517 #ifdef CONFIG_IEEE80211R
518 if (wpa_key_mgmt_ft(sm
->key_mgmt
))
519 return wpa_derive_ptk_ft(sm
, src_addr
, key
, ptk
);
520 #endif /* CONFIG_IEEE80211R */
522 return wpa_pmk_to_ptk(sm
->pmk
, sm
->pmk_len
, "Pairwise key expansion",
523 sm
->own_addr
, sm
->bssid
, sm
->snonce
,
524 key
->key_nonce
, ptk
, sm
->key_mgmt
,
525 sm
->pairwise_cipher
);
529 static void wpa_supplicant_process_1_of_4(struct wpa_sm
*sm
,
530 const unsigned char *src_addr
,
531 const struct wpa_eapol_key
*key
,
532 u16 ver
, const u8
*key_data
,
535 struct wpa_eapol_ie_parse ie
;
538 u8
*kde
, *kde_buf
= NULL
;
541 if (wpa_sm_get_network_ctx(sm
) == NULL
) {
542 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
, "WPA: No SSID info "
543 "found (msg 1 of 4)");
547 wpa_sm_set_state(sm
, WPA_4WAY_HANDSHAKE
);
548 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: RX message 1 of 4-Way "
549 "Handshake from " MACSTR
" (ver=%d)", MAC2STR(src_addr
), ver
);
551 os_memset(&ie
, 0, sizeof(ie
));
553 if (sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
) {
554 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
555 wpa_hexdump(MSG_DEBUG
, "RSN: msg 1/4 key data",
556 key_data
, key_data_len
);
557 if (wpa_supplicant_parse_ies(key_data
, key_data_len
, &ie
) < 0)
560 wpa_hexdump(MSG_DEBUG
, "RSN: PMKID from "
561 "Authenticator", ie
.pmkid
, PMKID_LEN
);
565 res
= wpa_supplicant_get_pmk(sm
, src_addr
, ie
.pmkid
);
567 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "RSN: Do not reply to "
568 "msg 1/4 - requesting full EAP authentication");
574 if (sm
->renew_snonce
) {
575 if (random_get_bytes(sm
->snonce
, WPA_NONCE_LEN
)) {
576 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
577 "WPA: Failed to get random data for SNonce");
580 sm
->renew_snonce
= 0;
581 wpa_hexdump(MSG_DEBUG
, "WPA: Renewed SNonce",
582 sm
->snonce
, WPA_NONCE_LEN
);
585 /* Calculate PTK which will be stored as a temporary PTK until it has
586 * been verified when processing message 3/4. */
588 if (wpa_derive_ptk(sm
, src_addr
, key
, ptk
) < 0)
590 if (sm
->pairwise_cipher
== WPA_CIPHER_TKIP
) {
592 /* Supplicant: swap tx/rx Mic keys */
593 os_memcpy(buf
, &ptk
->tk
[16], 8);
594 os_memcpy(&ptk
->tk
[16], &ptk
->tk
[24], 8);
595 os_memcpy(&ptk
->tk
[24], buf
, 8);
596 os_memset(buf
, 0, sizeof(buf
));
600 kde
= sm
->assoc_wpa_ie
;
601 kde_len
= sm
->assoc_wpa_ie_len
;
605 kde_buf
= os_malloc(kde_len
+ 2 + RSN_SELECTOR_LEN
+ 1);
608 wpa_printf(MSG_DEBUG
, "P2P: Add IP Address Request KDE "
609 "into EAPOL-Key 2/4");
610 os_memcpy(kde_buf
, kde
, kde_len
);
613 *pos
++ = WLAN_EID_VENDOR_SPECIFIC
;
614 *pos
++ = RSN_SELECTOR_LEN
+ 1;
615 RSN_SELECTOR_PUT(pos
, WFA_KEY_DATA_IP_ADDR_REQ
);
616 pos
+= RSN_SELECTOR_LEN
;
621 #endif /* CONFIG_P2P */
623 if (wpa_supplicant_send_2_of_4(sm
, sm
->bssid
, key
, ver
, sm
->snonce
,
624 kde
, kde_len
, ptk
) < 0)
628 os_memcpy(sm
->anonce
, key
->key_nonce
, WPA_NONCE_LEN
);
633 wpa_sm_deauthenticate(sm
, WLAN_REASON_UNSPECIFIED
);
637 static void wpa_sm_start_preauth(void *eloop_ctx
, void *timeout_ctx
)
639 struct wpa_sm
*sm
= eloop_ctx
;
640 rsn_preauth_candidate_process(sm
);
644 static void wpa_supplicant_key_neg_complete(struct wpa_sm
*sm
,
645 const u8
*addr
, int secure
)
647 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
648 "WPA: Key negotiation completed with "
649 MACSTR
" [PTK=%s GTK=%s]", MAC2STR(addr
),
650 wpa_cipher_txt(sm
->pairwise_cipher
),
651 wpa_cipher_txt(sm
->group_cipher
));
652 wpa_sm_cancel_auth_timeout(sm
);
653 wpa_sm_set_state(sm
, WPA_COMPLETED
);
656 wpa_sm_mlme_setprotection(
657 sm
, addr
, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX
,
658 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE
);
659 eapol_sm_notify_portValid(sm
->eapol
, TRUE
);
660 if (wpa_key_mgmt_wpa_psk(sm
->key_mgmt
) ||
661 sm
->key_mgmt
== WPA_KEY_MGMT_DPP
||
662 sm
->key_mgmt
== WPA_KEY_MGMT_OWE
)
663 eapol_sm_notify_eap_success(sm
->eapol
, TRUE
);
665 * Start preauthentication after a short wait to avoid a
666 * possible race condition between the data receive and key
667 * configuration after the 4-Way Handshake. This increases the
668 * likelihood of the first preauth EAPOL-Start frame getting to
671 eloop_register_timeout(1, 0, wpa_sm_start_preauth
, sm
, NULL
);
674 if (sm
->cur_pmksa
&& sm
->cur_pmksa
->opportunistic
) {
675 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
676 "RSN: Authenticator accepted "
677 "opportunistic PMKSA entry - marking it valid");
678 sm
->cur_pmksa
->opportunistic
= 0;
681 #ifdef CONFIG_IEEE80211R
682 if (wpa_key_mgmt_ft(sm
->key_mgmt
)) {
683 /* Prepare for the next transition */
684 wpa_ft_prepare_auth_request(sm
, NULL
);
686 #endif /* CONFIG_IEEE80211R */
690 static void wpa_sm_rekey_ptk(void *eloop_ctx
, void *timeout_ctx
)
692 struct wpa_sm
*sm
= eloop_ctx
;
693 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Request PTK rekeying");
694 wpa_sm_key_request(sm
, 0, 1);
698 static int wpa_supplicant_install_ptk(struct wpa_sm
*sm
,
699 const struct wpa_eapol_key
*key
)
705 if (sm
->ptk
.installed
) {
706 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
707 "WPA: Do not re-install same PTK to the driver");
711 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
712 "WPA: Installing PTK to the driver");
714 if (sm
->pairwise_cipher
== WPA_CIPHER_NONE
) {
715 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Pairwise Cipher "
716 "Suite: NONE - do not use pairwise keys");
720 if (!wpa_cipher_valid_pairwise(sm
->pairwise_cipher
)) {
721 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
722 "WPA: Unsupported pairwise cipher %d",
723 sm
->pairwise_cipher
);
727 alg
= wpa_cipher_to_alg(sm
->pairwise_cipher
);
728 keylen
= wpa_cipher_key_len(sm
->pairwise_cipher
);
729 rsclen
= wpa_cipher_rsc_len(sm
->pairwise_cipher
);
731 if (sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
) {
734 key_rsc
= key
->key_rsc
;
735 wpa_hexdump(MSG_DEBUG
, "WPA: RSC", key_rsc
, rsclen
);
738 if (wpa_sm_set_key(sm
, alg
, sm
->bssid
, 0, 1, key_rsc
, rsclen
,
739 sm
->ptk
.tk
, keylen
) < 0) {
740 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
741 "WPA: Failed to set PTK to the "
742 "driver (alg=%d keylen=%d bssid=" MACSTR
")",
743 alg
, keylen
, MAC2STR(sm
->bssid
));
747 /* TK is not needed anymore in supplicant */
748 os_memset(sm
->ptk
.tk
, 0, WPA_TK_MAX_LEN
);
749 sm
->ptk
.installed
= 1;
751 if (sm
->wpa_ptk_rekey
) {
752 eloop_cancel_timeout(wpa_sm_rekey_ptk
, sm
, NULL
);
753 eloop_register_timeout(sm
->wpa_ptk_rekey
, 0, wpa_sm_rekey_ptk
,
761 static int wpa_supplicant_check_group_cipher(struct wpa_sm
*sm
,
763 int keylen
, int maxkeylen
,
769 *alg
= wpa_cipher_to_alg(group_cipher
);
770 if (*alg
== WPA_ALG_NONE
) {
771 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
772 "WPA: Unsupported Group Cipher %d",
776 *key_rsc_len
= wpa_cipher_rsc_len(group_cipher
);
778 klen
= wpa_cipher_key_len(group_cipher
);
779 if (keylen
!= klen
|| maxkeylen
< klen
) {
780 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
781 "WPA: Unsupported %s Group Cipher key length %d (%d)",
782 wpa_cipher_txt(group_cipher
), keylen
, maxkeylen
);
789 struct wpa_gtk_data
{
791 int tx
, key_rsc_len
, keyidx
;
797 static int wpa_supplicant_install_gtk(struct wpa_sm
*sm
,
798 const struct wpa_gtk_data
*gd
,
799 const u8
*key_rsc
, int wnm_sleep
)
801 const u8
*_gtk
= gd
->gtk
;
804 /* Detect possible key reinstallation */
805 if ((sm
->gtk
.gtk_len
== (size_t) gd
->gtk_len
&&
806 os_memcmp(sm
->gtk
.gtk
, gd
->gtk
, sm
->gtk
.gtk_len
) == 0) ||
807 (sm
->gtk_wnm_sleep
.gtk_len
== (size_t) gd
->gtk_len
&&
808 os_memcmp(sm
->gtk_wnm_sleep
.gtk
, gd
->gtk
,
809 sm
->gtk_wnm_sleep
.gtk_len
) == 0)) {
810 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
811 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
812 gd
->keyidx
, gd
->tx
, gd
->gtk_len
);
816 wpa_hexdump_key(MSG_DEBUG
, "WPA: Group Key", gd
->gtk
, gd
->gtk_len
);
817 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
818 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
819 gd
->keyidx
, gd
->tx
, gd
->gtk_len
);
820 wpa_hexdump(MSG_DEBUG
, "WPA: RSC", key_rsc
, gd
->key_rsc_len
);
821 if (sm
->group_cipher
== WPA_CIPHER_TKIP
) {
822 /* Swap Tx/Rx keys for Michael MIC */
823 os_memcpy(gtk_buf
, gd
->gtk
, 16);
824 os_memcpy(gtk_buf
+ 16, gd
->gtk
+ 24, 8);
825 os_memcpy(gtk_buf
+ 24, gd
->gtk
+ 16, 8);
828 if (sm
->pairwise_cipher
== WPA_CIPHER_NONE
) {
829 if (wpa_sm_set_key(sm
, gd
->alg
, NULL
,
830 gd
->keyidx
, 1, key_rsc
, gd
->key_rsc_len
,
831 _gtk
, gd
->gtk_len
) < 0) {
832 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
833 "WPA: Failed to set GTK to the driver "
835 os_memset(gtk_buf
, 0, sizeof(gtk_buf
));
838 } else if (wpa_sm_set_key(sm
, gd
->alg
, broadcast_ether_addr
,
839 gd
->keyidx
, gd
->tx
, key_rsc
, gd
->key_rsc_len
,
840 _gtk
, gd
->gtk_len
) < 0) {
841 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
842 "WPA: Failed to set GTK to "
843 "the driver (alg=%d keylen=%d keyidx=%d)",
844 gd
->alg
, gd
->gtk_len
, gd
->keyidx
);
845 os_memset(gtk_buf
, 0, sizeof(gtk_buf
));
848 os_memset(gtk_buf
, 0, sizeof(gtk_buf
));
851 sm
->gtk_wnm_sleep
.gtk_len
= gd
->gtk_len
;
852 os_memcpy(sm
->gtk_wnm_sleep
.gtk
, gd
->gtk
,
853 sm
->gtk_wnm_sleep
.gtk_len
);
855 sm
->gtk
.gtk_len
= gd
->gtk_len
;
856 os_memcpy(sm
->gtk
.gtk
, gd
->gtk
, sm
->gtk
.gtk_len
);
863 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm
*sm
,
866 if (tx
&& sm
->pairwise_cipher
!= WPA_CIPHER_NONE
) {
867 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
868 * seemed to set this bit (incorrectly, since Tx is only when
869 * doing Group Key only APs) and without this workaround, the
870 * data connection does not work because wpa_supplicant
871 * configured non-zero keyidx to be used for unicast. */
872 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
873 "WPA: Tx bit set for GTK, but pairwise "
874 "keys are used - ignore Tx bit");
881 static int wpa_supplicant_rsc_relaxation(const struct wpa_sm
*sm
,
886 if (!sm
->wpa_rsc_relaxation
)
889 rsclen
= wpa_cipher_rsc_len(sm
->group_cipher
);
892 * Try to detect RSC (endian) corruption issue where the AP sends
893 * the RSC bytes in EAPOL-Key message in the wrong order, both if
894 * it's actually a 6-byte field (as it should be) and if it treats
895 * it as an 8-byte field.
896 * An AP model known to have this bug is the Sapido RB-1632.
898 if (rsclen
== 6 && ((rsc
[5] && !rsc
[0]) || rsc
[6] || rsc
[7])) {
899 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
900 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
901 rsc
[0], rsc
[1], rsc
[2], rsc
[3],
902 rsc
[4], rsc
[5], rsc
[6], rsc
[7]);
911 static int wpa_supplicant_pairwise_gtk(struct wpa_sm
*sm
,
912 const struct wpa_eapol_key
*key
,
913 const u8
*gtk
, size_t gtk_len
,
916 struct wpa_gtk_data gd
;
920 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
922 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
923 * Reserved [bits 0-7]
927 os_memset(&gd
, 0, sizeof(gd
));
928 wpa_hexdump_key(MSG_DEBUG
, "RSN: received GTK in pairwise handshake",
931 if (gtk_len
< 2 || gtk_len
- 2 > sizeof(gd
.gtk
))
934 gd
.keyidx
= gtk
[0] & 0x3;
935 gd
.tx
= wpa_supplicant_gtk_tx_bit_workaround(sm
,
936 !!(gtk
[0] & BIT(2)));
940 os_memcpy(gd
.gtk
, gtk
, gtk_len
);
941 gd
.gtk_len
= gtk_len
;
943 key_rsc
= key
->key_rsc
;
944 if (wpa_supplicant_rsc_relaxation(sm
, key
->key_rsc
))
947 if (sm
->group_cipher
!= WPA_CIPHER_GTK_NOT_USED
&&
948 (wpa_supplicant_check_group_cipher(sm
, sm
->group_cipher
,
950 &gd
.key_rsc_len
, &gd
.alg
) ||
951 wpa_supplicant_install_gtk(sm
, &gd
, key_rsc
, 0))) {
952 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
953 "RSN: Failed to install GTK");
954 os_memset(&gd
, 0, sizeof(gd
));
957 os_memset(&gd
, 0, sizeof(gd
));
959 wpa_supplicant_key_neg_complete(sm
, sm
->bssid
,
960 key_info
& WPA_KEY_INFO_SECURE
);
965 #ifdef CONFIG_IEEE80211W
966 static int wpa_supplicant_install_igtk(struct wpa_sm
*sm
,
967 const struct wpa_igtk_kde
*igtk
,
970 size_t len
= wpa_cipher_key_len(sm
->mgmt_group_cipher
);
971 u16 keyidx
= WPA_GET_LE16(igtk
->keyid
);
973 /* Detect possible key reinstallation */
974 if ((sm
->igtk
.igtk_len
== len
&&
975 os_memcmp(sm
->igtk
.igtk
, igtk
->igtk
, sm
->igtk
.igtk_len
) == 0) ||
976 (sm
->igtk_wnm_sleep
.igtk_len
== len
&&
977 os_memcmp(sm
->igtk_wnm_sleep
.igtk
, igtk
->igtk
,
978 sm
->igtk_wnm_sleep
.igtk_len
) == 0)) {
979 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
980 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
985 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
986 "WPA: IGTK keyid %d pn %02x%02x%02x%02x%02x%02x",
987 keyidx
, MAC2STR(igtk
->pn
));
988 wpa_hexdump_key(MSG_DEBUG
, "WPA: IGTK", igtk
->igtk
, len
);
990 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
991 "WPA: Invalid IGTK KeyID %d", keyidx
);
994 if (wpa_sm_set_key(sm
, wpa_cipher_to_alg(sm
->mgmt_group_cipher
),
995 broadcast_ether_addr
,
996 keyidx
, 0, igtk
->pn
, sizeof(igtk
->pn
),
997 igtk
->igtk
, len
) < 0) {
998 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
999 "WPA: Failed to configure IGTK to the driver");
1004 sm
->igtk_wnm_sleep
.igtk_len
= len
;
1005 os_memcpy(sm
->igtk_wnm_sleep
.igtk
, igtk
->igtk
,
1006 sm
->igtk_wnm_sleep
.igtk_len
);
1008 sm
->igtk
.igtk_len
= len
;
1009 os_memcpy(sm
->igtk
.igtk
, igtk
->igtk
, sm
->igtk
.igtk_len
);
1014 #endif /* CONFIG_IEEE80211W */
1017 static int ieee80211w_set_keys(struct wpa_sm
*sm
,
1018 struct wpa_eapol_ie_parse
*ie
)
1020 #ifdef CONFIG_IEEE80211W
1021 if (!wpa_cipher_valid_mgmt_group(sm
->mgmt_group_cipher
))
1026 const struct wpa_igtk_kde
*igtk
;
1028 len
= wpa_cipher_key_len(sm
->mgmt_group_cipher
);
1029 if (ie
->igtk_len
!= WPA_IGTK_KDE_PREFIX_LEN
+ len
)
1032 igtk
= (const struct wpa_igtk_kde
*) ie
->igtk
;
1033 if (wpa_supplicant_install_igtk(sm
, igtk
, 0) < 0)
1038 #else /* CONFIG_IEEE80211W */
1040 #endif /* CONFIG_IEEE80211W */
1044 static void wpa_report_ie_mismatch(struct wpa_sm
*sm
,
1045 const char *reason
, const u8
*src_addr
,
1046 const u8
*wpa_ie
, size_t wpa_ie_len
,
1047 const u8
*rsn_ie
, size_t rsn_ie_len
)
1049 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
, "WPA: %s (src=" MACSTR
")",
1050 reason
, MAC2STR(src_addr
));
1052 if (sm
->ap_wpa_ie
) {
1053 wpa_hexdump(MSG_INFO
, "WPA: WPA IE in Beacon/ProbeResp",
1054 sm
->ap_wpa_ie
, sm
->ap_wpa_ie_len
);
1057 if (!sm
->ap_wpa_ie
) {
1058 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1059 "WPA: No WPA IE in Beacon/ProbeResp");
1061 wpa_hexdump(MSG_INFO
, "WPA: WPA IE in 3/4 msg",
1062 wpa_ie
, wpa_ie_len
);
1065 if (sm
->ap_rsn_ie
) {
1066 wpa_hexdump(MSG_INFO
, "WPA: RSN IE in Beacon/ProbeResp",
1067 sm
->ap_rsn_ie
, sm
->ap_rsn_ie_len
);
1070 if (!sm
->ap_rsn_ie
) {
1071 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1072 "WPA: No RSN IE in Beacon/ProbeResp");
1074 wpa_hexdump(MSG_INFO
, "WPA: RSN IE in 3/4 msg",
1075 rsn_ie
, rsn_ie_len
);
1078 wpa_sm_deauthenticate(sm
, WLAN_REASON_IE_IN_4WAY_DIFFERS
);
1082 #ifdef CONFIG_IEEE80211R
1084 static int ft_validate_mdie(struct wpa_sm
*sm
,
1085 const unsigned char *src_addr
,
1086 struct wpa_eapol_ie_parse
*ie
,
1087 const u8
*assoc_resp_mdie
)
1089 struct rsn_mdie
*mdie
;
1091 mdie
= (struct rsn_mdie
*) (ie
->mdie
+ 2);
1092 if (ie
->mdie
== NULL
|| ie
->mdie_len
< 2 + sizeof(*mdie
) ||
1093 os_memcmp(mdie
->mobility_domain
, sm
->mobility_domain
,
1094 MOBILITY_DOMAIN_ID_LEN
) != 0) {
1095 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "FT: MDIE in msg 3/4 did "
1096 "not match with the current mobility domain");
1100 if (assoc_resp_mdie
&&
1101 (assoc_resp_mdie
[1] != ie
->mdie
[1] ||
1102 os_memcmp(assoc_resp_mdie
, ie
->mdie
, 2 + ie
->mdie
[1]) != 0)) {
1103 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "FT: MDIE mismatch");
1104 wpa_hexdump(MSG_DEBUG
, "FT: MDIE in EAPOL-Key msg 3/4",
1105 ie
->mdie
, 2 + ie
->mdie
[1]);
1106 wpa_hexdump(MSG_DEBUG
, "FT: MDIE in (Re)Association Response",
1107 assoc_resp_mdie
, 2 + assoc_resp_mdie
[1]);
1115 static int ft_validate_ftie(struct wpa_sm
*sm
,
1116 const unsigned char *src_addr
,
1117 struct wpa_eapol_ie_parse
*ie
,
1118 const u8
*assoc_resp_ftie
)
1120 if (ie
->ftie
== NULL
) {
1121 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1122 "FT: No FTIE in EAPOL-Key msg 3/4");
1126 if (assoc_resp_ftie
== NULL
)
1129 if (assoc_resp_ftie
[1] != ie
->ftie
[1] ||
1130 os_memcmp(assoc_resp_ftie
, ie
->ftie
, 2 + ie
->ftie
[1]) != 0) {
1131 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "FT: FTIE mismatch");
1132 wpa_hexdump(MSG_DEBUG
, "FT: FTIE in EAPOL-Key msg 3/4",
1133 ie
->ftie
, 2 + ie
->ftie
[1]);
1134 wpa_hexdump(MSG_DEBUG
, "FT: FTIE in (Re)Association Response",
1135 assoc_resp_ftie
, 2 + assoc_resp_ftie
[1]);
1143 static int ft_validate_rsnie(struct wpa_sm
*sm
,
1144 const unsigned char *src_addr
,
1145 struct wpa_eapol_ie_parse
*ie
)
1147 struct wpa_ie_data rsn
;
1153 * Verify that PMKR1Name from EAPOL-Key message 3/4
1154 * matches with the value we derived.
1156 if (wpa_parse_wpa_ie_rsn(ie
->rsn_ie
, ie
->rsn_ie_len
, &rsn
) < 0 ||
1157 rsn
.num_pmkid
!= 1 || rsn
.pmkid
== NULL
) {
1158 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "FT: No PMKR1Name in "
1159 "FT 4-way handshake message 3/4");
1163 if (os_memcmp_const(rsn
.pmkid
, sm
->pmk_r1_name
, WPA_PMK_NAME_LEN
) != 0)
1165 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1166 "FT: PMKR1Name mismatch in "
1167 "FT 4-way handshake message 3/4");
1168 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name from Authenticator",
1169 rsn
.pmkid
, WPA_PMK_NAME_LEN
);
1170 wpa_hexdump(MSG_DEBUG
, "FT: Derived PMKR1Name",
1171 sm
->pmk_r1_name
, WPA_PMK_NAME_LEN
);
1179 static int wpa_supplicant_validate_ie_ft(struct wpa_sm
*sm
,
1180 const unsigned char *src_addr
,
1181 struct wpa_eapol_ie_parse
*ie
)
1183 const u8
*pos
, *end
, *mdie
= NULL
, *ftie
= NULL
;
1185 if (sm
->assoc_resp_ies
) {
1186 pos
= sm
->assoc_resp_ies
;
1187 end
= pos
+ sm
->assoc_resp_ies_len
;
1188 while (end
- pos
> 2) {
1189 if (2 + pos
[1] > end
- pos
)
1192 case WLAN_EID_MOBILITY_DOMAIN
:
1195 case WLAN_EID_FAST_BSS_TRANSITION
:
1203 if (ft_validate_mdie(sm
, src_addr
, ie
, mdie
) < 0 ||
1204 ft_validate_ftie(sm
, src_addr
, ie
, ftie
) < 0 ||
1205 ft_validate_rsnie(sm
, src_addr
, ie
) < 0)
1211 #endif /* CONFIG_IEEE80211R */
1214 static int wpa_supplicant_validate_ie(struct wpa_sm
*sm
,
1215 const unsigned char *src_addr
,
1216 struct wpa_eapol_ie_parse
*ie
)
1218 if (sm
->ap_wpa_ie
== NULL
&& sm
->ap_rsn_ie
== NULL
) {
1219 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1220 "WPA: No WPA/RSN IE for this AP known. "
1221 "Trying to get from scan results");
1222 if (wpa_sm_get_beacon_ie(sm
) < 0) {
1223 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1224 "WPA: Could not find AP from "
1225 "the scan results");
1227 wpa_msg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1228 "WPA: Found the current AP from "
1229 "updated scan results");
1233 if (ie
->wpa_ie
== NULL
&& ie
->rsn_ie
== NULL
&&
1234 (sm
->ap_wpa_ie
|| sm
->ap_rsn_ie
)) {
1235 wpa_report_ie_mismatch(sm
, "IE in 3/4 msg does not match "
1236 "with IE in Beacon/ProbeResp (no IE?)",
1237 src_addr
, ie
->wpa_ie
, ie
->wpa_ie_len
,
1238 ie
->rsn_ie
, ie
->rsn_ie_len
);
1242 if ((ie
->wpa_ie
&& sm
->ap_wpa_ie
&&
1243 (ie
->wpa_ie_len
!= sm
->ap_wpa_ie_len
||
1244 os_memcmp(ie
->wpa_ie
, sm
->ap_wpa_ie
, ie
->wpa_ie_len
) != 0)) ||
1245 (ie
->rsn_ie
&& sm
->ap_rsn_ie
&&
1246 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm
->key_mgmt
),
1247 sm
->ap_rsn_ie
, sm
->ap_rsn_ie_len
,
1248 ie
->rsn_ie
, ie
->rsn_ie_len
))) {
1249 wpa_report_ie_mismatch(sm
, "IE in 3/4 msg does not match "
1250 "with IE in Beacon/ProbeResp",
1251 src_addr
, ie
->wpa_ie
, ie
->wpa_ie_len
,
1252 ie
->rsn_ie
, ie
->rsn_ie_len
);
1256 if (sm
->proto
== WPA_PROTO_WPA
&&
1257 ie
->rsn_ie
&& sm
->ap_rsn_ie
== NULL
&& sm
->rsn_enabled
) {
1258 wpa_report_ie_mismatch(sm
, "Possible downgrade attack "
1259 "detected - RSN was enabled and RSN IE "
1260 "was in msg 3/4, but not in "
1262 src_addr
, ie
->wpa_ie
, ie
->wpa_ie_len
,
1263 ie
->rsn_ie
, ie
->rsn_ie_len
);
1267 #ifdef CONFIG_IEEE80211R
1268 if (wpa_key_mgmt_ft(sm
->key_mgmt
) &&
1269 wpa_supplicant_validate_ie_ft(sm
, src_addr
, ie
) < 0)
1271 #endif /* CONFIG_IEEE80211R */
1278 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1279 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1280 * @dst: Destination address for the frame
1281 * @key: Pointer to the EAPOL-Key frame header
1282 * @ver: Version bits from EAPOL-Key Key Info
1283 * @key_info: Key Info
1284 * @ptk: PTK to use for keyed hash and encryption
1285 * Returns: >= 0 on success, < 0 on failure
1287 int wpa_supplicant_send_4_of_4(struct wpa_sm
*sm
, const unsigned char *dst
,
1288 const struct wpa_eapol_key
*key
,
1289 u16 ver
, u16 key_info
,
1290 struct wpa_ptk
*ptk
)
1292 size_t mic_len
, hdrlen
, rlen
;
1293 struct wpa_eapol_key
*reply
;
1296 mic_len
= wpa_mic_len(sm
->key_mgmt
, sm
->pmk_len
);
1297 hdrlen
= sizeof(*reply
) + mic_len
+ 2;
1298 rbuf
= wpa_sm_alloc_eapol(sm
, IEEE802_1X_TYPE_EAPOL_KEY
, NULL
,
1299 hdrlen
, &rlen
, (void *) &reply
);
1303 reply
->type
= (sm
->proto
== WPA_PROTO_RSN
||
1304 sm
->proto
== WPA_PROTO_OSEN
) ?
1305 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
1306 key_info
&= WPA_KEY_INFO_SECURE
;
1307 key_info
|= ver
| WPA_KEY_INFO_KEY_TYPE
;
1309 key_info
|= WPA_KEY_INFO_MIC
;
1311 key_info
|= WPA_KEY_INFO_ENCR_KEY_DATA
;
1312 WPA_PUT_BE16(reply
->key_info
, key_info
);
1313 if (sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
)
1314 WPA_PUT_BE16(reply
->key_length
, 0);
1316 os_memcpy(reply
->key_length
, key
->key_length
, 2);
1317 os_memcpy(reply
->replay_counter
, key
->replay_counter
,
1318 WPA_REPLAY_COUNTER_LEN
);
1320 key_mic
= (u8
*) (reply
+ 1);
1321 WPA_PUT_BE16(key_mic
+ mic_len
, 0);
1323 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Sending EAPOL-Key 4/4");
1324 return wpa_eapol_key_send(sm
, ptk
, ver
, dst
, ETH_P_EAPOL
, rbuf
, rlen
,
1329 static void wpa_supplicant_process_3_of_4(struct wpa_sm
*sm
,
1330 const struct wpa_eapol_key
*key
,
1331 u16 ver
, const u8
*key_data
,
1332 size_t key_data_len
)
1334 u16 key_info
, keylen
;
1335 struct wpa_eapol_ie_parse ie
;
1337 wpa_sm_set_state(sm
, WPA_4WAY_HANDSHAKE
);
1338 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: RX message 3 of 4-Way "
1339 "Handshake from " MACSTR
" (ver=%d)", MAC2STR(sm
->bssid
), ver
);
1341 key_info
= WPA_GET_BE16(key
->key_info
);
1343 wpa_hexdump(MSG_DEBUG
, "WPA: IE KeyData", key_data
, key_data_len
);
1344 if (wpa_supplicant_parse_ies(key_data
, key_data_len
, &ie
) < 0)
1346 if (ie
.gtk
&& !(key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
1347 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1348 "WPA: GTK IE in unencrypted key data");
1351 #ifdef CONFIG_IEEE80211W
1352 if (ie
.igtk
&& !(key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
1353 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1354 "WPA: IGTK KDE in unencrypted key data");
1359 wpa_cipher_valid_mgmt_group(sm
->mgmt_group_cipher
) &&
1360 ie
.igtk_len
!= WPA_IGTK_KDE_PREFIX_LEN
+
1361 (unsigned int) wpa_cipher_key_len(sm
->mgmt_group_cipher
)) {
1362 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1363 "WPA: Invalid IGTK KDE length %lu",
1364 (unsigned long) ie
.igtk_len
);
1367 #endif /* CONFIG_IEEE80211W */
1369 if (wpa_supplicant_validate_ie(sm
, sm
->bssid
, &ie
) < 0)
1372 if (os_memcmp(sm
->anonce
, key
->key_nonce
, WPA_NONCE_LEN
) != 0) {
1373 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1374 "WPA: ANonce from message 1 of 4-Way Handshake "
1375 "differs from 3 of 4-Way Handshake - drop packet (src="
1376 MACSTR
")", MAC2STR(sm
->bssid
));
1380 keylen
= WPA_GET_BE16(key
->key_length
);
1381 if (keylen
!= wpa_cipher_key_len(sm
->pairwise_cipher
)) {
1382 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1383 "WPA: Invalid %s key length %d (src=" MACSTR
1384 ")", wpa_cipher_txt(sm
->pairwise_cipher
), keylen
,
1385 MAC2STR(sm
->bssid
));
1390 if (ie
.ip_addr_alloc
) {
1391 os_memcpy(sm
->p2p_ip_addr
, ie
.ip_addr_alloc
, 3 * 4);
1392 wpa_hexdump(MSG_DEBUG
, "P2P: IP address info",
1393 sm
->p2p_ip_addr
, sizeof(sm
->p2p_ip_addr
));
1395 #endif /* CONFIG_P2P */
1397 if (wpa_supplicant_send_4_of_4(sm
, sm
->bssid
, key
, ver
, key_info
,
1402 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1403 * for the next 4-Way Handshake. If msg 3 is received again, the old
1404 * SNonce will still be used to avoid changing PTK. */
1405 sm
->renew_snonce
= 1;
1407 if (key_info
& WPA_KEY_INFO_INSTALL
) {
1408 if (wpa_supplicant_install_ptk(sm
, key
))
1412 if (key_info
& WPA_KEY_INFO_SECURE
) {
1413 wpa_sm_mlme_setprotection(
1414 sm
, sm
->bssid
, MLME_SETPROTECTION_PROTECT_TYPE_RX
,
1415 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE
);
1416 eapol_sm_notify_portValid(sm
->eapol
, TRUE
);
1418 wpa_sm_set_state(sm
, WPA_GROUP_HANDSHAKE
);
1420 if (sm
->group_cipher
== WPA_CIPHER_GTK_NOT_USED
) {
1421 wpa_supplicant_key_neg_complete(sm
, sm
->bssid
,
1422 key_info
& WPA_KEY_INFO_SECURE
);
1423 } else if (ie
.gtk
&&
1424 wpa_supplicant_pairwise_gtk(sm
, key
,
1425 ie
.gtk
, ie
.gtk_len
, key_info
) < 0) {
1426 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1427 "RSN: Failed to configure GTK");
1431 if (ieee80211w_set_keys(sm
, &ie
) < 0) {
1432 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1433 "RSN: Failed to configure IGTK");
1438 wpa_sm_set_rekey_offload(sm
);
1440 if (sm
->proto
== WPA_PROTO_RSN
&& wpa_key_mgmt_suite_b(sm
->key_mgmt
)) {
1441 struct rsn_pmksa_cache_entry
*sa
;
1443 sa
= pmksa_cache_add(sm
->pmksa
, sm
->pmk
, sm
->pmk_len
, NULL
,
1444 sm
->ptk
.kck
, sm
->ptk
.kck_len
,
1445 sm
->bssid
, sm
->own_addr
,
1446 sm
->network_ctx
, sm
->key_mgmt
, NULL
);
1451 sm
->msg_3_of_4_ok
= 1;
1455 wpa_sm_deauthenticate(sm
, WLAN_REASON_UNSPECIFIED
);
1459 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm
*sm
,
1463 struct wpa_gtk_data
*gd
)
1466 struct wpa_eapol_ie_parse ie
;
1468 wpa_hexdump_key(MSG_DEBUG
, "RSN: msg 1/2 key data",
1469 keydata
, keydatalen
);
1470 if (wpa_supplicant_parse_ies(keydata
, keydatalen
, &ie
) < 0)
1472 if (ie
.gtk
&& !(key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
1473 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1474 "WPA: GTK IE in unencrypted key data");
1477 if (ie
.gtk
== NULL
) {
1478 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1479 "WPA: No GTK IE in Group Key msg 1/2");
1482 maxkeylen
= gd
->gtk_len
= ie
.gtk_len
- 2;
1484 if (wpa_supplicant_check_group_cipher(sm
, sm
->group_cipher
,
1485 gd
->gtk_len
, maxkeylen
,
1486 &gd
->key_rsc_len
, &gd
->alg
))
1489 wpa_hexdump_key(MSG_DEBUG
, "RSN: received GTK in group key handshake",
1490 ie
.gtk
, ie
.gtk_len
);
1491 gd
->keyidx
= ie
.gtk
[0] & 0x3;
1492 gd
->tx
= wpa_supplicant_gtk_tx_bit_workaround(sm
,
1493 !!(ie
.gtk
[0] & BIT(2)));
1494 if (ie
.gtk_len
- 2 > sizeof(gd
->gtk
)) {
1495 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1496 "RSN: Too long GTK in GTK IE (len=%lu)",
1497 (unsigned long) ie
.gtk_len
- 2);
1500 os_memcpy(gd
->gtk
, ie
.gtk
+ 2, ie
.gtk_len
- 2);
1502 if (ieee80211w_set_keys(sm
, &ie
) < 0)
1503 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1504 "RSN: Failed to configure IGTK");
1510 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm
*sm
,
1511 const struct wpa_eapol_key
*key
,
1513 size_t key_data_len
, u16 key_info
,
1514 u16 ver
, struct wpa_gtk_data
*gd
)
1519 gtk_len
= WPA_GET_BE16(key
->key_length
);
1520 maxkeylen
= key_data_len
;
1521 if (ver
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
1522 if (maxkeylen
< 8) {
1523 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1524 "WPA: Too short maxkeylen (%lu)",
1525 (unsigned long) maxkeylen
);
1531 if (gtk_len
> maxkeylen
||
1532 wpa_supplicant_check_group_cipher(sm
, sm
->group_cipher
,
1534 &gd
->key_rsc_len
, &gd
->alg
))
1537 gd
->gtk_len
= gtk_len
;
1538 gd
->keyidx
= (key_info
& WPA_KEY_INFO_KEY_INDEX_MASK
) >>
1539 WPA_KEY_INFO_KEY_INDEX_SHIFT
;
1540 if (ver
== WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
&& sm
->ptk
.kek_len
== 16) {
1541 #ifdef CONFIG_NO_RC4
1542 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1543 "WPA: RC4 not supported in the build");
1545 #else /* CONFIG_NO_RC4 */
1547 if (key_data_len
> sizeof(gd
->gtk
)) {
1548 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1549 "WPA: RC4 key data too long (%lu)",
1550 (unsigned long) key_data_len
);
1553 os_memcpy(ek
, key
->key_iv
, 16);
1554 os_memcpy(ek
+ 16, sm
->ptk
.kek
, sm
->ptk
.kek_len
);
1555 os_memcpy(gd
->gtk
, key_data
, key_data_len
);
1556 if (rc4_skip(ek
, 32, 256, gd
->gtk
, key_data_len
)) {
1557 os_memset(ek
, 0, sizeof(ek
));
1558 wpa_msg(sm
->ctx
->msg_ctx
, MSG_ERROR
,
1562 os_memset(ek
, 0, sizeof(ek
));
1563 #endif /* CONFIG_NO_RC4 */
1564 } else if (ver
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
1565 if (maxkeylen
% 8) {
1566 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1567 "WPA: Unsupported AES-WRAP len %lu",
1568 (unsigned long) maxkeylen
);
1571 if (maxkeylen
> sizeof(gd
->gtk
)) {
1572 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1573 "WPA: AES-WRAP key data "
1574 "too long (keydatalen=%lu maxkeylen=%lu)",
1575 (unsigned long) key_data_len
,
1576 (unsigned long) maxkeylen
);
1579 if (aes_unwrap(sm
->ptk
.kek
, sm
->ptk
.kek_len
, maxkeylen
/ 8,
1580 key_data
, gd
->gtk
)) {
1581 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1582 "WPA: AES unwrap failed - could not decrypt "
1587 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1588 "WPA: Unsupported key_info type %d", ver
);
1591 gd
->tx
= wpa_supplicant_gtk_tx_bit_workaround(
1592 sm
, !!(key_info
& WPA_KEY_INFO_TXRX
));
1597 static int wpa_supplicant_send_2_of_2(struct wpa_sm
*sm
,
1598 const struct wpa_eapol_key
*key
,
1599 int ver
, u16 key_info
)
1601 size_t mic_len
, hdrlen
, rlen
;
1602 struct wpa_eapol_key
*reply
;
1605 mic_len
= wpa_mic_len(sm
->key_mgmt
, sm
->pmk_len
);
1606 hdrlen
= sizeof(*reply
) + mic_len
+ 2;
1607 rbuf
= wpa_sm_alloc_eapol(sm
, IEEE802_1X_TYPE_EAPOL_KEY
, NULL
,
1608 hdrlen
, &rlen
, (void *) &reply
);
1612 reply
->type
= (sm
->proto
== WPA_PROTO_RSN
||
1613 sm
->proto
== WPA_PROTO_OSEN
) ?
1614 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
1615 key_info
&= WPA_KEY_INFO_KEY_INDEX_MASK
;
1616 key_info
|= ver
| WPA_KEY_INFO_SECURE
;
1618 key_info
|= WPA_KEY_INFO_MIC
;
1620 key_info
|= WPA_KEY_INFO_ENCR_KEY_DATA
;
1621 WPA_PUT_BE16(reply
->key_info
, key_info
);
1622 if (sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
)
1623 WPA_PUT_BE16(reply
->key_length
, 0);
1625 os_memcpy(reply
->key_length
, key
->key_length
, 2);
1626 os_memcpy(reply
->replay_counter
, key
->replay_counter
,
1627 WPA_REPLAY_COUNTER_LEN
);
1629 key_mic
= (u8
*) (reply
+ 1);
1630 WPA_PUT_BE16(key_mic
+ mic_len
, 0);
1632 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Sending EAPOL-Key 2/2");
1633 return wpa_eapol_key_send(sm
, &sm
->ptk
, ver
, sm
->bssid
, ETH_P_EAPOL
,
1634 rbuf
, rlen
, key_mic
);
1638 static void wpa_supplicant_process_1_of_2(struct wpa_sm
*sm
,
1639 const unsigned char *src_addr
,
1640 const struct wpa_eapol_key
*key
,
1642 size_t key_data_len
, u16 ver
)
1646 struct wpa_gtk_data gd
;
1649 if (!sm
->msg_3_of_4_ok
&& !wpa_fils_is_completed(sm
)) {
1650 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
1651 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
1655 os_memset(&gd
, 0, sizeof(gd
));
1657 rekey
= wpa_sm_get_state(sm
) == WPA_COMPLETED
;
1658 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: RX message 1 of Group Key "
1659 "Handshake from " MACSTR
" (ver=%d)", MAC2STR(src_addr
), ver
);
1661 key_info
= WPA_GET_BE16(key
->key_info
);
1663 if (sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
) {
1664 ret
= wpa_supplicant_process_1_of_2_rsn(sm
, key_data
,
1665 key_data_len
, key_info
,
1668 ret
= wpa_supplicant_process_1_of_2_wpa(sm
, key
, key_data
,
1670 key_info
, ver
, &gd
);
1673 wpa_sm_set_state(sm
, WPA_GROUP_HANDSHAKE
);
1678 key_rsc
= key
->key_rsc
;
1679 if (wpa_supplicant_rsc_relaxation(sm
, key
->key_rsc
))
1682 if (wpa_supplicant_install_gtk(sm
, &gd
, key_rsc
, 0) ||
1683 wpa_supplicant_send_2_of_2(sm
, key
, ver
, key_info
) < 0)
1685 os_memset(&gd
, 0, sizeof(gd
));
1688 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
, "WPA: Group rekeying "
1689 "completed with " MACSTR
" [GTK=%s]",
1690 MAC2STR(sm
->bssid
), wpa_cipher_txt(sm
->group_cipher
));
1691 wpa_sm_cancel_auth_timeout(sm
);
1692 wpa_sm_set_state(sm
, WPA_COMPLETED
);
1694 wpa_supplicant_key_neg_complete(sm
, sm
->bssid
,
1696 WPA_KEY_INFO_SECURE
);
1699 wpa_sm_set_rekey_offload(sm
);
1704 os_memset(&gd
, 0, sizeof(gd
));
1705 wpa_sm_deauthenticate(sm
, WLAN_REASON_UNSPECIFIED
);
1709 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm
*sm
,
1710 struct wpa_eapol_key
*key
,
1712 const u8
*buf
, size_t len
)
1714 u8 mic
[WPA_EAPOL_KEY_MIC_MAX_LEN
];
1716 size_t mic_len
= wpa_mic_len(sm
->key_mgmt
, sm
->pmk_len
);
1718 os_memcpy(mic
, key
+ 1, mic_len
);
1720 os_memset(key
+ 1, 0, mic_len
);
1721 wpa_eapol_key_mic(sm
->tptk
.kck
, sm
->tptk
.kck_len
, sm
->key_mgmt
,
1722 ver
, buf
, len
, (u8
*) (key
+ 1));
1723 if (os_memcmp_const(mic
, key
+ 1, mic_len
) != 0) {
1724 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1725 "WPA: Invalid EAPOL-Key MIC "
1726 "when using TPTK - ignoring TPTK");
1731 os_memcpy(&sm
->ptk
, &sm
->tptk
, sizeof(sm
->ptk
));
1732 os_memset(&sm
->tptk
, 0, sizeof(sm
->tptk
));
1734 * This assures the same TPTK in sm->tptk can never be
1735 * copied twice to sm->pkt as the new PTK. In
1736 * combination with the installed flag in the wpa_ptk
1737 * struct, this assures the same PTK is only installed
1740 sm
->renew_snonce
= 1;
1744 if (!ok
&& sm
->ptk_set
) {
1745 os_memset(key
+ 1, 0, mic_len
);
1746 wpa_eapol_key_mic(sm
->ptk
.kck
, sm
->ptk
.kck_len
, sm
->key_mgmt
,
1747 ver
, buf
, len
, (u8
*) (key
+ 1));
1748 if (os_memcmp_const(mic
, key
+ 1, mic_len
) != 0) {
1749 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1750 "WPA: Invalid EAPOL-Key MIC - "
1758 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1759 "WPA: Could not verify EAPOL-Key MIC - "
1764 os_memcpy(sm
->rx_replay_counter
, key
->replay_counter
,
1765 WPA_REPLAY_COUNTER_LEN
);
1766 sm
->rx_replay_counter_set
= 1;
1771 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1772 static int wpa_supplicant_decrypt_key_data(struct wpa_sm
*sm
,
1773 struct wpa_eapol_key
*key
,
1774 size_t mic_len
, u16 ver
,
1775 u8
*key_data
, size_t *key_data_len
)
1777 wpa_hexdump(MSG_DEBUG
, "RSN: encrypted key data",
1778 key_data
, *key_data_len
);
1780 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1781 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1786 /* Decrypt key data here so that this operation does not need
1787 * to be implemented separately for each message type. */
1788 if (ver
== WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
&& sm
->ptk
.kek_len
== 16) {
1789 #ifdef CONFIG_NO_RC4
1790 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1791 "WPA: RC4 not supported in the build");
1793 #else /* CONFIG_NO_RC4 */
1796 wpa_printf(MSG_DEBUG
, "WPA: Decrypt Key Data using RC4");
1797 os_memcpy(ek
, key
->key_iv
, 16);
1798 os_memcpy(ek
+ 16, sm
->ptk
.kek
, sm
->ptk
.kek_len
);
1799 if (rc4_skip(ek
, 32, 256, key_data
, *key_data_len
)) {
1800 os_memset(ek
, 0, sizeof(ek
));
1801 wpa_msg(sm
->ctx
->msg_ctx
, MSG_ERROR
,
1805 os_memset(ek
, 0, sizeof(ek
));
1806 #endif /* CONFIG_NO_RC4 */
1807 } else if (ver
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
1808 ver
== WPA_KEY_INFO_TYPE_AES_128_CMAC
||
1809 sm
->key_mgmt
== WPA_KEY_MGMT_OWE
||
1810 sm
->key_mgmt
== WPA_KEY_MGMT_DPP
||
1811 sm
->key_mgmt
== WPA_KEY_MGMT_OSEN
||
1812 wpa_key_mgmt_suite_b(sm
->key_mgmt
)) {
1815 wpa_printf(MSG_DEBUG
,
1816 "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)",
1817 (unsigned int) sm
->ptk
.kek_len
);
1818 if (*key_data_len
< 8 || *key_data_len
% 8) {
1819 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1820 "WPA: Unsupported AES-WRAP len %u",
1821 (unsigned int) *key_data_len
);
1824 *key_data_len
-= 8; /* AES-WRAP adds 8 bytes */
1825 buf
= os_malloc(*key_data_len
);
1827 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1828 "WPA: No memory for AES-UNWRAP buffer");
1831 if (aes_unwrap(sm
->ptk
.kek
, sm
->ptk
.kek_len
, *key_data_len
/ 8,
1833 bin_clear_free(buf
, *key_data_len
);
1834 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1835 "WPA: AES unwrap failed - "
1836 "could not decrypt EAPOL-Key key data");
1839 os_memcpy(key_data
, buf
, *key_data_len
);
1840 bin_clear_free(buf
, *key_data_len
);
1841 WPA_PUT_BE16(((u8
*) (key
+ 1)) + mic_len
, *key_data_len
);
1843 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
1844 "WPA: Unsupported key_info type %d", ver
);
1847 wpa_hexdump_key(MSG_DEBUG
, "WPA: decrypted EAPOL-Key key data",
1848 key_data
, *key_data_len
);
1854 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1855 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1857 void wpa_sm_aborted_cached(struct wpa_sm
*sm
)
1859 if (sm
&& sm
->cur_pmksa
) {
1860 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1861 "RSN: Cancelling PMKSA caching attempt");
1862 sm
->cur_pmksa
= NULL
;
1867 static void wpa_eapol_key_dump(struct wpa_sm
*sm
,
1868 const struct wpa_eapol_key
*key
,
1869 unsigned int key_data_len
,
1870 const u8
*mic
, unsigned int mic_len
)
1872 #ifndef CONFIG_NO_STDOUT_DEBUG
1873 u16 key_info
= WPA_GET_BE16(key
->key_info
);
1875 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, " EAPOL-Key type=%d", key
->type
);
1876 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1877 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
1878 key_info
, key_info
& WPA_KEY_INFO_TYPE_MASK
,
1879 (key_info
& WPA_KEY_INFO_KEY_INDEX_MASK
) >>
1880 WPA_KEY_INFO_KEY_INDEX_SHIFT
,
1881 (key_info
& (BIT(13) | BIT(14) | BIT(15))) >> 13,
1882 key_info
& WPA_KEY_INFO_KEY_TYPE
? "Pairwise" : "Group",
1883 key_info
& WPA_KEY_INFO_INSTALL
? " Install" : "",
1884 key_info
& WPA_KEY_INFO_ACK
? " Ack" : "",
1885 key_info
& WPA_KEY_INFO_MIC
? " MIC" : "",
1886 key_info
& WPA_KEY_INFO_SECURE
? " Secure" : "",
1887 key_info
& WPA_KEY_INFO_ERROR
? " Error" : "",
1888 key_info
& WPA_KEY_INFO_REQUEST
? " Request" : "",
1889 key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
? " Encr" : "");
1890 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
1891 " key_length=%u key_data_length=%u",
1892 WPA_GET_BE16(key
->key_length
), key_data_len
);
1893 wpa_hexdump(MSG_DEBUG
, " replay_counter",
1894 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
1895 wpa_hexdump(MSG_DEBUG
, " key_nonce", key
->key_nonce
, WPA_NONCE_LEN
);
1896 wpa_hexdump(MSG_DEBUG
, " key_iv", key
->key_iv
, 16);
1897 wpa_hexdump(MSG_DEBUG
, " key_rsc", key
->key_rsc
, 8);
1898 wpa_hexdump(MSG_DEBUG
, " key_id (reserved)", key
->key_id
, 8);
1899 wpa_hexdump(MSG_DEBUG
, " key_mic", mic
, mic_len
);
1900 #endif /* CONFIG_NO_STDOUT_DEBUG */
1905 static int wpa_supp_aead_decrypt(struct wpa_sm
*sm
, u8
*buf
, size_t buf_len
,
1906 size_t *key_data_len
)
1908 struct wpa_ptk
*ptk
;
1909 struct ieee802_1x_hdr
*hdr
;
1910 struct wpa_eapol_key
*key
;
1915 if (*key_data_len
< AES_BLOCK_SIZE
) {
1916 wpa_printf(MSG_INFO
, "No room for AES-SIV data in the frame");
1922 else if (sm
->ptk_set
)
1927 hdr
= (struct ieee802_1x_hdr
*) buf
;
1928 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1929 pos
= (u8
*) (key
+ 1);
1930 pos
+= 2; /* Pointing at the Encrypted Key Data field */
1932 tmp
= os_malloc(*key_data_len
);
1936 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
1937 * to Key Data (exclusive). */
1939 aad_len
[0] = pos
- buf
;
1940 if (aes_siv_decrypt(ptk
->kek
, ptk
->kek_len
, pos
, *key_data_len
,
1941 1, aad
, aad_len
, tmp
) < 0) {
1942 wpa_printf(MSG_INFO
, "Invalid AES-SIV data in the frame");
1943 bin_clear_free(tmp
, *key_data_len
);
1947 /* AEAD decryption and validation completed successfully */
1948 (*key_data_len
) -= AES_BLOCK_SIZE
;
1949 wpa_hexdump_key(MSG_DEBUG
, "WPA: Decrypted Key Data",
1950 tmp
, *key_data_len
);
1952 /* Replace Key Data field with the decrypted version */
1953 os_memcpy(pos
, tmp
, *key_data_len
);
1954 pos
-= 2; /* Key Data Length field */
1955 WPA_PUT_BE16(pos
, *key_data_len
);
1956 bin_clear_free(tmp
, *key_data_len
);
1961 os_memcpy(&sm
->ptk
, &sm
->tptk
, sizeof(sm
->ptk
));
1962 os_memset(&sm
->tptk
, 0, sizeof(sm
->tptk
));
1965 os_memcpy(sm
->rx_replay_counter
, key
->replay_counter
,
1966 WPA_REPLAY_COUNTER_LEN
);
1967 sm
->rx_replay_counter_set
= 1;
1971 #endif /* CONFIG_FILS */
1975 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1976 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1977 * @src_addr: Source MAC address of the EAPOL packet
1978 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1979 * @len: Length of the EAPOL frame
1980 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1982 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1983 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1984 * only processing WPA and WPA2 EAPOL-Key frames.
1986 * The received EAPOL-Key packets are validated and valid packets are replied
1987 * to. In addition, key material (PTK, GTK) is configured at the end of a
1988 * successful key handshake.
1990 int wpa_sm_rx_eapol(struct wpa_sm
*sm
, const u8
*src_addr
,
1991 const u8
*buf
, size_t len
)
1993 size_t plen
, data_len
, key_data_len
;
1994 const struct ieee802_1x_hdr
*hdr
;
1995 struct wpa_eapol_key
*key
;
2000 size_t mic_len
, keyhdrlen
;
2002 #ifdef CONFIG_IEEE80211R
2003 sm
->ft_completed
= 0;
2004 #endif /* CONFIG_IEEE80211R */
2006 mic_len
= wpa_mic_len(sm
->key_mgmt
, sm
->pmk_len
);
2007 keyhdrlen
= sizeof(*key
) + mic_len
+ 2;
2009 if (len
< sizeof(*hdr
) + keyhdrlen
) {
2010 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2011 "WPA: EAPOL frame too short to be a WPA "
2012 "EAPOL-Key (len %lu, expecting at least %lu)",
2013 (unsigned long) len
,
2014 (unsigned long) sizeof(*hdr
) + keyhdrlen
);
2018 hdr
= (const struct ieee802_1x_hdr
*) buf
;
2019 plen
= be_to_host16(hdr
->length
);
2020 data_len
= plen
+ sizeof(*hdr
);
2021 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2022 "IEEE 802.1X RX: version=%d type=%d length=%lu",
2023 hdr
->version
, hdr
->type
, (unsigned long) plen
);
2025 if (hdr
->version
< EAPOL_VERSION
) {
2026 /* TODO: backwards compatibility */
2028 if (hdr
->type
!= IEEE802_1X_TYPE_EAPOL_KEY
) {
2029 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2030 "WPA: EAPOL frame (type %u) discarded, "
2031 "not a Key frame", hdr
->type
);
2035 wpa_hexdump(MSG_MSGDUMP
, "WPA: RX EAPOL-Key", buf
, len
);
2036 if (plen
> len
- sizeof(*hdr
) || plen
< keyhdrlen
) {
2037 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2038 "WPA: EAPOL frame payload size %lu "
2039 "invalid (frame size %lu)",
2040 (unsigned long) plen
, (unsigned long) len
);
2044 if (data_len
< len
) {
2045 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2046 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
2047 (unsigned long) len
- data_len
);
2051 * Make a copy of the frame since we need to modify the buffer during
2052 * MAC validation and Key Data decryption.
2054 tmp
= os_memdup(buf
, data_len
);
2057 key
= (struct wpa_eapol_key
*) (tmp
+ sizeof(struct ieee802_1x_hdr
));
2058 mic
= (u8
*) (key
+ 1);
2059 key_data
= mic
+ mic_len
+ 2;
2061 if (key
->type
!= EAPOL_KEY_TYPE_WPA
&& key
->type
!= EAPOL_KEY_TYPE_RSN
)
2063 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2064 "WPA: EAPOL-Key type (%d) unknown, discarded",
2070 key_data_len
= WPA_GET_BE16(mic
+ mic_len
);
2071 wpa_eapol_key_dump(sm
, key
, key_data_len
, mic
, mic_len
);
2073 if (key_data_len
> plen
- keyhdrlen
) {
2074 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
, "WPA: Invalid EAPOL-Key "
2075 "frame - key_data overflow (%u > %u)",
2076 (unsigned int) key_data_len
,
2077 (unsigned int) (plen
- keyhdrlen
));
2081 eapol_sm_notify_lower_layer_success(sm
->eapol
, 0);
2082 key_info
= WPA_GET_BE16(key
->key_info
);
2083 ver
= key_info
& WPA_KEY_INFO_TYPE_MASK
;
2084 if (ver
!= WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
&&
2085 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
2086 ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
&&
2087 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
2088 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
&&
2089 !wpa_key_mgmt_suite_b(sm
->key_mgmt
) &&
2090 !wpa_key_mgmt_fils(sm
->key_mgmt
) &&
2091 sm
->key_mgmt
!= WPA_KEY_MGMT_OWE
&&
2092 sm
->key_mgmt
!= WPA_KEY_MGMT_DPP
&&
2093 sm
->key_mgmt
!= WPA_KEY_MGMT_OSEN
) {
2094 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2095 "WPA: Unsupported EAPOL-Key descriptor version %d",
2100 if (sm
->key_mgmt
== WPA_KEY_MGMT_OSEN
&&
2101 ver
!= WPA_KEY_INFO_TYPE_AKM_DEFINED
) {
2102 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2103 "OSEN: Unsupported EAPOL-Key descriptor version %d",
2108 if ((wpa_key_mgmt_suite_b(sm
->key_mgmt
) ||
2109 wpa_key_mgmt_fils(sm
->key_mgmt
) ||
2110 sm
->key_mgmt
== WPA_KEY_MGMT_DPP
||
2111 sm
->key_mgmt
== WPA_KEY_MGMT_OWE
) &&
2112 ver
!= WPA_KEY_INFO_TYPE_AKM_DEFINED
) {
2113 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2114 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
2119 #ifdef CONFIG_IEEE80211R
2120 if (wpa_key_mgmt_ft(sm
->key_mgmt
)) {
2121 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
2122 if (ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
2123 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2124 "FT: AP did not use AES-128-CMAC");
2128 #endif /* CONFIG_IEEE80211R */
2129 #ifdef CONFIG_IEEE80211W
2130 if (wpa_key_mgmt_sha256(sm
->key_mgmt
)) {
2131 if (ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
&&
2132 sm
->key_mgmt
!= WPA_KEY_MGMT_OSEN
&&
2133 !wpa_key_mgmt_fils(sm
->key_mgmt
) &&
2134 !wpa_key_mgmt_suite_b(sm
->key_mgmt
)) {
2135 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2136 "WPA: AP did not use the "
2137 "negotiated AES-128-CMAC");
2141 #endif /* CONFIG_IEEE80211W */
2142 if (sm
->pairwise_cipher
== WPA_CIPHER_CCMP
&&
2143 !wpa_key_mgmt_suite_b(sm
->key_mgmt
) &&
2144 !wpa_key_mgmt_fils(sm
->key_mgmt
) &&
2145 sm
->key_mgmt
!= WPA_KEY_MGMT_OWE
&&
2146 sm
->key_mgmt
!= WPA_KEY_MGMT_DPP
&&
2147 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
2148 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2149 "WPA: CCMP is used, but EAPOL-Key "
2150 "descriptor version (%d) is not 2", ver
);
2151 if (sm
->group_cipher
!= WPA_CIPHER_CCMP
&&
2152 !(key_info
& WPA_KEY_INFO_KEY_TYPE
)) {
2153 /* Earlier versions of IEEE 802.11i did not explicitly
2154 * require version 2 descriptor for all EAPOL-Key
2155 * packets, so allow group keys to use version 1 if
2156 * CCMP is not used for them. */
2157 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2158 "WPA: Backwards compatibility: allow invalid "
2159 "version for non-CCMP group keys");
2160 } else if (ver
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
2161 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2162 "WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
2165 } else if (sm
->pairwise_cipher
== WPA_CIPHER_GCMP
&&
2166 !wpa_key_mgmt_suite_b(sm
->key_mgmt
) &&
2167 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
2168 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2169 "WPA: GCMP is used, but EAPOL-Key "
2170 "descriptor version (%d) is not 2", ver
);
2174 if (sm
->rx_replay_counter_set
&&
2175 os_memcmp(key
->replay_counter
, sm
->rx_replay_counter
,
2176 WPA_REPLAY_COUNTER_LEN
) <= 0) {
2177 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2178 "WPA: EAPOL-Key Replay Counter did not increase - "
2183 if (key_info
& WPA_KEY_INFO_SMK_MESSAGE
) {
2184 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2185 "WPA: Unsupported SMK bit in key_info");
2189 if (!(key_info
& WPA_KEY_INFO_ACK
)) {
2190 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2191 "WPA: No Ack bit in key_info");
2195 if (key_info
& WPA_KEY_INFO_REQUEST
) {
2196 wpa_msg(sm
->ctx
->msg_ctx
, MSG_INFO
,
2197 "WPA: EAPOL-Key with Request bit - dropped");
2201 if ((key_info
& WPA_KEY_INFO_MIC
) &&
2202 wpa_supplicant_verify_eapol_key_mic(sm
, key
, ver
, tmp
, data_len
))
2206 if (!mic_len
&& (key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
2207 if (wpa_supp_aead_decrypt(sm
, tmp
, data_len
, &key_data_len
))
2210 #endif /* CONFIG_FILS */
2212 if ((sm
->proto
== WPA_PROTO_RSN
|| sm
->proto
== WPA_PROTO_OSEN
) &&
2213 (key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
) && mic_len
) {
2214 if (wpa_supplicant_decrypt_key_data(sm
, key
, mic_len
,
2220 if (key_info
& WPA_KEY_INFO_KEY_TYPE
) {
2221 if (key_info
& WPA_KEY_INFO_KEY_INDEX_MASK
) {
2222 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2223 "WPA: Ignored EAPOL-Key (Pairwise) with "
2224 "non-zero key index");
2227 if (key_info
& (WPA_KEY_INFO_MIC
|
2228 WPA_KEY_INFO_ENCR_KEY_DATA
)) {
2229 /* 3/4 4-Way Handshake */
2230 wpa_supplicant_process_3_of_4(sm
, key
, ver
, key_data
,
2233 /* 1/4 4-Way Handshake */
2234 wpa_supplicant_process_1_of_4(sm
, src_addr
, key
,
2239 if ((mic_len
&& (key_info
& WPA_KEY_INFO_MIC
)) ||
2240 (!mic_len
&& (key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
))) {
2241 /* 1/2 Group Key Handshake */
2242 wpa_supplicant_process_1_of_2(sm
, src_addr
, key
,
2243 key_data
, key_data_len
,
2246 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
2247 "WPA: EAPOL-Key (Group) without Mic/Encr bit - "
2255 bin_clear_free(tmp
, data_len
);
2260 #ifdef CONFIG_CTRL_IFACE
2261 static u32
wpa_key_mgmt_suite(struct wpa_sm
*sm
)
2263 switch (sm
->key_mgmt
) {
2264 case WPA_KEY_MGMT_IEEE8021X
:
2265 return ((sm
->proto
== WPA_PROTO_RSN
||
2266 sm
->proto
== WPA_PROTO_OSEN
) ?
2267 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X
:
2268 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X
);
2269 case WPA_KEY_MGMT_PSK
:
2270 return (sm
->proto
== WPA_PROTO_RSN
?
2271 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X
:
2272 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X
);
2273 #ifdef CONFIG_IEEE80211R
2274 case WPA_KEY_MGMT_FT_IEEE8021X
:
2275 return RSN_AUTH_KEY_MGMT_FT_802_1X
;
2276 case WPA_KEY_MGMT_FT_PSK
:
2277 return RSN_AUTH_KEY_MGMT_FT_PSK
;
2278 #endif /* CONFIG_IEEE80211R */
2279 #ifdef CONFIG_IEEE80211W
2280 case WPA_KEY_MGMT_IEEE8021X_SHA256
:
2281 return RSN_AUTH_KEY_MGMT_802_1X_SHA256
;
2282 case WPA_KEY_MGMT_PSK_SHA256
:
2283 return RSN_AUTH_KEY_MGMT_PSK_SHA256
;
2284 #endif /* CONFIG_IEEE80211W */
2285 case WPA_KEY_MGMT_CCKM
:
2286 return (sm
->proto
== WPA_PROTO_RSN
?
2287 RSN_AUTH_KEY_MGMT_CCKM
:
2288 WPA_AUTH_KEY_MGMT_CCKM
);
2289 case WPA_KEY_MGMT_WPA_NONE
:
2290 return WPA_AUTH_KEY_MGMT_NONE
;
2291 case WPA_KEY_MGMT_IEEE8021X_SUITE_B
:
2292 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B
;
2293 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192
:
2294 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192
;
2301 #define RSN_SUITE "%02x-%02x-%02x-%d"
2302 #define RSN_SUITE_ARG(s) \
2303 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2306 * wpa_sm_get_mib - Dump text list of MIB entries
2307 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2308 * @buf: Buffer for the list
2309 * @buflen: Length of the buffer
2310 * Returns: Number of bytes written to buffer
2312 * This function is used fetch dot11 MIB variables.
2314 int wpa_sm_get_mib(struct wpa_sm
*sm
, char *buf
, size_t buflen
)
2316 char pmkid_txt
[PMKID_LEN
* 2 + 1];
2320 if (sm
->cur_pmksa
) {
2321 wpa_snprintf_hex(pmkid_txt
, sizeof(pmkid_txt
),
2322 sm
->cur_pmksa
->pmkid
, PMKID_LEN
);
2324 pmkid_txt
[0] = '\0';
2326 if ((wpa_key_mgmt_wpa_psk(sm
->key_mgmt
) ||
2327 wpa_key_mgmt_wpa_ieee8021x(sm
->key_mgmt
)) &&
2328 sm
->proto
== WPA_PROTO_RSN
)
2333 ret
= os_snprintf(buf
, buflen
,
2334 "dot11RSNAOptionImplemented=TRUE\n"
2335 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2336 "dot11RSNAEnabled=%s\n"
2337 "dot11RSNAPreauthenticationEnabled=%s\n"
2338 "dot11RSNAConfigVersion=%d\n"
2339 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2340 "dot11RSNAConfigGroupCipherSize=%d\n"
2341 "dot11RSNAConfigPMKLifetime=%d\n"
2342 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2343 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2344 "dot11RSNAConfigSATimeout=%d\n",
2345 rsna
? "TRUE" : "FALSE",
2346 rsna
? "TRUE" : "FALSE",
2348 wpa_cipher_key_len(sm
->group_cipher
) * 8,
2349 sm
->dot11RSNAConfigPMKLifetime
,
2350 sm
->dot11RSNAConfigPMKReauthThreshold
,
2351 sm
->dot11RSNAConfigSATimeout
);
2352 if (os_snprintf_error(buflen
, ret
))
2357 buf
+ len
, buflen
- len
,
2358 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE
"\n"
2359 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE
"\n"
2360 "dot11RSNAGroupCipherSelected=" RSN_SUITE
"\n"
2361 "dot11RSNAPMKIDUsed=%s\n"
2362 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE
"\n"
2363 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE
"\n"
2364 "dot11RSNAGroupCipherRequested=" RSN_SUITE
"\n"
2365 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2366 "dot11RSNA4WayHandshakeFailures=%u\n",
2367 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm
)),
2368 RSN_SUITE_ARG(wpa_cipher_to_suite(sm
->proto
,
2369 sm
->pairwise_cipher
)),
2370 RSN_SUITE_ARG(wpa_cipher_to_suite(sm
->proto
,
2373 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm
)),
2374 RSN_SUITE_ARG(wpa_cipher_to_suite(sm
->proto
,
2375 sm
->pairwise_cipher
)),
2376 RSN_SUITE_ARG(wpa_cipher_to_suite(sm
->proto
,
2378 sm
->dot11RSNA4WayHandshakeFailures
);
2379 if (!os_snprintf_error(buflen
- len
, ret
))
2384 #endif /* CONFIG_CTRL_IFACE */
2387 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry
*entry
,
2388 void *ctx
, enum pmksa_free_reason reason
)
2390 struct wpa_sm
*sm
= ctx
;
2393 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "RSN: PMKSA cache entry free_cb: "
2394 MACSTR
" reason=%d", MAC2STR(entry
->aa
), reason
);
2396 if (sm
->cur_pmksa
== entry
) {
2397 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2398 "RSN: %s current PMKSA entry",
2399 reason
== PMKSA_REPLACE
? "replaced" : "removed");
2400 pmksa_cache_clear_current(sm
);
2403 * If an entry is simply being replaced, there's no need to
2404 * deauthenticate because it will be immediately re-added.
2405 * This happens when EAP authentication is completed again
2406 * (reauth or failed PMKSA caching attempt).
2408 if (reason
!= PMKSA_REPLACE
)
2412 if (reason
== PMKSA_EXPIRE
&&
2413 (sm
->pmk_len
== entry
->pmk_len
&&
2414 os_memcmp(sm
->pmk
, entry
->pmk
, sm
->pmk_len
) == 0)) {
2415 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2416 "RSN: deauthenticating due to expired PMK");
2417 pmksa_cache_clear_current(sm
);
2422 os_memset(sm
->pmk
, 0, sizeof(sm
->pmk
));
2423 wpa_sm_deauthenticate(sm
, WLAN_REASON_UNSPECIFIED
);
2429 * wpa_sm_init - Initialize WPA state machine
2430 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2431 * Returns: Pointer to the allocated WPA state machine data
2433 * This function is used to allocate a new WPA state machine and the returned
2434 * value is passed to all WPA state machine calls.
2436 struct wpa_sm
* wpa_sm_init(struct wpa_sm_ctx
*ctx
)
2440 sm
= os_zalloc(sizeof(*sm
));
2443 dl_list_init(&sm
->pmksa_candidates
);
2444 sm
->renew_snonce
= 1;
2447 sm
->dot11RSNAConfigPMKLifetime
= 43200;
2448 sm
->dot11RSNAConfigPMKReauthThreshold
= 70;
2449 sm
->dot11RSNAConfigSATimeout
= 60;
2451 sm
->pmksa
= pmksa_cache_init(wpa_sm_pmksa_free_cb
, sm
, sm
);
2452 if (sm
->pmksa
== NULL
) {
2453 wpa_msg(sm
->ctx
->msg_ctx
, MSG_ERROR
,
2454 "RSN: PMKSA cache initialization failed");
2464 * wpa_sm_deinit - Deinitialize WPA state machine
2465 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2467 void wpa_sm_deinit(struct wpa_sm
*sm
)
2471 pmksa_cache_deinit(sm
->pmksa
);
2472 eloop_cancel_timeout(wpa_sm_start_preauth
, sm
, NULL
);
2473 eloop_cancel_timeout(wpa_sm_rekey_ptk
, sm
, NULL
);
2474 os_free(sm
->assoc_wpa_ie
);
2475 os_free(sm
->ap_wpa_ie
);
2476 os_free(sm
->ap_rsn_ie
);
2479 #ifdef CONFIG_IEEE80211R
2480 os_free(sm
->assoc_resp_ies
);
2481 #endif /* CONFIG_IEEE80211R */
2482 #ifdef CONFIG_TESTING_OPTIONS
2483 wpabuf_free(sm
->test_assoc_ie
);
2484 #endif /* CONFIG_TESTING_OPTIONS */
2485 #ifdef CONFIG_FILS_SK_PFS
2486 crypto_ecdh_deinit(sm
->fils_ecdh
);
2487 #endif /* CONFIG_FILS_SK_PFS */
2489 wpabuf_free(sm
->fils_ft_ies
);
2490 #endif /* CONFIG_FILS */
2492 crypto_ecdh_deinit(sm
->owe_ecdh
);
2493 #endif /* CONFIG_OWE */
2499 * wpa_sm_notify_assoc - Notify WPA state machine about association
2500 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2501 * @bssid: The BSSID of the new association
2503 * This function is called to let WPA state machine know that the connection
2506 void wpa_sm_notify_assoc(struct wpa_sm
*sm
, const u8
*bssid
)
2513 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2514 "WPA: Association event - clear replay counter");
2515 os_memcpy(sm
->bssid
, bssid
, ETH_ALEN
);
2516 os_memset(sm
->rx_replay_counter
, 0, WPA_REPLAY_COUNTER_LEN
);
2517 sm
->rx_replay_counter_set
= 0;
2518 sm
->renew_snonce
= 1;
2519 if (os_memcmp(sm
->preauth_bssid
, bssid
, ETH_ALEN
) == 0)
2520 rsn_preauth_deinit(sm
);
2522 #ifdef CONFIG_IEEE80211R
2523 if (wpa_ft_is_completed(sm
)) {
2525 * Clear portValid to kick EAPOL state machine to re-enter
2526 * AUTHENTICATED state to get the EAPOL port Authorized.
2528 eapol_sm_notify_portValid(sm
->eapol
, FALSE
);
2529 wpa_supplicant_key_neg_complete(sm
, sm
->bssid
, 1);
2531 /* Prepare for the next transition */
2532 wpa_ft_prepare_auth_request(sm
, NULL
);
2536 #endif /* CONFIG_IEEE80211R */
2538 if (sm
->fils_completed
) {
2540 * Clear portValid to kick EAPOL state machine to re-enter
2541 * AUTHENTICATED state to get the EAPOL port Authorized.
2543 wpa_supplicant_key_neg_complete(sm
, sm
->bssid
, 1);
2546 #endif /* CONFIG_FILS */
2550 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2551 * this is not part of a Fast BSS Transition.
2553 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Clear old PTK");
2555 os_memset(&sm
->ptk
, 0, sizeof(sm
->ptk
));
2557 os_memset(&sm
->tptk
, 0, sizeof(sm
->tptk
));
2558 os_memset(&sm
->gtk
, 0, sizeof(sm
->gtk
));
2559 os_memset(&sm
->gtk_wnm_sleep
, 0, sizeof(sm
->gtk_wnm_sleep
));
2560 #ifdef CONFIG_IEEE80211W
2561 os_memset(&sm
->igtk
, 0, sizeof(sm
->igtk
));
2562 os_memset(&sm
->igtk_wnm_sleep
, 0, sizeof(sm
->igtk_wnm_sleep
));
2563 #endif /* CONFIG_IEEE80211W */
2568 #endif /* CONFIG_TDLS */
2571 os_memset(sm
->p2p_ip_addr
, 0, sizeof(sm
->p2p_ip_addr
));
2572 #endif /* CONFIG_P2P */
2577 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2578 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2580 * This function is called to let WPA state machine know that the connection
2581 * was lost. This will abort any existing pre-authentication session.
2583 void wpa_sm_notify_disassoc(struct wpa_sm
*sm
)
2585 eloop_cancel_timeout(wpa_sm_start_preauth
, sm
, NULL
);
2586 eloop_cancel_timeout(wpa_sm_rekey_ptk
, sm
, NULL
);
2587 rsn_preauth_deinit(sm
);
2588 pmksa_cache_clear_current(sm
);
2589 if (wpa_sm_get_state(sm
) == WPA_4WAY_HANDSHAKE
)
2590 sm
->dot11RSNA4WayHandshakeFailures
++;
2592 wpa_tdls_disassoc(sm
);
2593 #endif /* CONFIG_TDLS */
2595 sm
->fils_completed
= 0;
2596 #endif /* CONFIG_FILS */
2597 #ifdef CONFIG_IEEE80211R
2598 sm
->ft_reassoc_completed
= 0;
2599 #endif /* CONFIG_IEEE80211R */
2601 /* Keys are not needed in the WPA state machine anymore */
2604 sm
->msg_3_of_4_ok
= 0;
2609 * wpa_sm_set_pmk - Set PMK
2610 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2612 * @pmk_len: The length of the new PMK in bytes
2613 * @pmkid: Calculated PMKID
2614 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
2616 * Configure the PMK for WPA state machine.
2618 void wpa_sm_set_pmk(struct wpa_sm
*sm
, const u8
*pmk
, size_t pmk_len
,
2619 const u8
*pmkid
, const u8
*bssid
)
2624 wpa_hexdump_key(MSG_DEBUG
, "WPA: Set PMK based on external data",
2626 sm
->pmk_len
= pmk_len
;
2627 os_memcpy(sm
->pmk
, pmk
, pmk_len
);
2629 #ifdef CONFIG_IEEE80211R
2630 /* Set XXKey to be PSK for FT key derivation */
2631 sm
->xxkey_len
= pmk_len
;
2632 os_memcpy(sm
->xxkey
, pmk
, pmk_len
);
2633 #endif /* CONFIG_IEEE80211R */
2636 pmksa_cache_add(sm
->pmksa
, pmk
, pmk_len
, pmkid
, NULL
, 0,
2637 bssid
, sm
->own_addr
,
2638 sm
->network_ctx
, sm
->key_mgmt
, NULL
);
2644 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2645 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2647 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2650 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm
*sm
)
2655 if (sm
->cur_pmksa
) {
2656 wpa_hexdump_key(MSG_DEBUG
,
2657 "WPA: Set PMK based on current PMKSA",
2658 sm
->cur_pmksa
->pmk
, sm
->cur_pmksa
->pmk_len
);
2659 sm
->pmk_len
= sm
->cur_pmksa
->pmk_len
;
2660 os_memcpy(sm
->pmk
, sm
->cur_pmksa
->pmk
, sm
->pmk_len
);
2662 wpa_printf(MSG_DEBUG
, "WPA: No current PMKSA - clear PMK");
2664 os_memset(sm
->pmk
, 0, PMK_LEN_MAX
);
2670 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2671 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2672 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2674 void wpa_sm_set_fast_reauth(struct wpa_sm
*sm
, int fast_reauth
)
2677 sm
->fast_reauth
= fast_reauth
;
2682 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2683 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2684 * @scard_ctx: Context pointer for smartcard related callback functions
2686 void wpa_sm_set_scard_ctx(struct wpa_sm
*sm
, void *scard_ctx
)
2690 sm
->scard_ctx
= scard_ctx
;
2691 if (sm
->preauth_eapol
)
2692 eapol_sm_register_scard_ctx(sm
->preauth_eapol
, scard_ctx
);
2697 * wpa_sm_set_config - Notification of current configration change
2698 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2699 * @config: Pointer to current network configuration
2701 * Notify WPA state machine that configuration has changed. config will be
2702 * stored as a backpointer to network configuration. This can be %NULL to clear
2703 * the stored pointed.
2705 void wpa_sm_set_config(struct wpa_sm
*sm
, struct rsn_supp_config
*config
)
2711 sm
->network_ctx
= config
->network_ctx
;
2712 sm
->allowed_pairwise_cipher
= config
->allowed_pairwise_cipher
;
2713 sm
->proactive_key_caching
= config
->proactive_key_caching
;
2714 sm
->eap_workaround
= config
->eap_workaround
;
2715 sm
->eap_conf_ctx
= config
->eap_conf_ctx
;
2717 os_memcpy(sm
->ssid
, config
->ssid
, config
->ssid_len
);
2718 sm
->ssid_len
= config
->ssid_len
;
2721 sm
->wpa_ptk_rekey
= config
->wpa_ptk_rekey
;
2722 sm
->p2p
= config
->p2p
;
2723 sm
->wpa_rsc_relaxation
= config
->wpa_rsc_relaxation
;
2725 if (config
->fils_cache_id
) {
2726 sm
->fils_cache_id_set
= 1;
2727 os_memcpy(sm
->fils_cache_id
, config
->fils_cache_id
,
2730 sm
->fils_cache_id_set
= 0;
2732 #endif /* CONFIG_FILS */
2734 sm
->network_ctx
= NULL
;
2735 sm
->allowed_pairwise_cipher
= 0;
2736 sm
->proactive_key_caching
= 0;
2737 sm
->eap_workaround
= 0;
2738 sm
->eap_conf_ctx
= NULL
;
2740 sm
->wpa_ptk_rekey
= 0;
2742 sm
->wpa_rsc_relaxation
= 0;
2748 * wpa_sm_set_own_addr - Set own MAC address
2749 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2750 * @addr: Own MAC address
2752 void wpa_sm_set_own_addr(struct wpa_sm
*sm
, const u8
*addr
)
2755 os_memcpy(sm
->own_addr
, addr
, ETH_ALEN
);
2760 * wpa_sm_set_ifname - Set network interface name
2761 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2762 * @ifname: Interface name
2763 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2765 void wpa_sm_set_ifname(struct wpa_sm
*sm
, const char *ifname
,
2766 const char *bridge_ifname
)
2769 sm
->ifname
= ifname
;
2770 sm
->bridge_ifname
= bridge_ifname
;
2776 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2777 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2778 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2780 void wpa_sm_set_eapol(struct wpa_sm
*sm
, struct eapol_sm
*eapol
)
2788 * wpa_sm_set_param - Set WPA state machine parameters
2789 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2790 * @param: Parameter field
2791 * @value: Parameter value
2792 * Returns: 0 on success, -1 on failure
2794 int wpa_sm_set_param(struct wpa_sm
*sm
, enum wpa_sm_conf_params param
,
2803 case RSNA_PMK_LIFETIME
:
2805 sm
->dot11RSNAConfigPMKLifetime
= value
;
2809 case RSNA_PMK_REAUTH_THRESHOLD
:
2810 if (value
> 0 && value
<= 100)
2811 sm
->dot11RSNAConfigPMKReauthThreshold
= value
;
2815 case RSNA_SA_TIMEOUT
:
2817 sm
->dot11RSNAConfigSATimeout
= value
;
2821 case WPA_PARAM_PROTO
:
2824 case WPA_PARAM_PAIRWISE
:
2825 sm
->pairwise_cipher
= value
;
2827 case WPA_PARAM_GROUP
:
2828 sm
->group_cipher
= value
;
2830 case WPA_PARAM_KEY_MGMT
:
2831 sm
->key_mgmt
= value
;
2833 #ifdef CONFIG_IEEE80211W
2834 case WPA_PARAM_MGMT_GROUP
:
2835 sm
->mgmt_group_cipher
= value
;
2837 #endif /* CONFIG_IEEE80211W */
2838 case WPA_PARAM_RSN_ENABLED
:
2839 sm
->rsn_enabled
= value
;
2853 * wpa_sm_get_status - Get WPA state machine
2854 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2855 * @buf: Buffer for status information
2856 * @buflen: Maximum buffer length
2857 * @verbose: Whether to include verbose status information
2858 * Returns: Number of bytes written to buf.
2860 * Query WPA state machine for status information. This function fills in
2861 * a text area with current status information. If the buffer (buf) is not
2862 * large enough, status information will be truncated to fit the buffer.
2864 int wpa_sm_get_status(struct wpa_sm
*sm
, char *buf
, size_t buflen
,
2867 char *pos
= buf
, *end
= buf
+ buflen
;
2870 ret
= os_snprintf(pos
, end
- pos
,
2871 "pairwise_cipher=%s\n"
2874 wpa_cipher_txt(sm
->pairwise_cipher
),
2875 wpa_cipher_txt(sm
->group_cipher
),
2876 wpa_key_mgmt_txt(sm
->key_mgmt
, sm
->proto
));
2877 if (os_snprintf_error(end
- pos
, ret
))
2881 if (sm
->mfp
!= NO_MGMT_FRAME_PROTECTION
&& sm
->ap_rsn_ie
) {
2882 struct wpa_ie_data rsn
;
2883 if (wpa_parse_wpa_ie_rsn(sm
->ap_rsn_ie
, sm
->ap_rsn_ie_len
, &rsn
)
2885 rsn
.capabilities
& (WPA_CAPABILITY_MFPR
|
2886 WPA_CAPABILITY_MFPC
)) {
2887 ret
= os_snprintf(pos
, end
- pos
, "pmf=%d\n"
2888 "mgmt_group_cipher=%s\n",
2890 WPA_CAPABILITY_MFPR
) ? 2 : 1,
2892 sm
->mgmt_group_cipher
));
2893 if (os_snprintf_error(end
- pos
, ret
))
2903 int wpa_sm_pmf_enabled(struct wpa_sm
*sm
)
2905 struct wpa_ie_data rsn
;
2907 if (sm
->mfp
== NO_MGMT_FRAME_PROTECTION
|| !sm
->ap_rsn_ie
)
2910 if (wpa_parse_wpa_ie_rsn(sm
->ap_rsn_ie
, sm
->ap_rsn_ie_len
, &rsn
) >= 0 &&
2911 rsn
.capabilities
& (WPA_CAPABILITY_MFPR
| WPA_CAPABILITY_MFPC
))
2919 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2920 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2921 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2922 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2923 * Returns: 0 on success, -1 on failure
2925 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm
*sm
, u8
*wpa_ie
,
2933 #ifdef CONFIG_TESTING_OPTIONS
2934 if (sm
->test_assoc_ie
) {
2935 wpa_printf(MSG_DEBUG
,
2936 "TESTING: Replace association WPA/RSN IE");
2937 if (*wpa_ie_len
< wpabuf_len(sm
->test_assoc_ie
))
2939 os_memcpy(wpa_ie
, wpabuf_head(sm
->test_assoc_ie
),
2940 wpabuf_len(sm
->test_assoc_ie
));
2941 res
= wpabuf_len(sm
->test_assoc_ie
);
2943 #endif /* CONFIG_TESTING_OPTIONS */
2944 res
= wpa_gen_wpa_ie(sm
, wpa_ie
, *wpa_ie_len
);
2949 wpa_hexdump(MSG_DEBUG
, "WPA: Set own WPA IE default",
2950 wpa_ie
, *wpa_ie_len
);
2952 if (sm
->assoc_wpa_ie
== NULL
) {
2954 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2955 * the correct version of the IE even if PMKSA caching is
2956 * aborted (which would remove PMKID from IE generation).
2958 sm
->assoc_wpa_ie
= os_memdup(wpa_ie
, *wpa_ie_len
);
2959 if (sm
->assoc_wpa_ie
== NULL
)
2962 sm
->assoc_wpa_ie_len
= *wpa_ie_len
;
2964 wpa_hexdump(MSG_DEBUG
,
2965 "WPA: Leave previously set WPA IE default",
2966 sm
->assoc_wpa_ie
, sm
->assoc_wpa_ie_len
);
2974 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2975 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2976 * @ie: Pointer to IE data (starting from id)
2978 * Returns: 0 on success, -1 on failure
2980 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2981 * Request frame. The IE will be used to override the default value generated
2982 * with wpa_sm_set_assoc_wpa_ie_default().
2984 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm
*sm
, const u8
*ie
, size_t len
)
2989 os_free(sm
->assoc_wpa_ie
);
2990 if (ie
== NULL
|| len
== 0) {
2991 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
2992 "WPA: clearing own WPA/RSN IE");
2993 sm
->assoc_wpa_ie
= NULL
;
2994 sm
->assoc_wpa_ie_len
= 0;
2996 wpa_hexdump(MSG_DEBUG
, "WPA: set own WPA/RSN IE", ie
, len
);
2997 sm
->assoc_wpa_ie
= os_memdup(ie
, len
);
2998 if (sm
->assoc_wpa_ie
== NULL
)
3001 sm
->assoc_wpa_ie_len
= len
;
3009 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
3010 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3011 * @ie: Pointer to IE data (starting from id)
3013 * Returns: 0 on success, -1 on failure
3015 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
3018 int wpa_sm_set_ap_wpa_ie(struct wpa_sm
*sm
, const u8
*ie
, size_t len
)
3023 os_free(sm
->ap_wpa_ie
);
3024 if (ie
== NULL
|| len
== 0) {
3025 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
3026 "WPA: clearing AP WPA IE");
3027 sm
->ap_wpa_ie
= NULL
;
3028 sm
->ap_wpa_ie_len
= 0;
3030 wpa_hexdump(MSG_DEBUG
, "WPA: set AP WPA IE", ie
, len
);
3031 sm
->ap_wpa_ie
= os_memdup(ie
, len
);
3032 if (sm
->ap_wpa_ie
== NULL
)
3035 sm
->ap_wpa_ie_len
= len
;
3043 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
3044 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3045 * @ie: Pointer to IE data (starting from id)
3047 * Returns: 0 on success, -1 on failure
3049 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
3052 int wpa_sm_set_ap_rsn_ie(struct wpa_sm
*sm
, const u8
*ie
, size_t len
)
3057 os_free(sm
->ap_rsn_ie
);
3058 if (ie
== NULL
|| len
== 0) {
3059 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
3060 "WPA: clearing AP RSN IE");
3061 sm
->ap_rsn_ie
= NULL
;
3062 sm
->ap_rsn_ie_len
= 0;
3064 wpa_hexdump(MSG_DEBUG
, "WPA: set AP RSN IE", ie
, len
);
3065 sm
->ap_rsn_ie
= os_memdup(ie
, len
);
3066 if (sm
->ap_rsn_ie
== NULL
)
3069 sm
->ap_rsn_ie_len
= len
;
3077 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
3078 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3079 * @data: Pointer to data area for parsing results
3080 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
3082 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
3083 * parsed data into data.
3085 int wpa_sm_parse_own_wpa_ie(struct wpa_sm
*sm
, struct wpa_ie_data
*data
)
3090 if (sm
->assoc_wpa_ie
== NULL
) {
3091 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
,
3092 "WPA: No WPA/RSN IE available from association info");
3095 if (wpa_parse_wpa_ie(sm
->assoc_wpa_ie
, sm
->assoc_wpa_ie_len
, data
))
3101 int wpa_sm_pmksa_cache_list(struct wpa_sm
*sm
, char *buf
, size_t len
)
3103 return pmksa_cache_list(sm
->pmksa
, buf
, len
);
3107 struct rsn_pmksa_cache_entry
* wpa_sm_pmksa_cache_head(struct wpa_sm
*sm
)
3109 return pmksa_cache_head(sm
->pmksa
);
3113 struct rsn_pmksa_cache_entry
*
3114 wpa_sm_pmksa_cache_add_entry(struct wpa_sm
*sm
,
3115 struct rsn_pmksa_cache_entry
* entry
)
3117 return pmksa_cache_add_entry(sm
->pmksa
, entry
);
3121 void wpa_sm_pmksa_cache_add(struct wpa_sm
*sm
, const u8
*pmk
, size_t pmk_len
,
3122 const u8
*pmkid
, const u8
*bssid
,
3123 const u8
*fils_cache_id
)
3125 sm
->cur_pmksa
= pmksa_cache_add(sm
->pmksa
, pmk
, pmk_len
, pmkid
, NULL
, 0,
3126 bssid
, sm
->own_addr
, sm
->network_ctx
,
3127 sm
->key_mgmt
, fils_cache_id
);
3131 int wpa_sm_pmksa_exists(struct wpa_sm
*sm
, const u8
*bssid
,
3132 const void *network_ctx
)
3134 return pmksa_cache_get(sm
->pmksa
, bssid
, NULL
, network_ctx
) != NULL
;
3138 void wpa_sm_drop_sa(struct wpa_sm
*sm
)
3140 wpa_dbg(sm
->ctx
->msg_ctx
, MSG_DEBUG
, "WPA: Clear old PMK and PTK");
3143 os_memset(sm
->pmk
, 0, sizeof(sm
->pmk
));
3144 os_memset(&sm
->ptk
, 0, sizeof(sm
->ptk
));
3145 os_memset(&sm
->tptk
, 0, sizeof(sm
->tptk
));
3146 os_memset(&sm
->gtk
, 0, sizeof(sm
->gtk
));
3147 os_memset(&sm
->gtk_wnm_sleep
, 0, sizeof(sm
->gtk_wnm_sleep
));
3148 #ifdef CONFIG_IEEE80211W
3149 os_memset(&sm
->igtk
, 0, sizeof(sm
->igtk
));
3150 os_memset(&sm
->igtk_wnm_sleep
, 0, sizeof(sm
->igtk_wnm_sleep
));
3151 #endif /* CONFIG_IEEE80211W */
3152 #ifdef CONFIG_IEEE80211R
3153 os_memset(sm
->xxkey
, 0, sizeof(sm
->xxkey
));
3154 os_memset(sm
->pmk_r0
, 0, sizeof(sm
->pmk_r0
));
3155 os_memset(sm
->pmk_r1
, 0, sizeof(sm
->pmk_r1
));
3156 #endif /* CONFIG_IEEE80211R */
3160 int wpa_sm_has_ptk(struct wpa_sm
*sm
)
3168 void wpa_sm_update_replay_ctr(struct wpa_sm
*sm
, const u8
*replay_ctr
)
3170 os_memcpy(sm
->rx_replay_counter
, replay_ctr
, WPA_REPLAY_COUNTER_LEN
);
3174 void wpa_sm_pmksa_cache_flush(struct wpa_sm
*sm
, void *network_ctx
)
3176 pmksa_cache_flush(sm
->pmksa
, network_ctx
, NULL
, 0);
3181 int wpa_wnmsleep_install_key(struct wpa_sm
*sm
, u8 subelem_id
, u8
*buf
)
3184 u8 keylen
; /* plaintext key len */
3187 if (subelem_id
== WNM_SLEEP_SUBELEM_GTK
) {
3188 struct wpa_gtk_data gd
;
3190 os_memset(&gd
, 0, sizeof(gd
));
3191 keylen
= wpa_cipher_key_len(sm
->group_cipher
);
3192 gd
.key_rsc_len
= wpa_cipher_rsc_len(sm
->group_cipher
);
3193 gd
.alg
= wpa_cipher_to_alg(sm
->group_cipher
);
3194 if (gd
.alg
== WPA_ALG_NONE
) {
3195 wpa_printf(MSG_DEBUG
, "Unsupported group cipher suite");
3200 keyinfo
= WPA_GET_LE16(buf
+ 2);
3201 gd
.gtk_len
= keylen
;
3202 if (gd
.gtk_len
!= buf
[4]) {
3203 wpa_printf(MSG_DEBUG
, "GTK len mismatch len %d vs %d",
3204 gd
.gtk_len
, buf
[4]);
3207 gd
.keyidx
= keyinfo
& 0x03; /* B0 - B1 */
3208 gd
.tx
= wpa_supplicant_gtk_tx_bit_workaround(
3209 sm
, !!(keyinfo
& WPA_KEY_INFO_TXRX
));
3211 os_memcpy(gd
.gtk
, buf
+ 13, gd
.gtk_len
);
3213 wpa_hexdump_key(MSG_DEBUG
, "Install GTK (WNM SLEEP)",
3214 gd
.gtk
, gd
.gtk_len
);
3215 if (wpa_supplicant_install_gtk(sm
, &gd
, key_rsc
, 1)) {
3216 os_memset(&gd
, 0, sizeof(gd
));
3217 wpa_printf(MSG_DEBUG
, "Failed to install the GTK in "
3221 os_memset(&gd
, 0, sizeof(gd
));
3222 #ifdef CONFIG_IEEE80211W
3223 } else if (subelem_id
== WNM_SLEEP_SUBELEM_IGTK
) {
3224 const struct wpa_igtk_kde
*igtk
;
3226 igtk
= (const struct wpa_igtk_kde
*) (buf
+ 2);
3227 if (wpa_supplicant_install_igtk(sm
, igtk
, 1) < 0)
3229 #endif /* CONFIG_IEEE80211W */
3231 wpa_printf(MSG_DEBUG
, "Unknown element id");
3237 #endif /* CONFIG_WNM */
3242 int wpa_sm_get_p2p_ip_addr(struct wpa_sm
*sm
, u8
*buf
)
3244 if (sm
== NULL
|| WPA_GET_BE32(sm
->p2p_ip_addr
) == 0)
3246 os_memcpy(buf
, sm
->p2p_ip_addr
, 3 * 4);
3250 #endif /* CONFIG_P2P */
3253 void wpa_sm_set_rx_replay_ctr(struct wpa_sm
*sm
, const u8
*rx_replay_counter
)
3255 if (rx_replay_counter
== NULL
)
3258 os_memcpy(sm
->rx_replay_counter
, rx_replay_counter
,
3259 WPA_REPLAY_COUNTER_LEN
);
3260 sm
->rx_replay_counter_set
= 1;
3261 wpa_printf(MSG_DEBUG
, "Updated key replay counter");
3265 void wpa_sm_set_ptk_kck_kek(struct wpa_sm
*sm
,
3266 const u8
*ptk_kck
, size_t ptk_kck_len
,
3267 const u8
*ptk_kek
, size_t ptk_kek_len
)
3269 if (ptk_kck
&& ptk_kck_len
<= WPA_KCK_MAX_LEN
) {
3270 os_memcpy(sm
->ptk
.kck
, ptk_kck
, ptk_kck_len
);
3271 sm
->ptk
.kck_len
= ptk_kck_len
;
3272 wpa_printf(MSG_DEBUG
, "Updated PTK KCK");
3274 if (ptk_kek
&& ptk_kek_len
<= WPA_KEK_MAX_LEN
) {
3275 os_memcpy(sm
->ptk
.kek
, ptk_kek
, ptk_kek_len
);
3276 sm
->ptk
.kek_len
= ptk_kek_len
;
3277 wpa_printf(MSG_DEBUG
, "Updated PTK KEK");
3283 #ifdef CONFIG_TESTING_OPTIONS
3284 void wpa_sm_set_test_assoc_ie(struct wpa_sm
*sm
, struct wpabuf
*buf
)
3286 wpabuf_free(sm
->test_assoc_ie
);
3287 sm
->test_assoc_ie
= buf
;
3289 #endif /* CONFIG_TESTING_OPTIONS */
3294 struct wpabuf
* fils_build_auth(struct wpa_sm
*sm
, int dh_group
, const u8
*md
)
3296 struct wpabuf
*buf
= NULL
;
3297 struct wpabuf
*erp_msg
;
3298 struct wpabuf
*pub
= NULL
;
3300 erp_msg
= eapol_sm_build_erp_reauth_start(sm
->eapol
);
3301 if (!erp_msg
&& !sm
->cur_pmksa
) {
3302 wpa_printf(MSG_DEBUG
,
3303 "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS");
3307 wpa_printf(MSG_DEBUG
, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)",
3308 erp_msg
!= NULL
, sm
->cur_pmksa
!= NULL
);
3310 sm
->fils_completed
= 0;
3312 if (!sm
->assoc_wpa_ie
) {
3313 wpa_printf(MSG_INFO
, "FILS: No own RSN IE set for FILS");
3317 if (random_get_bytes(sm
->fils_nonce
, FILS_NONCE_LEN
) < 0 ||
3318 random_get_bytes(sm
->fils_session
, FILS_SESSION_LEN
) < 0)
3321 wpa_hexdump(MSG_DEBUG
, "FILS: Generated FILS Nonce",
3322 sm
->fils_nonce
, FILS_NONCE_LEN
);
3323 wpa_hexdump(MSG_DEBUG
, "FILS: Generated FILS Session",
3324 sm
->fils_session
, FILS_SESSION_LEN
);
3326 #ifdef CONFIG_FILS_SK_PFS
3327 sm
->fils_dh_group
= dh_group
;
3329 crypto_ecdh_deinit(sm
->fils_ecdh
);
3330 sm
->fils_ecdh
= crypto_ecdh_init(dh_group
);
3331 if (!sm
->fils_ecdh
) {
3332 wpa_printf(MSG_INFO
,
3333 "FILS: Could not initialize ECDH with group %d",
3337 pub
= crypto_ecdh_get_pubkey(sm
->fils_ecdh
, 1);
3340 wpa_hexdump_buf(MSG_DEBUG
, "FILS: Element (DH public key)",
3342 sm
->fils_dh_elem_len
= wpabuf_len(pub
);
3344 #endif /* CONFIG_FILS_SK_PFS */
3346 buf
= wpabuf_alloc(1000 + sm
->assoc_wpa_ie_len
+
3347 (pub
? wpabuf_len(pub
) : 0));
3351 /* Fields following the Authentication algorithm number field */
3353 /* Authentication Transaction seq# */
3354 wpabuf_put_le16(buf
, 1);
3357 wpabuf_put_le16(buf
, WLAN_STATUS_SUCCESS
);
3360 #ifdef CONFIG_FILS_SK_PFS
3362 /* Finite Cyclic Group */
3363 wpabuf_put_le16(buf
, dh_group
);
3365 wpabuf_put_buf(buf
, pub
);
3367 #endif /* CONFIG_FILS_SK_PFS */
3370 wpa_hexdump(MSG_DEBUG
, "FILS: RSNE in FILS Authentication frame",
3371 sm
->assoc_wpa_ie
, sm
->assoc_wpa_ie_len
);
3372 wpabuf_put_data(buf
, sm
->assoc_wpa_ie
, sm
->assoc_wpa_ie_len
);
3375 /* MDE when using FILS for FT initial association */
3376 struct rsn_mdie
*mdie
;
3378 wpabuf_put_u8(buf
, WLAN_EID_MOBILITY_DOMAIN
);
3379 wpabuf_put_u8(buf
, sizeof(*mdie
));
3380 mdie
= wpabuf_put(buf
, sizeof(*mdie
));
3381 os_memcpy(mdie
->mobility_domain
, md
, MOBILITY_DOMAIN_ID_LEN
);
3386 wpabuf_put_u8(buf
, WLAN_EID_EXTENSION
); /* Element ID */
3387 wpabuf_put_u8(buf
, 1 + FILS_NONCE_LEN
); /* Length */
3388 /* Element ID Extension */
3389 wpabuf_put_u8(buf
, WLAN_EID_EXT_FILS_NONCE
);
3390 wpabuf_put_data(buf
, sm
->fils_nonce
, FILS_NONCE_LEN
);
3393 wpabuf_put_u8(buf
, WLAN_EID_EXTENSION
); /* Element ID */
3394 wpabuf_put_u8(buf
, 1 + FILS_SESSION_LEN
); /* Length */
3395 /* Element ID Extension */
3396 wpabuf_put_u8(buf
, WLAN_EID_EXT_FILS_SESSION
);
3397 wpabuf_put_data(buf
, sm
->fils_session
, FILS_SESSION_LEN
);
3399 /* FILS Wrapped Data */
3400 sm
->fils_erp_pmkid_set
= 0;
3402 wpabuf_put_u8(buf
, WLAN_EID_EXTENSION
); /* Element ID */
3403 wpabuf_put_u8(buf
, 1 + wpabuf_len(erp_msg
)); /* Length */
3404 /* Element ID Extension */
3405 wpabuf_put_u8(buf
, WLAN_EID_EXT_FILS_WRAPPED_DATA
);
3406 wpabuf_put_buf(buf
, erp_msg
);
3407 /* Calculate pending PMKID here so that we do not need to
3408 * maintain a copy of the EAP-Initiate/Reauth message. */
3409 if (fils_pmkid_erp(sm
->key_mgmt
, wpabuf_head(erp_msg
),
3410 wpabuf_len(erp_msg
),
3411 sm
->fils_erp_pmkid
) == 0)
3412 sm
->fils_erp_pmkid_set
= 1;
3415 wpa_hexdump_buf(MSG_DEBUG
, "RSN: FILS fields for Authentication frame",
3419 wpabuf_free(erp_msg
);
3425 int fils_process_auth(struct wpa_sm
*sm
, const u8
*bssid
, const u8
*data
,
3428 const u8
*pos
, *end
;
3429 struct ieee802_11_elems elems
;
3430 struct wpa_ie_data rsn
;
3431 int pmkid_match
= 0;
3432 u8 ick
[FILS_ICK_MAX_LEN
];
3435 struct wpabuf
*dh_ss
= NULL
;
3436 const u8
*g_sta
= NULL
;
3437 size_t g_sta_len
= 0;
3438 const u8
*g_ap
= NULL
;
3439 size_t g_ap_len
= 0;
3440 struct wpabuf
*pub
= NULL
;
3442 os_memcpy(sm
->bssid
, bssid
, ETH_ALEN
);
3444 wpa_hexdump(MSG_DEBUG
, "FILS: Authentication frame fields",
3450 #ifdef CONFIG_FILS_SK_PFS
3451 if (sm
->fils_dh_group
) {
3454 /* Using FILS PFS */
3456 /* Finite Cyclic Group */
3457 if (end
- pos
< 2) {
3458 wpa_printf(MSG_DEBUG
,
3459 "FILS: No room for Finite Cyclic Group");
3462 group
= WPA_GET_LE16(pos
);
3464 if (group
!= sm
->fils_dh_group
) {
3465 wpa_printf(MSG_DEBUG
,
3466 "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)",
3467 group
, sm
->fils_dh_group
);
3472 if ((size_t) (end
- pos
) < sm
->fils_dh_elem_len
) {
3473 wpa_printf(MSG_DEBUG
, "FILS: No room for Element");
3477 if (!sm
->fils_ecdh
) {
3478 wpa_printf(MSG_DEBUG
, "FILS: No ECDH state available");
3481 dh_ss
= crypto_ecdh_set_peerkey(sm
->fils_ecdh
, 1, pos
,
3482 sm
->fils_dh_elem_len
);
3484 wpa_printf(MSG_DEBUG
, "FILS: ECDH operation failed");
3487 wpa_hexdump_buf_key(MSG_DEBUG
, "FILS: DH_SS", dh_ss
);
3489 g_ap_len
= sm
->fils_dh_elem_len
;
3490 pos
+= sm
->fils_dh_elem_len
;
3492 #endif /* CONFIG_FILS_SK_PFS */
3494 wpa_hexdump(MSG_DEBUG
, "FILS: Remaining IEs", pos
, end
- pos
);
3495 if (ieee802_11_parse_elems(pos
, end
- pos
, &elems
, 1) == ParseFailed
) {
3496 wpa_printf(MSG_DEBUG
, "FILS: Could not parse elements");
3501 wpa_hexdump(MSG_DEBUG
, "FILS: RSN element", elems
.rsn_ie
,
3503 if (!elems
.rsn_ie
||
3504 wpa_parse_wpa_ie_rsn(elems
.rsn_ie
- 2, elems
.rsn_ie_len
+ 2,
3506 wpa_printf(MSG_DEBUG
, "FILS: No RSN element");
3510 if (!elems
.fils_nonce
) {
3511 wpa_printf(MSG_DEBUG
, "FILS: No FILS Nonce field");
3514 os_memcpy(sm
->fils_anonce
, elems
.fils_nonce
, FILS_NONCE_LEN
);
3515 wpa_hexdump(MSG_DEBUG
, "FILS: ANonce", sm
->fils_anonce
, FILS_NONCE_LEN
);
3517 if (wpa_key_mgmt_ft(sm
->key_mgmt
)) {
3518 struct wpa_ft_ies parse
;
3520 if (!elems
.mdie
|| !elems
.ftie
) {
3521 wpa_printf(MSG_DEBUG
, "FILS+FT: No MDE or FTE");
3525 if (wpa_ft_parse_ies(pos
, end
- pos
, &parse
) < 0) {
3526 wpa_printf(MSG_DEBUG
, "FILS+FT: Failed to parse IEs");
3530 if (!parse
.r0kh_id
) {
3531 wpa_printf(MSG_DEBUG
,
3532 "FILS+FT: No R0KH-ID subelem in FTE");
3535 os_memcpy(sm
->r0kh_id
, parse
.r0kh_id
, parse
.r0kh_id_len
);
3536 sm
->r0kh_id_len
= parse
.r0kh_id_len
;
3537 wpa_hexdump_ascii(MSG_DEBUG
, "FILS+FT: R0KH-ID",
3538 sm
->r0kh_id
, sm
->r0kh_id_len
);
3540 if (!parse
.r1kh_id
) {
3541 wpa_printf(MSG_DEBUG
,
3542 "FILS+FT: No R1KH-ID subelem in FTE");
3545 os_memcpy(sm
->r1kh_id
, parse
.r1kh_id
, FT_R1KH_ID_LEN
);
3546 wpa_hexdump(MSG_DEBUG
, "FILS+FT: R1KH-ID",
3547 sm
->r1kh_id
, FT_R1KH_ID_LEN
);
3549 /* TODO: Check MDE and FTE payload */
3551 wpabuf_free(sm
->fils_ft_ies
);
3552 sm
->fils_ft_ies
= wpabuf_alloc(2 + elems
.mdie_len
+
3553 2 + elems
.ftie_len
);
3554 if (!sm
->fils_ft_ies
)
3556 wpabuf_put_data(sm
->fils_ft_ies
, elems
.mdie
- 2,
3557 2 + elems
.mdie_len
);
3558 wpabuf_put_data(sm
->fils_ft_ies
, elems
.ftie
- 2,
3559 2 + elems
.ftie_len
);
3561 wpabuf_free(sm
->fils_ft_ies
);
3562 sm
->fils_ft_ies
= NULL
;
3566 if (rsn
.pmkid
&& rsn
.num_pmkid
> 0) {
3567 wpa_hexdump(MSG_DEBUG
, "FILS: PMKID List",
3568 rsn
.pmkid
, rsn
.num_pmkid
* PMKID_LEN
);
3570 if (rsn
.num_pmkid
!= 1) {
3571 wpa_printf(MSG_DEBUG
, "FILS: Invalid PMKID selection");
3574 wpa_hexdump(MSG_DEBUG
, "FILS: PMKID", rsn
.pmkid
, PMKID_LEN
);
3575 if (os_memcmp(sm
->cur_pmksa
->pmkid
, rsn
.pmkid
, PMKID_LEN
) != 0)
3577 wpa_printf(MSG_DEBUG
, "FILS: PMKID mismatch");
3578 wpa_hexdump(MSG_DEBUG
, "FILS: Expected PMKID",
3579 sm
->cur_pmksa
->pmkid
, PMKID_LEN
);
3582 wpa_printf(MSG_DEBUG
,
3583 "FILS: Matching PMKID - continue using PMKSA caching");
3586 if (!pmkid_match
&& sm
->cur_pmksa
) {
3587 wpa_printf(MSG_DEBUG
,
3588 "FILS: No PMKID match - cannot use cached PMKSA entry");
3589 sm
->cur_pmksa
= NULL
;
3593 if (!elems
.fils_session
) {
3594 wpa_printf(MSG_DEBUG
, "FILS: No FILS Session element");
3597 wpa_hexdump(MSG_DEBUG
, "FILS: FILS Session", elems
.fils_session
,
3599 if (os_memcmp(sm
->fils_session
, elems
.fils_session
, FILS_SESSION_LEN
)
3601 wpa_printf(MSG_DEBUG
, "FILS: Session mismatch");
3602 wpa_hexdump(MSG_DEBUG
, "FILS: Expected FILS Session",
3603 sm
->fils_session
, FILS_SESSION_LEN
);
3607 /* FILS Wrapped Data */
3608 if (!sm
->cur_pmksa
&& elems
.fils_wrapped_data
) {
3609 u8 rmsk
[ERP_MAX_KEY_LEN
];
3612 wpa_hexdump(MSG_DEBUG
, "FILS: Wrapped Data",
3613 elems
.fils_wrapped_data
,
3614 elems
.fils_wrapped_data_len
);
3615 eapol_sm_process_erp_finish(sm
->eapol
, elems
.fils_wrapped_data
,
3616 elems
.fils_wrapped_data_len
);
3617 if (eapol_sm_failed(sm
->eapol
))
3620 rmsk_len
= ERP_MAX_KEY_LEN
;
3621 res
= eapol_sm_get_key(sm
->eapol
, rmsk
, rmsk_len
);
3622 if (res
== PMK_LEN
) {
3624 res
= eapol_sm_get_key(sm
->eapol
, rmsk
, rmsk_len
);
3629 res
= fils_rmsk_to_pmk(sm
->key_mgmt
, rmsk
, rmsk_len
,
3630 sm
->fils_nonce
, sm
->fils_anonce
,
3631 dh_ss
? wpabuf_head(dh_ss
) : NULL
,
3632 dh_ss
? wpabuf_len(dh_ss
) : 0,
3633 sm
->pmk
, &sm
->pmk_len
);
3634 os_memset(rmsk
, 0, sizeof(rmsk
));
3636 /* Don't use DHss in PTK derivation if PMKSA caching is not
3638 wpabuf_clear_free(dh_ss
);
3644 if (!sm
->fils_erp_pmkid_set
) {
3645 wpa_printf(MSG_DEBUG
, "FILS: PMKID not available");
3648 wpa_hexdump(MSG_DEBUG
, "FILS: PMKID", sm
->fils_erp_pmkid
,
3650 wpa_printf(MSG_DEBUG
, "FILS: ERP processing succeeded - add PMKSA cache entry for the result");
3651 sm
->cur_pmksa
= pmksa_cache_add(sm
->pmksa
, sm
->pmk
, sm
->pmk_len
,
3652 sm
->fils_erp_pmkid
, NULL
, 0,
3653 sm
->bssid
, sm
->own_addr
,
3654 sm
->network_ctx
, sm
->key_mgmt
,
3658 if (!sm
->cur_pmksa
) {
3659 wpa_printf(MSG_DEBUG
,
3660 "FILS: No remaining options to continue FILS authentication");
3664 if (fils_pmk_to_ptk(sm
->pmk
, sm
->pmk_len
, sm
->own_addr
, sm
->bssid
,
3665 sm
->fils_nonce
, sm
->fils_anonce
,
3666 dh_ss
? wpabuf_head(dh_ss
) : NULL
,
3667 dh_ss
? wpabuf_len(dh_ss
) : 0,
3668 &sm
->ptk
, ick
, &ick_len
,
3669 sm
->key_mgmt
, sm
->pairwise_cipher
,
3670 sm
->fils_ft
, &sm
->fils_ft_len
) < 0) {
3671 wpa_printf(MSG_DEBUG
, "FILS: Failed to derive PTK");
3675 wpabuf_clear_free(dh_ss
);
3680 os_memset(&sm
->tptk
, 0, sizeof(sm
->tptk
));
3682 #ifdef CONFIG_FILS_SK_PFS
3683 if (sm
->fils_dh_group
) {
3684 if (!sm
->fils_ecdh
) {
3685 wpa_printf(MSG_INFO
, "FILS: ECDH not initialized");
3688 pub
= crypto_ecdh_get_pubkey(sm
->fils_ecdh
, 1);
3691 wpa_hexdump_buf(MSG_DEBUG
, "FILS: gSTA", pub
);
3692 g_sta
= wpabuf_head(pub
);
3693 g_sta_len
= wpabuf_len(pub
);
3695 wpa_printf(MSG_INFO
, "FILS: gAP not available");
3698 wpa_hexdump(MSG_DEBUG
, "FILS: gAP", g_ap
, g_ap_len
);
3700 #endif /* CONFIG_FILS_SK_PFS */
3702 res
= fils_key_auth_sk(ick
, ick_len
, sm
->fils_nonce
,
3703 sm
->fils_anonce
, sm
->own_addr
, sm
->bssid
,
3704 g_sta
, g_sta_len
, g_ap
, g_ap_len
,
3705 sm
->key_mgmt
, sm
->fils_key_auth_sta
,
3706 sm
->fils_key_auth_ap
,
3707 &sm
->fils_key_auth_len
);
3709 os_memset(ick
, 0, sizeof(ick
));
3713 wpabuf_clear_free(dh_ss
);
3718 #ifdef CONFIG_IEEE80211R
3719 static int fils_ft_build_assoc_req_rsne(struct wpa_sm
*sm
, struct wpabuf
*buf
)
3721 struct rsn_ie_hdr
*rsnie
;
3725 /* RSNIE[PMKR0Name/PMKR1Name] */
3726 rsnie
= wpabuf_put(buf
, sizeof(*rsnie
));
3727 rsnie
->elem_id
= WLAN_EID_RSN
;
3728 WPA_PUT_LE16(rsnie
->version
, RSN_VERSION
);
3730 /* Group Suite Selector */
3731 if (!wpa_cipher_valid_group(sm
->group_cipher
)) {
3732 wpa_printf(MSG_WARNING
, "FT: Invalid group cipher (%d)",
3736 pos
= wpabuf_put(buf
, RSN_SELECTOR_LEN
);
3737 RSN_SELECTOR_PUT(pos
, wpa_cipher_to_suite(WPA_PROTO_RSN
,
3740 /* Pairwise Suite Count */
3741 wpabuf_put_le16(buf
, 1);
3743 /* Pairwise Suite List */
3744 if (!wpa_cipher_valid_pairwise(sm
->pairwise_cipher
)) {
3745 wpa_printf(MSG_WARNING
, "FT: Invalid pairwise cipher (%d)",
3746 sm
->pairwise_cipher
);
3749 pos
= wpabuf_put(buf
, RSN_SELECTOR_LEN
);
3750 RSN_SELECTOR_PUT(pos
, wpa_cipher_to_suite(WPA_PROTO_RSN
,
3751 sm
->pairwise_cipher
));
3753 /* Authenticated Key Management Suite Count */
3754 wpabuf_put_le16(buf
, 1);
3756 /* Authenticated Key Management Suite List */
3757 pos
= wpabuf_put(buf
, RSN_SELECTOR_LEN
);
3758 if (sm
->key_mgmt
== WPA_KEY_MGMT_FT_FILS_SHA256
)
3759 RSN_SELECTOR_PUT(pos
, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256
);
3760 else if (sm
->key_mgmt
== WPA_KEY_MGMT_FT_FILS_SHA384
)
3761 RSN_SELECTOR_PUT(pos
, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384
);
3763 wpa_printf(MSG_WARNING
,
3764 "FILS+FT: Invalid key management type (%d)",
3769 /* RSN Capabilities */
3771 #ifdef CONFIG_IEEE80211W
3772 if (sm
->mgmt_group_cipher
== WPA_CIPHER_AES_128_CMAC
)
3773 capab
|= WPA_CAPABILITY_MFPC
;
3774 #endif /* CONFIG_IEEE80211W */
3775 wpabuf_put_le16(buf
, capab
);
3778 wpabuf_put_le16(buf
, 1);
3780 /* PMKID List [PMKR1Name] */
3781 wpa_hexdump_key(MSG_DEBUG
, "FILS+FT: XXKey (FILS-FT)",
3782 sm
->fils_ft
, sm
->fils_ft_len
);
3783 wpa_hexdump_ascii(MSG_DEBUG
, "FILS+FT: SSID", sm
->ssid
, sm
->ssid_len
);
3784 wpa_hexdump(MSG_DEBUG
, "FILS+FT: MDID",
3785 sm
->mobility_domain
, MOBILITY_DOMAIN_ID_LEN
);
3786 wpa_hexdump_ascii(MSG_DEBUG
, "FILS+FT: R0KH-ID",
3787 sm
->r0kh_id
, sm
->r0kh_id_len
);
3788 if (wpa_derive_pmk_r0(sm
->fils_ft
, sm
->fils_ft_len
, sm
->ssid
,
3789 sm
->ssid_len
, sm
->mobility_domain
,
3790 sm
->r0kh_id
, sm
->r0kh_id_len
, sm
->own_addr
,
3791 sm
->pmk_r0
, sm
->pmk_r0_name
) < 0) {
3792 wpa_printf(MSG_WARNING
, "FILS+FT: Could not derive PMK-R0");
3795 wpa_hexdump_key(MSG_DEBUG
, "FILS+FT: PMK-R0", sm
->pmk_r0
, PMK_LEN
);
3796 wpa_hexdump(MSG_DEBUG
, "FILS+FT: PMKR0Name",
3797 sm
->pmk_r0_name
, WPA_PMK_NAME_LEN
);
3798 wpa_printf(MSG_DEBUG
, "FILS+FT: R1KH-ID: " MACSTR
,
3799 MAC2STR(sm
->r1kh_id
));
3800 pos
= wpabuf_put(buf
, WPA_PMK_NAME_LEN
);
3801 if (wpa_derive_pmk_r1_name(sm
->pmk_r0_name
, sm
->r1kh_id
, sm
->own_addr
,
3803 wpa_printf(MSG_WARNING
, "FILS+FT: Could not derive PMKR1Name");
3806 wpa_hexdump(MSG_DEBUG
, "FILS+FT: PMKR1Name", pos
, WPA_PMK_NAME_LEN
);
3808 #ifdef CONFIG_IEEE80211W
3809 if (sm
->mgmt_group_cipher
== WPA_CIPHER_AES_128_CMAC
) {
3810 /* Management Group Cipher Suite */
3811 pos
= wpabuf_put(buf
, RSN_SELECTOR_LEN
);
3812 RSN_SELECTOR_PUT(pos
, RSN_CIPHER_SUITE_AES_128_CMAC
);
3814 #endif /* CONFIG_IEEE80211W */
3816 rsnie
->len
= ((u8
*) wpabuf_put(buf
, 0) - (u8
*) rsnie
) - 2;
3819 #endif /* CONFIG_IEEE80211R */
3822 struct wpabuf
* fils_build_assoc_req(struct wpa_sm
*sm
, const u8
**kek
,
3823 size_t *kek_len
, const u8
**snonce
,
3825 const struct wpabuf
**hlp
,
3826 unsigned int num_hlp
)
3833 #ifdef CONFIG_IEEE80211R
3834 if (sm
->fils_ft_ies
)
3835 len
+= wpabuf_len(sm
->fils_ft_ies
);
3836 if (wpa_key_mgmt_ft(sm
->key_mgmt
))
3838 #endif /* CONFIG_IEEE80211R */
3839 for (i
= 0; hlp
&& i
< num_hlp
; i
++)
3840 len
+= 10 + wpabuf_len(hlp
[i
]);
3841 buf
= wpabuf_alloc(len
);
3845 #ifdef CONFIG_IEEE80211R
3846 if (wpa_key_mgmt_ft(sm
->key_mgmt
) && sm
->fils_ft_ies
) {
3847 /* MDE and FTE when using FILS+FT */
3848 wpabuf_put_buf(buf
, sm
->fils_ft_ies
);
3849 /* RSNE with PMKR1Name in PMKID field */
3850 if (fils_ft_build_assoc_req_rsne(sm
, buf
) < 0) {
3855 #endif /* CONFIG_IEEE80211R */
3858 wpabuf_put_u8(buf
, WLAN_EID_EXTENSION
); /* Element ID */
3859 wpabuf_put_u8(buf
, 1 + FILS_SESSION_LEN
); /* Length */
3860 /* Element ID Extension */
3861 wpabuf_put_u8(buf
, WLAN_EID_EXT_FILS_SESSION
);
3862 wpabuf_put_data(buf
, sm
->fils_session
, FILS_SESSION_LEN
);
3864 /* Everything after FILS Session element gets encrypted in the driver
3865 * with KEK. The buffer returned from here is the plaintext version. */
3867 /* TODO: FILS Public Key */
3869 /* FILS Key Confirm */
3870 wpabuf_put_u8(buf
, WLAN_EID_EXTENSION
); /* Element ID */
3871 wpabuf_put_u8(buf
, 1 + sm
->fils_key_auth_len
); /* Length */
3872 /* Element ID Extension */
3873 wpabuf_put_u8(buf
, WLAN_EID_EXT_FILS_KEY_CONFIRM
);
3874 wpabuf_put_data(buf
, sm
->fils_key_auth_sta
, sm
->fils_key_auth_len
);
3876 /* FILS HLP Container */
3877 for (i
= 0; hlp
&& i
< num_hlp
; i
++) {
3878 const u8
*pos
= wpabuf_head(hlp
[i
]);
3879 size_t left
= wpabuf_len(hlp
[i
]);
3881 wpabuf_put_u8(buf
, WLAN_EID_EXTENSION
); /* Element ID */
3886 wpabuf_put_u8(buf
, len
); /* Length */
3887 /* Element ID Extension */
3888 wpabuf_put_u8(buf
, WLAN_EID_EXT_FILS_HLP_CONTAINER
);
3889 /* Destination MAC Address, Source MAC Address, HLP Packet.
3890 * HLP Packet is in MSDU format (i.e., included the LLC/SNAP
3891 * header when LPD is used). */
3892 wpabuf_put_data(buf
, pos
, len
- 1);
3896 wpabuf_put_u8(buf
, WLAN_EID_FRAGMENT
);
3897 len
= left
> 255 ? 255 : left
;
3898 wpabuf_put_u8(buf
, len
);
3899 wpabuf_put_data(buf
, pos
, len
);
3905 /* TODO: FILS IP Address Assignment */
3907 wpa_hexdump_buf(MSG_DEBUG
, "FILS: Association Request plaintext", buf
);
3910 *kek_len
= sm
->ptk
.kek_len
;
3911 wpa_hexdump_key(MSG_DEBUG
, "FILS: KEK for AEAD", *kek
, *kek_len
);
3912 *snonce
= sm
->fils_nonce
;
3913 wpa_hexdump(MSG_DEBUG
, "FILS: SNonce for AEAD AAD",
3914 *snonce
, FILS_NONCE_LEN
);
3915 *anonce
= sm
->fils_anonce
;
3916 wpa_hexdump(MSG_DEBUG
, "FILS: ANonce for AEAD AAD",
3917 *anonce
, FILS_NONCE_LEN
);
3923 static void fils_process_hlp_resp(struct wpa_sm
*sm
, const u8
*resp
, size_t len
)
3925 const u8
*pos
, *end
;
3927 wpa_hexdump(MSG_MSGDUMP
, "FILS: HLP response", resp
, len
);
3928 if (len
< 2 * ETH_ALEN
)
3930 pos
= resp
+ 2 * ETH_ALEN
;
3932 if (end
- pos
>= 6 &&
3933 os_memcmp(pos
, "\xaa\xaa\x03\x00\x00\x00", 6) == 0)
3934 pos
+= 6; /* Remove SNAP/LLC header */
3935 wpa_sm_fils_hlp_rx(sm
, resp
, resp
+ ETH_ALEN
, pos
, end
- pos
);
3939 static void fils_process_hlp_container(struct wpa_sm
*sm
, const u8
*pos
,
3942 const u8
*end
= pos
+ len
;
3945 /* Check if there are any FILS HLP Container elements */
3946 while (end
- pos
>= 2) {
3947 if (2 + pos
[1] > end
- pos
)
3949 if (pos
[0] == WLAN_EID_EXTENSION
&&
3950 pos
[1] >= 1 + 2 * ETH_ALEN
&&
3951 pos
[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER
)
3956 return; /* No FILS HLP Container elements */
3958 tmp
= os_malloc(end
- pos
);
3962 while (end
- pos
>= 2) {
3963 if (2 + pos
[1] > end
- pos
||
3964 pos
[0] != WLAN_EID_EXTENSION
||
3965 pos
[1] < 1 + 2 * ETH_ALEN
||
3966 pos
[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER
)
3969 os_memcpy(tmp_pos
, pos
+ 3, pos
[1] - 1);
3970 tmp_pos
+= pos
[1] - 1;
3973 /* Add possible fragments */
3974 while (end
- pos
>= 2 && pos
[0] == WLAN_EID_FRAGMENT
&&
3975 2 + pos
[1] <= end
- pos
) {
3976 os_memcpy(tmp_pos
, pos
+ 2, pos
[1]);
3981 fils_process_hlp_resp(sm
, tmp
, tmp_pos
- tmp
);
3988 int fils_process_assoc_resp(struct wpa_sm
*sm
, const u8
*resp
, size_t len
)
3990 const struct ieee80211_mgmt
*mgmt
;
3991 const u8
*end
, *ie_start
;
3992 struct ieee802_11_elems elems
;
3995 struct wpa_gtk_data gd
;
3997 struct wpa_eapol_ie_parse kde
;
3999 if (!sm
|| !sm
->ptk_set
) {
4000 wpa_printf(MSG_DEBUG
, "FILS: No KEK available");
4004 if (!wpa_key_mgmt_fils(sm
->key_mgmt
)) {
4005 wpa_printf(MSG_DEBUG
, "FILS: Not a FILS AKM");
4009 if (sm
->fils_completed
) {
4010 wpa_printf(MSG_DEBUG
,
4011 "FILS: Association has already been completed for this FILS authentication - ignore unexpected retransmission");
4015 wpa_hexdump(MSG_DEBUG
, "FILS: (Re)Association Response frame",
4018 mgmt
= (const struct ieee80211_mgmt
*) resp
;
4019 if (len
< IEEE80211_HDRLEN
+ sizeof(mgmt
->u
.assoc_resp
))
4023 /* Same offset for Association Response and Reassociation Response */
4024 ie_start
= mgmt
->u
.assoc_resp
.variable
;
4026 if (ieee802_11_parse_elems(ie_start
, end
- ie_start
, &elems
, 1) ==
4028 wpa_printf(MSG_DEBUG
,
4029 "FILS: Failed to parse decrypted elements");
4033 if (!elems
.fils_session
) {
4034 wpa_printf(MSG_DEBUG
, "FILS: No FILS Session element");
4037 if (os_memcmp(elems
.fils_session
, sm
->fils_session
,
4038 FILS_SESSION_LEN
) != 0) {
4039 wpa_printf(MSG_DEBUG
, "FILS: FILS Session mismatch");
4040 wpa_hexdump(MSG_DEBUG
, "FILS: Received FILS Session",
4041 elems
.fils_session
, FILS_SESSION_LEN
);
4042 wpa_hexdump(MSG_DEBUG
, "FILS: Expected FILS Session",
4043 sm
->fils_session
, FILS_SESSION_LEN
);
4046 /* TODO: FILS Public Key */
4048 if (!elems
.fils_key_confirm
) {
4049 wpa_printf(MSG_DEBUG
, "FILS: No FILS Key Confirm element");
4052 if (elems
.fils_key_confirm_len
!= sm
->fils_key_auth_len
) {
4053 wpa_printf(MSG_DEBUG
,
4054 "FILS: Unexpected Key-Auth length %d (expected %d)",
4055 elems
.fils_key_confirm_len
,
4056 (int) sm
->fils_key_auth_len
);
4059 if (os_memcmp(elems
.fils_key_confirm
, sm
->fils_key_auth_ap
,
4060 sm
->fils_key_auth_len
) != 0) {
4061 wpa_printf(MSG_DEBUG
, "FILS: Key-Auth mismatch");
4062 wpa_hexdump(MSG_DEBUG
, "FILS: Received Key-Auth",
4063 elems
.fils_key_confirm
,
4064 elems
.fils_key_confirm_len
);
4065 wpa_hexdump(MSG_DEBUG
, "FILS: Expected Key-Auth",
4066 sm
->fils_key_auth_ap
, sm
->fils_key_auth_len
);
4071 if (!elems
.key_delivery
) {
4072 wpa_printf(MSG_DEBUG
, "FILS: No Key Delivery element");
4076 /* Parse GTK and set the key to the driver */
4077 os_memset(&gd
, 0, sizeof(gd
));
4078 if (wpa_supplicant_parse_ies(elems
.key_delivery
+ WPA_KEY_RSC_LEN
,
4079 elems
.key_delivery_len
- WPA_KEY_RSC_LEN
,
4081 wpa_printf(MSG_DEBUG
, "FILS: Failed to parse KDEs");
4085 wpa_printf(MSG_DEBUG
, "FILS: No GTK KDE");
4088 maxkeylen
= gd
.gtk_len
= kde
.gtk_len
- 2;
4089 if (wpa_supplicant_check_group_cipher(sm
, sm
->group_cipher
,
4090 gd
.gtk_len
, maxkeylen
,
4091 &gd
.key_rsc_len
, &gd
.alg
))
4094 wpa_hexdump_key(MSG_DEBUG
, "FILS: Received GTK", kde
.gtk
, kde
.gtk_len
);
4095 gd
.keyidx
= kde
.gtk
[0] & 0x3;
4096 gd
.tx
= wpa_supplicant_gtk_tx_bit_workaround(sm
,
4097 !!(kde
.gtk
[0] & BIT(2)));
4098 if (kde
.gtk_len
- 2 > sizeof(gd
.gtk
)) {
4099 wpa_printf(MSG_DEBUG
, "FILS: Too long GTK in GTK KDE (len=%lu)",
4100 (unsigned long) kde
.gtk_len
- 2);
4103 os_memcpy(gd
.gtk
, kde
.gtk
+ 2, kde
.gtk_len
- 2);
4105 wpa_printf(MSG_DEBUG
, "FILS: Set GTK to driver");
4106 if (wpa_supplicant_install_gtk(sm
, &gd
, elems
.key_delivery
, 0) < 0) {
4107 wpa_printf(MSG_DEBUG
, "FILS: Failed to set GTK");
4111 if (ieee80211w_set_keys(sm
, &kde
) < 0) {
4112 wpa_printf(MSG_DEBUG
, "FILS: Failed to set IGTK");
4116 alg
= wpa_cipher_to_alg(sm
->pairwise_cipher
);
4117 keylen
= wpa_cipher_key_len(sm
->pairwise_cipher
);
4118 rsclen
= wpa_cipher_rsc_len(sm
->pairwise_cipher
);
4119 wpa_hexdump_key(MSG_DEBUG
, "FILS: Set TK to driver",
4120 sm
->ptk
.tk
, keylen
);
4121 if (wpa_sm_set_key(sm
, alg
, sm
->bssid
, 0, 1, null_rsc
, rsclen
,
4122 sm
->ptk
.tk
, keylen
) < 0) {
4123 wpa_msg(sm
->ctx
->msg_ctx
, MSG_WARNING
,
4124 "FILS: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
4126 alg
, keylen
, MAC2STR(sm
->bssid
));
4130 /* TODO: TK could be cleared after auth frame exchange now that driver
4131 * takes care of association frame encryption/decryption. */
4132 /* TK is not needed anymore in supplicant */
4133 os_memset(sm
->ptk
.tk
, 0, WPA_TK_MAX_LEN
);
4134 sm
->ptk
.installed
= 1;
4136 /* FILS HLP Container */
4137 fils_process_hlp_container(sm
, ie_start
, end
- ie_start
);
4139 /* TODO: FILS IP Address Assignment */
4141 wpa_printf(MSG_DEBUG
, "FILS: Auth+Assoc completed successfully");
4142 sm
->fils_completed
= 1;
4150 void wpa_sm_set_reset_fils_completed(struct wpa_sm
*sm
, int set
)
4153 sm
->fils_completed
= !!set
;
4156 #endif /* CONFIG_FILS */
4159 int wpa_fils_is_completed(struct wpa_sm
*sm
)
4162 return sm
&& sm
->fils_completed
;
4163 #else /* CONFIG_FILS */
4165 #endif /* CONFIG_FILS */
4171 struct wpabuf
* owe_build_assoc_req(struct wpa_sm
*sm
, u16 group
)
4173 struct wpabuf
*ie
= NULL
, *pub
= NULL
;
4178 else if (group
== 20)
4180 else if (group
== 21)
4185 crypto_ecdh_deinit(sm
->owe_ecdh
);
4186 sm
->owe_ecdh
= crypto_ecdh_init(group
);
4189 sm
->owe_group
= group
;
4190 pub
= crypto_ecdh_get_pubkey(sm
->owe_ecdh
, 0);
4191 pub
= wpabuf_zeropad(pub
, prime_len
);
4195 ie
= wpabuf_alloc(5 + wpabuf_len(pub
));
4198 wpabuf_put_u8(ie
, WLAN_EID_EXTENSION
);
4199 wpabuf_put_u8(ie
, 1 + 2 + wpabuf_len(pub
));
4200 wpabuf_put_u8(ie
, WLAN_EID_EXT_OWE_DH_PARAM
);
4201 wpabuf_put_le16(ie
, group
);
4202 wpabuf_put_buf(ie
, pub
);
4204 wpa_hexdump_buf(MSG_DEBUG
, "OWE: Diffie-Hellman Parameter element",
4210 crypto_ecdh_deinit(sm
->owe_ecdh
);
4211 sm
->owe_ecdh
= NULL
;
4216 int owe_process_assoc_resp(struct wpa_sm
*sm
, const u8
*bssid
,
4217 const u8
*resp_ies
, size_t resp_ies_len
)
4219 struct ieee802_11_elems elems
;
4221 struct wpabuf
*secret
, *pub
, *hkey
;
4223 u8 prk
[SHA512_MAC_LEN
], pmkid
[SHA512_MAC_LEN
];
4224 const char *info
= "OWE Key Generation";
4227 size_t hash_len
, prime_len
;
4228 struct wpa_ie_data data
;
4231 ieee802_11_parse_elems(resp_ies
, resp_ies_len
, &elems
, 1) ==
4233 wpa_printf(MSG_INFO
,
4234 "OWE: Could not parse Association Response frame elements");
4238 if (sm
->cur_pmksa
&& elems
.rsn_ie
&&
4239 wpa_parse_wpa_ie_rsn(elems
.rsn_ie
- 2, 2 + elems
.rsn_ie_len
,
4241 data
.num_pmkid
== 1 && data
.pmkid
&&
4242 os_memcmp(sm
->cur_pmksa
->pmkid
, data
.pmkid
, PMKID_LEN
) == 0) {
4243 wpa_printf(MSG_DEBUG
, "OWE: Use PMKSA caching");
4244 wpa_sm_set_pmk_from_pmksa(sm
);
4248 if (!elems
.owe_dh
) {
4249 wpa_printf(MSG_INFO
,
4250 "OWE: No Diffie-Hellman Parameter element found in Association Response frame");
4254 group
= WPA_GET_LE16(elems
.owe_dh
);
4255 if (group
!= sm
->owe_group
) {
4256 wpa_printf(MSG_INFO
,
4257 "OWE: Unexpected Diffie-Hellman group in response: %u",
4262 if (!sm
->owe_ecdh
) {
4263 wpa_printf(MSG_INFO
, "OWE: No ECDH state available");
4269 else if (group
== 20)
4271 else if (group
== 21)
4276 secret
= crypto_ecdh_set_peerkey(sm
->owe_ecdh
, 0,
4278 elems
.owe_dh_len
- 2);
4279 secret
= wpabuf_zeropad(secret
, prime_len
);
4281 wpa_printf(MSG_DEBUG
, "OWE: Invalid peer DH public key");
4284 wpa_hexdump_buf_key(MSG_DEBUG
, "OWE: DH shared secret", secret
);
4286 /* prk = HKDF-extract(C | A | group, z) */
4288 pub
= crypto_ecdh_get_pubkey(sm
->owe_ecdh
, 0);
4290 wpabuf_clear_free(secret
);
4294 /* PMKID = Truncate-128(Hash(C | A)) */
4295 addr
[0] = wpabuf_head(pub
);
4296 len
[0] = wpabuf_len(pub
);
4297 addr
[1] = elems
.owe_dh
+ 2;
4298 len
[1] = elems
.owe_dh_len
- 2;
4300 res
= sha256_vector(2, addr
, len
, pmkid
);
4301 hash_len
= SHA256_MAC_LEN
;
4302 } else if (group
== 20) {
4303 res
= sha384_vector(2, addr
, len
, pmkid
);
4304 hash_len
= SHA384_MAC_LEN
;
4305 } else if (group
== 21) {
4306 res
= sha512_vector(2, addr
, len
, pmkid
);
4307 hash_len
= SHA512_MAC_LEN
;
4312 pub
= wpabuf_zeropad(pub
, prime_len
);
4313 if (res
< 0 || !pub
) {
4315 wpabuf_clear_free(secret
);
4319 hkey
= wpabuf_alloc(wpabuf_len(pub
) + elems
.owe_dh_len
- 2 + 2);
4322 wpabuf_clear_free(secret
);
4326 wpabuf_put_buf(hkey
, pub
); /* C */
4328 wpabuf_put_data(hkey
, elems
.owe_dh
+ 2, elems
.owe_dh_len
- 2); /* A */
4329 wpabuf_put_le16(hkey
, sm
->owe_group
); /* group */
4331 res
= hmac_sha256(wpabuf_head(hkey
), wpabuf_len(hkey
),
4332 wpabuf_head(secret
), wpabuf_len(secret
), prk
);
4333 else if (group
== 20)
4334 res
= hmac_sha384(wpabuf_head(hkey
), wpabuf_len(hkey
),
4335 wpabuf_head(secret
), wpabuf_len(secret
), prk
);
4336 else if (group
== 21)
4337 res
= hmac_sha512(wpabuf_head(hkey
), wpabuf_len(hkey
),
4338 wpabuf_head(secret
), wpabuf_len(secret
), prk
);
4339 wpabuf_clear_free(hkey
);
4340 wpabuf_clear_free(secret
);
4344 wpa_hexdump_key(MSG_DEBUG
, "OWE: prk", prk
, hash_len
);
4346 /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
4349 res
= hmac_sha256_kdf(prk
, hash_len
, NULL
, (const u8
*) info
,
4350 os_strlen(info
), sm
->pmk
, hash_len
);
4351 else if (group
== 20)
4352 res
= hmac_sha384_kdf(prk
, hash_len
, NULL
, (const u8
*) info
,
4353 os_strlen(info
), sm
->pmk
, hash_len
);
4354 else if (group
== 21)
4355 res
= hmac_sha512_kdf(prk
, hash_len
, NULL
, (const u8
*) info
,
4356 os_strlen(info
), sm
->pmk
, hash_len
);
4357 os_memset(prk
, 0, SHA512_MAC_LEN
);
4360 sm
->pmk_len
= hash_len
;
4362 wpa_hexdump_key(MSG_DEBUG
, "OWE: PMK", sm
->pmk
, sm
->pmk_len
);
4363 wpa_hexdump(MSG_DEBUG
, "OWE: PMKID", pmkid
, PMKID_LEN
);
4364 pmksa_cache_add(sm
->pmksa
, sm
->pmk
, sm
->pmk_len
, pmkid
, NULL
, 0,
4365 bssid
, sm
->own_addr
, sm
->network_ctx
, sm
->key_mgmt
,
4371 #endif /* CONFIG_OWE */
4374 void wpa_sm_set_fils_cache_id(struct wpa_sm
*sm
, const u8
*fils_cache_id
)
4377 if (sm
&& fils_cache_id
) {
4378 sm
->fils_cache_id_set
= 1;
4379 os_memcpy(sm
->fils_cache_id
, fils_cache_id
, FILS_CACHE_ID_LEN
);
4381 #endif /* CONFIG_FILS */