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 void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm *sm)
143 #ifdef CONFIG_IEEE80211R
144 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
145 if (wpa_sm_key_mgmt_set_pmk(sm, sm->xxkey, sm->xxkey_len))
146 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
147 "RSN: Cannot set low order 256 bits of MSK for key management offload");
149 #endif /* CONFIG_IEEE80211R */
150 if (wpa_sm_key_mgmt_set_pmk(sm, sm->pmk, sm->pmk_len))
151 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
152 "RSN: Cannot set PMK for key management offload");
153 #ifdef CONFIG_IEEE80211R
155 #endif /* CONFIG_IEEE80211R */
159 static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
160 const unsigned char *src_addr,
163 int abort_cached = 0;
165 if (pmkid && !sm->cur_pmksa) {
166 /* When using drivers that generate RSN IE, wpa_supplicant may
167 * not have enough time to get the association information
168 * event before receiving this 1/4 message, so try to find a
169 * matching PMKSA cache entry here. */
170 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid,
173 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
174 "RSN: found matching PMKID from PMKSA cache");
176 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
177 "RSN: no matching PMKID found");
182 if (pmkid && sm->cur_pmksa &&
183 os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
184 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
185 wpa_sm_set_pmk_from_pmksa(sm);
186 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
187 sm->pmk, sm->pmk_len);
188 eapol_sm_notify_cached(sm->eapol);
189 #ifdef CONFIG_IEEE80211R
191 #endif /* CONFIG_IEEE80211R */
192 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
195 res = eapol_sm_get_key(sm->eapol, sm->pmk, PMK_LEN);
198 * EAP-LEAP is an exception from other EAP methods: it
199 * uses only 16-byte PMK.
201 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
204 #ifdef CONFIG_IEEE80211R
206 if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
208 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
209 sm->xxkey_len = PMK_LEN;
210 os_memset(buf, 0, sizeof(buf));
212 #endif /* CONFIG_IEEE80211R */
215 struct rsn_pmksa_cache_entry *sa = NULL;
216 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
217 "machines", sm->pmk, pmk_len);
218 sm->pmk_len = pmk_len;
219 wpa_supplicant_key_mgmt_set_pmk(sm);
220 if (sm->proto == WPA_PROTO_RSN &&
221 !wpa_key_mgmt_ft(sm->key_mgmt)) {
222 sa = pmksa_cache_add(sm->pmksa,
224 src_addr, sm->own_addr,
228 if (!sm->cur_pmksa && pmkid &&
229 pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL))
231 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
232 "RSN: the new PMK matches with the "
240 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
241 "WPA: Failed to get master session key from "
242 "EAPOL state machines - key handshake "
245 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
246 "RSN: Cancelled PMKSA caching "
248 sm->cur_pmksa = NULL;
250 } else if (!abort_cached) {
256 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
257 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
259 /* Send EAPOL-Start to trigger full EAP authentication. */
263 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
264 "RSN: no PMKSA entry found - trigger "
265 "full EAP authentication");
266 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
267 NULL, 0, &buflen, NULL);
269 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
283 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
284 * @sm: Pointer to WPA state machine data from wpa_sm_init()
285 * @dst: Destination address for the frame
286 * @key: Pointer to the EAPOL-Key frame header
287 * @ver: Version bits from EAPOL-Key Key Info
288 * @nonce: Nonce value for the EAPOL-Key frame
289 * @wpa_ie: WPA/RSN IE
290 * @wpa_ie_len: Length of the WPA/RSN IE
291 * @ptk: PTK to use for keyed hash and encryption
292 * Returns: 0 on success, -1 on failure
294 int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
295 const struct wpa_eapol_key *key,
296 int ver, const u8 *nonce,
297 const u8 *wpa_ie, size_t wpa_ie_len,
301 struct wpa_eapol_key *reply;
303 u8 *rsn_ie_buf = NULL;
305 if (wpa_ie == NULL) {
306 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
307 "cannot generate msg 2/4");
311 #ifdef CONFIG_IEEE80211R
312 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
316 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
317 * FTIE from (Re)Association Response.
319 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
320 sm->assoc_resp_ies_len);
321 if (rsn_ie_buf == NULL)
323 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
324 res = wpa_insert_pmkid(rsn_ie_buf, wpa_ie_len,
332 if (sm->assoc_resp_ies) {
333 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
334 sm->assoc_resp_ies_len);
335 wpa_ie_len += sm->assoc_resp_ies_len;
340 #endif /* CONFIG_IEEE80211R */
342 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
344 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
345 NULL, sizeof(*reply) + wpa_ie_len,
346 &rlen, (void *) &reply);
352 reply->type = (sm->proto == WPA_PROTO_RSN ||
353 sm->proto == WPA_PROTO_OSEN) ?
354 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
355 WPA_PUT_BE16(reply->key_info,
356 ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC);
357 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
358 WPA_PUT_BE16(reply->key_length, 0);
360 os_memcpy(reply->key_length, key->key_length, 2);
361 os_memcpy(reply->replay_counter, key->replay_counter,
362 WPA_REPLAY_COUNTER_LEN);
363 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
364 WPA_REPLAY_COUNTER_LEN);
366 WPA_PUT_BE16(reply->key_data_length, wpa_ie_len);
367 os_memcpy(reply + 1, wpa_ie, wpa_ie_len);
370 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
372 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
373 wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
374 rbuf, rlen, reply->key_mic);
380 static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
381 const struct wpa_eapol_key *key,
384 size_t ptk_len = wpa_cipher_key_len(sm->pairwise_cipher) + 32;
385 #ifdef CONFIG_IEEE80211R
386 if (wpa_key_mgmt_ft(sm->key_mgmt))
387 return wpa_derive_ptk_ft(sm, src_addr, key, ptk, ptk_len);
388 #endif /* CONFIG_IEEE80211R */
390 wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
391 sm->own_addr, sm->bssid, sm->snonce, key->key_nonce,
393 wpa_key_mgmt_sha256(sm->key_mgmt));
398 static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
399 const unsigned char *src_addr,
400 const struct wpa_eapol_key *key,
401 u16 ver, const u8 *key_data,
404 struct wpa_eapol_ie_parse ie;
407 u8 *kde, *kde_buf = NULL;
410 if (wpa_sm_get_network_ctx(sm) == NULL) {
411 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
412 "found (msg 1 of 4)");
416 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
417 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of 4-Way "
418 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
420 os_memset(&ie, 0, sizeof(ie));
422 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
423 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
424 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
425 key_data, key_data_len);
426 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
429 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
430 "Authenticator", ie.pmkid, PMKID_LEN);
434 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
436 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
437 "msg 1/4 - requesting full EAP authentication");
443 if (sm->renew_snonce) {
444 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
445 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
446 "WPA: Failed to get random data for SNonce");
449 sm->renew_snonce = 0;
450 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
451 sm->snonce, WPA_NONCE_LEN);
454 /* Calculate PTK which will be stored as a temporary PTK until it has
455 * been verified when processing message 3/4. */
457 wpa_derive_ptk(sm, src_addr, key, ptk);
458 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
460 /* Supplicant: swap tx/rx Mic keys */
461 os_memcpy(buf, ptk->u.auth.tx_mic_key, 8);
462 os_memcpy(ptk->u.auth.tx_mic_key, ptk->u.auth.rx_mic_key, 8);
463 os_memcpy(ptk->u.auth.rx_mic_key, buf, 8);
464 os_memset(buf, 0, sizeof(buf));
468 kde = sm->assoc_wpa_ie;
469 kde_len = sm->assoc_wpa_ie_len;
473 kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 1);
476 wpa_printf(MSG_DEBUG, "P2P: Add IP Address Request KDE "
477 "into EAPOL-Key 2/4");
478 os_memcpy(kde_buf, kde, kde_len);
481 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
482 *pos++ = RSN_SELECTOR_LEN + 1;
483 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
484 pos += RSN_SELECTOR_LEN;
489 #endif /* CONFIG_P2P */
491 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
496 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
501 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
505 static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
507 struct wpa_sm *sm = eloop_ctx;
508 rsn_preauth_candidate_process(sm);
512 static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
513 const u8 *addr, int secure)
515 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
516 "WPA: Key negotiation completed with "
517 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
518 wpa_cipher_txt(sm->pairwise_cipher),
519 wpa_cipher_txt(sm->group_cipher));
520 wpa_sm_cancel_auth_timeout(sm);
521 wpa_sm_set_state(sm, WPA_COMPLETED);
524 wpa_sm_mlme_setprotection(
525 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
526 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
527 eapol_sm_notify_portValid(sm->eapol, TRUE);
528 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt))
529 eapol_sm_notify_eap_success(sm->eapol, TRUE);
531 * Start preauthentication after a short wait to avoid a
532 * possible race condition between the data receive and key
533 * configuration after the 4-Way Handshake. This increases the
534 * likelihood of the first preauth EAPOL-Start frame getting to
537 eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
540 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
541 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
542 "RSN: Authenticator accepted "
543 "opportunistic PMKSA entry - marking it valid");
544 sm->cur_pmksa->opportunistic = 0;
547 #ifdef CONFIG_IEEE80211R
548 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
549 /* Prepare for the next transition */
550 wpa_ft_prepare_auth_request(sm, NULL);
552 #endif /* CONFIG_IEEE80211R */
556 static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
558 struct wpa_sm *sm = eloop_ctx;
559 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
560 wpa_sm_key_request(sm, 0, 1);
564 static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
565 const struct wpa_eapol_key *key)
570 u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
572 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
573 "WPA: Installing PTK to the driver");
575 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
576 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
577 "Suite: NONE - do not use pairwise keys");
581 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
582 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
583 "WPA: Unsupported pairwise cipher %d",
584 sm->pairwise_cipher);
588 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
589 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
590 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
592 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
595 key_rsc = key->key_rsc;
596 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
599 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
600 (u8 *) sm->ptk.tk1, keylen) < 0) {
601 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
602 "WPA: Failed to set PTK to the "
603 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
604 alg, keylen, MAC2STR(sm->bssid));
608 if (sm->wpa_ptk_rekey) {
609 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
610 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
618 static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
620 int keylen, int maxkeylen,
626 *alg = wpa_cipher_to_alg(group_cipher);
627 if (*alg == WPA_ALG_NONE) {
628 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
629 "WPA: Unsupported Group Cipher %d",
633 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
635 klen = wpa_cipher_key_len(group_cipher);
636 if (keylen != klen || maxkeylen < klen) {
637 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
638 "WPA: Unsupported %s Group Cipher key length %d (%d)",
639 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
646 struct wpa_gtk_data {
648 int tx, key_rsc_len, keyidx;
654 static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
655 const struct wpa_gtk_data *gd,
658 const u8 *_gtk = gd->gtk;
661 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
662 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
663 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
664 gd->keyidx, gd->tx, gd->gtk_len);
665 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
666 if (sm->group_cipher == WPA_CIPHER_TKIP) {
667 /* Swap Tx/Rx keys for Michael MIC */
668 os_memcpy(gtk_buf, gd->gtk, 16);
669 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
670 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
673 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
674 if (wpa_sm_set_key(sm, gd->alg, NULL,
675 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
676 _gtk, gd->gtk_len) < 0) {
677 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
678 "WPA: Failed to set GTK to the driver "
680 os_memset(gtk_buf, 0, sizeof(gtk_buf));
683 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
684 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
685 _gtk, gd->gtk_len) < 0) {
686 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
687 "WPA: Failed to set GTK to "
688 "the driver (alg=%d keylen=%d keyidx=%d)",
689 gd->alg, gd->gtk_len, gd->keyidx);
690 os_memset(gtk_buf, 0, sizeof(gtk_buf));
693 os_memset(gtk_buf, 0, sizeof(gtk_buf));
699 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
702 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
703 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
704 * seemed to set this bit (incorrectly, since Tx is only when
705 * doing Group Key only APs) and without this workaround, the
706 * data connection does not work because wpa_supplicant
707 * configured non-zero keyidx to be used for unicast. */
708 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
709 "WPA: Tx bit set for GTK, but pairwise "
710 "keys are used - ignore Tx bit");
717 static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
718 const struct wpa_eapol_key *key,
719 const u8 *gtk, size_t gtk_len,
722 struct wpa_gtk_data gd;
725 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
727 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
728 * Reserved [bits 0-7]
732 os_memset(&gd, 0, sizeof(gd));
733 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
736 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
739 gd.keyidx = gtk[0] & 0x3;
740 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
741 !!(gtk[0] & BIT(2)));
745 os_memcpy(gd.gtk, gtk, gtk_len);
746 gd.gtk_len = gtk_len;
748 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
749 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
751 &gd.key_rsc_len, &gd.alg) ||
752 wpa_supplicant_install_gtk(sm, &gd, key->key_rsc))) {
753 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
754 "RSN: Failed to install GTK");
755 os_memset(&gd, 0, sizeof(gd));
758 os_memset(&gd, 0, sizeof(gd));
760 wpa_supplicant_key_neg_complete(sm, sm->bssid,
761 key_info & WPA_KEY_INFO_SECURE);
766 static int ieee80211w_set_keys(struct wpa_sm *sm,
767 struct wpa_eapol_ie_parse *ie)
769 #ifdef CONFIG_IEEE80211W
770 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
775 const struct wpa_igtk_kde *igtk;
777 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
778 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
780 igtk = (const struct wpa_igtk_kde *) ie->igtk;
781 keyidx = WPA_GET_LE16(igtk->keyid);
782 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: IGTK keyid %d "
783 "pn %02x%02x%02x%02x%02x%02x",
784 keyidx, MAC2STR(igtk->pn));
785 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK",
788 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
789 "WPA: Invalid IGTK KeyID %d", keyidx);
792 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
793 broadcast_ether_addr,
794 keyidx, 0, igtk->pn, sizeof(igtk->pn),
795 igtk->igtk, len) < 0) {
796 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
797 "WPA: Failed to configure IGTK to the driver");
803 #else /* CONFIG_IEEE80211W */
805 #endif /* CONFIG_IEEE80211W */
809 static void wpa_report_ie_mismatch(struct wpa_sm *sm,
810 const char *reason, const u8 *src_addr,
811 const u8 *wpa_ie, size_t wpa_ie_len,
812 const u8 *rsn_ie, size_t rsn_ie_len)
814 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
815 reason, MAC2STR(src_addr));
818 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
819 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
822 if (!sm->ap_wpa_ie) {
823 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
824 "WPA: No WPA IE in Beacon/ProbeResp");
826 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
831 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
832 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
835 if (!sm->ap_rsn_ie) {
836 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
837 "WPA: No RSN IE in Beacon/ProbeResp");
839 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
843 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
847 #ifdef CONFIG_IEEE80211R
849 static int ft_validate_mdie(struct wpa_sm *sm,
850 const unsigned char *src_addr,
851 struct wpa_eapol_ie_parse *ie,
852 const u8 *assoc_resp_mdie)
854 struct rsn_mdie *mdie;
856 mdie = (struct rsn_mdie *) (ie->mdie + 2);
857 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
858 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
859 MOBILITY_DOMAIN_ID_LEN) != 0) {
860 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
861 "not match with the current mobility domain");
865 if (assoc_resp_mdie &&
866 (assoc_resp_mdie[1] != ie->mdie[1] ||
867 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
868 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
869 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
870 ie->mdie, 2 + ie->mdie[1]);
871 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
872 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
880 static int ft_validate_ftie(struct wpa_sm *sm,
881 const unsigned char *src_addr,
882 struct wpa_eapol_ie_parse *ie,
883 const u8 *assoc_resp_ftie)
885 if (ie->ftie == NULL) {
886 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
887 "FT: No FTIE in EAPOL-Key msg 3/4");
891 if (assoc_resp_ftie == NULL)
894 if (assoc_resp_ftie[1] != ie->ftie[1] ||
895 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
896 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
897 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
898 ie->ftie, 2 + ie->ftie[1]);
899 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
900 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
908 static int ft_validate_rsnie(struct wpa_sm *sm,
909 const unsigned char *src_addr,
910 struct wpa_eapol_ie_parse *ie)
912 struct wpa_ie_data rsn;
918 * Verify that PMKR1Name from EAPOL-Key message 3/4
919 * matches with the value we derived.
921 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
922 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
923 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
924 "FT 4-way handshake message 3/4");
928 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
930 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
931 "FT: PMKR1Name mismatch in "
932 "FT 4-way handshake message 3/4");
933 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
934 rsn.pmkid, WPA_PMK_NAME_LEN);
935 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
936 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
944 static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
945 const unsigned char *src_addr,
946 struct wpa_eapol_ie_parse *ie)
948 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
950 if (sm->assoc_resp_ies) {
951 pos = sm->assoc_resp_ies;
952 end = pos + sm->assoc_resp_ies_len;
953 while (pos + 2 < end) {
954 if (pos + 2 + pos[1] > end)
957 case WLAN_EID_MOBILITY_DOMAIN:
960 case WLAN_EID_FAST_BSS_TRANSITION:
968 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
969 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
970 ft_validate_rsnie(sm, src_addr, ie) < 0)
976 #endif /* CONFIG_IEEE80211R */
979 static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
980 const unsigned char *src_addr,
981 struct wpa_eapol_ie_parse *ie)
983 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
984 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
985 "WPA: No WPA/RSN IE for this AP known. "
986 "Trying to get from scan results");
987 if (wpa_sm_get_beacon_ie(sm) < 0) {
988 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
989 "WPA: Could not find AP from "
992 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
993 "WPA: Found the current AP from "
994 "updated scan results");
998 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
999 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1000 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1001 "with IE in Beacon/ProbeResp (no IE?)",
1002 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1003 ie->rsn_ie, ie->rsn_ie_len);
1007 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1008 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1009 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1010 (ie->rsn_ie && sm->ap_rsn_ie &&
1011 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1012 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1013 ie->rsn_ie, ie->rsn_ie_len))) {
1014 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1015 "with IE in Beacon/ProbeResp",
1016 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1017 ie->rsn_ie, ie->rsn_ie_len);
1021 if (sm->proto == WPA_PROTO_WPA &&
1022 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1023 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1024 "detected - RSN was enabled and RSN IE "
1025 "was in msg 3/4, but not in "
1027 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1028 ie->rsn_ie, ie->rsn_ie_len);
1032 #ifdef CONFIG_IEEE80211R
1033 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1034 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1036 #endif /* CONFIG_IEEE80211R */
1043 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1044 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1045 * @dst: Destination address for the frame
1046 * @key: Pointer to the EAPOL-Key frame header
1047 * @ver: Version bits from EAPOL-Key Key Info
1048 * @key_info: Key Info
1049 * @ptk: PTK to use for keyed hash and encryption
1050 * Returns: 0 on success, -1 on failure
1052 int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1053 const struct wpa_eapol_key *key,
1054 u16 ver, u16 key_info,
1055 struct wpa_ptk *ptk)
1058 struct wpa_eapol_key *reply;
1061 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1062 sizeof(*reply), &rlen, (void *) &reply);
1066 reply->type = (sm->proto == WPA_PROTO_RSN ||
1067 sm->proto == WPA_PROTO_OSEN) ?
1068 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1069 key_info &= WPA_KEY_INFO_SECURE;
1070 key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
1071 WPA_PUT_BE16(reply->key_info, key_info);
1072 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
1073 WPA_PUT_BE16(reply->key_length, 0);
1075 os_memcpy(reply->key_length, key->key_length, 2);
1076 os_memcpy(reply->replay_counter, key->replay_counter,
1077 WPA_REPLAY_COUNTER_LEN);
1079 WPA_PUT_BE16(reply->key_data_length, 0);
1081 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
1082 wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
1083 rbuf, rlen, reply->key_mic);
1089 static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1090 const struct wpa_eapol_key *key,
1091 u16 ver, const u8 *key_data,
1092 size_t key_data_len)
1094 u16 key_info, keylen;
1095 struct wpa_eapol_ie_parse ie;
1097 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
1098 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way "
1099 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1101 key_info = WPA_GET_BE16(key->key_info);
1103 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1104 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
1106 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1107 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1108 "WPA: GTK IE in unencrypted key data");
1111 #ifdef CONFIG_IEEE80211W
1112 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1113 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1114 "WPA: IGTK KDE in unencrypted key data");
1119 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1120 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1121 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
1122 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1123 "WPA: Invalid IGTK KDE length %lu",
1124 (unsigned long) ie.igtk_len);
1127 #endif /* CONFIG_IEEE80211W */
1129 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1132 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1133 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1134 "WPA: ANonce from message 1 of 4-Way Handshake "
1135 "differs from 3 of 4-Way Handshake - drop packet (src="
1136 MACSTR ")", MAC2STR(sm->bssid));
1140 keylen = WPA_GET_BE16(key->key_length);
1141 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1142 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1143 "WPA: Invalid %s key length %d (src=" MACSTR
1144 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1145 MAC2STR(sm->bssid));
1150 if (ie.ip_addr_alloc) {
1151 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1152 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1153 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1155 #endif /* CONFIG_P2P */
1157 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
1162 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1163 * for the next 4-Way Handshake. If msg 3 is received again, the old
1164 * SNonce will still be used to avoid changing PTK. */
1165 sm->renew_snonce = 1;
1167 if (key_info & WPA_KEY_INFO_INSTALL) {
1168 if (wpa_supplicant_install_ptk(sm, key))
1172 if (key_info & WPA_KEY_INFO_SECURE) {
1173 wpa_sm_mlme_setprotection(
1174 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1175 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1176 eapol_sm_notify_portValid(sm->eapol, TRUE);
1178 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1180 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
1181 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1182 key_info & WPA_KEY_INFO_SECURE);
1183 } else if (ie.gtk &&
1184 wpa_supplicant_pairwise_gtk(sm, key,
1185 ie.gtk, ie.gtk_len, key_info) < 0) {
1186 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1187 "RSN: Failed to configure GTK");
1191 if (ieee80211w_set_keys(sm, &ie) < 0) {
1192 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1193 "RSN: Failed to configure IGTK");
1198 wpa_sm_set_rekey_offload(sm);
1203 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1207 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1211 struct wpa_gtk_data *gd)
1214 struct wpa_eapol_ie_parse ie;
1216 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
1217 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1219 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1220 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1221 "WPA: GTK IE in unencrypted key data");
1224 if (ie.gtk == NULL) {
1225 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1226 "WPA: No GTK IE in Group Key msg 1/2");
1229 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1231 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1232 gd->gtk_len, maxkeylen,
1233 &gd->key_rsc_len, &gd->alg))
1236 wpa_hexdump(MSG_DEBUG, "RSN: received GTK in group key handshake",
1237 ie.gtk, ie.gtk_len);
1238 gd->keyidx = ie.gtk[0] & 0x3;
1239 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1240 !!(ie.gtk[0] & BIT(2)));
1241 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1242 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1243 "RSN: Too long GTK in GTK IE (len=%lu)",
1244 (unsigned long) ie.gtk_len - 2);
1247 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1249 if (ieee80211w_set_keys(sm, &ie) < 0)
1250 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1251 "RSN: Failed to configure IGTK");
1257 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1258 const struct wpa_eapol_key *key,
1260 size_t key_data_len, u16 key_info,
1261 u16 ver, struct wpa_gtk_data *gd)
1265 gd->gtk_len = WPA_GET_BE16(key->key_length);
1266 maxkeylen = key_data_len;
1267 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1268 if (maxkeylen < 8) {
1269 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1270 "WPA: Too short maxkeylen (%lu)",
1271 (unsigned long) maxkeylen);
1277 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1278 gd->gtk_len, maxkeylen,
1279 &gd->key_rsc_len, &gd->alg))
1282 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1283 WPA_KEY_INFO_KEY_INDEX_SHIFT;
1284 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1286 if (key_data_len > sizeof(gd->gtk)) {
1287 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1288 "WPA: RC4 key data too long (%lu)",
1289 (unsigned long) key_data_len);
1292 os_memcpy(ek, key->key_iv, 16);
1293 os_memcpy(ek + 16, sm->ptk.kek, 16);
1294 os_memcpy(gd->gtk, key_data, key_data_len);
1295 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
1296 os_memset(ek, 0, sizeof(ek));
1297 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1301 os_memset(ek, 0, sizeof(ek));
1302 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1303 if (maxkeylen % 8) {
1304 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1305 "WPA: Unsupported AES-WRAP len %lu",
1306 (unsigned long) maxkeylen);
1309 if (maxkeylen > sizeof(gd->gtk)) {
1310 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1311 "WPA: AES-WRAP key data "
1312 "too long (keydatalen=%lu maxkeylen=%lu)",
1313 (unsigned long) key_data_len,
1314 (unsigned long) maxkeylen);
1317 if (aes_unwrap(sm->ptk.kek, 16, maxkeylen / 8, key_data,
1319 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1320 "WPA: AES unwrap failed - could not decrypt "
1325 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1326 "WPA: Unsupported key_info type %d", ver);
1329 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1330 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1335 static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1336 const struct wpa_eapol_key *key,
1337 int ver, u16 key_info)
1340 struct wpa_eapol_key *reply;
1343 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1344 sizeof(*reply), &rlen, (void *) &reply);
1348 reply->type = (sm->proto == WPA_PROTO_RSN ||
1349 sm->proto == WPA_PROTO_OSEN) ?
1350 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1351 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1352 key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
1353 WPA_PUT_BE16(reply->key_info, key_info);
1354 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
1355 WPA_PUT_BE16(reply->key_length, 0);
1357 os_memcpy(reply->key_length, key->key_length, 2);
1358 os_memcpy(reply->replay_counter, key->replay_counter,
1359 WPA_REPLAY_COUNTER_LEN);
1361 WPA_PUT_BE16(reply->key_data_length, 0);
1363 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
1364 wpa_eapol_key_send(sm, sm->ptk.kck, ver, sm->bssid, ETH_P_EAPOL,
1365 rbuf, rlen, reply->key_mic);
1371 static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1372 const unsigned char *src_addr,
1373 const struct wpa_eapol_key *key,
1375 size_t key_data_len, u16 ver)
1379 struct wpa_gtk_data gd;
1381 os_memset(&gd, 0, sizeof(gd));
1383 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1384 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1385 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1387 key_info = WPA_GET_BE16(key->key_info);
1389 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
1390 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
1391 key_data_len, key_info,
1394 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
1396 key_info, ver, &gd);
1399 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1404 if (wpa_supplicant_install_gtk(sm, &gd, key->key_rsc) ||
1405 wpa_supplicant_send_2_of_2(sm, key, ver, key_info))
1409 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
1410 "completed with " MACSTR " [GTK=%s]",
1411 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1412 wpa_sm_cancel_auth_timeout(sm);
1413 wpa_sm_set_state(sm, WPA_COMPLETED);
1415 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1417 WPA_KEY_INFO_SECURE);
1420 wpa_sm_set_rekey_offload(sm);
1425 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1429 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
1430 struct wpa_eapol_key *key,
1432 const u8 *buf, size_t len)
1437 os_memcpy(mic, key->key_mic, 16);
1439 os_memset(key->key_mic, 0, 16);
1440 wpa_eapol_key_mic(sm->tptk.kck, ver, buf, len,
1442 if (os_memcmp_const(mic, key->key_mic, 16) != 0) {
1443 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1444 "WPA: Invalid EAPOL-Key MIC "
1445 "when using TPTK - ignoring TPTK");
1450 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
1451 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
1455 if (!ok && sm->ptk_set) {
1456 os_memset(key->key_mic, 0, 16);
1457 wpa_eapol_key_mic(sm->ptk.kck, ver, buf, len,
1459 if (os_memcmp_const(mic, key->key_mic, 16) != 0) {
1460 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1461 "WPA: Invalid EAPOL-Key MIC - "
1469 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1470 "WPA: Could not verify EAPOL-Key MIC - "
1475 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1476 WPA_REPLAY_COUNTER_LEN);
1477 sm->rx_replay_counter_set = 1;
1482 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1483 static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
1484 struct wpa_eapol_key *key, u16 ver,
1485 u8 *key_data, size_t *key_data_len)
1487 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
1488 key_data, *key_data_len);
1490 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1491 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1496 /* Decrypt key data here so that this operation does not need
1497 * to be implemented separately for each message type. */
1498 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1500 os_memcpy(ek, key->key_iv, 16);
1501 os_memcpy(ek + 16, sm->ptk.kek, 16);
1502 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
1503 os_memset(ek, 0, sizeof(ek));
1504 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1508 os_memset(ek, 0, sizeof(ek));
1509 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1510 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
1511 sm->key_mgmt == WPA_KEY_MGMT_OSEN) {
1513 if (*key_data_len < 8 || *key_data_len % 8) {
1514 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1515 "WPA: Unsupported AES-WRAP len %u",
1516 (unsigned int) *key_data_len);
1519 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
1520 buf = os_malloc(*key_data_len);
1522 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1523 "WPA: No memory for AES-UNWRAP buffer");
1526 if (aes_unwrap(sm->ptk.kek, 16, *key_data_len / 8,
1529 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1530 "WPA: AES unwrap failed - "
1531 "could not decrypt EAPOL-Key key data");
1534 os_memcpy(key_data, buf, *key_data_len);
1536 WPA_PUT_BE16(key->key_data_length, *key_data_len);
1538 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1539 "WPA: Unsupported key_info type %d", ver);
1542 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
1543 key_data, *key_data_len);
1549 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1550 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1552 void wpa_sm_aborted_cached(struct wpa_sm *sm)
1554 if (sm && sm->cur_pmksa) {
1555 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1556 "RSN: Cancelling PMKSA caching attempt");
1557 sm->cur_pmksa = NULL;
1562 static void wpa_eapol_key_dump(struct wpa_sm *sm,
1563 const struct wpa_eapol_key *key)
1565 #ifndef CONFIG_NO_STDOUT_DEBUG
1566 u16 key_info = WPA_GET_BE16(key->key_info);
1568 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1569 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1570 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
1571 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1572 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1573 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1574 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1575 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1576 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1577 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1578 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1579 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1580 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1581 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1582 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1583 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1584 " key_length=%u key_data_length=%u",
1585 WPA_GET_BE16(key->key_length),
1586 WPA_GET_BE16(key->key_data_length));
1587 wpa_hexdump(MSG_DEBUG, " replay_counter",
1588 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1589 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1590 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1591 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1592 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
1593 wpa_hexdump(MSG_DEBUG, " key_mic", key->key_mic, 16);
1594 #endif /* CONFIG_NO_STDOUT_DEBUG */
1599 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1600 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1601 * @src_addr: Source MAC address of the EAPOL packet
1602 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1603 * @len: Length of the EAPOL frame
1604 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1606 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1607 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1608 * only processing WPA and WPA2 EAPOL-Key frames.
1610 * The received EAPOL-Key packets are validated and valid packets are replied
1611 * to. In addition, key material (PTK, GTK) is configured at the end of a
1612 * successful key handshake.
1614 int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1615 const u8 *buf, size_t len)
1617 size_t plen, data_len, key_data_len;
1618 const struct ieee802_1x_hdr *hdr;
1619 struct wpa_eapol_key *key;
1623 struct wpa_peerkey *peerkey = NULL;
1626 #ifdef CONFIG_IEEE80211R
1627 sm->ft_completed = 0;
1628 #endif /* CONFIG_IEEE80211R */
1630 if (len < sizeof(*hdr) + sizeof(*key)) {
1631 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1632 "WPA: EAPOL frame too short to be a WPA "
1633 "EAPOL-Key (len %lu, expecting at least %lu)",
1634 (unsigned long) len,
1635 (unsigned long) sizeof(*hdr) + sizeof(*key));
1639 hdr = (const struct ieee802_1x_hdr *) buf;
1640 plen = be_to_host16(hdr->length);
1641 data_len = plen + sizeof(*hdr);
1642 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1643 "IEEE 802.1X RX: version=%d type=%d length=%lu",
1644 hdr->version, hdr->type, (unsigned long) plen);
1646 if (hdr->version < EAPOL_VERSION) {
1647 /* TODO: backwards compatibility */
1649 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
1650 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1651 "WPA: EAPOL frame (type %u) discarded, "
1652 "not a Key frame", hdr->type);
1656 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
1657 if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
1658 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1659 "WPA: EAPOL frame payload size %lu "
1660 "invalid (frame size %lu)",
1661 (unsigned long) plen, (unsigned long) len);
1665 if (data_len < len) {
1666 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1667 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
1668 (unsigned long) len - data_len);
1672 * Make a copy of the frame since we need to modify the buffer during
1673 * MAC validation and Key Data decryption.
1675 tmp = os_malloc(data_len);
1678 os_memcpy(tmp, buf, data_len);
1679 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
1680 key_data = (u8 *) (key + 1);
1682 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
1684 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1685 "WPA: EAPOL-Key type (%d) unknown, discarded",
1690 wpa_eapol_key_dump(sm, key);
1692 key_data_len = WPA_GET_BE16(key->key_data_length);
1693 if (key_data_len > plen - sizeof(struct wpa_eapol_key)) {
1694 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
1695 "frame - key_data overflow (%u > %u)",
1696 (unsigned int) key_data_len,
1697 (unsigned int) (plen - sizeof(struct wpa_eapol_key)));
1701 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
1702 key_info = WPA_GET_BE16(key->key_info);
1703 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1704 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
1705 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
1706 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1707 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
1708 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
1709 sm->key_mgmt != WPA_KEY_MGMT_OSEN) {
1710 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1711 "WPA: Unsupported EAPOL-Key descriptor version %d",
1716 if (sm->key_mgmt == WPA_KEY_MGMT_OSEN &&
1717 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1718 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1719 "OSEN: Unsupported EAPOL-Key descriptor version %d",
1724 #ifdef CONFIG_IEEE80211R
1725 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
1726 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
1727 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1728 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1729 "FT: AP did not use AES-128-CMAC");
1733 #endif /* CONFIG_IEEE80211R */
1734 #ifdef CONFIG_IEEE80211W
1735 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
1736 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1737 sm->key_mgmt != WPA_KEY_MGMT_OSEN) {
1738 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1739 "WPA: AP did not use the "
1740 "negotiated AES-128-CMAC");
1744 #endif /* CONFIG_IEEE80211W */
1745 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
1746 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1747 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1748 "WPA: CCMP is used, but EAPOL-Key "
1749 "descriptor version (%d) is not 2", ver);
1750 if (sm->group_cipher != WPA_CIPHER_CCMP &&
1751 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1752 /* Earlier versions of IEEE 802.11i did not explicitly
1753 * require version 2 descriptor for all EAPOL-Key
1754 * packets, so allow group keys to use version 1 if
1755 * CCMP is not used for them. */
1756 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1757 "WPA: Backwards compatibility: allow invalid "
1758 "version for non-CCMP group keys");
1759 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1760 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1761 "WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
1764 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
1765 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1766 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1767 "WPA: GCMP is used, but EAPOL-Key "
1768 "descriptor version (%d) is not 2", ver);
1772 #ifdef CONFIG_PEERKEY
1773 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
1774 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
1778 if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
1779 if (!peerkey->initiator && peerkey->replay_counter_set &&
1780 os_memcmp(key->replay_counter, peerkey->replay_counter,
1781 WPA_REPLAY_COUNTER_LEN) <= 0) {
1782 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1783 "RSN: EAPOL-Key Replay Counter did not "
1784 "increase (STK) - dropping packet");
1786 } else if (peerkey->initiator) {
1787 u8 _tmp[WPA_REPLAY_COUNTER_LEN];
1788 os_memcpy(_tmp, key->replay_counter,
1789 WPA_REPLAY_COUNTER_LEN);
1790 inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
1791 if (os_memcmp(_tmp, peerkey->replay_counter,
1792 WPA_REPLAY_COUNTER_LEN) != 0) {
1793 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1794 "RSN: EAPOL-Key Replay "
1795 "Counter did not match (STK) - "
1802 if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
1803 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1804 "RSN: Ack bit in key_info from STK peer");
1807 #endif /* CONFIG_PEERKEY */
1809 if (!peerkey && sm->rx_replay_counter_set &&
1810 os_memcmp(key->replay_counter, sm->rx_replay_counter,
1811 WPA_REPLAY_COUNTER_LEN) <= 0) {
1812 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1813 "WPA: EAPOL-Key Replay Counter did not increase - "
1818 if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
1819 #ifdef CONFIG_PEERKEY
1820 && (peerkey == NULL || !peerkey->initiator)
1821 #endif /* CONFIG_PEERKEY */
1823 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1824 "WPA: No Ack bit in key_info");
1828 if (key_info & WPA_KEY_INFO_REQUEST) {
1829 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1830 "WPA: EAPOL-Key with Request bit - dropped");
1834 if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
1835 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
1838 #ifdef CONFIG_PEERKEY
1839 if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
1840 peerkey_verify_eapol_key_mic(sm, peerkey, key, ver, tmp, data_len))
1842 #endif /* CONFIG_PEERKEY */
1844 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
1845 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1846 if (wpa_supplicant_decrypt_key_data(sm, key, ver, key_data,
1851 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1852 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
1853 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1854 "WPA: Ignored EAPOL-Key (Pairwise) with "
1855 "non-zero key index");
1859 /* PeerKey 4-Way Handshake */
1860 peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver,
1861 key_data, key_data_len);
1862 } else if (key_info & WPA_KEY_INFO_MIC) {
1863 /* 3/4 4-Way Handshake */
1864 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
1867 /* 1/4 4-Way Handshake */
1868 wpa_supplicant_process_1_of_4(sm, src_addr, key,
1872 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
1873 /* PeerKey SMK Handshake */
1874 peerkey_rx_eapol_smk(sm, src_addr, key, key_data_len, key_info,
1877 if (key_info & WPA_KEY_INFO_MIC) {
1878 /* 1/2 Group Key Handshake */
1879 wpa_supplicant_process_1_of_2(sm, src_addr, key,
1880 key_data, key_data_len,
1883 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1884 "WPA: EAPOL-Key (Group) without Mic bit - "
1897 #ifdef CONFIG_CTRL_IFACE
1898 static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
1900 switch (sm->key_mgmt) {
1901 case WPA_KEY_MGMT_IEEE8021X:
1902 return ((sm->proto == WPA_PROTO_RSN ||
1903 sm->proto == WPA_PROTO_OSEN) ?
1904 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
1905 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
1906 case WPA_KEY_MGMT_PSK:
1907 return (sm->proto == WPA_PROTO_RSN ?
1908 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
1909 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
1910 #ifdef CONFIG_IEEE80211R
1911 case WPA_KEY_MGMT_FT_IEEE8021X:
1912 return RSN_AUTH_KEY_MGMT_FT_802_1X;
1913 case WPA_KEY_MGMT_FT_PSK:
1914 return RSN_AUTH_KEY_MGMT_FT_PSK;
1915 #endif /* CONFIG_IEEE80211R */
1916 #ifdef CONFIG_IEEE80211W
1917 case WPA_KEY_MGMT_IEEE8021X_SHA256:
1918 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
1919 case WPA_KEY_MGMT_PSK_SHA256:
1920 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
1921 #endif /* CONFIG_IEEE80211W */
1922 case WPA_KEY_MGMT_CCKM:
1923 return (sm->proto == WPA_PROTO_RSN ?
1924 RSN_AUTH_KEY_MGMT_CCKM:
1925 WPA_AUTH_KEY_MGMT_CCKM);
1926 case WPA_KEY_MGMT_WPA_NONE:
1927 return WPA_AUTH_KEY_MGMT_NONE;
1928 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
1929 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
1936 #define RSN_SUITE "%02x-%02x-%02x-%d"
1937 #define RSN_SUITE_ARG(s) \
1938 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
1941 * wpa_sm_get_mib - Dump text list of MIB entries
1942 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1943 * @buf: Buffer for the list
1944 * @buflen: Length of the buffer
1945 * Returns: Number of bytes written to buffer
1947 * This function is used fetch dot11 MIB variables.
1949 int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
1951 char pmkid_txt[PMKID_LEN * 2 + 1];
1955 if (sm->cur_pmksa) {
1956 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
1957 sm->cur_pmksa->pmkid, PMKID_LEN);
1959 pmkid_txt[0] = '\0';
1961 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
1962 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
1963 sm->proto == WPA_PROTO_RSN)
1968 ret = os_snprintf(buf, buflen,
1969 "dot11RSNAOptionImplemented=TRUE\n"
1970 "dot11RSNAPreauthenticationImplemented=TRUE\n"
1971 "dot11RSNAEnabled=%s\n"
1972 "dot11RSNAPreauthenticationEnabled=%s\n"
1973 "dot11RSNAConfigVersion=%d\n"
1974 "dot11RSNAConfigPairwiseKeysSupported=5\n"
1975 "dot11RSNAConfigGroupCipherSize=%d\n"
1976 "dot11RSNAConfigPMKLifetime=%d\n"
1977 "dot11RSNAConfigPMKReauthThreshold=%d\n"
1978 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
1979 "dot11RSNAConfigSATimeout=%d\n",
1980 rsna ? "TRUE" : "FALSE",
1981 rsna ? "TRUE" : "FALSE",
1983 wpa_cipher_key_len(sm->group_cipher) * 8,
1984 sm->dot11RSNAConfigPMKLifetime,
1985 sm->dot11RSNAConfigPMKReauthThreshold,
1986 sm->dot11RSNAConfigSATimeout);
1987 if (ret < 0 || (size_t) ret >= buflen)
1992 buf + len, buflen - len,
1993 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
1994 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
1995 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
1996 "dot11RSNAPMKIDUsed=%s\n"
1997 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
1998 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
1999 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2000 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2001 "dot11RSNA4WayHandshakeFailures=%u\n",
2002 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
2003 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2004 sm->pairwise_cipher)),
2005 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2008 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
2009 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2010 sm->pairwise_cipher)),
2011 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2013 sm->dot11RSNA4WayHandshakeFailures);
2014 if (ret >= 0 && (size_t) ret < buflen)
2019 #endif /* CONFIG_CTRL_IFACE */
2022 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
2023 void *ctx, enum pmksa_free_reason reason)
2025 struct wpa_sm *sm = ctx;
2028 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2029 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2031 if (sm->cur_pmksa == entry) {
2032 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2033 "RSN: %s current PMKSA entry",
2034 reason == PMKSA_REPLACE ? "replaced" : "removed");
2035 pmksa_cache_clear_current(sm);
2038 * If an entry is simply being replaced, there's no need to
2039 * deauthenticate because it will be immediately re-added.
2040 * This happens when EAP authentication is completed again
2041 * (reauth or failed PMKSA caching attempt).
2043 if (reason != PMKSA_REPLACE)
2047 if (reason == PMKSA_EXPIRE &&
2048 (sm->pmk_len == entry->pmk_len &&
2049 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2050 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2051 "RSN: deauthenticating due to expired PMK");
2052 pmksa_cache_clear_current(sm);
2057 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2058 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2064 * wpa_sm_init - Initialize WPA state machine
2065 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2066 * Returns: Pointer to the allocated WPA state machine data
2068 * This function is used to allocate a new WPA state machine and the returned
2069 * value is passed to all WPA state machine calls.
2071 struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2075 sm = os_zalloc(sizeof(*sm));
2078 dl_list_init(&sm->pmksa_candidates);
2079 sm->renew_snonce = 1;
2082 sm->dot11RSNAConfigPMKLifetime = 43200;
2083 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2084 sm->dot11RSNAConfigSATimeout = 60;
2086 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2087 if (sm->pmksa == NULL) {
2088 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2089 "RSN: PMKSA cache initialization failed");
2099 * wpa_sm_deinit - Deinitialize WPA state machine
2100 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2102 void wpa_sm_deinit(struct wpa_sm *sm)
2106 pmksa_cache_deinit(sm->pmksa);
2107 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2108 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2109 os_free(sm->assoc_wpa_ie);
2110 os_free(sm->ap_wpa_ie);
2111 os_free(sm->ap_rsn_ie);
2114 #ifdef CONFIG_IEEE80211R
2115 os_free(sm->assoc_resp_ies);
2116 #endif /* CONFIG_IEEE80211R */
2122 * wpa_sm_notify_assoc - Notify WPA state machine about association
2123 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2124 * @bssid: The BSSID of the new association
2126 * This function is called to let WPA state machine know that the connection
2129 void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2136 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2137 "WPA: Association event - clear replay counter");
2138 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2139 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2140 sm->rx_replay_counter_set = 0;
2141 sm->renew_snonce = 1;
2142 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2143 rsn_preauth_deinit(sm);
2145 #ifdef CONFIG_IEEE80211R
2146 if (wpa_ft_is_completed(sm)) {
2148 * Clear portValid to kick EAPOL state machine to re-enter
2149 * AUTHENTICATED state to get the EAPOL port Authorized.
2151 eapol_sm_notify_portValid(sm->eapol, FALSE);
2152 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2154 /* Prepare for the next transition */
2155 wpa_ft_prepare_auth_request(sm, NULL);
2159 #endif /* CONFIG_IEEE80211R */
2163 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2164 * this is not part of a Fast BSS Transition.
2166 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
2168 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2170 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2175 #endif /* CONFIG_TDLS */
2178 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
2179 #endif /* CONFIG_P2P */
2184 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2185 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2187 * This function is called to let WPA state machine know that the connection
2188 * was lost. This will abort any existing pre-authentication session.
2190 void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2193 rsn_preauth_deinit(sm);
2194 pmksa_cache_clear_current(sm);
2195 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2196 sm->dot11RSNA4WayHandshakeFailures++;
2198 wpa_tdls_disassoc(sm);
2199 #endif /* CONFIG_TDLS */
2204 * wpa_sm_set_pmk - Set PMK
2205 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2207 * @pmk_len: The length of the new PMK in bytes
2208 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
2210 * Configure the PMK for WPA state machine.
2212 void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
2218 sm->pmk_len = pmk_len;
2219 os_memcpy(sm->pmk, pmk, pmk_len);
2221 #ifdef CONFIG_IEEE80211R
2222 /* Set XXKey to be PSK for FT key derivation */
2223 sm->xxkey_len = pmk_len;
2224 os_memcpy(sm->xxkey, pmk, pmk_len);
2225 #endif /* CONFIG_IEEE80211R */
2228 pmksa_cache_add(sm->pmksa, pmk, pmk_len, bssid, sm->own_addr,
2229 sm->network_ctx, sm->key_mgmt);
2235 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2236 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2238 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2241 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2246 if (sm->cur_pmksa) {
2247 sm->pmk_len = sm->cur_pmksa->pmk_len;
2248 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2250 sm->pmk_len = PMK_LEN;
2251 os_memset(sm->pmk, 0, PMK_LEN);
2257 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2258 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2259 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2261 void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2264 sm->fast_reauth = fast_reauth;
2269 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2270 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2271 * @scard_ctx: Context pointer for smartcard related callback functions
2273 void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2277 sm->scard_ctx = scard_ctx;
2278 if (sm->preauth_eapol)
2279 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2284 * wpa_sm_set_config - Notification of current configration change
2285 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2286 * @config: Pointer to current network configuration
2288 * Notify WPA state machine that configuration has changed. config will be
2289 * stored as a backpointer to network configuration. This can be %NULL to clear
2290 * the stored pointed.
2292 void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2298 sm->network_ctx = config->network_ctx;
2299 sm->peerkey_enabled = config->peerkey_enabled;
2300 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2301 sm->proactive_key_caching = config->proactive_key_caching;
2302 sm->eap_workaround = config->eap_workaround;
2303 sm->eap_conf_ctx = config->eap_conf_ctx;
2305 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2306 sm->ssid_len = config->ssid_len;
2309 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
2310 sm->p2p = config->p2p;
2312 sm->network_ctx = NULL;
2313 sm->peerkey_enabled = 0;
2314 sm->allowed_pairwise_cipher = 0;
2315 sm->proactive_key_caching = 0;
2316 sm->eap_workaround = 0;
2317 sm->eap_conf_ctx = NULL;
2319 sm->wpa_ptk_rekey = 0;
2326 * wpa_sm_set_own_addr - Set own MAC address
2327 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2328 * @addr: Own MAC address
2330 void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2333 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2338 * wpa_sm_set_ifname - Set network interface name
2339 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2340 * @ifname: Interface name
2341 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2343 void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2344 const char *bridge_ifname)
2347 sm->ifname = ifname;
2348 sm->bridge_ifname = bridge_ifname;
2354 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2355 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2356 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2358 void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2366 * wpa_sm_set_param - Set WPA state machine parameters
2367 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2368 * @param: Parameter field
2369 * @value: Parameter value
2370 * Returns: 0 on success, -1 on failure
2372 int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2381 case RSNA_PMK_LIFETIME:
2383 sm->dot11RSNAConfigPMKLifetime = value;
2387 case RSNA_PMK_REAUTH_THRESHOLD:
2388 if (value > 0 && value <= 100)
2389 sm->dot11RSNAConfigPMKReauthThreshold = value;
2393 case RSNA_SA_TIMEOUT:
2395 sm->dot11RSNAConfigSATimeout = value;
2399 case WPA_PARAM_PROTO:
2402 case WPA_PARAM_PAIRWISE:
2403 sm->pairwise_cipher = value;
2405 case WPA_PARAM_GROUP:
2406 sm->group_cipher = value;
2408 case WPA_PARAM_KEY_MGMT:
2409 sm->key_mgmt = value;
2411 #ifdef CONFIG_IEEE80211W
2412 case WPA_PARAM_MGMT_GROUP:
2413 sm->mgmt_group_cipher = value;
2415 #endif /* CONFIG_IEEE80211W */
2416 case WPA_PARAM_RSN_ENABLED:
2417 sm->rsn_enabled = value;
2431 * wpa_sm_get_status - Get WPA state machine
2432 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2433 * @buf: Buffer for status information
2434 * @buflen: Maximum buffer length
2435 * @verbose: Whether to include verbose status information
2436 * Returns: Number of bytes written to buf.
2438 * Query WPA state machine for status information. This function fills in
2439 * a text area with current status information. If the buffer (buf) is not
2440 * large enough, status information will be truncated to fit the buffer.
2442 int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2445 char *pos = buf, *end = buf + buflen;
2448 ret = os_snprintf(pos, end - pos,
2449 "pairwise_cipher=%s\n"
2452 wpa_cipher_txt(sm->pairwise_cipher),
2453 wpa_cipher_txt(sm->group_cipher),
2454 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
2455 if (ret < 0 || ret >= end - pos)
2459 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
2460 struct wpa_ie_data rsn;
2461 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
2463 rsn.capabilities & (WPA_CAPABILITY_MFPR |
2464 WPA_CAPABILITY_MFPC)) {
2465 ret = os_snprintf(pos, end - pos, "pmf=%d\n",
2467 WPA_CAPABILITY_MFPR) ? 2 : 1);
2468 if (ret < 0 || ret >= end - pos)
2478 int wpa_sm_pmf_enabled(struct wpa_sm *sm)
2480 struct wpa_ie_data rsn;
2482 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
2485 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
2486 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
2494 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2495 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2496 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2497 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2498 * Returns: 0 on success, -1 on failure
2500 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2508 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2513 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2514 wpa_ie, *wpa_ie_len);
2516 if (sm->assoc_wpa_ie == NULL) {
2518 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2519 * the correct version of the IE even if PMKSA caching is
2520 * aborted (which would remove PMKID from IE generation).
2522 sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2523 if (sm->assoc_wpa_ie == NULL)
2526 os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2527 sm->assoc_wpa_ie_len = *wpa_ie_len;
2535 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2536 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2537 * @ie: Pointer to IE data (starting from id)
2539 * Returns: 0 on success, -1 on failure
2541 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2542 * Request frame. The IE will be used to override the default value generated
2543 * with wpa_sm_set_assoc_wpa_ie_default().
2545 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2550 os_free(sm->assoc_wpa_ie);
2551 if (ie == NULL || len == 0) {
2552 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2553 "WPA: clearing own WPA/RSN IE");
2554 sm->assoc_wpa_ie = NULL;
2555 sm->assoc_wpa_ie_len = 0;
2557 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2558 sm->assoc_wpa_ie = os_malloc(len);
2559 if (sm->assoc_wpa_ie == NULL)
2562 os_memcpy(sm->assoc_wpa_ie, ie, len);
2563 sm->assoc_wpa_ie_len = len;
2571 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2572 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2573 * @ie: Pointer to IE data (starting from id)
2575 * Returns: 0 on success, -1 on failure
2577 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2580 int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2585 os_free(sm->ap_wpa_ie);
2586 if (ie == NULL || len == 0) {
2587 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2588 "WPA: clearing AP WPA IE");
2589 sm->ap_wpa_ie = NULL;
2590 sm->ap_wpa_ie_len = 0;
2592 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2593 sm->ap_wpa_ie = os_malloc(len);
2594 if (sm->ap_wpa_ie == NULL)
2597 os_memcpy(sm->ap_wpa_ie, ie, len);
2598 sm->ap_wpa_ie_len = len;
2606 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2607 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2608 * @ie: Pointer to IE data (starting from id)
2610 * Returns: 0 on success, -1 on failure
2612 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2615 int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2620 os_free(sm->ap_rsn_ie);
2621 if (ie == NULL || len == 0) {
2622 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2623 "WPA: clearing AP RSN IE");
2624 sm->ap_rsn_ie = NULL;
2625 sm->ap_rsn_ie_len = 0;
2627 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2628 sm->ap_rsn_ie = os_malloc(len);
2629 if (sm->ap_rsn_ie == NULL)
2632 os_memcpy(sm->ap_rsn_ie, ie, len);
2633 sm->ap_rsn_ie_len = len;
2641 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2642 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2643 * @data: Pointer to data area for parsing results
2644 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2646 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2647 * parsed data into data.
2649 int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2654 if (sm->assoc_wpa_ie == NULL) {
2655 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2656 "WPA: No WPA/RSN IE available from association info");
2659 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
2665 int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
2667 return pmksa_cache_list(sm->pmksa, buf, len);
2671 #ifdef CONFIG_TESTING_OPTIONS
2672 void wpa_sm_drop_sa(struct wpa_sm *sm)
2674 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
2677 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2678 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2679 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2681 #endif /* CONFIG_TESTING_OPTIONS */
2684 int wpa_sm_has_ptk(struct wpa_sm *sm)
2692 void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
2694 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
2698 void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
2700 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
2705 int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
2708 u8 keylen; /* plaintext key len */
2711 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
2712 struct wpa_gtk_data gd;
2714 os_memset(&gd, 0, sizeof(gd));
2715 keylen = wpa_cipher_key_len(sm->group_cipher);
2716 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
2717 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
2718 if (gd.alg == WPA_ALG_NONE) {
2719 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
2724 keyinfo = WPA_GET_LE16(buf + 2);
2725 gd.gtk_len = keylen;
2726 if (gd.gtk_len != buf[4]) {
2727 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
2728 gd.gtk_len, buf[4]);
2731 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
2732 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
2733 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
2735 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
2737 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
2738 gd.gtk, gd.gtk_len);
2739 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc)) {
2740 os_memset(&gd, 0, sizeof(gd));
2741 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
2745 os_memset(&gd, 0, sizeof(gd));
2746 #ifdef CONFIG_IEEE80211W
2747 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
2748 struct wpa_igtk_kde igd;
2751 os_memset(&igd, 0, sizeof(igd));
2752 keylen = wpa_cipher_key_len(sm->mgmt_group_cipher);
2753 os_memcpy(igd.keyid, buf + 2, 2);
2754 os_memcpy(igd.pn, buf + 4, 6);
2756 keyidx = WPA_GET_LE16(igd.keyid);
2757 os_memcpy(igd.igtk, buf + 10, keylen);
2759 wpa_hexdump_key(MSG_DEBUG, "Install IGTK (WNM SLEEP)",
2761 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
2762 broadcast_ether_addr,
2763 keyidx, 0, igd.pn, sizeof(igd.pn),
2764 igd.igtk, keylen) < 0) {
2765 wpa_printf(MSG_DEBUG, "Failed to install the IGTK in "
2767 os_memset(&igd, 0, sizeof(igd));
2770 os_memset(&igd, 0, sizeof(igd));
2771 #endif /* CONFIG_IEEE80211W */
2773 wpa_printf(MSG_DEBUG, "Unknown element id");
2779 #endif /* CONFIG_WNM */
2782 #ifdef CONFIG_PEERKEY
2783 int wpa_sm_rx_eapol_peerkey(struct wpa_sm *sm, const u8 *src_addr,
2784 const u8 *buf, size_t len)
2786 struct wpa_peerkey *peerkey;
2788 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
2789 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
2796 wpa_sm_rx_eapol(sm, src_addr, buf, len);
2800 #endif /* CONFIG_PEERKEY */
2805 int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
2807 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
2809 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
2813 #endif /* CONFIG_P2P */
2816 void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
2818 if (rx_replay_counter == NULL)
2821 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
2822 WPA_REPLAY_COUNTER_LEN);
2823 sm->rx_replay_counter_set = 1;
2824 wpa_printf(MSG_DEBUG, "Updated key replay counter");
2828 void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm, const u8 *ptk_kck,
2832 os_memcpy(sm->ptk.kck, ptk_kck, 16);
2833 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
2836 os_memcpy(sm->ptk.kek, ptk_kek, 16);
2837 wpa_printf(MSG_DEBUG, "Updated PTK KEK");