2 * WPA Supplicant - WPA state machine and EAPOL-Key processing
3 * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "crypto/aes_wrap.h"
13 #include "crypto/crypto.h"
14 #include "crypto/random.h"
15 #include "common/ieee802_11_defs.h"
16 #include "eapol_supp/eapol_supp_sm.h"
20 #include "pmksa_cache.h"
27 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
28 * @sm: Pointer to WPA state machine data from wpa_sm_init()
29 * @kck: Key Confirmation Key (KCK, part of PTK)
30 * @ver: Version field from Key Info
31 * @dest: Destination address for the frame
32 * @proto: Ethertype (usually ETH_P_EAPOL)
33 * @msg: EAPOL-Key message
34 * @msg_len: Length of message
35 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
37 void wpa_eapol_key_send(struct wpa_sm *sm, const u8 *kck,
38 int ver, const u8 *dest, u16 proto,
39 u8 *msg, size_t msg_len, u8 *key_mic)
41 if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
43 * Association event was not yet received; try to fetch
44 * BSSID from the driver.
46 if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
47 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
48 "WPA: Failed to read BSSID for "
49 "EAPOL-Key destination address");
52 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
53 "WPA: Use BSSID (" MACSTR
54 ") as the destination for EAPOL-Key",
59 wpa_eapol_key_mic(kck, ver, msg, msg_len, key_mic)) {
60 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
61 "WPA: Failed to generate EAPOL-Key "
62 "version %d MIC", ver);
65 wpa_hexdump_key(MSG_DEBUG, "WPA: KCK", kck, 16);
66 wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC", key_mic, 16);
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 (sm->key_mgmt == WPA_KEY_MGMT_OSEN)
93 ver = WPA_KEY_INFO_TYPE_AKM_DEFINED;
94 else if (wpa_key_mgmt_ft(sm->key_mgmt) ||
95 wpa_key_mgmt_sha256(sm->key_mgmt))
96 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
97 else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
98 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
100 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
102 if (wpa_sm_get_bssid(sm, bssid) < 0) {
103 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
104 "Failed to read BSSID for EAPOL-Key request");
108 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
109 sizeof(*reply), &rlen, (void *) &reply);
113 reply->type = (sm->proto == WPA_PROTO_RSN ||
114 sm->proto == WPA_PROTO_OSEN) ?
115 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
116 key_info = WPA_KEY_INFO_REQUEST | ver;
118 key_info |= WPA_KEY_INFO_MIC;
120 key_info |= WPA_KEY_INFO_ERROR;
122 key_info |= WPA_KEY_INFO_KEY_TYPE;
123 WPA_PUT_BE16(reply->key_info, key_info);
124 WPA_PUT_BE16(reply->key_length, 0);
125 os_memcpy(reply->replay_counter, sm->request_counter,
126 WPA_REPLAY_COUNTER_LEN);
127 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
129 WPA_PUT_BE16(reply->key_data_length, 0);
131 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
132 "WPA: Sending EAPOL-Key Request (error=%d "
133 "pairwise=%d ptk_set=%d len=%lu)",
134 error, pairwise, sm->ptk_set, (unsigned long) rlen);
135 wpa_eapol_key_send(sm, sm->ptk.kck, ver, bssid, ETH_P_EAPOL,
136 rbuf, rlen, key_info & WPA_KEY_INFO_MIC ?
137 reply->key_mic : NULL);
141 static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
142 const unsigned char *src_addr,
145 int abort_cached = 0;
147 if (pmkid && !sm->cur_pmksa) {
148 /* When using drivers that generate RSN IE, wpa_supplicant may
149 * not have enough time to get the association information
150 * event before receiving this 1/4 message, so try to find a
151 * matching PMKSA cache entry here. */
152 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid,
155 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
156 "RSN: found matching PMKID from PMKSA cache");
158 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
159 "RSN: no matching PMKID found");
164 if (pmkid && sm->cur_pmksa &&
165 os_memcmp(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
166 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
167 wpa_sm_set_pmk_from_pmksa(sm);
168 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
169 sm->pmk, sm->pmk_len);
170 eapol_sm_notify_cached(sm->eapol);
171 #ifdef CONFIG_IEEE80211R
173 #endif /* CONFIG_IEEE80211R */
174 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
177 res = eapol_sm_get_key(sm->eapol, sm->pmk, PMK_LEN);
180 * EAP-LEAP is an exception from other EAP methods: it
181 * uses only 16-byte PMK.
183 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
186 #ifdef CONFIG_IEEE80211R
188 if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
190 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
191 sm->xxkey_len = PMK_LEN;
192 os_memset(buf, 0, sizeof(buf));
194 #endif /* CONFIG_IEEE80211R */
197 struct rsn_pmksa_cache_entry *sa = NULL;
198 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
199 "machines", sm->pmk, pmk_len);
200 sm->pmk_len = pmk_len;
201 if (sm->proto == WPA_PROTO_RSN &&
202 !wpa_key_mgmt_ft(sm->key_mgmt)) {
203 sa = pmksa_cache_add(sm->pmksa,
205 src_addr, sm->own_addr,
209 if (!sm->cur_pmksa && pmkid &&
210 pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL))
212 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
213 "RSN: the new PMK matches with the "
221 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
222 "WPA: Failed to get master session key from "
223 "EAPOL state machines - key handshake "
226 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
227 "RSN: Cancelled PMKSA caching "
229 sm->cur_pmksa = NULL;
231 } else if (!abort_cached) {
237 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
238 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
240 /* Send EAPOL-Start to trigger full EAP authentication. */
244 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
245 "RSN: no PMKSA entry found - trigger "
246 "full EAP authentication");
247 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
248 NULL, 0, &buflen, NULL);
250 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
264 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
265 * @sm: Pointer to WPA state machine data from wpa_sm_init()
266 * @dst: Destination address for the frame
267 * @key: Pointer to the EAPOL-Key frame header
268 * @ver: Version bits from EAPOL-Key Key Info
269 * @nonce: Nonce value for the EAPOL-Key frame
270 * @wpa_ie: WPA/RSN IE
271 * @wpa_ie_len: Length of the WPA/RSN IE
272 * @ptk: PTK to use for keyed hash and encryption
273 * Returns: 0 on success, -1 on failure
275 int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
276 const struct wpa_eapol_key *key,
277 int ver, const u8 *nonce,
278 const u8 *wpa_ie, size_t wpa_ie_len,
282 struct wpa_eapol_key *reply;
284 u8 *rsn_ie_buf = NULL;
286 if (wpa_ie == NULL) {
287 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
288 "cannot generate msg 2/4");
292 #ifdef CONFIG_IEEE80211R
293 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
297 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
298 * FTIE from (Re)Association Response.
300 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
301 sm->assoc_resp_ies_len);
302 if (rsn_ie_buf == NULL)
304 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
305 res = wpa_insert_pmkid(rsn_ie_buf, wpa_ie_len,
313 if (sm->assoc_resp_ies) {
314 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
315 sm->assoc_resp_ies_len);
316 wpa_ie_len += sm->assoc_resp_ies_len;
321 #endif /* CONFIG_IEEE80211R */
323 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
325 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
326 NULL, sizeof(*reply) + wpa_ie_len,
327 &rlen, (void *) &reply);
333 reply->type = (sm->proto == WPA_PROTO_RSN ||
334 sm->proto == WPA_PROTO_OSEN) ?
335 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
336 WPA_PUT_BE16(reply->key_info,
337 ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC);
338 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
339 WPA_PUT_BE16(reply->key_length, 0);
341 os_memcpy(reply->key_length, key->key_length, 2);
342 os_memcpy(reply->replay_counter, key->replay_counter,
343 WPA_REPLAY_COUNTER_LEN);
344 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
345 WPA_REPLAY_COUNTER_LEN);
347 WPA_PUT_BE16(reply->key_data_length, wpa_ie_len);
348 os_memcpy(reply + 1, wpa_ie, wpa_ie_len);
351 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
353 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
354 wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
355 rbuf, rlen, reply->key_mic);
361 static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
362 const struct wpa_eapol_key *key,
365 size_t ptk_len = wpa_cipher_key_len(sm->pairwise_cipher) + 32;
366 #ifdef CONFIG_IEEE80211R
367 if (wpa_key_mgmt_ft(sm->key_mgmt))
368 return wpa_derive_ptk_ft(sm, src_addr, key, ptk, ptk_len);
369 #endif /* CONFIG_IEEE80211R */
371 wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
372 sm->own_addr, sm->bssid, sm->snonce, key->key_nonce,
374 wpa_key_mgmt_sha256(sm->key_mgmt));
379 static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
380 const unsigned char *src_addr,
381 const struct wpa_eapol_key *key,
382 u16 ver, const u8 *key_data,
385 struct wpa_eapol_ie_parse ie;
388 u8 *kde, *kde_buf = NULL;
391 if (wpa_sm_get_network_ctx(sm) == NULL) {
392 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
393 "found (msg 1 of 4)");
397 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
398 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of 4-Way "
399 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
401 os_memset(&ie, 0, sizeof(ie));
403 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
404 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
405 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
406 key_data, key_data_len);
407 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
410 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
411 "Authenticator", ie.pmkid, PMKID_LEN);
415 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
417 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
418 "msg 1/4 - requesting full EAP authentication");
424 if (sm->renew_snonce) {
425 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
426 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
427 "WPA: Failed to get random data for SNonce");
430 sm->renew_snonce = 0;
431 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
432 sm->snonce, WPA_NONCE_LEN);
435 /* Calculate PTK which will be stored as a temporary PTK until it has
436 * been verified when processing message 3/4. */
438 wpa_derive_ptk(sm, src_addr, key, ptk);
439 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
441 /* Supplicant: swap tx/rx Mic keys */
442 os_memcpy(buf, ptk->u.auth.tx_mic_key, 8);
443 os_memcpy(ptk->u.auth.tx_mic_key, ptk->u.auth.rx_mic_key, 8);
444 os_memcpy(ptk->u.auth.rx_mic_key, buf, 8);
445 os_memset(buf, 0, sizeof(buf));
449 kde = sm->assoc_wpa_ie;
450 kde_len = sm->assoc_wpa_ie_len;
454 kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 1);
457 wpa_printf(MSG_DEBUG, "P2P: Add IP Address Request KDE "
458 "into EAPOL-Key 2/4");
459 os_memcpy(kde_buf, kde, kde_len);
462 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
463 *pos++ = RSN_SELECTOR_LEN + 1;
464 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
465 pos += RSN_SELECTOR_LEN;
470 #endif /* CONFIG_P2P */
472 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
477 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
482 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
486 static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
488 struct wpa_sm *sm = eloop_ctx;
489 rsn_preauth_candidate_process(sm);
493 static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
494 const u8 *addr, int secure)
496 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
497 "WPA: Key negotiation completed with "
498 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
499 wpa_cipher_txt(sm->pairwise_cipher),
500 wpa_cipher_txt(sm->group_cipher));
501 wpa_sm_cancel_auth_timeout(sm);
502 wpa_sm_set_state(sm, WPA_COMPLETED);
505 wpa_sm_mlme_setprotection(
506 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
507 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
508 eapol_sm_notify_portValid(sm->eapol, TRUE);
509 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt))
510 eapol_sm_notify_eap_success(sm->eapol, TRUE);
512 * Start preauthentication after a short wait to avoid a
513 * possible race condition between the data receive and key
514 * configuration after the 4-Way Handshake. This increases the
515 * likelihood of the first preauth EAPOL-Start frame getting to
518 eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
521 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
522 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
523 "RSN: Authenticator accepted "
524 "opportunistic PMKSA entry - marking it valid");
525 sm->cur_pmksa->opportunistic = 0;
528 #ifdef CONFIG_IEEE80211R
529 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
530 /* Prepare for the next transition */
531 wpa_ft_prepare_auth_request(sm, NULL);
533 #endif /* CONFIG_IEEE80211R */
537 static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
539 struct wpa_sm *sm = eloop_ctx;
540 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
541 wpa_sm_key_request(sm, 0, 1);
545 static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
546 const struct wpa_eapol_key *key)
551 u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
553 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
554 "WPA: Installing PTK to the driver");
556 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
557 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
558 "Suite: NONE - do not use pairwise keys");
562 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
563 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
564 "WPA: Unsupported pairwise cipher %d",
565 sm->pairwise_cipher);
569 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
570 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
571 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
573 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
576 key_rsc = key->key_rsc;
577 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
580 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
581 (u8 *) sm->ptk.tk1, keylen) < 0) {
582 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
583 "WPA: Failed to set PTK to the "
584 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
585 alg, keylen, MAC2STR(sm->bssid));
589 if (sm->wpa_ptk_rekey) {
590 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
591 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
599 static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
601 int keylen, int maxkeylen,
607 *alg = wpa_cipher_to_alg(group_cipher);
608 if (*alg == WPA_ALG_NONE) {
609 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
610 "WPA: Unsupported Group Cipher %d",
614 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
616 klen = wpa_cipher_key_len(group_cipher);
617 if (keylen != klen || maxkeylen < klen) {
618 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
619 "WPA: Unsupported %s Group Cipher key length %d (%d)",
620 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
627 struct wpa_gtk_data {
629 int tx, key_rsc_len, keyidx;
635 static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
636 const struct wpa_gtk_data *gd,
639 const u8 *_gtk = gd->gtk;
642 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
643 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
644 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
645 gd->keyidx, gd->tx, gd->gtk_len);
646 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
647 if (sm->group_cipher == WPA_CIPHER_TKIP) {
648 /* Swap Tx/Rx keys for Michael MIC */
649 os_memcpy(gtk_buf, gd->gtk, 16);
650 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
651 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
654 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
655 if (wpa_sm_set_key(sm, gd->alg, NULL,
656 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
657 _gtk, gd->gtk_len) < 0) {
658 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
659 "WPA: Failed to set GTK to the driver "
661 os_memset(gtk_buf, 0, sizeof(gtk_buf));
664 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
665 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
666 _gtk, gd->gtk_len) < 0) {
667 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
668 "WPA: Failed to set GTK to "
669 "the driver (alg=%d keylen=%d keyidx=%d)",
670 gd->alg, gd->gtk_len, gd->keyidx);
671 os_memset(gtk_buf, 0, sizeof(gtk_buf));
674 os_memset(gtk_buf, 0, sizeof(gtk_buf));
680 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
683 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
684 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
685 * seemed to set this bit (incorrectly, since Tx is only when
686 * doing Group Key only APs) and without this workaround, the
687 * data connection does not work because wpa_supplicant
688 * configured non-zero keyidx to be used for unicast. */
689 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
690 "WPA: Tx bit set for GTK, but pairwise "
691 "keys are used - ignore Tx bit");
698 static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
699 const struct wpa_eapol_key *key,
700 const u8 *gtk, size_t gtk_len,
703 struct wpa_gtk_data gd;
706 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
708 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
709 * Reserved [bits 0-7]
713 os_memset(&gd, 0, sizeof(gd));
714 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
717 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
720 gd.keyidx = gtk[0] & 0x3;
721 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
722 !!(gtk[0] & BIT(2)));
726 os_memcpy(gd.gtk, gtk, gtk_len);
727 gd.gtk_len = gtk_len;
729 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
730 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
732 &gd.key_rsc_len, &gd.alg) ||
733 wpa_supplicant_install_gtk(sm, &gd, key->key_rsc))) {
734 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
735 "RSN: Failed to install GTK");
736 os_memset(&gd, 0, sizeof(gd));
739 os_memset(&gd, 0, sizeof(gd));
741 wpa_supplicant_key_neg_complete(sm, sm->bssid,
742 key_info & WPA_KEY_INFO_SECURE);
747 static int ieee80211w_set_keys(struct wpa_sm *sm,
748 struct wpa_eapol_ie_parse *ie)
750 #ifdef CONFIG_IEEE80211W
751 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
756 const struct wpa_igtk_kde *igtk;
758 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
759 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
761 igtk = (const struct wpa_igtk_kde *) ie->igtk;
762 keyidx = WPA_GET_LE16(igtk->keyid);
763 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: IGTK keyid %d "
764 "pn %02x%02x%02x%02x%02x%02x",
765 keyidx, MAC2STR(igtk->pn));
766 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK",
769 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
770 "WPA: Invalid IGTK KeyID %d", keyidx);
773 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
774 broadcast_ether_addr,
775 keyidx, 0, igtk->pn, sizeof(igtk->pn),
776 igtk->igtk, len) < 0) {
777 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
778 "WPA: Failed to configure IGTK to the driver");
784 #else /* CONFIG_IEEE80211W */
786 #endif /* CONFIG_IEEE80211W */
790 static void wpa_report_ie_mismatch(struct wpa_sm *sm,
791 const char *reason, const u8 *src_addr,
792 const u8 *wpa_ie, size_t wpa_ie_len,
793 const u8 *rsn_ie, size_t rsn_ie_len)
795 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
796 reason, MAC2STR(src_addr));
799 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
800 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
803 if (!sm->ap_wpa_ie) {
804 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
805 "WPA: No WPA IE in Beacon/ProbeResp");
807 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
812 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
813 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
816 if (!sm->ap_rsn_ie) {
817 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
818 "WPA: No RSN IE in Beacon/ProbeResp");
820 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
824 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
828 #ifdef CONFIG_IEEE80211R
830 static int ft_validate_mdie(struct wpa_sm *sm,
831 const unsigned char *src_addr,
832 struct wpa_eapol_ie_parse *ie,
833 const u8 *assoc_resp_mdie)
835 struct rsn_mdie *mdie;
837 mdie = (struct rsn_mdie *) (ie->mdie + 2);
838 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
839 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
840 MOBILITY_DOMAIN_ID_LEN) != 0) {
841 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
842 "not match with the current mobility domain");
846 if (assoc_resp_mdie &&
847 (assoc_resp_mdie[1] != ie->mdie[1] ||
848 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
849 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
850 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
851 ie->mdie, 2 + ie->mdie[1]);
852 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
853 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
861 static int ft_validate_ftie(struct wpa_sm *sm,
862 const unsigned char *src_addr,
863 struct wpa_eapol_ie_parse *ie,
864 const u8 *assoc_resp_ftie)
866 if (ie->ftie == NULL) {
867 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
868 "FT: No FTIE in EAPOL-Key msg 3/4");
872 if (assoc_resp_ftie == NULL)
875 if (assoc_resp_ftie[1] != ie->ftie[1] ||
876 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
877 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
878 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
879 ie->ftie, 2 + ie->ftie[1]);
880 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
881 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
889 static int ft_validate_rsnie(struct wpa_sm *sm,
890 const unsigned char *src_addr,
891 struct wpa_eapol_ie_parse *ie)
893 struct wpa_ie_data rsn;
899 * Verify that PMKR1Name from EAPOL-Key message 3/4
900 * matches with the value we derived.
902 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
903 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
904 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
905 "FT 4-way handshake message 3/4");
909 if (os_memcmp(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0) {
910 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
911 "FT: PMKR1Name mismatch in "
912 "FT 4-way handshake message 3/4");
913 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
914 rsn.pmkid, WPA_PMK_NAME_LEN);
915 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
916 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
924 static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
925 const unsigned char *src_addr,
926 struct wpa_eapol_ie_parse *ie)
928 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
930 if (sm->assoc_resp_ies) {
931 pos = sm->assoc_resp_ies;
932 end = pos + sm->assoc_resp_ies_len;
933 while (pos + 2 < end) {
934 if (pos + 2 + pos[1] > end)
937 case WLAN_EID_MOBILITY_DOMAIN:
940 case WLAN_EID_FAST_BSS_TRANSITION:
948 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
949 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
950 ft_validate_rsnie(sm, src_addr, ie) < 0)
956 #endif /* CONFIG_IEEE80211R */
959 static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
960 const unsigned char *src_addr,
961 struct wpa_eapol_ie_parse *ie)
963 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
964 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
965 "WPA: No WPA/RSN IE for this AP known. "
966 "Trying to get from scan results");
967 if (wpa_sm_get_beacon_ie(sm) < 0) {
968 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
969 "WPA: Could not find AP from "
972 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
973 "WPA: Found the current AP from "
974 "updated scan results");
978 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
979 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
980 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
981 "with IE in Beacon/ProbeResp (no IE?)",
982 src_addr, ie->wpa_ie, ie->wpa_ie_len,
983 ie->rsn_ie, ie->rsn_ie_len);
987 if ((ie->wpa_ie && sm->ap_wpa_ie &&
988 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
989 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
990 (ie->rsn_ie && sm->ap_rsn_ie &&
991 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
992 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
993 ie->rsn_ie, ie->rsn_ie_len))) {
994 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
995 "with IE in Beacon/ProbeResp",
996 src_addr, ie->wpa_ie, ie->wpa_ie_len,
997 ie->rsn_ie, ie->rsn_ie_len);
1001 if (sm->proto == WPA_PROTO_WPA &&
1002 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1003 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1004 "detected - RSN was enabled and RSN IE "
1005 "was in msg 3/4, but not in "
1007 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1008 ie->rsn_ie, ie->rsn_ie_len);
1012 #ifdef CONFIG_IEEE80211R
1013 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1014 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1016 #endif /* CONFIG_IEEE80211R */
1023 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1024 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1025 * @dst: Destination address for the frame
1026 * @key: Pointer to the EAPOL-Key frame header
1027 * @ver: Version bits from EAPOL-Key Key Info
1028 * @key_info: Key Info
1029 * @ptk: PTK to use for keyed hash and encryption
1030 * Returns: 0 on success, -1 on failure
1032 int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1033 const struct wpa_eapol_key *key,
1034 u16 ver, u16 key_info,
1035 struct wpa_ptk *ptk)
1038 struct wpa_eapol_key *reply;
1041 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1042 sizeof(*reply), &rlen, (void *) &reply);
1046 reply->type = (sm->proto == WPA_PROTO_RSN ||
1047 sm->proto == WPA_PROTO_OSEN) ?
1048 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1049 key_info &= WPA_KEY_INFO_SECURE;
1050 key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
1051 WPA_PUT_BE16(reply->key_info, key_info);
1052 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
1053 WPA_PUT_BE16(reply->key_length, 0);
1055 os_memcpy(reply->key_length, key->key_length, 2);
1056 os_memcpy(reply->replay_counter, key->replay_counter,
1057 WPA_REPLAY_COUNTER_LEN);
1059 WPA_PUT_BE16(reply->key_data_length, 0);
1061 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
1062 wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
1063 rbuf, rlen, reply->key_mic);
1069 static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1070 const struct wpa_eapol_key *key,
1071 u16 ver, const u8 *key_data,
1072 size_t key_data_len)
1074 u16 key_info, keylen;
1075 struct wpa_eapol_ie_parse ie;
1077 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
1078 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way "
1079 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1081 key_info = WPA_GET_BE16(key->key_info);
1083 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1084 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
1086 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1087 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1088 "WPA: GTK IE in unencrypted key data");
1091 #ifdef CONFIG_IEEE80211W
1092 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1093 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1094 "WPA: IGTK KDE in unencrypted key data");
1099 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1100 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1101 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
1102 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1103 "WPA: Invalid IGTK KDE length %lu",
1104 (unsigned long) ie.igtk_len);
1107 #endif /* CONFIG_IEEE80211W */
1109 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1112 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1113 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1114 "WPA: ANonce from message 1 of 4-Way Handshake "
1115 "differs from 3 of 4-Way Handshake - drop packet (src="
1116 MACSTR ")", MAC2STR(sm->bssid));
1120 keylen = WPA_GET_BE16(key->key_length);
1121 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1122 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1123 "WPA: Invalid %s key length %d (src=" MACSTR
1124 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1125 MAC2STR(sm->bssid));
1130 if (ie.ip_addr_alloc) {
1131 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1132 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1133 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1135 #endif /* CONFIG_P2P */
1137 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
1142 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1143 * for the next 4-Way Handshake. If msg 3 is received again, the old
1144 * SNonce will still be used to avoid changing PTK. */
1145 sm->renew_snonce = 1;
1147 if (key_info & WPA_KEY_INFO_INSTALL) {
1148 if (wpa_supplicant_install_ptk(sm, key))
1152 if (key_info & WPA_KEY_INFO_SECURE) {
1153 wpa_sm_mlme_setprotection(
1154 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1155 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1156 eapol_sm_notify_portValid(sm->eapol, TRUE);
1158 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1160 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
1161 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1162 key_info & WPA_KEY_INFO_SECURE);
1163 } else if (ie.gtk &&
1164 wpa_supplicant_pairwise_gtk(sm, key,
1165 ie.gtk, ie.gtk_len, key_info) < 0) {
1166 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1167 "RSN: Failed to configure GTK");
1171 if (ieee80211w_set_keys(sm, &ie) < 0) {
1172 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1173 "RSN: Failed to configure IGTK");
1178 wpa_sm_set_rekey_offload(sm);
1183 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1187 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1191 struct wpa_gtk_data *gd)
1194 struct wpa_eapol_ie_parse ie;
1196 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
1197 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1199 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1200 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1201 "WPA: GTK IE in unencrypted key data");
1204 if (ie.gtk == NULL) {
1205 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1206 "WPA: No GTK IE in Group Key msg 1/2");
1209 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1211 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1212 gd->gtk_len, maxkeylen,
1213 &gd->key_rsc_len, &gd->alg))
1216 wpa_hexdump(MSG_DEBUG, "RSN: received GTK in group key handshake",
1217 ie.gtk, ie.gtk_len);
1218 gd->keyidx = ie.gtk[0] & 0x3;
1219 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1220 !!(ie.gtk[0] & BIT(2)));
1221 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1222 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1223 "RSN: Too long GTK in GTK IE (len=%lu)",
1224 (unsigned long) ie.gtk_len - 2);
1227 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1229 if (ieee80211w_set_keys(sm, &ie) < 0)
1230 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1231 "RSN: Failed to configure IGTK");
1237 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1238 const struct wpa_eapol_key *key,
1240 size_t key_data_len, u16 key_info,
1241 u16 ver, struct wpa_gtk_data *gd)
1245 gd->gtk_len = WPA_GET_BE16(key->key_length);
1246 maxkeylen = key_data_len;
1247 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1248 if (maxkeylen < 8) {
1249 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1250 "WPA: Too short maxkeylen (%lu)",
1251 (unsigned long) maxkeylen);
1257 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1258 gd->gtk_len, maxkeylen,
1259 &gd->key_rsc_len, &gd->alg))
1262 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1263 WPA_KEY_INFO_KEY_INDEX_SHIFT;
1264 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1266 if (key_data_len > sizeof(gd->gtk)) {
1267 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1268 "WPA: RC4 key data too long (%lu)",
1269 (unsigned long) key_data_len);
1272 os_memcpy(ek, key->key_iv, 16);
1273 os_memcpy(ek + 16, sm->ptk.kek, 16);
1274 os_memcpy(gd->gtk, key_data, key_data_len);
1275 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
1276 os_memset(ek, 0, sizeof(ek));
1277 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1281 os_memset(ek, 0, sizeof(ek));
1282 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1283 if (maxkeylen % 8) {
1284 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1285 "WPA: Unsupported AES-WRAP len %lu",
1286 (unsigned long) maxkeylen);
1289 if (maxkeylen > sizeof(gd->gtk)) {
1290 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1291 "WPA: AES-WRAP key data "
1292 "too long (keydatalen=%lu maxkeylen=%lu)",
1293 (unsigned long) key_data_len,
1294 (unsigned long) maxkeylen);
1297 if (aes_unwrap(sm->ptk.kek, maxkeylen / 8, key_data, gd->gtk)) {
1298 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1299 "WPA: AES unwrap failed - could not decrypt "
1304 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1305 "WPA: Unsupported key_info type %d", ver);
1308 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1309 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1314 static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1315 const struct wpa_eapol_key *key,
1316 int ver, u16 key_info)
1319 struct wpa_eapol_key *reply;
1322 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1323 sizeof(*reply), &rlen, (void *) &reply);
1327 reply->type = (sm->proto == WPA_PROTO_RSN ||
1328 sm->proto == WPA_PROTO_OSEN) ?
1329 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1330 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1331 key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
1332 WPA_PUT_BE16(reply->key_info, key_info);
1333 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
1334 WPA_PUT_BE16(reply->key_length, 0);
1336 os_memcpy(reply->key_length, key->key_length, 2);
1337 os_memcpy(reply->replay_counter, key->replay_counter,
1338 WPA_REPLAY_COUNTER_LEN);
1340 WPA_PUT_BE16(reply->key_data_length, 0);
1342 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
1343 wpa_eapol_key_send(sm, sm->ptk.kck, ver, sm->bssid, ETH_P_EAPOL,
1344 rbuf, rlen, reply->key_mic);
1350 static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1351 const unsigned char *src_addr,
1352 const struct wpa_eapol_key *key,
1354 size_t key_data_len, u16 ver)
1358 struct wpa_gtk_data gd;
1360 os_memset(&gd, 0, sizeof(gd));
1362 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1363 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1364 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1366 key_info = WPA_GET_BE16(key->key_info);
1368 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
1369 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
1370 key_data_len, key_info,
1373 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
1375 key_info, ver, &gd);
1378 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1383 if (wpa_supplicant_install_gtk(sm, &gd, key->key_rsc) ||
1384 wpa_supplicant_send_2_of_2(sm, key, ver, key_info))
1388 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
1389 "completed with " MACSTR " [GTK=%s]",
1390 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1391 wpa_sm_cancel_auth_timeout(sm);
1392 wpa_sm_set_state(sm, WPA_COMPLETED);
1394 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1396 WPA_KEY_INFO_SECURE);
1399 wpa_sm_set_rekey_offload(sm);
1404 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1408 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
1409 struct wpa_eapol_key *key,
1411 const u8 *buf, size_t len)
1416 os_memcpy(mic, key->key_mic, 16);
1418 os_memset(key->key_mic, 0, 16);
1419 wpa_eapol_key_mic(sm->tptk.kck, ver, buf, len,
1421 if (os_memcmp(mic, key->key_mic, 16) != 0) {
1422 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1423 "WPA: Invalid EAPOL-Key MIC "
1424 "when using TPTK - ignoring TPTK");
1429 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
1430 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
1434 if (!ok && sm->ptk_set) {
1435 os_memset(key->key_mic, 0, 16);
1436 wpa_eapol_key_mic(sm->ptk.kck, ver, buf, len,
1438 if (os_memcmp(mic, key->key_mic, 16) != 0) {
1439 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1440 "WPA: Invalid EAPOL-Key MIC - "
1448 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1449 "WPA: Could not verify EAPOL-Key MIC - "
1454 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1455 WPA_REPLAY_COUNTER_LEN);
1456 sm->rx_replay_counter_set = 1;
1461 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1462 static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
1463 struct wpa_eapol_key *key, u16 ver,
1464 u8 *key_data, size_t *key_data_len)
1466 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
1467 key_data, *key_data_len);
1469 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1470 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1475 /* Decrypt key data here so that this operation does not need
1476 * to be implemented separately for each message type. */
1477 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1479 os_memcpy(ek, key->key_iv, 16);
1480 os_memcpy(ek + 16, sm->ptk.kek, 16);
1481 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
1482 os_memset(ek, 0, sizeof(ek));
1483 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1487 os_memset(ek, 0, sizeof(ek));
1488 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1489 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
1490 sm->key_mgmt == WPA_KEY_MGMT_OSEN) {
1492 if (*key_data_len < 8 || *key_data_len % 8) {
1493 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1494 "WPA: Unsupported AES-WRAP len %u",
1495 (unsigned int) *key_data_len);
1498 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
1499 buf = os_malloc(*key_data_len);
1501 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1502 "WPA: No memory for AES-UNWRAP buffer");
1505 if (aes_unwrap(sm->ptk.kek, *key_data_len / 8,
1508 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1509 "WPA: AES unwrap failed - "
1510 "could not decrypt EAPOL-Key key data");
1513 os_memcpy(key_data, buf, *key_data_len);
1515 WPA_PUT_BE16(key->key_data_length, *key_data_len);
1517 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1518 "WPA: Unsupported key_info type %d", ver);
1521 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
1522 key_data, *key_data_len);
1528 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1529 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1531 void wpa_sm_aborted_cached(struct wpa_sm *sm)
1533 if (sm && sm->cur_pmksa) {
1534 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1535 "RSN: Cancelling PMKSA caching attempt");
1536 sm->cur_pmksa = NULL;
1541 static void wpa_eapol_key_dump(struct wpa_sm *sm,
1542 const struct wpa_eapol_key *key)
1544 #ifndef CONFIG_NO_STDOUT_DEBUG
1545 u16 key_info = WPA_GET_BE16(key->key_info);
1547 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1548 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1549 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
1550 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1551 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1552 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1553 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1554 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1555 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1556 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1557 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1558 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1559 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1560 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1561 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1562 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1563 " key_length=%u key_data_length=%u",
1564 WPA_GET_BE16(key->key_length),
1565 WPA_GET_BE16(key->key_data_length));
1566 wpa_hexdump(MSG_DEBUG, " replay_counter",
1567 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1568 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1569 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1570 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1571 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
1572 wpa_hexdump(MSG_DEBUG, " key_mic", key->key_mic, 16);
1573 #endif /* CONFIG_NO_STDOUT_DEBUG */
1578 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1579 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1580 * @src_addr: Source MAC address of the EAPOL packet
1581 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1582 * @len: Length of the EAPOL frame
1583 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1585 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1586 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1587 * only processing WPA and WPA2 EAPOL-Key frames.
1589 * The received EAPOL-Key packets are validated and valid packets are replied
1590 * to. In addition, key material (PTK, GTK) is configured at the end of a
1591 * successful key handshake.
1593 int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1594 const u8 *buf, size_t len)
1596 size_t plen, data_len, key_data_len;
1597 const struct ieee802_1x_hdr *hdr;
1598 struct wpa_eapol_key *key;
1602 struct wpa_peerkey *peerkey = NULL;
1605 #ifdef CONFIG_IEEE80211R
1606 sm->ft_completed = 0;
1607 #endif /* CONFIG_IEEE80211R */
1609 if (len < sizeof(*hdr) + sizeof(*key)) {
1610 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1611 "WPA: EAPOL frame too short to be a WPA "
1612 "EAPOL-Key (len %lu, expecting at least %lu)",
1613 (unsigned long) len,
1614 (unsigned long) sizeof(*hdr) + sizeof(*key));
1618 hdr = (const struct ieee802_1x_hdr *) buf;
1619 plen = be_to_host16(hdr->length);
1620 data_len = plen + sizeof(*hdr);
1621 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1622 "IEEE 802.1X RX: version=%d type=%d length=%lu",
1623 hdr->version, hdr->type, (unsigned long) plen);
1625 if (hdr->version < EAPOL_VERSION) {
1626 /* TODO: backwards compatibility */
1628 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
1629 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1630 "WPA: EAPOL frame (type %u) discarded, "
1631 "not a Key frame", hdr->type);
1635 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
1636 if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
1637 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1638 "WPA: EAPOL frame payload size %lu "
1639 "invalid (frame size %lu)",
1640 (unsigned long) plen, (unsigned long) len);
1644 if (data_len < len) {
1645 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1646 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
1647 (unsigned long) len - data_len);
1651 * Make a copy of the frame since we need to modify the buffer during
1652 * MAC validation and Key Data decryption.
1654 tmp = os_malloc(data_len);
1657 os_memcpy(tmp, buf, data_len);
1658 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
1659 key_data = (u8 *) (key + 1);
1661 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
1663 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1664 "WPA: EAPOL-Key type (%d) unknown, discarded",
1669 wpa_eapol_key_dump(sm, key);
1671 key_data_len = WPA_GET_BE16(key->key_data_length);
1672 if (key_data_len > plen - sizeof(struct wpa_eapol_key)) {
1673 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
1674 "frame - key_data overflow (%u > %u)",
1675 (unsigned int) key_data_len,
1676 (unsigned int) (plen - sizeof(struct wpa_eapol_key)));
1680 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
1681 key_info = WPA_GET_BE16(key->key_info);
1682 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1683 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
1684 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
1685 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1686 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
1687 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
1688 sm->key_mgmt != WPA_KEY_MGMT_OSEN) {
1689 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1690 "WPA: Unsupported EAPOL-Key descriptor version %d",
1695 if (sm->key_mgmt == WPA_KEY_MGMT_OSEN &&
1696 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1697 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1698 "OSEN: Unsupported EAPOL-Key descriptor version %d",
1703 #ifdef CONFIG_IEEE80211R
1704 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
1705 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
1706 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1707 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1708 "FT: AP did not use AES-128-CMAC");
1712 #endif /* CONFIG_IEEE80211R */
1713 #ifdef CONFIG_IEEE80211W
1714 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
1715 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1716 sm->key_mgmt != WPA_KEY_MGMT_OSEN) {
1717 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1718 "WPA: AP did not use the "
1719 "negotiated AES-128-CMAC");
1723 #endif /* CONFIG_IEEE80211W */
1724 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
1725 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1726 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1727 "WPA: CCMP is used, but EAPOL-Key "
1728 "descriptor version (%d) is not 2", ver);
1729 if (sm->group_cipher != WPA_CIPHER_CCMP &&
1730 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1731 /* Earlier versions of IEEE 802.11i did not explicitly
1732 * require version 2 descriptor for all EAPOL-Key
1733 * packets, so allow group keys to use version 1 if
1734 * CCMP is not used for them. */
1735 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1736 "WPA: Backwards compatibility: allow invalid "
1737 "version for non-CCMP group keys");
1740 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
1741 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1742 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1743 "WPA: GCMP is used, but EAPOL-Key "
1744 "descriptor version (%d) is not 2", ver);
1748 #ifdef CONFIG_PEERKEY
1749 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
1750 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
1754 if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
1755 if (!peerkey->initiator && peerkey->replay_counter_set &&
1756 os_memcmp(key->replay_counter, peerkey->replay_counter,
1757 WPA_REPLAY_COUNTER_LEN) <= 0) {
1758 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1759 "RSN: EAPOL-Key Replay Counter did not "
1760 "increase (STK) - dropping packet");
1762 } else if (peerkey->initiator) {
1763 u8 _tmp[WPA_REPLAY_COUNTER_LEN];
1764 os_memcpy(_tmp, key->replay_counter,
1765 WPA_REPLAY_COUNTER_LEN);
1766 inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
1767 if (os_memcmp(_tmp, peerkey->replay_counter,
1768 WPA_REPLAY_COUNTER_LEN) != 0) {
1769 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1770 "RSN: EAPOL-Key Replay "
1771 "Counter did not match (STK) - "
1778 if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
1779 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1780 "RSN: Ack bit in key_info from STK peer");
1783 #endif /* CONFIG_PEERKEY */
1785 if (!peerkey && sm->rx_replay_counter_set &&
1786 os_memcmp(key->replay_counter, sm->rx_replay_counter,
1787 WPA_REPLAY_COUNTER_LEN) <= 0) {
1788 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1789 "WPA: EAPOL-Key Replay Counter did not increase - "
1794 if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
1795 #ifdef CONFIG_PEERKEY
1796 && (peerkey == NULL || !peerkey->initiator)
1797 #endif /* CONFIG_PEERKEY */
1799 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1800 "WPA: No Ack bit in key_info");
1804 if (key_info & WPA_KEY_INFO_REQUEST) {
1805 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1806 "WPA: EAPOL-Key with Request bit - dropped");
1810 if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
1811 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
1814 #ifdef CONFIG_PEERKEY
1815 if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
1816 peerkey_verify_eapol_key_mic(sm, peerkey, key, ver, tmp, data_len))
1818 #endif /* CONFIG_PEERKEY */
1820 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
1821 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1822 if (wpa_supplicant_decrypt_key_data(sm, key, ver, key_data,
1827 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1828 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
1829 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1830 "WPA: Ignored EAPOL-Key (Pairwise) with "
1831 "non-zero key index");
1835 /* PeerKey 4-Way Handshake */
1836 peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver,
1837 key_data, key_data_len);
1838 } else if (key_info & WPA_KEY_INFO_MIC) {
1839 /* 3/4 4-Way Handshake */
1840 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
1843 /* 1/4 4-Way Handshake */
1844 wpa_supplicant_process_1_of_4(sm, src_addr, key,
1848 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
1849 /* PeerKey SMK Handshake */
1850 peerkey_rx_eapol_smk(sm, src_addr, key, key_data_len, key_info,
1853 if (key_info & WPA_KEY_INFO_MIC) {
1854 /* 1/2 Group Key Handshake */
1855 wpa_supplicant_process_1_of_2(sm, src_addr, key,
1856 key_data, key_data_len,
1859 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1860 "WPA: EAPOL-Key (Group) without Mic bit - "
1873 #ifdef CONFIG_CTRL_IFACE
1874 static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
1876 switch (sm->key_mgmt) {
1877 case WPA_KEY_MGMT_IEEE8021X:
1878 return ((sm->proto == WPA_PROTO_RSN ||
1879 sm->proto == WPA_PROTO_OSEN) ?
1880 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
1881 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
1882 case WPA_KEY_MGMT_PSK:
1883 return (sm->proto == WPA_PROTO_RSN ?
1884 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
1885 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
1886 #ifdef CONFIG_IEEE80211R
1887 case WPA_KEY_MGMT_FT_IEEE8021X:
1888 return RSN_AUTH_KEY_MGMT_FT_802_1X;
1889 case WPA_KEY_MGMT_FT_PSK:
1890 return RSN_AUTH_KEY_MGMT_FT_PSK;
1891 #endif /* CONFIG_IEEE80211R */
1892 #ifdef CONFIG_IEEE80211W
1893 case WPA_KEY_MGMT_IEEE8021X_SHA256:
1894 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
1895 case WPA_KEY_MGMT_PSK_SHA256:
1896 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
1897 #endif /* CONFIG_IEEE80211W */
1898 case WPA_KEY_MGMT_CCKM:
1899 return (sm->proto == WPA_PROTO_RSN ?
1900 RSN_AUTH_KEY_MGMT_CCKM:
1901 WPA_AUTH_KEY_MGMT_CCKM);
1902 case WPA_KEY_MGMT_WPA_NONE:
1903 return WPA_AUTH_KEY_MGMT_NONE;
1910 #define RSN_SUITE "%02x-%02x-%02x-%d"
1911 #define RSN_SUITE_ARG(s) \
1912 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
1915 * wpa_sm_get_mib - Dump text list of MIB entries
1916 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1917 * @buf: Buffer for the list
1918 * @buflen: Length of the buffer
1919 * Returns: Number of bytes written to buffer
1921 * This function is used fetch dot11 MIB variables.
1923 int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
1925 char pmkid_txt[PMKID_LEN * 2 + 1];
1929 if (sm->cur_pmksa) {
1930 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
1931 sm->cur_pmksa->pmkid, PMKID_LEN);
1933 pmkid_txt[0] = '\0';
1935 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
1936 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
1937 sm->proto == WPA_PROTO_RSN)
1942 ret = os_snprintf(buf, buflen,
1943 "dot11RSNAOptionImplemented=TRUE\n"
1944 "dot11RSNAPreauthenticationImplemented=TRUE\n"
1945 "dot11RSNAEnabled=%s\n"
1946 "dot11RSNAPreauthenticationEnabled=%s\n"
1947 "dot11RSNAConfigVersion=%d\n"
1948 "dot11RSNAConfigPairwiseKeysSupported=5\n"
1949 "dot11RSNAConfigGroupCipherSize=%d\n"
1950 "dot11RSNAConfigPMKLifetime=%d\n"
1951 "dot11RSNAConfigPMKReauthThreshold=%d\n"
1952 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
1953 "dot11RSNAConfigSATimeout=%d\n",
1954 rsna ? "TRUE" : "FALSE",
1955 rsna ? "TRUE" : "FALSE",
1957 wpa_cipher_key_len(sm->group_cipher) * 8,
1958 sm->dot11RSNAConfigPMKLifetime,
1959 sm->dot11RSNAConfigPMKReauthThreshold,
1960 sm->dot11RSNAConfigSATimeout);
1961 if (ret < 0 || (size_t) ret >= buflen)
1966 buf + len, buflen - len,
1967 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
1968 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
1969 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
1970 "dot11RSNAPMKIDUsed=%s\n"
1971 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
1972 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
1973 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
1974 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
1975 "dot11RSNA4WayHandshakeFailures=%u\n",
1976 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
1977 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
1978 sm->pairwise_cipher)),
1979 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
1982 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
1983 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
1984 sm->pairwise_cipher)),
1985 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
1987 sm->dot11RSNA4WayHandshakeFailures);
1988 if (ret >= 0 && (size_t) ret < buflen)
1993 #endif /* CONFIG_CTRL_IFACE */
1996 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
1997 void *ctx, enum pmksa_free_reason reason)
1999 struct wpa_sm *sm = ctx;
2002 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2003 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2005 if (sm->cur_pmksa == entry) {
2006 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2007 "RSN: %s current PMKSA entry",
2008 reason == PMKSA_REPLACE ? "replaced" : "removed");
2009 pmksa_cache_clear_current(sm);
2012 * If an entry is simply being replaced, there's no need to
2013 * deauthenticate because it will be immediately re-added.
2014 * This happens when EAP authentication is completed again
2015 * (reauth or failed PMKSA caching attempt).
2017 if (reason != PMKSA_REPLACE)
2021 if (reason == PMKSA_EXPIRE &&
2022 (sm->pmk_len == entry->pmk_len &&
2023 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2024 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2025 "RSN: deauthenticating due to expired PMK");
2026 pmksa_cache_clear_current(sm);
2031 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2032 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2038 * wpa_sm_init - Initialize WPA state machine
2039 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2040 * Returns: Pointer to the allocated WPA state machine data
2042 * This function is used to allocate a new WPA state machine and the returned
2043 * value is passed to all WPA state machine calls.
2045 struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2049 sm = os_zalloc(sizeof(*sm));
2052 dl_list_init(&sm->pmksa_candidates);
2053 sm->renew_snonce = 1;
2056 sm->dot11RSNAConfigPMKLifetime = 43200;
2057 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2058 sm->dot11RSNAConfigSATimeout = 60;
2060 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2061 if (sm->pmksa == NULL) {
2062 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2063 "RSN: PMKSA cache initialization failed");
2073 * wpa_sm_deinit - Deinitialize WPA state machine
2074 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2076 void wpa_sm_deinit(struct wpa_sm *sm)
2080 pmksa_cache_deinit(sm->pmksa);
2081 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2082 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2083 os_free(sm->assoc_wpa_ie);
2084 os_free(sm->ap_wpa_ie);
2085 os_free(sm->ap_rsn_ie);
2088 #ifdef CONFIG_IEEE80211R
2089 os_free(sm->assoc_resp_ies);
2090 #endif /* CONFIG_IEEE80211R */
2096 * wpa_sm_notify_assoc - Notify WPA state machine about association
2097 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2098 * @bssid: The BSSID of the new association
2100 * This function is called to let WPA state machine know that the connection
2103 void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2110 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2111 "WPA: Association event - clear replay counter");
2112 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2113 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2114 sm->rx_replay_counter_set = 0;
2115 sm->renew_snonce = 1;
2116 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2117 rsn_preauth_deinit(sm);
2119 #ifdef CONFIG_IEEE80211R
2120 if (wpa_ft_is_completed(sm)) {
2122 * Clear portValid to kick EAPOL state machine to re-enter
2123 * AUTHENTICATED state to get the EAPOL port Authorized.
2125 eapol_sm_notify_portValid(sm->eapol, FALSE);
2126 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2128 /* Prepare for the next transition */
2129 wpa_ft_prepare_auth_request(sm, NULL);
2133 #endif /* CONFIG_IEEE80211R */
2137 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2138 * this is not part of a Fast BSS Transition.
2140 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
2142 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2144 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2149 #endif /* CONFIG_TDLS */
2152 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
2153 #endif /* CONFIG_P2P */
2158 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2159 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2161 * This function is called to let WPA state machine know that the connection
2162 * was lost. This will abort any existing pre-authentication session.
2164 void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2167 rsn_preauth_deinit(sm);
2168 pmksa_cache_clear_current(sm);
2169 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2170 sm->dot11RSNA4WayHandshakeFailures++;
2172 wpa_tdls_disassoc(sm);
2173 #endif /* CONFIG_TDLS */
2178 * wpa_sm_set_pmk - Set PMK
2179 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2181 * @pmk_len: The length of the new PMK in bytes
2183 * Configure the PMK for WPA state machine.
2185 void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len)
2190 sm->pmk_len = pmk_len;
2191 os_memcpy(sm->pmk, pmk, pmk_len);
2193 #ifdef CONFIG_IEEE80211R
2194 /* Set XXKey to be PSK for FT key derivation */
2195 sm->xxkey_len = pmk_len;
2196 os_memcpy(sm->xxkey, pmk, pmk_len);
2197 #endif /* CONFIG_IEEE80211R */
2202 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2203 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2205 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2208 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2213 if (sm->cur_pmksa) {
2214 sm->pmk_len = sm->cur_pmksa->pmk_len;
2215 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2217 sm->pmk_len = PMK_LEN;
2218 os_memset(sm->pmk, 0, PMK_LEN);
2224 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2225 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2226 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2228 void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2231 sm->fast_reauth = fast_reauth;
2236 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2237 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2238 * @scard_ctx: Context pointer for smartcard related callback functions
2240 void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2244 sm->scard_ctx = scard_ctx;
2245 if (sm->preauth_eapol)
2246 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2251 * wpa_sm_set_config - Notification of current configration change
2252 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2253 * @config: Pointer to current network configuration
2255 * Notify WPA state machine that configuration has changed. config will be
2256 * stored as a backpointer to network configuration. This can be %NULL to clear
2257 * the stored pointed.
2259 void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2265 sm->network_ctx = config->network_ctx;
2266 sm->peerkey_enabled = config->peerkey_enabled;
2267 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2268 sm->proactive_key_caching = config->proactive_key_caching;
2269 sm->eap_workaround = config->eap_workaround;
2270 sm->eap_conf_ctx = config->eap_conf_ctx;
2272 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2273 sm->ssid_len = config->ssid_len;
2276 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
2277 sm->p2p = config->p2p;
2279 sm->network_ctx = NULL;
2280 sm->peerkey_enabled = 0;
2281 sm->allowed_pairwise_cipher = 0;
2282 sm->proactive_key_caching = 0;
2283 sm->eap_workaround = 0;
2284 sm->eap_conf_ctx = NULL;
2286 sm->wpa_ptk_rekey = 0;
2293 * wpa_sm_set_own_addr - Set own MAC address
2294 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2295 * @addr: Own MAC address
2297 void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2300 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2305 * wpa_sm_set_ifname - Set network interface name
2306 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2307 * @ifname: Interface name
2308 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2310 void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2311 const char *bridge_ifname)
2314 sm->ifname = ifname;
2315 sm->bridge_ifname = bridge_ifname;
2321 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2322 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2323 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2325 void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2333 * wpa_sm_set_param - Set WPA state machine parameters
2334 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2335 * @param: Parameter field
2336 * @value: Parameter value
2337 * Returns: 0 on success, -1 on failure
2339 int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2348 case RSNA_PMK_LIFETIME:
2350 sm->dot11RSNAConfigPMKLifetime = value;
2354 case RSNA_PMK_REAUTH_THRESHOLD:
2355 if (value > 0 && value <= 100)
2356 sm->dot11RSNAConfigPMKReauthThreshold = value;
2360 case RSNA_SA_TIMEOUT:
2362 sm->dot11RSNAConfigSATimeout = value;
2366 case WPA_PARAM_PROTO:
2369 case WPA_PARAM_PAIRWISE:
2370 sm->pairwise_cipher = value;
2372 case WPA_PARAM_GROUP:
2373 sm->group_cipher = value;
2375 case WPA_PARAM_KEY_MGMT:
2376 sm->key_mgmt = value;
2378 #ifdef CONFIG_IEEE80211W
2379 case WPA_PARAM_MGMT_GROUP:
2380 sm->mgmt_group_cipher = value;
2382 #endif /* CONFIG_IEEE80211W */
2383 case WPA_PARAM_RSN_ENABLED:
2384 sm->rsn_enabled = value;
2398 * wpa_sm_get_status - Get WPA state machine
2399 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2400 * @buf: Buffer for status information
2401 * @buflen: Maximum buffer length
2402 * @verbose: Whether to include verbose status information
2403 * Returns: Number of bytes written to buf.
2405 * Query WPA state machine for status information. This function fills in
2406 * a text area with current status information. If the buffer (buf) is not
2407 * large enough, status information will be truncated to fit the buffer.
2409 int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2412 char *pos = buf, *end = buf + buflen;
2415 ret = os_snprintf(pos, end - pos,
2416 "pairwise_cipher=%s\n"
2419 wpa_cipher_txt(sm->pairwise_cipher),
2420 wpa_cipher_txt(sm->group_cipher),
2421 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
2422 if (ret < 0 || ret >= end - pos)
2426 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
2427 struct wpa_ie_data rsn;
2428 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
2430 rsn.capabilities & (WPA_CAPABILITY_MFPR |
2431 WPA_CAPABILITY_MFPC)) {
2432 ret = os_snprintf(pos, end - pos, "pmf=%d\n",
2434 WPA_CAPABILITY_MFPR) ? 2 : 1);
2435 if (ret < 0 || ret >= end - pos)
2445 int wpa_sm_pmf_enabled(struct wpa_sm *sm)
2447 struct wpa_ie_data rsn;
2449 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
2452 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
2453 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
2461 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2462 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2463 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2464 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2465 * Returns: 0 on success, -1 on failure
2467 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2475 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2480 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2481 wpa_ie, *wpa_ie_len);
2483 if (sm->assoc_wpa_ie == NULL) {
2485 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2486 * the correct version of the IE even if PMKSA caching is
2487 * aborted (which would remove PMKID from IE generation).
2489 sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2490 if (sm->assoc_wpa_ie == NULL)
2493 os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2494 sm->assoc_wpa_ie_len = *wpa_ie_len;
2502 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2503 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2504 * @ie: Pointer to IE data (starting from id)
2506 * Returns: 0 on success, -1 on failure
2508 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2509 * Request frame. The IE will be used to override the default value generated
2510 * with wpa_sm_set_assoc_wpa_ie_default().
2512 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2517 os_free(sm->assoc_wpa_ie);
2518 if (ie == NULL || len == 0) {
2519 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2520 "WPA: clearing own WPA/RSN IE");
2521 sm->assoc_wpa_ie = NULL;
2522 sm->assoc_wpa_ie_len = 0;
2524 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2525 sm->assoc_wpa_ie = os_malloc(len);
2526 if (sm->assoc_wpa_ie == NULL)
2529 os_memcpy(sm->assoc_wpa_ie, ie, len);
2530 sm->assoc_wpa_ie_len = len;
2538 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2539 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2540 * @ie: Pointer to IE data (starting from id)
2542 * Returns: 0 on success, -1 on failure
2544 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2547 int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2552 os_free(sm->ap_wpa_ie);
2553 if (ie == NULL || len == 0) {
2554 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2555 "WPA: clearing AP WPA IE");
2556 sm->ap_wpa_ie = NULL;
2557 sm->ap_wpa_ie_len = 0;
2559 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2560 sm->ap_wpa_ie = os_malloc(len);
2561 if (sm->ap_wpa_ie == NULL)
2564 os_memcpy(sm->ap_wpa_ie, ie, len);
2565 sm->ap_wpa_ie_len = len;
2573 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2574 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2575 * @ie: Pointer to IE data (starting from id)
2577 * Returns: 0 on success, -1 on failure
2579 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2582 int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2587 os_free(sm->ap_rsn_ie);
2588 if (ie == NULL || len == 0) {
2589 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2590 "WPA: clearing AP RSN IE");
2591 sm->ap_rsn_ie = NULL;
2592 sm->ap_rsn_ie_len = 0;
2594 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2595 sm->ap_rsn_ie = os_malloc(len);
2596 if (sm->ap_rsn_ie == NULL)
2599 os_memcpy(sm->ap_rsn_ie, ie, len);
2600 sm->ap_rsn_ie_len = len;
2608 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2609 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2610 * @data: Pointer to data area for parsing results
2611 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2613 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2614 * parsed data into data.
2616 int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2621 if (sm->assoc_wpa_ie == NULL) {
2622 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2623 "WPA: No WPA/RSN IE available from association info");
2626 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
2632 int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
2634 return pmksa_cache_list(sm->pmksa, buf, len);
2638 #ifdef CONFIG_TESTING_OPTIONS
2639 void wpa_sm_drop_sa(struct wpa_sm *sm)
2641 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
2644 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2645 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2646 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2648 #endif /* CONFIG_TESTING_OPTIONS */
2651 int wpa_sm_has_ptk(struct wpa_sm *sm)
2659 void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
2661 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
2665 void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
2667 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
2672 int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
2675 u8 keylen; /* plaintext key len */
2678 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
2679 struct wpa_gtk_data gd;
2681 os_memset(&gd, 0, sizeof(gd));
2682 keylen = wpa_cipher_key_len(sm->group_cipher);
2683 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
2684 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
2685 if (gd.alg == WPA_ALG_NONE) {
2686 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
2691 keyinfo = WPA_GET_LE16(buf + 2);
2692 gd.gtk_len = keylen;
2693 if (gd.gtk_len != buf[4]) {
2694 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
2695 gd.gtk_len, buf[4]);
2698 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
2699 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
2700 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
2702 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
2704 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
2705 gd.gtk, gd.gtk_len);
2706 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc)) {
2707 os_memset(&gd, 0, sizeof(gd));
2708 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
2712 os_memset(&gd, 0, sizeof(gd));
2713 #ifdef CONFIG_IEEE80211W
2714 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
2715 struct wpa_igtk_kde igd;
2718 os_memset(&igd, 0, sizeof(igd));
2719 keylen = wpa_cipher_key_len(sm->mgmt_group_cipher);
2720 os_memcpy(igd.keyid, buf + 2, 2);
2721 os_memcpy(igd.pn, buf + 4, 6);
2723 keyidx = WPA_GET_LE16(igd.keyid);
2724 os_memcpy(igd.igtk, buf + 10, keylen);
2726 wpa_hexdump_key(MSG_DEBUG, "Install IGTK (WNM SLEEP)",
2728 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
2729 broadcast_ether_addr,
2730 keyidx, 0, igd.pn, sizeof(igd.pn),
2731 igd.igtk, keylen) < 0) {
2732 wpa_printf(MSG_DEBUG, "Failed to install the IGTK in "
2734 os_memset(&igd, 0, sizeof(igd));
2737 os_memset(&igd, 0, sizeof(igd));
2738 #endif /* CONFIG_IEEE80211W */
2740 wpa_printf(MSG_DEBUG, "Unknown element id");
2746 #endif /* CONFIG_WNM */
2749 #ifdef CONFIG_PEERKEY
2750 int wpa_sm_rx_eapol_peerkey(struct wpa_sm *sm, const u8 *src_addr,
2751 const u8 *buf, size_t len)
2753 struct wpa_peerkey *peerkey;
2755 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
2756 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
2763 wpa_sm_rx_eapol(sm, src_addr, buf, len);
2767 #endif /* CONFIG_PEERKEY */
2772 int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
2774 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
2776 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
2780 #endif /* CONFIG_P2P */