2 * WPA Supplicant - WPA state machine and EAPOL-Key processing
3 * Copyright (c) 2003-2015, 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 * @kck_len: KCK length in octets
31 * @ver: Version field from Key Info
32 * @dest: Destination address for the frame
33 * @proto: Ethertype (usually ETH_P_EAPOL)
34 * @msg: EAPOL-Key message
35 * @msg_len: Length of message
36 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
38 void wpa_eapol_key_send(struct wpa_sm *sm, const u8 *kck, size_t kck_len,
39 int ver, const u8 *dest, u16 proto,
40 u8 *msg, size_t msg_len, u8 *key_mic)
44 if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
46 * Association event was not yet received; try to fetch
47 * BSSID from the driver.
49 if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
50 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
51 "WPA: Failed to read BSSID for "
52 "EAPOL-Key destination address");
55 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
56 "WPA: Use BSSID (" MACSTR
57 ") as the destination for EAPOL-Key",
62 wpa_eapol_key_mic(kck, kck_len, sm->key_mgmt, ver, msg, msg_len,
64 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
65 "WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC",
69 wpa_hexdump_key(MSG_DEBUG, "WPA: KCK", kck, kck_len);
70 wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC", key_mic, mic_len);
71 wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
72 wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
73 eapol_sm_notify_tx_eapol_key(sm->eapol);
80 * wpa_sm_key_request - Send EAPOL-Key Request
81 * @sm: Pointer to WPA state machine data from wpa_sm_init()
82 * @error: Indicate whether this is an Michael MIC error report
83 * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
85 * Send an EAPOL-Key Request to the current authenticator. This function is
86 * used to request rekeying and it is usually called when a local Michael MIC
87 * failure is detected.
89 void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
92 struct wpa_eapol_key *reply;
94 u8 bssid[ETH_ALEN], *rbuf;
96 if (sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
97 wpa_key_mgmt_suite_b(sm->key_mgmt))
98 ver = WPA_KEY_INFO_TYPE_AKM_DEFINED;
99 else if (wpa_key_mgmt_ft(sm->key_mgmt) ||
100 wpa_key_mgmt_sha256(sm->key_mgmt))
101 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
102 else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
103 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
105 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
107 if (wpa_sm_get_bssid(sm, bssid) < 0) {
108 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
109 "Failed to read BSSID for EAPOL-Key request");
113 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
114 sizeof(*reply), &rlen, (void *) &reply);
118 reply->type = (sm->proto == WPA_PROTO_RSN ||
119 sm->proto == WPA_PROTO_OSEN) ?
120 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
121 key_info = WPA_KEY_INFO_REQUEST | ver;
123 key_info |= WPA_KEY_INFO_MIC;
125 key_info |= WPA_KEY_INFO_ERROR;
127 key_info |= WPA_KEY_INFO_KEY_TYPE;
128 WPA_PUT_BE16(reply->key_info, key_info);
129 WPA_PUT_BE16(reply->key_length, 0);
130 os_memcpy(reply->replay_counter, sm->request_counter,
131 WPA_REPLAY_COUNTER_LEN);
132 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
134 WPA_PUT_BE16(reply->key_data_length, 0);
136 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
137 "WPA: Sending EAPOL-Key Request (error=%d "
138 "pairwise=%d ptk_set=%d len=%lu)",
139 error, pairwise, sm->ptk_set, (unsigned long) rlen);
140 wpa_eapol_key_send(sm, sm->ptk.kck, sm->ptk.kck_len, ver, bssid,
141 ETH_P_EAPOL, rbuf, rlen,
142 key_info & WPA_KEY_INFO_MIC ? reply->key_mic : NULL);
146 static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm *sm)
148 #ifdef CONFIG_IEEE80211R
149 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
150 if (wpa_sm_key_mgmt_set_pmk(sm, sm->xxkey, sm->xxkey_len))
151 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
152 "RSN: Cannot set low order 256 bits of MSK for key management offload");
154 #endif /* CONFIG_IEEE80211R */
155 if (wpa_sm_key_mgmt_set_pmk(sm, sm->pmk, sm->pmk_len))
156 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
157 "RSN: Cannot set PMK for key management offload");
158 #ifdef CONFIG_IEEE80211R
160 #endif /* CONFIG_IEEE80211R */
164 static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
165 const unsigned char *src_addr,
168 int abort_cached = 0;
170 if (pmkid && !sm->cur_pmksa) {
171 /* When using drivers that generate RSN IE, wpa_supplicant may
172 * not have enough time to get the association information
173 * event before receiving this 1/4 message, so try to find a
174 * matching PMKSA cache entry here. */
175 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid,
178 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
179 "RSN: found matching PMKID from PMKSA cache");
181 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
182 "RSN: no matching PMKID found");
187 if (pmkid && sm->cur_pmksa &&
188 os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
189 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
190 wpa_sm_set_pmk_from_pmksa(sm);
191 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
192 sm->pmk, sm->pmk_len);
193 eapol_sm_notify_cached(sm->eapol);
194 #ifdef CONFIG_IEEE80211R
196 #endif /* CONFIG_IEEE80211R */
197 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
200 res = eapol_sm_get_key(sm->eapol, sm->pmk, PMK_LEN);
203 * EAP-LEAP is an exception from other EAP methods: it
204 * uses only 16-byte PMK.
206 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
209 #ifdef CONFIG_IEEE80211R
211 if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
213 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
214 sm->xxkey_len = PMK_LEN;
215 os_memset(buf, 0, sizeof(buf));
217 #endif /* CONFIG_IEEE80211R */
220 struct rsn_pmksa_cache_entry *sa = NULL;
221 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
222 "machines", sm->pmk, pmk_len);
223 sm->pmk_len = pmk_len;
224 wpa_supplicant_key_mgmt_set_pmk(sm);
225 if (sm->proto == WPA_PROTO_RSN &&
226 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
227 !wpa_key_mgmt_ft(sm->key_mgmt)) {
228 sa = pmksa_cache_add(sm->pmksa,
231 src_addr, sm->own_addr,
235 if (!sm->cur_pmksa && pmkid &&
236 pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL))
238 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
239 "RSN: the new PMK matches with the "
247 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
248 "WPA: Failed to get master session key from "
249 "EAPOL state machines - key handshake "
252 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
253 "RSN: Cancelled PMKSA caching "
255 sm->cur_pmksa = NULL;
257 } else if (!abort_cached) {
263 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
264 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
265 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
267 /* Send EAPOL-Start to trigger full EAP authentication. */
271 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
272 "RSN: no PMKSA entry found - trigger "
273 "full EAP authentication");
274 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
275 NULL, 0, &buflen, NULL);
277 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
291 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
292 * @sm: Pointer to WPA state machine data from wpa_sm_init()
293 * @dst: Destination address for the frame
294 * @key: Pointer to the EAPOL-Key frame header
295 * @ver: Version bits from EAPOL-Key Key Info
296 * @nonce: Nonce value for the EAPOL-Key frame
297 * @wpa_ie: WPA/RSN IE
298 * @wpa_ie_len: Length of the WPA/RSN IE
299 * @ptk: PTK to use for keyed hash and encryption
300 * Returns: 0 on success, -1 on failure
302 int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
303 const struct wpa_eapol_key *key,
304 int ver, const u8 *nonce,
305 const u8 *wpa_ie, size_t wpa_ie_len,
309 struct wpa_eapol_key *reply;
311 u8 *rsn_ie_buf = NULL;
313 if (wpa_ie == NULL) {
314 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
315 "cannot generate msg 2/4");
319 #ifdef CONFIG_IEEE80211R
320 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
324 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
325 * FTIE from (Re)Association Response.
327 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
328 sm->assoc_resp_ies_len);
329 if (rsn_ie_buf == NULL)
331 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
332 res = wpa_insert_pmkid(rsn_ie_buf, wpa_ie_len,
340 if (sm->assoc_resp_ies) {
341 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
342 sm->assoc_resp_ies_len);
343 wpa_ie_len += sm->assoc_resp_ies_len;
348 #endif /* CONFIG_IEEE80211R */
350 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
352 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
353 NULL, sizeof(*reply) + wpa_ie_len,
354 &rlen, (void *) &reply);
360 reply->type = (sm->proto == WPA_PROTO_RSN ||
361 sm->proto == WPA_PROTO_OSEN) ?
362 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
363 WPA_PUT_BE16(reply->key_info,
364 ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC);
365 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
366 WPA_PUT_BE16(reply->key_length, 0);
368 os_memcpy(reply->key_length, key->key_length, 2);
369 os_memcpy(reply->replay_counter, key->replay_counter,
370 WPA_REPLAY_COUNTER_LEN);
371 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
372 WPA_REPLAY_COUNTER_LEN);
374 WPA_PUT_BE16(reply->key_data_length, wpa_ie_len);
375 os_memcpy(reply + 1, wpa_ie, wpa_ie_len);
378 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
380 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
381 wpa_eapol_key_send(sm, ptk->kck, ptk->kck_len, ver, dst, ETH_P_EAPOL,
382 rbuf, rlen, reply->key_mic);
388 static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
389 const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
391 #ifdef CONFIG_IEEE80211R
392 if (wpa_key_mgmt_ft(sm->key_mgmt))
393 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
394 #endif /* CONFIG_IEEE80211R */
396 return wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
397 sm->own_addr, sm->bssid, sm->snonce,
398 key->key_nonce, ptk, sm->key_mgmt,
399 sm->pairwise_cipher);
403 static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
404 const unsigned char *src_addr,
405 const struct wpa_eapol_key *key,
406 u16 ver, const u8 *key_data,
409 struct wpa_eapol_ie_parse ie;
412 u8 *kde, *kde_buf = NULL;
415 if (wpa_sm_get_network_ctx(sm) == NULL) {
416 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
417 "found (msg 1 of 4)");
421 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
422 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of 4-Way "
423 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
425 os_memset(&ie, 0, sizeof(ie));
427 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
428 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
429 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
430 key_data, key_data_len);
431 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
434 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
435 "Authenticator", ie.pmkid, PMKID_LEN);
439 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
441 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
442 "msg 1/4 - requesting full EAP authentication");
448 if (sm->renew_snonce) {
449 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
450 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
451 "WPA: Failed to get random data for SNonce");
454 sm->renew_snonce = 0;
455 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
456 sm->snonce, WPA_NONCE_LEN);
459 /* Calculate PTK which will be stored as a temporary PTK until it has
460 * been verified when processing message 3/4. */
462 wpa_derive_ptk(sm, src_addr, key, ptk);
463 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
465 /* Supplicant: swap tx/rx Mic keys */
466 os_memcpy(buf, &ptk->tk[16], 8);
467 os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
468 os_memcpy(&ptk->tk[24], buf, 8);
469 os_memset(buf, 0, sizeof(buf));
473 kde = sm->assoc_wpa_ie;
474 kde_len = sm->assoc_wpa_ie_len;
478 kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 1);
481 wpa_printf(MSG_DEBUG, "P2P: Add IP Address Request KDE "
482 "into EAPOL-Key 2/4");
483 os_memcpy(kde_buf, kde, kde_len);
486 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
487 *pos++ = RSN_SELECTOR_LEN + 1;
488 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
489 pos += RSN_SELECTOR_LEN;
494 #endif /* CONFIG_P2P */
496 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
501 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
506 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
510 static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
512 struct wpa_sm *sm = eloop_ctx;
513 rsn_preauth_candidate_process(sm);
517 static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
518 const u8 *addr, int secure)
520 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
521 "WPA: Key negotiation completed with "
522 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
523 wpa_cipher_txt(sm->pairwise_cipher),
524 wpa_cipher_txt(sm->group_cipher));
525 wpa_sm_cancel_auth_timeout(sm);
526 wpa_sm_set_state(sm, WPA_COMPLETED);
529 wpa_sm_mlme_setprotection(
530 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
531 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
532 eapol_sm_notify_portValid(sm->eapol, TRUE);
533 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt))
534 eapol_sm_notify_eap_success(sm->eapol, TRUE);
536 * Start preauthentication after a short wait to avoid a
537 * possible race condition between the data receive and key
538 * configuration after the 4-Way Handshake. This increases the
539 * likelihood of the first preauth EAPOL-Start frame getting to
542 eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
545 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
546 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
547 "RSN: Authenticator accepted "
548 "opportunistic PMKSA entry - marking it valid");
549 sm->cur_pmksa->opportunistic = 0;
552 #ifdef CONFIG_IEEE80211R
553 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
554 /* Prepare for the next transition */
555 wpa_ft_prepare_auth_request(sm, NULL);
557 #endif /* CONFIG_IEEE80211R */
561 static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
563 struct wpa_sm *sm = eloop_ctx;
564 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
565 wpa_sm_key_request(sm, 0, 1);
569 static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
570 const struct wpa_eapol_key *key)
575 u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
577 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
578 "WPA: Installing PTK to the driver");
580 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
581 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
582 "Suite: NONE - do not use pairwise keys");
586 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
587 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
588 "WPA: Unsupported pairwise cipher %d",
589 sm->pairwise_cipher);
593 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
594 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
595 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
597 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
600 key_rsc = key->key_rsc;
601 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
604 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
605 sm->ptk.tk, keylen) < 0) {
606 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
607 "WPA: Failed to set PTK to the "
608 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
609 alg, keylen, MAC2STR(sm->bssid));
613 /* TK is not needed anymore in supplicant */
614 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
616 if (sm->wpa_ptk_rekey) {
617 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
618 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
626 static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
628 int keylen, int maxkeylen,
634 *alg = wpa_cipher_to_alg(group_cipher);
635 if (*alg == WPA_ALG_NONE) {
636 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
637 "WPA: Unsupported Group Cipher %d",
641 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
643 klen = wpa_cipher_key_len(group_cipher);
644 if (keylen != klen || maxkeylen < klen) {
645 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
646 "WPA: Unsupported %s Group Cipher key length %d (%d)",
647 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
654 struct wpa_gtk_data {
656 int tx, key_rsc_len, keyidx;
662 static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
663 const struct wpa_gtk_data *gd,
666 const u8 *_gtk = gd->gtk;
669 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
670 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
671 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
672 gd->keyidx, gd->tx, gd->gtk_len);
673 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
674 if (sm->group_cipher == WPA_CIPHER_TKIP) {
675 /* Swap Tx/Rx keys for Michael MIC */
676 os_memcpy(gtk_buf, gd->gtk, 16);
677 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
678 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
681 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
682 if (wpa_sm_set_key(sm, gd->alg, NULL,
683 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
684 _gtk, gd->gtk_len) < 0) {
685 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
686 "WPA: Failed to set GTK to the driver "
688 os_memset(gtk_buf, 0, sizeof(gtk_buf));
691 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
692 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
693 _gtk, gd->gtk_len) < 0) {
694 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
695 "WPA: Failed to set GTK to "
696 "the driver (alg=%d keylen=%d keyidx=%d)",
697 gd->alg, gd->gtk_len, gd->keyidx);
698 os_memset(gtk_buf, 0, sizeof(gtk_buf));
701 os_memset(gtk_buf, 0, sizeof(gtk_buf));
707 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
710 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
711 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
712 * seemed to set this bit (incorrectly, since Tx is only when
713 * doing Group Key only APs) and without this workaround, the
714 * data connection does not work because wpa_supplicant
715 * configured non-zero keyidx to be used for unicast. */
716 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
717 "WPA: Tx bit set for GTK, but pairwise "
718 "keys are used - ignore Tx bit");
725 static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
726 const struct wpa_eapol_key *key,
727 const u8 *gtk, size_t gtk_len,
730 struct wpa_gtk_data gd;
733 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
735 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
736 * Reserved [bits 0-7]
740 os_memset(&gd, 0, sizeof(gd));
741 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
744 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
747 gd.keyidx = gtk[0] & 0x3;
748 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
749 !!(gtk[0] & BIT(2)));
753 os_memcpy(gd.gtk, gtk, gtk_len);
754 gd.gtk_len = gtk_len;
756 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
757 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
759 &gd.key_rsc_len, &gd.alg) ||
760 wpa_supplicant_install_gtk(sm, &gd, key->key_rsc))) {
761 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
762 "RSN: Failed to install GTK");
763 os_memset(&gd, 0, sizeof(gd));
766 os_memset(&gd, 0, sizeof(gd));
768 wpa_supplicant_key_neg_complete(sm, sm->bssid,
769 key_info & WPA_KEY_INFO_SECURE);
774 static int ieee80211w_set_keys(struct wpa_sm *sm,
775 struct wpa_eapol_ie_parse *ie)
777 #ifdef CONFIG_IEEE80211W
778 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
783 const struct wpa_igtk_kde *igtk;
785 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
786 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
788 igtk = (const struct wpa_igtk_kde *) ie->igtk;
789 keyidx = WPA_GET_LE16(igtk->keyid);
790 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: IGTK keyid %d "
791 "pn %02x%02x%02x%02x%02x%02x",
792 keyidx, MAC2STR(igtk->pn));
793 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK",
796 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
797 "WPA: Invalid IGTK KeyID %d", keyidx);
800 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
801 broadcast_ether_addr,
802 keyidx, 0, igtk->pn, sizeof(igtk->pn),
803 igtk->igtk, len) < 0) {
804 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
805 "WPA: Failed to configure IGTK to the driver");
811 #else /* CONFIG_IEEE80211W */
813 #endif /* CONFIG_IEEE80211W */
817 static void wpa_report_ie_mismatch(struct wpa_sm *sm,
818 const char *reason, const u8 *src_addr,
819 const u8 *wpa_ie, size_t wpa_ie_len,
820 const u8 *rsn_ie, size_t rsn_ie_len)
822 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
823 reason, MAC2STR(src_addr));
826 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
827 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
830 if (!sm->ap_wpa_ie) {
831 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
832 "WPA: No WPA IE in Beacon/ProbeResp");
834 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
839 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
840 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
843 if (!sm->ap_rsn_ie) {
844 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
845 "WPA: No RSN IE in Beacon/ProbeResp");
847 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
851 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
855 #ifdef CONFIG_IEEE80211R
857 static int ft_validate_mdie(struct wpa_sm *sm,
858 const unsigned char *src_addr,
859 struct wpa_eapol_ie_parse *ie,
860 const u8 *assoc_resp_mdie)
862 struct rsn_mdie *mdie;
864 mdie = (struct rsn_mdie *) (ie->mdie + 2);
865 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
866 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
867 MOBILITY_DOMAIN_ID_LEN) != 0) {
868 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
869 "not match with the current mobility domain");
873 if (assoc_resp_mdie &&
874 (assoc_resp_mdie[1] != ie->mdie[1] ||
875 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
876 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
877 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
878 ie->mdie, 2 + ie->mdie[1]);
879 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
880 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
888 static int ft_validate_ftie(struct wpa_sm *sm,
889 const unsigned char *src_addr,
890 struct wpa_eapol_ie_parse *ie,
891 const u8 *assoc_resp_ftie)
893 if (ie->ftie == NULL) {
894 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
895 "FT: No FTIE in EAPOL-Key msg 3/4");
899 if (assoc_resp_ftie == NULL)
902 if (assoc_resp_ftie[1] != ie->ftie[1] ||
903 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
904 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
905 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
906 ie->ftie, 2 + ie->ftie[1]);
907 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
908 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
916 static int ft_validate_rsnie(struct wpa_sm *sm,
917 const unsigned char *src_addr,
918 struct wpa_eapol_ie_parse *ie)
920 struct wpa_ie_data rsn;
926 * Verify that PMKR1Name from EAPOL-Key message 3/4
927 * matches with the value we derived.
929 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
930 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
931 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
932 "FT 4-way handshake message 3/4");
936 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
938 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
939 "FT: PMKR1Name mismatch in "
940 "FT 4-way handshake message 3/4");
941 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
942 rsn.pmkid, WPA_PMK_NAME_LEN);
943 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
944 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
952 static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
953 const unsigned char *src_addr,
954 struct wpa_eapol_ie_parse *ie)
956 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
958 if (sm->assoc_resp_ies) {
959 pos = sm->assoc_resp_ies;
960 end = pos + sm->assoc_resp_ies_len;
961 while (pos + 2 < end) {
962 if (pos + 2 + pos[1] > end)
965 case WLAN_EID_MOBILITY_DOMAIN:
968 case WLAN_EID_FAST_BSS_TRANSITION:
976 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
977 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
978 ft_validate_rsnie(sm, src_addr, ie) < 0)
984 #endif /* CONFIG_IEEE80211R */
987 static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
988 const unsigned char *src_addr,
989 struct wpa_eapol_ie_parse *ie)
991 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
992 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
993 "WPA: No WPA/RSN IE for this AP known. "
994 "Trying to get from scan results");
995 if (wpa_sm_get_beacon_ie(sm) < 0) {
996 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
997 "WPA: Could not find AP from "
1000 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1001 "WPA: Found the current AP from "
1002 "updated scan results");
1006 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1007 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1008 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1009 "with IE in Beacon/ProbeResp (no IE?)",
1010 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1011 ie->rsn_ie, ie->rsn_ie_len);
1015 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1016 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1017 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1018 (ie->rsn_ie && sm->ap_rsn_ie &&
1019 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1020 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1021 ie->rsn_ie, ie->rsn_ie_len))) {
1022 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1023 "with IE in Beacon/ProbeResp",
1024 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1025 ie->rsn_ie, ie->rsn_ie_len);
1029 if (sm->proto == WPA_PROTO_WPA &&
1030 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1031 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1032 "detected - RSN was enabled and RSN IE "
1033 "was in msg 3/4, but not in "
1035 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1036 ie->rsn_ie, ie->rsn_ie_len);
1040 #ifdef CONFIG_IEEE80211R
1041 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1042 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1044 #endif /* CONFIG_IEEE80211R */
1051 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1052 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1053 * @dst: Destination address for the frame
1054 * @key: Pointer to the EAPOL-Key frame header
1055 * @ver: Version bits from EAPOL-Key Key Info
1056 * @key_info: Key Info
1057 * @ptk: PTK to use for keyed hash and encryption
1058 * Returns: 0 on success, -1 on failure
1060 int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1061 const struct wpa_eapol_key *key,
1062 u16 ver, u16 key_info,
1063 struct wpa_ptk *ptk)
1066 struct wpa_eapol_key *reply;
1069 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1070 sizeof(*reply), &rlen, (void *) &reply);
1074 reply->type = (sm->proto == WPA_PROTO_RSN ||
1075 sm->proto == WPA_PROTO_OSEN) ?
1076 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1077 key_info &= WPA_KEY_INFO_SECURE;
1078 key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
1079 WPA_PUT_BE16(reply->key_info, key_info);
1080 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
1081 WPA_PUT_BE16(reply->key_length, 0);
1083 os_memcpy(reply->key_length, key->key_length, 2);
1084 os_memcpy(reply->replay_counter, key->replay_counter,
1085 WPA_REPLAY_COUNTER_LEN);
1087 WPA_PUT_BE16(reply->key_data_length, 0);
1089 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
1090 wpa_eapol_key_send(sm, ptk->kck, ptk->kck_len, ver, dst, ETH_P_EAPOL,
1091 rbuf, rlen, reply->key_mic);
1097 static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1098 const struct wpa_eapol_key *key,
1099 u16 ver, const u8 *key_data,
1100 size_t key_data_len)
1102 u16 key_info, keylen;
1103 struct wpa_eapol_ie_parse ie;
1105 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
1106 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way "
1107 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1109 key_info = WPA_GET_BE16(key->key_info);
1111 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1112 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
1114 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1115 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1116 "WPA: GTK IE in unencrypted key data");
1119 #ifdef CONFIG_IEEE80211W
1120 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1121 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1122 "WPA: IGTK KDE in unencrypted key data");
1127 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1128 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1129 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
1130 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1131 "WPA: Invalid IGTK KDE length %lu",
1132 (unsigned long) ie.igtk_len);
1135 #endif /* CONFIG_IEEE80211W */
1137 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1140 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1141 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1142 "WPA: ANonce from message 1 of 4-Way Handshake "
1143 "differs from 3 of 4-Way Handshake - drop packet (src="
1144 MACSTR ")", MAC2STR(sm->bssid));
1148 keylen = WPA_GET_BE16(key->key_length);
1149 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1150 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1151 "WPA: Invalid %s key length %d (src=" MACSTR
1152 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1153 MAC2STR(sm->bssid));
1158 if (ie.ip_addr_alloc) {
1159 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1160 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1161 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1163 #endif /* CONFIG_P2P */
1165 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
1170 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1171 * for the next 4-Way Handshake. If msg 3 is received again, the old
1172 * SNonce will still be used to avoid changing PTK. */
1173 sm->renew_snonce = 1;
1175 if (key_info & WPA_KEY_INFO_INSTALL) {
1176 if (wpa_supplicant_install_ptk(sm, key))
1180 if (key_info & WPA_KEY_INFO_SECURE) {
1181 wpa_sm_mlme_setprotection(
1182 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1183 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1184 eapol_sm_notify_portValid(sm->eapol, TRUE);
1186 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1188 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
1189 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1190 key_info & WPA_KEY_INFO_SECURE);
1191 } else if (ie.gtk &&
1192 wpa_supplicant_pairwise_gtk(sm, key,
1193 ie.gtk, ie.gtk_len, key_info) < 0) {
1194 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1195 "RSN: Failed to configure GTK");
1199 if (ieee80211w_set_keys(sm, &ie) < 0) {
1200 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1201 "RSN: Failed to configure IGTK");
1206 wpa_sm_set_rekey_offload(sm);
1208 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt)) {
1209 struct rsn_pmksa_cache_entry *sa;
1211 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
1212 sm->ptk.kck, sm->ptk.kck_len,
1213 sm->bssid, sm->own_addr,
1214 sm->network_ctx, sm->key_mgmt);
1222 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1226 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1230 struct wpa_gtk_data *gd)
1233 struct wpa_eapol_ie_parse ie;
1235 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
1236 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1238 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1239 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1240 "WPA: GTK IE in unencrypted key data");
1243 if (ie.gtk == NULL) {
1244 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1245 "WPA: No GTK IE in Group Key msg 1/2");
1248 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1250 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1251 gd->gtk_len, maxkeylen,
1252 &gd->key_rsc_len, &gd->alg))
1255 wpa_hexdump(MSG_DEBUG, "RSN: received GTK in group key handshake",
1256 ie.gtk, ie.gtk_len);
1257 gd->keyidx = ie.gtk[0] & 0x3;
1258 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1259 !!(ie.gtk[0] & BIT(2)));
1260 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1261 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1262 "RSN: Too long GTK in GTK IE (len=%lu)",
1263 (unsigned long) ie.gtk_len - 2);
1266 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1268 if (ieee80211w_set_keys(sm, &ie) < 0)
1269 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1270 "RSN: Failed to configure IGTK");
1276 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1277 const struct wpa_eapol_key *key,
1279 size_t key_data_len, u16 key_info,
1280 u16 ver, struct wpa_gtk_data *gd)
1285 gtk_len = WPA_GET_BE16(key->key_length);
1286 maxkeylen = key_data_len;
1287 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1288 if (maxkeylen < 8) {
1289 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1290 "WPA: Too short maxkeylen (%lu)",
1291 (unsigned long) maxkeylen);
1297 if (gtk_len > maxkeylen ||
1298 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1300 &gd->key_rsc_len, &gd->alg))
1303 gd->gtk_len = gtk_len;
1304 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1305 WPA_KEY_INFO_KEY_INDEX_SHIFT;
1306 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
1308 if (key_data_len > sizeof(gd->gtk)) {
1309 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1310 "WPA: RC4 key data too long (%lu)",
1311 (unsigned long) key_data_len);
1314 os_memcpy(ek, key->key_iv, 16);
1315 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
1316 os_memcpy(gd->gtk, key_data, key_data_len);
1317 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
1318 os_memset(ek, 0, sizeof(ek));
1319 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1323 os_memset(ek, 0, sizeof(ek));
1324 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1325 if (maxkeylen % 8) {
1326 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1327 "WPA: Unsupported AES-WRAP len %lu",
1328 (unsigned long) maxkeylen);
1331 if (maxkeylen > sizeof(gd->gtk)) {
1332 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1333 "WPA: AES-WRAP key data "
1334 "too long (keydatalen=%lu maxkeylen=%lu)",
1335 (unsigned long) key_data_len,
1336 (unsigned long) maxkeylen);
1339 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
1340 key_data, gd->gtk)) {
1341 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1342 "WPA: AES unwrap failed - could not decrypt "
1347 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1348 "WPA: Unsupported key_info type %d", ver);
1351 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1352 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1357 static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1358 const struct wpa_eapol_key *key,
1359 int ver, u16 key_info)
1362 struct wpa_eapol_key *reply;
1365 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1366 sizeof(*reply), &rlen, (void *) &reply);
1370 reply->type = (sm->proto == WPA_PROTO_RSN ||
1371 sm->proto == WPA_PROTO_OSEN) ?
1372 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1373 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1374 key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
1375 WPA_PUT_BE16(reply->key_info, key_info);
1376 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
1377 WPA_PUT_BE16(reply->key_length, 0);
1379 os_memcpy(reply->key_length, key->key_length, 2);
1380 os_memcpy(reply->replay_counter, key->replay_counter,
1381 WPA_REPLAY_COUNTER_LEN);
1383 WPA_PUT_BE16(reply->key_data_length, 0);
1385 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
1386 wpa_eapol_key_send(sm, sm->ptk.kck, sm->ptk.kck_len, ver, sm->bssid,
1387 ETH_P_EAPOL, rbuf, rlen, reply->key_mic);
1393 static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1394 const unsigned char *src_addr,
1395 const struct wpa_eapol_key *key,
1397 size_t key_data_len, u16 ver)
1401 struct wpa_gtk_data gd;
1403 os_memset(&gd, 0, sizeof(gd));
1405 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1406 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1407 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1409 key_info = WPA_GET_BE16(key->key_info);
1411 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
1412 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
1413 key_data_len, key_info,
1416 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
1418 key_info, ver, &gd);
1421 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1426 if (wpa_supplicant_install_gtk(sm, &gd, key->key_rsc) ||
1427 wpa_supplicant_send_2_of_2(sm, key, ver, key_info))
1429 os_memset(&gd, 0, sizeof(gd));
1432 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
1433 "completed with " MACSTR " [GTK=%s]",
1434 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1435 wpa_sm_cancel_auth_timeout(sm);
1436 wpa_sm_set_state(sm, WPA_COMPLETED);
1438 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1440 WPA_KEY_INFO_SECURE);
1443 wpa_sm_set_rekey_offload(sm);
1448 os_memset(&gd, 0, sizeof(gd));
1449 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1453 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
1454 struct wpa_eapol_key *key,
1456 const u8 *buf, size_t len)
1458 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
1460 size_t mic_len = 16;
1462 os_memcpy(mic, key->key_mic, mic_len);
1464 os_memset(key->key_mic, 0, mic_len);
1465 wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len, sm->key_mgmt,
1466 ver, buf, len, key->key_mic);
1467 if (os_memcmp_const(mic, key->key_mic, mic_len) != 0) {
1468 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1469 "WPA: Invalid EAPOL-Key MIC "
1470 "when using TPTK - ignoring TPTK");
1475 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
1476 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
1480 if (!ok && sm->ptk_set) {
1481 os_memset(key->key_mic, 0, mic_len);
1482 wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len, sm->key_mgmt,
1483 ver, buf, len, key->key_mic);
1484 if (os_memcmp_const(mic, key->key_mic, mic_len) != 0) {
1485 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1486 "WPA: Invalid EAPOL-Key MIC - "
1494 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1495 "WPA: Could not verify EAPOL-Key MIC - "
1500 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1501 WPA_REPLAY_COUNTER_LEN);
1502 sm->rx_replay_counter_set = 1;
1507 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1508 static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
1509 struct wpa_eapol_key *key, u16 ver,
1510 u8 *key_data, size_t *key_data_len)
1512 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
1513 key_data, *key_data_len);
1515 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1516 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1521 /* Decrypt key data here so that this operation does not need
1522 * to be implemented separately for each message type. */
1523 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
1525 os_memcpy(ek, key->key_iv, 16);
1526 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
1527 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
1528 os_memset(ek, 0, sizeof(ek));
1529 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1533 os_memset(ek, 0, sizeof(ek));
1534 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1535 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
1536 sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
1537 wpa_key_mgmt_suite_b(sm->key_mgmt)) {
1539 if (*key_data_len < 8 || *key_data_len % 8) {
1540 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1541 "WPA: Unsupported AES-WRAP len %u",
1542 (unsigned int) *key_data_len);
1545 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
1546 buf = os_malloc(*key_data_len);
1548 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1549 "WPA: No memory for AES-UNWRAP buffer");
1552 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
1555 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1556 "WPA: AES unwrap failed - "
1557 "could not decrypt EAPOL-Key key data");
1560 os_memcpy(key_data, buf, *key_data_len);
1562 WPA_PUT_BE16(key->key_data_length, *key_data_len);
1564 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1565 "WPA: Unsupported key_info type %d", ver);
1568 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
1569 key_data, *key_data_len);
1575 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1576 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1578 void wpa_sm_aborted_cached(struct wpa_sm *sm)
1580 if (sm && sm->cur_pmksa) {
1581 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1582 "RSN: Cancelling PMKSA caching attempt");
1583 sm->cur_pmksa = NULL;
1588 static void wpa_eapol_key_dump(struct wpa_sm *sm,
1589 const struct wpa_eapol_key *key)
1591 #ifndef CONFIG_NO_STDOUT_DEBUG
1592 u16 key_info = WPA_GET_BE16(key->key_info);
1594 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1595 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1596 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
1597 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1598 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1599 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1600 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1601 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1602 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1603 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1604 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1605 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1606 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1607 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1608 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1609 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1610 " key_length=%u key_data_length=%u",
1611 WPA_GET_BE16(key->key_length),
1612 WPA_GET_BE16(key->key_data_length));
1613 wpa_hexdump(MSG_DEBUG, " replay_counter",
1614 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1615 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1616 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1617 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1618 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
1619 wpa_hexdump(MSG_DEBUG, " key_mic", key->key_mic, 16);
1620 #endif /* CONFIG_NO_STDOUT_DEBUG */
1625 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1626 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1627 * @src_addr: Source MAC address of the EAPOL packet
1628 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1629 * @len: Length of the EAPOL frame
1630 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1632 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1633 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1634 * only processing WPA and WPA2 EAPOL-Key frames.
1636 * The received EAPOL-Key packets are validated and valid packets are replied
1637 * to. In addition, key material (PTK, GTK) is configured at the end of a
1638 * successful key handshake.
1640 int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1641 const u8 *buf, size_t len)
1643 size_t plen, data_len, key_data_len;
1644 const struct ieee802_1x_hdr *hdr;
1645 struct wpa_eapol_key *key;
1649 struct wpa_peerkey *peerkey = NULL;
1652 #ifdef CONFIG_IEEE80211R
1653 sm->ft_completed = 0;
1654 #endif /* CONFIG_IEEE80211R */
1656 if (len < sizeof(*hdr) + sizeof(*key)) {
1657 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1658 "WPA: EAPOL frame too short to be a WPA "
1659 "EAPOL-Key (len %lu, expecting at least %lu)",
1660 (unsigned long) len,
1661 (unsigned long) sizeof(*hdr) + sizeof(*key));
1665 hdr = (const struct ieee802_1x_hdr *) buf;
1666 plen = be_to_host16(hdr->length);
1667 data_len = plen + sizeof(*hdr);
1668 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1669 "IEEE 802.1X RX: version=%d type=%d length=%lu",
1670 hdr->version, hdr->type, (unsigned long) plen);
1672 if (hdr->version < EAPOL_VERSION) {
1673 /* TODO: backwards compatibility */
1675 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
1676 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1677 "WPA: EAPOL frame (type %u) discarded, "
1678 "not a Key frame", hdr->type);
1682 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
1683 if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
1684 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1685 "WPA: EAPOL frame payload size %lu "
1686 "invalid (frame size %lu)",
1687 (unsigned long) plen, (unsigned long) len);
1691 if (data_len < len) {
1692 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1693 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
1694 (unsigned long) len - data_len);
1698 * Make a copy of the frame since we need to modify the buffer during
1699 * MAC validation and Key Data decryption.
1701 tmp = os_malloc(data_len);
1704 os_memcpy(tmp, buf, data_len);
1705 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
1706 key_data = (u8 *) (key + 1);
1708 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
1710 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1711 "WPA: EAPOL-Key type (%d) unknown, discarded",
1716 wpa_eapol_key_dump(sm, key);
1718 key_data_len = WPA_GET_BE16(key->key_data_length);
1719 if (key_data_len > plen - sizeof(struct wpa_eapol_key)) {
1720 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
1721 "frame - key_data overflow (%u > %u)",
1722 (unsigned int) key_data_len,
1723 (unsigned int) (plen - sizeof(struct wpa_eapol_key)));
1727 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
1728 key_info = WPA_GET_BE16(key->key_info);
1729 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1730 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
1731 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
1732 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1733 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
1734 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
1735 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1736 sm->key_mgmt != WPA_KEY_MGMT_OSEN) {
1737 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1738 "WPA: Unsupported EAPOL-Key descriptor version %d",
1743 if (sm->key_mgmt == WPA_KEY_MGMT_OSEN &&
1744 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1745 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1746 "OSEN: Unsupported EAPOL-Key descriptor version %d",
1751 if (wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1752 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1753 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1754 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
1759 #ifdef CONFIG_IEEE80211R
1760 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
1761 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
1762 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1763 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1764 "FT: AP did not use AES-128-CMAC");
1768 #endif /* CONFIG_IEEE80211R */
1769 #ifdef CONFIG_IEEE80211W
1770 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
1771 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1772 sm->key_mgmt != WPA_KEY_MGMT_OSEN &&
1773 !wpa_key_mgmt_suite_b(sm->key_mgmt)) {
1774 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1775 "WPA: AP did not use the "
1776 "negotiated AES-128-CMAC");
1780 #endif /* CONFIG_IEEE80211W */
1781 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
1782 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1783 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1784 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1785 "WPA: CCMP is used, but EAPOL-Key "
1786 "descriptor version (%d) is not 2", ver);
1787 if (sm->group_cipher != WPA_CIPHER_CCMP &&
1788 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1789 /* Earlier versions of IEEE 802.11i did not explicitly
1790 * require version 2 descriptor for all EAPOL-Key
1791 * packets, so allow group keys to use version 1 if
1792 * CCMP is not used for them. */
1793 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1794 "WPA: Backwards compatibility: allow invalid "
1795 "version for non-CCMP group keys");
1796 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1797 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1798 "WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
1801 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
1802 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1803 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1804 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1805 "WPA: GCMP is used, but EAPOL-Key "
1806 "descriptor version (%d) is not 2", ver);
1810 #ifdef CONFIG_PEERKEY
1811 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
1812 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
1816 if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
1817 if (!peerkey->initiator && peerkey->replay_counter_set &&
1818 os_memcmp(key->replay_counter, peerkey->replay_counter,
1819 WPA_REPLAY_COUNTER_LEN) <= 0) {
1820 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1821 "RSN: EAPOL-Key Replay Counter did not "
1822 "increase (STK) - dropping packet");
1824 } else if (peerkey->initiator) {
1825 u8 _tmp[WPA_REPLAY_COUNTER_LEN];
1826 os_memcpy(_tmp, key->replay_counter,
1827 WPA_REPLAY_COUNTER_LEN);
1828 inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
1829 if (os_memcmp(_tmp, peerkey->replay_counter,
1830 WPA_REPLAY_COUNTER_LEN) != 0) {
1831 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1832 "RSN: EAPOL-Key Replay "
1833 "Counter did not match (STK) - "
1840 if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
1841 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1842 "RSN: Ack bit in key_info from STK peer");
1845 #endif /* CONFIG_PEERKEY */
1847 if (!peerkey && sm->rx_replay_counter_set &&
1848 os_memcmp(key->replay_counter, sm->rx_replay_counter,
1849 WPA_REPLAY_COUNTER_LEN) <= 0) {
1850 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1851 "WPA: EAPOL-Key Replay Counter did not increase - "
1856 if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
1857 #ifdef CONFIG_PEERKEY
1858 && (peerkey == NULL || !peerkey->initiator)
1859 #endif /* CONFIG_PEERKEY */
1861 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1862 "WPA: No Ack bit in key_info");
1866 if (key_info & WPA_KEY_INFO_REQUEST) {
1867 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1868 "WPA: EAPOL-Key with Request bit - dropped");
1872 if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
1873 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
1876 #ifdef CONFIG_PEERKEY
1877 if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
1878 peerkey_verify_eapol_key_mic(sm, peerkey, key, ver, tmp, data_len))
1880 #endif /* CONFIG_PEERKEY */
1882 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
1883 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1884 if (wpa_supplicant_decrypt_key_data(sm, key, ver, key_data,
1889 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1890 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
1891 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1892 "WPA: Ignored EAPOL-Key (Pairwise) with "
1893 "non-zero key index");
1897 /* PeerKey 4-Way Handshake */
1898 peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver,
1899 key_data, key_data_len);
1900 } else if (key_info & WPA_KEY_INFO_MIC) {
1901 /* 3/4 4-Way Handshake */
1902 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
1905 /* 1/4 4-Way Handshake */
1906 wpa_supplicant_process_1_of_4(sm, src_addr, key,
1910 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
1911 /* PeerKey SMK Handshake */
1912 peerkey_rx_eapol_smk(sm, src_addr, key, key_data_len, key_info,
1915 if (key_info & WPA_KEY_INFO_MIC) {
1916 /* 1/2 Group Key Handshake */
1917 wpa_supplicant_process_1_of_2(sm, src_addr, key,
1918 key_data, key_data_len,
1921 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1922 "WPA: EAPOL-Key (Group) without Mic bit - "
1930 bin_clear_free(tmp, data_len);
1935 #ifdef CONFIG_CTRL_IFACE
1936 static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
1938 switch (sm->key_mgmt) {
1939 case WPA_KEY_MGMT_IEEE8021X:
1940 return ((sm->proto == WPA_PROTO_RSN ||
1941 sm->proto == WPA_PROTO_OSEN) ?
1942 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
1943 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
1944 case WPA_KEY_MGMT_PSK:
1945 return (sm->proto == WPA_PROTO_RSN ?
1946 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
1947 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
1948 #ifdef CONFIG_IEEE80211R
1949 case WPA_KEY_MGMT_FT_IEEE8021X:
1950 return RSN_AUTH_KEY_MGMT_FT_802_1X;
1951 case WPA_KEY_MGMT_FT_PSK:
1952 return RSN_AUTH_KEY_MGMT_FT_PSK;
1953 #endif /* CONFIG_IEEE80211R */
1954 #ifdef CONFIG_IEEE80211W
1955 case WPA_KEY_MGMT_IEEE8021X_SHA256:
1956 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
1957 case WPA_KEY_MGMT_PSK_SHA256:
1958 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
1959 #endif /* CONFIG_IEEE80211W */
1960 case WPA_KEY_MGMT_CCKM:
1961 return (sm->proto == WPA_PROTO_RSN ?
1962 RSN_AUTH_KEY_MGMT_CCKM:
1963 WPA_AUTH_KEY_MGMT_CCKM);
1964 case WPA_KEY_MGMT_WPA_NONE:
1965 return WPA_AUTH_KEY_MGMT_NONE;
1966 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
1967 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
1974 #define RSN_SUITE "%02x-%02x-%02x-%d"
1975 #define RSN_SUITE_ARG(s) \
1976 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
1979 * wpa_sm_get_mib - Dump text list of MIB entries
1980 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1981 * @buf: Buffer for the list
1982 * @buflen: Length of the buffer
1983 * Returns: Number of bytes written to buffer
1985 * This function is used fetch dot11 MIB variables.
1987 int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
1989 char pmkid_txt[PMKID_LEN * 2 + 1];
1993 if (sm->cur_pmksa) {
1994 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
1995 sm->cur_pmksa->pmkid, PMKID_LEN);
1997 pmkid_txt[0] = '\0';
1999 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
2000 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
2001 sm->proto == WPA_PROTO_RSN)
2006 ret = os_snprintf(buf, buflen,
2007 "dot11RSNAOptionImplemented=TRUE\n"
2008 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2009 "dot11RSNAEnabled=%s\n"
2010 "dot11RSNAPreauthenticationEnabled=%s\n"
2011 "dot11RSNAConfigVersion=%d\n"
2012 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2013 "dot11RSNAConfigGroupCipherSize=%d\n"
2014 "dot11RSNAConfigPMKLifetime=%d\n"
2015 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2016 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2017 "dot11RSNAConfigSATimeout=%d\n",
2018 rsna ? "TRUE" : "FALSE",
2019 rsna ? "TRUE" : "FALSE",
2021 wpa_cipher_key_len(sm->group_cipher) * 8,
2022 sm->dot11RSNAConfigPMKLifetime,
2023 sm->dot11RSNAConfigPMKReauthThreshold,
2024 sm->dot11RSNAConfigSATimeout);
2025 if (os_snprintf_error(buflen, ret))
2030 buf + len, buflen - len,
2031 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2032 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2033 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2034 "dot11RSNAPMKIDUsed=%s\n"
2035 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2036 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2037 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2038 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2039 "dot11RSNA4WayHandshakeFailures=%u\n",
2040 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
2041 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2042 sm->pairwise_cipher)),
2043 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2046 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
2047 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2048 sm->pairwise_cipher)),
2049 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2051 sm->dot11RSNA4WayHandshakeFailures);
2052 if (!os_snprintf_error(buflen - len, ret))
2057 #endif /* CONFIG_CTRL_IFACE */
2060 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
2061 void *ctx, enum pmksa_free_reason reason)
2063 struct wpa_sm *sm = ctx;
2066 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2067 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2069 if (sm->cur_pmksa == entry) {
2070 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2071 "RSN: %s current PMKSA entry",
2072 reason == PMKSA_REPLACE ? "replaced" : "removed");
2073 pmksa_cache_clear_current(sm);
2076 * If an entry is simply being replaced, there's no need to
2077 * deauthenticate because it will be immediately re-added.
2078 * This happens when EAP authentication is completed again
2079 * (reauth or failed PMKSA caching attempt).
2081 if (reason != PMKSA_REPLACE)
2085 if (reason == PMKSA_EXPIRE &&
2086 (sm->pmk_len == entry->pmk_len &&
2087 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2088 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2089 "RSN: deauthenticating due to expired PMK");
2090 pmksa_cache_clear_current(sm);
2095 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2096 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2102 * wpa_sm_init - Initialize WPA state machine
2103 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2104 * Returns: Pointer to the allocated WPA state machine data
2106 * This function is used to allocate a new WPA state machine and the returned
2107 * value is passed to all WPA state machine calls.
2109 struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2113 sm = os_zalloc(sizeof(*sm));
2116 dl_list_init(&sm->pmksa_candidates);
2117 sm->renew_snonce = 1;
2120 sm->dot11RSNAConfigPMKLifetime = 43200;
2121 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2122 sm->dot11RSNAConfigSATimeout = 60;
2124 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2125 if (sm->pmksa == NULL) {
2126 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2127 "RSN: PMKSA cache initialization failed");
2137 * wpa_sm_deinit - Deinitialize WPA state machine
2138 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2140 void wpa_sm_deinit(struct wpa_sm *sm)
2144 pmksa_cache_deinit(sm->pmksa);
2145 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2146 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2147 os_free(sm->assoc_wpa_ie);
2148 os_free(sm->ap_wpa_ie);
2149 os_free(sm->ap_rsn_ie);
2153 #ifdef CONFIG_IEEE80211R
2154 os_free(sm->assoc_resp_ies);
2155 #endif /* CONFIG_IEEE80211R */
2161 * wpa_sm_notify_assoc - Notify WPA state machine about association
2162 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2163 * @bssid: The BSSID of the new association
2165 * This function is called to let WPA state machine know that the connection
2168 void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2175 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2176 "WPA: Association event - clear replay counter");
2177 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2178 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2179 sm->rx_replay_counter_set = 0;
2180 sm->renew_snonce = 1;
2181 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2182 rsn_preauth_deinit(sm);
2184 #ifdef CONFIG_IEEE80211R
2185 if (wpa_ft_is_completed(sm)) {
2187 * Clear portValid to kick EAPOL state machine to re-enter
2188 * AUTHENTICATED state to get the EAPOL port Authorized.
2190 eapol_sm_notify_portValid(sm->eapol, FALSE);
2191 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2193 /* Prepare for the next transition */
2194 wpa_ft_prepare_auth_request(sm, NULL);
2198 #endif /* CONFIG_IEEE80211R */
2202 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2203 * this is not part of a Fast BSS Transition.
2205 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
2207 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2209 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2214 #endif /* CONFIG_TDLS */
2217 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
2218 #endif /* CONFIG_P2P */
2223 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2224 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2226 * This function is called to let WPA state machine know that the connection
2227 * was lost. This will abort any existing pre-authentication session.
2229 void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2232 rsn_preauth_deinit(sm);
2233 pmksa_cache_clear_current(sm);
2234 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2235 sm->dot11RSNA4WayHandshakeFailures++;
2237 wpa_tdls_disassoc(sm);
2238 #endif /* CONFIG_TDLS */
2240 /* Keys are not needed in the WPA state machine anymore */
2246 * wpa_sm_set_pmk - Set PMK
2247 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2249 * @pmk_len: The length of the new PMK in bytes
2250 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
2252 * Configure the PMK for WPA state machine.
2254 void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
2260 sm->pmk_len = pmk_len;
2261 os_memcpy(sm->pmk, pmk, pmk_len);
2263 #ifdef CONFIG_IEEE80211R
2264 /* Set XXKey to be PSK for FT key derivation */
2265 sm->xxkey_len = pmk_len;
2266 os_memcpy(sm->xxkey, pmk, pmk_len);
2267 #endif /* CONFIG_IEEE80211R */
2270 pmksa_cache_add(sm->pmksa, pmk, pmk_len, NULL, 0,
2271 bssid, sm->own_addr,
2272 sm->network_ctx, sm->key_mgmt);
2278 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2279 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2281 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2284 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2289 if (sm->cur_pmksa) {
2290 sm->pmk_len = sm->cur_pmksa->pmk_len;
2291 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2293 sm->pmk_len = PMK_LEN;
2294 os_memset(sm->pmk, 0, PMK_LEN);
2300 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2301 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2302 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2304 void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2307 sm->fast_reauth = fast_reauth;
2312 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2313 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2314 * @scard_ctx: Context pointer for smartcard related callback functions
2316 void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2320 sm->scard_ctx = scard_ctx;
2321 if (sm->preauth_eapol)
2322 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2327 * wpa_sm_set_config - Notification of current configration change
2328 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2329 * @config: Pointer to current network configuration
2331 * Notify WPA state machine that configuration has changed. config will be
2332 * stored as a backpointer to network configuration. This can be %NULL to clear
2333 * the stored pointed.
2335 void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2341 sm->network_ctx = config->network_ctx;
2342 sm->peerkey_enabled = config->peerkey_enabled;
2343 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2344 sm->proactive_key_caching = config->proactive_key_caching;
2345 sm->eap_workaround = config->eap_workaround;
2346 sm->eap_conf_ctx = config->eap_conf_ctx;
2348 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2349 sm->ssid_len = config->ssid_len;
2352 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
2353 sm->p2p = config->p2p;
2355 sm->network_ctx = NULL;
2356 sm->peerkey_enabled = 0;
2357 sm->allowed_pairwise_cipher = 0;
2358 sm->proactive_key_caching = 0;
2359 sm->eap_workaround = 0;
2360 sm->eap_conf_ctx = NULL;
2362 sm->wpa_ptk_rekey = 0;
2369 * wpa_sm_set_own_addr - Set own MAC address
2370 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2371 * @addr: Own MAC address
2373 void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2376 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2381 * wpa_sm_set_ifname - Set network interface name
2382 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2383 * @ifname: Interface name
2384 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2386 void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2387 const char *bridge_ifname)
2390 sm->ifname = ifname;
2391 sm->bridge_ifname = bridge_ifname;
2397 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2398 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2399 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2401 void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2409 * wpa_sm_set_param - Set WPA state machine parameters
2410 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2411 * @param: Parameter field
2412 * @value: Parameter value
2413 * Returns: 0 on success, -1 on failure
2415 int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2424 case RSNA_PMK_LIFETIME:
2426 sm->dot11RSNAConfigPMKLifetime = value;
2430 case RSNA_PMK_REAUTH_THRESHOLD:
2431 if (value > 0 && value <= 100)
2432 sm->dot11RSNAConfigPMKReauthThreshold = value;
2436 case RSNA_SA_TIMEOUT:
2438 sm->dot11RSNAConfigSATimeout = value;
2442 case WPA_PARAM_PROTO:
2445 case WPA_PARAM_PAIRWISE:
2446 sm->pairwise_cipher = value;
2448 case WPA_PARAM_GROUP:
2449 sm->group_cipher = value;
2451 case WPA_PARAM_KEY_MGMT:
2452 sm->key_mgmt = value;
2454 #ifdef CONFIG_IEEE80211W
2455 case WPA_PARAM_MGMT_GROUP:
2456 sm->mgmt_group_cipher = value;
2458 #endif /* CONFIG_IEEE80211W */
2459 case WPA_PARAM_RSN_ENABLED:
2460 sm->rsn_enabled = value;
2474 * wpa_sm_get_status - Get WPA state machine
2475 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2476 * @buf: Buffer for status information
2477 * @buflen: Maximum buffer length
2478 * @verbose: Whether to include verbose status information
2479 * Returns: Number of bytes written to buf.
2481 * Query WPA state machine for status information. This function fills in
2482 * a text area with current status information. If the buffer (buf) is not
2483 * large enough, status information will be truncated to fit the buffer.
2485 int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2488 char *pos = buf, *end = buf + buflen;
2491 ret = os_snprintf(pos, end - pos,
2492 "pairwise_cipher=%s\n"
2495 wpa_cipher_txt(sm->pairwise_cipher),
2496 wpa_cipher_txt(sm->group_cipher),
2497 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
2498 if (os_snprintf_error(end - pos, ret))
2502 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
2503 struct wpa_ie_data rsn;
2504 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
2506 rsn.capabilities & (WPA_CAPABILITY_MFPR |
2507 WPA_CAPABILITY_MFPC)) {
2508 ret = os_snprintf(pos, end - pos, "pmf=%d\n",
2510 WPA_CAPABILITY_MFPR) ? 2 : 1);
2511 if (os_snprintf_error(end - pos, ret))
2521 int wpa_sm_pmf_enabled(struct wpa_sm *sm)
2523 struct wpa_ie_data rsn;
2525 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
2528 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
2529 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
2537 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2538 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2539 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2540 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2541 * Returns: 0 on success, -1 on failure
2543 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2551 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2556 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2557 wpa_ie, *wpa_ie_len);
2559 if (sm->assoc_wpa_ie == NULL) {
2561 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2562 * the correct version of the IE even if PMKSA caching is
2563 * aborted (which would remove PMKID from IE generation).
2565 sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2566 if (sm->assoc_wpa_ie == NULL)
2569 os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2570 sm->assoc_wpa_ie_len = *wpa_ie_len;
2578 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2579 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2580 * @ie: Pointer to IE data (starting from id)
2582 * Returns: 0 on success, -1 on failure
2584 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2585 * Request frame. The IE will be used to override the default value generated
2586 * with wpa_sm_set_assoc_wpa_ie_default().
2588 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2593 os_free(sm->assoc_wpa_ie);
2594 if (ie == NULL || len == 0) {
2595 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2596 "WPA: clearing own WPA/RSN IE");
2597 sm->assoc_wpa_ie = NULL;
2598 sm->assoc_wpa_ie_len = 0;
2600 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2601 sm->assoc_wpa_ie = os_malloc(len);
2602 if (sm->assoc_wpa_ie == NULL)
2605 os_memcpy(sm->assoc_wpa_ie, ie, len);
2606 sm->assoc_wpa_ie_len = len;
2614 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2615 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2616 * @ie: Pointer to IE data (starting from id)
2618 * Returns: 0 on success, -1 on failure
2620 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2623 int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2628 os_free(sm->ap_wpa_ie);
2629 if (ie == NULL || len == 0) {
2630 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2631 "WPA: clearing AP WPA IE");
2632 sm->ap_wpa_ie = NULL;
2633 sm->ap_wpa_ie_len = 0;
2635 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2636 sm->ap_wpa_ie = os_malloc(len);
2637 if (sm->ap_wpa_ie == NULL)
2640 os_memcpy(sm->ap_wpa_ie, ie, len);
2641 sm->ap_wpa_ie_len = len;
2649 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2650 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2651 * @ie: Pointer to IE data (starting from id)
2653 * Returns: 0 on success, -1 on failure
2655 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2658 int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2663 os_free(sm->ap_rsn_ie);
2664 if (ie == NULL || len == 0) {
2665 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2666 "WPA: clearing AP RSN IE");
2667 sm->ap_rsn_ie = NULL;
2668 sm->ap_rsn_ie_len = 0;
2670 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2671 sm->ap_rsn_ie = os_malloc(len);
2672 if (sm->ap_rsn_ie == NULL)
2675 os_memcpy(sm->ap_rsn_ie, ie, len);
2676 sm->ap_rsn_ie_len = len;
2684 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2685 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2686 * @data: Pointer to data area for parsing results
2687 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2689 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2690 * parsed data into data.
2692 int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2697 if (sm->assoc_wpa_ie == NULL) {
2698 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2699 "WPA: No WPA/RSN IE available from association info");
2702 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
2708 int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
2710 return pmksa_cache_list(sm->pmksa, buf, len);
2714 void wpa_sm_drop_sa(struct wpa_sm *sm)
2716 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
2719 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2720 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2721 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2722 #ifdef CONFIG_IEEE80211R
2723 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
2724 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
2725 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
2726 #endif /* CONFIG_IEEE80211R */
2730 int wpa_sm_has_ptk(struct wpa_sm *sm)
2738 void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
2740 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
2744 void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
2746 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
2751 int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
2754 u8 keylen; /* plaintext key len */
2757 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
2758 struct wpa_gtk_data gd;
2760 os_memset(&gd, 0, sizeof(gd));
2761 keylen = wpa_cipher_key_len(sm->group_cipher);
2762 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
2763 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
2764 if (gd.alg == WPA_ALG_NONE) {
2765 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
2770 keyinfo = WPA_GET_LE16(buf + 2);
2771 gd.gtk_len = keylen;
2772 if (gd.gtk_len != buf[4]) {
2773 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
2774 gd.gtk_len, buf[4]);
2777 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
2778 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
2779 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
2781 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
2783 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
2784 gd.gtk, gd.gtk_len);
2785 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc)) {
2786 os_memset(&gd, 0, sizeof(gd));
2787 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
2791 os_memset(&gd, 0, sizeof(gd));
2792 #ifdef CONFIG_IEEE80211W
2793 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
2794 struct wpa_igtk_kde igd;
2797 os_memset(&igd, 0, sizeof(igd));
2798 keylen = wpa_cipher_key_len(sm->mgmt_group_cipher);
2799 os_memcpy(igd.keyid, buf + 2, 2);
2800 os_memcpy(igd.pn, buf + 4, 6);
2802 keyidx = WPA_GET_LE16(igd.keyid);
2803 os_memcpy(igd.igtk, buf + 10, keylen);
2805 wpa_hexdump_key(MSG_DEBUG, "Install IGTK (WNM SLEEP)",
2807 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
2808 broadcast_ether_addr,
2809 keyidx, 0, igd.pn, sizeof(igd.pn),
2810 igd.igtk, keylen) < 0) {
2811 wpa_printf(MSG_DEBUG, "Failed to install the IGTK in "
2813 os_memset(&igd, 0, sizeof(igd));
2816 os_memset(&igd, 0, sizeof(igd));
2817 #endif /* CONFIG_IEEE80211W */
2819 wpa_printf(MSG_DEBUG, "Unknown element id");
2825 #endif /* CONFIG_WNM */
2828 #ifdef CONFIG_PEERKEY
2829 int wpa_sm_rx_eapol_peerkey(struct wpa_sm *sm, const u8 *src_addr,
2830 const u8 *buf, size_t len)
2832 struct wpa_peerkey *peerkey;
2834 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
2835 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
2842 wpa_sm_rx_eapol(sm, src_addr, buf, len);
2846 #endif /* CONFIG_PEERKEY */
2851 int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
2853 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
2855 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
2859 #endif /* CONFIG_P2P */
2862 void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
2864 if (rx_replay_counter == NULL)
2867 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
2868 WPA_REPLAY_COUNTER_LEN);
2869 sm->rx_replay_counter_set = 1;
2870 wpa_printf(MSG_DEBUG, "Updated key replay counter");
2874 void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
2875 const u8 *ptk_kck, size_t ptk_kck_len,
2876 const u8 *ptk_kek, size_t ptk_kek_len)
2878 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
2879 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
2880 sm->ptk.kck_len = ptk_kck_len;
2881 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
2883 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
2884 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
2885 sm->ptk.kek_len = ptk_kek_len;
2886 wpa_printf(MSG_DEBUG, "Updated PTK KEK");