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 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
521 "WPA: Installing PTK to the driver");
523 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
524 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
525 "Suite: NONE - do not use pairwise keys");
529 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
530 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
531 "WPA: Unsupported pairwise cipher %d",
532 sm->pairwise_cipher);
536 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
537 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
538 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
540 if (sm->proto == WPA_PROTO_RSN) {
543 key_rsc = key->key_rsc;
544 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
547 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
548 (u8 *) sm->ptk.tk1, keylen) < 0) {
549 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
550 "WPA: Failed to set PTK to the "
551 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
552 alg, keylen, MAC2STR(sm->bssid));
556 if (sm->wpa_ptk_rekey) {
557 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
558 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
566 static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
568 int keylen, int maxkeylen,
574 *alg = wpa_cipher_to_alg(group_cipher);
575 if (*alg == WPA_ALG_NONE) {
576 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
577 "WPA: Unsupported Group Cipher %d",
581 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
583 klen = wpa_cipher_key_len(group_cipher);
584 if (keylen != klen || maxkeylen < klen) {
585 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
586 "WPA: Unsupported %s Group Cipher key length %d (%d)",
587 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
594 struct wpa_gtk_data {
596 int tx, key_rsc_len, keyidx;
602 static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
603 const struct wpa_gtk_data *gd,
606 const u8 *_gtk = gd->gtk;
609 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
610 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
611 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
612 gd->keyidx, gd->tx, gd->gtk_len);
613 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
614 if (sm->group_cipher == WPA_CIPHER_TKIP) {
615 /* Swap Tx/Rx keys for Michael MIC */
616 os_memcpy(gtk_buf, gd->gtk, 16);
617 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
618 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
621 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
622 if (wpa_sm_set_key(sm, gd->alg, NULL,
623 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
624 _gtk, gd->gtk_len) < 0) {
625 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
626 "WPA: Failed to set GTK to the driver "
630 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
631 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
632 _gtk, gd->gtk_len) < 0) {
633 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
634 "WPA: Failed to set GTK to "
635 "the driver (alg=%d keylen=%d keyidx=%d)",
636 gd->alg, gd->gtk_len, gd->keyidx);
644 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
647 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
648 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
649 * seemed to set this bit (incorrectly, since Tx is only when
650 * doing Group Key only APs) and without this workaround, the
651 * data connection does not work because wpa_supplicant
652 * configured non-zero keyidx to be used for unicast. */
653 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
654 "WPA: Tx bit set for GTK, but pairwise "
655 "keys are used - ignore Tx bit");
662 static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
663 const struct wpa_eapol_key *key,
664 const u8 *gtk, size_t gtk_len,
667 #ifndef CONFIG_NO_WPA2
668 struct wpa_gtk_data gd;
671 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
673 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
674 * Reserved [bits 0-7]
678 os_memset(&gd, 0, sizeof(gd));
679 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
682 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
685 gd.keyidx = gtk[0] & 0x3;
686 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
687 !!(gtk[0] & BIT(2)));
691 os_memcpy(gd.gtk, gtk, gtk_len);
692 gd.gtk_len = gtk_len;
694 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
696 &gd.key_rsc_len, &gd.alg) ||
697 wpa_supplicant_install_gtk(sm, &gd, key->key_rsc)) {
698 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
699 "RSN: Failed to install GTK");
703 wpa_supplicant_key_neg_complete(sm, sm->bssid,
704 key_info & WPA_KEY_INFO_SECURE);
706 #else /* CONFIG_NO_WPA2 */
708 #endif /* CONFIG_NO_WPA2 */
712 static int ieee80211w_set_keys(struct wpa_sm *sm,
713 struct wpa_eapol_ie_parse *ie)
715 #ifdef CONFIG_IEEE80211W
716 if (sm->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC)
720 const struct wpa_igtk_kde *igtk;
722 if (ie->igtk_len != sizeof(*igtk))
724 igtk = (const struct wpa_igtk_kde *) ie->igtk;
725 keyidx = WPA_GET_LE16(igtk->keyid);
726 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: IGTK keyid %d "
727 "pn %02x%02x%02x%02x%02x%02x",
728 keyidx, MAC2STR(igtk->pn));
729 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK",
730 igtk->igtk, WPA_IGTK_LEN);
732 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
733 "WPA: Invalid IGTK KeyID %d", keyidx);
736 if (wpa_sm_set_key(sm, WPA_ALG_IGTK, broadcast_ether_addr,
737 keyidx, 0, igtk->pn, sizeof(igtk->pn),
738 igtk->igtk, WPA_IGTK_LEN) < 0) {
739 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
740 "WPA: Failed to configure IGTK to the driver");
746 #else /* CONFIG_IEEE80211W */
748 #endif /* CONFIG_IEEE80211W */
752 static void wpa_report_ie_mismatch(struct wpa_sm *sm,
753 const char *reason, const u8 *src_addr,
754 const u8 *wpa_ie, size_t wpa_ie_len,
755 const u8 *rsn_ie, size_t rsn_ie_len)
757 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
758 reason, MAC2STR(src_addr));
761 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
762 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
765 if (!sm->ap_wpa_ie) {
766 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
767 "WPA: No WPA IE in Beacon/ProbeResp");
769 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
774 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
775 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
778 if (!sm->ap_rsn_ie) {
779 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
780 "WPA: No RSN IE in Beacon/ProbeResp");
782 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
786 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
790 #ifdef CONFIG_IEEE80211R
792 static int ft_validate_mdie(struct wpa_sm *sm,
793 const unsigned char *src_addr,
794 struct wpa_eapol_ie_parse *ie,
795 const u8 *assoc_resp_mdie)
797 struct rsn_mdie *mdie;
799 mdie = (struct rsn_mdie *) (ie->mdie + 2);
800 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
801 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
802 MOBILITY_DOMAIN_ID_LEN) != 0) {
803 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
804 "not match with the current mobility domain");
808 if (assoc_resp_mdie &&
809 (assoc_resp_mdie[1] != ie->mdie[1] ||
810 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
811 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
812 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
813 ie->mdie, 2 + ie->mdie[1]);
814 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
815 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
823 static int ft_validate_ftie(struct wpa_sm *sm,
824 const unsigned char *src_addr,
825 struct wpa_eapol_ie_parse *ie,
826 const u8 *assoc_resp_ftie)
828 if (ie->ftie == NULL) {
829 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
830 "FT: No FTIE in EAPOL-Key msg 3/4");
834 if (assoc_resp_ftie == NULL)
837 if (assoc_resp_ftie[1] != ie->ftie[1] ||
838 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
839 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
840 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
841 ie->ftie, 2 + ie->ftie[1]);
842 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
843 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
851 static int ft_validate_rsnie(struct wpa_sm *sm,
852 const unsigned char *src_addr,
853 struct wpa_eapol_ie_parse *ie)
855 struct wpa_ie_data rsn;
861 * Verify that PMKR1Name from EAPOL-Key message 3/4
862 * matches with the value we derived.
864 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
865 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
866 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
867 "FT 4-way handshake message 3/4");
871 if (os_memcmp(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0) {
872 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
873 "FT: PMKR1Name mismatch in "
874 "FT 4-way handshake message 3/4");
875 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
876 rsn.pmkid, WPA_PMK_NAME_LEN);
877 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
878 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
886 static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
887 const unsigned char *src_addr,
888 struct wpa_eapol_ie_parse *ie)
890 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
892 if (sm->assoc_resp_ies) {
893 pos = sm->assoc_resp_ies;
894 end = pos + sm->assoc_resp_ies_len;
895 while (pos + 2 < end) {
896 if (pos + 2 + pos[1] > end)
899 case WLAN_EID_MOBILITY_DOMAIN:
902 case WLAN_EID_FAST_BSS_TRANSITION:
910 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
911 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
912 ft_validate_rsnie(sm, src_addr, ie) < 0)
918 #endif /* CONFIG_IEEE80211R */
921 static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
922 const unsigned char *src_addr,
923 struct wpa_eapol_ie_parse *ie)
925 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
926 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
927 "WPA: No WPA/RSN IE for this AP known. "
928 "Trying to get from scan results");
929 if (wpa_sm_get_beacon_ie(sm) < 0) {
930 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
931 "WPA: Could not find AP from "
934 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
935 "WPA: Found the current AP from "
936 "updated scan results");
940 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
941 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
942 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
943 "with IE in Beacon/ProbeResp (no IE?)",
944 src_addr, ie->wpa_ie, ie->wpa_ie_len,
945 ie->rsn_ie, ie->rsn_ie_len);
949 if ((ie->wpa_ie && sm->ap_wpa_ie &&
950 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
951 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
952 (ie->rsn_ie && sm->ap_rsn_ie &&
953 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
954 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
955 ie->rsn_ie, ie->rsn_ie_len))) {
956 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
957 "with IE in Beacon/ProbeResp",
958 src_addr, ie->wpa_ie, ie->wpa_ie_len,
959 ie->rsn_ie, ie->rsn_ie_len);
963 if (sm->proto == WPA_PROTO_WPA &&
964 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
965 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
966 "detected - RSN was enabled and RSN IE "
967 "was in msg 3/4, but not in "
969 src_addr, ie->wpa_ie, ie->wpa_ie_len,
970 ie->rsn_ie, ie->rsn_ie_len);
974 #ifdef CONFIG_IEEE80211R
975 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
976 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
978 #endif /* CONFIG_IEEE80211R */
985 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
986 * @sm: Pointer to WPA state machine data from wpa_sm_init()
987 * @dst: Destination address for the frame
988 * @key: Pointer to the EAPOL-Key frame header
989 * @ver: Version bits from EAPOL-Key Key Info
990 * @key_info: Key Info
991 * @kde: KDEs to include the EAPOL-Key frame
992 * @kde_len: Length of KDEs
993 * @ptk: PTK to use for keyed hash and encryption
994 * Returns: 0 on success, -1 on failure
996 int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
997 const struct wpa_eapol_key *key,
998 u16 ver, u16 key_info,
999 const u8 *kde, size_t kde_len,
1000 struct wpa_ptk *ptk)
1003 struct wpa_eapol_key *reply;
1007 wpa_hexdump(MSG_DEBUG, "WPA: KDE for msg 4/4", kde, kde_len);
1009 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1010 sizeof(*reply) + kde_len,
1011 &rlen, (void *) &reply);
1015 reply->type = sm->proto == WPA_PROTO_RSN ?
1016 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1017 key_info &= WPA_KEY_INFO_SECURE;
1018 key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
1019 WPA_PUT_BE16(reply->key_info, key_info);
1020 if (sm->proto == WPA_PROTO_RSN)
1021 WPA_PUT_BE16(reply->key_length, 0);
1023 os_memcpy(reply->key_length, key->key_length, 2);
1024 os_memcpy(reply->replay_counter, key->replay_counter,
1025 WPA_REPLAY_COUNTER_LEN);
1027 WPA_PUT_BE16(reply->key_data_length, kde_len);
1029 os_memcpy(reply + 1, kde, kde_len);
1031 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
1032 wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
1033 rbuf, rlen, reply->key_mic);
1039 static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1040 const struct wpa_eapol_key *key,
1043 u16 key_info, keylen, len;
1045 struct wpa_eapol_ie_parse ie;
1047 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
1048 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way "
1049 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1051 key_info = WPA_GET_BE16(key->key_info);
1053 pos = (const u8 *) (key + 1);
1054 len = WPA_GET_BE16(key->key_data_length);
1055 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", pos, len);
1056 if (wpa_supplicant_parse_ies(pos, len, &ie) < 0)
1058 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1059 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1060 "WPA: GTK IE in unencrypted key data");
1063 #ifdef CONFIG_IEEE80211W
1064 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1065 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1066 "WPA: IGTK KDE in unencrypted key data");
1070 if (ie.igtk && ie.igtk_len != sizeof(struct wpa_igtk_kde)) {
1071 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1072 "WPA: Invalid IGTK KDE length %lu",
1073 (unsigned long) ie.igtk_len);
1076 #endif /* CONFIG_IEEE80211W */
1078 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1081 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1082 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1083 "WPA: ANonce from message 1 of 4-Way Handshake "
1084 "differs from 3 of 4-Way Handshake - drop packet (src="
1085 MACSTR ")", MAC2STR(sm->bssid));
1089 keylen = WPA_GET_BE16(key->key_length);
1090 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1091 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1092 "WPA: Invalid %s key length %d (src=" MACSTR
1093 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1094 MAC2STR(sm->bssid));
1098 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
1099 NULL, 0, &sm->ptk)) {
1103 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1104 * for the next 4-Way Handshake. If msg 3 is received again, the old
1105 * SNonce will still be used to avoid changing PTK. */
1106 sm->renew_snonce = 1;
1108 if (key_info & WPA_KEY_INFO_INSTALL) {
1109 if (wpa_supplicant_install_ptk(sm, key))
1113 if (key_info & WPA_KEY_INFO_SECURE) {
1114 wpa_sm_mlme_setprotection(
1115 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1116 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1117 eapol_sm_notify_portValid(sm->eapol, TRUE);
1119 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1122 wpa_supplicant_pairwise_gtk(sm, key,
1123 ie.gtk, ie.gtk_len, key_info) < 0) {
1124 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1125 "RSN: Failed to configure GTK");
1129 if (ieee80211w_set_keys(sm, &ie) < 0) {
1130 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1131 "RSN: Failed to configure IGTK");
1135 wpa_sm_set_rekey_offload(sm);
1140 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1144 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1148 struct wpa_gtk_data *gd)
1151 struct wpa_eapol_ie_parse ie;
1153 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
1154 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1156 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1157 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1158 "WPA: GTK IE in unencrypted key data");
1161 if (ie.gtk == NULL) {
1162 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1163 "WPA: No GTK IE in Group Key msg 1/2");
1166 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1168 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1169 gd->gtk_len, maxkeylen,
1170 &gd->key_rsc_len, &gd->alg))
1173 wpa_hexdump(MSG_DEBUG, "RSN: received GTK in group key handshake",
1174 ie.gtk, ie.gtk_len);
1175 gd->keyidx = ie.gtk[0] & 0x3;
1176 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1177 !!(ie.gtk[0] & BIT(2)));
1178 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1179 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1180 "RSN: Too long GTK in GTK IE (len=%lu)",
1181 (unsigned long) ie.gtk_len - 2);
1184 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1186 if (ieee80211w_set_keys(sm, &ie) < 0)
1187 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1188 "RSN: Failed to configure IGTK");
1194 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1195 const struct wpa_eapol_key *key,
1196 size_t keydatalen, int key_info,
1197 size_t extra_len, u16 ver,
1198 struct wpa_gtk_data *gd)
1203 gd->gtk_len = WPA_GET_BE16(key->key_length);
1204 maxkeylen = keydatalen;
1205 if (keydatalen > extra_len) {
1206 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1207 "WPA: Truncated EAPOL-Key packet: "
1208 "key_data_length=%lu > extra_len=%lu",
1209 (unsigned long) keydatalen, (unsigned long) extra_len);
1212 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1213 if (maxkeylen < 8) {
1214 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1215 "WPA: Too short maxkeylen (%lu)",
1216 (unsigned long) maxkeylen);
1222 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1223 gd->gtk_len, maxkeylen,
1224 &gd->key_rsc_len, &gd->alg))
1227 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1228 WPA_KEY_INFO_KEY_INDEX_SHIFT;
1229 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1230 os_memcpy(ek, key->key_iv, 16);
1231 os_memcpy(ek + 16, sm->ptk.kek, 16);
1232 if (keydatalen > sizeof(gd->gtk)) {
1233 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1234 "WPA: RC4 key data too long (%lu)",
1235 (unsigned long) keydatalen);
1238 os_memcpy(gd->gtk, key + 1, keydatalen);
1239 if (rc4_skip(ek, 32, 256, gd->gtk, keydatalen)) {
1240 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1244 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1245 if (keydatalen % 8) {
1246 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1247 "WPA: Unsupported AES-WRAP len %lu",
1248 (unsigned long) keydatalen);
1251 if (maxkeylen > sizeof(gd->gtk)) {
1252 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1253 "WPA: AES-WRAP key data "
1254 "too long (keydatalen=%lu maxkeylen=%lu)",
1255 (unsigned long) keydatalen,
1256 (unsigned long) maxkeylen);
1259 if (aes_unwrap(sm->ptk.kek, maxkeylen / 8,
1260 (const u8 *) (key + 1), gd->gtk)) {
1261 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1262 "WPA: AES unwrap failed - could not decrypt "
1267 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1268 "WPA: Unsupported key_info type %d", ver);
1271 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1272 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1277 static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1278 const struct wpa_eapol_key *key,
1279 int ver, u16 key_info)
1282 struct wpa_eapol_key *reply;
1285 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1286 sizeof(*reply), &rlen, (void *) &reply);
1290 reply->type = sm->proto == WPA_PROTO_RSN ?
1291 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1292 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1293 key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
1294 WPA_PUT_BE16(reply->key_info, key_info);
1295 if (sm->proto == WPA_PROTO_RSN)
1296 WPA_PUT_BE16(reply->key_length, 0);
1298 os_memcpy(reply->key_length, key->key_length, 2);
1299 os_memcpy(reply->replay_counter, key->replay_counter,
1300 WPA_REPLAY_COUNTER_LEN);
1302 WPA_PUT_BE16(reply->key_data_length, 0);
1304 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
1305 wpa_eapol_key_send(sm, sm->ptk.kck, ver, sm->bssid, ETH_P_EAPOL,
1306 rbuf, rlen, reply->key_mic);
1312 static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1313 const unsigned char *src_addr,
1314 const struct wpa_eapol_key *key,
1315 int extra_len, u16 ver)
1317 u16 key_info, keydatalen;
1319 struct wpa_gtk_data gd;
1321 os_memset(&gd, 0, sizeof(gd));
1323 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1324 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1325 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1327 key_info = WPA_GET_BE16(key->key_info);
1328 keydatalen = WPA_GET_BE16(key->key_data_length);
1330 if (sm->proto == WPA_PROTO_RSN) {
1331 ret = wpa_supplicant_process_1_of_2_rsn(sm,
1332 (const u8 *) (key + 1),
1333 keydatalen, key_info,
1336 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, keydatalen,
1337 key_info, extra_len,
1341 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1346 if (wpa_supplicant_install_gtk(sm, &gd, key->key_rsc) ||
1347 wpa_supplicant_send_2_of_2(sm, key, ver, key_info))
1351 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Group rekeying "
1352 "completed with " MACSTR " [GTK=%s]",
1353 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1354 wpa_sm_cancel_auth_timeout(sm);
1355 wpa_sm_set_state(sm, WPA_COMPLETED);
1357 wpa_sm_set_rekey_offload(sm);
1359 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1361 WPA_KEY_INFO_SECURE);
1366 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1370 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
1371 struct wpa_eapol_key *key,
1373 const u8 *buf, size_t len)
1378 os_memcpy(mic, key->key_mic, 16);
1380 os_memset(key->key_mic, 0, 16);
1381 wpa_eapol_key_mic(sm->tptk.kck, ver, buf, len,
1383 if (os_memcmp(mic, key->key_mic, 16) != 0) {
1384 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1385 "WPA: Invalid EAPOL-Key MIC "
1386 "when using TPTK - ignoring TPTK");
1391 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
1395 if (!ok && sm->ptk_set) {
1396 os_memset(key->key_mic, 0, 16);
1397 wpa_eapol_key_mic(sm->ptk.kck, ver, buf, len,
1399 if (os_memcmp(mic, key->key_mic, 16) != 0) {
1400 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1401 "WPA: Invalid EAPOL-Key MIC - "
1409 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1410 "WPA: Could not verify EAPOL-Key MIC - "
1415 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1416 WPA_REPLAY_COUNTER_LEN);
1417 sm->rx_replay_counter_set = 1;
1422 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1423 static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
1424 struct wpa_eapol_key *key, u16 ver)
1426 u16 keydatalen = WPA_GET_BE16(key->key_data_length);
1428 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
1429 (u8 *) (key + 1), keydatalen);
1431 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1432 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1437 /* Decrypt key data here so that this operation does not need
1438 * to be implemented separately for each message type. */
1439 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1441 os_memcpy(ek, key->key_iv, 16);
1442 os_memcpy(ek + 16, sm->ptk.kek, 16);
1443 if (rc4_skip(ek, 32, 256, (u8 *) (key + 1), keydatalen)) {
1444 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1448 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1449 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1451 if (keydatalen % 8) {
1452 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1453 "WPA: Unsupported AES-WRAP len %d",
1457 keydatalen -= 8; /* AES-WRAP adds 8 bytes */
1458 buf = os_malloc(keydatalen);
1460 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1461 "WPA: No memory for AES-UNWRAP buffer");
1464 if (aes_unwrap(sm->ptk.kek, keydatalen / 8,
1465 (u8 *) (key + 1), buf)) {
1467 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1468 "WPA: AES unwrap failed - "
1469 "could not decrypt EAPOL-Key key data");
1472 os_memcpy(key + 1, buf, keydatalen);
1474 WPA_PUT_BE16(key->key_data_length, keydatalen);
1476 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1477 "WPA: Unsupported key_info type %d", ver);
1480 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
1481 (u8 *) (key + 1), keydatalen);
1487 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1488 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1490 void wpa_sm_aborted_cached(struct wpa_sm *sm)
1492 if (sm && sm->cur_pmksa) {
1493 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1494 "RSN: Cancelling PMKSA caching attempt");
1495 sm->cur_pmksa = NULL;
1500 static void wpa_eapol_key_dump(struct wpa_sm *sm,
1501 const struct wpa_eapol_key *key)
1503 #ifndef CONFIG_NO_STDOUT_DEBUG
1504 u16 key_info = WPA_GET_BE16(key->key_info);
1506 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1507 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1508 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
1509 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1510 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1511 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1512 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1513 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1514 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1515 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1516 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1517 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1518 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1519 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1520 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1521 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1522 " key_length=%u key_data_length=%u",
1523 WPA_GET_BE16(key->key_length),
1524 WPA_GET_BE16(key->key_data_length));
1525 wpa_hexdump(MSG_DEBUG, " replay_counter",
1526 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1527 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1528 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1529 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1530 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
1531 wpa_hexdump(MSG_DEBUG, " key_mic", key->key_mic, 16);
1532 #endif /* CONFIG_NO_STDOUT_DEBUG */
1537 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1538 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1539 * @src_addr: Source MAC address of the EAPOL packet
1540 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1541 * @len: Length of the EAPOL frame
1542 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1544 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1545 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1546 * only processing WPA and WPA2 EAPOL-Key frames.
1548 * The received EAPOL-Key packets are validated and valid packets are replied
1549 * to. In addition, key material (PTK, GTK) is configured at the end of a
1550 * successful key handshake.
1552 int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1553 const u8 *buf, size_t len)
1555 size_t plen, data_len, extra_len;
1556 struct ieee802_1x_hdr *hdr;
1557 struct wpa_eapol_key *key;
1561 struct wpa_peerkey *peerkey = NULL;
1563 #ifdef CONFIG_IEEE80211R
1564 sm->ft_completed = 0;
1565 #endif /* CONFIG_IEEE80211R */
1567 if (len < sizeof(*hdr) + sizeof(*key)) {
1568 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1569 "WPA: EAPOL frame too short to be a WPA "
1570 "EAPOL-Key (len %lu, expecting at least %lu)",
1571 (unsigned long) len,
1572 (unsigned long) sizeof(*hdr) + sizeof(*key));
1576 tmp = os_malloc(len);
1579 os_memcpy(tmp, buf, len);
1581 hdr = (struct ieee802_1x_hdr *) tmp;
1582 key = (struct wpa_eapol_key *) (hdr + 1);
1583 plen = be_to_host16(hdr->length);
1584 data_len = plen + sizeof(*hdr);
1585 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1586 "IEEE 802.1X RX: version=%d type=%d length=%lu",
1587 hdr->version, hdr->type, (unsigned long) plen);
1589 if (hdr->version < EAPOL_VERSION) {
1590 /* TODO: backwards compatibility */
1592 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
1593 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1594 "WPA: EAPOL frame (type %u) discarded, "
1595 "not a Key frame", hdr->type);
1599 if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
1600 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1601 "WPA: EAPOL frame payload size %lu "
1602 "invalid (frame size %lu)",
1603 (unsigned long) plen, (unsigned long) len);
1608 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
1610 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1611 "WPA: EAPOL-Key type (%d) unknown, discarded",
1616 wpa_eapol_key_dump(sm, key);
1618 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
1619 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", tmp, len);
1620 if (data_len < len) {
1621 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1622 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
1623 (unsigned long) len - data_len);
1625 key_info = WPA_GET_BE16(key->key_info);
1626 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1627 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
1628 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
1629 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1630 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
1631 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1632 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1633 "WPA: Unsupported EAPOL-Key descriptor version %d",
1638 #ifdef CONFIG_IEEE80211R
1639 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
1640 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
1641 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1642 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1643 "FT: AP did not use AES-128-CMAC");
1647 #endif /* CONFIG_IEEE80211R */
1648 #ifdef CONFIG_IEEE80211W
1649 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
1650 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1651 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1652 "WPA: AP did not use the "
1653 "negotiated AES-128-CMAC");
1657 #endif /* CONFIG_IEEE80211W */
1658 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
1659 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1660 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1661 "WPA: CCMP is used, but EAPOL-Key "
1662 "descriptor version (%d) is not 2", ver);
1663 if (sm->group_cipher != WPA_CIPHER_CCMP &&
1664 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1665 /* Earlier versions of IEEE 802.11i did not explicitly
1666 * require version 2 descriptor for all EAPOL-Key
1667 * packets, so allow group keys to use version 1 if
1668 * CCMP is not used for them. */
1669 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1670 "WPA: Backwards compatibility: allow invalid "
1671 "version for non-CCMP group keys");
1675 if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
1676 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1677 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1678 "WPA: GCMP is used, but EAPOL-Key "
1679 "descriptor version (%d) is not 2", ver);
1683 #ifdef CONFIG_PEERKEY
1684 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
1685 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
1689 if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
1690 if (!peerkey->initiator && peerkey->replay_counter_set &&
1691 os_memcmp(key->replay_counter, peerkey->replay_counter,
1692 WPA_REPLAY_COUNTER_LEN) <= 0) {
1693 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1694 "RSN: EAPOL-Key Replay Counter did not "
1695 "increase (STK) - dropping packet");
1697 } else if (peerkey->initiator) {
1698 u8 _tmp[WPA_REPLAY_COUNTER_LEN];
1699 os_memcpy(_tmp, key->replay_counter,
1700 WPA_REPLAY_COUNTER_LEN);
1701 inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
1702 if (os_memcmp(_tmp, peerkey->replay_counter,
1703 WPA_REPLAY_COUNTER_LEN) != 0) {
1704 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1705 "RSN: EAPOL-Key Replay "
1706 "Counter did not match (STK) - "
1713 if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
1714 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1715 "RSN: Ack bit in key_info from STK peer");
1718 #endif /* CONFIG_PEERKEY */
1720 if (!peerkey && sm->rx_replay_counter_set &&
1721 os_memcmp(key->replay_counter, sm->rx_replay_counter,
1722 WPA_REPLAY_COUNTER_LEN) <= 0) {
1723 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1724 "WPA: EAPOL-Key Replay Counter did not increase - "
1729 if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
1730 #ifdef CONFIG_PEERKEY
1731 && (peerkey == NULL || !peerkey->initiator)
1732 #endif /* CONFIG_PEERKEY */
1734 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1735 "WPA: No Ack bit in key_info");
1739 if (key_info & WPA_KEY_INFO_REQUEST) {
1740 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1741 "WPA: EAPOL-Key with Request bit - dropped");
1745 if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
1746 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
1749 #ifdef CONFIG_PEERKEY
1750 if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
1751 peerkey_verify_eapol_key_mic(sm, peerkey, key, ver, tmp, data_len))
1753 #endif /* CONFIG_PEERKEY */
1755 extra_len = data_len - sizeof(*hdr) - sizeof(*key);
1757 if (WPA_GET_BE16(key->key_data_length) > extra_len) {
1758 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
1759 "frame - key_data overflow (%d > %lu)",
1760 WPA_GET_BE16(key->key_data_length),
1761 (unsigned long) extra_len);
1764 extra_len = WPA_GET_BE16(key->key_data_length);
1766 if (sm->proto == WPA_PROTO_RSN &&
1767 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1768 if (wpa_supplicant_decrypt_key_data(sm, key, ver))
1770 extra_len = WPA_GET_BE16(key->key_data_length);
1773 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1774 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
1775 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1776 "WPA: Ignored EAPOL-Key (Pairwise) with "
1777 "non-zero key index");
1781 /* PeerKey 4-Way Handshake */
1782 peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver);
1783 } else if (key_info & WPA_KEY_INFO_MIC) {
1784 /* 3/4 4-Way Handshake */
1785 wpa_supplicant_process_3_of_4(sm, key, ver);
1787 /* 1/4 4-Way Handshake */
1788 wpa_supplicant_process_1_of_4(sm, src_addr, key,
1791 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
1792 /* PeerKey SMK Handshake */
1793 peerkey_rx_eapol_smk(sm, src_addr, key, extra_len, key_info,
1796 if (key_info & WPA_KEY_INFO_MIC) {
1797 /* 1/2 Group Key Handshake */
1798 wpa_supplicant_process_1_of_2(sm, src_addr, key,
1801 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1802 "WPA: EAPOL-Key (Group) without Mic bit - "
1815 #ifdef CONFIG_CTRL_IFACE
1816 static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
1818 switch (sm->key_mgmt) {
1819 case WPA_KEY_MGMT_IEEE8021X:
1820 return (sm->proto == WPA_PROTO_RSN ?
1821 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
1822 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
1823 case WPA_KEY_MGMT_PSK:
1824 return (sm->proto == WPA_PROTO_RSN ?
1825 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
1826 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
1827 #ifdef CONFIG_IEEE80211R
1828 case WPA_KEY_MGMT_FT_IEEE8021X:
1829 return RSN_AUTH_KEY_MGMT_FT_802_1X;
1830 case WPA_KEY_MGMT_FT_PSK:
1831 return RSN_AUTH_KEY_MGMT_FT_PSK;
1832 #endif /* CONFIG_IEEE80211R */
1833 #ifdef CONFIG_IEEE80211W
1834 case WPA_KEY_MGMT_IEEE8021X_SHA256:
1835 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
1836 case WPA_KEY_MGMT_PSK_SHA256:
1837 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
1838 #endif /* CONFIG_IEEE80211W */
1839 case WPA_KEY_MGMT_CCKM:
1840 return (sm->proto == WPA_PROTO_RSN ?
1841 RSN_AUTH_KEY_MGMT_CCKM:
1842 WPA_AUTH_KEY_MGMT_CCKM);
1843 case WPA_KEY_MGMT_WPA_NONE:
1844 return WPA_AUTH_KEY_MGMT_NONE;
1851 #define RSN_SUITE "%02x-%02x-%02x-%d"
1852 #define RSN_SUITE_ARG(s) \
1853 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
1856 * wpa_sm_get_mib - Dump text list of MIB entries
1857 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1858 * @buf: Buffer for the list
1859 * @buflen: Length of the buffer
1860 * Returns: Number of bytes written to buffer
1862 * This function is used fetch dot11 MIB variables.
1864 int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
1866 char pmkid_txt[PMKID_LEN * 2 + 1];
1870 if (sm->cur_pmksa) {
1871 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
1872 sm->cur_pmksa->pmkid, PMKID_LEN);
1874 pmkid_txt[0] = '\0';
1876 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
1877 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
1878 sm->proto == WPA_PROTO_RSN)
1883 ret = os_snprintf(buf, buflen,
1884 "dot11RSNAOptionImplemented=TRUE\n"
1885 "dot11RSNAPreauthenticationImplemented=TRUE\n"
1886 "dot11RSNAEnabled=%s\n"
1887 "dot11RSNAPreauthenticationEnabled=%s\n"
1888 "dot11RSNAConfigVersion=%d\n"
1889 "dot11RSNAConfigPairwiseKeysSupported=5\n"
1890 "dot11RSNAConfigGroupCipherSize=%d\n"
1891 "dot11RSNAConfigPMKLifetime=%d\n"
1892 "dot11RSNAConfigPMKReauthThreshold=%d\n"
1893 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
1894 "dot11RSNAConfigSATimeout=%d\n",
1895 rsna ? "TRUE" : "FALSE",
1896 rsna ? "TRUE" : "FALSE",
1898 wpa_cipher_key_len(sm->group_cipher) * 8,
1899 sm->dot11RSNAConfigPMKLifetime,
1900 sm->dot11RSNAConfigPMKReauthThreshold,
1901 sm->dot11RSNAConfigSATimeout);
1902 if (ret < 0 || (size_t) ret >= buflen)
1907 buf + len, buflen - len,
1908 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
1909 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
1910 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
1911 "dot11RSNAPMKIDUsed=%s\n"
1912 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
1913 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
1914 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
1915 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
1916 "dot11RSNA4WayHandshakeFailures=%u\n",
1917 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
1918 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
1919 sm->pairwise_cipher)),
1920 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
1923 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
1924 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
1925 sm->pairwise_cipher)),
1926 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
1928 sm->dot11RSNA4WayHandshakeFailures);
1929 if (ret >= 0 && (size_t) ret < buflen)
1934 #endif /* CONFIG_CTRL_IFACE */
1937 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
1938 void *ctx, enum pmksa_free_reason reason)
1940 struct wpa_sm *sm = ctx;
1943 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
1944 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
1946 if (sm->cur_pmksa == entry) {
1947 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1948 "RSN: %s current PMKSA entry",
1949 reason == PMKSA_REPLACE ? "replaced" : "removed");
1950 pmksa_cache_clear_current(sm);
1953 * If an entry is simply being replaced, there's no need to
1954 * deauthenticate because it will be immediately re-added.
1955 * This happens when EAP authentication is completed again
1956 * (reauth or failed PMKSA caching attempt).
1958 if (reason != PMKSA_REPLACE)
1962 if (reason == PMKSA_EXPIRE &&
1963 (sm->pmk_len == entry->pmk_len &&
1964 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
1965 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1966 "RSN: deauthenticating due to expired PMK");
1967 pmksa_cache_clear_current(sm);
1972 os_memset(sm->pmk, 0, sizeof(sm->pmk));
1973 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1979 * wpa_sm_init - Initialize WPA state machine
1980 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
1981 * Returns: Pointer to the allocated WPA state machine data
1983 * This function is used to allocate a new WPA state machine and the returned
1984 * value is passed to all WPA state machine calls.
1986 struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
1990 sm = os_zalloc(sizeof(*sm));
1993 dl_list_init(&sm->pmksa_candidates);
1994 sm->renew_snonce = 1;
1997 sm->dot11RSNAConfigPMKLifetime = 43200;
1998 sm->dot11RSNAConfigPMKReauthThreshold = 70;
1999 sm->dot11RSNAConfigSATimeout = 60;
2001 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2002 if (sm->pmksa == NULL) {
2003 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2004 "RSN: PMKSA cache initialization failed");
2014 * wpa_sm_deinit - Deinitialize WPA state machine
2015 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2017 void wpa_sm_deinit(struct wpa_sm *sm)
2021 pmksa_cache_deinit(sm->pmksa);
2022 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2023 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2024 os_free(sm->assoc_wpa_ie);
2025 os_free(sm->ap_wpa_ie);
2026 os_free(sm->ap_rsn_ie);
2029 #ifdef CONFIG_IEEE80211R
2030 os_free(sm->assoc_resp_ies);
2031 #endif /* CONFIG_IEEE80211R */
2037 * wpa_sm_notify_assoc - Notify WPA state machine about association
2038 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2039 * @bssid: The BSSID of the new association
2041 * This function is called to let WPA state machine know that the connection
2044 void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2051 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2052 "WPA: Association event - clear replay counter");
2053 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2054 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2055 sm->rx_replay_counter_set = 0;
2056 sm->renew_snonce = 1;
2057 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2058 rsn_preauth_deinit(sm);
2060 #ifdef CONFIG_IEEE80211R
2061 if (wpa_ft_is_completed(sm)) {
2063 * Clear portValid to kick EAPOL state machine to re-enter
2064 * AUTHENTICATED state to get the EAPOL port Authorized.
2066 eapol_sm_notify_portValid(sm->eapol, FALSE);
2067 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2069 /* Prepare for the next transition */
2070 wpa_ft_prepare_auth_request(sm, NULL);
2074 #endif /* CONFIG_IEEE80211R */
2078 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2079 * this is not part of a Fast BSS Transition.
2081 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
2088 #endif /* CONFIG_TDLS */
2093 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2094 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2096 * This function is called to let WPA state machine know that the connection
2097 * was lost. This will abort any existing pre-authentication session.
2099 void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2101 rsn_preauth_deinit(sm);
2102 pmksa_cache_clear_current(sm);
2103 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2104 sm->dot11RSNA4WayHandshakeFailures++;
2106 wpa_tdls_disassoc(sm);
2107 #endif /* CONFIG_TDLS */
2112 * wpa_sm_set_pmk - Set PMK
2113 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2115 * @pmk_len: The length of the new PMK in bytes
2117 * Configure the PMK for WPA state machine.
2119 void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len)
2124 sm->pmk_len = pmk_len;
2125 os_memcpy(sm->pmk, pmk, pmk_len);
2127 #ifdef CONFIG_IEEE80211R
2128 /* Set XXKey to be PSK for FT key derivation */
2129 sm->xxkey_len = pmk_len;
2130 os_memcpy(sm->xxkey, pmk, pmk_len);
2131 #endif /* CONFIG_IEEE80211R */
2136 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2137 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2139 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2142 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2147 if (sm->cur_pmksa) {
2148 sm->pmk_len = sm->cur_pmksa->pmk_len;
2149 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2151 sm->pmk_len = PMK_LEN;
2152 os_memset(sm->pmk, 0, PMK_LEN);
2158 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2159 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2160 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2162 void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2165 sm->fast_reauth = fast_reauth;
2170 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2171 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2172 * @scard_ctx: Context pointer for smartcard related callback functions
2174 void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2178 sm->scard_ctx = scard_ctx;
2179 if (sm->preauth_eapol)
2180 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2185 * wpa_sm_set_config - Notification of current configration change
2186 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2187 * @config: Pointer to current network configuration
2189 * Notify WPA state machine that configuration has changed. config will be
2190 * stored as a backpointer to network configuration. This can be %NULL to clear
2191 * the stored pointed.
2193 void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2199 sm->network_ctx = config->network_ctx;
2200 sm->peerkey_enabled = config->peerkey_enabled;
2201 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2202 sm->proactive_key_caching = config->proactive_key_caching;
2203 sm->eap_workaround = config->eap_workaround;
2204 sm->eap_conf_ctx = config->eap_conf_ctx;
2206 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2207 sm->ssid_len = config->ssid_len;
2210 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
2212 sm->network_ctx = NULL;
2213 sm->peerkey_enabled = 0;
2214 sm->allowed_pairwise_cipher = 0;
2215 sm->proactive_key_caching = 0;
2216 sm->eap_workaround = 0;
2217 sm->eap_conf_ctx = NULL;
2219 sm->wpa_ptk_rekey = 0;
2225 * wpa_sm_set_own_addr - Set own MAC address
2226 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2227 * @addr: Own MAC address
2229 void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2232 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2237 * wpa_sm_set_ifname - Set network interface name
2238 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2239 * @ifname: Interface name
2240 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2242 void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2243 const char *bridge_ifname)
2246 sm->ifname = ifname;
2247 sm->bridge_ifname = bridge_ifname;
2253 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2254 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2255 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2257 void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2265 * wpa_sm_set_param - Set WPA state machine parameters
2266 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2267 * @param: Parameter field
2268 * @value: Parameter value
2269 * Returns: 0 on success, -1 on failure
2271 int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2280 case RSNA_PMK_LIFETIME:
2282 sm->dot11RSNAConfigPMKLifetime = value;
2286 case RSNA_PMK_REAUTH_THRESHOLD:
2287 if (value > 0 && value <= 100)
2288 sm->dot11RSNAConfigPMKReauthThreshold = value;
2292 case RSNA_SA_TIMEOUT:
2294 sm->dot11RSNAConfigSATimeout = value;
2298 case WPA_PARAM_PROTO:
2301 case WPA_PARAM_PAIRWISE:
2302 sm->pairwise_cipher = value;
2304 case WPA_PARAM_GROUP:
2305 sm->group_cipher = value;
2307 case WPA_PARAM_KEY_MGMT:
2308 sm->key_mgmt = value;
2310 #ifdef CONFIG_IEEE80211W
2311 case WPA_PARAM_MGMT_GROUP:
2312 sm->mgmt_group_cipher = value;
2314 #endif /* CONFIG_IEEE80211W */
2315 case WPA_PARAM_RSN_ENABLED:
2316 sm->rsn_enabled = value;
2330 * wpa_sm_get_param - Get WPA state machine parameters
2331 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2332 * @param: Parameter field
2333 * Returns: Parameter value
2335 unsigned int wpa_sm_get_param(struct wpa_sm *sm, enum wpa_sm_conf_params param)
2341 case RSNA_PMK_LIFETIME:
2342 return sm->dot11RSNAConfigPMKLifetime;
2343 case RSNA_PMK_REAUTH_THRESHOLD:
2344 return sm->dot11RSNAConfigPMKReauthThreshold;
2345 case RSNA_SA_TIMEOUT:
2346 return sm->dot11RSNAConfigSATimeout;
2347 case WPA_PARAM_PROTO:
2349 case WPA_PARAM_PAIRWISE:
2350 return sm->pairwise_cipher;
2351 case WPA_PARAM_GROUP:
2352 return sm->group_cipher;
2353 case WPA_PARAM_KEY_MGMT:
2354 return sm->key_mgmt;
2355 #ifdef CONFIG_IEEE80211W
2356 case WPA_PARAM_MGMT_GROUP:
2357 return sm->mgmt_group_cipher;
2358 #endif /* CONFIG_IEEE80211W */
2359 case WPA_PARAM_RSN_ENABLED:
2360 return sm->rsn_enabled;
2368 * wpa_sm_get_status - Get WPA state machine
2369 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2370 * @buf: Buffer for status information
2371 * @buflen: Maximum buffer length
2372 * @verbose: Whether to include verbose status information
2373 * Returns: Number of bytes written to buf.
2375 * Query WPA state machine for status information. This function fills in
2376 * a text area with current status information. If the buffer (buf) is not
2377 * large enough, status information will be truncated to fit the buffer.
2379 int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2382 char *pos = buf, *end = buf + buflen;
2385 ret = os_snprintf(pos, end - pos,
2386 "pairwise_cipher=%s\n"
2389 wpa_cipher_txt(sm->pairwise_cipher),
2390 wpa_cipher_txt(sm->group_cipher),
2391 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
2392 if (ret < 0 || ret >= end - pos)
2396 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
2397 struct wpa_ie_data rsn;
2398 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
2400 rsn.capabilities & (WPA_CAPABILITY_MFPR |
2401 WPA_CAPABILITY_MFPC)) {
2402 ret = os_snprintf(pos, end - pos, "pmf=%d\n",
2404 WPA_CAPABILITY_MFPR) ? 2 : 1);
2405 if (ret < 0 || ret >= end - pos)
2416 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2417 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2418 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2419 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2420 * Returns: 0 on success, -1 on failure
2422 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2430 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2435 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2436 wpa_ie, *wpa_ie_len);
2438 if (sm->assoc_wpa_ie == NULL) {
2440 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2441 * the correct version of the IE even if PMKSA caching is
2442 * aborted (which would remove PMKID from IE generation).
2444 sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2445 if (sm->assoc_wpa_ie == NULL)
2448 os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2449 sm->assoc_wpa_ie_len = *wpa_ie_len;
2457 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2458 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2459 * @ie: Pointer to IE data (starting from id)
2461 * Returns: 0 on success, -1 on failure
2463 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2464 * Request frame. The IE will be used to override the default value generated
2465 * with wpa_sm_set_assoc_wpa_ie_default().
2467 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2472 os_free(sm->assoc_wpa_ie);
2473 if (ie == NULL || len == 0) {
2474 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2475 "WPA: clearing own WPA/RSN IE");
2476 sm->assoc_wpa_ie = NULL;
2477 sm->assoc_wpa_ie_len = 0;
2479 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2480 sm->assoc_wpa_ie = os_malloc(len);
2481 if (sm->assoc_wpa_ie == NULL)
2484 os_memcpy(sm->assoc_wpa_ie, ie, len);
2485 sm->assoc_wpa_ie_len = len;
2493 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2494 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2495 * @ie: Pointer to IE data (starting from id)
2497 * Returns: 0 on success, -1 on failure
2499 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2502 int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2507 os_free(sm->ap_wpa_ie);
2508 if (ie == NULL || len == 0) {
2509 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2510 "WPA: clearing AP WPA IE");
2511 sm->ap_wpa_ie = NULL;
2512 sm->ap_wpa_ie_len = 0;
2514 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2515 sm->ap_wpa_ie = os_malloc(len);
2516 if (sm->ap_wpa_ie == NULL)
2519 os_memcpy(sm->ap_wpa_ie, ie, len);
2520 sm->ap_wpa_ie_len = len;
2528 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2529 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2530 * @ie: Pointer to IE data (starting from id)
2532 * Returns: 0 on success, -1 on failure
2534 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2537 int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2542 os_free(sm->ap_rsn_ie);
2543 if (ie == NULL || len == 0) {
2544 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2545 "WPA: clearing AP RSN IE");
2546 sm->ap_rsn_ie = NULL;
2547 sm->ap_rsn_ie_len = 0;
2549 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2550 sm->ap_rsn_ie = os_malloc(len);
2551 if (sm->ap_rsn_ie == NULL)
2554 os_memcpy(sm->ap_rsn_ie, ie, len);
2555 sm->ap_rsn_ie_len = len;
2563 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2564 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2565 * @data: Pointer to data area for parsing results
2566 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2568 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2569 * parsed data into data.
2571 int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2576 if (sm->assoc_wpa_ie == NULL) {
2577 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2578 "WPA: No WPA/RSN IE available from association info");
2581 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
2587 int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
2589 #ifndef CONFIG_NO_WPA2
2590 return pmksa_cache_list(sm->pmksa, buf, len);
2591 #else /* CONFIG_NO_WPA2 */
2593 #endif /* CONFIG_NO_WPA2 */
2597 void wpa_sm_drop_sa(struct wpa_sm *sm)
2599 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
2602 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2603 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2604 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2608 int wpa_sm_has_ptk(struct wpa_sm *sm)
2616 void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
2618 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
2622 void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
2624 #ifndef CONFIG_NO_WPA2
2625 pmksa_cache_flush(sm->pmksa, network_ctx);
2626 #endif /* CONFIG_NO_WPA2 */
2631 int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
2633 struct wpa_gtk_data gd;
2634 #ifdef CONFIG_IEEE80211W
2635 struct wpa_igtk_kde igd;
2637 #endif /* CONFIG_IEEE80211W */
2639 u8 keylen; /* plaintext key len */
2642 os_memset(&gd, 0, sizeof(gd));
2643 #ifdef CONFIG_IEEE80211W
2644 os_memset(&igd, 0, sizeof(igd));
2645 #endif /* CONFIG_IEEE80211W */
2647 keylen = wpa_cipher_key_len(sm->group_cipher);
2648 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
2649 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
2650 if (gd.alg == WPA_ALG_NONE) {
2651 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
2655 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
2657 keyinfo = WPA_GET_LE16(buf + 2);
2658 gd.gtk_len = keylen;
2659 if (gd.gtk_len != buf[4]) {
2660 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
2661 gd.gtk_len, buf[4]);
2664 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
2665 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
2666 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
2668 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
2670 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
2671 gd.gtk, gd.gtk_len);
2672 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc)) {
2673 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
2677 #ifdef CONFIG_IEEE80211W
2678 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
2679 os_memcpy(igd.keyid, buf + 2, 2);
2680 os_memcpy(igd.pn, buf + 4, 6);
2682 keyidx = WPA_GET_LE16(igd.keyid);
2683 os_memcpy(igd.igtk, buf + 10, WPA_IGTK_LEN);
2685 wpa_hexdump_key(MSG_DEBUG, "Install IGTK (WNM SLEEP)",
2686 igd.igtk, WPA_IGTK_LEN);
2687 if (wpa_sm_set_key(sm, WPA_ALG_IGTK, broadcast_ether_addr,
2688 keyidx, 0, igd.pn, sizeof(igd.pn),
2689 igd.igtk, WPA_IGTK_LEN) < 0) {
2690 wpa_printf(MSG_DEBUG, "Failed to install the IGTK in "
2694 #endif /* CONFIG_IEEE80211W */
2696 wpa_printf(MSG_DEBUG, "Unknown element id");
2702 #endif /* CONFIG_WNM */