2 * WPA Supplicant - WPA state machine and EAPOL-Key processing
3 * Copyright (c) 2003-2015, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "crypto/aes_wrap.h"
13 #include "crypto/crypto.h"
14 #include "crypto/random.h"
15 #include "common/ieee802_11_defs.h"
16 #include "eapol_supp/eapol_supp_sm.h"
20 #include "pmksa_cache.h"
27 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
28 * @sm: Pointer to WPA state machine data from wpa_sm_init()
29 * @kck: Key Confirmation Key (KCK, part of PTK)
30 * @kck_len: KCK length in octets
31 * @ver: Version field from Key Info
32 * @dest: Destination address for the frame
33 * @proto: Ethertype (usually ETH_P_EAPOL)
34 * @msg: EAPOL-Key message
35 * @msg_len: Length of message
36 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
38 void wpa_eapol_key_send(struct wpa_sm *sm, const u8 *kck, size_t kck_len,
39 int ver, const u8 *dest, u16 proto,
40 u8 *msg, size_t msg_len, u8 *key_mic)
42 size_t mic_len = wpa_mic_len(sm->key_mgmt);
44 if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
46 * Association event was not yet received; try to fetch
47 * BSSID from the driver.
49 if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
50 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
51 "WPA: Failed to read BSSID for "
52 "EAPOL-Key destination address");
55 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
56 "WPA: Use BSSID (" MACSTR
57 ") as the destination for EAPOL-Key",
62 wpa_eapol_key_mic(kck, kck_len, sm->key_mgmt, ver, msg, msg_len,
64 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
65 "WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC",
69 wpa_hexdump_key(MSG_DEBUG, "WPA: KCK", kck, kck_len);
70 wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC", key_mic, mic_len);
71 wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
72 wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
73 eapol_sm_notify_tx_eapol_key(sm->eapol);
80 * wpa_sm_key_request - Send EAPOL-Key Request
81 * @sm: Pointer to WPA state machine data from wpa_sm_init()
82 * @error: Indicate whether this is an Michael MIC error report
83 * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
85 * Send an EAPOL-Key Request to the current authenticator. This function is
86 * used to request rekeying and it is usually called when a local Michael MIC
87 * failure is detected.
89 void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
91 size_t mic_len, hdrlen, rlen;
92 struct wpa_eapol_key *reply;
93 struct wpa_eapol_key_192 *reply192;
95 u8 bssid[ETH_ALEN], *rbuf, *key_mic;
97 if (sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
98 wpa_key_mgmt_suite_b(sm->key_mgmt))
99 ver = WPA_KEY_INFO_TYPE_AKM_DEFINED;
100 else if (wpa_key_mgmt_ft(sm->key_mgmt) ||
101 wpa_key_mgmt_sha256(sm->key_mgmt))
102 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
103 else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
104 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
106 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
108 if (wpa_sm_get_bssid(sm, bssid) < 0) {
109 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
110 "Failed to read BSSID for EAPOL-Key request");
114 mic_len = wpa_mic_len(sm->key_mgmt);
115 hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
116 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
117 hdrlen, &rlen, (void *) &reply);
120 reply192 = (struct wpa_eapol_key_192 *) reply;
122 reply->type = (sm->proto == WPA_PROTO_RSN ||
123 sm->proto == WPA_PROTO_OSEN) ?
124 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
125 key_info = WPA_KEY_INFO_REQUEST | ver;
127 key_info |= WPA_KEY_INFO_MIC;
129 key_info |= WPA_KEY_INFO_ERROR;
131 key_info |= WPA_KEY_INFO_KEY_TYPE;
132 WPA_PUT_BE16(reply->key_info, key_info);
133 WPA_PUT_BE16(reply->key_length, 0);
134 os_memcpy(reply->replay_counter, sm->request_counter,
135 WPA_REPLAY_COUNTER_LEN);
136 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
139 WPA_PUT_BE16(reply192->key_data_length, 0);
141 WPA_PUT_BE16(reply->key_data_length, 0);
142 if (!(key_info & WPA_KEY_INFO_MIC))
145 key_mic = reply192->key_mic; /* same offset in reply */
147 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
148 "WPA: Sending EAPOL-Key Request (error=%d "
149 "pairwise=%d ptk_set=%d len=%lu)",
150 error, pairwise, sm->ptk_set, (unsigned long) rlen);
151 wpa_eapol_key_send(sm, sm->ptk.kck, sm->ptk.kck_len, ver, bssid,
152 ETH_P_EAPOL, rbuf, rlen, key_mic);
156 static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm *sm)
158 #ifdef CONFIG_IEEE80211R
159 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
160 if (wpa_sm_key_mgmt_set_pmk(sm, sm->xxkey, sm->xxkey_len))
161 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
162 "RSN: Cannot set low order 256 bits of MSK for key management offload");
164 #endif /* CONFIG_IEEE80211R */
165 if (wpa_sm_key_mgmt_set_pmk(sm, sm->pmk, sm->pmk_len))
166 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
167 "RSN: Cannot set PMK for key management offload");
168 #ifdef CONFIG_IEEE80211R
170 #endif /* CONFIG_IEEE80211R */
174 static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
175 const unsigned char *src_addr,
178 int abort_cached = 0;
180 if (pmkid && !sm->cur_pmksa) {
181 /* When using drivers that generate RSN IE, wpa_supplicant may
182 * not have enough time to get the association information
183 * event before receiving this 1/4 message, so try to find a
184 * matching PMKSA cache entry here. */
185 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid,
188 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
189 "RSN: found matching PMKID from PMKSA cache");
191 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
192 "RSN: no matching PMKID found");
197 if (pmkid && sm->cur_pmksa &&
198 os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
199 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
200 wpa_sm_set_pmk_from_pmksa(sm);
201 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
202 sm->pmk, sm->pmk_len);
203 eapol_sm_notify_cached(sm->eapol);
204 #ifdef CONFIG_IEEE80211R
206 #endif /* CONFIG_IEEE80211R */
207 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
210 res = eapol_sm_get_key(sm->eapol, sm->pmk, PMK_LEN);
213 * EAP-LEAP is an exception from other EAP methods: it
214 * uses only 16-byte PMK.
216 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
219 #ifdef CONFIG_IEEE80211R
221 if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
223 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
224 sm->xxkey_len = PMK_LEN;
225 os_memset(buf, 0, sizeof(buf));
227 #endif /* CONFIG_IEEE80211R */
230 struct rsn_pmksa_cache_entry *sa = NULL;
231 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
232 "machines", sm->pmk, pmk_len);
233 sm->pmk_len = pmk_len;
234 wpa_supplicant_key_mgmt_set_pmk(sm);
235 if (sm->proto == WPA_PROTO_RSN &&
236 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
237 !wpa_key_mgmt_ft(sm->key_mgmt)) {
238 sa = pmksa_cache_add(sm->pmksa,
241 src_addr, sm->own_addr,
245 if (!sm->cur_pmksa && pmkid &&
246 pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL))
248 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
249 "RSN: the new PMK matches with the "
252 } else if (sa && !sm->cur_pmksa && pmkid) {
254 * It looks like the authentication server
255 * derived mismatching MSK. This should not
256 * really happen, but bugs happen.. There is not
257 * much we can do here without knowing what
258 * exactly caused the server to misbehave.
260 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO,
261 "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
268 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
269 "WPA: Failed to get master session key from "
270 "EAPOL state machines - key handshake "
273 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
274 "RSN: Cancelled PMKSA caching "
276 sm->cur_pmksa = NULL;
278 } else if (!abort_cached) {
284 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
285 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
286 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
288 /* Send EAPOL-Start to trigger full EAP authentication. */
292 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
293 "RSN: no PMKSA entry found - trigger "
294 "full EAP authentication");
295 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
296 NULL, 0, &buflen, NULL);
298 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
312 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
313 * @sm: Pointer to WPA state machine data from wpa_sm_init()
314 * @dst: Destination address for the frame
315 * @key: Pointer to the EAPOL-Key frame header
316 * @ver: Version bits from EAPOL-Key Key Info
317 * @nonce: Nonce value for the EAPOL-Key frame
318 * @wpa_ie: WPA/RSN IE
319 * @wpa_ie_len: Length of the WPA/RSN IE
320 * @ptk: PTK to use for keyed hash and encryption
321 * Returns: 0 on success, -1 on failure
323 int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
324 const struct wpa_eapol_key *key,
325 int ver, const u8 *nonce,
326 const u8 *wpa_ie, size_t wpa_ie_len,
329 size_t mic_len, hdrlen, rlen;
330 struct wpa_eapol_key *reply;
331 struct wpa_eapol_key_192 *reply192;
333 u8 *rsn_ie_buf = NULL;
335 if (wpa_ie == NULL) {
336 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
337 "cannot generate msg 2/4");
341 #ifdef CONFIG_IEEE80211R
342 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
346 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
347 * FTIE from (Re)Association Response.
349 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
350 sm->assoc_resp_ies_len);
351 if (rsn_ie_buf == NULL)
353 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
354 res = wpa_insert_pmkid(rsn_ie_buf, wpa_ie_len,
362 if (sm->assoc_resp_ies) {
363 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
364 sm->assoc_resp_ies_len);
365 wpa_ie_len += sm->assoc_resp_ies_len;
370 #endif /* CONFIG_IEEE80211R */
372 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
374 mic_len = wpa_mic_len(sm->key_mgmt);
375 hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
376 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
377 NULL, hdrlen + wpa_ie_len,
378 &rlen, (void *) &reply);
383 reply192 = (struct wpa_eapol_key_192 *) reply;
385 reply->type = (sm->proto == WPA_PROTO_RSN ||
386 sm->proto == WPA_PROTO_OSEN) ?
387 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
388 WPA_PUT_BE16(reply->key_info,
389 ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC);
390 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
391 WPA_PUT_BE16(reply->key_length, 0);
393 os_memcpy(reply->key_length, key->key_length, 2);
394 os_memcpy(reply->replay_counter, key->replay_counter,
395 WPA_REPLAY_COUNTER_LEN);
396 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
397 WPA_REPLAY_COUNTER_LEN);
399 key_mic = reply192->key_mic; /* same offset for reply and reply192 */
401 WPA_PUT_BE16(reply192->key_data_length, wpa_ie_len);
402 os_memcpy(reply192 + 1, wpa_ie, wpa_ie_len);
404 WPA_PUT_BE16(reply->key_data_length, wpa_ie_len);
405 os_memcpy(reply + 1, wpa_ie, wpa_ie_len);
409 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
411 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
412 wpa_eapol_key_send(sm, ptk->kck, ptk->kck_len, ver, dst, ETH_P_EAPOL,
413 rbuf, rlen, key_mic);
419 static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
420 const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
422 #ifdef CONFIG_IEEE80211R
423 if (wpa_key_mgmt_ft(sm->key_mgmt))
424 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
425 #endif /* CONFIG_IEEE80211R */
427 return wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
428 sm->own_addr, sm->bssid, sm->snonce,
429 key->key_nonce, ptk, sm->key_mgmt,
430 sm->pairwise_cipher);
434 static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
435 const unsigned char *src_addr,
436 const struct wpa_eapol_key *key,
437 u16 ver, const u8 *key_data,
440 struct wpa_eapol_ie_parse ie;
443 u8 *kde, *kde_buf = NULL;
446 if (wpa_sm_get_network_ctx(sm) == NULL) {
447 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
448 "found (msg 1 of 4)");
452 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
453 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of 4-Way "
454 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
456 os_memset(&ie, 0, sizeof(ie));
458 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
459 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
460 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
461 key_data, key_data_len);
462 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
465 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
466 "Authenticator", ie.pmkid, PMKID_LEN);
470 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
472 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
473 "msg 1/4 - requesting full EAP authentication");
479 if (sm->renew_snonce) {
480 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
481 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
482 "WPA: Failed to get random data for SNonce");
485 sm->renew_snonce = 0;
486 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
487 sm->snonce, WPA_NONCE_LEN);
490 /* Calculate PTK which will be stored as a temporary PTK until it has
491 * been verified when processing message 3/4. */
493 wpa_derive_ptk(sm, src_addr, key, ptk);
494 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
496 /* Supplicant: swap tx/rx Mic keys */
497 os_memcpy(buf, &ptk->tk[16], 8);
498 os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
499 os_memcpy(&ptk->tk[24], buf, 8);
500 os_memset(buf, 0, sizeof(buf));
504 kde = sm->assoc_wpa_ie;
505 kde_len = sm->assoc_wpa_ie_len;
509 kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 1);
512 wpa_printf(MSG_DEBUG, "P2P: Add IP Address Request KDE "
513 "into EAPOL-Key 2/4");
514 os_memcpy(kde_buf, kde, kde_len);
517 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
518 *pos++ = RSN_SELECTOR_LEN + 1;
519 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
520 pos += RSN_SELECTOR_LEN;
525 #endif /* CONFIG_P2P */
527 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
532 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
537 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
541 static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
543 struct wpa_sm *sm = eloop_ctx;
544 rsn_preauth_candidate_process(sm);
548 static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
549 const u8 *addr, int secure)
551 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
552 "WPA: Key negotiation completed with "
553 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
554 wpa_cipher_txt(sm->pairwise_cipher),
555 wpa_cipher_txt(sm->group_cipher));
556 wpa_sm_cancel_auth_timeout(sm);
557 wpa_sm_set_state(sm, WPA_COMPLETED);
560 wpa_sm_mlme_setprotection(
561 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
562 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
563 eapol_sm_notify_portValid(sm->eapol, TRUE);
564 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt))
565 eapol_sm_notify_eap_success(sm->eapol, TRUE);
567 * Start preauthentication after a short wait to avoid a
568 * possible race condition between the data receive and key
569 * configuration after the 4-Way Handshake. This increases the
570 * likelihood of the first preauth EAPOL-Start frame getting to
573 eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
576 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
577 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
578 "RSN: Authenticator accepted "
579 "opportunistic PMKSA entry - marking it valid");
580 sm->cur_pmksa->opportunistic = 0;
583 #ifdef CONFIG_IEEE80211R
584 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
585 /* Prepare for the next transition */
586 wpa_ft_prepare_auth_request(sm, NULL);
588 #endif /* CONFIG_IEEE80211R */
592 static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
594 struct wpa_sm *sm = eloop_ctx;
595 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
596 wpa_sm_key_request(sm, 0, 1);
600 static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
601 const struct wpa_eapol_key *key)
606 u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
608 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
609 "WPA: Installing PTK to the driver");
611 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
612 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
613 "Suite: NONE - do not use pairwise keys");
617 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
618 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
619 "WPA: Unsupported pairwise cipher %d",
620 sm->pairwise_cipher);
624 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
625 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
626 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
628 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
631 key_rsc = key->key_rsc;
632 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
635 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
636 sm->ptk.tk, keylen) < 0) {
637 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
638 "WPA: Failed to set PTK to the "
639 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
640 alg, keylen, MAC2STR(sm->bssid));
644 /* TK is not needed anymore in supplicant */
645 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
647 if (sm->wpa_ptk_rekey) {
648 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
649 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
657 static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
659 int keylen, int maxkeylen,
665 *alg = wpa_cipher_to_alg(group_cipher);
666 if (*alg == WPA_ALG_NONE) {
667 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
668 "WPA: Unsupported Group Cipher %d",
672 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
674 klen = wpa_cipher_key_len(group_cipher);
675 if (keylen != klen || maxkeylen < klen) {
676 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
677 "WPA: Unsupported %s Group Cipher key length %d (%d)",
678 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
685 struct wpa_gtk_data {
687 int tx, key_rsc_len, keyidx;
693 static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
694 const struct wpa_gtk_data *gd,
697 const u8 *_gtk = gd->gtk;
700 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
701 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
702 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
703 gd->keyidx, gd->tx, gd->gtk_len);
704 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
705 if (sm->group_cipher == WPA_CIPHER_TKIP) {
706 /* Swap Tx/Rx keys for Michael MIC */
707 os_memcpy(gtk_buf, gd->gtk, 16);
708 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
709 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
712 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
713 if (wpa_sm_set_key(sm, gd->alg, NULL,
714 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
715 _gtk, gd->gtk_len) < 0) {
716 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
717 "WPA: Failed to set GTK to the driver "
719 os_memset(gtk_buf, 0, sizeof(gtk_buf));
722 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
723 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
724 _gtk, gd->gtk_len) < 0) {
725 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
726 "WPA: Failed to set GTK to "
727 "the driver (alg=%d keylen=%d keyidx=%d)",
728 gd->alg, gd->gtk_len, gd->keyidx);
729 os_memset(gtk_buf, 0, sizeof(gtk_buf));
732 os_memset(gtk_buf, 0, sizeof(gtk_buf));
738 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
741 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
742 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
743 * seemed to set this bit (incorrectly, since Tx is only when
744 * doing Group Key only APs) and without this workaround, the
745 * data connection does not work because wpa_supplicant
746 * configured non-zero keyidx to be used for unicast. */
747 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
748 "WPA: Tx bit set for GTK, but pairwise "
749 "keys are used - ignore Tx bit");
756 static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
757 const struct wpa_eapol_key *key,
758 const u8 *gtk, size_t gtk_len,
761 struct wpa_gtk_data gd;
764 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
766 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
767 * Reserved [bits 0-7]
771 os_memset(&gd, 0, sizeof(gd));
772 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
775 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
778 gd.keyidx = gtk[0] & 0x3;
779 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
780 !!(gtk[0] & BIT(2)));
784 os_memcpy(gd.gtk, gtk, gtk_len);
785 gd.gtk_len = gtk_len;
787 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
788 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
790 &gd.key_rsc_len, &gd.alg) ||
791 wpa_supplicant_install_gtk(sm, &gd, key->key_rsc))) {
792 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
793 "RSN: Failed to install GTK");
794 os_memset(&gd, 0, sizeof(gd));
797 os_memset(&gd, 0, sizeof(gd));
799 wpa_supplicant_key_neg_complete(sm, sm->bssid,
800 key_info & WPA_KEY_INFO_SECURE);
805 static int ieee80211w_set_keys(struct wpa_sm *sm,
806 struct wpa_eapol_ie_parse *ie)
808 #ifdef CONFIG_IEEE80211W
809 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
814 const struct wpa_igtk_kde *igtk;
816 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
817 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
819 igtk = (const struct wpa_igtk_kde *) ie->igtk;
820 keyidx = WPA_GET_LE16(igtk->keyid);
821 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: IGTK keyid %d "
822 "pn %02x%02x%02x%02x%02x%02x",
823 keyidx, MAC2STR(igtk->pn));
824 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK",
827 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
828 "WPA: Invalid IGTK KeyID %d", keyidx);
831 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
832 broadcast_ether_addr,
833 keyidx, 0, igtk->pn, sizeof(igtk->pn),
834 igtk->igtk, len) < 0) {
835 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
836 "WPA: Failed to configure IGTK to the driver");
842 #else /* CONFIG_IEEE80211W */
844 #endif /* CONFIG_IEEE80211W */
848 static void wpa_report_ie_mismatch(struct wpa_sm *sm,
849 const char *reason, const u8 *src_addr,
850 const u8 *wpa_ie, size_t wpa_ie_len,
851 const u8 *rsn_ie, size_t rsn_ie_len)
853 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
854 reason, MAC2STR(src_addr));
857 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
858 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
861 if (!sm->ap_wpa_ie) {
862 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
863 "WPA: No WPA IE in Beacon/ProbeResp");
865 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
870 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
871 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
874 if (!sm->ap_rsn_ie) {
875 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
876 "WPA: No RSN IE in Beacon/ProbeResp");
878 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
882 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
886 #ifdef CONFIG_IEEE80211R
888 static int ft_validate_mdie(struct wpa_sm *sm,
889 const unsigned char *src_addr,
890 struct wpa_eapol_ie_parse *ie,
891 const u8 *assoc_resp_mdie)
893 struct rsn_mdie *mdie;
895 mdie = (struct rsn_mdie *) (ie->mdie + 2);
896 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
897 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
898 MOBILITY_DOMAIN_ID_LEN) != 0) {
899 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
900 "not match with the current mobility domain");
904 if (assoc_resp_mdie &&
905 (assoc_resp_mdie[1] != ie->mdie[1] ||
906 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
907 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
908 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
909 ie->mdie, 2 + ie->mdie[1]);
910 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
911 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
919 static int ft_validate_ftie(struct wpa_sm *sm,
920 const unsigned char *src_addr,
921 struct wpa_eapol_ie_parse *ie,
922 const u8 *assoc_resp_ftie)
924 if (ie->ftie == NULL) {
925 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
926 "FT: No FTIE in EAPOL-Key msg 3/4");
930 if (assoc_resp_ftie == NULL)
933 if (assoc_resp_ftie[1] != ie->ftie[1] ||
934 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
935 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
936 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
937 ie->ftie, 2 + ie->ftie[1]);
938 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
939 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
947 static int ft_validate_rsnie(struct wpa_sm *sm,
948 const unsigned char *src_addr,
949 struct wpa_eapol_ie_parse *ie)
951 struct wpa_ie_data rsn;
957 * Verify that PMKR1Name from EAPOL-Key message 3/4
958 * matches with the value we derived.
960 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
961 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
962 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
963 "FT 4-way handshake message 3/4");
967 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
969 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
970 "FT: PMKR1Name mismatch in "
971 "FT 4-way handshake message 3/4");
972 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
973 rsn.pmkid, WPA_PMK_NAME_LEN);
974 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
975 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
983 static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
984 const unsigned char *src_addr,
985 struct wpa_eapol_ie_parse *ie)
987 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
989 if (sm->assoc_resp_ies) {
990 pos = sm->assoc_resp_ies;
991 end = pos + sm->assoc_resp_ies_len;
992 while (pos + 2 < end) {
993 if (pos + 2 + pos[1] > end)
996 case WLAN_EID_MOBILITY_DOMAIN:
999 case WLAN_EID_FAST_BSS_TRANSITION:
1007 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
1008 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
1009 ft_validate_rsnie(sm, src_addr, ie) < 0)
1015 #endif /* CONFIG_IEEE80211R */
1018 static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
1019 const unsigned char *src_addr,
1020 struct wpa_eapol_ie_parse *ie)
1022 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
1023 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1024 "WPA: No WPA/RSN IE for this AP known. "
1025 "Trying to get from scan results");
1026 if (wpa_sm_get_beacon_ie(sm) < 0) {
1027 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1028 "WPA: Could not find AP from "
1029 "the scan results");
1031 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1032 "WPA: Found the current AP from "
1033 "updated scan results");
1037 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1038 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1039 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1040 "with IE in Beacon/ProbeResp (no IE?)",
1041 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1042 ie->rsn_ie, ie->rsn_ie_len);
1046 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1047 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1048 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1049 (ie->rsn_ie && sm->ap_rsn_ie &&
1050 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1051 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1052 ie->rsn_ie, ie->rsn_ie_len))) {
1053 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1054 "with IE in Beacon/ProbeResp",
1055 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1056 ie->rsn_ie, ie->rsn_ie_len);
1060 if (sm->proto == WPA_PROTO_WPA &&
1061 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1062 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1063 "detected - RSN was enabled and RSN IE "
1064 "was in msg 3/4, but not in "
1066 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1067 ie->rsn_ie, ie->rsn_ie_len);
1071 #ifdef CONFIG_IEEE80211R
1072 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1073 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1075 #endif /* CONFIG_IEEE80211R */
1082 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1083 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1084 * @dst: Destination address for the frame
1085 * @key: Pointer to the EAPOL-Key frame header
1086 * @ver: Version bits from EAPOL-Key Key Info
1087 * @key_info: Key Info
1088 * @ptk: PTK to use for keyed hash and encryption
1089 * Returns: 0 on success, -1 on failure
1091 int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1092 const struct wpa_eapol_key *key,
1093 u16 ver, u16 key_info,
1094 struct wpa_ptk *ptk)
1096 size_t mic_len, hdrlen, rlen;
1097 struct wpa_eapol_key *reply;
1098 struct wpa_eapol_key_192 *reply192;
1101 mic_len = wpa_mic_len(sm->key_mgmt);
1102 hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
1103 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1104 hdrlen, &rlen, (void *) &reply);
1107 reply192 = (struct wpa_eapol_key_192 *) reply;
1109 reply->type = (sm->proto == WPA_PROTO_RSN ||
1110 sm->proto == WPA_PROTO_OSEN) ?
1111 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1112 key_info &= WPA_KEY_INFO_SECURE;
1113 key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
1114 WPA_PUT_BE16(reply->key_info, key_info);
1115 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
1116 WPA_PUT_BE16(reply->key_length, 0);
1118 os_memcpy(reply->key_length, key->key_length, 2);
1119 os_memcpy(reply->replay_counter, key->replay_counter,
1120 WPA_REPLAY_COUNTER_LEN);
1122 key_mic = reply192->key_mic; /* same offset for reply and reply192 */
1124 WPA_PUT_BE16(reply192->key_data_length, 0);
1126 WPA_PUT_BE16(reply->key_data_length, 0);
1128 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
1129 wpa_eapol_key_send(sm, ptk->kck, ptk->kck_len, ver, dst, ETH_P_EAPOL,
1130 rbuf, rlen, key_mic);
1136 static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1137 const struct wpa_eapol_key *key,
1138 u16 ver, const u8 *key_data,
1139 size_t key_data_len)
1141 u16 key_info, keylen;
1142 struct wpa_eapol_ie_parse ie;
1144 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
1145 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way "
1146 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1148 key_info = WPA_GET_BE16(key->key_info);
1150 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1151 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
1153 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1154 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1155 "WPA: GTK IE in unencrypted key data");
1158 #ifdef CONFIG_IEEE80211W
1159 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1160 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1161 "WPA: IGTK KDE in unencrypted key data");
1166 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1167 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1168 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
1169 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1170 "WPA: Invalid IGTK KDE length %lu",
1171 (unsigned long) ie.igtk_len);
1174 #endif /* CONFIG_IEEE80211W */
1176 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1179 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1180 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1181 "WPA: ANonce from message 1 of 4-Way Handshake "
1182 "differs from 3 of 4-Way Handshake - drop packet (src="
1183 MACSTR ")", MAC2STR(sm->bssid));
1187 keylen = WPA_GET_BE16(key->key_length);
1188 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1189 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1190 "WPA: Invalid %s key length %d (src=" MACSTR
1191 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1192 MAC2STR(sm->bssid));
1197 if (ie.ip_addr_alloc) {
1198 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1199 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1200 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1202 #endif /* CONFIG_P2P */
1204 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
1209 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1210 * for the next 4-Way Handshake. If msg 3 is received again, the old
1211 * SNonce will still be used to avoid changing PTK. */
1212 sm->renew_snonce = 1;
1214 if (key_info & WPA_KEY_INFO_INSTALL) {
1215 if (wpa_supplicant_install_ptk(sm, key))
1219 if (key_info & WPA_KEY_INFO_SECURE) {
1220 wpa_sm_mlme_setprotection(
1221 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1222 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1223 eapol_sm_notify_portValid(sm->eapol, TRUE);
1225 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1227 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
1228 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1229 key_info & WPA_KEY_INFO_SECURE);
1230 } else if (ie.gtk &&
1231 wpa_supplicant_pairwise_gtk(sm, key,
1232 ie.gtk, ie.gtk_len, key_info) < 0) {
1233 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1234 "RSN: Failed to configure GTK");
1238 if (ieee80211w_set_keys(sm, &ie) < 0) {
1239 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1240 "RSN: Failed to configure IGTK");
1245 wpa_sm_set_rekey_offload(sm);
1247 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt)) {
1248 struct rsn_pmksa_cache_entry *sa;
1250 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
1251 sm->ptk.kck, sm->ptk.kck_len,
1252 sm->bssid, sm->own_addr,
1253 sm->network_ctx, sm->key_mgmt);
1258 sm->msg_3_of_4_ok = 1;
1262 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1266 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1270 struct wpa_gtk_data *gd)
1273 struct wpa_eapol_ie_parse ie;
1275 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
1276 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1278 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1279 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1280 "WPA: GTK IE in unencrypted key data");
1283 if (ie.gtk == NULL) {
1284 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1285 "WPA: No GTK IE in Group Key msg 1/2");
1288 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1290 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1291 gd->gtk_len, maxkeylen,
1292 &gd->key_rsc_len, &gd->alg))
1295 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
1296 ie.gtk, ie.gtk_len);
1297 gd->keyidx = ie.gtk[0] & 0x3;
1298 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1299 !!(ie.gtk[0] & BIT(2)));
1300 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1301 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1302 "RSN: Too long GTK in GTK IE (len=%lu)",
1303 (unsigned long) ie.gtk_len - 2);
1306 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1308 if (ieee80211w_set_keys(sm, &ie) < 0)
1309 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1310 "RSN: Failed to configure IGTK");
1316 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1317 const struct wpa_eapol_key *key,
1319 size_t key_data_len, u16 key_info,
1320 u16 ver, struct wpa_gtk_data *gd)
1325 gtk_len = WPA_GET_BE16(key->key_length);
1326 maxkeylen = key_data_len;
1327 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1328 if (maxkeylen < 8) {
1329 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1330 "WPA: Too short maxkeylen (%lu)",
1331 (unsigned long) maxkeylen);
1337 if (gtk_len > maxkeylen ||
1338 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1340 &gd->key_rsc_len, &gd->alg))
1343 gd->gtk_len = gtk_len;
1344 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1345 WPA_KEY_INFO_KEY_INDEX_SHIFT;
1346 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
1347 #ifdef CONFIG_NO_RC4
1348 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1349 "WPA: RC4 not supported in the build");
1351 #else /* CONFIG_NO_RC4 */
1353 if (key_data_len > sizeof(gd->gtk)) {
1354 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1355 "WPA: RC4 key data too long (%lu)",
1356 (unsigned long) key_data_len);
1359 os_memcpy(ek, key->key_iv, 16);
1360 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
1361 os_memcpy(gd->gtk, key_data, key_data_len);
1362 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
1363 os_memset(ek, 0, sizeof(ek));
1364 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1368 os_memset(ek, 0, sizeof(ek));
1369 #endif /* CONFIG_NO_RC4 */
1370 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1371 if (maxkeylen % 8) {
1372 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1373 "WPA: Unsupported AES-WRAP len %lu",
1374 (unsigned long) maxkeylen);
1377 if (maxkeylen > sizeof(gd->gtk)) {
1378 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1379 "WPA: AES-WRAP key data "
1380 "too long (keydatalen=%lu maxkeylen=%lu)",
1381 (unsigned long) key_data_len,
1382 (unsigned long) maxkeylen);
1385 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
1386 key_data, gd->gtk)) {
1387 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1388 "WPA: AES unwrap failed - could not decrypt "
1393 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1394 "WPA: Unsupported key_info type %d", ver);
1397 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1398 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1403 static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1404 const struct wpa_eapol_key *key,
1405 int ver, u16 key_info)
1407 size_t mic_len, hdrlen, rlen;
1408 struct wpa_eapol_key *reply;
1409 struct wpa_eapol_key_192 *reply192;
1412 mic_len = wpa_mic_len(sm->key_mgmt);
1413 hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
1414 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1415 hdrlen, &rlen, (void *) &reply);
1418 reply192 = (struct wpa_eapol_key_192 *) reply;
1420 reply->type = (sm->proto == WPA_PROTO_RSN ||
1421 sm->proto == WPA_PROTO_OSEN) ?
1422 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1423 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1424 key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
1425 WPA_PUT_BE16(reply->key_info, key_info);
1426 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
1427 WPA_PUT_BE16(reply->key_length, 0);
1429 os_memcpy(reply->key_length, key->key_length, 2);
1430 os_memcpy(reply->replay_counter, key->replay_counter,
1431 WPA_REPLAY_COUNTER_LEN);
1433 key_mic = reply192->key_mic; /* same offset for reply and reply192 */
1435 WPA_PUT_BE16(reply192->key_data_length, 0);
1437 WPA_PUT_BE16(reply->key_data_length, 0);
1439 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
1440 wpa_eapol_key_send(sm, sm->ptk.kck, sm->ptk.kck_len, ver, sm->bssid,
1441 ETH_P_EAPOL, rbuf, rlen, key_mic);
1447 static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1448 const unsigned char *src_addr,
1449 const struct wpa_eapol_key *key,
1451 size_t key_data_len, u16 ver)
1455 struct wpa_gtk_data gd;
1457 if (!sm->msg_3_of_4_ok) {
1458 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1459 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
1463 os_memset(&gd, 0, sizeof(gd));
1465 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1466 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1467 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1469 key_info = WPA_GET_BE16(key->key_info);
1471 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
1472 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
1473 key_data_len, key_info,
1476 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
1478 key_info, ver, &gd);
1481 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1486 if (wpa_supplicant_install_gtk(sm, &gd, key->key_rsc) ||
1487 wpa_supplicant_send_2_of_2(sm, key, ver, key_info))
1489 os_memset(&gd, 0, sizeof(gd));
1492 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Group rekeying "
1493 "completed with " MACSTR " [GTK=%s]",
1494 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1495 wpa_sm_cancel_auth_timeout(sm);
1496 wpa_sm_set_state(sm, WPA_COMPLETED);
1498 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1500 WPA_KEY_INFO_SECURE);
1503 wpa_sm_set_rekey_offload(sm);
1508 os_memset(&gd, 0, sizeof(gd));
1509 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1513 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
1514 struct wpa_eapol_key_192 *key,
1516 const u8 *buf, size_t len)
1518 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
1520 size_t mic_len = wpa_mic_len(sm->key_mgmt);
1522 os_memcpy(mic, key->key_mic, mic_len);
1524 os_memset(key->key_mic, 0, mic_len);
1525 wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len, sm->key_mgmt,
1526 ver, buf, len, key->key_mic);
1527 if (os_memcmp_const(mic, key->key_mic, mic_len) != 0) {
1528 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1529 "WPA: Invalid EAPOL-Key MIC "
1530 "when using TPTK - ignoring TPTK");
1535 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
1536 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
1540 if (!ok && sm->ptk_set) {
1541 os_memset(key->key_mic, 0, mic_len);
1542 wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len, sm->key_mgmt,
1543 ver, buf, len, key->key_mic);
1544 if (os_memcmp_const(mic, key->key_mic, mic_len) != 0) {
1545 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1546 "WPA: Invalid EAPOL-Key MIC - "
1554 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1555 "WPA: Could not verify EAPOL-Key MIC - "
1560 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1561 WPA_REPLAY_COUNTER_LEN);
1562 sm->rx_replay_counter_set = 1;
1567 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1568 static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
1569 struct wpa_eapol_key *key, u16 ver,
1570 u8 *key_data, size_t *key_data_len)
1572 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
1573 key_data, *key_data_len);
1575 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1576 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1581 /* Decrypt key data here so that this operation does not need
1582 * to be implemented separately for each message type. */
1583 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
1584 #ifdef CONFIG_NO_RC4
1585 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1586 "WPA: RC4 not supported in the build");
1588 #else /* CONFIG_NO_RC4 */
1590 os_memcpy(ek, key->key_iv, 16);
1591 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
1592 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
1593 os_memset(ek, 0, sizeof(ek));
1594 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1598 os_memset(ek, 0, sizeof(ek));
1599 #endif /* CONFIG_NO_RC4 */
1600 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1601 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
1602 sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
1603 wpa_key_mgmt_suite_b(sm->key_mgmt)) {
1605 if (*key_data_len < 8 || *key_data_len % 8) {
1606 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1607 "WPA: Unsupported AES-WRAP len %u",
1608 (unsigned int) *key_data_len);
1611 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
1612 buf = os_malloc(*key_data_len);
1614 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1615 "WPA: No memory for AES-UNWRAP buffer");
1618 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
1621 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1622 "WPA: AES unwrap failed - "
1623 "could not decrypt EAPOL-Key key data");
1626 os_memcpy(key_data, buf, *key_data_len);
1628 WPA_PUT_BE16(key->key_data_length, *key_data_len);
1630 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1631 "WPA: Unsupported key_info type %d", ver);
1634 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
1635 key_data, *key_data_len);
1641 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1642 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1644 void wpa_sm_aborted_cached(struct wpa_sm *sm)
1646 if (sm && sm->cur_pmksa) {
1647 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1648 "RSN: Cancelling PMKSA caching attempt");
1649 sm->cur_pmksa = NULL;
1654 static void wpa_eapol_key_dump(struct wpa_sm *sm,
1655 const struct wpa_eapol_key *key,
1656 unsigned int key_data_len,
1657 const u8 *mic, unsigned int mic_len)
1659 #ifndef CONFIG_NO_STDOUT_DEBUG
1660 u16 key_info = WPA_GET_BE16(key->key_info);
1662 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1663 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1664 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
1665 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1666 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1667 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1668 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1669 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1670 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1671 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1672 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1673 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1674 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1675 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1676 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1677 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1678 " key_length=%u key_data_length=%u",
1679 WPA_GET_BE16(key->key_length), key_data_len);
1680 wpa_hexdump(MSG_DEBUG, " replay_counter",
1681 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1682 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1683 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1684 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1685 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
1686 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
1687 #endif /* CONFIG_NO_STDOUT_DEBUG */
1692 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1693 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1694 * @src_addr: Source MAC address of the EAPOL packet
1695 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1696 * @len: Length of the EAPOL frame
1697 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1699 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1700 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1701 * only processing WPA and WPA2 EAPOL-Key frames.
1703 * The received EAPOL-Key packets are validated and valid packets are replied
1704 * to. In addition, key material (PTK, GTK) is configured at the end of a
1705 * successful key handshake.
1707 int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1708 const u8 *buf, size_t len)
1710 size_t plen, data_len, key_data_len;
1711 const struct ieee802_1x_hdr *hdr;
1712 struct wpa_eapol_key *key;
1713 struct wpa_eapol_key_192 *key192;
1717 struct wpa_peerkey *peerkey = NULL;
1719 size_t mic_len, keyhdrlen;
1721 #ifdef CONFIG_IEEE80211R
1722 sm->ft_completed = 0;
1723 #endif /* CONFIG_IEEE80211R */
1725 mic_len = wpa_mic_len(sm->key_mgmt);
1726 keyhdrlen = mic_len == 24 ? sizeof(*key192) : sizeof(*key);
1728 if (len < sizeof(*hdr) + keyhdrlen) {
1729 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1730 "WPA: EAPOL frame too short to be a WPA "
1731 "EAPOL-Key (len %lu, expecting at least %lu)",
1732 (unsigned long) len,
1733 (unsigned long) sizeof(*hdr) + keyhdrlen);
1737 hdr = (const struct ieee802_1x_hdr *) buf;
1738 plen = be_to_host16(hdr->length);
1739 data_len = plen + sizeof(*hdr);
1740 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1741 "IEEE 802.1X RX: version=%d type=%d length=%lu",
1742 hdr->version, hdr->type, (unsigned long) plen);
1744 if (hdr->version < EAPOL_VERSION) {
1745 /* TODO: backwards compatibility */
1747 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
1748 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1749 "WPA: EAPOL frame (type %u) discarded, "
1750 "not a Key frame", hdr->type);
1754 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
1755 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
1756 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1757 "WPA: EAPOL frame payload size %lu "
1758 "invalid (frame size %lu)",
1759 (unsigned long) plen, (unsigned long) len);
1763 if (data_len < len) {
1764 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1765 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
1766 (unsigned long) len - data_len);
1770 * Make a copy of the frame since we need to modify the buffer during
1771 * MAC validation and Key Data decryption.
1773 tmp = os_malloc(data_len);
1776 os_memcpy(tmp, buf, data_len);
1777 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
1778 key192 = (struct wpa_eapol_key_192 *)
1779 (tmp + sizeof(struct ieee802_1x_hdr));
1781 key_data = (u8 *) (key192 + 1);
1783 key_data = (u8 *) (key + 1);
1785 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
1787 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1788 "WPA: EAPOL-Key type (%d) unknown, discarded",
1795 key_data_len = WPA_GET_BE16(key192->key_data_length);
1797 key_data_len = WPA_GET_BE16(key->key_data_length);
1798 wpa_eapol_key_dump(sm, key, key_data_len, key192->key_mic, mic_len);
1800 if (key_data_len > plen - keyhdrlen) {
1801 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
1802 "frame - key_data overflow (%u > %u)",
1803 (unsigned int) key_data_len,
1804 (unsigned int) (plen - keyhdrlen));
1808 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
1809 key_info = WPA_GET_BE16(key->key_info);
1810 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1811 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
1812 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
1813 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1814 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
1815 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
1816 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1817 sm->key_mgmt != WPA_KEY_MGMT_OSEN) {
1818 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1819 "WPA: Unsupported EAPOL-Key descriptor version %d",
1824 if (sm->key_mgmt == WPA_KEY_MGMT_OSEN &&
1825 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1826 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1827 "OSEN: Unsupported EAPOL-Key descriptor version %d",
1832 if (wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1833 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1834 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1835 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
1840 #ifdef CONFIG_IEEE80211R
1841 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
1842 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
1843 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1844 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1845 "FT: AP did not use AES-128-CMAC");
1849 #endif /* CONFIG_IEEE80211R */
1850 #ifdef CONFIG_IEEE80211W
1851 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
1852 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1853 sm->key_mgmt != WPA_KEY_MGMT_OSEN &&
1854 !wpa_key_mgmt_suite_b(sm->key_mgmt)) {
1855 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1856 "WPA: AP did not use the "
1857 "negotiated AES-128-CMAC");
1861 #endif /* CONFIG_IEEE80211W */
1862 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
1863 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1864 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1865 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1866 "WPA: CCMP is used, but EAPOL-Key "
1867 "descriptor version (%d) is not 2", ver);
1868 if (sm->group_cipher != WPA_CIPHER_CCMP &&
1869 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1870 /* Earlier versions of IEEE 802.11i did not explicitly
1871 * require version 2 descriptor for all EAPOL-Key
1872 * packets, so allow group keys to use version 1 if
1873 * CCMP is not used for them. */
1874 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1875 "WPA: Backwards compatibility: allow invalid "
1876 "version for non-CCMP group keys");
1877 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1878 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1879 "WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
1882 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
1883 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1884 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1885 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1886 "WPA: GCMP is used, but EAPOL-Key "
1887 "descriptor version (%d) is not 2", ver);
1891 #ifdef CONFIG_PEERKEY
1892 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
1893 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
1897 if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
1898 if (!peerkey->initiator && peerkey->replay_counter_set &&
1899 os_memcmp(key->replay_counter, peerkey->replay_counter,
1900 WPA_REPLAY_COUNTER_LEN) <= 0) {
1901 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1902 "RSN: EAPOL-Key Replay Counter did not "
1903 "increase (STK) - dropping packet");
1905 } else if (peerkey->initiator) {
1906 u8 _tmp[WPA_REPLAY_COUNTER_LEN];
1907 os_memcpy(_tmp, key->replay_counter,
1908 WPA_REPLAY_COUNTER_LEN);
1909 inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
1910 if (os_memcmp(_tmp, peerkey->replay_counter,
1911 WPA_REPLAY_COUNTER_LEN) != 0) {
1912 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1913 "RSN: EAPOL-Key Replay "
1914 "Counter did not match (STK) - "
1921 if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
1922 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1923 "RSN: Ack bit in key_info from STK peer");
1926 #endif /* CONFIG_PEERKEY */
1928 if (!peerkey && sm->rx_replay_counter_set &&
1929 os_memcmp(key->replay_counter, sm->rx_replay_counter,
1930 WPA_REPLAY_COUNTER_LEN) <= 0) {
1931 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1932 "WPA: EAPOL-Key Replay Counter did not increase - "
1937 if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
1938 #ifdef CONFIG_PEERKEY
1939 && (peerkey == NULL || !peerkey->initiator)
1940 #endif /* CONFIG_PEERKEY */
1942 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1943 "WPA: No Ack bit in key_info");
1947 if (key_info & WPA_KEY_INFO_REQUEST) {
1948 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1949 "WPA: EAPOL-Key with Request bit - dropped");
1953 if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
1954 wpa_supplicant_verify_eapol_key_mic(sm, key192, ver, tmp, data_len))
1957 #ifdef CONFIG_PEERKEY
1958 if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
1959 peerkey_verify_eapol_key_mic(sm, peerkey, key192, ver, tmp,
1962 #endif /* CONFIG_PEERKEY */
1964 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
1965 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1966 if (wpa_supplicant_decrypt_key_data(sm, key, ver, key_data,
1971 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1972 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
1973 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1974 "WPA: Ignored EAPOL-Key (Pairwise) with "
1975 "non-zero key index");
1979 /* PeerKey 4-Way Handshake */
1980 peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver,
1981 key_data, key_data_len);
1982 } else if (key_info & WPA_KEY_INFO_MIC) {
1983 /* 3/4 4-Way Handshake */
1984 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
1987 /* 1/4 4-Way Handshake */
1988 wpa_supplicant_process_1_of_4(sm, src_addr, key,
1992 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
1993 /* PeerKey SMK Handshake */
1994 peerkey_rx_eapol_smk(sm, src_addr, key, key_data_len, key_info,
1997 if (key_info & WPA_KEY_INFO_MIC) {
1998 /* 1/2 Group Key Handshake */
1999 wpa_supplicant_process_1_of_2(sm, src_addr, key,
2000 key_data, key_data_len,
2003 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2004 "WPA: EAPOL-Key (Group) without Mic bit - "
2012 bin_clear_free(tmp, data_len);
2017 #ifdef CONFIG_CTRL_IFACE
2018 static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
2020 switch (sm->key_mgmt) {
2021 case WPA_KEY_MGMT_IEEE8021X:
2022 return ((sm->proto == WPA_PROTO_RSN ||
2023 sm->proto == WPA_PROTO_OSEN) ?
2024 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
2025 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
2026 case WPA_KEY_MGMT_PSK:
2027 return (sm->proto == WPA_PROTO_RSN ?
2028 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
2029 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
2030 #ifdef CONFIG_IEEE80211R
2031 case WPA_KEY_MGMT_FT_IEEE8021X:
2032 return RSN_AUTH_KEY_MGMT_FT_802_1X;
2033 case WPA_KEY_MGMT_FT_PSK:
2034 return RSN_AUTH_KEY_MGMT_FT_PSK;
2035 #endif /* CONFIG_IEEE80211R */
2036 #ifdef CONFIG_IEEE80211W
2037 case WPA_KEY_MGMT_IEEE8021X_SHA256:
2038 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
2039 case WPA_KEY_MGMT_PSK_SHA256:
2040 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
2041 #endif /* CONFIG_IEEE80211W */
2042 case WPA_KEY_MGMT_CCKM:
2043 return (sm->proto == WPA_PROTO_RSN ?
2044 RSN_AUTH_KEY_MGMT_CCKM:
2045 WPA_AUTH_KEY_MGMT_CCKM);
2046 case WPA_KEY_MGMT_WPA_NONE:
2047 return WPA_AUTH_KEY_MGMT_NONE;
2048 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
2049 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
2050 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
2051 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
2058 #define RSN_SUITE "%02x-%02x-%02x-%d"
2059 #define RSN_SUITE_ARG(s) \
2060 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2063 * wpa_sm_get_mib - Dump text list of MIB entries
2064 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2065 * @buf: Buffer for the list
2066 * @buflen: Length of the buffer
2067 * Returns: Number of bytes written to buffer
2069 * This function is used fetch dot11 MIB variables.
2071 int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
2073 char pmkid_txt[PMKID_LEN * 2 + 1];
2077 if (sm->cur_pmksa) {
2078 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2079 sm->cur_pmksa->pmkid, PMKID_LEN);
2081 pmkid_txt[0] = '\0';
2083 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
2084 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
2085 sm->proto == WPA_PROTO_RSN)
2090 ret = os_snprintf(buf, buflen,
2091 "dot11RSNAOptionImplemented=TRUE\n"
2092 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2093 "dot11RSNAEnabled=%s\n"
2094 "dot11RSNAPreauthenticationEnabled=%s\n"
2095 "dot11RSNAConfigVersion=%d\n"
2096 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2097 "dot11RSNAConfigGroupCipherSize=%d\n"
2098 "dot11RSNAConfigPMKLifetime=%d\n"
2099 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2100 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2101 "dot11RSNAConfigSATimeout=%d\n",
2102 rsna ? "TRUE" : "FALSE",
2103 rsna ? "TRUE" : "FALSE",
2105 wpa_cipher_key_len(sm->group_cipher) * 8,
2106 sm->dot11RSNAConfigPMKLifetime,
2107 sm->dot11RSNAConfigPMKReauthThreshold,
2108 sm->dot11RSNAConfigSATimeout);
2109 if (os_snprintf_error(buflen, ret))
2114 buf + len, buflen - len,
2115 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2116 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2117 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2118 "dot11RSNAPMKIDUsed=%s\n"
2119 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2120 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2121 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2122 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2123 "dot11RSNA4WayHandshakeFailures=%u\n",
2124 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
2125 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2126 sm->pairwise_cipher)),
2127 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2130 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
2131 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2132 sm->pairwise_cipher)),
2133 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2135 sm->dot11RSNA4WayHandshakeFailures);
2136 if (!os_snprintf_error(buflen - len, ret))
2141 #endif /* CONFIG_CTRL_IFACE */
2144 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
2145 void *ctx, enum pmksa_free_reason reason)
2147 struct wpa_sm *sm = ctx;
2150 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2151 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2153 if (sm->cur_pmksa == entry) {
2154 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2155 "RSN: %s current PMKSA entry",
2156 reason == PMKSA_REPLACE ? "replaced" : "removed");
2157 pmksa_cache_clear_current(sm);
2160 * If an entry is simply being replaced, there's no need to
2161 * deauthenticate because it will be immediately re-added.
2162 * This happens when EAP authentication is completed again
2163 * (reauth or failed PMKSA caching attempt).
2165 if (reason != PMKSA_REPLACE)
2169 if (reason == PMKSA_EXPIRE &&
2170 (sm->pmk_len == entry->pmk_len &&
2171 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2172 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2173 "RSN: deauthenticating due to expired PMK");
2174 pmksa_cache_clear_current(sm);
2179 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2180 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2186 * wpa_sm_init - Initialize WPA state machine
2187 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2188 * Returns: Pointer to the allocated WPA state machine data
2190 * This function is used to allocate a new WPA state machine and the returned
2191 * value is passed to all WPA state machine calls.
2193 struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2197 sm = os_zalloc(sizeof(*sm));
2200 dl_list_init(&sm->pmksa_candidates);
2201 sm->renew_snonce = 1;
2204 sm->dot11RSNAConfigPMKLifetime = 43200;
2205 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2206 sm->dot11RSNAConfigSATimeout = 60;
2208 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2209 if (sm->pmksa == NULL) {
2210 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2211 "RSN: PMKSA cache initialization failed");
2221 * wpa_sm_deinit - Deinitialize WPA state machine
2222 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2224 void wpa_sm_deinit(struct wpa_sm *sm)
2228 pmksa_cache_deinit(sm->pmksa);
2229 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2230 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2231 os_free(sm->assoc_wpa_ie);
2232 os_free(sm->ap_wpa_ie);
2233 os_free(sm->ap_rsn_ie);
2237 #ifdef CONFIG_IEEE80211R
2238 os_free(sm->assoc_resp_ies);
2239 #endif /* CONFIG_IEEE80211R */
2245 * wpa_sm_notify_assoc - Notify WPA state machine about association
2246 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2247 * @bssid: The BSSID of the new association
2249 * This function is called to let WPA state machine know that the connection
2252 void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2259 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2260 "WPA: Association event - clear replay counter");
2261 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2262 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2263 sm->rx_replay_counter_set = 0;
2264 sm->renew_snonce = 1;
2265 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2266 rsn_preauth_deinit(sm);
2268 #ifdef CONFIG_IEEE80211R
2269 if (wpa_ft_is_completed(sm)) {
2271 * Clear portValid to kick EAPOL state machine to re-enter
2272 * AUTHENTICATED state to get the EAPOL port Authorized.
2274 eapol_sm_notify_portValid(sm->eapol, FALSE);
2275 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2277 /* Prepare for the next transition */
2278 wpa_ft_prepare_auth_request(sm, NULL);
2282 #endif /* CONFIG_IEEE80211R */
2286 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2287 * this is not part of a Fast BSS Transition.
2289 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
2291 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2293 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2298 #endif /* CONFIG_TDLS */
2301 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
2302 #endif /* CONFIG_P2P */
2307 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2308 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2310 * This function is called to let WPA state machine know that the connection
2311 * was lost. This will abort any existing pre-authentication session.
2313 void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2315 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2316 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2318 rsn_preauth_deinit(sm);
2319 pmksa_cache_clear_current(sm);
2320 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2321 sm->dot11RSNA4WayHandshakeFailures++;
2323 wpa_tdls_disassoc(sm);
2324 #endif /* CONFIG_TDLS */
2326 /* Keys are not needed in the WPA state machine anymore */
2329 sm->msg_3_of_4_ok = 0;
2334 * wpa_sm_set_pmk - Set PMK
2335 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2337 * @pmk_len: The length of the new PMK in bytes
2338 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
2340 * Configure the PMK for WPA state machine.
2342 void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
2348 sm->pmk_len = pmk_len;
2349 os_memcpy(sm->pmk, pmk, pmk_len);
2351 #ifdef CONFIG_IEEE80211R
2352 /* Set XXKey to be PSK for FT key derivation */
2353 sm->xxkey_len = pmk_len;
2354 os_memcpy(sm->xxkey, pmk, pmk_len);
2355 #endif /* CONFIG_IEEE80211R */
2358 pmksa_cache_add(sm->pmksa, pmk, pmk_len, NULL, 0,
2359 bssid, sm->own_addr,
2360 sm->network_ctx, sm->key_mgmt);
2366 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2367 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2369 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2372 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2377 if (sm->cur_pmksa) {
2378 sm->pmk_len = sm->cur_pmksa->pmk_len;
2379 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2381 sm->pmk_len = PMK_LEN;
2382 os_memset(sm->pmk, 0, PMK_LEN);
2388 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2389 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2390 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2392 void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2395 sm->fast_reauth = fast_reauth;
2400 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2401 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2402 * @scard_ctx: Context pointer for smartcard related callback functions
2404 void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2408 sm->scard_ctx = scard_ctx;
2409 if (sm->preauth_eapol)
2410 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2415 * wpa_sm_set_config - Notification of current configration change
2416 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2417 * @config: Pointer to current network configuration
2419 * Notify WPA state machine that configuration has changed. config will be
2420 * stored as a backpointer to network configuration. This can be %NULL to clear
2421 * the stored pointed.
2423 void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2429 sm->network_ctx = config->network_ctx;
2430 sm->peerkey_enabled = config->peerkey_enabled;
2431 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2432 sm->proactive_key_caching = config->proactive_key_caching;
2433 sm->eap_workaround = config->eap_workaround;
2434 sm->eap_conf_ctx = config->eap_conf_ctx;
2436 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2437 sm->ssid_len = config->ssid_len;
2440 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
2441 sm->p2p = config->p2p;
2443 sm->network_ctx = NULL;
2444 sm->peerkey_enabled = 0;
2445 sm->allowed_pairwise_cipher = 0;
2446 sm->proactive_key_caching = 0;
2447 sm->eap_workaround = 0;
2448 sm->eap_conf_ctx = NULL;
2450 sm->wpa_ptk_rekey = 0;
2457 * wpa_sm_set_own_addr - Set own MAC address
2458 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2459 * @addr: Own MAC address
2461 void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2464 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2469 * wpa_sm_set_ifname - Set network interface name
2470 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2471 * @ifname: Interface name
2472 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2474 void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2475 const char *bridge_ifname)
2478 sm->ifname = ifname;
2479 sm->bridge_ifname = bridge_ifname;
2485 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2486 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2487 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2489 void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2497 * wpa_sm_set_param - Set WPA state machine parameters
2498 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2499 * @param: Parameter field
2500 * @value: Parameter value
2501 * Returns: 0 on success, -1 on failure
2503 int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2512 case RSNA_PMK_LIFETIME:
2514 sm->dot11RSNAConfigPMKLifetime = value;
2518 case RSNA_PMK_REAUTH_THRESHOLD:
2519 if (value > 0 && value <= 100)
2520 sm->dot11RSNAConfigPMKReauthThreshold = value;
2524 case RSNA_SA_TIMEOUT:
2526 sm->dot11RSNAConfigSATimeout = value;
2530 case WPA_PARAM_PROTO:
2533 case WPA_PARAM_PAIRWISE:
2534 sm->pairwise_cipher = value;
2536 case WPA_PARAM_GROUP:
2537 sm->group_cipher = value;
2539 case WPA_PARAM_KEY_MGMT:
2540 sm->key_mgmt = value;
2542 #ifdef CONFIG_IEEE80211W
2543 case WPA_PARAM_MGMT_GROUP:
2544 sm->mgmt_group_cipher = value;
2546 #endif /* CONFIG_IEEE80211W */
2547 case WPA_PARAM_RSN_ENABLED:
2548 sm->rsn_enabled = value;
2562 * wpa_sm_get_status - Get WPA state machine
2563 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2564 * @buf: Buffer for status information
2565 * @buflen: Maximum buffer length
2566 * @verbose: Whether to include verbose status information
2567 * Returns: Number of bytes written to buf.
2569 * Query WPA state machine for status information. This function fills in
2570 * a text area with current status information. If the buffer (buf) is not
2571 * large enough, status information will be truncated to fit the buffer.
2573 int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2576 char *pos = buf, *end = buf + buflen;
2579 ret = os_snprintf(pos, end - pos,
2580 "pairwise_cipher=%s\n"
2583 wpa_cipher_txt(sm->pairwise_cipher),
2584 wpa_cipher_txt(sm->group_cipher),
2585 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
2586 if (os_snprintf_error(end - pos, ret))
2590 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
2591 struct wpa_ie_data rsn;
2592 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
2594 rsn.capabilities & (WPA_CAPABILITY_MFPR |
2595 WPA_CAPABILITY_MFPC)) {
2596 ret = os_snprintf(pos, end - pos, "pmf=%d\n",
2598 WPA_CAPABILITY_MFPR) ? 2 : 1);
2599 if (os_snprintf_error(end - pos, ret))
2609 int wpa_sm_pmf_enabled(struct wpa_sm *sm)
2611 struct wpa_ie_data rsn;
2613 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
2616 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
2617 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
2625 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2626 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2627 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2628 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2629 * Returns: 0 on success, -1 on failure
2631 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2639 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2644 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2645 wpa_ie, *wpa_ie_len);
2647 if (sm->assoc_wpa_ie == NULL) {
2649 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2650 * the correct version of the IE even if PMKSA caching is
2651 * aborted (which would remove PMKID from IE generation).
2653 sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2654 if (sm->assoc_wpa_ie == NULL)
2657 os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2658 sm->assoc_wpa_ie_len = *wpa_ie_len;
2666 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2667 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2668 * @ie: Pointer to IE data (starting from id)
2670 * Returns: 0 on success, -1 on failure
2672 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2673 * Request frame. The IE will be used to override the default value generated
2674 * with wpa_sm_set_assoc_wpa_ie_default().
2676 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2681 os_free(sm->assoc_wpa_ie);
2682 if (ie == NULL || len == 0) {
2683 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2684 "WPA: clearing own WPA/RSN IE");
2685 sm->assoc_wpa_ie = NULL;
2686 sm->assoc_wpa_ie_len = 0;
2688 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2689 sm->assoc_wpa_ie = os_malloc(len);
2690 if (sm->assoc_wpa_ie == NULL)
2693 os_memcpy(sm->assoc_wpa_ie, ie, len);
2694 sm->assoc_wpa_ie_len = len;
2702 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2703 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2704 * @ie: Pointer to IE data (starting from id)
2706 * Returns: 0 on success, -1 on failure
2708 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2711 int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2716 os_free(sm->ap_wpa_ie);
2717 if (ie == NULL || len == 0) {
2718 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2719 "WPA: clearing AP WPA IE");
2720 sm->ap_wpa_ie = NULL;
2721 sm->ap_wpa_ie_len = 0;
2723 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2724 sm->ap_wpa_ie = os_malloc(len);
2725 if (sm->ap_wpa_ie == NULL)
2728 os_memcpy(sm->ap_wpa_ie, ie, len);
2729 sm->ap_wpa_ie_len = len;
2737 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2738 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2739 * @ie: Pointer to IE data (starting from id)
2741 * Returns: 0 on success, -1 on failure
2743 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2746 int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2751 os_free(sm->ap_rsn_ie);
2752 if (ie == NULL || len == 0) {
2753 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2754 "WPA: clearing AP RSN IE");
2755 sm->ap_rsn_ie = NULL;
2756 sm->ap_rsn_ie_len = 0;
2758 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2759 sm->ap_rsn_ie = os_malloc(len);
2760 if (sm->ap_rsn_ie == NULL)
2763 os_memcpy(sm->ap_rsn_ie, ie, len);
2764 sm->ap_rsn_ie_len = len;
2772 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2773 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2774 * @data: Pointer to data area for parsing results
2775 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2777 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2778 * parsed data into data.
2780 int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2785 if (sm->assoc_wpa_ie == NULL) {
2786 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2787 "WPA: No WPA/RSN IE available from association info");
2790 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
2796 int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
2798 return pmksa_cache_list(sm->pmksa, buf, len);
2802 void wpa_sm_drop_sa(struct wpa_sm *sm)
2804 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
2807 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2808 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2809 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2810 #ifdef CONFIG_IEEE80211R
2811 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
2812 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
2813 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
2814 #endif /* CONFIG_IEEE80211R */
2818 int wpa_sm_has_ptk(struct wpa_sm *sm)
2826 void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
2828 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
2832 void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
2834 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
2839 int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
2842 u8 keylen; /* plaintext key len */
2845 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
2846 struct wpa_gtk_data gd;
2848 os_memset(&gd, 0, sizeof(gd));
2849 keylen = wpa_cipher_key_len(sm->group_cipher);
2850 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
2851 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
2852 if (gd.alg == WPA_ALG_NONE) {
2853 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
2858 keyinfo = WPA_GET_LE16(buf + 2);
2859 gd.gtk_len = keylen;
2860 if (gd.gtk_len != buf[4]) {
2861 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
2862 gd.gtk_len, buf[4]);
2865 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
2866 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
2867 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
2869 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
2871 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
2872 gd.gtk, gd.gtk_len);
2873 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc)) {
2874 os_memset(&gd, 0, sizeof(gd));
2875 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
2879 os_memset(&gd, 0, sizeof(gd));
2880 #ifdef CONFIG_IEEE80211W
2881 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
2882 struct wpa_igtk_kde igd;
2885 os_memset(&igd, 0, sizeof(igd));
2886 keylen = wpa_cipher_key_len(sm->mgmt_group_cipher);
2887 os_memcpy(igd.keyid, buf + 2, 2);
2888 os_memcpy(igd.pn, buf + 4, 6);
2890 keyidx = WPA_GET_LE16(igd.keyid);
2891 os_memcpy(igd.igtk, buf + 10, keylen);
2893 wpa_hexdump_key(MSG_DEBUG, "Install IGTK (WNM SLEEP)",
2895 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
2896 broadcast_ether_addr,
2897 keyidx, 0, igd.pn, sizeof(igd.pn),
2898 igd.igtk, keylen) < 0) {
2899 wpa_printf(MSG_DEBUG, "Failed to install the IGTK in "
2901 os_memset(&igd, 0, sizeof(igd));
2904 os_memset(&igd, 0, sizeof(igd));
2905 #endif /* CONFIG_IEEE80211W */
2907 wpa_printf(MSG_DEBUG, "Unknown element id");
2913 #endif /* CONFIG_WNM */
2916 #ifdef CONFIG_PEERKEY
2917 int wpa_sm_rx_eapol_peerkey(struct wpa_sm *sm, const u8 *src_addr,
2918 const u8 *buf, size_t len)
2920 struct wpa_peerkey *peerkey;
2922 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
2923 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
2930 wpa_sm_rx_eapol(sm, src_addr, buf, len);
2934 #endif /* CONFIG_PEERKEY */
2939 int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
2941 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
2943 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
2947 #endif /* CONFIG_P2P */
2950 void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
2952 if (rx_replay_counter == NULL)
2955 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
2956 WPA_REPLAY_COUNTER_LEN);
2957 sm->rx_replay_counter_set = 1;
2958 wpa_printf(MSG_DEBUG, "Updated key replay counter");
2962 void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
2963 const u8 *ptk_kck, size_t ptk_kck_len,
2964 const u8 *ptk_kek, size_t ptk_kek_len)
2966 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
2967 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
2968 sm->ptk.kck_len = ptk_kck_len;
2969 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
2971 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
2972 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
2973 sm->ptk.kek_len = ptk_kek_len;
2974 wpa_printf(MSG_DEBUG, "Updated PTK KEK");