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));
515 kde = sm->assoc_wpa_ie;
516 kde_len = sm->assoc_wpa_ie_len;
520 kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 1);
523 wpa_printf(MSG_DEBUG, "P2P: Add IP Address Request KDE "
524 "into EAPOL-Key 2/4");
525 os_memcpy(kde_buf, kde, kde_len);
528 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
529 *pos++ = RSN_SELECTOR_LEN + 1;
530 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
531 pos += RSN_SELECTOR_LEN;
536 #endif /* CONFIG_P2P */
538 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
539 kde, kde_len, ptk) < 0)
543 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
548 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
552 static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
554 struct wpa_sm *sm = eloop_ctx;
555 rsn_preauth_candidate_process(sm);
559 static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
560 const u8 *addr, int secure)
562 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
563 "WPA: Key negotiation completed with "
564 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
565 wpa_cipher_txt(sm->pairwise_cipher),
566 wpa_cipher_txt(sm->group_cipher));
567 wpa_sm_cancel_auth_timeout(sm);
568 wpa_sm_set_state(sm, WPA_COMPLETED);
571 wpa_sm_mlme_setprotection(
572 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
573 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
574 eapol_sm_notify_portValid(sm->eapol, TRUE);
575 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt))
576 eapol_sm_notify_eap_success(sm->eapol, TRUE);
578 * Start preauthentication after a short wait to avoid a
579 * possible race condition between the data receive and key
580 * configuration after the 4-Way Handshake. This increases the
581 * likelihood of the first preauth EAPOL-Start frame getting to
584 eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
587 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
588 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
589 "RSN: Authenticator accepted "
590 "opportunistic PMKSA entry - marking it valid");
591 sm->cur_pmksa->opportunistic = 0;
594 #ifdef CONFIG_IEEE80211R
595 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
596 /* Prepare for the next transition */
597 wpa_ft_prepare_auth_request(sm, NULL);
599 #endif /* CONFIG_IEEE80211R */
603 static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
605 struct wpa_sm *sm = eloop_ctx;
606 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
607 wpa_sm_key_request(sm, 0, 1);
611 static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
612 const struct wpa_eapol_key *key)
618 if (sm->ptk.installed) {
619 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
620 "WPA: Do not re-install same PTK to the driver");
624 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
625 "WPA: Installing PTK to the driver");
627 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
628 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
629 "Suite: NONE - do not use pairwise keys");
633 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
634 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
635 "WPA: Unsupported pairwise cipher %d",
636 sm->pairwise_cipher);
640 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
641 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
642 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
644 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
647 key_rsc = key->key_rsc;
648 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
651 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
652 sm->ptk.tk, keylen) < 0) {
653 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
654 "WPA: Failed to set PTK to the "
655 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
656 alg, keylen, MAC2STR(sm->bssid));
660 /* TK is not needed anymore in supplicant */
661 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
662 sm->ptk.installed = 1;
664 if (sm->wpa_ptk_rekey) {
665 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
666 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
674 static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
676 int keylen, int maxkeylen,
682 *alg = wpa_cipher_to_alg(group_cipher);
683 if (*alg == WPA_ALG_NONE) {
684 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
685 "WPA: Unsupported Group Cipher %d",
689 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
691 klen = wpa_cipher_key_len(group_cipher);
692 if (keylen != klen || maxkeylen < klen) {
693 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
694 "WPA: Unsupported %s Group Cipher key length %d (%d)",
695 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
702 struct wpa_gtk_data {
704 int tx, key_rsc_len, keyidx;
710 static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
711 const struct wpa_gtk_data *gd,
712 const u8 *key_rsc, int wnm_sleep)
714 const u8 *_gtk = gd->gtk;
717 /* Detect possible key reinstallation */
718 if ((sm->gtk.gtk_len == (size_t) gd->gtk_len &&
719 os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) ||
720 (sm->gtk_wnm_sleep.gtk_len == (size_t) gd->gtk_len &&
721 os_memcmp(sm->gtk_wnm_sleep.gtk, gd->gtk,
722 sm->gtk_wnm_sleep.gtk_len) == 0)) {
723 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
724 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
725 gd->keyidx, gd->tx, gd->gtk_len);
729 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
730 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
731 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
732 gd->keyidx, gd->tx, gd->gtk_len);
733 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
734 if (sm->group_cipher == WPA_CIPHER_TKIP) {
735 /* Swap Tx/Rx keys for Michael MIC */
736 os_memcpy(gtk_buf, gd->gtk, 16);
737 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
738 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
741 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
742 if (wpa_sm_set_key(sm, gd->alg, NULL,
743 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
744 _gtk, gd->gtk_len) < 0) {
745 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
746 "WPA: Failed to set GTK to the driver "
748 os_memset(gtk_buf, 0, sizeof(gtk_buf));
751 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
752 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
753 _gtk, gd->gtk_len) < 0) {
754 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
755 "WPA: Failed to set GTK to "
756 "the driver (alg=%d keylen=%d keyidx=%d)",
757 gd->alg, gd->gtk_len, gd->keyidx);
758 os_memset(gtk_buf, 0, sizeof(gtk_buf));
761 os_memset(gtk_buf, 0, sizeof(gtk_buf));
764 sm->gtk_wnm_sleep.gtk_len = gd->gtk_len;
765 os_memcpy(sm->gtk_wnm_sleep.gtk, gd->gtk,
766 sm->gtk_wnm_sleep.gtk_len);
768 sm->gtk.gtk_len = gd->gtk_len;
769 os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
776 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
779 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
780 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
781 * seemed to set this bit (incorrectly, since Tx is only when
782 * doing Group Key only APs) and without this workaround, the
783 * data connection does not work because wpa_supplicant
784 * configured non-zero keyidx to be used for unicast. */
785 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
786 "WPA: Tx bit set for GTK, but pairwise "
787 "keys are used - ignore Tx bit");
794 static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
799 if (!sm->wpa_rsc_relaxation)
802 rsclen = wpa_cipher_rsc_len(sm->group_cipher);
805 * Try to detect RSC (endian) corruption issue where the AP sends
806 * the RSC bytes in EAPOL-Key message in the wrong order, both if
807 * it's actually a 6-byte field (as it should be) and if it treats
808 * it as an 8-byte field.
809 * An AP model known to have this bug is the Sapido RB-1632.
811 if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
812 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
813 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
814 rsc[0], rsc[1], rsc[2], rsc[3],
815 rsc[4], rsc[5], rsc[6], rsc[7]);
824 static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
825 const struct wpa_eapol_key *key,
826 const u8 *gtk, size_t gtk_len,
829 struct wpa_gtk_data gd;
833 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
835 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
836 * Reserved [bits 0-7]
840 os_memset(&gd, 0, sizeof(gd));
841 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
844 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
847 gd.keyidx = gtk[0] & 0x3;
848 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
849 !!(gtk[0] & BIT(2)));
853 os_memcpy(gd.gtk, gtk, gtk_len);
854 gd.gtk_len = gtk_len;
856 key_rsc = key->key_rsc;
857 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
860 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
861 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
863 &gd.key_rsc_len, &gd.alg) ||
864 wpa_supplicant_install_gtk(sm, &gd, key->key_rsc, 0))) {
865 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
866 "RSN: Failed to install GTK");
867 os_memset(&gd, 0, sizeof(gd));
870 os_memset(&gd, 0, sizeof(gd));
872 wpa_supplicant_key_neg_complete(sm, sm->bssid,
873 key_info & WPA_KEY_INFO_SECURE);
878 #ifdef CONFIG_IEEE80211W
879 static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
880 const struct wpa_igtk_kde *igtk,
883 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
884 u16 keyidx = WPA_GET_LE16(igtk->keyid);
886 /* Detect possible key reinstallation */
887 if ((sm->igtk.igtk_len == len &&
888 os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) ||
889 (sm->igtk_wnm_sleep.igtk_len == len &&
890 os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk,
891 sm->igtk_wnm_sleep.igtk_len) == 0)) {
892 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
893 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
898 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
899 "WPA: IGTK keyid %d pn %02x%02x%02x%02x%02x%02x",
900 keyidx, MAC2STR(igtk->pn));
901 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
903 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
904 "WPA: Invalid IGTK KeyID %d", keyidx);
907 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
908 broadcast_ether_addr,
909 keyidx, 0, igtk->pn, sizeof(igtk->pn),
910 igtk->igtk, len) < 0) {
911 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
912 "WPA: Failed to configure IGTK to the driver");
917 sm->igtk_wnm_sleep.igtk_len = len;
918 os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk,
919 sm->igtk_wnm_sleep.igtk_len);
921 sm->igtk.igtk_len = len;
922 os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
927 #endif /* CONFIG_IEEE80211W */
930 static int ieee80211w_set_keys(struct wpa_sm *sm,
931 struct wpa_eapol_ie_parse *ie)
933 #ifdef CONFIG_IEEE80211W
934 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
939 const struct wpa_igtk_kde *igtk;
941 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
942 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
945 igtk = (const struct wpa_igtk_kde *) ie->igtk;
946 if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0)
951 #else /* CONFIG_IEEE80211W */
953 #endif /* CONFIG_IEEE80211W */
957 static void wpa_report_ie_mismatch(struct wpa_sm *sm,
958 const char *reason, const u8 *src_addr,
959 const u8 *wpa_ie, size_t wpa_ie_len,
960 const u8 *rsn_ie, size_t rsn_ie_len)
962 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
963 reason, MAC2STR(src_addr));
966 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
967 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
970 if (!sm->ap_wpa_ie) {
971 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
972 "WPA: No WPA IE in Beacon/ProbeResp");
974 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
979 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
980 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
983 if (!sm->ap_rsn_ie) {
984 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
985 "WPA: No RSN IE in Beacon/ProbeResp");
987 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
991 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
995 #ifdef CONFIG_IEEE80211R
997 static int ft_validate_mdie(struct wpa_sm *sm,
998 const unsigned char *src_addr,
999 struct wpa_eapol_ie_parse *ie,
1000 const u8 *assoc_resp_mdie)
1002 struct rsn_mdie *mdie;
1004 mdie = (struct rsn_mdie *) (ie->mdie + 2);
1005 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
1006 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
1007 MOBILITY_DOMAIN_ID_LEN) != 0) {
1008 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
1009 "not match with the current mobility domain");
1013 if (assoc_resp_mdie &&
1014 (assoc_resp_mdie[1] != ie->mdie[1] ||
1015 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
1016 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
1017 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
1018 ie->mdie, 2 + ie->mdie[1]);
1019 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
1020 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
1028 static int ft_validate_ftie(struct wpa_sm *sm,
1029 const unsigned char *src_addr,
1030 struct wpa_eapol_ie_parse *ie,
1031 const u8 *assoc_resp_ftie)
1033 if (ie->ftie == NULL) {
1034 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1035 "FT: No FTIE in EAPOL-Key msg 3/4");
1039 if (assoc_resp_ftie == NULL)
1042 if (assoc_resp_ftie[1] != ie->ftie[1] ||
1043 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
1044 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
1045 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
1046 ie->ftie, 2 + ie->ftie[1]);
1047 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
1048 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
1056 static int ft_validate_rsnie(struct wpa_sm *sm,
1057 const unsigned char *src_addr,
1058 struct wpa_eapol_ie_parse *ie)
1060 struct wpa_ie_data rsn;
1066 * Verify that PMKR1Name from EAPOL-Key message 3/4
1067 * matches with the value we derived.
1069 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
1070 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
1071 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
1072 "FT 4-way handshake message 3/4");
1076 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
1078 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1079 "FT: PMKR1Name mismatch in "
1080 "FT 4-way handshake message 3/4");
1081 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
1082 rsn.pmkid, WPA_PMK_NAME_LEN);
1083 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1084 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1092 static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
1093 const unsigned char *src_addr,
1094 struct wpa_eapol_ie_parse *ie)
1096 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
1098 if (sm->assoc_resp_ies) {
1099 pos = sm->assoc_resp_ies;
1100 end = pos + sm->assoc_resp_ies_len;
1101 while (end - pos > 2) {
1102 if (2 + pos[1] > end - pos)
1105 case WLAN_EID_MOBILITY_DOMAIN:
1108 case WLAN_EID_FAST_BSS_TRANSITION:
1116 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
1117 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
1118 ft_validate_rsnie(sm, src_addr, ie) < 0)
1124 #endif /* CONFIG_IEEE80211R */
1127 static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
1128 const unsigned char *src_addr,
1129 struct wpa_eapol_ie_parse *ie)
1131 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
1132 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1133 "WPA: No WPA/RSN IE for this AP known. "
1134 "Trying to get from scan results");
1135 if (wpa_sm_get_beacon_ie(sm) < 0) {
1136 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1137 "WPA: Could not find AP from "
1138 "the scan results");
1140 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1141 "WPA: Found the current AP from "
1142 "updated scan results");
1146 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1147 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1148 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1149 "with IE in Beacon/ProbeResp (no IE?)",
1150 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1151 ie->rsn_ie, ie->rsn_ie_len);
1155 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1156 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1157 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1158 (ie->rsn_ie && sm->ap_rsn_ie &&
1159 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1160 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1161 ie->rsn_ie, ie->rsn_ie_len))) {
1162 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1163 "with IE in Beacon/ProbeResp",
1164 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1165 ie->rsn_ie, ie->rsn_ie_len);
1169 if (sm->proto == WPA_PROTO_WPA &&
1170 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1171 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1172 "detected - RSN was enabled and RSN IE "
1173 "was in msg 3/4, but not in "
1175 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1176 ie->rsn_ie, ie->rsn_ie_len);
1180 #ifdef CONFIG_IEEE80211R
1181 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1182 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1184 #endif /* CONFIG_IEEE80211R */
1191 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1192 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1193 * @dst: Destination address for the frame
1194 * @key: Pointer to the EAPOL-Key frame header
1195 * @ver: Version bits from EAPOL-Key Key Info
1196 * @key_info: Key Info
1197 * @ptk: PTK to use for keyed hash and encryption
1198 * Returns: >= 0 on success, < 0 on failure
1200 int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1201 const struct wpa_eapol_key *key,
1202 u16 ver, u16 key_info,
1203 struct wpa_ptk *ptk)
1205 size_t mic_len, hdrlen, rlen;
1206 struct wpa_eapol_key *reply;
1207 struct wpa_eapol_key_192 *reply192;
1210 mic_len = wpa_mic_len(sm->key_mgmt);
1211 hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
1212 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1213 hdrlen, &rlen, (void *) &reply);
1216 reply192 = (struct wpa_eapol_key_192 *) reply;
1218 reply->type = (sm->proto == WPA_PROTO_RSN ||
1219 sm->proto == WPA_PROTO_OSEN) ?
1220 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1221 key_info &= WPA_KEY_INFO_SECURE;
1222 key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
1223 WPA_PUT_BE16(reply->key_info, key_info);
1224 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
1225 WPA_PUT_BE16(reply->key_length, 0);
1227 os_memcpy(reply->key_length, key->key_length, 2);
1228 os_memcpy(reply->replay_counter, key->replay_counter,
1229 WPA_REPLAY_COUNTER_LEN);
1231 key_mic = reply192->key_mic; /* same offset for reply and reply192 */
1233 WPA_PUT_BE16(reply192->key_data_length, 0);
1235 WPA_PUT_BE16(reply->key_data_length, 0);
1237 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
1238 return wpa_eapol_key_send(sm, ptk->kck, ptk->kck_len, ver, dst,
1239 ETH_P_EAPOL, rbuf, rlen, key_mic);
1243 static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1244 const struct wpa_eapol_key *key,
1245 u16 ver, const u8 *key_data,
1246 size_t key_data_len)
1248 u16 key_info, keylen;
1249 struct wpa_eapol_ie_parse ie;
1251 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
1252 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way "
1253 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1255 key_info = WPA_GET_BE16(key->key_info);
1257 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1258 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
1260 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1261 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1262 "WPA: GTK IE in unencrypted key data");
1265 #ifdef CONFIG_IEEE80211W
1266 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1267 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1268 "WPA: IGTK KDE in unencrypted key data");
1273 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1274 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1275 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
1276 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1277 "WPA: Invalid IGTK KDE length %lu",
1278 (unsigned long) ie.igtk_len);
1281 #endif /* CONFIG_IEEE80211W */
1283 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1286 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1287 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1288 "WPA: ANonce from message 1 of 4-Way Handshake "
1289 "differs from 3 of 4-Way Handshake - drop packet (src="
1290 MACSTR ")", MAC2STR(sm->bssid));
1294 keylen = WPA_GET_BE16(key->key_length);
1295 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1296 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1297 "WPA: Invalid %s key length %d (src=" MACSTR
1298 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1299 MAC2STR(sm->bssid));
1304 if (ie.ip_addr_alloc) {
1305 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1306 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1307 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1309 #endif /* CONFIG_P2P */
1311 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
1316 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1317 * for the next 4-Way Handshake. If msg 3 is received again, the old
1318 * SNonce will still be used to avoid changing PTK. */
1319 sm->renew_snonce = 1;
1321 if (key_info & WPA_KEY_INFO_INSTALL) {
1322 if (wpa_supplicant_install_ptk(sm, key))
1326 if (key_info & WPA_KEY_INFO_SECURE) {
1327 wpa_sm_mlme_setprotection(
1328 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1329 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1330 eapol_sm_notify_portValid(sm->eapol, TRUE);
1332 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1334 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
1335 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1336 key_info & WPA_KEY_INFO_SECURE);
1337 } else if (ie.gtk &&
1338 wpa_supplicant_pairwise_gtk(sm, key,
1339 ie.gtk, ie.gtk_len, key_info) < 0) {
1340 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1341 "RSN: Failed to configure GTK");
1345 if (ieee80211w_set_keys(sm, &ie) < 0) {
1346 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1347 "RSN: Failed to configure IGTK");
1352 wpa_sm_set_rekey_offload(sm);
1354 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt)) {
1355 struct rsn_pmksa_cache_entry *sa;
1357 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
1358 sm->ptk.kck, sm->ptk.kck_len,
1359 sm->bssid, sm->own_addr,
1360 sm->network_ctx, sm->key_mgmt);
1365 sm->msg_3_of_4_ok = 1;
1369 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1373 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1377 struct wpa_gtk_data *gd)
1380 struct wpa_eapol_ie_parse ie;
1382 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
1383 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1385 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1386 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1387 "WPA: GTK IE in unencrypted key data");
1390 if (ie.gtk == NULL) {
1391 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1392 "WPA: No GTK IE in Group Key msg 1/2");
1395 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1397 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1398 gd->gtk_len, maxkeylen,
1399 &gd->key_rsc_len, &gd->alg))
1402 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
1403 ie.gtk, ie.gtk_len);
1404 gd->keyidx = ie.gtk[0] & 0x3;
1405 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1406 !!(ie.gtk[0] & BIT(2)));
1407 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1408 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1409 "RSN: Too long GTK in GTK IE (len=%lu)",
1410 (unsigned long) ie.gtk_len - 2);
1413 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1415 if (ieee80211w_set_keys(sm, &ie) < 0)
1416 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1417 "RSN: Failed to configure IGTK");
1423 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1424 const struct wpa_eapol_key *key,
1426 size_t key_data_len, u16 key_info,
1427 u16 ver, struct wpa_gtk_data *gd)
1432 gtk_len = WPA_GET_BE16(key->key_length);
1433 maxkeylen = key_data_len;
1434 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1435 if (maxkeylen < 8) {
1436 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1437 "WPA: Too short maxkeylen (%lu)",
1438 (unsigned long) maxkeylen);
1444 if (gtk_len > maxkeylen ||
1445 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1447 &gd->key_rsc_len, &gd->alg))
1450 gd->gtk_len = gtk_len;
1451 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1452 WPA_KEY_INFO_KEY_INDEX_SHIFT;
1453 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
1454 #ifdef CONFIG_NO_RC4
1455 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1456 "WPA: RC4 not supported in the build");
1458 #else /* CONFIG_NO_RC4 */
1460 if (key_data_len > sizeof(gd->gtk)) {
1461 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1462 "WPA: RC4 key data too long (%lu)",
1463 (unsigned long) key_data_len);
1466 os_memcpy(ek, key->key_iv, 16);
1467 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
1468 os_memcpy(gd->gtk, key_data, key_data_len);
1469 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
1470 os_memset(ek, 0, sizeof(ek));
1471 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1475 os_memset(ek, 0, sizeof(ek));
1476 #endif /* CONFIG_NO_RC4 */
1477 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1478 if (maxkeylen % 8) {
1479 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1480 "WPA: Unsupported AES-WRAP len %lu",
1481 (unsigned long) maxkeylen);
1484 if (maxkeylen > sizeof(gd->gtk)) {
1485 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1486 "WPA: AES-WRAP key data "
1487 "too long (keydatalen=%lu maxkeylen=%lu)",
1488 (unsigned long) key_data_len,
1489 (unsigned long) maxkeylen);
1492 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
1493 key_data, gd->gtk)) {
1494 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1495 "WPA: AES unwrap failed - could not decrypt "
1500 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1501 "WPA: Unsupported key_info type %d", ver);
1504 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1505 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1510 static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1511 const struct wpa_eapol_key *key,
1512 int ver, u16 key_info)
1514 size_t mic_len, hdrlen, rlen;
1515 struct wpa_eapol_key *reply;
1516 struct wpa_eapol_key_192 *reply192;
1519 mic_len = wpa_mic_len(sm->key_mgmt);
1520 hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
1521 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1522 hdrlen, &rlen, (void *) &reply);
1525 reply192 = (struct wpa_eapol_key_192 *) reply;
1527 reply->type = (sm->proto == WPA_PROTO_RSN ||
1528 sm->proto == WPA_PROTO_OSEN) ?
1529 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1530 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1531 key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
1532 WPA_PUT_BE16(reply->key_info, key_info);
1533 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
1534 WPA_PUT_BE16(reply->key_length, 0);
1536 os_memcpy(reply->key_length, key->key_length, 2);
1537 os_memcpy(reply->replay_counter, key->replay_counter,
1538 WPA_REPLAY_COUNTER_LEN);
1540 key_mic = reply192->key_mic; /* same offset for reply and reply192 */
1542 WPA_PUT_BE16(reply192->key_data_length, 0);
1544 WPA_PUT_BE16(reply->key_data_length, 0);
1546 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
1547 return wpa_eapol_key_send(sm, sm->ptk.kck, sm->ptk.kck_len, ver,
1548 sm->bssid, ETH_P_EAPOL, rbuf, rlen, key_mic);
1552 static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1553 const unsigned char *src_addr,
1554 const struct wpa_eapol_key *key,
1556 size_t key_data_len, u16 ver)
1560 struct wpa_gtk_data gd;
1563 if (!sm->msg_3_of_4_ok) {
1564 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1565 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
1569 os_memset(&gd, 0, sizeof(gd));
1571 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1572 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1573 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1575 key_info = WPA_GET_BE16(key->key_info);
1577 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
1578 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
1579 key_data_len, key_info,
1582 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
1584 key_info, ver, &gd);
1587 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1592 key_rsc = key->key_rsc;
1593 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1596 if (wpa_supplicant_install_gtk(sm, &gd, key->key_rsc, 0) ||
1597 wpa_supplicant_send_2_of_2(sm, key, ver, key_info))
1599 os_memset(&gd, 0, sizeof(gd));
1602 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Group rekeying "
1603 "completed with " MACSTR " [GTK=%s]",
1604 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1605 wpa_sm_cancel_auth_timeout(sm);
1606 wpa_sm_set_state(sm, WPA_COMPLETED);
1608 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1610 WPA_KEY_INFO_SECURE);
1613 wpa_sm_set_rekey_offload(sm);
1618 os_memset(&gd, 0, sizeof(gd));
1619 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1623 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
1624 struct wpa_eapol_key_192 *key,
1626 const u8 *buf, size_t len)
1628 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
1630 size_t mic_len = wpa_mic_len(sm->key_mgmt);
1632 os_memcpy(mic, key->key_mic, mic_len);
1634 os_memset(key->key_mic, 0, mic_len);
1635 wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len, sm->key_mgmt,
1636 ver, buf, len, key->key_mic);
1637 if (os_memcmp_const(mic, key->key_mic, mic_len) != 0) {
1638 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1639 "WPA: Invalid EAPOL-Key MIC "
1640 "when using TPTK - ignoring TPTK");
1645 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
1646 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
1650 if (!ok && sm->ptk_set) {
1651 os_memset(key->key_mic, 0, mic_len);
1652 wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len, sm->key_mgmt,
1653 ver, buf, len, key->key_mic);
1654 if (os_memcmp_const(mic, key->key_mic, mic_len) != 0) {
1655 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1656 "WPA: Invalid EAPOL-Key MIC - "
1664 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1665 "WPA: Could not verify EAPOL-Key MIC - "
1670 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1671 WPA_REPLAY_COUNTER_LEN);
1672 sm->rx_replay_counter_set = 1;
1677 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1678 static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
1679 struct wpa_eapol_key *key, u16 ver,
1680 u8 *key_data, size_t *key_data_len)
1682 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
1683 key_data, *key_data_len);
1685 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1686 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1691 /* Decrypt key data here so that this operation does not need
1692 * to be implemented separately for each message type. */
1693 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
1694 #ifdef CONFIG_NO_RC4
1695 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1696 "WPA: RC4 not supported in the build");
1698 #else /* CONFIG_NO_RC4 */
1700 os_memcpy(ek, key->key_iv, 16);
1701 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
1702 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
1703 os_memset(ek, 0, sizeof(ek));
1704 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1708 os_memset(ek, 0, sizeof(ek));
1709 #endif /* CONFIG_NO_RC4 */
1710 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1711 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
1712 sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
1713 wpa_key_mgmt_suite_b(sm->key_mgmt)) {
1715 if (*key_data_len < 8 || *key_data_len % 8) {
1716 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1717 "WPA: Unsupported AES-WRAP len %u",
1718 (unsigned int) *key_data_len);
1721 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
1722 buf = os_malloc(*key_data_len);
1724 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1725 "WPA: No memory for AES-UNWRAP buffer");
1728 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
1730 bin_clear_free(buf, *key_data_len);
1731 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1732 "WPA: AES unwrap failed - "
1733 "could not decrypt EAPOL-Key key data");
1736 os_memcpy(key_data, buf, *key_data_len);
1737 bin_clear_free(buf, *key_data_len);
1738 WPA_PUT_BE16(key->key_data_length, *key_data_len);
1740 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1741 "WPA: Unsupported key_info type %d", ver);
1744 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
1745 key_data, *key_data_len);
1751 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1752 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1754 void wpa_sm_aborted_cached(struct wpa_sm *sm)
1756 if (sm && sm->cur_pmksa) {
1757 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1758 "RSN: Cancelling PMKSA caching attempt");
1759 sm->cur_pmksa = NULL;
1764 static void wpa_eapol_key_dump(struct wpa_sm *sm,
1765 const struct wpa_eapol_key *key,
1766 unsigned int key_data_len,
1767 const u8 *mic, unsigned int mic_len)
1769 #ifndef CONFIG_NO_STDOUT_DEBUG
1770 u16 key_info = WPA_GET_BE16(key->key_info);
1772 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1773 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1774 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
1775 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1776 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1777 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1778 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1779 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1780 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1781 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1782 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1783 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1784 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1785 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1786 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1787 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1788 " key_length=%u key_data_length=%u",
1789 WPA_GET_BE16(key->key_length), key_data_len);
1790 wpa_hexdump(MSG_DEBUG, " replay_counter",
1791 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1792 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1793 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1794 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1795 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
1796 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
1797 #endif /* CONFIG_NO_STDOUT_DEBUG */
1802 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1803 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1804 * @src_addr: Source MAC address of the EAPOL packet
1805 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1806 * @len: Length of the EAPOL frame
1807 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1809 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1810 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1811 * only processing WPA and WPA2 EAPOL-Key frames.
1813 * The received EAPOL-Key packets are validated and valid packets are replied
1814 * to. In addition, key material (PTK, GTK) is configured at the end of a
1815 * successful key handshake.
1817 int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1818 const u8 *buf, size_t len)
1820 size_t plen, data_len, key_data_len;
1821 const struct ieee802_1x_hdr *hdr;
1822 struct wpa_eapol_key *key;
1823 struct wpa_eapol_key_192 *key192;
1827 struct wpa_peerkey *peerkey = NULL;
1829 size_t mic_len, keyhdrlen;
1831 #ifdef CONFIG_IEEE80211R
1832 sm->ft_completed = 0;
1833 #endif /* CONFIG_IEEE80211R */
1835 mic_len = wpa_mic_len(sm->key_mgmt);
1836 keyhdrlen = mic_len == 24 ? sizeof(*key192) : sizeof(*key);
1838 if (len < sizeof(*hdr) + keyhdrlen) {
1839 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1840 "WPA: EAPOL frame too short to be a WPA "
1841 "EAPOL-Key (len %lu, expecting at least %lu)",
1842 (unsigned long) len,
1843 (unsigned long) sizeof(*hdr) + keyhdrlen);
1847 hdr = (const struct ieee802_1x_hdr *) buf;
1848 plen = be_to_host16(hdr->length);
1849 data_len = plen + sizeof(*hdr);
1850 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1851 "IEEE 802.1X RX: version=%d type=%d length=%lu",
1852 hdr->version, hdr->type, (unsigned long) plen);
1854 if (hdr->version < EAPOL_VERSION) {
1855 /* TODO: backwards compatibility */
1857 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
1858 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1859 "WPA: EAPOL frame (type %u) discarded, "
1860 "not a Key frame", hdr->type);
1864 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
1865 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
1866 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1867 "WPA: EAPOL frame payload size %lu "
1868 "invalid (frame size %lu)",
1869 (unsigned long) plen, (unsigned long) len);
1873 if (data_len < len) {
1874 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1875 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
1876 (unsigned long) len - data_len);
1880 * Make a copy of the frame since we need to modify the buffer during
1881 * MAC validation and Key Data decryption.
1883 tmp = os_malloc(data_len);
1886 os_memcpy(tmp, buf, data_len);
1887 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
1888 key192 = (struct wpa_eapol_key_192 *)
1889 (tmp + sizeof(struct ieee802_1x_hdr));
1891 key_data = (u8 *) (key192 + 1);
1893 key_data = (u8 *) (key + 1);
1895 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
1897 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1898 "WPA: EAPOL-Key type (%d) unknown, discarded",
1905 key_data_len = WPA_GET_BE16(key192->key_data_length);
1907 key_data_len = WPA_GET_BE16(key->key_data_length);
1908 wpa_eapol_key_dump(sm, key, key_data_len, key192->key_mic, mic_len);
1910 if (key_data_len > plen - keyhdrlen) {
1911 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
1912 "frame - key_data overflow (%u > %u)",
1913 (unsigned int) key_data_len,
1914 (unsigned int) (plen - keyhdrlen));
1918 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
1919 key_info = WPA_GET_BE16(key->key_info);
1920 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1921 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
1922 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
1923 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1924 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
1925 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
1926 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1927 sm->key_mgmt != WPA_KEY_MGMT_OSEN) {
1928 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1929 "WPA: Unsupported EAPOL-Key descriptor version %d",
1934 if (sm->key_mgmt == WPA_KEY_MGMT_OSEN &&
1935 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1936 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1937 "OSEN: Unsupported EAPOL-Key descriptor version %d",
1942 if (wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1943 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1944 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1945 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
1950 #ifdef CONFIG_IEEE80211R
1951 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
1952 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
1953 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1954 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1955 "FT: AP did not use AES-128-CMAC");
1959 #endif /* CONFIG_IEEE80211R */
1960 #ifdef CONFIG_IEEE80211W
1961 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
1962 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1963 sm->key_mgmt != WPA_KEY_MGMT_OSEN &&
1964 !wpa_key_mgmt_suite_b(sm->key_mgmt)) {
1965 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1966 "WPA: AP did not use the "
1967 "negotiated AES-128-CMAC");
1971 #endif /* CONFIG_IEEE80211W */
1972 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
1973 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1974 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1975 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1976 "WPA: CCMP is used, but EAPOL-Key "
1977 "descriptor version (%d) is not 2", ver);
1978 if (sm->group_cipher != WPA_CIPHER_CCMP &&
1979 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1980 /* Earlier versions of IEEE 802.11i did not explicitly
1981 * require version 2 descriptor for all EAPOL-Key
1982 * packets, so allow group keys to use version 1 if
1983 * CCMP is not used for them. */
1984 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1985 "WPA: Backwards compatibility: allow invalid "
1986 "version for non-CCMP group keys");
1987 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1988 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1989 "WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
1992 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
1993 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1994 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1995 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1996 "WPA: GCMP is used, but EAPOL-Key "
1997 "descriptor version (%d) is not 2", ver);
2001 #ifdef CONFIG_PEERKEY
2002 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
2003 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
2007 if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
2008 if (!peerkey->initiator && peerkey->replay_counter_set &&
2009 os_memcmp(key->replay_counter, peerkey->replay_counter,
2010 WPA_REPLAY_COUNTER_LEN) <= 0) {
2011 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2012 "RSN: EAPOL-Key Replay Counter did not "
2013 "increase (STK) - dropping packet");
2015 } else if (peerkey->initiator) {
2016 u8 _tmp[WPA_REPLAY_COUNTER_LEN];
2017 os_memcpy(_tmp, key->replay_counter,
2018 WPA_REPLAY_COUNTER_LEN);
2019 inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
2020 if (os_memcmp(_tmp, peerkey->replay_counter,
2021 WPA_REPLAY_COUNTER_LEN) != 0) {
2022 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2023 "RSN: EAPOL-Key Replay "
2024 "Counter did not match (STK) - "
2031 if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
2032 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2033 "RSN: Ack bit in key_info from STK peer");
2036 #endif /* CONFIG_PEERKEY */
2038 if (!peerkey && sm->rx_replay_counter_set &&
2039 os_memcmp(key->replay_counter, sm->rx_replay_counter,
2040 WPA_REPLAY_COUNTER_LEN) <= 0) {
2041 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2042 "WPA: EAPOL-Key Replay Counter did not increase - "
2047 if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
2048 #ifdef CONFIG_PEERKEY
2049 && (peerkey == NULL || !peerkey->initiator)
2050 #endif /* CONFIG_PEERKEY */
2052 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2053 "WPA: No Ack bit in key_info");
2057 if (key_info & WPA_KEY_INFO_REQUEST) {
2058 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2059 "WPA: EAPOL-Key with Request bit - dropped");
2063 if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
2064 wpa_supplicant_verify_eapol_key_mic(sm, key192, ver, tmp, data_len))
2067 #ifdef CONFIG_PEERKEY
2068 if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
2069 peerkey_verify_eapol_key_mic(sm, peerkey, key192, ver, tmp,
2072 #endif /* CONFIG_PEERKEY */
2074 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
2075 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
2076 if (wpa_supplicant_decrypt_key_data(sm, key, ver, key_data,
2081 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
2082 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
2083 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2084 "WPA: Ignored EAPOL-Key (Pairwise) with "
2085 "non-zero key index");
2089 /* PeerKey 4-Way Handshake */
2090 peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver,
2091 key_data, key_data_len);
2092 } else if (key_info & WPA_KEY_INFO_MIC) {
2093 /* 3/4 4-Way Handshake */
2094 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
2097 /* 1/4 4-Way Handshake */
2098 wpa_supplicant_process_1_of_4(sm, src_addr, key,
2102 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
2103 /* PeerKey SMK Handshake */
2104 peerkey_rx_eapol_smk(sm, src_addr, key, key_data_len, key_info,
2107 if (key_info & WPA_KEY_INFO_MIC) {
2108 /* 1/2 Group Key Handshake */
2109 wpa_supplicant_process_1_of_2(sm, src_addr, key,
2110 key_data, key_data_len,
2113 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2114 "WPA: EAPOL-Key (Group) without Mic bit - "
2122 bin_clear_free(tmp, data_len);
2127 #ifdef CONFIG_CTRL_IFACE
2128 static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
2130 switch (sm->key_mgmt) {
2131 case WPA_KEY_MGMT_IEEE8021X:
2132 return ((sm->proto == WPA_PROTO_RSN ||
2133 sm->proto == WPA_PROTO_OSEN) ?
2134 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
2135 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
2136 case WPA_KEY_MGMT_PSK:
2137 return (sm->proto == WPA_PROTO_RSN ?
2138 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
2139 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
2140 #ifdef CONFIG_IEEE80211R
2141 case WPA_KEY_MGMT_FT_IEEE8021X:
2142 return RSN_AUTH_KEY_MGMT_FT_802_1X;
2143 case WPA_KEY_MGMT_FT_PSK:
2144 return RSN_AUTH_KEY_MGMT_FT_PSK;
2145 #endif /* CONFIG_IEEE80211R */
2146 #ifdef CONFIG_IEEE80211W
2147 case WPA_KEY_MGMT_IEEE8021X_SHA256:
2148 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
2149 case WPA_KEY_MGMT_PSK_SHA256:
2150 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
2151 #endif /* CONFIG_IEEE80211W */
2152 case WPA_KEY_MGMT_CCKM:
2153 return (sm->proto == WPA_PROTO_RSN ?
2154 RSN_AUTH_KEY_MGMT_CCKM:
2155 WPA_AUTH_KEY_MGMT_CCKM);
2156 case WPA_KEY_MGMT_WPA_NONE:
2157 return WPA_AUTH_KEY_MGMT_NONE;
2158 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
2159 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
2160 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
2161 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
2168 #define RSN_SUITE "%02x-%02x-%02x-%d"
2169 #define RSN_SUITE_ARG(s) \
2170 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2173 * wpa_sm_get_mib - Dump text list of MIB entries
2174 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2175 * @buf: Buffer for the list
2176 * @buflen: Length of the buffer
2177 * Returns: Number of bytes written to buffer
2179 * This function is used fetch dot11 MIB variables.
2181 int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
2183 char pmkid_txt[PMKID_LEN * 2 + 1];
2187 if (sm->cur_pmksa) {
2188 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2189 sm->cur_pmksa->pmkid, PMKID_LEN);
2191 pmkid_txt[0] = '\0';
2193 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
2194 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
2195 sm->proto == WPA_PROTO_RSN)
2200 ret = os_snprintf(buf, buflen,
2201 "dot11RSNAOptionImplemented=TRUE\n"
2202 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2203 "dot11RSNAEnabled=%s\n"
2204 "dot11RSNAPreauthenticationEnabled=%s\n"
2205 "dot11RSNAConfigVersion=%d\n"
2206 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2207 "dot11RSNAConfigGroupCipherSize=%d\n"
2208 "dot11RSNAConfigPMKLifetime=%d\n"
2209 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2210 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2211 "dot11RSNAConfigSATimeout=%d\n",
2212 rsna ? "TRUE" : "FALSE",
2213 rsna ? "TRUE" : "FALSE",
2215 wpa_cipher_key_len(sm->group_cipher) * 8,
2216 sm->dot11RSNAConfigPMKLifetime,
2217 sm->dot11RSNAConfigPMKReauthThreshold,
2218 sm->dot11RSNAConfigSATimeout);
2219 if (os_snprintf_error(buflen, ret))
2224 buf + len, buflen - len,
2225 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2226 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2227 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2228 "dot11RSNAPMKIDUsed=%s\n"
2229 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2230 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2231 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2232 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2233 "dot11RSNA4WayHandshakeFailures=%u\n",
2234 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
2235 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2236 sm->pairwise_cipher)),
2237 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2240 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
2241 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2242 sm->pairwise_cipher)),
2243 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2245 sm->dot11RSNA4WayHandshakeFailures);
2246 if (!os_snprintf_error(buflen - len, ret))
2251 #endif /* CONFIG_CTRL_IFACE */
2254 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
2255 void *ctx, enum pmksa_free_reason reason)
2257 struct wpa_sm *sm = ctx;
2260 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2261 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2263 if (sm->cur_pmksa == entry) {
2264 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2265 "RSN: %s current PMKSA entry",
2266 reason == PMKSA_REPLACE ? "replaced" : "removed");
2267 pmksa_cache_clear_current(sm);
2270 * If an entry is simply being replaced, there's no need to
2271 * deauthenticate because it will be immediately re-added.
2272 * This happens when EAP authentication is completed again
2273 * (reauth or failed PMKSA caching attempt).
2275 if (reason != PMKSA_REPLACE)
2279 if (reason == PMKSA_EXPIRE &&
2280 (sm->pmk_len == entry->pmk_len &&
2281 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2282 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2283 "RSN: deauthenticating due to expired PMK");
2284 pmksa_cache_clear_current(sm);
2289 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2290 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2296 * wpa_sm_init - Initialize WPA state machine
2297 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2298 * Returns: Pointer to the allocated WPA state machine data
2300 * This function is used to allocate a new WPA state machine and the returned
2301 * value is passed to all WPA state machine calls.
2303 struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2307 sm = os_zalloc(sizeof(*sm));
2310 dl_list_init(&sm->pmksa_candidates);
2311 sm->renew_snonce = 1;
2314 sm->dot11RSNAConfigPMKLifetime = 43200;
2315 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2316 sm->dot11RSNAConfigSATimeout = 60;
2318 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2319 if (sm->pmksa == NULL) {
2320 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2321 "RSN: PMKSA cache initialization failed");
2331 * wpa_sm_deinit - Deinitialize WPA state machine
2332 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2334 void wpa_sm_deinit(struct wpa_sm *sm)
2338 pmksa_cache_deinit(sm->pmksa);
2339 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2340 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2341 os_free(sm->assoc_wpa_ie);
2342 os_free(sm->ap_wpa_ie);
2343 os_free(sm->ap_rsn_ie);
2347 #ifdef CONFIG_IEEE80211R
2348 os_free(sm->assoc_resp_ies);
2349 #endif /* CONFIG_IEEE80211R */
2350 #ifdef CONFIG_TESTING_OPTIONS
2351 wpabuf_free(sm->test_assoc_ie);
2352 #endif /* CONFIG_TESTING_OPTIONS */
2358 * wpa_sm_notify_assoc - Notify WPA state machine about association
2359 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2360 * @bssid: The BSSID of the new association
2362 * This function is called to let WPA state machine know that the connection
2365 void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2372 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2373 "WPA: Association event - clear replay counter");
2374 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2375 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2376 sm->rx_replay_counter_set = 0;
2377 sm->renew_snonce = 1;
2378 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2379 rsn_preauth_deinit(sm);
2381 #ifdef CONFIG_IEEE80211R
2382 if (wpa_ft_is_completed(sm)) {
2384 * Clear portValid to kick EAPOL state machine to re-enter
2385 * AUTHENTICATED state to get the EAPOL port Authorized.
2387 eapol_sm_notify_portValid(sm->eapol, FALSE);
2388 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2390 /* Prepare for the next transition */
2391 wpa_ft_prepare_auth_request(sm, NULL);
2395 #endif /* CONFIG_IEEE80211R */
2399 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2400 * this is not part of a Fast BSS Transition.
2402 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
2404 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2406 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2407 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
2408 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
2409 #ifdef CONFIG_IEEE80211W
2410 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
2411 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
2412 #endif /* CONFIG_IEEE80211W */
2417 #endif /* CONFIG_TDLS */
2420 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
2421 #endif /* CONFIG_P2P */
2426 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2427 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2429 * This function is called to let WPA state machine know that the connection
2430 * was lost. This will abort any existing pre-authentication session.
2432 void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2434 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2435 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2437 rsn_preauth_deinit(sm);
2438 pmksa_cache_clear_current(sm);
2439 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2440 sm->dot11RSNA4WayHandshakeFailures++;
2442 wpa_tdls_disassoc(sm);
2443 #endif /* CONFIG_TDLS */
2444 #ifdef CONFIG_IEEE80211R
2445 sm->ft_reassoc_completed = 0;
2446 #endif /* CONFIG_IEEE80211R */
2448 /* Keys are not needed in the WPA state machine anymore */
2451 sm->msg_3_of_4_ok = 0;
2456 * wpa_sm_set_pmk - Set PMK
2457 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2459 * @pmk_len: The length of the new PMK in bytes
2460 * @pmkid: Calculated PMKID
2461 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
2463 * Configure the PMK for WPA state machine.
2465 void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
2466 const u8 *pmkid, const u8 *bssid)
2471 sm->pmk_len = pmk_len;
2472 os_memcpy(sm->pmk, pmk, pmk_len);
2474 #ifdef CONFIG_IEEE80211R
2475 /* Set XXKey to be PSK for FT key derivation */
2476 sm->xxkey_len = pmk_len;
2477 os_memcpy(sm->xxkey, pmk, pmk_len);
2478 #endif /* CONFIG_IEEE80211R */
2481 pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
2482 bssid, sm->own_addr,
2483 sm->network_ctx, sm->key_mgmt);
2489 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2490 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2492 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2495 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2500 if (sm->cur_pmksa) {
2501 sm->pmk_len = sm->cur_pmksa->pmk_len;
2502 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2504 sm->pmk_len = PMK_LEN;
2505 os_memset(sm->pmk, 0, PMK_LEN);
2511 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2512 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2513 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2515 void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2518 sm->fast_reauth = fast_reauth;
2523 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2524 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2525 * @scard_ctx: Context pointer for smartcard related callback functions
2527 void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2531 sm->scard_ctx = scard_ctx;
2532 if (sm->preauth_eapol)
2533 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2538 * wpa_sm_set_config - Notification of current configration change
2539 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2540 * @config: Pointer to current network configuration
2542 * Notify WPA state machine that configuration has changed. config will be
2543 * stored as a backpointer to network configuration. This can be %NULL to clear
2544 * the stored pointed.
2546 void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2552 sm->network_ctx = config->network_ctx;
2553 sm->peerkey_enabled = config->peerkey_enabled;
2554 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2555 sm->proactive_key_caching = config->proactive_key_caching;
2556 sm->eap_workaround = config->eap_workaround;
2557 sm->eap_conf_ctx = config->eap_conf_ctx;
2559 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2560 sm->ssid_len = config->ssid_len;
2563 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
2564 sm->p2p = config->p2p;
2565 sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
2567 sm->network_ctx = NULL;
2568 sm->peerkey_enabled = 0;
2569 sm->allowed_pairwise_cipher = 0;
2570 sm->proactive_key_caching = 0;
2571 sm->eap_workaround = 0;
2572 sm->eap_conf_ctx = NULL;
2574 sm->wpa_ptk_rekey = 0;
2576 sm->wpa_rsc_relaxation = 0;
2582 * wpa_sm_set_own_addr - Set own MAC address
2583 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2584 * @addr: Own MAC address
2586 void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2589 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2594 * wpa_sm_set_ifname - Set network interface name
2595 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2596 * @ifname: Interface name
2597 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2599 void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2600 const char *bridge_ifname)
2603 sm->ifname = ifname;
2604 sm->bridge_ifname = bridge_ifname;
2610 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2611 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2612 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2614 void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2622 * wpa_sm_set_param - Set WPA state machine parameters
2623 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2624 * @param: Parameter field
2625 * @value: Parameter value
2626 * Returns: 0 on success, -1 on failure
2628 int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2637 case RSNA_PMK_LIFETIME:
2639 sm->dot11RSNAConfigPMKLifetime = value;
2643 case RSNA_PMK_REAUTH_THRESHOLD:
2644 if (value > 0 && value <= 100)
2645 sm->dot11RSNAConfigPMKReauthThreshold = value;
2649 case RSNA_SA_TIMEOUT:
2651 sm->dot11RSNAConfigSATimeout = value;
2655 case WPA_PARAM_PROTO:
2658 case WPA_PARAM_PAIRWISE:
2659 sm->pairwise_cipher = value;
2661 case WPA_PARAM_GROUP:
2662 sm->group_cipher = value;
2664 case WPA_PARAM_KEY_MGMT:
2665 sm->key_mgmt = value;
2667 #ifdef CONFIG_IEEE80211W
2668 case WPA_PARAM_MGMT_GROUP:
2669 sm->mgmt_group_cipher = value;
2671 #endif /* CONFIG_IEEE80211W */
2672 case WPA_PARAM_RSN_ENABLED:
2673 sm->rsn_enabled = value;
2687 * wpa_sm_get_status - Get WPA state machine
2688 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2689 * @buf: Buffer for status information
2690 * @buflen: Maximum buffer length
2691 * @verbose: Whether to include verbose status information
2692 * Returns: Number of bytes written to buf.
2694 * Query WPA state machine for status information. This function fills in
2695 * a text area with current status information. If the buffer (buf) is not
2696 * large enough, status information will be truncated to fit the buffer.
2698 int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2701 char *pos = buf, *end = buf + buflen;
2704 ret = os_snprintf(pos, end - pos,
2705 "pairwise_cipher=%s\n"
2708 wpa_cipher_txt(sm->pairwise_cipher),
2709 wpa_cipher_txt(sm->group_cipher),
2710 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
2711 if (os_snprintf_error(end - pos, ret))
2715 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
2716 struct wpa_ie_data rsn;
2717 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
2719 rsn.capabilities & (WPA_CAPABILITY_MFPR |
2720 WPA_CAPABILITY_MFPC)) {
2721 ret = os_snprintf(pos, end - pos, "pmf=%d\n",
2723 WPA_CAPABILITY_MFPR) ? 2 : 1);
2724 if (os_snprintf_error(end - pos, ret))
2734 int wpa_sm_pmf_enabled(struct wpa_sm *sm)
2736 struct wpa_ie_data rsn;
2738 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
2741 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
2742 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
2750 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2751 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2752 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2753 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2754 * Returns: 0 on success, -1 on failure
2756 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2764 #ifdef CONFIG_TESTING_OPTIONS
2765 if (sm->test_assoc_ie) {
2766 wpa_printf(MSG_DEBUG,
2767 "TESTING: Replace association WPA/RSN IE");
2768 if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
2770 os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
2771 wpabuf_len(sm->test_assoc_ie));
2772 res = wpabuf_len(sm->test_assoc_ie);
2774 #endif /* CONFIG_TESTING_OPTIONS */
2775 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2780 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2781 wpa_ie, *wpa_ie_len);
2783 if (sm->assoc_wpa_ie == NULL) {
2785 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2786 * the correct version of the IE even if PMKSA caching is
2787 * aborted (which would remove PMKID from IE generation).
2789 sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2790 if (sm->assoc_wpa_ie == NULL)
2793 os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2794 sm->assoc_wpa_ie_len = *wpa_ie_len;
2802 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2803 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2804 * @ie: Pointer to IE data (starting from id)
2806 * Returns: 0 on success, -1 on failure
2808 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2809 * Request frame. The IE will be used to override the default value generated
2810 * with wpa_sm_set_assoc_wpa_ie_default().
2812 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2817 os_free(sm->assoc_wpa_ie);
2818 if (ie == NULL || len == 0) {
2819 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2820 "WPA: clearing own WPA/RSN IE");
2821 sm->assoc_wpa_ie = NULL;
2822 sm->assoc_wpa_ie_len = 0;
2824 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2825 sm->assoc_wpa_ie = os_malloc(len);
2826 if (sm->assoc_wpa_ie == NULL)
2829 os_memcpy(sm->assoc_wpa_ie, ie, len);
2830 sm->assoc_wpa_ie_len = len;
2838 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2839 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2840 * @ie: Pointer to IE data (starting from id)
2842 * Returns: 0 on success, -1 on failure
2844 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2847 int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2852 os_free(sm->ap_wpa_ie);
2853 if (ie == NULL || len == 0) {
2854 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2855 "WPA: clearing AP WPA IE");
2856 sm->ap_wpa_ie = NULL;
2857 sm->ap_wpa_ie_len = 0;
2859 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2860 sm->ap_wpa_ie = os_malloc(len);
2861 if (sm->ap_wpa_ie == NULL)
2864 os_memcpy(sm->ap_wpa_ie, ie, len);
2865 sm->ap_wpa_ie_len = len;
2873 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2874 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2875 * @ie: Pointer to IE data (starting from id)
2877 * Returns: 0 on success, -1 on failure
2879 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2882 int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2887 os_free(sm->ap_rsn_ie);
2888 if (ie == NULL || len == 0) {
2889 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2890 "WPA: clearing AP RSN IE");
2891 sm->ap_rsn_ie = NULL;
2892 sm->ap_rsn_ie_len = 0;
2894 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2895 sm->ap_rsn_ie = os_malloc(len);
2896 if (sm->ap_rsn_ie == NULL)
2899 os_memcpy(sm->ap_rsn_ie, ie, len);
2900 sm->ap_rsn_ie_len = len;
2908 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2909 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2910 * @data: Pointer to data area for parsing results
2911 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2913 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2914 * parsed data into data.
2916 int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2921 if (sm->assoc_wpa_ie == NULL) {
2922 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2923 "WPA: No WPA/RSN IE available from association info");
2926 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
2932 int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
2934 return pmksa_cache_list(sm->pmksa, buf, len);
2938 void wpa_sm_drop_sa(struct wpa_sm *sm)
2940 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
2943 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2944 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2945 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2946 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
2947 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
2948 #ifdef CONFIG_IEEE80211W
2949 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
2950 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
2951 #endif /* CONFIG_IEEE80211W */
2952 #ifdef CONFIG_IEEE80211R
2953 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
2954 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
2955 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
2956 #endif /* CONFIG_IEEE80211R */
2960 int wpa_sm_has_ptk(struct wpa_sm *sm)
2968 void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
2970 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
2974 void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
2976 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
2981 int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
2984 u8 keylen; /* plaintext key len */
2987 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
2988 struct wpa_gtk_data gd;
2990 os_memset(&gd, 0, sizeof(gd));
2991 keylen = wpa_cipher_key_len(sm->group_cipher);
2992 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
2993 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
2994 if (gd.alg == WPA_ALG_NONE) {
2995 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
3000 keyinfo = WPA_GET_LE16(buf + 2);
3001 gd.gtk_len = keylen;
3002 if (gd.gtk_len != buf[4]) {
3003 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
3004 gd.gtk_len, buf[4]);
3007 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
3008 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
3009 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
3011 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
3013 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
3014 gd.gtk, gd.gtk_len);
3015 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) {
3016 os_memset(&gd, 0, sizeof(gd));
3017 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
3021 os_memset(&gd, 0, sizeof(gd));
3022 #ifdef CONFIG_IEEE80211W
3023 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
3024 const struct wpa_igtk_kde *igtk;
3026 igtk = (const struct wpa_igtk_kde *) (buf + 2);
3027 if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0)
3029 #endif /* CONFIG_IEEE80211W */
3031 wpa_printf(MSG_DEBUG, "Unknown element id");
3037 #endif /* CONFIG_WNM */
3040 #ifdef CONFIG_PEERKEY
3041 int wpa_sm_rx_eapol_peerkey(struct wpa_sm *sm, const u8 *src_addr,
3042 const u8 *buf, size_t len)
3044 struct wpa_peerkey *peerkey;
3046 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
3047 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
3054 wpa_sm_rx_eapol(sm, src_addr, buf, len);
3058 #endif /* CONFIG_PEERKEY */
3063 int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
3065 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
3067 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
3071 #endif /* CONFIG_P2P */
3074 void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
3076 if (rx_replay_counter == NULL)
3079 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
3080 WPA_REPLAY_COUNTER_LEN);
3081 sm->rx_replay_counter_set = 1;
3082 wpa_printf(MSG_DEBUG, "Updated key replay counter");
3086 void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
3087 const u8 *ptk_kck, size_t ptk_kck_len,
3088 const u8 *ptk_kek, size_t ptk_kek_len)
3090 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
3091 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
3092 sm->ptk.kck_len = ptk_kck_len;
3093 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
3095 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
3096 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
3097 sm->ptk.kek_len = ptk_kek_len;
3098 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
3104 #ifdef CONFIG_TESTING_OPTIONS
3105 void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
3107 wpabuf_free(sm->test_assoc_ie);
3108 sm->test_assoc_ie = buf;
3110 #endif /* CONFIG_TESTING_OPTIONS */