2 * WPA Supplicant - WPA state machine and EAPOL-Key processing
3 * Copyright (c) 2003-2015, Jouni Malinen <j@w1.fi>
4 * Copyright(c) 2015 Intel Deutschland GmbH
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
13 #include "crypto/aes_wrap.h"
14 #include "crypto/crypto.h"
15 #include "crypto/random.h"
16 #include "common/ieee802_11_defs.h"
17 #include "eapol_supp/eapol_supp_sm.h"
21 #include "pmksa_cache.h"
27 static const u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
31 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
32 * @sm: Pointer to WPA state machine data from wpa_sm_init()
33 * @kck: Key Confirmation Key (KCK, part of PTK)
34 * @kck_len: KCK length in octets
35 * @ver: Version field from Key Info
36 * @dest: Destination address for the frame
37 * @proto: Ethertype (usually ETH_P_EAPOL)
38 * @msg: EAPOL-Key message
39 * @msg_len: Length of message
40 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
41 * Returns: >= 0 on success, < 0 on failure
43 int wpa_eapol_key_send(struct wpa_sm *sm, const u8 *kck, size_t kck_len,
44 int ver, const u8 *dest, u16 proto,
45 u8 *msg, size_t msg_len, u8 *key_mic)
48 size_t mic_len = wpa_mic_len(sm->key_mgmt);
50 if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
52 * Association event was not yet received; try to fetch
53 * BSSID from the driver.
55 if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
56 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
57 "WPA: Failed to read BSSID for "
58 "EAPOL-Key destination address");
61 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
62 "WPA: Use BSSID (" MACSTR
63 ") as the destination for EAPOL-Key",
68 wpa_eapol_key_mic(kck, kck_len, sm->key_mgmt, ver, msg, msg_len,
70 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
71 "WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC",
75 wpa_hexdump_key(MSG_DEBUG, "WPA: KCK", kck, kck_len);
76 wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC", key_mic, mic_len);
77 wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
78 ret = wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
79 eapol_sm_notify_tx_eapol_key(sm->eapol);
87 * wpa_sm_key_request - Send EAPOL-Key Request
88 * @sm: Pointer to WPA state machine data from wpa_sm_init()
89 * @error: Indicate whether this is an Michael MIC error report
90 * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
92 * Send an EAPOL-Key Request to the current authenticator. This function is
93 * used to request rekeying and it is usually called when a local Michael MIC
94 * failure is detected.
96 void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
98 size_t mic_len, hdrlen, rlen;
99 struct wpa_eapol_key *reply;
100 struct wpa_eapol_key_192 *reply192;
102 u8 bssid[ETH_ALEN], *rbuf, *key_mic;
104 if (sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
105 wpa_key_mgmt_suite_b(sm->key_mgmt))
106 ver = WPA_KEY_INFO_TYPE_AKM_DEFINED;
107 else if (wpa_key_mgmt_ft(sm->key_mgmt) ||
108 wpa_key_mgmt_sha256(sm->key_mgmt))
109 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
110 else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
111 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
113 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
115 if (wpa_sm_get_bssid(sm, bssid) < 0) {
116 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
117 "Failed to read BSSID for EAPOL-Key request");
121 mic_len = wpa_mic_len(sm->key_mgmt);
122 hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
123 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
124 hdrlen, &rlen, (void *) &reply);
127 reply192 = (struct wpa_eapol_key_192 *) reply;
129 reply->type = (sm->proto == WPA_PROTO_RSN ||
130 sm->proto == WPA_PROTO_OSEN) ?
131 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
132 key_info = WPA_KEY_INFO_REQUEST | ver;
134 key_info |= WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
136 key_info |= WPA_KEY_INFO_ERROR;
138 key_info |= WPA_KEY_INFO_KEY_TYPE;
139 WPA_PUT_BE16(reply->key_info, key_info);
140 WPA_PUT_BE16(reply->key_length, 0);
141 os_memcpy(reply->replay_counter, sm->request_counter,
142 WPA_REPLAY_COUNTER_LEN);
143 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
146 WPA_PUT_BE16(reply192->key_data_length, 0);
148 WPA_PUT_BE16(reply->key_data_length, 0);
149 if (!(key_info & WPA_KEY_INFO_MIC))
152 key_mic = reply192->key_mic; /* same offset in reply */
154 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
155 "WPA: Sending EAPOL-Key Request (error=%d "
156 "pairwise=%d ptk_set=%d len=%lu)",
157 error, pairwise, sm->ptk_set, (unsigned long) rlen);
158 wpa_eapol_key_send(sm, sm->ptk.kck, sm->ptk.kck_len, ver, bssid,
159 ETH_P_EAPOL, rbuf, rlen, key_mic);
163 static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm *sm)
165 #ifdef CONFIG_IEEE80211R
166 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
167 if (wpa_sm_key_mgmt_set_pmk(sm, sm->xxkey, sm->xxkey_len))
168 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
169 "RSN: Cannot set low order 256 bits of MSK for key management offload");
171 #endif /* CONFIG_IEEE80211R */
172 if (wpa_sm_key_mgmt_set_pmk(sm, sm->pmk, sm->pmk_len))
173 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
174 "RSN: Cannot set PMK for key management offload");
175 #ifdef CONFIG_IEEE80211R
177 #endif /* CONFIG_IEEE80211R */
181 static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
182 const unsigned char *src_addr,
185 int abort_cached = 0;
187 if (pmkid && !sm->cur_pmksa) {
188 /* When using drivers that generate RSN IE, wpa_supplicant may
189 * not have enough time to get the association information
190 * event before receiving this 1/4 message, so try to find a
191 * matching PMKSA cache entry here. */
192 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid,
195 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
196 "RSN: found matching PMKID from PMKSA cache");
198 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
199 "RSN: no matching PMKID found");
204 if (pmkid && sm->cur_pmksa &&
205 os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
206 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
207 wpa_sm_set_pmk_from_pmksa(sm);
208 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
209 sm->pmk, sm->pmk_len);
210 eapol_sm_notify_cached(sm->eapol);
211 #ifdef CONFIG_IEEE80211R
213 #endif /* CONFIG_IEEE80211R */
214 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
217 if (sm->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
218 pmk_len = PMK_LEN_SUITE_B_192;
221 res = eapol_sm_get_key(sm->eapol, sm->pmk, pmk_len);
223 if (pmk_len == PMK_LEN) {
225 * EAP-LEAP is an exception from other EAP
226 * methods: it uses only 16-byte PMK.
228 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
232 #ifdef CONFIG_IEEE80211R
234 if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
236 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
237 sm->xxkey_len = PMK_LEN;
238 os_memset(buf, 0, sizeof(buf));
240 #endif /* CONFIG_IEEE80211R */
243 struct rsn_pmksa_cache_entry *sa = NULL;
244 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
245 "machines", sm->pmk, pmk_len);
246 sm->pmk_len = pmk_len;
247 wpa_supplicant_key_mgmt_set_pmk(sm);
248 if (sm->proto == WPA_PROTO_RSN &&
249 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
250 !wpa_key_mgmt_ft(sm->key_mgmt)) {
251 sa = pmksa_cache_add(sm->pmksa,
252 sm->pmk, pmk_len, NULL,
254 src_addr, sm->own_addr,
258 if (!sm->cur_pmksa && pmkid &&
259 pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL))
261 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
262 "RSN: the new PMK matches with the "
265 } else if (sa && !sm->cur_pmksa && pmkid) {
267 * It looks like the authentication server
268 * derived mismatching MSK. This should not
269 * really happen, but bugs happen.. There is not
270 * much we can do here without knowing what
271 * exactly caused the server to misbehave.
273 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
274 "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
281 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
282 "WPA: Failed to get master session key from "
283 "EAPOL state machines - key handshake "
286 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
287 "RSN: Cancelled PMKSA caching "
289 sm->cur_pmksa = NULL;
291 } else if (!abort_cached) {
297 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
298 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
299 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
301 /* Send EAPOL-Start to trigger full EAP authentication. */
305 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
306 "RSN: no PMKSA entry found - trigger "
307 "full EAP authentication");
308 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
309 NULL, 0, &buflen, NULL);
311 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
325 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
326 * @sm: Pointer to WPA state machine data from wpa_sm_init()
327 * @dst: Destination address for the frame
328 * @key: Pointer to the EAPOL-Key frame header
329 * @ver: Version bits from EAPOL-Key Key Info
330 * @nonce: Nonce value for the EAPOL-Key frame
331 * @wpa_ie: WPA/RSN IE
332 * @wpa_ie_len: Length of the WPA/RSN IE
333 * @ptk: PTK to use for keyed hash and encryption
334 * Returns: >= 0 on success, < 0 on failure
336 int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
337 const struct wpa_eapol_key *key,
338 int ver, const u8 *nonce,
339 const u8 *wpa_ie, size_t wpa_ie_len,
342 size_t mic_len, hdrlen, rlen;
343 struct wpa_eapol_key *reply;
344 struct wpa_eapol_key_192 *reply192;
346 u8 *rsn_ie_buf = NULL;
348 if (wpa_ie == NULL) {
349 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
350 "cannot generate msg 2/4");
354 #ifdef CONFIG_IEEE80211R
355 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
359 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
360 * FTIE from (Re)Association Response.
362 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
363 sm->assoc_resp_ies_len);
364 if (rsn_ie_buf == NULL)
366 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
367 res = wpa_insert_pmkid(rsn_ie_buf, &wpa_ie_len,
374 if (sm->assoc_resp_ies) {
375 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
376 sm->assoc_resp_ies_len);
377 wpa_ie_len += sm->assoc_resp_ies_len;
382 #endif /* CONFIG_IEEE80211R */
384 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
386 mic_len = wpa_mic_len(sm->key_mgmt);
387 hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
388 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
389 NULL, hdrlen + wpa_ie_len,
390 &rlen, (void *) &reply);
395 reply192 = (struct wpa_eapol_key_192 *) reply;
397 reply->type = (sm->proto == WPA_PROTO_RSN ||
398 sm->proto == WPA_PROTO_OSEN) ?
399 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
400 WPA_PUT_BE16(reply->key_info,
401 ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC);
402 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
403 WPA_PUT_BE16(reply->key_length, 0);
405 os_memcpy(reply->key_length, key->key_length, 2);
406 os_memcpy(reply->replay_counter, key->replay_counter,
407 WPA_REPLAY_COUNTER_LEN);
408 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
409 WPA_REPLAY_COUNTER_LEN);
411 key_mic = reply192->key_mic; /* same offset for reply and reply192 */
413 WPA_PUT_BE16(reply192->key_data_length, wpa_ie_len);
414 os_memcpy(reply192 + 1, wpa_ie, wpa_ie_len);
416 WPA_PUT_BE16(reply->key_data_length, wpa_ie_len);
417 os_memcpy(reply + 1, wpa_ie, wpa_ie_len);
421 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
423 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
424 return wpa_eapol_key_send(sm, ptk->kck, ptk->kck_len, ver, dst,
425 ETH_P_EAPOL, rbuf, rlen, key_mic);
429 static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
430 const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
432 #ifdef CONFIG_IEEE80211R
433 if (wpa_key_mgmt_ft(sm->key_mgmt))
434 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
435 #endif /* CONFIG_IEEE80211R */
437 return wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
438 sm->own_addr, sm->bssid, sm->snonce,
439 key->key_nonce, ptk, sm->key_mgmt,
440 sm->pairwise_cipher);
444 static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
445 const unsigned char *src_addr,
446 const struct wpa_eapol_key *key,
447 u16 ver, const u8 *key_data,
450 struct wpa_eapol_ie_parse ie;
453 u8 *kde, *kde_buf = NULL;
456 if (wpa_sm_get_network_ctx(sm) == NULL) {
457 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
458 "found (msg 1 of 4)");
462 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
463 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of 4-Way "
464 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
466 os_memset(&ie, 0, sizeof(ie));
468 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
469 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
470 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
471 key_data, key_data_len);
472 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
475 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
476 "Authenticator", ie.pmkid, PMKID_LEN);
480 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
482 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
483 "msg 1/4 - requesting full EAP authentication");
489 if (sm->renew_snonce) {
490 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
491 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
492 "WPA: Failed to get random data for SNonce");
495 sm->renew_snonce = 0;
496 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
497 sm->snonce, WPA_NONCE_LEN);
500 /* Calculate PTK which will be stored as a temporary PTK until it has
501 * been verified when processing message 3/4. */
503 wpa_derive_ptk(sm, src_addr, key, ptk);
504 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
506 /* Supplicant: swap tx/rx Mic keys */
507 os_memcpy(buf, &ptk->tk[16], 8);
508 os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
509 os_memcpy(&ptk->tk[24], buf, 8);
510 os_memset(buf, 0, sizeof(buf));
514 kde = sm->assoc_wpa_ie;
515 kde_len = sm->assoc_wpa_ie_len;
519 kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 1);
522 wpa_printf(MSG_DEBUG, "P2P: Add IP Address Request KDE "
523 "into EAPOL-Key 2/4");
524 os_memcpy(kde_buf, kde, kde_len);
527 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
528 *pos++ = RSN_SELECTOR_LEN + 1;
529 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
530 pos += RSN_SELECTOR_LEN;
535 #endif /* CONFIG_P2P */
537 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
538 kde, kde_len, ptk) < 0)
542 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
547 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
551 static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
553 struct wpa_sm *sm = eloop_ctx;
554 rsn_preauth_candidate_process(sm);
558 static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
559 const u8 *addr, int secure)
561 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
562 "WPA: Key negotiation completed with "
563 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
564 wpa_cipher_txt(sm->pairwise_cipher),
565 wpa_cipher_txt(sm->group_cipher));
566 wpa_sm_cancel_auth_timeout(sm);
567 wpa_sm_set_state(sm, WPA_COMPLETED);
570 wpa_sm_mlme_setprotection(
571 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
572 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
573 eapol_sm_notify_portValid(sm->eapol, TRUE);
574 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt))
575 eapol_sm_notify_eap_success(sm->eapol, TRUE);
577 * Start preauthentication after a short wait to avoid a
578 * possible race condition between the data receive and key
579 * configuration after the 4-Way Handshake. This increases the
580 * likelihood of the first preauth EAPOL-Start frame getting to
583 eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
586 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
587 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
588 "RSN: Authenticator accepted "
589 "opportunistic PMKSA entry - marking it valid");
590 sm->cur_pmksa->opportunistic = 0;
593 #ifdef CONFIG_IEEE80211R
594 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
595 /* Prepare for the next transition */
596 wpa_ft_prepare_auth_request(sm, NULL);
598 #endif /* CONFIG_IEEE80211R */
602 static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
604 struct wpa_sm *sm = eloop_ctx;
605 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
606 wpa_sm_key_request(sm, 0, 1);
610 static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
611 const struct wpa_eapol_key *key)
617 if (sm->ptk.installed) {
618 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
619 "WPA: Do not re-install same PTK to the driver");
623 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
624 "WPA: Installing PTK to the driver");
626 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
627 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
628 "Suite: NONE - do not use pairwise keys");
632 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
633 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
634 "WPA: Unsupported pairwise cipher %d",
635 sm->pairwise_cipher);
639 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
640 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
641 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
643 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
646 key_rsc = key->key_rsc;
647 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
650 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
651 sm->ptk.tk, keylen) < 0) {
652 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
653 "WPA: Failed to set PTK to the "
654 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
655 alg, keylen, MAC2STR(sm->bssid));
659 /* TK is not needed anymore in supplicant */
660 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
661 sm->ptk.installed = 1;
663 if (sm->wpa_ptk_rekey) {
664 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
665 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
673 static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
675 int keylen, int maxkeylen,
681 *alg = wpa_cipher_to_alg(group_cipher);
682 if (*alg == WPA_ALG_NONE) {
683 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
684 "WPA: Unsupported Group Cipher %d",
688 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
690 klen = wpa_cipher_key_len(group_cipher);
691 if (keylen != klen || maxkeylen < klen) {
692 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
693 "WPA: Unsupported %s Group Cipher key length %d (%d)",
694 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
701 struct wpa_gtk_data {
703 int tx, key_rsc_len, keyidx;
709 static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
710 const struct wpa_gtk_data *gd,
711 const u8 *key_rsc, int wnm_sleep)
713 const u8 *_gtk = gd->gtk;
716 /* Detect possible key reinstallation */
717 if ((sm->gtk.gtk_len == (size_t) gd->gtk_len &&
718 os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) ||
719 (sm->gtk_wnm_sleep.gtk_len == (size_t) gd->gtk_len &&
720 os_memcmp(sm->gtk_wnm_sleep.gtk, gd->gtk,
721 sm->gtk_wnm_sleep.gtk_len) == 0)) {
722 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
723 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
724 gd->keyidx, gd->tx, gd->gtk_len);
728 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
729 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
730 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
731 gd->keyidx, gd->tx, gd->gtk_len);
732 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
733 if (sm->group_cipher == WPA_CIPHER_TKIP) {
734 /* Swap Tx/Rx keys for Michael MIC */
735 os_memcpy(gtk_buf, gd->gtk, 16);
736 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
737 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
740 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
741 if (wpa_sm_set_key(sm, gd->alg, NULL,
742 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
743 _gtk, gd->gtk_len) < 0) {
744 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
745 "WPA: Failed to set GTK to the driver "
747 os_memset(gtk_buf, 0, sizeof(gtk_buf));
750 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
751 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
752 _gtk, gd->gtk_len) < 0) {
753 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
754 "WPA: Failed to set GTK to "
755 "the driver (alg=%d keylen=%d keyidx=%d)",
756 gd->alg, gd->gtk_len, gd->keyidx);
757 os_memset(gtk_buf, 0, sizeof(gtk_buf));
760 os_memset(gtk_buf, 0, sizeof(gtk_buf));
763 sm->gtk_wnm_sleep.gtk_len = gd->gtk_len;
764 os_memcpy(sm->gtk_wnm_sleep.gtk, gd->gtk,
765 sm->gtk_wnm_sleep.gtk_len);
767 sm->gtk.gtk_len = gd->gtk_len;
768 os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
775 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
778 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
779 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
780 * seemed to set this bit (incorrectly, since Tx is only when
781 * doing Group Key only APs) and without this workaround, the
782 * data connection does not work because wpa_supplicant
783 * configured non-zero keyidx to be used for unicast. */
784 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
785 "WPA: Tx bit set for GTK, but pairwise "
786 "keys are used - ignore Tx bit");
793 static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
798 if (!sm->wpa_rsc_relaxation)
801 rsclen = wpa_cipher_rsc_len(sm->group_cipher);
804 * Try to detect RSC (endian) corruption issue where the AP sends
805 * the RSC bytes in EAPOL-Key message in the wrong order, both if
806 * it's actually a 6-byte field (as it should be) and if it treats
807 * it as an 8-byte field.
808 * An AP model known to have this bug is the Sapido RB-1632.
810 if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
811 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
812 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
813 rsc[0], rsc[1], rsc[2], rsc[3],
814 rsc[4], rsc[5], rsc[6], rsc[7]);
823 static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
824 const struct wpa_eapol_key *key,
825 const u8 *gtk, size_t gtk_len,
828 struct wpa_gtk_data gd;
832 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
834 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
835 * Reserved [bits 0-7]
839 os_memset(&gd, 0, sizeof(gd));
840 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
843 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
846 gd.keyidx = gtk[0] & 0x3;
847 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
848 !!(gtk[0] & BIT(2)));
852 os_memcpy(gd.gtk, gtk, gtk_len);
853 gd.gtk_len = gtk_len;
855 key_rsc = key->key_rsc;
856 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
859 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
860 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
862 &gd.key_rsc_len, &gd.alg) ||
863 wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0))) {
864 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
865 "RSN: Failed to install GTK");
866 os_memset(&gd, 0, sizeof(gd));
869 os_memset(&gd, 0, sizeof(gd));
871 wpa_supplicant_key_neg_complete(sm, sm->bssid,
872 key_info & WPA_KEY_INFO_SECURE);
877 #ifdef CONFIG_IEEE80211W
878 static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
879 const struct wpa_igtk_kde *igtk,
882 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
883 u16 keyidx = WPA_GET_LE16(igtk->keyid);
885 /* Detect possible key reinstallation */
886 if ((sm->igtk.igtk_len == len &&
887 os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) ||
888 (sm->igtk_wnm_sleep.igtk_len == len &&
889 os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk,
890 sm->igtk_wnm_sleep.igtk_len) == 0)) {
891 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
892 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
897 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
898 "WPA: IGTK keyid %d pn %02x%02x%02x%02x%02x%02x",
899 keyidx, MAC2STR(igtk->pn));
900 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
902 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
903 "WPA: Invalid IGTK KeyID %d", keyidx);
906 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
907 broadcast_ether_addr,
908 keyidx, 0, igtk->pn, sizeof(igtk->pn),
909 igtk->igtk, len) < 0) {
910 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
911 "WPA: Failed to configure IGTK to the driver");
916 sm->igtk_wnm_sleep.igtk_len = len;
917 os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk,
918 sm->igtk_wnm_sleep.igtk_len);
920 sm->igtk.igtk_len = len;
921 os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
926 #endif /* CONFIG_IEEE80211W */
929 static int ieee80211w_set_keys(struct wpa_sm *sm,
930 struct wpa_eapol_ie_parse *ie)
932 #ifdef CONFIG_IEEE80211W
933 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
938 const struct wpa_igtk_kde *igtk;
940 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
941 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
944 igtk = (const struct wpa_igtk_kde *) ie->igtk;
945 if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0)
950 #else /* CONFIG_IEEE80211W */
952 #endif /* CONFIG_IEEE80211W */
956 static void wpa_report_ie_mismatch(struct wpa_sm *sm,
957 const char *reason, const u8 *src_addr,
958 const u8 *wpa_ie, size_t wpa_ie_len,
959 const u8 *rsn_ie, size_t rsn_ie_len)
961 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
962 reason, MAC2STR(src_addr));
965 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
966 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
969 if (!sm->ap_wpa_ie) {
970 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
971 "WPA: No WPA IE in Beacon/ProbeResp");
973 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
978 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
979 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
982 if (!sm->ap_rsn_ie) {
983 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
984 "WPA: No RSN IE in Beacon/ProbeResp");
986 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
990 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
994 #ifdef CONFIG_IEEE80211R
996 static int ft_validate_mdie(struct wpa_sm *sm,
997 const unsigned char *src_addr,
998 struct wpa_eapol_ie_parse *ie,
999 const u8 *assoc_resp_mdie)
1001 struct rsn_mdie *mdie;
1003 mdie = (struct rsn_mdie *) (ie->mdie + 2);
1004 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
1005 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
1006 MOBILITY_DOMAIN_ID_LEN) != 0) {
1007 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
1008 "not match with the current mobility domain");
1012 if (assoc_resp_mdie &&
1013 (assoc_resp_mdie[1] != ie->mdie[1] ||
1014 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
1015 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
1016 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
1017 ie->mdie, 2 + ie->mdie[1]);
1018 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
1019 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
1027 static int ft_validate_ftie(struct wpa_sm *sm,
1028 const unsigned char *src_addr,
1029 struct wpa_eapol_ie_parse *ie,
1030 const u8 *assoc_resp_ftie)
1032 if (ie->ftie == NULL) {
1033 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1034 "FT: No FTIE in EAPOL-Key msg 3/4");
1038 if (assoc_resp_ftie == NULL)
1041 if (assoc_resp_ftie[1] != ie->ftie[1] ||
1042 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
1043 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
1044 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
1045 ie->ftie, 2 + ie->ftie[1]);
1046 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
1047 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
1055 static int ft_validate_rsnie(struct wpa_sm *sm,
1056 const unsigned char *src_addr,
1057 struct wpa_eapol_ie_parse *ie)
1059 struct wpa_ie_data rsn;
1065 * Verify that PMKR1Name from EAPOL-Key message 3/4
1066 * matches with the value we derived.
1068 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
1069 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
1070 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
1071 "FT 4-way handshake message 3/4");
1075 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
1077 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1078 "FT: PMKR1Name mismatch in "
1079 "FT 4-way handshake message 3/4");
1080 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
1081 rsn.pmkid, WPA_PMK_NAME_LEN);
1082 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1083 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1091 static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
1092 const unsigned char *src_addr,
1093 struct wpa_eapol_ie_parse *ie)
1095 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
1097 if (sm->assoc_resp_ies) {
1098 pos = sm->assoc_resp_ies;
1099 end = pos + sm->assoc_resp_ies_len;
1100 while (end - pos > 2) {
1101 if (2 + pos[1] > end - pos)
1104 case WLAN_EID_MOBILITY_DOMAIN:
1107 case WLAN_EID_FAST_BSS_TRANSITION:
1115 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
1116 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
1117 ft_validate_rsnie(sm, src_addr, ie) < 0)
1123 #endif /* CONFIG_IEEE80211R */
1126 static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
1127 const unsigned char *src_addr,
1128 struct wpa_eapol_ie_parse *ie)
1130 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
1131 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1132 "WPA: No WPA/RSN IE for this AP known. "
1133 "Trying to get from scan results");
1134 if (wpa_sm_get_beacon_ie(sm) < 0) {
1135 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1136 "WPA: Could not find AP from "
1137 "the scan results");
1139 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1140 "WPA: Found the current AP from "
1141 "updated scan results");
1145 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1146 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1147 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1148 "with IE in Beacon/ProbeResp (no IE?)",
1149 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1150 ie->rsn_ie, ie->rsn_ie_len);
1154 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1155 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1156 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1157 (ie->rsn_ie && sm->ap_rsn_ie &&
1158 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1159 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1160 ie->rsn_ie, ie->rsn_ie_len))) {
1161 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1162 "with IE in Beacon/ProbeResp",
1163 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1164 ie->rsn_ie, ie->rsn_ie_len);
1168 if (sm->proto == WPA_PROTO_WPA &&
1169 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1170 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1171 "detected - RSN was enabled and RSN IE "
1172 "was in msg 3/4, but not in "
1174 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1175 ie->rsn_ie, ie->rsn_ie_len);
1179 #ifdef CONFIG_IEEE80211R
1180 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1181 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1183 #endif /* CONFIG_IEEE80211R */
1190 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1191 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1192 * @dst: Destination address for the frame
1193 * @key: Pointer to the EAPOL-Key frame header
1194 * @ver: Version bits from EAPOL-Key Key Info
1195 * @key_info: Key Info
1196 * @ptk: PTK to use for keyed hash and encryption
1197 * Returns: >= 0 on success, < 0 on failure
1199 int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1200 const struct wpa_eapol_key *key,
1201 u16 ver, u16 key_info,
1202 struct wpa_ptk *ptk)
1204 size_t mic_len, hdrlen, rlen;
1205 struct wpa_eapol_key *reply;
1206 struct wpa_eapol_key_192 *reply192;
1209 mic_len = wpa_mic_len(sm->key_mgmt);
1210 hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
1211 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1212 hdrlen, &rlen, (void *) &reply);
1215 reply192 = (struct wpa_eapol_key_192 *) reply;
1217 reply->type = (sm->proto == WPA_PROTO_RSN ||
1218 sm->proto == WPA_PROTO_OSEN) ?
1219 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1220 key_info &= WPA_KEY_INFO_SECURE;
1221 key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
1222 WPA_PUT_BE16(reply->key_info, key_info);
1223 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
1224 WPA_PUT_BE16(reply->key_length, 0);
1226 os_memcpy(reply->key_length, key->key_length, 2);
1227 os_memcpy(reply->replay_counter, key->replay_counter,
1228 WPA_REPLAY_COUNTER_LEN);
1230 key_mic = reply192->key_mic; /* same offset for reply and reply192 */
1232 WPA_PUT_BE16(reply192->key_data_length, 0);
1234 WPA_PUT_BE16(reply->key_data_length, 0);
1236 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
1237 return wpa_eapol_key_send(sm, ptk->kck, ptk->kck_len, ver, dst,
1238 ETH_P_EAPOL, rbuf, rlen, key_mic);
1242 static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1243 const struct wpa_eapol_key *key,
1244 u16 ver, const u8 *key_data,
1245 size_t key_data_len)
1247 u16 key_info, keylen;
1248 struct wpa_eapol_ie_parse ie;
1250 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
1251 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way "
1252 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1254 key_info = WPA_GET_BE16(key->key_info);
1256 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1257 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
1259 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1260 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1261 "WPA: GTK IE in unencrypted key data");
1264 #ifdef CONFIG_IEEE80211W
1265 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1266 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1267 "WPA: IGTK KDE in unencrypted key data");
1272 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1273 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1274 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
1275 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1276 "WPA: Invalid IGTK KDE length %lu",
1277 (unsigned long) ie.igtk_len);
1280 #endif /* CONFIG_IEEE80211W */
1282 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1285 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1286 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1287 "WPA: ANonce from message 1 of 4-Way Handshake "
1288 "differs from 3 of 4-Way Handshake - drop packet (src="
1289 MACSTR ")", MAC2STR(sm->bssid));
1293 keylen = WPA_GET_BE16(key->key_length);
1294 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1295 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1296 "WPA: Invalid %s key length %d (src=" MACSTR
1297 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1298 MAC2STR(sm->bssid));
1303 if (ie.ip_addr_alloc) {
1304 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1305 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1306 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1308 #endif /* CONFIG_P2P */
1310 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
1315 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1316 * for the next 4-Way Handshake. If msg 3 is received again, the old
1317 * SNonce will still be used to avoid changing PTK. */
1318 sm->renew_snonce = 1;
1320 if (key_info & WPA_KEY_INFO_INSTALL) {
1321 if (wpa_supplicant_install_ptk(sm, key))
1325 if (key_info & WPA_KEY_INFO_SECURE) {
1326 wpa_sm_mlme_setprotection(
1327 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1328 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1329 eapol_sm_notify_portValid(sm->eapol, TRUE);
1331 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1333 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
1334 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1335 key_info & WPA_KEY_INFO_SECURE);
1336 } else if (ie.gtk &&
1337 wpa_supplicant_pairwise_gtk(sm, key,
1338 ie.gtk, ie.gtk_len, key_info) < 0) {
1339 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1340 "RSN: Failed to configure GTK");
1344 if (ieee80211w_set_keys(sm, &ie) < 0) {
1345 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1346 "RSN: Failed to configure IGTK");
1351 wpa_sm_set_rekey_offload(sm);
1353 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt)) {
1354 struct rsn_pmksa_cache_entry *sa;
1356 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
1357 sm->ptk.kck, sm->ptk.kck_len,
1358 sm->bssid, sm->own_addr,
1359 sm->network_ctx, sm->key_mgmt);
1364 sm->msg_3_of_4_ok = 1;
1368 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1372 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1376 struct wpa_gtk_data *gd)
1379 struct wpa_eapol_ie_parse ie;
1381 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
1382 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1384 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1385 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1386 "WPA: GTK IE in unencrypted key data");
1389 if (ie.gtk == NULL) {
1390 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1391 "WPA: No GTK IE in Group Key msg 1/2");
1394 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1396 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1397 gd->gtk_len, maxkeylen,
1398 &gd->key_rsc_len, &gd->alg))
1401 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
1402 ie.gtk, ie.gtk_len);
1403 gd->keyidx = ie.gtk[0] & 0x3;
1404 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1405 !!(ie.gtk[0] & BIT(2)));
1406 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1407 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1408 "RSN: Too long GTK in GTK IE (len=%lu)",
1409 (unsigned long) ie.gtk_len - 2);
1412 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1414 if (ieee80211w_set_keys(sm, &ie) < 0)
1415 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1416 "RSN: Failed to configure IGTK");
1422 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1423 const struct wpa_eapol_key *key,
1425 size_t key_data_len, u16 key_info,
1426 u16 ver, struct wpa_gtk_data *gd)
1431 gtk_len = WPA_GET_BE16(key->key_length);
1432 maxkeylen = key_data_len;
1433 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1434 if (maxkeylen < 8) {
1435 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1436 "WPA: Too short maxkeylen (%lu)",
1437 (unsigned long) maxkeylen);
1443 if (gtk_len > maxkeylen ||
1444 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1446 &gd->key_rsc_len, &gd->alg))
1449 gd->gtk_len = gtk_len;
1450 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1451 WPA_KEY_INFO_KEY_INDEX_SHIFT;
1452 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
1453 #ifdef CONFIG_NO_RC4
1454 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1455 "WPA: RC4 not supported in the build");
1457 #else /* CONFIG_NO_RC4 */
1459 if (key_data_len > sizeof(gd->gtk)) {
1460 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1461 "WPA: RC4 key data too long (%lu)",
1462 (unsigned long) key_data_len);
1465 os_memcpy(ek, key->key_iv, 16);
1466 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
1467 os_memcpy(gd->gtk, key_data, key_data_len);
1468 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
1469 os_memset(ek, 0, sizeof(ek));
1470 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1474 os_memset(ek, 0, sizeof(ek));
1475 #endif /* CONFIG_NO_RC4 */
1476 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1477 if (maxkeylen % 8) {
1478 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1479 "WPA: Unsupported AES-WRAP len %lu",
1480 (unsigned long) maxkeylen);
1483 if (maxkeylen > sizeof(gd->gtk)) {
1484 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1485 "WPA: AES-WRAP key data "
1486 "too long (keydatalen=%lu maxkeylen=%lu)",
1487 (unsigned long) key_data_len,
1488 (unsigned long) maxkeylen);
1491 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
1492 key_data, gd->gtk)) {
1493 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1494 "WPA: AES unwrap failed - could not decrypt "
1499 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1500 "WPA: Unsupported key_info type %d", ver);
1503 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1504 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1509 static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1510 const struct wpa_eapol_key *key,
1511 int ver, u16 key_info)
1513 size_t mic_len, hdrlen, rlen;
1514 struct wpa_eapol_key *reply;
1515 struct wpa_eapol_key_192 *reply192;
1518 mic_len = wpa_mic_len(sm->key_mgmt);
1519 hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
1520 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1521 hdrlen, &rlen, (void *) &reply);
1524 reply192 = (struct wpa_eapol_key_192 *) reply;
1526 reply->type = (sm->proto == WPA_PROTO_RSN ||
1527 sm->proto == WPA_PROTO_OSEN) ?
1528 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1529 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1530 key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
1531 WPA_PUT_BE16(reply->key_info, key_info);
1532 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
1533 WPA_PUT_BE16(reply->key_length, 0);
1535 os_memcpy(reply->key_length, key->key_length, 2);
1536 os_memcpy(reply->replay_counter, key->replay_counter,
1537 WPA_REPLAY_COUNTER_LEN);
1539 key_mic = reply192->key_mic; /* same offset for reply and reply192 */
1541 WPA_PUT_BE16(reply192->key_data_length, 0);
1543 WPA_PUT_BE16(reply->key_data_length, 0);
1545 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
1546 return wpa_eapol_key_send(sm, sm->ptk.kck, sm->ptk.kck_len, ver,
1547 sm->bssid, ETH_P_EAPOL, rbuf, rlen, key_mic);
1551 static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1552 const unsigned char *src_addr,
1553 const struct wpa_eapol_key *key,
1555 size_t key_data_len, u16 ver)
1559 struct wpa_gtk_data gd;
1562 if (!sm->msg_3_of_4_ok) {
1563 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1564 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
1568 os_memset(&gd, 0, sizeof(gd));
1570 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1571 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1572 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1574 key_info = WPA_GET_BE16(key->key_info);
1576 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
1577 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
1578 key_data_len, key_info,
1581 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
1583 key_info, ver, &gd);
1586 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1591 key_rsc = key->key_rsc;
1592 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1595 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
1596 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
1598 os_memset(&gd, 0, sizeof(gd));
1601 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
1602 "completed with " MACSTR " [GTK=%s]",
1603 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1604 wpa_sm_cancel_auth_timeout(sm);
1605 wpa_sm_set_state(sm, WPA_COMPLETED);
1607 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1609 WPA_KEY_INFO_SECURE);
1612 wpa_sm_set_rekey_offload(sm);
1617 os_memset(&gd, 0, sizeof(gd));
1618 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1622 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
1623 struct wpa_eapol_key_192 *key,
1625 const u8 *buf, size_t len)
1627 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
1629 size_t mic_len = wpa_mic_len(sm->key_mgmt);
1631 os_memcpy(mic, key->key_mic, mic_len);
1633 os_memset(key->key_mic, 0, mic_len);
1634 wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len, sm->key_mgmt,
1635 ver, buf, len, key->key_mic);
1636 if (os_memcmp_const(mic, key->key_mic, mic_len) != 0) {
1637 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1638 "WPA: Invalid EAPOL-Key MIC "
1639 "when using TPTK - ignoring TPTK");
1644 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
1645 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
1649 if (!ok && sm->ptk_set) {
1650 os_memset(key->key_mic, 0, mic_len);
1651 wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len, sm->key_mgmt,
1652 ver, buf, len, key->key_mic);
1653 if (os_memcmp_const(mic, key->key_mic, mic_len) != 0) {
1654 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1655 "WPA: Invalid EAPOL-Key MIC - "
1663 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1664 "WPA: Could not verify EAPOL-Key MIC - "
1669 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1670 WPA_REPLAY_COUNTER_LEN);
1671 sm->rx_replay_counter_set = 1;
1676 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1677 static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
1678 struct wpa_eapol_key *key, u16 ver,
1679 u8 *key_data, size_t *key_data_len)
1681 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
1682 key_data, *key_data_len);
1684 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1685 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1690 /* Decrypt key data here so that this operation does not need
1691 * to be implemented separately for each message type. */
1692 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
1693 #ifdef CONFIG_NO_RC4
1694 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1695 "WPA: RC4 not supported in the build");
1697 #else /* CONFIG_NO_RC4 */
1699 os_memcpy(ek, key->key_iv, 16);
1700 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
1701 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
1702 os_memset(ek, 0, sizeof(ek));
1703 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1707 os_memset(ek, 0, sizeof(ek));
1708 #endif /* CONFIG_NO_RC4 */
1709 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1710 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
1711 sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
1712 wpa_key_mgmt_suite_b(sm->key_mgmt)) {
1714 if (*key_data_len < 8 || *key_data_len % 8) {
1715 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1716 "WPA: Unsupported AES-WRAP len %u",
1717 (unsigned int) *key_data_len);
1720 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
1721 buf = os_malloc(*key_data_len);
1723 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1724 "WPA: No memory for AES-UNWRAP buffer");
1727 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
1729 bin_clear_free(buf, *key_data_len);
1730 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1731 "WPA: AES unwrap failed - "
1732 "could not decrypt EAPOL-Key key data");
1735 os_memcpy(key_data, buf, *key_data_len);
1736 bin_clear_free(buf, *key_data_len);
1737 WPA_PUT_BE16(key->key_data_length, *key_data_len);
1739 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1740 "WPA: Unsupported key_info type %d", ver);
1743 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
1744 key_data, *key_data_len);
1750 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1751 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1753 void wpa_sm_aborted_cached(struct wpa_sm *sm)
1755 if (sm && sm->cur_pmksa) {
1756 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1757 "RSN: Cancelling PMKSA caching attempt");
1758 sm->cur_pmksa = NULL;
1763 static void wpa_eapol_key_dump(struct wpa_sm *sm,
1764 const struct wpa_eapol_key *key,
1765 unsigned int key_data_len,
1766 const u8 *mic, unsigned int mic_len)
1768 #ifndef CONFIG_NO_STDOUT_DEBUG
1769 u16 key_info = WPA_GET_BE16(key->key_info);
1771 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1772 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1773 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
1774 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1775 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1776 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1777 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1778 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1779 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1780 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1781 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1782 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1783 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1784 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1785 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1786 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1787 " key_length=%u key_data_length=%u",
1788 WPA_GET_BE16(key->key_length), key_data_len);
1789 wpa_hexdump(MSG_DEBUG, " replay_counter",
1790 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1791 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1792 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1793 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1794 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
1795 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
1796 #endif /* CONFIG_NO_STDOUT_DEBUG */
1801 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1802 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1803 * @src_addr: Source MAC address of the EAPOL packet
1804 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1805 * @len: Length of the EAPOL frame
1806 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1808 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1809 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1810 * only processing WPA and WPA2 EAPOL-Key frames.
1812 * The received EAPOL-Key packets are validated and valid packets are replied
1813 * to. In addition, key material (PTK, GTK) is configured at the end of a
1814 * successful key handshake.
1816 int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1817 const u8 *buf, size_t len)
1819 size_t plen, data_len, key_data_len;
1820 const struct ieee802_1x_hdr *hdr;
1821 struct wpa_eapol_key *key;
1822 struct wpa_eapol_key_192 *key192;
1826 struct wpa_peerkey *peerkey = NULL;
1828 size_t mic_len, keyhdrlen;
1830 #ifdef CONFIG_IEEE80211R
1831 sm->ft_completed = 0;
1832 #endif /* CONFIG_IEEE80211R */
1834 mic_len = wpa_mic_len(sm->key_mgmt);
1835 keyhdrlen = mic_len == 24 ? sizeof(*key192) : sizeof(*key);
1837 if (len < sizeof(*hdr) + keyhdrlen) {
1838 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1839 "WPA: EAPOL frame too short to be a WPA "
1840 "EAPOL-Key (len %lu, expecting at least %lu)",
1841 (unsigned long) len,
1842 (unsigned long) sizeof(*hdr) + keyhdrlen);
1846 hdr = (const struct ieee802_1x_hdr *) buf;
1847 plen = be_to_host16(hdr->length);
1848 data_len = plen + sizeof(*hdr);
1849 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1850 "IEEE 802.1X RX: version=%d type=%d length=%lu",
1851 hdr->version, hdr->type, (unsigned long) plen);
1853 if (hdr->version < EAPOL_VERSION) {
1854 /* TODO: backwards compatibility */
1856 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
1857 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1858 "WPA: EAPOL frame (type %u) discarded, "
1859 "not a Key frame", hdr->type);
1863 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
1864 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
1865 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1866 "WPA: EAPOL frame payload size %lu "
1867 "invalid (frame size %lu)",
1868 (unsigned long) plen, (unsigned long) len);
1872 if (data_len < len) {
1873 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1874 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
1875 (unsigned long) len - data_len);
1879 * Make a copy of the frame since we need to modify the buffer during
1880 * MAC validation and Key Data decryption.
1882 tmp = os_malloc(data_len);
1885 os_memcpy(tmp, buf, data_len);
1886 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
1887 key192 = (struct wpa_eapol_key_192 *)
1888 (tmp + sizeof(struct ieee802_1x_hdr));
1890 key_data = (u8 *) (key192 + 1);
1892 key_data = (u8 *) (key + 1);
1894 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
1896 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1897 "WPA: EAPOL-Key type (%d) unknown, discarded",
1904 key_data_len = WPA_GET_BE16(key192->key_data_length);
1906 key_data_len = WPA_GET_BE16(key->key_data_length);
1907 wpa_eapol_key_dump(sm, key, key_data_len, key192->key_mic, mic_len);
1909 if (key_data_len > plen - keyhdrlen) {
1910 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
1911 "frame - key_data overflow (%u > %u)",
1912 (unsigned int) key_data_len,
1913 (unsigned int) (plen - keyhdrlen));
1917 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
1918 key_info = WPA_GET_BE16(key->key_info);
1919 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1920 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
1921 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
1922 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1923 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
1924 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
1925 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1926 sm->key_mgmt != WPA_KEY_MGMT_OSEN) {
1927 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1928 "WPA: Unsupported EAPOL-Key descriptor version %d",
1933 if (sm->key_mgmt == WPA_KEY_MGMT_OSEN &&
1934 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1935 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1936 "OSEN: Unsupported EAPOL-Key descriptor version %d",
1941 if (wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1942 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1943 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1944 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
1949 #ifdef CONFIG_IEEE80211R
1950 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
1951 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
1952 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1953 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1954 "FT: AP did not use AES-128-CMAC");
1958 #endif /* CONFIG_IEEE80211R */
1959 #ifdef CONFIG_IEEE80211W
1960 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
1961 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1962 sm->key_mgmt != WPA_KEY_MGMT_OSEN &&
1963 !wpa_key_mgmt_suite_b(sm->key_mgmt)) {
1964 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1965 "WPA: AP did not use the "
1966 "negotiated AES-128-CMAC");
1970 #endif /* CONFIG_IEEE80211W */
1971 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
1972 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1973 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1974 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1975 "WPA: CCMP is used, but EAPOL-Key "
1976 "descriptor version (%d) is not 2", ver);
1977 if (sm->group_cipher != WPA_CIPHER_CCMP &&
1978 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1979 /* Earlier versions of IEEE 802.11i did not explicitly
1980 * require version 2 descriptor for all EAPOL-Key
1981 * packets, so allow group keys to use version 1 if
1982 * CCMP is not used for them. */
1983 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1984 "WPA: Backwards compatibility: allow invalid "
1985 "version for non-CCMP group keys");
1986 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1987 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1988 "WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
1991 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
1992 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1993 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1994 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1995 "WPA: GCMP is used, but EAPOL-Key "
1996 "descriptor version (%d) is not 2", ver);
2000 #ifdef CONFIG_PEERKEY
2001 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
2002 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
2006 if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
2007 if (!peerkey->initiator && peerkey->replay_counter_set &&
2008 os_memcmp(key->replay_counter, peerkey->replay_counter,
2009 WPA_REPLAY_COUNTER_LEN) <= 0) {
2010 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2011 "RSN: EAPOL-Key Replay Counter did not "
2012 "increase (STK) - dropping packet");
2014 } else if (peerkey->initiator) {
2015 u8 _tmp[WPA_REPLAY_COUNTER_LEN];
2016 os_memcpy(_tmp, key->replay_counter,
2017 WPA_REPLAY_COUNTER_LEN);
2018 inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
2019 if (os_memcmp(_tmp, peerkey->replay_counter,
2020 WPA_REPLAY_COUNTER_LEN) != 0) {
2021 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2022 "RSN: EAPOL-Key Replay "
2023 "Counter did not match (STK) - "
2030 if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
2031 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2032 "RSN: Ack bit in key_info from STK peer");
2035 #endif /* CONFIG_PEERKEY */
2037 if (!peerkey && sm->rx_replay_counter_set &&
2038 os_memcmp(key->replay_counter, sm->rx_replay_counter,
2039 WPA_REPLAY_COUNTER_LEN) <= 0) {
2040 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2041 "WPA: EAPOL-Key Replay Counter did not increase - "
2046 if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
2047 #ifdef CONFIG_PEERKEY
2048 && (peerkey == NULL || !peerkey->initiator)
2049 #endif /* CONFIG_PEERKEY */
2051 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2052 "WPA: No Ack bit in key_info");
2056 if (key_info & WPA_KEY_INFO_REQUEST) {
2057 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2058 "WPA: EAPOL-Key with Request bit - dropped");
2062 if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
2063 wpa_supplicant_verify_eapol_key_mic(sm, key192, ver, tmp, data_len))
2066 #ifdef CONFIG_PEERKEY
2067 if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
2068 peerkey_verify_eapol_key_mic(sm, peerkey, key192, ver, tmp,
2071 #endif /* CONFIG_PEERKEY */
2073 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
2074 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
2076 * Only decrypt the Key Data field if the frame's authenticity
2077 * was verified. When using AES-SIV (FILS), the MIC flag is not
2078 * set, so this check should only be performed if mic_len != 0
2079 * which is the case in this code branch.
2081 if (!(key_info & WPA_KEY_INFO_MIC)) {
2082 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2083 "WPA: Ignore EAPOL-Key with encrypted but unauthenticated data");
2086 if (wpa_supplicant_decrypt_key_data(sm, key, ver, key_data,
2091 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
2092 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
2093 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2094 "WPA: Ignored EAPOL-Key (Pairwise) with "
2095 "non-zero key index");
2099 /* PeerKey 4-Way Handshake */
2100 peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver,
2101 key_data, key_data_len);
2102 } else if (key_info & WPA_KEY_INFO_MIC) {
2103 /* 3/4 4-Way Handshake */
2104 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
2107 /* 1/4 4-Way Handshake */
2108 wpa_supplicant_process_1_of_4(sm, src_addr, key,
2112 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
2113 /* PeerKey SMK Handshake */
2114 peerkey_rx_eapol_smk(sm, src_addr, key, key_data_len, key_info,
2117 if (key_info & WPA_KEY_INFO_MIC) {
2118 /* 1/2 Group Key Handshake */
2119 wpa_supplicant_process_1_of_2(sm, src_addr, key,
2120 key_data, key_data_len,
2123 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2124 "WPA: EAPOL-Key (Group) without Mic bit - "
2132 bin_clear_free(tmp, data_len);
2137 #ifdef CONFIG_CTRL_IFACE
2138 static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
2140 switch (sm->key_mgmt) {
2141 case WPA_KEY_MGMT_IEEE8021X:
2142 return ((sm->proto == WPA_PROTO_RSN ||
2143 sm->proto == WPA_PROTO_OSEN) ?
2144 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
2145 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
2146 case WPA_KEY_MGMT_PSK:
2147 return (sm->proto == WPA_PROTO_RSN ?
2148 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
2149 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
2150 #ifdef CONFIG_IEEE80211R
2151 case WPA_KEY_MGMT_FT_IEEE8021X:
2152 return RSN_AUTH_KEY_MGMT_FT_802_1X;
2153 case WPA_KEY_MGMT_FT_PSK:
2154 return RSN_AUTH_KEY_MGMT_FT_PSK;
2155 #endif /* CONFIG_IEEE80211R */
2156 #ifdef CONFIG_IEEE80211W
2157 case WPA_KEY_MGMT_IEEE8021X_SHA256:
2158 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
2159 case WPA_KEY_MGMT_PSK_SHA256:
2160 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
2161 #endif /* CONFIG_IEEE80211W */
2162 case WPA_KEY_MGMT_CCKM:
2163 return (sm->proto == WPA_PROTO_RSN ?
2164 RSN_AUTH_KEY_MGMT_CCKM:
2165 WPA_AUTH_KEY_MGMT_CCKM);
2166 case WPA_KEY_MGMT_WPA_NONE:
2167 return WPA_AUTH_KEY_MGMT_NONE;
2168 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
2169 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
2170 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
2171 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
2178 #define RSN_SUITE "%02x-%02x-%02x-%d"
2179 #define RSN_SUITE_ARG(s) \
2180 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2183 * wpa_sm_get_mib - Dump text list of MIB entries
2184 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2185 * @buf: Buffer for the list
2186 * @buflen: Length of the buffer
2187 * Returns: Number of bytes written to buffer
2189 * This function is used fetch dot11 MIB variables.
2191 int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
2193 char pmkid_txt[PMKID_LEN * 2 + 1];
2197 if (sm->cur_pmksa) {
2198 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2199 sm->cur_pmksa->pmkid, PMKID_LEN);
2201 pmkid_txt[0] = '\0';
2203 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
2204 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
2205 sm->proto == WPA_PROTO_RSN)
2210 ret = os_snprintf(buf, buflen,
2211 "dot11RSNAOptionImplemented=TRUE\n"
2212 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2213 "dot11RSNAEnabled=%s\n"
2214 "dot11RSNAPreauthenticationEnabled=%s\n"
2215 "dot11RSNAConfigVersion=%d\n"
2216 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2217 "dot11RSNAConfigGroupCipherSize=%d\n"
2218 "dot11RSNAConfigPMKLifetime=%d\n"
2219 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2220 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2221 "dot11RSNAConfigSATimeout=%d\n",
2222 rsna ? "TRUE" : "FALSE",
2223 rsna ? "TRUE" : "FALSE",
2225 wpa_cipher_key_len(sm->group_cipher) * 8,
2226 sm->dot11RSNAConfigPMKLifetime,
2227 sm->dot11RSNAConfigPMKReauthThreshold,
2228 sm->dot11RSNAConfigSATimeout);
2229 if (os_snprintf_error(buflen, ret))
2234 buf + len, buflen - len,
2235 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2236 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2237 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2238 "dot11RSNAPMKIDUsed=%s\n"
2239 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2240 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2241 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2242 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2243 "dot11RSNA4WayHandshakeFailures=%u\n",
2244 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
2245 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2246 sm->pairwise_cipher)),
2247 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2250 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
2251 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2252 sm->pairwise_cipher)),
2253 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2255 sm->dot11RSNA4WayHandshakeFailures);
2256 if (!os_snprintf_error(buflen - len, ret))
2261 #endif /* CONFIG_CTRL_IFACE */
2264 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
2265 void *ctx, enum pmksa_free_reason reason)
2267 struct wpa_sm *sm = ctx;
2270 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2271 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2273 if (sm->cur_pmksa == entry) {
2274 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2275 "RSN: %s current PMKSA entry",
2276 reason == PMKSA_REPLACE ? "replaced" : "removed");
2277 pmksa_cache_clear_current(sm);
2280 * If an entry is simply being replaced, there's no need to
2281 * deauthenticate because it will be immediately re-added.
2282 * This happens when EAP authentication is completed again
2283 * (reauth or failed PMKSA caching attempt).
2285 if (reason != PMKSA_REPLACE)
2289 if (reason == PMKSA_EXPIRE &&
2290 (sm->pmk_len == entry->pmk_len &&
2291 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2292 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2293 "RSN: deauthenticating due to expired PMK");
2294 pmksa_cache_clear_current(sm);
2299 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2300 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2306 * wpa_sm_init - Initialize WPA state machine
2307 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2308 * Returns: Pointer to the allocated WPA state machine data
2310 * This function is used to allocate a new WPA state machine and the returned
2311 * value is passed to all WPA state machine calls.
2313 struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2317 sm = os_zalloc(sizeof(*sm));
2320 dl_list_init(&sm->pmksa_candidates);
2321 sm->renew_snonce = 1;
2324 sm->dot11RSNAConfigPMKLifetime = 43200;
2325 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2326 sm->dot11RSNAConfigSATimeout = 60;
2328 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2329 if (sm->pmksa == NULL) {
2330 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2331 "RSN: PMKSA cache initialization failed");
2341 * wpa_sm_deinit - Deinitialize WPA state machine
2342 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2344 void wpa_sm_deinit(struct wpa_sm *sm)
2348 pmksa_cache_deinit(sm->pmksa);
2349 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2350 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2351 os_free(sm->assoc_wpa_ie);
2352 os_free(sm->ap_wpa_ie);
2353 os_free(sm->ap_rsn_ie);
2357 #ifdef CONFIG_IEEE80211R
2358 os_free(sm->assoc_resp_ies);
2359 #endif /* CONFIG_IEEE80211R */
2360 #ifdef CONFIG_TESTING_OPTIONS
2361 wpabuf_free(sm->test_assoc_ie);
2362 #endif /* CONFIG_TESTING_OPTIONS */
2368 * wpa_sm_notify_assoc - Notify WPA state machine about association
2369 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2370 * @bssid: The BSSID of the new association
2372 * This function is called to let WPA state machine know that the connection
2375 void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2382 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2383 "WPA: Association event - clear replay counter");
2384 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2385 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2386 sm->rx_replay_counter_set = 0;
2387 sm->renew_snonce = 1;
2388 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2389 rsn_preauth_deinit(sm);
2391 #ifdef CONFIG_IEEE80211R
2392 if (wpa_ft_is_completed(sm)) {
2394 * Clear portValid to kick EAPOL state machine to re-enter
2395 * AUTHENTICATED state to get the EAPOL port Authorized.
2397 eapol_sm_notify_portValid(sm->eapol, FALSE);
2398 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2400 /* Prepare for the next transition */
2401 wpa_ft_prepare_auth_request(sm, NULL);
2405 #endif /* CONFIG_IEEE80211R */
2409 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2410 * this is not part of a Fast BSS Transition.
2412 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
2414 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2416 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2417 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
2418 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
2419 #ifdef CONFIG_IEEE80211W
2420 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
2421 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
2422 #endif /* CONFIG_IEEE80211W */
2427 #endif /* CONFIG_TDLS */
2430 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
2431 #endif /* CONFIG_P2P */
2436 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2437 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2439 * This function is called to let WPA state machine know that the connection
2440 * was lost. This will abort any existing pre-authentication session.
2442 void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2444 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2445 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2447 rsn_preauth_deinit(sm);
2448 pmksa_cache_clear_current(sm);
2449 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2450 sm->dot11RSNA4WayHandshakeFailures++;
2452 wpa_tdls_disassoc(sm);
2453 #endif /* CONFIG_TDLS */
2454 #ifdef CONFIG_IEEE80211R
2455 sm->ft_reassoc_completed = 0;
2456 #endif /* CONFIG_IEEE80211R */
2458 /* Keys are not needed in the WPA state machine anymore */
2461 sm->msg_3_of_4_ok = 0;
2466 * wpa_sm_set_pmk - Set PMK
2467 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2469 * @pmk_len: The length of the new PMK in bytes
2470 * @pmkid: Calculated PMKID
2471 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
2473 * Configure the PMK for WPA state machine.
2475 void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
2476 const u8 *pmkid, const u8 *bssid)
2481 sm->pmk_len = pmk_len;
2482 os_memcpy(sm->pmk, pmk, pmk_len);
2484 #ifdef CONFIG_IEEE80211R
2485 /* Set XXKey to be PSK for FT key derivation */
2486 sm->xxkey_len = pmk_len;
2487 os_memcpy(sm->xxkey, pmk, pmk_len);
2488 #endif /* CONFIG_IEEE80211R */
2491 pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
2492 bssid, sm->own_addr,
2493 sm->network_ctx, sm->key_mgmt);
2499 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2500 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2502 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2505 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2510 if (sm->cur_pmksa) {
2511 sm->pmk_len = sm->cur_pmksa->pmk_len;
2512 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2514 sm->pmk_len = PMK_LEN;
2515 os_memset(sm->pmk, 0, PMK_LEN);
2521 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2522 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2523 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2525 void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2528 sm->fast_reauth = fast_reauth;
2533 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2534 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2535 * @scard_ctx: Context pointer for smartcard related callback functions
2537 void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2541 sm->scard_ctx = scard_ctx;
2542 if (sm->preauth_eapol)
2543 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2548 * wpa_sm_set_config - Notification of current configration change
2549 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2550 * @config: Pointer to current network configuration
2552 * Notify WPA state machine that configuration has changed. config will be
2553 * stored as a backpointer to network configuration. This can be %NULL to clear
2554 * the stored pointed.
2556 void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2562 sm->network_ctx = config->network_ctx;
2563 sm->peerkey_enabled = config->peerkey_enabled;
2564 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2565 sm->proactive_key_caching = config->proactive_key_caching;
2566 sm->eap_workaround = config->eap_workaround;
2567 sm->eap_conf_ctx = config->eap_conf_ctx;
2569 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2570 sm->ssid_len = config->ssid_len;
2573 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
2574 sm->p2p = config->p2p;
2575 sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
2577 sm->network_ctx = NULL;
2578 sm->peerkey_enabled = 0;
2579 sm->allowed_pairwise_cipher = 0;
2580 sm->proactive_key_caching = 0;
2581 sm->eap_workaround = 0;
2582 sm->eap_conf_ctx = NULL;
2584 sm->wpa_ptk_rekey = 0;
2586 sm->wpa_rsc_relaxation = 0;
2592 * wpa_sm_set_own_addr - Set own MAC address
2593 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2594 * @addr: Own MAC address
2596 void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2599 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2604 * wpa_sm_set_ifname - Set network interface name
2605 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2606 * @ifname: Interface name
2607 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2609 void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2610 const char *bridge_ifname)
2613 sm->ifname = ifname;
2614 sm->bridge_ifname = bridge_ifname;
2620 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2621 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2622 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2624 void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2632 * wpa_sm_set_param - Set WPA state machine parameters
2633 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2634 * @param: Parameter field
2635 * @value: Parameter value
2636 * Returns: 0 on success, -1 on failure
2638 int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2647 case RSNA_PMK_LIFETIME:
2649 sm->dot11RSNAConfigPMKLifetime = value;
2653 case RSNA_PMK_REAUTH_THRESHOLD:
2654 if (value > 0 && value <= 100)
2655 sm->dot11RSNAConfigPMKReauthThreshold = value;
2659 case RSNA_SA_TIMEOUT:
2661 sm->dot11RSNAConfigSATimeout = value;
2665 case WPA_PARAM_PROTO:
2668 case WPA_PARAM_PAIRWISE:
2669 sm->pairwise_cipher = value;
2671 case WPA_PARAM_GROUP:
2672 sm->group_cipher = value;
2674 case WPA_PARAM_KEY_MGMT:
2675 sm->key_mgmt = value;
2677 #ifdef CONFIG_IEEE80211W
2678 case WPA_PARAM_MGMT_GROUP:
2679 sm->mgmt_group_cipher = value;
2681 #endif /* CONFIG_IEEE80211W */
2682 case WPA_PARAM_RSN_ENABLED:
2683 sm->rsn_enabled = value;
2697 * wpa_sm_get_status - Get WPA state machine
2698 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2699 * @buf: Buffer for status information
2700 * @buflen: Maximum buffer length
2701 * @verbose: Whether to include verbose status information
2702 * Returns: Number of bytes written to buf.
2704 * Query WPA state machine for status information. This function fills in
2705 * a text area with current status information. If the buffer (buf) is not
2706 * large enough, status information will be truncated to fit the buffer.
2708 int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2711 char *pos = buf, *end = buf + buflen;
2714 ret = os_snprintf(pos, end - pos,
2715 "pairwise_cipher=%s\n"
2718 wpa_cipher_txt(sm->pairwise_cipher),
2719 wpa_cipher_txt(sm->group_cipher),
2720 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
2721 if (os_snprintf_error(end - pos, ret))
2725 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
2726 struct wpa_ie_data rsn;
2727 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
2729 rsn.capabilities & (WPA_CAPABILITY_MFPR |
2730 WPA_CAPABILITY_MFPC)) {
2731 ret = os_snprintf(pos, end - pos, "pmf=%d\n",
2733 WPA_CAPABILITY_MFPR) ? 2 : 1);
2734 if (os_snprintf_error(end - pos, ret))
2744 int wpa_sm_pmf_enabled(struct wpa_sm *sm)
2746 struct wpa_ie_data rsn;
2748 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
2751 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
2752 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
2760 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2761 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2762 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2763 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2764 * Returns: 0 on success, -1 on failure
2766 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2774 #ifdef CONFIG_TESTING_OPTIONS
2775 if (sm->test_assoc_ie) {
2776 wpa_printf(MSG_DEBUG,
2777 "TESTING: Replace association WPA/RSN IE");
2778 if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
2780 os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
2781 wpabuf_len(sm->test_assoc_ie));
2782 res = wpabuf_len(sm->test_assoc_ie);
2784 #endif /* CONFIG_TESTING_OPTIONS */
2785 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2790 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2791 wpa_ie, *wpa_ie_len);
2793 if (sm->assoc_wpa_ie == NULL) {
2795 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2796 * the correct version of the IE even if PMKSA caching is
2797 * aborted (which would remove PMKID from IE generation).
2799 sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2800 if (sm->assoc_wpa_ie == NULL)
2803 os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2804 sm->assoc_wpa_ie_len = *wpa_ie_len;
2812 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2813 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2814 * @ie: Pointer to IE data (starting from id)
2816 * Returns: 0 on success, -1 on failure
2818 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2819 * Request frame. The IE will be used to override the default value generated
2820 * with wpa_sm_set_assoc_wpa_ie_default().
2822 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2827 os_free(sm->assoc_wpa_ie);
2828 if (ie == NULL || len == 0) {
2829 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2830 "WPA: clearing own WPA/RSN IE");
2831 sm->assoc_wpa_ie = NULL;
2832 sm->assoc_wpa_ie_len = 0;
2834 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2835 sm->assoc_wpa_ie = os_malloc(len);
2836 if (sm->assoc_wpa_ie == NULL)
2839 os_memcpy(sm->assoc_wpa_ie, ie, len);
2840 sm->assoc_wpa_ie_len = len;
2848 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2849 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2850 * @ie: Pointer to IE data (starting from id)
2852 * Returns: 0 on success, -1 on failure
2854 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2857 int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2862 os_free(sm->ap_wpa_ie);
2863 if (ie == NULL || len == 0) {
2864 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2865 "WPA: clearing AP WPA IE");
2866 sm->ap_wpa_ie = NULL;
2867 sm->ap_wpa_ie_len = 0;
2869 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2870 sm->ap_wpa_ie = os_malloc(len);
2871 if (sm->ap_wpa_ie == NULL)
2874 os_memcpy(sm->ap_wpa_ie, ie, len);
2875 sm->ap_wpa_ie_len = len;
2883 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2884 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2885 * @ie: Pointer to IE data (starting from id)
2887 * Returns: 0 on success, -1 on failure
2889 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2892 int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2897 os_free(sm->ap_rsn_ie);
2898 if (ie == NULL || len == 0) {
2899 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2900 "WPA: clearing AP RSN IE");
2901 sm->ap_rsn_ie = NULL;
2902 sm->ap_rsn_ie_len = 0;
2904 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2905 sm->ap_rsn_ie = os_malloc(len);
2906 if (sm->ap_rsn_ie == NULL)
2909 os_memcpy(sm->ap_rsn_ie, ie, len);
2910 sm->ap_rsn_ie_len = len;
2918 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2919 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2920 * @data: Pointer to data area for parsing results
2921 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2923 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2924 * parsed data into data.
2926 int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2931 if (sm->assoc_wpa_ie == NULL) {
2932 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2933 "WPA: No WPA/RSN IE available from association info");
2936 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
2942 int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
2944 return pmksa_cache_list(sm->pmksa, buf, len);
2948 void wpa_sm_drop_sa(struct wpa_sm *sm)
2950 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
2953 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2954 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2955 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2956 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
2957 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
2958 #ifdef CONFIG_IEEE80211W
2959 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
2960 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
2961 #endif /* CONFIG_IEEE80211W */
2962 #ifdef CONFIG_IEEE80211R
2963 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
2964 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
2965 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
2966 #endif /* CONFIG_IEEE80211R */
2970 int wpa_sm_has_ptk(struct wpa_sm *sm)
2978 void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
2980 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
2984 void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
2986 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
2991 int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
2994 u8 keylen; /* plaintext key len */
2997 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
2998 struct wpa_gtk_data gd;
3000 os_memset(&gd, 0, sizeof(gd));
3001 keylen = wpa_cipher_key_len(sm->group_cipher);
3002 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
3003 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
3004 if (gd.alg == WPA_ALG_NONE) {
3005 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
3010 keyinfo = WPA_GET_LE16(buf + 2);
3011 gd.gtk_len = keylen;
3012 if (gd.gtk_len != buf[4]) {
3013 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
3014 gd.gtk_len, buf[4]);
3017 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
3018 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
3019 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
3021 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
3023 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
3024 gd.gtk, gd.gtk_len);
3025 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) {
3026 os_memset(&gd, 0, sizeof(gd));
3027 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
3031 os_memset(&gd, 0, sizeof(gd));
3032 #ifdef CONFIG_IEEE80211W
3033 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
3034 const struct wpa_igtk_kde *igtk;
3036 igtk = (const struct wpa_igtk_kde *) (buf + 2);
3037 if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0)
3039 #endif /* CONFIG_IEEE80211W */
3041 wpa_printf(MSG_DEBUG, "Unknown element id");
3047 #endif /* CONFIG_WNM */
3050 #ifdef CONFIG_PEERKEY
3051 int wpa_sm_rx_eapol_peerkey(struct wpa_sm *sm, const u8 *src_addr,
3052 const u8 *buf, size_t len)
3054 struct wpa_peerkey *peerkey;
3056 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
3057 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
3064 wpa_sm_rx_eapol(sm, src_addr, buf, len);
3068 #endif /* CONFIG_PEERKEY */
3073 int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
3075 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
3077 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
3081 #endif /* CONFIG_P2P */
3084 void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
3086 if (rx_replay_counter == NULL)
3089 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
3090 WPA_REPLAY_COUNTER_LEN);
3091 sm->rx_replay_counter_set = 1;
3092 wpa_printf(MSG_DEBUG, "Updated key replay counter");
3096 void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
3097 const u8 *ptk_kck, size_t ptk_kck_len,
3098 const u8 *ptk_kek, size_t ptk_kek_len)
3100 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
3101 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
3102 sm->ptk.kck_len = ptk_kck_len;
3103 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
3105 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
3106 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
3107 sm->ptk.kek_len = ptk_kek_len;
3108 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
3114 #ifdef CONFIG_TESTING_OPTIONS
3115 void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
3117 wpabuf_free(sm->test_assoc_ie);
3118 sm->test_assoc_ie = buf;
3120 #endif /* CONFIG_TESTING_OPTIONS */