2 * WPA Supplicant - WPA state machine and EAPOL-Key processing
3 * Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
18 #include "crypto/aes_wrap.h"
19 #include "crypto/crypto.h"
20 #include "common/ieee802_11_defs.h"
21 #include "eapol_supp/eapol_supp_sm.h"
25 #include "pmksa_cache.h"
32 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
33 * @sm: Pointer to WPA state machine data from wpa_sm_init()
34 * @kck: Key Confirmation Key (KCK, part of PTK)
35 * @ver: Version field from Key Info
36 * @dest: Destination address for the frame
37 * @proto: Ethertype (usually ETH_P_EAPOL)
38 * @msg: EAPOL-Key message
39 * @msg_len: Length of message
40 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
42 void wpa_eapol_key_send(struct wpa_sm *sm, const u8 *kck,
43 int ver, const u8 *dest, u16 proto,
44 u8 *msg, size_t msg_len, u8 *key_mic)
46 if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
48 * Association event was not yet received; try to fetch
49 * BSSID from the driver.
51 if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
52 wpa_printf(MSG_DEBUG, "WPA: Failed to read BSSID for "
53 "EAPOL-Key destination address");
56 wpa_printf(MSG_DEBUG, "WPA: Use BSSID (" MACSTR
57 ") as the destination for EAPOL-Key",
62 wpa_eapol_key_mic(kck, ver, msg, msg_len, key_mic)) {
63 wpa_printf(MSG_ERROR, "WPA: Failed to generate EAPOL-Key "
64 "version %d MIC", ver);
67 wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
68 wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
69 eapol_sm_notify_tx_eapol_key(sm->eapol);
76 * wpa_sm_key_request - Send EAPOL-Key Request
77 * @sm: Pointer to WPA state machine data from wpa_sm_init()
78 * @error: Indicate whether this is an Michael MIC error report
79 * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
81 * Send an EAPOL-Key Request to the current authenticator. This function is
82 * used to request rekeying and it is usually called when a local Michael MIC
83 * failure is detected.
85 void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
88 struct wpa_eapol_key *reply;
90 u8 bssid[ETH_ALEN], *rbuf;
92 if (wpa_key_mgmt_ft(sm->key_mgmt) || wpa_key_mgmt_sha256(sm->key_mgmt))
93 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
94 else if (sm->pairwise_cipher == WPA_CIPHER_CCMP)
95 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
97 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
99 if (wpa_sm_get_bssid(sm, bssid) < 0) {
100 wpa_printf(MSG_WARNING, "Failed to read BSSID for EAPOL-Key "
105 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
106 sizeof(*reply), &rlen, (void *) &reply);
110 reply->type = sm->proto == WPA_PROTO_RSN ?
111 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
112 key_info = WPA_KEY_INFO_REQUEST | ver;
114 key_info |= WPA_KEY_INFO_MIC;
116 key_info |= WPA_KEY_INFO_ERROR;
118 key_info |= WPA_KEY_INFO_KEY_TYPE;
119 WPA_PUT_BE16(reply->key_info, key_info);
120 WPA_PUT_BE16(reply->key_length, 0);
121 os_memcpy(reply->replay_counter, sm->request_counter,
122 WPA_REPLAY_COUNTER_LEN);
123 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
125 WPA_PUT_BE16(reply->key_data_length, 0);
127 wpa_printf(MSG_INFO, "WPA: Sending EAPOL-Key Request (error=%d "
128 "pairwise=%d ptk_set=%d len=%lu)",
129 error, pairwise, sm->ptk_set, (unsigned long) rlen);
130 wpa_eapol_key_send(sm, sm->ptk.kck, ver, bssid, ETH_P_EAPOL,
131 rbuf, rlen, key_info & WPA_KEY_INFO_MIC ?
132 reply->key_mic : NULL);
136 static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
137 const unsigned char *src_addr,
140 int abort_cached = 0;
142 if (pmkid && !sm->cur_pmksa) {
143 /* When using drivers that generate RSN IE, wpa_supplicant may
144 * not have enough time to get the association information
145 * event before receiving this 1/4 message, so try to find a
146 * matching PMKSA cache entry here. */
147 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid);
149 wpa_printf(MSG_DEBUG, "RSN: found matching PMKID from "
152 wpa_printf(MSG_DEBUG, "RSN: no matching PMKID found");
157 if (pmkid && sm->cur_pmksa &&
158 os_memcmp(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
159 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
160 wpa_sm_set_pmk_from_pmksa(sm);
161 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
162 sm->pmk, sm->pmk_len);
163 eapol_sm_notify_cached(sm->eapol);
164 #ifdef CONFIG_IEEE80211R
166 #endif /* CONFIG_IEEE80211R */
167 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
170 res = eapol_sm_get_key(sm->eapol, sm->pmk, PMK_LEN);
173 * EAP-LEAP is an exception from other EAP methods: it
174 * uses only 16-byte PMK.
176 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
179 #ifdef CONFIG_IEEE80211R
181 if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
183 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
184 sm->xxkey_len = PMK_LEN;
185 os_memset(buf, 0, sizeof(buf));
187 #endif /* CONFIG_IEEE80211R */
190 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
191 "machines", sm->pmk, pmk_len);
192 sm->pmk_len = pmk_len;
193 if (sm->proto == WPA_PROTO_RSN) {
194 pmksa_cache_add(sm->pmksa, sm->pmk, pmk_len,
195 src_addr, sm->own_addr,
196 sm->network_ctx, sm->key_mgmt);
198 if (!sm->cur_pmksa && pmkid &&
199 pmksa_cache_get(sm->pmksa, src_addr, pmkid)) {
200 wpa_printf(MSG_DEBUG, "RSN: the new PMK "
201 "matches with the PMKID");
205 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
206 "WPA: Failed to get master session key from "
207 "EAPOL state machines");
208 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
209 "WPA: Key handshake aborted");
211 wpa_printf(MSG_DEBUG, "RSN: Cancelled PMKSA "
213 sm->cur_pmksa = NULL;
215 } else if (!abort_cached) {
221 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) {
222 /* Send EAPOL-Start to trigger full EAP authentication. */
226 wpa_printf(MSG_DEBUG, "RSN: no PMKSA entry found - trigger "
227 "full EAP authentication");
228 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
229 NULL, 0, &buflen, NULL);
231 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
244 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
245 * @sm: Pointer to WPA state machine data from wpa_sm_init()
246 * @dst: Destination address for the frame
247 * @key: Pointer to the EAPOL-Key frame header
248 * @ver: Version bits from EAPOL-Key Key Info
249 * @nonce: Nonce value for the EAPOL-Key frame
250 * @wpa_ie: WPA/RSN IE
251 * @wpa_ie_len: Length of the WPA/RSN IE
252 * @ptk: PTK to use for keyed hash and encryption
253 * Returns: 0 on success, -1 on failure
255 int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
256 const struct wpa_eapol_key *key,
257 int ver, const u8 *nonce,
258 const u8 *wpa_ie, size_t wpa_ie_len,
262 struct wpa_eapol_key *reply;
264 u8 *rsn_ie_buf = NULL;
266 if (wpa_ie == NULL) {
267 wpa_printf(MSG_WARNING, "WPA: No wpa_ie set - cannot "
272 #ifdef CONFIG_IEEE80211R
273 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
277 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
278 * FTIE from (Re)Association Response.
280 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
281 sm->assoc_resp_ies_len);
282 if (rsn_ie_buf == NULL)
284 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
285 res = wpa_insert_pmkid(rsn_ie_buf, wpa_ie_len,
293 if (sm->assoc_resp_ies) {
294 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
295 sm->assoc_resp_ies_len);
296 wpa_ie_len += sm->assoc_resp_ies_len;
301 #endif /* CONFIG_IEEE80211R */
303 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
305 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
306 NULL, sizeof(*reply) + wpa_ie_len,
307 &rlen, (void *) &reply);
313 reply->type = sm->proto == WPA_PROTO_RSN ?
314 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
315 WPA_PUT_BE16(reply->key_info,
316 ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC);
317 if (sm->proto == WPA_PROTO_RSN)
318 WPA_PUT_BE16(reply->key_length, 0);
320 os_memcpy(reply->key_length, key->key_length, 2);
321 os_memcpy(reply->replay_counter, key->replay_counter,
322 WPA_REPLAY_COUNTER_LEN);
324 WPA_PUT_BE16(reply->key_data_length, wpa_ie_len);
325 os_memcpy(reply + 1, wpa_ie, wpa_ie_len);
328 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
330 wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
331 wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
332 rbuf, rlen, reply->key_mic);
338 static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
339 const struct wpa_eapol_key *key,
342 size_t ptk_len = sm->pairwise_cipher == WPA_CIPHER_CCMP ? 48 : 64;
343 #ifdef CONFIG_IEEE80211R
344 if (wpa_key_mgmt_ft(sm->key_mgmt))
345 return wpa_derive_ptk_ft(sm, src_addr, key, ptk, ptk_len);
346 #endif /* CONFIG_IEEE80211R */
348 wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
349 sm->own_addr, sm->bssid, sm->snonce, key->key_nonce,
351 wpa_key_mgmt_sha256(sm->key_mgmt));
356 static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
357 const unsigned char *src_addr,
358 const struct wpa_eapol_key *key,
361 struct wpa_eapol_ie_parse ie;
365 if (wpa_sm_get_network_ctx(sm) == NULL) {
366 wpa_printf(MSG_WARNING, "WPA: No SSID info found (msg 1 of "
371 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
372 wpa_printf(MSG_DEBUG, "WPA: RX message 1 of 4-Way Handshake from "
373 MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
375 os_memset(&ie, 0, sizeof(ie));
377 #ifndef CONFIG_NO_WPA2
378 if (sm->proto == WPA_PROTO_RSN) {
379 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
380 const u8 *_buf = (const u8 *) (key + 1);
381 size_t len = WPA_GET_BE16(key->key_data_length);
382 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data", _buf, len);
383 wpa_supplicant_parse_ies(_buf, len, &ie);
385 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
386 "Authenticator", ie.pmkid, PMKID_LEN);
389 #endif /* CONFIG_NO_WPA2 */
391 if (wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid))
394 if (sm->renew_snonce) {
395 if (os_get_random(sm->snonce, WPA_NONCE_LEN)) {
396 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
397 "WPA: Failed to get random data for SNonce");
400 sm->renew_snonce = 0;
401 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
402 sm->snonce, WPA_NONCE_LEN);
405 /* Calculate PTK which will be stored as a temporary PTK until it has
406 * been verified when processing message 3/4. */
408 wpa_derive_ptk(sm, src_addr, key, ptk);
409 /* Supplicant: swap tx/rx Mic keys */
410 os_memcpy(buf, ptk->u.auth.tx_mic_key, 8);
411 os_memcpy(ptk->u.auth.tx_mic_key, ptk->u.auth.rx_mic_key, 8);
412 os_memcpy(ptk->u.auth.rx_mic_key, buf, 8);
415 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
416 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len,
420 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
424 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
428 static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
430 struct wpa_sm *sm = eloop_ctx;
431 rsn_preauth_candidate_process(sm);
435 static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
436 const u8 *addr, int secure)
438 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
439 "WPA: Key negotiation completed with "
440 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
441 wpa_cipher_txt(sm->pairwise_cipher),
442 wpa_cipher_txt(sm->group_cipher));
443 wpa_sm_cancel_auth_timeout(sm);
444 wpa_sm_set_state(sm, WPA_COMPLETED);
447 wpa_sm_mlme_setprotection(
448 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
449 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
450 eapol_sm_notify_portValid(sm->eapol, TRUE);
451 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt))
452 eapol_sm_notify_eap_success(sm->eapol, TRUE);
454 * Start preauthentication after a short wait to avoid a
455 * possible race condition between the data receive and key
456 * configuration after the 4-Way Handshake. This increases the
457 * likelyhood of the first preauth EAPOL-Start frame getting to
460 eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
463 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
464 wpa_printf(MSG_DEBUG, "RSN: Authenticator accepted "
465 "opportunistic PMKSA entry - marking it valid");
466 sm->cur_pmksa->opportunistic = 0;
469 #ifdef CONFIG_IEEE80211R
470 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
471 /* Prepare for the next transition */
472 wpa_ft_prepare_auth_request(sm, NULL);
474 #endif /* CONFIG_IEEE80211R */
478 static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
480 struct wpa_sm *sm = eloop_ctx;
481 wpa_printf(MSG_DEBUG, "WPA: Request PTK rekeying");
482 wpa_sm_key_request(sm, 0, 1);
486 static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
487 const struct wpa_eapol_key *key)
492 u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
494 wpa_printf(MSG_DEBUG, "WPA: Installing PTK to the driver.");
496 switch (sm->pairwise_cipher) {
497 case WPA_CIPHER_CCMP:
502 case WPA_CIPHER_TKIP:
507 case WPA_CIPHER_NONE:
508 wpa_printf(MSG_DEBUG, "WPA: Pairwise Cipher Suite: "
509 "NONE - do not use pairwise keys");
512 wpa_printf(MSG_WARNING, "WPA: Unsupported pairwise cipher %d",
513 sm->pairwise_cipher);
517 if (sm->proto == WPA_PROTO_RSN) {
520 key_rsc = key->key_rsc;
521 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
524 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
525 (u8 *) sm->ptk.tk1, keylen) < 0) {
526 wpa_printf(MSG_WARNING, "WPA: Failed to set PTK to the "
527 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
528 alg, keylen, MAC2STR(sm->bssid));
532 if (sm->wpa_ptk_rekey) {
533 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
534 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
542 static int wpa_supplicant_check_group_cipher(int group_cipher,
543 int keylen, int maxkeylen,
549 switch (group_cipher) {
550 case WPA_CIPHER_CCMP:
551 if (keylen != 16 || maxkeylen < 16) {
558 case WPA_CIPHER_TKIP:
559 if (keylen != 32 || maxkeylen < 32) {
566 case WPA_CIPHER_WEP104:
567 if (keylen != 13 || maxkeylen < 13) {
574 case WPA_CIPHER_WEP40:
575 if (keylen != 5 || maxkeylen < 5) {
583 wpa_printf(MSG_WARNING, "WPA: Unsupported Group Cipher %d",
589 wpa_printf(MSG_WARNING, "WPA: Unsupported %s Group Cipher key "
591 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
598 struct wpa_gtk_data {
600 int tx, key_rsc_len, keyidx;
606 static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
607 const struct wpa_gtk_data *gd,
610 const u8 *_gtk = gd->gtk;
613 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
614 wpa_printf(MSG_DEBUG, "WPA: Installing GTK to the driver "
615 "(keyidx=%d tx=%d len=%d).", gd->keyidx, gd->tx,
617 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
618 if (sm->group_cipher == WPA_CIPHER_TKIP) {
619 /* Swap Tx/Rx keys for Michael MIC */
620 os_memcpy(gtk_buf, gd->gtk, 16);
621 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
622 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
625 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
626 if (wpa_sm_set_key(sm, gd->alg,
627 (u8 *) "\xff\xff\xff\xff\xff\xff",
628 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
629 _gtk, gd->gtk_len) < 0) {
630 wpa_printf(MSG_WARNING, "WPA: Failed to set "
631 "GTK to the driver (Group only).");
634 } else if (wpa_sm_set_key(sm, gd->alg,
635 (u8 *) "\xff\xff\xff\xff\xff\xff",
636 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
637 _gtk, gd->gtk_len) < 0) {
638 wpa_printf(MSG_WARNING, "WPA: Failed to set GTK to "
639 "the driver (alg=%d keylen=%d keyidx=%d)",
640 gd->alg, gd->gtk_len, gd->keyidx);
648 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
651 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
652 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
653 * seemed to set this bit (incorrectly, since Tx is only when
654 * doing Group Key only APs) and without this workaround, the
655 * data connection does not work because wpa_supplicant
656 * configured non-zero keyidx to be used for unicast. */
657 wpa_printf(MSG_INFO, "WPA: Tx bit set for GTK, but pairwise "
658 "keys are used - ignore Tx bit");
665 static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
666 const struct wpa_eapol_key *key,
667 const u8 *gtk, size_t gtk_len,
670 #ifndef CONFIG_NO_WPA2
671 struct wpa_gtk_data gd;
674 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
676 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
677 * Reserved [bits 0-7]
681 os_memset(&gd, 0, sizeof(gd));
682 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
685 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
688 gd.keyidx = gtk[0] & 0x3;
689 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
690 !!(gtk[0] & BIT(2)));
694 os_memcpy(gd.gtk, gtk, gtk_len);
695 gd.gtk_len = gtk_len;
697 if (wpa_supplicant_check_group_cipher(sm->group_cipher,
699 &gd.key_rsc_len, &gd.alg) ||
700 wpa_supplicant_install_gtk(sm, &gd, key->key_rsc)) {
701 wpa_printf(MSG_DEBUG, "RSN: Failed to install GTK");
705 wpa_supplicant_key_neg_complete(sm, sm->bssid,
706 key_info & WPA_KEY_INFO_SECURE);
708 #else /* CONFIG_NO_WPA2 */
710 #endif /* CONFIG_NO_WPA2 */
714 static int ieee80211w_set_keys(struct wpa_sm *sm,
715 struct wpa_eapol_ie_parse *ie)
717 #ifdef CONFIG_IEEE80211W
718 if (sm->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC)
722 const struct wpa_igtk_kde *igtk;
724 if (ie->igtk_len != sizeof(*igtk))
726 igtk = (const struct wpa_igtk_kde *) ie->igtk;
727 keyidx = WPA_GET_LE16(igtk->keyid);
728 wpa_printf(MSG_DEBUG, "WPA: IGTK keyid %d "
729 "pn %02x%02x%02x%02x%02x%02x",
730 keyidx, MAC2STR(igtk->pn));
731 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK",
732 igtk->igtk, WPA_IGTK_LEN);
734 wpa_printf(MSG_WARNING, "WPA: Invalid IGTK KeyID %d",
738 if (wpa_sm_set_key(sm, WPA_ALG_IGTK,
739 (u8 *) "\xff\xff\xff\xff\xff\xff",
740 keyidx, 0, igtk->pn, sizeof(igtk->pn),
741 igtk->igtk, WPA_IGTK_LEN) < 0) {
742 wpa_printf(MSG_WARNING, "WPA: Failed to configure IGTK"
749 #else /* CONFIG_IEEE80211W */
751 #endif /* CONFIG_IEEE80211W */
755 static void wpa_report_ie_mismatch(struct wpa_sm *sm,
756 const char *reason, const u8 *src_addr,
757 const u8 *wpa_ie, size_t wpa_ie_len,
758 const u8 *rsn_ie, size_t rsn_ie_len)
760 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
761 reason, MAC2STR(src_addr));
764 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
765 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
768 if (!sm->ap_wpa_ie) {
769 wpa_printf(MSG_INFO, "WPA: No WPA IE in "
772 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
777 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
778 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
781 if (!sm->ap_rsn_ie) {
782 wpa_printf(MSG_INFO, "WPA: No RSN IE in "
785 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
789 wpa_sm_disassociate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
793 #ifdef CONFIG_IEEE80211R
795 static int ft_validate_mdie(struct wpa_sm *sm,
796 const unsigned char *src_addr,
797 struct wpa_eapol_ie_parse *ie)
799 struct rsn_mdie *mdie;
801 /* TODO: verify that full MDIE matches with the one from scan
802 * results, not only mobility domain */
804 mdie = (struct rsn_mdie *) (ie->mdie + 2);
805 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
806 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
807 MOBILITY_DOMAIN_ID_LEN) != 0) {
808 wpa_printf(MSG_DEBUG, "FT: MDIE in msg 3/4 did not "
809 "match with the current mobility domain");
817 static int ft_validate_rsnie(struct wpa_sm *sm,
818 const unsigned char *src_addr,
819 struct wpa_eapol_ie_parse *ie)
821 struct wpa_ie_data rsn;
827 * Verify that PMKR1Name from EAPOL-Key message 3/4
828 * matches with the value we derived.
830 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
831 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
832 wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
833 "FT 4-way handshake message 3/4");
837 if (os_memcmp(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0) {
838 wpa_printf(MSG_DEBUG, "FT: PMKR1Name mismatch in "
839 "FT 4-way handshake message 3/4");
840 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
841 rsn.pmkid, WPA_PMK_NAME_LEN);
842 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
843 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
851 static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
852 const unsigned char *src_addr,
853 struct wpa_eapol_ie_parse *ie)
855 if (ft_validate_mdie(sm, src_addr, ie) < 0 ||
856 ft_validate_rsnie(sm, src_addr, ie) < 0)
862 #endif /* CONFIG_IEEE80211R */
865 static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
866 const unsigned char *src_addr,
867 struct wpa_eapol_ie_parse *ie)
869 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
870 wpa_printf(MSG_DEBUG, "WPA: No WPA/RSN IE for this AP known. "
871 "Trying to get from scan results");
872 if (wpa_sm_get_beacon_ie(sm) < 0) {
873 wpa_printf(MSG_WARNING, "WPA: Could not find AP from "
876 wpa_printf(MSG_DEBUG, "WPA: Found the current AP from "
877 "updated scan results");
881 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
882 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
883 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
884 "with IE in Beacon/ProbeResp (no IE?)",
885 src_addr, ie->wpa_ie, ie->wpa_ie_len,
886 ie->rsn_ie, ie->rsn_ie_len);
890 if ((ie->wpa_ie && sm->ap_wpa_ie &&
891 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
892 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
893 (ie->rsn_ie && sm->ap_rsn_ie &&
894 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
895 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
896 ie->rsn_ie, ie->rsn_ie_len))) {
897 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
898 "with IE in Beacon/ProbeResp",
899 src_addr, ie->wpa_ie, ie->wpa_ie_len,
900 ie->rsn_ie, ie->rsn_ie_len);
904 if (sm->proto == WPA_PROTO_WPA &&
905 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
906 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
907 "detected - RSN was enabled and RSN IE "
908 "was in msg 3/4, but not in "
910 src_addr, ie->wpa_ie, ie->wpa_ie_len,
911 ie->rsn_ie, ie->rsn_ie_len);
915 #ifdef CONFIG_IEEE80211R
916 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
917 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
919 #endif /* CONFIG_IEEE80211R */
926 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
927 * @sm: Pointer to WPA state machine data from wpa_sm_init()
928 * @dst: Destination address for the frame
929 * @key: Pointer to the EAPOL-Key frame header
930 * @ver: Version bits from EAPOL-Key Key Info
931 * @key_info: Key Info
932 * @kde: KDEs to include the EAPOL-Key frame
933 * @kde_len: Length of KDEs
934 * @ptk: PTK to use for keyed hash and encryption
935 * Returns: 0 on success, -1 on failure
937 int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
938 const struct wpa_eapol_key *key,
939 u16 ver, u16 key_info,
940 const u8 *kde, size_t kde_len,
944 struct wpa_eapol_key *reply;
948 wpa_hexdump(MSG_DEBUG, "WPA: KDE for msg 4/4", kde, kde_len);
950 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
951 sizeof(*reply) + kde_len,
952 &rlen, (void *) &reply);
956 reply->type = sm->proto == WPA_PROTO_RSN ?
957 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
958 key_info &= WPA_KEY_INFO_SECURE;
959 key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
960 WPA_PUT_BE16(reply->key_info, key_info);
961 if (sm->proto == WPA_PROTO_RSN)
962 WPA_PUT_BE16(reply->key_length, 0);
964 os_memcpy(reply->key_length, key->key_length, 2);
965 os_memcpy(reply->replay_counter, key->replay_counter,
966 WPA_REPLAY_COUNTER_LEN);
968 WPA_PUT_BE16(reply->key_data_length, kde_len);
970 os_memcpy(reply + 1, kde, kde_len);
972 wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
973 wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
974 rbuf, rlen, reply->key_mic);
980 static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
981 const struct wpa_eapol_key *key,
984 u16 key_info, keylen, len;
986 struct wpa_eapol_ie_parse ie;
988 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
989 wpa_printf(MSG_DEBUG, "WPA: RX message 3 of 4-Way Handshake from "
990 MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
992 key_info = WPA_GET_BE16(key->key_info);
994 pos = (const u8 *) (key + 1);
995 len = WPA_GET_BE16(key->key_data_length);
996 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", pos, len);
997 wpa_supplicant_parse_ies(pos, len, &ie);
998 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
999 wpa_printf(MSG_WARNING, "WPA: GTK IE in unencrypted key data");
1002 #ifdef CONFIG_IEEE80211W
1003 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1004 wpa_printf(MSG_WARNING, "WPA: IGTK KDE in unencrypted key "
1009 if (ie.igtk && ie.igtk_len != sizeof(struct wpa_igtk_kde)) {
1010 wpa_printf(MSG_WARNING, "WPA: Invalid IGTK KDE length %lu",
1011 (unsigned long) ie.igtk_len);
1014 #endif /* CONFIG_IEEE80211W */
1016 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1019 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1020 wpa_printf(MSG_WARNING, "WPA: ANonce from message 1 of 4-Way "
1021 "Handshake differs from 3 of 4-Way Handshake - drop"
1022 " packet (src=" MACSTR ")", MAC2STR(sm->bssid));
1026 keylen = WPA_GET_BE16(key->key_length);
1027 switch (sm->pairwise_cipher) {
1028 case WPA_CIPHER_CCMP:
1030 wpa_printf(MSG_WARNING, "WPA: Invalid CCMP key length "
1031 "%d (src=" MACSTR ")",
1032 keylen, MAC2STR(sm->bssid));
1036 case WPA_CIPHER_TKIP:
1038 wpa_printf(MSG_WARNING, "WPA: Invalid TKIP key length "
1039 "%d (src=" MACSTR ")",
1040 keylen, MAC2STR(sm->bssid));
1046 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
1047 NULL, 0, &sm->ptk)) {
1051 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1052 * for the next 4-Way Handshake. If msg 3 is received again, the old
1053 * SNonce will still be used to avoid changing PTK. */
1054 sm->renew_snonce = 1;
1056 if (key_info & WPA_KEY_INFO_INSTALL) {
1057 if (wpa_supplicant_install_ptk(sm, key))
1061 if (key_info & WPA_KEY_INFO_SECURE) {
1062 wpa_sm_mlme_setprotection(
1063 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1064 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1065 eapol_sm_notify_portValid(sm->eapol, TRUE);
1067 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1070 wpa_supplicant_pairwise_gtk(sm, key,
1071 ie.gtk, ie.gtk_len, key_info) < 0) {
1072 wpa_printf(MSG_INFO, "RSN: Failed to configure GTK");
1076 if (ieee80211w_set_keys(sm, &ie) < 0) {
1077 wpa_printf(MSG_INFO, "RSN: Failed to configure IGTK");
1084 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1088 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1092 struct wpa_gtk_data *gd)
1095 struct wpa_eapol_ie_parse ie;
1097 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
1098 wpa_supplicant_parse_ies(keydata, keydatalen, &ie);
1099 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1100 wpa_printf(MSG_WARNING, "WPA: GTK IE in unencrypted key data");
1103 if (ie.gtk == NULL) {
1104 wpa_printf(MSG_INFO, "WPA: No GTK IE in Group Key msg 1/2");
1107 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1109 if (wpa_supplicant_check_group_cipher(sm->group_cipher,
1110 gd->gtk_len, maxkeylen,
1111 &gd->key_rsc_len, &gd->alg))
1114 wpa_hexdump(MSG_DEBUG, "RSN: received GTK in group key handshake",
1115 ie.gtk, ie.gtk_len);
1116 gd->keyidx = ie.gtk[0] & 0x3;
1117 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1118 !!(ie.gtk[0] & BIT(2)));
1119 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1120 wpa_printf(MSG_INFO, "RSN: Too long GTK in GTK IE "
1121 "(len=%lu)", (unsigned long) ie.gtk_len - 2);
1124 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1126 if (ieee80211w_set_keys(sm, &ie) < 0)
1127 wpa_printf(MSG_INFO, "RSN: Failed to configure IGTK");
1133 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1134 const struct wpa_eapol_key *key,
1135 size_t keydatalen, int key_info,
1136 size_t extra_len, u16 ver,
1137 struct wpa_gtk_data *gd)
1142 gd->gtk_len = WPA_GET_BE16(key->key_length);
1143 maxkeylen = keydatalen;
1144 if (keydatalen > extra_len) {
1145 wpa_printf(MSG_INFO, "WPA: Truncated EAPOL-Key packet:"
1146 " key_data_length=%lu > extra_len=%lu",
1147 (unsigned long) keydatalen,
1148 (unsigned long) extra_len);
1151 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1152 if (maxkeylen < 8) {
1153 wpa_printf(MSG_INFO, "WPA: Too short maxkeylen (%lu)",
1154 (unsigned long) maxkeylen);
1160 if (wpa_supplicant_check_group_cipher(sm->group_cipher,
1161 gd->gtk_len, maxkeylen,
1162 &gd->key_rsc_len, &gd->alg))
1165 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1166 WPA_KEY_INFO_KEY_INDEX_SHIFT;
1167 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1168 os_memcpy(ek, key->key_iv, 16);
1169 os_memcpy(ek + 16, sm->ptk.kek, 16);
1170 if (keydatalen > sizeof(gd->gtk)) {
1171 wpa_printf(MSG_WARNING, "WPA: RC4 key data "
1173 (unsigned long) keydatalen);
1176 os_memcpy(gd->gtk, key + 1, keydatalen);
1177 if (rc4_skip(ek, 32, 256, gd->gtk, keydatalen)) {
1178 wpa_printf(MSG_ERROR, "WPA: RC4 failed");
1181 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1182 if (keydatalen % 8) {
1183 wpa_printf(MSG_WARNING, "WPA: Unsupported AES-WRAP "
1184 "len %lu", (unsigned long) keydatalen);
1187 if (maxkeylen > sizeof(gd->gtk)) {
1188 wpa_printf(MSG_WARNING, "WPA: AES-WRAP key data "
1189 "too long (keydatalen=%lu maxkeylen=%lu)",
1190 (unsigned long) keydatalen,
1191 (unsigned long) maxkeylen);
1194 if (aes_unwrap(sm->ptk.kek, maxkeylen / 8,
1195 (const u8 *) (key + 1), gd->gtk)) {
1196 wpa_printf(MSG_WARNING, "WPA: AES unwrap "
1197 "failed - could not decrypt GTK");
1201 wpa_printf(MSG_WARNING, "WPA: Unsupported key_info type %d",
1205 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1206 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1211 static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1212 const struct wpa_eapol_key *key,
1213 int ver, u16 key_info)
1216 struct wpa_eapol_key *reply;
1219 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1220 sizeof(*reply), &rlen, (void *) &reply);
1224 reply->type = sm->proto == WPA_PROTO_RSN ?
1225 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1226 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1227 key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
1228 WPA_PUT_BE16(reply->key_info, key_info);
1229 if (sm->proto == WPA_PROTO_RSN)
1230 WPA_PUT_BE16(reply->key_length, 0);
1232 os_memcpy(reply->key_length, key->key_length, 2);
1233 os_memcpy(reply->replay_counter, key->replay_counter,
1234 WPA_REPLAY_COUNTER_LEN);
1236 WPA_PUT_BE16(reply->key_data_length, 0);
1238 wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
1239 wpa_eapol_key_send(sm, sm->ptk.kck, ver, sm->bssid, ETH_P_EAPOL,
1240 rbuf, rlen, reply->key_mic);
1246 static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1247 const unsigned char *src_addr,
1248 const struct wpa_eapol_key *key,
1249 int extra_len, u16 ver)
1251 u16 key_info, keydatalen;
1253 struct wpa_gtk_data gd;
1255 os_memset(&gd, 0, sizeof(gd));
1257 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1258 wpa_printf(MSG_DEBUG, "WPA: RX message 1 of Group Key Handshake from "
1259 MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1261 key_info = WPA_GET_BE16(key->key_info);
1262 keydatalen = WPA_GET_BE16(key->key_data_length);
1264 if (sm->proto == WPA_PROTO_RSN) {
1265 ret = wpa_supplicant_process_1_of_2_rsn(sm,
1266 (const u8 *) (key + 1),
1267 keydatalen, key_info,
1270 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, keydatalen,
1271 key_info, extra_len,
1275 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1280 if (wpa_supplicant_install_gtk(sm, &gd, key->key_rsc) ||
1281 wpa_supplicant_send_2_of_2(sm, key, ver, key_info))
1285 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
1286 "completed with " MACSTR " [GTK=%s]",
1287 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1288 wpa_sm_cancel_auth_timeout(sm);
1289 wpa_sm_set_state(sm, WPA_COMPLETED);
1291 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1293 WPA_KEY_INFO_SECURE);
1298 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1302 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
1303 struct wpa_eapol_key *key,
1305 const u8 *buf, size_t len)
1310 os_memcpy(mic, key->key_mic, 16);
1312 os_memset(key->key_mic, 0, 16);
1313 wpa_eapol_key_mic(sm->tptk.kck, ver, buf, len,
1315 if (os_memcmp(mic, key->key_mic, 16) != 0) {
1316 wpa_printf(MSG_WARNING, "WPA: Invalid EAPOL-Key MIC "
1317 "when using TPTK - ignoring TPTK");
1322 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
1326 if (!ok && sm->ptk_set) {
1327 os_memset(key->key_mic, 0, 16);
1328 wpa_eapol_key_mic(sm->ptk.kck, ver, buf, len,
1330 if (os_memcmp(mic, key->key_mic, 16) != 0) {
1331 wpa_printf(MSG_WARNING, "WPA: Invalid EAPOL-Key MIC "
1332 "- dropping packet");
1339 wpa_printf(MSG_WARNING, "WPA: Could not verify EAPOL-Key MIC "
1340 "- dropping packet");
1344 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1345 WPA_REPLAY_COUNTER_LEN);
1346 sm->rx_replay_counter_set = 1;
1351 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1352 static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
1353 struct wpa_eapol_key *key, u16 ver)
1355 u16 keydatalen = WPA_GET_BE16(key->key_data_length);
1357 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
1358 (u8 *) (key + 1), keydatalen);
1360 wpa_printf(MSG_WARNING, "WPA: PTK not available, "
1361 "cannot decrypt EAPOL-Key key data.");
1365 /* Decrypt key data here so that this operation does not need
1366 * to be implemented separately for each message type. */
1367 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1369 os_memcpy(ek, key->key_iv, 16);
1370 os_memcpy(ek + 16, sm->ptk.kek, 16);
1371 if (rc4_skip(ek, 32, 256, (u8 *) (key + 1), keydatalen)) {
1372 wpa_printf(MSG_ERROR, "WPA: RC4 failed");
1375 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1376 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1378 if (keydatalen % 8) {
1379 wpa_printf(MSG_WARNING, "WPA: Unsupported "
1380 "AES-WRAP len %d", keydatalen);
1383 keydatalen -= 8; /* AES-WRAP adds 8 bytes */
1384 buf = os_malloc(keydatalen);
1386 wpa_printf(MSG_WARNING, "WPA: No memory for "
1387 "AES-UNWRAP buffer");
1390 if (aes_unwrap(sm->ptk.kek, keydatalen / 8,
1391 (u8 *) (key + 1), buf)) {
1393 wpa_printf(MSG_WARNING, "WPA: AES unwrap failed - "
1394 "could not decrypt EAPOL-Key key data");
1397 os_memcpy(key + 1, buf, keydatalen);
1399 WPA_PUT_BE16(key->key_data_length, keydatalen);
1401 wpa_printf(MSG_WARNING, "WPA: Unsupported key_info type %d",
1405 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
1406 (u8 *) (key + 1), keydatalen);
1412 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1413 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1415 void wpa_sm_aborted_cached(struct wpa_sm *sm)
1417 if (sm && sm->cur_pmksa) {
1418 wpa_printf(MSG_DEBUG, "RSN: Cancelling PMKSA caching attempt");
1419 sm->cur_pmksa = NULL;
1424 static void wpa_eapol_key_dump(const struct wpa_eapol_key *key)
1426 #ifndef CONFIG_NO_STDOUT_DEBUG
1427 u16 key_info = WPA_GET_BE16(key->key_info);
1429 wpa_printf(MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1430 wpa_printf(MSG_DEBUG, " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s"
1432 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1433 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1434 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1435 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1436 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1437 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1438 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1439 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1440 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1441 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1442 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1443 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1444 wpa_printf(MSG_DEBUG, " key_length=%u key_data_length=%u",
1445 WPA_GET_BE16(key->key_length),
1446 WPA_GET_BE16(key->key_data_length));
1447 wpa_hexdump(MSG_DEBUG, " replay_counter",
1448 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1449 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1450 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1451 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1452 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
1453 wpa_hexdump(MSG_DEBUG, " key_mic", key->key_mic, 16);
1454 #endif /* CONFIG_NO_STDOUT_DEBUG */
1459 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1460 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1461 * @src_addr: Source MAC address of the EAPOL packet
1462 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1463 * @len: Length of the EAPOL frame
1464 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1466 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1467 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1468 * only processing WPA and WPA2 EAPOL-Key frames.
1470 * The received EAPOL-Key packets are validated and valid packets are replied
1471 * to. In addition, key material (PTK, GTK) is configured at the end of a
1472 * successful key handshake.
1474 int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1475 const u8 *buf, size_t len)
1477 size_t plen, data_len, extra_len;
1478 struct ieee802_1x_hdr *hdr;
1479 struct wpa_eapol_key *key;
1483 struct wpa_peerkey *peerkey = NULL;
1485 #ifdef CONFIG_IEEE80211R
1486 sm->ft_completed = 0;
1487 #endif /* CONFIG_IEEE80211R */
1489 if (len < sizeof(*hdr) + sizeof(*key)) {
1490 wpa_printf(MSG_DEBUG, "WPA: EAPOL frame too short to be a WPA "
1491 "EAPOL-Key (len %lu, expecting at least %lu)",
1492 (unsigned long) len,
1493 (unsigned long) sizeof(*hdr) + sizeof(*key));
1497 tmp = os_malloc(len);
1500 os_memcpy(tmp, buf, len);
1502 hdr = (struct ieee802_1x_hdr *) tmp;
1503 key = (struct wpa_eapol_key *) (hdr + 1);
1504 plen = be_to_host16(hdr->length);
1505 data_len = plen + sizeof(*hdr);
1506 wpa_printf(MSG_DEBUG, "IEEE 802.1X RX: version=%d type=%d length=%lu",
1507 hdr->version, hdr->type, (unsigned long) plen);
1509 if (hdr->version < EAPOL_VERSION) {
1510 /* TODO: backwards compatibility */
1512 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
1513 wpa_printf(MSG_DEBUG, "WPA: EAPOL frame (type %u) discarded, "
1514 "not a Key frame", hdr->type);
1518 if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
1519 wpa_printf(MSG_DEBUG, "WPA: EAPOL frame payload size %lu "
1520 "invalid (frame size %lu)",
1521 (unsigned long) plen, (unsigned long) len);
1526 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
1528 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key type (%d) unknown, "
1529 "discarded", key->type);
1533 wpa_eapol_key_dump(key);
1535 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
1536 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", tmp, len);
1537 if (data_len < len) {
1538 wpa_printf(MSG_DEBUG, "WPA: ignoring %lu bytes after the IEEE "
1539 "802.1X data", (unsigned long) len - data_len);
1541 key_info = WPA_GET_BE16(key->key_info);
1542 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1543 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
1544 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
1545 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1546 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
1547 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1548 wpa_printf(MSG_INFO, "WPA: Unsupported EAPOL-Key descriptor "
1549 "version %d.", ver);
1553 #ifdef CONFIG_IEEE80211R
1554 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
1555 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
1556 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1557 wpa_printf(MSG_INFO, "FT: AP did not use "
1562 #endif /* CONFIG_IEEE80211R */
1563 #ifdef CONFIG_IEEE80211W
1564 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
1565 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1566 wpa_printf(MSG_INFO, "WPA: AP did not use the "
1567 "negotiated AES-128-CMAC.");
1571 #endif /* CONFIG_IEEE80211W */
1572 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
1573 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1574 wpa_printf(MSG_INFO, "WPA: CCMP is used, but EAPOL-Key "
1575 "descriptor version (%d) is not 2.", ver);
1576 if (sm->group_cipher != WPA_CIPHER_CCMP &&
1577 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1578 /* Earlier versions of IEEE 802.11i did not explicitly
1579 * require version 2 descriptor for all EAPOL-Key
1580 * packets, so allow group keys to use version 1 if
1581 * CCMP is not used for them. */
1582 wpa_printf(MSG_INFO, "WPA: Backwards compatibility: "
1583 "allow invalid version for non-CCMP group "
1589 #ifdef CONFIG_PEERKEY
1590 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
1591 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
1595 if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
1596 if (!peerkey->initiator && peerkey->replay_counter_set &&
1597 os_memcmp(key->replay_counter, peerkey->replay_counter,
1598 WPA_REPLAY_COUNTER_LEN) <= 0) {
1599 wpa_printf(MSG_WARNING, "RSN: EAPOL-Key Replay "
1600 "Counter did not increase (STK) - dropping "
1603 } else if (peerkey->initiator) {
1604 u8 _tmp[WPA_REPLAY_COUNTER_LEN];
1605 os_memcpy(_tmp, key->replay_counter,
1606 WPA_REPLAY_COUNTER_LEN);
1607 inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
1608 if (os_memcmp(_tmp, peerkey->replay_counter,
1609 WPA_REPLAY_COUNTER_LEN) != 0) {
1610 wpa_printf(MSG_DEBUG, "RSN: EAPOL-Key Replay "
1611 "Counter did not match (STK) - "
1618 if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
1619 wpa_printf(MSG_INFO, "RSN: Ack bit in key_info from STK peer");
1622 #endif /* CONFIG_PEERKEY */
1624 if (!peerkey && sm->rx_replay_counter_set &&
1625 os_memcmp(key->replay_counter, sm->rx_replay_counter,
1626 WPA_REPLAY_COUNTER_LEN) <= 0) {
1627 wpa_printf(MSG_WARNING, "WPA: EAPOL-Key Replay Counter did not"
1628 " increase - dropping packet");
1632 if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
1633 #ifdef CONFIG_PEERKEY
1634 && (peerkey == NULL || !peerkey->initiator)
1635 #endif /* CONFIG_PEERKEY */
1637 wpa_printf(MSG_INFO, "WPA: No Ack bit in key_info");
1641 if (key_info & WPA_KEY_INFO_REQUEST) {
1642 wpa_printf(MSG_INFO, "WPA: EAPOL-Key with Request bit - "
1647 if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
1648 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
1651 #ifdef CONFIG_PEERKEY
1652 if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
1653 peerkey_verify_eapol_key_mic(sm, peerkey, key, ver, tmp, data_len))
1655 #endif /* CONFIG_PEERKEY */
1657 extra_len = data_len - sizeof(*hdr) - sizeof(*key);
1659 if (WPA_GET_BE16(key->key_data_length) > extra_len) {
1660 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
1661 "frame - key_data overflow (%d > %lu)",
1662 WPA_GET_BE16(key->key_data_length),
1663 (unsigned long) extra_len);
1666 extra_len = WPA_GET_BE16(key->key_data_length);
1668 if (sm->proto == WPA_PROTO_RSN &&
1669 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1670 if (wpa_supplicant_decrypt_key_data(sm, key, ver))
1672 extra_len = WPA_GET_BE16(key->key_data_length);
1675 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1676 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
1677 wpa_printf(MSG_WARNING, "WPA: Ignored EAPOL-Key "
1678 "(Pairwise) with non-zero key index");
1682 /* PeerKey 4-Way Handshake */
1683 peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver);
1684 } else if (key_info & WPA_KEY_INFO_MIC) {
1685 /* 3/4 4-Way Handshake */
1686 wpa_supplicant_process_3_of_4(sm, key, ver);
1688 /* 1/4 4-Way Handshake */
1689 wpa_supplicant_process_1_of_4(sm, src_addr, key,
1692 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
1693 /* PeerKey SMK Handshake */
1694 peerkey_rx_eapol_smk(sm, src_addr, key, extra_len, key_info,
1697 if (key_info & WPA_KEY_INFO_MIC) {
1698 /* 1/2 Group Key Handshake */
1699 wpa_supplicant_process_1_of_2(sm, src_addr, key,
1702 wpa_printf(MSG_WARNING, "WPA: EAPOL-Key (Group) "
1703 "without Mic bit - dropped");
1715 #ifdef CONFIG_CTRL_IFACE
1716 static int wpa_cipher_bits(int cipher)
1719 case WPA_CIPHER_CCMP:
1721 case WPA_CIPHER_TKIP:
1723 case WPA_CIPHER_WEP104:
1725 case WPA_CIPHER_WEP40:
1733 static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
1735 switch (sm->key_mgmt) {
1736 case WPA_KEY_MGMT_IEEE8021X:
1737 return (sm->proto == WPA_PROTO_RSN ?
1738 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
1739 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
1740 case WPA_KEY_MGMT_PSK:
1741 return (sm->proto == WPA_PROTO_RSN ?
1742 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
1743 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
1744 #ifdef CONFIG_IEEE80211R
1745 case WPA_KEY_MGMT_FT_IEEE8021X:
1746 return RSN_AUTH_KEY_MGMT_FT_802_1X;
1747 case WPA_KEY_MGMT_FT_PSK:
1748 return RSN_AUTH_KEY_MGMT_FT_PSK;
1749 #endif /* CONFIG_IEEE80211R */
1750 #ifdef CONFIG_IEEE80211W
1751 case WPA_KEY_MGMT_IEEE8021X_SHA256:
1752 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
1753 case WPA_KEY_MGMT_PSK_SHA256:
1754 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
1755 #endif /* CONFIG_IEEE80211W */
1756 case WPA_KEY_MGMT_WPA_NONE:
1757 return WPA_AUTH_KEY_MGMT_NONE;
1764 static u32 wpa_cipher_suite(struct wpa_sm *sm, int cipher)
1767 case WPA_CIPHER_CCMP:
1768 return (sm->proto == WPA_PROTO_RSN ?
1769 RSN_CIPHER_SUITE_CCMP : WPA_CIPHER_SUITE_CCMP);
1770 case WPA_CIPHER_TKIP:
1771 return (sm->proto == WPA_PROTO_RSN ?
1772 RSN_CIPHER_SUITE_TKIP : WPA_CIPHER_SUITE_TKIP);
1773 case WPA_CIPHER_WEP104:
1774 return (sm->proto == WPA_PROTO_RSN ?
1775 RSN_CIPHER_SUITE_WEP104 : WPA_CIPHER_SUITE_WEP104);
1776 case WPA_CIPHER_WEP40:
1777 return (sm->proto == WPA_PROTO_RSN ?
1778 RSN_CIPHER_SUITE_WEP40 : WPA_CIPHER_SUITE_WEP40);
1779 case WPA_CIPHER_NONE:
1780 return (sm->proto == WPA_PROTO_RSN ?
1781 RSN_CIPHER_SUITE_NONE : WPA_CIPHER_SUITE_NONE);
1788 #define RSN_SUITE "%02x-%02x-%02x-%d"
1789 #define RSN_SUITE_ARG(s) \
1790 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
1793 * wpa_sm_get_mib - Dump text list of MIB entries
1794 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1795 * @buf: Buffer for the list
1796 * @buflen: Length of the buffer
1797 * Returns: Number of bytes written to buffer
1799 * This function is used fetch dot11 MIB variables.
1801 int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
1803 char pmkid_txt[PMKID_LEN * 2 + 1];
1807 if (sm->cur_pmksa) {
1808 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
1809 sm->cur_pmksa->pmkid, PMKID_LEN);
1811 pmkid_txt[0] = '\0';
1813 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
1814 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
1815 sm->proto == WPA_PROTO_RSN)
1820 ret = os_snprintf(buf, buflen,
1821 "dot11RSNAOptionImplemented=TRUE\n"
1822 "dot11RSNAPreauthenticationImplemented=TRUE\n"
1823 "dot11RSNAEnabled=%s\n"
1824 "dot11RSNAPreauthenticationEnabled=%s\n"
1825 "dot11RSNAConfigVersion=%d\n"
1826 "dot11RSNAConfigPairwiseKeysSupported=5\n"
1827 "dot11RSNAConfigGroupCipherSize=%d\n"
1828 "dot11RSNAConfigPMKLifetime=%d\n"
1829 "dot11RSNAConfigPMKReauthThreshold=%d\n"
1830 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
1831 "dot11RSNAConfigSATimeout=%d\n",
1832 rsna ? "TRUE" : "FALSE",
1833 rsna ? "TRUE" : "FALSE",
1835 wpa_cipher_bits(sm->group_cipher),
1836 sm->dot11RSNAConfigPMKLifetime,
1837 sm->dot11RSNAConfigPMKReauthThreshold,
1838 sm->dot11RSNAConfigSATimeout);
1839 if (ret < 0 || (size_t) ret >= buflen)
1844 buf + len, buflen - len,
1845 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
1846 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
1847 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
1848 "dot11RSNAPMKIDUsed=%s\n"
1849 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
1850 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
1851 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
1852 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
1853 "dot11RSNA4WayHandshakeFailures=%u\n",
1854 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
1855 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->pairwise_cipher)),
1856 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->group_cipher)),
1858 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
1859 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->pairwise_cipher)),
1860 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->group_cipher)),
1861 sm->dot11RSNA4WayHandshakeFailures);
1862 if (ret >= 0 && (size_t) ret < buflen)
1867 #endif /* CONFIG_CTRL_IFACE */
1870 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
1871 void *ctx, int replace)
1873 struct wpa_sm *sm = ctx;
1875 if (sm->cur_pmksa == entry ||
1876 (sm->pmk_len == entry->pmk_len &&
1877 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
1878 wpa_printf(MSG_DEBUG, "RSN: removed current PMKSA entry");
1879 sm->cur_pmksa = NULL;
1882 /* A new entry is being added, so no need to
1883 * deauthenticate in this case. This happens when EAP
1884 * authentication is completed again (reauth or failed
1885 * PMKSA caching attempt). */
1889 os_memset(sm->pmk, 0, sizeof(sm->pmk));
1890 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1896 * wpa_sm_init - Initialize WPA state machine
1897 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
1898 * Returns: Pointer to the allocated WPA state machine data
1900 * This function is used to allocate a new WPA state machine and the returned
1901 * value is passed to all WPA state machine calls.
1903 struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
1907 sm = os_zalloc(sizeof(*sm));
1910 dl_list_init(&sm->pmksa_candidates);
1911 sm->renew_snonce = 1;
1914 sm->dot11RSNAConfigPMKLifetime = 43200;
1915 sm->dot11RSNAConfigPMKReauthThreshold = 70;
1916 sm->dot11RSNAConfigSATimeout = 60;
1918 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
1919 if (sm->pmksa == NULL) {
1920 wpa_printf(MSG_ERROR, "RSN: PMKSA cache initialization "
1931 * wpa_sm_deinit - Deinitialize WPA state machine
1932 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1934 void wpa_sm_deinit(struct wpa_sm *sm)
1938 pmksa_cache_deinit(sm->pmksa);
1939 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
1940 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
1941 os_free(sm->assoc_wpa_ie);
1942 os_free(sm->ap_wpa_ie);
1943 os_free(sm->ap_rsn_ie);
1946 #ifdef CONFIG_IEEE80211R
1947 os_free(sm->assoc_resp_ies);
1948 #endif /* CONFIG_IEEE80211R */
1954 * wpa_sm_notify_assoc - Notify WPA state machine about association
1955 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1956 * @bssid: The BSSID of the new association
1958 * This function is called to let WPA state machine know that the connection
1961 void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
1968 wpa_printf(MSG_DEBUG, "WPA: Association event - clear replay counter");
1969 os_memcpy(sm->bssid, bssid, ETH_ALEN);
1970 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
1971 sm->rx_replay_counter_set = 0;
1972 sm->renew_snonce = 1;
1973 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
1974 rsn_preauth_deinit(sm);
1976 #ifdef CONFIG_IEEE80211R
1977 if (wpa_ft_is_completed(sm)) {
1979 * Clear portValid to kick EAPOL state machine to re-enter
1980 * AUTHENTICATED state to get the EAPOL port Authorized.
1982 eapol_sm_notify_portValid(sm->eapol, FALSE);
1983 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
1985 /* Prepare for the next transition */
1986 wpa_ft_prepare_auth_request(sm, NULL);
1990 #endif /* CONFIG_IEEE80211R */
1994 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
1995 * this is not part of a Fast BSS Transition.
1997 wpa_printf(MSG_DEBUG, "WPA: Clear old PTK");
2005 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2006 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2008 * This function is called to let WPA state machine know that the connection
2009 * was lost. This will abort any existing pre-authentication session.
2011 void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2013 rsn_preauth_deinit(sm);
2014 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2015 sm->dot11RSNA4WayHandshakeFailures++;
2020 * wpa_sm_set_pmk - Set PMK
2021 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2023 * @pmk_len: The length of the new PMK in bytes
2025 * Configure the PMK for WPA state machine.
2027 void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len)
2032 sm->pmk_len = pmk_len;
2033 os_memcpy(sm->pmk, pmk, pmk_len);
2035 #ifdef CONFIG_IEEE80211R
2036 /* Set XXKey to be PSK for FT key derivation */
2037 sm->xxkey_len = pmk_len;
2038 os_memcpy(sm->xxkey, pmk, pmk_len);
2039 #endif /* CONFIG_IEEE80211R */
2044 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2045 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2047 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2050 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2055 if (sm->cur_pmksa) {
2056 sm->pmk_len = sm->cur_pmksa->pmk_len;
2057 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2059 sm->pmk_len = PMK_LEN;
2060 os_memset(sm->pmk, 0, PMK_LEN);
2066 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2067 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2068 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2070 void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2073 sm->fast_reauth = fast_reauth;
2078 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2079 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2080 * @scard_ctx: Context pointer for smartcard related callback functions
2082 void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2086 sm->scard_ctx = scard_ctx;
2087 if (sm->preauth_eapol)
2088 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2093 * wpa_sm_set_config - Notification of current configration change
2094 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2095 * @config: Pointer to current network configuration
2097 * Notify WPA state machine that configuration has changed. config will be
2098 * stored as a backpointer to network configuration. This can be %NULL to clear
2099 * the stored pointed.
2101 void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2107 sm->network_ctx = config->network_ctx;
2108 sm->peerkey_enabled = config->peerkey_enabled;
2109 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2110 sm->proactive_key_caching = config->proactive_key_caching;
2111 sm->eap_workaround = config->eap_workaround;
2112 sm->eap_conf_ctx = config->eap_conf_ctx;
2114 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2115 sm->ssid_len = config->ssid_len;
2118 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
2120 sm->network_ctx = NULL;
2121 sm->peerkey_enabled = 0;
2122 sm->allowed_pairwise_cipher = 0;
2123 sm->proactive_key_caching = 0;
2124 sm->eap_workaround = 0;
2125 sm->eap_conf_ctx = NULL;
2127 sm->wpa_ptk_rekey = 0;
2129 if (config == NULL || config->network_ctx != sm->network_ctx)
2130 pmksa_cache_notify_reconfig(sm->pmksa);
2135 * wpa_sm_set_own_addr - Set own MAC address
2136 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2137 * @addr: Own MAC address
2139 void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2142 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2147 * wpa_sm_set_ifname - Set network interface name
2148 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2149 * @ifname: Interface name
2150 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2152 void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2153 const char *bridge_ifname)
2156 sm->ifname = ifname;
2157 sm->bridge_ifname = bridge_ifname;
2163 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2164 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2165 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2167 void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2175 * wpa_sm_set_param - Set WPA state machine parameters
2176 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2177 * @param: Parameter field
2178 * @value: Parameter value
2179 * Returns: 0 on success, -1 on failure
2181 int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2190 case RSNA_PMK_LIFETIME:
2192 sm->dot11RSNAConfigPMKLifetime = value;
2196 case RSNA_PMK_REAUTH_THRESHOLD:
2197 if (value > 0 && value <= 100)
2198 sm->dot11RSNAConfigPMKReauthThreshold = value;
2202 case RSNA_SA_TIMEOUT:
2204 sm->dot11RSNAConfigSATimeout = value;
2208 case WPA_PARAM_PROTO:
2211 case WPA_PARAM_PAIRWISE:
2212 sm->pairwise_cipher = value;
2214 case WPA_PARAM_GROUP:
2215 sm->group_cipher = value;
2217 case WPA_PARAM_KEY_MGMT:
2218 sm->key_mgmt = value;
2220 #ifdef CONFIG_IEEE80211W
2221 case WPA_PARAM_MGMT_GROUP:
2222 sm->mgmt_group_cipher = value;
2224 #endif /* CONFIG_IEEE80211W */
2225 case WPA_PARAM_RSN_ENABLED:
2226 sm->rsn_enabled = value;
2240 * wpa_sm_get_param - Get WPA state machine parameters
2241 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2242 * @param: Parameter field
2243 * Returns: Parameter value
2245 unsigned int wpa_sm_get_param(struct wpa_sm *sm, enum wpa_sm_conf_params param)
2251 case RSNA_PMK_LIFETIME:
2252 return sm->dot11RSNAConfigPMKLifetime;
2253 case RSNA_PMK_REAUTH_THRESHOLD:
2254 return sm->dot11RSNAConfigPMKReauthThreshold;
2255 case RSNA_SA_TIMEOUT:
2256 return sm->dot11RSNAConfigSATimeout;
2257 case WPA_PARAM_PROTO:
2259 case WPA_PARAM_PAIRWISE:
2260 return sm->pairwise_cipher;
2261 case WPA_PARAM_GROUP:
2262 return sm->group_cipher;
2263 case WPA_PARAM_KEY_MGMT:
2264 return sm->key_mgmt;
2265 #ifdef CONFIG_IEEE80211W
2266 case WPA_PARAM_MGMT_GROUP:
2267 return sm->mgmt_group_cipher;
2268 #endif /* CONFIG_IEEE80211W */
2269 case WPA_PARAM_RSN_ENABLED:
2270 return sm->rsn_enabled;
2278 * wpa_sm_get_status - Get WPA state machine
2279 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2280 * @buf: Buffer for status information
2281 * @buflen: Maximum buffer length
2282 * @verbose: Whether to include verbose status information
2283 * Returns: Number of bytes written to buf.
2285 * Query WPA state machine for status information. This function fills in
2286 * a text area with current status information. If the buffer (buf) is not
2287 * large enough, status information will be truncated to fit the buffer.
2289 int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2292 char *pos = buf, *end = buf + buflen;
2295 ret = os_snprintf(pos, end - pos,
2296 "pairwise_cipher=%s\n"
2299 wpa_cipher_txt(sm->pairwise_cipher),
2300 wpa_cipher_txt(sm->group_cipher),
2301 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
2302 if (ret < 0 || ret >= end - pos)
2310 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2311 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2312 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2313 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2314 * Returns: 0 on success, -1 on failure
2316 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2324 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2329 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2330 wpa_ie, *wpa_ie_len);
2332 if (sm->assoc_wpa_ie == NULL) {
2334 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2335 * the correct version of the IE even if PMKSA caching is
2336 * aborted (which would remove PMKID from IE generation).
2338 sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2339 if (sm->assoc_wpa_ie == NULL)
2342 os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2343 sm->assoc_wpa_ie_len = *wpa_ie_len;
2351 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2352 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2353 * @ie: Pointer to IE data (starting from id)
2355 * Returns: 0 on success, -1 on failure
2357 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2358 * Request frame. The IE will be used to override the default value generated
2359 * with wpa_sm_set_assoc_wpa_ie_default().
2361 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2366 os_free(sm->assoc_wpa_ie);
2367 if (ie == NULL || len == 0) {
2368 wpa_printf(MSG_DEBUG, "WPA: clearing own WPA/RSN IE");
2369 sm->assoc_wpa_ie = NULL;
2370 sm->assoc_wpa_ie_len = 0;
2372 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2373 sm->assoc_wpa_ie = os_malloc(len);
2374 if (sm->assoc_wpa_ie == NULL)
2377 os_memcpy(sm->assoc_wpa_ie, ie, len);
2378 sm->assoc_wpa_ie_len = len;
2386 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2387 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2388 * @ie: Pointer to IE data (starting from id)
2390 * Returns: 0 on success, -1 on failure
2392 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2395 int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2400 os_free(sm->ap_wpa_ie);
2401 if (ie == NULL || len == 0) {
2402 wpa_printf(MSG_DEBUG, "WPA: clearing AP WPA IE");
2403 sm->ap_wpa_ie = NULL;
2404 sm->ap_wpa_ie_len = 0;
2406 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2407 sm->ap_wpa_ie = os_malloc(len);
2408 if (sm->ap_wpa_ie == NULL)
2411 os_memcpy(sm->ap_wpa_ie, ie, len);
2412 sm->ap_wpa_ie_len = len;
2420 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2421 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2422 * @ie: Pointer to IE data (starting from id)
2424 * Returns: 0 on success, -1 on failure
2426 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2429 int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2434 os_free(sm->ap_rsn_ie);
2435 if (ie == NULL || len == 0) {
2436 wpa_printf(MSG_DEBUG, "WPA: clearing AP RSN IE");
2437 sm->ap_rsn_ie = NULL;
2438 sm->ap_rsn_ie_len = 0;
2440 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2441 sm->ap_rsn_ie = os_malloc(len);
2442 if (sm->ap_rsn_ie == NULL)
2445 os_memcpy(sm->ap_rsn_ie, ie, len);
2446 sm->ap_rsn_ie_len = len;
2454 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2455 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2456 * @data: Pointer to data area for parsing results
2457 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2459 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2460 * parsed data into data.
2462 int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2464 if (sm == NULL || sm->assoc_wpa_ie == NULL) {
2465 wpa_printf(MSG_DEBUG, "WPA: No WPA/RSN IE available from "
2466 "association info");
2469 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
2475 int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
2477 #ifndef CONFIG_NO_WPA2
2478 return pmksa_cache_list(sm->pmksa, buf, len);
2479 #else /* CONFIG_NO_WPA2 */
2481 #endif /* CONFIG_NO_WPA2 */
2485 void wpa_sm_drop_sa(struct wpa_sm *sm)
2487 wpa_printf(MSG_DEBUG, "WPA: Clear old PMK and PTK");
2490 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2491 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2492 os_memset(&sm->tptk, 0, sizeof(sm->tptk));