blob: 935a1aa33c057286595d47473ec4047296d7662b [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
Sunil Ravi77d572f2023-01-17 23:58:31 +000041static void _wpa_hexdump_link(int level, u8 link_id, const char *title,
42 const void *buf, size_t len, bool key)
43{
44 char *link_title = NULL;
45
46 if (link_id >= MAX_NUM_MLD_LINKS)
47 goto out;
48
49 link_title = os_malloc(os_strlen(title) + 20);
50 if (!link_title)
51 goto out;
52
53 os_snprintf(link_title, os_strlen(title) + 20, "MLO link[%u]: %s",
54 link_id, title);
55
56out:
57 if (key)
58 wpa_hexdump_key(level, link_title ? link_title : title, buf,
59 len);
60 else
61 wpa_hexdump(level, link_title ? link_title : title, buf, len);
62 os_free(link_title);
63}
64
65
66static void wpa_hexdump_link(int level, u8 link_id, const char *title,
67 const void *buf, size_t len)
68{
69 _wpa_hexdump_link(level, link_id, title, buf, len, false);
70}
71
72
73static void wpa_hexdump_link_key(int level, u8 link_id, const char *title,
74 const void *buf, size_t len)
75{
76 _wpa_hexdump_link(level, link_id, title, buf, len, true);
77}
78
79
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070080/**
81 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
82 * @sm: Pointer to WPA state machine data from wpa_sm_init()
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080083 * @ptk: PTK for Key Confirmation/Encryption Key
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070084 * @ver: Version field from Key Info
85 * @dest: Destination address for the frame
86 * @proto: Ethertype (usually ETH_P_EAPOL)
87 * @msg: EAPOL-Key message
88 * @msg_len: Length of message
89 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080090 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070091 */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080092int wpa_eapol_key_send(struct wpa_sm *sm, struct wpa_ptk *ptk,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080093 int ver, const u8 *dest, u16 proto,
94 u8 *msg, size_t msg_len, u8 *key_mic)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070095{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080096 int ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070097 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -080098
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070099 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL-Key frame to " MACSTR
100 " ver=%d mic_len=%d key_mgmt=0x%x",
101 MAC2STR(dest), ver, (int) mic_len, sm->key_mgmt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700102 if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
103 /*
104 * Association event was not yet received; try to fetch
105 * BSSID from the driver.
106 */
107 if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
108 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
109 "WPA: Failed to read BSSID for "
110 "EAPOL-Key destination address");
111 } else {
112 dest = sm->bssid;
113 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
114 "WPA: Use BSSID (" MACSTR
115 ") as the destination for EAPOL-Key",
116 MAC2STR(dest));
117 }
118 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800119
120 if (mic_len) {
121 if (key_mic && (!ptk || !ptk->kck_len))
122 goto out;
123
124 if (key_mic &&
125 wpa_eapol_key_mic(ptk->kck, ptk->kck_len, sm->key_mgmt, ver,
126 msg, msg_len, key_mic)) {
127 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
128 "WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC",
129 ver, sm->key_mgmt);
130 goto out;
131 }
Dmitry Shmidt29333592017-01-09 12:27:11 -0800132 if (ptk)
133 wpa_hexdump_key(MSG_DEBUG, "WPA: KCK",
134 ptk->kck, ptk->kck_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800135 wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC",
136 key_mic, mic_len);
137 } else {
138#ifdef CONFIG_FILS
139 /* AEAD cipher - Key MIC field not used */
140 struct ieee802_1x_hdr *s_hdr, *hdr;
141 struct wpa_eapol_key *s_key, *key;
142 u8 *buf, *s_key_data, *key_data;
143 size_t buf_len = msg_len + AES_BLOCK_SIZE;
144 size_t key_data_len;
145 u16 eapol_len;
146 const u8 *aad[1];
147 size_t aad_len[1];
148
149 if (!ptk || !ptk->kek_len)
150 goto out;
151
152 key_data_len = msg_len - sizeof(struct ieee802_1x_hdr) -
153 sizeof(struct wpa_eapol_key) - 2;
154
155 buf = os_malloc(buf_len);
156 if (!buf)
157 goto out;
158
159 os_memcpy(buf, msg, msg_len);
160 hdr = (struct ieee802_1x_hdr *) buf;
161 key = (struct wpa_eapol_key *) (hdr + 1);
162 key_data = ((u8 *) (key + 1)) + 2;
163
164 /* Update EAPOL header to include AES-SIV overhead */
165 eapol_len = be_to_host16(hdr->length);
166 eapol_len += AES_BLOCK_SIZE;
167 hdr->length = host_to_be16(eapol_len);
168
169 /* Update Key Data Length field to include AES-SIV overhead */
170 WPA_PUT_BE16((u8 *) (key + 1), AES_BLOCK_SIZE + key_data_len);
171
172 s_hdr = (struct ieee802_1x_hdr *) msg;
173 s_key = (struct wpa_eapol_key *) (s_hdr + 1);
174 s_key_data = ((u8 *) (s_key + 1)) + 2;
175
176 wpa_hexdump_key(MSG_DEBUG, "WPA: Plaintext Key Data",
177 s_key_data, key_data_len);
178
179 wpa_hexdump_key(MSG_DEBUG, "WPA: KEK", ptk->kek, ptk->kek_len);
180 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
181 * to Key Data (exclusive). */
182 aad[0] = buf;
183 aad_len[0] = key_data - buf;
184 if (aes_siv_encrypt(ptk->kek, ptk->kek_len,
185 s_key_data, key_data_len,
186 1, aad, aad_len, key_data) < 0) {
187 os_free(buf);
188 goto out;
189 }
190
191 wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV",
192 key_data, AES_BLOCK_SIZE + key_data_len);
193
194 os_free(msg);
195 msg = buf;
196 msg_len = buf_len;
197#else /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700198 goto out;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800199#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700200 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800201
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700202 wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800203 ret = wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700204 eapol_sm_notify_tx_eapol_key(sm->eapol);
205out:
206 os_free(msg);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800207 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700208}
209
210
211/**
212 * wpa_sm_key_request - Send EAPOL-Key Request
213 * @sm: Pointer to WPA state machine data from wpa_sm_init()
214 * @error: Indicate whether this is an Michael MIC error report
215 * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
216 *
217 * Send an EAPOL-Key Request to the current authenticator. This function is
218 * used to request rekeying and it is usually called when a local Michael MIC
219 * failure is detected.
220 */
221void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
222{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800223 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700224 struct wpa_eapol_key *reply;
225 int key_info, ver;
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000226 u8 *rbuf, *key_mic, *mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700227
Hai Shalomfdcde762020-04-02 11:19:20 -0700228 if (pairwise && sm->wpa_deny_ptk0_rekey && !sm->use_ext_key_id &&
Sunil8cd6f4d2022-06-28 18:40:46 +0000229 wpa_sm_get_state(sm) == WPA_COMPLETED && !error) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700230 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
231 "WPA: PTK0 rekey not allowed, reconnecting");
232 wpa_sm_reconnect(sm);
233 return;
234 }
235
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000236 if (!sm->ptk_set) {
237 wpa_printf(MSG_INFO,
238 "WPA: No PTK derived yet - cannot send EAPOL-Key Request");
239 return;
240 }
241
Roshan Pius3a1667e2018-07-03 15:17:14 -0700242 if (wpa_use_akm_defined(sm->key_mgmt))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800243 ver = WPA_KEY_INFO_TYPE_AKM_DEFINED;
244 else if (wpa_key_mgmt_ft(sm->key_mgmt) ||
245 wpa_key_mgmt_sha256(sm->key_mgmt))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700246 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700247 else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
249 else
250 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
251
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700252 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800253 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700254 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800255 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700256 if (rbuf == NULL)
257 return;
258
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800259 reply->type = (sm->proto == WPA_PROTO_RSN ||
260 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700261 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
262 key_info = WPA_KEY_INFO_REQUEST | ver;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000263 key_info |= WPA_KEY_INFO_SECURE;
264 if (mic_len)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800265 key_info |= WPA_KEY_INFO_MIC;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000266 else
267 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700268 if (error)
269 key_info |= WPA_KEY_INFO_ERROR;
270 if (pairwise)
271 key_info |= WPA_KEY_INFO_KEY_TYPE;
272 WPA_PUT_BE16(reply->key_info, key_info);
273 WPA_PUT_BE16(reply->key_length, 0);
274 os_memcpy(reply->replay_counter, sm->request_counter,
275 WPA_REPLAY_COUNTER_LEN);
276 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
277
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800278 mic = (u8 *) (reply + 1);
279 WPA_PUT_BE16(mic + mic_len, 0);
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800280 if (!(key_info & WPA_KEY_INFO_MIC))
281 key_mic = NULL;
282 else
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800283 key_mic = mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284
285 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
286 "WPA: Sending EAPOL-Key Request (error=%d "
287 "pairwise=%d ptk_set=%d len=%lu)",
288 error, pairwise, sm->ptk_set, (unsigned long) rlen);
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000289 wpa_eapol_key_send(sm, &sm->ptk, ver, wpa_sm_get_auth_addr(sm),
290 ETH_P_EAPOL, rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291}
292
293
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800294static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm *sm)
295{
296#ifdef CONFIG_IEEE80211R
297 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
298 if (wpa_sm_key_mgmt_set_pmk(sm, sm->xxkey, sm->xxkey_len))
299 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
300 "RSN: Cannot set low order 256 bits of MSK for key management offload");
301 } else {
302#endif /* CONFIG_IEEE80211R */
303 if (wpa_sm_key_mgmt_set_pmk(sm, sm->pmk, sm->pmk_len))
304 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
305 "RSN: Cannot set PMK for key management offload");
306#ifdef CONFIG_IEEE80211R
307 }
308#endif /* CONFIG_IEEE80211R */
309}
310
311
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700312static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
313 const unsigned char *src_addr,
314 const u8 *pmkid)
315{
316 int abort_cached = 0;
317
318 if (pmkid && !sm->cur_pmksa) {
319 /* When using drivers that generate RSN IE, wpa_supplicant may
320 * not have enough time to get the association information
321 * event before receiving this 1/4 message, so try to find a
322 * matching PMKSA cache entry here. */
Sunil Ravi77d572f2023-01-17 23:58:31 +0000323 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr,
324 sm->own_addr, pmkid,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700325 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700326 if (sm->cur_pmksa) {
327 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
328 "RSN: found matching PMKID from PMKSA cache");
329 } else {
330 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
331 "RSN: no matching PMKID found");
332 abort_cached = 1;
333 }
334 }
335
336 if (pmkid && sm->cur_pmksa &&
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700337 os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700338 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
339 wpa_sm_set_pmk_from_pmksa(sm);
340 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
341 sm->pmk, sm->pmk_len);
342 eapol_sm_notify_cached(sm->eapol);
343#ifdef CONFIG_IEEE80211R
344 sm->xxkey_len = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700345#ifdef CONFIG_SAE
Sunil Ravi89eba102022-09-13 21:04:37 -0700346 if ((sm->key_mgmt == WPA_KEY_MGMT_FT_SAE ||
347 sm->key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -0700348 sm->pmk_len == PMK_LEN) {
349 /* Need to allow FT key derivation to proceed with
350 * PMK from SAE being used as the XXKey in cases where
351 * the PMKID in msg 1/4 matches the PMKSA entry that was
352 * just added based on SAE authentication for the
353 * initial mobility domain association. */
354 os_memcpy(sm->xxkey, sm->pmk, sm->pmk_len);
355 sm->xxkey_len = sm->pmk_len;
356 }
357#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700358#endif /* CONFIG_IEEE80211R */
359 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
360 int res, pmk_len;
Hai Shalom81f62d82019-07-22 12:10:00 -0700361#ifdef CONFIG_IEEE80211R
362 u8 buf[2 * PMK_LEN];
363#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800364
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800365 if (wpa_key_mgmt_sha384(sm->key_mgmt))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800366 pmk_len = PMK_LEN_SUITE_B_192;
367 else
368 pmk_len = PMK_LEN;
369 res = eapol_sm_get_key(sm->eapol, sm->pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700370 if (res) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800371 if (pmk_len == PMK_LEN) {
372 /*
373 * EAP-LEAP is an exception from other EAP
374 * methods: it uses only 16-byte PMK.
375 */
376 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
377 pmk_len = 16;
378 }
Hai Shalomf1c97642019-07-19 23:42:07 +0000379 }
Hai Shalom81f62d82019-07-22 12:10:00 -0700380#ifdef CONFIG_IEEE80211R
381 if (res == 0 &&
382 eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0) {
383 if (wpa_key_mgmt_sha384(sm->key_mgmt)) {
384 os_memcpy(sm->xxkey, buf, SHA384_MAC_LEN);
385 sm->xxkey_len = SHA384_MAC_LEN;
386 } else {
387 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
388 sm->xxkey_len = PMK_LEN;
389 }
390 forced_memzero(buf, sizeof(buf));
391 if (sm->proto == WPA_PROTO_RSN &&
392 wpa_key_mgmt_ft(sm->key_mgmt)) {
393 struct rsn_pmksa_cache_entry *sa = NULL;
394 const u8 *fils_cache_id = NULL;
395
396#ifdef CONFIG_FILS
397 if (sm->fils_cache_id_set)
398 fils_cache_id = sm->fils_cache_id;
399#endif /* CONFIG_FILS */
400 wpa_hexdump_key(MSG_DEBUG,
401 "FT: Cache XXKey/MPMK",
402 sm->xxkey, sm->xxkey_len);
403 sa = pmksa_cache_add(sm->pmksa,
404 sm->xxkey, sm->xxkey_len,
405 NULL, NULL, 0,
406 src_addr, sm->own_addr,
407 sm->network_ctx,
408 sm->key_mgmt,
409 fils_cache_id);
410 if (!sm->cur_pmksa)
411 sm->cur_pmksa = sa;
412 }
413 }
414#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700415 if (res == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700416 struct rsn_pmksa_cache_entry *sa = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700417 const u8 *fils_cache_id = NULL;
418
419#ifdef CONFIG_FILS
420 if (sm->fils_cache_id_set)
421 fils_cache_id = sm->fils_cache_id;
422#endif /* CONFIG_FILS */
423
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700424 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
425 "machines", sm->pmk, pmk_len);
426 sm->pmk_len = pmk_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800427 wpa_supplicant_key_mgmt_set_pmk(sm);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700428 if (sm->proto == WPA_PROTO_RSN &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800429 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700430 !wpa_key_mgmt_ft(sm->key_mgmt)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700431 sa = pmksa_cache_add(sm->pmksa,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800432 sm->pmk, pmk_len, NULL,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800433 NULL, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700434 src_addr, sm->own_addr,
435 sm->network_ctx,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700436 sm->key_mgmt,
437 fils_cache_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700438 }
439 if (!sm->cur_pmksa && pmkid &&
Sunil Ravi77d572f2023-01-17 23:58:31 +0000440 pmksa_cache_get(sm->pmksa, src_addr, sm->own_addr,
441 pmkid, NULL, 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700442 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
443 "RSN: the new PMK matches with the "
444 "PMKID");
445 abort_cached = 0;
Jouni Malinen6ec30382015-07-08 20:48:18 +0300446 } else if (sa && !sm->cur_pmksa && pmkid) {
447 /*
448 * It looks like the authentication server
449 * derived mismatching MSK. This should not
450 * really happen, but bugs happen.. There is not
451 * much we can do here without knowing what
452 * exactly caused the server to misbehave.
453 */
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800454 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Jouni Malinen6ec30382015-07-08 20:48:18 +0300455 "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
456 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700457 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700458
459 if (!sm->cur_pmksa)
460 sm->cur_pmksa = sa;
Hai Shalom81f62d82019-07-22 12:10:00 -0700461#ifdef CONFIG_IEEE80211R
462 } else if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->ft_protocol) {
463 wpa_printf(MSG_DEBUG,
464 "FT: Continue 4-way handshake without PMK/PMKID for association using FT protocol");
465#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700466 } else {
467 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
468 "WPA: Failed to get master session key from "
469 "EAPOL state machines - key handshake "
470 "aborted");
471 if (sm->cur_pmksa) {
472 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
473 "RSN: Cancelled PMKSA caching "
474 "attempt");
475 sm->cur_pmksa = NULL;
476 abort_cached = 1;
477 } else if (!abort_cached) {
478 return -1;
479 }
480 }
481 }
482
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700483 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800484 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800485 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
486 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700487 /* Send EAPOL-Start to trigger full EAP authentication. */
488 u8 *buf;
489 size_t buflen;
490
491 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
492 "RSN: no PMKSA entry found - trigger "
493 "full EAP authentication");
494 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
495 NULL, 0, &buflen, NULL);
496 if (buf) {
Hai Shalomc1a21442022-02-04 13:43:00 -0800497 /* Set and reset eapFail to allow EAP state machine to
498 * proceed with new authentication. */
499 eapol_sm_notify_eap_fail(sm->eapol, true);
500 eapol_sm_notify_eap_fail(sm->eapol, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700501 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
502 buf, buflen);
503 os_free(buf);
504 return -2;
505 }
506
507 return -1;
508 }
509
510 return 0;
511}
512
513
514/**
515 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
516 * @sm: Pointer to WPA state machine data from wpa_sm_init()
517 * @dst: Destination address for the frame
518 * @key: Pointer to the EAPOL-Key frame header
519 * @ver: Version bits from EAPOL-Key Key Info
520 * @nonce: Nonce value for the EAPOL-Key frame
521 * @wpa_ie: WPA/RSN IE
522 * @wpa_ie_len: Length of the WPA/RSN IE
523 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800524 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700525 */
526int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
527 const struct wpa_eapol_key *key,
528 int ver, const u8 *nonce,
529 const u8 *wpa_ie, size_t wpa_ie_len,
530 struct wpa_ptk *ptk)
531{
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000532 size_t mic_len, hdrlen, rlen, extra_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700533 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800534 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700535 u8 *rsn_ie_buf = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800536 u16 key_info;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000537#ifdef CONFIG_TESTING_OPTIONS
538 size_t pad_len = 0;
539#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700540
541 if (wpa_ie == NULL) {
542 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
543 "cannot generate msg 2/4");
544 return -1;
545 }
546
547#ifdef CONFIG_IEEE80211R
548 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
549 int res;
550
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800551 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE before FT processing",
552 wpa_ie, wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700553 /*
554 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
555 * FTIE from (Re)Association Response.
556 */
557 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
558 sm->assoc_resp_ies_len);
559 if (rsn_ie_buf == NULL)
560 return -1;
561 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800562 res = wpa_insert_pmkid(rsn_ie_buf, &wpa_ie_len,
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000563 sm->pmk_r1_name, !sm->ft_prepend_pmkid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700564 if (res < 0) {
565 os_free(rsn_ie_buf);
566 return -1;
567 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800568 wpa_hexdump(MSG_DEBUG,
569 "WPA: WPA IE after PMKID[PMKR1Name] addition into RSNE",
570 rsn_ie_buf, wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700571
572 if (sm->assoc_resp_ies) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800573 wpa_hexdump(MSG_DEBUG, "WPA: Add assoc_resp_ies",
574 sm->assoc_resp_ies,
575 sm->assoc_resp_ies_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700576 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
577 sm->assoc_resp_ies_len);
578 wpa_ie_len += sm->assoc_resp_ies_len;
579 }
580
581 wpa_ie = rsn_ie_buf;
582 }
583#endif /* CONFIG_IEEE80211R */
584
585 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
586
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000587#ifdef CONFIG_TESTING_OPTIONS
588 if (sm->test_eapol_m2_elems)
589 extra_len = wpabuf_len(sm->test_eapol_m2_elems);
590 if (sm->encrypt_eapol_m2) {
591 pad_len = (wpa_ie_len + extra_len) % 8;
592 if (pad_len)
593 pad_len = 8 - pad_len;
594 extra_len += pad_len + 8;
595 }
596#endif /* CONFIG_TESTING_OPTIONS */
597
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700598 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800599 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700600 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000601 NULL, hdrlen + wpa_ie_len + extra_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700602 &rlen, (void *) &reply);
603 if (rbuf == NULL) {
604 os_free(rsn_ie_buf);
605 return -1;
606 }
607
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800608 reply->type = (sm->proto == WPA_PROTO_RSN ||
609 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700610 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800611 key_info = ver | WPA_KEY_INFO_KEY_TYPE;
Sunil8cd6f4d2022-06-28 18:40:46 +0000612 if (sm->ptk_set && sm->proto != WPA_PROTO_WPA)
613 key_info |= WPA_KEY_INFO_SECURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800614 if (mic_len)
615 key_info |= WPA_KEY_INFO_MIC;
616 else
617 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000618#ifdef CONFIG_TESTING_OPTIONS
619 if (sm->encrypt_eapol_m2)
620 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
621#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800622 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800623 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700624 WPA_PUT_BE16(reply->key_length, 0);
625 else
626 os_memcpy(reply->key_length, key->key_length, 2);
627 os_memcpy(reply->replay_counter, key->replay_counter,
628 WPA_REPLAY_COUNTER_LEN);
629 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
630 WPA_REPLAY_COUNTER_LEN);
631
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800632 key_mic = (u8 *) (reply + 1);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000633 /* Key Data Length */
634 WPA_PUT_BE16(key_mic + mic_len, wpa_ie_len + extra_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800635 os_memcpy(key_mic + mic_len + 2, wpa_ie, wpa_ie_len); /* Key Data */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700636 os_free(rsn_ie_buf);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000637#ifdef CONFIG_TESTING_OPTIONS
638 if (sm->test_eapol_m2_elems) {
639 os_memcpy(key_mic + mic_len + 2 + wpa_ie_len,
640 wpabuf_head(sm->test_eapol_m2_elems),
641 wpabuf_len(sm->test_eapol_m2_elems));
642 }
643
644 if (sm->encrypt_eapol_m2) {
645 u8 *plain;
646 size_t plain_len;
647
648 if (sm->test_eapol_m2_elems)
649 extra_len = wpabuf_len(sm->test_eapol_m2_elems);
650 else
651 extra_len = 0;
652 plain_len = wpa_ie_len + extra_len + pad_len;
653 plain = os_memdup(key_mic + mic_len + 2, plain_len);
654 if (!plain) {
655 os_free(rbuf);
656 return -1;
657 }
658 if (pad_len)
659 plain[plain_len - pad_len] = 0xdd;
660
661 wpa_hexdump_key(MSG_DEBUG, "RSN: AES-WRAP using KEK",
662 ptk->kek, ptk->kek_len);
663 if (aes_wrap(ptk->kek, ptk->kek_len, plain_len / 8, plain,
664 key_mic + mic_len + 2)) {
665 os_free(plain);
666 os_free(rbuf);
667 return -1;
668 }
669 wpa_hexdump(MSG_DEBUG,
670 "RSN: Encrypted Key Data from AES-WRAP",
671 key_mic + mic_len + 2, plain_len + 8);
672 os_free(plain);
673 }
674#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700675
676 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
677
Roshan Pius5e7db942018-04-12 12:27:41 -0700678 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 2/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800679 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
680 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700681}
682
683
684static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800685 const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700686{
Sunil Ravi89eba102022-09-13 21:04:37 -0700687 int ret;
Hai Shalom021b0b52019-04-10 11:17:58 -0700688 const u8 *z = NULL;
Hai Shalom60840252021-02-19 19:02:11 -0800689 size_t z_len = 0, kdk_len;
Hai Shalomfdcde762020-04-02 11:19:20 -0700690 int akmp;
Hai Shalom021b0b52019-04-10 11:17:58 -0700691
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700692#ifdef CONFIG_IEEE80211R
693 if (wpa_key_mgmt_ft(sm->key_mgmt))
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800694 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700695#endif /* CONFIG_IEEE80211R */
696
Hai Shalom021b0b52019-04-10 11:17:58 -0700697#ifdef CONFIG_DPP2
698 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
699 z = wpabuf_head(sm->dpp_z);
700 z_len = wpabuf_len(sm->dpp_z);
701 }
702#endif /* CONFIG_DPP2 */
703
Hai Shalomfdcde762020-04-02 11:19:20 -0700704 akmp = sm->key_mgmt;
705#ifdef CONFIG_OWE
706 if (sm->owe_ptk_workaround && akmp == WPA_KEY_MGMT_OWE &&
707 sm->pmk_len > 32) {
708 wpa_printf(MSG_DEBUG,
709 "OWE: Force SHA256 for PTK derivation");
710 akmp |= WPA_KEY_MGMT_PSK_SHA256;
711 }
712#endif /* CONFIG_OWE */
Hai Shalom60840252021-02-19 19:02:11 -0800713
714 if (sm->force_kdk_derivation ||
Hai Shalomc1a21442022-02-04 13:43:00 -0800715 (sm->secure_ltf &&
716 ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
Hai Shalom60840252021-02-19 19:02:11 -0800717 kdk_len = WPA_KDK_MAX_LEN;
718 else
719 kdk_len = 0;
720
Sunil Ravi89eba102022-09-13 21:04:37 -0700721 ret = wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
Sunil Ravi77d572f2023-01-17 23:58:31 +0000722 sm->own_addr, wpa_sm_get_auth_addr(sm), sm->snonce,
Sunil Ravi89eba102022-09-13 21:04:37 -0700723 key->key_nonce, ptk, akmp,
724 sm->pairwise_cipher, z, z_len,
725 kdk_len);
726 if (ret) {
727 wpa_printf(MSG_ERROR, "WPA: PTK derivation failed");
728 return ret;
729 }
730
731#ifdef CONFIG_PASN
732 if (sm->secure_ltf &&
733 ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF))
734 ret = wpa_ltf_keyseed(ptk, akmp, sm->pairwise_cipher);
735#endif /* CONFIG_PASN */
736
737 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738}
739
740
Hai Shalomfdcde762020-04-02 11:19:20 -0700741static int wpa_handle_ext_key_id(struct wpa_sm *sm,
742 struct wpa_eapol_ie_parse *kde)
743{
744 if (sm->ext_key_id) {
745 u16 key_id;
746
747 if (!kde->key_id) {
748 wpa_msg(sm->ctx->msg_ctx,
749 sm->use_ext_key_id ? MSG_INFO : MSG_DEBUG,
750 "RSN: No Key ID in Extended Key ID handshake");
751 sm->keyidx_active = 0;
752 return sm->use_ext_key_id ? -1 : 0;
753 }
754
755 key_id = kde->key_id[0] & 0x03;
756 if (key_id > 1) {
757 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
758 "RSN: Invalid Extended Key ID: %d", key_id);
759 return -1;
760 }
761 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
762 "RSN: Using Extended Key ID %d", key_id);
763 sm->keyidx_active = key_id;
764 sm->use_ext_key_id = 1;
765 } else {
766 if (kde->key_id && (kde->key_id[0] & 0x03)) {
767 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
768 "RSN: Non-zero Extended Key ID Key ID in PTK0 handshake");
769 return -1;
770 }
771
772 if (kde->key_id) {
773 /* This is not supposed to be included here, but ignore
774 * the case of matching Key ID 0 just in case. */
775 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
776 "RSN: Extended Key ID Key ID 0 in PTK0 handshake");
777 }
778 sm->keyidx_active = 0;
779 sm->use_ext_key_id = 0;
780 }
781
782 return 0;
783}
784
785
Sunil Ravi77d572f2023-01-17 23:58:31 +0000786static u8 * rsn_add_kde(u8 *pos, u32 kde, const u8 *data, size_t data_len)
787{
788 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
789 *pos++ = RSN_SELECTOR_LEN + data_len;
790 RSN_SELECTOR_PUT(pos, kde);
791 pos += RSN_SELECTOR_LEN;
792 os_memcpy(pos, data, data_len);
793 pos += data_len;
794
795 return pos;
796}
797
798
799static size_t wpa_mlo_link_kde_len(struct wpa_sm *sm)
800{
801 int i;
802 unsigned int num_links = 0;
803
Sunil Ravi99c035e2024-07-12 01:42:03 +0000804 for_each_link(sm->mlo.req_links, i) {
805 if (sm->mlo.assoc_link_id != i)
Sunil Ravi77d572f2023-01-17 23:58:31 +0000806 num_links++;
807 }
808
809 return num_links * (RSN_SELECTOR_LEN + 1 + ETH_ALEN + 2);
810}
811
812
813static u8 * wpa_mlo_link_kde(struct wpa_sm *sm, u8 *pos)
814{
815 int i;
816 u8 hdr[1 + ETH_ALEN];
817
Sunil Ravi99c035e2024-07-12 01:42:03 +0000818 for_each_link(sm->mlo.req_links, i) {
819 if (sm->mlo.assoc_link_id == i)
Sunil Ravi77d572f2023-01-17 23:58:31 +0000820 continue;
821
822 wpa_printf(MSG_DEBUG,
823 "MLO: Add MLO Link %d KDE in EAPOL-Key 2/4", i);
824 hdr[0] = i & 0xF; /* LinkID; no RSNE or RSNXE */
825 os_memcpy(&hdr[1], sm->mlo.links[i].addr, ETH_ALEN);
826 pos = rsn_add_kde(pos, RSN_KEY_DATA_MLO_LINK, hdr, sizeof(hdr));
827 }
828
829 return pos;
830}
831
832
833static bool is_valid_ap_mld_mac_kde(struct wpa_sm *sm, const u8 *mac_kde)
834{
835 return mac_kde &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000836 ether_addr_equal(mac_kde, sm->mlo.ap_mld_addr);
Sunil Ravi77d572f2023-01-17 23:58:31 +0000837}
838
839
840static void wpas_swap_tkip_mic_keys(struct wpa_ptk *ptk)
841{
842 u8 buf[8];
843
844 /* Supplicant: swap tx/rx Mic keys */
845 os_memcpy(buf, &ptk->tk[16], 8);
846 os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
847 os_memcpy(&ptk->tk[24], buf, 8);
848 forced_memzero(buf, sizeof(buf));
849}
850
851
852static void wpa_supplicant_process_1_of_4_wpa(struct wpa_sm *sm,
853 const unsigned char *src_addr,
854 const struct wpa_eapol_key *key,
855 u16 ver, const u8 *key_data,
856 size_t key_data_len,
857 enum frame_encryption encrypted)
858{
859 struct wpa_eapol_ie_parse ie;
860 struct wpa_ptk *ptk;
861 int res;
862
863 if (wpa_sm_get_network_ctx(sm) == NULL) {
864 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
865 "WPA: No SSID info found (msg 1 of 4)");
866 return;
867 }
868
869 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
870 "WPA: RX message 1 of 4-Way Handshake from " MACSTR
871 " (ver=%d)", MAC2STR(src_addr), ver);
872
873 os_memset(&ie, 0, sizeof(ie));
874
875 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
876 if (res == -2) {
877 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
878 "WPA: Do not reply to msg 1/4 - requesting full EAP authentication");
879 return;
880 }
881 if (res)
882 goto failed;
883
884 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
885
886 if (sm->renew_snonce) {
887 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
888 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
889 "WPA: Failed to get random data for SNonce");
890 goto failed;
891 }
892 sm->renew_snonce = 0;
893 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
894 sm->snonce, WPA_NONCE_LEN);
895 }
896
897 /* Calculate PTK which will be stored as a temporary PTK until it has
898 * been verified when processing message 3/4. */
899 ptk = &sm->tptk;
900 if (wpa_derive_ptk(sm, src_addr, key, ptk) < 0)
901 goto failed;
902 if (sm->pairwise_cipher == WPA_CIPHER_TKIP)
903 wpas_swap_tkip_mic_keys(ptk);
904 sm->tptk_set = 1;
905
906 if (wpa_supplicant_send_2_of_4(sm, wpa_sm_get_auth_addr(sm), key, ver,
907 sm->snonce, sm->assoc_wpa_ie,
908 sm->assoc_wpa_ie_len, ptk) < 0)
909 goto failed;
910
911 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
912 return;
913
914failed:
915 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
916}
917
918
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700919static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
920 const unsigned char *src_addr,
921 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700922 u16 ver, const u8 *key_data,
Sunil8cd6f4d2022-06-28 18:40:46 +0000923 size_t key_data_len,
924 enum frame_encryption encrypted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925{
926 struct wpa_eapol_ie_parse ie;
927 struct wpa_ptk *ptk;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928 int res;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800929 u8 *kde, *kde_buf = NULL;
930 size_t kde_len;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000931 size_t mlo_kde_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700932
Sunil8cd6f4d2022-06-28 18:40:46 +0000933 if (encrypted == FRAME_NOT_ENCRYPTED && sm->tk_set &&
934 wpa_sm_pmf_enabled(sm)) {
935 wpa_printf(MSG_DEBUG,
936 "RSN: Discard unencrypted EAPOL-Key msg 1/4 when TK is set and PMF is enabled");
937 return;
938 }
939
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700940 if (wpa_sm_get_network_ctx(sm) == NULL) {
941 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
942 "found (msg 1 of 4)");
943 return;
944 }
945
Hai Shalomfdcde762020-04-02 11:19:20 -0700946 if (sm->wpa_deny_ptk0_rekey && !sm->use_ext_key_id &&
947 wpa_sm_get_state(sm) == WPA_COMPLETED) {
948 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
949 "WPA: PTK0 rekey not allowed, reconnecting");
950 wpa_sm_reconnect(sm);
951 return;
952 }
953
Roshan Pius5e7db942018-04-12 12:27:41 -0700954 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: RX message 1 of 4-Way "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700955 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
956
957 os_memset(&ie, 0, sizeof(ie));
958
Sunil Ravi77d572f2023-01-17 23:58:31 +0000959 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
960 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data", key_data, key_data_len);
961 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0) {
962 wpa_printf(MSG_DEBUG,
963 "RSN: Discard EAPOL-Key msg 1/4 with invalid IEs/KDEs");
964 return;
965 }
966 if (ie.pmkid) {
967 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from Authenticator",
968 ie.pmkid, PMKID_LEN);
969 }
970
971 if (sm->mlo.valid_links && !is_valid_ap_mld_mac_kde(sm, ie.mac_addr)) {
972 wpa_printf(MSG_INFO,
973 "RSN: Discard EAPOL-Key msg 1/4 with invalid AP MLD MAC address KDE");
974 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700975 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700976
977 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
978 if (res == -2) {
979 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
980 "msg 1/4 - requesting full EAP authentication");
981 return;
982 }
983 if (res)
984 goto failed;
985
Sunil8cd6f4d2022-06-28 18:40:46 +0000986 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
987
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700988 if (sm->renew_snonce) {
989 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
990 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
991 "WPA: Failed to get random data for SNonce");
992 goto failed;
993 }
994 sm->renew_snonce = 0;
995 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
996 sm->snonce, WPA_NONCE_LEN);
997 }
998
999 /* Calculate PTK which will be stored as a temporary PTK until it has
1000 * been verified when processing message 3/4. */
1001 ptk = &sm->tptk;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001002 if (wpa_derive_ptk(sm, src_addr, key, ptk) < 0)
1003 goto failed;
Sunil Ravi77d572f2023-01-17 23:58:31 +00001004 if (sm->pairwise_cipher == WPA_CIPHER_TKIP)
1005 wpas_swap_tkip_mic_keys(ptk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001006 sm->tptk_set = 1;
1007
Sunil Ravi77d572f2023-01-17 23:58:31 +00001008 /* Add MLO Link KDE and MAC KDE in M2 for ML connection */
1009 if (sm->mlo.valid_links)
1010 mlo_kde_len = wpa_mlo_link_kde_len(sm) +
1011 RSN_SELECTOR_LEN + ETH_ALEN + 2;
1012
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001013 kde = sm->assoc_wpa_ie;
1014 kde_len = sm->assoc_wpa_ie_len;
Hai Shalomc3565922019-10-28 11:58:20 -07001015 kde_buf = os_malloc(kde_len +
1016 2 + RSN_SELECTOR_LEN + 3 +
1017 sm->assoc_rsnxe_len +
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001018 2 + RSN_SELECTOR_LEN + 1 +
Sunil Ravi77d572f2023-01-17 23:58:31 +00001019 2 + RSN_SELECTOR_LEN + 2 + mlo_kde_len);
1020
Hai Shalomc3565922019-10-28 11:58:20 -07001021 if (!kde_buf)
1022 goto failed;
1023 os_memcpy(kde_buf, kde, kde_len);
1024 kde = kde_buf;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001025
Hai Shalom74f70d42019-02-11 14:42:39 -08001026#ifdef CONFIG_OCV
1027 if (wpa_sm_ocv_enabled(sm)) {
1028 struct wpa_channel_info ci;
1029 u8 *pos;
1030
Hai Shalomc3565922019-10-28 11:58:20 -07001031 pos = kde + kde_len;
Hai Shalom74f70d42019-02-11 14:42:39 -08001032 if (wpa_sm_channel_info(sm, &ci) != 0) {
1033 wpa_printf(MSG_WARNING,
1034 "Failed to get channel info for OCI element in EAPOL-Key 2/4");
1035 goto failed;
1036 }
Hai Shalom899fcc72020-10-19 14:38:18 -07001037#ifdef CONFIG_TESTING_OPTIONS
1038 if (sm->oci_freq_override_eapol) {
1039 wpa_printf(MSG_INFO,
1040 "TEST: Override OCI KDE frequency %d -> %d MHz",
1041 ci.frequency, sm->oci_freq_override_eapol);
1042 ci.frequency = sm->oci_freq_override_eapol;
1043 }
1044#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -08001045
Hai Shalom74f70d42019-02-11 14:42:39 -08001046 if (ocv_insert_oci_kde(&ci, &pos) < 0)
1047 goto failed;
1048 kde_len = pos - kde;
1049 }
1050#endif /* CONFIG_OCV */
1051
Hai Shalomc3565922019-10-28 11:58:20 -07001052 if (sm->assoc_rsnxe && sm->assoc_rsnxe_len) {
1053 os_memcpy(kde + kde_len, sm->assoc_rsnxe, sm->assoc_rsnxe_len);
1054 kde_len += sm->assoc_rsnxe_len;
1055 }
1056
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001057#ifdef CONFIG_P2P
1058 if (sm->p2p) {
Hai Shalomc3565922019-10-28 11:58:20 -07001059 u8 *pos;
1060
1061 wpa_printf(MSG_DEBUG,
1062 "P2P: Add IP Address Request KDE into EAPOL-Key 2/4");
1063 pos = kde + kde_len;
1064 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
1065 *pos++ = RSN_SELECTOR_LEN + 1;
1066 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
1067 pos += RSN_SELECTOR_LEN;
1068 *pos++ = 0x01;
1069 kde_len = pos - kde;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001070 }
1071#endif /* CONFIG_P2P */
1072
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001073#ifdef CONFIG_DPP2
1074 if (DPP_VERSION > 1 && sm->key_mgmt == WPA_KEY_MGMT_DPP) {
1075 u8 *pos;
1076
1077 wpa_printf(MSG_DEBUG, "DPP: Add DPP KDE into EAPOL-Key 2/4");
1078 pos = kde + kde_len;
1079 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
1080 *pos++ = RSN_SELECTOR_LEN + 2;
1081 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_DPP);
1082 pos += RSN_SELECTOR_LEN;
1083 *pos++ = DPP_VERSION; /* Protocol Version */
1084 *pos = 0; /* Flags */
1085 if (sm->dpp_pfs == 0)
1086 *pos |= DPP_KDE_PFS_ALLOWED;
1087 else if (sm->dpp_pfs == 1)
1088 *pos |= DPP_KDE_PFS_ALLOWED | DPP_KDE_PFS_REQUIRED;
1089 pos++;
1090 kde_len = pos - kde;
1091 }
1092#endif /* CONFIG_DPP2 */
1093
Sunil Ravi77d572f2023-01-17 23:58:31 +00001094 if (sm->mlo.valid_links) {
1095 u8 *pos;
1096
1097 /* Add MAC KDE */
1098 wpa_printf(MSG_DEBUG, "MLO: Add MAC KDE into EAPOL-Key 2/4");
1099 pos = kde + kde_len;
1100 pos = rsn_add_kde(pos, RSN_KEY_DATA_MAC_ADDR, sm->own_addr,
1101 ETH_ALEN);
1102
1103 /* Add MLO Link KDE */
1104 wpa_printf(MSG_DEBUG, "Add MLO Link KDE(s) into EAPOL-Key 2/4");
1105 pos = wpa_mlo_link_kde(sm, pos);
1106 kde_len = pos - kde;
1107 }
1108
1109 if (wpa_supplicant_send_2_of_4(sm, wpa_sm_get_auth_addr(sm), key, ver,
1110 sm->snonce, kde, kde_len, ptk) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001111 goto failed;
1112
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001113 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001114 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
1115 return;
1116
1117failed:
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001118 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001119 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1120}
1121
1122
1123static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
1124{
1125 struct wpa_sm *sm = eloop_ctx;
1126 rsn_preauth_candidate_process(sm);
1127}
1128
1129
1130static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
1131 const u8 *addr, int secure)
1132{
1133 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1134 "WPA: Key negotiation completed with "
1135 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
1136 wpa_cipher_txt(sm->pairwise_cipher),
1137 wpa_cipher_txt(sm->group_cipher));
1138 wpa_sm_cancel_auth_timeout(sm);
1139 wpa_sm_set_state(sm, WPA_COMPLETED);
1140
1141 if (secure) {
1142 wpa_sm_mlme_setprotection(
1143 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
1144 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
Hai Shalome21d4e82020-04-29 16:34:06 -07001145 eapol_sm_notify_portValid(sm->eapol, true);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001146 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
1147 sm->key_mgmt == WPA_KEY_MGMT_DPP ||
1148 sm->key_mgmt == WPA_KEY_MGMT_OWE)
Hai Shalome21d4e82020-04-29 16:34:06 -07001149 eapol_sm_notify_eap_success(sm->eapol, true);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001150 /*
1151 * Start preauthentication after a short wait to avoid a
1152 * possible race condition between the data receive and key
1153 * configuration after the 4-Way Handshake. This increases the
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001154 * likelihood of the first preauth EAPOL-Start frame getting to
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001155 * the target AP.
1156 */
Hai Shalom74f70d42019-02-11 14:42:39 -08001157 if (!dl_list_empty(&sm->pmksa_candidates))
1158 eloop_register_timeout(1, 0, wpa_sm_start_preauth,
1159 sm, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001160 }
1161
1162 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
1163 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1164 "RSN: Authenticator accepted "
1165 "opportunistic PMKSA entry - marking it valid");
1166 sm->cur_pmksa->opportunistic = 0;
1167 }
1168
1169#ifdef CONFIG_IEEE80211R
1170 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
1171 /* Prepare for the next transition */
1172 wpa_ft_prepare_auth_request(sm, NULL);
1173 }
1174#endif /* CONFIG_IEEE80211R */
1175}
1176
1177
1178static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
1179{
1180 struct wpa_sm *sm = eloop_ctx;
1181 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
1182 wpa_sm_key_request(sm, 0, 1);
1183}
1184
1185
1186static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
Hai Shalomfdcde762020-04-02 11:19:20 -07001187 const struct wpa_eapol_key *key,
1188 enum key_flag key_flag)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001189{
1190 int keylen, rsclen;
1191 enum wpa_alg alg;
1192 const u8 *key_rsc;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001193
Mathy Vanhoefc66556c2017-09-29 04:22:51 +02001194 if (sm->ptk.installed) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001195 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1196 "WPA: Do not re-install same PTK to the driver");
1197 return 0;
1198 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001199
1200 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1201 "WPA: Installing PTK to the driver");
1202
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001203 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001204 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
1205 "Suite: NONE - do not use pairwise keys");
1206 return 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001207 }
1208
1209 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001210 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1211 "WPA: Unsupported pairwise cipher %d",
1212 sm->pairwise_cipher);
1213 return -1;
1214 }
1215
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001216 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
1217 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001218 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
1219 wpa_printf(MSG_DEBUG, "WPA: TK length mismatch: %d != %lu",
1220 keylen, (long unsigned int) sm->ptk.tk_len);
1221 return -1;
1222 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001223 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
1224
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001225 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001226 key_rsc = null_rsc;
1227 } else {
1228 key_rsc = key->key_rsc;
1229 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
1230 }
1231
Sunil Ravi77d572f2023-01-17 23:58:31 +00001232 if (wpa_sm_set_key(sm, -1, alg, wpa_sm_get_auth_addr(sm),
1233 sm->keyidx_active, 1, key_rsc, rsclen, sm->ptk.tk,
1234 keylen, KEY_FLAG_PAIRWISE | key_flag) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Sunil Ravi77d572f2023-01-17 23:58:31 +00001236 "WPA: Failed to set PTK to the driver (alg=%d keylen=%d auth_addr="
Hai Shalomfdcde762020-04-02 11:19:20 -07001237 MACSTR " idx=%d key_flag=0x%x)",
Sunil Ravi77d572f2023-01-17 23:58:31 +00001238 alg, keylen, MAC2STR(wpa_sm_get_auth_addr(sm)),
Hai Shalomfdcde762020-04-02 11:19:20 -07001239 sm->keyidx_active, key_flag);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001240 return -1;
1241 }
1242
Sunil Ravi89eba102022-09-13 21:04:37 -07001243#ifdef CONFIG_PASN
1244 if (sm->secure_ltf &&
1245 ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF) &&
1246 wpa_sm_set_ltf_keyseed(sm, sm->own_addr, sm->bssid,
1247 sm->ptk.ltf_keyseed_len,
1248 sm->ptk.ltf_keyseed) < 0) {
1249 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1250 "WPA: Failed to set LTF keyseed to the driver (keylen=%zu bssid="
1251 MACSTR ")", sm->ptk.ltf_keyseed_len,
1252 MAC2STR(sm->bssid));
1253 return -1;
1254 }
1255#endif /* CONFIG_PASN */
1256
Hai Shalom60840252021-02-19 19:02:11 -08001257 wpa_sm_store_ptk(sm, sm->bssid, sm->pairwise_cipher,
1258 sm->dot11RSNAConfigPMKLifetime, &sm->ptk);
1259
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001260 /* TK is not needed anymore in supplicant */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001261 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001262 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +02001263 sm->ptk.installed = 1;
Sunil8cd6f4d2022-06-28 18:40:46 +00001264 sm->tk_set = true;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001265
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001266 if (sm->wpa_ptk_rekey) {
1267 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
1268 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
1269 sm, NULL);
1270 }
Hai Shalomfdcde762020-04-02 11:19:20 -07001271 return 0;
1272}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001273
Hai Shalomfdcde762020-04-02 11:19:20 -07001274
1275static int wpa_supplicant_activate_ptk(struct wpa_sm *sm)
1276{
1277 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Sunil Ravi77d572f2023-01-17 23:58:31 +00001278 "WPA: Activate PTK (idx=%d auth_addr=" MACSTR ")",
1279 sm->keyidx_active, MAC2STR(wpa_sm_get_auth_addr(sm)));
Hai Shalomfdcde762020-04-02 11:19:20 -07001280
Sunil Ravi77d572f2023-01-17 23:58:31 +00001281 if (wpa_sm_set_key(sm, -1, 0, wpa_sm_get_auth_addr(sm),
1282 sm->keyidx_active, 0, NULL, 0, NULL, 0,
1283 KEY_FLAG_PAIRWISE_RX_TX_MODIFY) < 0) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001284 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Sunil Ravi77d572f2023-01-17 23:58:31 +00001285 "WPA: Failed to activate PTK for TX (idx=%d auth_addr="
1286 MACSTR ")", sm->keyidx_active,
1287 MAC2STR(wpa_sm_get_auth_addr(sm)));
Hai Shalomfdcde762020-04-02 11:19:20 -07001288 return -1;
1289 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001290 return 0;
1291}
1292
1293
1294static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
1295 int group_cipher,
1296 int keylen, int maxkeylen,
1297 int *key_rsc_len,
1298 enum wpa_alg *alg)
1299{
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001300 int klen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001301
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001302 *alg = wpa_cipher_to_alg(group_cipher);
1303 if (*alg == WPA_ALG_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001304 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1305 "WPA: Unsupported Group Cipher %d",
1306 group_cipher);
1307 return -1;
1308 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001309 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001311 klen = wpa_cipher_key_len(group_cipher);
1312 if (keylen != klen || maxkeylen < klen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001313 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1314 "WPA: Unsupported %s Group Cipher key length %d (%d)",
1315 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001316 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001317 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001318 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001319}
1320
1321
1322struct wpa_gtk_data {
1323 enum wpa_alg alg;
1324 int tx, key_rsc_len, keyidx;
1325 u8 gtk[32];
1326 int gtk_len;
1327};
1328
1329
1330static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
1331 const struct wpa_gtk_data *gd,
Jouni Malinen58c0e962017-10-01 12:12:24 +03001332 const u8 *key_rsc, int wnm_sleep)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001333{
1334 const u8 *_gtk = gd->gtk;
1335 u8 gtk_buf[32];
1336
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001337 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +03001338 if ((sm->gtk.gtk_len == (size_t) gd->gtk_len &&
1339 os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) ||
1340 (sm->gtk_wnm_sleep.gtk_len == (size_t) gd->gtk_len &&
1341 os_memcmp(sm->gtk_wnm_sleep.gtk, gd->gtk,
1342 sm->gtk_wnm_sleep.gtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001343 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1344 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
1345 gd->keyidx, gd->tx, gd->gtk_len);
1346 return 0;
1347 }
1348
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
1350 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1351 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
1352 gd->keyidx, gd->tx, gd->gtk_len);
1353 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
1354 if (sm->group_cipher == WPA_CIPHER_TKIP) {
1355 /* Swap Tx/Rx keys for Michael MIC */
1356 os_memcpy(gtk_buf, gd->gtk, 16);
1357 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
1358 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
1359 _gtk = gtk_buf;
1360 }
1361 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00001362 if (wpa_sm_set_key(sm, -1, gd->alg, NULL,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001363 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07001364 _gtk, gd->gtk_len,
1365 KEY_FLAG_GROUP_RX_TX_DEFAULT) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001366 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1367 "WPA: Failed to set GTK to the driver "
1368 "(Group only)");
Hai Shalom81f62d82019-07-22 12:10:00 -07001369 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370 return -1;
1371 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00001372 } else if (wpa_sm_set_key(sm, -1, gd->alg, broadcast_ether_addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001373 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07001374 _gtk, gd->gtk_len, KEY_FLAG_GROUP_RX) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001375 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1376 "WPA: Failed to set GTK to "
1377 "the driver (alg=%d keylen=%d keyidx=%d)",
1378 gd->alg, gd->gtk_len, gd->keyidx);
Hai Shalom81f62d82019-07-22 12:10:00 -07001379 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001380 return -1;
1381 }
Hai Shalom81f62d82019-07-22 12:10:00 -07001382 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001383
Jouni Malinen58c0e962017-10-01 12:12:24 +03001384 if (wnm_sleep) {
1385 sm->gtk_wnm_sleep.gtk_len = gd->gtk_len;
1386 os_memcpy(sm->gtk_wnm_sleep.gtk, gd->gtk,
1387 sm->gtk_wnm_sleep.gtk_len);
1388 } else {
1389 sm->gtk.gtk_len = gd->gtk_len;
1390 os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
1391 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001392
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001393 return 0;
1394}
1395
1396
Sunil Ravi77d572f2023-01-17 23:58:31 +00001397static int wpa_supplicant_install_mlo_gtk(struct wpa_sm *sm, u8 link_id,
1398 const struct wpa_gtk_data *gd,
1399 const u8 *key_rsc, int wnm_sleep)
1400{
1401 const u8 *gtk = gd->gtk;
Sunil Ravi7f769292024-07-23 22:21:32 +00001402 u8 gtk_buf[32];
Sunil Ravi77d572f2023-01-17 23:58:31 +00001403
1404 /* Detect possible key reinstallation */
1405 if ((sm->mlo.links[link_id].gtk.gtk_len == (size_t) gd->gtk_len &&
1406 os_memcmp(sm->mlo.links[link_id].gtk.gtk, gd->gtk,
1407 sm->mlo.links[link_id].gtk.gtk_len) == 0) ||
1408 (sm->mlo.links[link_id].gtk_wnm_sleep.gtk_len ==
1409 (size_t) gd->gtk_len &&
1410 os_memcmp(sm->mlo.links[link_id].gtk_wnm_sleep.gtk, gd->gtk,
1411 sm->mlo.links[link_id].gtk_wnm_sleep.gtk_len) == 0)) {
1412 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1413 "RSN: Not reinstalling already in-use GTK to the driver (link_id=%d keyidx=%d tx=%d len=%d)",
1414 link_id, gd->keyidx, gd->tx, gd->gtk_len);
1415 return 0;
1416 }
1417
1418 wpa_hexdump_link_key(MSG_DEBUG, link_id, "RSN: Group Key", gd->gtk,
1419 gd->gtk_len);
1420 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1421 "RSN: Installing GTK to the driver (link_id=%d keyidx=%d tx=%d len=%d)",
1422 link_id, gd->keyidx, gd->tx, gd->gtk_len);
1423 wpa_hexdump_link(MSG_DEBUG, link_id, "RSN: RSC",
1424 key_rsc, gd->key_rsc_len);
Sunil Ravi7f769292024-07-23 22:21:32 +00001425 if (sm->group_cipher == WPA_CIPHER_TKIP) {
1426 /* Swap Tx/Rx keys for Michael MIC */
1427 os_memcpy(gtk_buf, gd->gtk, 16);
1428 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
1429 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
1430 gtk = gtk_buf;
1431 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00001432 if (wpa_sm_set_key(sm, link_id, gd->alg, broadcast_ether_addr,
1433 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len, gtk,
1434 gd->gtk_len, KEY_FLAG_GROUP_RX) < 0) {
1435 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1436 "RSN: Failed to set GTK to the driver (link_id=%d alg=%d keylen=%d keyidx=%d)",
1437 link_id, gd->alg, gd->gtk_len, gd->keyidx);
Sunil Ravi7f769292024-07-23 22:21:32 +00001438 forced_memzero(gtk_buf, sizeof(gtk_buf));
Sunil Ravi77d572f2023-01-17 23:58:31 +00001439 return -1;
1440 }
Sunil Ravi7f769292024-07-23 22:21:32 +00001441 forced_memzero(gtk_buf, sizeof(gtk_buf));
Sunil Ravi77d572f2023-01-17 23:58:31 +00001442
1443 if (wnm_sleep) {
1444 sm->mlo.links[link_id].gtk_wnm_sleep.gtk_len = gd->gtk_len;
1445 os_memcpy(sm->mlo.links[link_id].gtk_wnm_sleep.gtk, gd->gtk,
1446 sm->mlo.links[link_id].gtk_wnm_sleep.gtk_len);
1447 } else {
1448 sm->mlo.links[link_id].gtk.gtk_len = gd->gtk_len;
1449 os_memcpy(sm->mlo.links[link_id].gtk.gtk, gd->gtk,
1450 sm->mlo.links[link_id].gtk.gtk_len);
1451 }
1452
1453 return 0;
1454}
1455
1456
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001457static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
1458 int tx)
1459{
1460 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
1461 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
1462 * seemed to set this bit (incorrectly, since Tx is only when
1463 * doing Group Key only APs) and without this workaround, the
1464 * data connection does not work because wpa_supplicant
1465 * configured non-zero keyidx to be used for unicast. */
1466 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1467 "WPA: Tx bit set for GTK, but pairwise "
1468 "keys are used - ignore Tx bit");
1469 return 0;
1470 }
1471 return tx;
1472}
1473
1474
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001475static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
1476 const u8 *rsc)
1477{
1478 int rsclen;
1479
1480 if (!sm->wpa_rsc_relaxation)
1481 return 0;
1482
1483 rsclen = wpa_cipher_rsc_len(sm->group_cipher);
1484
1485 /*
1486 * Try to detect RSC (endian) corruption issue where the AP sends
1487 * the RSC bytes in EAPOL-Key message in the wrong order, both if
1488 * it's actually a 6-byte field (as it should be) and if it treats
1489 * it as an 8-byte field.
1490 * An AP model known to have this bug is the Sapido RB-1632.
1491 */
1492 if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
1493 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1494 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
1495 rsc[0], rsc[1], rsc[2], rsc[3],
1496 rsc[4], rsc[5], rsc[6], rsc[7]);
1497
1498 return 1;
1499 }
1500
1501 return 0;
1502}
1503
1504
Sunil Ravi77d572f2023-01-17 23:58:31 +00001505static int wpa_supplicant_mlo_gtk(struct wpa_sm *sm, u8 link_id, const u8 *gtk,
1506 size_t gtk_len, int key_info)
1507{
1508 struct wpa_gtk_data gd;
1509 const u8 *key_rsc;
1510 int ret;
1511
1512 /*
1513 * MLO GTK KDE format:
1514 * KeyID[bits 0-1], Tx [bit 2], Reserved [bit 3], link id [4-7]
1515 * PN
1516 * GTK
1517 */
1518 os_memset(&gd, 0, sizeof(gd));
1519 wpa_hexdump_link_key(MSG_DEBUG, link_id,
1520 "RSN: received GTK in pairwise handshake",
1521 gtk, gtk_len);
1522
1523 if (gtk_len < RSN_MLO_GTK_KDE_PREFIX_LENGTH ||
1524 gtk_len - RSN_MLO_GTK_KDE_PREFIX_LENGTH > sizeof(gd.gtk))
1525 return -1;
1526
1527 gd.keyidx = gtk[0] & 0x3;
1528 gtk += 1;
1529 gtk_len -= 1;
1530
1531 key_rsc = gtk;
1532
1533 gtk += 6;
1534 gtk_len -= 6;
1535
1536 os_memcpy(gd.gtk, gtk, gtk_len);
1537 gd.gtk_len = gtk_len;
1538
1539 ret = 0;
1540 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher, gtk_len,
1541 gtk_len, &gd.key_rsc_len,
1542 &gd.alg) ||
1543 wpa_supplicant_install_mlo_gtk(sm, link_id, &gd, key_rsc, 0)) {
1544 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1545 "RSN: Failed to install GTK for MLO Link ID %u",
1546 link_id);
1547 ret = -1;
1548 goto out;
1549 }
1550
1551out:
1552 forced_memzero(&gd, sizeof(gd));
1553 return ret;
1554}
1555
1556
1557static int wpa_supplicant_pairwise_mlo_gtk(struct wpa_sm *sm,
1558 const struct wpa_eapol_key *key,
1559 struct wpa_eapol_ie_parse *ie,
1560 int key_info)
1561{
1562 u8 i;
1563
Sunil Ravi99c035e2024-07-12 01:42:03 +00001564 for_each_link(sm->mlo.valid_links, i) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00001565 if (!ie->mlo_gtk[i]) {
1566 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1567 "MLO RSN: GTK not found for link ID %u", i);
1568 return -1;
1569 }
1570
1571 if (wpa_supplicant_mlo_gtk(sm, i, ie->mlo_gtk[i],
1572 ie->mlo_gtk_len[i], key_info))
1573 return -1;
1574 }
1575
1576 return 0;
1577}
1578
1579
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001580static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
1581 const struct wpa_eapol_key *key,
1582 const u8 *gtk, size_t gtk_len,
1583 int key_info)
1584{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001585 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001586 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001587
1588 /*
1589 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
1590 * GTK KDE format:
1591 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
1592 * Reserved [bits 0-7]
1593 * GTK
1594 */
1595
1596 os_memset(&gd, 0, sizeof(gd));
1597 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
1598 gtk, gtk_len);
1599
1600 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
1601 return -1;
1602
1603 gd.keyidx = gtk[0] & 0x3;
1604 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1605 !!(gtk[0] & BIT(2)));
1606 gtk += 2;
1607 gtk_len -= 2;
1608
1609 os_memcpy(gd.gtk, gtk, gtk_len);
1610 gd.gtk_len = gtk_len;
1611
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001612 key_rsc = key->key_rsc;
1613 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1614 key_rsc = null_rsc;
1615
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001616 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
1617 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1618 gtk_len, gtk_len,
1619 &gd.key_rsc_len, &gd.alg) ||
Jouni Malinen58c0e962017-10-01 12:12:24 +03001620 wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001621 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1622 "RSN: Failed to install GTK");
Hai Shalom81f62d82019-07-22 12:10:00 -07001623 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001624 return -1;
1625 }
Hai Shalom81f62d82019-07-22 12:10:00 -07001626 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001627
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001628 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001629}
1630
1631
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001632static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
Jouni Malinen58c0e962017-10-01 12:12:24 +03001633 const struct wpa_igtk_kde *igtk,
1634 int wnm_sleep)
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001635{
1636 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1637 u16 keyidx = WPA_GET_LE16(igtk->keyid);
1638
1639 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +03001640 if ((sm->igtk.igtk_len == len &&
1641 os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) ||
1642 (sm->igtk_wnm_sleep.igtk_len == len &&
1643 os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1644 sm->igtk_wnm_sleep.igtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001645 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1646 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
1647 keyidx);
1648 return 0;
1649 }
1650
1651 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001652 "WPA: IGTK keyid %d pn " COMPACT_MACSTR,
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001653 keyidx, MAC2STR(igtk->pn));
1654 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
1655 if (keyidx > 4095) {
1656 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1657 "WPA: Invalid IGTK KeyID %d", keyidx);
1658 return -1;
1659 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00001660 if (wpa_sm_set_key(sm, -1, wpa_cipher_to_alg(sm->mgmt_group_cipher),
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001661 broadcast_ether_addr,
1662 keyidx, 0, igtk->pn, sizeof(igtk->pn),
Hai Shalomfdcde762020-04-02 11:19:20 -07001663 igtk->igtk, len, KEY_FLAG_GROUP_RX) < 0) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07001664 if (keyidx == 0x0400 || keyidx == 0x0500) {
1665 /* Assume the AP has broken PMF implementation since it
1666 * seems to have swapped the KeyID bytes. The AP cannot
1667 * be trusted to implement BIP correctly or provide a
1668 * valid IGTK, so do not try to configure this key with
1669 * swapped KeyID bytes. Instead, continue without
1670 * configuring the IGTK so that the driver can drop any
1671 * received group-addressed robust management frames due
1672 * to missing keys.
1673 *
1674 * Normally, this error behavior would result in us
1675 * disconnecting, but there are number of deployed APs
1676 * with this broken behavior, so as an interoperability
1677 * workaround, allow the connection to proceed. */
1678 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1679 "WPA: Ignore IGTK configuration error due to invalid IGTK KeyID byte order");
1680 } else {
1681 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1682 "WPA: Failed to configure IGTK to the driver");
1683 return -1;
1684 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001685 }
1686
Jouni Malinen58c0e962017-10-01 12:12:24 +03001687 if (wnm_sleep) {
1688 sm->igtk_wnm_sleep.igtk_len = len;
1689 os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1690 sm->igtk_wnm_sleep.igtk_len);
1691 } else {
1692 sm->igtk.igtk_len = len;
1693 os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
1694 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001695
1696 return 0;
1697}
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001698
1699
Hai Shalomfdcde762020-04-02 11:19:20 -07001700static int wpa_supplicant_install_bigtk(struct wpa_sm *sm,
1701 const struct wpa_bigtk_kde *bigtk,
1702 int wnm_sleep)
1703{
1704 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1705 u16 keyidx = WPA_GET_LE16(bigtk->keyid);
1706
1707 /* Detect possible key reinstallation */
1708 if ((sm->bigtk.bigtk_len == len &&
1709 os_memcmp(sm->bigtk.bigtk, bigtk->bigtk,
1710 sm->bigtk.bigtk_len) == 0) ||
1711 (sm->bigtk_wnm_sleep.bigtk_len == len &&
1712 os_memcmp(sm->bigtk_wnm_sleep.bigtk, bigtk->bigtk,
1713 sm->bigtk_wnm_sleep.bigtk_len) == 0)) {
1714 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1715 "WPA: Not reinstalling already in-use BIGTK to the driver (keyidx=%d)",
1716 keyidx);
1717 return 0;
1718 }
1719
1720 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1721 "WPA: BIGTK keyid %d pn " COMPACT_MACSTR,
1722 keyidx, MAC2STR(bigtk->pn));
1723 wpa_hexdump_key(MSG_DEBUG, "WPA: BIGTK", bigtk->bigtk, len);
1724 if (keyidx < 6 || keyidx > 7) {
1725 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1726 "WPA: Invalid BIGTK KeyID %d", keyidx);
1727 return -1;
1728 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00001729 if (wpa_sm_set_key(sm, -1, wpa_cipher_to_alg(sm->mgmt_group_cipher),
Hai Shalomfdcde762020-04-02 11:19:20 -07001730 broadcast_ether_addr,
1731 keyidx, 0, bigtk->pn, sizeof(bigtk->pn),
1732 bigtk->bigtk, len, KEY_FLAG_GROUP_RX) < 0) {
1733 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1734 "WPA: Failed to configure BIGTK to the driver");
1735 return -1;
1736 }
1737
1738 if (wnm_sleep) {
1739 sm->bigtk_wnm_sleep.bigtk_len = len;
1740 os_memcpy(sm->bigtk_wnm_sleep.bigtk, bigtk->bigtk,
1741 sm->bigtk_wnm_sleep.bigtk_len);
1742 } else {
1743 sm->bigtk.bigtk_len = len;
1744 os_memcpy(sm->bigtk.bigtk, bigtk->bigtk, sm->bigtk.bigtk_len);
1745 }
1746
1747 return 0;
1748}
1749
1750
Sunil Ravi77d572f2023-01-17 23:58:31 +00001751static int wpa_supplicant_install_mlo_igtk(struct wpa_sm *sm, u8 link_id,
1752 const struct rsn_mlo_igtk_kde *igtk,
1753 int wnm_sleep)
1754{
1755 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1756 u16 keyidx = WPA_GET_LE16(igtk->keyid);
1757
1758 /* Detect possible key reinstallation */
1759 if ((sm->mlo.links[link_id].igtk.igtk_len == len &&
1760 os_memcmp(sm->mlo.links[link_id].igtk.igtk, igtk->igtk,
1761 sm->mlo.links[link_id].igtk.igtk_len) == 0) ||
1762 (sm->mlo.links[link_id].igtk_wnm_sleep.igtk_len == len &&
1763 os_memcmp(sm->mlo.links[link_id].igtk_wnm_sleep.igtk, igtk->igtk,
1764 sm->mlo.links[link_id].igtk_wnm_sleep.igtk_len) == 0)) {
1765 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1766 "RSN: Not reinstalling already in-use IGTK to the driver (link_id=%d keyidx=%d)",
1767 link_id, keyidx);
1768 return 0;
1769 }
1770
1771 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1772 "RSN: MLO Link %u IGTK keyid %d pn " COMPACT_MACSTR,
1773 link_id, keyidx, MAC2STR(igtk->pn));
1774 wpa_hexdump_link_key(MSG_DEBUG, link_id, "RSN: IGTK", igtk->igtk, len);
1775 if (keyidx > 4095) {
1776 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1777 "RSN: Invalid MLO Link %d IGTK KeyID %d", link_id,
1778 keyidx);
1779 return -1;
1780 }
1781 if (wpa_sm_set_key(sm, link_id,
1782 wpa_cipher_to_alg(sm->mgmt_group_cipher),
1783 broadcast_ether_addr, keyidx, 0, igtk->pn,
1784 sizeof(igtk->pn), igtk->igtk, len,
1785 KEY_FLAG_GROUP_RX) < 0) {
1786 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1787 "RSN: Failed to configure MLO Link %d IGTK to the driver",
1788 link_id);
1789 return -1;
1790 }
1791
1792 if (wnm_sleep) {
1793 sm->mlo.links[link_id].igtk_wnm_sleep.igtk_len = len;
1794 os_memcpy(sm->mlo.links[link_id].igtk_wnm_sleep.igtk,
1795 igtk->igtk,
1796 sm->mlo.links[link_id].igtk_wnm_sleep.igtk_len);
1797 } else {
1798 sm->mlo.links[link_id].igtk.igtk_len = len;
1799 os_memcpy(sm->mlo.links[link_id].igtk.igtk, igtk->igtk,
1800 sm->mlo.links[link_id].igtk.igtk_len);
1801 }
1802
1803 return 0;
1804}
1805
1806
1807static int
1808wpa_supplicant_install_mlo_bigtk(struct wpa_sm *sm, u8 link_id,
1809 const struct rsn_mlo_bigtk_kde *bigtk,
1810 int wnm_sleep)
1811{
1812 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1813 u16 keyidx = WPA_GET_LE16(bigtk->keyid);
1814
1815 /* Detect possible key reinstallation */
1816 if ((sm->mlo.links[link_id].bigtk.bigtk_len == len &&
1817 os_memcmp(sm->mlo.links[link_id].bigtk.bigtk, bigtk->bigtk,
1818 sm->mlo.links[link_id].bigtk.bigtk_len) == 0) ||
1819 (sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk_len == len &&
1820 os_memcmp(sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk,
1821 bigtk->bigtk,
1822 sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk_len) ==
1823 0)) {
1824 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1825 "RSN: Not reinstalling already in-use BIGTK to the driver (link_id=%d keyidx=%d)",
1826 link_id, keyidx);
1827 return 0;
1828 }
1829
1830 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1831 "RSN: MLO Link %u BIGTK keyid %d pn " COMPACT_MACSTR,
1832 link_id, keyidx, MAC2STR(bigtk->pn));
1833 wpa_hexdump_link_key(MSG_DEBUG, link_id, "RSN: BIGTK", bigtk->bigtk,
1834 len);
1835 if (keyidx < 6 || keyidx > 7) {
1836 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1837 "RSN: Invalid MLO Link %d BIGTK KeyID %d", link_id,
1838 keyidx);
1839 return -1;
1840 }
1841 if (wpa_sm_set_key(sm, link_id,
1842 wpa_cipher_to_alg(sm->mgmt_group_cipher),
1843 broadcast_ether_addr, keyidx, 0, bigtk->pn,
1844 sizeof(bigtk->pn), bigtk->bigtk, len,
1845 KEY_FLAG_GROUP_RX) < 0) {
1846 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1847 "RSN: Failed to configure MLO Link %d BIGTK to the driver",
1848 link_id);
1849 return -1;
1850 }
1851
1852 if (wnm_sleep) {
1853 sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk_len = len;
1854 os_memcpy(sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk,
1855 bigtk->bigtk,
1856 sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk_len);
1857 } else {
1858 sm->mlo.links[link_id].bigtk.bigtk_len = len;
1859 os_memcpy(sm->mlo.links[link_id].bigtk.bigtk, bigtk->bigtk,
1860 sm->mlo.links[link_id].bigtk.bigtk_len);
1861 }
1862
1863 return 0;
1864}
1865
1866
1867static int _mlo_ieee80211w_set_keys(struct wpa_sm *sm, u8 link_id,
1868 struct wpa_eapol_ie_parse *ie)
1869{
1870 size_t len;
1871
1872 if (ie->mlo_igtk[link_id]) {
1873 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1874 if (ie->mlo_igtk_len[link_id] !=
1875 RSN_MLO_IGTK_KDE_PREFIX_LENGTH + len)
1876 return -1;
1877
1878 if (wpa_supplicant_install_mlo_igtk(
1879 sm, link_id,
1880 (const struct rsn_mlo_igtk_kde *)
1881 ie->mlo_igtk[link_id],
1882 0) < 0)
1883 return -1;
1884 }
1885
1886 if (ie->mlo_bigtk[link_id] && sm->beacon_prot) {
1887 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1888 if (ie->mlo_bigtk_len[link_id] !=
1889 RSN_MLO_BIGTK_KDE_PREFIX_LENGTH + len)
1890 return -1;
1891
1892 if (wpa_supplicant_install_mlo_bigtk(
1893 sm, link_id,
1894 (const struct rsn_mlo_bigtk_kde *)
1895 ie->mlo_bigtk[link_id],
1896 0) < 0)
1897 return -1;
1898 }
1899
1900 return 0;
1901}
1902
1903
1904static int mlo_ieee80211w_set_keys(struct wpa_sm *sm,
1905 struct wpa_eapol_ie_parse *ie)
1906{
1907 u8 i;
1908
1909 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) ||
1910 sm->mgmt_group_cipher == WPA_CIPHER_GTK_NOT_USED)
1911 return 0;
1912
Sunil Ravi99c035e2024-07-12 01:42:03 +00001913 for_each_link(sm->mlo.valid_links, i) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00001914 if (_mlo_ieee80211w_set_keys(sm, i, ie))
1915 return -1;
1916 }
1917
1918 return 0;
1919}
1920
1921
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001922static int ieee80211w_set_keys(struct wpa_sm *sm,
1923 struct wpa_eapol_ie_parse *ie)
1924{
Hai Shalomfdcde762020-04-02 11:19:20 -07001925 size_t len;
1926
Hai Shalom60840252021-02-19 19:02:11 -08001927 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) ||
1928 sm->mgmt_group_cipher == WPA_CIPHER_GTK_NOT_USED)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001929 return 0;
1930
1931 if (ie->igtk) {
1932 const struct wpa_igtk_kde *igtk;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001933
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001934 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1935 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001936 return -1;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001937
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001938 igtk = (const struct wpa_igtk_kde *) ie->igtk;
Jouni Malinen58c0e962017-10-01 12:12:24 +03001939 if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001940 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001941 }
1942
Hai Shalomfdcde762020-04-02 11:19:20 -07001943 if (ie->bigtk && sm->beacon_prot) {
1944 const struct wpa_bigtk_kde *bigtk;
1945
1946 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1947 if (ie->bigtk_len != WPA_BIGTK_KDE_PREFIX_LEN + len)
1948 return -1;
1949
1950 bigtk = (const struct wpa_bigtk_kde *) ie->bigtk;
1951 if (wpa_supplicant_install_bigtk(sm, bigtk, 0) < 0)
1952 return -1;
1953 }
1954
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001955 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001956}
1957
1958
1959static void wpa_report_ie_mismatch(struct wpa_sm *sm,
1960 const char *reason, const u8 *src_addr,
1961 const u8 *wpa_ie, size_t wpa_ie_len,
1962 const u8 *rsn_ie, size_t rsn_ie_len)
1963{
1964 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
1965 reason, MAC2STR(src_addr));
1966
1967 if (sm->ap_wpa_ie) {
1968 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
1969 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
1970 }
1971 if (wpa_ie) {
1972 if (!sm->ap_wpa_ie) {
1973 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1974 "WPA: No WPA IE in Beacon/ProbeResp");
1975 }
1976 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
1977 wpa_ie, wpa_ie_len);
1978 }
1979
1980 if (sm->ap_rsn_ie) {
1981 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
1982 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
1983 }
1984 if (rsn_ie) {
1985 if (!sm->ap_rsn_ie) {
1986 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1987 "WPA: No RSN IE in Beacon/ProbeResp");
1988 }
1989 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
1990 rsn_ie, rsn_ie_len);
1991 }
1992
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001993 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001994}
1995
1996
1997#ifdef CONFIG_IEEE80211R
1998
1999static int ft_validate_mdie(struct wpa_sm *sm,
2000 const unsigned char *src_addr,
2001 struct wpa_eapol_ie_parse *ie,
2002 const u8 *assoc_resp_mdie)
2003{
2004 struct rsn_mdie *mdie;
2005
2006 mdie = (struct rsn_mdie *) (ie->mdie + 2);
2007 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
2008 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
2009 MOBILITY_DOMAIN_ID_LEN) != 0) {
2010 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
2011 "not match with the current mobility domain");
2012 return -1;
2013 }
2014
2015 if (assoc_resp_mdie &&
2016 (assoc_resp_mdie[1] != ie->mdie[1] ||
2017 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
2018 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
2019 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
2020 ie->mdie, 2 + ie->mdie[1]);
2021 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
2022 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
2023 return -1;
2024 }
2025
2026 return 0;
2027}
2028
2029
2030static int ft_validate_ftie(struct wpa_sm *sm,
2031 const unsigned char *src_addr,
2032 struct wpa_eapol_ie_parse *ie,
2033 const u8 *assoc_resp_ftie)
2034{
2035 if (ie->ftie == NULL) {
2036 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2037 "FT: No FTIE in EAPOL-Key msg 3/4");
2038 return -1;
2039 }
2040
2041 if (assoc_resp_ftie == NULL)
2042 return 0;
2043
2044 if (assoc_resp_ftie[1] != ie->ftie[1] ||
2045 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
2046 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
2047 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
2048 ie->ftie, 2 + ie->ftie[1]);
2049 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
2050 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
2051 return -1;
2052 }
2053
2054 return 0;
2055}
2056
2057
2058static int ft_validate_rsnie(struct wpa_sm *sm,
2059 const unsigned char *src_addr,
2060 struct wpa_eapol_ie_parse *ie)
2061{
2062 struct wpa_ie_data rsn;
2063
2064 if (!ie->rsn_ie)
2065 return 0;
2066
2067 /*
2068 * Verify that PMKR1Name from EAPOL-Key message 3/4
2069 * matches with the value we derived.
2070 */
2071 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
2072 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
2073 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
2074 "FT 4-way handshake message 3/4");
2075 return -1;
2076 }
2077
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002078 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
2079 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002080 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2081 "FT: PMKR1Name mismatch in "
2082 "FT 4-way handshake message 3/4");
2083 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
2084 rsn.pmkid, WPA_PMK_NAME_LEN);
2085 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
2086 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
2087 return -1;
2088 }
2089
2090 return 0;
2091}
2092
2093
2094static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
2095 const unsigned char *src_addr,
2096 struct wpa_eapol_ie_parse *ie)
2097{
2098 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
2099
2100 if (sm->assoc_resp_ies) {
2101 pos = sm->assoc_resp_ies;
2102 end = pos + sm->assoc_resp_ies_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002103 while (end - pos > 2) {
2104 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002105 break;
2106 switch (*pos) {
2107 case WLAN_EID_MOBILITY_DOMAIN:
2108 mdie = pos;
2109 break;
2110 case WLAN_EID_FAST_BSS_TRANSITION:
2111 ftie = pos;
2112 break;
2113 }
2114 pos += 2 + pos[1];
2115 }
2116 }
2117
2118 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
2119 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
2120 ft_validate_rsnie(sm, src_addr, ie) < 0)
2121 return -1;
2122
2123 return 0;
2124}
2125
2126#endif /* CONFIG_IEEE80211R */
2127
2128
2129static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
2130 const unsigned char *src_addr,
2131 struct wpa_eapol_ie_parse *ie)
2132{
2133 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
2134 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2135 "WPA: No WPA/RSN IE for this AP known. "
2136 "Trying to get from scan results");
2137 if (wpa_sm_get_beacon_ie(sm) < 0) {
2138 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2139 "WPA: Could not find AP from "
2140 "the scan results");
Hai Shalomfdcde762020-04-02 11:19:20 -07002141 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002142 }
Hai Shalomfdcde762020-04-02 11:19:20 -07002143 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
2144 "WPA: Found the current AP from updated scan results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002145 }
2146
2147 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
2148 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
2149 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
2150 "with IE in Beacon/ProbeResp (no IE?)",
2151 src_addr, ie->wpa_ie, ie->wpa_ie_len,
2152 ie->rsn_ie, ie->rsn_ie_len);
2153 return -1;
2154 }
2155
2156 if ((ie->wpa_ie && sm->ap_wpa_ie &&
2157 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
2158 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
2159 (ie->rsn_ie && sm->ap_rsn_ie &&
2160 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
2161 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
2162 ie->rsn_ie, ie->rsn_ie_len))) {
2163 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
2164 "with IE in Beacon/ProbeResp",
2165 src_addr, ie->wpa_ie, ie->wpa_ie_len,
2166 ie->rsn_ie, ie->rsn_ie_len);
2167 return -1;
2168 }
2169
2170 if (sm->proto == WPA_PROTO_WPA &&
2171 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
2172 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
2173 "detected - RSN was enabled and RSN IE "
2174 "was in msg 3/4, but not in "
2175 "Beacon/ProbeResp",
2176 src_addr, ie->wpa_ie, ie->wpa_ie_len,
2177 ie->rsn_ie, ie->rsn_ie_len);
2178 return -1;
2179 }
2180
Hai Shalom60840252021-02-19 19:02:11 -08002181 if (sm->proto == WPA_PROTO_RSN &&
2182 ((sm->ap_rsnxe && !ie->rsnxe) ||
2183 (!sm->ap_rsnxe && ie->rsnxe) ||
2184 (sm->ap_rsnxe && ie->rsnxe &&
2185 (sm->ap_rsnxe_len != ie->rsnxe_len ||
2186 os_memcmp(sm->ap_rsnxe, ie->rsnxe, sm->ap_rsnxe_len) != 0)))) {
Hai Shalomc3565922019-10-28 11:58:20 -07002187 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2188 "WPA: RSNXE mismatch between Beacon/ProbeResp and EAPOL-Key msg 3/4");
Hai Shalomfdcde762020-04-02 11:19:20 -07002189 wpa_hexdump(MSG_INFO, "RSNXE in Beacon/ProbeResp",
2190 sm->ap_rsnxe, sm->ap_rsnxe_len);
2191 wpa_hexdump(MSG_INFO, "RSNXE in EAPOL-Key msg 3/4",
2192 ie->rsnxe, ie->rsnxe_len);
2193 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Hai Shalomc3565922019-10-28 11:58:20 -07002194 return -1;
2195 }
2196
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002197#ifdef CONFIG_IEEE80211R
2198 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
2199 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
2200 return -1;
2201#endif /* CONFIG_IEEE80211R */
2202
2203 return 0;
2204}
2205
2206
2207/**
2208 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
2209 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2210 * @dst: Destination address for the frame
2211 * @key: Pointer to the EAPOL-Key frame header
2212 * @ver: Version bits from EAPOL-Key Key Info
2213 * @key_info: Key Info
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002214 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002215 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002216 */
2217int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
2218 const struct wpa_eapol_key *key,
2219 u16 ver, u16 key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002220 struct wpa_ptk *ptk)
2221{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002222 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002223 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002224 u8 *rbuf, *key_mic;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002225 u8 *kde = NULL;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002226 size_t kde_len = 0, extra_len = 0;
2227#ifdef CONFIG_TESTING_OPTIONS
2228 size_t pad_len = 0;
2229#endif /* CONFIG_TESTING_OPTIONS */
Sunil Ravi77d572f2023-01-17 23:58:31 +00002230
2231 if (sm->mlo.valid_links) {
2232 u8 *pos;
2233
2234 kde = os_malloc(RSN_SELECTOR_LEN + ETH_ALEN + 2);
2235 if (!kde)
2236 return -1;
2237
2238 /* Add MAC KDE */
2239 wpa_printf(MSG_DEBUG, "MLO: Add MAC KDE into EAPOL-Key 4/4");
2240 pos = kde;
2241 pos = rsn_add_kde(pos, RSN_KEY_DATA_MAC_ADDR, sm->own_addr,
2242 ETH_ALEN);
2243 kde_len = pos - kde;
2244 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002245
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002246#ifdef CONFIG_TESTING_OPTIONS
2247 if (sm->test_eapol_m4_elems)
2248 extra_len = wpabuf_len(sm->test_eapol_m4_elems);
2249 if (sm->encrypt_eapol_m4) {
2250 pad_len = (kde_len + extra_len) % 8;
2251 if (pad_len)
2252 pad_len = 8 - pad_len;
2253 extra_len += pad_len + 8;
2254 }
2255#endif /* CONFIG_TESTING_OPTIONS */
2256
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002257 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002258 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002259 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002260 hdrlen + kde_len + extra_len, &rlen,
2261 (void *) &reply);
Sunil Ravi77d572f2023-01-17 23:58:31 +00002262 if (!rbuf) {
2263 os_free(kde);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002264 return -1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002265 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002266
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002267 reply->type = (sm->proto == WPA_PROTO_RSN ||
2268 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002269 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
2270 key_info &= WPA_KEY_INFO_SECURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002271 key_info |= ver | WPA_KEY_INFO_KEY_TYPE;
2272 if (mic_len)
2273 key_info |= WPA_KEY_INFO_MIC;
2274 else
2275 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002276#ifdef CONFIG_TESTING_OPTIONS
2277 if (sm->encrypt_eapol_m4)
2278 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
2279#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002280 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002281 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002282 WPA_PUT_BE16(reply->key_length, 0);
2283 else
2284 os_memcpy(reply->key_length, key->key_length, 2);
2285 os_memcpy(reply->replay_counter, key->replay_counter,
2286 WPA_REPLAY_COUNTER_LEN);
2287
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002288 key_mic = (u8 *) (reply + 1);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002289 /* Key Data length */
2290 WPA_PUT_BE16(key_mic + mic_len, kde_len + extra_len);
Sunil Ravi77d572f2023-01-17 23:58:31 +00002291 if (kde) {
2292 os_memcpy(key_mic + mic_len + 2, kde, kde_len); /* Key Data */
2293 os_free(kde);
2294 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002295
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002296#ifdef CONFIG_TESTING_OPTIONS
2297 if (sm->test_eapol_m4_elems) {
2298 os_memcpy(key_mic + mic_len + 2 + kde_len,
2299 wpabuf_head(sm->test_eapol_m4_elems),
2300 wpabuf_len(sm->test_eapol_m4_elems));
2301 }
2302
2303 if (sm->encrypt_eapol_m4) {
2304 u8 *plain;
2305 size_t plain_len;
2306
2307 if (sm->test_eapol_m4_elems)
2308 extra_len = wpabuf_len(sm->test_eapol_m4_elems);
2309 else
2310 extra_len = 0;
2311 plain_len = kde_len + extra_len + pad_len;
2312 plain = os_memdup(key_mic + mic_len + 2, plain_len);
2313 if (!plain) {
2314 os_free(rbuf);
2315 return -1;
2316 }
2317 if (pad_len)
2318 plain[plain_len - pad_len] = 0xdd;
2319
2320 wpa_hexdump_key(MSG_DEBUG, "RSN: AES-WRAP using KEK",
2321 ptk->kek, ptk->kek_len);
2322 if (aes_wrap(ptk->kek, ptk->kek_len, plain_len / 8, plain,
2323 key_mic + mic_len + 2)) {
2324 os_free(plain);
2325 os_free(rbuf);
2326 return -1;
2327 }
2328 wpa_hexdump(MSG_DEBUG,
2329 "RSN: Encrypted Key Data from AES-WRAP",
2330 key_mic + mic_len + 2, plain_len + 8);
2331 os_free(plain);
2332 }
2333#endif /* CONFIG_TESTING_OPTIONS */
2334
Roshan Pius5e7db942018-04-12 12:27:41 -07002335 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 4/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002336 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
2337 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002338}
2339
2340
Sunil Ravi77d572f2023-01-17 23:58:31 +00002341static int wpa_supplicant_validate_link_kde(struct wpa_sm *sm, u8 link_id,
2342 const u8 *link_kde,
2343 size_t link_kde_len)
2344{
2345 size_t rsne_len = 0, rsnxe_len = 0;
2346 const u8 *rsne = NULL, *rsnxe = NULL;
2347
2348 if (!link_kde ||
2349 link_kde_len < RSN_MLO_LINK_KDE_LINK_MAC_INDEX + ETH_ALEN) {
2350 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2351 "RSN: MLO Link KDE is not found for link ID %d",
2352 link_id);
2353 return -1;
2354 }
2355
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002356 if (!ether_addr_equal(sm->mlo.links[link_id].bssid,
2357 &link_kde[RSN_MLO_LINK_KDE_LINK_MAC_INDEX])) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00002358 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2359 "RSN: MLO Link %u MAC address (" MACSTR
2360 ") not matching association response (" MACSTR ")",
2361 link_id,
2362 MAC2STR(&link_kde[RSN_MLO_LINK_KDE_LINK_MAC_INDEX]),
2363 MAC2STR(sm->mlo.links[link_id].bssid));
2364 return -1;
2365 }
2366
2367 if (link_kde[0] & RSN_MLO_LINK_KDE_LI_RSNE_INFO) {
2368 rsne = link_kde + RSN_MLO_LINK_KDE_FIXED_LENGTH;
2369 if (link_kde_len < RSN_MLO_LINK_KDE_FIXED_LENGTH + 2 ||
2370 link_kde_len <
2371 (size_t) (RSN_MLO_LINK_KDE_FIXED_LENGTH + 2 + rsne[1])) {
2372 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2373 "RSN: No room for link %u RSNE in MLO Link KDE",
2374 link_id);
2375 return -1;
2376 }
2377
2378 rsne_len = rsne[1] + 2;
2379 }
2380
2381 if (!rsne) {
2382 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2383 "RSN: RSNE not present in MLO Link %u KDE", link_id);
2384 return -1;
2385 }
2386
2387 if (link_kde[0] & RSN_MLO_LINK_KDE_LI_RSNXE_INFO) {
2388 rsnxe = link_kde + RSN_MLO_LINK_KDE_FIXED_LENGTH + rsne_len;
2389 if (link_kde_len <
2390 (RSN_MLO_LINK_KDE_FIXED_LENGTH + rsne_len + 2) ||
2391 link_kde_len <
2392 (RSN_MLO_LINK_KDE_FIXED_LENGTH + rsne_len + 2 + rsnxe[1])) {
2393 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2394 "RSN: No room for link %u RSNXE in MLO Link KDE",
2395 link_id);
2396 return -1;
2397 }
2398
2399 rsnxe_len = rsnxe[1] + 2;
2400 }
2401
2402 if (wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
2403 sm->mlo.links[link_id].ap_rsne,
2404 sm->mlo.links[link_id].ap_rsne_len,
2405 rsne, rsne_len)) {
2406 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2407 "RSN MLO: IE in 3/4 msg does not match with IE in Beacon/ProbeResp for link ID %u",
2408 link_id);
2409 wpa_hexdump(MSG_INFO, "RSNE in Beacon/ProbeResp",
2410 sm->mlo.links[link_id].ap_rsne,
2411 sm->mlo.links[link_id].ap_rsne_len);
2412 wpa_hexdump(MSG_INFO, "RSNE in EAPOL-Key msg 3/4",
2413 rsne, rsne_len);
2414 return -1;
2415 }
2416
2417 if ((sm->mlo.links[link_id].ap_rsnxe && !rsnxe) ||
2418 (!sm->mlo.links[link_id].ap_rsnxe && rsnxe) ||
2419 (sm->mlo.links[link_id].ap_rsnxe && rsnxe &&
2420 (sm->mlo.links[link_id].ap_rsnxe_len != rsnxe_len ||
2421 os_memcmp(sm->mlo.links[link_id].ap_rsnxe, rsnxe,
2422 sm->mlo.links[link_id].ap_rsnxe_len) != 0))) {
2423 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2424 "RSN MLO: RSNXE mismatch between Beacon/ProbeResp and EAPOL-Key msg 3/4 for link ID %u",
2425 link_id);
2426 wpa_hexdump(MSG_INFO, "RSNXE in Beacon/ProbeResp",
2427 sm->mlo.links[link_id].ap_rsnxe,
2428 sm->mlo.links[link_id].ap_rsnxe_len);
2429 wpa_hexdump(MSG_INFO, "RSNXE in EAPOL-Key msg 3/4",
2430 rsnxe, rsnxe_len);
2431 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
2432 return -1;
2433 }
2434
2435 return 0;
2436}
2437
2438
2439static int wpa_validate_mlo_ieee80211w_kdes(struct wpa_sm *sm,
2440 u8 link_id,
2441 struct wpa_eapol_ie_parse *ie)
2442{
2443 if (ie->mlo_igtk[link_id] &&
2444 ie->mlo_igtk_len[link_id] != RSN_MLO_IGTK_KDE_PREFIX_LENGTH +
2445 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
2446 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2447 "RSN MLO: Invalid IGTK KDE length %lu for link ID %u",
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002448 (unsigned long) ie->mlo_igtk_len[link_id], link_id);
Sunil Ravi77d572f2023-01-17 23:58:31 +00002449 return -1;
2450 }
2451
2452 if (!sm->beacon_prot)
2453 return 0;
2454
2455 if (ie->mlo_bigtk[link_id] &&
2456 ie->mlo_bigtk_len[link_id] != RSN_MLO_BIGTK_KDE_PREFIX_LENGTH +
2457 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
2458 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2459 "RSN MLO: Invalid BIGTK KDE length %lu for link ID %u",
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002460 (unsigned long) ie->mlo_bigtk_len[link_id], link_id);
Sunil Ravi77d572f2023-01-17 23:58:31 +00002461 return -1;
2462 }
2463
2464 return 0;
2465}
2466
2467
2468static void wpa_supplicant_process_3_of_4_wpa(struct wpa_sm *sm,
2469 const struct wpa_eapol_key *key,
2470 u16 ver, const u8 *key_data,
2471 size_t key_data_len)
2472{
2473 u16 key_info, keylen;
2474 struct wpa_eapol_ie_parse ie;
2475
2476 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
2477 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2478 "WPA: RX message 3 of 4-Way Handshake from " MACSTR
2479 " (ver=%d)", MAC2STR(sm->bssid), ver);
2480
2481 key_info = WPA_GET_BE16(key->key_info);
2482
2483 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
2484 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
2485 goto failed;
2486
2487 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
2488 goto failed;
2489
2490 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
2491 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2492 "WPA: ANonce from message 1 of 4-Way Handshake differs from 3 of 4-Way Handshake - drop packet (src="
2493 MACSTR ")", MAC2STR(sm->bssid));
2494 goto failed;
2495 }
2496
2497 keylen = WPA_GET_BE16(key->key_length);
2498 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
2499 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2500 "WPA: Invalid %s key length %d (src=" MACSTR ")",
2501 wpa_cipher_txt(sm->pairwise_cipher), keylen,
2502 MAC2STR(sm->bssid));
2503 goto failed;
2504 }
2505
2506 if (wpa_supplicant_send_4_of_4(sm, wpa_sm_get_auth_addr(sm), key, ver,
2507 key_info, &sm->ptk) < 0)
2508 goto failed;
2509
2510 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
2511 * for the next 4-Way Handshake. If msg 3 is received again, the old
2512 * SNonce will still be used to avoid changing PTK. */
2513 sm->renew_snonce = 1;
2514
2515 if ((key_info & WPA_KEY_INFO_INSTALL) &&
2516 wpa_supplicant_install_ptk(sm, key, KEY_FLAG_RX_TX))
2517 goto failed;
2518
2519 if (key_info & WPA_KEY_INFO_SECURE) {
2520 wpa_sm_mlme_setprotection(
2521 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
2522 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
2523 eapol_sm_notify_portValid(sm->eapol, true);
2524 }
2525 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
2526
2527 sm->msg_3_of_4_ok = 1;
2528 return;
2529
2530failed:
2531 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2532}
2533
2534
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002535static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
2536 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002537 u16 ver, const u8 *key_data,
2538 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002539{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002540 u16 key_info, keylen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002541 struct wpa_eapol_ie_parse ie;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002542 bool mlo = sm->mlo.valid_links;
2543 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002544
2545 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
Sunil Ravi77d572f2023-01-17 23:58:31 +00002546 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO,
2547 "RSN: RX message 3 of 4-Way Handshake from " MACSTR
2548 " (ver=%d)%s", MAC2STR(sm->bssid), ver, mlo ? " (MLO)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002549
2550 key_info = WPA_GET_BE16(key->key_info);
2551
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002552 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
2553 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002554 goto failed;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002555
Sunil Ravi7f769292024-07-23 22:21:32 +00002556 if (sm->ssid_protection) {
2557 if (!ie.ssid) {
2558 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2559 "RSN: No SSID included in EAPOL-Key msg 3/4");
2560 goto failed;
2561 }
2562
2563 if (ie.ssid_len != sm->ssid_len ||
2564 os_memcmp(ie.ssid, sm->ssid, sm->ssid_len) != 0) {
2565 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2566 "RSN: SSID mismatch in EAPOL-Key msg 3/4");
2567 wpa_hexdump_ascii(MSG_DEBUG, "RSN: Received SSID",
2568 ie.ssid, ie.ssid_len);
2569 wpa_hexdump_ascii(MSG_DEBUG, "RSN: Expected SSID",
2570 sm->ssid, sm->ssid_len);
2571 goto failed;
2572 }
2573
2574 wpa_sm_ssid_verified(sm);
2575 }
2576
Sunil Ravi77d572f2023-01-17 23:58:31 +00002577 if (mlo && !ie.valid_mlo_gtks) {
2578 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2579 "MLO RSN: No GTK KDE included in EAPOL-Key msg 3/4");
2580 goto failed;
2581 }
2582 if (mlo &&
2583 (key_info &
2584 (WPA_KEY_INFO_ENCR_KEY_DATA | WPA_KEY_INFO_INSTALL |
2585 WPA_KEY_INFO_SECURE)) !=
2586 (WPA_KEY_INFO_ENCR_KEY_DATA | WPA_KEY_INFO_INSTALL |
2587 WPA_KEY_INFO_SECURE)) {
2588 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2589 "RSN MLO: Invalid key info (0x%x) in EAPOL-Key msg 3/4",
2590 key_info);
2591 goto failed;
2592 }
2593
2594 if (mlo && !is_valid_ap_mld_mac_kde(sm, ie.mac_addr)) {
2595 wpa_printf(MSG_DEBUG, "RSN: Invalid AP MLD MAC address KDE");
2596 goto failed;
2597 }
2598
2599 for (i = 0; mlo && i < MAX_NUM_MLD_LINKS; i++) {
2600 if (!(sm->mlo.req_links & BIT(i)))
2601 continue;
2602
2603 if (wpa_supplicant_validate_link_kde(sm, i, ie.mlo_link[i],
2604 ie.mlo_link_len[i]) < 0)
2605 goto failed;
2606
2607 if (!(sm->mlo.valid_links & BIT(i)))
2608 continue;
2609
2610 if (!ie.mlo_gtk[i]) {
2611 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2612 "RSN: GTK not found for link ID %u", i);
2613 goto failed;
2614 }
2615
2616 if (sm->mgmt_group_cipher != WPA_CIPHER_GTK_NOT_USED &&
2617 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
2618 wpa_validate_mlo_ieee80211w_kdes(sm, i, &ie) < 0)
2619 goto failed;
2620 }
2621
2622#ifdef CONFIG_IEEE80211R
2623 if (mlo && wpa_key_mgmt_ft(sm->key_mgmt) &&
2624 wpa_supplicant_validate_ie_ft(sm, sm->bssid, &ie) < 0)
2625 goto failed;
2626#endif /* CONFIG_IEEE80211R */
2627
2628 if (!mlo && ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002629 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2630 "WPA: GTK IE in unencrypted key data");
2631 goto failed;
2632 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00002633 if (!mlo && ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002634 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2635 "WPA: IGTK KDE in unencrypted key data");
2636 goto failed;
2637 }
2638
Sunil Ravi77d572f2023-01-17 23:58:31 +00002639 if (!mlo && ie.igtk &&
Hai Shalom60840252021-02-19 19:02:11 -08002640 sm->mgmt_group_cipher != WPA_CIPHER_GTK_NOT_USED &&
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002641 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
2642 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
2643 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002644 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2645 "WPA: Invalid IGTK KDE length %lu",
2646 (unsigned long) ie.igtk_len);
2647 goto failed;
2648 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002649
Sunil Ravi77d572f2023-01-17 23:58:31 +00002650 if (!mlo && wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002651 goto failed;
2652
Hai Shalomfdcde762020-04-02 11:19:20 -07002653 if (wpa_handle_ext_key_id(sm, &ie))
2654 goto failed;
2655
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002656 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
2657 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2658 "WPA: ANonce from message 1 of 4-Way Handshake "
2659 "differs from 3 of 4-Way Handshake - drop packet (src="
2660 MACSTR ")", MAC2STR(sm->bssid));
2661 goto failed;
2662 }
2663
2664 keylen = WPA_GET_BE16(key->key_length);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002665 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
2666 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2667 "WPA: Invalid %s key length %d (src=" MACSTR
2668 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
2669 MAC2STR(sm->bssid));
2670 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002671 }
2672
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002673#ifdef CONFIG_P2P
2674 if (ie.ip_addr_alloc) {
2675 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
2676 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
2677 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
2678 }
2679#endif /* CONFIG_P2P */
2680
Hai Shalom74f70d42019-02-11 14:42:39 -08002681#ifdef CONFIG_OCV
2682 if (wpa_sm_ocv_enabled(sm)) {
2683 struct wpa_channel_info ci;
2684
2685 if (wpa_sm_channel_info(sm, &ci) != 0) {
2686 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2687 "Failed to get channel info to validate received OCI in EAPOL-Key 3/4");
2688 return;
2689 }
2690
2691 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
2692 channel_width_to_int(ci.chanwidth),
Hai Shalom899fcc72020-10-19 14:38:18 -07002693 ci.seg1_idx) != OCI_SUCCESS) {
2694 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
2695 "addr=" MACSTR " frame=eapol-key-m3 error=%s",
2696 MAC2STR(sm->bssid), ocv_errorstr);
Hai Shalom74f70d42019-02-11 14:42:39 -08002697 return;
2698 }
2699 }
2700#endif /* CONFIG_OCV */
2701
Hai Shalom4fbc08f2020-05-18 12:37:00 -07002702#ifdef CONFIG_DPP2
2703 if (DPP_VERSION > 1 && ie.dpp_kde) {
2704 wpa_printf(MSG_DEBUG,
2705 "DPP: peer Protocol Version %u Flags 0x%x",
2706 ie.dpp_kde[0], ie.dpp_kde[1]);
2707 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_pfs != 2 &&
2708 (ie.dpp_kde[1] & DPP_KDE_PFS_ALLOWED) && !sm->dpp_z) {
2709 wpa_printf(MSG_INFO,
2710 "DPP: Peer indicated it supports PFS and local configuration allows this, but PFS was not negotiated for the association");
2711 goto failed;
2712 }
2713 }
2714#endif /* CONFIG_DPP2 */
2715
Hai Shalomfdcde762020-04-02 11:19:20 -07002716 if (sm->use_ext_key_id &&
2717 wpa_supplicant_install_ptk(sm, key, KEY_FLAG_RX))
2718 goto failed;
2719
Sunil Ravi77d572f2023-01-17 23:58:31 +00002720 if (wpa_supplicant_send_4_of_4(sm, wpa_sm_get_auth_addr(sm), key, ver,
2721 key_info, &sm->ptk) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002722 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002723
2724 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
2725 * for the next 4-Way Handshake. If msg 3 is received again, the old
2726 * SNonce will still be used to avoid changing PTK. */
2727 sm->renew_snonce = 1;
2728
2729 if (key_info & WPA_KEY_INFO_INSTALL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002730 int res;
2731
2732 if (sm->use_ext_key_id)
2733 res = wpa_supplicant_activate_ptk(sm);
2734 else
2735 res = wpa_supplicant_install_ptk(sm, key,
2736 KEY_FLAG_RX_TX);
2737 if (res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002738 goto failed;
2739 }
2740
2741 if (key_info & WPA_KEY_INFO_SECURE) {
2742 wpa_sm_mlme_setprotection(
2743 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
2744 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
Hai Shalome21d4e82020-04-29 16:34:06 -07002745 eapol_sm_notify_portValid(sm->eapol, true);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002746 }
2747 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
2748
Sunil Ravi77d572f2023-01-17 23:58:31 +00002749 if (mlo) {
2750 if (wpa_supplicant_pairwise_mlo_gtk(sm, key, &ie,
2751 key_info) < 0) {
2752 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2753 "MLO RSN: Failed to configure MLO GTKs");
2754 goto failed;
2755 }
2756 } else if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07002757 /* No GTK to be set to the driver */
2758 } else if (!ie.gtk && sm->proto == WPA_PROTO_RSN) {
2759 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2760 "RSN: No GTK KDE included in EAPOL-Key msg 3/4");
2761 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002762 } else if (ie.gtk &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002763 wpa_supplicant_pairwise_gtk(sm, key,
2764 ie.gtk, ie.gtk_len, key_info) < 0) {
2765 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2766 "RSN: Failed to configure GTK");
2767 goto failed;
2768 }
2769
Sunil Ravi77d572f2023-01-17 23:58:31 +00002770 if ((mlo && mlo_ieee80211w_set_keys(sm, &ie) < 0) ||
2771 (!mlo && ieee80211w_set_keys(sm, &ie) < 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002772 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2773 "RSN: Failed to configure IGTK");
2774 goto failed;
2775 }
2776
Sunil Ravi77d572f2023-01-17 23:58:31 +00002777 if (mlo || sm->group_cipher == WPA_CIPHER_GTK_NOT_USED || ie.gtk)
Hai Shalom5f92bc92019-04-18 11:54:11 -07002778 wpa_supplicant_key_neg_complete(sm, sm->bssid,
2779 key_info & WPA_KEY_INFO_SECURE);
2780
Sunil Ravi77d572f2023-01-17 23:58:31 +00002781 if (mlo || ie.gtk)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002782 wpa_sm_set_rekey_offload(sm);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002783
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002784 /* Add PMKSA cache entry for Suite B AKMs here since PMKID can be
2785 * calculated only after KCK has been derived. Though, do not replace an
2786 * existing PMKSA entry after each 4-way handshake (i.e., new KCK/PMKID)
2787 * to avoid unnecessary changes of PMKID while continuing to use the
2788 * same PMK. */
2789 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt) &&
2790 !sm->cur_pmksa) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002791 struct rsn_pmksa_cache_entry *sa;
2792
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002793 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002794 sm->ptk.kck, sm->ptk.kck_len,
Sunil Ravi77d572f2023-01-17 23:58:31 +00002795 wpa_sm_get_auth_addr(sm), sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002796 sm->network_ctx, sm->key_mgmt, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002797 if (!sm->cur_pmksa)
2798 sm->cur_pmksa = sa;
2799 }
2800
Hai Shalomfdcde762020-04-02 11:19:20 -07002801 if (ie.transition_disable)
2802 wpa_sm_transition_disable(sm, ie.transition_disable[0]);
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002803 sm->msg_3_of_4_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002804 return;
2805
2806failed:
2807 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2808}
2809
2810
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002811static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
2812 const struct wpa_eapol_key *key,
2813 int ver, u16 key_info)
2814{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002815 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002816 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002817 u8 *rbuf, *key_mic;
Hai Shalom74f70d42019-02-11 14:42:39 -08002818 size_t kde_len = 0;
2819
Sunil Ravia04bd252022-05-02 22:54:18 -07002820#ifdef CONFIG_TESTING_OPTIONS
2821 if (sm->disable_eapol_g2_tx) {
2822 wpa_printf(MSG_INFO, "TEST: Disable sending EAPOL-Key 2/2");
2823 return 0;
2824 }
2825#endif /* CONFIG_TESTING_OPTIONS */
2826
Hai Shalom74f70d42019-02-11 14:42:39 -08002827#ifdef CONFIG_OCV
2828 if (wpa_sm_ocv_enabled(sm))
2829 kde_len = OCV_OCI_KDE_LEN;
2830#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002831
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002832 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002833 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002834 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Hai Shalom74f70d42019-02-11 14:42:39 -08002835 hdrlen + kde_len, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002836 if (rbuf == NULL)
2837 return -1;
2838
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002839 reply->type = (sm->proto == WPA_PROTO_RSN ||
2840 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002841 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
2842 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002843 key_info |= ver | WPA_KEY_INFO_SECURE;
2844 if (mic_len)
2845 key_info |= WPA_KEY_INFO_MIC;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002846 else
2847 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002848 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002849 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002850 WPA_PUT_BE16(reply->key_length, 0);
2851 else
2852 os_memcpy(reply->key_length, key->key_length, 2);
2853 os_memcpy(reply->replay_counter, key->replay_counter,
2854 WPA_REPLAY_COUNTER_LEN);
2855
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002856 key_mic = (u8 *) (reply + 1);
Hai Shalom74f70d42019-02-11 14:42:39 -08002857 WPA_PUT_BE16(key_mic + mic_len, kde_len); /* Key Data Length */
2858
2859#ifdef CONFIG_OCV
2860 if (wpa_sm_ocv_enabled(sm)) {
2861 struct wpa_channel_info ci;
2862 u8 *pos;
2863
2864 if (wpa_sm_channel_info(sm, &ci) != 0) {
2865 wpa_printf(MSG_WARNING,
2866 "Failed to get channel info for OCI element in EAPOL-Key 2/2");
2867 os_free(rbuf);
2868 return -1;
2869 }
Hai Shalom899fcc72020-10-19 14:38:18 -07002870#ifdef CONFIG_TESTING_OPTIONS
2871 if (sm->oci_freq_override_eapol_g2) {
2872 wpa_printf(MSG_INFO,
2873 "TEST: Override OCI KDE frequency %d -> %d MHz",
2874 ci.frequency,
2875 sm->oci_freq_override_eapol_g2);
2876 ci.frequency = sm->oci_freq_override_eapol_g2;
2877 }
2878#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -08002879
2880 pos = key_mic + mic_len + 2; /* Key Data */
2881 if (ocv_insert_oci_kde(&ci, &pos) < 0) {
2882 os_free(rbuf);
2883 return -1;
2884 }
2885 }
2886#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002887
2888 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002889 return wpa_eapol_key_send(sm, &sm->ptk, ver, wpa_sm_get_auth_addr(sm),
2890 ETH_P_EAPOL, rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002891}
2892
2893
Sunil Ravi77d572f2023-01-17 23:58:31 +00002894static void wpa_supplicant_process_mlo_1_of_2(struct wpa_sm *sm,
2895 const unsigned char *src_addr,
2896 const struct wpa_eapol_key *key,
2897 const u8 *key_data,
2898 size_t key_data_len, u16 ver)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002899{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002900 u16 key_info;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002901 u8 i;
2902 struct wpa_eapol_ie_parse ie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002903
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002904 if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) {
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002905 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00002906 "MLO RSN: Group Key Handshake started prior to completion of 4-way handshake");
2907 goto failed;
2908 }
2909
2910 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "MLO RSN: RX message 1 of Group "
2911 "Key Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr),
2912 ver);
2913
2914 key_info = WPA_GET_BE16(key->key_info);
2915
2916 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
2917
2918 wpa_hexdump_key(MSG_DEBUG, "MLO RSN: msg 1/2 key data", key_data,
2919 key_data_len);
2920 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
2921 goto failed;
2922
2923 if (!ie.valid_mlo_gtks) {
2924 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2925 "MLO RSN: No MLO GTK KDE in Group Key msg 1/2");
2926 goto failed;
2927 }
2928
2929 if (!(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
2930 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2931 "MLO RSN: MLO GTK KDE in unencrypted key data");
2932 goto failed;
2933 }
2934
2935#ifdef CONFIG_OCV
2936 if (wpa_sm_ocv_enabled(sm)) {
2937 struct wpa_channel_info ci;
2938
2939 if (wpa_sm_channel_info(sm, &ci) != 0) {
2940 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2941 "Failed to get channel info to validate received OCI in EAPOL-Key group msg 1/2");
2942 goto failed;
2943 }
2944
2945 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
2946 channel_width_to_int(ci.chanwidth),
2947 ci.seg1_idx) != OCI_SUCCESS) {
2948 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
2949 "addr=" MACSTR " frame=eapol-key-g1 error=%s",
2950 MAC2STR(sm->bssid), ocv_errorstr);
2951 goto failed;
2952 }
2953 }
2954#endif /* CONFIG_OCV */
2955
2956 if (mlo_ieee80211w_set_keys(sm, &ie) < 0)
2957 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2958 "MLO RSN: Failed to configure MLO IGTK");
2959
Sunil Ravi99c035e2024-07-12 01:42:03 +00002960 for_each_link(sm->mlo.valid_links, i) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00002961 /*
2962 * AP may send group keys for subset of the all links during
2963 * rekey
2964 */
2965 if (!ie.mlo_gtk[i])
2966 continue;
2967
2968 if (wpa_supplicant_mlo_gtk(sm, i, ie.mlo_gtk[i],
2969 ie.mlo_gtk_len[i], key_info))
2970 goto failed;
2971 }
2972
2973 if (wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
2974 goto failed;
2975
2976 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "MLO RSN: Group rekeying completed "
2977 "with " MACSTR " [GTK=%s]", MAC2STR(sm->mlo.ap_mld_addr),
2978 wpa_cipher_txt(sm->group_cipher));
2979 wpa_sm_cancel_auth_timeout(sm);
2980 wpa_sm_set_state(sm, WPA_COMPLETED);
2981
2982 wpa_sm_set_rekey_offload(sm);
2983
2984 return;
2985
2986failed:
2987 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2988}
2989
2990
2991static void wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
2992 const unsigned char *src_addr,
2993 const struct wpa_eapol_key *key,
2994 const u8 *key_data,
2995 size_t key_data_len, u16 ver)
2996{
2997 u16 key_info;
2998 int rekey;
2999 struct wpa_gtk_data gd;
3000 const u8 *key_rsc;
3001 size_t maxkeylen;
3002 u16 gtk_len;
3003
3004 if (!sm->msg_3_of_4_ok) {
3005 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07003006 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
3007 goto failed;
3008 }
3009
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003010 os_memset(&gd, 0, sizeof(gd));
3011
3012 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003013 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3014 "WPA: RX message 1 of Group Key Handshake from " MACSTR
3015 " (ver=%d)", MAC2STR(src_addr), ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003016
3017 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003018
Sunil Ravi77d572f2023-01-17 23:58:31 +00003019 gtk_len = WPA_GET_BE16(key->key_length);
3020 maxkeylen = key_data_len;
3021 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
3022 if (maxkeylen < 8) {
3023 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3024 "WPA: Too short maxkeylen (%lu)",
3025 (unsigned long) maxkeylen);
3026 goto failed;
3027 }
3028 maxkeylen -= 8;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003029 }
3030
Sunil Ravi77d572f2023-01-17 23:58:31 +00003031 if (gtk_len > maxkeylen ||
3032 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
3033 gtk_len, maxkeylen,
3034 &gd.key_rsc_len, &gd.alg))
3035 goto failed;
3036
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003037 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
3038
Sunil Ravi77d572f2023-01-17 23:58:31 +00003039 gd.gtk_len = gtk_len;
3040 gd.keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
3041 WPA_KEY_INFO_KEY_INDEX_SHIFT;
3042 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
3043#if defined(CONFIG_NO_RC4) || defined(CONFIG_FIPS)
3044 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3045 "WPA: RC4 not supported in the build");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003046 goto failed;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003047#else /* CONFIG_NO_RC4 || CONFIG_FIPS */
3048 u8 ek[32];
3049 if (key_data_len > sizeof(gd.gtk)) {
3050 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3051 "WPA: RC4 key data too long (%lu)",
3052 (unsigned long) key_data_len);
3053 goto failed;
3054 }
3055 os_memcpy(ek, key->key_iv, 16);
3056 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
3057 os_memcpy(gd.gtk, key_data, key_data_len);
3058 if (rc4_skip(ek, 32, 256, gd.gtk, key_data_len)) {
3059 forced_memzero(ek, sizeof(ek));
3060 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
3061 "WPA: RC4 failed");
3062 goto failed;
3063 }
3064 forced_memzero(ek, sizeof(ek));
3065#endif /* CONFIG_NO_RC4 || CONFIG_FIPS */
3066 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
3067 if (maxkeylen % 8) {
3068 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3069 "WPA: Unsupported AES-WRAP len %lu",
3070 (unsigned long) maxkeylen);
3071 goto failed;
3072 }
3073 if (maxkeylen > sizeof(gd.gtk)) {
3074 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3075 "WPA: AES-WRAP key data "
3076 "too long (keydatalen=%lu maxkeylen=%lu)",
3077 (unsigned long) key_data_len,
3078 (unsigned long) maxkeylen);
3079 goto failed;
3080 }
3081 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
3082 key_data, gd.gtk)) {
3083 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3084 "WPA: AES unwrap failed - could not decrypt "
3085 "GTK");
3086 goto failed;
3087 }
3088 } else {
3089 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3090 "WPA: Unsupported key_info type %d", ver);
3091 goto failed;
3092 }
3093 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
3094 sm, !!(key_info & WPA_KEY_INFO_TXRX));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003095
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003096 key_rsc = key->key_rsc;
3097 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
3098 key_rsc = null_rsc;
3099
Jouni Malinen58c0e962017-10-01 12:12:24 +03003100 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003101 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003102 goto failed;
Hai Shalom81f62d82019-07-22 12:10:00 -07003103 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003104
3105 if (rekey) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00003106 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3107 "WPA: Group rekeying completed with " MACSTR
3108 " [GTK=%s]",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003109 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
3110 wpa_sm_cancel_auth_timeout(sm);
3111 wpa_sm_set_state(sm, WPA_COMPLETED);
3112 } else {
3113 wpa_supplicant_key_neg_complete(sm, sm->bssid,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003114 key_info & WPA_KEY_INFO_SECURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003115 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003116
3117 wpa_sm_set_rekey_offload(sm);
3118
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003119 return;
3120
3121failed:
Hai Shalom81f62d82019-07-22 12:10:00 -07003122 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003123 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
3124}
3125
3126
Sunil Ravi77d572f2023-01-17 23:58:31 +00003127static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
3128 const unsigned char *src_addr,
3129 const struct wpa_eapol_key *key,
3130 const u8 *key_data,
3131 size_t key_data_len, u16 ver)
3132{
3133 u16 key_info;
3134 struct wpa_gtk_data gd;
3135 const u8 *key_rsc;
3136 int maxkeylen;
3137 struct wpa_eapol_ie_parse ie;
3138 u16 gtk_len;
3139
3140 if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) {
3141 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3142 "RSN: Group Key Handshake started prior to completion of 4-way handshake");
3143 goto failed;
3144 }
3145
3146 os_memset(&gd, 0, sizeof(gd));
3147
3148 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3149 "RSN: RX message 1 of Group Key Handshake from " MACSTR
3150 " (ver=%d)", MAC2STR(src_addr), ver);
3151
3152 key_info = WPA_GET_BE16(key->key_info);
3153
3154 wpa_hexdump_key(MSG_DEBUG, "RSN: msg 1/2 key data",
3155 key_data, key_data_len);
3156 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
3157 goto failed;
3158
3159 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
3160
3161 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
3162 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3163 "RSN: GTK KDE in unencrypted key data");
3164 goto failed;
3165 }
3166 if (!ie.gtk) {
3167 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3168 "RSN: No GTK KDE in Group Key msg 1/2");
3169 goto failed;
3170 }
3171 gtk_len = ie.gtk_len;
3172 if (gtk_len < 2) {
3173 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3174 "RSN: Invalid GTK KDE length (%u) in Group Key msg 1/2",
3175 gtk_len);
3176 goto failed;
3177 }
3178 gtk_len -= 2;
3179 if (gtk_len > sizeof(gd.gtk)) {
3180 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3181 "RSN: Too long GTK in GTK KDE (len=%u)", gtk_len);
3182 goto failed;
3183 }
3184 maxkeylen = gd.gtk_len = gtk_len;
3185
3186#ifdef CONFIG_OCV
3187 if (wpa_sm_ocv_enabled(sm)) {
3188 struct wpa_channel_info ci;
3189
3190 if (wpa_sm_channel_info(sm, &ci) != 0) {
3191 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3192 "Failed to get channel info to validate received OCI in EAPOL-Key group msg 1/2");
3193 goto failed;
3194 }
3195
3196 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
3197 channel_width_to_int(ci.chanwidth),
3198 ci.seg1_idx) != OCI_SUCCESS) {
3199 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
3200 "addr=" MACSTR " frame=eapol-key-g1 error=%s",
3201 MAC2STR(sm->bssid), ocv_errorstr);
3202 goto failed;
3203 }
3204 }
3205#endif /* CONFIG_OCV */
3206
3207 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
3208 gtk_len, maxkeylen,
3209 &gd.key_rsc_len, &gd.alg))
3210 goto failed;
3211
3212 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
3213 ie.gtk, 2 + gtk_len);
3214 gd.keyidx = ie.gtk[0] & 0x3;
3215 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
3216 !!(ie.gtk[0] & BIT(2)));
3217 os_memcpy(gd.gtk, ie.gtk + 2, gtk_len);
3218
3219 if (ieee80211w_set_keys(sm, &ie) < 0)
3220 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3221 "RSN: Failed to configure IGTK");
3222
3223 key_rsc = key->key_rsc;
3224 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
3225 key_rsc = null_rsc;
3226
3227 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
3228 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
3229 goto failed;
3230 forced_memzero(&gd, sizeof(gd));
3231
3232 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3233 "RSN: Group rekeying completed with " MACSTR " [GTK=%s]",
3234 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
3235 wpa_sm_cancel_auth_timeout(sm);
3236 wpa_sm_set_state(sm, WPA_COMPLETED);
3237
3238 wpa_sm_set_rekey_offload(sm);
3239
3240 return;
3241
3242failed:
3243 forced_memzero(&gd, sizeof(gd));
3244 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
3245}
3246
3247
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003248static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003249 struct wpa_eapol_key *key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003250 u16 ver,
3251 const u8 *buf, size_t len)
3252{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003253 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003254 int ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003255 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003256
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003257 os_memcpy(mic, key + 1, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003258 if (sm->tptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003259 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003260 if (wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len,
3261 sm->key_mgmt,
3262 ver, buf, len, (u8 *) (key + 1)) < 0 ||
3263 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003264 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3265 "WPA: Invalid EAPOL-Key MIC "
3266 "when using TPTK - ignoring TPTK");
Hai Shalom74f70d42019-02-11 14:42:39 -08003267#ifdef TEST_FUZZ
3268 wpa_printf(MSG_INFO,
3269 "TEST: Ignore Key MIC failure for fuzz testing");
3270 goto continue_fuzz;
3271#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003272 } else {
Hai Shalom74f70d42019-02-11 14:42:39 -08003273#ifdef TEST_FUZZ
3274 continue_fuzz:
3275#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003276 ok = 1;
3277 sm->tptk_set = 0;
3278 sm->ptk_set = 1;
3279 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003280 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003281 /*
3282 * This assures the same TPTK in sm->tptk can never be
Roshan Pius3a1667e2018-07-03 15:17:14 -07003283 * copied twice to sm->ptk as the new PTK. In
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003284 * combination with the installed flag in the wpa_ptk
3285 * struct, this assures the same PTK is only installed
3286 * once.
3287 */
3288 sm->renew_snonce = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003289 }
3290 }
3291
3292 if (!ok && sm->ptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003293 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003294 if (wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len,
3295 sm->key_mgmt,
3296 ver, buf, len, (u8 *) (key + 1)) < 0 ||
3297 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003298 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3299 "WPA: Invalid EAPOL-Key MIC - "
3300 "dropping packet");
Hai Shalom74f70d42019-02-11 14:42:39 -08003301#ifdef TEST_FUZZ
3302 wpa_printf(MSG_INFO,
3303 "TEST: Ignore Key MIC failure for fuzz testing");
3304 goto continue_fuzz2;
3305#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003306 return -1;
3307 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003308#ifdef TEST_FUZZ
3309 continue_fuzz2:
3310#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003311 ok = 1;
3312 }
3313
3314 if (!ok) {
3315 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3316 "WPA: Could not verify EAPOL-Key MIC - "
3317 "dropping packet");
3318 return -1;
3319 }
3320
3321 os_memcpy(sm->rx_replay_counter, key->replay_counter,
3322 WPA_REPLAY_COUNTER_LEN);
3323 sm->rx_replay_counter_set = 1;
3324 return 0;
3325}
3326
3327
3328/* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
3329static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003330 struct wpa_eapol_key *key,
3331 size_t mic_len, u16 ver,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003332 u8 *key_data, size_t *key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003333{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003334 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003335 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003336 if (!sm->ptk_set) {
3337 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3338 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
3339 "Data");
3340 return -1;
3341 }
3342
3343 /* Decrypt key data here so that this operation does not need
3344 * to be implemented separately for each message type. */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003345 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00003346#if defined(CONFIG_NO_RC4) || defined(CONFIG_FIPS)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003347 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3348 "WPA: RC4 not supported in the build");
3349 return -1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003350#else /* CONFIG_NO_RC4 || CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003351 u8 ek[32];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003352
3353 wpa_printf(MSG_DEBUG, "WPA: Decrypt Key Data using RC4");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003354 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003355 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003356 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07003357 forced_memzero(ek, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003358 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
3359 "WPA: RC4 failed");
3360 return -1;
3361 }
Hai Shalom81f62d82019-07-22 12:10:00 -07003362 forced_memzero(ek, sizeof(ek));
Sunil Ravi77d572f2023-01-17 23:58:31 +00003363#endif /* CONFIG_NO_RC4 || CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003364 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003365 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07003366 wpa_use_aes_key_wrap(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003367 u8 *buf;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003368
3369 wpa_printf(MSG_DEBUG,
3370 "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)",
3371 (unsigned int) sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003372 if (*key_data_len < 8 || *key_data_len % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003373 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003374 "WPA: Unsupported AES-WRAP len %u",
3375 (unsigned int) *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003376 return -1;
3377 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003378 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
3379 buf = os_malloc(*key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003380 if (buf == NULL) {
3381 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3382 "WPA: No memory for AES-UNWRAP buffer");
3383 return -1;
3384 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003385#ifdef TEST_FUZZ
3386 os_memset(buf, 0x11, *key_data_len);
3387#endif /* TEST_FUZZ */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003388 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003389 key_data, buf)) {
Hai Shalom74f70d42019-02-11 14:42:39 -08003390#ifdef TEST_FUZZ
3391 wpa_printf(MSG_INFO,
3392 "TEST: Ignore AES unwrap failure for fuzz testing");
3393 goto continue_fuzz;
3394#endif /* TEST_FUZZ */
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08003395 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003396 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3397 "WPA: AES unwrap failed - "
3398 "could not decrypt EAPOL-Key key data");
3399 return -1;
3400 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003401#ifdef TEST_FUZZ
3402 continue_fuzz:
3403#endif /* TEST_FUZZ */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003404 os_memcpy(key_data, buf, *key_data_len);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08003405 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003406 WPA_PUT_BE16(((u8 *) (key + 1)) + mic_len, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003407 } else {
3408 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3409 "WPA: Unsupported key_info type %d", ver);
3410 return -1;
3411 }
3412 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003413 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003414 return 0;
3415}
3416
3417
3418/**
3419 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
3420 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3421 */
3422void wpa_sm_aborted_cached(struct wpa_sm *sm)
3423{
3424 if (sm && sm->cur_pmksa) {
3425 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3426 "RSN: Cancelling PMKSA caching attempt");
3427 sm->cur_pmksa = NULL;
3428 }
3429}
3430
3431
Hai Shalomc1a21442022-02-04 13:43:00 -08003432void wpa_sm_aborted_external_cached(struct wpa_sm *sm)
3433{
3434 if (sm && sm->cur_pmksa && sm->cur_pmksa->external) {
3435 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3436 "RSN: Cancelling external PMKSA caching attempt");
3437 sm->cur_pmksa = NULL;
3438 }
3439}
3440
3441
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003442static void wpa_eapol_key_dump(struct wpa_sm *sm,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003443 const struct wpa_eapol_key *key,
3444 unsigned int key_data_len,
3445 const u8 *mic, unsigned int mic_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003446{
3447#ifndef CONFIG_NO_STDOUT_DEBUG
3448 u16 key_info = WPA_GET_BE16(key->key_info);
3449
3450 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
3451 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3452 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
3453 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
3454 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
3455 WPA_KEY_INFO_KEY_INDEX_SHIFT,
3456 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
3457 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
3458 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
3459 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
3460 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
3461 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
3462 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
3463 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
3464 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
3465 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3466 " key_length=%u key_data_length=%u",
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003467 WPA_GET_BE16(key->key_length), key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003468 wpa_hexdump(MSG_DEBUG, " replay_counter",
3469 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
3470 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
3471 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
3472 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
3473 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003474 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003475#endif /* CONFIG_NO_STDOUT_DEBUG */
3476}
3477
3478
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003479#ifdef CONFIG_FILS
3480static int wpa_supp_aead_decrypt(struct wpa_sm *sm, u8 *buf, size_t buf_len,
3481 size_t *key_data_len)
3482{
3483 struct wpa_ptk *ptk;
3484 struct ieee802_1x_hdr *hdr;
3485 struct wpa_eapol_key *key;
3486 u8 *pos, *tmp;
3487 const u8 *aad[1];
3488 size_t aad_len[1];
3489
3490 if (*key_data_len < AES_BLOCK_SIZE) {
3491 wpa_printf(MSG_INFO, "No room for AES-SIV data in the frame");
3492 return -1;
3493 }
3494
3495 if (sm->tptk_set)
3496 ptk = &sm->tptk;
3497 else if (sm->ptk_set)
3498 ptk = &sm->ptk;
3499 else
3500 return -1;
3501
3502 hdr = (struct ieee802_1x_hdr *) buf;
3503 key = (struct wpa_eapol_key *) (hdr + 1);
3504 pos = (u8 *) (key + 1);
3505 pos += 2; /* Pointing at the Encrypted Key Data field */
3506
3507 tmp = os_malloc(*key_data_len);
3508 if (!tmp)
3509 return -1;
3510
3511 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
3512 * to Key Data (exclusive). */
3513 aad[0] = buf;
3514 aad_len[0] = pos - buf;
3515 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, *key_data_len,
3516 1, aad, aad_len, tmp) < 0) {
3517 wpa_printf(MSG_INFO, "Invalid AES-SIV data in the frame");
3518 bin_clear_free(tmp, *key_data_len);
3519 return -1;
3520 }
3521
3522 /* AEAD decryption and validation completed successfully */
3523 (*key_data_len) -= AES_BLOCK_SIZE;
3524 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
3525 tmp, *key_data_len);
3526
3527 /* Replace Key Data field with the decrypted version */
3528 os_memcpy(pos, tmp, *key_data_len);
3529 pos -= 2; /* Key Data Length field */
3530 WPA_PUT_BE16(pos, *key_data_len);
3531 bin_clear_free(tmp, *key_data_len);
3532
3533 if (sm->tptk_set) {
3534 sm->tptk_set = 0;
3535 sm->ptk_set = 1;
3536 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
3537 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
3538 }
3539
3540 os_memcpy(sm->rx_replay_counter, key->replay_counter,
3541 WPA_REPLAY_COUNTER_LEN);
3542 sm->rx_replay_counter_set = 1;
3543
3544 return 0;
3545}
3546#endif /* CONFIG_FILS */
3547
3548
Sunil Ravi77d572f2023-01-17 23:58:31 +00003549static int wpa_sm_rx_eapol_wpa(struct wpa_sm *sm, const u8 *src_addr,
3550 struct wpa_eapol_key *key,
3551 enum frame_encryption encrypted,
3552 const u8 *tmp, size_t data_len,
3553 u8 *key_data, size_t key_data_len)
3554{
3555 u16 key_info, ver;
3556
3557 key_info = WPA_GET_BE16(key->key_info);
3558
3559 if (key->type != EAPOL_KEY_TYPE_WPA) {
3560 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3561 "WPA: Unsupported EAPOL-Key type %d", key->type);
3562 return -1;
3563 }
3564
3565 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
3566 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
3567 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
3568 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3569 "WPA: Unsupported EAPOL-Key descriptor version %d",
3570 ver);
3571 return -1;
3572 }
3573
3574 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
3575 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
3576 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3577 "WPA: CCMP is used, but EAPOL-Key descriptor version (%d) is not 2",
3578 ver);
3579 if (sm->group_cipher != WPA_CIPHER_CCMP &&
3580 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
3581 /* Earlier versions of IEEE 802.11i did not explicitly
3582 * require version 2 descriptor for all EAPOL-Key
3583 * packets, so allow group keys to use version 1 if
3584 * CCMP is not used for them. */
3585 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3586 "WPA: Backwards compatibility: allow invalid version for non-CCMP group keys");
3587 } else
3588 return -1;
3589 }
3590
3591 if ((key_info & WPA_KEY_INFO_MIC) &&
3592 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
3593 return -1;
3594
3595 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
3596 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
3597 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3598 "WPA: Ignored EAPOL-Key (Pairwise) with non-zero key index");
3599 return -1;
3600 }
3601 if (key_info & (WPA_KEY_INFO_MIC |
3602 WPA_KEY_INFO_ENCR_KEY_DATA)) {
3603 /* 3/4 4-Way Handshake */
3604 wpa_supplicant_process_3_of_4_wpa(sm, key, ver,
3605 key_data,
3606 key_data_len);
3607 } else {
3608 /* 1/4 4-Way Handshake */
3609 wpa_supplicant_process_1_of_4_wpa(sm, src_addr, key,
3610 ver, key_data,
3611 key_data_len,
3612 encrypted);
3613 }
3614 } else {
3615 if (key_info & WPA_KEY_INFO_MIC) {
3616 /* 1/2 Group Key Handshake */
3617 wpa_supplicant_process_1_of_2_wpa(sm, src_addr, key,
3618 key_data,
3619 key_data_len,
3620 ver);
3621 } else {
3622 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3623 "WPA: EAPOL-Key (Group) without Mic/Encr bit - dropped");
3624 }
3625 }
3626
3627 return 1;
3628}
3629
3630
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003631/**
3632 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
3633 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3634 * @src_addr: Source MAC address of the EAPOL packet
3635 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
3636 * @len: Length of the EAPOL frame
Sunil8cd6f4d2022-06-28 18:40:46 +00003637 * @encrypted: Whether the frame was encrypted
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003638 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
3639 *
3640 * This function is called for each received EAPOL frame. Other than EAPOL-Key
3641 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
3642 * only processing WPA and WPA2 EAPOL-Key frames.
3643 *
3644 * The received EAPOL-Key packets are validated and valid packets are replied
3645 * to. In addition, key material (PTK, GTK) is configured at the end of a
3646 * successful key handshake.
3647 */
3648int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
Sunil8cd6f4d2022-06-28 18:40:46 +00003649 const u8 *buf, size_t len, enum frame_encryption encrypted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003650{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003651 size_t plen, data_len, key_data_len;
3652 const struct ieee802_1x_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003653 struct wpa_eapol_key *key;
3654 u16 key_info, ver;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003655 u8 *tmp = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003656 int ret = -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003657 u8 *mic, *key_data;
Hai Shalom899fcc72020-10-19 14:38:18 -07003658 size_t mic_len, keyhdrlen, pmk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003659
3660#ifdef CONFIG_IEEE80211R
3661 sm->ft_completed = 0;
3662#endif /* CONFIG_IEEE80211R */
3663
Hai Shalom899fcc72020-10-19 14:38:18 -07003664 pmk_len = sm->pmk_len;
3665 if (!pmk_len && sm->cur_pmksa)
3666 pmk_len = sm->cur_pmksa->pmk_len;
3667 mic_len = wpa_mic_len(sm->key_mgmt, pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003668 keyhdrlen = sizeof(*key) + mic_len + 2;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003669
3670 if (len < sizeof(*hdr) + keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003671 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3672 "WPA: EAPOL frame too short to be a WPA "
3673 "EAPOL-Key (len %lu, expecting at least %lu)",
3674 (unsigned long) len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003675 (unsigned long) sizeof(*hdr) + keyhdrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003676 return 0;
3677 }
3678
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003679 hdr = (const struct ieee802_1x_hdr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003680 plen = be_to_host16(hdr->length);
3681 data_len = plen + sizeof(*hdr);
3682 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3683 "IEEE 802.1X RX: version=%d type=%d length=%lu",
3684 hdr->version, hdr->type, (unsigned long) plen);
3685
3686 if (hdr->version < EAPOL_VERSION) {
3687 /* TODO: backwards compatibility */
3688 }
3689 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
3690 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3691 "WPA: EAPOL frame (type %u) discarded, "
3692 "not a Key frame", hdr->type);
3693 ret = 0;
3694 goto out;
3695 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003696 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003697 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003698 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3699 "WPA: EAPOL frame payload size %lu "
3700 "invalid (frame size %lu)",
3701 (unsigned long) plen, (unsigned long) len);
3702 ret = 0;
3703 goto out;
3704 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003705 if (data_len < len) {
3706 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3707 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
3708 (unsigned long) len - data_len);
3709 }
3710
3711 /*
3712 * Make a copy of the frame since we need to modify the buffer during
3713 * MAC validation and Key Data decryption.
3714 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003715 tmp = os_memdup(buf, data_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003716 if (tmp == NULL)
3717 goto out;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003718 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003719 mic = (u8 *) (key + 1);
3720 key_data = mic + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003721
3722 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
3723 {
3724 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3725 "WPA: EAPOL-Key type (%d) unknown, discarded",
3726 key->type);
3727 ret = 0;
3728 goto out;
3729 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003730
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003731 key_data_len = WPA_GET_BE16(mic + mic_len);
3732 wpa_eapol_key_dump(sm, key, key_data_len, mic, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003733
3734 if (key_data_len > plen - keyhdrlen) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003735 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
3736 "frame - key_data overflow (%u > %u)",
3737 (unsigned int) key_data_len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003738 (unsigned int) (plen - keyhdrlen));
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003739 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003740 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003741
Sunil Ravi77d572f2023-01-17 23:58:31 +00003742 if (sm->rx_replay_counter_set &&
3743 os_memcmp(key->replay_counter, sm->rx_replay_counter,
3744 WPA_REPLAY_COUNTER_LEN) <= 0) {
3745 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3746 "WPA: EAPOL-Key Replay Counter did not increase - dropping packet");
3747 goto out;
3748 }
3749
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003750 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
Sunil Ravi77d572f2023-01-17 23:58:31 +00003751
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003752 key_info = WPA_GET_BE16(key->key_info);
Sunil Ravi77d572f2023-01-17 23:58:31 +00003753
3754 if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
3755 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3756 "WPA: Unsupported SMK bit in key_info");
3757 goto out;
3758 }
3759
3760 if (!(key_info & WPA_KEY_INFO_ACK)) {
3761 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3762 "WPA: No Ack bit in key_info");
3763 goto out;
3764 }
3765
3766 if (key_info & WPA_KEY_INFO_REQUEST) {
3767 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3768 "WPA: EAPOL-Key with Request bit - dropped");
3769 goto out;
3770 }
3771
3772 if (sm->proto == WPA_PROTO_WPA) {
3773 ret = wpa_sm_rx_eapol_wpa(sm, src_addr, key, encrypted,
3774 tmp, data_len,
3775 key_data, key_data_len);
3776 goto out;
3777 }
3778
3779 if (key->type != EAPOL_KEY_TYPE_RSN) {
3780 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3781 "RSN: Unsupported EAPOL-Key type %d", key->type);
3782 goto out;
3783 }
3784
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003785 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
3786 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003787 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003788 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07003789 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003790 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003791 "RSN: Unsupported EAPOL-Key descriptor version %d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003792 ver);
3793 goto out;
3794 }
3795
Sunil Ravi77d572f2023-01-17 23:58:31 +00003796 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
3797 sm->pairwise_cipher != WPA_CIPHER_TKIP) {
3798 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3799 "RSN: EAPOL-Key descriptor version %d not allowed without TKIP as the pairwise cipher",
3800 ver);
3801 goto out;
3802 }
3803
3804 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
3805 (sm->key_mgmt != WPA_KEY_MGMT_IEEE8021X &&
3806 sm->key_mgmt != WPA_KEY_MGMT_PSK)) {
3807 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3808 "RSN: EAPOL-Key descriptor version %d not allowed due to negotiated AKM (0x%x)",
3809 ver, sm->key_mgmt);
3810 goto out;
3811 }
3812
Roshan Pius3a1667e2018-07-03 15:17:14 -07003813 if (wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003814 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
3815 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3816 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
3817 ver);
3818 goto out;
3819 }
3820
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003821#ifdef CONFIG_IEEE80211R
3822 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
3823 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
Roshan Pius3a1667e2018-07-03 15:17:14 -07003824 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
3825 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003826 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3827 "FT: AP did not use AES-128-CMAC");
3828 goto out;
3829 }
3830 } else
3831#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003832 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003833 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07003834 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003835 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003836 "RSN: AP did not use the negotiated AES-128-CMAC");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003837 goto out;
3838 }
Hai Shalomc3565922019-10-28 11:58:20 -07003839 } else if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
3840 !wpa_use_akm_defined(sm->key_mgmt) &&
3841 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003842 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003843 "RSN: CCMP is used, but EAPOL-Key descriptor version (%d) is not 2", ver);
3844 if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003845 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003846 "RSN: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
3847 } else {
Jouni Malinen658fb4a2014-11-14 20:57:05 +02003848 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003849 "RSN: Unexpected descriptor version %u", ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003850 goto out;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003851 }
Dmitry Shmidt71757432014-06-02 13:50:35 -07003852 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07003853 !wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt71757432014-06-02 13:50:35 -07003854 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003855 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003856 "RSN: GCMP is used, but EAPOL-Key descriptor version (%d) is not 2",
3857 ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003858 goto out;
3859 }
3860
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003861 if ((key_info & WPA_KEY_INFO_MIC) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003862 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003863 goto out;
3864
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003865#ifdef CONFIG_FILS
3866 if (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
3867 if (wpa_supp_aead_decrypt(sm, tmp, data_len, &key_data_len))
3868 goto out;
3869 }
3870#endif /* CONFIG_FILS */
3871
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003872 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003873 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && mic_len) {
Hai Shalomce48b4a2018-09-05 11:41:35 -07003874 /*
3875 * Only decrypt the Key Data field if the frame's authenticity
3876 * was verified. When using AES-SIV (FILS), the MIC flag is not
3877 * set, so this check should only be performed if mic_len != 0
3878 * which is the case in this code branch.
3879 */
3880 if (!(key_info & WPA_KEY_INFO_MIC)) {
3881 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3882 "WPA: Ignore EAPOL-Key with encrypted but unauthenticated data");
3883 goto out;
3884 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003885 if (wpa_supplicant_decrypt_key_data(sm, key, mic_len,
3886 ver, key_data,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003887 &key_data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003888 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003889 }
3890
3891 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
3892 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
3893 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003894 "RSN: Ignored EAPOL-Key (Pairwise) with non-zero key index");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003895 goto out;
3896 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003897 if (key_info & (WPA_KEY_INFO_MIC |
3898 WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003899 /* 3/4 4-Way Handshake */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003900 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
3901 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003902 } else {
3903 /* 1/4 4-Way Handshake */
3904 wpa_supplicant_process_1_of_4(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003905 ver, key_data,
Sunil8cd6f4d2022-06-28 18:40:46 +00003906 key_data_len,
3907 encrypted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003908 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003909 } else {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003910 if ((mic_len && (key_info & WPA_KEY_INFO_MIC)) ||
3911 (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003912 /* 1/2 Group Key Handshake */
Sunil Ravi77d572f2023-01-17 23:58:31 +00003913 if (sm->mlo.valid_links)
3914 wpa_supplicant_process_mlo_1_of_2(sm, src_addr,
3915 key, key_data,
3916 key_data_len,
3917 ver);
3918 else
3919 wpa_supplicant_process_1_of_2(sm, src_addr, key,
3920 key_data,
3921 key_data_len,
3922 ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003923 } else {
3924 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003925 "RSN: EAPOL-Key (Group) without Mic/Encr bit - dropped");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003926 }
3927 }
3928
3929 ret = 1;
3930
3931out:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003932 bin_clear_free(tmp, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003933 return ret;
3934}
3935
3936
3937#ifdef CONFIG_CTRL_IFACE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003938static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
3939{
3940 switch (sm->key_mgmt) {
3941 case WPA_KEY_MGMT_IEEE8021X:
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003942 return ((sm->proto == WPA_PROTO_RSN ||
3943 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003944 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
3945 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
3946 case WPA_KEY_MGMT_PSK:
3947 return (sm->proto == WPA_PROTO_RSN ?
3948 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
3949 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
3950#ifdef CONFIG_IEEE80211R
3951 case WPA_KEY_MGMT_FT_IEEE8021X:
3952 return RSN_AUTH_KEY_MGMT_FT_802_1X;
3953 case WPA_KEY_MGMT_FT_PSK:
3954 return RSN_AUTH_KEY_MGMT_FT_PSK;
3955#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003956 case WPA_KEY_MGMT_IEEE8021X_SHA256:
3957 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
3958 case WPA_KEY_MGMT_PSK_SHA256:
3959 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003960 case WPA_KEY_MGMT_CCKM:
3961 return (sm->proto == WPA_PROTO_RSN ?
3962 RSN_AUTH_KEY_MGMT_CCKM:
3963 WPA_AUTH_KEY_MGMT_CCKM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003964 case WPA_KEY_MGMT_WPA_NONE:
3965 return WPA_AUTH_KEY_MGMT_NONE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003966 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
3967 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003968 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
3969 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003970 case WPA_KEY_MGMT_IEEE8021X_SHA384:
3971 return RSN_AUTH_KEY_MGMT_802_1X_SHA384;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003972 default:
3973 return 0;
3974 }
3975}
3976
3977
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003978#define RSN_SUITE "%02x-%02x-%02x-%d"
3979#define RSN_SUITE_ARG(s) \
3980((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
3981
3982/**
3983 * wpa_sm_get_mib - Dump text list of MIB entries
3984 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3985 * @buf: Buffer for the list
3986 * @buflen: Length of the buffer
3987 * Returns: Number of bytes written to buffer
3988 *
3989 * This function is used fetch dot11 MIB variables.
3990 */
3991int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
3992{
3993 char pmkid_txt[PMKID_LEN * 2 + 1];
Hai Shalome21d4e82020-04-29 16:34:06 -07003994 bool rsna;
3995 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003996 size_t len;
3997
3998 if (sm->cur_pmksa) {
3999 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
4000 sm->cur_pmksa->pmkid, PMKID_LEN);
4001 } else
4002 pmkid_txt[0] = '\0';
4003
Hai Shalome21d4e82020-04-29 16:34:06 -07004004 rsna = (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
4005 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
4006 sm->proto == WPA_PROTO_RSN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004007
4008 ret = os_snprintf(buf, buflen,
4009 "dot11RSNAOptionImplemented=TRUE\n"
4010 "dot11RSNAPreauthenticationImplemented=TRUE\n"
4011 "dot11RSNAEnabled=%s\n"
4012 "dot11RSNAPreauthenticationEnabled=%s\n"
4013 "dot11RSNAConfigVersion=%d\n"
4014 "dot11RSNAConfigPairwiseKeysSupported=5\n"
4015 "dot11RSNAConfigGroupCipherSize=%d\n"
4016 "dot11RSNAConfigPMKLifetime=%d\n"
4017 "dot11RSNAConfigPMKReauthThreshold=%d\n"
4018 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
4019 "dot11RSNAConfigSATimeout=%d\n",
4020 rsna ? "TRUE" : "FALSE",
4021 rsna ? "TRUE" : "FALSE",
4022 RSN_VERSION,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004023 wpa_cipher_key_len(sm->group_cipher) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004024 sm->dot11RSNAConfigPMKLifetime,
4025 sm->dot11RSNAConfigPMKReauthThreshold,
4026 sm->dot11RSNAConfigSATimeout);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004027 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004028 return 0;
4029 len = ret;
4030
4031 ret = os_snprintf(
4032 buf + len, buflen - len,
4033 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
4034 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
4035 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
4036 "dot11RSNAPMKIDUsed=%s\n"
4037 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
4038 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
4039 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
4040 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
4041 "dot11RSNA4WayHandshakeFailures=%u\n",
4042 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004043 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
4044 sm->pairwise_cipher)),
4045 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
4046 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004047 pmkid_txt,
4048 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004049 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
4050 sm->pairwise_cipher)),
4051 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
4052 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004053 sm->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004054 if (!os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004055 len += ret;
4056
4057 return (int) len;
4058}
4059#endif /* CONFIG_CTRL_IFACE */
4060
4061
4062static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004063 void *ctx, enum pmksa_free_reason reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004064{
4065 struct wpa_sm *sm = ctx;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004066 int deauth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004067
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004068 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
4069 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
4070
4071 if (sm->cur_pmksa == entry) {
4072 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4073 "RSN: %s current PMKSA entry",
4074 reason == PMKSA_REPLACE ? "replaced" : "removed");
4075 pmksa_cache_clear_current(sm);
4076
4077 /*
4078 * If an entry is simply being replaced, there's no need to
4079 * deauthenticate because it will be immediately re-added.
4080 * This happens when EAP authentication is completed again
4081 * (reauth or failed PMKSA caching attempt).
4082 */
4083 if (reason != PMKSA_REPLACE)
4084 deauth = 1;
4085 }
4086
4087 if (reason == PMKSA_EXPIRE &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004088 (sm->pmk_len == entry->pmk_len &&
4089 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
4090 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004091 "RSN: deauthenticating due to expired PMK");
4092 pmksa_cache_clear_current(sm);
4093 deauth = 1;
4094 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004095
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004096 if (deauth) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07004097 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004098 os_memset(sm->pmk, 0, sizeof(sm->pmk));
4099 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
4100 }
4101}
4102
4103
Hai Shalomc1a21442022-02-04 13:43:00 -08004104static bool wpa_sm_pmksa_is_current_cb(struct rsn_pmksa_cache_entry *entry,
4105 void *ctx)
4106{
4107 struct wpa_sm *sm = ctx;
4108
4109 return sm->cur_pmksa == entry;
4110}
4111
4112
Sunil Ravi77d572f2023-01-17 23:58:31 +00004113static void wpa_sm_pmksa_notify_cb(struct rsn_pmksa_cache_entry *entry,
4114 void *ctx)
4115{
4116 struct wpa_sm *sm = ctx;
4117
4118 wpa_sm_notify_pmksa_cache_entry(sm, entry);
4119}
4120
4121
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004122/**
4123 * wpa_sm_init - Initialize WPA state machine
4124 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
4125 * Returns: Pointer to the allocated WPA state machine data
4126 *
4127 * This function is used to allocate a new WPA state machine and the returned
4128 * value is passed to all WPA state machine calls.
4129 */
4130struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
4131{
4132 struct wpa_sm *sm;
4133
4134 sm = os_zalloc(sizeof(*sm));
4135 if (sm == NULL)
4136 return NULL;
4137 dl_list_init(&sm->pmksa_candidates);
4138 sm->renew_snonce = 1;
4139 sm->ctx = ctx;
4140
4141 sm->dot11RSNAConfigPMKLifetime = 43200;
4142 sm->dot11RSNAConfigPMKReauthThreshold = 70;
4143 sm->dot11RSNAConfigSATimeout = 60;
4144
Hai Shalomc1a21442022-02-04 13:43:00 -08004145 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb,
Sunil Ravi77d572f2023-01-17 23:58:31 +00004146 wpa_sm_pmksa_is_current_cb,
4147 wpa_sm_pmksa_notify_cb, sm, sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004148 if (sm->pmksa == NULL) {
4149 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
4150 "RSN: PMKSA cache initialization failed");
4151 os_free(sm);
4152 return NULL;
4153 }
4154
4155 return sm;
4156}
4157
4158
4159/**
4160 * wpa_sm_deinit - Deinitialize WPA state machine
4161 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4162 */
4163void wpa_sm_deinit(struct wpa_sm *sm)
4164{
Sunil Ravi77d572f2023-01-17 23:58:31 +00004165 int i;
4166
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004167 if (sm == NULL)
4168 return;
4169 pmksa_cache_deinit(sm->pmksa);
4170 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
4171 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
4172 os_free(sm->assoc_wpa_ie);
Hai Shalomc3565922019-10-28 11:58:20 -07004173 os_free(sm->assoc_rsnxe);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004174 os_free(sm->ap_wpa_ie);
4175 os_free(sm->ap_rsn_ie);
Hai Shalomc3565922019-10-28 11:58:20 -07004176 os_free(sm->ap_rsnxe);
Sunil Ravi77d572f2023-01-17 23:58:31 +00004177 for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
4178 os_free(sm->mlo.links[i].ap_rsne);
4179 os_free(sm->mlo.links[i].ap_rsnxe);
4180 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004181 wpa_sm_drop_sa(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004182 os_free(sm->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004183#ifdef CONFIG_IEEE80211R
4184 os_free(sm->assoc_resp_ies);
4185#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08004186#ifdef CONFIG_TESTING_OPTIONS
4187 wpabuf_free(sm->test_assoc_ie);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004188 wpabuf_free(sm->test_eapol_m2_elems);
4189 wpabuf_free(sm->test_eapol_m4_elems);
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08004190#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004191#ifdef CONFIG_FILS_SK_PFS
4192 crypto_ecdh_deinit(sm->fils_ecdh);
4193#endif /* CONFIG_FILS_SK_PFS */
4194#ifdef CONFIG_FILS
4195 wpabuf_free(sm->fils_ft_ies);
4196#endif /* CONFIG_FILS */
4197#ifdef CONFIG_OWE
4198 crypto_ecdh_deinit(sm->owe_ecdh);
4199#endif /* CONFIG_OWE */
Hai Shalom021b0b52019-04-10 11:17:58 -07004200#ifdef CONFIG_DPP2
4201 wpabuf_clear_free(sm->dpp_z);
4202#endif /* CONFIG_DPP2 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004203 os_free(sm);
4204}
4205
4206
Sunil Ravi77d572f2023-01-17 23:58:31 +00004207static void wpa_sm_clear_ptk(struct wpa_sm *sm)
4208{
4209 int i;
4210
4211 sm->ptk_set = 0;
4212 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
4213 sm->tptk_set = 0;
4214 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
4215 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
4216 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
4217 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
4218 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004219 os_memset(&sm->bigtk, 0, sizeof(sm->bigtk));
4220 os_memset(&sm->bigtk_wnm_sleep, 0, sizeof(sm->bigtk_wnm_sleep));
Sunil Ravi77d572f2023-01-17 23:58:31 +00004221 sm->tk_set = false;
4222 for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
4223 os_memset(&sm->mlo.links[i].gtk, 0,
4224 sizeof(sm->mlo.links[i].gtk));
4225 os_memset(&sm->mlo.links[i].gtk_wnm_sleep, 0,
4226 sizeof(sm->mlo.links[i].gtk_wnm_sleep));
4227 os_memset(&sm->mlo.links[i].igtk, 0,
4228 sizeof(sm->mlo.links[i].igtk));
4229 os_memset(&sm->mlo.links[i].igtk_wnm_sleep, 0,
4230 sizeof(sm->mlo.links[i].igtk_wnm_sleep));
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004231 os_memset(&sm->mlo.links[i].bigtk, 0,
4232 sizeof(sm->mlo.links[i].bigtk));
4233 os_memset(&sm->mlo.links[i].bigtk_wnm_sleep, 0,
4234 sizeof(sm->mlo.links[i].bigtk_wnm_sleep));
Sunil Ravi77d572f2023-01-17 23:58:31 +00004235 }
4236}
4237
4238
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004239/**
4240 * wpa_sm_notify_assoc - Notify WPA state machine about association
4241 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4242 * @bssid: The BSSID of the new association
4243 *
4244 * This function is called to let WPA state machine know that the connection
4245 * was established.
4246 */
4247void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
4248{
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02004249 int clear_keys = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004250
4251 if (sm == NULL)
4252 return;
4253
4254 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4255 "WPA: Association event - clear replay counter");
4256 os_memcpy(sm->bssid, bssid, ETH_ALEN);
4257 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
4258 sm->rx_replay_counter_set = 0;
4259 sm->renew_snonce = 1;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004260 if (ether_addr_equal(sm->preauth_bssid, bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004261 rsn_preauth_deinit(sm);
4262
4263#ifdef CONFIG_IEEE80211R
4264 if (wpa_ft_is_completed(sm)) {
4265 /*
4266 * Clear portValid to kick EAPOL state machine to re-enter
4267 * AUTHENTICATED state to get the EAPOL port Authorized.
4268 */
Hai Shalome21d4e82020-04-29 16:34:06 -07004269 eapol_sm_notify_portValid(sm->eapol, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004270 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
4271
4272 /* Prepare for the next transition */
4273 wpa_ft_prepare_auth_request(sm, NULL);
4274
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02004275 clear_keys = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07004276 sm->ft_protocol = 1;
4277 } else {
4278 sm->ft_protocol = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004279 }
4280#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004281#ifdef CONFIG_FILS
4282 if (sm->fils_completed) {
4283 /*
4284 * Clear portValid to kick EAPOL state machine to re-enter
4285 * AUTHENTICATED state to get the EAPOL port Authorized.
4286 */
4287 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02004288 clear_keys = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004289 }
4290#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004291
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02004292 if (clear_keys) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004293 /*
4294 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
4295 * this is not part of a Fast BSS Transition.
4296 */
4297 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
Sunil Ravi77d572f2023-01-17 23:58:31 +00004298 wpa_sm_clear_ptk(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004299 }
4300
4301#ifdef CONFIG_TDLS
4302 wpa_tdls_assoc(sm);
4303#endif /* CONFIG_TDLS */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004304
4305#ifdef CONFIG_P2P
4306 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
4307#endif /* CONFIG_P2P */
Hai Shalomfdcde762020-04-02 11:19:20 -07004308
4309 sm->keyidx_active = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004310}
4311
4312
4313/**
4314 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
4315 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4316 *
4317 * This function is called to let WPA state machine know that the connection
4318 * was lost. This will abort any existing pre-authentication session.
4319 */
4320void wpa_sm_notify_disassoc(struct wpa_sm *sm)
4321{
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07004322 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
4323 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004324 rsn_preauth_deinit(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004325 pmksa_cache_clear_current(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004326 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
4327 sm->dot11RSNA4WayHandshakeFailures++;
4328#ifdef CONFIG_TDLS
4329 wpa_tdls_disassoc(sm);
4330#endif /* CONFIG_TDLS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004331#ifdef CONFIG_FILS
4332 sm->fils_completed = 0;
4333#endif /* CONFIG_FILS */
Jouni Malinen4283f9e2017-09-22 12:06:37 +03004334#ifdef CONFIG_IEEE80211R
4335 sm->ft_reassoc_completed = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07004336 sm->ft_protocol = 0;
Jouni Malinen4283f9e2017-09-22 12:06:37 +03004337#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004338
4339 /* Keys are not needed in the WPA state machine anymore */
4340 wpa_sm_drop_sa(sm);
Hai Shalomfdcde762020-04-02 11:19:20 -07004341 sm->keyidx_active = 0;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07004342
4343 sm->msg_3_of_4_ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004344 os_memset(sm->bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004345}
4346
4347
4348/**
4349 * wpa_sm_set_pmk - Set PMK
4350 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4351 * @pmk: The new PMK
4352 * @pmk_len: The length of the new PMK in bytes
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004353 * @pmkid: Calculated PMKID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004354 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004355 *
4356 * Configure the PMK for WPA state machine.
4357 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004358void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004359 const u8 *pmkid, const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004360{
4361 if (sm == NULL)
4362 return;
4363
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004364 wpa_hexdump_key(MSG_DEBUG, "WPA: Set PMK based on external data",
4365 pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004366 sm->pmk_len = pmk_len;
4367 os_memcpy(sm->pmk, pmk, pmk_len);
4368
4369#ifdef CONFIG_IEEE80211R
4370 /* Set XXKey to be PSK for FT key derivation */
4371 sm->xxkey_len = pmk_len;
4372 os_memcpy(sm->xxkey, pmk, pmk_len);
4373#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004374
4375 if (bssid) {
Hai Shalomc1a21442022-02-04 13:43:00 -08004376 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len,
4377 pmkid, NULL, 0, bssid,
4378 sm->own_addr,
4379 sm->network_ctx, sm->key_mgmt,
4380 NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004381 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004382}
4383
4384
4385/**
4386 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
4387 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4388 *
4389 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
4390 * will be cleared.
4391 */
4392void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
4393{
4394 if (sm == NULL)
4395 return;
4396
4397 if (sm->cur_pmksa) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004398 wpa_hexdump_key(MSG_DEBUG,
4399 "WPA: Set PMK based on current PMKSA",
4400 sm->cur_pmksa->pmk, sm->cur_pmksa->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004401 sm->pmk_len = sm->cur_pmksa->pmk_len;
4402 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
4403 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004404 wpa_printf(MSG_DEBUG, "WPA: No current PMKSA - clear PMK");
4405 sm->pmk_len = 0;
4406 os_memset(sm->pmk, 0, PMK_LEN_MAX);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004407 }
4408}
4409
4410
4411/**
4412 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
4413 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4414 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
4415 */
4416void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
4417{
4418 if (sm)
4419 sm->fast_reauth = fast_reauth;
4420}
4421
4422
4423/**
4424 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
4425 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4426 * @scard_ctx: Context pointer for smartcard related callback functions
4427 */
4428void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
4429{
4430 if (sm == NULL)
4431 return;
4432 sm->scard_ctx = scard_ctx;
4433 if (sm->preauth_eapol)
4434 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
4435}
4436
4437
4438/**
Hai Shalomfdcde762020-04-02 11:19:20 -07004439 * wpa_sm_set_config - Notification of current configuration change
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004440 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4441 * @config: Pointer to current network configuration
4442 *
4443 * Notify WPA state machine that configuration has changed. config will be
4444 * stored as a backpointer to network configuration. This can be %NULL to clear
4445 * the stored pointed.
4446 */
4447void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
4448{
4449 if (!sm)
4450 return;
4451
4452 if (config) {
4453 sm->network_ctx = config->network_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004454 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
4455 sm->proactive_key_caching = config->proactive_key_caching;
4456 sm->eap_workaround = config->eap_workaround;
4457 sm->eap_conf_ctx = config->eap_conf_ctx;
4458 if (config->ssid) {
4459 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
4460 sm->ssid_len = config->ssid_len;
4461 } else
4462 sm->ssid_len = 0;
4463 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004464 sm->p2p = config->p2p;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004465 sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
Hai Shalomfdcde762020-04-02 11:19:20 -07004466 sm->owe_ptk_workaround = config->owe_ptk_workaround;
Hai Shalom60840252021-02-19 19:02:11 -08004467 sm->force_kdk_derivation = config->force_kdk_derivation;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004468#ifdef CONFIG_FILS
4469 if (config->fils_cache_id) {
4470 sm->fils_cache_id_set = 1;
4471 os_memcpy(sm->fils_cache_id, config->fils_cache_id,
4472 FILS_CACHE_ID_LEN);
4473 } else {
4474 sm->fils_cache_id_set = 0;
4475 }
4476#endif /* CONFIG_FILS */
Hai Shalomfdcde762020-04-02 11:19:20 -07004477 sm->beacon_prot = config->beacon_prot;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004478 } else {
4479 sm->network_ctx = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004480 sm->allowed_pairwise_cipher = 0;
4481 sm->proactive_key_caching = 0;
4482 sm->eap_workaround = 0;
4483 sm->eap_conf_ctx = NULL;
4484 sm->ssid_len = 0;
4485 sm->wpa_ptk_rekey = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004486 sm->p2p = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004487 sm->wpa_rsc_relaxation = 0;
Hai Shalomfdcde762020-04-02 11:19:20 -07004488 sm->owe_ptk_workaround = 0;
4489 sm->beacon_prot = 0;
Hai Shalom60840252021-02-19 19:02:11 -08004490 sm->force_kdk_derivation = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004491 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004492}
4493
4494
Sunil Ravi7f769292024-07-23 22:21:32 +00004495void wpa_sm_set_ssid(struct wpa_sm *sm, const u8 *ssid, size_t ssid_len)
4496{
4497 if (!sm)
4498 return;
4499
4500 if (ssid) {
4501 os_memcpy(sm->ssid, ssid, ssid_len);
4502 sm->ssid_len = ssid_len;
4503 } else {
4504 sm->ssid_len = 0;
4505 }
4506}
4507
4508
Sunil Ravi77d572f2023-01-17 23:58:31 +00004509int wpa_sm_set_mlo_params(struct wpa_sm *sm, const struct wpa_sm_mlo *mlo)
4510{
4511 int i;
4512
4513 if (!sm)
4514 return -1;
4515
4516 os_memcpy(sm->mlo.ap_mld_addr, mlo->ap_mld_addr, ETH_ALEN);
4517 sm->mlo.assoc_link_id = mlo->assoc_link_id;
4518 sm->mlo.valid_links = mlo->valid_links;
4519 sm->mlo.req_links = mlo->req_links;
4520
4521 for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
4522 const u8 *ie;
4523 size_t len;
4524
4525 if (sm->mlo.req_links & BIT(i)) {
4526 if (!mlo->links[i].ap_rsne ||
4527 mlo->links[i].ap_rsne_len == 0) {
4528 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO,
4529 "RSN: No RSNE for AP MLO link %d with BSSID "
4530 MACSTR,
4531 i, MAC2STR(mlo->links[i].bssid));
4532 return -1;
4533
4534 }
4535 os_memcpy(sm->mlo.links[i].addr, mlo->links[i].addr,
4536 ETH_ALEN);
4537 os_memcpy(sm->mlo.links[i].bssid, mlo->links[i].bssid,
4538 ETH_ALEN);
4539 }
4540
4541 ie = mlo->links[i].ap_rsne;
4542 len = mlo->links[i].ap_rsne_len;
4543 os_free(sm->mlo.links[i].ap_rsne);
4544 if (!ie || len == 0) {
4545 if (sm->mlo.links[i].ap_rsne)
4546 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4547 "RSN: Clearing MLO link[%u] AP RSNE",
4548 i);
4549 sm->mlo.links[i].ap_rsne = NULL;
4550 sm->mlo.links[i].ap_rsne_len = 0;
4551 } else {
4552 wpa_hexdump_link(MSG_DEBUG, i, "RSN: Set AP RSNE",
4553 ie, len);
Sunil Ravi7f769292024-07-23 22:21:32 +00004554 if (ie[0] == WLAN_EID_VENDOR_SPECIFIC && len > 2 + 4) {
4555 sm->mlo.links[i].ap_rsne = os_malloc(len - 4);
4556 if (!sm->mlo.links[i].ap_rsne)
4557 return -1;
4558 sm->mlo.links[i].ap_rsne[0] = WLAN_EID_RSN;
4559 sm->mlo.links[i].ap_rsne[1] = len - 2 - 4;
4560 os_memcpy(&sm->mlo.links[i].ap_rsne[2],
4561 ie + 2 + 4, len - 2 - 4);
4562 sm->mlo.links[i].ap_rsne_len = len - 4;
4563 wpa_hexdump(MSG_DEBUG,
4564 "RSN: Converted RSNE override to RSNE",
4565 sm->mlo.links[i].ap_rsne,
4566 sm->mlo.links[i].ap_rsne_len);
4567 } else {
4568 sm->mlo.links[i].ap_rsne = os_memdup(ie, len);
4569 if (!sm->mlo.links[i].ap_rsne) {
4570 sm->mlo.links[i].ap_rsne_len = 0;
4571 return -1;
4572 }
4573 sm->mlo.links[i].ap_rsne_len = len;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004574 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00004575 }
4576
4577 ie = mlo->links[i].ap_rsnxe;
4578 len = mlo->links[i].ap_rsnxe_len;
4579 os_free(sm->mlo.links[i].ap_rsnxe);
4580 if (!ie || len == 0) {
4581 if (sm->mlo.links[i].ap_rsnxe)
4582 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4583 "RSN: Clearing MLO link[%u] AP RSNXE",
4584 i);
4585 sm->mlo.links[i].ap_rsnxe = NULL;
4586 sm->mlo.links[i].ap_rsnxe_len = 0;
4587 } else {
4588 wpa_hexdump_link(MSG_DEBUG, i, "RSN: Set AP RSNXE", ie,
4589 len);
Sunil Ravi7f769292024-07-23 22:21:32 +00004590 if (ie[0] == WLAN_EID_VENDOR_SPECIFIC && len > 2 + 4) {
4591 sm->mlo.links[i].ap_rsnxe = os_malloc(len - 4);
4592 if (!sm->mlo.links[i].ap_rsnxe)
4593 return -1;
4594 sm->mlo.links[i].ap_rsnxe[0] = WLAN_EID_RSNX;
4595 sm->mlo.links[i].ap_rsnxe[1] = len - 2 - 4;
4596 os_memcpy(&sm->mlo.links[i].ap_rsnxe[2],
4597 ie + 2 + 4, len - 2 - 4);
4598 sm->mlo.links[i].ap_rsnxe_len = len - 4;
4599 wpa_hexdump(MSG_DEBUG,
4600 "RSN: Converted RSNXE override to RSNXE",
4601 sm->mlo.links[i].ap_rsnxe,
4602 sm->mlo.links[i].ap_rsnxe_len);
4603 } else {
4604 sm->mlo.links[i].ap_rsnxe = os_memdup(ie, len);
4605 if (!sm->mlo.links[i].ap_rsnxe) {
4606 sm->mlo.links[i].ap_rsnxe_len = 0;
4607 return -1;
4608 }
4609 sm->mlo.links[i].ap_rsnxe_len = len;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004610 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00004611 }
4612 }
4613
4614 return 0;
4615}
4616
4617
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004618/**
4619 * wpa_sm_set_own_addr - Set own MAC address
4620 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4621 * @addr: Own MAC address
4622 */
4623void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
4624{
4625 if (sm)
4626 os_memcpy(sm->own_addr, addr, ETH_ALEN);
4627}
4628
4629
4630/**
4631 * wpa_sm_set_ifname - Set network interface name
4632 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4633 * @ifname: Interface name
4634 * @bridge_ifname: Optional bridge interface name (for pre-auth)
4635 */
4636void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
4637 const char *bridge_ifname)
4638{
4639 if (sm) {
4640 sm->ifname = ifname;
4641 sm->bridge_ifname = bridge_ifname;
4642 }
4643}
4644
4645
4646/**
4647 * wpa_sm_set_eapol - Set EAPOL state machine pointer
4648 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4649 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
4650 */
4651void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
4652{
4653 if (sm)
4654 sm->eapol = eapol;
4655}
4656
4657
4658/**
4659 * wpa_sm_set_param - Set WPA state machine parameters
4660 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4661 * @param: Parameter field
4662 * @value: Parameter value
4663 * Returns: 0 on success, -1 on failure
4664 */
4665int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
4666 unsigned int value)
4667{
4668 int ret = 0;
4669
4670 if (sm == NULL)
4671 return -1;
4672
4673 switch (param) {
4674 case RSNA_PMK_LIFETIME:
4675 if (value > 0)
4676 sm->dot11RSNAConfigPMKLifetime = value;
4677 else
4678 ret = -1;
4679 break;
4680 case RSNA_PMK_REAUTH_THRESHOLD:
4681 if (value > 0 && value <= 100)
4682 sm->dot11RSNAConfigPMKReauthThreshold = value;
4683 else
4684 ret = -1;
4685 break;
4686 case RSNA_SA_TIMEOUT:
4687 if (value > 0)
4688 sm->dot11RSNAConfigSATimeout = value;
4689 else
4690 ret = -1;
4691 break;
4692 case WPA_PARAM_PROTO:
4693 sm->proto = value;
4694 break;
4695 case WPA_PARAM_PAIRWISE:
4696 sm->pairwise_cipher = value;
4697 break;
4698 case WPA_PARAM_GROUP:
4699 sm->group_cipher = value;
4700 break;
4701 case WPA_PARAM_KEY_MGMT:
4702 sm->key_mgmt = value;
4703 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004704 case WPA_PARAM_MGMT_GROUP:
4705 sm->mgmt_group_cipher = value;
4706 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004707 case WPA_PARAM_RSN_ENABLED:
4708 sm->rsn_enabled = value;
4709 break;
4710 case WPA_PARAM_MFP:
4711 sm->mfp = value;
4712 break;
Hai Shalom74f70d42019-02-11 14:42:39 -08004713 case WPA_PARAM_OCV:
4714 sm->ocv = value;
4715 break;
Hai Shalomc3565922019-10-28 11:58:20 -07004716 case WPA_PARAM_SAE_PWE:
4717 sm->sae_pwe = value;
4718 break;
Hai Shalom899fcc72020-10-19 14:38:18 -07004719 case WPA_PARAM_SAE_PK:
4720 sm->sae_pk = value;
4721 break;
Hai Shalomfdcde762020-04-02 11:19:20 -07004722 case WPA_PARAM_DENY_PTK0_REKEY:
4723 sm->wpa_deny_ptk0_rekey = value;
4724 break;
4725 case WPA_PARAM_EXT_KEY_ID:
4726 sm->ext_key_id = value;
4727 break;
4728 case WPA_PARAM_USE_EXT_KEY_ID:
4729 sm->use_ext_key_id = value;
4730 break;
Hai Shalomb755a2a2020-04-23 21:49:02 -07004731#ifdef CONFIG_TESTING_OPTIONS
4732 case WPA_PARAM_FT_RSNXE_USED:
4733 sm->ft_rsnxe_used = value;
4734 break;
Hai Shalom899fcc72020-10-19 14:38:18 -07004735 case WPA_PARAM_OCI_FREQ_EAPOL:
4736 sm->oci_freq_override_eapol = value;
4737 break;
4738 case WPA_PARAM_OCI_FREQ_EAPOL_G2:
4739 sm->oci_freq_override_eapol_g2 = value;
4740 break;
4741 case WPA_PARAM_OCI_FREQ_FT_ASSOC:
4742 sm->oci_freq_override_ft_assoc = value;
4743 break;
4744 case WPA_PARAM_OCI_FREQ_FILS_ASSOC:
4745 sm->oci_freq_override_fils_assoc = value;
4746 break;
Sunil Ravia04bd252022-05-02 22:54:18 -07004747 case WPA_PARAM_DISABLE_EAPOL_G2_TX:
4748 sm->disable_eapol_g2_tx = value;
4749 break;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004750 case WPA_PARAM_ENCRYPT_EAPOL_M2:
4751 sm->encrypt_eapol_m2 = value;
4752 break;
4753 case WPA_PARAM_ENCRYPT_EAPOL_M4:
4754 sm->encrypt_eapol_m4 = value;
4755 break;
Hai Shalomb755a2a2020-04-23 21:49:02 -07004756#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004757#ifdef CONFIG_DPP2
4758 case WPA_PARAM_DPP_PFS:
4759 sm->dpp_pfs = value;
4760 break;
4761#endif /* CONFIG_DPP2 */
Sunil Ravi640215c2023-06-28 23:08:09 +00004762 case WPA_PARAM_WMM_ENABLED:
4763 sm->wmm_enabled = value;
4764 break;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004765 case WPA_PARAM_FT_PREPEND_PMKID:
4766 sm->ft_prepend_pmkid = value;
4767 break;
Sunil Ravi7f769292024-07-23 22:21:32 +00004768 case WPA_PARAM_SSID_PROTECTION:
4769 sm->ssid_protection = value;
4770 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004771 default:
4772 break;
4773 }
4774
4775 return ret;
4776}
4777
4778
4779/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004780 * wpa_sm_get_status - Get WPA state machine
4781 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4782 * @buf: Buffer for status information
4783 * @buflen: Maximum buffer length
4784 * @verbose: Whether to include verbose status information
4785 * Returns: Number of bytes written to buf.
4786 *
4787 * Query WPA state machine for status information. This function fills in
4788 * a text area with current status information. If the buffer (buf) is not
4789 * large enough, status information will be truncated to fit the buffer.
4790 */
4791int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
4792 int verbose)
4793{
4794 char *pos = buf, *end = buf + buflen;
4795 int ret;
4796
4797 ret = os_snprintf(pos, end - pos,
4798 "pairwise_cipher=%s\n"
4799 "group_cipher=%s\n"
4800 "key_mgmt=%s\n",
4801 wpa_cipher_txt(sm->pairwise_cipher),
4802 wpa_cipher_txt(sm->group_cipher),
4803 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004804 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004805 return pos - buf;
4806 pos += ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004807
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004808#ifdef CONFIG_DPP2
4809 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
4810 ret = os_snprintf(pos, end - pos, "dpp_pfs=1\n");
4811 if (os_snprintf_error(end - pos, ret))
4812 return pos - buf;
4813 pos += ret;
4814 }
4815#endif /* CONFIG_DPP2 */
4816
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004817 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
4818 struct wpa_ie_data rsn;
4819 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
4820 >= 0 &&
4821 rsn.capabilities & (WPA_CAPABILITY_MFPR |
4822 WPA_CAPABILITY_MFPC)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004823 ret = os_snprintf(pos, end - pos, "pmf=%d\n"
4824 "mgmt_group_cipher=%s\n",
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004825 (rsn.capabilities &
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004826 WPA_CAPABILITY_MFPR) ? 2 : 1,
4827 wpa_cipher_txt(
4828 sm->mgmt_group_cipher));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004829 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004830 return pos - buf;
4831 pos += ret;
4832 }
4833 }
4834
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004835 return pos - buf;
4836}
4837
4838
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004839int wpa_sm_pmf_enabled(struct wpa_sm *sm)
4840{
4841 struct wpa_ie_data rsn;
4842
4843 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
4844 return 0;
4845
4846 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
4847 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
4848 return 1;
4849
4850 return 0;
4851}
4852
4853
Hai Shalomfdcde762020-04-02 11:19:20 -07004854int wpa_sm_ext_key_id(struct wpa_sm *sm)
4855{
4856 return sm ? sm->ext_key_id : 0;
4857}
4858
4859
4860int wpa_sm_ext_key_id_active(struct wpa_sm *sm)
4861{
4862 return sm ? sm->use_ext_key_id : 0;
4863}
4864
4865
Hai Shalom74f70d42019-02-11 14:42:39 -08004866int wpa_sm_ocv_enabled(struct wpa_sm *sm)
4867{
4868 struct wpa_ie_data rsn;
4869
4870 if (!sm->ocv || !sm->ap_rsn_ie)
4871 return 0;
4872
4873 return wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len,
4874 &rsn) >= 0 &&
4875 (rsn.capabilities & WPA_CAPABILITY_OCVC);
4876}
4877
4878
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004879/**
4880 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
4881 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4882 * @wpa_ie: Pointer to buffer for WPA/RSN IE
4883 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
4884 * Returns: 0 on success, -1 on failure
4885 */
4886int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
4887 size_t *wpa_ie_len)
4888{
4889 int res;
4890
4891 if (sm == NULL)
4892 return -1;
4893
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08004894#ifdef CONFIG_TESTING_OPTIONS
4895 if (sm->test_assoc_ie) {
4896 wpa_printf(MSG_DEBUG,
4897 "TESTING: Replace association WPA/RSN IE");
4898 if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
4899 return -1;
4900 os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
4901 wpabuf_len(sm->test_assoc_ie));
4902 res = wpabuf_len(sm->test_assoc_ie);
4903 } else
4904#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004905 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
4906 if (res < 0)
4907 return -1;
4908 *wpa_ie_len = res;
4909
4910 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
4911 wpa_ie, *wpa_ie_len);
4912
4913 if (sm->assoc_wpa_ie == NULL) {
4914 /*
4915 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
4916 * the correct version of the IE even if PMKSA caching is
4917 * aborted (which would remove PMKID from IE generation).
4918 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004919 sm->assoc_wpa_ie = os_memdup(wpa_ie, *wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004920 if (sm->assoc_wpa_ie == NULL)
4921 return -1;
4922
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004923 sm->assoc_wpa_ie_len = *wpa_ie_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004924 } else {
4925 wpa_hexdump(MSG_DEBUG,
4926 "WPA: Leave previously set WPA IE default",
4927 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004928 }
4929
4930 return 0;
4931}
4932
4933
4934/**
4935 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
4936 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4937 * @ie: Pointer to IE data (starting from id)
4938 * @len: IE length
4939 * Returns: 0 on success, -1 on failure
4940 *
4941 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
4942 * Request frame. The IE will be used to override the default value generated
4943 * with wpa_sm_set_assoc_wpa_ie_default().
4944 */
4945int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
4946{
4947 if (sm == NULL)
4948 return -1;
4949
4950 os_free(sm->assoc_wpa_ie);
4951 if (ie == NULL || len == 0) {
4952 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4953 "WPA: clearing own WPA/RSN IE");
4954 sm->assoc_wpa_ie = NULL;
4955 sm->assoc_wpa_ie_len = 0;
4956 } else {
4957 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004958 sm->assoc_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004959 if (sm->assoc_wpa_ie == NULL)
4960 return -1;
4961
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004962 sm->assoc_wpa_ie_len = len;
4963 }
4964
4965 return 0;
4966}
4967
4968
4969/**
Hai Shalomc3565922019-10-28 11:58:20 -07004970 * wpa_sm_set_assoc_rsnxe_default - Generate own RSNXE from configuration
4971 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4972 * @rsnxe: Pointer to buffer for RSNXE
4973 * @rsnxe_len: Pointer to the length of the rsne buffer
4974 * Returns: 0 on success, -1 on failure
4975 */
4976int wpa_sm_set_assoc_rsnxe_default(struct wpa_sm *sm, u8 *rsnxe,
4977 size_t *rsnxe_len)
4978{
4979 int res;
4980
4981 if (!sm)
4982 return -1;
4983
4984 res = wpa_gen_rsnxe(sm, rsnxe, *rsnxe_len);
4985 if (res < 0)
4986 return -1;
4987 *rsnxe_len = res;
4988
4989 wpa_hexdump(MSG_DEBUG, "RSN: Set own RSNXE default", rsnxe, *rsnxe_len);
4990
4991 if (sm->assoc_rsnxe) {
4992 wpa_hexdump(MSG_DEBUG,
4993 "RSN: Leave previously set RSNXE default",
4994 sm->assoc_rsnxe, sm->assoc_rsnxe_len);
4995 } else if (*rsnxe_len > 0) {
4996 /*
4997 * Make a copy of the RSNXE so that 4-Way Handshake gets the
4998 * correct version of the IE even if it gets changed.
4999 */
5000 sm->assoc_rsnxe = os_memdup(rsnxe, *rsnxe_len);
5001 if (!sm->assoc_rsnxe)
5002 return -1;
5003
5004 sm->assoc_rsnxe_len = *rsnxe_len;
5005 }
5006
5007 return 0;
5008}
5009
5010
5011/**
5012 * wpa_sm_set_assoc_rsnxe - Set own RSNXE from (Re)AssocReq
5013 * @sm: Pointer to WPA state machine data from wpa_sm_init()
5014 * @ie: Pointer to IE data (starting from id)
5015 * @len: IE length
5016 * Returns: 0 on success, -1 on failure
5017 *
5018 * Inform WPA state machine about the RSNXE used in (Re)Association Request
5019 * frame. The IE will be used to override the default value generated
5020 * with wpa_sm_set_assoc_rsnxe_default().
5021 */
5022int wpa_sm_set_assoc_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len)
5023{
5024 if (!sm)
5025 return -1;
5026
5027 os_free(sm->assoc_rsnxe);
5028 if (!ie || len == 0) {
5029 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
5030 "RSN: clearing own RSNXE");
5031 sm->assoc_rsnxe = NULL;
5032 sm->assoc_rsnxe_len = 0;
5033 } else {
5034 wpa_hexdump(MSG_DEBUG, "RSN: set own RSNXE", ie, len);
5035 sm->assoc_rsnxe = os_memdup(ie, len);
5036 if (!sm->assoc_rsnxe)
5037 return -1;
5038
5039 sm->assoc_rsnxe_len = len;
5040 }
5041
Sunil Ravi7f769292024-07-23 22:21:32 +00005042 if (sm->ssid_protection &&
5043 !ieee802_11_rsnx_capab(sm->assoc_rsnxe,
5044 WLAN_RSNX_CAPAB_SSID_PROTECTION)) {
5045 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
5046 "RSN: Disabling SSID protection based on own RSNXE update");
5047 sm->ssid_protection = 0;
5048 }
5049
Hai Shalomc3565922019-10-28 11:58:20 -07005050 return 0;
5051}
5052
5053
5054/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005055 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
5056 * @sm: Pointer to WPA state machine data from wpa_sm_init()
5057 * @ie: Pointer to IE data (starting from id)
5058 * @len: IE length
5059 * Returns: 0 on success, -1 on failure
5060 *
5061 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
5062 * frame.
5063 */
5064int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
5065{
5066 if (sm == NULL)
5067 return -1;
5068
5069 os_free(sm->ap_wpa_ie);
5070 if (ie == NULL || len == 0) {
5071 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
5072 "WPA: clearing AP WPA IE");
5073 sm->ap_wpa_ie = NULL;
5074 sm->ap_wpa_ie_len = 0;
5075 } else {
5076 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005077 sm->ap_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005078 if (sm->ap_wpa_ie == NULL)
5079 return -1;
5080
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005081 sm->ap_wpa_ie_len = len;
5082 }
5083
5084 return 0;
5085}
5086
5087
5088/**
5089 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
5090 * @sm: Pointer to WPA state machine data from wpa_sm_init()
5091 * @ie: Pointer to IE data (starting from id)
5092 * @len: IE length
5093 * Returns: 0 on success, -1 on failure
5094 *
5095 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
5096 * frame.
5097 */
5098int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
5099{
5100 if (sm == NULL)
5101 return -1;
5102
5103 os_free(sm->ap_rsn_ie);
5104 if (ie == NULL || len == 0) {
5105 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
5106 "WPA: clearing AP RSN IE");
5107 sm->ap_rsn_ie = NULL;
5108 sm->ap_rsn_ie_len = 0;
5109 } else {
5110 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
Sunil Ravi7f769292024-07-23 22:21:32 +00005111 if (ie[0] == WLAN_EID_VENDOR_SPECIFIC && len > 2 + 4) {
5112 sm->ap_rsn_ie = os_malloc(len - 4);
5113 if (!sm->ap_rsn_ie)
5114 return -1;
5115 sm->ap_rsn_ie[0] = WLAN_EID_RSN;
5116 sm->ap_rsn_ie[1] = len - 2 - 4;
5117 os_memcpy(&sm->ap_rsn_ie[2], ie + 2 + 4, len - 2 - 4);
5118 sm->ap_rsn_ie_len = len - 4;
5119 wpa_hexdump(MSG_DEBUG,
5120 "RSN: Converted RSNE override to RSNE",
5121 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
5122 } else {
5123 sm->ap_rsn_ie = os_memdup(ie, len);
5124 if (sm->ap_rsn_ie == NULL)
5125 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005126
Sunil Ravi7f769292024-07-23 22:21:32 +00005127 sm->ap_rsn_ie_len = len;
5128 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005129 }
5130
5131 return 0;
5132}
5133
5134
5135/**
Hai Shalomc3565922019-10-28 11:58:20 -07005136 * wpa_sm_set_ap_rsnxe - Set AP RSNXE from Beacon/ProbeResp
5137 * @sm: Pointer to WPA state machine data from wpa_sm_init()
5138 * @ie: Pointer to IE data (starting from id)
5139 * @len: IE length
5140 * Returns: 0 on success, -1 on failure
5141 *
5142 * Inform WPA state machine about the RSNXE used in Beacon / Probe Response
5143 * frame.
5144 */
5145int wpa_sm_set_ap_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len)
5146{
5147 if (!sm)
5148 return -1;
5149
5150 os_free(sm->ap_rsnxe);
5151 if (!ie || len == 0) {
5152 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: clearing AP RSNXE");
5153 sm->ap_rsnxe = NULL;
5154 sm->ap_rsnxe_len = 0;
5155 } else {
5156 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSNXE", ie, len);
Sunil Ravi7f769292024-07-23 22:21:32 +00005157 if (ie[0] == WLAN_EID_VENDOR_SPECIFIC && len > 2 + 4) {
5158 sm->ap_rsnxe = os_malloc(len - 4);
5159 if (!sm->ap_rsnxe)
5160 return -1;
5161 sm->ap_rsnxe[0] = WLAN_EID_RSNX;
5162 sm->ap_rsnxe[1] = len - 2 - 4;
5163 os_memcpy(&sm->ap_rsnxe[2], ie + 2 + 4, len - 2 - 4);
5164 sm->ap_rsnxe_len = len - 4;
5165 wpa_hexdump(MSG_DEBUG,
5166 "RSN: Converted RSNXE override to RSNXE",
5167 sm->ap_rsnxe, sm->ap_rsnxe_len);
5168 } else {
5169 sm->ap_rsnxe = os_memdup(ie, len);
5170 if (!sm->ap_rsnxe)
5171 return -1;
Hai Shalomc3565922019-10-28 11:58:20 -07005172
Sunil Ravi7f769292024-07-23 22:21:32 +00005173 sm->ap_rsnxe_len = len;
5174 }
Hai Shalomc3565922019-10-28 11:58:20 -07005175 }
5176
5177 return 0;
5178}
5179
5180
5181/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005182 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
5183 * @sm: Pointer to WPA state machine data from wpa_sm_init()
5184 * @data: Pointer to data area for parsing results
5185 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
5186 *
5187 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
5188 * parsed data into data.
5189 */
5190int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
5191{
5192 if (sm == NULL)
5193 return -1;
5194
5195 if (sm->assoc_wpa_ie == NULL) {
5196 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
5197 "WPA: No WPA/RSN IE available from association info");
5198 return -1;
5199 }
5200 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
5201 return -2;
5202 return 0;
5203}
5204
5205
5206int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
5207{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005208 return pmksa_cache_list(sm->pmksa, buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005209}
5210
5211
Dmitry Shmidt29333592017-01-09 12:27:11 -08005212struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_head(struct wpa_sm *sm)
5213{
5214 return pmksa_cache_head(sm->pmksa);
5215}
5216
5217
5218struct rsn_pmksa_cache_entry *
5219wpa_sm_pmksa_cache_add_entry(struct wpa_sm *sm,
5220 struct rsn_pmksa_cache_entry * entry)
5221{
5222 return pmksa_cache_add_entry(sm->pmksa, entry);
5223}
5224
5225
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005226void wpa_sm_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
5227 const u8 *pmkid, const u8 *bssid,
5228 const u8 *fils_cache_id)
5229{
5230 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
5231 bssid, sm->own_addr, sm->network_ctx,
5232 sm->key_mgmt, fils_cache_id);
5233}
5234
5235
Sunil Ravi77d572f2023-01-17 23:58:31 +00005236int wpa_sm_pmksa_exists(struct wpa_sm *sm, const u8 *bssid, const u8 *own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005237 const void *network_ctx)
5238{
Sunil Ravi77d572f2023-01-17 23:58:31 +00005239 return pmksa_cache_get(sm->pmksa, bssid, own_addr, NULL, network_ctx,
5240 0) != NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005241}
5242
5243
Hai Shalom60840252021-02-19 19:02:11 -08005244struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_get(struct wpa_sm *sm,
5245 const u8 *aa,
5246 const u8 *pmkid,
5247 const void *network_ctx,
5248 int akmp)
5249{
Sunil Ravi77d572f2023-01-17 23:58:31 +00005250 return pmksa_cache_get(sm->pmksa, aa, sm->own_addr, pmkid, network_ctx,
5251 akmp);
5252}
5253
5254
5255void wpa_sm_pmksa_cache_remove(struct wpa_sm *sm,
5256 struct rsn_pmksa_cache_entry *entry)
5257{
5258 if (sm && sm->pmksa)
5259 pmksa_cache_remove(sm->pmksa, entry);
Hai Shalom60840252021-02-19 19:02:11 -08005260}
5261
5262
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005263void wpa_sm_drop_sa(struct wpa_sm *sm)
5264{
5265 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
Sunil Ravi77d572f2023-01-17 23:58:31 +00005266 wpa_sm_clear_ptk(sm);
Roshan Pius3a1667e2018-07-03 15:17:14 -07005267 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005268 os_memset(sm->pmk, 0, sizeof(sm->pmk));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005269#ifdef CONFIG_IEEE80211R
5270 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
Roshan Pius3a1667e2018-07-03 15:17:14 -07005271 sm->xxkey_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005272 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
Roshan Pius3a1667e2018-07-03 15:17:14 -07005273 sm->pmk_r0_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005274 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
Roshan Pius3a1667e2018-07-03 15:17:14 -07005275 sm->pmk_r1_len = 0;
Hai Shalom60840252021-02-19 19:02:11 -08005276#ifdef CONFIG_PASN
5277 os_free(sm->pasn_r1kh);
5278 sm->pasn_r1kh = NULL;
5279 sm->n_pasn_r1kh = 0;
5280#endif /* CONFIG_PASN */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005281#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005282}
5283
5284
Sunil Ravi77d572f2023-01-17 23:58:31 +00005285#ifdef CONFIG_IEEE80211R
5286bool wpa_sm_has_ft_keys(struct wpa_sm *sm, const u8 *md)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005287{
Sunil Ravi77d572f2023-01-17 23:58:31 +00005288 if (!sm)
5289 return false;
5290 if (!wpa_key_mgmt_ft(sm->key_mgmt) ||
5291 os_memcmp(md, sm->key_mobility_domain,
5292 MOBILITY_DOMAIN_ID_LEN) != 0) {
5293 /* Do not allow FT protocol to be used even if we were to have
5294 * an PTK since the mobility domain has changed. */
5295 return false;
5296 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005297 return sm->ptk_set;
5298}
Sunil Ravi77d572f2023-01-17 23:58:31 +00005299#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005300
5301
Hai Shalomfdcde762020-04-02 11:19:20 -07005302int wpa_sm_has_ptk_installed(struct wpa_sm *sm)
5303{
5304 if (!sm)
5305 return 0;
Sunil8cd6f4d2022-06-28 18:40:46 +00005306 return sm->tk_set || sm->ptk.installed;
Hai Shalomfdcde762020-04-02 11:19:20 -07005307}
5308
5309
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005310void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
5311{
5312 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
5313}
5314
5315
5316void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
5317{
Hai Shalomc1a21442022-02-04 13:43:00 -08005318 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0, false);
5319}
5320
5321
5322void wpa_sm_external_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
5323{
5324 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0, true);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005325}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005326
Andy Kuoaba17c12022-04-14 16:05:31 +08005327#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Mir Ali677e7482020-11-12 19:49:02 +05305328void wpa_sm_install_pmk(struct wpa_sm *sm)
5329{
5330 /* In case the driver wants to handle re-assocs, pass it down the PMK. */
Sunil Ravi77d572f2023-01-17 23:58:31 +00005331 if (wpa_sm_set_key(sm, -1, wpa_cipher_to_alg(sm->pairwise_cipher), NULL, 0, 0, NULL, 0,
Mir Ali677e7482020-11-12 19:49:02 +05305332 (u8*)sm->pmk, sm->pmk_len, KEY_FLAG_PMK) < 0) {
5333 wpa_hexdump(MSG_DEBUG, "PSK: Install PMK to the driver for driver reassociations",
5334 (u8*)sm->pmk, sm->pmk_len);
5335 /* No harm if the driver doesn't support. */
5336 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
5337 "WPA: Failed to set PMK to the driver");
5338 }
5339}
Mir Alieaaf04e2021-06-07 12:17:29 +05305340
5341void wpa_sm_notify_brcm_ft_reassoc(struct wpa_sm *sm, const u8 *bssid)
5342{
5343 u8 buf[256];
5344 struct wpa_supplicant *wpa_s = sm->ctx->ctx;
5345
5346 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
5347 "WPA: BRCM FT Reassociation event - clear replay counter");
5348 os_memcpy(sm->bssid, bssid, ETH_ALEN);
5349 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
5350 sm->rx_replay_counter_set = 0;
5351
5352 if (wpa_drv_driver_cmd(wpa_s, "GET_FTKEY", (char *)buf, sizeof(buf)) < 0) {
5353 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
5354 "WPA: Failed to get FT KEY information");
5355 wpa_supplicant_deauthenticate(
5356 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
5357
5358 } else {
5359 /* update kck and kek */
5360 os_memcpy(sm->ptk.kck, buf, 16);
5361 os_memcpy(sm->ptk.kek, buf + 16, 16);
5362 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
5363 "WPA: Updated KCK and KEK after FT reassoc");
5364 }
5365}
Andy Kuoaba17c12022-04-14 16:05:31 +08005366#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
5367
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005368
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005369#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005370int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
5371{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005372 u16 keyinfo;
5373 u8 keylen; /* plaintext key len */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005374 u8 *key_rsc;
5375
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005376 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07005377 struct wpa_gtk_data gd;
5378
5379 os_memset(&gd, 0, sizeof(gd));
5380 keylen = wpa_cipher_key_len(sm->group_cipher);
5381 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
5382 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
5383 if (gd.alg == WPA_ALG_NONE) {
5384 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
5385 return -1;
5386 }
5387
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005388 key_rsc = buf + 5;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005389 keyinfo = WPA_GET_LE16(buf + 2);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005390 gd.gtk_len = keylen;
5391 if (gd.gtk_len != buf[4]) {
5392 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
5393 gd.gtk_len, buf[4]);
5394 return -1;
5395 }
5396 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
5397 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
5398 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
5399
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005400 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005401
5402 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
5403 gd.gtk, gd.gtk_len);
Jouni Malinen58c0e962017-10-01 12:12:24 +03005404 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07005405 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005406 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
5407 "WNM mode");
5408 return -1;
5409 }
Hai Shalom81f62d82019-07-22 12:10:00 -07005410 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005411 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02005412 const struct wpa_igtk_kde *igtk;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07005413
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02005414 igtk = (const struct wpa_igtk_kde *) (buf + 2);
Jouni Malinen58c0e962017-10-01 12:12:24 +03005415 if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005416 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07005417 } else if (subelem_id == WNM_SLEEP_SUBELEM_BIGTK) {
5418 const struct wpa_bigtk_kde *bigtk;
5419
5420 bigtk = (const struct wpa_bigtk_kde *) (buf + 2);
5421 if (sm->beacon_prot &&
5422 wpa_supplicant_install_bigtk(sm, bigtk, 1) < 0)
5423 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005424 } else {
5425 wpa_printf(MSG_DEBUG, "Unknown element id");
5426 return -1;
5427 }
5428
5429 return 0;
5430}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005431#endif /* CONFIG_WNM */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005432
5433
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005434#ifdef CONFIG_P2P
5435
5436int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
5437{
5438 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
5439 return -1;
5440 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
5441 return 0;
5442}
5443
5444#endif /* CONFIG_P2P */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005445
5446
5447void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
5448{
5449 if (rx_replay_counter == NULL)
5450 return;
5451
5452 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
5453 WPA_REPLAY_COUNTER_LEN);
5454 sm->rx_replay_counter_set = 1;
5455 wpa_printf(MSG_DEBUG, "Updated key replay counter");
5456}
5457
5458
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005459void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
5460 const u8 *ptk_kck, size_t ptk_kck_len,
5461 const u8 *ptk_kek, size_t ptk_kek_len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005462{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005463 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
5464 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
5465 sm->ptk.kck_len = ptk_kck_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005466 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
5467 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005468 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
5469 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
5470 sm->ptk.kek_len = ptk_kek_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005471 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
5472 }
5473 sm->ptk_set = 1;
5474}
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08005475
5476
5477#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005478
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08005479void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
5480{
5481 wpabuf_free(sm->test_assoc_ie);
5482 sm->test_assoc_ie = buf;
5483}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005484
5485
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005486void wpa_sm_set_test_eapol_m2_elems(struct wpa_sm *sm, struct wpabuf *buf)
5487{
5488 wpabuf_free(sm->test_eapol_m2_elems);
5489 sm->test_eapol_m2_elems = buf;
5490}
5491
5492
5493void wpa_sm_set_test_eapol_m4_elems(struct wpa_sm *sm, struct wpabuf *buf)
5494{
5495 wpabuf_free(sm->test_eapol_m4_elems);
5496 sm->test_eapol_m4_elems = buf;
5497}
5498
5499
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005500const u8 * wpa_sm_get_anonce(struct wpa_sm *sm)
5501{
5502 return sm->anonce;
5503}
5504
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08005505#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005506
5507
Roshan Pius3a1667e2018-07-03 15:17:14 -07005508unsigned int wpa_sm_get_key_mgmt(struct wpa_sm *sm)
5509{
5510 return sm->key_mgmt;
5511}
5512
5513
Sunil Ravi77d572f2023-01-17 23:58:31 +00005514const u8 * wpa_sm_get_auth_addr(struct wpa_sm *sm)
5515{
5516 return sm->mlo.valid_links ? sm->mlo.ap_mld_addr : sm->bssid;
5517}
5518
5519
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005520#ifdef CONFIG_FILS
5521
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005522struct wpabuf * fils_build_auth(struct wpa_sm *sm, int dh_group, const u8 *md)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005523{
5524 struct wpabuf *buf = NULL;
5525 struct wpabuf *erp_msg;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005526 struct wpabuf *pub = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005527
5528 erp_msg = eapol_sm_build_erp_reauth_start(sm->eapol);
5529 if (!erp_msg && !sm->cur_pmksa) {
5530 wpa_printf(MSG_DEBUG,
5531 "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS");
5532 goto fail;
5533 }
5534
5535 wpa_printf(MSG_DEBUG, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)",
5536 erp_msg != NULL, sm->cur_pmksa != NULL);
5537
5538 sm->fils_completed = 0;
5539
5540 if (!sm->assoc_wpa_ie) {
5541 wpa_printf(MSG_INFO, "FILS: No own RSN IE set for FILS");
5542 goto fail;
5543 }
5544
5545 if (random_get_bytes(sm->fils_nonce, FILS_NONCE_LEN) < 0 ||
5546 random_get_bytes(sm->fils_session, FILS_SESSION_LEN) < 0)
5547 goto fail;
5548
5549 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Nonce",
5550 sm->fils_nonce, FILS_NONCE_LEN);
5551 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Session",
5552 sm->fils_session, FILS_SESSION_LEN);
5553
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005554#ifdef CONFIG_FILS_SK_PFS
5555 sm->fils_dh_group = dh_group;
5556 if (dh_group) {
5557 crypto_ecdh_deinit(sm->fils_ecdh);
5558 sm->fils_ecdh = crypto_ecdh_init(dh_group);
5559 if (!sm->fils_ecdh) {
5560 wpa_printf(MSG_INFO,
5561 "FILS: Could not initialize ECDH with group %d",
5562 dh_group);
5563 goto fail;
5564 }
5565 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
5566 if (!pub)
5567 goto fail;
5568 wpa_hexdump_buf(MSG_DEBUG, "FILS: Element (DH public key)",
5569 pub);
5570 sm->fils_dh_elem_len = wpabuf_len(pub);
5571 }
5572#endif /* CONFIG_FILS_SK_PFS */
5573
5574 buf = wpabuf_alloc(1000 + sm->assoc_wpa_ie_len +
5575 (pub ? wpabuf_len(pub) : 0));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005576 if (!buf)
5577 goto fail;
5578
5579 /* Fields following the Authentication algorithm number field */
5580
5581 /* Authentication Transaction seq# */
5582 wpabuf_put_le16(buf, 1);
5583
5584 /* Status Code */
5585 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
5586
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005587 /* TODO: FILS PK */
5588#ifdef CONFIG_FILS_SK_PFS
5589 if (dh_group) {
5590 /* Finite Cyclic Group */
5591 wpabuf_put_le16(buf, dh_group);
5592 /* Element */
5593 wpabuf_put_buf(buf, pub);
5594 }
5595#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005596
5597 /* RSNE */
5598 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in FILS Authentication frame",
5599 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
5600 wpabuf_put_data(buf, sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
5601
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005602 if (md) {
5603 /* MDE when using FILS for FT initial association */
5604 struct rsn_mdie *mdie;
5605
5606 wpabuf_put_u8(buf, WLAN_EID_MOBILITY_DOMAIN);
5607 wpabuf_put_u8(buf, sizeof(*mdie));
5608 mdie = wpabuf_put(buf, sizeof(*mdie));
5609 os_memcpy(mdie->mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
5610 mdie->ft_capab = 0;
5611 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005612
5613 /* FILS Nonce */
5614 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
5615 wpabuf_put_u8(buf, 1 + FILS_NONCE_LEN); /* Length */
5616 /* Element ID Extension */
5617 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_NONCE);
5618 wpabuf_put_data(buf, sm->fils_nonce, FILS_NONCE_LEN);
5619
5620 /* FILS Session */
5621 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
5622 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
5623 /* Element ID Extension */
5624 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
5625 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
5626
Hai Shalomfdcde762020-04-02 11:19:20 -07005627 /* Wrapped Data */
Paul Stewart092955c2017-02-06 09:13:09 -08005628 sm->fils_erp_pmkid_set = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005629 if (erp_msg) {
5630 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
5631 wpabuf_put_u8(buf, 1 + wpabuf_len(erp_msg)); /* Length */
5632 /* Element ID Extension */
Hai Shalomfdcde762020-04-02 11:19:20 -07005633 wpabuf_put_u8(buf, WLAN_EID_EXT_WRAPPED_DATA);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005634 wpabuf_put_buf(buf, erp_msg);
Paul Stewart092955c2017-02-06 09:13:09 -08005635 /* Calculate pending PMKID here so that we do not need to
5636 * maintain a copy of the EAP-Initiate/Reauth message. */
5637 if (fils_pmkid_erp(sm->key_mgmt, wpabuf_head(erp_msg),
5638 wpabuf_len(erp_msg),
5639 sm->fils_erp_pmkid) == 0)
5640 sm->fils_erp_pmkid_set = 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005641 }
5642
5643 wpa_hexdump_buf(MSG_DEBUG, "RSN: FILS fields for Authentication frame",
5644 buf);
5645
5646fail:
5647 wpabuf_free(erp_msg);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005648 wpabuf_free(pub);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005649 return buf;
5650}
5651
5652
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005653int fils_process_auth(struct wpa_sm *sm, const u8 *bssid, const u8 *data,
5654 size_t len)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005655{
5656 const u8 *pos, *end;
5657 struct ieee802_11_elems elems;
5658 struct wpa_ie_data rsn;
5659 int pmkid_match = 0;
5660 u8 ick[FILS_ICK_MAX_LEN];
5661 size_t ick_len;
5662 int res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005663 struct wpabuf *dh_ss = NULL;
5664 const u8 *g_sta = NULL;
5665 size_t g_sta_len = 0;
5666 const u8 *g_ap = NULL;
Hai Shalom60840252021-02-19 19:02:11 -08005667 size_t g_ap_len = 0, kdk_len;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005668 struct wpabuf *pub = NULL;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005669#ifdef CONFIG_IEEE80211R
5670 struct wpa_ft_ies parse;
5671
5672 os_memset(&parse, 0, sizeof(parse));
5673#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005674
5675 os_memcpy(sm->bssid, bssid, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005676
5677 wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
5678 data, len);
5679 pos = data;
5680 end = data + len;
5681
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005682 /* TODO: FILS PK */
5683#ifdef CONFIG_FILS_SK_PFS
5684 if (sm->fils_dh_group) {
5685 u16 group;
5686
5687 /* Using FILS PFS */
5688
5689 /* Finite Cyclic Group */
5690 if (end - pos < 2) {
5691 wpa_printf(MSG_DEBUG,
5692 "FILS: No room for Finite Cyclic Group");
5693 goto fail;
5694 }
5695 group = WPA_GET_LE16(pos);
5696 pos += 2;
5697 if (group != sm->fils_dh_group) {
5698 wpa_printf(MSG_DEBUG,
5699 "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)",
5700 group, sm->fils_dh_group);
5701 goto fail;
5702 }
5703
5704 /* Element */
5705 if ((size_t) (end - pos) < sm->fils_dh_elem_len) {
5706 wpa_printf(MSG_DEBUG, "FILS: No room for Element");
5707 goto fail;
5708 }
5709
5710 if (!sm->fils_ecdh) {
5711 wpa_printf(MSG_DEBUG, "FILS: No ECDH state available");
5712 goto fail;
5713 }
5714 dh_ss = crypto_ecdh_set_peerkey(sm->fils_ecdh, 1, pos,
5715 sm->fils_dh_elem_len);
5716 if (!dh_ss) {
5717 wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
5718 goto fail;
5719 }
5720 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", dh_ss);
5721 g_ap = pos;
5722 g_ap_len = sm->fils_dh_elem_len;
5723 pos += sm->fils_dh_elem_len;
5724 }
5725#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005726
5727 wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
5728 if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
5729 wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005730 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005731 }
5732
5733 /* RSNE */
5734 wpa_hexdump(MSG_DEBUG, "FILS: RSN element", elems.rsn_ie,
5735 elems.rsn_ie_len);
5736 if (!elems.rsn_ie ||
5737 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
5738 &rsn) < 0) {
5739 wpa_printf(MSG_DEBUG, "FILS: No RSN element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005740 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005741 }
5742
5743 if (!elems.fils_nonce) {
5744 wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005745 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005746 }
5747 os_memcpy(sm->fils_anonce, elems.fils_nonce, FILS_NONCE_LEN);
5748 wpa_hexdump(MSG_DEBUG, "FILS: ANonce", sm->fils_anonce, FILS_NONCE_LEN);
5749
Roshan Pius3a1667e2018-07-03 15:17:14 -07005750#ifdef CONFIG_IEEE80211R
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005751 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005752 if (!elems.mdie || !elems.ftie) {
5753 wpa_printf(MSG_DEBUG, "FILS+FT: No MDE or FTE");
5754 goto fail;
5755 }
5756
Roshan Pius3a1667e2018-07-03 15:17:14 -07005757 if (wpa_ft_parse_ies(pos, end - pos, &parse,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005758 sm->key_mgmt, false) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005759 wpa_printf(MSG_DEBUG, "FILS+FT: Failed to parse IEs");
5760 goto fail;
5761 }
5762
5763 if (!parse.r0kh_id) {
5764 wpa_printf(MSG_DEBUG,
5765 "FILS+FT: No R0KH-ID subelem in FTE");
5766 goto fail;
5767 }
5768 os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
5769 sm->r0kh_id_len = parse.r0kh_id_len;
5770 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
5771 sm->r0kh_id, sm->r0kh_id_len);
5772
5773 if (!parse.r1kh_id) {
5774 wpa_printf(MSG_DEBUG,
5775 "FILS+FT: No R1KH-ID subelem in FTE");
5776 goto fail;
5777 }
5778 os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
5779 wpa_hexdump(MSG_DEBUG, "FILS+FT: R1KH-ID",
5780 sm->r1kh_id, FT_R1KH_ID_LEN);
5781
5782 /* TODO: Check MDE and FTE payload */
5783
5784 wpabuf_free(sm->fils_ft_ies);
5785 sm->fils_ft_ies = wpabuf_alloc(2 + elems.mdie_len +
5786 2 + elems.ftie_len);
5787 if (!sm->fils_ft_ies)
5788 goto fail;
5789 wpabuf_put_data(sm->fils_ft_ies, elems.mdie - 2,
5790 2 + elems.mdie_len);
5791 wpabuf_put_data(sm->fils_ft_ies, elems.ftie - 2,
5792 2 + elems.ftie_len);
5793 } else {
5794 wpabuf_free(sm->fils_ft_ies);
5795 sm->fils_ft_ies = NULL;
5796 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07005797#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005798
5799 /* PMKID List */
5800 if (rsn.pmkid && rsn.num_pmkid > 0) {
5801 wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
5802 rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
5803
5804 if (rsn.num_pmkid != 1) {
5805 wpa_printf(MSG_DEBUG, "FILS: Invalid PMKID selection");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005806 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005807 }
5808 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", rsn.pmkid, PMKID_LEN);
5809 if (os_memcmp(sm->cur_pmksa->pmkid, rsn.pmkid, PMKID_LEN) != 0)
5810 {
5811 wpa_printf(MSG_DEBUG, "FILS: PMKID mismatch");
5812 wpa_hexdump(MSG_DEBUG, "FILS: Expected PMKID",
5813 sm->cur_pmksa->pmkid, PMKID_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005814 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005815 }
5816 wpa_printf(MSG_DEBUG,
5817 "FILS: Matching PMKID - continue using PMKSA caching");
5818 pmkid_match = 1;
5819 }
5820 if (!pmkid_match && sm->cur_pmksa) {
5821 wpa_printf(MSG_DEBUG,
5822 "FILS: No PMKID match - cannot use cached PMKSA entry");
5823 sm->cur_pmksa = NULL;
5824 }
5825
5826 /* FILS Session */
5827 if (!elems.fils_session) {
5828 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005829 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005830 }
5831 wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
5832 FILS_SESSION_LEN);
5833 if (os_memcmp(sm->fils_session, elems.fils_session, FILS_SESSION_LEN)
5834 != 0) {
5835 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
5836 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
5837 sm->fils_session, FILS_SESSION_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005838 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005839 }
5840
Hai Shalomfdcde762020-04-02 11:19:20 -07005841 /* Wrapped Data */
5842 if (!sm->cur_pmksa && elems.wrapped_data) {
Paul Stewart092955c2017-02-06 09:13:09 -08005843 u8 rmsk[ERP_MAX_KEY_LEN];
5844 size_t rmsk_len;
5845
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005846 wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
Hai Shalomfdcde762020-04-02 11:19:20 -07005847 elems.wrapped_data,
5848 elems.wrapped_data_len);
5849 eapol_sm_process_erp_finish(sm->eapol, elems.wrapped_data,
5850 elems.wrapped_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005851 if (eapol_sm_failed(sm->eapol))
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005852 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005853
Paul Stewart092955c2017-02-06 09:13:09 -08005854 rmsk_len = ERP_MAX_KEY_LEN;
5855 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
5856 if (res == PMK_LEN) {
5857 rmsk_len = PMK_LEN;
5858 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
5859 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005860 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005861 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005862
Paul Stewart092955c2017-02-06 09:13:09 -08005863 res = fils_rmsk_to_pmk(sm->key_mgmt, rmsk, rmsk_len,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005864 sm->fils_nonce, sm->fils_anonce,
5865 dh_ss ? wpabuf_head(dh_ss) : NULL,
5866 dh_ss ? wpabuf_len(dh_ss) : 0,
Paul Stewart092955c2017-02-06 09:13:09 -08005867 sm->pmk, &sm->pmk_len);
Hai Shalom81f62d82019-07-22 12:10:00 -07005868 forced_memzero(rmsk, sizeof(rmsk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005869
5870 /* Don't use DHss in PTK derivation if PMKSA caching is not
5871 * used. */
5872 wpabuf_clear_free(dh_ss);
5873 dh_ss = NULL;
5874
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005875 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005876 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08005877
5878 if (!sm->fils_erp_pmkid_set) {
5879 wpa_printf(MSG_DEBUG, "FILS: PMKID not available");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005880 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08005881 }
5882 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", sm->fils_erp_pmkid,
5883 PMKID_LEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005884 wpa_printf(MSG_DEBUG, "FILS: ERP processing succeeded - add PMKSA cache entry for the result");
Paul Stewart092955c2017-02-06 09:13:09 -08005885 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
5886 sm->fils_erp_pmkid, NULL, 0,
5887 sm->bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005888 sm->network_ctx, sm->key_mgmt,
5889 NULL);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005890 }
5891
5892 if (!sm->cur_pmksa) {
5893 wpa_printf(MSG_DEBUG,
5894 "FILS: No remaining options to continue FILS authentication");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005895 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005896 }
5897
Hai Shalom60840252021-02-19 19:02:11 -08005898 if (sm->force_kdk_derivation ||
Hai Shalomc1a21442022-02-04 13:43:00 -08005899 (sm->secure_ltf &&
5900 ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
Hai Shalom60840252021-02-19 19:02:11 -08005901 kdk_len = WPA_KDK_MAX_LEN;
5902 else
5903 kdk_len = 0;
5904
Sunil Ravi77d572f2023-01-17 23:58:31 +00005905 if (fils_pmk_to_ptk(sm->pmk, sm->pmk_len, sm->own_addr,
5906 wpa_sm_get_auth_addr(sm),
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005907 sm->fils_nonce, sm->fils_anonce,
5908 dh_ss ? wpabuf_head(dh_ss) : NULL,
5909 dh_ss ? wpabuf_len(dh_ss) : 0,
5910 &sm->ptk, ick, &ick_len,
5911 sm->key_mgmt, sm->pairwise_cipher,
Hai Shalom60840252021-02-19 19:02:11 -08005912 sm->fils_ft, &sm->fils_ft_len,
5913 kdk_len) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005914 wpa_printf(MSG_DEBUG, "FILS: Failed to derive PTK");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005915 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005916 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005917
Sunil Ravi89eba102022-09-13 21:04:37 -07005918#ifdef CONFIG_PASN
5919 if (sm->secure_ltf &&
5920 ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF) &&
5921 wpa_ltf_keyseed(&sm->ptk, sm->key_mgmt, sm->pairwise_cipher)) {
5922 wpa_printf(MSG_DEBUG, "FILS: Failed to derive LTF keyseed");
5923 goto fail;
5924 }
5925#endif /* CONFIG_PASN */
5926
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005927 wpabuf_clear_free(dh_ss);
5928 dh_ss = NULL;
5929
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005930 sm->ptk_set = 1;
5931 sm->tptk_set = 0;
5932 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
5933
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005934#ifdef CONFIG_FILS_SK_PFS
5935 if (sm->fils_dh_group) {
5936 if (!sm->fils_ecdh) {
5937 wpa_printf(MSG_INFO, "FILS: ECDH not initialized");
5938 goto fail;
5939 }
5940 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
5941 if (!pub)
5942 goto fail;
5943 wpa_hexdump_buf(MSG_DEBUG, "FILS: gSTA", pub);
5944 g_sta = wpabuf_head(pub);
5945 g_sta_len = wpabuf_len(pub);
5946 if (!g_ap) {
5947 wpa_printf(MSG_INFO, "FILS: gAP not available");
5948 goto fail;
5949 }
5950 wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len);
5951 }
5952#endif /* CONFIG_FILS_SK_PFS */
5953
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005954 res = fils_key_auth_sk(ick, ick_len, sm->fils_nonce,
5955 sm->fils_anonce, sm->own_addr, sm->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005956 g_sta, g_sta_len, g_ap, g_ap_len,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005957 sm->key_mgmt, sm->fils_key_auth_sta,
5958 sm->fils_key_auth_ap,
5959 &sm->fils_key_auth_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005960 wpabuf_free(pub);
Hai Shalom81f62d82019-07-22 12:10:00 -07005961 forced_memzero(ick, sizeof(ick));
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005962#ifdef CONFIG_IEEE80211R
5963 wpa_ft_parse_ies_free(&parse);
5964#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005965 return res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005966fail:
5967 wpabuf_free(pub);
5968 wpabuf_clear_free(dh_ss);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005969#ifdef CONFIG_IEEE80211R
5970 wpa_ft_parse_ies_free(&parse);
5971#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005972 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005973}
5974
5975
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005976#ifdef CONFIG_IEEE80211R
5977static int fils_ft_build_assoc_req_rsne(struct wpa_sm *sm, struct wpabuf *buf)
5978{
5979 struct rsn_ie_hdr *rsnie;
5980 u16 capab;
5981 u8 *pos;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005982 int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005983
5984 /* RSNIE[PMKR0Name/PMKR1Name] */
5985 rsnie = wpabuf_put(buf, sizeof(*rsnie));
5986 rsnie->elem_id = WLAN_EID_RSN;
5987 WPA_PUT_LE16(rsnie->version, RSN_VERSION);
5988
5989 /* Group Suite Selector */
5990 if (!wpa_cipher_valid_group(sm->group_cipher)) {
5991 wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
5992 sm->group_cipher);
5993 return -1;
5994 }
5995 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
5996 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
5997 sm->group_cipher));
5998
5999 /* Pairwise Suite Count */
6000 wpabuf_put_le16(buf, 1);
6001
6002 /* Pairwise Suite List */
6003 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
6004 wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
6005 sm->pairwise_cipher);
6006 return -1;
6007 }
6008 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
6009 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
6010 sm->pairwise_cipher));
6011
6012 /* Authenticated Key Management Suite Count */
6013 wpabuf_put_le16(buf, 1);
6014
6015 /* Authenticated Key Management Suite List */
6016 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
6017 if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256)
6018 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
6019 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384)
6020 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
6021 else {
6022 wpa_printf(MSG_WARNING,
6023 "FILS+FT: Invalid key management type (%d)",
6024 sm->key_mgmt);
6025 return -1;
6026 }
6027
6028 /* RSN Capabilities */
6029 capab = 0;
Hai Shalomc3565922019-10-28 11:58:20 -07006030 if (sm->mfp)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006031 capab |= WPA_CAPABILITY_MFPC;
Hai Shalomc3565922019-10-28 11:58:20 -07006032 if (sm->mfp == 2)
6033 capab |= WPA_CAPABILITY_MFPR;
Hai Shalom74f70d42019-02-11 14:42:39 -08006034 if (sm->ocv)
6035 capab |= WPA_CAPABILITY_OCVC;
Hai Shalomfdcde762020-04-02 11:19:20 -07006036 if (sm->ext_key_id)
6037 capab |= WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006038 wpabuf_put_le16(buf, capab);
6039
6040 /* PMKID Count */
6041 wpabuf_put_le16(buf, 1);
6042
6043 /* PMKID List [PMKR1Name] */
6044 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: XXKey (FILS-FT)",
6045 sm->fils_ft, sm->fils_ft_len);
6046 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: SSID", sm->ssid, sm->ssid_len);
6047 wpa_hexdump(MSG_DEBUG, "FILS+FT: MDID",
6048 sm->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
6049 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
6050 sm->r0kh_id, sm->r0kh_id_len);
6051 if (wpa_derive_pmk_r0(sm->fils_ft, sm->fils_ft_len, sm->ssid,
6052 sm->ssid_len, sm->mobility_domain,
6053 sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
Sunil Ravi77d572f2023-01-17 23:58:31 +00006054 sm->pmk_r0, sm->pmk_r0_name, sm->key_mgmt) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006055 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMK-R0");
6056 return -1;
6057 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00006058 if (wpa_key_mgmt_sae_ext_key(sm->key_mgmt))
6059 sm->pmk_r0_len = sm->fils_ft_len;
6060 else
6061 sm->pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006062 wpa_printf(MSG_DEBUG, "FILS+FT: R1KH-ID: " MACSTR,
6063 MAC2STR(sm->r1kh_id));
6064 pos = wpabuf_put(buf, WPA_PMK_NAME_LEN);
6065 if (wpa_derive_pmk_r1_name(sm->pmk_r0_name, sm->r1kh_id, sm->own_addr,
Sunil Ravi77d572f2023-01-17 23:58:31 +00006066 sm->pmk_r1_name, sm->fils_ft_len) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006067 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMKR1Name");
6068 return -1;
6069 }
Hai Shalom021b0b52019-04-10 11:17:58 -07006070 os_memcpy(pos, sm->pmk_r1_name, WPA_PMK_NAME_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006071
Sunil Ravi77d572f2023-01-17 23:58:31 +00006072 os_memcpy(sm->key_mobility_domain, sm->mobility_domain,
6073 MOBILITY_DOMAIN_ID_LEN);
6074
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006075 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
6076 /* Management Group Cipher Suite */
6077 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
6078 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
6079 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006080
6081 rsnie->len = ((u8 *) wpabuf_put(buf, 0) - (u8 *) rsnie) - 2;
6082 return 0;
6083}
6084#endif /* CONFIG_IEEE80211R */
6085
6086
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006087struct wpabuf * fils_build_assoc_req(struct wpa_sm *sm, const u8 **kek,
6088 size_t *kek_len, const u8 **snonce,
Paul Stewart092955c2017-02-06 09:13:09 -08006089 const u8 **anonce,
6090 const struct wpabuf **hlp,
6091 unsigned int num_hlp)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006092{
6093 struct wpabuf *buf;
Paul Stewart092955c2017-02-06 09:13:09 -08006094 size_t len;
6095 unsigned int i;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006096
Paul Stewart092955c2017-02-06 09:13:09 -08006097 len = 1000;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006098#ifdef CONFIG_IEEE80211R
6099 if (sm->fils_ft_ies)
6100 len += wpabuf_len(sm->fils_ft_ies);
6101 if (wpa_key_mgmt_ft(sm->key_mgmt))
6102 len += 256;
6103#endif /* CONFIG_IEEE80211R */
Paul Stewart092955c2017-02-06 09:13:09 -08006104 for (i = 0; hlp && i < num_hlp; i++)
6105 len += 10 + wpabuf_len(hlp[i]);
6106 buf = wpabuf_alloc(len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006107 if (!buf)
6108 return NULL;
6109
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006110#ifdef CONFIG_IEEE80211R
6111 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
6112 /* MDE and FTE when using FILS+FT */
6113 wpabuf_put_buf(buf, sm->fils_ft_ies);
6114 /* RSNE with PMKR1Name in PMKID field */
6115 if (fils_ft_build_assoc_req_rsne(sm, buf) < 0) {
6116 wpabuf_free(buf);
6117 return NULL;
6118 }
6119 }
6120#endif /* CONFIG_IEEE80211R */
6121
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006122 /* FILS Session */
6123 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
6124 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
6125 /* Element ID Extension */
6126 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
6127 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
6128
6129 /* Everything after FILS Session element gets encrypted in the driver
6130 * with KEK. The buffer returned from here is the plaintext version. */
6131
6132 /* TODO: FILS Public Key */
6133
6134 /* FILS Key Confirm */
6135 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
6136 wpabuf_put_u8(buf, 1 + sm->fils_key_auth_len); /* Length */
6137 /* Element ID Extension */
6138 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_KEY_CONFIRM);
6139 wpabuf_put_data(buf, sm->fils_key_auth_sta, sm->fils_key_auth_len);
6140
Paul Stewart092955c2017-02-06 09:13:09 -08006141 /* FILS HLP Container */
6142 for (i = 0; hlp && i < num_hlp; i++) {
6143 const u8 *pos = wpabuf_head(hlp[i]);
6144 size_t left = wpabuf_len(hlp[i]);
6145
6146 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
6147 if (left <= 254)
6148 len = 1 + left;
6149 else
6150 len = 255;
6151 wpabuf_put_u8(buf, len); /* Length */
6152 /* Element ID Extension */
6153 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_HLP_CONTAINER);
6154 /* Destination MAC Address, Source MAC Address, HLP Packet.
6155 * HLP Packet is in MSDU format (i.e., included the LLC/SNAP
6156 * header when LPD is used). */
6157 wpabuf_put_data(buf, pos, len - 1);
6158 pos += len - 1;
6159 left -= len - 1;
6160 while (left) {
6161 wpabuf_put_u8(buf, WLAN_EID_FRAGMENT);
6162 len = left > 255 ? 255 : left;
6163 wpabuf_put_u8(buf, len);
6164 wpabuf_put_data(buf, pos, len);
6165 pos += len;
6166 left -= len;
6167 }
6168 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006169
6170 /* TODO: FILS IP Address Assignment */
6171
Hai Shalom74f70d42019-02-11 14:42:39 -08006172#ifdef CONFIG_OCV
6173 if (wpa_sm_ocv_enabled(sm)) {
6174 struct wpa_channel_info ci;
6175 u8 *pos;
6176
6177 if (wpa_sm_channel_info(sm, &ci) != 0) {
6178 wpa_printf(MSG_WARNING,
6179 "FILS: Failed to get channel info for OCI element");
6180 wpabuf_free(buf);
6181 return NULL;
6182 }
Hai Shalom899fcc72020-10-19 14:38:18 -07006183#ifdef CONFIG_TESTING_OPTIONS
6184 if (sm->oci_freq_override_fils_assoc) {
6185 wpa_printf(MSG_INFO,
6186 "TEST: Override OCI KDE frequency %d -> %d MHz",
6187 ci.frequency,
6188 sm->oci_freq_override_fils_assoc);
6189 ci.frequency = sm->oci_freq_override_fils_assoc;
6190 }
6191#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -08006192
6193 pos = wpabuf_put(buf, OCV_OCI_EXTENDED_LEN);
6194 if (ocv_insert_extended_oci(&ci, pos) < 0) {
6195 wpabuf_free(buf);
6196 return NULL;
6197 }
6198 }
6199#endif /* CONFIG_OCV */
6200
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006201 wpa_hexdump_buf(MSG_DEBUG, "FILS: Association Request plaintext", buf);
6202
6203 *kek = sm->ptk.kek;
6204 *kek_len = sm->ptk.kek_len;
6205 wpa_hexdump_key(MSG_DEBUG, "FILS: KEK for AEAD", *kek, *kek_len);
6206 *snonce = sm->fils_nonce;
6207 wpa_hexdump(MSG_DEBUG, "FILS: SNonce for AEAD AAD",
6208 *snonce, FILS_NONCE_LEN);
6209 *anonce = sm->fils_anonce;
6210 wpa_hexdump(MSG_DEBUG, "FILS: ANonce for AEAD AAD",
6211 *anonce, FILS_NONCE_LEN);
6212
6213 return buf;
6214}
6215
6216
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08006217static void fils_process_hlp_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
6218{
6219 const u8 *pos, *end;
6220
6221 wpa_hexdump(MSG_MSGDUMP, "FILS: HLP response", resp, len);
6222 if (len < 2 * ETH_ALEN)
6223 return;
6224 pos = resp + 2 * ETH_ALEN;
6225 end = resp + len;
6226 if (end - pos >= 6 &&
6227 os_memcmp(pos, "\xaa\xaa\x03\x00\x00\x00", 6) == 0)
6228 pos += 6; /* Remove SNAP/LLC header */
6229 wpa_sm_fils_hlp_rx(sm, resp, resp + ETH_ALEN, pos, end - pos);
6230}
6231
6232
6233static void fils_process_hlp_container(struct wpa_sm *sm, const u8 *pos,
6234 size_t len)
6235{
6236 const u8 *end = pos + len;
6237 u8 *tmp, *tmp_pos;
6238
6239 /* Check if there are any FILS HLP Container elements */
6240 while (end - pos >= 2) {
6241 if (2 + pos[1] > end - pos)
6242 return;
6243 if (pos[0] == WLAN_EID_EXTENSION &&
6244 pos[1] >= 1 + 2 * ETH_ALEN &&
6245 pos[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER)
6246 break;
6247 pos += 2 + pos[1];
6248 }
6249 if (end - pos < 2)
6250 return; /* No FILS HLP Container elements */
6251
6252 tmp = os_malloc(end - pos);
6253 if (!tmp)
6254 return;
6255
6256 while (end - pos >= 2) {
6257 if (2 + pos[1] > end - pos ||
6258 pos[0] != WLAN_EID_EXTENSION ||
6259 pos[1] < 1 + 2 * ETH_ALEN ||
6260 pos[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER)
6261 break;
6262 tmp_pos = tmp;
6263 os_memcpy(tmp_pos, pos + 3, pos[1] - 1);
6264 tmp_pos += pos[1] - 1;
6265 pos += 2 + pos[1];
6266
6267 /* Add possible fragments */
6268 while (end - pos >= 2 && pos[0] == WLAN_EID_FRAGMENT &&
6269 2 + pos[1] <= end - pos) {
6270 os_memcpy(tmp_pos, pos + 2, pos[1]);
6271 tmp_pos += pos[1];
6272 pos += 2 + pos[1];
6273 }
6274
6275 fils_process_hlp_resp(sm, tmp, tmp_pos - tmp);
6276 }
6277
6278 os_free(tmp);
6279}
6280
6281
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006282int fils_process_assoc_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
6283{
6284 const struct ieee80211_mgmt *mgmt;
6285 const u8 *end, *ie_start;
6286 struct ieee802_11_elems elems;
6287 int keylen, rsclen;
6288 enum wpa_alg alg;
6289 struct wpa_gtk_data gd;
6290 int maxkeylen;
6291 struct wpa_eapol_ie_parse kde;
6292
6293 if (!sm || !sm->ptk_set) {
6294 wpa_printf(MSG_DEBUG, "FILS: No KEK available");
6295 return -1;
6296 }
6297
6298 if (!wpa_key_mgmt_fils(sm->key_mgmt)) {
6299 wpa_printf(MSG_DEBUG, "FILS: Not a FILS AKM");
6300 return -1;
6301 }
6302
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006303 if (sm->fils_completed) {
6304 wpa_printf(MSG_DEBUG,
6305 "FILS: Association has already been completed for this FILS authentication - ignore unexpected retransmission");
6306 return -1;
6307 }
6308
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006309 wpa_hexdump(MSG_DEBUG, "FILS: (Re)Association Response frame",
6310 resp, len);
6311
6312 mgmt = (const struct ieee80211_mgmt *) resp;
6313 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp))
6314 return -1;
6315
6316 end = resp + len;
6317 /* Same offset for Association Response and Reassociation Response */
6318 ie_start = mgmt->u.assoc_resp.variable;
6319
6320 if (ieee802_11_parse_elems(ie_start, end - ie_start, &elems, 1) ==
6321 ParseFailed) {
6322 wpa_printf(MSG_DEBUG,
6323 "FILS: Failed to parse decrypted elements");
6324 goto fail;
6325 }
6326
6327 if (!elems.fils_session) {
6328 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
6329 return -1;
6330 }
6331 if (os_memcmp(elems.fils_session, sm->fils_session,
6332 FILS_SESSION_LEN) != 0) {
6333 wpa_printf(MSG_DEBUG, "FILS: FILS Session mismatch");
6334 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
6335 elems.fils_session, FILS_SESSION_LEN);
6336 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
6337 sm->fils_session, FILS_SESSION_LEN);
6338 }
6339
Hai Shalom81f62d82019-07-22 12:10:00 -07006340 if (!elems.rsn_ie) {
6341 wpa_printf(MSG_DEBUG,
6342 "FILS: No RSNE in (Re)Association Response");
6343 /* As an interop workaround, allow this for now since IEEE Std
6344 * 802.11ai-2016 did not include all the needed changes to make
6345 * a FILS AP include RSNE in the frame. This workaround might
6346 * eventually be removed and replaced with rejection (goto fail)
6347 * to follow a strict interpretation of the standard. */
6348 } else if (wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
6349 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
6350 elems.rsn_ie - 2, elems.rsn_ie_len + 2)) {
6351 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
6352 "FILS: RSNE mismatch between Beacon/Probe Response and (Re)Association Response");
6353 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in Beacon/Probe Response",
6354 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
6355 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in (Re)Association Response",
6356 elems.rsn_ie, elems.rsn_ie_len);
6357 goto fail;
6358 }
6359
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006360 /* TODO: FILS Public Key */
6361
6362 if (!elems.fils_key_confirm) {
6363 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
6364 goto fail;
6365 }
6366 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
6367 wpa_printf(MSG_DEBUG,
6368 "FILS: Unexpected Key-Auth length %d (expected %d)",
6369 elems.fils_key_confirm_len,
6370 (int) sm->fils_key_auth_len);
6371 goto fail;
6372 }
6373 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_ap,
6374 sm->fils_key_auth_len) != 0) {
6375 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
6376 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
6377 elems.fils_key_confirm,
6378 elems.fils_key_confirm_len);
6379 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
6380 sm->fils_key_auth_ap, sm->fils_key_auth_len);
6381 goto fail;
6382 }
6383
Hai Shalom74f70d42019-02-11 14:42:39 -08006384#ifdef CONFIG_OCV
6385 if (wpa_sm_ocv_enabled(sm)) {
6386 struct wpa_channel_info ci;
6387
6388 if (wpa_sm_channel_info(sm, &ci) != 0) {
6389 wpa_printf(MSG_WARNING,
6390 "Failed to get channel info to validate received OCI in FILS (Re)Association Response frame");
6391 goto fail;
6392 }
6393
6394 if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci,
6395 channel_width_to_int(ci.chanwidth),
Hai Shalom899fcc72020-10-19 14:38:18 -07006396 ci.seg1_idx) != OCI_SUCCESS) {
6397 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
6398 "addr=" MACSTR " frame=fils-assoc error=%s",
6399 MAC2STR(sm->bssid), ocv_errorstr);
Hai Shalom74f70d42019-02-11 14:42:39 -08006400 goto fail;
6401 }
6402 }
6403#endif /* CONFIG_OCV */
6404
Hai Shalom021b0b52019-04-10 11:17:58 -07006405#ifdef CONFIG_IEEE80211R
6406 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
6407 struct wpa_ie_data rsn;
6408
6409 /* Check that PMKR1Name derived by the AP matches */
6410 if (!elems.rsn_ie ||
6411 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
6412 &rsn) < 0 ||
6413 !rsn.pmkid || rsn.num_pmkid != 1 ||
6414 os_memcmp(rsn.pmkid, sm->pmk_r1_name,
6415 WPA_PMK_NAME_LEN) != 0) {
6416 wpa_printf(MSG_DEBUG,
6417 "FILS+FT: No RSNE[PMKR1Name] match in AssocResp");
6418 goto fail;
6419 }
6420 }
6421#endif /* CONFIG_IEEE80211R */
6422
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006423 /* Key Delivery */
6424 if (!elems.key_delivery) {
6425 wpa_printf(MSG_DEBUG, "FILS: No Key Delivery element");
6426 goto fail;
6427 }
6428
6429 /* Parse GTK and set the key to the driver */
6430 os_memset(&gd, 0, sizeof(gd));
6431 if (wpa_supplicant_parse_ies(elems.key_delivery + WPA_KEY_RSC_LEN,
6432 elems.key_delivery_len - WPA_KEY_RSC_LEN,
6433 &kde) < 0) {
6434 wpa_printf(MSG_DEBUG, "FILS: Failed to parse KDEs");
6435 goto fail;
6436 }
6437 if (!kde.gtk) {
6438 wpa_printf(MSG_DEBUG, "FILS: No GTK KDE");
6439 goto fail;
6440 }
6441 maxkeylen = gd.gtk_len = kde.gtk_len - 2;
6442 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
6443 gd.gtk_len, maxkeylen,
6444 &gd.key_rsc_len, &gd.alg))
6445 goto fail;
6446
6447 wpa_hexdump_key(MSG_DEBUG, "FILS: Received GTK", kde.gtk, kde.gtk_len);
6448 gd.keyidx = kde.gtk[0] & 0x3;
6449 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
6450 !!(kde.gtk[0] & BIT(2)));
6451 if (kde.gtk_len - 2 > sizeof(gd.gtk)) {
6452 wpa_printf(MSG_DEBUG, "FILS: Too long GTK in GTK KDE (len=%lu)",
6453 (unsigned long) kde.gtk_len - 2);
6454 goto fail;
6455 }
6456 os_memcpy(gd.gtk, kde.gtk + 2, kde.gtk_len - 2);
6457
6458 wpa_printf(MSG_DEBUG, "FILS: Set GTK to driver");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006459 if (wpa_supplicant_install_gtk(sm, &gd, elems.key_delivery, 0) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006460 wpa_printf(MSG_DEBUG, "FILS: Failed to set GTK");
6461 goto fail;
6462 }
6463
6464 if (ieee80211w_set_keys(sm, &kde) < 0) {
6465 wpa_printf(MSG_DEBUG, "FILS: Failed to set IGTK");
6466 goto fail;
6467 }
6468
6469 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
6470 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006471 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
6472 wpa_printf(MSG_DEBUG, "FILS: TK length mismatch: %u != %lu",
6473 keylen, (long unsigned int) sm->ptk.tk_len);
6474 goto fail;
6475 }
Hai Shalomfdcde762020-04-02 11:19:20 -07006476
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006477 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
6478 wpa_hexdump_key(MSG_DEBUG, "FILS: Set TK to driver",
6479 sm->ptk.tk, keylen);
Sunil Ravi77d572f2023-01-17 23:58:31 +00006480 if (wpa_sm_set_key(sm, -1, alg, wpa_sm_get_auth_addr(sm), 0, 1,
6481 null_rsc, rsclen,
Hai Shalomfdcde762020-04-02 11:19:20 -07006482 sm->ptk.tk, keylen, KEY_FLAG_PAIRWISE_RX_TX) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006483 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Sunil Ravi77d572f2023-01-17 23:58:31 +00006484 "FILS: Failed to set PTK to the driver (alg=%d keylen=%d auth_addr="
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006485 MACSTR ")",
Sunil Ravi77d572f2023-01-17 23:58:31 +00006486 alg, keylen, MAC2STR(wpa_sm_get_auth_addr(sm)));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006487 goto fail;
6488 }
6489
Hai Shalom60840252021-02-19 19:02:11 -08006490 wpa_sm_store_ptk(sm, sm->bssid, sm->pairwise_cipher,
6491 sm->dot11RSNAConfigPMKLifetime, &sm->ptk);
6492
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006493 /* TODO: TK could be cleared after auth frame exchange now that driver
6494 * takes care of association frame encryption/decryption. */
6495 /* TK is not needed anymore in supplicant */
6496 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006497 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +02006498 sm->ptk.installed = 1;
Sunil8cd6f4d2022-06-28 18:40:46 +00006499 sm->tk_set = true;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006500
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08006501 /* FILS HLP Container */
6502 fils_process_hlp_container(sm, ie_start, end - ie_start);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006503
6504 /* TODO: FILS IP Address Assignment */
6505
6506 wpa_printf(MSG_DEBUG, "FILS: Auth+Assoc completed successfully");
6507 sm->fils_completed = 1;
Hai Shalom81f62d82019-07-22 12:10:00 -07006508 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006509
Hai Shalomfdcde762020-04-02 11:19:20 -07006510 if (kde.transition_disable)
6511 wpa_sm_transition_disable(sm, kde.transition_disable[0]);
6512
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006513 return 0;
6514fail:
Hai Shalom81f62d82019-07-22 12:10:00 -07006515 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006516 return -1;
6517}
6518
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006519
6520void wpa_sm_set_reset_fils_completed(struct wpa_sm *sm, int set)
6521{
6522 if (sm)
6523 sm->fils_completed = !!set;
6524}
6525
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006526#endif /* CONFIG_FILS */
6527
6528
6529int wpa_fils_is_completed(struct wpa_sm *sm)
6530{
6531#ifdef CONFIG_FILS
6532 return sm && sm->fils_completed;
6533#else /* CONFIG_FILS */
6534 return 0;
6535#endif /* CONFIG_FILS */
6536}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006537
6538
6539#ifdef CONFIG_OWE
6540
6541struct wpabuf * owe_build_assoc_req(struct wpa_sm *sm, u16 group)
6542{
6543 struct wpabuf *ie = NULL, *pub = NULL;
6544 size_t prime_len;
6545
6546 if (group == 19)
6547 prime_len = 32;
6548 else if (group == 20)
6549 prime_len = 48;
6550 else if (group == 21)
6551 prime_len = 66;
6552 else
6553 return NULL;
6554
6555 crypto_ecdh_deinit(sm->owe_ecdh);
6556 sm->owe_ecdh = crypto_ecdh_init(group);
6557 if (!sm->owe_ecdh)
6558 goto fail;
6559 sm->owe_group = group;
6560 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
6561 pub = wpabuf_zeropad(pub, prime_len);
6562 if (!pub)
6563 goto fail;
6564
6565 ie = wpabuf_alloc(5 + wpabuf_len(pub));
6566 if (!ie)
6567 goto fail;
6568 wpabuf_put_u8(ie, WLAN_EID_EXTENSION);
6569 wpabuf_put_u8(ie, 1 + 2 + wpabuf_len(pub));
6570 wpabuf_put_u8(ie, WLAN_EID_EXT_OWE_DH_PARAM);
6571 wpabuf_put_le16(ie, group);
6572 wpabuf_put_buf(ie, pub);
6573 wpabuf_free(pub);
6574 wpa_hexdump_buf(MSG_DEBUG, "OWE: Diffie-Hellman Parameter element",
6575 ie);
6576
6577 return ie;
6578fail:
6579 wpabuf_free(pub);
6580 crypto_ecdh_deinit(sm->owe_ecdh);
6581 sm->owe_ecdh = NULL;
6582 return NULL;
6583}
6584
6585
6586int owe_process_assoc_resp(struct wpa_sm *sm, const u8 *bssid,
6587 const u8 *resp_ies, size_t resp_ies_len)
6588{
6589 struct ieee802_11_elems elems;
6590 u16 group;
6591 struct wpabuf *secret, *pub, *hkey;
6592 int res;
6593 u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN];
6594 const char *info = "OWE Key Generation";
6595 const u8 *addr[2];
6596 size_t len[2];
6597 size_t hash_len, prime_len;
6598 struct wpa_ie_data data;
6599
6600 if (!resp_ies ||
6601 ieee802_11_parse_elems(resp_ies, resp_ies_len, &elems, 1) ==
6602 ParseFailed) {
6603 wpa_printf(MSG_INFO,
6604 "OWE: Could not parse Association Response frame elements");
6605 return -1;
6606 }
6607
6608 if (sm->cur_pmksa && elems.rsn_ie &&
6609 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, 2 + elems.rsn_ie_len,
6610 &data) == 0 &&
6611 data.num_pmkid == 1 && data.pmkid &&
6612 os_memcmp(sm->cur_pmksa->pmkid, data.pmkid, PMKID_LEN) == 0) {
6613 wpa_printf(MSG_DEBUG, "OWE: Use PMKSA caching");
6614 wpa_sm_set_pmk_from_pmksa(sm);
6615 return 0;
6616 }
6617
6618 if (!elems.owe_dh) {
6619 wpa_printf(MSG_INFO,
6620 "OWE: No Diffie-Hellman Parameter element found in Association Response frame");
6621 return -1;
6622 }
6623
6624 group = WPA_GET_LE16(elems.owe_dh);
6625 if (group != sm->owe_group) {
6626 wpa_printf(MSG_INFO,
6627 "OWE: Unexpected Diffie-Hellman group in response: %u",
6628 group);
6629 return -1;
6630 }
6631
6632 if (!sm->owe_ecdh) {
6633 wpa_printf(MSG_INFO, "OWE: No ECDH state available");
6634 return -1;
6635 }
6636
6637 if (group == 19)
6638 prime_len = 32;
6639 else if (group == 20)
6640 prime_len = 48;
6641 else if (group == 21)
6642 prime_len = 66;
6643 else
6644 return -1;
6645
6646 secret = crypto_ecdh_set_peerkey(sm->owe_ecdh, 0,
6647 elems.owe_dh + 2,
6648 elems.owe_dh_len - 2);
6649 secret = wpabuf_zeropad(secret, prime_len);
6650 if (!secret) {
6651 wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
6652 return -1;
6653 }
6654 wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret);
6655
6656 /* prk = HKDF-extract(C | A | group, z) */
6657
6658 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
6659 if (!pub) {
6660 wpabuf_clear_free(secret);
6661 return -1;
6662 }
6663
6664 /* PMKID = Truncate-128(Hash(C | A)) */
6665 addr[0] = wpabuf_head(pub);
6666 len[0] = wpabuf_len(pub);
6667 addr[1] = elems.owe_dh + 2;
6668 len[1] = elems.owe_dh_len - 2;
6669 if (group == 19) {
6670 res = sha256_vector(2, addr, len, pmkid);
6671 hash_len = SHA256_MAC_LEN;
6672 } else if (group == 20) {
6673 res = sha384_vector(2, addr, len, pmkid);
6674 hash_len = SHA384_MAC_LEN;
6675 } else if (group == 21) {
6676 res = sha512_vector(2, addr, len, pmkid);
6677 hash_len = SHA512_MAC_LEN;
6678 } else {
6679 res = -1;
6680 hash_len = 0;
6681 }
6682 pub = wpabuf_zeropad(pub, prime_len);
6683 if (res < 0 || !pub) {
6684 wpabuf_free(pub);
6685 wpabuf_clear_free(secret);
6686 return -1;
6687 }
6688
6689 hkey = wpabuf_alloc(wpabuf_len(pub) + elems.owe_dh_len - 2 + 2);
6690 if (!hkey) {
6691 wpabuf_free(pub);
6692 wpabuf_clear_free(secret);
6693 return -1;
6694 }
6695
6696 wpabuf_put_buf(hkey, pub); /* C */
6697 wpabuf_free(pub);
6698 wpabuf_put_data(hkey, elems.owe_dh + 2, elems.owe_dh_len - 2); /* A */
6699 wpabuf_put_le16(hkey, sm->owe_group); /* group */
6700 if (group == 19)
6701 res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
6702 wpabuf_head(secret), wpabuf_len(secret), prk);
6703 else if (group == 20)
6704 res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey),
6705 wpabuf_head(secret), wpabuf_len(secret), prk);
6706 else if (group == 21)
6707 res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey),
6708 wpabuf_head(secret), wpabuf_len(secret), prk);
6709 wpabuf_clear_free(hkey);
6710 wpabuf_clear_free(secret);
6711 if (res < 0)
6712 return -1;
6713
6714 wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
6715
6716 /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
6717
6718 if (group == 19)
6719 res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info,
6720 os_strlen(info), sm->pmk, hash_len);
6721 else if (group == 20)
6722 res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info,
6723 os_strlen(info), sm->pmk, hash_len);
6724 else if (group == 21)
6725 res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info,
6726 os_strlen(info), sm->pmk, hash_len);
Hai Shalom81f62d82019-07-22 12:10:00 -07006727 forced_memzero(prk, SHA512_MAC_LEN);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006728 if (res < 0) {
6729 sm->pmk_len = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006730 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006731 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006732 sm->pmk_len = hash_len;
6733
6734 wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sm->pmk, sm->pmk_len);
6735 wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
6736 pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, pmkid, NULL, 0,
6737 bssid, sm->own_addr, sm->network_ctx, sm->key_mgmt,
6738 NULL);
6739
6740 return 0;
6741}
6742
6743#endif /* CONFIG_OWE */
6744
6745
6746void wpa_sm_set_fils_cache_id(struct wpa_sm *sm, const u8 *fils_cache_id)
6747{
6748#ifdef CONFIG_FILS
6749 if (sm && fils_cache_id) {
6750 sm->fils_cache_id_set = 1;
6751 os_memcpy(sm->fils_cache_id, fils_cache_id, FILS_CACHE_ID_LEN);
6752 }
6753#endif /* CONFIG_FILS */
6754}
Hai Shalom021b0b52019-04-10 11:17:58 -07006755
6756
6757#ifdef CONFIG_DPP2
6758void wpa_sm_set_dpp_z(struct wpa_sm *sm, const struct wpabuf *z)
6759{
6760 if (sm) {
6761 wpabuf_clear_free(sm->dpp_z);
6762 sm->dpp_z = z ? wpabuf_dup(z) : NULL;
6763 }
6764}
6765#endif /* CONFIG_DPP2 */
Hai Shalom60840252021-02-19 19:02:11 -08006766
6767
6768#ifdef CONFIG_PASN
Sunil Ravi89eba102022-09-13 21:04:37 -07006769
Sunil Ravi89eba102022-09-13 21:04:37 -07006770void wpa_pasn_sm_set_caps(struct wpa_sm *sm, unsigned int flags2)
6771{
6772 if (flags2 & WPA_DRIVER_FLAGS2_SEC_LTF_STA)
6773 sm->secure_ltf = 1;
6774 if (flags2 & WPA_DRIVER_FLAGS2_SEC_RTT_STA)
6775 sm->secure_rtt = 1;
6776 if (flags2 & WPA_DRIVER_FLAGS2_PROT_RANGE_NEG_STA)
6777 sm->prot_range_neg = 1;
6778}
6779
Hai Shalom60840252021-02-19 19:02:11 -08006780#endif /* CONFIG_PASN */
Hai Shalomc1a21442022-02-04 13:43:00 -08006781
6782
6783void wpa_sm_pmksa_cache_reconfig(struct wpa_sm *sm)
6784{
6785 if (sm)
6786 pmksa_cache_reconfig(sm->pmksa);
6787}
Sunil Ravi77d572f2023-01-17 23:58:31 +00006788
6789
6790struct rsn_pmksa_cache * wpa_sm_get_pmksa_cache(struct wpa_sm *sm)
6791{
6792 return sm ? sm->pmksa : NULL;
6793}
6794
6795
6796void wpa_sm_set_cur_pmksa(struct wpa_sm *sm,
6797 struct rsn_pmksa_cache_entry *entry)
6798{
6799 if (sm)
6800 sm->cur_pmksa = entry;
6801}
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006802
6803
6804void wpa_sm_set_driver_bss_selection(struct wpa_sm *sm,
6805 bool driver_bss_selection)
6806{
6807 if (sm)
6808 sm->driver_bss_selection = driver_bss_selection;
6809}