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 (wpa_key_mgmt_ft(sm->key_mgmt) || wpa_key_mgmt_sha256(sm->key_mgmt))
93 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
94 else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
95 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
97 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
99 if (wpa_sm_get_bssid(sm, bssid) < 0) {
100 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
101 "Failed to read BSSID for EAPOL-Key request");
105 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
106 sizeof(*reply), &rlen, (void *) &reply);
110 reply->type = sm->proto == WPA_PROTO_RSN ?
111 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
112 key_info = WPA_KEY_INFO_REQUEST | ver;
114 key_info |= WPA_KEY_INFO_MIC;
116 key_info |= WPA_KEY_INFO_ERROR;
118 key_info |= WPA_KEY_INFO_KEY_TYPE;
119 WPA_PUT_BE16(reply->key_info, key_info);
120 WPA_PUT_BE16(reply->key_length, 0);
121 os_memcpy(reply->replay_counter, sm->request_counter,
122 WPA_REPLAY_COUNTER_LEN);
123 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
125 WPA_PUT_BE16(reply->key_data_length, 0);
127 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
128 "WPA: Sending EAPOL-Key Request (error=%d "
129 "pairwise=%d ptk_set=%d len=%lu)",
130 error, pairwise, sm->ptk_set, (unsigned long) rlen);
131 wpa_eapol_key_send(sm, sm->ptk.kck, ver, bssid, ETH_P_EAPOL,
132 rbuf, rlen, key_info & WPA_KEY_INFO_MIC ?
133 reply->key_mic : NULL);
137 static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
138 const unsigned char *src_addr,
141 int abort_cached = 0;
143 if (pmkid && !sm->cur_pmksa) {
144 /* When using drivers that generate RSN IE, wpa_supplicant may
145 * not have enough time to get the association information
146 * event before receiving this 1/4 message, so try to find a
147 * matching PMKSA cache entry here. */
148 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid,
151 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
152 "RSN: found matching PMKID from PMKSA cache");
154 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
155 "RSN: no matching PMKID found");
160 if (pmkid && sm->cur_pmksa &&
161 os_memcmp(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
162 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
163 wpa_sm_set_pmk_from_pmksa(sm);
164 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
165 sm->pmk, sm->pmk_len);
166 eapol_sm_notify_cached(sm->eapol);
167 #ifdef CONFIG_IEEE80211R
169 #endif /* CONFIG_IEEE80211R */
170 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
173 res = eapol_sm_get_key(sm->eapol, sm->pmk, PMK_LEN);
176 * EAP-LEAP is an exception from other EAP methods: it
177 * uses only 16-byte PMK.
179 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
182 #ifdef CONFIG_IEEE80211R
184 if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
186 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
187 sm->xxkey_len = PMK_LEN;
188 os_memset(buf, 0, sizeof(buf));
190 #endif /* CONFIG_IEEE80211R */
193 struct rsn_pmksa_cache_entry *sa = NULL;
194 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
195 "machines", sm->pmk, pmk_len);
196 sm->pmk_len = pmk_len;
197 if (sm->proto == WPA_PROTO_RSN &&
198 !wpa_key_mgmt_ft(sm->key_mgmt)) {
199 sa = pmksa_cache_add(sm->pmksa,
201 src_addr, sm->own_addr,
205 if (!sm->cur_pmksa && pmkid &&
206 pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL))
208 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
209 "RSN: the new PMK matches with the "
217 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
218 "WPA: Failed to get master session key from "
219 "EAPOL state machines - key handshake "
222 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
223 "RSN: Cancelled PMKSA caching "
225 sm->cur_pmksa = NULL;
227 } else if (!abort_cached) {
233 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
234 !wpa_key_mgmt_ft(sm->key_mgmt)) {
235 /* Send EAPOL-Start to trigger full EAP authentication. */
239 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
240 "RSN: no PMKSA entry found - trigger "
241 "full EAP authentication");
242 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
243 NULL, 0, &buflen, NULL);
245 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
259 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
260 * @sm: Pointer to WPA state machine data from wpa_sm_init()
261 * @dst: Destination address for the frame
262 * @key: Pointer to the EAPOL-Key frame header
263 * @ver: Version bits from EAPOL-Key Key Info
264 * @nonce: Nonce value for the EAPOL-Key frame
265 * @wpa_ie: WPA/RSN IE
266 * @wpa_ie_len: Length of the WPA/RSN IE
267 * @ptk: PTK to use for keyed hash and encryption
268 * Returns: 0 on success, -1 on failure
270 int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
271 const struct wpa_eapol_key *key,
272 int ver, const u8 *nonce,
273 const u8 *wpa_ie, size_t wpa_ie_len,
277 struct wpa_eapol_key *reply;
279 u8 *rsn_ie_buf = NULL;
281 if (wpa_ie == NULL) {
282 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
283 "cannot generate msg 2/4");
287 #ifdef CONFIG_IEEE80211R
288 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
292 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
293 * FTIE from (Re)Association Response.
295 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
296 sm->assoc_resp_ies_len);
297 if (rsn_ie_buf == NULL)
299 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
300 res = wpa_insert_pmkid(rsn_ie_buf, wpa_ie_len,
308 if (sm->assoc_resp_ies) {
309 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
310 sm->assoc_resp_ies_len);
311 wpa_ie_len += sm->assoc_resp_ies_len;
316 #endif /* CONFIG_IEEE80211R */
318 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
320 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
321 NULL, sizeof(*reply) + wpa_ie_len,
322 &rlen, (void *) &reply);
328 reply->type = sm->proto == WPA_PROTO_RSN ?
329 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
330 WPA_PUT_BE16(reply->key_info,
331 ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC);
332 if (sm->proto == WPA_PROTO_RSN)
333 WPA_PUT_BE16(reply->key_length, 0);
335 os_memcpy(reply->key_length, key->key_length, 2);
336 os_memcpy(reply->replay_counter, key->replay_counter,
337 WPA_REPLAY_COUNTER_LEN);
338 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
339 WPA_REPLAY_COUNTER_LEN);
341 WPA_PUT_BE16(reply->key_data_length, wpa_ie_len);
342 os_memcpy(reply + 1, wpa_ie, wpa_ie_len);
345 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
347 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
348 wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
349 rbuf, rlen, reply->key_mic);
355 static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
356 const struct wpa_eapol_key *key,
359 size_t ptk_len = sm->pairwise_cipher != WPA_CIPHER_TKIP ? 48 : 64;
360 #ifdef CONFIG_IEEE80211R
361 if (wpa_key_mgmt_ft(sm->key_mgmt))
362 return wpa_derive_ptk_ft(sm, src_addr, key, ptk, ptk_len);
363 #endif /* CONFIG_IEEE80211R */
365 wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
366 sm->own_addr, sm->bssid, sm->snonce, key->key_nonce,
368 wpa_key_mgmt_sha256(sm->key_mgmt));
373 static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
374 const unsigned char *src_addr,
375 const struct wpa_eapol_key *key,
378 struct wpa_eapol_ie_parse ie;
383 if (wpa_sm_get_network_ctx(sm) == NULL) {
384 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
385 "found (msg 1 of 4)");
389 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
390 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of 4-Way "
391 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
393 os_memset(&ie, 0, sizeof(ie));
395 #ifndef CONFIG_NO_WPA2
396 if (sm->proto == WPA_PROTO_RSN) {
397 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
398 const u8 *_buf = (const u8 *) (key + 1);
399 size_t len = WPA_GET_BE16(key->key_data_length);
400 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data", _buf, len);
401 if (wpa_supplicant_parse_ies(_buf, len, &ie) < 0)
404 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
405 "Authenticator", ie.pmkid, PMKID_LEN);
408 #endif /* CONFIG_NO_WPA2 */
410 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
412 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
413 "msg 1/4 - requesting full EAP authentication");
419 if (sm->renew_snonce) {
420 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
421 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
422 "WPA: Failed to get random data for SNonce");
425 sm->renew_snonce = 0;
426 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
427 sm->snonce, WPA_NONCE_LEN);
430 /* Calculate PTK which will be stored as a temporary PTK until it has
431 * been verified when processing message 3/4. */
433 wpa_derive_ptk(sm, src_addr, key, ptk);
434 /* Supplicant: swap tx/rx Mic keys */
435 os_memcpy(buf, ptk->u.auth.tx_mic_key, 8);
436 os_memcpy(ptk->u.auth.tx_mic_key, ptk->u.auth.rx_mic_key, 8);
437 os_memcpy(ptk->u.auth.rx_mic_key, buf, 8);
440 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
441 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len,
445 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
449 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
453 static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
455 struct wpa_sm *sm = eloop_ctx;
456 rsn_preauth_candidate_process(sm);
460 static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
461 const u8 *addr, int secure)
463 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
464 "WPA: Key negotiation completed with "
465 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
466 wpa_cipher_txt(sm->pairwise_cipher),
467 wpa_cipher_txt(sm->group_cipher));
468 wpa_sm_cancel_auth_timeout(sm);
469 wpa_sm_set_state(sm, WPA_COMPLETED);
472 wpa_sm_mlme_setprotection(
473 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
474 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
475 eapol_sm_notify_portValid(sm->eapol, TRUE);
476 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt))
477 eapol_sm_notify_eap_success(sm->eapol, TRUE);
479 * Start preauthentication after a short wait to avoid a
480 * possible race condition between the data receive and key
481 * configuration after the 4-Way Handshake. This increases the
482 * likelihood of the first preauth EAPOL-Start frame getting to
485 eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
488 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
489 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
490 "RSN: Authenticator accepted "
491 "opportunistic PMKSA entry - marking it valid");
492 sm->cur_pmksa->opportunistic = 0;
495 #ifdef CONFIG_IEEE80211R
496 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
497 /* Prepare for the next transition */
498 wpa_ft_prepare_auth_request(sm, NULL);
500 #endif /* CONFIG_IEEE80211R */
504 static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
506 struct wpa_sm *sm = eloop_ctx;
507 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
508 wpa_sm_key_request(sm, 0, 1);
512 static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
513 const struct wpa_eapol_key *key)
518 u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
520 if (sm->ptk_installed) {
521 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
522 "WPA: Do not re-install same PTK to the driver");
526 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
527 "WPA: Installing PTK to the driver");
529 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
530 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
531 "Suite: NONE - do not use pairwise keys");
535 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
536 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
537 "WPA: Unsupported pairwise cipher %d",
538 sm->pairwise_cipher);
542 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
543 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
544 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
546 if (sm->proto == WPA_PROTO_RSN) {
549 key_rsc = key->key_rsc;
550 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
553 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
554 (u8 *) sm->ptk.tk1, keylen) < 0) {
555 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
556 "WPA: Failed to set PTK to the "
557 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
558 alg, keylen, MAC2STR(sm->bssid));
562 sm->ptk_installed = 1;
564 if (sm->wpa_ptk_rekey) {
565 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
566 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
574 static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
576 int keylen, int maxkeylen,
582 *alg = wpa_cipher_to_alg(group_cipher);
583 if (*alg == WPA_ALG_NONE) {
584 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
585 "WPA: Unsupported Group Cipher %d",
589 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
591 klen = wpa_cipher_key_len(group_cipher);
592 if (keylen != klen || maxkeylen < klen) {
593 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
594 "WPA: Unsupported %s Group Cipher key length %d (%d)",
595 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
602 struct wpa_gtk_data {
604 int tx, key_rsc_len, keyidx;
610 static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
611 const struct wpa_gtk_data *gd,
612 const u8 *key_rsc, int wnm_sleep)
614 const u8 *_gtk = gd->gtk;
617 /* Detect possible key reinstallation */
618 if ((sm->gtk.gtk_len == (size_t) gd->gtk_len &&
619 os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) ||
620 (sm->gtk_wnm_sleep.gtk_len == (size_t) gd->gtk_len &&
621 os_memcmp(sm->gtk_wnm_sleep.gtk, gd->gtk,
622 sm->gtk_wnm_sleep.gtk_len) == 0)) {
623 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
624 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
625 gd->keyidx, gd->tx, gd->gtk_len);
629 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
630 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
631 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
632 gd->keyidx, gd->tx, gd->gtk_len);
633 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
634 if (sm->group_cipher == WPA_CIPHER_TKIP) {
635 /* Swap Tx/Rx keys for Michael MIC */
636 os_memcpy(gtk_buf, gd->gtk, 16);
637 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
638 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
641 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
642 if (wpa_sm_set_key(sm, gd->alg, NULL,
643 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
644 _gtk, gd->gtk_len) < 0) {
645 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
646 "WPA: Failed to set GTK to the driver "
650 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
651 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
652 _gtk, gd->gtk_len) < 0) {
653 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
654 "WPA: Failed to set GTK to "
655 "the driver (alg=%d keylen=%d keyidx=%d)",
656 gd->alg, gd->gtk_len, gd->keyidx);
661 sm->gtk_wnm_sleep.gtk_len = gd->gtk_len;
662 os_memcpy(sm->gtk_wnm_sleep.gtk, gd->gtk,
663 sm->gtk_wnm_sleep.gtk_len);
665 sm->gtk.gtk_len = gd->gtk_len;
666 os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
673 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
676 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
677 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
678 * seemed to set this bit (incorrectly, since Tx is only when
679 * doing Group Key only APs) and without this workaround, the
680 * data connection does not work because wpa_supplicant
681 * configured non-zero keyidx to be used for unicast. */
682 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
683 "WPA: Tx bit set for GTK, but pairwise "
684 "keys are used - ignore Tx bit");
691 static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
692 const struct wpa_eapol_key *key,
693 const u8 *gtk, size_t gtk_len,
696 #ifndef CONFIG_NO_WPA2
697 struct wpa_gtk_data gd;
700 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
702 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
703 * Reserved [bits 0-7]
707 os_memset(&gd, 0, sizeof(gd));
708 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
711 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
714 gd.keyidx = gtk[0] & 0x3;
715 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
716 !!(gtk[0] & BIT(2)));
720 os_memcpy(gd.gtk, gtk, gtk_len);
721 gd.gtk_len = gtk_len;
723 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
725 &gd.key_rsc_len, &gd.alg) ||
726 wpa_supplicant_install_gtk(sm, &gd, key->key_rsc, 0)) {
727 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
728 "RSN: Failed to install GTK");
732 wpa_supplicant_key_neg_complete(sm, sm->bssid,
733 key_info & WPA_KEY_INFO_SECURE);
735 #else /* CONFIG_NO_WPA2 */
737 #endif /* CONFIG_NO_WPA2 */
741 #ifdef CONFIG_IEEE80211W
742 static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
743 const struct wpa_igtk_kde *igtk,
746 size_t len = WPA_IGTK_LEN;
747 u16 keyidx = WPA_GET_LE16(igtk->keyid);
749 /* Detect possible key reinstallation */
750 if ((sm->igtk.igtk_len == len &&
751 os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) ||
752 (sm->igtk_wnm_sleep.igtk_len == len &&
753 os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk,
754 sm->igtk_wnm_sleep.igtk_len) == 0)) {
755 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
756 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
761 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
762 "WPA: IGTK keyid %d pn %02x%02x%02x%02x%02x%02x",
763 keyidx, MAC2STR(igtk->pn));
764 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
766 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
767 "WPA: Invalid IGTK KeyID %d", keyidx);
770 if (wpa_sm_set_key(sm, WPA_ALG_IGTK, broadcast_ether_addr,
771 keyidx, 0, igtk->pn, sizeof(igtk->pn),
772 igtk->igtk, WPA_IGTK_LEN) < 0) {
773 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
774 "WPA: Failed to configure IGTK to the driver");
779 sm->igtk_wnm_sleep.igtk_len = len;
780 os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk,
781 sm->igtk_wnm_sleep.igtk_len);
783 sm->igtk.igtk_len = len;
784 os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
789 #endif /* CONFIG_IEEE80211W */
792 static int ieee80211w_set_keys(struct wpa_sm *sm,
793 struct wpa_eapol_ie_parse *ie)
795 #ifdef CONFIG_IEEE80211W
796 if (sm->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC)
800 const struct wpa_igtk_kde *igtk;
801 if (ie->igtk_len != sizeof(*igtk))
804 igtk = (const struct wpa_igtk_kde *) ie->igtk;
805 if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0)
810 #else /* CONFIG_IEEE80211W */
812 #endif /* CONFIG_IEEE80211W */
816 static void wpa_report_ie_mismatch(struct wpa_sm *sm,
817 const char *reason, const u8 *src_addr,
818 const u8 *wpa_ie, size_t wpa_ie_len,
819 const u8 *rsn_ie, size_t rsn_ie_len)
821 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
822 reason, MAC2STR(src_addr));
825 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
826 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
829 if (!sm->ap_wpa_ie) {
830 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
831 "WPA: No WPA IE in Beacon/ProbeResp");
833 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
838 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
839 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
842 if (!sm->ap_rsn_ie) {
843 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
844 "WPA: No RSN IE in Beacon/ProbeResp");
846 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
850 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
854 #ifdef CONFIG_IEEE80211R
856 static int ft_validate_mdie(struct wpa_sm *sm,
857 const unsigned char *src_addr,
858 struct wpa_eapol_ie_parse *ie,
859 const u8 *assoc_resp_mdie)
861 struct rsn_mdie *mdie;
863 mdie = (struct rsn_mdie *) (ie->mdie + 2);
864 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
865 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
866 MOBILITY_DOMAIN_ID_LEN) != 0) {
867 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
868 "not match with the current mobility domain");
872 if (assoc_resp_mdie &&
873 (assoc_resp_mdie[1] != ie->mdie[1] ||
874 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
875 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
876 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
877 ie->mdie, 2 + ie->mdie[1]);
878 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
879 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
887 static int ft_validate_ftie(struct wpa_sm *sm,
888 const unsigned char *src_addr,
889 struct wpa_eapol_ie_parse *ie,
890 const u8 *assoc_resp_ftie)
892 if (ie->ftie == NULL) {
893 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
894 "FT: No FTIE in EAPOL-Key msg 3/4");
898 if (assoc_resp_ftie == NULL)
901 if (assoc_resp_ftie[1] != ie->ftie[1] ||
902 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
903 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
904 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
905 ie->ftie, 2 + ie->ftie[1]);
906 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
907 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
915 static int ft_validate_rsnie(struct wpa_sm *sm,
916 const unsigned char *src_addr,
917 struct wpa_eapol_ie_parse *ie)
919 struct wpa_ie_data rsn;
925 * Verify that PMKR1Name from EAPOL-Key message 3/4
926 * matches with the value we derived.
928 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
929 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
930 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
931 "FT 4-way handshake message 3/4");
935 if (os_memcmp(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0) {
936 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
937 "FT: PMKR1Name mismatch in "
938 "FT 4-way handshake message 3/4");
939 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
940 rsn.pmkid, WPA_PMK_NAME_LEN);
941 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
942 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
950 static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
951 const unsigned char *src_addr,
952 struct wpa_eapol_ie_parse *ie)
954 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
956 if (sm->assoc_resp_ies) {
957 pos = sm->assoc_resp_ies;
958 end = pos + sm->assoc_resp_ies_len;
959 while (pos + 2 < end) {
960 if (pos + 2 + pos[1] > end)
963 case WLAN_EID_MOBILITY_DOMAIN:
966 case WLAN_EID_FAST_BSS_TRANSITION:
974 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
975 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
976 ft_validate_rsnie(sm, src_addr, ie) < 0)
982 #endif /* CONFIG_IEEE80211R */
985 static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
986 const unsigned char *src_addr,
987 struct wpa_eapol_ie_parse *ie)
989 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
990 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
991 "WPA: No WPA/RSN IE for this AP known. "
992 "Trying to get from scan results");
993 if (wpa_sm_get_beacon_ie(sm) < 0) {
994 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
995 "WPA: Could not find AP from "
998 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
999 "WPA: Found the current AP from "
1000 "updated scan results");
1004 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1005 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1006 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1007 "with IE in Beacon/ProbeResp (no IE?)",
1008 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1009 ie->rsn_ie, ie->rsn_ie_len);
1013 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1014 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1015 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1016 (ie->rsn_ie && sm->ap_rsn_ie &&
1017 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1018 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1019 ie->rsn_ie, ie->rsn_ie_len))) {
1020 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1021 "with IE in Beacon/ProbeResp",
1022 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1023 ie->rsn_ie, ie->rsn_ie_len);
1027 if (sm->proto == WPA_PROTO_WPA &&
1028 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1029 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1030 "detected - RSN was enabled and RSN IE "
1031 "was in msg 3/4, but not in "
1033 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1034 ie->rsn_ie, ie->rsn_ie_len);
1038 #ifdef CONFIG_IEEE80211R
1039 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1040 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1042 #endif /* CONFIG_IEEE80211R */
1049 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1050 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1051 * @dst: Destination address for the frame
1052 * @key: Pointer to the EAPOL-Key frame header
1053 * @ver: Version bits from EAPOL-Key Key Info
1054 * @key_info: Key Info
1055 * @kde: KDEs to include the EAPOL-Key frame
1056 * @kde_len: Length of KDEs
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 const u8 *kde, size_t kde_len,
1064 struct wpa_ptk *ptk)
1067 struct wpa_eapol_key *reply;
1071 wpa_hexdump(MSG_DEBUG, "WPA: KDE for msg 4/4", kde, kde_len);
1073 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1074 sizeof(*reply) + kde_len,
1075 &rlen, (void *) &reply);
1079 reply->type = sm->proto == WPA_PROTO_RSN ?
1080 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1081 key_info &= WPA_KEY_INFO_SECURE;
1082 key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
1083 WPA_PUT_BE16(reply->key_info, key_info);
1084 if (sm->proto == WPA_PROTO_RSN)
1085 WPA_PUT_BE16(reply->key_length, 0);
1087 os_memcpy(reply->key_length, key->key_length, 2);
1088 os_memcpy(reply->replay_counter, key->replay_counter,
1089 WPA_REPLAY_COUNTER_LEN);
1091 WPA_PUT_BE16(reply->key_data_length, kde_len);
1093 os_memcpy(reply + 1, kde, kde_len);
1095 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
1096 wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
1097 rbuf, rlen, reply->key_mic);
1103 static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1104 const struct wpa_eapol_key *key,
1107 u16 key_info, keylen, len;
1109 struct wpa_eapol_ie_parse ie;
1111 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
1112 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way "
1113 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1115 key_info = WPA_GET_BE16(key->key_info);
1117 pos = (const u8 *) (key + 1);
1118 len = WPA_GET_BE16(key->key_data_length);
1119 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", pos, len);
1120 if (wpa_supplicant_parse_ies(pos, len, &ie) < 0)
1122 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1123 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1124 "WPA: GTK IE in unencrypted key data");
1127 #ifdef CONFIG_IEEE80211W
1128 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1129 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1130 "WPA: IGTK KDE in unencrypted key data");
1134 if (ie.igtk && ie.igtk_len != sizeof(struct wpa_igtk_kde)) {
1135 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1136 "WPA: Invalid IGTK KDE length %lu",
1137 (unsigned long) ie.igtk_len);
1140 #endif /* CONFIG_IEEE80211W */
1142 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1145 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1146 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1147 "WPA: ANonce from message 1 of 4-Way Handshake "
1148 "differs from 3 of 4-Way Handshake - drop packet (src="
1149 MACSTR ")", MAC2STR(sm->bssid));
1153 keylen = WPA_GET_BE16(key->key_length);
1154 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1155 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1156 "WPA: Invalid %s key length %d (src=" MACSTR
1157 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1158 MAC2STR(sm->bssid));
1162 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
1163 NULL, 0, &sm->ptk)) {
1167 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1168 * for the next 4-Way Handshake. If msg 3 is received again, the old
1169 * SNonce will still be used to avoid changing PTK. */
1170 sm->renew_snonce = 1;
1172 if (key_info & WPA_KEY_INFO_INSTALL) {
1173 if (wpa_supplicant_install_ptk(sm, key))
1177 if (key_info & WPA_KEY_INFO_SECURE) {
1178 wpa_sm_mlme_setprotection(
1179 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1180 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1181 eapol_sm_notify_portValid(sm->eapol, TRUE);
1183 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1186 wpa_supplicant_pairwise_gtk(sm, key,
1187 ie.gtk, ie.gtk_len, key_info) < 0) {
1188 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1189 "RSN: Failed to configure GTK");
1193 if (ieee80211w_set_keys(sm, &ie) < 0) {
1194 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1195 "RSN: Failed to configure IGTK");
1199 wpa_sm_set_rekey_offload(sm);
1204 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1208 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1212 struct wpa_gtk_data *gd)
1215 struct wpa_eapol_ie_parse ie;
1217 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
1218 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1220 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1221 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1222 "WPA: GTK IE in unencrypted key data");
1225 if (ie.gtk == NULL) {
1226 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1227 "WPA: No GTK IE in Group Key msg 1/2");
1230 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1232 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1233 gd->gtk_len, maxkeylen,
1234 &gd->key_rsc_len, &gd->alg))
1237 wpa_hexdump(MSG_DEBUG, "RSN: received GTK in group key handshake",
1238 ie.gtk, ie.gtk_len);
1239 gd->keyidx = ie.gtk[0] & 0x3;
1240 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1241 !!(ie.gtk[0] & BIT(2)));
1242 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1243 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1244 "RSN: Too long GTK in GTK IE (len=%lu)",
1245 (unsigned long) ie.gtk_len - 2);
1248 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1250 if (ieee80211w_set_keys(sm, &ie) < 0)
1251 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1252 "RSN: Failed to configure IGTK");
1258 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1259 const struct wpa_eapol_key *key,
1260 size_t keydatalen, int key_info,
1261 size_t extra_len, u16 ver,
1262 struct wpa_gtk_data *gd)
1267 gd->gtk_len = WPA_GET_BE16(key->key_length);
1268 maxkeylen = keydatalen;
1269 if (keydatalen > extra_len) {
1270 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1271 "WPA: Truncated EAPOL-Key packet: "
1272 "key_data_length=%lu > extra_len=%lu",
1273 (unsigned long) keydatalen, (unsigned long) extra_len);
1276 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1277 if (maxkeylen < 8) {
1278 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1279 "WPA: Too short maxkeylen (%lu)",
1280 (unsigned long) maxkeylen);
1286 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1287 gd->gtk_len, maxkeylen,
1288 &gd->key_rsc_len, &gd->alg))
1291 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1292 WPA_KEY_INFO_KEY_INDEX_SHIFT;
1293 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1294 os_memcpy(ek, key->key_iv, 16);
1295 os_memcpy(ek + 16, sm->ptk.kek, 16);
1296 if (keydatalen > sizeof(gd->gtk)) {
1297 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1298 "WPA: RC4 key data too long (%lu)",
1299 (unsigned long) keydatalen);
1302 os_memcpy(gd->gtk, key + 1, keydatalen);
1303 if (rc4_skip(ek, 32, 256, gd->gtk, keydatalen)) {
1304 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1308 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1309 if (keydatalen % 8) {
1310 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1311 "WPA: Unsupported AES-WRAP len %lu",
1312 (unsigned long) keydatalen);
1315 if (maxkeylen > sizeof(gd->gtk)) {
1316 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1317 "WPA: AES-WRAP key data "
1318 "too long (keydatalen=%lu maxkeylen=%lu)",
1319 (unsigned long) keydatalen,
1320 (unsigned long) maxkeylen);
1323 if (aes_unwrap(sm->ptk.kek, maxkeylen / 8,
1324 (const u8 *) (key + 1), gd->gtk)) {
1325 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1326 "WPA: AES unwrap failed - could not decrypt "
1331 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1332 "WPA: Unsupported key_info type %d", ver);
1335 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1336 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1341 static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1342 const struct wpa_eapol_key *key,
1343 int ver, u16 key_info)
1346 struct wpa_eapol_key *reply;
1349 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1350 sizeof(*reply), &rlen, (void *) &reply);
1354 reply->type = sm->proto == WPA_PROTO_RSN ?
1355 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1356 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1357 key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
1358 WPA_PUT_BE16(reply->key_info, key_info);
1359 if (sm->proto == WPA_PROTO_RSN)
1360 WPA_PUT_BE16(reply->key_length, 0);
1362 os_memcpy(reply->key_length, key->key_length, 2);
1363 os_memcpy(reply->replay_counter, key->replay_counter,
1364 WPA_REPLAY_COUNTER_LEN);
1366 WPA_PUT_BE16(reply->key_data_length, 0);
1368 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
1369 wpa_eapol_key_send(sm, sm->ptk.kck, ver, sm->bssid, ETH_P_EAPOL,
1370 rbuf, rlen, reply->key_mic);
1376 static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1377 const unsigned char *src_addr,
1378 const struct wpa_eapol_key *key,
1379 int extra_len, u16 ver)
1381 u16 key_info, keydatalen;
1383 struct wpa_gtk_data gd;
1385 os_memset(&gd, 0, sizeof(gd));
1387 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1388 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1389 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1391 key_info = WPA_GET_BE16(key->key_info);
1392 keydatalen = WPA_GET_BE16(key->key_data_length);
1394 if (sm->proto == WPA_PROTO_RSN) {
1395 ret = wpa_supplicant_process_1_of_2_rsn(sm,
1396 (const u8 *) (key + 1),
1397 keydatalen, key_info,
1400 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, keydatalen,
1401 key_info, extra_len,
1405 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1410 if (wpa_supplicant_install_gtk(sm, &gd, key->key_rsc, 0) ||
1411 wpa_supplicant_send_2_of_2(sm, key, ver, key_info))
1415 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Group rekeying "
1416 "completed with " MACSTR " [GTK=%s]",
1417 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1418 wpa_sm_cancel_auth_timeout(sm);
1419 wpa_sm_set_state(sm, WPA_COMPLETED);
1421 wpa_sm_set_rekey_offload(sm);
1423 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1425 WPA_KEY_INFO_SECURE);
1430 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1434 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
1435 struct wpa_eapol_key *key,
1437 const u8 *buf, size_t len)
1442 os_memcpy(mic, key->key_mic, 16);
1444 os_memset(key->key_mic, 0, 16);
1445 wpa_eapol_key_mic(sm->tptk.kck, ver, buf, len,
1447 if (os_memcmp(mic, key->key_mic, 16) != 0) {
1448 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1449 "WPA: Invalid EAPOL-Key MIC "
1450 "when using TPTK - ignoring TPTK");
1455 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
1459 if (!ok && sm->ptk_set) {
1460 os_memset(key->key_mic, 0, 16);
1461 wpa_eapol_key_mic(sm->ptk.kck, ver, buf, len,
1463 if (os_memcmp(mic, key->key_mic, 16) != 0) {
1464 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1465 "WPA: Invalid EAPOL-Key MIC - "
1473 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1474 "WPA: Could not verify EAPOL-Key MIC - "
1479 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1480 WPA_REPLAY_COUNTER_LEN);
1481 sm->rx_replay_counter_set = 1;
1486 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1487 static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
1488 struct wpa_eapol_key *key, u16 ver)
1490 u16 keydatalen = WPA_GET_BE16(key->key_data_length);
1492 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
1493 (u8 *) (key + 1), keydatalen);
1495 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1496 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1501 /* Decrypt key data here so that this operation does not need
1502 * to be implemented separately for each message type. */
1503 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1505 os_memcpy(ek, key->key_iv, 16);
1506 os_memcpy(ek + 16, sm->ptk.kek, 16);
1507 if (rc4_skip(ek, 32, 256, (u8 *) (key + 1), keydatalen)) {
1508 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1512 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1513 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1515 if (keydatalen % 8) {
1516 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1517 "WPA: Unsupported AES-WRAP len %d",
1521 keydatalen -= 8; /* AES-WRAP adds 8 bytes */
1522 buf = os_malloc(keydatalen);
1524 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1525 "WPA: No memory for AES-UNWRAP buffer");
1528 if (aes_unwrap(sm->ptk.kek, keydatalen / 8,
1529 (u8 *) (key + 1), buf)) {
1531 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1532 "WPA: AES unwrap failed - "
1533 "could not decrypt EAPOL-Key key data");
1536 os_memcpy(key + 1, buf, keydatalen);
1538 WPA_PUT_BE16(key->key_data_length, keydatalen);
1540 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1541 "WPA: Unsupported key_info type %d", ver);
1544 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
1545 (u8 *) (key + 1), keydatalen);
1551 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1552 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1554 void wpa_sm_aborted_cached(struct wpa_sm *sm)
1556 if (sm && sm->cur_pmksa) {
1557 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1558 "RSN: Cancelling PMKSA caching attempt");
1559 sm->cur_pmksa = NULL;
1564 static void wpa_eapol_key_dump(struct wpa_sm *sm,
1565 const struct wpa_eapol_key *key)
1567 #ifndef CONFIG_NO_STDOUT_DEBUG
1568 u16 key_info = WPA_GET_BE16(key->key_info);
1570 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1571 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1572 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
1573 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1574 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1575 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1576 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1577 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1578 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1579 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1580 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1581 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1582 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1583 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1584 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1585 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1586 " key_length=%u key_data_length=%u",
1587 WPA_GET_BE16(key->key_length),
1588 WPA_GET_BE16(key->key_data_length));
1589 wpa_hexdump(MSG_DEBUG, " replay_counter",
1590 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1591 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1592 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1593 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1594 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
1595 wpa_hexdump(MSG_DEBUG, " key_mic", key->key_mic, 16);
1596 #endif /* CONFIG_NO_STDOUT_DEBUG */
1601 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1602 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1603 * @src_addr: Source MAC address of the EAPOL packet
1604 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1605 * @len: Length of the EAPOL frame
1606 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1608 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1609 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1610 * only processing WPA and WPA2 EAPOL-Key frames.
1612 * The received EAPOL-Key packets are validated and valid packets are replied
1613 * to. In addition, key material (PTK, GTK) is configured at the end of a
1614 * successful key handshake.
1616 int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1617 const u8 *buf, size_t len)
1619 size_t plen, data_len, extra_len;
1620 struct ieee802_1x_hdr *hdr;
1621 struct wpa_eapol_key *key;
1625 struct wpa_peerkey *peerkey = NULL;
1627 #ifdef CONFIG_IEEE80211R
1628 sm->ft_completed = 0;
1629 #endif /* CONFIG_IEEE80211R */
1631 if (len < sizeof(*hdr) + sizeof(*key)) {
1632 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1633 "WPA: EAPOL frame too short to be a WPA "
1634 "EAPOL-Key (len %lu, expecting at least %lu)",
1635 (unsigned long) len,
1636 (unsigned long) sizeof(*hdr) + sizeof(*key));
1640 tmp = os_malloc(len);
1643 os_memcpy(tmp, buf, len);
1645 hdr = (struct ieee802_1x_hdr *) tmp;
1646 key = (struct wpa_eapol_key *) (hdr + 1);
1647 plen = be_to_host16(hdr->length);
1648 data_len = plen + sizeof(*hdr);
1649 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1650 "IEEE 802.1X RX: version=%d type=%d length=%lu",
1651 hdr->version, hdr->type, (unsigned long) plen);
1653 if (hdr->version < EAPOL_VERSION) {
1654 /* TODO: backwards compatibility */
1656 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
1657 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1658 "WPA: EAPOL frame (type %u) discarded, "
1659 "not a Key frame", hdr->type);
1663 if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
1664 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1665 "WPA: EAPOL frame payload size %lu "
1666 "invalid (frame size %lu)",
1667 (unsigned long) plen, (unsigned long) len);
1672 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
1674 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1675 "WPA: EAPOL-Key type (%d) unknown, discarded",
1680 wpa_eapol_key_dump(sm, key);
1682 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
1683 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", tmp, len);
1684 if (data_len < len) {
1685 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1686 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
1687 (unsigned long) len - data_len);
1689 key_info = WPA_GET_BE16(key->key_info);
1690 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1691 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
1692 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
1693 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1694 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
1695 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1696 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1697 "WPA: Unsupported EAPOL-Key descriptor version %d",
1702 #ifdef CONFIG_IEEE80211R
1703 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
1704 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
1705 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1706 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1707 "FT: AP did not use AES-128-CMAC");
1711 #endif /* CONFIG_IEEE80211R */
1712 #ifdef CONFIG_IEEE80211W
1713 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
1714 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1715 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1716 "WPA: AP did not use the "
1717 "negotiated AES-128-CMAC");
1721 #endif /* CONFIG_IEEE80211W */
1722 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
1723 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1724 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1725 "WPA: CCMP is used, but EAPOL-Key "
1726 "descriptor version (%d) is not 2", ver);
1727 if (sm->group_cipher != WPA_CIPHER_CCMP &&
1728 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1729 /* Earlier versions of IEEE 802.11i did not explicitly
1730 * require version 2 descriptor for all EAPOL-Key
1731 * packets, so allow group keys to use version 1 if
1732 * CCMP is not used for them. */
1733 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1734 "WPA: Backwards compatibility: allow invalid "
1735 "version for non-CCMP group keys");
1739 if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
1740 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1741 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1742 "WPA: GCMP is used, but EAPOL-Key "
1743 "descriptor version (%d) is not 2", ver);
1747 #ifdef CONFIG_PEERKEY
1748 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
1749 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
1753 if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
1754 if (!peerkey->initiator && peerkey->replay_counter_set &&
1755 os_memcmp(key->replay_counter, peerkey->replay_counter,
1756 WPA_REPLAY_COUNTER_LEN) <= 0) {
1757 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1758 "RSN: EAPOL-Key Replay Counter did not "
1759 "increase (STK) - dropping packet");
1761 } else if (peerkey->initiator) {
1762 u8 _tmp[WPA_REPLAY_COUNTER_LEN];
1763 os_memcpy(_tmp, key->replay_counter,
1764 WPA_REPLAY_COUNTER_LEN);
1765 inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
1766 if (os_memcmp(_tmp, peerkey->replay_counter,
1767 WPA_REPLAY_COUNTER_LEN) != 0) {
1768 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1769 "RSN: EAPOL-Key Replay "
1770 "Counter did not match (STK) - "
1777 if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
1778 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1779 "RSN: Ack bit in key_info from STK peer");
1782 #endif /* CONFIG_PEERKEY */
1784 if (!peerkey && sm->rx_replay_counter_set &&
1785 os_memcmp(key->replay_counter, sm->rx_replay_counter,
1786 WPA_REPLAY_COUNTER_LEN) <= 0) {
1787 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1788 "WPA: EAPOL-Key Replay Counter did not increase - "
1793 if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
1794 #ifdef CONFIG_PEERKEY
1795 && (peerkey == NULL || !peerkey->initiator)
1796 #endif /* CONFIG_PEERKEY */
1798 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1799 "WPA: No Ack bit in key_info");
1803 if (key_info & WPA_KEY_INFO_REQUEST) {
1804 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1805 "WPA: EAPOL-Key with Request bit - dropped");
1809 if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
1810 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
1813 #ifdef CONFIG_PEERKEY
1814 if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
1815 peerkey_verify_eapol_key_mic(sm, peerkey, key, ver, tmp, data_len))
1817 #endif /* CONFIG_PEERKEY */
1819 extra_len = data_len - sizeof(*hdr) - sizeof(*key);
1821 if (WPA_GET_BE16(key->key_data_length) > extra_len) {
1822 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
1823 "frame - key_data overflow (%d > %lu)",
1824 WPA_GET_BE16(key->key_data_length),
1825 (unsigned long) extra_len);
1828 extra_len = WPA_GET_BE16(key->key_data_length);
1830 if (sm->proto == WPA_PROTO_RSN &&
1831 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1833 * Only decrypt the Key Data field if the frame's authenticity
1834 * was verified. When using AES-SIV (FILS), the MIC flag is not
1835 * set, so this check should only be performed if mic_len != 0
1836 * which is the case in this code branch.
1838 if (!(key_info & WPA_KEY_INFO_MIC)) {
1839 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1840 "WPA: Ignore EAPOL-Key with encrypted but unauthenticated data");
1843 if (wpa_supplicant_decrypt_key_data(sm, key, ver))
1845 extra_len = WPA_GET_BE16(key->key_data_length);
1848 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1849 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
1850 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1851 "WPA: Ignored EAPOL-Key (Pairwise) with "
1852 "non-zero key index");
1856 /* PeerKey 4-Way Handshake */
1857 peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver);
1858 } else if (key_info & WPA_KEY_INFO_MIC) {
1859 /* 3/4 4-Way Handshake */
1860 wpa_supplicant_process_3_of_4(sm, key, ver);
1862 /* 1/4 4-Way Handshake */
1863 wpa_supplicant_process_1_of_4(sm, src_addr, key,
1866 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
1867 /* PeerKey SMK Handshake */
1868 peerkey_rx_eapol_smk(sm, src_addr, key, extra_len, key_info,
1871 if (key_info & WPA_KEY_INFO_MIC) {
1872 /* 1/2 Group Key Handshake */
1873 wpa_supplicant_process_1_of_2(sm, src_addr, key,
1876 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1877 "WPA: EAPOL-Key (Group) without Mic bit - "
1890 #ifdef CONFIG_CTRL_IFACE
1891 static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
1893 switch (sm->key_mgmt) {
1894 case WPA_KEY_MGMT_IEEE8021X:
1895 return (sm->proto == WPA_PROTO_RSN ?
1896 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
1897 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
1898 case WPA_KEY_MGMT_PSK:
1899 return (sm->proto == WPA_PROTO_RSN ?
1900 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
1901 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
1902 #ifdef CONFIG_IEEE80211R
1903 case WPA_KEY_MGMT_FT_IEEE8021X:
1904 return RSN_AUTH_KEY_MGMT_FT_802_1X;
1905 case WPA_KEY_MGMT_FT_PSK:
1906 return RSN_AUTH_KEY_MGMT_FT_PSK;
1907 #endif /* CONFIG_IEEE80211R */
1908 #ifdef CONFIG_IEEE80211W
1909 case WPA_KEY_MGMT_IEEE8021X_SHA256:
1910 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
1911 case WPA_KEY_MGMT_PSK_SHA256:
1912 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
1913 #endif /* CONFIG_IEEE80211W */
1914 case WPA_KEY_MGMT_CCKM:
1915 return (sm->proto == WPA_PROTO_RSN ?
1916 RSN_AUTH_KEY_MGMT_CCKM:
1917 WPA_AUTH_KEY_MGMT_CCKM);
1918 case WPA_KEY_MGMT_WPA_NONE:
1919 return WPA_AUTH_KEY_MGMT_NONE;
1926 #define RSN_SUITE "%02x-%02x-%02x-%d"
1927 #define RSN_SUITE_ARG(s) \
1928 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
1931 * wpa_sm_get_mib - Dump text list of MIB entries
1932 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1933 * @buf: Buffer for the list
1934 * @buflen: Length of the buffer
1935 * Returns: Number of bytes written to buffer
1937 * This function is used fetch dot11 MIB variables.
1939 int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
1941 char pmkid_txt[PMKID_LEN * 2 + 1];
1945 if (sm->cur_pmksa) {
1946 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
1947 sm->cur_pmksa->pmkid, PMKID_LEN);
1949 pmkid_txt[0] = '\0';
1951 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
1952 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
1953 sm->proto == WPA_PROTO_RSN)
1958 ret = os_snprintf(buf, buflen,
1959 "dot11RSNAOptionImplemented=TRUE\n"
1960 "dot11RSNAPreauthenticationImplemented=TRUE\n"
1961 "dot11RSNAEnabled=%s\n"
1962 "dot11RSNAPreauthenticationEnabled=%s\n"
1963 "dot11RSNAConfigVersion=%d\n"
1964 "dot11RSNAConfigPairwiseKeysSupported=5\n"
1965 "dot11RSNAConfigGroupCipherSize=%d\n"
1966 "dot11RSNAConfigPMKLifetime=%d\n"
1967 "dot11RSNAConfigPMKReauthThreshold=%d\n"
1968 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
1969 "dot11RSNAConfigSATimeout=%d\n",
1970 rsna ? "TRUE" : "FALSE",
1971 rsna ? "TRUE" : "FALSE",
1973 wpa_cipher_key_len(sm->group_cipher) * 8,
1974 sm->dot11RSNAConfigPMKLifetime,
1975 sm->dot11RSNAConfigPMKReauthThreshold,
1976 sm->dot11RSNAConfigSATimeout);
1977 if (ret < 0 || (size_t) ret >= buflen)
1982 buf + len, buflen - len,
1983 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
1984 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
1985 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
1986 "dot11RSNAPMKIDUsed=%s\n"
1987 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
1988 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
1989 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
1990 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
1991 "dot11RSNA4WayHandshakeFailures=%u\n",
1992 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
1993 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
1994 sm->pairwise_cipher)),
1995 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
1998 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
1999 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2000 sm->pairwise_cipher)),
2001 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2003 sm->dot11RSNA4WayHandshakeFailures);
2004 if (ret >= 0 && (size_t) ret < buflen)
2009 #endif /* CONFIG_CTRL_IFACE */
2012 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
2013 void *ctx, enum pmksa_free_reason reason)
2015 struct wpa_sm *sm = ctx;
2018 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2019 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2021 if (sm->cur_pmksa == entry) {
2022 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2023 "RSN: %s current PMKSA entry",
2024 reason == PMKSA_REPLACE ? "replaced" : "removed");
2025 pmksa_cache_clear_current(sm);
2028 * If an entry is simply being replaced, there's no need to
2029 * deauthenticate because it will be immediately re-added.
2030 * This happens when EAP authentication is completed again
2031 * (reauth or failed PMKSA caching attempt).
2033 if (reason != PMKSA_REPLACE)
2037 if (reason == PMKSA_EXPIRE &&
2038 (sm->pmk_len == entry->pmk_len &&
2039 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2040 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2041 "RSN: deauthenticating due to expired PMK");
2042 pmksa_cache_clear_current(sm);
2047 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2048 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2054 * wpa_sm_init - Initialize WPA state machine
2055 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2056 * Returns: Pointer to the allocated WPA state machine data
2058 * This function is used to allocate a new WPA state machine and the returned
2059 * value is passed to all WPA state machine calls.
2061 struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2065 sm = os_zalloc(sizeof(*sm));
2068 dl_list_init(&sm->pmksa_candidates);
2069 sm->renew_snonce = 1;
2072 sm->dot11RSNAConfigPMKLifetime = 43200;
2073 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2074 sm->dot11RSNAConfigSATimeout = 60;
2076 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2077 if (sm->pmksa == NULL) {
2078 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2079 "RSN: PMKSA cache initialization failed");
2089 * wpa_sm_deinit - Deinitialize WPA state machine
2090 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2092 void wpa_sm_deinit(struct wpa_sm *sm)
2096 pmksa_cache_deinit(sm->pmksa);
2097 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2098 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2099 os_free(sm->assoc_wpa_ie);
2100 os_free(sm->ap_wpa_ie);
2101 os_free(sm->ap_rsn_ie);
2104 #ifdef CONFIG_IEEE80211R
2105 os_free(sm->assoc_resp_ies);
2106 #endif /* CONFIG_IEEE80211R */
2112 * wpa_sm_notify_assoc - Notify WPA state machine about association
2113 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2114 * @bssid: The BSSID of the new association
2116 * This function is called to let WPA state machine know that the connection
2119 void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2126 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2127 "WPA: Association event - clear replay counter");
2128 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2129 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2130 sm->rx_replay_counter_set = 0;
2131 sm->renew_snonce = 1;
2132 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2133 rsn_preauth_deinit(sm);
2135 #ifdef CONFIG_IEEE80211R
2136 if (wpa_ft_is_completed(sm)) {
2138 * Clear portValid to kick EAPOL state machine to re-enter
2139 * AUTHENTICATED state to get the EAPOL port Authorized.
2141 eapol_sm_notify_portValid(sm->eapol, FALSE);
2142 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2144 /* Prepare for the next transition */
2145 wpa_ft_prepare_auth_request(sm, NULL);
2149 #endif /* CONFIG_IEEE80211R */
2153 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2154 * this is not part of a Fast BSS Transition.
2156 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
2159 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
2160 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
2161 #ifdef CONFIG_IEEE80211W
2162 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
2163 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
2164 #endif /* CONFIG_IEEE80211W */
2169 #endif /* CONFIG_TDLS */
2174 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2175 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2177 * This function is called to let WPA state machine know that the connection
2178 * was lost. This will abort any existing pre-authentication session.
2180 void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2182 rsn_preauth_deinit(sm);
2183 pmksa_cache_clear_current(sm);
2184 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2185 sm->dot11RSNA4WayHandshakeFailures++;
2187 wpa_tdls_disassoc(sm);
2188 #endif /* CONFIG_TDLS */
2189 #ifdef CONFIG_IEEE80211R
2190 sm->ft_reassoc_completed = 0;
2191 #endif /* CONFIG_IEEE80211R */
2196 * wpa_sm_set_pmk - Set PMK
2197 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2199 * @pmk_len: The length of the new PMK in bytes
2201 * Configure the PMK for WPA state machine.
2203 void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len)
2208 sm->pmk_len = pmk_len;
2209 os_memcpy(sm->pmk, pmk, pmk_len);
2211 #ifdef CONFIG_IEEE80211R
2212 /* Set XXKey to be PSK for FT key derivation */
2213 sm->xxkey_len = pmk_len;
2214 os_memcpy(sm->xxkey, pmk, pmk_len);
2215 #endif /* CONFIG_IEEE80211R */
2220 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2221 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2223 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2226 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2231 if (sm->cur_pmksa) {
2232 sm->pmk_len = sm->cur_pmksa->pmk_len;
2233 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2235 sm->pmk_len = PMK_LEN;
2236 os_memset(sm->pmk, 0, PMK_LEN);
2242 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2243 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2244 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2246 void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2249 sm->fast_reauth = fast_reauth;
2254 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2255 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2256 * @scard_ctx: Context pointer for smartcard related callback functions
2258 void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2262 sm->scard_ctx = scard_ctx;
2263 if (sm->preauth_eapol)
2264 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2269 * wpa_sm_set_config - Notification of current configration change
2270 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2271 * @config: Pointer to current network configuration
2273 * Notify WPA state machine that configuration has changed. config will be
2274 * stored as a backpointer to network configuration. This can be %NULL to clear
2275 * the stored pointed.
2277 void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2283 sm->network_ctx = config->network_ctx;
2284 sm->peerkey_enabled = config->peerkey_enabled;
2285 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2286 sm->proactive_key_caching = config->proactive_key_caching;
2287 sm->eap_workaround = config->eap_workaround;
2288 sm->eap_conf_ctx = config->eap_conf_ctx;
2290 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2291 sm->ssid_len = config->ssid_len;
2294 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
2296 sm->network_ctx = NULL;
2297 sm->peerkey_enabled = 0;
2298 sm->allowed_pairwise_cipher = 0;
2299 sm->proactive_key_caching = 0;
2300 sm->eap_workaround = 0;
2301 sm->eap_conf_ctx = NULL;
2303 sm->wpa_ptk_rekey = 0;
2309 * wpa_sm_set_own_addr - Set own MAC address
2310 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2311 * @addr: Own MAC address
2313 void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2316 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2321 * wpa_sm_set_ifname - Set network interface name
2322 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2323 * @ifname: Interface name
2324 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2326 void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2327 const char *bridge_ifname)
2330 sm->ifname = ifname;
2331 sm->bridge_ifname = bridge_ifname;
2337 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2338 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2339 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2341 void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2349 * wpa_sm_set_param - Set WPA state machine parameters
2350 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2351 * @param: Parameter field
2352 * @value: Parameter value
2353 * Returns: 0 on success, -1 on failure
2355 int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2364 case RSNA_PMK_LIFETIME:
2366 sm->dot11RSNAConfigPMKLifetime = value;
2370 case RSNA_PMK_REAUTH_THRESHOLD:
2371 if (value > 0 && value <= 100)
2372 sm->dot11RSNAConfigPMKReauthThreshold = value;
2376 case RSNA_SA_TIMEOUT:
2378 sm->dot11RSNAConfigSATimeout = value;
2382 case WPA_PARAM_PROTO:
2385 case WPA_PARAM_PAIRWISE:
2386 sm->pairwise_cipher = value;
2388 case WPA_PARAM_GROUP:
2389 sm->group_cipher = value;
2391 case WPA_PARAM_KEY_MGMT:
2392 sm->key_mgmt = value;
2394 #ifdef CONFIG_IEEE80211W
2395 case WPA_PARAM_MGMT_GROUP:
2396 sm->mgmt_group_cipher = value;
2398 #endif /* CONFIG_IEEE80211W */
2399 case WPA_PARAM_RSN_ENABLED:
2400 sm->rsn_enabled = value;
2414 * wpa_sm_get_param - Get WPA state machine parameters
2415 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2416 * @param: Parameter field
2417 * Returns: Parameter value
2419 unsigned int wpa_sm_get_param(struct wpa_sm *sm, enum wpa_sm_conf_params param)
2425 case RSNA_PMK_LIFETIME:
2426 return sm->dot11RSNAConfigPMKLifetime;
2427 case RSNA_PMK_REAUTH_THRESHOLD:
2428 return sm->dot11RSNAConfigPMKReauthThreshold;
2429 case RSNA_SA_TIMEOUT:
2430 return sm->dot11RSNAConfigSATimeout;
2431 case WPA_PARAM_PROTO:
2433 case WPA_PARAM_PAIRWISE:
2434 return sm->pairwise_cipher;
2435 case WPA_PARAM_GROUP:
2436 return sm->group_cipher;
2437 case WPA_PARAM_KEY_MGMT:
2438 return sm->key_mgmt;
2439 #ifdef CONFIG_IEEE80211W
2440 case WPA_PARAM_MGMT_GROUP:
2441 return sm->mgmt_group_cipher;
2442 #endif /* CONFIG_IEEE80211W */
2443 case WPA_PARAM_RSN_ENABLED:
2444 return sm->rsn_enabled;
2452 * wpa_sm_get_status - Get WPA state machine
2453 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2454 * @buf: Buffer for status information
2455 * @buflen: Maximum buffer length
2456 * @verbose: Whether to include verbose status information
2457 * Returns: Number of bytes written to buf.
2459 * Query WPA state machine for status information. This function fills in
2460 * a text area with current status information. If the buffer (buf) is not
2461 * large enough, status information will be truncated to fit the buffer.
2463 int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2466 char *pos = buf, *end = buf + buflen;
2469 ret = os_snprintf(pos, end - pos,
2470 "pairwise_cipher=%s\n"
2473 wpa_cipher_txt(sm->pairwise_cipher),
2474 wpa_cipher_txt(sm->group_cipher),
2475 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
2476 if (ret < 0 || ret >= end - pos)
2480 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
2481 struct wpa_ie_data rsn;
2482 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
2484 rsn.capabilities & (WPA_CAPABILITY_MFPR |
2485 WPA_CAPABILITY_MFPC)) {
2486 ret = os_snprintf(pos, end - pos, "pmf=%d\n",
2488 WPA_CAPABILITY_MFPR) ? 2 : 1);
2489 if (ret < 0 || ret >= end - pos)
2500 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2501 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2502 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2503 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2504 * Returns: 0 on success, -1 on failure
2506 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2514 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2519 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2520 wpa_ie, *wpa_ie_len);
2522 if (sm->assoc_wpa_ie == NULL) {
2524 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2525 * the correct version of the IE even if PMKSA caching is
2526 * aborted (which would remove PMKID from IE generation).
2528 sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2529 if (sm->assoc_wpa_ie == NULL)
2532 os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2533 sm->assoc_wpa_ie_len = *wpa_ie_len;
2541 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2542 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2543 * @ie: Pointer to IE data (starting from id)
2545 * Returns: 0 on success, -1 on failure
2547 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2548 * Request frame. The IE will be used to override the default value generated
2549 * with wpa_sm_set_assoc_wpa_ie_default().
2551 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2556 os_free(sm->assoc_wpa_ie);
2557 if (ie == NULL || len == 0) {
2558 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2559 "WPA: clearing own WPA/RSN IE");
2560 sm->assoc_wpa_ie = NULL;
2561 sm->assoc_wpa_ie_len = 0;
2563 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2564 sm->assoc_wpa_ie = os_malloc(len);
2565 if (sm->assoc_wpa_ie == NULL)
2568 os_memcpy(sm->assoc_wpa_ie, ie, len);
2569 sm->assoc_wpa_ie_len = len;
2577 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2578 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2579 * @ie: Pointer to IE data (starting from id)
2581 * Returns: 0 on success, -1 on failure
2583 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2586 int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2591 os_free(sm->ap_wpa_ie);
2592 if (ie == NULL || len == 0) {
2593 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2594 "WPA: clearing AP WPA IE");
2595 sm->ap_wpa_ie = NULL;
2596 sm->ap_wpa_ie_len = 0;
2598 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2599 sm->ap_wpa_ie = os_malloc(len);
2600 if (sm->ap_wpa_ie == NULL)
2603 os_memcpy(sm->ap_wpa_ie, ie, len);
2604 sm->ap_wpa_ie_len = len;
2612 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2613 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2614 * @ie: Pointer to IE data (starting from id)
2616 * Returns: 0 on success, -1 on failure
2618 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2621 int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2626 os_free(sm->ap_rsn_ie);
2627 if (ie == NULL || len == 0) {
2628 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2629 "WPA: clearing AP RSN IE");
2630 sm->ap_rsn_ie = NULL;
2631 sm->ap_rsn_ie_len = 0;
2633 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2634 sm->ap_rsn_ie = os_malloc(len);
2635 if (sm->ap_rsn_ie == NULL)
2638 os_memcpy(sm->ap_rsn_ie, ie, len);
2639 sm->ap_rsn_ie_len = len;
2647 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2648 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2649 * @data: Pointer to data area for parsing results
2650 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2652 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2653 * parsed data into data.
2655 int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2660 if (sm->assoc_wpa_ie == NULL) {
2661 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2662 "WPA: No WPA/RSN IE available from association info");
2665 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
2671 int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
2673 #ifndef CONFIG_NO_WPA2
2674 return pmksa_cache_list(sm->pmksa, buf, len);
2675 #else /* CONFIG_NO_WPA2 */
2677 #endif /* CONFIG_NO_WPA2 */
2681 void wpa_sm_drop_sa(struct wpa_sm *sm)
2683 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
2686 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2687 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2688 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2689 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
2690 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
2691 #ifdef CONFIG_IEEE80211W
2692 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
2693 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
2694 #endif /* CONFIG_IEEE80211W */
2698 int wpa_sm_has_ptk(struct wpa_sm *sm)
2706 void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
2708 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
2712 void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
2714 #ifndef CONFIG_NO_WPA2
2715 pmksa_cache_flush(sm->pmksa, network_ctx);
2716 #endif /* CONFIG_NO_WPA2 */
2721 int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
2723 struct wpa_gtk_data gd;
2724 #ifdef CONFIG_IEEE80211W
2725 struct wpa_igtk_kde igd;
2727 #endif /* CONFIG_IEEE80211W */
2729 u8 keylen; /* plaintext key len */
2732 os_memset(&gd, 0, sizeof(gd));
2733 #ifdef CONFIG_IEEE80211W
2734 os_memset(&igd, 0, sizeof(igd));
2735 #endif /* CONFIG_IEEE80211W */
2737 keylen = wpa_cipher_key_len(sm->group_cipher);
2738 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
2739 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
2740 if (gd.alg == WPA_ALG_NONE) {
2741 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
2745 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
2747 keyinfo = WPA_GET_LE16(buf + 2);
2748 gd.gtk_len = keylen;
2749 if (gd.gtk_len != buf[4]) {
2750 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
2751 gd.gtk_len, buf[4]);
2754 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
2755 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
2756 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
2758 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
2760 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
2761 gd.gtk, gd.gtk_len);
2762 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) {
2763 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
2767 #ifdef CONFIG_IEEE80211W
2768 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
2769 const struct wpa_igtk_kde *igtk;
2771 igtk = (const struct wpa_igtk_kde *) (buf + 2);
2772 if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0)
2774 #endif /* CONFIG_IEEE80211W */
2776 wpa_printf(MSG_DEBUG, "Unknown element id");
2782 #endif /* CONFIG_WNM */