2 * WPA Supplicant - WPA state machine and EAPOL-Key processing
3 * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
22 #include "eapol_supp/eapol_supp_sm.h"
24 #include "pmksa_cache.h"
28 #include "ieee802_11_defs.h"
32 * wpa_cipher_txt - Convert cipher suite to a text string
33 * @cipher: Cipher suite (WPA_CIPHER_* enum)
34 * Returns: Pointer to a text string of the cipher suite name
36 static const char * wpa_cipher_txt(int cipher
)
41 case WPA_CIPHER_WEP40
:
43 case WPA_CIPHER_WEP104
:
56 * wpa_key_mgmt_txt - Convert key management suite to a text string
57 * @key_mgmt: Key management suite (WPA_KEY_MGMT_* enum)
58 * @proto: WPA/WPA2 version (WPA_PROTO_*)
59 * Returns: Pointer to a text string of the key management suite name
61 static const char * wpa_key_mgmt_txt(int key_mgmt
, int proto
)
64 case WPA_KEY_MGMT_IEEE8021X
:
65 return proto
== WPA_PROTO_RSN
?
66 "WPA2/IEEE 802.1X/EAP" : "WPA/IEEE 802.1X/EAP";
67 case WPA_KEY_MGMT_PSK
:
68 return proto
== WPA_PROTO_RSN
?
69 "WPA2-PSK" : "WPA-PSK";
70 case WPA_KEY_MGMT_NONE
:
72 case WPA_KEY_MGMT_IEEE8021X_NO_WPA
:
73 return "IEEE 802.1X (no WPA)";
74 #ifdef CONFIG_IEEE80211R
75 case WPA_KEY_MGMT_FT_IEEE8021X
:
77 case WPA_KEY_MGMT_FT_PSK
:
79 #endif /* CONFIG_IEEE80211R */
87 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
88 * @sm: Pointer to WPA state machine data from wpa_sm_init()
89 * @kck: Key Confirmation Key (KCK, part of PTK)
90 * @ver: Version field from Key Info
91 * @dest: Destination address for the frame
92 * @proto: Ethertype (usually ETH_P_EAPOL)
93 * @msg: EAPOL-Key message
94 * @msg_len: Length of message
95 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
97 void wpa_eapol_key_send(struct wpa_sm
*sm
, const u8
*kck
,
98 int ver
, const u8
*dest
, u16 proto
,
99 u8
*msg
, size_t msg_len
, u8
*key_mic
)
101 if (is_zero_ether_addr(dest
) && is_zero_ether_addr(sm
->bssid
)) {
103 * Association event was not yet received; try to fetch
104 * BSSID from the driver.
106 if (wpa_sm_get_bssid(sm
, sm
->bssid
) < 0) {
107 wpa_printf(MSG_DEBUG
, "WPA: Failed to read BSSID for "
108 "EAPOL-Key destination address");
111 wpa_printf(MSG_DEBUG
, "WPA: Use BSSID (" MACSTR
112 ") as the destination for EAPOL-Key",
117 wpa_eapol_key_mic(kck
, ver
, msg
, msg_len
, key_mic
);
118 wpa_hexdump(MSG_MSGDUMP
, "WPA: TX EAPOL-Key", msg
, msg_len
);
119 wpa_sm_ether_send(sm
, dest
, proto
, msg
, msg_len
);
120 eapol_sm_notify_tx_eapol_key(sm
->eapol
);
126 * wpa_sm_key_request - Send EAPOL-Key Request
127 * @sm: Pointer to WPA state machine data from wpa_sm_init()
128 * @error: Indicate whether this is an Michael MIC error report
129 * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
130 * Returns: Pointer to the current network structure or %NULL on failure
132 * Send an EAPOL-Key Request to the current authenticator. This function is
133 * used to request rekeying and it is usually called when a local Michael MIC
134 * failure is detected.
136 void wpa_sm_key_request(struct wpa_sm
*sm
, int error
, int pairwise
)
139 struct wpa_eapol_key
*reply
;
141 u8 bssid
[ETH_ALEN
], *rbuf
;
143 if (sm
->key_mgmt
== WPA_KEY_MGMT_FT_IEEE8021X
||
144 sm
->key_mgmt
== WPA_KEY_MGMT_FT_PSK
)
145 ver
= WPA_KEY_INFO_TYPE_AES_128_CMAC
;
146 else if (sm
->pairwise_cipher
== WPA_CIPHER_CCMP
)
147 ver
= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
;
149 ver
= WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
;
151 if (wpa_sm_get_bssid(sm
, bssid
) < 0) {
152 wpa_printf(MSG_WARNING
, "Failed to read BSSID for EAPOL-Key "
157 rbuf
= wpa_sm_alloc_eapol(sm
, IEEE802_1X_TYPE_EAPOL_KEY
, NULL
,
158 sizeof(*reply
), &rlen
, (void *) &reply
);
162 reply
->type
= sm
->proto
== WPA_PROTO_RSN
?
163 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
164 key_info
= WPA_KEY_INFO_REQUEST
| ver
;
166 key_info
|= WPA_KEY_INFO_MIC
;
168 key_info
|= WPA_KEY_INFO_ERROR
;
170 key_info
|= WPA_KEY_INFO_KEY_TYPE
;
171 WPA_PUT_BE16(reply
->key_info
, key_info
);
172 WPA_PUT_BE16(reply
->key_length
, 0);
173 os_memcpy(reply
->replay_counter
, sm
->request_counter
,
174 WPA_REPLAY_COUNTER_LEN
);
175 inc_byte_array(sm
->request_counter
, WPA_REPLAY_COUNTER_LEN
);
177 WPA_PUT_BE16(reply
->key_data_length
, 0);
179 wpa_printf(MSG_INFO
, "WPA: Sending EAPOL-Key Request (error=%d "
180 "pairwise=%d ptk_set=%d len=%lu)",
181 error
, pairwise
, sm
->ptk_set
, (unsigned long) rlen
);
182 wpa_eapol_key_send(sm
, sm
->ptk
.kck
, ver
, bssid
, ETH_P_EAPOL
,
183 rbuf
, rlen
, key_info
& WPA_KEY_INFO_MIC
?
184 reply
->key_mic
: NULL
);
188 static int wpa_supplicant_get_pmk(struct wpa_sm
*sm
,
189 const unsigned char *src_addr
,
192 int abort_cached
= 0;
194 if (pmkid
&& !sm
->cur_pmksa
) {
195 /* When using drivers that generate RSN IE, wpa_supplicant may
196 * not have enough time to get the association information
197 * event before receiving this 1/4 message, so try to find a
198 * matching PMKSA cache entry here. */
199 sm
->cur_pmksa
= pmksa_cache_get(sm
->pmksa
, src_addr
, pmkid
);
201 wpa_printf(MSG_DEBUG
, "RSN: found matching PMKID from "
204 wpa_printf(MSG_DEBUG
, "RSN: no matching PMKID found");
209 if (pmkid
&& sm
->cur_pmksa
&&
210 os_memcmp(pmkid
, sm
->cur_pmksa
->pmkid
, PMKID_LEN
) == 0) {
211 wpa_hexdump(MSG_DEBUG
, "RSN: matched PMKID", pmkid
, PMKID_LEN
);
212 wpa_sm_set_pmk_from_pmksa(sm
);
213 wpa_hexdump_key(MSG_DEBUG
, "RSN: PMK from PMKSA cache",
214 sm
->pmk
, sm
->pmk_len
);
215 eapol_sm_notify_cached(sm
->eapol
);
216 #ifdef CONFIG_IEEE80211R
218 #endif /* CONFIG_IEEE80211R */
219 } else if ((sm
->key_mgmt
== WPA_KEY_MGMT_IEEE8021X
||
220 sm
->key_mgmt
== WPA_KEY_MGMT_FT_IEEE8021X
) && sm
->eapol
) {
223 res
= eapol_sm_get_key(sm
->eapol
, sm
->pmk
, PMK_LEN
);
226 * EAP-LEAP is an exception from other EAP methods: it
227 * uses only 16-byte PMK.
229 res
= eapol_sm_get_key(sm
->eapol
, sm
->pmk
, 16);
232 #ifdef CONFIG_IEEE80211R
234 if (eapol_sm_get_key(sm
->eapol
, buf
, 2 * PMK_LEN
) == 0)
236 os_memcpy(sm
->xxkey
, buf
+ PMK_LEN
, PMK_LEN
);
237 sm
->xxkey_len
= PMK_LEN
;
238 os_memset(buf
, 0, sizeof(buf
));
240 #endif /* CONFIG_IEEE80211R */
243 wpa_hexdump_key(MSG_DEBUG
, "WPA: PMK from EAPOL state "
244 "machines", sm
->pmk
, pmk_len
);
245 sm
->pmk_len
= pmk_len
;
246 pmksa_cache_add(sm
->pmksa
, sm
->pmk
, pmk_len
, src_addr
,
247 sm
->own_addr
, sm
->network_ctx
);
248 if (!sm
->cur_pmksa
&& pmkid
&&
249 pmksa_cache_get(sm
->pmksa
, src_addr
, pmkid
)) {
250 wpa_printf(MSG_DEBUG
, "RSN: the new PMK "
251 "matches with the PMKID");
255 wpa_msg(sm
->ctx
->ctx
, MSG_WARNING
,
256 "WPA: Failed to get master session key from "
257 "EAPOL state machines");
258 wpa_msg(sm
->ctx
->ctx
, MSG_WARNING
,
259 "WPA: Key handshake aborted");
261 wpa_printf(MSG_DEBUG
, "RSN: Cancelled PMKSA "
263 sm
->cur_pmksa
= NULL
;
271 if (abort_cached
&& (sm
->key_mgmt
== WPA_KEY_MGMT_IEEE8021X
||
272 sm
->key_mgmt
== WPA_KEY_MGMT_FT_IEEE8021X
)) {
273 /* Send EAPOL-Start to trigger full EAP authentication. */
277 wpa_printf(MSG_DEBUG
, "RSN: no PMKSA entry found - trigger "
278 "full EAP authentication");
279 buf
= wpa_sm_alloc_eapol(sm
, IEEE802_1X_TYPE_EAPOL_START
,
280 NULL
, 0, &buflen
, NULL
);
282 wpa_sm_ether_send(sm
, sm
->bssid
, ETH_P_EAPOL
,
295 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
296 * @sm: Pointer to WPA state machine data from wpa_sm_init()
297 * @dst: Destination address for the frame
298 * @key: Pointer to the EAPOL-Key frame header
299 * @ver: Version bits from EAPOL-Key Key Info
300 * @nonce: Nonce value for the EAPOL-Key frame
301 * @wpa_ie: WPA/RSN IE
302 * @wpa_ie_len: Length of the WPA/RSN IE
303 * @ptk: PTK to use for keyed hash and encryption
304 * Returns: 0 on success, -1 on failure
306 int wpa_supplicant_send_2_of_4(struct wpa_sm
*sm
, const unsigned char *dst
,
307 const struct wpa_eapol_key
*key
,
308 int ver
, const u8
*nonce
,
309 const u8
*wpa_ie
, size_t wpa_ie_len
,
313 struct wpa_eapol_key
*reply
;
316 if (wpa_ie
== NULL
) {
317 wpa_printf(MSG_WARNING
, "WPA: No wpa_ie set - cannot "
322 wpa_hexdump(MSG_DEBUG
, "WPA: WPA IE for msg 2/4", wpa_ie
, wpa_ie_len
);
324 rbuf
= wpa_sm_alloc_eapol(sm
, IEEE802_1X_TYPE_EAPOL_KEY
,
325 NULL
, sizeof(*reply
) + wpa_ie_len
,
326 &rlen
, (void *) &reply
);
330 reply
->type
= sm
->proto
== WPA_PROTO_RSN
?
331 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
332 WPA_PUT_BE16(reply
->key_info
,
333 ver
| WPA_KEY_INFO_KEY_TYPE
| WPA_KEY_INFO_MIC
);
334 if (sm
->proto
== WPA_PROTO_RSN
)
335 WPA_PUT_BE16(reply
->key_length
, 0);
337 os_memcpy(reply
->key_length
, key
->key_length
, 2);
338 os_memcpy(reply
->replay_counter
, key
->replay_counter
,
339 WPA_REPLAY_COUNTER_LEN
);
341 WPA_PUT_BE16(reply
->key_data_length
, wpa_ie_len
);
342 os_memcpy(reply
+ 1, wpa_ie
, wpa_ie_len
);
344 os_memcpy(reply
->key_nonce
, nonce
, WPA_NONCE_LEN
);
346 wpa_printf(MSG_DEBUG
, "WPA: Sending EAPOL-Key 2/4");
347 wpa_eapol_key_send(sm
, ptk
->kck
, ver
, dst
, ETH_P_EAPOL
,
348 rbuf
, rlen
, reply
->key_mic
);
354 static int wpa_derive_ptk(struct wpa_sm
*sm
, const unsigned char *src_addr
,
355 const struct wpa_eapol_key
*key
,
358 #ifdef CONFIG_IEEE80211R
359 if (sm
->key_mgmt
== WPA_KEY_MGMT_FT_IEEE8021X
||
360 sm
->key_mgmt
== WPA_KEY_MGMT_FT_PSK
)
361 return wpa_derive_ptk_ft(sm
, src_addr
, key
, ptk
);
362 #endif /* CONFIG_IEEE80211R */
364 wpa_pmk_to_ptk(sm
->pmk
, sm
->pmk_len
, "Pairwise key expansion",
365 sm
->own_addr
, sm
->bssid
, sm
->snonce
, key
->key_nonce
,
366 (u8
*) ptk
, sizeof(*ptk
));
371 static void wpa_supplicant_process_1_of_4(struct wpa_sm
*sm
,
372 const unsigned char *src_addr
,
373 const struct wpa_eapol_key
*key
,
376 struct wpa_eapol_ie_parse ie
;
380 if (wpa_sm_get_network_ctx(sm
) == NULL
) {
381 wpa_printf(MSG_WARNING
, "WPA: No SSID info found (msg 1 of "
386 wpa_sm_set_state(sm
, WPA_4WAY_HANDSHAKE
);
387 wpa_printf(MSG_DEBUG
, "WPA: RX message 1 of 4-Way Handshake from "
388 MACSTR
" (ver=%d)", MAC2STR(src_addr
), ver
);
390 os_memset(&ie
, 0, sizeof(ie
));
392 #ifndef CONFIG_NO_WPA2
393 if (sm
->proto
== WPA_PROTO_RSN
) {
394 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
395 const u8
*_buf
= (const u8
*) (key
+ 1);
396 size_t len
= WPA_GET_BE16(key
->key_data_length
);
397 wpa_hexdump(MSG_DEBUG
, "RSN: msg 1/4 key data", _buf
, len
);
398 wpa_supplicant_parse_ies(_buf
, len
, &ie
);
400 wpa_hexdump(MSG_DEBUG
, "RSN: PMKID from "
401 "Authenticator", ie
.pmkid
, PMKID_LEN
);
404 #endif /* CONFIG_NO_WPA2 */
406 if (wpa_supplicant_get_pmk(sm
, src_addr
, ie
.pmkid
))
409 if (sm
->renew_snonce
) {
410 if (os_get_random(sm
->snonce
, WPA_NONCE_LEN
)) {
411 wpa_msg(sm
->ctx
->ctx
, MSG_WARNING
,
412 "WPA: Failed to get random data for SNonce");
415 sm
->renew_snonce
= 0;
416 wpa_hexdump(MSG_DEBUG
, "WPA: Renewed SNonce",
417 sm
->snonce
, WPA_NONCE_LEN
);
420 /* Calculate PTK which will be stored as a temporary PTK until it has
421 * been verified when processing message 3/4. */
423 wpa_derive_ptk(sm
, src_addr
, key
, ptk
);
424 /* Supplicant: swap tx/rx Mic keys */
425 os_memcpy(buf
, ptk
->u
.auth
.tx_mic_key
, 8);
426 os_memcpy(ptk
->u
.auth
.tx_mic_key
, ptk
->u
.auth
.rx_mic_key
, 8);
427 os_memcpy(ptk
->u
.auth
.rx_mic_key
, buf
, 8);
430 if (wpa_supplicant_send_2_of_4(sm
, sm
->bssid
, key
, ver
, sm
->snonce
,
431 sm
->assoc_wpa_ie
, sm
->assoc_wpa_ie_len
,
435 os_memcpy(sm
->anonce
, key
->key_nonce
, WPA_NONCE_LEN
);
439 static void wpa_sm_start_preauth(void *eloop_ctx
, void *timeout_ctx
)
441 struct wpa_sm
*sm
= eloop_ctx
;
442 rsn_preauth_candidate_process(sm
);
446 static void wpa_supplicant_key_neg_complete(struct wpa_sm
*sm
,
447 const u8
*addr
, int secure
)
449 wpa_msg(sm
->ctx
->ctx
, MSG_INFO
, "WPA: Key negotiation completed with "
450 MACSTR
" [PTK=%s GTK=%s]", MAC2STR(addr
),
451 wpa_cipher_txt(sm
->pairwise_cipher
),
452 wpa_cipher_txt(sm
->group_cipher
));
453 wpa_sm_cancel_auth_timeout(sm
);
454 wpa_sm_set_state(sm
, WPA_COMPLETED
);
457 wpa_sm_mlme_setprotection(
458 sm
, addr
, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX
,
459 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE
);
460 eapol_sm_notify_portValid(sm
->eapol
, TRUE
);
461 if (sm
->key_mgmt
== WPA_KEY_MGMT_PSK
||
462 sm
->key_mgmt
== WPA_KEY_MGMT_FT_PSK
)
463 eapol_sm_notify_eap_success(sm
->eapol
, TRUE
);
465 * Start preauthentication after a short wait to avoid a
466 * possible race condition between the data receive and key
467 * configuration after the 4-Way Handshake. This increases the
468 * likelyhood of the first preauth EAPOL-Start frame getting to
471 eloop_register_timeout(1, 0, wpa_sm_start_preauth
, sm
, NULL
);
474 if (sm
->cur_pmksa
&& sm
->cur_pmksa
->opportunistic
) {
475 wpa_printf(MSG_DEBUG
, "RSN: Authenticator accepted "
476 "opportunistic PMKSA entry - marking it valid");
477 sm
->cur_pmksa
->opportunistic
= 0;
480 #ifdef CONFIG_IEEE80211R
481 if (sm
->key_mgmt
== WPA_KEY_MGMT_FT_IEEE8021X
||
482 sm
->key_mgmt
== WPA_KEY_MGMT_FT_PSK
) {
483 /* Prepare for the next transition */
484 wpa_ft_prepare_auth_request(sm
);
486 #endif /* CONFIG_IEEE80211R */
490 static int wpa_supplicant_install_ptk(struct wpa_sm
*sm
,
491 const struct wpa_eapol_key
*key
)
496 u8 null_rsc
[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
498 wpa_printf(MSG_DEBUG
, "WPA: Installing PTK to the driver.");
500 switch (sm
->pairwise_cipher
) {
501 case WPA_CIPHER_CCMP
:
506 case WPA_CIPHER_TKIP
:
511 case WPA_CIPHER_NONE
:
512 wpa_printf(MSG_DEBUG
, "WPA: Pairwise Cipher Suite: "
513 "NONE - do not use pairwise keys");
516 wpa_printf(MSG_WARNING
, "WPA: Unsupported pairwise cipher %d",
517 sm
->pairwise_cipher
);
521 if (sm
->proto
== WPA_PROTO_RSN
) {
524 key_rsc
= key
->key_rsc
;
525 wpa_hexdump(MSG_DEBUG
, "WPA: RSC", key_rsc
, rsclen
);
528 if (wpa_sm_set_key(sm
, alg
, sm
->bssid
, 0, 1, key_rsc
, rsclen
,
529 (u8
*) sm
->ptk
.tk1
, keylen
) < 0) {
530 wpa_printf(MSG_WARNING
, "WPA: Failed to set PTK to the "
538 static int wpa_supplicant_check_group_cipher(int group_cipher
,
539 int keylen
, int maxkeylen
,
540 int *key_rsc_len
, wpa_alg
*alg
)
544 switch (group_cipher
) {
545 case WPA_CIPHER_CCMP
:
546 if (keylen
!= 16 || maxkeylen
< 16) {
553 case WPA_CIPHER_TKIP
:
554 if (keylen
!= 32 || maxkeylen
< 32) {
561 case WPA_CIPHER_WEP104
:
562 if (keylen
!= 13 || maxkeylen
< 13) {
569 case WPA_CIPHER_WEP40
:
570 if (keylen
!= 5 || maxkeylen
< 5) {
578 wpa_printf(MSG_WARNING
, "WPA: Unsupported Group Cipher %d",
584 wpa_printf(MSG_WARNING
, "WPA: Unsupported %s Group Cipher key "
586 wpa_cipher_txt(group_cipher
), keylen
, maxkeylen
);
593 struct wpa_gtk_data
{
595 int tx
, key_rsc_len
, keyidx
;
601 static int wpa_supplicant_install_gtk(struct wpa_sm
*sm
,
602 const struct wpa_gtk_data
*gd
,
605 const u8
*_gtk
= gd
->gtk
;
608 wpa_hexdump_key(MSG_DEBUG
, "WPA: Group Key", gd
->gtk
, gd
->gtk_len
);
609 wpa_printf(MSG_DEBUG
, "WPA: Installing GTK to the driver "
610 "(keyidx=%d tx=%d len=%d).", gd
->keyidx
, gd
->tx
,
612 wpa_hexdump(MSG_DEBUG
, "WPA: RSC", key_rsc
, gd
->key_rsc_len
);
613 if (sm
->group_cipher
== WPA_CIPHER_TKIP
) {
614 /* Swap Tx/Rx keys for Michael MIC */
615 os_memcpy(gtk_buf
, gd
->gtk
, 16);
616 os_memcpy(gtk_buf
+ 16, gd
->gtk
+ 24, 8);
617 os_memcpy(gtk_buf
+ 24, gd
->gtk
+ 16, 8);
620 if (sm
->pairwise_cipher
== WPA_CIPHER_NONE
) {
621 if (wpa_sm_set_key(sm
, gd
->alg
,
622 (u8
*) "\xff\xff\xff\xff\xff\xff",
623 gd
->keyidx
, 1, key_rsc
, gd
->key_rsc_len
,
624 _gtk
, gd
->gtk_len
) < 0) {
625 wpa_printf(MSG_WARNING
, "WPA: Failed to set "
626 "GTK to the driver (Group only).");
629 } else if (wpa_sm_set_key(sm
, gd
->alg
,
630 (u8
*) "\xff\xff\xff\xff\xff\xff",
631 gd
->keyidx
, gd
->tx
, key_rsc
, gd
->key_rsc_len
,
632 _gtk
, gd
->gtk_len
) < 0) {
633 wpa_printf(MSG_WARNING
, "WPA: Failed to set GTK to "
642 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm
*sm
,
645 if (tx
&& sm
->pairwise_cipher
!= WPA_CIPHER_NONE
) {
646 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
647 * seemed to set this bit (incorrectly, since Tx is only when
648 * doing Group Key only APs) and without this workaround, the
649 * data connection does not work because wpa_supplicant
650 * configured non-zero keyidx to be used for unicast. */
651 wpa_printf(MSG_INFO
, "WPA: Tx bit set for GTK, but pairwise "
652 "keys are used - ignore Tx bit");
659 static int wpa_supplicant_pairwise_gtk(struct wpa_sm
*sm
,
660 const struct wpa_eapol_key
*key
,
661 const u8
*gtk
, size_t gtk_len
,
664 #ifndef CONFIG_NO_WPA2
665 struct wpa_gtk_data gd
;
668 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
670 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
671 * Reserved [bits 0-7]
675 os_memset(&gd
, 0, sizeof(gd
));
676 wpa_hexdump_key(MSG_DEBUG
, "RSN: received GTK in pairwise handshake",
679 if (gtk_len
< 2 || gtk_len
- 2 > sizeof(gd
.gtk
))
682 gd
.keyidx
= gtk
[0] & 0x3;
683 gd
.tx
= wpa_supplicant_gtk_tx_bit_workaround(sm
,
684 !!(gtk
[0] & BIT(2)));
688 os_memcpy(gd
.gtk
, gtk
, gtk_len
);
689 gd
.gtk_len
= gtk_len
;
691 if (wpa_supplicant_check_group_cipher(sm
->group_cipher
,
693 &gd
.key_rsc_len
, &gd
.alg
) ||
694 wpa_supplicant_install_gtk(sm
, &gd
, key
->key_rsc
)) {
695 wpa_printf(MSG_DEBUG
, "RSN: Failed to install GTK");
699 wpa_supplicant_key_neg_complete(sm
, sm
->bssid
,
700 key_info
& WPA_KEY_INFO_SECURE
);
702 #else /* CONFIG_NO_WPA2 */
704 #endif /* CONFIG_NO_WPA2 */
708 static int ieee80211w_set_keys(struct wpa_sm
*sm
,
709 struct wpa_eapol_ie_parse
*ie
)
711 #ifdef CONFIG_IEEE80211W
712 if (sm
->mgmt_group_cipher
!= WPA_CIPHER_AES_128_CMAC
)
716 const struct wpa_igtk_kde
*igtk
;
718 if (ie
->igtk_len
!= sizeof(*igtk
))
720 igtk
= (const struct wpa_igtk_kde
*) ie
->igtk
;
721 keyidx
= WPA_GET_LE16(igtk
->keyid
);
722 wpa_printf(MSG_DEBUG
, "WPA: IGTK keyid %d "
723 "pn %02x%02x%02x%02x%02x%02x",
724 keyidx
, MAC2STR(igtk
->pn
));
725 wpa_hexdump_key(MSG_DEBUG
, "WPA: IGTK",
726 igtk
->igtk
, WPA_IGTK_LEN
);
728 wpa_printf(MSG_WARNING
, "WPA: Invalid IGTK KeyID %d",
732 if (wpa_sm_set_key(sm
, WPA_ALG_IGTK
,
733 (u8
*) "\xff\xff\xff\xff\xff\xff",
734 keyidx
, 0, igtk
->pn
, sizeof(igtk
->pn
),
735 igtk
->igtk
, WPA_IGTK_LEN
) < 0) {
736 wpa_printf(MSG_WARNING
, "WPA: Failed to configure IGTK"
743 #else /* CONFIG_IEEE80211W */
745 #endif /* CONFIG_IEEE80211W */
749 static void wpa_report_ie_mismatch(struct wpa_sm
*sm
,
750 const char *reason
, const u8
*src_addr
,
751 const u8
*wpa_ie
, size_t wpa_ie_len
,
752 const u8
*rsn_ie
, size_t rsn_ie_len
)
754 wpa_msg(sm
->ctx
->ctx
, MSG_WARNING
, "WPA: %s (src=" MACSTR
")",
755 reason
, MAC2STR(src_addr
));
758 wpa_hexdump(MSG_INFO
, "WPA: WPA IE in Beacon/ProbeResp",
759 sm
->ap_wpa_ie
, sm
->ap_wpa_ie_len
);
762 if (!sm
->ap_wpa_ie
) {
763 wpa_printf(MSG_INFO
, "WPA: No WPA IE in "
766 wpa_hexdump(MSG_INFO
, "WPA: WPA IE in 3/4 msg",
771 wpa_hexdump(MSG_INFO
, "WPA: RSN IE in Beacon/ProbeResp",
772 sm
->ap_rsn_ie
, sm
->ap_rsn_ie_len
);
775 if (!sm
->ap_rsn_ie
) {
776 wpa_printf(MSG_INFO
, "WPA: No RSN IE in "
779 wpa_hexdump(MSG_INFO
, "WPA: RSN IE in 3/4 msg",
783 wpa_sm_disassociate(sm
, WLAN_REASON_IE_IN_4WAY_DIFFERS
);
787 static int wpa_supplicant_validate_ie(struct wpa_sm
*sm
,
788 const unsigned char *src_addr
,
789 struct wpa_eapol_ie_parse
*ie
)
791 if (sm
->ap_wpa_ie
== NULL
&& sm
->ap_rsn_ie
== NULL
) {
792 wpa_printf(MSG_DEBUG
, "WPA: No WPA/RSN IE for this AP known. "
793 "Trying to get from scan results");
794 if (wpa_sm_get_beacon_ie(sm
) < 0) {
795 wpa_printf(MSG_WARNING
, "WPA: Could not find AP from "
798 wpa_printf(MSG_DEBUG
, "WPA: Found the current AP from "
799 "updated scan results");
803 if (ie
->wpa_ie
== NULL
&& ie
->rsn_ie
== NULL
&&
804 (sm
->ap_wpa_ie
|| sm
->ap_rsn_ie
)) {
805 wpa_report_ie_mismatch(sm
, "IE in 3/4 msg does not match "
806 "with IE in Beacon/ProbeResp (no IE?)",
807 src_addr
, ie
->wpa_ie
, ie
->wpa_ie_len
,
808 ie
->rsn_ie
, ie
->rsn_ie_len
);
812 if ((ie
->wpa_ie
&& sm
->ap_wpa_ie
&&
813 (ie
->wpa_ie_len
!= sm
->ap_wpa_ie_len
||
814 os_memcmp(ie
->wpa_ie
, sm
->ap_wpa_ie
, ie
->wpa_ie_len
) != 0)) ||
815 (ie
->rsn_ie
&& sm
->ap_rsn_ie
&&
816 (ie
->rsn_ie_len
!= sm
->ap_rsn_ie_len
||
817 os_memcmp(ie
->rsn_ie
, sm
->ap_rsn_ie
, ie
->rsn_ie_len
) != 0))) {
818 wpa_report_ie_mismatch(sm
, "IE in 3/4 msg does not match "
819 "with IE in Beacon/ProbeResp",
820 src_addr
, ie
->wpa_ie
, ie
->wpa_ie_len
,
821 ie
->rsn_ie
, ie
->rsn_ie_len
);
825 if (sm
->proto
== WPA_PROTO_WPA
&&
826 ie
->rsn_ie
&& sm
->ap_rsn_ie
== NULL
&& sm
->rsn_enabled
) {
827 wpa_report_ie_mismatch(sm
, "Possible downgrade attack "
828 "detected - RSN was enabled and RSN IE "
829 "was in msg 3/4, but not in "
831 src_addr
, ie
->wpa_ie
, ie
->wpa_ie_len
,
832 ie
->rsn_ie
, ie
->rsn_ie_len
);
836 #ifdef CONFIG_IEEE80211R
837 if (sm
->key_mgmt
== WPA_KEY_MGMT_FT_IEEE8021X
||
838 sm
->key_mgmt
== WPA_KEY_MGMT_FT_PSK
) {
839 struct rsn_mdie
*mdie
;
840 /* TODO: verify that full MDIE matches with the one from scan
841 * results, not only mobility domain */
842 mdie
= (struct rsn_mdie
*) (ie
->mdie
+ 2);
843 if (ie
->mdie
== NULL
|| ie
->mdie_len
< 2 + sizeof(*mdie
) ||
844 os_memcmp(mdie
->mobility_domain
, sm
->mobility_domain
,
845 MOBILITY_DOMAIN_ID_LEN
) != 0) {
846 wpa_printf(MSG_DEBUG
, "FT: MDIE in msg 3/4 did not "
847 "match with the current mobility domain");
851 #endif /* CONFIG_IEEE80211R */
858 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
859 * @sm: Pointer to WPA state machine data from wpa_sm_init()
860 * @dst: Destination address for the frame
861 * @key: Pointer to the EAPOL-Key frame header
862 * @ver: Version bits from EAPOL-Key Key Info
863 * @key_info: Key Info
864 * @kde: KDEs to include the EAPOL-Key frame
865 * @kde_len: Length of KDEs
866 * @ptk: PTK to use for keyed hash and encryption
867 * Returns: 0 on success, -1 on failure
869 int wpa_supplicant_send_4_of_4(struct wpa_sm
*sm
, const unsigned char *dst
,
870 const struct wpa_eapol_key
*key
,
871 u16 ver
, u16 key_info
,
872 const u8
*kde
, size_t kde_len
,
876 struct wpa_eapol_key
*reply
;
880 wpa_hexdump(MSG_DEBUG
, "WPA: KDE for msg 4/4", kde
, kde_len
);
882 rbuf
= wpa_sm_alloc_eapol(sm
, IEEE802_1X_TYPE_EAPOL_KEY
, NULL
,
883 sizeof(*reply
) + kde_len
,
884 &rlen
, (void *) &reply
);
888 reply
->type
= sm
->proto
== WPA_PROTO_RSN
?
889 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
890 key_info
&= WPA_KEY_INFO_SECURE
;
891 key_info
|= ver
| WPA_KEY_INFO_KEY_TYPE
| WPA_KEY_INFO_MIC
;
892 WPA_PUT_BE16(reply
->key_info
, key_info
);
893 if (sm
->proto
== WPA_PROTO_RSN
)
894 WPA_PUT_BE16(reply
->key_length
, 0);
896 os_memcpy(reply
->key_length
, key
->key_length
, 2);
897 os_memcpy(reply
->replay_counter
, key
->replay_counter
,
898 WPA_REPLAY_COUNTER_LEN
);
900 WPA_PUT_BE16(reply
->key_data_length
, kde_len
);
902 os_memcpy(reply
+ 1, kde
, kde_len
);
904 wpa_printf(MSG_DEBUG
, "WPA: Sending EAPOL-Key 4/4");
905 wpa_eapol_key_send(sm
, ptk
->kck
, ver
, dst
, ETH_P_EAPOL
,
906 rbuf
, rlen
, reply
->key_mic
);
912 static void wpa_supplicant_process_3_of_4(struct wpa_sm
*sm
,
913 const struct wpa_eapol_key
*key
,
916 u16 key_info
, keylen
, len
;
918 struct wpa_eapol_ie_parse ie
;
920 wpa_sm_set_state(sm
, WPA_4WAY_HANDSHAKE
);
921 wpa_printf(MSG_DEBUG
, "WPA: RX message 3 of 4-Way Handshake from "
922 MACSTR
" (ver=%d)", MAC2STR(sm
->bssid
), ver
);
924 key_info
= WPA_GET_BE16(key
->key_info
);
926 pos
= (const u8
*) (key
+ 1);
927 len
= WPA_GET_BE16(key
->key_data_length
);
928 wpa_hexdump(MSG_DEBUG
, "WPA: IE KeyData", pos
, len
);
929 wpa_supplicant_parse_ies(pos
, len
, &ie
);
930 if (ie
.gtk
&& !(key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
931 wpa_printf(MSG_WARNING
, "WPA: GTK IE in unencrypted key data");
934 #ifdef CONFIG_IEEE80211W
935 if (ie
.igtk
&& !(key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
936 wpa_printf(MSG_WARNING
, "WPA: IGTK KDE in unencrypted key "
941 if (ie
.igtk
&& ie
.igtk_len
!= sizeof(struct wpa_igtk_kde
)) {
942 wpa_printf(MSG_WARNING
, "WPA: Invalid IGTK KDE length %lu",
943 (unsigned long) ie
.igtk_len
);
946 #endif /* CONFIG_IEEE80211W */
948 if (wpa_supplicant_validate_ie(sm
, sm
->bssid
, &ie
) < 0)
951 if (os_memcmp(sm
->anonce
, key
->key_nonce
, WPA_NONCE_LEN
) != 0) {
952 wpa_printf(MSG_WARNING
, "WPA: ANonce from message 1 of 4-Way "
953 "Handshake differs from 3 of 4-Way Handshake - drop"
954 " packet (src=" MACSTR
")", MAC2STR(sm
->bssid
));
958 keylen
= WPA_GET_BE16(key
->key_length
);
959 switch (sm
->pairwise_cipher
) {
960 case WPA_CIPHER_CCMP
:
962 wpa_printf(MSG_WARNING
, "WPA: Invalid CCMP key length "
963 "%d (src=" MACSTR
")",
964 keylen
, MAC2STR(sm
->bssid
));
968 case WPA_CIPHER_TKIP
:
970 wpa_printf(MSG_WARNING
, "WPA: Invalid TKIP key length "
971 "%d (src=" MACSTR
")",
972 keylen
, MAC2STR(sm
->bssid
));
978 if (wpa_supplicant_send_4_of_4(sm
, sm
->bssid
, key
, ver
, key_info
,
982 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
983 * for the next 4-Way Handshake. If msg 3 is received again, the old
984 * SNonce will still be used to avoid changing PTK. */
985 sm
->renew_snonce
= 1;
987 if (key_info
& WPA_KEY_INFO_INSTALL
) {
988 wpa_supplicant_install_ptk(sm
, key
);
991 if (key_info
& WPA_KEY_INFO_SECURE
) {
992 wpa_sm_mlme_setprotection(
993 sm
, sm
->bssid
, MLME_SETPROTECTION_PROTECT_TYPE_RX
,
994 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE
);
995 eapol_sm_notify_portValid(sm
->eapol
, TRUE
);
997 wpa_sm_set_state(sm
, WPA_GROUP_HANDSHAKE
);
1000 wpa_supplicant_pairwise_gtk(sm
, key
,
1001 ie
.gtk
, ie
.gtk_len
, key_info
) < 0) {
1002 wpa_printf(MSG_INFO
, "RSN: Failed to configure GTK");
1005 if (ieee80211w_set_keys(sm
, &ie
) < 0)
1006 wpa_printf(MSG_INFO
, "RSN: Failed to configure IGTK");
1010 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm
*sm
,
1014 struct wpa_gtk_data
*gd
)
1017 struct wpa_eapol_ie_parse ie
;
1019 wpa_hexdump(MSG_DEBUG
, "RSN: msg 1/2 key data", keydata
, keydatalen
);
1020 wpa_supplicant_parse_ies(keydata
, keydatalen
, &ie
);
1021 if (ie
.gtk
&& !(key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
1022 wpa_printf(MSG_WARNING
, "WPA: GTK IE in unencrypted key data");
1025 if (ie
.gtk
== NULL
) {
1026 wpa_printf(MSG_INFO
, "WPA: No GTK IE in Group Key msg 1/2");
1029 maxkeylen
= gd
->gtk_len
= ie
.gtk_len
- 2;
1031 if (wpa_supplicant_check_group_cipher(sm
->group_cipher
,
1032 gd
->gtk_len
, maxkeylen
,
1033 &gd
->key_rsc_len
, &gd
->alg
))
1036 wpa_hexdump(MSG_DEBUG
, "RSN: received GTK in group key handshake",
1037 ie
.gtk
, ie
.gtk_len
);
1038 gd
->keyidx
= ie
.gtk
[0] & 0x3;
1039 gd
->tx
= wpa_supplicant_gtk_tx_bit_workaround(sm
,
1040 !!(ie
.gtk
[0] & BIT(2)));
1041 if (ie
.gtk_len
- 2 > sizeof(gd
->gtk
)) {
1042 wpa_printf(MSG_INFO
, "RSN: Too long GTK in GTK IE "
1043 "(len=%lu)", (unsigned long) ie
.gtk_len
- 2);
1046 os_memcpy(gd
->gtk
, ie
.gtk
+ 2, ie
.gtk_len
- 2);
1048 if (ieee80211w_set_keys(sm
, &ie
) < 0)
1049 wpa_printf(MSG_INFO
, "RSN: Failed to configure IGTK");
1055 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm
*sm
,
1056 const struct wpa_eapol_key
*key
,
1057 size_t keydatalen
, int key_info
,
1058 size_t extra_len
, u16 ver
,
1059 struct wpa_gtk_data
*gd
)
1064 gd
->gtk_len
= WPA_GET_BE16(key
->key_length
);
1065 maxkeylen
= keydatalen
;
1066 if (keydatalen
> extra_len
) {
1067 wpa_printf(MSG_INFO
, "WPA: Truncated EAPOL-Key packet:"
1068 " key_data_length=%lu > extra_len=%lu",
1069 (unsigned long) keydatalen
,
1070 (unsigned long) extra_len
);
1073 if (ver
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
1074 if (maxkeylen
< 8) {
1075 wpa_printf(MSG_INFO
, "WPA: Too short maxkeylen (%lu)",
1076 (unsigned long) maxkeylen
);
1082 if (wpa_supplicant_check_group_cipher(sm
->group_cipher
,
1083 gd
->gtk_len
, maxkeylen
,
1084 &gd
->key_rsc_len
, &gd
->alg
))
1087 gd
->keyidx
= (key_info
& WPA_KEY_INFO_KEY_INDEX_MASK
) >>
1088 WPA_KEY_INFO_KEY_INDEX_SHIFT
;
1089 if (ver
== WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
) {
1090 os_memcpy(ek
, key
->key_iv
, 16);
1091 os_memcpy(ek
+ 16, sm
->ptk
.kek
, 16);
1092 if (keydatalen
> sizeof(gd
->gtk
)) {
1093 wpa_printf(MSG_WARNING
, "WPA: RC4 key data "
1095 (unsigned long) keydatalen
);
1098 os_memcpy(gd
->gtk
, key
+ 1, keydatalen
);
1099 rc4_skip(ek
, 32, 256, gd
->gtk
, keydatalen
);
1100 } else if (ver
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
1101 if (keydatalen
% 8) {
1102 wpa_printf(MSG_WARNING
, "WPA: Unsupported AES-WRAP "
1103 "len %lu", (unsigned long) keydatalen
);
1106 if (maxkeylen
> sizeof(gd
->gtk
)) {
1107 wpa_printf(MSG_WARNING
, "WPA: AES-WRAP key data "
1108 "too long (keydatalen=%lu maxkeylen=%lu)",
1109 (unsigned long) keydatalen
,
1110 (unsigned long) maxkeylen
);
1113 if (aes_unwrap(sm
->ptk
.kek
, maxkeylen
/ 8,
1114 (const u8
*) (key
+ 1), gd
->gtk
)) {
1115 wpa_printf(MSG_WARNING
, "WPA: AES unwrap "
1116 "failed - could not decrypt GTK");
1120 wpa_printf(MSG_WARNING
, "WPA: Unsupported key_info type %d",
1124 gd
->tx
= wpa_supplicant_gtk_tx_bit_workaround(
1125 sm
, !!(key_info
& WPA_KEY_INFO_TXRX
));
1130 static int wpa_supplicant_send_2_of_2(struct wpa_sm
*sm
,
1131 const struct wpa_eapol_key
*key
,
1132 int ver
, u16 key_info
)
1135 struct wpa_eapol_key
*reply
;
1138 rbuf
= wpa_sm_alloc_eapol(sm
, IEEE802_1X_TYPE_EAPOL_KEY
, NULL
,
1139 sizeof(*reply
), &rlen
, (void *) &reply
);
1143 reply
->type
= sm
->proto
== WPA_PROTO_RSN
?
1144 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
1145 key_info
&= WPA_KEY_INFO_KEY_INDEX_MASK
;
1146 key_info
|= ver
| WPA_KEY_INFO_MIC
| WPA_KEY_INFO_SECURE
;
1147 WPA_PUT_BE16(reply
->key_info
, key_info
);
1148 if (sm
->proto
== WPA_PROTO_RSN
)
1149 WPA_PUT_BE16(reply
->key_length
, 0);
1151 os_memcpy(reply
->key_length
, key
->key_length
, 2);
1152 os_memcpy(reply
->replay_counter
, key
->replay_counter
,
1153 WPA_REPLAY_COUNTER_LEN
);
1155 WPA_PUT_BE16(reply
->key_data_length
, 0);
1157 wpa_printf(MSG_DEBUG
, "WPA: Sending EAPOL-Key 2/2");
1158 wpa_eapol_key_send(sm
, sm
->ptk
.kck
, ver
, sm
->bssid
, ETH_P_EAPOL
,
1159 rbuf
, rlen
, reply
->key_mic
);
1165 static void wpa_supplicant_process_1_of_2(struct wpa_sm
*sm
,
1166 const unsigned char *src_addr
,
1167 const struct wpa_eapol_key
*key
,
1168 int extra_len
, u16 ver
)
1170 u16 key_info
, keydatalen
;
1172 struct wpa_gtk_data gd
;
1174 os_memset(&gd
, 0, sizeof(gd
));
1176 rekey
= wpa_sm_get_state(sm
) == WPA_COMPLETED
;
1177 wpa_printf(MSG_DEBUG
, "WPA: RX message 1 of Group Key Handshake from "
1178 MACSTR
" (ver=%d)", MAC2STR(src_addr
), ver
);
1180 key_info
= WPA_GET_BE16(key
->key_info
);
1181 keydatalen
= WPA_GET_BE16(key
->key_data_length
);
1183 if (sm
->proto
== WPA_PROTO_RSN
) {
1184 ret
= wpa_supplicant_process_1_of_2_rsn(sm
,
1185 (const u8
*) (key
+ 1),
1186 keydatalen
, key_info
,
1189 ret
= wpa_supplicant_process_1_of_2_wpa(sm
, key
, keydatalen
,
1190 key_info
, extra_len
,
1194 wpa_sm_set_state(sm
, WPA_GROUP_HANDSHAKE
);
1199 if (wpa_supplicant_install_gtk(sm
, &gd
, key
->key_rsc
) ||
1200 wpa_supplicant_send_2_of_2(sm
, key
, ver
, key_info
))
1204 wpa_msg(sm
->ctx
->ctx
, MSG_INFO
, "WPA: Group rekeying "
1205 "completed with " MACSTR
" [GTK=%s]",
1206 MAC2STR(sm
->bssid
), wpa_cipher_txt(sm
->group_cipher
));
1207 wpa_sm_cancel_auth_timeout(sm
);
1208 wpa_sm_set_state(sm
, WPA_COMPLETED
);
1210 wpa_supplicant_key_neg_complete(sm
, sm
->bssid
,
1212 WPA_KEY_INFO_SECURE
);
1217 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm
*sm
,
1218 struct wpa_eapol_key
*key
,
1220 const u8
*buf
, size_t len
)
1225 os_memcpy(mic
, key
->key_mic
, 16);
1227 os_memset(key
->key_mic
, 0, 16);
1228 wpa_eapol_key_mic(sm
->tptk
.kck
, ver
, buf
, len
,
1230 if (os_memcmp(mic
, key
->key_mic
, 16) != 0) {
1231 wpa_printf(MSG_WARNING
, "WPA: Invalid EAPOL-Key MIC "
1232 "when using TPTK - ignoring TPTK");
1237 os_memcpy(&sm
->ptk
, &sm
->tptk
, sizeof(sm
->ptk
));
1241 if (!ok
&& sm
->ptk_set
) {
1242 os_memset(key
->key_mic
, 0, 16);
1243 wpa_eapol_key_mic(sm
->ptk
.kck
, ver
, buf
, len
,
1245 if (os_memcmp(mic
, key
->key_mic
, 16) != 0) {
1246 wpa_printf(MSG_WARNING
, "WPA: Invalid EAPOL-Key MIC "
1247 "- dropping packet");
1254 wpa_printf(MSG_WARNING
, "WPA: Could not verify EAPOL-Key MIC "
1255 "- dropping packet");
1259 os_memcpy(sm
->rx_replay_counter
, key
->replay_counter
,
1260 WPA_REPLAY_COUNTER_LEN
);
1261 sm
->rx_replay_counter_set
= 1;
1266 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1267 static int wpa_supplicant_decrypt_key_data(struct wpa_sm
*sm
,
1268 struct wpa_eapol_key
*key
, u16 ver
)
1270 u16 keydatalen
= WPA_GET_BE16(key
->key_data_length
);
1272 wpa_hexdump(MSG_DEBUG
, "RSN: encrypted key data",
1273 (u8
*) (key
+ 1), keydatalen
);
1275 wpa_printf(MSG_WARNING
, "WPA: PTK not available, "
1276 "cannot decrypt EAPOL-Key key data.");
1280 /* Decrypt key data here so that this operation does not need
1281 * to be implemented separately for each message type. */
1282 if (ver
== WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
) {
1284 os_memcpy(ek
, key
->key_iv
, 16);
1285 os_memcpy(ek
+ 16, sm
->ptk
.kek
, 16);
1286 rc4_skip(ek
, 32, 256, (u8
*) (key
+ 1), keydatalen
);
1287 } else if (ver
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
1288 ver
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
1290 if (keydatalen
% 8) {
1291 wpa_printf(MSG_WARNING
, "WPA: Unsupported "
1292 "AES-WRAP len %d", keydatalen
);
1295 keydatalen
-= 8; /* AES-WRAP adds 8 bytes */
1296 buf
= os_malloc(keydatalen
);
1298 wpa_printf(MSG_WARNING
, "WPA: No memory for "
1299 "AES-UNWRAP buffer");
1302 if (aes_unwrap(sm
->ptk
.kek
, keydatalen
/ 8,
1303 (u8
*) (key
+ 1), buf
)) {
1305 wpa_printf(MSG_WARNING
, "WPA: AES unwrap failed - "
1306 "could not decrypt EAPOL-Key key data");
1309 os_memcpy(key
+ 1, buf
, keydatalen
);
1311 WPA_PUT_BE16(key
->key_data_length
, keydatalen
);
1313 wpa_printf(MSG_WARNING
, "WPA: Unsupported key_info type %d",
1317 wpa_hexdump_key(MSG_DEBUG
, "WPA: decrypted EAPOL-Key key data",
1318 (u8
*) (key
+ 1), keydatalen
);
1324 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1325 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1327 void wpa_sm_aborted_cached(struct wpa_sm
*sm
)
1329 if (sm
&& sm
->cur_pmksa
) {
1330 wpa_printf(MSG_DEBUG
, "RSN: Cancelling PMKSA caching attempt");
1331 sm
->cur_pmksa
= NULL
;
1336 static void wpa_eapol_key_dump(const struct wpa_eapol_key
*key
)
1338 #ifndef CONFIG_NO_STDOUT_DEBUG
1339 u16 key_info
= WPA_GET_BE16(key
->key_info
);
1341 wpa_printf(MSG_DEBUG
, " EAPOL-Key type=%d", key
->type
);
1342 wpa_printf(MSG_DEBUG
, " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s"
1344 key_info
, key_info
& WPA_KEY_INFO_TYPE_MASK
,
1345 (key_info
& WPA_KEY_INFO_KEY_INDEX_MASK
) >>
1346 WPA_KEY_INFO_KEY_INDEX_SHIFT
,
1347 (key_info
& (BIT(13) | BIT(14) | BIT(15))) >> 13,
1348 key_info
& WPA_KEY_INFO_KEY_TYPE
? "Pairwise" : "Group",
1349 key_info
& WPA_KEY_INFO_INSTALL
? " Install" : "",
1350 key_info
& WPA_KEY_INFO_ACK
? " Ack" : "",
1351 key_info
& WPA_KEY_INFO_MIC
? " MIC" : "",
1352 key_info
& WPA_KEY_INFO_SECURE
? " Secure" : "",
1353 key_info
& WPA_KEY_INFO_ERROR
? " Error" : "",
1354 key_info
& WPA_KEY_INFO_REQUEST
? " Request" : "",
1355 key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
? " Encr" : "");
1356 wpa_printf(MSG_DEBUG
, " key_length=%u key_data_length=%u",
1357 WPA_GET_BE16(key
->key_length
),
1358 WPA_GET_BE16(key
->key_data_length
));
1359 wpa_hexdump(MSG_DEBUG
, " replay_counter",
1360 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
1361 wpa_hexdump(MSG_DEBUG
, " key_nonce", key
->key_nonce
, WPA_NONCE_LEN
);
1362 wpa_hexdump(MSG_DEBUG
, " key_iv", key
->key_iv
, 16);
1363 wpa_hexdump(MSG_DEBUG
, " key_rsc", key
->key_rsc
, 8);
1364 wpa_hexdump(MSG_DEBUG
, " key_id (reserved)", key
->key_id
, 8);
1365 wpa_hexdump(MSG_DEBUG
, " key_mic", key
->key_mic
, 16);
1366 #endif /* CONFIG_NO_STDOUT_DEBUG */
1371 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1372 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1373 * @src_addr: Source MAC address of the EAPOL packet
1374 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1375 * @len: Length of the EAPOL frame
1376 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1378 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1379 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1380 * only processing WPA and WPA2 EAPOL-Key frames.
1382 * The received EAPOL-Key packets are validated and valid packets are replied
1383 * to. In addition, key material (PTK, GTK) is configured at the end of a
1384 * successful key handshake.
1386 int wpa_sm_rx_eapol(struct wpa_sm
*sm
, const u8
*src_addr
,
1387 const u8
*buf
, size_t len
)
1389 size_t plen
, data_len
, extra_len
;
1390 struct ieee802_1x_hdr
*hdr
;
1391 struct wpa_eapol_key
*key
;
1395 struct wpa_peerkey
*peerkey
= NULL
;
1397 #ifdef CONFIG_IEEE80211R
1398 sm
->ft_completed
= 0;
1399 #endif /* CONFIG_IEEE80211R */
1401 if (len
< sizeof(*hdr
) + sizeof(*key
)) {
1402 wpa_printf(MSG_DEBUG
, "WPA: EAPOL frame too short to be a WPA "
1403 "EAPOL-Key (len %lu, expecting at least %lu)",
1404 (unsigned long) len
,
1405 (unsigned long) sizeof(*hdr
) + sizeof(*key
));
1409 tmp
= os_malloc(len
);
1412 os_memcpy(tmp
, buf
, len
);
1414 hdr
= (struct ieee802_1x_hdr
*) tmp
;
1415 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1416 plen
= be_to_host16(hdr
->length
);
1417 data_len
= plen
+ sizeof(*hdr
);
1418 wpa_printf(MSG_DEBUG
, "IEEE 802.1X RX: version=%d type=%d length=%lu",
1419 hdr
->version
, hdr
->type
, (unsigned long) plen
);
1421 if (hdr
->version
< EAPOL_VERSION
) {
1422 /* TODO: backwards compatibility */
1424 if (hdr
->type
!= IEEE802_1X_TYPE_EAPOL_KEY
) {
1425 wpa_printf(MSG_DEBUG
, "WPA: EAPOL frame (type %u) discarded, "
1426 "not a Key frame", hdr
->type
);
1430 if (plen
> len
- sizeof(*hdr
) || plen
< sizeof(*key
)) {
1431 wpa_printf(MSG_DEBUG
, "WPA: EAPOL frame payload size %lu "
1432 "invalid (frame size %lu)",
1433 (unsigned long) plen
, (unsigned long) len
);
1438 if (key
->type
!= EAPOL_KEY_TYPE_WPA
&& key
->type
!= EAPOL_KEY_TYPE_RSN
)
1440 wpa_printf(MSG_DEBUG
, "WPA: EAPOL-Key type (%d) unknown, "
1441 "discarded", key
->type
);
1445 wpa_eapol_key_dump(key
);
1447 eapol_sm_notify_lower_layer_success(sm
->eapol
, 0);
1448 wpa_hexdump(MSG_MSGDUMP
, "WPA: RX EAPOL-Key", tmp
, len
);
1449 if (data_len
< len
) {
1450 wpa_printf(MSG_DEBUG
, "WPA: ignoring %lu bytes after the IEEE "
1451 "802.1X data", (unsigned long) len
- data_len
);
1453 key_info
= WPA_GET_BE16(key
->key_info
);
1454 ver
= key_info
& WPA_KEY_INFO_TYPE_MASK
;
1455 if (ver
!= WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
&&
1456 #ifdef CONFIG_IEEE80211R
1457 ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
&&
1458 #endif /* CONFIG_IEEE80211R */
1459 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
1460 wpa_printf(MSG_INFO
, "WPA: Unsupported EAPOL-Key descriptor "
1461 "version %d.", ver
);
1465 #ifdef CONFIG_IEEE80211R
1466 if (sm
->key_mgmt
== WPA_KEY_MGMT_FT_IEEE8021X
||
1467 sm
->key_mgmt
== WPA_KEY_MGMT_FT_PSK
) {
1468 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
1469 if (ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
1470 wpa_printf(MSG_INFO
, "FT: AP did not use "
1475 #endif /* CONFIG_IEEE80211R */
1476 if (sm
->pairwise_cipher
== WPA_CIPHER_CCMP
&&
1477 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
1478 wpa_printf(MSG_INFO
, "WPA: CCMP is used, but EAPOL-Key "
1479 "descriptor version (%d) is not 2.", ver
);
1480 if (sm
->group_cipher
!= WPA_CIPHER_CCMP
&&
1481 !(key_info
& WPA_KEY_INFO_KEY_TYPE
)) {
1482 /* Earlier versions of IEEE 802.11i did not explicitly
1483 * require version 2 descriptor for all EAPOL-Key
1484 * packets, so allow group keys to use version 1 if
1485 * CCMP is not used for them. */
1486 wpa_printf(MSG_INFO
, "WPA: Backwards compatibility: "
1487 "allow invalid version for non-CCMP group "
1493 #ifdef CONFIG_PEERKEY
1494 for (peerkey
= sm
->peerkey
; peerkey
; peerkey
= peerkey
->next
) {
1495 if (os_memcmp(peerkey
->addr
, src_addr
, ETH_ALEN
) == 0)
1499 if (!(key_info
& WPA_KEY_INFO_SMK_MESSAGE
) && peerkey
) {
1500 if (!peerkey
->initiator
&& peerkey
->replay_counter_set
&&
1501 os_memcmp(key
->replay_counter
, peerkey
->replay_counter
,
1502 WPA_REPLAY_COUNTER_LEN
) <= 0) {
1503 wpa_printf(MSG_WARNING
, "RSN: EAPOL-Key Replay "
1504 "Counter did not increase (STK) - dropping "
1507 } else if (peerkey
->initiator
) {
1508 u8 _tmp
[WPA_REPLAY_COUNTER_LEN
];
1509 os_memcpy(_tmp
, key
->replay_counter
,
1510 WPA_REPLAY_COUNTER_LEN
);
1511 inc_byte_array(_tmp
, WPA_REPLAY_COUNTER_LEN
);
1512 if (os_memcmp(_tmp
, peerkey
->replay_counter
,
1513 WPA_REPLAY_COUNTER_LEN
) != 0) {
1514 wpa_printf(MSG_DEBUG
, "RSN: EAPOL-Key Replay "
1515 "Counter did not match (STK) - "
1522 if (peerkey
&& peerkey
->initiator
&& (key_info
& WPA_KEY_INFO_ACK
)) {
1523 wpa_printf(MSG_INFO
, "RSN: Ack bit in key_info from STK peer");
1526 #endif /* CONFIG_PEERKEY */
1528 if (!peerkey
&& sm
->rx_replay_counter_set
&&
1529 os_memcmp(key
->replay_counter
, sm
->rx_replay_counter
,
1530 WPA_REPLAY_COUNTER_LEN
) <= 0) {
1531 wpa_printf(MSG_WARNING
, "WPA: EAPOL-Key Replay Counter did not"
1532 " increase - dropping packet");
1536 if (!(key_info
& (WPA_KEY_INFO_ACK
| WPA_KEY_INFO_SMK_MESSAGE
))
1537 #ifdef CONFIG_PEERKEY
1538 && (peerkey
== NULL
|| !peerkey
->initiator
)
1539 #endif /* CONFIG_PEERKEY */
1541 wpa_printf(MSG_INFO
, "WPA: No Ack bit in key_info");
1545 if (key_info
& WPA_KEY_INFO_REQUEST
) {
1546 wpa_printf(MSG_INFO
, "WPA: EAPOL-Key with Request bit - "
1551 if ((key_info
& WPA_KEY_INFO_MIC
) && !peerkey
&&
1552 wpa_supplicant_verify_eapol_key_mic(sm
, key
, ver
, tmp
, data_len
))
1555 #ifdef CONFIG_PEERKEY
1556 if ((key_info
& WPA_KEY_INFO_MIC
) && peerkey
&&
1557 peerkey_verify_eapol_key_mic(sm
, peerkey
, key
, ver
, tmp
, data_len
))
1559 #endif /* CONFIG_PEERKEY */
1561 extra_len
= data_len
- sizeof(*hdr
) - sizeof(*key
);
1563 if (WPA_GET_BE16(key
->key_data_length
) > extra_len
) {
1564 wpa_msg(sm
->ctx
->ctx
, MSG_INFO
, "WPA: Invalid EAPOL-Key "
1565 "frame - key_data overflow (%d > %lu)",
1566 WPA_GET_BE16(key
->key_data_length
),
1567 (unsigned long) extra_len
);
1570 extra_len
= WPA_GET_BE16(key
->key_data_length
);
1572 if (sm
->proto
== WPA_PROTO_RSN
&&
1573 (key_info
& WPA_KEY_INFO_ENCR_KEY_DATA
)) {
1574 if (wpa_supplicant_decrypt_key_data(sm
, key
, ver
))
1576 extra_len
= WPA_GET_BE16(key
->key_data_length
);
1579 if (key_info
& WPA_KEY_INFO_KEY_TYPE
) {
1580 if (key_info
& WPA_KEY_INFO_KEY_INDEX_MASK
) {
1581 wpa_printf(MSG_WARNING
, "WPA: Ignored EAPOL-Key "
1582 "(Pairwise) with non-zero key index");
1586 /* PeerKey 4-Way Handshake */
1587 peerkey_rx_eapol_4way(sm
, peerkey
, key
, key_info
, ver
);
1588 } else if (key_info
& WPA_KEY_INFO_MIC
) {
1589 /* 3/4 4-Way Handshake */
1590 wpa_supplicant_process_3_of_4(sm
, key
, ver
);
1592 /* 1/4 4-Way Handshake */
1593 wpa_supplicant_process_1_of_4(sm
, src_addr
, key
,
1596 } else if (key_info
& WPA_KEY_INFO_SMK_MESSAGE
) {
1597 /* PeerKey SMK Handshake */
1598 peerkey_rx_eapol_smk(sm
, src_addr
, key
, extra_len
, key_info
,
1601 if (key_info
& WPA_KEY_INFO_MIC
) {
1602 /* 1/2 Group Key Handshake */
1603 wpa_supplicant_process_1_of_2(sm
, src_addr
, key
,
1606 wpa_printf(MSG_WARNING
, "WPA: EAPOL-Key (Group) "
1607 "without Mic bit - dropped");
1619 #ifdef CONFIG_CTRL_IFACE
1620 static int wpa_cipher_bits(int cipher
)
1623 case WPA_CIPHER_CCMP
:
1625 case WPA_CIPHER_TKIP
:
1627 case WPA_CIPHER_WEP104
:
1629 case WPA_CIPHER_WEP40
:
1637 static u32
wpa_key_mgmt_suite(struct wpa_sm
*sm
)
1639 switch (sm
->key_mgmt
) {
1640 case WPA_KEY_MGMT_IEEE8021X
:
1641 return (sm
->proto
== WPA_PROTO_RSN
?
1642 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X
:
1643 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X
);
1644 case WPA_KEY_MGMT_PSK
:
1645 return (sm
->proto
== WPA_PROTO_RSN
?
1646 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X
:
1647 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X
);
1648 #ifdef CONFIG_IEEE80211R
1649 case WPA_KEY_MGMT_FT_IEEE8021X
:
1650 return RSN_AUTH_KEY_MGMT_FT_802_1X
;
1651 case WPA_KEY_MGMT_FT_PSK
:
1652 return RSN_AUTH_KEY_MGMT_FT_PSK
;
1653 #endif /* CONFIG_IEEE80211R */
1654 case WPA_KEY_MGMT_WPA_NONE
:
1655 return WPA_AUTH_KEY_MGMT_NONE
;
1662 static u32
wpa_cipher_suite(struct wpa_sm
*sm
, int cipher
)
1665 case WPA_CIPHER_CCMP
:
1666 return (sm
->proto
== WPA_PROTO_RSN
?
1667 RSN_CIPHER_SUITE_CCMP
: WPA_CIPHER_SUITE_CCMP
);
1668 case WPA_CIPHER_TKIP
:
1669 return (sm
->proto
== WPA_PROTO_RSN
?
1670 RSN_CIPHER_SUITE_TKIP
: WPA_CIPHER_SUITE_TKIP
);
1671 case WPA_CIPHER_WEP104
:
1672 return (sm
->proto
== WPA_PROTO_RSN
?
1673 RSN_CIPHER_SUITE_WEP104
: WPA_CIPHER_SUITE_WEP104
);
1674 case WPA_CIPHER_WEP40
:
1675 return (sm
->proto
== WPA_PROTO_RSN
?
1676 RSN_CIPHER_SUITE_WEP40
: WPA_CIPHER_SUITE_WEP40
);
1677 case WPA_CIPHER_NONE
:
1678 return (sm
->proto
== WPA_PROTO_RSN
?
1679 RSN_CIPHER_SUITE_NONE
: WPA_CIPHER_SUITE_NONE
);
1686 #define RSN_SUITE "%02x-%02x-%02x-%d"
1687 #define RSN_SUITE_ARG(s) \
1688 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
1691 * wpa_sm_get_mib - Dump text list of MIB entries
1692 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1693 * @buf: Buffer for the list
1694 * @buflen: Length of the buffer
1695 * Returns: Number of bytes written to buffer
1697 * This function is used fetch dot11 MIB variables.
1699 int wpa_sm_get_mib(struct wpa_sm
*sm
, char *buf
, size_t buflen
)
1701 char pmkid_txt
[PMKID_LEN
* 2 + 1];
1705 if (sm
->cur_pmksa
) {
1706 wpa_snprintf_hex(pmkid_txt
, sizeof(pmkid_txt
),
1707 sm
->cur_pmksa
->pmkid
, PMKID_LEN
);
1709 pmkid_txt
[0] = '\0';
1711 if ((sm
->key_mgmt
== WPA_KEY_MGMT_PSK
||
1712 sm
->key_mgmt
== WPA_KEY_MGMT_IEEE8021X
||
1713 sm
->key_mgmt
== WPA_KEY_MGMT_FT_PSK
||
1714 sm
->key_mgmt
== WPA_KEY_MGMT_FT_IEEE8021X
) &&
1715 sm
->proto
== WPA_PROTO_RSN
)
1720 ret
= os_snprintf(buf
, buflen
,
1721 "dot11RSNAOptionImplemented=TRUE\n"
1722 "dot11RSNAPreauthenticationImplemented=TRUE\n"
1723 "dot11RSNAEnabled=%s\n"
1724 "dot11RSNAPreauthenticationEnabled=%s\n"
1725 "dot11RSNAConfigVersion=%d\n"
1726 "dot11RSNAConfigPairwiseKeysSupported=5\n"
1727 "dot11RSNAConfigGroupCipherSize=%d\n"
1728 "dot11RSNAConfigPMKLifetime=%d\n"
1729 "dot11RSNAConfigPMKReauthThreshold=%d\n"
1730 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
1731 "dot11RSNAConfigSATimeout=%d\n",
1732 rsna
? "TRUE" : "FALSE",
1733 rsna
? "TRUE" : "FALSE",
1735 wpa_cipher_bits(sm
->group_cipher
),
1736 sm
->dot11RSNAConfigPMKLifetime
,
1737 sm
->dot11RSNAConfigPMKReauthThreshold
,
1738 sm
->dot11RSNAConfigSATimeout
);
1739 if (ret
< 0 || (size_t) ret
>= buflen
)
1744 buf
+ len
, buflen
- len
,
1745 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE
"\n"
1746 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE
"\n"
1747 "dot11RSNAGroupCipherSelected=" RSN_SUITE
"\n"
1748 "dot11RSNAPMKIDUsed=%s\n"
1749 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE
"\n"
1750 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE
"\n"
1751 "dot11RSNAGroupCipherRequested=" RSN_SUITE
"\n"
1752 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
1753 "dot11RSNA4WayHandshakeFailures=%u\n",
1754 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm
)),
1755 RSN_SUITE_ARG(wpa_cipher_suite(sm
, sm
->pairwise_cipher
)),
1756 RSN_SUITE_ARG(wpa_cipher_suite(sm
, sm
->group_cipher
)),
1758 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm
)),
1759 RSN_SUITE_ARG(wpa_cipher_suite(sm
, sm
->pairwise_cipher
)),
1760 RSN_SUITE_ARG(wpa_cipher_suite(sm
, sm
->group_cipher
)),
1761 sm
->dot11RSNA4WayHandshakeFailures
);
1762 if (ret
>= 0 && (size_t) ret
< buflen
)
1767 #endif /* CONFIG_CTRL_IFACE */
1770 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry
*entry
,
1771 void *ctx
, int replace
)
1773 struct wpa_sm
*sm
= ctx
;
1775 if (sm
->cur_pmksa
== entry
||
1776 (sm
->pmk_len
== entry
->pmk_len
&&
1777 os_memcmp(sm
->pmk
, entry
->pmk
, sm
->pmk_len
) == 0)) {
1778 wpa_printf(MSG_DEBUG
, "RSN: removed current PMKSA entry");
1779 sm
->cur_pmksa
= NULL
;
1782 /* A new entry is being added, so no need to
1783 * deauthenticate in this case. This happens when EAP
1784 * authentication is completed again (reauth or failed
1785 * PMKSA caching attempt). */
1789 os_memset(sm
->pmk
, 0, sizeof(sm
->pmk
));
1790 wpa_sm_deauthenticate(sm
, WLAN_REASON_UNSPECIFIED
);
1796 * wpa_sm_init - Initialize WPA state machine
1797 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
1798 * Returns: Pointer to the allocated WPA state machine data
1800 * This function is used to allocate a new WPA state machine and the returned
1801 * value is passed to all WPA state machine calls.
1803 struct wpa_sm
* wpa_sm_init(struct wpa_sm_ctx
*ctx
)
1807 sm
= os_zalloc(sizeof(*sm
));
1810 sm
->renew_snonce
= 1;
1813 sm
->dot11RSNAConfigPMKLifetime
= 43200;
1814 sm
->dot11RSNAConfigPMKReauthThreshold
= 70;
1815 sm
->dot11RSNAConfigSATimeout
= 60;
1817 sm
->pmksa
= pmksa_cache_init(wpa_sm_pmksa_free_cb
, sm
, sm
);
1818 if (sm
->pmksa
== NULL
) {
1819 wpa_printf(MSG_ERROR
, "RSN: PMKSA cache initialization "
1830 * wpa_sm_deinit - Deinitialize WPA state machine
1831 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1833 void wpa_sm_deinit(struct wpa_sm
*sm
)
1837 pmksa_cache_deinit(sm
->pmksa
);
1838 eloop_cancel_timeout(wpa_sm_start_preauth
, sm
, NULL
);
1839 os_free(sm
->assoc_wpa_ie
);
1840 os_free(sm
->ap_wpa_ie
);
1841 os_free(sm
->ap_rsn_ie
);
1849 * wpa_sm_notify_assoc - Notify WPA state machine about association
1850 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1851 * @bssid: The BSSID of the new association
1853 * This function is called to let WPA state machine know that the connection
1856 void wpa_sm_notify_assoc(struct wpa_sm
*sm
, const u8
*bssid
)
1863 wpa_printf(MSG_DEBUG
, "WPA: Association event - clear replay counter");
1864 os_memcpy(sm
->bssid
, bssid
, ETH_ALEN
);
1865 os_memset(sm
->rx_replay_counter
, 0, WPA_REPLAY_COUNTER_LEN
);
1866 sm
->rx_replay_counter_set
= 0;
1867 sm
->renew_snonce
= 1;
1868 if (os_memcmp(sm
->preauth_bssid
, bssid
, ETH_ALEN
) == 0)
1869 rsn_preauth_deinit(sm
);
1871 #ifdef CONFIG_IEEE80211R
1872 if (wpa_ft_is_completed(sm
)) {
1873 wpa_supplicant_key_neg_complete(sm
, sm
->bssid
, 1);
1875 /* Prepare for the next transition */
1876 wpa_ft_prepare_auth_request(sm
);
1880 #endif /* CONFIG_IEEE80211R */
1884 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
1885 * this is not part of a Fast BSS Transition.
1887 wpa_printf(MSG_DEBUG
, "WPA: Clear old PTK");
1895 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
1896 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1898 * This function is called to let WPA state machine know that the connection
1899 * was lost. This will abort any existing pre-authentication session.
1901 void wpa_sm_notify_disassoc(struct wpa_sm
*sm
)
1903 rsn_preauth_deinit(sm
);
1904 if (wpa_sm_get_state(sm
) == WPA_4WAY_HANDSHAKE
)
1905 sm
->dot11RSNA4WayHandshakeFailures
++;
1910 * wpa_sm_set_pmk - Set PMK
1911 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1913 * @pmk_len: The length of the new PMK in bytes
1915 * Configure the PMK for WPA state machine.
1917 void wpa_sm_set_pmk(struct wpa_sm
*sm
, const u8
*pmk
, size_t pmk_len
)
1922 sm
->pmk_len
= pmk_len
;
1923 os_memcpy(sm
->pmk
, pmk
, pmk_len
);
1925 #ifdef CONFIG_IEEE80211R
1926 /* Set XXKey to be PSK for FT key derivation */
1927 sm
->xxkey_len
= pmk_len
;
1928 os_memcpy(sm
->xxkey
, pmk
, pmk_len
);
1929 #endif /* CONFIG_IEEE80211R */
1934 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
1935 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1937 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
1940 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm
*sm
)
1945 if (sm
->cur_pmksa
) {
1946 sm
->pmk_len
= sm
->cur_pmksa
->pmk_len
;
1947 os_memcpy(sm
->pmk
, sm
->cur_pmksa
->pmk
, sm
->pmk_len
);
1949 sm
->pmk_len
= PMK_LEN
;
1950 os_memset(sm
->pmk
, 0, PMK_LEN
);
1956 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
1957 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1958 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
1960 void wpa_sm_set_fast_reauth(struct wpa_sm
*sm
, int fast_reauth
)
1963 sm
->fast_reauth
= fast_reauth
;
1968 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
1969 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1970 * @scard_ctx: Context pointer for smartcard related callback functions
1972 void wpa_sm_set_scard_ctx(struct wpa_sm
*sm
, void *scard_ctx
)
1976 sm
->scard_ctx
= scard_ctx
;
1977 if (sm
->preauth_eapol
)
1978 eapol_sm_register_scard_ctx(sm
->preauth_eapol
, scard_ctx
);
1983 * wpa_sm_set_config - Notification of current configration change
1984 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1985 * @config: Pointer to current network configuration
1987 * Notify WPA state machine that configuration has changed. config will be
1988 * stored as a backpointer to network configuration. This can be %NULL to clear
1989 * the stored pointed.
1991 void wpa_sm_set_config(struct wpa_sm
*sm
, struct rsn_supp_config
*config
)
1997 sm
->network_ctx
= config
->network_ctx
;
1998 sm
->peerkey_enabled
= config
->peerkey_enabled
;
1999 sm
->allowed_pairwise_cipher
= config
->allowed_pairwise_cipher
;
2000 sm
->proactive_key_caching
= config
->proactive_key_caching
;
2001 sm
->eap_workaround
= config
->eap_workaround
;
2002 sm
->eap_conf_ctx
= config
->eap_conf_ctx
;
2004 os_memcpy(sm
->ssid
, config
->ssid
, config
->ssid_len
);
2005 sm
->ssid_len
= config
->ssid_len
;
2009 sm
->network_ctx
= NULL
;
2010 sm
->peerkey_enabled
= 0;
2011 sm
->allowed_pairwise_cipher
= 0;
2012 sm
->proactive_key_caching
= 0;
2013 sm
->eap_workaround
= 0;
2014 sm
->eap_conf_ctx
= NULL
;
2017 if (config
== NULL
|| config
->network_ctx
!= sm
->network_ctx
)
2018 pmksa_cache_notify_reconfig(sm
->pmksa
);
2023 * wpa_sm_set_own_addr - Set own MAC address
2024 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2025 * @addr: Own MAC address
2027 void wpa_sm_set_own_addr(struct wpa_sm
*sm
, const u8
*addr
)
2030 os_memcpy(sm
->own_addr
, addr
, ETH_ALEN
);
2035 * wpa_sm_set_ifname - Set network interface name
2036 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2037 * @ifname: Interface name
2038 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2040 void wpa_sm_set_ifname(struct wpa_sm
*sm
, const char *ifname
,
2041 const char *bridge_ifname
)
2044 sm
->ifname
= ifname
;
2045 sm
->bridge_ifname
= bridge_ifname
;
2051 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2052 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2053 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2055 void wpa_sm_set_eapol(struct wpa_sm
*sm
, struct eapol_sm
*eapol
)
2063 * wpa_sm_set_param - Set WPA state machine parameters
2064 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2065 * @param: Parameter field
2066 * @value: Parameter value
2067 * Returns: 0 on success, -1 on failure
2069 int wpa_sm_set_param(struct wpa_sm
*sm
, enum wpa_sm_conf_params param
,
2078 case RSNA_PMK_LIFETIME
:
2080 sm
->dot11RSNAConfigPMKLifetime
= value
;
2084 case RSNA_PMK_REAUTH_THRESHOLD
:
2085 if (value
> 0 && value
<= 100)
2086 sm
->dot11RSNAConfigPMKReauthThreshold
= value
;
2090 case RSNA_SA_TIMEOUT
:
2092 sm
->dot11RSNAConfigSATimeout
= value
;
2096 case WPA_PARAM_PROTO
:
2099 case WPA_PARAM_PAIRWISE
:
2100 sm
->pairwise_cipher
= value
;
2102 case WPA_PARAM_GROUP
:
2103 sm
->group_cipher
= value
;
2105 case WPA_PARAM_KEY_MGMT
:
2106 sm
->key_mgmt
= value
;
2108 #ifdef CONFIG_IEEE80211W
2109 case WPA_PARAM_MGMT_GROUP
:
2110 sm
->mgmt_group_cipher
= value
;
2112 #endif /* CONFIG_IEEE80211W */
2113 case WPA_PARAM_RSN_ENABLED
:
2114 sm
->rsn_enabled
= value
;
2125 * wpa_sm_get_param - Get WPA state machine parameters
2126 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2127 * @param: Parameter field
2128 * Returns: Parameter value
2130 unsigned int wpa_sm_get_param(struct wpa_sm
*sm
, enum wpa_sm_conf_params param
)
2136 case RSNA_PMK_LIFETIME
:
2137 return sm
->dot11RSNAConfigPMKLifetime
;
2138 case RSNA_PMK_REAUTH_THRESHOLD
:
2139 return sm
->dot11RSNAConfigPMKReauthThreshold
;
2140 case RSNA_SA_TIMEOUT
:
2141 return sm
->dot11RSNAConfigSATimeout
;
2142 case WPA_PARAM_PROTO
:
2144 case WPA_PARAM_PAIRWISE
:
2145 return sm
->pairwise_cipher
;
2146 case WPA_PARAM_GROUP
:
2147 return sm
->group_cipher
;
2148 case WPA_PARAM_KEY_MGMT
:
2149 return sm
->key_mgmt
;
2150 #ifdef CONFIG_IEEE80211W
2151 case WPA_PARAM_MGMT_GROUP
:
2152 return sm
->mgmt_group_cipher
;
2153 #endif /* CONFIG_IEEE80211W */
2154 case WPA_PARAM_RSN_ENABLED
:
2155 return sm
->rsn_enabled
;
2163 * wpa_sm_get_status - Get WPA state machine
2164 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2165 * @buf: Buffer for status information
2166 * @buflen: Maximum buffer length
2167 * @verbose: Whether to include verbose status information
2168 * Returns: Number of bytes written to buf.
2170 * Query WPA state machine for status information. This function fills in
2171 * a text area with current status information. If the buffer (buf) is not
2172 * large enough, status information will be truncated to fit the buffer.
2174 int wpa_sm_get_status(struct wpa_sm
*sm
, char *buf
, size_t buflen
,
2177 char *pos
= buf
, *end
= buf
+ buflen
;
2180 ret
= os_snprintf(pos
, end
- pos
,
2181 "pairwise_cipher=%s\n"
2184 wpa_cipher_txt(sm
->pairwise_cipher
),
2185 wpa_cipher_txt(sm
->group_cipher
),
2186 wpa_key_mgmt_txt(sm
->key_mgmt
, sm
->proto
));
2187 if (ret
< 0 || ret
>= end
- pos
)
2195 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2196 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2197 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2198 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2199 * Returns: 0 on success, -1 on failure
2201 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm
*sm
, u8
*wpa_ie
,
2209 res
= wpa_gen_wpa_ie(sm
, wpa_ie
, *wpa_ie_len
);
2214 wpa_hexdump(MSG_DEBUG
, "WPA: Set own WPA IE default",
2215 wpa_ie
, *wpa_ie_len
);
2217 if (sm
->assoc_wpa_ie
== NULL
) {
2219 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2220 * the correct version of the IE even if PMKSA caching is
2221 * aborted (which would remove PMKID from IE generation).
2223 sm
->assoc_wpa_ie
= os_malloc(*wpa_ie_len
);
2224 if (sm
->assoc_wpa_ie
== NULL
)
2227 os_memcpy(sm
->assoc_wpa_ie
, wpa_ie
, *wpa_ie_len
);
2228 sm
->assoc_wpa_ie_len
= *wpa_ie_len
;
2236 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2237 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2238 * @ie: Pointer to IE data (starting from id)
2240 * Returns: 0 on success, -1 on failure
2242 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2243 * Request frame. The IE will be used to override the default value generated
2244 * with wpa_sm_set_assoc_wpa_ie_default().
2246 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm
*sm
, const u8
*ie
, size_t len
)
2251 os_free(sm
->assoc_wpa_ie
);
2252 if (ie
== NULL
|| len
== 0) {
2253 wpa_printf(MSG_DEBUG
, "WPA: clearing own WPA/RSN IE");
2254 sm
->assoc_wpa_ie
= NULL
;
2255 sm
->assoc_wpa_ie_len
= 0;
2257 wpa_hexdump(MSG_DEBUG
, "WPA: set own WPA/RSN IE", ie
, len
);
2258 sm
->assoc_wpa_ie
= os_malloc(len
);
2259 if (sm
->assoc_wpa_ie
== NULL
)
2262 os_memcpy(sm
->assoc_wpa_ie
, ie
, len
);
2263 sm
->assoc_wpa_ie_len
= len
;
2271 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2272 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2273 * @ie: Pointer to IE data (starting from id)
2275 * Returns: 0 on success, -1 on failure
2277 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2280 int wpa_sm_set_ap_wpa_ie(struct wpa_sm
*sm
, const u8
*ie
, size_t len
)
2285 os_free(sm
->ap_wpa_ie
);
2286 if (ie
== NULL
|| len
== 0) {
2287 wpa_printf(MSG_DEBUG
, "WPA: clearing AP WPA IE");
2288 sm
->ap_wpa_ie
= NULL
;
2289 sm
->ap_wpa_ie_len
= 0;
2291 wpa_hexdump(MSG_DEBUG
, "WPA: set AP WPA IE", ie
, len
);
2292 sm
->ap_wpa_ie
= os_malloc(len
);
2293 if (sm
->ap_wpa_ie
== NULL
)
2296 os_memcpy(sm
->ap_wpa_ie
, ie
, len
);
2297 sm
->ap_wpa_ie_len
= len
;
2305 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2306 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2307 * @ie: Pointer to IE data (starting from id)
2309 * Returns: 0 on success, -1 on failure
2311 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2314 int wpa_sm_set_ap_rsn_ie(struct wpa_sm
*sm
, const u8
*ie
, size_t len
)
2319 os_free(sm
->ap_rsn_ie
);
2320 if (ie
== NULL
|| len
== 0) {
2321 wpa_printf(MSG_DEBUG
, "WPA: clearing AP RSN IE");
2322 sm
->ap_rsn_ie
= NULL
;
2323 sm
->ap_rsn_ie_len
= 0;
2325 wpa_hexdump(MSG_DEBUG
, "WPA: set AP RSN IE", ie
, len
);
2326 sm
->ap_rsn_ie
= os_malloc(len
);
2327 if (sm
->ap_rsn_ie
== NULL
)
2330 os_memcpy(sm
->ap_rsn_ie
, ie
, len
);
2331 sm
->ap_rsn_ie_len
= len
;
2339 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2340 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2341 * @data: Pointer to data area for parsing results
2342 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2344 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2345 * parsed data into data.
2347 int wpa_sm_parse_own_wpa_ie(struct wpa_sm
*sm
, struct wpa_ie_data
*data
)
2349 if (sm
== NULL
|| sm
->assoc_wpa_ie
== NULL
) {
2350 wpa_printf(MSG_DEBUG
, "WPA: No WPA/RSN IE available from "
2351 "association info");
2354 if (wpa_parse_wpa_ie(sm
->assoc_wpa_ie
, sm
->assoc_wpa_ie_len
, data
))