blob: 17a79f927cf61816e70e4f892a6446ddcd9d224a [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - WPA state machine and EAPOL-Key processing
Roshan Pius3a1667e2018-07-03 15:17:14 -07003 * Copyright (c) 2003-2018, Jouni Malinen <j@w1.fi>
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004 * Copyright(c) 2015 Intel Deutschland GmbH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008 */
9
10#include "includes.h"
11
12#include "common.h"
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080013#include "crypto/aes.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014#include "crypto/aes_wrap.h"
15#include "crypto/crypto.h"
16#include "crypto/random.h"
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080017#include "crypto/aes_siv.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070018#include "crypto/sha256.h"
19#include "crypto/sha384.h"
20#include "crypto/sha512.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070021#include "common/ieee802_11_defs.h"
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080022#include "common/ieee802_11_common.h"
Hai Shalom74f70d42019-02-11 14:42:39 -080023#include "common/ocv.h"
Hai Shalom4fbc08f2020-05-18 12:37:00 -070024#include "common/dpp.h"
Hai Shalom899fcc72020-10-19 14:38:18 -070025#include "common/wpa_ctrl.h"
Paul Stewart092955c2017-02-06 09:13:09 -080026#include "eap_common/eap_defs.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027#include "eapol_supp/eapol_supp_sm.h"
Hai Shalom74f70d42019-02-11 14:42:39 -080028#include "drivers/driver.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070029#include "wpa.h"
30#include "eloop.h"
31#include "preauth.h"
32#include "pmksa_cache.h"
33#include "wpa_i.h"
34#include "wpa_ie.h"
Mir Alieaaf04e2021-06-07 12:17:29 +053035#include "wpa_supplicant_i.h"
36#include "driver_i.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080038static const u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
39
40
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070041/**
42 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
43 * @sm: Pointer to WPA state machine data from wpa_sm_init()
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080044 * @ptk: PTK for Key Confirmation/Encryption Key
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045 * @ver: Version field from Key Info
46 * @dest: Destination address for the frame
47 * @proto: Ethertype (usually ETH_P_EAPOL)
48 * @msg: EAPOL-Key message
49 * @msg_len: Length of message
50 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080051 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070052 */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080053int wpa_eapol_key_send(struct wpa_sm *sm, struct wpa_ptk *ptk,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080054 int ver, const u8 *dest, u16 proto,
55 u8 *msg, size_t msg_len, u8 *key_mic)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070056{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080057 int ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070058 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -080059
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070060 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL-Key frame to " MACSTR
61 " ver=%d mic_len=%d key_mgmt=0x%x",
62 MAC2STR(dest), ver, (int) mic_len, sm->key_mgmt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070063 if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
64 /*
65 * Association event was not yet received; try to fetch
66 * BSSID from the driver.
67 */
68 if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
69 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
70 "WPA: Failed to read BSSID for "
71 "EAPOL-Key destination address");
72 } else {
73 dest = sm->bssid;
74 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
75 "WPA: Use BSSID (" MACSTR
76 ") as the destination for EAPOL-Key",
77 MAC2STR(dest));
78 }
79 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080080
81 if (mic_len) {
82 if (key_mic && (!ptk || !ptk->kck_len))
83 goto out;
84
85 if (key_mic &&
86 wpa_eapol_key_mic(ptk->kck, ptk->kck_len, sm->key_mgmt, ver,
87 msg, msg_len, key_mic)) {
88 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
89 "WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC",
90 ver, sm->key_mgmt);
91 goto out;
92 }
Dmitry Shmidt29333592017-01-09 12:27:11 -080093 if (ptk)
94 wpa_hexdump_key(MSG_DEBUG, "WPA: KCK",
95 ptk->kck, ptk->kck_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080096 wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC",
97 key_mic, mic_len);
98 } else {
99#ifdef CONFIG_FILS
100 /* AEAD cipher - Key MIC field not used */
101 struct ieee802_1x_hdr *s_hdr, *hdr;
102 struct wpa_eapol_key *s_key, *key;
103 u8 *buf, *s_key_data, *key_data;
104 size_t buf_len = msg_len + AES_BLOCK_SIZE;
105 size_t key_data_len;
106 u16 eapol_len;
107 const u8 *aad[1];
108 size_t aad_len[1];
109
110 if (!ptk || !ptk->kek_len)
111 goto out;
112
113 key_data_len = msg_len - sizeof(struct ieee802_1x_hdr) -
114 sizeof(struct wpa_eapol_key) - 2;
115
116 buf = os_malloc(buf_len);
117 if (!buf)
118 goto out;
119
120 os_memcpy(buf, msg, msg_len);
121 hdr = (struct ieee802_1x_hdr *) buf;
122 key = (struct wpa_eapol_key *) (hdr + 1);
123 key_data = ((u8 *) (key + 1)) + 2;
124
125 /* Update EAPOL header to include AES-SIV overhead */
126 eapol_len = be_to_host16(hdr->length);
127 eapol_len += AES_BLOCK_SIZE;
128 hdr->length = host_to_be16(eapol_len);
129
130 /* Update Key Data Length field to include AES-SIV overhead */
131 WPA_PUT_BE16((u8 *) (key + 1), AES_BLOCK_SIZE + key_data_len);
132
133 s_hdr = (struct ieee802_1x_hdr *) msg;
134 s_key = (struct wpa_eapol_key *) (s_hdr + 1);
135 s_key_data = ((u8 *) (s_key + 1)) + 2;
136
137 wpa_hexdump_key(MSG_DEBUG, "WPA: Plaintext Key Data",
138 s_key_data, key_data_len);
139
140 wpa_hexdump_key(MSG_DEBUG, "WPA: KEK", ptk->kek, ptk->kek_len);
141 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
142 * to Key Data (exclusive). */
143 aad[0] = buf;
144 aad_len[0] = key_data - buf;
145 if (aes_siv_encrypt(ptk->kek, ptk->kek_len,
146 s_key_data, key_data_len,
147 1, aad, aad_len, key_data) < 0) {
148 os_free(buf);
149 goto out;
150 }
151
152 wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV",
153 key_data, AES_BLOCK_SIZE + key_data_len);
154
155 os_free(msg);
156 msg = buf;
157 msg_len = buf_len;
158#else /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700159 goto out;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800160#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700161 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800162
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700163 wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800164 ret = wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700165 eapol_sm_notify_tx_eapol_key(sm->eapol);
166out:
167 os_free(msg);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800168 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700169}
170
171
172/**
173 * wpa_sm_key_request - Send EAPOL-Key Request
174 * @sm: Pointer to WPA state machine data from wpa_sm_init()
175 * @error: Indicate whether this is an Michael MIC error report
176 * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
177 *
178 * Send an EAPOL-Key Request to the current authenticator. This function is
179 * used to request rekeying and it is usually called when a local Michael MIC
180 * failure is detected.
181 */
182void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
183{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800184 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700185 struct wpa_eapol_key *reply;
186 int key_info, ver;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800187 u8 bssid[ETH_ALEN], *rbuf, *key_mic, *mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700188
Hai Shalomfdcde762020-04-02 11:19:20 -0700189 if (pairwise && sm->wpa_deny_ptk0_rekey && !sm->use_ext_key_id &&
Sunil8cd6f4d2022-06-28 18:40:46 +0000190 wpa_sm_get_state(sm) == WPA_COMPLETED && !error) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700191 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
192 "WPA: PTK0 rekey not allowed, reconnecting");
193 wpa_sm_reconnect(sm);
194 return;
195 }
196
Roshan Pius3a1667e2018-07-03 15:17:14 -0700197 if (wpa_use_akm_defined(sm->key_mgmt))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800198 ver = WPA_KEY_INFO_TYPE_AKM_DEFINED;
199 else if (wpa_key_mgmt_ft(sm->key_mgmt) ||
200 wpa_key_mgmt_sha256(sm->key_mgmt))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700201 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700202 else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700203 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
204 else
205 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
206
207 if (wpa_sm_get_bssid(sm, bssid) < 0) {
208 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
209 "Failed to read BSSID for EAPOL-Key request");
210 return;
211 }
212
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700213 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800214 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800216 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700217 if (rbuf == NULL)
218 return;
219
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800220 reply->type = (sm->proto == WPA_PROTO_RSN ||
221 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
223 key_info = WPA_KEY_INFO_REQUEST | ver;
224 if (sm->ptk_set)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800225 key_info |= WPA_KEY_INFO_SECURE;
226 if (sm->ptk_set && mic_len)
227 key_info |= WPA_KEY_INFO_MIC;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700228 if (error)
229 key_info |= WPA_KEY_INFO_ERROR;
230 if (pairwise)
231 key_info |= WPA_KEY_INFO_KEY_TYPE;
232 WPA_PUT_BE16(reply->key_info, key_info);
233 WPA_PUT_BE16(reply->key_length, 0);
234 os_memcpy(reply->replay_counter, sm->request_counter,
235 WPA_REPLAY_COUNTER_LEN);
236 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
237
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800238 mic = (u8 *) (reply + 1);
239 WPA_PUT_BE16(mic + mic_len, 0);
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800240 if (!(key_info & WPA_KEY_INFO_MIC))
241 key_mic = NULL;
242 else
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800243 key_mic = mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700244
245 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
246 "WPA: Sending EAPOL-Key Request (error=%d "
247 "pairwise=%d ptk_set=%d len=%lu)",
248 error, pairwise, sm->ptk_set, (unsigned long) rlen);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800249 wpa_eapol_key_send(sm, &sm->ptk, ver, bssid, ETH_P_EAPOL, rbuf, rlen,
250 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700251}
252
253
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800254static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm *sm)
255{
256#ifdef CONFIG_IEEE80211R
257 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
258 if (wpa_sm_key_mgmt_set_pmk(sm, sm->xxkey, sm->xxkey_len))
259 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
260 "RSN: Cannot set low order 256 bits of MSK for key management offload");
261 } else {
262#endif /* CONFIG_IEEE80211R */
263 if (wpa_sm_key_mgmt_set_pmk(sm, sm->pmk, sm->pmk_len))
264 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
265 "RSN: Cannot set PMK for key management offload");
266#ifdef CONFIG_IEEE80211R
267 }
268#endif /* CONFIG_IEEE80211R */
269}
270
271
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700272static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
273 const unsigned char *src_addr,
274 const u8 *pmkid)
275{
276 int abort_cached = 0;
277
278 if (pmkid && !sm->cur_pmksa) {
279 /* When using drivers that generate RSN IE, wpa_supplicant may
280 * not have enough time to get the association information
281 * event before receiving this 1/4 message, so try to find a
282 * matching PMKSA cache entry here. */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800283 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700284 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700285 if (sm->cur_pmksa) {
286 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
287 "RSN: found matching PMKID from PMKSA cache");
288 } else {
289 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
290 "RSN: no matching PMKID found");
291 abort_cached = 1;
292 }
293 }
294
295 if (pmkid && sm->cur_pmksa &&
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700296 os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700297 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
298 wpa_sm_set_pmk_from_pmksa(sm);
299 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
300 sm->pmk, sm->pmk_len);
301 eapol_sm_notify_cached(sm->eapol);
302#ifdef CONFIG_IEEE80211R
303 sm->xxkey_len = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700304#ifdef CONFIG_SAE
305 if (sm->key_mgmt == WPA_KEY_MGMT_FT_SAE &&
306 sm->pmk_len == PMK_LEN) {
307 /* Need to allow FT key derivation to proceed with
308 * PMK from SAE being used as the XXKey in cases where
309 * the PMKID in msg 1/4 matches the PMKSA entry that was
310 * just added based on SAE authentication for the
311 * initial mobility domain association. */
312 os_memcpy(sm->xxkey, sm->pmk, sm->pmk_len);
313 sm->xxkey_len = sm->pmk_len;
314 }
315#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700316#endif /* CONFIG_IEEE80211R */
317 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
318 int res, pmk_len;
Hai Shalom81f62d82019-07-22 12:10:00 -0700319#ifdef CONFIG_IEEE80211R
320 u8 buf[2 * PMK_LEN];
321#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800322
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800323 if (wpa_key_mgmt_sha384(sm->key_mgmt))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800324 pmk_len = PMK_LEN_SUITE_B_192;
325 else
326 pmk_len = PMK_LEN;
327 res = eapol_sm_get_key(sm->eapol, sm->pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700328 if (res) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800329 if (pmk_len == PMK_LEN) {
330 /*
331 * EAP-LEAP is an exception from other EAP
332 * methods: it uses only 16-byte PMK.
333 */
334 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
335 pmk_len = 16;
336 }
Hai Shalomf1c97642019-07-19 23:42:07 +0000337 }
Hai Shalom81f62d82019-07-22 12:10:00 -0700338#ifdef CONFIG_IEEE80211R
339 if (res == 0 &&
340 eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0) {
341 if (wpa_key_mgmt_sha384(sm->key_mgmt)) {
342 os_memcpy(sm->xxkey, buf, SHA384_MAC_LEN);
343 sm->xxkey_len = SHA384_MAC_LEN;
344 } else {
345 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
346 sm->xxkey_len = PMK_LEN;
347 }
348 forced_memzero(buf, sizeof(buf));
349 if (sm->proto == WPA_PROTO_RSN &&
350 wpa_key_mgmt_ft(sm->key_mgmt)) {
351 struct rsn_pmksa_cache_entry *sa = NULL;
352 const u8 *fils_cache_id = NULL;
353
354#ifdef CONFIG_FILS
355 if (sm->fils_cache_id_set)
356 fils_cache_id = sm->fils_cache_id;
357#endif /* CONFIG_FILS */
358 wpa_hexdump_key(MSG_DEBUG,
359 "FT: Cache XXKey/MPMK",
360 sm->xxkey, sm->xxkey_len);
361 sa = pmksa_cache_add(sm->pmksa,
362 sm->xxkey, sm->xxkey_len,
363 NULL, NULL, 0,
364 src_addr, sm->own_addr,
365 sm->network_ctx,
366 sm->key_mgmt,
367 fils_cache_id);
368 if (!sm->cur_pmksa)
369 sm->cur_pmksa = sa;
370 }
371 }
372#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700373 if (res == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700374 struct rsn_pmksa_cache_entry *sa = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700375 const u8 *fils_cache_id = NULL;
376
377#ifdef CONFIG_FILS
378 if (sm->fils_cache_id_set)
379 fils_cache_id = sm->fils_cache_id;
380#endif /* CONFIG_FILS */
381
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700382 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
383 "machines", sm->pmk, pmk_len);
384 sm->pmk_len = pmk_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800385 wpa_supplicant_key_mgmt_set_pmk(sm);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700386 if (sm->proto == WPA_PROTO_RSN &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800387 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700388 !wpa_key_mgmt_ft(sm->key_mgmt)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700389 sa = pmksa_cache_add(sm->pmksa,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800390 sm->pmk, pmk_len, NULL,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800391 NULL, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700392 src_addr, sm->own_addr,
393 sm->network_ctx,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700394 sm->key_mgmt,
395 fils_cache_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700396 }
397 if (!sm->cur_pmksa && pmkid &&
Roshan Pius3a1667e2018-07-03 15:17:14 -0700398 pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL,
399 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700400 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
401 "RSN: the new PMK matches with the "
402 "PMKID");
403 abort_cached = 0;
Jouni Malinen6ec30382015-07-08 20:48:18 +0300404 } else if (sa && !sm->cur_pmksa && pmkid) {
405 /*
406 * It looks like the authentication server
407 * derived mismatching MSK. This should not
408 * really happen, but bugs happen.. There is not
409 * much we can do here without knowing what
410 * exactly caused the server to misbehave.
411 */
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800412 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Jouni Malinen6ec30382015-07-08 20:48:18 +0300413 "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
414 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700415 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700416
417 if (!sm->cur_pmksa)
418 sm->cur_pmksa = sa;
Hai Shalom81f62d82019-07-22 12:10:00 -0700419#ifdef CONFIG_IEEE80211R
420 } else if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->ft_protocol) {
421 wpa_printf(MSG_DEBUG,
422 "FT: Continue 4-way handshake without PMK/PMKID for association using FT protocol");
423#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700424 } else {
425 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
426 "WPA: Failed to get master session key from "
427 "EAPOL state machines - key handshake "
428 "aborted");
429 if (sm->cur_pmksa) {
430 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
431 "RSN: Cancelled PMKSA caching "
432 "attempt");
433 sm->cur_pmksa = NULL;
434 abort_cached = 1;
435 } else if (!abort_cached) {
436 return -1;
437 }
438 }
439 }
440
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700441 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800442 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800443 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
444 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700445 /* Send EAPOL-Start to trigger full EAP authentication. */
446 u8 *buf;
447 size_t buflen;
448
449 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
450 "RSN: no PMKSA entry found - trigger "
451 "full EAP authentication");
452 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
453 NULL, 0, &buflen, NULL);
454 if (buf) {
Hai Shalomc1a21442022-02-04 13:43:00 -0800455 /* Set and reset eapFail to allow EAP state machine to
456 * proceed with new authentication. */
457 eapol_sm_notify_eap_fail(sm->eapol, true);
458 eapol_sm_notify_eap_fail(sm->eapol, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700459 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
460 buf, buflen);
461 os_free(buf);
462 return -2;
463 }
464
465 return -1;
466 }
467
468 return 0;
469}
470
471
472/**
473 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
474 * @sm: Pointer to WPA state machine data from wpa_sm_init()
475 * @dst: Destination address for the frame
476 * @key: Pointer to the EAPOL-Key frame header
477 * @ver: Version bits from EAPOL-Key Key Info
478 * @nonce: Nonce value for the EAPOL-Key frame
479 * @wpa_ie: WPA/RSN IE
480 * @wpa_ie_len: Length of the WPA/RSN IE
481 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800482 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700483 */
484int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
485 const struct wpa_eapol_key *key,
486 int ver, const u8 *nonce,
487 const u8 *wpa_ie, size_t wpa_ie_len,
488 struct wpa_ptk *ptk)
489{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800490 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700491 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800492 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700493 u8 *rsn_ie_buf = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800494 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700495
496 if (wpa_ie == NULL) {
497 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
498 "cannot generate msg 2/4");
499 return -1;
500 }
501
502#ifdef CONFIG_IEEE80211R
503 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
504 int res;
505
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800506 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE before FT processing",
507 wpa_ie, wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700508 /*
509 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
510 * FTIE from (Re)Association Response.
511 */
512 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
513 sm->assoc_resp_ies_len);
514 if (rsn_ie_buf == NULL)
515 return -1;
516 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800517 res = wpa_insert_pmkid(rsn_ie_buf, &wpa_ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700518 sm->pmk_r1_name);
519 if (res < 0) {
520 os_free(rsn_ie_buf);
521 return -1;
522 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800523 wpa_hexdump(MSG_DEBUG,
524 "WPA: WPA IE after PMKID[PMKR1Name] addition into RSNE",
525 rsn_ie_buf, wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700526
527 if (sm->assoc_resp_ies) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800528 wpa_hexdump(MSG_DEBUG, "WPA: Add assoc_resp_ies",
529 sm->assoc_resp_ies,
530 sm->assoc_resp_ies_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700531 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
532 sm->assoc_resp_ies_len);
533 wpa_ie_len += sm->assoc_resp_ies_len;
534 }
535
536 wpa_ie = rsn_ie_buf;
537 }
538#endif /* CONFIG_IEEE80211R */
539
540 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
541
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700542 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800543 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700544 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800545 NULL, hdrlen + wpa_ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700546 &rlen, (void *) &reply);
547 if (rbuf == NULL) {
548 os_free(rsn_ie_buf);
549 return -1;
550 }
551
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800552 reply->type = (sm->proto == WPA_PROTO_RSN ||
553 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700554 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800555 key_info = ver | WPA_KEY_INFO_KEY_TYPE;
Sunil8cd6f4d2022-06-28 18:40:46 +0000556 if (sm->ptk_set && sm->proto != WPA_PROTO_WPA)
557 key_info |= WPA_KEY_INFO_SECURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800558 if (mic_len)
559 key_info |= WPA_KEY_INFO_MIC;
560 else
561 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
562 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800563 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700564 WPA_PUT_BE16(reply->key_length, 0);
565 else
566 os_memcpy(reply->key_length, key->key_length, 2);
567 os_memcpy(reply->replay_counter, key->replay_counter,
568 WPA_REPLAY_COUNTER_LEN);
569 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
570 WPA_REPLAY_COUNTER_LEN);
571
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800572 key_mic = (u8 *) (reply + 1);
573 WPA_PUT_BE16(key_mic + mic_len, wpa_ie_len); /* Key Data Length */
574 os_memcpy(key_mic + mic_len + 2, wpa_ie, wpa_ie_len); /* Key Data */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700575 os_free(rsn_ie_buf);
576
577 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
578
Roshan Pius5e7db942018-04-12 12:27:41 -0700579 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 2/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800580 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
581 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700582}
583
584
585static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800586 const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700587{
Hai Shalom021b0b52019-04-10 11:17:58 -0700588 const u8 *z = NULL;
Hai Shalom60840252021-02-19 19:02:11 -0800589 size_t z_len = 0, kdk_len;
Hai Shalomfdcde762020-04-02 11:19:20 -0700590 int akmp;
Hai Shalom021b0b52019-04-10 11:17:58 -0700591
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700592#ifdef CONFIG_IEEE80211R
593 if (wpa_key_mgmt_ft(sm->key_mgmt))
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800594 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700595#endif /* CONFIG_IEEE80211R */
596
Hai Shalom021b0b52019-04-10 11:17:58 -0700597#ifdef CONFIG_DPP2
598 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
599 z = wpabuf_head(sm->dpp_z);
600 z_len = wpabuf_len(sm->dpp_z);
601 }
602#endif /* CONFIG_DPP2 */
603
Hai Shalomfdcde762020-04-02 11:19:20 -0700604 akmp = sm->key_mgmt;
605#ifdef CONFIG_OWE
606 if (sm->owe_ptk_workaround && akmp == WPA_KEY_MGMT_OWE &&
607 sm->pmk_len > 32) {
608 wpa_printf(MSG_DEBUG,
609 "OWE: Force SHA256 for PTK derivation");
610 akmp |= WPA_KEY_MGMT_PSK_SHA256;
611 }
612#endif /* CONFIG_OWE */
Hai Shalom60840252021-02-19 19:02:11 -0800613
614 if (sm->force_kdk_derivation ||
Hai Shalomc1a21442022-02-04 13:43:00 -0800615 (sm->secure_ltf &&
616 ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
Hai Shalom60840252021-02-19 19:02:11 -0800617 kdk_len = WPA_KDK_MAX_LEN;
618 else
619 kdk_len = 0;
620
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800621 return wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
622 sm->own_addr, sm->bssid, sm->snonce,
Hai Shalomfdcde762020-04-02 11:19:20 -0700623 key->key_nonce, ptk, akmp,
Hai Shalom60840252021-02-19 19:02:11 -0800624 sm->pairwise_cipher, z, z_len,
625 kdk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700626}
627
628
Hai Shalomfdcde762020-04-02 11:19:20 -0700629static int wpa_handle_ext_key_id(struct wpa_sm *sm,
630 struct wpa_eapol_ie_parse *kde)
631{
632 if (sm->ext_key_id) {
633 u16 key_id;
634
635 if (!kde->key_id) {
636 wpa_msg(sm->ctx->msg_ctx,
637 sm->use_ext_key_id ? MSG_INFO : MSG_DEBUG,
638 "RSN: No Key ID in Extended Key ID handshake");
639 sm->keyidx_active = 0;
640 return sm->use_ext_key_id ? -1 : 0;
641 }
642
643 key_id = kde->key_id[0] & 0x03;
644 if (key_id > 1) {
645 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
646 "RSN: Invalid Extended Key ID: %d", key_id);
647 return -1;
648 }
649 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
650 "RSN: Using Extended Key ID %d", key_id);
651 sm->keyidx_active = key_id;
652 sm->use_ext_key_id = 1;
653 } else {
654 if (kde->key_id && (kde->key_id[0] & 0x03)) {
655 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
656 "RSN: Non-zero Extended Key ID Key ID in PTK0 handshake");
657 return -1;
658 }
659
660 if (kde->key_id) {
661 /* This is not supposed to be included here, but ignore
662 * the case of matching Key ID 0 just in case. */
663 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
664 "RSN: Extended Key ID Key ID 0 in PTK0 handshake");
665 }
666 sm->keyidx_active = 0;
667 sm->use_ext_key_id = 0;
668 }
669
670 return 0;
671}
672
673
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700674static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
675 const unsigned char *src_addr,
676 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700677 u16 ver, const u8 *key_data,
Sunil8cd6f4d2022-06-28 18:40:46 +0000678 size_t key_data_len,
679 enum frame_encryption encrypted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700680{
681 struct wpa_eapol_ie_parse ie;
682 struct wpa_ptk *ptk;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700683 int res;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800684 u8 *kde, *kde_buf = NULL;
685 size_t kde_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700686
Sunil8cd6f4d2022-06-28 18:40:46 +0000687 if (encrypted == FRAME_NOT_ENCRYPTED && sm->tk_set &&
688 wpa_sm_pmf_enabled(sm)) {
689 wpa_printf(MSG_DEBUG,
690 "RSN: Discard unencrypted EAPOL-Key msg 1/4 when TK is set and PMF is enabled");
691 return;
692 }
693
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700694 if (wpa_sm_get_network_ctx(sm) == NULL) {
695 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
696 "found (msg 1 of 4)");
697 return;
698 }
699
Hai Shalomfdcde762020-04-02 11:19:20 -0700700 if (sm->wpa_deny_ptk0_rekey && !sm->use_ext_key_id &&
701 wpa_sm_get_state(sm) == WPA_COMPLETED) {
702 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
703 "WPA: PTK0 rekey not allowed, reconnecting");
704 wpa_sm_reconnect(sm);
705 return;
706 }
707
Roshan Pius5e7db942018-04-12 12:27:41 -0700708 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: RX message 1 of 4-Way "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700709 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
710
711 os_memset(&ie, 0, sizeof(ie));
712
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800713 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700714 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700715 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
716 key_data, key_data_len);
Sunil8cd6f4d2022-06-28 18:40:46 +0000717 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0) {
718 wpa_printf(MSG_DEBUG,
719 "RSN: Discard EAPOL-Key msg 1/4 with invalid IEs/KDEs");
720 return;
721 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700722 if (ie.pmkid) {
723 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
724 "Authenticator", ie.pmkid, PMKID_LEN);
725 }
726 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700727
728 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
729 if (res == -2) {
730 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
731 "msg 1/4 - requesting full EAP authentication");
732 return;
733 }
734 if (res)
735 goto failed;
736
Sunil8cd6f4d2022-06-28 18:40:46 +0000737 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
738
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700739 if (sm->renew_snonce) {
740 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
741 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
742 "WPA: Failed to get random data for SNonce");
743 goto failed;
744 }
745 sm->renew_snonce = 0;
746 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
747 sm->snonce, WPA_NONCE_LEN);
748 }
749
750 /* Calculate PTK which will be stored as a temporary PTK until it has
751 * been verified when processing message 3/4. */
752 ptk = &sm->tptk;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700753 if (wpa_derive_ptk(sm, src_addr, key, ptk) < 0)
754 goto failed;
Dmitry Shmidt98660862014-03-11 17:26:21 -0700755 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700756 u8 buf[8];
Dmitry Shmidt98660862014-03-11 17:26:21 -0700757 /* Supplicant: swap tx/rx Mic keys */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800758 os_memcpy(buf, &ptk->tk[16], 8);
759 os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
760 os_memcpy(&ptk->tk[24], buf, 8);
Hai Shalom81f62d82019-07-22 12:10:00 -0700761 forced_memzero(buf, sizeof(buf));
Dmitry Shmidt98660862014-03-11 17:26:21 -0700762 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700763 sm->tptk_set = 1;
764
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800765 kde = sm->assoc_wpa_ie;
766 kde_len = sm->assoc_wpa_ie_len;
Hai Shalomc3565922019-10-28 11:58:20 -0700767 kde_buf = os_malloc(kde_len +
768 2 + RSN_SELECTOR_LEN + 3 +
769 sm->assoc_rsnxe_len +
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700770 2 + RSN_SELECTOR_LEN + 1 +
771 2 + RSN_SELECTOR_LEN + 2);
Hai Shalomc3565922019-10-28 11:58:20 -0700772 if (!kde_buf)
773 goto failed;
774 os_memcpy(kde_buf, kde, kde_len);
775 kde = kde_buf;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800776
Hai Shalom74f70d42019-02-11 14:42:39 -0800777#ifdef CONFIG_OCV
778 if (wpa_sm_ocv_enabled(sm)) {
779 struct wpa_channel_info ci;
780 u8 *pos;
781
Hai Shalomc3565922019-10-28 11:58:20 -0700782 pos = kde + kde_len;
Hai Shalom74f70d42019-02-11 14:42:39 -0800783 if (wpa_sm_channel_info(sm, &ci) != 0) {
784 wpa_printf(MSG_WARNING,
785 "Failed to get channel info for OCI element in EAPOL-Key 2/4");
786 goto failed;
787 }
Hai Shalom899fcc72020-10-19 14:38:18 -0700788#ifdef CONFIG_TESTING_OPTIONS
789 if (sm->oci_freq_override_eapol) {
790 wpa_printf(MSG_INFO,
791 "TEST: Override OCI KDE frequency %d -> %d MHz",
792 ci.frequency, sm->oci_freq_override_eapol);
793 ci.frequency = sm->oci_freq_override_eapol;
794 }
795#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -0800796
Hai Shalom74f70d42019-02-11 14:42:39 -0800797 if (ocv_insert_oci_kde(&ci, &pos) < 0)
798 goto failed;
799 kde_len = pos - kde;
800 }
801#endif /* CONFIG_OCV */
802
Hai Shalomc3565922019-10-28 11:58:20 -0700803 if (sm->assoc_rsnxe && sm->assoc_rsnxe_len) {
804 os_memcpy(kde + kde_len, sm->assoc_rsnxe, sm->assoc_rsnxe_len);
805 kde_len += sm->assoc_rsnxe_len;
806 }
807
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800808#ifdef CONFIG_P2P
809 if (sm->p2p) {
Hai Shalomc3565922019-10-28 11:58:20 -0700810 u8 *pos;
811
812 wpa_printf(MSG_DEBUG,
813 "P2P: Add IP Address Request KDE into EAPOL-Key 2/4");
814 pos = kde + kde_len;
815 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
816 *pos++ = RSN_SELECTOR_LEN + 1;
817 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
818 pos += RSN_SELECTOR_LEN;
819 *pos++ = 0x01;
820 kde_len = pos - kde;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800821 }
822#endif /* CONFIG_P2P */
823
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700824#ifdef CONFIG_DPP2
825 if (DPP_VERSION > 1 && sm->key_mgmt == WPA_KEY_MGMT_DPP) {
826 u8 *pos;
827
828 wpa_printf(MSG_DEBUG, "DPP: Add DPP KDE into EAPOL-Key 2/4");
829 pos = kde + kde_len;
830 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
831 *pos++ = RSN_SELECTOR_LEN + 2;
832 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_DPP);
833 pos += RSN_SELECTOR_LEN;
834 *pos++ = DPP_VERSION; /* Protocol Version */
835 *pos = 0; /* Flags */
836 if (sm->dpp_pfs == 0)
837 *pos |= DPP_KDE_PFS_ALLOWED;
838 else if (sm->dpp_pfs == 1)
839 *pos |= DPP_KDE_PFS_ALLOWED | DPP_KDE_PFS_REQUIRED;
840 pos++;
841 kde_len = pos - kde;
842 }
843#endif /* CONFIG_DPP2 */
844
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700845 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800846 kde, kde_len, ptk) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700847 goto failed;
848
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800849 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700850 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
851 return;
852
853failed:
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800854 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700855 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
856}
857
858
859static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
860{
861 struct wpa_sm *sm = eloop_ctx;
862 rsn_preauth_candidate_process(sm);
863}
864
865
866static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
867 const u8 *addr, int secure)
868{
869 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
870 "WPA: Key negotiation completed with "
871 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
872 wpa_cipher_txt(sm->pairwise_cipher),
873 wpa_cipher_txt(sm->group_cipher));
874 wpa_sm_cancel_auth_timeout(sm);
875 wpa_sm_set_state(sm, WPA_COMPLETED);
876
877 if (secure) {
878 wpa_sm_mlme_setprotection(
879 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
880 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
Hai Shalome21d4e82020-04-29 16:34:06 -0700881 eapol_sm_notify_portValid(sm->eapol, true);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700882 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
883 sm->key_mgmt == WPA_KEY_MGMT_DPP ||
884 sm->key_mgmt == WPA_KEY_MGMT_OWE)
Hai Shalome21d4e82020-04-29 16:34:06 -0700885 eapol_sm_notify_eap_success(sm->eapol, true);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700886 /*
887 * Start preauthentication after a short wait to avoid a
888 * possible race condition between the data receive and key
889 * configuration after the 4-Way Handshake. This increases the
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800890 * likelihood of the first preauth EAPOL-Start frame getting to
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700891 * the target AP.
892 */
Hai Shalom74f70d42019-02-11 14:42:39 -0800893 if (!dl_list_empty(&sm->pmksa_candidates))
894 eloop_register_timeout(1, 0, wpa_sm_start_preauth,
895 sm, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700896 }
897
898 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
899 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
900 "RSN: Authenticator accepted "
901 "opportunistic PMKSA entry - marking it valid");
902 sm->cur_pmksa->opportunistic = 0;
903 }
904
905#ifdef CONFIG_IEEE80211R
906 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
907 /* Prepare for the next transition */
908 wpa_ft_prepare_auth_request(sm, NULL);
909 }
910#endif /* CONFIG_IEEE80211R */
911}
912
913
914static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
915{
916 struct wpa_sm *sm = eloop_ctx;
917 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
918 wpa_sm_key_request(sm, 0, 1);
919}
920
921
922static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
Hai Shalomfdcde762020-04-02 11:19:20 -0700923 const struct wpa_eapol_key *key,
924 enum key_flag key_flag)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925{
926 int keylen, rsclen;
927 enum wpa_alg alg;
928 const u8 *key_rsc;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800929
Mathy Vanhoefc66556c2017-09-29 04:22:51 +0200930 if (sm->ptk.installed) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800931 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
932 "WPA: Do not re-install same PTK to the driver");
933 return 0;
934 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700935
936 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
937 "WPA: Installing PTK to the driver");
938
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700939 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700940 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
941 "Suite: NONE - do not use pairwise keys");
942 return 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700943 }
944
945 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700946 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
947 "WPA: Unsupported pairwise cipher %d",
948 sm->pairwise_cipher);
949 return -1;
950 }
951
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700952 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
953 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700954 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
955 wpa_printf(MSG_DEBUG, "WPA: TK length mismatch: %d != %lu",
956 keylen, (long unsigned int) sm->ptk.tk_len);
957 return -1;
958 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700959 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
960
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800961 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700962 key_rsc = null_rsc;
963 } else {
964 key_rsc = key->key_rsc;
965 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
966 }
967
Hai Shalomfdcde762020-04-02 11:19:20 -0700968 if (wpa_sm_set_key(sm, alg, sm->bssid, sm->keyidx_active, 1, key_rsc,
969 rsclen, sm->ptk.tk, keylen,
970 KEY_FLAG_PAIRWISE | key_flag) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700971 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Hai Shalomfdcde762020-04-02 11:19:20 -0700972 "WPA: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
973 MACSTR " idx=%d key_flag=0x%x)",
974 alg, keylen, MAC2STR(sm->bssid),
975 sm->keyidx_active, key_flag);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700976 return -1;
977 }
978
Hai Shalom60840252021-02-19 19:02:11 -0800979 wpa_sm_store_ptk(sm, sm->bssid, sm->pairwise_cipher,
980 sm->dot11RSNAConfigPMKLifetime, &sm->ptk);
981
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800982 /* TK is not needed anymore in supplicant */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800983 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700984 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +0200985 sm->ptk.installed = 1;
Sunil8cd6f4d2022-06-28 18:40:46 +0000986 sm->tk_set = true;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800987
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700988 if (sm->wpa_ptk_rekey) {
989 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
990 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
991 sm, NULL);
992 }
Hai Shalomfdcde762020-04-02 11:19:20 -0700993 return 0;
994}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700995
Hai Shalomfdcde762020-04-02 11:19:20 -0700996
997static int wpa_supplicant_activate_ptk(struct wpa_sm *sm)
998{
999 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1000 "WPA: Activate PTK (idx=%d bssid=" MACSTR ")",
1001 sm->keyidx_active, MAC2STR(sm->bssid));
1002
1003 if (wpa_sm_set_key(sm, 0, sm->bssid, sm->keyidx_active, 0, NULL, 0,
1004 NULL, 0, KEY_FLAG_PAIRWISE_RX_TX_MODIFY) < 0) {
1005 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1006 "WPA: Failed to activate PTK for TX (idx=%d bssid="
1007 MACSTR ")", sm->keyidx_active, MAC2STR(sm->bssid));
1008 return -1;
1009 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001010 return 0;
1011}
1012
1013
1014static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
1015 int group_cipher,
1016 int keylen, int maxkeylen,
1017 int *key_rsc_len,
1018 enum wpa_alg *alg)
1019{
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001020 int klen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001021
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001022 *alg = wpa_cipher_to_alg(group_cipher);
1023 if (*alg == WPA_ALG_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001024 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1025 "WPA: Unsupported Group Cipher %d",
1026 group_cipher);
1027 return -1;
1028 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001029 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001030
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001031 klen = wpa_cipher_key_len(group_cipher);
1032 if (keylen != klen || maxkeylen < klen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001033 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1034 "WPA: Unsupported %s Group Cipher key length %d (%d)",
1035 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001036 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001037 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001038 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001039}
1040
1041
1042struct wpa_gtk_data {
1043 enum wpa_alg alg;
1044 int tx, key_rsc_len, keyidx;
1045 u8 gtk[32];
1046 int gtk_len;
1047};
1048
1049
1050static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
1051 const struct wpa_gtk_data *gd,
Jouni Malinen58c0e962017-10-01 12:12:24 +03001052 const u8 *key_rsc, int wnm_sleep)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001053{
1054 const u8 *_gtk = gd->gtk;
1055 u8 gtk_buf[32];
1056
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001057 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +03001058 if ((sm->gtk.gtk_len == (size_t) gd->gtk_len &&
1059 os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) ||
1060 (sm->gtk_wnm_sleep.gtk_len == (size_t) gd->gtk_len &&
1061 os_memcmp(sm->gtk_wnm_sleep.gtk, gd->gtk,
1062 sm->gtk_wnm_sleep.gtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001063 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1064 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
1065 gd->keyidx, gd->tx, gd->gtk_len);
1066 return 0;
1067 }
1068
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001069 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
1070 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1071 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
1072 gd->keyidx, gd->tx, gd->gtk_len);
1073 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
1074 if (sm->group_cipher == WPA_CIPHER_TKIP) {
1075 /* Swap Tx/Rx keys for Michael MIC */
1076 os_memcpy(gtk_buf, gd->gtk, 16);
1077 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
1078 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
1079 _gtk = gtk_buf;
1080 }
1081 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
1082 if (wpa_sm_set_key(sm, gd->alg, NULL,
1083 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07001084 _gtk, gd->gtk_len,
1085 KEY_FLAG_GROUP_RX_TX_DEFAULT) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001086 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1087 "WPA: Failed to set GTK to the driver "
1088 "(Group only)");
Hai Shalom81f62d82019-07-22 12:10:00 -07001089 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001090 return -1;
1091 }
1092 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
1093 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07001094 _gtk, gd->gtk_len, KEY_FLAG_GROUP_RX) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1096 "WPA: Failed to set GTK to "
1097 "the driver (alg=%d keylen=%d keyidx=%d)",
1098 gd->alg, gd->gtk_len, gd->keyidx);
Hai Shalom81f62d82019-07-22 12:10:00 -07001099 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001100 return -1;
1101 }
Hai Shalom81f62d82019-07-22 12:10:00 -07001102 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001103
Jouni Malinen58c0e962017-10-01 12:12:24 +03001104 if (wnm_sleep) {
1105 sm->gtk_wnm_sleep.gtk_len = gd->gtk_len;
1106 os_memcpy(sm->gtk_wnm_sleep.gtk, gd->gtk,
1107 sm->gtk_wnm_sleep.gtk_len);
1108 } else {
1109 sm->gtk.gtk_len = gd->gtk_len;
1110 os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
1111 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001112
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001113 return 0;
1114}
1115
1116
1117static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
1118 int tx)
1119{
1120 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
1121 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
1122 * seemed to set this bit (incorrectly, since Tx is only when
1123 * doing Group Key only APs) and without this workaround, the
1124 * data connection does not work because wpa_supplicant
1125 * configured non-zero keyidx to be used for unicast. */
1126 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1127 "WPA: Tx bit set for GTK, but pairwise "
1128 "keys are used - ignore Tx bit");
1129 return 0;
1130 }
1131 return tx;
1132}
1133
1134
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001135static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
1136 const u8 *rsc)
1137{
1138 int rsclen;
1139
1140 if (!sm->wpa_rsc_relaxation)
1141 return 0;
1142
1143 rsclen = wpa_cipher_rsc_len(sm->group_cipher);
1144
1145 /*
1146 * Try to detect RSC (endian) corruption issue where the AP sends
1147 * the RSC bytes in EAPOL-Key message in the wrong order, both if
1148 * it's actually a 6-byte field (as it should be) and if it treats
1149 * it as an 8-byte field.
1150 * An AP model known to have this bug is the Sapido RB-1632.
1151 */
1152 if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
1153 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1154 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
1155 rsc[0], rsc[1], rsc[2], rsc[3],
1156 rsc[4], rsc[5], rsc[6], rsc[7]);
1157
1158 return 1;
1159 }
1160
1161 return 0;
1162}
1163
1164
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001165static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
1166 const struct wpa_eapol_key *key,
1167 const u8 *gtk, size_t gtk_len,
1168 int key_info)
1169{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001170 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001171 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001172
1173 /*
1174 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
1175 * GTK KDE format:
1176 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
1177 * Reserved [bits 0-7]
1178 * GTK
1179 */
1180
1181 os_memset(&gd, 0, sizeof(gd));
1182 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
1183 gtk, gtk_len);
1184
1185 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
1186 return -1;
1187
1188 gd.keyidx = gtk[0] & 0x3;
1189 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1190 !!(gtk[0] & BIT(2)));
1191 gtk += 2;
1192 gtk_len -= 2;
1193
1194 os_memcpy(gd.gtk, gtk, gtk_len);
1195 gd.gtk_len = gtk_len;
1196
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001197 key_rsc = key->key_rsc;
1198 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1199 key_rsc = null_rsc;
1200
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001201 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
1202 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1203 gtk_len, gtk_len,
1204 &gd.key_rsc_len, &gd.alg) ||
Jouni Malinen58c0e962017-10-01 12:12:24 +03001205 wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001206 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1207 "RSN: Failed to install GTK");
Hai Shalom81f62d82019-07-22 12:10:00 -07001208 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001209 return -1;
1210 }
Hai Shalom81f62d82019-07-22 12:10:00 -07001211 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001212
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001213 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001214}
1215
1216
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001217static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
Jouni Malinen58c0e962017-10-01 12:12:24 +03001218 const struct wpa_igtk_kde *igtk,
1219 int wnm_sleep)
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001220{
1221 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1222 u16 keyidx = WPA_GET_LE16(igtk->keyid);
1223
1224 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +03001225 if ((sm->igtk.igtk_len == len &&
1226 os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) ||
1227 (sm->igtk_wnm_sleep.igtk_len == len &&
1228 os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1229 sm->igtk_wnm_sleep.igtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001230 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1231 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
1232 keyidx);
1233 return 0;
1234 }
1235
1236 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001237 "WPA: IGTK keyid %d pn " COMPACT_MACSTR,
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001238 keyidx, MAC2STR(igtk->pn));
1239 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
1240 if (keyidx > 4095) {
1241 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1242 "WPA: Invalid IGTK KeyID %d", keyidx);
1243 return -1;
1244 }
1245 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
1246 broadcast_ether_addr,
1247 keyidx, 0, igtk->pn, sizeof(igtk->pn),
Hai Shalomfdcde762020-04-02 11:19:20 -07001248 igtk->igtk, len, KEY_FLAG_GROUP_RX) < 0) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07001249 if (keyidx == 0x0400 || keyidx == 0x0500) {
1250 /* Assume the AP has broken PMF implementation since it
1251 * seems to have swapped the KeyID bytes. The AP cannot
1252 * be trusted to implement BIP correctly or provide a
1253 * valid IGTK, so do not try to configure this key with
1254 * swapped KeyID bytes. Instead, continue without
1255 * configuring the IGTK so that the driver can drop any
1256 * received group-addressed robust management frames due
1257 * to missing keys.
1258 *
1259 * Normally, this error behavior would result in us
1260 * disconnecting, but there are number of deployed APs
1261 * with this broken behavior, so as an interoperability
1262 * workaround, allow the connection to proceed. */
1263 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1264 "WPA: Ignore IGTK configuration error due to invalid IGTK KeyID byte order");
1265 } else {
1266 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1267 "WPA: Failed to configure IGTK to the driver");
1268 return -1;
1269 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001270 }
1271
Jouni Malinen58c0e962017-10-01 12:12:24 +03001272 if (wnm_sleep) {
1273 sm->igtk_wnm_sleep.igtk_len = len;
1274 os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1275 sm->igtk_wnm_sleep.igtk_len);
1276 } else {
1277 sm->igtk.igtk_len = len;
1278 os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
1279 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001280
1281 return 0;
1282}
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001283
1284
Hai Shalomfdcde762020-04-02 11:19:20 -07001285static int wpa_supplicant_install_bigtk(struct wpa_sm *sm,
1286 const struct wpa_bigtk_kde *bigtk,
1287 int wnm_sleep)
1288{
1289 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1290 u16 keyidx = WPA_GET_LE16(bigtk->keyid);
1291
1292 /* Detect possible key reinstallation */
1293 if ((sm->bigtk.bigtk_len == len &&
1294 os_memcmp(sm->bigtk.bigtk, bigtk->bigtk,
1295 sm->bigtk.bigtk_len) == 0) ||
1296 (sm->bigtk_wnm_sleep.bigtk_len == len &&
1297 os_memcmp(sm->bigtk_wnm_sleep.bigtk, bigtk->bigtk,
1298 sm->bigtk_wnm_sleep.bigtk_len) == 0)) {
1299 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1300 "WPA: Not reinstalling already in-use BIGTK to the driver (keyidx=%d)",
1301 keyidx);
1302 return 0;
1303 }
1304
1305 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1306 "WPA: BIGTK keyid %d pn " COMPACT_MACSTR,
1307 keyidx, MAC2STR(bigtk->pn));
1308 wpa_hexdump_key(MSG_DEBUG, "WPA: BIGTK", bigtk->bigtk, len);
1309 if (keyidx < 6 || keyidx > 7) {
1310 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1311 "WPA: Invalid BIGTK KeyID %d", keyidx);
1312 return -1;
1313 }
1314 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
1315 broadcast_ether_addr,
1316 keyidx, 0, bigtk->pn, sizeof(bigtk->pn),
1317 bigtk->bigtk, len, KEY_FLAG_GROUP_RX) < 0) {
1318 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1319 "WPA: Failed to configure BIGTK to the driver");
1320 return -1;
1321 }
1322
1323 if (wnm_sleep) {
1324 sm->bigtk_wnm_sleep.bigtk_len = len;
1325 os_memcpy(sm->bigtk_wnm_sleep.bigtk, bigtk->bigtk,
1326 sm->bigtk_wnm_sleep.bigtk_len);
1327 } else {
1328 sm->bigtk.bigtk_len = len;
1329 os_memcpy(sm->bigtk.bigtk, bigtk->bigtk, sm->bigtk.bigtk_len);
1330 }
1331
1332 return 0;
1333}
1334
1335
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001336static int ieee80211w_set_keys(struct wpa_sm *sm,
1337 struct wpa_eapol_ie_parse *ie)
1338{
Hai Shalomfdcde762020-04-02 11:19:20 -07001339 size_t len;
1340
Hai Shalom60840252021-02-19 19:02:11 -08001341 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) ||
1342 sm->mgmt_group_cipher == WPA_CIPHER_GTK_NOT_USED)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001343 return 0;
1344
1345 if (ie->igtk) {
1346 const struct wpa_igtk_kde *igtk;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001347
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001348 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1349 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001350 return -1;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001351
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001352 igtk = (const struct wpa_igtk_kde *) ie->igtk;
Jouni Malinen58c0e962017-10-01 12:12:24 +03001353 if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001354 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355 }
1356
Hai Shalomfdcde762020-04-02 11:19:20 -07001357 if (ie->bigtk && sm->beacon_prot) {
1358 const struct wpa_bigtk_kde *bigtk;
1359
1360 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1361 if (ie->bigtk_len != WPA_BIGTK_KDE_PREFIX_LEN + len)
1362 return -1;
1363
1364 bigtk = (const struct wpa_bigtk_kde *) ie->bigtk;
1365 if (wpa_supplicant_install_bigtk(sm, bigtk, 0) < 0)
1366 return -1;
1367 }
1368
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001369 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370}
1371
1372
1373static void wpa_report_ie_mismatch(struct wpa_sm *sm,
1374 const char *reason, const u8 *src_addr,
1375 const u8 *wpa_ie, size_t wpa_ie_len,
1376 const u8 *rsn_ie, size_t rsn_ie_len)
1377{
1378 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
1379 reason, MAC2STR(src_addr));
1380
1381 if (sm->ap_wpa_ie) {
1382 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
1383 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
1384 }
1385 if (wpa_ie) {
1386 if (!sm->ap_wpa_ie) {
1387 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1388 "WPA: No WPA IE in Beacon/ProbeResp");
1389 }
1390 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
1391 wpa_ie, wpa_ie_len);
1392 }
1393
1394 if (sm->ap_rsn_ie) {
1395 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
1396 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
1397 }
1398 if (rsn_ie) {
1399 if (!sm->ap_rsn_ie) {
1400 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1401 "WPA: No RSN IE in Beacon/ProbeResp");
1402 }
1403 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
1404 rsn_ie, rsn_ie_len);
1405 }
1406
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001407 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001408}
1409
1410
1411#ifdef CONFIG_IEEE80211R
1412
1413static int ft_validate_mdie(struct wpa_sm *sm,
1414 const unsigned char *src_addr,
1415 struct wpa_eapol_ie_parse *ie,
1416 const u8 *assoc_resp_mdie)
1417{
1418 struct rsn_mdie *mdie;
1419
1420 mdie = (struct rsn_mdie *) (ie->mdie + 2);
1421 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
1422 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
1423 MOBILITY_DOMAIN_ID_LEN) != 0) {
1424 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
1425 "not match with the current mobility domain");
1426 return -1;
1427 }
1428
1429 if (assoc_resp_mdie &&
1430 (assoc_resp_mdie[1] != ie->mdie[1] ||
1431 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
1432 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
1433 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
1434 ie->mdie, 2 + ie->mdie[1]);
1435 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
1436 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
1437 return -1;
1438 }
1439
1440 return 0;
1441}
1442
1443
1444static int ft_validate_ftie(struct wpa_sm *sm,
1445 const unsigned char *src_addr,
1446 struct wpa_eapol_ie_parse *ie,
1447 const u8 *assoc_resp_ftie)
1448{
1449 if (ie->ftie == NULL) {
1450 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1451 "FT: No FTIE in EAPOL-Key msg 3/4");
1452 return -1;
1453 }
1454
1455 if (assoc_resp_ftie == NULL)
1456 return 0;
1457
1458 if (assoc_resp_ftie[1] != ie->ftie[1] ||
1459 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
1460 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
1461 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
1462 ie->ftie, 2 + ie->ftie[1]);
1463 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
1464 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
1465 return -1;
1466 }
1467
1468 return 0;
1469}
1470
1471
1472static int ft_validate_rsnie(struct wpa_sm *sm,
1473 const unsigned char *src_addr,
1474 struct wpa_eapol_ie_parse *ie)
1475{
1476 struct wpa_ie_data rsn;
1477
1478 if (!ie->rsn_ie)
1479 return 0;
1480
1481 /*
1482 * Verify that PMKR1Name from EAPOL-Key message 3/4
1483 * matches with the value we derived.
1484 */
1485 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
1486 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
1487 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
1488 "FT 4-way handshake message 3/4");
1489 return -1;
1490 }
1491
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001492 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
1493 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001494 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1495 "FT: PMKR1Name mismatch in "
1496 "FT 4-way handshake message 3/4");
1497 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
1498 rsn.pmkid, WPA_PMK_NAME_LEN);
1499 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1500 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1501 return -1;
1502 }
1503
1504 return 0;
1505}
1506
1507
1508static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
1509 const unsigned char *src_addr,
1510 struct wpa_eapol_ie_parse *ie)
1511{
1512 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
1513
1514 if (sm->assoc_resp_ies) {
1515 pos = sm->assoc_resp_ies;
1516 end = pos + sm->assoc_resp_ies_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001517 while (end - pos > 2) {
1518 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001519 break;
1520 switch (*pos) {
1521 case WLAN_EID_MOBILITY_DOMAIN:
1522 mdie = pos;
1523 break;
1524 case WLAN_EID_FAST_BSS_TRANSITION:
1525 ftie = pos;
1526 break;
1527 }
1528 pos += 2 + pos[1];
1529 }
1530 }
1531
1532 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
1533 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
1534 ft_validate_rsnie(sm, src_addr, ie) < 0)
1535 return -1;
1536
1537 return 0;
1538}
1539
1540#endif /* CONFIG_IEEE80211R */
1541
1542
1543static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
1544 const unsigned char *src_addr,
1545 struct wpa_eapol_ie_parse *ie)
1546{
1547 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
1548 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1549 "WPA: No WPA/RSN IE for this AP known. "
1550 "Trying to get from scan results");
1551 if (wpa_sm_get_beacon_ie(sm) < 0) {
1552 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1553 "WPA: Could not find AP from "
1554 "the scan results");
Hai Shalomfdcde762020-04-02 11:19:20 -07001555 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001556 }
Hai Shalomfdcde762020-04-02 11:19:20 -07001557 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1558 "WPA: Found the current AP from updated scan results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001559 }
1560
1561 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1562 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1563 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1564 "with IE in Beacon/ProbeResp (no IE?)",
1565 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1566 ie->rsn_ie, ie->rsn_ie_len);
1567 return -1;
1568 }
1569
1570 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1571 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1572 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1573 (ie->rsn_ie && sm->ap_rsn_ie &&
1574 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1575 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1576 ie->rsn_ie, ie->rsn_ie_len))) {
1577 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1578 "with IE in Beacon/ProbeResp",
1579 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1580 ie->rsn_ie, ie->rsn_ie_len);
1581 return -1;
1582 }
1583
1584 if (sm->proto == WPA_PROTO_WPA &&
1585 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1586 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1587 "detected - RSN was enabled and RSN IE "
1588 "was in msg 3/4, but not in "
1589 "Beacon/ProbeResp",
1590 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1591 ie->rsn_ie, ie->rsn_ie_len);
1592 return -1;
1593 }
1594
Hai Shalom60840252021-02-19 19:02:11 -08001595 if (sm->proto == WPA_PROTO_RSN &&
1596 ((sm->ap_rsnxe && !ie->rsnxe) ||
1597 (!sm->ap_rsnxe && ie->rsnxe) ||
1598 (sm->ap_rsnxe && ie->rsnxe &&
1599 (sm->ap_rsnxe_len != ie->rsnxe_len ||
1600 os_memcmp(sm->ap_rsnxe, ie->rsnxe, sm->ap_rsnxe_len) != 0)))) {
Hai Shalomc3565922019-10-28 11:58:20 -07001601 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1602 "WPA: RSNXE mismatch between Beacon/ProbeResp and EAPOL-Key msg 3/4");
Hai Shalomfdcde762020-04-02 11:19:20 -07001603 wpa_hexdump(MSG_INFO, "RSNXE in Beacon/ProbeResp",
1604 sm->ap_rsnxe, sm->ap_rsnxe_len);
1605 wpa_hexdump(MSG_INFO, "RSNXE in EAPOL-Key msg 3/4",
1606 ie->rsnxe, ie->rsnxe_len);
1607 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Hai Shalomc3565922019-10-28 11:58:20 -07001608 return -1;
1609 }
1610
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001611#ifdef CONFIG_IEEE80211R
1612 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1613 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1614 return -1;
1615#endif /* CONFIG_IEEE80211R */
1616
1617 return 0;
1618}
1619
1620
1621/**
1622 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1623 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1624 * @dst: Destination address for the frame
1625 * @key: Pointer to the EAPOL-Key frame header
1626 * @ver: Version bits from EAPOL-Key Key Info
1627 * @key_info: Key Info
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001628 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001629 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001630 */
1631int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1632 const struct wpa_eapol_key *key,
1633 u16 ver, u16 key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001634 struct wpa_ptk *ptk)
1635{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001636 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001637 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001638 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001639
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001640 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001641 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001642 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001643 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001644 if (rbuf == NULL)
1645 return -1;
1646
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001647 reply->type = (sm->proto == WPA_PROTO_RSN ||
1648 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001649 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1650 key_info &= WPA_KEY_INFO_SECURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001651 key_info |= ver | WPA_KEY_INFO_KEY_TYPE;
1652 if (mic_len)
1653 key_info |= WPA_KEY_INFO_MIC;
1654 else
1655 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001656 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001657 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001658 WPA_PUT_BE16(reply->key_length, 0);
1659 else
1660 os_memcpy(reply->key_length, key->key_length, 2);
1661 os_memcpy(reply->replay_counter, key->replay_counter,
1662 WPA_REPLAY_COUNTER_LEN);
1663
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001664 key_mic = (u8 *) (reply + 1);
1665 WPA_PUT_BE16(key_mic + mic_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666
Roshan Pius5e7db942018-04-12 12:27:41 -07001667 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 4/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001668 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
1669 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001670}
1671
1672
1673static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1674 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001675 u16 ver, const u8 *key_data,
1676 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001677{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001678 u16 key_info, keylen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001679 struct wpa_eapol_ie_parse ie;
1680
1681 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
Roshan Pius5e7db942018-04-12 12:27:41 -07001682 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: RX message 3 of 4-Way "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001683 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1684
1685 key_info = WPA_GET_BE16(key->key_info);
1686
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001687 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1688 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001689 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001690 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1691 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1692 "WPA: GTK IE in unencrypted key data");
1693 goto failed;
1694 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001695 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1696 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1697 "WPA: IGTK KDE in unencrypted key data");
1698 goto failed;
1699 }
1700
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001701 if (ie.igtk &&
Hai Shalom60840252021-02-19 19:02:11 -08001702 sm->mgmt_group_cipher != WPA_CIPHER_GTK_NOT_USED &&
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001703 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1704 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1705 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001706 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1707 "WPA: Invalid IGTK KDE length %lu",
1708 (unsigned long) ie.igtk_len);
1709 goto failed;
1710 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001711
1712 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1713 goto failed;
1714
Hai Shalomfdcde762020-04-02 11:19:20 -07001715 if (wpa_handle_ext_key_id(sm, &ie))
1716 goto failed;
1717
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001718 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1719 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1720 "WPA: ANonce from message 1 of 4-Way Handshake "
1721 "differs from 3 of 4-Way Handshake - drop packet (src="
1722 MACSTR ")", MAC2STR(sm->bssid));
1723 goto failed;
1724 }
1725
1726 keylen = WPA_GET_BE16(key->key_length);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001727 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1728 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1729 "WPA: Invalid %s key length %d (src=" MACSTR
1730 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1731 MAC2STR(sm->bssid));
1732 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001733 }
1734
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001735#ifdef CONFIG_P2P
1736 if (ie.ip_addr_alloc) {
1737 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1738 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1739 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1740 }
1741#endif /* CONFIG_P2P */
1742
Hai Shalom74f70d42019-02-11 14:42:39 -08001743#ifdef CONFIG_OCV
1744 if (wpa_sm_ocv_enabled(sm)) {
1745 struct wpa_channel_info ci;
1746
1747 if (wpa_sm_channel_info(sm, &ci) != 0) {
1748 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1749 "Failed to get channel info to validate received OCI in EAPOL-Key 3/4");
1750 return;
1751 }
1752
1753 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
1754 channel_width_to_int(ci.chanwidth),
Hai Shalom899fcc72020-10-19 14:38:18 -07001755 ci.seg1_idx) != OCI_SUCCESS) {
1756 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
1757 "addr=" MACSTR " frame=eapol-key-m3 error=%s",
1758 MAC2STR(sm->bssid), ocv_errorstr);
Hai Shalom74f70d42019-02-11 14:42:39 -08001759 return;
1760 }
1761 }
1762#endif /* CONFIG_OCV */
1763
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001764#ifdef CONFIG_DPP2
1765 if (DPP_VERSION > 1 && ie.dpp_kde) {
1766 wpa_printf(MSG_DEBUG,
1767 "DPP: peer Protocol Version %u Flags 0x%x",
1768 ie.dpp_kde[0], ie.dpp_kde[1]);
1769 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_pfs != 2 &&
1770 (ie.dpp_kde[1] & DPP_KDE_PFS_ALLOWED) && !sm->dpp_z) {
1771 wpa_printf(MSG_INFO,
1772 "DPP: Peer indicated it supports PFS and local configuration allows this, but PFS was not negotiated for the association");
1773 goto failed;
1774 }
1775 }
1776#endif /* CONFIG_DPP2 */
1777
Hai Shalomfdcde762020-04-02 11:19:20 -07001778 if (sm->use_ext_key_id &&
1779 wpa_supplicant_install_ptk(sm, key, KEY_FLAG_RX))
1780 goto failed;
1781
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001782 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001783 &sm->ptk) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001784 goto failed;
1785 }
1786
1787 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1788 * for the next 4-Way Handshake. If msg 3 is received again, the old
1789 * SNonce will still be used to avoid changing PTK. */
1790 sm->renew_snonce = 1;
1791
1792 if (key_info & WPA_KEY_INFO_INSTALL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001793 int res;
1794
1795 if (sm->use_ext_key_id)
1796 res = wpa_supplicant_activate_ptk(sm);
1797 else
1798 res = wpa_supplicant_install_ptk(sm, key,
1799 KEY_FLAG_RX_TX);
1800 if (res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001801 goto failed;
1802 }
1803
1804 if (key_info & WPA_KEY_INFO_SECURE) {
1805 wpa_sm_mlme_setprotection(
1806 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1807 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
Hai Shalome21d4e82020-04-29 16:34:06 -07001808 eapol_sm_notify_portValid(sm->eapol, true);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001809 }
1810 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1811
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001812 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07001813 /* No GTK to be set to the driver */
1814 } else if (!ie.gtk && sm->proto == WPA_PROTO_RSN) {
1815 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1816 "RSN: No GTK KDE included in EAPOL-Key msg 3/4");
1817 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001818 } else if (ie.gtk &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001819 wpa_supplicant_pairwise_gtk(sm, key,
1820 ie.gtk, ie.gtk_len, key_info) < 0) {
1821 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1822 "RSN: Failed to configure GTK");
1823 goto failed;
1824 }
1825
1826 if (ieee80211w_set_keys(sm, &ie) < 0) {
1827 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1828 "RSN: Failed to configure IGTK");
1829 goto failed;
1830 }
1831
Hai Shalom5f92bc92019-04-18 11:54:11 -07001832 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED || ie.gtk)
1833 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1834 key_info & WPA_KEY_INFO_SECURE);
1835
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001836 if (ie.gtk)
1837 wpa_sm_set_rekey_offload(sm);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001838
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001839 /* Add PMKSA cache entry for Suite B AKMs here since PMKID can be
1840 * calculated only after KCK has been derived. Though, do not replace an
1841 * existing PMKSA entry after each 4-way handshake (i.e., new KCK/PMKID)
1842 * to avoid unnecessary changes of PMKID while continuing to use the
1843 * same PMK. */
1844 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1845 !sm->cur_pmksa) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001846 struct rsn_pmksa_cache_entry *sa;
1847
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001848 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001849 sm->ptk.kck, sm->ptk.kck_len,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001850 sm->bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001851 sm->network_ctx, sm->key_mgmt, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001852 if (!sm->cur_pmksa)
1853 sm->cur_pmksa = sa;
1854 }
1855
Hai Shalomfdcde762020-04-02 11:19:20 -07001856 if (ie.transition_disable)
1857 wpa_sm_transition_disable(sm, ie.transition_disable[0]);
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001858 sm->msg_3_of_4_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001859 return;
1860
1861failed:
1862 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1863}
1864
1865
1866static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1867 const u8 *keydata,
1868 size_t keydatalen,
1869 u16 key_info,
1870 struct wpa_gtk_data *gd)
1871{
1872 int maxkeylen;
1873 struct wpa_eapol_ie_parse ie;
Hai Shalom60840252021-02-19 19:02:11 -08001874 u16 gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001875
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001876 wpa_hexdump_key(MSG_DEBUG, "RSN: msg 1/2 key data",
1877 keydata, keydatalen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001878 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1879 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001880 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1881 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1882 "WPA: GTK IE in unencrypted key data");
1883 return -1;
1884 }
1885 if (ie.gtk == NULL) {
1886 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1887 "WPA: No GTK IE in Group Key msg 1/2");
1888 return -1;
1889 }
Hai Shalom60840252021-02-19 19:02:11 -08001890 gtk_len = ie.gtk_len;
1891 if (gtk_len < 2) {
1892 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1893 "RSN: Invalid GTK KDE length (%u) in Group Key msg 1/2",
1894 gtk_len);
1895 return -1;
1896 }
1897 gtk_len -= 2;
1898 if (gtk_len > sizeof(gd->gtk)) {
1899 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1900 "RSN: Too long GTK in GTK KDE (len=%u)", gtk_len);
1901 return -1;
1902 }
1903 maxkeylen = gd->gtk_len = gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001904
Hai Shalom74f70d42019-02-11 14:42:39 -08001905#ifdef CONFIG_OCV
1906 if (wpa_sm_ocv_enabled(sm)) {
1907 struct wpa_channel_info ci;
1908
1909 if (wpa_sm_channel_info(sm, &ci) != 0) {
1910 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1911 "Failed to get channel info to validate received OCI in EAPOL-Key group msg 1/2");
1912 return -1;
1913 }
1914
1915 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
1916 channel_width_to_int(ci.chanwidth),
Hai Shalom899fcc72020-10-19 14:38:18 -07001917 ci.seg1_idx) != OCI_SUCCESS) {
1918 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
1919 "addr=" MACSTR " frame=eapol-key-g1 error=%s",
1920 MAC2STR(sm->bssid), ocv_errorstr);
Hai Shalom74f70d42019-02-11 14:42:39 -08001921 return -1;
1922 }
1923 }
1924#endif /* CONFIG_OCV */
1925
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001926 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
Hai Shalom60840252021-02-19 19:02:11 -08001927 gtk_len, maxkeylen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001928 &gd->key_rsc_len, &gd->alg))
1929 return -1;
1930
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001931 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
Hai Shalom60840252021-02-19 19:02:11 -08001932 ie.gtk, 2 + gtk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001933 gd->keyidx = ie.gtk[0] & 0x3;
1934 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1935 !!(ie.gtk[0] & BIT(2)));
Hai Shalom60840252021-02-19 19:02:11 -08001936 os_memcpy(gd->gtk, ie.gtk + 2, gtk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001937
1938 if (ieee80211w_set_keys(sm, &ie) < 0)
1939 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1940 "RSN: Failed to configure IGTK");
1941
1942 return 0;
1943}
1944
1945
1946static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1947 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001948 const u8 *key_data,
1949 size_t key_data_len, u16 key_info,
1950 u16 ver, struct wpa_gtk_data *gd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001951{
1952 size_t maxkeylen;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001953 u16 gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001954
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001955 gtk_len = WPA_GET_BE16(key->key_length);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001956 maxkeylen = key_data_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001957 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1958 if (maxkeylen < 8) {
1959 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1960 "WPA: Too short maxkeylen (%lu)",
1961 (unsigned long) maxkeylen);
1962 return -1;
1963 }
1964 maxkeylen -= 8;
1965 }
1966
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001967 if (gtk_len > maxkeylen ||
1968 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1969 gtk_len, maxkeylen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001970 &gd->key_rsc_len, &gd->alg))
1971 return -1;
1972
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001973 gd->gtk_len = gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001974 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1975 WPA_KEY_INFO_KEY_INDEX_SHIFT;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001976 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001977#ifdef CONFIG_NO_RC4
1978 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1979 "WPA: RC4 not supported in the build");
1980 return -1;
1981#else /* CONFIG_NO_RC4 */
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001982 u8 ek[32];
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001983 if (key_data_len > sizeof(gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001984 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1985 "WPA: RC4 key data too long (%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001986 (unsigned long) key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001987 return -1;
1988 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001989 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001990 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001991 os_memcpy(gd->gtk, key_data, key_data_len);
1992 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001993 forced_memzero(ek, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001994 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1995 "WPA: RC4 failed");
1996 return -1;
1997 }
Hai Shalom81f62d82019-07-22 12:10:00 -07001998 forced_memzero(ek, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001999#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002000 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002001 if (maxkeylen % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002002 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2003 "WPA: Unsupported AES-WRAP len %lu",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002004 (unsigned long) maxkeylen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002005 return -1;
2006 }
2007 if (maxkeylen > sizeof(gd->gtk)) {
2008 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2009 "WPA: AES-WRAP key data "
2010 "too long (keydatalen=%lu maxkeylen=%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002011 (unsigned long) key_data_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002012 (unsigned long) maxkeylen);
2013 return -1;
2014 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002015 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
2016 key_data, gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002017 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2018 "WPA: AES unwrap failed - could not decrypt "
2019 "GTK");
2020 return -1;
2021 }
2022 } else {
2023 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2024 "WPA: Unsupported key_info type %d", ver);
2025 return -1;
2026 }
2027 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
2028 sm, !!(key_info & WPA_KEY_INFO_TXRX));
2029 return 0;
2030}
2031
2032
2033static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
2034 const struct wpa_eapol_key *key,
2035 int ver, u16 key_info)
2036{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002037 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002038 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002039 u8 *rbuf, *key_mic;
Hai Shalom74f70d42019-02-11 14:42:39 -08002040 size_t kde_len = 0;
2041
Sunil Ravia04bd252022-05-02 22:54:18 -07002042#ifdef CONFIG_TESTING_OPTIONS
2043 if (sm->disable_eapol_g2_tx) {
2044 wpa_printf(MSG_INFO, "TEST: Disable sending EAPOL-Key 2/2");
2045 return 0;
2046 }
2047#endif /* CONFIG_TESTING_OPTIONS */
2048
Hai Shalom74f70d42019-02-11 14:42:39 -08002049#ifdef CONFIG_OCV
2050 if (wpa_sm_ocv_enabled(sm))
2051 kde_len = OCV_OCI_KDE_LEN;
2052#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002053
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002054 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002055 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002056 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Hai Shalom74f70d42019-02-11 14:42:39 -08002057 hdrlen + kde_len, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002058 if (rbuf == NULL)
2059 return -1;
2060
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002061 reply->type = (sm->proto == WPA_PROTO_RSN ||
2062 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002063 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
2064 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002065 key_info |= ver | WPA_KEY_INFO_SECURE;
2066 if (mic_len)
2067 key_info |= WPA_KEY_INFO_MIC;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002068 else
2069 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002070 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002071 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002072 WPA_PUT_BE16(reply->key_length, 0);
2073 else
2074 os_memcpy(reply->key_length, key->key_length, 2);
2075 os_memcpy(reply->replay_counter, key->replay_counter,
2076 WPA_REPLAY_COUNTER_LEN);
2077
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002078 key_mic = (u8 *) (reply + 1);
Hai Shalom74f70d42019-02-11 14:42:39 -08002079 WPA_PUT_BE16(key_mic + mic_len, kde_len); /* Key Data Length */
2080
2081#ifdef CONFIG_OCV
2082 if (wpa_sm_ocv_enabled(sm)) {
2083 struct wpa_channel_info ci;
2084 u8 *pos;
2085
2086 if (wpa_sm_channel_info(sm, &ci) != 0) {
2087 wpa_printf(MSG_WARNING,
2088 "Failed to get channel info for OCI element in EAPOL-Key 2/2");
2089 os_free(rbuf);
2090 return -1;
2091 }
Hai Shalom899fcc72020-10-19 14:38:18 -07002092#ifdef CONFIG_TESTING_OPTIONS
2093 if (sm->oci_freq_override_eapol_g2) {
2094 wpa_printf(MSG_INFO,
2095 "TEST: Override OCI KDE frequency %d -> %d MHz",
2096 ci.frequency,
2097 sm->oci_freq_override_eapol_g2);
2098 ci.frequency = sm->oci_freq_override_eapol_g2;
2099 }
2100#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -08002101
2102 pos = key_mic + mic_len + 2; /* Key Data */
2103 if (ocv_insert_oci_kde(&ci, &pos) < 0) {
2104 os_free(rbuf);
2105 return -1;
2106 }
2107 }
2108#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002109
2110 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002111 return wpa_eapol_key_send(sm, &sm->ptk, ver, sm->bssid, ETH_P_EAPOL,
2112 rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002113}
2114
2115
2116static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
2117 const unsigned char *src_addr,
2118 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002119 const u8 *key_data,
2120 size_t key_data_len, u16 ver)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002121{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002122 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002123 int rekey, ret;
2124 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002125 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002126
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002127 if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) {
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002128 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2129 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
2130 goto failed;
2131 }
2132
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002133 os_memset(&gd, 0, sizeof(gd));
2134
2135 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
2136 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
2137 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
2138
2139 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002140
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002141 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002142 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
2143 key_data_len, key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002144 &gd);
2145 } else {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002146 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
2147 key_data_len,
2148 key_info, ver, &gd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002149 }
2150
2151 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
2152
2153 if (ret)
2154 goto failed;
2155
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002156 key_rsc = key->key_rsc;
2157 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
2158 key_rsc = null_rsc;
2159
Jouni Malinen58c0e962017-10-01 12:12:24 +03002160 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002161 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002162 goto failed;
Hai Shalom81f62d82019-07-22 12:10:00 -07002163 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002164
2165 if (rekey) {
2166 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
2167 "completed with " MACSTR " [GTK=%s]",
2168 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
2169 wpa_sm_cancel_auth_timeout(sm);
2170 wpa_sm_set_state(sm, WPA_COMPLETED);
2171 } else {
2172 wpa_supplicant_key_neg_complete(sm, sm->bssid,
2173 key_info &
2174 WPA_KEY_INFO_SECURE);
2175 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002176
2177 wpa_sm_set_rekey_offload(sm);
2178
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002179 return;
2180
2181failed:
Hai Shalom81f62d82019-07-22 12:10:00 -07002182 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002183 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2184}
2185
2186
2187static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002188 struct wpa_eapol_key *key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002189 u16 ver,
2190 const u8 *buf, size_t len)
2191{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002192 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002193 int ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002194 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002195
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002196 os_memcpy(mic, key + 1, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002197 if (sm->tptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002198 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002199 if (wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len,
2200 sm->key_mgmt,
2201 ver, buf, len, (u8 *) (key + 1)) < 0 ||
2202 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002203 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2204 "WPA: Invalid EAPOL-Key MIC "
2205 "when using TPTK - ignoring TPTK");
Hai Shalom74f70d42019-02-11 14:42:39 -08002206#ifdef TEST_FUZZ
2207 wpa_printf(MSG_INFO,
2208 "TEST: Ignore Key MIC failure for fuzz testing");
2209 goto continue_fuzz;
2210#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002211 } else {
Hai Shalom74f70d42019-02-11 14:42:39 -08002212#ifdef TEST_FUZZ
2213 continue_fuzz:
2214#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002215 ok = 1;
2216 sm->tptk_set = 0;
2217 sm->ptk_set = 1;
2218 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002219 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002220 /*
2221 * This assures the same TPTK in sm->tptk can never be
Roshan Pius3a1667e2018-07-03 15:17:14 -07002222 * copied twice to sm->ptk as the new PTK. In
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002223 * combination with the installed flag in the wpa_ptk
2224 * struct, this assures the same PTK is only installed
2225 * once.
2226 */
2227 sm->renew_snonce = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002228 }
2229 }
2230
2231 if (!ok && sm->ptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002232 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002233 if (wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len,
2234 sm->key_mgmt,
2235 ver, buf, len, (u8 *) (key + 1)) < 0 ||
2236 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002237 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2238 "WPA: Invalid EAPOL-Key MIC - "
2239 "dropping packet");
Hai Shalom74f70d42019-02-11 14:42:39 -08002240#ifdef TEST_FUZZ
2241 wpa_printf(MSG_INFO,
2242 "TEST: Ignore Key MIC failure for fuzz testing");
2243 goto continue_fuzz2;
2244#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002245 return -1;
2246 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002247#ifdef TEST_FUZZ
2248 continue_fuzz2:
2249#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002250 ok = 1;
2251 }
2252
2253 if (!ok) {
2254 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2255 "WPA: Could not verify EAPOL-Key MIC - "
2256 "dropping packet");
2257 return -1;
2258 }
2259
2260 os_memcpy(sm->rx_replay_counter, key->replay_counter,
2261 WPA_REPLAY_COUNTER_LEN);
2262 sm->rx_replay_counter_set = 1;
2263 return 0;
2264}
2265
2266
2267/* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
2268static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002269 struct wpa_eapol_key *key,
2270 size_t mic_len, u16 ver,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002271 u8 *key_data, size_t *key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002272{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002273 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002274 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002275 if (!sm->ptk_set) {
2276 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2277 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
2278 "Data");
2279 return -1;
2280 }
2281
2282 /* Decrypt key data here so that this operation does not need
2283 * to be implemented separately for each message type. */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002284 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002285#ifdef CONFIG_NO_RC4
2286 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2287 "WPA: RC4 not supported in the build");
2288 return -1;
2289#else /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002290 u8 ek[32];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002291
2292 wpa_printf(MSG_DEBUG, "WPA: Decrypt Key Data using RC4");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002293 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002294 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002295 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07002296 forced_memzero(ek, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002297 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2298 "WPA: RC4 failed");
2299 return -1;
2300 }
Hai Shalom81f62d82019-07-22 12:10:00 -07002301 forced_memzero(ek, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002302#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002303 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002304 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07002305 wpa_use_aes_key_wrap(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002306 u8 *buf;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002307
2308 wpa_printf(MSG_DEBUG,
2309 "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)",
2310 (unsigned int) sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002311 if (*key_data_len < 8 || *key_data_len % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002312 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002313 "WPA: Unsupported AES-WRAP len %u",
2314 (unsigned int) *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002315 return -1;
2316 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002317 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
2318 buf = os_malloc(*key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002319 if (buf == NULL) {
2320 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2321 "WPA: No memory for AES-UNWRAP buffer");
2322 return -1;
2323 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002324#ifdef TEST_FUZZ
2325 os_memset(buf, 0x11, *key_data_len);
2326#endif /* TEST_FUZZ */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002327 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002328 key_data, buf)) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002329#ifdef TEST_FUZZ
2330 wpa_printf(MSG_INFO,
2331 "TEST: Ignore AES unwrap failure for fuzz testing");
2332 goto continue_fuzz;
2333#endif /* TEST_FUZZ */
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002334 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002335 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2336 "WPA: AES unwrap failed - "
2337 "could not decrypt EAPOL-Key key data");
2338 return -1;
2339 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002340#ifdef TEST_FUZZ
2341 continue_fuzz:
2342#endif /* TEST_FUZZ */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002343 os_memcpy(key_data, buf, *key_data_len);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002344 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002345 WPA_PUT_BE16(((u8 *) (key + 1)) + mic_len, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002346 } else {
2347 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2348 "WPA: Unsupported key_info type %d", ver);
2349 return -1;
2350 }
2351 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002352 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002353 return 0;
2354}
2355
2356
2357/**
2358 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
2359 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2360 */
2361void wpa_sm_aborted_cached(struct wpa_sm *sm)
2362{
2363 if (sm && sm->cur_pmksa) {
2364 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2365 "RSN: Cancelling PMKSA caching attempt");
2366 sm->cur_pmksa = NULL;
2367 }
2368}
2369
2370
Hai Shalomc1a21442022-02-04 13:43:00 -08002371void wpa_sm_aborted_external_cached(struct wpa_sm *sm)
2372{
2373 if (sm && sm->cur_pmksa && sm->cur_pmksa->external) {
2374 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2375 "RSN: Cancelling external PMKSA caching attempt");
2376 sm->cur_pmksa = NULL;
2377 }
2378}
2379
2380
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002381static void wpa_eapol_key_dump(struct wpa_sm *sm,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002382 const struct wpa_eapol_key *key,
2383 unsigned int key_data_len,
2384 const u8 *mic, unsigned int mic_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002385{
2386#ifndef CONFIG_NO_STDOUT_DEBUG
2387 u16 key_info = WPA_GET_BE16(key->key_info);
2388
2389 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
2390 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2391 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
2392 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
2393 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
2394 WPA_KEY_INFO_KEY_INDEX_SHIFT,
2395 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
2396 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
2397 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
2398 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
2399 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
2400 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
2401 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
2402 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
2403 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
2404 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2405 " key_length=%u key_data_length=%u",
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002406 WPA_GET_BE16(key->key_length), key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002407 wpa_hexdump(MSG_DEBUG, " replay_counter",
2408 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
2409 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
2410 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
2411 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
2412 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002413 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002414#endif /* CONFIG_NO_STDOUT_DEBUG */
2415}
2416
2417
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002418#ifdef CONFIG_FILS
2419static int wpa_supp_aead_decrypt(struct wpa_sm *sm, u8 *buf, size_t buf_len,
2420 size_t *key_data_len)
2421{
2422 struct wpa_ptk *ptk;
2423 struct ieee802_1x_hdr *hdr;
2424 struct wpa_eapol_key *key;
2425 u8 *pos, *tmp;
2426 const u8 *aad[1];
2427 size_t aad_len[1];
2428
2429 if (*key_data_len < AES_BLOCK_SIZE) {
2430 wpa_printf(MSG_INFO, "No room for AES-SIV data in the frame");
2431 return -1;
2432 }
2433
2434 if (sm->tptk_set)
2435 ptk = &sm->tptk;
2436 else if (sm->ptk_set)
2437 ptk = &sm->ptk;
2438 else
2439 return -1;
2440
2441 hdr = (struct ieee802_1x_hdr *) buf;
2442 key = (struct wpa_eapol_key *) (hdr + 1);
2443 pos = (u8 *) (key + 1);
2444 pos += 2; /* Pointing at the Encrypted Key Data field */
2445
2446 tmp = os_malloc(*key_data_len);
2447 if (!tmp)
2448 return -1;
2449
2450 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2451 * to Key Data (exclusive). */
2452 aad[0] = buf;
2453 aad_len[0] = pos - buf;
2454 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, *key_data_len,
2455 1, aad, aad_len, tmp) < 0) {
2456 wpa_printf(MSG_INFO, "Invalid AES-SIV data in the frame");
2457 bin_clear_free(tmp, *key_data_len);
2458 return -1;
2459 }
2460
2461 /* AEAD decryption and validation completed successfully */
2462 (*key_data_len) -= AES_BLOCK_SIZE;
2463 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
2464 tmp, *key_data_len);
2465
2466 /* Replace Key Data field with the decrypted version */
2467 os_memcpy(pos, tmp, *key_data_len);
2468 pos -= 2; /* Key Data Length field */
2469 WPA_PUT_BE16(pos, *key_data_len);
2470 bin_clear_free(tmp, *key_data_len);
2471
2472 if (sm->tptk_set) {
2473 sm->tptk_set = 0;
2474 sm->ptk_set = 1;
2475 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
2476 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2477 }
2478
2479 os_memcpy(sm->rx_replay_counter, key->replay_counter,
2480 WPA_REPLAY_COUNTER_LEN);
2481 sm->rx_replay_counter_set = 1;
2482
2483 return 0;
2484}
2485#endif /* CONFIG_FILS */
2486
2487
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002488/**
2489 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
2490 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2491 * @src_addr: Source MAC address of the EAPOL packet
2492 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
2493 * @len: Length of the EAPOL frame
Sunil8cd6f4d2022-06-28 18:40:46 +00002494 * @encrypted: Whether the frame was encrypted
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002495 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
2496 *
2497 * This function is called for each received EAPOL frame. Other than EAPOL-Key
2498 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
2499 * only processing WPA and WPA2 EAPOL-Key frames.
2500 *
2501 * The received EAPOL-Key packets are validated and valid packets are replied
2502 * to. In addition, key material (PTK, GTK) is configured at the end of a
2503 * successful key handshake.
2504 */
2505int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
Sunil8cd6f4d2022-06-28 18:40:46 +00002506 const u8 *buf, size_t len, enum frame_encryption encrypted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002507{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002508 size_t plen, data_len, key_data_len;
2509 const struct ieee802_1x_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002510 struct wpa_eapol_key *key;
2511 u16 key_info, ver;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002512 u8 *tmp = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002513 int ret = -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002514 u8 *mic, *key_data;
Hai Shalom899fcc72020-10-19 14:38:18 -07002515 size_t mic_len, keyhdrlen, pmk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002516
2517#ifdef CONFIG_IEEE80211R
2518 sm->ft_completed = 0;
2519#endif /* CONFIG_IEEE80211R */
2520
Hai Shalom899fcc72020-10-19 14:38:18 -07002521 pmk_len = sm->pmk_len;
2522 if (!pmk_len && sm->cur_pmksa)
2523 pmk_len = sm->cur_pmksa->pmk_len;
2524 mic_len = wpa_mic_len(sm->key_mgmt, pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002525 keyhdrlen = sizeof(*key) + mic_len + 2;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002526
2527 if (len < sizeof(*hdr) + keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002528 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2529 "WPA: EAPOL frame too short to be a WPA "
2530 "EAPOL-Key (len %lu, expecting at least %lu)",
2531 (unsigned long) len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002532 (unsigned long) sizeof(*hdr) + keyhdrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002533 return 0;
2534 }
2535
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002536 hdr = (const struct ieee802_1x_hdr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537 plen = be_to_host16(hdr->length);
2538 data_len = plen + sizeof(*hdr);
2539 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2540 "IEEE 802.1X RX: version=%d type=%d length=%lu",
2541 hdr->version, hdr->type, (unsigned long) plen);
2542
2543 if (hdr->version < EAPOL_VERSION) {
2544 /* TODO: backwards compatibility */
2545 }
2546 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
2547 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2548 "WPA: EAPOL frame (type %u) discarded, "
2549 "not a Key frame", hdr->type);
2550 ret = 0;
2551 goto out;
2552 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002553 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002554 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002555 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2556 "WPA: EAPOL frame payload size %lu "
2557 "invalid (frame size %lu)",
2558 (unsigned long) plen, (unsigned long) len);
2559 ret = 0;
2560 goto out;
2561 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002562 if (data_len < len) {
2563 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2564 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
2565 (unsigned long) len - data_len);
2566 }
2567
2568 /*
2569 * Make a copy of the frame since we need to modify the buffer during
2570 * MAC validation and Key Data decryption.
2571 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002572 tmp = os_memdup(buf, data_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002573 if (tmp == NULL)
2574 goto out;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002575 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002576 mic = (u8 *) (key + 1);
2577 key_data = mic + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002578
2579 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
2580 {
2581 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2582 "WPA: EAPOL-Key type (%d) unknown, discarded",
2583 key->type);
2584 ret = 0;
2585 goto out;
2586 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002587
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002588 key_data_len = WPA_GET_BE16(mic + mic_len);
2589 wpa_eapol_key_dump(sm, key, key_data_len, mic, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002590
2591 if (key_data_len > plen - keyhdrlen) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002592 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
2593 "frame - key_data overflow (%u > %u)",
2594 (unsigned int) key_data_len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002595 (unsigned int) (plen - keyhdrlen));
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002596 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002597 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002598
2599 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002600 key_info = WPA_GET_BE16(key->key_info);
2601 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
2602 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002603 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002604 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002605 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002606 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2607 "WPA: Unsupported EAPOL-Key descriptor version %d",
2608 ver);
2609 goto out;
2610 }
2611
Roshan Pius3a1667e2018-07-03 15:17:14 -07002612 if (wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002613 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
2614 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2615 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
2616 ver);
2617 goto out;
2618 }
2619
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002620#ifdef CONFIG_IEEE80211R
2621 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
2622 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002623 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
2624 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002625 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2626 "FT: AP did not use AES-128-CMAC");
2627 goto out;
2628 }
2629 } else
2630#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002631 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002632 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002633 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002634 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2635 "WPA: AP did not use the "
2636 "negotiated AES-128-CMAC");
2637 goto out;
2638 }
Hai Shalomc3565922019-10-28 11:58:20 -07002639 } else if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
2640 !wpa_use_akm_defined(sm->key_mgmt) &&
2641 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002642 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2643 "WPA: CCMP is used, but EAPOL-Key "
2644 "descriptor version (%d) is not 2", ver);
2645 if (sm->group_cipher != WPA_CIPHER_CCMP &&
2646 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
2647 /* Earlier versions of IEEE 802.11i did not explicitly
2648 * require version 2 descriptor for all EAPOL-Key
2649 * packets, so allow group keys to use version 1 if
2650 * CCMP is not used for them. */
2651 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2652 "WPA: Backwards compatibility: allow invalid "
2653 "version for non-CCMP group keys");
Jouni Malinen658fb4a2014-11-14 20:57:05 +02002654 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
2655 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2656 "WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002657 } else
2658 goto out;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002659 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002660 !wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt71757432014-06-02 13:50:35 -07002661 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002662 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2663 "WPA: GCMP is used, but EAPOL-Key "
2664 "descriptor version (%d) is not 2", ver);
2665 goto out;
2666 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002667
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002668 if (sm->rx_replay_counter_set &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002669 os_memcmp(key->replay_counter, sm->rx_replay_counter,
2670 WPA_REPLAY_COUNTER_LEN) <= 0) {
2671 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2672 "WPA: EAPOL-Key Replay Counter did not increase - "
2673 "dropping packet");
2674 goto out;
2675 }
2676
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002677 if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
2678 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2679 "WPA: Unsupported SMK bit in key_info");
2680 goto out;
2681 }
2682
2683 if (!(key_info & WPA_KEY_INFO_ACK)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002684 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2685 "WPA: No Ack bit in key_info");
2686 goto out;
2687 }
2688
2689 if (key_info & WPA_KEY_INFO_REQUEST) {
2690 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2691 "WPA: EAPOL-Key with Request bit - dropped");
2692 goto out;
2693 }
2694
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002695 if ((key_info & WPA_KEY_INFO_MIC) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002696 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002697 goto out;
2698
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002699#ifdef CONFIG_FILS
2700 if (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
2701 if (wpa_supp_aead_decrypt(sm, tmp, data_len, &key_data_len))
2702 goto out;
2703 }
2704#endif /* CONFIG_FILS */
2705
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002706 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002707 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && mic_len) {
Hai Shalomce48b4a2018-09-05 11:41:35 -07002708 /*
2709 * Only decrypt the Key Data field if the frame's authenticity
2710 * was verified. When using AES-SIV (FILS), the MIC flag is not
2711 * set, so this check should only be performed if mic_len != 0
2712 * which is the case in this code branch.
2713 */
2714 if (!(key_info & WPA_KEY_INFO_MIC)) {
2715 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2716 "WPA: Ignore EAPOL-Key with encrypted but unauthenticated data");
2717 goto out;
2718 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002719 if (wpa_supplicant_decrypt_key_data(sm, key, mic_len,
2720 ver, key_data,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002721 &key_data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002722 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002723 }
2724
2725 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
2726 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
2727 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2728 "WPA: Ignored EAPOL-Key (Pairwise) with "
2729 "non-zero key index");
2730 goto out;
2731 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002732 if (key_info & (WPA_KEY_INFO_MIC |
2733 WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002734 /* 3/4 4-Way Handshake */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002735 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
2736 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002737 } else {
2738 /* 1/4 4-Way Handshake */
2739 wpa_supplicant_process_1_of_4(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002740 ver, key_data,
Sunil8cd6f4d2022-06-28 18:40:46 +00002741 key_data_len,
2742 encrypted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002743 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002744 } else {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002745 if ((mic_len && (key_info & WPA_KEY_INFO_MIC)) ||
2746 (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002747 /* 1/2 Group Key Handshake */
2748 wpa_supplicant_process_1_of_2(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002749 key_data, key_data_len,
2750 ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002751 } else {
2752 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002753 "WPA: EAPOL-Key (Group) without Mic/Encr bit - "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002754 "dropped");
2755 }
2756 }
2757
2758 ret = 1;
2759
2760out:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002761 bin_clear_free(tmp, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002762 return ret;
2763}
2764
2765
2766#ifdef CONFIG_CTRL_IFACE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002767static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
2768{
2769 switch (sm->key_mgmt) {
2770 case WPA_KEY_MGMT_IEEE8021X:
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002771 return ((sm->proto == WPA_PROTO_RSN ||
2772 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002773 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
2774 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
2775 case WPA_KEY_MGMT_PSK:
2776 return (sm->proto == WPA_PROTO_RSN ?
2777 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
2778 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
2779#ifdef CONFIG_IEEE80211R
2780 case WPA_KEY_MGMT_FT_IEEE8021X:
2781 return RSN_AUTH_KEY_MGMT_FT_802_1X;
2782 case WPA_KEY_MGMT_FT_PSK:
2783 return RSN_AUTH_KEY_MGMT_FT_PSK;
2784#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002785 case WPA_KEY_MGMT_IEEE8021X_SHA256:
2786 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
2787 case WPA_KEY_MGMT_PSK_SHA256:
2788 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002789 case WPA_KEY_MGMT_CCKM:
2790 return (sm->proto == WPA_PROTO_RSN ?
2791 RSN_AUTH_KEY_MGMT_CCKM:
2792 WPA_AUTH_KEY_MGMT_CCKM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002793 case WPA_KEY_MGMT_WPA_NONE:
2794 return WPA_AUTH_KEY_MGMT_NONE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002795 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
2796 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002797 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
2798 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002799 default:
2800 return 0;
2801 }
2802}
2803
2804
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002805#define RSN_SUITE "%02x-%02x-%02x-%d"
2806#define RSN_SUITE_ARG(s) \
2807((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2808
2809/**
2810 * wpa_sm_get_mib - Dump text list of MIB entries
2811 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2812 * @buf: Buffer for the list
2813 * @buflen: Length of the buffer
2814 * Returns: Number of bytes written to buffer
2815 *
2816 * This function is used fetch dot11 MIB variables.
2817 */
2818int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
2819{
2820 char pmkid_txt[PMKID_LEN * 2 + 1];
Hai Shalome21d4e82020-04-29 16:34:06 -07002821 bool rsna;
2822 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002823 size_t len;
2824
2825 if (sm->cur_pmksa) {
2826 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2827 sm->cur_pmksa->pmkid, PMKID_LEN);
2828 } else
2829 pmkid_txt[0] = '\0';
2830
Hai Shalome21d4e82020-04-29 16:34:06 -07002831 rsna = (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
2832 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
2833 sm->proto == WPA_PROTO_RSN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002834
2835 ret = os_snprintf(buf, buflen,
2836 "dot11RSNAOptionImplemented=TRUE\n"
2837 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2838 "dot11RSNAEnabled=%s\n"
2839 "dot11RSNAPreauthenticationEnabled=%s\n"
2840 "dot11RSNAConfigVersion=%d\n"
2841 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2842 "dot11RSNAConfigGroupCipherSize=%d\n"
2843 "dot11RSNAConfigPMKLifetime=%d\n"
2844 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2845 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2846 "dot11RSNAConfigSATimeout=%d\n",
2847 rsna ? "TRUE" : "FALSE",
2848 rsna ? "TRUE" : "FALSE",
2849 RSN_VERSION,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002850 wpa_cipher_key_len(sm->group_cipher) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002851 sm->dot11RSNAConfigPMKLifetime,
2852 sm->dot11RSNAConfigPMKReauthThreshold,
2853 sm->dot11RSNAConfigSATimeout);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002854 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002855 return 0;
2856 len = ret;
2857
2858 ret = os_snprintf(
2859 buf + len, buflen - len,
2860 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2861 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2862 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2863 "dot11RSNAPMKIDUsed=%s\n"
2864 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2865 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2866 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2867 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2868 "dot11RSNA4WayHandshakeFailures=%u\n",
2869 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002870 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2871 sm->pairwise_cipher)),
2872 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2873 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002874 pmkid_txt,
2875 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002876 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2877 sm->pairwise_cipher)),
2878 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2879 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002880 sm->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002881 if (!os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002882 len += ret;
2883
2884 return (int) len;
2885}
2886#endif /* CONFIG_CTRL_IFACE */
2887
2888
2889static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002890 void *ctx, enum pmksa_free_reason reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002891{
2892 struct wpa_sm *sm = ctx;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002893 int deauth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002894
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002895 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2896 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2897
2898 if (sm->cur_pmksa == entry) {
2899 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2900 "RSN: %s current PMKSA entry",
2901 reason == PMKSA_REPLACE ? "replaced" : "removed");
2902 pmksa_cache_clear_current(sm);
2903
2904 /*
2905 * If an entry is simply being replaced, there's no need to
2906 * deauthenticate because it will be immediately re-added.
2907 * This happens when EAP authentication is completed again
2908 * (reauth or failed PMKSA caching attempt).
2909 */
2910 if (reason != PMKSA_REPLACE)
2911 deauth = 1;
2912 }
2913
2914 if (reason == PMKSA_EXPIRE &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002915 (sm->pmk_len == entry->pmk_len &&
2916 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2917 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002918 "RSN: deauthenticating due to expired PMK");
2919 pmksa_cache_clear_current(sm);
2920 deauth = 1;
2921 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002922
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002923 if (deauth) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002924 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002925 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2926 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2927 }
2928}
2929
2930
Hai Shalomc1a21442022-02-04 13:43:00 -08002931static bool wpa_sm_pmksa_is_current_cb(struct rsn_pmksa_cache_entry *entry,
2932 void *ctx)
2933{
2934 struct wpa_sm *sm = ctx;
2935
2936 return sm->cur_pmksa == entry;
2937}
2938
2939
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002940/**
2941 * wpa_sm_init - Initialize WPA state machine
2942 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2943 * Returns: Pointer to the allocated WPA state machine data
2944 *
2945 * This function is used to allocate a new WPA state machine and the returned
2946 * value is passed to all WPA state machine calls.
2947 */
2948struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2949{
2950 struct wpa_sm *sm;
2951
2952 sm = os_zalloc(sizeof(*sm));
2953 if (sm == NULL)
2954 return NULL;
2955 dl_list_init(&sm->pmksa_candidates);
2956 sm->renew_snonce = 1;
2957 sm->ctx = ctx;
2958
2959 sm->dot11RSNAConfigPMKLifetime = 43200;
2960 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2961 sm->dot11RSNAConfigSATimeout = 60;
2962
Hai Shalomc1a21442022-02-04 13:43:00 -08002963 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb,
2964 wpa_sm_pmksa_is_current_cb, sm, sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002965 if (sm->pmksa == NULL) {
2966 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2967 "RSN: PMKSA cache initialization failed");
2968 os_free(sm);
2969 return NULL;
2970 }
2971
2972 return sm;
2973}
2974
2975
2976/**
2977 * wpa_sm_deinit - Deinitialize WPA state machine
2978 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2979 */
2980void wpa_sm_deinit(struct wpa_sm *sm)
2981{
2982 if (sm == NULL)
2983 return;
2984 pmksa_cache_deinit(sm->pmksa);
2985 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2986 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2987 os_free(sm->assoc_wpa_ie);
Hai Shalomc3565922019-10-28 11:58:20 -07002988 os_free(sm->assoc_rsnxe);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002989 os_free(sm->ap_wpa_ie);
2990 os_free(sm->ap_rsn_ie);
Hai Shalomc3565922019-10-28 11:58:20 -07002991 os_free(sm->ap_rsnxe);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002992 wpa_sm_drop_sa(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002993 os_free(sm->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002994#ifdef CONFIG_IEEE80211R
2995 os_free(sm->assoc_resp_ies);
2996#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002997#ifdef CONFIG_TESTING_OPTIONS
2998 wpabuf_free(sm->test_assoc_ie);
2999#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003000#ifdef CONFIG_FILS_SK_PFS
3001 crypto_ecdh_deinit(sm->fils_ecdh);
3002#endif /* CONFIG_FILS_SK_PFS */
3003#ifdef CONFIG_FILS
3004 wpabuf_free(sm->fils_ft_ies);
3005#endif /* CONFIG_FILS */
3006#ifdef CONFIG_OWE
3007 crypto_ecdh_deinit(sm->owe_ecdh);
3008#endif /* CONFIG_OWE */
Hai Shalom021b0b52019-04-10 11:17:58 -07003009#ifdef CONFIG_DPP2
3010 wpabuf_clear_free(sm->dpp_z);
3011#endif /* CONFIG_DPP2 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003012 os_free(sm);
3013}
3014
3015
3016/**
3017 * wpa_sm_notify_assoc - Notify WPA state machine about association
3018 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3019 * @bssid: The BSSID of the new association
3020 *
3021 * This function is called to let WPA state machine know that the connection
3022 * was established.
3023 */
3024void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
3025{
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003026 int clear_keys = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003027
3028 if (sm == NULL)
3029 return;
3030
3031 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3032 "WPA: Association event - clear replay counter");
3033 os_memcpy(sm->bssid, bssid, ETH_ALEN);
3034 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
3035 sm->rx_replay_counter_set = 0;
3036 sm->renew_snonce = 1;
3037 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
3038 rsn_preauth_deinit(sm);
3039
3040#ifdef CONFIG_IEEE80211R
3041 if (wpa_ft_is_completed(sm)) {
3042 /*
3043 * Clear portValid to kick EAPOL state machine to re-enter
3044 * AUTHENTICATED state to get the EAPOL port Authorized.
3045 */
Hai Shalome21d4e82020-04-29 16:34:06 -07003046 eapol_sm_notify_portValid(sm->eapol, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003047 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
3048
3049 /* Prepare for the next transition */
3050 wpa_ft_prepare_auth_request(sm, NULL);
3051
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003052 clear_keys = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07003053 sm->ft_protocol = 1;
3054 } else {
3055 sm->ft_protocol = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003056 }
3057#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003058#ifdef CONFIG_FILS
3059 if (sm->fils_completed) {
3060 /*
3061 * Clear portValid to kick EAPOL state machine to re-enter
3062 * AUTHENTICATED state to get the EAPOL port Authorized.
3063 */
3064 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003065 clear_keys = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003066 }
3067#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003068
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003069 if (clear_keys) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003070 /*
3071 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
3072 * this is not part of a Fast BSS Transition.
3073 */
3074 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
3075 sm->ptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003076 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003077 sm->tptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003078 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003079 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003080 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003081 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003082 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
Sunil8cd6f4d2022-06-28 18:40:46 +00003083 sm->tk_set = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003084 }
3085
3086#ifdef CONFIG_TDLS
3087 wpa_tdls_assoc(sm);
3088#endif /* CONFIG_TDLS */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003089
3090#ifdef CONFIG_P2P
3091 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
3092#endif /* CONFIG_P2P */
Hai Shalomfdcde762020-04-02 11:19:20 -07003093
3094 sm->keyidx_active = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003095}
3096
3097
3098/**
3099 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
3100 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3101 *
3102 * This function is called to let WPA state machine know that the connection
3103 * was lost. This will abort any existing pre-authentication session.
3104 */
3105void wpa_sm_notify_disassoc(struct wpa_sm *sm)
3106{
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07003107 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
3108 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003109 rsn_preauth_deinit(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003110 pmksa_cache_clear_current(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003111 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
3112 sm->dot11RSNA4WayHandshakeFailures++;
3113#ifdef CONFIG_TDLS
3114 wpa_tdls_disassoc(sm);
3115#endif /* CONFIG_TDLS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003116#ifdef CONFIG_FILS
3117 sm->fils_completed = 0;
3118#endif /* CONFIG_FILS */
Jouni Malinen4283f9e2017-09-22 12:06:37 +03003119#ifdef CONFIG_IEEE80211R
3120 sm->ft_reassoc_completed = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07003121 sm->ft_protocol = 0;
Jouni Malinen4283f9e2017-09-22 12:06:37 +03003122#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003123
3124 /* Keys are not needed in the WPA state machine anymore */
3125 wpa_sm_drop_sa(sm);
Hai Shalomfdcde762020-04-02 11:19:20 -07003126 sm->keyidx_active = 0;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07003127
3128 sm->msg_3_of_4_ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003129 os_memset(sm->bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003130}
3131
3132
3133/**
3134 * wpa_sm_set_pmk - Set PMK
3135 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3136 * @pmk: The new PMK
3137 * @pmk_len: The length of the new PMK in bytes
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003138 * @pmkid: Calculated PMKID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003139 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003140 *
3141 * Configure the PMK for WPA state machine.
3142 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003143void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003144 const u8 *pmkid, const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003145{
3146 if (sm == NULL)
3147 return;
3148
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003149 wpa_hexdump_key(MSG_DEBUG, "WPA: Set PMK based on external data",
3150 pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003151 sm->pmk_len = pmk_len;
3152 os_memcpy(sm->pmk, pmk, pmk_len);
3153
3154#ifdef CONFIG_IEEE80211R
3155 /* Set XXKey to be PSK for FT key derivation */
3156 sm->xxkey_len = pmk_len;
3157 os_memcpy(sm->xxkey, pmk, pmk_len);
3158#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003159
3160 if (bssid) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003161 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len,
3162 pmkid, NULL, 0, bssid,
3163 sm->own_addr,
3164 sm->network_ctx, sm->key_mgmt,
3165 NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003166 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003167}
3168
3169
3170/**
3171 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
3172 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3173 *
3174 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
3175 * will be cleared.
3176 */
3177void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
3178{
3179 if (sm == NULL)
3180 return;
3181
3182 if (sm->cur_pmksa) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003183 wpa_hexdump_key(MSG_DEBUG,
3184 "WPA: Set PMK based on current PMKSA",
3185 sm->cur_pmksa->pmk, sm->cur_pmksa->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003186 sm->pmk_len = sm->cur_pmksa->pmk_len;
3187 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
3188 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003189 wpa_printf(MSG_DEBUG, "WPA: No current PMKSA - clear PMK");
3190 sm->pmk_len = 0;
3191 os_memset(sm->pmk, 0, PMK_LEN_MAX);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003192 }
3193}
3194
3195
3196/**
3197 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
3198 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3199 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
3200 */
3201void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
3202{
3203 if (sm)
3204 sm->fast_reauth = fast_reauth;
3205}
3206
3207
3208/**
3209 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
3210 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3211 * @scard_ctx: Context pointer for smartcard related callback functions
3212 */
3213void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
3214{
3215 if (sm == NULL)
3216 return;
3217 sm->scard_ctx = scard_ctx;
3218 if (sm->preauth_eapol)
3219 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
3220}
3221
3222
3223/**
Hai Shalomfdcde762020-04-02 11:19:20 -07003224 * wpa_sm_set_config - Notification of current configuration change
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003225 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3226 * @config: Pointer to current network configuration
3227 *
3228 * Notify WPA state machine that configuration has changed. config will be
3229 * stored as a backpointer to network configuration. This can be %NULL to clear
3230 * the stored pointed.
3231 */
3232void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
3233{
3234 if (!sm)
3235 return;
3236
3237 if (config) {
3238 sm->network_ctx = config->network_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003239 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
3240 sm->proactive_key_caching = config->proactive_key_caching;
3241 sm->eap_workaround = config->eap_workaround;
3242 sm->eap_conf_ctx = config->eap_conf_ctx;
3243 if (config->ssid) {
3244 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
3245 sm->ssid_len = config->ssid_len;
3246 } else
3247 sm->ssid_len = 0;
3248 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003249 sm->p2p = config->p2p;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003250 sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
Hai Shalomfdcde762020-04-02 11:19:20 -07003251 sm->owe_ptk_workaround = config->owe_ptk_workaround;
Hai Shalom60840252021-02-19 19:02:11 -08003252 sm->force_kdk_derivation = config->force_kdk_derivation;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003253#ifdef CONFIG_FILS
3254 if (config->fils_cache_id) {
3255 sm->fils_cache_id_set = 1;
3256 os_memcpy(sm->fils_cache_id, config->fils_cache_id,
3257 FILS_CACHE_ID_LEN);
3258 } else {
3259 sm->fils_cache_id_set = 0;
3260 }
3261#endif /* CONFIG_FILS */
Hai Shalomfdcde762020-04-02 11:19:20 -07003262 sm->beacon_prot = config->beacon_prot;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003263 } else {
3264 sm->network_ctx = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003265 sm->allowed_pairwise_cipher = 0;
3266 sm->proactive_key_caching = 0;
3267 sm->eap_workaround = 0;
3268 sm->eap_conf_ctx = NULL;
3269 sm->ssid_len = 0;
3270 sm->wpa_ptk_rekey = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003271 sm->p2p = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003272 sm->wpa_rsc_relaxation = 0;
Hai Shalomfdcde762020-04-02 11:19:20 -07003273 sm->owe_ptk_workaround = 0;
3274 sm->beacon_prot = 0;
Hai Shalom60840252021-02-19 19:02:11 -08003275 sm->force_kdk_derivation = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003276 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003277}
3278
3279
3280/**
3281 * wpa_sm_set_own_addr - Set own MAC address
3282 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3283 * @addr: Own MAC address
3284 */
3285void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
3286{
3287 if (sm)
3288 os_memcpy(sm->own_addr, addr, ETH_ALEN);
3289}
3290
3291
3292/**
3293 * wpa_sm_set_ifname - Set network interface name
3294 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3295 * @ifname: Interface name
3296 * @bridge_ifname: Optional bridge interface name (for pre-auth)
3297 */
3298void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
3299 const char *bridge_ifname)
3300{
3301 if (sm) {
3302 sm->ifname = ifname;
3303 sm->bridge_ifname = bridge_ifname;
3304 }
3305}
3306
3307
3308/**
3309 * wpa_sm_set_eapol - Set EAPOL state machine pointer
3310 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3311 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
3312 */
3313void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
3314{
3315 if (sm)
3316 sm->eapol = eapol;
3317}
3318
3319
3320/**
3321 * wpa_sm_set_param - Set WPA state machine parameters
3322 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3323 * @param: Parameter field
3324 * @value: Parameter value
3325 * Returns: 0 on success, -1 on failure
3326 */
3327int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
3328 unsigned int value)
3329{
3330 int ret = 0;
3331
3332 if (sm == NULL)
3333 return -1;
3334
3335 switch (param) {
3336 case RSNA_PMK_LIFETIME:
3337 if (value > 0)
3338 sm->dot11RSNAConfigPMKLifetime = value;
3339 else
3340 ret = -1;
3341 break;
3342 case RSNA_PMK_REAUTH_THRESHOLD:
3343 if (value > 0 && value <= 100)
3344 sm->dot11RSNAConfigPMKReauthThreshold = value;
3345 else
3346 ret = -1;
3347 break;
3348 case RSNA_SA_TIMEOUT:
3349 if (value > 0)
3350 sm->dot11RSNAConfigSATimeout = value;
3351 else
3352 ret = -1;
3353 break;
3354 case WPA_PARAM_PROTO:
3355 sm->proto = value;
3356 break;
3357 case WPA_PARAM_PAIRWISE:
3358 sm->pairwise_cipher = value;
3359 break;
3360 case WPA_PARAM_GROUP:
3361 sm->group_cipher = value;
3362 break;
3363 case WPA_PARAM_KEY_MGMT:
3364 sm->key_mgmt = value;
3365 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003366 case WPA_PARAM_MGMT_GROUP:
3367 sm->mgmt_group_cipher = value;
3368 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003369 case WPA_PARAM_RSN_ENABLED:
3370 sm->rsn_enabled = value;
3371 break;
3372 case WPA_PARAM_MFP:
3373 sm->mfp = value;
3374 break;
Hai Shalom74f70d42019-02-11 14:42:39 -08003375 case WPA_PARAM_OCV:
3376 sm->ocv = value;
3377 break;
Hai Shalomc3565922019-10-28 11:58:20 -07003378 case WPA_PARAM_SAE_PWE:
3379 sm->sae_pwe = value;
3380 break;
Hai Shalom899fcc72020-10-19 14:38:18 -07003381 case WPA_PARAM_SAE_PK:
3382 sm->sae_pk = value;
3383 break;
Hai Shalomfdcde762020-04-02 11:19:20 -07003384 case WPA_PARAM_DENY_PTK0_REKEY:
3385 sm->wpa_deny_ptk0_rekey = value;
3386 break;
3387 case WPA_PARAM_EXT_KEY_ID:
3388 sm->ext_key_id = value;
3389 break;
3390 case WPA_PARAM_USE_EXT_KEY_ID:
3391 sm->use_ext_key_id = value;
3392 break;
Hai Shalomb755a2a2020-04-23 21:49:02 -07003393#ifdef CONFIG_TESTING_OPTIONS
3394 case WPA_PARAM_FT_RSNXE_USED:
3395 sm->ft_rsnxe_used = value;
3396 break;
Hai Shalom899fcc72020-10-19 14:38:18 -07003397 case WPA_PARAM_OCI_FREQ_EAPOL:
3398 sm->oci_freq_override_eapol = value;
3399 break;
3400 case WPA_PARAM_OCI_FREQ_EAPOL_G2:
3401 sm->oci_freq_override_eapol_g2 = value;
3402 break;
3403 case WPA_PARAM_OCI_FREQ_FT_ASSOC:
3404 sm->oci_freq_override_ft_assoc = value;
3405 break;
3406 case WPA_PARAM_OCI_FREQ_FILS_ASSOC:
3407 sm->oci_freq_override_fils_assoc = value;
3408 break;
Sunil Ravia04bd252022-05-02 22:54:18 -07003409 case WPA_PARAM_DISABLE_EAPOL_G2_TX:
3410 sm->disable_eapol_g2_tx = value;
3411 break;
Hai Shalomb755a2a2020-04-23 21:49:02 -07003412#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003413#ifdef CONFIG_DPP2
3414 case WPA_PARAM_DPP_PFS:
3415 sm->dpp_pfs = value;
3416 break;
3417#endif /* CONFIG_DPP2 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003418 default:
3419 break;
3420 }
3421
3422 return ret;
3423}
3424
3425
3426/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003427 * wpa_sm_get_status - Get WPA state machine
3428 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3429 * @buf: Buffer for status information
3430 * @buflen: Maximum buffer length
3431 * @verbose: Whether to include verbose status information
3432 * Returns: Number of bytes written to buf.
3433 *
3434 * Query WPA state machine for status information. This function fills in
3435 * a text area with current status information. If the buffer (buf) is not
3436 * large enough, status information will be truncated to fit the buffer.
3437 */
3438int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
3439 int verbose)
3440{
3441 char *pos = buf, *end = buf + buflen;
3442 int ret;
3443
3444 ret = os_snprintf(pos, end - pos,
3445 "pairwise_cipher=%s\n"
3446 "group_cipher=%s\n"
3447 "key_mgmt=%s\n",
3448 wpa_cipher_txt(sm->pairwise_cipher),
3449 wpa_cipher_txt(sm->group_cipher),
3450 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003451 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003452 return pos - buf;
3453 pos += ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003454
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003455#ifdef CONFIG_DPP2
3456 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
3457 ret = os_snprintf(pos, end - pos, "dpp_pfs=1\n");
3458 if (os_snprintf_error(end - pos, ret))
3459 return pos - buf;
3460 pos += ret;
3461 }
3462#endif /* CONFIG_DPP2 */
3463
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003464 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
3465 struct wpa_ie_data rsn;
3466 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
3467 >= 0 &&
3468 rsn.capabilities & (WPA_CAPABILITY_MFPR |
3469 WPA_CAPABILITY_MFPC)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003470 ret = os_snprintf(pos, end - pos, "pmf=%d\n"
3471 "mgmt_group_cipher=%s\n",
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003472 (rsn.capabilities &
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003473 WPA_CAPABILITY_MFPR) ? 2 : 1,
3474 wpa_cipher_txt(
3475 sm->mgmt_group_cipher));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003476 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003477 return pos - buf;
3478 pos += ret;
3479 }
3480 }
3481
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003482 return pos - buf;
3483}
3484
3485
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003486int wpa_sm_pmf_enabled(struct wpa_sm *sm)
3487{
3488 struct wpa_ie_data rsn;
3489
3490 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
3491 return 0;
3492
3493 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
3494 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
3495 return 1;
3496
3497 return 0;
3498}
3499
3500
Hai Shalomfdcde762020-04-02 11:19:20 -07003501int wpa_sm_ext_key_id(struct wpa_sm *sm)
3502{
3503 return sm ? sm->ext_key_id : 0;
3504}
3505
3506
3507int wpa_sm_ext_key_id_active(struct wpa_sm *sm)
3508{
3509 return sm ? sm->use_ext_key_id : 0;
3510}
3511
3512
Hai Shalom74f70d42019-02-11 14:42:39 -08003513int wpa_sm_ocv_enabled(struct wpa_sm *sm)
3514{
3515 struct wpa_ie_data rsn;
3516
3517 if (!sm->ocv || !sm->ap_rsn_ie)
3518 return 0;
3519
3520 return wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len,
3521 &rsn) >= 0 &&
3522 (rsn.capabilities & WPA_CAPABILITY_OCVC);
3523}
3524
3525
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003526/**
3527 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
3528 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3529 * @wpa_ie: Pointer to buffer for WPA/RSN IE
3530 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
3531 * Returns: 0 on success, -1 on failure
3532 */
3533int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
3534 size_t *wpa_ie_len)
3535{
3536 int res;
3537
3538 if (sm == NULL)
3539 return -1;
3540
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003541#ifdef CONFIG_TESTING_OPTIONS
3542 if (sm->test_assoc_ie) {
3543 wpa_printf(MSG_DEBUG,
3544 "TESTING: Replace association WPA/RSN IE");
3545 if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
3546 return -1;
3547 os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
3548 wpabuf_len(sm->test_assoc_ie));
3549 res = wpabuf_len(sm->test_assoc_ie);
3550 } else
3551#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003552 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
3553 if (res < 0)
3554 return -1;
3555 *wpa_ie_len = res;
3556
3557 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
3558 wpa_ie, *wpa_ie_len);
3559
3560 if (sm->assoc_wpa_ie == NULL) {
3561 /*
3562 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
3563 * the correct version of the IE even if PMKSA caching is
3564 * aborted (which would remove PMKID from IE generation).
3565 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003566 sm->assoc_wpa_ie = os_memdup(wpa_ie, *wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003567 if (sm->assoc_wpa_ie == NULL)
3568 return -1;
3569
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003570 sm->assoc_wpa_ie_len = *wpa_ie_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003571 } else {
3572 wpa_hexdump(MSG_DEBUG,
3573 "WPA: Leave previously set WPA IE default",
3574 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003575 }
3576
3577 return 0;
3578}
3579
3580
3581/**
3582 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
3583 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3584 * @ie: Pointer to IE data (starting from id)
3585 * @len: IE length
3586 * Returns: 0 on success, -1 on failure
3587 *
3588 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
3589 * Request frame. The IE will be used to override the default value generated
3590 * with wpa_sm_set_assoc_wpa_ie_default().
3591 */
3592int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3593{
3594 if (sm == NULL)
3595 return -1;
3596
3597 os_free(sm->assoc_wpa_ie);
3598 if (ie == NULL || len == 0) {
3599 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3600 "WPA: clearing own WPA/RSN IE");
3601 sm->assoc_wpa_ie = NULL;
3602 sm->assoc_wpa_ie_len = 0;
3603 } else {
3604 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003605 sm->assoc_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003606 if (sm->assoc_wpa_ie == NULL)
3607 return -1;
3608
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003609 sm->assoc_wpa_ie_len = len;
3610 }
3611
3612 return 0;
3613}
3614
3615
3616/**
Hai Shalomc3565922019-10-28 11:58:20 -07003617 * wpa_sm_set_assoc_rsnxe_default - Generate own RSNXE from configuration
3618 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3619 * @rsnxe: Pointer to buffer for RSNXE
3620 * @rsnxe_len: Pointer to the length of the rsne buffer
3621 * Returns: 0 on success, -1 on failure
3622 */
3623int wpa_sm_set_assoc_rsnxe_default(struct wpa_sm *sm, u8 *rsnxe,
3624 size_t *rsnxe_len)
3625{
3626 int res;
3627
3628 if (!sm)
3629 return -1;
3630
3631 res = wpa_gen_rsnxe(sm, rsnxe, *rsnxe_len);
3632 if (res < 0)
3633 return -1;
3634 *rsnxe_len = res;
3635
3636 wpa_hexdump(MSG_DEBUG, "RSN: Set own RSNXE default", rsnxe, *rsnxe_len);
3637
3638 if (sm->assoc_rsnxe) {
3639 wpa_hexdump(MSG_DEBUG,
3640 "RSN: Leave previously set RSNXE default",
3641 sm->assoc_rsnxe, sm->assoc_rsnxe_len);
3642 } else if (*rsnxe_len > 0) {
3643 /*
3644 * Make a copy of the RSNXE so that 4-Way Handshake gets the
3645 * correct version of the IE even if it gets changed.
3646 */
3647 sm->assoc_rsnxe = os_memdup(rsnxe, *rsnxe_len);
3648 if (!sm->assoc_rsnxe)
3649 return -1;
3650
3651 sm->assoc_rsnxe_len = *rsnxe_len;
3652 }
3653
3654 return 0;
3655}
3656
3657
3658/**
3659 * wpa_sm_set_assoc_rsnxe - Set own RSNXE from (Re)AssocReq
3660 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3661 * @ie: Pointer to IE data (starting from id)
3662 * @len: IE length
3663 * Returns: 0 on success, -1 on failure
3664 *
3665 * Inform WPA state machine about the RSNXE used in (Re)Association Request
3666 * frame. The IE will be used to override the default value generated
3667 * with wpa_sm_set_assoc_rsnxe_default().
3668 */
3669int wpa_sm_set_assoc_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len)
3670{
3671 if (!sm)
3672 return -1;
3673
3674 os_free(sm->assoc_rsnxe);
3675 if (!ie || len == 0) {
3676 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3677 "RSN: clearing own RSNXE");
3678 sm->assoc_rsnxe = NULL;
3679 sm->assoc_rsnxe_len = 0;
3680 } else {
3681 wpa_hexdump(MSG_DEBUG, "RSN: set own RSNXE", ie, len);
3682 sm->assoc_rsnxe = os_memdup(ie, len);
3683 if (!sm->assoc_rsnxe)
3684 return -1;
3685
3686 sm->assoc_rsnxe_len = len;
3687 }
3688
3689 return 0;
3690}
3691
3692
3693/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003694 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
3695 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3696 * @ie: Pointer to IE data (starting from id)
3697 * @len: IE length
3698 * Returns: 0 on success, -1 on failure
3699 *
3700 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
3701 * frame.
3702 */
3703int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3704{
3705 if (sm == NULL)
3706 return -1;
3707
3708 os_free(sm->ap_wpa_ie);
3709 if (ie == NULL || len == 0) {
3710 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3711 "WPA: clearing AP WPA IE");
3712 sm->ap_wpa_ie = NULL;
3713 sm->ap_wpa_ie_len = 0;
3714 } else {
3715 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003716 sm->ap_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003717 if (sm->ap_wpa_ie == NULL)
3718 return -1;
3719
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003720 sm->ap_wpa_ie_len = len;
3721 }
3722
3723 return 0;
3724}
3725
3726
3727/**
3728 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
3729 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3730 * @ie: Pointer to IE data (starting from id)
3731 * @len: IE length
3732 * Returns: 0 on success, -1 on failure
3733 *
3734 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
3735 * frame.
3736 */
3737int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3738{
3739 if (sm == NULL)
3740 return -1;
3741
3742 os_free(sm->ap_rsn_ie);
3743 if (ie == NULL || len == 0) {
3744 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3745 "WPA: clearing AP RSN IE");
3746 sm->ap_rsn_ie = NULL;
3747 sm->ap_rsn_ie_len = 0;
3748 } else {
3749 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003750 sm->ap_rsn_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003751 if (sm->ap_rsn_ie == NULL)
3752 return -1;
3753
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003754 sm->ap_rsn_ie_len = len;
3755 }
3756
3757 return 0;
3758}
3759
3760
3761/**
Hai Shalomc3565922019-10-28 11:58:20 -07003762 * wpa_sm_set_ap_rsnxe - Set AP RSNXE from Beacon/ProbeResp
3763 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3764 * @ie: Pointer to IE data (starting from id)
3765 * @len: IE length
3766 * Returns: 0 on success, -1 on failure
3767 *
3768 * Inform WPA state machine about the RSNXE used in Beacon / Probe Response
3769 * frame.
3770 */
3771int wpa_sm_set_ap_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len)
3772{
3773 if (!sm)
3774 return -1;
3775
3776 os_free(sm->ap_rsnxe);
3777 if (!ie || len == 0) {
3778 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: clearing AP RSNXE");
3779 sm->ap_rsnxe = NULL;
3780 sm->ap_rsnxe_len = 0;
3781 } else {
3782 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSNXE", ie, len);
3783 sm->ap_rsnxe = os_memdup(ie, len);
3784 if (!sm->ap_rsnxe)
3785 return -1;
3786
3787 sm->ap_rsnxe_len = len;
3788 }
3789
3790 return 0;
3791}
3792
3793
3794/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003795 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
3796 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3797 * @data: Pointer to data area for parsing results
3798 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
3799 *
3800 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
3801 * parsed data into data.
3802 */
3803int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
3804{
3805 if (sm == NULL)
3806 return -1;
3807
3808 if (sm->assoc_wpa_ie == NULL) {
3809 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3810 "WPA: No WPA/RSN IE available from association info");
3811 return -1;
3812 }
3813 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
3814 return -2;
3815 return 0;
3816}
3817
3818
3819int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
3820{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003821 return pmksa_cache_list(sm->pmksa, buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003822}
3823
3824
Dmitry Shmidt29333592017-01-09 12:27:11 -08003825struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_head(struct wpa_sm *sm)
3826{
3827 return pmksa_cache_head(sm->pmksa);
3828}
3829
3830
3831struct rsn_pmksa_cache_entry *
3832wpa_sm_pmksa_cache_add_entry(struct wpa_sm *sm,
3833 struct rsn_pmksa_cache_entry * entry)
3834{
3835 return pmksa_cache_add_entry(sm->pmksa, entry);
3836}
3837
3838
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003839void wpa_sm_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
3840 const u8 *pmkid, const u8 *bssid,
3841 const u8 *fils_cache_id)
3842{
3843 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
3844 bssid, sm->own_addr, sm->network_ctx,
3845 sm->key_mgmt, fils_cache_id);
3846}
3847
3848
3849int wpa_sm_pmksa_exists(struct wpa_sm *sm, const u8 *bssid,
3850 const void *network_ctx)
3851{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003852 return pmksa_cache_get(sm->pmksa, bssid, NULL, network_ctx, 0) != NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003853}
3854
3855
Hai Shalom60840252021-02-19 19:02:11 -08003856struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_get(struct wpa_sm *sm,
3857 const u8 *aa,
3858 const u8 *pmkid,
3859 const void *network_ctx,
3860 int akmp)
3861{
3862 return pmksa_cache_get(sm->pmksa, aa, pmkid, network_ctx, akmp);
3863}
3864
3865
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003866void wpa_sm_drop_sa(struct wpa_sm *sm)
3867{
3868 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
3869 sm->ptk_set = 0;
3870 sm->tptk_set = 0;
Sunil8cd6f4d2022-06-28 18:40:46 +00003871 sm->tk_set = false;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003872 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003873 os_memset(sm->pmk, 0, sizeof(sm->pmk));
3874 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
3875 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003876 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003877 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003878 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003879 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003880#ifdef CONFIG_IEEE80211R
3881 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003882 sm->xxkey_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003883 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003884 sm->pmk_r0_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003885 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003886 sm->pmk_r1_len = 0;
Hai Shalom60840252021-02-19 19:02:11 -08003887#ifdef CONFIG_PASN
3888 os_free(sm->pasn_r1kh);
3889 sm->pasn_r1kh = NULL;
3890 sm->n_pasn_r1kh = 0;
3891#endif /* CONFIG_PASN */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003892#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003893}
3894
3895
3896int wpa_sm_has_ptk(struct wpa_sm *sm)
3897{
3898 if (sm == NULL)
3899 return 0;
3900 return sm->ptk_set;
3901}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003902
3903
Hai Shalomfdcde762020-04-02 11:19:20 -07003904int wpa_sm_has_ptk_installed(struct wpa_sm *sm)
3905{
3906 if (!sm)
3907 return 0;
Sunil8cd6f4d2022-06-28 18:40:46 +00003908 return sm->tk_set || sm->ptk.installed;
Hai Shalomfdcde762020-04-02 11:19:20 -07003909}
3910
3911
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003912void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
3913{
3914 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
3915}
3916
3917
3918void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
3919{
Hai Shalomc1a21442022-02-04 13:43:00 -08003920 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0, false);
3921}
3922
3923
3924void wpa_sm_external_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
3925{
3926 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0, true);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003927}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003928
Andy Kuoaba17c12022-04-14 16:05:31 +08003929#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Mir Ali677e7482020-11-12 19:49:02 +05303930void wpa_sm_install_pmk(struct wpa_sm *sm)
3931{
3932 /* In case the driver wants to handle re-assocs, pass it down the PMK. */
3933 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->pairwise_cipher), NULL, 0, 0, NULL, 0,
3934 (u8*)sm->pmk, sm->pmk_len, KEY_FLAG_PMK) < 0) {
3935 wpa_hexdump(MSG_DEBUG, "PSK: Install PMK to the driver for driver reassociations",
3936 (u8*)sm->pmk, sm->pmk_len);
3937 /* No harm if the driver doesn't support. */
3938 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
3939 "WPA: Failed to set PMK to the driver");
3940 }
3941}
Mir Alieaaf04e2021-06-07 12:17:29 +05303942
3943void wpa_sm_notify_brcm_ft_reassoc(struct wpa_sm *sm, const u8 *bssid)
3944{
3945 u8 buf[256];
3946 struct wpa_supplicant *wpa_s = sm->ctx->ctx;
3947
3948 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3949 "WPA: BRCM FT Reassociation event - clear replay counter");
3950 os_memcpy(sm->bssid, bssid, ETH_ALEN);
3951 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
3952 sm->rx_replay_counter_set = 0;
3953
3954 if (wpa_drv_driver_cmd(wpa_s, "GET_FTKEY", (char *)buf, sizeof(buf)) < 0) {
3955 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
3956 "WPA: Failed to get FT KEY information");
3957 wpa_supplicant_deauthenticate(
3958 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
3959
3960 } else {
3961 /* update kck and kek */
3962 os_memcpy(sm->ptk.kck, buf, 16);
3963 os_memcpy(sm->ptk.kek, buf + 16, 16);
3964 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3965 "WPA: Updated KCK and KEK after FT reassoc");
3966 }
3967}
Andy Kuoaba17c12022-04-14 16:05:31 +08003968#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
3969
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003970
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003971#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003972int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
3973{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003974 u16 keyinfo;
3975 u8 keylen; /* plaintext key len */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003976 u8 *key_rsc;
3977
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003978 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003979 struct wpa_gtk_data gd;
3980
3981 os_memset(&gd, 0, sizeof(gd));
3982 keylen = wpa_cipher_key_len(sm->group_cipher);
3983 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
3984 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
3985 if (gd.alg == WPA_ALG_NONE) {
3986 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
3987 return -1;
3988 }
3989
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003990 key_rsc = buf + 5;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003991 keyinfo = WPA_GET_LE16(buf + 2);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003992 gd.gtk_len = keylen;
3993 if (gd.gtk_len != buf[4]) {
3994 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
3995 gd.gtk_len, buf[4]);
3996 return -1;
3997 }
3998 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
3999 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
4000 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
4001
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004002 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004003
4004 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
4005 gd.gtk, gd.gtk_len);
Jouni Malinen58c0e962017-10-01 12:12:24 +03004006 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07004007 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004008 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
4009 "WNM mode");
4010 return -1;
4011 }
Hai Shalom81f62d82019-07-22 12:10:00 -07004012 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004013 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02004014 const struct wpa_igtk_kde *igtk;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07004015
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02004016 igtk = (const struct wpa_igtk_kde *) (buf + 2);
Jouni Malinen58c0e962017-10-01 12:12:24 +03004017 if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004018 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07004019 } else if (subelem_id == WNM_SLEEP_SUBELEM_BIGTK) {
4020 const struct wpa_bigtk_kde *bigtk;
4021
4022 bigtk = (const struct wpa_bigtk_kde *) (buf + 2);
4023 if (sm->beacon_prot &&
4024 wpa_supplicant_install_bigtk(sm, bigtk, 1) < 0)
4025 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004026 } else {
4027 wpa_printf(MSG_DEBUG, "Unknown element id");
4028 return -1;
4029 }
4030
4031 return 0;
4032}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004033#endif /* CONFIG_WNM */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004034
4035
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004036#ifdef CONFIG_P2P
4037
4038int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
4039{
4040 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
4041 return -1;
4042 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
4043 return 0;
4044}
4045
4046#endif /* CONFIG_P2P */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004047
4048
4049void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
4050{
4051 if (rx_replay_counter == NULL)
4052 return;
4053
4054 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
4055 WPA_REPLAY_COUNTER_LEN);
4056 sm->rx_replay_counter_set = 1;
4057 wpa_printf(MSG_DEBUG, "Updated key replay counter");
4058}
4059
4060
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004061void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
4062 const u8 *ptk_kck, size_t ptk_kck_len,
4063 const u8 *ptk_kek, size_t ptk_kek_len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004064{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004065 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
4066 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
4067 sm->ptk.kck_len = ptk_kck_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004068 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
4069 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004070 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
4071 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
4072 sm->ptk.kek_len = ptk_kek_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004073 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
4074 }
4075 sm->ptk_set = 1;
4076}
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08004077
4078
4079#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004080
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08004081void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
4082{
4083 wpabuf_free(sm->test_assoc_ie);
4084 sm->test_assoc_ie = buf;
4085}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004086
4087
4088const u8 * wpa_sm_get_anonce(struct wpa_sm *sm)
4089{
4090 return sm->anonce;
4091}
4092
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08004093#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004094
4095
Roshan Pius3a1667e2018-07-03 15:17:14 -07004096unsigned int wpa_sm_get_key_mgmt(struct wpa_sm *sm)
4097{
4098 return sm->key_mgmt;
4099}
4100
4101
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004102#ifdef CONFIG_FILS
4103
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004104struct wpabuf * fils_build_auth(struct wpa_sm *sm, int dh_group, const u8 *md)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004105{
4106 struct wpabuf *buf = NULL;
4107 struct wpabuf *erp_msg;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004108 struct wpabuf *pub = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004109
4110 erp_msg = eapol_sm_build_erp_reauth_start(sm->eapol);
4111 if (!erp_msg && !sm->cur_pmksa) {
4112 wpa_printf(MSG_DEBUG,
4113 "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS");
4114 goto fail;
4115 }
4116
4117 wpa_printf(MSG_DEBUG, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)",
4118 erp_msg != NULL, sm->cur_pmksa != NULL);
4119
4120 sm->fils_completed = 0;
4121
4122 if (!sm->assoc_wpa_ie) {
4123 wpa_printf(MSG_INFO, "FILS: No own RSN IE set for FILS");
4124 goto fail;
4125 }
4126
4127 if (random_get_bytes(sm->fils_nonce, FILS_NONCE_LEN) < 0 ||
4128 random_get_bytes(sm->fils_session, FILS_SESSION_LEN) < 0)
4129 goto fail;
4130
4131 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Nonce",
4132 sm->fils_nonce, FILS_NONCE_LEN);
4133 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Session",
4134 sm->fils_session, FILS_SESSION_LEN);
4135
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004136#ifdef CONFIG_FILS_SK_PFS
4137 sm->fils_dh_group = dh_group;
4138 if (dh_group) {
4139 crypto_ecdh_deinit(sm->fils_ecdh);
4140 sm->fils_ecdh = crypto_ecdh_init(dh_group);
4141 if (!sm->fils_ecdh) {
4142 wpa_printf(MSG_INFO,
4143 "FILS: Could not initialize ECDH with group %d",
4144 dh_group);
4145 goto fail;
4146 }
4147 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
4148 if (!pub)
4149 goto fail;
4150 wpa_hexdump_buf(MSG_DEBUG, "FILS: Element (DH public key)",
4151 pub);
4152 sm->fils_dh_elem_len = wpabuf_len(pub);
4153 }
4154#endif /* CONFIG_FILS_SK_PFS */
4155
4156 buf = wpabuf_alloc(1000 + sm->assoc_wpa_ie_len +
4157 (pub ? wpabuf_len(pub) : 0));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004158 if (!buf)
4159 goto fail;
4160
4161 /* Fields following the Authentication algorithm number field */
4162
4163 /* Authentication Transaction seq# */
4164 wpabuf_put_le16(buf, 1);
4165
4166 /* Status Code */
4167 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
4168
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004169 /* TODO: FILS PK */
4170#ifdef CONFIG_FILS_SK_PFS
4171 if (dh_group) {
4172 /* Finite Cyclic Group */
4173 wpabuf_put_le16(buf, dh_group);
4174 /* Element */
4175 wpabuf_put_buf(buf, pub);
4176 }
4177#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004178
4179 /* RSNE */
4180 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in FILS Authentication frame",
4181 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
4182 wpabuf_put_data(buf, sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
4183
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004184 if (md) {
4185 /* MDE when using FILS for FT initial association */
4186 struct rsn_mdie *mdie;
4187
4188 wpabuf_put_u8(buf, WLAN_EID_MOBILITY_DOMAIN);
4189 wpabuf_put_u8(buf, sizeof(*mdie));
4190 mdie = wpabuf_put(buf, sizeof(*mdie));
4191 os_memcpy(mdie->mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
4192 mdie->ft_capab = 0;
4193 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004194
4195 /* FILS Nonce */
4196 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4197 wpabuf_put_u8(buf, 1 + FILS_NONCE_LEN); /* Length */
4198 /* Element ID Extension */
4199 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_NONCE);
4200 wpabuf_put_data(buf, sm->fils_nonce, FILS_NONCE_LEN);
4201
4202 /* FILS Session */
4203 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4204 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
4205 /* Element ID Extension */
4206 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
4207 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
4208
Hai Shalomfdcde762020-04-02 11:19:20 -07004209 /* Wrapped Data */
Paul Stewart092955c2017-02-06 09:13:09 -08004210 sm->fils_erp_pmkid_set = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004211 if (erp_msg) {
4212 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4213 wpabuf_put_u8(buf, 1 + wpabuf_len(erp_msg)); /* Length */
4214 /* Element ID Extension */
Hai Shalomfdcde762020-04-02 11:19:20 -07004215 wpabuf_put_u8(buf, WLAN_EID_EXT_WRAPPED_DATA);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004216 wpabuf_put_buf(buf, erp_msg);
Paul Stewart092955c2017-02-06 09:13:09 -08004217 /* Calculate pending PMKID here so that we do not need to
4218 * maintain a copy of the EAP-Initiate/Reauth message. */
4219 if (fils_pmkid_erp(sm->key_mgmt, wpabuf_head(erp_msg),
4220 wpabuf_len(erp_msg),
4221 sm->fils_erp_pmkid) == 0)
4222 sm->fils_erp_pmkid_set = 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004223 }
4224
4225 wpa_hexdump_buf(MSG_DEBUG, "RSN: FILS fields for Authentication frame",
4226 buf);
4227
4228fail:
4229 wpabuf_free(erp_msg);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004230 wpabuf_free(pub);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004231 return buf;
4232}
4233
4234
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004235int fils_process_auth(struct wpa_sm *sm, const u8 *bssid, const u8 *data,
4236 size_t len)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004237{
4238 const u8 *pos, *end;
4239 struct ieee802_11_elems elems;
4240 struct wpa_ie_data rsn;
4241 int pmkid_match = 0;
4242 u8 ick[FILS_ICK_MAX_LEN];
4243 size_t ick_len;
4244 int res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004245 struct wpabuf *dh_ss = NULL;
4246 const u8 *g_sta = NULL;
4247 size_t g_sta_len = 0;
4248 const u8 *g_ap = NULL;
Hai Shalom60840252021-02-19 19:02:11 -08004249 size_t g_ap_len = 0, kdk_len;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004250 struct wpabuf *pub = NULL;
4251
4252 os_memcpy(sm->bssid, bssid, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004253
4254 wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
4255 data, len);
4256 pos = data;
4257 end = data + len;
4258
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004259 /* TODO: FILS PK */
4260#ifdef CONFIG_FILS_SK_PFS
4261 if (sm->fils_dh_group) {
4262 u16 group;
4263
4264 /* Using FILS PFS */
4265
4266 /* Finite Cyclic Group */
4267 if (end - pos < 2) {
4268 wpa_printf(MSG_DEBUG,
4269 "FILS: No room for Finite Cyclic Group");
4270 goto fail;
4271 }
4272 group = WPA_GET_LE16(pos);
4273 pos += 2;
4274 if (group != sm->fils_dh_group) {
4275 wpa_printf(MSG_DEBUG,
4276 "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)",
4277 group, sm->fils_dh_group);
4278 goto fail;
4279 }
4280
4281 /* Element */
4282 if ((size_t) (end - pos) < sm->fils_dh_elem_len) {
4283 wpa_printf(MSG_DEBUG, "FILS: No room for Element");
4284 goto fail;
4285 }
4286
4287 if (!sm->fils_ecdh) {
4288 wpa_printf(MSG_DEBUG, "FILS: No ECDH state available");
4289 goto fail;
4290 }
4291 dh_ss = crypto_ecdh_set_peerkey(sm->fils_ecdh, 1, pos,
4292 sm->fils_dh_elem_len);
4293 if (!dh_ss) {
4294 wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
4295 goto fail;
4296 }
4297 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", dh_ss);
4298 g_ap = pos;
4299 g_ap_len = sm->fils_dh_elem_len;
4300 pos += sm->fils_dh_elem_len;
4301 }
4302#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004303
4304 wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
4305 if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
4306 wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004307 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004308 }
4309
4310 /* RSNE */
4311 wpa_hexdump(MSG_DEBUG, "FILS: RSN element", elems.rsn_ie,
4312 elems.rsn_ie_len);
4313 if (!elems.rsn_ie ||
4314 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
4315 &rsn) < 0) {
4316 wpa_printf(MSG_DEBUG, "FILS: No RSN element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004317 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004318 }
4319
4320 if (!elems.fils_nonce) {
4321 wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004322 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004323 }
4324 os_memcpy(sm->fils_anonce, elems.fils_nonce, FILS_NONCE_LEN);
4325 wpa_hexdump(MSG_DEBUG, "FILS: ANonce", sm->fils_anonce, FILS_NONCE_LEN);
4326
Roshan Pius3a1667e2018-07-03 15:17:14 -07004327#ifdef CONFIG_IEEE80211R
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004328 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
4329 struct wpa_ft_ies parse;
4330
4331 if (!elems.mdie || !elems.ftie) {
4332 wpa_printf(MSG_DEBUG, "FILS+FT: No MDE or FTE");
4333 goto fail;
4334 }
4335
Roshan Pius3a1667e2018-07-03 15:17:14 -07004336 if (wpa_ft_parse_ies(pos, end - pos, &parse,
4337 wpa_key_mgmt_sha384(sm->key_mgmt)) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004338 wpa_printf(MSG_DEBUG, "FILS+FT: Failed to parse IEs");
4339 goto fail;
4340 }
4341
4342 if (!parse.r0kh_id) {
4343 wpa_printf(MSG_DEBUG,
4344 "FILS+FT: No R0KH-ID subelem in FTE");
4345 goto fail;
4346 }
4347 os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
4348 sm->r0kh_id_len = parse.r0kh_id_len;
4349 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
4350 sm->r0kh_id, sm->r0kh_id_len);
4351
4352 if (!parse.r1kh_id) {
4353 wpa_printf(MSG_DEBUG,
4354 "FILS+FT: No R1KH-ID subelem in FTE");
4355 goto fail;
4356 }
4357 os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
4358 wpa_hexdump(MSG_DEBUG, "FILS+FT: R1KH-ID",
4359 sm->r1kh_id, FT_R1KH_ID_LEN);
4360
4361 /* TODO: Check MDE and FTE payload */
4362
4363 wpabuf_free(sm->fils_ft_ies);
4364 sm->fils_ft_ies = wpabuf_alloc(2 + elems.mdie_len +
4365 2 + elems.ftie_len);
4366 if (!sm->fils_ft_ies)
4367 goto fail;
4368 wpabuf_put_data(sm->fils_ft_ies, elems.mdie - 2,
4369 2 + elems.mdie_len);
4370 wpabuf_put_data(sm->fils_ft_ies, elems.ftie - 2,
4371 2 + elems.ftie_len);
4372 } else {
4373 wpabuf_free(sm->fils_ft_ies);
4374 sm->fils_ft_ies = NULL;
4375 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07004376#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004377
4378 /* PMKID List */
4379 if (rsn.pmkid && rsn.num_pmkid > 0) {
4380 wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
4381 rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
4382
4383 if (rsn.num_pmkid != 1) {
4384 wpa_printf(MSG_DEBUG, "FILS: Invalid PMKID selection");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004385 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004386 }
4387 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", rsn.pmkid, PMKID_LEN);
4388 if (os_memcmp(sm->cur_pmksa->pmkid, rsn.pmkid, PMKID_LEN) != 0)
4389 {
4390 wpa_printf(MSG_DEBUG, "FILS: PMKID mismatch");
4391 wpa_hexdump(MSG_DEBUG, "FILS: Expected PMKID",
4392 sm->cur_pmksa->pmkid, PMKID_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004393 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004394 }
4395 wpa_printf(MSG_DEBUG,
4396 "FILS: Matching PMKID - continue using PMKSA caching");
4397 pmkid_match = 1;
4398 }
4399 if (!pmkid_match && sm->cur_pmksa) {
4400 wpa_printf(MSG_DEBUG,
4401 "FILS: No PMKID match - cannot use cached PMKSA entry");
4402 sm->cur_pmksa = NULL;
4403 }
4404
4405 /* FILS Session */
4406 if (!elems.fils_session) {
4407 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004408 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004409 }
4410 wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
4411 FILS_SESSION_LEN);
4412 if (os_memcmp(sm->fils_session, elems.fils_session, FILS_SESSION_LEN)
4413 != 0) {
4414 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
4415 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
4416 sm->fils_session, FILS_SESSION_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004417 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004418 }
4419
Hai Shalomfdcde762020-04-02 11:19:20 -07004420 /* Wrapped Data */
4421 if (!sm->cur_pmksa && elems.wrapped_data) {
Paul Stewart092955c2017-02-06 09:13:09 -08004422 u8 rmsk[ERP_MAX_KEY_LEN];
4423 size_t rmsk_len;
4424
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004425 wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
Hai Shalomfdcde762020-04-02 11:19:20 -07004426 elems.wrapped_data,
4427 elems.wrapped_data_len);
4428 eapol_sm_process_erp_finish(sm->eapol, elems.wrapped_data,
4429 elems.wrapped_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004430 if (eapol_sm_failed(sm->eapol))
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004431 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004432
Paul Stewart092955c2017-02-06 09:13:09 -08004433 rmsk_len = ERP_MAX_KEY_LEN;
4434 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
4435 if (res == PMK_LEN) {
4436 rmsk_len = PMK_LEN;
4437 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
4438 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004439 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004440 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004441
Paul Stewart092955c2017-02-06 09:13:09 -08004442 res = fils_rmsk_to_pmk(sm->key_mgmt, rmsk, rmsk_len,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004443 sm->fils_nonce, sm->fils_anonce,
4444 dh_ss ? wpabuf_head(dh_ss) : NULL,
4445 dh_ss ? wpabuf_len(dh_ss) : 0,
Paul Stewart092955c2017-02-06 09:13:09 -08004446 sm->pmk, &sm->pmk_len);
Hai Shalom81f62d82019-07-22 12:10:00 -07004447 forced_memzero(rmsk, sizeof(rmsk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004448
4449 /* Don't use DHss in PTK derivation if PMKSA caching is not
4450 * used. */
4451 wpabuf_clear_free(dh_ss);
4452 dh_ss = NULL;
4453
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004454 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004455 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08004456
4457 if (!sm->fils_erp_pmkid_set) {
4458 wpa_printf(MSG_DEBUG, "FILS: PMKID not available");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004459 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08004460 }
4461 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", sm->fils_erp_pmkid,
4462 PMKID_LEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004463 wpa_printf(MSG_DEBUG, "FILS: ERP processing succeeded - add PMKSA cache entry for the result");
Paul Stewart092955c2017-02-06 09:13:09 -08004464 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
4465 sm->fils_erp_pmkid, NULL, 0,
4466 sm->bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004467 sm->network_ctx, sm->key_mgmt,
4468 NULL);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004469 }
4470
4471 if (!sm->cur_pmksa) {
4472 wpa_printf(MSG_DEBUG,
4473 "FILS: No remaining options to continue FILS authentication");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004474 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004475 }
4476
Hai Shalom60840252021-02-19 19:02:11 -08004477 if (sm->force_kdk_derivation ||
Hai Shalomc1a21442022-02-04 13:43:00 -08004478 (sm->secure_ltf &&
4479 ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
Hai Shalom60840252021-02-19 19:02:11 -08004480 kdk_len = WPA_KDK_MAX_LEN;
4481 else
4482 kdk_len = 0;
4483
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004484 if (fils_pmk_to_ptk(sm->pmk, sm->pmk_len, sm->own_addr, sm->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004485 sm->fils_nonce, sm->fils_anonce,
4486 dh_ss ? wpabuf_head(dh_ss) : NULL,
4487 dh_ss ? wpabuf_len(dh_ss) : 0,
4488 &sm->ptk, ick, &ick_len,
4489 sm->key_mgmt, sm->pairwise_cipher,
Hai Shalom60840252021-02-19 19:02:11 -08004490 sm->fils_ft, &sm->fils_ft_len,
4491 kdk_len) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004492 wpa_printf(MSG_DEBUG, "FILS: Failed to derive PTK");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004493 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004494 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004495
4496 wpabuf_clear_free(dh_ss);
4497 dh_ss = NULL;
4498
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004499 sm->ptk_set = 1;
4500 sm->tptk_set = 0;
4501 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
4502
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004503#ifdef CONFIG_FILS_SK_PFS
4504 if (sm->fils_dh_group) {
4505 if (!sm->fils_ecdh) {
4506 wpa_printf(MSG_INFO, "FILS: ECDH not initialized");
4507 goto fail;
4508 }
4509 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
4510 if (!pub)
4511 goto fail;
4512 wpa_hexdump_buf(MSG_DEBUG, "FILS: gSTA", pub);
4513 g_sta = wpabuf_head(pub);
4514 g_sta_len = wpabuf_len(pub);
4515 if (!g_ap) {
4516 wpa_printf(MSG_INFO, "FILS: gAP not available");
4517 goto fail;
4518 }
4519 wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len);
4520 }
4521#endif /* CONFIG_FILS_SK_PFS */
4522
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004523 res = fils_key_auth_sk(ick, ick_len, sm->fils_nonce,
4524 sm->fils_anonce, sm->own_addr, sm->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004525 g_sta, g_sta_len, g_ap, g_ap_len,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004526 sm->key_mgmt, sm->fils_key_auth_sta,
4527 sm->fils_key_auth_ap,
4528 &sm->fils_key_auth_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004529 wpabuf_free(pub);
Hai Shalom81f62d82019-07-22 12:10:00 -07004530 forced_memzero(ick, sizeof(ick));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004531 return res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004532fail:
4533 wpabuf_free(pub);
4534 wpabuf_clear_free(dh_ss);
4535 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004536}
4537
4538
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004539#ifdef CONFIG_IEEE80211R
4540static int fils_ft_build_assoc_req_rsne(struct wpa_sm *sm, struct wpabuf *buf)
4541{
4542 struct rsn_ie_hdr *rsnie;
4543 u16 capab;
4544 u8 *pos;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004545 int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004546
4547 /* RSNIE[PMKR0Name/PMKR1Name] */
4548 rsnie = wpabuf_put(buf, sizeof(*rsnie));
4549 rsnie->elem_id = WLAN_EID_RSN;
4550 WPA_PUT_LE16(rsnie->version, RSN_VERSION);
4551
4552 /* Group Suite Selector */
4553 if (!wpa_cipher_valid_group(sm->group_cipher)) {
4554 wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
4555 sm->group_cipher);
4556 return -1;
4557 }
4558 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4559 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
4560 sm->group_cipher));
4561
4562 /* Pairwise Suite Count */
4563 wpabuf_put_le16(buf, 1);
4564
4565 /* Pairwise Suite List */
4566 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
4567 wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
4568 sm->pairwise_cipher);
4569 return -1;
4570 }
4571 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4572 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
4573 sm->pairwise_cipher));
4574
4575 /* Authenticated Key Management Suite Count */
4576 wpabuf_put_le16(buf, 1);
4577
4578 /* Authenticated Key Management Suite List */
4579 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4580 if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256)
4581 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
4582 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384)
4583 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
4584 else {
4585 wpa_printf(MSG_WARNING,
4586 "FILS+FT: Invalid key management type (%d)",
4587 sm->key_mgmt);
4588 return -1;
4589 }
4590
4591 /* RSN Capabilities */
4592 capab = 0;
Hai Shalomc3565922019-10-28 11:58:20 -07004593 if (sm->mfp)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004594 capab |= WPA_CAPABILITY_MFPC;
Hai Shalomc3565922019-10-28 11:58:20 -07004595 if (sm->mfp == 2)
4596 capab |= WPA_CAPABILITY_MFPR;
Hai Shalom74f70d42019-02-11 14:42:39 -08004597 if (sm->ocv)
4598 capab |= WPA_CAPABILITY_OCVC;
Hai Shalomfdcde762020-04-02 11:19:20 -07004599 if (sm->ext_key_id)
4600 capab |= WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004601 wpabuf_put_le16(buf, capab);
4602
4603 /* PMKID Count */
4604 wpabuf_put_le16(buf, 1);
4605
4606 /* PMKID List [PMKR1Name] */
4607 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: XXKey (FILS-FT)",
4608 sm->fils_ft, sm->fils_ft_len);
4609 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: SSID", sm->ssid, sm->ssid_len);
4610 wpa_hexdump(MSG_DEBUG, "FILS+FT: MDID",
4611 sm->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
4612 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
4613 sm->r0kh_id, sm->r0kh_id_len);
4614 if (wpa_derive_pmk_r0(sm->fils_ft, sm->fils_ft_len, sm->ssid,
4615 sm->ssid_len, sm->mobility_domain,
4616 sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004617 sm->pmk_r0, sm->pmk_r0_name, use_sha384) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004618 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMK-R0");
4619 return -1;
4620 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07004621 sm->pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004622 wpa_printf(MSG_DEBUG, "FILS+FT: R1KH-ID: " MACSTR,
4623 MAC2STR(sm->r1kh_id));
4624 pos = wpabuf_put(buf, WPA_PMK_NAME_LEN);
4625 if (wpa_derive_pmk_r1_name(sm->pmk_r0_name, sm->r1kh_id, sm->own_addr,
Hai Shalom021b0b52019-04-10 11:17:58 -07004626 sm->pmk_r1_name, use_sha384) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004627 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMKR1Name");
4628 return -1;
4629 }
Hai Shalom021b0b52019-04-10 11:17:58 -07004630 os_memcpy(pos, sm->pmk_r1_name, WPA_PMK_NAME_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004631
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004632 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
4633 /* Management Group Cipher Suite */
4634 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4635 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
4636 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004637
4638 rsnie->len = ((u8 *) wpabuf_put(buf, 0) - (u8 *) rsnie) - 2;
4639 return 0;
4640}
4641#endif /* CONFIG_IEEE80211R */
4642
4643
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004644struct wpabuf * fils_build_assoc_req(struct wpa_sm *sm, const u8 **kek,
4645 size_t *kek_len, const u8 **snonce,
Paul Stewart092955c2017-02-06 09:13:09 -08004646 const u8 **anonce,
4647 const struct wpabuf **hlp,
4648 unsigned int num_hlp)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004649{
4650 struct wpabuf *buf;
Paul Stewart092955c2017-02-06 09:13:09 -08004651 size_t len;
4652 unsigned int i;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004653
Paul Stewart092955c2017-02-06 09:13:09 -08004654 len = 1000;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004655#ifdef CONFIG_IEEE80211R
4656 if (sm->fils_ft_ies)
4657 len += wpabuf_len(sm->fils_ft_ies);
4658 if (wpa_key_mgmt_ft(sm->key_mgmt))
4659 len += 256;
4660#endif /* CONFIG_IEEE80211R */
Paul Stewart092955c2017-02-06 09:13:09 -08004661 for (i = 0; hlp && i < num_hlp; i++)
4662 len += 10 + wpabuf_len(hlp[i]);
4663 buf = wpabuf_alloc(len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004664 if (!buf)
4665 return NULL;
4666
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004667#ifdef CONFIG_IEEE80211R
4668 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
4669 /* MDE and FTE when using FILS+FT */
4670 wpabuf_put_buf(buf, sm->fils_ft_ies);
4671 /* RSNE with PMKR1Name in PMKID field */
4672 if (fils_ft_build_assoc_req_rsne(sm, buf) < 0) {
4673 wpabuf_free(buf);
4674 return NULL;
4675 }
4676 }
4677#endif /* CONFIG_IEEE80211R */
4678
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004679 /* FILS Session */
4680 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4681 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
4682 /* Element ID Extension */
4683 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
4684 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
4685
4686 /* Everything after FILS Session element gets encrypted in the driver
4687 * with KEK. The buffer returned from here is the plaintext version. */
4688
4689 /* TODO: FILS Public Key */
4690
4691 /* FILS Key Confirm */
4692 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4693 wpabuf_put_u8(buf, 1 + sm->fils_key_auth_len); /* Length */
4694 /* Element ID Extension */
4695 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_KEY_CONFIRM);
4696 wpabuf_put_data(buf, sm->fils_key_auth_sta, sm->fils_key_auth_len);
4697
Paul Stewart092955c2017-02-06 09:13:09 -08004698 /* FILS HLP Container */
4699 for (i = 0; hlp && i < num_hlp; i++) {
4700 const u8 *pos = wpabuf_head(hlp[i]);
4701 size_t left = wpabuf_len(hlp[i]);
4702
4703 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4704 if (left <= 254)
4705 len = 1 + left;
4706 else
4707 len = 255;
4708 wpabuf_put_u8(buf, len); /* Length */
4709 /* Element ID Extension */
4710 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_HLP_CONTAINER);
4711 /* Destination MAC Address, Source MAC Address, HLP Packet.
4712 * HLP Packet is in MSDU format (i.e., included the LLC/SNAP
4713 * header when LPD is used). */
4714 wpabuf_put_data(buf, pos, len - 1);
4715 pos += len - 1;
4716 left -= len - 1;
4717 while (left) {
4718 wpabuf_put_u8(buf, WLAN_EID_FRAGMENT);
4719 len = left > 255 ? 255 : left;
4720 wpabuf_put_u8(buf, len);
4721 wpabuf_put_data(buf, pos, len);
4722 pos += len;
4723 left -= len;
4724 }
4725 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004726
4727 /* TODO: FILS IP Address Assignment */
4728
Hai Shalom74f70d42019-02-11 14:42:39 -08004729#ifdef CONFIG_OCV
4730 if (wpa_sm_ocv_enabled(sm)) {
4731 struct wpa_channel_info ci;
4732 u8 *pos;
4733
4734 if (wpa_sm_channel_info(sm, &ci) != 0) {
4735 wpa_printf(MSG_WARNING,
4736 "FILS: Failed to get channel info for OCI element");
4737 wpabuf_free(buf);
4738 return NULL;
4739 }
Hai Shalom899fcc72020-10-19 14:38:18 -07004740#ifdef CONFIG_TESTING_OPTIONS
4741 if (sm->oci_freq_override_fils_assoc) {
4742 wpa_printf(MSG_INFO,
4743 "TEST: Override OCI KDE frequency %d -> %d MHz",
4744 ci.frequency,
4745 sm->oci_freq_override_fils_assoc);
4746 ci.frequency = sm->oci_freq_override_fils_assoc;
4747 }
4748#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -08004749
4750 pos = wpabuf_put(buf, OCV_OCI_EXTENDED_LEN);
4751 if (ocv_insert_extended_oci(&ci, pos) < 0) {
4752 wpabuf_free(buf);
4753 return NULL;
4754 }
4755 }
4756#endif /* CONFIG_OCV */
4757
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004758 wpa_hexdump_buf(MSG_DEBUG, "FILS: Association Request plaintext", buf);
4759
4760 *kek = sm->ptk.kek;
4761 *kek_len = sm->ptk.kek_len;
4762 wpa_hexdump_key(MSG_DEBUG, "FILS: KEK for AEAD", *kek, *kek_len);
4763 *snonce = sm->fils_nonce;
4764 wpa_hexdump(MSG_DEBUG, "FILS: SNonce for AEAD AAD",
4765 *snonce, FILS_NONCE_LEN);
4766 *anonce = sm->fils_anonce;
4767 wpa_hexdump(MSG_DEBUG, "FILS: ANonce for AEAD AAD",
4768 *anonce, FILS_NONCE_LEN);
4769
4770 return buf;
4771}
4772
4773
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004774static void fils_process_hlp_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
4775{
4776 const u8 *pos, *end;
4777
4778 wpa_hexdump(MSG_MSGDUMP, "FILS: HLP response", resp, len);
4779 if (len < 2 * ETH_ALEN)
4780 return;
4781 pos = resp + 2 * ETH_ALEN;
4782 end = resp + len;
4783 if (end - pos >= 6 &&
4784 os_memcmp(pos, "\xaa\xaa\x03\x00\x00\x00", 6) == 0)
4785 pos += 6; /* Remove SNAP/LLC header */
4786 wpa_sm_fils_hlp_rx(sm, resp, resp + ETH_ALEN, pos, end - pos);
4787}
4788
4789
4790static void fils_process_hlp_container(struct wpa_sm *sm, const u8 *pos,
4791 size_t len)
4792{
4793 const u8 *end = pos + len;
4794 u8 *tmp, *tmp_pos;
4795
4796 /* Check if there are any FILS HLP Container elements */
4797 while (end - pos >= 2) {
4798 if (2 + pos[1] > end - pos)
4799 return;
4800 if (pos[0] == WLAN_EID_EXTENSION &&
4801 pos[1] >= 1 + 2 * ETH_ALEN &&
4802 pos[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER)
4803 break;
4804 pos += 2 + pos[1];
4805 }
4806 if (end - pos < 2)
4807 return; /* No FILS HLP Container elements */
4808
4809 tmp = os_malloc(end - pos);
4810 if (!tmp)
4811 return;
4812
4813 while (end - pos >= 2) {
4814 if (2 + pos[1] > end - pos ||
4815 pos[0] != WLAN_EID_EXTENSION ||
4816 pos[1] < 1 + 2 * ETH_ALEN ||
4817 pos[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER)
4818 break;
4819 tmp_pos = tmp;
4820 os_memcpy(tmp_pos, pos + 3, pos[1] - 1);
4821 tmp_pos += pos[1] - 1;
4822 pos += 2 + pos[1];
4823
4824 /* Add possible fragments */
4825 while (end - pos >= 2 && pos[0] == WLAN_EID_FRAGMENT &&
4826 2 + pos[1] <= end - pos) {
4827 os_memcpy(tmp_pos, pos + 2, pos[1]);
4828 tmp_pos += pos[1];
4829 pos += 2 + pos[1];
4830 }
4831
4832 fils_process_hlp_resp(sm, tmp, tmp_pos - tmp);
4833 }
4834
4835 os_free(tmp);
4836}
4837
4838
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004839int fils_process_assoc_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
4840{
4841 const struct ieee80211_mgmt *mgmt;
4842 const u8 *end, *ie_start;
4843 struct ieee802_11_elems elems;
4844 int keylen, rsclen;
4845 enum wpa_alg alg;
4846 struct wpa_gtk_data gd;
4847 int maxkeylen;
4848 struct wpa_eapol_ie_parse kde;
4849
4850 if (!sm || !sm->ptk_set) {
4851 wpa_printf(MSG_DEBUG, "FILS: No KEK available");
4852 return -1;
4853 }
4854
4855 if (!wpa_key_mgmt_fils(sm->key_mgmt)) {
4856 wpa_printf(MSG_DEBUG, "FILS: Not a FILS AKM");
4857 return -1;
4858 }
4859
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004860 if (sm->fils_completed) {
4861 wpa_printf(MSG_DEBUG,
4862 "FILS: Association has already been completed for this FILS authentication - ignore unexpected retransmission");
4863 return -1;
4864 }
4865
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004866 wpa_hexdump(MSG_DEBUG, "FILS: (Re)Association Response frame",
4867 resp, len);
4868
4869 mgmt = (const struct ieee80211_mgmt *) resp;
4870 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp))
4871 return -1;
4872
4873 end = resp + len;
4874 /* Same offset for Association Response and Reassociation Response */
4875 ie_start = mgmt->u.assoc_resp.variable;
4876
4877 if (ieee802_11_parse_elems(ie_start, end - ie_start, &elems, 1) ==
4878 ParseFailed) {
4879 wpa_printf(MSG_DEBUG,
4880 "FILS: Failed to parse decrypted elements");
4881 goto fail;
4882 }
4883
4884 if (!elems.fils_session) {
4885 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
4886 return -1;
4887 }
4888 if (os_memcmp(elems.fils_session, sm->fils_session,
4889 FILS_SESSION_LEN) != 0) {
4890 wpa_printf(MSG_DEBUG, "FILS: FILS Session mismatch");
4891 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
4892 elems.fils_session, FILS_SESSION_LEN);
4893 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
4894 sm->fils_session, FILS_SESSION_LEN);
4895 }
4896
Hai Shalom81f62d82019-07-22 12:10:00 -07004897 if (!elems.rsn_ie) {
4898 wpa_printf(MSG_DEBUG,
4899 "FILS: No RSNE in (Re)Association Response");
4900 /* As an interop workaround, allow this for now since IEEE Std
4901 * 802.11ai-2016 did not include all the needed changes to make
4902 * a FILS AP include RSNE in the frame. This workaround might
4903 * eventually be removed and replaced with rejection (goto fail)
4904 * to follow a strict interpretation of the standard. */
4905 } else if (wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
4906 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
4907 elems.rsn_ie - 2, elems.rsn_ie_len + 2)) {
4908 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
4909 "FILS: RSNE mismatch between Beacon/Probe Response and (Re)Association Response");
4910 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in Beacon/Probe Response",
4911 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
4912 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in (Re)Association Response",
4913 elems.rsn_ie, elems.rsn_ie_len);
4914 goto fail;
4915 }
4916
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004917 /* TODO: FILS Public Key */
4918
4919 if (!elems.fils_key_confirm) {
4920 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
4921 goto fail;
4922 }
4923 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
4924 wpa_printf(MSG_DEBUG,
4925 "FILS: Unexpected Key-Auth length %d (expected %d)",
4926 elems.fils_key_confirm_len,
4927 (int) sm->fils_key_auth_len);
4928 goto fail;
4929 }
4930 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_ap,
4931 sm->fils_key_auth_len) != 0) {
4932 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
4933 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
4934 elems.fils_key_confirm,
4935 elems.fils_key_confirm_len);
4936 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
4937 sm->fils_key_auth_ap, sm->fils_key_auth_len);
4938 goto fail;
4939 }
4940
Hai Shalom74f70d42019-02-11 14:42:39 -08004941#ifdef CONFIG_OCV
4942 if (wpa_sm_ocv_enabled(sm)) {
4943 struct wpa_channel_info ci;
4944
4945 if (wpa_sm_channel_info(sm, &ci) != 0) {
4946 wpa_printf(MSG_WARNING,
4947 "Failed to get channel info to validate received OCI in FILS (Re)Association Response frame");
4948 goto fail;
4949 }
4950
4951 if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci,
4952 channel_width_to_int(ci.chanwidth),
Hai Shalom899fcc72020-10-19 14:38:18 -07004953 ci.seg1_idx) != OCI_SUCCESS) {
4954 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
4955 "addr=" MACSTR " frame=fils-assoc error=%s",
4956 MAC2STR(sm->bssid), ocv_errorstr);
Hai Shalom74f70d42019-02-11 14:42:39 -08004957 goto fail;
4958 }
4959 }
4960#endif /* CONFIG_OCV */
4961
Hai Shalom021b0b52019-04-10 11:17:58 -07004962#ifdef CONFIG_IEEE80211R
4963 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
4964 struct wpa_ie_data rsn;
4965
4966 /* Check that PMKR1Name derived by the AP matches */
4967 if (!elems.rsn_ie ||
4968 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
4969 &rsn) < 0 ||
4970 !rsn.pmkid || rsn.num_pmkid != 1 ||
4971 os_memcmp(rsn.pmkid, sm->pmk_r1_name,
4972 WPA_PMK_NAME_LEN) != 0) {
4973 wpa_printf(MSG_DEBUG,
4974 "FILS+FT: No RSNE[PMKR1Name] match in AssocResp");
4975 goto fail;
4976 }
4977 }
4978#endif /* CONFIG_IEEE80211R */
4979
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004980 /* Key Delivery */
4981 if (!elems.key_delivery) {
4982 wpa_printf(MSG_DEBUG, "FILS: No Key Delivery element");
4983 goto fail;
4984 }
4985
4986 /* Parse GTK and set the key to the driver */
4987 os_memset(&gd, 0, sizeof(gd));
4988 if (wpa_supplicant_parse_ies(elems.key_delivery + WPA_KEY_RSC_LEN,
4989 elems.key_delivery_len - WPA_KEY_RSC_LEN,
4990 &kde) < 0) {
4991 wpa_printf(MSG_DEBUG, "FILS: Failed to parse KDEs");
4992 goto fail;
4993 }
4994 if (!kde.gtk) {
4995 wpa_printf(MSG_DEBUG, "FILS: No GTK KDE");
4996 goto fail;
4997 }
4998 maxkeylen = gd.gtk_len = kde.gtk_len - 2;
4999 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
5000 gd.gtk_len, maxkeylen,
5001 &gd.key_rsc_len, &gd.alg))
5002 goto fail;
5003
5004 wpa_hexdump_key(MSG_DEBUG, "FILS: Received GTK", kde.gtk, kde.gtk_len);
5005 gd.keyidx = kde.gtk[0] & 0x3;
5006 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
5007 !!(kde.gtk[0] & BIT(2)));
5008 if (kde.gtk_len - 2 > sizeof(gd.gtk)) {
5009 wpa_printf(MSG_DEBUG, "FILS: Too long GTK in GTK KDE (len=%lu)",
5010 (unsigned long) kde.gtk_len - 2);
5011 goto fail;
5012 }
5013 os_memcpy(gd.gtk, kde.gtk + 2, kde.gtk_len - 2);
5014
5015 wpa_printf(MSG_DEBUG, "FILS: Set GTK to driver");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005016 if (wpa_supplicant_install_gtk(sm, &gd, elems.key_delivery, 0) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005017 wpa_printf(MSG_DEBUG, "FILS: Failed to set GTK");
5018 goto fail;
5019 }
5020
5021 if (ieee80211w_set_keys(sm, &kde) < 0) {
5022 wpa_printf(MSG_DEBUG, "FILS: Failed to set IGTK");
5023 goto fail;
5024 }
5025
5026 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
5027 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005028 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
5029 wpa_printf(MSG_DEBUG, "FILS: TK length mismatch: %u != %lu",
5030 keylen, (long unsigned int) sm->ptk.tk_len);
5031 goto fail;
5032 }
Hai Shalomfdcde762020-04-02 11:19:20 -07005033
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005034 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
5035 wpa_hexdump_key(MSG_DEBUG, "FILS: Set TK to driver",
5036 sm->ptk.tk, keylen);
5037 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, null_rsc, rsclen,
Hai Shalomfdcde762020-04-02 11:19:20 -07005038 sm->ptk.tk, keylen, KEY_FLAG_PAIRWISE_RX_TX) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005039 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
5040 "FILS: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
5041 MACSTR ")",
5042 alg, keylen, MAC2STR(sm->bssid));
5043 goto fail;
5044 }
5045
Hai Shalom60840252021-02-19 19:02:11 -08005046 wpa_sm_store_ptk(sm, sm->bssid, sm->pairwise_cipher,
5047 sm->dot11RSNAConfigPMKLifetime, &sm->ptk);
5048
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005049 /* TODO: TK could be cleared after auth frame exchange now that driver
5050 * takes care of association frame encryption/decryption. */
5051 /* TK is not needed anymore in supplicant */
5052 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005053 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +02005054 sm->ptk.installed = 1;
Sunil8cd6f4d2022-06-28 18:40:46 +00005055 sm->tk_set = true;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005056
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005057 /* FILS HLP Container */
5058 fils_process_hlp_container(sm, ie_start, end - ie_start);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005059
5060 /* TODO: FILS IP Address Assignment */
5061
5062 wpa_printf(MSG_DEBUG, "FILS: Auth+Assoc completed successfully");
5063 sm->fils_completed = 1;
Hai Shalom81f62d82019-07-22 12:10:00 -07005064 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005065
Hai Shalomfdcde762020-04-02 11:19:20 -07005066 if (kde.transition_disable)
5067 wpa_sm_transition_disable(sm, kde.transition_disable[0]);
5068
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005069 return 0;
5070fail:
Hai Shalom81f62d82019-07-22 12:10:00 -07005071 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005072 return -1;
5073}
5074
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005075
5076void wpa_sm_set_reset_fils_completed(struct wpa_sm *sm, int set)
5077{
5078 if (sm)
5079 sm->fils_completed = !!set;
5080}
5081
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005082#endif /* CONFIG_FILS */
5083
5084
5085int wpa_fils_is_completed(struct wpa_sm *sm)
5086{
5087#ifdef CONFIG_FILS
5088 return sm && sm->fils_completed;
5089#else /* CONFIG_FILS */
5090 return 0;
5091#endif /* CONFIG_FILS */
5092}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005093
5094
5095#ifdef CONFIG_OWE
5096
5097struct wpabuf * owe_build_assoc_req(struct wpa_sm *sm, u16 group)
5098{
5099 struct wpabuf *ie = NULL, *pub = NULL;
5100 size_t prime_len;
5101
5102 if (group == 19)
5103 prime_len = 32;
5104 else if (group == 20)
5105 prime_len = 48;
5106 else if (group == 21)
5107 prime_len = 66;
5108 else
5109 return NULL;
5110
5111 crypto_ecdh_deinit(sm->owe_ecdh);
5112 sm->owe_ecdh = crypto_ecdh_init(group);
5113 if (!sm->owe_ecdh)
5114 goto fail;
5115 sm->owe_group = group;
5116 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
5117 pub = wpabuf_zeropad(pub, prime_len);
5118 if (!pub)
5119 goto fail;
5120
5121 ie = wpabuf_alloc(5 + wpabuf_len(pub));
5122 if (!ie)
5123 goto fail;
5124 wpabuf_put_u8(ie, WLAN_EID_EXTENSION);
5125 wpabuf_put_u8(ie, 1 + 2 + wpabuf_len(pub));
5126 wpabuf_put_u8(ie, WLAN_EID_EXT_OWE_DH_PARAM);
5127 wpabuf_put_le16(ie, group);
5128 wpabuf_put_buf(ie, pub);
5129 wpabuf_free(pub);
5130 wpa_hexdump_buf(MSG_DEBUG, "OWE: Diffie-Hellman Parameter element",
5131 ie);
5132
5133 return ie;
5134fail:
5135 wpabuf_free(pub);
5136 crypto_ecdh_deinit(sm->owe_ecdh);
5137 sm->owe_ecdh = NULL;
5138 return NULL;
5139}
5140
5141
5142int owe_process_assoc_resp(struct wpa_sm *sm, const u8 *bssid,
5143 const u8 *resp_ies, size_t resp_ies_len)
5144{
5145 struct ieee802_11_elems elems;
5146 u16 group;
5147 struct wpabuf *secret, *pub, *hkey;
5148 int res;
5149 u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN];
5150 const char *info = "OWE Key Generation";
5151 const u8 *addr[2];
5152 size_t len[2];
5153 size_t hash_len, prime_len;
5154 struct wpa_ie_data data;
5155
5156 if (!resp_ies ||
5157 ieee802_11_parse_elems(resp_ies, resp_ies_len, &elems, 1) ==
5158 ParseFailed) {
5159 wpa_printf(MSG_INFO,
5160 "OWE: Could not parse Association Response frame elements");
5161 return -1;
5162 }
5163
5164 if (sm->cur_pmksa && elems.rsn_ie &&
5165 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, 2 + elems.rsn_ie_len,
5166 &data) == 0 &&
5167 data.num_pmkid == 1 && data.pmkid &&
5168 os_memcmp(sm->cur_pmksa->pmkid, data.pmkid, PMKID_LEN) == 0) {
5169 wpa_printf(MSG_DEBUG, "OWE: Use PMKSA caching");
5170 wpa_sm_set_pmk_from_pmksa(sm);
5171 return 0;
5172 }
5173
5174 if (!elems.owe_dh) {
5175 wpa_printf(MSG_INFO,
5176 "OWE: No Diffie-Hellman Parameter element found in Association Response frame");
5177 return -1;
5178 }
5179
5180 group = WPA_GET_LE16(elems.owe_dh);
5181 if (group != sm->owe_group) {
5182 wpa_printf(MSG_INFO,
5183 "OWE: Unexpected Diffie-Hellman group in response: %u",
5184 group);
5185 return -1;
5186 }
5187
5188 if (!sm->owe_ecdh) {
5189 wpa_printf(MSG_INFO, "OWE: No ECDH state available");
5190 return -1;
5191 }
5192
5193 if (group == 19)
5194 prime_len = 32;
5195 else if (group == 20)
5196 prime_len = 48;
5197 else if (group == 21)
5198 prime_len = 66;
5199 else
5200 return -1;
5201
5202 secret = crypto_ecdh_set_peerkey(sm->owe_ecdh, 0,
5203 elems.owe_dh + 2,
5204 elems.owe_dh_len - 2);
5205 secret = wpabuf_zeropad(secret, prime_len);
5206 if (!secret) {
5207 wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
5208 return -1;
5209 }
5210 wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret);
5211
5212 /* prk = HKDF-extract(C | A | group, z) */
5213
5214 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
5215 if (!pub) {
5216 wpabuf_clear_free(secret);
5217 return -1;
5218 }
5219
5220 /* PMKID = Truncate-128(Hash(C | A)) */
5221 addr[0] = wpabuf_head(pub);
5222 len[0] = wpabuf_len(pub);
5223 addr[1] = elems.owe_dh + 2;
5224 len[1] = elems.owe_dh_len - 2;
5225 if (group == 19) {
5226 res = sha256_vector(2, addr, len, pmkid);
5227 hash_len = SHA256_MAC_LEN;
5228 } else if (group == 20) {
5229 res = sha384_vector(2, addr, len, pmkid);
5230 hash_len = SHA384_MAC_LEN;
5231 } else if (group == 21) {
5232 res = sha512_vector(2, addr, len, pmkid);
5233 hash_len = SHA512_MAC_LEN;
5234 } else {
5235 res = -1;
5236 hash_len = 0;
5237 }
5238 pub = wpabuf_zeropad(pub, prime_len);
5239 if (res < 0 || !pub) {
5240 wpabuf_free(pub);
5241 wpabuf_clear_free(secret);
5242 return -1;
5243 }
5244
5245 hkey = wpabuf_alloc(wpabuf_len(pub) + elems.owe_dh_len - 2 + 2);
5246 if (!hkey) {
5247 wpabuf_free(pub);
5248 wpabuf_clear_free(secret);
5249 return -1;
5250 }
5251
5252 wpabuf_put_buf(hkey, pub); /* C */
5253 wpabuf_free(pub);
5254 wpabuf_put_data(hkey, elems.owe_dh + 2, elems.owe_dh_len - 2); /* A */
5255 wpabuf_put_le16(hkey, sm->owe_group); /* group */
5256 if (group == 19)
5257 res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
5258 wpabuf_head(secret), wpabuf_len(secret), prk);
5259 else if (group == 20)
5260 res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey),
5261 wpabuf_head(secret), wpabuf_len(secret), prk);
5262 else if (group == 21)
5263 res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey),
5264 wpabuf_head(secret), wpabuf_len(secret), prk);
5265 wpabuf_clear_free(hkey);
5266 wpabuf_clear_free(secret);
5267 if (res < 0)
5268 return -1;
5269
5270 wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
5271
5272 /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
5273
5274 if (group == 19)
5275 res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info,
5276 os_strlen(info), sm->pmk, hash_len);
5277 else if (group == 20)
5278 res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info,
5279 os_strlen(info), sm->pmk, hash_len);
5280 else if (group == 21)
5281 res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info,
5282 os_strlen(info), sm->pmk, hash_len);
Hai Shalom81f62d82019-07-22 12:10:00 -07005283 forced_memzero(prk, SHA512_MAC_LEN);
Roshan Pius3a1667e2018-07-03 15:17:14 -07005284 if (res < 0) {
5285 sm->pmk_len = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005286 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005287 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005288 sm->pmk_len = hash_len;
5289
5290 wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sm->pmk, sm->pmk_len);
5291 wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
5292 pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, pmkid, NULL, 0,
5293 bssid, sm->own_addr, sm->network_ctx, sm->key_mgmt,
5294 NULL);
5295
5296 return 0;
5297}
5298
5299#endif /* CONFIG_OWE */
5300
5301
5302void wpa_sm_set_fils_cache_id(struct wpa_sm *sm, const u8 *fils_cache_id)
5303{
5304#ifdef CONFIG_FILS
5305 if (sm && fils_cache_id) {
5306 sm->fils_cache_id_set = 1;
5307 os_memcpy(sm->fils_cache_id, fils_cache_id, FILS_CACHE_ID_LEN);
5308 }
5309#endif /* CONFIG_FILS */
5310}
Hai Shalom021b0b52019-04-10 11:17:58 -07005311
5312
5313#ifdef CONFIG_DPP2
5314void wpa_sm_set_dpp_z(struct wpa_sm *sm, const struct wpabuf *z)
5315{
5316 if (sm) {
5317 wpabuf_clear_free(sm->dpp_z);
5318 sm->dpp_z = z ? wpabuf_dup(z) : NULL;
5319 }
5320}
5321#endif /* CONFIG_DPP2 */
Hai Shalom60840252021-02-19 19:02:11 -08005322
5323
5324#ifdef CONFIG_PASN
5325void wpa_pasn_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
5326 const u8 *pmkid, const u8 *bssid, int key_mgmt)
5327{
5328 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
5329 bssid, sm->own_addr, NULL,
5330 key_mgmt, 0);
5331}
5332#endif /* CONFIG_PASN */
Hai Shalomc1a21442022-02-04 13:43:00 -08005333
5334
5335void wpa_sm_pmksa_cache_reconfig(struct wpa_sm *sm)
5336{
5337 if (sm)
5338 pmksa_cache_reconfig(sm->pmksa);
5339}