blob: f5e24f2ff348ad849723355f7a89fe2e1b30273b [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;
1402
1403 /* Detect possible key reinstallation */
1404 if ((sm->mlo.links[link_id].gtk.gtk_len == (size_t) gd->gtk_len &&
1405 os_memcmp(sm->mlo.links[link_id].gtk.gtk, gd->gtk,
1406 sm->mlo.links[link_id].gtk.gtk_len) == 0) ||
1407 (sm->mlo.links[link_id].gtk_wnm_sleep.gtk_len ==
1408 (size_t) gd->gtk_len &&
1409 os_memcmp(sm->mlo.links[link_id].gtk_wnm_sleep.gtk, gd->gtk,
1410 sm->mlo.links[link_id].gtk_wnm_sleep.gtk_len) == 0)) {
1411 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1412 "RSN: Not reinstalling already in-use GTK to the driver (link_id=%d keyidx=%d tx=%d len=%d)",
1413 link_id, gd->keyidx, gd->tx, gd->gtk_len);
1414 return 0;
1415 }
1416
1417 wpa_hexdump_link_key(MSG_DEBUG, link_id, "RSN: Group Key", gd->gtk,
1418 gd->gtk_len);
1419 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1420 "RSN: Installing GTK to the driver (link_id=%d keyidx=%d tx=%d len=%d)",
1421 link_id, gd->keyidx, gd->tx, gd->gtk_len);
1422 wpa_hexdump_link(MSG_DEBUG, link_id, "RSN: RSC",
1423 key_rsc, gd->key_rsc_len);
1424 if (wpa_sm_set_key(sm, link_id, gd->alg, broadcast_ether_addr,
1425 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len, gtk,
1426 gd->gtk_len, KEY_FLAG_GROUP_RX) < 0) {
1427 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1428 "RSN: Failed to set GTK to the driver (link_id=%d alg=%d keylen=%d keyidx=%d)",
1429 link_id, gd->alg, gd->gtk_len, gd->keyidx);
1430 return -1;
1431 }
1432
1433 if (wnm_sleep) {
1434 sm->mlo.links[link_id].gtk_wnm_sleep.gtk_len = gd->gtk_len;
1435 os_memcpy(sm->mlo.links[link_id].gtk_wnm_sleep.gtk, gd->gtk,
1436 sm->mlo.links[link_id].gtk_wnm_sleep.gtk_len);
1437 } else {
1438 sm->mlo.links[link_id].gtk.gtk_len = gd->gtk_len;
1439 os_memcpy(sm->mlo.links[link_id].gtk.gtk, gd->gtk,
1440 sm->mlo.links[link_id].gtk.gtk_len);
1441 }
1442
1443 return 0;
1444}
1445
1446
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001447static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
1448 int tx)
1449{
1450 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
1451 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
1452 * seemed to set this bit (incorrectly, since Tx is only when
1453 * doing Group Key only APs) and without this workaround, the
1454 * data connection does not work because wpa_supplicant
1455 * configured non-zero keyidx to be used for unicast. */
1456 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1457 "WPA: Tx bit set for GTK, but pairwise "
1458 "keys are used - ignore Tx bit");
1459 return 0;
1460 }
1461 return tx;
1462}
1463
1464
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001465static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
1466 const u8 *rsc)
1467{
1468 int rsclen;
1469
1470 if (!sm->wpa_rsc_relaxation)
1471 return 0;
1472
1473 rsclen = wpa_cipher_rsc_len(sm->group_cipher);
1474
1475 /*
1476 * Try to detect RSC (endian) corruption issue where the AP sends
1477 * the RSC bytes in EAPOL-Key message in the wrong order, both if
1478 * it's actually a 6-byte field (as it should be) and if it treats
1479 * it as an 8-byte field.
1480 * An AP model known to have this bug is the Sapido RB-1632.
1481 */
1482 if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
1483 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1484 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
1485 rsc[0], rsc[1], rsc[2], rsc[3],
1486 rsc[4], rsc[5], rsc[6], rsc[7]);
1487
1488 return 1;
1489 }
1490
1491 return 0;
1492}
1493
1494
Sunil Ravi77d572f2023-01-17 23:58:31 +00001495static int wpa_supplicant_mlo_gtk(struct wpa_sm *sm, u8 link_id, const u8 *gtk,
1496 size_t gtk_len, int key_info)
1497{
1498 struct wpa_gtk_data gd;
1499 const u8 *key_rsc;
1500 int ret;
1501
1502 /*
1503 * MLO GTK KDE format:
1504 * KeyID[bits 0-1], Tx [bit 2], Reserved [bit 3], link id [4-7]
1505 * PN
1506 * GTK
1507 */
1508 os_memset(&gd, 0, sizeof(gd));
1509 wpa_hexdump_link_key(MSG_DEBUG, link_id,
1510 "RSN: received GTK in pairwise handshake",
1511 gtk, gtk_len);
1512
1513 if (gtk_len < RSN_MLO_GTK_KDE_PREFIX_LENGTH ||
1514 gtk_len - RSN_MLO_GTK_KDE_PREFIX_LENGTH > sizeof(gd.gtk))
1515 return -1;
1516
1517 gd.keyidx = gtk[0] & 0x3;
1518 gtk += 1;
1519 gtk_len -= 1;
1520
1521 key_rsc = gtk;
1522
1523 gtk += 6;
1524 gtk_len -= 6;
1525
1526 os_memcpy(gd.gtk, gtk, gtk_len);
1527 gd.gtk_len = gtk_len;
1528
1529 ret = 0;
1530 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher, gtk_len,
1531 gtk_len, &gd.key_rsc_len,
1532 &gd.alg) ||
1533 wpa_supplicant_install_mlo_gtk(sm, link_id, &gd, key_rsc, 0)) {
1534 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1535 "RSN: Failed to install GTK for MLO Link ID %u",
1536 link_id);
1537 ret = -1;
1538 goto out;
1539 }
1540
1541out:
1542 forced_memzero(&gd, sizeof(gd));
1543 return ret;
1544}
1545
1546
1547static int wpa_supplicant_pairwise_mlo_gtk(struct wpa_sm *sm,
1548 const struct wpa_eapol_key *key,
1549 struct wpa_eapol_ie_parse *ie,
1550 int key_info)
1551{
1552 u8 i;
1553
Sunil Ravi99c035e2024-07-12 01:42:03 +00001554 for_each_link(sm->mlo.valid_links, i) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00001555 if (!ie->mlo_gtk[i]) {
1556 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1557 "MLO RSN: GTK not found for link ID %u", i);
1558 return -1;
1559 }
1560
1561 if (wpa_supplicant_mlo_gtk(sm, i, ie->mlo_gtk[i],
1562 ie->mlo_gtk_len[i], key_info))
1563 return -1;
1564 }
1565
1566 return 0;
1567}
1568
1569
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001570static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
1571 const struct wpa_eapol_key *key,
1572 const u8 *gtk, size_t gtk_len,
1573 int key_info)
1574{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001575 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001576 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001577
1578 /*
1579 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
1580 * GTK KDE format:
1581 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
1582 * Reserved [bits 0-7]
1583 * GTK
1584 */
1585
1586 os_memset(&gd, 0, sizeof(gd));
1587 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
1588 gtk, gtk_len);
1589
1590 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
1591 return -1;
1592
1593 gd.keyidx = gtk[0] & 0x3;
1594 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1595 !!(gtk[0] & BIT(2)));
1596 gtk += 2;
1597 gtk_len -= 2;
1598
1599 os_memcpy(gd.gtk, gtk, gtk_len);
1600 gd.gtk_len = gtk_len;
1601
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001602 key_rsc = key->key_rsc;
1603 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1604 key_rsc = null_rsc;
1605
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001606 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
1607 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1608 gtk_len, gtk_len,
1609 &gd.key_rsc_len, &gd.alg) ||
Jouni Malinen58c0e962017-10-01 12:12:24 +03001610 wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001611 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1612 "RSN: Failed to install GTK");
Hai Shalom81f62d82019-07-22 12:10:00 -07001613 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001614 return -1;
1615 }
Hai Shalom81f62d82019-07-22 12:10:00 -07001616 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001617
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001618 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001619}
1620
1621
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001622static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
Jouni Malinen58c0e962017-10-01 12:12:24 +03001623 const struct wpa_igtk_kde *igtk,
1624 int wnm_sleep)
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001625{
1626 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1627 u16 keyidx = WPA_GET_LE16(igtk->keyid);
1628
1629 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +03001630 if ((sm->igtk.igtk_len == len &&
1631 os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) ||
1632 (sm->igtk_wnm_sleep.igtk_len == len &&
1633 os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1634 sm->igtk_wnm_sleep.igtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001635 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1636 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
1637 keyidx);
1638 return 0;
1639 }
1640
1641 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001642 "WPA: IGTK keyid %d pn " COMPACT_MACSTR,
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001643 keyidx, MAC2STR(igtk->pn));
1644 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
1645 if (keyidx > 4095) {
1646 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1647 "WPA: Invalid IGTK KeyID %d", keyidx);
1648 return -1;
1649 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00001650 if (wpa_sm_set_key(sm, -1, wpa_cipher_to_alg(sm->mgmt_group_cipher),
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001651 broadcast_ether_addr,
1652 keyidx, 0, igtk->pn, sizeof(igtk->pn),
Hai Shalomfdcde762020-04-02 11:19:20 -07001653 igtk->igtk, len, KEY_FLAG_GROUP_RX) < 0) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07001654 if (keyidx == 0x0400 || keyidx == 0x0500) {
1655 /* Assume the AP has broken PMF implementation since it
1656 * seems to have swapped the KeyID bytes. The AP cannot
1657 * be trusted to implement BIP correctly or provide a
1658 * valid IGTK, so do not try to configure this key with
1659 * swapped KeyID bytes. Instead, continue without
1660 * configuring the IGTK so that the driver can drop any
1661 * received group-addressed robust management frames due
1662 * to missing keys.
1663 *
1664 * Normally, this error behavior would result in us
1665 * disconnecting, but there are number of deployed APs
1666 * with this broken behavior, so as an interoperability
1667 * workaround, allow the connection to proceed. */
1668 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1669 "WPA: Ignore IGTK configuration error due to invalid IGTK KeyID byte order");
1670 } else {
1671 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1672 "WPA: Failed to configure IGTK to the driver");
1673 return -1;
1674 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001675 }
1676
Jouni Malinen58c0e962017-10-01 12:12:24 +03001677 if (wnm_sleep) {
1678 sm->igtk_wnm_sleep.igtk_len = len;
1679 os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1680 sm->igtk_wnm_sleep.igtk_len);
1681 } else {
1682 sm->igtk.igtk_len = len;
1683 os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
1684 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001685
1686 return 0;
1687}
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001688
1689
Hai Shalomfdcde762020-04-02 11:19:20 -07001690static int wpa_supplicant_install_bigtk(struct wpa_sm *sm,
1691 const struct wpa_bigtk_kde *bigtk,
1692 int wnm_sleep)
1693{
1694 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1695 u16 keyidx = WPA_GET_LE16(bigtk->keyid);
1696
1697 /* Detect possible key reinstallation */
1698 if ((sm->bigtk.bigtk_len == len &&
1699 os_memcmp(sm->bigtk.bigtk, bigtk->bigtk,
1700 sm->bigtk.bigtk_len) == 0) ||
1701 (sm->bigtk_wnm_sleep.bigtk_len == len &&
1702 os_memcmp(sm->bigtk_wnm_sleep.bigtk, bigtk->bigtk,
1703 sm->bigtk_wnm_sleep.bigtk_len) == 0)) {
1704 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1705 "WPA: Not reinstalling already in-use BIGTK to the driver (keyidx=%d)",
1706 keyidx);
1707 return 0;
1708 }
1709
1710 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1711 "WPA: BIGTK keyid %d pn " COMPACT_MACSTR,
1712 keyidx, MAC2STR(bigtk->pn));
1713 wpa_hexdump_key(MSG_DEBUG, "WPA: BIGTK", bigtk->bigtk, len);
1714 if (keyidx < 6 || keyidx > 7) {
1715 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1716 "WPA: Invalid BIGTK KeyID %d", keyidx);
1717 return -1;
1718 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00001719 if (wpa_sm_set_key(sm, -1, wpa_cipher_to_alg(sm->mgmt_group_cipher),
Hai Shalomfdcde762020-04-02 11:19:20 -07001720 broadcast_ether_addr,
1721 keyidx, 0, bigtk->pn, sizeof(bigtk->pn),
1722 bigtk->bigtk, len, KEY_FLAG_GROUP_RX) < 0) {
1723 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1724 "WPA: Failed to configure BIGTK to the driver");
1725 return -1;
1726 }
1727
1728 if (wnm_sleep) {
1729 sm->bigtk_wnm_sleep.bigtk_len = len;
1730 os_memcpy(sm->bigtk_wnm_sleep.bigtk, bigtk->bigtk,
1731 sm->bigtk_wnm_sleep.bigtk_len);
1732 } else {
1733 sm->bigtk.bigtk_len = len;
1734 os_memcpy(sm->bigtk.bigtk, bigtk->bigtk, sm->bigtk.bigtk_len);
1735 }
1736
1737 return 0;
1738}
1739
1740
Sunil Ravi77d572f2023-01-17 23:58:31 +00001741static int wpa_supplicant_install_mlo_igtk(struct wpa_sm *sm, u8 link_id,
1742 const struct rsn_mlo_igtk_kde *igtk,
1743 int wnm_sleep)
1744{
1745 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1746 u16 keyidx = WPA_GET_LE16(igtk->keyid);
1747
1748 /* Detect possible key reinstallation */
1749 if ((sm->mlo.links[link_id].igtk.igtk_len == len &&
1750 os_memcmp(sm->mlo.links[link_id].igtk.igtk, igtk->igtk,
1751 sm->mlo.links[link_id].igtk.igtk_len) == 0) ||
1752 (sm->mlo.links[link_id].igtk_wnm_sleep.igtk_len == len &&
1753 os_memcmp(sm->mlo.links[link_id].igtk_wnm_sleep.igtk, igtk->igtk,
1754 sm->mlo.links[link_id].igtk_wnm_sleep.igtk_len) == 0)) {
1755 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1756 "RSN: Not reinstalling already in-use IGTK to the driver (link_id=%d keyidx=%d)",
1757 link_id, keyidx);
1758 return 0;
1759 }
1760
1761 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1762 "RSN: MLO Link %u IGTK keyid %d pn " COMPACT_MACSTR,
1763 link_id, keyidx, MAC2STR(igtk->pn));
1764 wpa_hexdump_link_key(MSG_DEBUG, link_id, "RSN: IGTK", igtk->igtk, len);
1765 if (keyidx > 4095) {
1766 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1767 "RSN: Invalid MLO Link %d IGTK KeyID %d", link_id,
1768 keyidx);
1769 return -1;
1770 }
1771 if (wpa_sm_set_key(sm, link_id,
1772 wpa_cipher_to_alg(sm->mgmt_group_cipher),
1773 broadcast_ether_addr, keyidx, 0, igtk->pn,
1774 sizeof(igtk->pn), igtk->igtk, len,
1775 KEY_FLAG_GROUP_RX) < 0) {
1776 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1777 "RSN: Failed to configure MLO Link %d IGTK to the driver",
1778 link_id);
1779 return -1;
1780 }
1781
1782 if (wnm_sleep) {
1783 sm->mlo.links[link_id].igtk_wnm_sleep.igtk_len = len;
1784 os_memcpy(sm->mlo.links[link_id].igtk_wnm_sleep.igtk,
1785 igtk->igtk,
1786 sm->mlo.links[link_id].igtk_wnm_sleep.igtk_len);
1787 } else {
1788 sm->mlo.links[link_id].igtk.igtk_len = len;
1789 os_memcpy(sm->mlo.links[link_id].igtk.igtk, igtk->igtk,
1790 sm->mlo.links[link_id].igtk.igtk_len);
1791 }
1792
1793 return 0;
1794}
1795
1796
1797static int
1798wpa_supplicant_install_mlo_bigtk(struct wpa_sm *sm, u8 link_id,
1799 const struct rsn_mlo_bigtk_kde *bigtk,
1800 int wnm_sleep)
1801{
1802 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1803 u16 keyidx = WPA_GET_LE16(bigtk->keyid);
1804
1805 /* Detect possible key reinstallation */
1806 if ((sm->mlo.links[link_id].bigtk.bigtk_len == len &&
1807 os_memcmp(sm->mlo.links[link_id].bigtk.bigtk, bigtk->bigtk,
1808 sm->mlo.links[link_id].bigtk.bigtk_len) == 0) ||
1809 (sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk_len == len &&
1810 os_memcmp(sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk,
1811 bigtk->bigtk,
1812 sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk_len) ==
1813 0)) {
1814 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1815 "RSN: Not reinstalling already in-use BIGTK to the driver (link_id=%d keyidx=%d)",
1816 link_id, keyidx);
1817 return 0;
1818 }
1819
1820 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1821 "RSN: MLO Link %u BIGTK keyid %d pn " COMPACT_MACSTR,
1822 link_id, keyidx, MAC2STR(bigtk->pn));
1823 wpa_hexdump_link_key(MSG_DEBUG, link_id, "RSN: BIGTK", bigtk->bigtk,
1824 len);
1825 if (keyidx < 6 || keyidx > 7) {
1826 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1827 "RSN: Invalid MLO Link %d BIGTK KeyID %d", link_id,
1828 keyidx);
1829 return -1;
1830 }
1831 if (wpa_sm_set_key(sm, link_id,
1832 wpa_cipher_to_alg(sm->mgmt_group_cipher),
1833 broadcast_ether_addr, keyidx, 0, bigtk->pn,
1834 sizeof(bigtk->pn), bigtk->bigtk, len,
1835 KEY_FLAG_GROUP_RX) < 0) {
1836 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1837 "RSN: Failed to configure MLO Link %d BIGTK to the driver",
1838 link_id);
1839 return -1;
1840 }
1841
1842 if (wnm_sleep) {
1843 sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk_len = len;
1844 os_memcpy(sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk,
1845 bigtk->bigtk,
1846 sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk_len);
1847 } else {
1848 sm->mlo.links[link_id].bigtk.bigtk_len = len;
1849 os_memcpy(sm->mlo.links[link_id].bigtk.bigtk, bigtk->bigtk,
1850 sm->mlo.links[link_id].bigtk.bigtk_len);
1851 }
1852
1853 return 0;
1854}
1855
1856
1857static int _mlo_ieee80211w_set_keys(struct wpa_sm *sm, u8 link_id,
1858 struct wpa_eapol_ie_parse *ie)
1859{
1860 size_t len;
1861
1862 if (ie->mlo_igtk[link_id]) {
1863 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1864 if (ie->mlo_igtk_len[link_id] !=
1865 RSN_MLO_IGTK_KDE_PREFIX_LENGTH + len)
1866 return -1;
1867
1868 if (wpa_supplicant_install_mlo_igtk(
1869 sm, link_id,
1870 (const struct rsn_mlo_igtk_kde *)
1871 ie->mlo_igtk[link_id],
1872 0) < 0)
1873 return -1;
1874 }
1875
1876 if (ie->mlo_bigtk[link_id] && sm->beacon_prot) {
1877 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1878 if (ie->mlo_bigtk_len[link_id] !=
1879 RSN_MLO_BIGTK_KDE_PREFIX_LENGTH + len)
1880 return -1;
1881
1882 if (wpa_supplicant_install_mlo_bigtk(
1883 sm, link_id,
1884 (const struct rsn_mlo_bigtk_kde *)
1885 ie->mlo_bigtk[link_id],
1886 0) < 0)
1887 return -1;
1888 }
1889
1890 return 0;
1891}
1892
1893
1894static int mlo_ieee80211w_set_keys(struct wpa_sm *sm,
1895 struct wpa_eapol_ie_parse *ie)
1896{
1897 u8 i;
1898
1899 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) ||
1900 sm->mgmt_group_cipher == WPA_CIPHER_GTK_NOT_USED)
1901 return 0;
1902
Sunil Ravi99c035e2024-07-12 01:42:03 +00001903 for_each_link(sm->mlo.valid_links, i) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00001904 if (_mlo_ieee80211w_set_keys(sm, i, ie))
1905 return -1;
1906 }
1907
1908 return 0;
1909}
1910
1911
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001912static int ieee80211w_set_keys(struct wpa_sm *sm,
1913 struct wpa_eapol_ie_parse *ie)
1914{
Hai Shalomfdcde762020-04-02 11:19:20 -07001915 size_t len;
1916
Hai Shalom60840252021-02-19 19:02:11 -08001917 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) ||
1918 sm->mgmt_group_cipher == WPA_CIPHER_GTK_NOT_USED)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001919 return 0;
1920
1921 if (ie->igtk) {
1922 const struct wpa_igtk_kde *igtk;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001923
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001924 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1925 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001926 return -1;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001927
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001928 igtk = (const struct wpa_igtk_kde *) ie->igtk;
Jouni Malinen58c0e962017-10-01 12:12:24 +03001929 if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001930 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001931 }
1932
Hai Shalomfdcde762020-04-02 11:19:20 -07001933 if (ie->bigtk && sm->beacon_prot) {
1934 const struct wpa_bigtk_kde *bigtk;
1935
1936 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1937 if (ie->bigtk_len != WPA_BIGTK_KDE_PREFIX_LEN + len)
1938 return -1;
1939
1940 bigtk = (const struct wpa_bigtk_kde *) ie->bigtk;
1941 if (wpa_supplicant_install_bigtk(sm, bigtk, 0) < 0)
1942 return -1;
1943 }
1944
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001945 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001946}
1947
1948
1949static void wpa_report_ie_mismatch(struct wpa_sm *sm,
1950 const char *reason, const u8 *src_addr,
1951 const u8 *wpa_ie, size_t wpa_ie_len,
1952 const u8 *rsn_ie, size_t rsn_ie_len)
1953{
1954 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
1955 reason, MAC2STR(src_addr));
1956
1957 if (sm->ap_wpa_ie) {
1958 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
1959 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
1960 }
1961 if (wpa_ie) {
1962 if (!sm->ap_wpa_ie) {
1963 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1964 "WPA: No WPA IE in Beacon/ProbeResp");
1965 }
1966 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
1967 wpa_ie, wpa_ie_len);
1968 }
1969
1970 if (sm->ap_rsn_ie) {
1971 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
1972 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
1973 }
1974 if (rsn_ie) {
1975 if (!sm->ap_rsn_ie) {
1976 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1977 "WPA: No RSN IE in Beacon/ProbeResp");
1978 }
1979 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
1980 rsn_ie, rsn_ie_len);
1981 }
1982
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001983 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001984}
1985
1986
1987#ifdef CONFIG_IEEE80211R
1988
1989static int ft_validate_mdie(struct wpa_sm *sm,
1990 const unsigned char *src_addr,
1991 struct wpa_eapol_ie_parse *ie,
1992 const u8 *assoc_resp_mdie)
1993{
1994 struct rsn_mdie *mdie;
1995
1996 mdie = (struct rsn_mdie *) (ie->mdie + 2);
1997 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
1998 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
1999 MOBILITY_DOMAIN_ID_LEN) != 0) {
2000 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
2001 "not match with the current mobility domain");
2002 return -1;
2003 }
2004
2005 if (assoc_resp_mdie &&
2006 (assoc_resp_mdie[1] != ie->mdie[1] ||
2007 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
2008 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
2009 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
2010 ie->mdie, 2 + ie->mdie[1]);
2011 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
2012 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
2013 return -1;
2014 }
2015
2016 return 0;
2017}
2018
2019
2020static int ft_validate_ftie(struct wpa_sm *sm,
2021 const unsigned char *src_addr,
2022 struct wpa_eapol_ie_parse *ie,
2023 const u8 *assoc_resp_ftie)
2024{
2025 if (ie->ftie == NULL) {
2026 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2027 "FT: No FTIE in EAPOL-Key msg 3/4");
2028 return -1;
2029 }
2030
2031 if (assoc_resp_ftie == NULL)
2032 return 0;
2033
2034 if (assoc_resp_ftie[1] != ie->ftie[1] ||
2035 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
2036 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
2037 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
2038 ie->ftie, 2 + ie->ftie[1]);
2039 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
2040 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
2041 return -1;
2042 }
2043
2044 return 0;
2045}
2046
2047
2048static int ft_validate_rsnie(struct wpa_sm *sm,
2049 const unsigned char *src_addr,
2050 struct wpa_eapol_ie_parse *ie)
2051{
2052 struct wpa_ie_data rsn;
2053
2054 if (!ie->rsn_ie)
2055 return 0;
2056
2057 /*
2058 * Verify that PMKR1Name from EAPOL-Key message 3/4
2059 * matches with the value we derived.
2060 */
2061 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
2062 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
2063 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
2064 "FT 4-way handshake message 3/4");
2065 return -1;
2066 }
2067
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002068 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
2069 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002070 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2071 "FT: PMKR1Name mismatch in "
2072 "FT 4-way handshake message 3/4");
2073 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
2074 rsn.pmkid, WPA_PMK_NAME_LEN);
2075 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
2076 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
2077 return -1;
2078 }
2079
2080 return 0;
2081}
2082
2083
2084static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
2085 const unsigned char *src_addr,
2086 struct wpa_eapol_ie_parse *ie)
2087{
2088 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
2089
2090 if (sm->assoc_resp_ies) {
2091 pos = sm->assoc_resp_ies;
2092 end = pos + sm->assoc_resp_ies_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002093 while (end - pos > 2) {
2094 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002095 break;
2096 switch (*pos) {
2097 case WLAN_EID_MOBILITY_DOMAIN:
2098 mdie = pos;
2099 break;
2100 case WLAN_EID_FAST_BSS_TRANSITION:
2101 ftie = pos;
2102 break;
2103 }
2104 pos += 2 + pos[1];
2105 }
2106 }
2107
2108 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
2109 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
2110 ft_validate_rsnie(sm, src_addr, ie) < 0)
2111 return -1;
2112
2113 return 0;
2114}
2115
2116#endif /* CONFIG_IEEE80211R */
2117
2118
2119static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
2120 const unsigned char *src_addr,
2121 struct wpa_eapol_ie_parse *ie)
2122{
2123 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
2124 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2125 "WPA: No WPA/RSN IE for this AP known. "
2126 "Trying to get from scan results");
2127 if (wpa_sm_get_beacon_ie(sm) < 0) {
2128 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2129 "WPA: Could not find AP from "
2130 "the scan results");
Hai Shalomfdcde762020-04-02 11:19:20 -07002131 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002132 }
Hai Shalomfdcde762020-04-02 11:19:20 -07002133 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
2134 "WPA: Found the current AP from updated scan results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002135 }
2136
2137 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
2138 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
2139 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
2140 "with IE in Beacon/ProbeResp (no IE?)",
2141 src_addr, ie->wpa_ie, ie->wpa_ie_len,
2142 ie->rsn_ie, ie->rsn_ie_len);
2143 return -1;
2144 }
2145
2146 if ((ie->wpa_ie && sm->ap_wpa_ie &&
2147 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
2148 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
2149 (ie->rsn_ie && sm->ap_rsn_ie &&
2150 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
2151 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
2152 ie->rsn_ie, ie->rsn_ie_len))) {
2153 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
2154 "with IE in Beacon/ProbeResp",
2155 src_addr, ie->wpa_ie, ie->wpa_ie_len,
2156 ie->rsn_ie, ie->rsn_ie_len);
2157 return -1;
2158 }
2159
2160 if (sm->proto == WPA_PROTO_WPA &&
2161 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
2162 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
2163 "detected - RSN was enabled and RSN IE "
2164 "was in msg 3/4, but not in "
2165 "Beacon/ProbeResp",
2166 src_addr, ie->wpa_ie, ie->wpa_ie_len,
2167 ie->rsn_ie, ie->rsn_ie_len);
2168 return -1;
2169 }
2170
Hai Shalom60840252021-02-19 19:02:11 -08002171 if (sm->proto == WPA_PROTO_RSN &&
2172 ((sm->ap_rsnxe && !ie->rsnxe) ||
2173 (!sm->ap_rsnxe && ie->rsnxe) ||
2174 (sm->ap_rsnxe && ie->rsnxe &&
2175 (sm->ap_rsnxe_len != ie->rsnxe_len ||
2176 os_memcmp(sm->ap_rsnxe, ie->rsnxe, sm->ap_rsnxe_len) != 0)))) {
Hai Shalomc3565922019-10-28 11:58:20 -07002177 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2178 "WPA: RSNXE mismatch between Beacon/ProbeResp and EAPOL-Key msg 3/4");
Hai Shalomfdcde762020-04-02 11:19:20 -07002179 wpa_hexdump(MSG_INFO, "RSNXE in Beacon/ProbeResp",
2180 sm->ap_rsnxe, sm->ap_rsnxe_len);
2181 wpa_hexdump(MSG_INFO, "RSNXE in EAPOL-Key msg 3/4",
2182 ie->rsnxe, ie->rsnxe_len);
2183 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Hai Shalomc3565922019-10-28 11:58:20 -07002184 return -1;
2185 }
2186
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002187#ifdef CONFIG_IEEE80211R
2188 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
2189 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
2190 return -1;
2191#endif /* CONFIG_IEEE80211R */
2192
2193 return 0;
2194}
2195
2196
2197/**
2198 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
2199 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2200 * @dst: Destination address for the frame
2201 * @key: Pointer to the EAPOL-Key frame header
2202 * @ver: Version bits from EAPOL-Key Key Info
2203 * @key_info: Key Info
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002204 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002205 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002206 */
2207int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
2208 const struct wpa_eapol_key *key,
2209 u16 ver, u16 key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002210 struct wpa_ptk *ptk)
2211{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002212 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002213 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002214 u8 *rbuf, *key_mic;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002215 u8 *kde = NULL;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002216 size_t kde_len = 0, extra_len = 0;
2217#ifdef CONFIG_TESTING_OPTIONS
2218 size_t pad_len = 0;
2219#endif /* CONFIG_TESTING_OPTIONS */
Sunil Ravi77d572f2023-01-17 23:58:31 +00002220
2221 if (sm->mlo.valid_links) {
2222 u8 *pos;
2223
2224 kde = os_malloc(RSN_SELECTOR_LEN + ETH_ALEN + 2);
2225 if (!kde)
2226 return -1;
2227
2228 /* Add MAC KDE */
2229 wpa_printf(MSG_DEBUG, "MLO: Add MAC KDE into EAPOL-Key 4/4");
2230 pos = kde;
2231 pos = rsn_add_kde(pos, RSN_KEY_DATA_MAC_ADDR, sm->own_addr,
2232 ETH_ALEN);
2233 kde_len = pos - kde;
2234 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002235
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002236#ifdef CONFIG_TESTING_OPTIONS
2237 if (sm->test_eapol_m4_elems)
2238 extra_len = wpabuf_len(sm->test_eapol_m4_elems);
2239 if (sm->encrypt_eapol_m4) {
2240 pad_len = (kde_len + extra_len) % 8;
2241 if (pad_len)
2242 pad_len = 8 - pad_len;
2243 extra_len += pad_len + 8;
2244 }
2245#endif /* CONFIG_TESTING_OPTIONS */
2246
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002247 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002248 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002249 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002250 hdrlen + kde_len + extra_len, &rlen,
2251 (void *) &reply);
Sunil Ravi77d572f2023-01-17 23:58:31 +00002252 if (!rbuf) {
2253 os_free(kde);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002254 return -1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002255 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002256
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002257 reply->type = (sm->proto == WPA_PROTO_RSN ||
2258 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002259 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
2260 key_info &= WPA_KEY_INFO_SECURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002261 key_info |= ver | WPA_KEY_INFO_KEY_TYPE;
2262 if (mic_len)
2263 key_info |= WPA_KEY_INFO_MIC;
2264 else
2265 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002266#ifdef CONFIG_TESTING_OPTIONS
2267 if (sm->encrypt_eapol_m4)
2268 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
2269#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002270 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002271 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002272 WPA_PUT_BE16(reply->key_length, 0);
2273 else
2274 os_memcpy(reply->key_length, key->key_length, 2);
2275 os_memcpy(reply->replay_counter, key->replay_counter,
2276 WPA_REPLAY_COUNTER_LEN);
2277
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002278 key_mic = (u8 *) (reply + 1);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002279 /* Key Data length */
2280 WPA_PUT_BE16(key_mic + mic_len, kde_len + extra_len);
Sunil Ravi77d572f2023-01-17 23:58:31 +00002281 if (kde) {
2282 os_memcpy(key_mic + mic_len + 2, kde, kde_len); /* Key Data */
2283 os_free(kde);
2284 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002285
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002286#ifdef CONFIG_TESTING_OPTIONS
2287 if (sm->test_eapol_m4_elems) {
2288 os_memcpy(key_mic + mic_len + 2 + kde_len,
2289 wpabuf_head(sm->test_eapol_m4_elems),
2290 wpabuf_len(sm->test_eapol_m4_elems));
2291 }
2292
2293 if (sm->encrypt_eapol_m4) {
2294 u8 *plain;
2295 size_t plain_len;
2296
2297 if (sm->test_eapol_m4_elems)
2298 extra_len = wpabuf_len(sm->test_eapol_m4_elems);
2299 else
2300 extra_len = 0;
2301 plain_len = kde_len + extra_len + pad_len;
2302 plain = os_memdup(key_mic + mic_len + 2, plain_len);
2303 if (!plain) {
2304 os_free(rbuf);
2305 return -1;
2306 }
2307 if (pad_len)
2308 plain[plain_len - pad_len] = 0xdd;
2309
2310 wpa_hexdump_key(MSG_DEBUG, "RSN: AES-WRAP using KEK",
2311 ptk->kek, ptk->kek_len);
2312 if (aes_wrap(ptk->kek, ptk->kek_len, plain_len / 8, plain,
2313 key_mic + mic_len + 2)) {
2314 os_free(plain);
2315 os_free(rbuf);
2316 return -1;
2317 }
2318 wpa_hexdump(MSG_DEBUG,
2319 "RSN: Encrypted Key Data from AES-WRAP",
2320 key_mic + mic_len + 2, plain_len + 8);
2321 os_free(plain);
2322 }
2323#endif /* CONFIG_TESTING_OPTIONS */
2324
Roshan Pius5e7db942018-04-12 12:27:41 -07002325 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 4/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002326 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
2327 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002328}
2329
2330
Sunil Ravi77d572f2023-01-17 23:58:31 +00002331static int wpa_supplicant_validate_link_kde(struct wpa_sm *sm, u8 link_id,
2332 const u8 *link_kde,
2333 size_t link_kde_len)
2334{
2335 size_t rsne_len = 0, rsnxe_len = 0;
2336 const u8 *rsne = NULL, *rsnxe = NULL;
2337
2338 if (!link_kde ||
2339 link_kde_len < RSN_MLO_LINK_KDE_LINK_MAC_INDEX + ETH_ALEN) {
2340 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2341 "RSN: MLO Link KDE is not found for link ID %d",
2342 link_id);
2343 return -1;
2344 }
2345
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002346 if (!ether_addr_equal(sm->mlo.links[link_id].bssid,
2347 &link_kde[RSN_MLO_LINK_KDE_LINK_MAC_INDEX])) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00002348 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2349 "RSN: MLO Link %u MAC address (" MACSTR
2350 ") not matching association response (" MACSTR ")",
2351 link_id,
2352 MAC2STR(&link_kde[RSN_MLO_LINK_KDE_LINK_MAC_INDEX]),
2353 MAC2STR(sm->mlo.links[link_id].bssid));
2354 return -1;
2355 }
2356
2357 if (link_kde[0] & RSN_MLO_LINK_KDE_LI_RSNE_INFO) {
2358 rsne = link_kde + RSN_MLO_LINK_KDE_FIXED_LENGTH;
2359 if (link_kde_len < RSN_MLO_LINK_KDE_FIXED_LENGTH + 2 ||
2360 link_kde_len <
2361 (size_t) (RSN_MLO_LINK_KDE_FIXED_LENGTH + 2 + rsne[1])) {
2362 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2363 "RSN: No room for link %u RSNE in MLO Link KDE",
2364 link_id);
2365 return -1;
2366 }
2367
2368 rsne_len = rsne[1] + 2;
2369 }
2370
2371 if (!rsne) {
2372 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2373 "RSN: RSNE not present in MLO Link %u KDE", link_id);
2374 return -1;
2375 }
2376
2377 if (link_kde[0] & RSN_MLO_LINK_KDE_LI_RSNXE_INFO) {
2378 rsnxe = link_kde + RSN_MLO_LINK_KDE_FIXED_LENGTH + rsne_len;
2379 if (link_kde_len <
2380 (RSN_MLO_LINK_KDE_FIXED_LENGTH + rsne_len + 2) ||
2381 link_kde_len <
2382 (RSN_MLO_LINK_KDE_FIXED_LENGTH + rsne_len + 2 + rsnxe[1])) {
2383 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2384 "RSN: No room for link %u RSNXE in MLO Link KDE",
2385 link_id);
2386 return -1;
2387 }
2388
2389 rsnxe_len = rsnxe[1] + 2;
2390 }
2391
2392 if (wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
2393 sm->mlo.links[link_id].ap_rsne,
2394 sm->mlo.links[link_id].ap_rsne_len,
2395 rsne, rsne_len)) {
2396 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2397 "RSN MLO: IE in 3/4 msg does not match with IE in Beacon/ProbeResp for link ID %u",
2398 link_id);
2399 wpa_hexdump(MSG_INFO, "RSNE in Beacon/ProbeResp",
2400 sm->mlo.links[link_id].ap_rsne,
2401 sm->mlo.links[link_id].ap_rsne_len);
2402 wpa_hexdump(MSG_INFO, "RSNE in EAPOL-Key msg 3/4",
2403 rsne, rsne_len);
2404 return -1;
2405 }
2406
2407 if ((sm->mlo.links[link_id].ap_rsnxe && !rsnxe) ||
2408 (!sm->mlo.links[link_id].ap_rsnxe && rsnxe) ||
2409 (sm->mlo.links[link_id].ap_rsnxe && rsnxe &&
2410 (sm->mlo.links[link_id].ap_rsnxe_len != rsnxe_len ||
2411 os_memcmp(sm->mlo.links[link_id].ap_rsnxe, rsnxe,
2412 sm->mlo.links[link_id].ap_rsnxe_len) != 0))) {
2413 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2414 "RSN MLO: RSNXE mismatch between Beacon/ProbeResp and EAPOL-Key msg 3/4 for link ID %u",
2415 link_id);
2416 wpa_hexdump(MSG_INFO, "RSNXE in Beacon/ProbeResp",
2417 sm->mlo.links[link_id].ap_rsnxe,
2418 sm->mlo.links[link_id].ap_rsnxe_len);
2419 wpa_hexdump(MSG_INFO, "RSNXE in EAPOL-Key msg 3/4",
2420 rsnxe, rsnxe_len);
2421 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
2422 return -1;
2423 }
2424
2425 return 0;
2426}
2427
2428
2429static int wpa_validate_mlo_ieee80211w_kdes(struct wpa_sm *sm,
2430 u8 link_id,
2431 struct wpa_eapol_ie_parse *ie)
2432{
2433 if (ie->mlo_igtk[link_id] &&
2434 ie->mlo_igtk_len[link_id] != RSN_MLO_IGTK_KDE_PREFIX_LENGTH +
2435 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
2436 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2437 "RSN MLO: Invalid IGTK KDE length %lu for link ID %u",
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002438 (unsigned long) ie->mlo_igtk_len[link_id], link_id);
Sunil Ravi77d572f2023-01-17 23:58:31 +00002439 return -1;
2440 }
2441
2442 if (!sm->beacon_prot)
2443 return 0;
2444
2445 if (ie->mlo_bigtk[link_id] &&
2446 ie->mlo_bigtk_len[link_id] != RSN_MLO_BIGTK_KDE_PREFIX_LENGTH +
2447 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
2448 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2449 "RSN MLO: Invalid BIGTK KDE length %lu for link ID %u",
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002450 (unsigned long) ie->mlo_bigtk_len[link_id], link_id);
Sunil Ravi77d572f2023-01-17 23:58:31 +00002451 return -1;
2452 }
2453
2454 return 0;
2455}
2456
2457
2458static void wpa_supplicant_process_3_of_4_wpa(struct wpa_sm *sm,
2459 const struct wpa_eapol_key *key,
2460 u16 ver, const u8 *key_data,
2461 size_t key_data_len)
2462{
2463 u16 key_info, keylen;
2464 struct wpa_eapol_ie_parse ie;
2465
2466 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
2467 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2468 "WPA: RX message 3 of 4-Way Handshake from " MACSTR
2469 " (ver=%d)", MAC2STR(sm->bssid), ver);
2470
2471 key_info = WPA_GET_BE16(key->key_info);
2472
2473 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
2474 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
2475 goto failed;
2476
2477 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
2478 goto failed;
2479
2480 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
2481 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2482 "WPA: ANonce from message 1 of 4-Way Handshake differs from 3 of 4-Way Handshake - drop packet (src="
2483 MACSTR ")", MAC2STR(sm->bssid));
2484 goto failed;
2485 }
2486
2487 keylen = WPA_GET_BE16(key->key_length);
2488 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
2489 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2490 "WPA: Invalid %s key length %d (src=" MACSTR ")",
2491 wpa_cipher_txt(sm->pairwise_cipher), keylen,
2492 MAC2STR(sm->bssid));
2493 goto failed;
2494 }
2495
2496 if (wpa_supplicant_send_4_of_4(sm, wpa_sm_get_auth_addr(sm), key, ver,
2497 key_info, &sm->ptk) < 0)
2498 goto failed;
2499
2500 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
2501 * for the next 4-Way Handshake. If msg 3 is received again, the old
2502 * SNonce will still be used to avoid changing PTK. */
2503 sm->renew_snonce = 1;
2504
2505 if ((key_info & WPA_KEY_INFO_INSTALL) &&
2506 wpa_supplicant_install_ptk(sm, key, KEY_FLAG_RX_TX))
2507 goto failed;
2508
2509 if (key_info & WPA_KEY_INFO_SECURE) {
2510 wpa_sm_mlme_setprotection(
2511 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
2512 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
2513 eapol_sm_notify_portValid(sm->eapol, true);
2514 }
2515 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
2516
2517 sm->msg_3_of_4_ok = 1;
2518 return;
2519
2520failed:
2521 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2522}
2523
2524
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002525static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
2526 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002527 u16 ver, const u8 *key_data,
2528 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002529{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002530 u16 key_info, keylen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002531 struct wpa_eapol_ie_parse ie;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002532 bool mlo = sm->mlo.valid_links;
2533 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002534
2535 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
Sunil Ravi77d572f2023-01-17 23:58:31 +00002536 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO,
2537 "RSN: RX message 3 of 4-Way Handshake from " MACSTR
2538 " (ver=%d)%s", MAC2STR(sm->bssid), ver, mlo ? " (MLO)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002539
2540 key_info = WPA_GET_BE16(key->key_info);
2541
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002542 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
2543 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002544 goto failed;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002545
2546 if (mlo && !ie.valid_mlo_gtks) {
2547 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2548 "MLO RSN: No GTK KDE included in EAPOL-Key msg 3/4");
2549 goto failed;
2550 }
2551 if (mlo &&
2552 (key_info &
2553 (WPA_KEY_INFO_ENCR_KEY_DATA | WPA_KEY_INFO_INSTALL |
2554 WPA_KEY_INFO_SECURE)) !=
2555 (WPA_KEY_INFO_ENCR_KEY_DATA | WPA_KEY_INFO_INSTALL |
2556 WPA_KEY_INFO_SECURE)) {
2557 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2558 "RSN MLO: Invalid key info (0x%x) in EAPOL-Key msg 3/4",
2559 key_info);
2560 goto failed;
2561 }
2562
2563 if (mlo && !is_valid_ap_mld_mac_kde(sm, ie.mac_addr)) {
2564 wpa_printf(MSG_DEBUG, "RSN: Invalid AP MLD MAC address KDE");
2565 goto failed;
2566 }
2567
2568 for (i = 0; mlo && i < MAX_NUM_MLD_LINKS; i++) {
2569 if (!(sm->mlo.req_links & BIT(i)))
2570 continue;
2571
2572 if (wpa_supplicant_validate_link_kde(sm, i, ie.mlo_link[i],
2573 ie.mlo_link_len[i]) < 0)
2574 goto failed;
2575
2576 if (!(sm->mlo.valid_links & BIT(i)))
2577 continue;
2578
2579 if (!ie.mlo_gtk[i]) {
2580 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2581 "RSN: GTK not found for link ID %u", i);
2582 goto failed;
2583 }
2584
2585 if (sm->mgmt_group_cipher != WPA_CIPHER_GTK_NOT_USED &&
2586 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
2587 wpa_validate_mlo_ieee80211w_kdes(sm, i, &ie) < 0)
2588 goto failed;
2589 }
2590
2591#ifdef CONFIG_IEEE80211R
2592 if (mlo && wpa_key_mgmt_ft(sm->key_mgmt) &&
2593 wpa_supplicant_validate_ie_ft(sm, sm->bssid, &ie) < 0)
2594 goto failed;
2595#endif /* CONFIG_IEEE80211R */
2596
2597 if (!mlo && ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002598 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2599 "WPA: GTK IE in unencrypted key data");
2600 goto failed;
2601 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00002602 if (!mlo && ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002603 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2604 "WPA: IGTK KDE in unencrypted key data");
2605 goto failed;
2606 }
2607
Sunil Ravi77d572f2023-01-17 23:58:31 +00002608 if (!mlo && ie.igtk &&
Hai Shalom60840252021-02-19 19:02:11 -08002609 sm->mgmt_group_cipher != WPA_CIPHER_GTK_NOT_USED &&
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002610 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
2611 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
2612 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002613 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2614 "WPA: Invalid IGTK KDE length %lu",
2615 (unsigned long) ie.igtk_len);
2616 goto failed;
2617 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002618
Sunil Ravi77d572f2023-01-17 23:58:31 +00002619 if (!mlo && wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002620 goto failed;
2621
Hai Shalomfdcde762020-04-02 11:19:20 -07002622 if (wpa_handle_ext_key_id(sm, &ie))
2623 goto failed;
2624
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002625 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
2626 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2627 "WPA: ANonce from message 1 of 4-Way Handshake "
2628 "differs from 3 of 4-Way Handshake - drop packet (src="
2629 MACSTR ")", MAC2STR(sm->bssid));
2630 goto failed;
2631 }
2632
2633 keylen = WPA_GET_BE16(key->key_length);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002634 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
2635 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2636 "WPA: Invalid %s key length %d (src=" MACSTR
2637 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
2638 MAC2STR(sm->bssid));
2639 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002640 }
2641
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002642#ifdef CONFIG_P2P
2643 if (ie.ip_addr_alloc) {
2644 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
2645 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
2646 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
2647 }
2648#endif /* CONFIG_P2P */
2649
Hai Shalom74f70d42019-02-11 14:42:39 -08002650#ifdef CONFIG_OCV
2651 if (wpa_sm_ocv_enabled(sm)) {
2652 struct wpa_channel_info ci;
2653
2654 if (wpa_sm_channel_info(sm, &ci) != 0) {
2655 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2656 "Failed to get channel info to validate received OCI in EAPOL-Key 3/4");
2657 return;
2658 }
2659
2660 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
2661 channel_width_to_int(ci.chanwidth),
Hai Shalom899fcc72020-10-19 14:38:18 -07002662 ci.seg1_idx) != OCI_SUCCESS) {
2663 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
2664 "addr=" MACSTR " frame=eapol-key-m3 error=%s",
2665 MAC2STR(sm->bssid), ocv_errorstr);
Hai Shalom74f70d42019-02-11 14:42:39 -08002666 return;
2667 }
2668 }
2669#endif /* CONFIG_OCV */
2670
Hai Shalom4fbc08f2020-05-18 12:37:00 -07002671#ifdef CONFIG_DPP2
2672 if (DPP_VERSION > 1 && ie.dpp_kde) {
2673 wpa_printf(MSG_DEBUG,
2674 "DPP: peer Protocol Version %u Flags 0x%x",
2675 ie.dpp_kde[0], ie.dpp_kde[1]);
2676 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_pfs != 2 &&
2677 (ie.dpp_kde[1] & DPP_KDE_PFS_ALLOWED) && !sm->dpp_z) {
2678 wpa_printf(MSG_INFO,
2679 "DPP: Peer indicated it supports PFS and local configuration allows this, but PFS was not negotiated for the association");
2680 goto failed;
2681 }
2682 }
2683#endif /* CONFIG_DPP2 */
2684
Hai Shalomfdcde762020-04-02 11:19:20 -07002685 if (sm->use_ext_key_id &&
2686 wpa_supplicant_install_ptk(sm, key, KEY_FLAG_RX))
2687 goto failed;
2688
Sunil Ravi77d572f2023-01-17 23:58:31 +00002689 if (wpa_supplicant_send_4_of_4(sm, wpa_sm_get_auth_addr(sm), key, ver,
2690 key_info, &sm->ptk) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002691 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002692
2693 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
2694 * for the next 4-Way Handshake. If msg 3 is received again, the old
2695 * SNonce will still be used to avoid changing PTK. */
2696 sm->renew_snonce = 1;
2697
2698 if (key_info & WPA_KEY_INFO_INSTALL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002699 int res;
2700
2701 if (sm->use_ext_key_id)
2702 res = wpa_supplicant_activate_ptk(sm);
2703 else
2704 res = wpa_supplicant_install_ptk(sm, key,
2705 KEY_FLAG_RX_TX);
2706 if (res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002707 goto failed;
2708 }
2709
2710 if (key_info & WPA_KEY_INFO_SECURE) {
2711 wpa_sm_mlme_setprotection(
2712 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
2713 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
Hai Shalome21d4e82020-04-29 16:34:06 -07002714 eapol_sm_notify_portValid(sm->eapol, true);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002715 }
2716 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
2717
Sunil Ravi77d572f2023-01-17 23:58:31 +00002718 if (mlo) {
2719 if (wpa_supplicant_pairwise_mlo_gtk(sm, key, &ie,
2720 key_info) < 0) {
2721 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2722 "MLO RSN: Failed to configure MLO GTKs");
2723 goto failed;
2724 }
2725 } else if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07002726 /* No GTK to be set to the driver */
2727 } else if (!ie.gtk && sm->proto == WPA_PROTO_RSN) {
2728 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2729 "RSN: No GTK KDE included in EAPOL-Key msg 3/4");
2730 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002731 } else if (ie.gtk &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002732 wpa_supplicant_pairwise_gtk(sm, key,
2733 ie.gtk, ie.gtk_len, key_info) < 0) {
2734 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2735 "RSN: Failed to configure GTK");
2736 goto failed;
2737 }
2738
Sunil Ravi77d572f2023-01-17 23:58:31 +00002739 if ((mlo && mlo_ieee80211w_set_keys(sm, &ie) < 0) ||
2740 (!mlo && ieee80211w_set_keys(sm, &ie) < 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002741 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2742 "RSN: Failed to configure IGTK");
2743 goto failed;
2744 }
2745
Sunil Ravi77d572f2023-01-17 23:58:31 +00002746 if (mlo || sm->group_cipher == WPA_CIPHER_GTK_NOT_USED || ie.gtk)
Hai Shalom5f92bc92019-04-18 11:54:11 -07002747 wpa_supplicant_key_neg_complete(sm, sm->bssid,
2748 key_info & WPA_KEY_INFO_SECURE);
2749
Sunil Ravi77d572f2023-01-17 23:58:31 +00002750 if (mlo || ie.gtk)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002751 wpa_sm_set_rekey_offload(sm);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002752
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002753 /* Add PMKSA cache entry for Suite B AKMs here since PMKID can be
2754 * calculated only after KCK has been derived. Though, do not replace an
2755 * existing PMKSA entry after each 4-way handshake (i.e., new KCK/PMKID)
2756 * to avoid unnecessary changes of PMKID while continuing to use the
2757 * same PMK. */
2758 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt) &&
2759 !sm->cur_pmksa) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002760 struct rsn_pmksa_cache_entry *sa;
2761
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002762 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002763 sm->ptk.kck, sm->ptk.kck_len,
Sunil Ravi77d572f2023-01-17 23:58:31 +00002764 wpa_sm_get_auth_addr(sm), sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002765 sm->network_ctx, sm->key_mgmt, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002766 if (!sm->cur_pmksa)
2767 sm->cur_pmksa = sa;
2768 }
2769
Hai Shalomfdcde762020-04-02 11:19:20 -07002770 if (ie.transition_disable)
2771 wpa_sm_transition_disable(sm, ie.transition_disable[0]);
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002772 sm->msg_3_of_4_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002773 return;
2774
2775failed:
2776 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2777}
2778
2779
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002780static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
2781 const struct wpa_eapol_key *key,
2782 int ver, u16 key_info)
2783{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002784 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002785 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002786 u8 *rbuf, *key_mic;
Hai Shalom74f70d42019-02-11 14:42:39 -08002787 size_t kde_len = 0;
2788
Sunil Ravia04bd252022-05-02 22:54:18 -07002789#ifdef CONFIG_TESTING_OPTIONS
2790 if (sm->disable_eapol_g2_tx) {
2791 wpa_printf(MSG_INFO, "TEST: Disable sending EAPOL-Key 2/2");
2792 return 0;
2793 }
2794#endif /* CONFIG_TESTING_OPTIONS */
2795
Hai Shalom74f70d42019-02-11 14:42:39 -08002796#ifdef CONFIG_OCV
2797 if (wpa_sm_ocv_enabled(sm))
2798 kde_len = OCV_OCI_KDE_LEN;
2799#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002800
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002801 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002802 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002803 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Hai Shalom74f70d42019-02-11 14:42:39 -08002804 hdrlen + kde_len, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002805 if (rbuf == NULL)
2806 return -1;
2807
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002808 reply->type = (sm->proto == WPA_PROTO_RSN ||
2809 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002810 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
2811 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002812 key_info |= ver | WPA_KEY_INFO_SECURE;
2813 if (mic_len)
2814 key_info |= WPA_KEY_INFO_MIC;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002815 else
2816 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002817 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002818 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002819 WPA_PUT_BE16(reply->key_length, 0);
2820 else
2821 os_memcpy(reply->key_length, key->key_length, 2);
2822 os_memcpy(reply->replay_counter, key->replay_counter,
2823 WPA_REPLAY_COUNTER_LEN);
2824
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002825 key_mic = (u8 *) (reply + 1);
Hai Shalom74f70d42019-02-11 14:42:39 -08002826 WPA_PUT_BE16(key_mic + mic_len, kde_len); /* Key Data Length */
2827
2828#ifdef CONFIG_OCV
2829 if (wpa_sm_ocv_enabled(sm)) {
2830 struct wpa_channel_info ci;
2831 u8 *pos;
2832
2833 if (wpa_sm_channel_info(sm, &ci) != 0) {
2834 wpa_printf(MSG_WARNING,
2835 "Failed to get channel info for OCI element in EAPOL-Key 2/2");
2836 os_free(rbuf);
2837 return -1;
2838 }
Hai Shalom899fcc72020-10-19 14:38:18 -07002839#ifdef CONFIG_TESTING_OPTIONS
2840 if (sm->oci_freq_override_eapol_g2) {
2841 wpa_printf(MSG_INFO,
2842 "TEST: Override OCI KDE frequency %d -> %d MHz",
2843 ci.frequency,
2844 sm->oci_freq_override_eapol_g2);
2845 ci.frequency = sm->oci_freq_override_eapol_g2;
2846 }
2847#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -08002848
2849 pos = key_mic + mic_len + 2; /* Key Data */
2850 if (ocv_insert_oci_kde(&ci, &pos) < 0) {
2851 os_free(rbuf);
2852 return -1;
2853 }
2854 }
2855#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002856
2857 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002858 return wpa_eapol_key_send(sm, &sm->ptk, ver, wpa_sm_get_auth_addr(sm),
2859 ETH_P_EAPOL, rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002860}
2861
2862
Sunil Ravi77d572f2023-01-17 23:58:31 +00002863static void wpa_supplicant_process_mlo_1_of_2(struct wpa_sm *sm,
2864 const unsigned char *src_addr,
2865 const struct wpa_eapol_key *key,
2866 const u8 *key_data,
2867 size_t key_data_len, u16 ver)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002868{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002869 u16 key_info;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002870 u8 i;
2871 struct wpa_eapol_ie_parse ie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002872
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002873 if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) {
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002874 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00002875 "MLO RSN: Group Key Handshake started prior to completion of 4-way handshake");
2876 goto failed;
2877 }
2878
2879 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "MLO RSN: RX message 1 of Group "
2880 "Key Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr),
2881 ver);
2882
2883 key_info = WPA_GET_BE16(key->key_info);
2884
2885 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
2886
2887 wpa_hexdump_key(MSG_DEBUG, "MLO RSN: msg 1/2 key data", key_data,
2888 key_data_len);
2889 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
2890 goto failed;
2891
2892 if (!ie.valid_mlo_gtks) {
2893 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2894 "MLO RSN: No MLO GTK KDE in Group Key msg 1/2");
2895 goto failed;
2896 }
2897
2898 if (!(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
2899 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2900 "MLO RSN: MLO GTK KDE in unencrypted key data");
2901 goto failed;
2902 }
2903
2904#ifdef CONFIG_OCV
2905 if (wpa_sm_ocv_enabled(sm)) {
2906 struct wpa_channel_info ci;
2907
2908 if (wpa_sm_channel_info(sm, &ci) != 0) {
2909 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2910 "Failed to get channel info to validate received OCI in EAPOL-Key group msg 1/2");
2911 goto failed;
2912 }
2913
2914 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
2915 channel_width_to_int(ci.chanwidth),
2916 ci.seg1_idx) != OCI_SUCCESS) {
2917 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
2918 "addr=" MACSTR " frame=eapol-key-g1 error=%s",
2919 MAC2STR(sm->bssid), ocv_errorstr);
2920 goto failed;
2921 }
2922 }
2923#endif /* CONFIG_OCV */
2924
2925 if (mlo_ieee80211w_set_keys(sm, &ie) < 0)
2926 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2927 "MLO RSN: Failed to configure MLO IGTK");
2928
Sunil Ravi99c035e2024-07-12 01:42:03 +00002929 for_each_link(sm->mlo.valid_links, i) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00002930 /*
2931 * AP may send group keys for subset of the all links during
2932 * rekey
2933 */
2934 if (!ie.mlo_gtk[i])
2935 continue;
2936
2937 if (wpa_supplicant_mlo_gtk(sm, i, ie.mlo_gtk[i],
2938 ie.mlo_gtk_len[i], key_info))
2939 goto failed;
2940 }
2941
2942 if (wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
2943 goto failed;
2944
2945 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "MLO RSN: Group rekeying completed "
2946 "with " MACSTR " [GTK=%s]", MAC2STR(sm->mlo.ap_mld_addr),
2947 wpa_cipher_txt(sm->group_cipher));
2948 wpa_sm_cancel_auth_timeout(sm);
2949 wpa_sm_set_state(sm, WPA_COMPLETED);
2950
2951 wpa_sm_set_rekey_offload(sm);
2952
2953 return;
2954
2955failed:
2956 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2957}
2958
2959
2960static void wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
2961 const unsigned char *src_addr,
2962 const struct wpa_eapol_key *key,
2963 const u8 *key_data,
2964 size_t key_data_len, u16 ver)
2965{
2966 u16 key_info;
2967 int rekey;
2968 struct wpa_gtk_data gd;
2969 const u8 *key_rsc;
2970 size_t maxkeylen;
2971 u16 gtk_len;
2972
2973 if (!sm->msg_3_of_4_ok) {
2974 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002975 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
2976 goto failed;
2977 }
2978
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002979 os_memset(&gd, 0, sizeof(gd));
2980
2981 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002982 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2983 "WPA: RX message 1 of Group Key Handshake from " MACSTR
2984 " (ver=%d)", MAC2STR(src_addr), ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002985
2986 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002987
Sunil Ravi77d572f2023-01-17 23:58:31 +00002988 gtk_len = WPA_GET_BE16(key->key_length);
2989 maxkeylen = key_data_len;
2990 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
2991 if (maxkeylen < 8) {
2992 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2993 "WPA: Too short maxkeylen (%lu)",
2994 (unsigned long) maxkeylen);
2995 goto failed;
2996 }
2997 maxkeylen -= 8;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002998 }
2999
Sunil Ravi77d572f2023-01-17 23:58:31 +00003000 if (gtk_len > maxkeylen ||
3001 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
3002 gtk_len, maxkeylen,
3003 &gd.key_rsc_len, &gd.alg))
3004 goto failed;
3005
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003006 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
3007
Sunil Ravi77d572f2023-01-17 23:58:31 +00003008 gd.gtk_len = gtk_len;
3009 gd.keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
3010 WPA_KEY_INFO_KEY_INDEX_SHIFT;
3011 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
3012#if defined(CONFIG_NO_RC4) || defined(CONFIG_FIPS)
3013 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3014 "WPA: RC4 not supported in the build");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003015 goto failed;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003016#else /* CONFIG_NO_RC4 || CONFIG_FIPS */
3017 u8 ek[32];
3018 if (key_data_len > sizeof(gd.gtk)) {
3019 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3020 "WPA: RC4 key data too long (%lu)",
3021 (unsigned long) key_data_len);
3022 goto failed;
3023 }
3024 os_memcpy(ek, key->key_iv, 16);
3025 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
3026 os_memcpy(gd.gtk, key_data, key_data_len);
3027 if (rc4_skip(ek, 32, 256, gd.gtk, key_data_len)) {
3028 forced_memzero(ek, sizeof(ek));
3029 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
3030 "WPA: RC4 failed");
3031 goto failed;
3032 }
3033 forced_memzero(ek, sizeof(ek));
3034#endif /* CONFIG_NO_RC4 || CONFIG_FIPS */
3035 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
3036 if (maxkeylen % 8) {
3037 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3038 "WPA: Unsupported AES-WRAP len %lu",
3039 (unsigned long) maxkeylen);
3040 goto failed;
3041 }
3042 if (maxkeylen > sizeof(gd.gtk)) {
3043 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3044 "WPA: AES-WRAP key data "
3045 "too long (keydatalen=%lu maxkeylen=%lu)",
3046 (unsigned long) key_data_len,
3047 (unsigned long) maxkeylen);
3048 goto failed;
3049 }
3050 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
3051 key_data, gd.gtk)) {
3052 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3053 "WPA: AES unwrap failed - could not decrypt "
3054 "GTK");
3055 goto failed;
3056 }
3057 } else {
3058 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3059 "WPA: Unsupported key_info type %d", ver);
3060 goto failed;
3061 }
3062 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
3063 sm, !!(key_info & WPA_KEY_INFO_TXRX));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003064
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003065 key_rsc = key->key_rsc;
3066 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
3067 key_rsc = null_rsc;
3068
Jouni Malinen58c0e962017-10-01 12:12:24 +03003069 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003070 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003071 goto failed;
Hai Shalom81f62d82019-07-22 12:10:00 -07003072 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003073
3074 if (rekey) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00003075 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3076 "WPA: Group rekeying completed with " MACSTR
3077 " [GTK=%s]",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003078 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
3079 wpa_sm_cancel_auth_timeout(sm);
3080 wpa_sm_set_state(sm, WPA_COMPLETED);
3081 } else {
3082 wpa_supplicant_key_neg_complete(sm, sm->bssid,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003083 key_info & WPA_KEY_INFO_SECURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003084 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003085
3086 wpa_sm_set_rekey_offload(sm);
3087
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003088 return;
3089
3090failed:
Hai Shalom81f62d82019-07-22 12:10:00 -07003091 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003092 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
3093}
3094
3095
Sunil Ravi77d572f2023-01-17 23:58:31 +00003096static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
3097 const unsigned char *src_addr,
3098 const struct wpa_eapol_key *key,
3099 const u8 *key_data,
3100 size_t key_data_len, u16 ver)
3101{
3102 u16 key_info;
3103 struct wpa_gtk_data gd;
3104 const u8 *key_rsc;
3105 int maxkeylen;
3106 struct wpa_eapol_ie_parse ie;
3107 u16 gtk_len;
3108
3109 if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) {
3110 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3111 "RSN: Group Key Handshake started prior to completion of 4-way handshake");
3112 goto failed;
3113 }
3114
3115 os_memset(&gd, 0, sizeof(gd));
3116
3117 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3118 "RSN: RX message 1 of Group Key Handshake from " MACSTR
3119 " (ver=%d)", MAC2STR(src_addr), ver);
3120
3121 key_info = WPA_GET_BE16(key->key_info);
3122
3123 wpa_hexdump_key(MSG_DEBUG, "RSN: msg 1/2 key data",
3124 key_data, key_data_len);
3125 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
3126 goto failed;
3127
3128 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
3129
3130 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
3131 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3132 "RSN: GTK KDE in unencrypted key data");
3133 goto failed;
3134 }
3135 if (!ie.gtk) {
3136 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3137 "RSN: No GTK KDE in Group Key msg 1/2");
3138 goto failed;
3139 }
3140 gtk_len = ie.gtk_len;
3141 if (gtk_len < 2) {
3142 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3143 "RSN: Invalid GTK KDE length (%u) in Group Key msg 1/2",
3144 gtk_len);
3145 goto failed;
3146 }
3147 gtk_len -= 2;
3148 if (gtk_len > sizeof(gd.gtk)) {
3149 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3150 "RSN: Too long GTK in GTK KDE (len=%u)", gtk_len);
3151 goto failed;
3152 }
3153 maxkeylen = gd.gtk_len = gtk_len;
3154
3155#ifdef CONFIG_OCV
3156 if (wpa_sm_ocv_enabled(sm)) {
3157 struct wpa_channel_info ci;
3158
3159 if (wpa_sm_channel_info(sm, &ci) != 0) {
3160 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3161 "Failed to get channel info to validate received OCI in EAPOL-Key group msg 1/2");
3162 goto failed;
3163 }
3164
3165 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
3166 channel_width_to_int(ci.chanwidth),
3167 ci.seg1_idx) != OCI_SUCCESS) {
3168 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
3169 "addr=" MACSTR " frame=eapol-key-g1 error=%s",
3170 MAC2STR(sm->bssid), ocv_errorstr);
3171 goto failed;
3172 }
3173 }
3174#endif /* CONFIG_OCV */
3175
3176 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
3177 gtk_len, maxkeylen,
3178 &gd.key_rsc_len, &gd.alg))
3179 goto failed;
3180
3181 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
3182 ie.gtk, 2 + gtk_len);
3183 gd.keyidx = ie.gtk[0] & 0x3;
3184 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
3185 !!(ie.gtk[0] & BIT(2)));
3186 os_memcpy(gd.gtk, ie.gtk + 2, gtk_len);
3187
3188 if (ieee80211w_set_keys(sm, &ie) < 0)
3189 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3190 "RSN: Failed to configure IGTK");
3191
3192 key_rsc = key->key_rsc;
3193 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
3194 key_rsc = null_rsc;
3195
3196 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
3197 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
3198 goto failed;
3199 forced_memzero(&gd, sizeof(gd));
3200
3201 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3202 "RSN: Group rekeying completed with " MACSTR " [GTK=%s]",
3203 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
3204 wpa_sm_cancel_auth_timeout(sm);
3205 wpa_sm_set_state(sm, WPA_COMPLETED);
3206
3207 wpa_sm_set_rekey_offload(sm);
3208
3209 return;
3210
3211failed:
3212 forced_memzero(&gd, sizeof(gd));
3213 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
3214}
3215
3216
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003217static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003218 struct wpa_eapol_key *key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003219 u16 ver,
3220 const u8 *buf, size_t len)
3221{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003222 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003223 int ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003224 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003225
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003226 os_memcpy(mic, key + 1, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003227 if (sm->tptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003228 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003229 if (wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len,
3230 sm->key_mgmt,
3231 ver, buf, len, (u8 *) (key + 1)) < 0 ||
3232 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003233 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3234 "WPA: Invalid EAPOL-Key MIC "
3235 "when using TPTK - ignoring TPTK");
Hai Shalom74f70d42019-02-11 14:42:39 -08003236#ifdef TEST_FUZZ
3237 wpa_printf(MSG_INFO,
3238 "TEST: Ignore Key MIC failure for fuzz testing");
3239 goto continue_fuzz;
3240#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003241 } else {
Hai Shalom74f70d42019-02-11 14:42:39 -08003242#ifdef TEST_FUZZ
3243 continue_fuzz:
3244#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003245 ok = 1;
3246 sm->tptk_set = 0;
3247 sm->ptk_set = 1;
3248 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003249 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003250 /*
3251 * This assures the same TPTK in sm->tptk can never be
Roshan Pius3a1667e2018-07-03 15:17:14 -07003252 * copied twice to sm->ptk as the new PTK. In
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003253 * combination with the installed flag in the wpa_ptk
3254 * struct, this assures the same PTK is only installed
3255 * once.
3256 */
3257 sm->renew_snonce = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003258 }
3259 }
3260
3261 if (!ok && sm->ptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003262 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003263 if (wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len,
3264 sm->key_mgmt,
3265 ver, buf, len, (u8 *) (key + 1)) < 0 ||
3266 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003267 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3268 "WPA: Invalid EAPOL-Key MIC - "
3269 "dropping packet");
Hai Shalom74f70d42019-02-11 14:42:39 -08003270#ifdef TEST_FUZZ
3271 wpa_printf(MSG_INFO,
3272 "TEST: Ignore Key MIC failure for fuzz testing");
3273 goto continue_fuzz2;
3274#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003275 return -1;
3276 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003277#ifdef TEST_FUZZ
3278 continue_fuzz2:
3279#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003280 ok = 1;
3281 }
3282
3283 if (!ok) {
3284 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3285 "WPA: Could not verify EAPOL-Key MIC - "
3286 "dropping packet");
3287 return -1;
3288 }
3289
3290 os_memcpy(sm->rx_replay_counter, key->replay_counter,
3291 WPA_REPLAY_COUNTER_LEN);
3292 sm->rx_replay_counter_set = 1;
3293 return 0;
3294}
3295
3296
3297/* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
3298static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003299 struct wpa_eapol_key *key,
3300 size_t mic_len, u16 ver,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003301 u8 *key_data, size_t *key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003302{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003303 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003304 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003305 if (!sm->ptk_set) {
3306 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3307 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
3308 "Data");
3309 return -1;
3310 }
3311
3312 /* Decrypt key data here so that this operation does not need
3313 * to be implemented separately for each message type. */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003314 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00003315#if defined(CONFIG_NO_RC4) || defined(CONFIG_FIPS)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003316 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3317 "WPA: RC4 not supported in the build");
3318 return -1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003319#else /* CONFIG_NO_RC4 || CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003320 u8 ek[32];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003321
3322 wpa_printf(MSG_DEBUG, "WPA: Decrypt Key Data using RC4");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003323 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003324 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003325 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07003326 forced_memzero(ek, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003327 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
3328 "WPA: RC4 failed");
3329 return -1;
3330 }
Hai Shalom81f62d82019-07-22 12:10:00 -07003331 forced_memzero(ek, sizeof(ek));
Sunil Ravi77d572f2023-01-17 23:58:31 +00003332#endif /* CONFIG_NO_RC4 || CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003333 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003334 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07003335 wpa_use_aes_key_wrap(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003336 u8 *buf;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003337
3338 wpa_printf(MSG_DEBUG,
3339 "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)",
3340 (unsigned int) sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003341 if (*key_data_len < 8 || *key_data_len % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003342 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003343 "WPA: Unsupported AES-WRAP len %u",
3344 (unsigned int) *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003345 return -1;
3346 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003347 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
3348 buf = os_malloc(*key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003349 if (buf == NULL) {
3350 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3351 "WPA: No memory for AES-UNWRAP buffer");
3352 return -1;
3353 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003354#ifdef TEST_FUZZ
3355 os_memset(buf, 0x11, *key_data_len);
3356#endif /* TEST_FUZZ */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003357 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003358 key_data, buf)) {
Hai Shalom74f70d42019-02-11 14:42:39 -08003359#ifdef TEST_FUZZ
3360 wpa_printf(MSG_INFO,
3361 "TEST: Ignore AES unwrap failure for fuzz testing");
3362 goto continue_fuzz;
3363#endif /* TEST_FUZZ */
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08003364 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003365 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3366 "WPA: AES unwrap failed - "
3367 "could not decrypt EAPOL-Key key data");
3368 return -1;
3369 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003370#ifdef TEST_FUZZ
3371 continue_fuzz:
3372#endif /* TEST_FUZZ */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003373 os_memcpy(key_data, buf, *key_data_len);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08003374 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003375 WPA_PUT_BE16(((u8 *) (key + 1)) + mic_len, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003376 } else {
3377 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3378 "WPA: Unsupported key_info type %d", ver);
3379 return -1;
3380 }
3381 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003382 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003383 return 0;
3384}
3385
3386
3387/**
3388 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
3389 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3390 */
3391void wpa_sm_aborted_cached(struct wpa_sm *sm)
3392{
3393 if (sm && sm->cur_pmksa) {
3394 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3395 "RSN: Cancelling PMKSA caching attempt");
3396 sm->cur_pmksa = NULL;
3397 }
3398}
3399
3400
Hai Shalomc1a21442022-02-04 13:43:00 -08003401void wpa_sm_aborted_external_cached(struct wpa_sm *sm)
3402{
3403 if (sm && sm->cur_pmksa && sm->cur_pmksa->external) {
3404 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3405 "RSN: Cancelling external PMKSA caching attempt");
3406 sm->cur_pmksa = NULL;
3407 }
3408}
3409
3410
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003411static void wpa_eapol_key_dump(struct wpa_sm *sm,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003412 const struct wpa_eapol_key *key,
3413 unsigned int key_data_len,
3414 const u8 *mic, unsigned int mic_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003415{
3416#ifndef CONFIG_NO_STDOUT_DEBUG
3417 u16 key_info = WPA_GET_BE16(key->key_info);
3418
3419 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
3420 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3421 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
3422 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
3423 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
3424 WPA_KEY_INFO_KEY_INDEX_SHIFT,
3425 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
3426 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
3427 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
3428 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
3429 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
3430 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
3431 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
3432 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
3433 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
3434 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3435 " key_length=%u key_data_length=%u",
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003436 WPA_GET_BE16(key->key_length), key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003437 wpa_hexdump(MSG_DEBUG, " replay_counter",
3438 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
3439 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
3440 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
3441 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
3442 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003443 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003444#endif /* CONFIG_NO_STDOUT_DEBUG */
3445}
3446
3447
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003448#ifdef CONFIG_FILS
3449static int wpa_supp_aead_decrypt(struct wpa_sm *sm, u8 *buf, size_t buf_len,
3450 size_t *key_data_len)
3451{
3452 struct wpa_ptk *ptk;
3453 struct ieee802_1x_hdr *hdr;
3454 struct wpa_eapol_key *key;
3455 u8 *pos, *tmp;
3456 const u8 *aad[1];
3457 size_t aad_len[1];
3458
3459 if (*key_data_len < AES_BLOCK_SIZE) {
3460 wpa_printf(MSG_INFO, "No room for AES-SIV data in the frame");
3461 return -1;
3462 }
3463
3464 if (sm->tptk_set)
3465 ptk = &sm->tptk;
3466 else if (sm->ptk_set)
3467 ptk = &sm->ptk;
3468 else
3469 return -1;
3470
3471 hdr = (struct ieee802_1x_hdr *) buf;
3472 key = (struct wpa_eapol_key *) (hdr + 1);
3473 pos = (u8 *) (key + 1);
3474 pos += 2; /* Pointing at the Encrypted Key Data field */
3475
3476 tmp = os_malloc(*key_data_len);
3477 if (!tmp)
3478 return -1;
3479
3480 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
3481 * to Key Data (exclusive). */
3482 aad[0] = buf;
3483 aad_len[0] = pos - buf;
3484 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, *key_data_len,
3485 1, aad, aad_len, tmp) < 0) {
3486 wpa_printf(MSG_INFO, "Invalid AES-SIV data in the frame");
3487 bin_clear_free(tmp, *key_data_len);
3488 return -1;
3489 }
3490
3491 /* AEAD decryption and validation completed successfully */
3492 (*key_data_len) -= AES_BLOCK_SIZE;
3493 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
3494 tmp, *key_data_len);
3495
3496 /* Replace Key Data field with the decrypted version */
3497 os_memcpy(pos, tmp, *key_data_len);
3498 pos -= 2; /* Key Data Length field */
3499 WPA_PUT_BE16(pos, *key_data_len);
3500 bin_clear_free(tmp, *key_data_len);
3501
3502 if (sm->tptk_set) {
3503 sm->tptk_set = 0;
3504 sm->ptk_set = 1;
3505 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
3506 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
3507 }
3508
3509 os_memcpy(sm->rx_replay_counter, key->replay_counter,
3510 WPA_REPLAY_COUNTER_LEN);
3511 sm->rx_replay_counter_set = 1;
3512
3513 return 0;
3514}
3515#endif /* CONFIG_FILS */
3516
3517
Sunil Ravi77d572f2023-01-17 23:58:31 +00003518static int wpa_sm_rx_eapol_wpa(struct wpa_sm *sm, const u8 *src_addr,
3519 struct wpa_eapol_key *key,
3520 enum frame_encryption encrypted,
3521 const u8 *tmp, size_t data_len,
3522 u8 *key_data, size_t key_data_len)
3523{
3524 u16 key_info, ver;
3525
3526 key_info = WPA_GET_BE16(key->key_info);
3527
3528 if (key->type != EAPOL_KEY_TYPE_WPA) {
3529 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3530 "WPA: Unsupported EAPOL-Key type %d", key->type);
3531 return -1;
3532 }
3533
3534 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
3535 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
3536 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
3537 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3538 "WPA: Unsupported EAPOL-Key descriptor version %d",
3539 ver);
3540 return -1;
3541 }
3542
3543 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
3544 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
3545 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3546 "WPA: CCMP is used, but EAPOL-Key descriptor version (%d) is not 2",
3547 ver);
3548 if (sm->group_cipher != WPA_CIPHER_CCMP &&
3549 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
3550 /* Earlier versions of IEEE 802.11i did not explicitly
3551 * require version 2 descriptor for all EAPOL-Key
3552 * packets, so allow group keys to use version 1 if
3553 * CCMP is not used for them. */
3554 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3555 "WPA: Backwards compatibility: allow invalid version for non-CCMP group keys");
3556 } else
3557 return -1;
3558 }
3559
3560 if ((key_info & WPA_KEY_INFO_MIC) &&
3561 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
3562 return -1;
3563
3564 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
3565 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
3566 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3567 "WPA: Ignored EAPOL-Key (Pairwise) with non-zero key index");
3568 return -1;
3569 }
3570 if (key_info & (WPA_KEY_INFO_MIC |
3571 WPA_KEY_INFO_ENCR_KEY_DATA)) {
3572 /* 3/4 4-Way Handshake */
3573 wpa_supplicant_process_3_of_4_wpa(sm, key, ver,
3574 key_data,
3575 key_data_len);
3576 } else {
3577 /* 1/4 4-Way Handshake */
3578 wpa_supplicant_process_1_of_4_wpa(sm, src_addr, key,
3579 ver, key_data,
3580 key_data_len,
3581 encrypted);
3582 }
3583 } else {
3584 if (key_info & WPA_KEY_INFO_MIC) {
3585 /* 1/2 Group Key Handshake */
3586 wpa_supplicant_process_1_of_2_wpa(sm, src_addr, key,
3587 key_data,
3588 key_data_len,
3589 ver);
3590 } else {
3591 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3592 "WPA: EAPOL-Key (Group) without Mic/Encr bit - dropped");
3593 }
3594 }
3595
3596 return 1;
3597}
3598
3599
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003600/**
3601 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
3602 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3603 * @src_addr: Source MAC address of the EAPOL packet
3604 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
3605 * @len: Length of the EAPOL frame
Sunil8cd6f4d2022-06-28 18:40:46 +00003606 * @encrypted: Whether the frame was encrypted
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003607 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
3608 *
3609 * This function is called for each received EAPOL frame. Other than EAPOL-Key
3610 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
3611 * only processing WPA and WPA2 EAPOL-Key frames.
3612 *
3613 * The received EAPOL-Key packets are validated and valid packets are replied
3614 * to. In addition, key material (PTK, GTK) is configured at the end of a
3615 * successful key handshake.
3616 */
3617int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
Sunil8cd6f4d2022-06-28 18:40:46 +00003618 const u8 *buf, size_t len, enum frame_encryption encrypted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003619{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003620 size_t plen, data_len, key_data_len;
3621 const struct ieee802_1x_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003622 struct wpa_eapol_key *key;
3623 u16 key_info, ver;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003624 u8 *tmp = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003625 int ret = -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003626 u8 *mic, *key_data;
Hai Shalom899fcc72020-10-19 14:38:18 -07003627 size_t mic_len, keyhdrlen, pmk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003628
3629#ifdef CONFIG_IEEE80211R
3630 sm->ft_completed = 0;
3631#endif /* CONFIG_IEEE80211R */
3632
Hai Shalom899fcc72020-10-19 14:38:18 -07003633 pmk_len = sm->pmk_len;
3634 if (!pmk_len && sm->cur_pmksa)
3635 pmk_len = sm->cur_pmksa->pmk_len;
3636 mic_len = wpa_mic_len(sm->key_mgmt, pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003637 keyhdrlen = sizeof(*key) + mic_len + 2;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003638
3639 if (len < sizeof(*hdr) + keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003640 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3641 "WPA: EAPOL frame too short to be a WPA "
3642 "EAPOL-Key (len %lu, expecting at least %lu)",
3643 (unsigned long) len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003644 (unsigned long) sizeof(*hdr) + keyhdrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003645 return 0;
3646 }
3647
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003648 hdr = (const struct ieee802_1x_hdr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003649 plen = be_to_host16(hdr->length);
3650 data_len = plen + sizeof(*hdr);
3651 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3652 "IEEE 802.1X RX: version=%d type=%d length=%lu",
3653 hdr->version, hdr->type, (unsigned long) plen);
3654
3655 if (hdr->version < EAPOL_VERSION) {
3656 /* TODO: backwards compatibility */
3657 }
3658 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
3659 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3660 "WPA: EAPOL frame (type %u) discarded, "
3661 "not a Key frame", hdr->type);
3662 ret = 0;
3663 goto out;
3664 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003665 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003666 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003667 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3668 "WPA: EAPOL frame payload size %lu "
3669 "invalid (frame size %lu)",
3670 (unsigned long) plen, (unsigned long) len);
3671 ret = 0;
3672 goto out;
3673 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003674 if (data_len < len) {
3675 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3676 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
3677 (unsigned long) len - data_len);
3678 }
3679
3680 /*
3681 * Make a copy of the frame since we need to modify the buffer during
3682 * MAC validation and Key Data decryption.
3683 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003684 tmp = os_memdup(buf, data_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003685 if (tmp == NULL)
3686 goto out;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003687 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003688 mic = (u8 *) (key + 1);
3689 key_data = mic + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003690
3691 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
3692 {
3693 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3694 "WPA: EAPOL-Key type (%d) unknown, discarded",
3695 key->type);
3696 ret = 0;
3697 goto out;
3698 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003699
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003700 key_data_len = WPA_GET_BE16(mic + mic_len);
3701 wpa_eapol_key_dump(sm, key, key_data_len, mic, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003702
3703 if (key_data_len > plen - keyhdrlen) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003704 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
3705 "frame - key_data overflow (%u > %u)",
3706 (unsigned int) key_data_len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003707 (unsigned int) (plen - keyhdrlen));
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003708 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003709 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003710
Sunil Ravi77d572f2023-01-17 23:58:31 +00003711 if (sm->rx_replay_counter_set &&
3712 os_memcmp(key->replay_counter, sm->rx_replay_counter,
3713 WPA_REPLAY_COUNTER_LEN) <= 0) {
3714 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3715 "WPA: EAPOL-Key Replay Counter did not increase - dropping packet");
3716 goto out;
3717 }
3718
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003719 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
Sunil Ravi77d572f2023-01-17 23:58:31 +00003720
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003721 key_info = WPA_GET_BE16(key->key_info);
Sunil Ravi77d572f2023-01-17 23:58:31 +00003722
3723 if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
3724 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3725 "WPA: Unsupported SMK bit in key_info");
3726 goto out;
3727 }
3728
3729 if (!(key_info & WPA_KEY_INFO_ACK)) {
3730 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3731 "WPA: No Ack bit in key_info");
3732 goto out;
3733 }
3734
3735 if (key_info & WPA_KEY_INFO_REQUEST) {
3736 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3737 "WPA: EAPOL-Key with Request bit - dropped");
3738 goto out;
3739 }
3740
3741 if (sm->proto == WPA_PROTO_WPA) {
3742 ret = wpa_sm_rx_eapol_wpa(sm, src_addr, key, encrypted,
3743 tmp, data_len,
3744 key_data, key_data_len);
3745 goto out;
3746 }
3747
3748 if (key->type != EAPOL_KEY_TYPE_RSN) {
3749 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3750 "RSN: Unsupported EAPOL-Key type %d", key->type);
3751 goto out;
3752 }
3753
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003754 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
3755 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003756 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003757 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07003758 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003759 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003760 "RSN: Unsupported EAPOL-Key descriptor version %d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003761 ver);
3762 goto out;
3763 }
3764
Sunil Ravi77d572f2023-01-17 23:58:31 +00003765 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
3766 sm->pairwise_cipher != WPA_CIPHER_TKIP) {
3767 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3768 "RSN: EAPOL-Key descriptor version %d not allowed without TKIP as the pairwise cipher",
3769 ver);
3770 goto out;
3771 }
3772
3773 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
3774 (sm->key_mgmt != WPA_KEY_MGMT_IEEE8021X &&
3775 sm->key_mgmt != WPA_KEY_MGMT_PSK)) {
3776 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3777 "RSN: EAPOL-Key descriptor version %d not allowed due to negotiated AKM (0x%x)",
3778 ver, sm->key_mgmt);
3779 goto out;
3780 }
3781
Roshan Pius3a1667e2018-07-03 15:17:14 -07003782 if (wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003783 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
3784 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3785 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
3786 ver);
3787 goto out;
3788 }
3789
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003790#ifdef CONFIG_IEEE80211R
3791 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
3792 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
Roshan Pius3a1667e2018-07-03 15:17:14 -07003793 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
3794 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003795 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3796 "FT: AP did not use AES-128-CMAC");
3797 goto out;
3798 }
3799 } else
3800#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003801 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003802 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07003803 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003804 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003805 "RSN: AP did not use the negotiated AES-128-CMAC");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003806 goto out;
3807 }
Hai Shalomc3565922019-10-28 11:58:20 -07003808 } else if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
3809 !wpa_use_akm_defined(sm->key_mgmt) &&
3810 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003811 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003812 "RSN: CCMP is used, but EAPOL-Key descriptor version (%d) is not 2", ver);
3813 if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003814 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003815 "RSN: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
3816 } else {
Jouni Malinen658fb4a2014-11-14 20:57:05 +02003817 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003818 "RSN: Unexpected descriptor version %u", ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003819 goto out;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003820 }
Dmitry Shmidt71757432014-06-02 13:50:35 -07003821 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07003822 !wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt71757432014-06-02 13:50:35 -07003823 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003824 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003825 "RSN: GCMP is used, but EAPOL-Key descriptor version (%d) is not 2",
3826 ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003827 goto out;
3828 }
3829
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003830 if ((key_info & WPA_KEY_INFO_MIC) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003831 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003832 goto out;
3833
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003834#ifdef CONFIG_FILS
3835 if (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
3836 if (wpa_supp_aead_decrypt(sm, tmp, data_len, &key_data_len))
3837 goto out;
3838 }
3839#endif /* CONFIG_FILS */
3840
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003841 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003842 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && mic_len) {
Hai Shalomce48b4a2018-09-05 11:41:35 -07003843 /*
3844 * Only decrypt the Key Data field if the frame's authenticity
3845 * was verified. When using AES-SIV (FILS), the MIC flag is not
3846 * set, so this check should only be performed if mic_len != 0
3847 * which is the case in this code branch.
3848 */
3849 if (!(key_info & WPA_KEY_INFO_MIC)) {
3850 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3851 "WPA: Ignore EAPOL-Key with encrypted but unauthenticated data");
3852 goto out;
3853 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003854 if (wpa_supplicant_decrypt_key_data(sm, key, mic_len,
3855 ver, key_data,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003856 &key_data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003857 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003858 }
3859
3860 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
3861 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
3862 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003863 "RSN: Ignored EAPOL-Key (Pairwise) with non-zero key index");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003864 goto out;
3865 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003866 if (key_info & (WPA_KEY_INFO_MIC |
3867 WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003868 /* 3/4 4-Way Handshake */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003869 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
3870 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003871 } else {
3872 /* 1/4 4-Way Handshake */
3873 wpa_supplicant_process_1_of_4(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003874 ver, key_data,
Sunil8cd6f4d2022-06-28 18:40:46 +00003875 key_data_len,
3876 encrypted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003877 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003878 } else {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003879 if ((mic_len && (key_info & WPA_KEY_INFO_MIC)) ||
3880 (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003881 /* 1/2 Group Key Handshake */
Sunil Ravi77d572f2023-01-17 23:58:31 +00003882 if (sm->mlo.valid_links)
3883 wpa_supplicant_process_mlo_1_of_2(sm, src_addr,
3884 key, key_data,
3885 key_data_len,
3886 ver);
3887 else
3888 wpa_supplicant_process_1_of_2(sm, src_addr, key,
3889 key_data,
3890 key_data_len,
3891 ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003892 } else {
3893 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003894 "RSN: EAPOL-Key (Group) without Mic/Encr bit - dropped");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003895 }
3896 }
3897
3898 ret = 1;
3899
3900out:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003901 bin_clear_free(tmp, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003902 return ret;
3903}
3904
3905
3906#ifdef CONFIG_CTRL_IFACE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003907static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
3908{
3909 switch (sm->key_mgmt) {
3910 case WPA_KEY_MGMT_IEEE8021X:
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003911 return ((sm->proto == WPA_PROTO_RSN ||
3912 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003913 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
3914 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
3915 case WPA_KEY_MGMT_PSK:
3916 return (sm->proto == WPA_PROTO_RSN ?
3917 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
3918 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
3919#ifdef CONFIG_IEEE80211R
3920 case WPA_KEY_MGMT_FT_IEEE8021X:
3921 return RSN_AUTH_KEY_MGMT_FT_802_1X;
3922 case WPA_KEY_MGMT_FT_PSK:
3923 return RSN_AUTH_KEY_MGMT_FT_PSK;
3924#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003925 case WPA_KEY_MGMT_IEEE8021X_SHA256:
3926 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
3927 case WPA_KEY_MGMT_PSK_SHA256:
3928 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003929 case WPA_KEY_MGMT_CCKM:
3930 return (sm->proto == WPA_PROTO_RSN ?
3931 RSN_AUTH_KEY_MGMT_CCKM:
3932 WPA_AUTH_KEY_MGMT_CCKM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003933 case WPA_KEY_MGMT_WPA_NONE:
3934 return WPA_AUTH_KEY_MGMT_NONE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003935 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
3936 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003937 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
3938 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003939 case WPA_KEY_MGMT_IEEE8021X_SHA384:
3940 return RSN_AUTH_KEY_MGMT_802_1X_SHA384;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003941 default:
3942 return 0;
3943 }
3944}
3945
3946
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003947#define RSN_SUITE "%02x-%02x-%02x-%d"
3948#define RSN_SUITE_ARG(s) \
3949((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
3950
3951/**
3952 * wpa_sm_get_mib - Dump text list of MIB entries
3953 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3954 * @buf: Buffer for the list
3955 * @buflen: Length of the buffer
3956 * Returns: Number of bytes written to buffer
3957 *
3958 * This function is used fetch dot11 MIB variables.
3959 */
3960int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
3961{
3962 char pmkid_txt[PMKID_LEN * 2 + 1];
Hai Shalome21d4e82020-04-29 16:34:06 -07003963 bool rsna;
3964 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003965 size_t len;
3966
3967 if (sm->cur_pmksa) {
3968 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
3969 sm->cur_pmksa->pmkid, PMKID_LEN);
3970 } else
3971 pmkid_txt[0] = '\0';
3972
Hai Shalome21d4e82020-04-29 16:34:06 -07003973 rsna = (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
3974 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
3975 sm->proto == WPA_PROTO_RSN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003976
3977 ret = os_snprintf(buf, buflen,
3978 "dot11RSNAOptionImplemented=TRUE\n"
3979 "dot11RSNAPreauthenticationImplemented=TRUE\n"
3980 "dot11RSNAEnabled=%s\n"
3981 "dot11RSNAPreauthenticationEnabled=%s\n"
3982 "dot11RSNAConfigVersion=%d\n"
3983 "dot11RSNAConfigPairwiseKeysSupported=5\n"
3984 "dot11RSNAConfigGroupCipherSize=%d\n"
3985 "dot11RSNAConfigPMKLifetime=%d\n"
3986 "dot11RSNAConfigPMKReauthThreshold=%d\n"
3987 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
3988 "dot11RSNAConfigSATimeout=%d\n",
3989 rsna ? "TRUE" : "FALSE",
3990 rsna ? "TRUE" : "FALSE",
3991 RSN_VERSION,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003992 wpa_cipher_key_len(sm->group_cipher) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003993 sm->dot11RSNAConfigPMKLifetime,
3994 sm->dot11RSNAConfigPMKReauthThreshold,
3995 sm->dot11RSNAConfigSATimeout);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003996 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003997 return 0;
3998 len = ret;
3999
4000 ret = os_snprintf(
4001 buf + len, buflen - len,
4002 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
4003 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
4004 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
4005 "dot11RSNAPMKIDUsed=%s\n"
4006 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
4007 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
4008 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
4009 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
4010 "dot11RSNA4WayHandshakeFailures=%u\n",
4011 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004012 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
4013 sm->pairwise_cipher)),
4014 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
4015 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004016 pmkid_txt,
4017 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004018 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
4019 sm->pairwise_cipher)),
4020 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
4021 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004022 sm->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004023 if (!os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004024 len += ret;
4025
4026 return (int) len;
4027}
4028#endif /* CONFIG_CTRL_IFACE */
4029
4030
4031static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004032 void *ctx, enum pmksa_free_reason reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004033{
4034 struct wpa_sm *sm = ctx;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004035 int deauth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004036
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004037 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
4038 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
4039
4040 if (sm->cur_pmksa == entry) {
4041 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4042 "RSN: %s current PMKSA entry",
4043 reason == PMKSA_REPLACE ? "replaced" : "removed");
4044 pmksa_cache_clear_current(sm);
4045
4046 /*
4047 * If an entry is simply being replaced, there's no need to
4048 * deauthenticate because it will be immediately re-added.
4049 * This happens when EAP authentication is completed again
4050 * (reauth or failed PMKSA caching attempt).
4051 */
4052 if (reason != PMKSA_REPLACE)
4053 deauth = 1;
4054 }
4055
4056 if (reason == PMKSA_EXPIRE &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004057 (sm->pmk_len == entry->pmk_len &&
4058 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
4059 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004060 "RSN: deauthenticating due to expired PMK");
4061 pmksa_cache_clear_current(sm);
4062 deauth = 1;
4063 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004064
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004065 if (deauth) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07004066 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004067 os_memset(sm->pmk, 0, sizeof(sm->pmk));
4068 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
4069 }
4070}
4071
4072
Hai Shalomc1a21442022-02-04 13:43:00 -08004073static bool wpa_sm_pmksa_is_current_cb(struct rsn_pmksa_cache_entry *entry,
4074 void *ctx)
4075{
4076 struct wpa_sm *sm = ctx;
4077
4078 return sm->cur_pmksa == entry;
4079}
4080
4081
Sunil Ravi77d572f2023-01-17 23:58:31 +00004082static void wpa_sm_pmksa_notify_cb(struct rsn_pmksa_cache_entry *entry,
4083 void *ctx)
4084{
4085 struct wpa_sm *sm = ctx;
4086
4087 wpa_sm_notify_pmksa_cache_entry(sm, entry);
4088}
4089
4090
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004091/**
4092 * wpa_sm_init - Initialize WPA state machine
4093 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
4094 * Returns: Pointer to the allocated WPA state machine data
4095 *
4096 * This function is used to allocate a new WPA state machine and the returned
4097 * value is passed to all WPA state machine calls.
4098 */
4099struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
4100{
4101 struct wpa_sm *sm;
4102
4103 sm = os_zalloc(sizeof(*sm));
4104 if (sm == NULL)
4105 return NULL;
4106 dl_list_init(&sm->pmksa_candidates);
4107 sm->renew_snonce = 1;
4108 sm->ctx = ctx;
4109
4110 sm->dot11RSNAConfigPMKLifetime = 43200;
4111 sm->dot11RSNAConfigPMKReauthThreshold = 70;
4112 sm->dot11RSNAConfigSATimeout = 60;
4113
Hai Shalomc1a21442022-02-04 13:43:00 -08004114 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb,
Sunil Ravi77d572f2023-01-17 23:58:31 +00004115 wpa_sm_pmksa_is_current_cb,
4116 wpa_sm_pmksa_notify_cb, sm, sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004117 if (sm->pmksa == NULL) {
4118 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
4119 "RSN: PMKSA cache initialization failed");
4120 os_free(sm);
4121 return NULL;
4122 }
4123
4124 return sm;
4125}
4126
4127
4128/**
4129 * wpa_sm_deinit - Deinitialize WPA state machine
4130 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4131 */
4132void wpa_sm_deinit(struct wpa_sm *sm)
4133{
Sunil Ravi77d572f2023-01-17 23:58:31 +00004134 int i;
4135
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004136 if (sm == NULL)
4137 return;
4138 pmksa_cache_deinit(sm->pmksa);
4139 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
4140 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
4141 os_free(sm->assoc_wpa_ie);
Hai Shalomc3565922019-10-28 11:58:20 -07004142 os_free(sm->assoc_rsnxe);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004143 os_free(sm->ap_wpa_ie);
4144 os_free(sm->ap_rsn_ie);
Hai Shalomc3565922019-10-28 11:58:20 -07004145 os_free(sm->ap_rsnxe);
Sunil Ravi77d572f2023-01-17 23:58:31 +00004146 for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
4147 os_free(sm->mlo.links[i].ap_rsne);
4148 os_free(sm->mlo.links[i].ap_rsnxe);
4149 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004150 wpa_sm_drop_sa(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004151 os_free(sm->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004152#ifdef CONFIG_IEEE80211R
4153 os_free(sm->assoc_resp_ies);
4154#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08004155#ifdef CONFIG_TESTING_OPTIONS
4156 wpabuf_free(sm->test_assoc_ie);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004157 wpabuf_free(sm->test_eapol_m2_elems);
4158 wpabuf_free(sm->test_eapol_m4_elems);
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08004159#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004160#ifdef CONFIG_FILS_SK_PFS
4161 crypto_ecdh_deinit(sm->fils_ecdh);
4162#endif /* CONFIG_FILS_SK_PFS */
4163#ifdef CONFIG_FILS
4164 wpabuf_free(sm->fils_ft_ies);
4165#endif /* CONFIG_FILS */
4166#ifdef CONFIG_OWE
4167 crypto_ecdh_deinit(sm->owe_ecdh);
4168#endif /* CONFIG_OWE */
Hai Shalom021b0b52019-04-10 11:17:58 -07004169#ifdef CONFIG_DPP2
4170 wpabuf_clear_free(sm->dpp_z);
4171#endif /* CONFIG_DPP2 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004172 os_free(sm);
4173}
4174
4175
Sunil Ravi77d572f2023-01-17 23:58:31 +00004176static void wpa_sm_clear_ptk(struct wpa_sm *sm)
4177{
4178 int i;
4179
4180 sm->ptk_set = 0;
4181 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
4182 sm->tptk_set = 0;
4183 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
4184 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
4185 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
4186 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
4187 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004188 os_memset(&sm->bigtk, 0, sizeof(sm->bigtk));
4189 os_memset(&sm->bigtk_wnm_sleep, 0, sizeof(sm->bigtk_wnm_sleep));
Sunil Ravi77d572f2023-01-17 23:58:31 +00004190 sm->tk_set = false;
4191 for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
4192 os_memset(&sm->mlo.links[i].gtk, 0,
4193 sizeof(sm->mlo.links[i].gtk));
4194 os_memset(&sm->mlo.links[i].gtk_wnm_sleep, 0,
4195 sizeof(sm->mlo.links[i].gtk_wnm_sleep));
4196 os_memset(&sm->mlo.links[i].igtk, 0,
4197 sizeof(sm->mlo.links[i].igtk));
4198 os_memset(&sm->mlo.links[i].igtk_wnm_sleep, 0,
4199 sizeof(sm->mlo.links[i].igtk_wnm_sleep));
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004200 os_memset(&sm->mlo.links[i].bigtk, 0,
4201 sizeof(sm->mlo.links[i].bigtk));
4202 os_memset(&sm->mlo.links[i].bigtk_wnm_sleep, 0,
4203 sizeof(sm->mlo.links[i].bigtk_wnm_sleep));
Sunil Ravi77d572f2023-01-17 23:58:31 +00004204 }
4205}
4206
4207
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004208/**
4209 * wpa_sm_notify_assoc - Notify WPA state machine about association
4210 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4211 * @bssid: The BSSID of the new association
4212 *
4213 * This function is called to let WPA state machine know that the connection
4214 * was established.
4215 */
4216void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
4217{
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02004218 int clear_keys = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004219
4220 if (sm == NULL)
4221 return;
4222
4223 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4224 "WPA: Association event - clear replay counter");
4225 os_memcpy(sm->bssid, bssid, ETH_ALEN);
4226 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
4227 sm->rx_replay_counter_set = 0;
4228 sm->renew_snonce = 1;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004229 if (ether_addr_equal(sm->preauth_bssid, bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004230 rsn_preauth_deinit(sm);
4231
4232#ifdef CONFIG_IEEE80211R
4233 if (wpa_ft_is_completed(sm)) {
4234 /*
4235 * Clear portValid to kick EAPOL state machine to re-enter
4236 * AUTHENTICATED state to get the EAPOL port Authorized.
4237 */
Hai Shalome21d4e82020-04-29 16:34:06 -07004238 eapol_sm_notify_portValid(sm->eapol, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004239 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
4240
4241 /* Prepare for the next transition */
4242 wpa_ft_prepare_auth_request(sm, NULL);
4243
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02004244 clear_keys = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07004245 sm->ft_protocol = 1;
4246 } else {
4247 sm->ft_protocol = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004248 }
4249#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004250#ifdef CONFIG_FILS
4251 if (sm->fils_completed) {
4252 /*
4253 * Clear portValid to kick EAPOL state machine to re-enter
4254 * AUTHENTICATED state to get the EAPOL port Authorized.
4255 */
4256 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02004257 clear_keys = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004258 }
4259#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004260
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02004261 if (clear_keys) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004262 /*
4263 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
4264 * this is not part of a Fast BSS Transition.
4265 */
4266 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
Sunil Ravi77d572f2023-01-17 23:58:31 +00004267 wpa_sm_clear_ptk(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004268 }
4269
4270#ifdef CONFIG_TDLS
4271 wpa_tdls_assoc(sm);
4272#endif /* CONFIG_TDLS */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004273
4274#ifdef CONFIG_P2P
4275 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
4276#endif /* CONFIG_P2P */
Hai Shalomfdcde762020-04-02 11:19:20 -07004277
4278 sm->keyidx_active = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004279}
4280
4281
4282/**
4283 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
4284 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4285 *
4286 * This function is called to let WPA state machine know that the connection
4287 * was lost. This will abort any existing pre-authentication session.
4288 */
4289void wpa_sm_notify_disassoc(struct wpa_sm *sm)
4290{
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07004291 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
4292 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004293 rsn_preauth_deinit(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004294 pmksa_cache_clear_current(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004295 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
4296 sm->dot11RSNA4WayHandshakeFailures++;
4297#ifdef CONFIG_TDLS
4298 wpa_tdls_disassoc(sm);
4299#endif /* CONFIG_TDLS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004300#ifdef CONFIG_FILS
4301 sm->fils_completed = 0;
4302#endif /* CONFIG_FILS */
Jouni Malinen4283f9e2017-09-22 12:06:37 +03004303#ifdef CONFIG_IEEE80211R
4304 sm->ft_reassoc_completed = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07004305 sm->ft_protocol = 0;
Jouni Malinen4283f9e2017-09-22 12:06:37 +03004306#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004307
4308 /* Keys are not needed in the WPA state machine anymore */
4309 wpa_sm_drop_sa(sm);
Hai Shalomfdcde762020-04-02 11:19:20 -07004310 sm->keyidx_active = 0;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07004311
4312 sm->msg_3_of_4_ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004313 os_memset(sm->bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004314}
4315
4316
4317/**
4318 * wpa_sm_set_pmk - Set PMK
4319 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4320 * @pmk: The new PMK
4321 * @pmk_len: The length of the new PMK in bytes
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004322 * @pmkid: Calculated PMKID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004323 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004324 *
4325 * Configure the PMK for WPA state machine.
4326 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004327void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004328 const u8 *pmkid, const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004329{
4330 if (sm == NULL)
4331 return;
4332
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004333 wpa_hexdump_key(MSG_DEBUG, "WPA: Set PMK based on external data",
4334 pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004335 sm->pmk_len = pmk_len;
4336 os_memcpy(sm->pmk, pmk, pmk_len);
4337
4338#ifdef CONFIG_IEEE80211R
4339 /* Set XXKey to be PSK for FT key derivation */
4340 sm->xxkey_len = pmk_len;
4341 os_memcpy(sm->xxkey, pmk, pmk_len);
4342#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004343
4344 if (bssid) {
Hai Shalomc1a21442022-02-04 13:43:00 -08004345 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len,
4346 pmkid, NULL, 0, bssid,
4347 sm->own_addr,
4348 sm->network_ctx, sm->key_mgmt,
4349 NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004350 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004351}
4352
4353
4354/**
4355 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
4356 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4357 *
4358 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
4359 * will be cleared.
4360 */
4361void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
4362{
4363 if (sm == NULL)
4364 return;
4365
4366 if (sm->cur_pmksa) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004367 wpa_hexdump_key(MSG_DEBUG,
4368 "WPA: Set PMK based on current PMKSA",
4369 sm->cur_pmksa->pmk, sm->cur_pmksa->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004370 sm->pmk_len = sm->cur_pmksa->pmk_len;
4371 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
4372 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004373 wpa_printf(MSG_DEBUG, "WPA: No current PMKSA - clear PMK");
4374 sm->pmk_len = 0;
4375 os_memset(sm->pmk, 0, PMK_LEN_MAX);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004376 }
4377}
4378
4379
4380/**
4381 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
4382 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4383 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
4384 */
4385void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
4386{
4387 if (sm)
4388 sm->fast_reauth = fast_reauth;
4389}
4390
4391
4392/**
4393 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
4394 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4395 * @scard_ctx: Context pointer for smartcard related callback functions
4396 */
4397void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
4398{
4399 if (sm == NULL)
4400 return;
4401 sm->scard_ctx = scard_ctx;
4402 if (sm->preauth_eapol)
4403 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
4404}
4405
4406
4407/**
Hai Shalomfdcde762020-04-02 11:19:20 -07004408 * wpa_sm_set_config - Notification of current configuration change
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004409 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4410 * @config: Pointer to current network configuration
4411 *
4412 * Notify WPA state machine that configuration has changed. config will be
4413 * stored as a backpointer to network configuration. This can be %NULL to clear
4414 * the stored pointed.
4415 */
4416void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
4417{
4418 if (!sm)
4419 return;
4420
4421 if (config) {
4422 sm->network_ctx = config->network_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004423 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
4424 sm->proactive_key_caching = config->proactive_key_caching;
4425 sm->eap_workaround = config->eap_workaround;
4426 sm->eap_conf_ctx = config->eap_conf_ctx;
4427 if (config->ssid) {
4428 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
4429 sm->ssid_len = config->ssid_len;
4430 } else
4431 sm->ssid_len = 0;
4432 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004433 sm->p2p = config->p2p;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004434 sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
Hai Shalomfdcde762020-04-02 11:19:20 -07004435 sm->owe_ptk_workaround = config->owe_ptk_workaround;
Hai Shalom60840252021-02-19 19:02:11 -08004436 sm->force_kdk_derivation = config->force_kdk_derivation;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004437#ifdef CONFIG_FILS
4438 if (config->fils_cache_id) {
4439 sm->fils_cache_id_set = 1;
4440 os_memcpy(sm->fils_cache_id, config->fils_cache_id,
4441 FILS_CACHE_ID_LEN);
4442 } else {
4443 sm->fils_cache_id_set = 0;
4444 }
4445#endif /* CONFIG_FILS */
Hai Shalomfdcde762020-04-02 11:19:20 -07004446 sm->beacon_prot = config->beacon_prot;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004447 } else {
4448 sm->network_ctx = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004449 sm->allowed_pairwise_cipher = 0;
4450 sm->proactive_key_caching = 0;
4451 sm->eap_workaround = 0;
4452 sm->eap_conf_ctx = NULL;
4453 sm->ssid_len = 0;
4454 sm->wpa_ptk_rekey = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004455 sm->p2p = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004456 sm->wpa_rsc_relaxation = 0;
Hai Shalomfdcde762020-04-02 11:19:20 -07004457 sm->owe_ptk_workaround = 0;
4458 sm->beacon_prot = 0;
Hai Shalom60840252021-02-19 19:02:11 -08004459 sm->force_kdk_derivation = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004460 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004461}
4462
4463
Sunil Ravi77d572f2023-01-17 23:58:31 +00004464int wpa_sm_set_mlo_params(struct wpa_sm *sm, const struct wpa_sm_mlo *mlo)
4465{
4466 int i;
4467
4468 if (!sm)
4469 return -1;
4470
4471 os_memcpy(sm->mlo.ap_mld_addr, mlo->ap_mld_addr, ETH_ALEN);
4472 sm->mlo.assoc_link_id = mlo->assoc_link_id;
4473 sm->mlo.valid_links = mlo->valid_links;
4474 sm->mlo.req_links = mlo->req_links;
4475
4476 for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
4477 const u8 *ie;
4478 size_t len;
4479
4480 if (sm->mlo.req_links & BIT(i)) {
4481 if (!mlo->links[i].ap_rsne ||
4482 mlo->links[i].ap_rsne_len == 0) {
4483 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO,
4484 "RSN: No RSNE for AP MLO link %d with BSSID "
4485 MACSTR,
4486 i, MAC2STR(mlo->links[i].bssid));
4487 return -1;
4488
4489 }
4490 os_memcpy(sm->mlo.links[i].addr, mlo->links[i].addr,
4491 ETH_ALEN);
4492 os_memcpy(sm->mlo.links[i].bssid, mlo->links[i].bssid,
4493 ETH_ALEN);
4494 }
4495
4496 ie = mlo->links[i].ap_rsne;
4497 len = mlo->links[i].ap_rsne_len;
4498 os_free(sm->mlo.links[i].ap_rsne);
4499 if (!ie || len == 0) {
4500 if (sm->mlo.links[i].ap_rsne)
4501 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4502 "RSN: Clearing MLO link[%u] AP RSNE",
4503 i);
4504 sm->mlo.links[i].ap_rsne = NULL;
4505 sm->mlo.links[i].ap_rsne_len = 0;
4506 } else {
4507 wpa_hexdump_link(MSG_DEBUG, i, "RSN: Set AP RSNE",
4508 ie, len);
4509 sm->mlo.links[i].ap_rsne = os_memdup(ie, len);
4510 if (!sm->mlo.links[i].ap_rsne) {
4511 sm->mlo.links[i].ap_rsne_len = 0;
4512 return -1;
4513 }
4514 sm->mlo.links[i].ap_rsne_len = len;
4515 }
4516
4517 ie = mlo->links[i].ap_rsnxe;
4518 len = mlo->links[i].ap_rsnxe_len;
4519 os_free(sm->mlo.links[i].ap_rsnxe);
4520 if (!ie || len == 0) {
4521 if (sm->mlo.links[i].ap_rsnxe)
4522 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4523 "RSN: Clearing MLO link[%u] AP RSNXE",
4524 i);
4525 sm->mlo.links[i].ap_rsnxe = NULL;
4526 sm->mlo.links[i].ap_rsnxe_len = 0;
4527 } else {
4528 wpa_hexdump_link(MSG_DEBUG, i, "RSN: Set AP RSNXE", ie,
4529 len);
4530 sm->mlo.links[i].ap_rsnxe = os_memdup(ie, len);
4531 if (!sm->mlo.links[i].ap_rsnxe) {
4532 sm->mlo.links[i].ap_rsnxe_len = 0;
4533 return -1;
4534 }
4535 sm->mlo.links[i].ap_rsnxe_len = len;
4536 }
4537 }
4538
4539 return 0;
4540}
4541
4542
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004543/**
4544 * wpa_sm_set_own_addr - Set own MAC address
4545 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4546 * @addr: Own MAC address
4547 */
4548void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
4549{
4550 if (sm)
4551 os_memcpy(sm->own_addr, addr, ETH_ALEN);
4552}
4553
4554
4555/**
4556 * wpa_sm_set_ifname - Set network interface name
4557 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4558 * @ifname: Interface name
4559 * @bridge_ifname: Optional bridge interface name (for pre-auth)
4560 */
4561void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
4562 const char *bridge_ifname)
4563{
4564 if (sm) {
4565 sm->ifname = ifname;
4566 sm->bridge_ifname = bridge_ifname;
4567 }
4568}
4569
4570
4571/**
4572 * wpa_sm_set_eapol - Set EAPOL state machine pointer
4573 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4574 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
4575 */
4576void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
4577{
4578 if (sm)
4579 sm->eapol = eapol;
4580}
4581
4582
4583/**
4584 * wpa_sm_set_param - Set WPA state machine parameters
4585 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4586 * @param: Parameter field
4587 * @value: Parameter value
4588 * Returns: 0 on success, -1 on failure
4589 */
4590int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
4591 unsigned int value)
4592{
4593 int ret = 0;
4594
4595 if (sm == NULL)
4596 return -1;
4597
4598 switch (param) {
4599 case RSNA_PMK_LIFETIME:
4600 if (value > 0)
4601 sm->dot11RSNAConfigPMKLifetime = value;
4602 else
4603 ret = -1;
4604 break;
4605 case RSNA_PMK_REAUTH_THRESHOLD:
4606 if (value > 0 && value <= 100)
4607 sm->dot11RSNAConfigPMKReauthThreshold = value;
4608 else
4609 ret = -1;
4610 break;
4611 case RSNA_SA_TIMEOUT:
4612 if (value > 0)
4613 sm->dot11RSNAConfigSATimeout = value;
4614 else
4615 ret = -1;
4616 break;
4617 case WPA_PARAM_PROTO:
4618 sm->proto = value;
4619 break;
4620 case WPA_PARAM_PAIRWISE:
4621 sm->pairwise_cipher = value;
4622 break;
4623 case WPA_PARAM_GROUP:
4624 sm->group_cipher = value;
4625 break;
4626 case WPA_PARAM_KEY_MGMT:
4627 sm->key_mgmt = value;
4628 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004629 case WPA_PARAM_MGMT_GROUP:
4630 sm->mgmt_group_cipher = value;
4631 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004632 case WPA_PARAM_RSN_ENABLED:
4633 sm->rsn_enabled = value;
4634 break;
4635 case WPA_PARAM_MFP:
4636 sm->mfp = value;
4637 break;
Hai Shalom74f70d42019-02-11 14:42:39 -08004638 case WPA_PARAM_OCV:
4639 sm->ocv = value;
4640 break;
Hai Shalomc3565922019-10-28 11:58:20 -07004641 case WPA_PARAM_SAE_PWE:
4642 sm->sae_pwe = value;
4643 break;
Hai Shalom899fcc72020-10-19 14:38:18 -07004644 case WPA_PARAM_SAE_PK:
4645 sm->sae_pk = value;
4646 break;
Hai Shalomfdcde762020-04-02 11:19:20 -07004647 case WPA_PARAM_DENY_PTK0_REKEY:
4648 sm->wpa_deny_ptk0_rekey = value;
4649 break;
4650 case WPA_PARAM_EXT_KEY_ID:
4651 sm->ext_key_id = value;
4652 break;
4653 case WPA_PARAM_USE_EXT_KEY_ID:
4654 sm->use_ext_key_id = value;
4655 break;
Hai Shalomb755a2a2020-04-23 21:49:02 -07004656#ifdef CONFIG_TESTING_OPTIONS
4657 case WPA_PARAM_FT_RSNXE_USED:
4658 sm->ft_rsnxe_used = value;
4659 break;
Hai Shalom899fcc72020-10-19 14:38:18 -07004660 case WPA_PARAM_OCI_FREQ_EAPOL:
4661 sm->oci_freq_override_eapol = value;
4662 break;
4663 case WPA_PARAM_OCI_FREQ_EAPOL_G2:
4664 sm->oci_freq_override_eapol_g2 = value;
4665 break;
4666 case WPA_PARAM_OCI_FREQ_FT_ASSOC:
4667 sm->oci_freq_override_ft_assoc = value;
4668 break;
4669 case WPA_PARAM_OCI_FREQ_FILS_ASSOC:
4670 sm->oci_freq_override_fils_assoc = value;
4671 break;
Sunil Ravia04bd252022-05-02 22:54:18 -07004672 case WPA_PARAM_DISABLE_EAPOL_G2_TX:
4673 sm->disable_eapol_g2_tx = value;
4674 break;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004675 case WPA_PARAM_ENCRYPT_EAPOL_M2:
4676 sm->encrypt_eapol_m2 = value;
4677 break;
4678 case WPA_PARAM_ENCRYPT_EAPOL_M4:
4679 sm->encrypt_eapol_m4 = value;
4680 break;
Hai Shalomb755a2a2020-04-23 21:49:02 -07004681#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004682#ifdef CONFIG_DPP2
4683 case WPA_PARAM_DPP_PFS:
4684 sm->dpp_pfs = value;
4685 break;
4686#endif /* CONFIG_DPP2 */
Sunil Ravi640215c2023-06-28 23:08:09 +00004687 case WPA_PARAM_WMM_ENABLED:
4688 sm->wmm_enabled = value;
4689 break;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004690 case WPA_PARAM_FT_PREPEND_PMKID:
4691 sm->ft_prepend_pmkid = value;
4692 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004693 default:
4694 break;
4695 }
4696
4697 return ret;
4698}
4699
4700
4701/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004702 * wpa_sm_get_status - Get WPA state machine
4703 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4704 * @buf: Buffer for status information
4705 * @buflen: Maximum buffer length
4706 * @verbose: Whether to include verbose status information
4707 * Returns: Number of bytes written to buf.
4708 *
4709 * Query WPA state machine for status information. This function fills in
4710 * a text area with current status information. If the buffer (buf) is not
4711 * large enough, status information will be truncated to fit the buffer.
4712 */
4713int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
4714 int verbose)
4715{
4716 char *pos = buf, *end = buf + buflen;
4717 int ret;
4718
4719 ret = os_snprintf(pos, end - pos,
4720 "pairwise_cipher=%s\n"
4721 "group_cipher=%s\n"
4722 "key_mgmt=%s\n",
4723 wpa_cipher_txt(sm->pairwise_cipher),
4724 wpa_cipher_txt(sm->group_cipher),
4725 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004726 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004727 return pos - buf;
4728 pos += ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004729
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004730#ifdef CONFIG_DPP2
4731 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
4732 ret = os_snprintf(pos, end - pos, "dpp_pfs=1\n");
4733 if (os_snprintf_error(end - pos, ret))
4734 return pos - buf;
4735 pos += ret;
4736 }
4737#endif /* CONFIG_DPP2 */
4738
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004739 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
4740 struct wpa_ie_data rsn;
4741 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
4742 >= 0 &&
4743 rsn.capabilities & (WPA_CAPABILITY_MFPR |
4744 WPA_CAPABILITY_MFPC)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004745 ret = os_snprintf(pos, end - pos, "pmf=%d\n"
4746 "mgmt_group_cipher=%s\n",
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004747 (rsn.capabilities &
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004748 WPA_CAPABILITY_MFPR) ? 2 : 1,
4749 wpa_cipher_txt(
4750 sm->mgmt_group_cipher));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004751 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004752 return pos - buf;
4753 pos += ret;
4754 }
4755 }
4756
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004757 return pos - buf;
4758}
4759
4760
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004761int wpa_sm_pmf_enabled(struct wpa_sm *sm)
4762{
4763 struct wpa_ie_data rsn;
4764
4765 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
4766 return 0;
4767
4768 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
4769 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
4770 return 1;
4771
4772 return 0;
4773}
4774
4775
Hai Shalomfdcde762020-04-02 11:19:20 -07004776int wpa_sm_ext_key_id(struct wpa_sm *sm)
4777{
4778 return sm ? sm->ext_key_id : 0;
4779}
4780
4781
4782int wpa_sm_ext_key_id_active(struct wpa_sm *sm)
4783{
4784 return sm ? sm->use_ext_key_id : 0;
4785}
4786
4787
Hai Shalom74f70d42019-02-11 14:42:39 -08004788int wpa_sm_ocv_enabled(struct wpa_sm *sm)
4789{
4790 struct wpa_ie_data rsn;
4791
4792 if (!sm->ocv || !sm->ap_rsn_ie)
4793 return 0;
4794
4795 return wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len,
4796 &rsn) >= 0 &&
4797 (rsn.capabilities & WPA_CAPABILITY_OCVC);
4798}
4799
4800
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004801/**
4802 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
4803 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4804 * @wpa_ie: Pointer to buffer for WPA/RSN IE
4805 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
4806 * Returns: 0 on success, -1 on failure
4807 */
4808int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
4809 size_t *wpa_ie_len)
4810{
4811 int res;
4812
4813 if (sm == NULL)
4814 return -1;
4815
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08004816#ifdef CONFIG_TESTING_OPTIONS
4817 if (sm->test_assoc_ie) {
4818 wpa_printf(MSG_DEBUG,
4819 "TESTING: Replace association WPA/RSN IE");
4820 if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
4821 return -1;
4822 os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
4823 wpabuf_len(sm->test_assoc_ie));
4824 res = wpabuf_len(sm->test_assoc_ie);
4825 } else
4826#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004827 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
4828 if (res < 0)
4829 return -1;
4830 *wpa_ie_len = res;
4831
4832 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
4833 wpa_ie, *wpa_ie_len);
4834
4835 if (sm->assoc_wpa_ie == NULL) {
4836 /*
4837 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
4838 * the correct version of the IE even if PMKSA caching is
4839 * aborted (which would remove PMKID from IE generation).
4840 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004841 sm->assoc_wpa_ie = os_memdup(wpa_ie, *wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004842 if (sm->assoc_wpa_ie == NULL)
4843 return -1;
4844
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004845 sm->assoc_wpa_ie_len = *wpa_ie_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004846 } else {
4847 wpa_hexdump(MSG_DEBUG,
4848 "WPA: Leave previously set WPA IE default",
4849 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004850 }
4851
4852 return 0;
4853}
4854
4855
4856/**
4857 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
4858 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4859 * @ie: Pointer to IE data (starting from id)
4860 * @len: IE length
4861 * Returns: 0 on success, -1 on failure
4862 *
4863 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
4864 * Request frame. The IE will be used to override the default value generated
4865 * with wpa_sm_set_assoc_wpa_ie_default().
4866 */
4867int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
4868{
4869 if (sm == NULL)
4870 return -1;
4871
4872 os_free(sm->assoc_wpa_ie);
4873 if (ie == NULL || len == 0) {
4874 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4875 "WPA: clearing own WPA/RSN IE");
4876 sm->assoc_wpa_ie = NULL;
4877 sm->assoc_wpa_ie_len = 0;
4878 } else {
4879 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004880 sm->assoc_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004881 if (sm->assoc_wpa_ie == NULL)
4882 return -1;
4883
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004884 sm->assoc_wpa_ie_len = len;
4885 }
4886
4887 return 0;
4888}
4889
4890
4891/**
Hai Shalomc3565922019-10-28 11:58:20 -07004892 * wpa_sm_set_assoc_rsnxe_default - Generate own RSNXE from configuration
4893 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4894 * @rsnxe: Pointer to buffer for RSNXE
4895 * @rsnxe_len: Pointer to the length of the rsne buffer
4896 * Returns: 0 on success, -1 on failure
4897 */
4898int wpa_sm_set_assoc_rsnxe_default(struct wpa_sm *sm, u8 *rsnxe,
4899 size_t *rsnxe_len)
4900{
4901 int res;
4902
4903 if (!sm)
4904 return -1;
4905
4906 res = wpa_gen_rsnxe(sm, rsnxe, *rsnxe_len);
4907 if (res < 0)
4908 return -1;
4909 *rsnxe_len = res;
4910
4911 wpa_hexdump(MSG_DEBUG, "RSN: Set own RSNXE default", rsnxe, *rsnxe_len);
4912
4913 if (sm->assoc_rsnxe) {
4914 wpa_hexdump(MSG_DEBUG,
4915 "RSN: Leave previously set RSNXE default",
4916 sm->assoc_rsnxe, sm->assoc_rsnxe_len);
4917 } else if (*rsnxe_len > 0) {
4918 /*
4919 * Make a copy of the RSNXE so that 4-Way Handshake gets the
4920 * correct version of the IE even if it gets changed.
4921 */
4922 sm->assoc_rsnxe = os_memdup(rsnxe, *rsnxe_len);
4923 if (!sm->assoc_rsnxe)
4924 return -1;
4925
4926 sm->assoc_rsnxe_len = *rsnxe_len;
4927 }
4928
4929 return 0;
4930}
4931
4932
4933/**
4934 * wpa_sm_set_assoc_rsnxe - Set own RSNXE from (Re)AssocReq
4935 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4936 * @ie: Pointer to IE data (starting from id)
4937 * @len: IE length
4938 * Returns: 0 on success, -1 on failure
4939 *
4940 * Inform WPA state machine about the RSNXE used in (Re)Association Request
4941 * frame. The IE will be used to override the default value generated
4942 * with wpa_sm_set_assoc_rsnxe_default().
4943 */
4944int wpa_sm_set_assoc_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len)
4945{
4946 if (!sm)
4947 return -1;
4948
4949 os_free(sm->assoc_rsnxe);
4950 if (!ie || len == 0) {
4951 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4952 "RSN: clearing own RSNXE");
4953 sm->assoc_rsnxe = NULL;
4954 sm->assoc_rsnxe_len = 0;
4955 } else {
4956 wpa_hexdump(MSG_DEBUG, "RSN: set own RSNXE", ie, len);
4957 sm->assoc_rsnxe = os_memdup(ie, len);
4958 if (!sm->assoc_rsnxe)
4959 return -1;
4960
4961 sm->assoc_rsnxe_len = len;
4962 }
4963
4964 return 0;
4965}
4966
4967
4968/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004969 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
4970 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4971 * @ie: Pointer to IE data (starting from id)
4972 * @len: IE length
4973 * Returns: 0 on success, -1 on failure
4974 *
4975 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
4976 * frame.
4977 */
4978int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
4979{
4980 if (sm == NULL)
4981 return -1;
4982
4983 os_free(sm->ap_wpa_ie);
4984 if (ie == NULL || len == 0) {
4985 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4986 "WPA: clearing AP WPA IE");
4987 sm->ap_wpa_ie = NULL;
4988 sm->ap_wpa_ie_len = 0;
4989 } else {
4990 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004991 sm->ap_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004992 if (sm->ap_wpa_ie == NULL)
4993 return -1;
4994
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004995 sm->ap_wpa_ie_len = len;
4996 }
4997
4998 return 0;
4999}
5000
5001
5002/**
5003 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
5004 * @sm: Pointer to WPA state machine data from wpa_sm_init()
5005 * @ie: Pointer to IE data (starting from id)
5006 * @len: IE length
5007 * Returns: 0 on success, -1 on failure
5008 *
5009 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
5010 * frame.
5011 */
5012int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
5013{
5014 if (sm == NULL)
5015 return -1;
5016
5017 os_free(sm->ap_rsn_ie);
5018 if (ie == NULL || len == 0) {
5019 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
5020 "WPA: clearing AP RSN IE");
5021 sm->ap_rsn_ie = NULL;
5022 sm->ap_rsn_ie_len = 0;
5023 } else {
5024 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005025 sm->ap_rsn_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005026 if (sm->ap_rsn_ie == NULL)
5027 return -1;
5028
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005029 sm->ap_rsn_ie_len = len;
5030 }
5031
5032 return 0;
5033}
5034
5035
5036/**
Hai Shalomc3565922019-10-28 11:58:20 -07005037 * wpa_sm_set_ap_rsnxe - Set AP RSNXE from Beacon/ProbeResp
5038 * @sm: Pointer to WPA state machine data from wpa_sm_init()
5039 * @ie: Pointer to IE data (starting from id)
5040 * @len: IE length
5041 * Returns: 0 on success, -1 on failure
5042 *
5043 * Inform WPA state machine about the RSNXE used in Beacon / Probe Response
5044 * frame.
5045 */
5046int wpa_sm_set_ap_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len)
5047{
5048 if (!sm)
5049 return -1;
5050
5051 os_free(sm->ap_rsnxe);
5052 if (!ie || len == 0) {
5053 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: clearing AP RSNXE");
5054 sm->ap_rsnxe = NULL;
5055 sm->ap_rsnxe_len = 0;
5056 } else {
5057 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSNXE", ie, len);
5058 sm->ap_rsnxe = os_memdup(ie, len);
5059 if (!sm->ap_rsnxe)
5060 return -1;
5061
5062 sm->ap_rsnxe_len = len;
5063 }
5064
5065 return 0;
5066}
5067
5068
5069/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005070 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
5071 * @sm: Pointer to WPA state machine data from wpa_sm_init()
5072 * @data: Pointer to data area for parsing results
5073 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
5074 *
5075 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
5076 * parsed data into data.
5077 */
5078int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
5079{
5080 if (sm == NULL)
5081 return -1;
5082
5083 if (sm->assoc_wpa_ie == NULL) {
5084 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
5085 "WPA: No WPA/RSN IE available from association info");
5086 return -1;
5087 }
5088 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
5089 return -2;
5090 return 0;
5091}
5092
5093
5094int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
5095{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005096 return pmksa_cache_list(sm->pmksa, buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005097}
5098
5099
Dmitry Shmidt29333592017-01-09 12:27:11 -08005100struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_head(struct wpa_sm *sm)
5101{
5102 return pmksa_cache_head(sm->pmksa);
5103}
5104
5105
5106struct rsn_pmksa_cache_entry *
5107wpa_sm_pmksa_cache_add_entry(struct wpa_sm *sm,
5108 struct rsn_pmksa_cache_entry * entry)
5109{
5110 return pmksa_cache_add_entry(sm->pmksa, entry);
5111}
5112
5113
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005114void wpa_sm_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
5115 const u8 *pmkid, const u8 *bssid,
5116 const u8 *fils_cache_id)
5117{
5118 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
5119 bssid, sm->own_addr, sm->network_ctx,
5120 sm->key_mgmt, fils_cache_id);
5121}
5122
5123
Sunil Ravi77d572f2023-01-17 23:58:31 +00005124int wpa_sm_pmksa_exists(struct wpa_sm *sm, const u8 *bssid, const u8 *own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005125 const void *network_ctx)
5126{
Sunil Ravi77d572f2023-01-17 23:58:31 +00005127 return pmksa_cache_get(sm->pmksa, bssid, own_addr, NULL, network_ctx,
5128 0) != NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005129}
5130
5131
Hai Shalom60840252021-02-19 19:02:11 -08005132struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_get(struct wpa_sm *sm,
5133 const u8 *aa,
5134 const u8 *pmkid,
5135 const void *network_ctx,
5136 int akmp)
5137{
Sunil Ravi77d572f2023-01-17 23:58:31 +00005138 return pmksa_cache_get(sm->pmksa, aa, sm->own_addr, pmkid, network_ctx,
5139 akmp);
5140}
5141
5142
5143void wpa_sm_pmksa_cache_remove(struct wpa_sm *sm,
5144 struct rsn_pmksa_cache_entry *entry)
5145{
5146 if (sm && sm->pmksa)
5147 pmksa_cache_remove(sm->pmksa, entry);
Hai Shalom60840252021-02-19 19:02:11 -08005148}
5149
5150
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005151void wpa_sm_drop_sa(struct wpa_sm *sm)
5152{
5153 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
Sunil Ravi77d572f2023-01-17 23:58:31 +00005154 wpa_sm_clear_ptk(sm);
Roshan Pius3a1667e2018-07-03 15:17:14 -07005155 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005156 os_memset(sm->pmk, 0, sizeof(sm->pmk));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005157#ifdef CONFIG_IEEE80211R
5158 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
Roshan Pius3a1667e2018-07-03 15:17:14 -07005159 sm->xxkey_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005160 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
Roshan Pius3a1667e2018-07-03 15:17:14 -07005161 sm->pmk_r0_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005162 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
Roshan Pius3a1667e2018-07-03 15:17:14 -07005163 sm->pmk_r1_len = 0;
Hai Shalom60840252021-02-19 19:02:11 -08005164#ifdef CONFIG_PASN
5165 os_free(sm->pasn_r1kh);
5166 sm->pasn_r1kh = NULL;
5167 sm->n_pasn_r1kh = 0;
5168#endif /* CONFIG_PASN */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005169#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005170}
5171
5172
Sunil Ravi77d572f2023-01-17 23:58:31 +00005173#ifdef CONFIG_IEEE80211R
5174bool wpa_sm_has_ft_keys(struct wpa_sm *sm, const u8 *md)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005175{
Sunil Ravi77d572f2023-01-17 23:58:31 +00005176 if (!sm)
5177 return false;
5178 if (!wpa_key_mgmt_ft(sm->key_mgmt) ||
5179 os_memcmp(md, sm->key_mobility_domain,
5180 MOBILITY_DOMAIN_ID_LEN) != 0) {
5181 /* Do not allow FT protocol to be used even if we were to have
5182 * an PTK since the mobility domain has changed. */
5183 return false;
5184 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005185 return sm->ptk_set;
5186}
Sunil Ravi77d572f2023-01-17 23:58:31 +00005187#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005188
5189
Hai Shalomfdcde762020-04-02 11:19:20 -07005190int wpa_sm_has_ptk_installed(struct wpa_sm *sm)
5191{
5192 if (!sm)
5193 return 0;
Sunil8cd6f4d2022-06-28 18:40:46 +00005194 return sm->tk_set || sm->ptk.installed;
Hai Shalomfdcde762020-04-02 11:19:20 -07005195}
5196
5197
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005198void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
5199{
5200 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
5201}
5202
5203
5204void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
5205{
Hai Shalomc1a21442022-02-04 13:43:00 -08005206 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0, false);
5207}
5208
5209
5210void wpa_sm_external_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
5211{
5212 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0, true);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005213}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005214
Andy Kuoaba17c12022-04-14 16:05:31 +08005215#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Mir Ali677e7482020-11-12 19:49:02 +05305216void wpa_sm_install_pmk(struct wpa_sm *sm)
5217{
5218 /* In case the driver wants to handle re-assocs, pass it down the PMK. */
Sunil Ravi77d572f2023-01-17 23:58:31 +00005219 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 +05305220 (u8*)sm->pmk, sm->pmk_len, KEY_FLAG_PMK) < 0) {
5221 wpa_hexdump(MSG_DEBUG, "PSK: Install PMK to the driver for driver reassociations",
5222 (u8*)sm->pmk, sm->pmk_len);
5223 /* No harm if the driver doesn't support. */
5224 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
5225 "WPA: Failed to set PMK to the driver");
5226 }
5227}
Mir Alieaaf04e2021-06-07 12:17:29 +05305228
5229void wpa_sm_notify_brcm_ft_reassoc(struct wpa_sm *sm, const u8 *bssid)
5230{
5231 u8 buf[256];
5232 struct wpa_supplicant *wpa_s = sm->ctx->ctx;
5233
5234 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
5235 "WPA: BRCM FT Reassociation event - clear replay counter");
5236 os_memcpy(sm->bssid, bssid, ETH_ALEN);
5237 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
5238 sm->rx_replay_counter_set = 0;
5239
5240 if (wpa_drv_driver_cmd(wpa_s, "GET_FTKEY", (char *)buf, sizeof(buf)) < 0) {
5241 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
5242 "WPA: Failed to get FT KEY information");
5243 wpa_supplicant_deauthenticate(
5244 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
5245
5246 } else {
5247 /* update kck and kek */
5248 os_memcpy(sm->ptk.kck, buf, 16);
5249 os_memcpy(sm->ptk.kek, buf + 16, 16);
5250 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
5251 "WPA: Updated KCK and KEK after FT reassoc");
5252 }
5253}
Andy Kuoaba17c12022-04-14 16:05:31 +08005254#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
5255
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005256
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005257#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005258int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
5259{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005260 u16 keyinfo;
5261 u8 keylen; /* plaintext key len */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005262 u8 *key_rsc;
5263
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005264 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07005265 struct wpa_gtk_data gd;
5266
5267 os_memset(&gd, 0, sizeof(gd));
5268 keylen = wpa_cipher_key_len(sm->group_cipher);
5269 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
5270 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
5271 if (gd.alg == WPA_ALG_NONE) {
5272 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
5273 return -1;
5274 }
5275
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005276 key_rsc = buf + 5;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005277 keyinfo = WPA_GET_LE16(buf + 2);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005278 gd.gtk_len = keylen;
5279 if (gd.gtk_len != buf[4]) {
5280 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
5281 gd.gtk_len, buf[4]);
5282 return -1;
5283 }
5284 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
5285 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
5286 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
5287
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005288 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005289
5290 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
5291 gd.gtk, gd.gtk_len);
Jouni Malinen58c0e962017-10-01 12:12:24 +03005292 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07005293 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005294 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
5295 "WNM mode");
5296 return -1;
5297 }
Hai Shalom81f62d82019-07-22 12:10:00 -07005298 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005299 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02005300 const struct wpa_igtk_kde *igtk;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07005301
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02005302 igtk = (const struct wpa_igtk_kde *) (buf + 2);
Jouni Malinen58c0e962017-10-01 12:12:24 +03005303 if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005304 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07005305 } else if (subelem_id == WNM_SLEEP_SUBELEM_BIGTK) {
5306 const struct wpa_bigtk_kde *bigtk;
5307
5308 bigtk = (const struct wpa_bigtk_kde *) (buf + 2);
5309 if (sm->beacon_prot &&
5310 wpa_supplicant_install_bigtk(sm, bigtk, 1) < 0)
5311 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005312 } else {
5313 wpa_printf(MSG_DEBUG, "Unknown element id");
5314 return -1;
5315 }
5316
5317 return 0;
5318}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005319#endif /* CONFIG_WNM */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005320
5321
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005322#ifdef CONFIG_P2P
5323
5324int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
5325{
5326 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
5327 return -1;
5328 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
5329 return 0;
5330}
5331
5332#endif /* CONFIG_P2P */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005333
5334
5335void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
5336{
5337 if (rx_replay_counter == NULL)
5338 return;
5339
5340 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
5341 WPA_REPLAY_COUNTER_LEN);
5342 sm->rx_replay_counter_set = 1;
5343 wpa_printf(MSG_DEBUG, "Updated key replay counter");
5344}
5345
5346
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005347void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
5348 const u8 *ptk_kck, size_t ptk_kck_len,
5349 const u8 *ptk_kek, size_t ptk_kek_len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005350{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005351 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
5352 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
5353 sm->ptk.kck_len = ptk_kck_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005354 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
5355 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005356 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
5357 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
5358 sm->ptk.kek_len = ptk_kek_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005359 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
5360 }
5361 sm->ptk_set = 1;
5362}
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08005363
5364
5365#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005366
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08005367void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
5368{
5369 wpabuf_free(sm->test_assoc_ie);
5370 sm->test_assoc_ie = buf;
5371}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005372
5373
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005374void wpa_sm_set_test_eapol_m2_elems(struct wpa_sm *sm, struct wpabuf *buf)
5375{
5376 wpabuf_free(sm->test_eapol_m2_elems);
5377 sm->test_eapol_m2_elems = buf;
5378}
5379
5380
5381void wpa_sm_set_test_eapol_m4_elems(struct wpa_sm *sm, struct wpabuf *buf)
5382{
5383 wpabuf_free(sm->test_eapol_m4_elems);
5384 sm->test_eapol_m4_elems = buf;
5385}
5386
5387
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005388const u8 * wpa_sm_get_anonce(struct wpa_sm *sm)
5389{
5390 return sm->anonce;
5391}
5392
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08005393#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005394
5395
Roshan Pius3a1667e2018-07-03 15:17:14 -07005396unsigned int wpa_sm_get_key_mgmt(struct wpa_sm *sm)
5397{
5398 return sm->key_mgmt;
5399}
5400
5401
Sunil Ravi77d572f2023-01-17 23:58:31 +00005402const u8 * wpa_sm_get_auth_addr(struct wpa_sm *sm)
5403{
5404 return sm->mlo.valid_links ? sm->mlo.ap_mld_addr : sm->bssid;
5405}
5406
5407
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005408#ifdef CONFIG_FILS
5409
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005410struct wpabuf * fils_build_auth(struct wpa_sm *sm, int dh_group, const u8 *md)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005411{
5412 struct wpabuf *buf = NULL;
5413 struct wpabuf *erp_msg;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005414 struct wpabuf *pub = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005415
5416 erp_msg = eapol_sm_build_erp_reauth_start(sm->eapol);
5417 if (!erp_msg && !sm->cur_pmksa) {
5418 wpa_printf(MSG_DEBUG,
5419 "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS");
5420 goto fail;
5421 }
5422
5423 wpa_printf(MSG_DEBUG, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)",
5424 erp_msg != NULL, sm->cur_pmksa != NULL);
5425
5426 sm->fils_completed = 0;
5427
5428 if (!sm->assoc_wpa_ie) {
5429 wpa_printf(MSG_INFO, "FILS: No own RSN IE set for FILS");
5430 goto fail;
5431 }
5432
5433 if (random_get_bytes(sm->fils_nonce, FILS_NONCE_LEN) < 0 ||
5434 random_get_bytes(sm->fils_session, FILS_SESSION_LEN) < 0)
5435 goto fail;
5436
5437 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Nonce",
5438 sm->fils_nonce, FILS_NONCE_LEN);
5439 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Session",
5440 sm->fils_session, FILS_SESSION_LEN);
5441
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005442#ifdef CONFIG_FILS_SK_PFS
5443 sm->fils_dh_group = dh_group;
5444 if (dh_group) {
5445 crypto_ecdh_deinit(sm->fils_ecdh);
5446 sm->fils_ecdh = crypto_ecdh_init(dh_group);
5447 if (!sm->fils_ecdh) {
5448 wpa_printf(MSG_INFO,
5449 "FILS: Could not initialize ECDH with group %d",
5450 dh_group);
5451 goto fail;
5452 }
5453 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
5454 if (!pub)
5455 goto fail;
5456 wpa_hexdump_buf(MSG_DEBUG, "FILS: Element (DH public key)",
5457 pub);
5458 sm->fils_dh_elem_len = wpabuf_len(pub);
5459 }
5460#endif /* CONFIG_FILS_SK_PFS */
5461
5462 buf = wpabuf_alloc(1000 + sm->assoc_wpa_ie_len +
5463 (pub ? wpabuf_len(pub) : 0));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005464 if (!buf)
5465 goto fail;
5466
5467 /* Fields following the Authentication algorithm number field */
5468
5469 /* Authentication Transaction seq# */
5470 wpabuf_put_le16(buf, 1);
5471
5472 /* Status Code */
5473 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
5474
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005475 /* TODO: FILS PK */
5476#ifdef CONFIG_FILS_SK_PFS
5477 if (dh_group) {
5478 /* Finite Cyclic Group */
5479 wpabuf_put_le16(buf, dh_group);
5480 /* Element */
5481 wpabuf_put_buf(buf, pub);
5482 }
5483#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005484
5485 /* RSNE */
5486 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in FILS Authentication frame",
5487 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
5488 wpabuf_put_data(buf, sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
5489
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005490 if (md) {
5491 /* MDE when using FILS for FT initial association */
5492 struct rsn_mdie *mdie;
5493
5494 wpabuf_put_u8(buf, WLAN_EID_MOBILITY_DOMAIN);
5495 wpabuf_put_u8(buf, sizeof(*mdie));
5496 mdie = wpabuf_put(buf, sizeof(*mdie));
5497 os_memcpy(mdie->mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
5498 mdie->ft_capab = 0;
5499 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005500
5501 /* FILS Nonce */
5502 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
5503 wpabuf_put_u8(buf, 1 + FILS_NONCE_LEN); /* Length */
5504 /* Element ID Extension */
5505 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_NONCE);
5506 wpabuf_put_data(buf, sm->fils_nonce, FILS_NONCE_LEN);
5507
5508 /* FILS Session */
5509 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
5510 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
5511 /* Element ID Extension */
5512 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
5513 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
5514
Hai Shalomfdcde762020-04-02 11:19:20 -07005515 /* Wrapped Data */
Paul Stewart092955c2017-02-06 09:13:09 -08005516 sm->fils_erp_pmkid_set = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005517 if (erp_msg) {
5518 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
5519 wpabuf_put_u8(buf, 1 + wpabuf_len(erp_msg)); /* Length */
5520 /* Element ID Extension */
Hai Shalomfdcde762020-04-02 11:19:20 -07005521 wpabuf_put_u8(buf, WLAN_EID_EXT_WRAPPED_DATA);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005522 wpabuf_put_buf(buf, erp_msg);
Paul Stewart092955c2017-02-06 09:13:09 -08005523 /* Calculate pending PMKID here so that we do not need to
5524 * maintain a copy of the EAP-Initiate/Reauth message. */
5525 if (fils_pmkid_erp(sm->key_mgmt, wpabuf_head(erp_msg),
5526 wpabuf_len(erp_msg),
5527 sm->fils_erp_pmkid) == 0)
5528 sm->fils_erp_pmkid_set = 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005529 }
5530
5531 wpa_hexdump_buf(MSG_DEBUG, "RSN: FILS fields for Authentication frame",
5532 buf);
5533
5534fail:
5535 wpabuf_free(erp_msg);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005536 wpabuf_free(pub);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005537 return buf;
5538}
5539
5540
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005541int fils_process_auth(struct wpa_sm *sm, const u8 *bssid, const u8 *data,
5542 size_t len)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005543{
5544 const u8 *pos, *end;
5545 struct ieee802_11_elems elems;
5546 struct wpa_ie_data rsn;
5547 int pmkid_match = 0;
5548 u8 ick[FILS_ICK_MAX_LEN];
5549 size_t ick_len;
5550 int res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005551 struct wpabuf *dh_ss = NULL;
5552 const u8 *g_sta = NULL;
5553 size_t g_sta_len = 0;
5554 const u8 *g_ap = NULL;
Hai Shalom60840252021-02-19 19:02:11 -08005555 size_t g_ap_len = 0, kdk_len;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005556 struct wpabuf *pub = NULL;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005557#ifdef CONFIG_IEEE80211R
5558 struct wpa_ft_ies parse;
5559
5560 os_memset(&parse, 0, sizeof(parse));
5561#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005562
5563 os_memcpy(sm->bssid, bssid, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005564
5565 wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
5566 data, len);
5567 pos = data;
5568 end = data + len;
5569
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005570 /* TODO: FILS PK */
5571#ifdef CONFIG_FILS_SK_PFS
5572 if (sm->fils_dh_group) {
5573 u16 group;
5574
5575 /* Using FILS PFS */
5576
5577 /* Finite Cyclic Group */
5578 if (end - pos < 2) {
5579 wpa_printf(MSG_DEBUG,
5580 "FILS: No room for Finite Cyclic Group");
5581 goto fail;
5582 }
5583 group = WPA_GET_LE16(pos);
5584 pos += 2;
5585 if (group != sm->fils_dh_group) {
5586 wpa_printf(MSG_DEBUG,
5587 "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)",
5588 group, sm->fils_dh_group);
5589 goto fail;
5590 }
5591
5592 /* Element */
5593 if ((size_t) (end - pos) < sm->fils_dh_elem_len) {
5594 wpa_printf(MSG_DEBUG, "FILS: No room for Element");
5595 goto fail;
5596 }
5597
5598 if (!sm->fils_ecdh) {
5599 wpa_printf(MSG_DEBUG, "FILS: No ECDH state available");
5600 goto fail;
5601 }
5602 dh_ss = crypto_ecdh_set_peerkey(sm->fils_ecdh, 1, pos,
5603 sm->fils_dh_elem_len);
5604 if (!dh_ss) {
5605 wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
5606 goto fail;
5607 }
5608 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", dh_ss);
5609 g_ap = pos;
5610 g_ap_len = sm->fils_dh_elem_len;
5611 pos += sm->fils_dh_elem_len;
5612 }
5613#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005614
5615 wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
5616 if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
5617 wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005618 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005619 }
5620
5621 /* RSNE */
5622 wpa_hexdump(MSG_DEBUG, "FILS: RSN element", elems.rsn_ie,
5623 elems.rsn_ie_len);
5624 if (!elems.rsn_ie ||
5625 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
5626 &rsn) < 0) {
5627 wpa_printf(MSG_DEBUG, "FILS: No RSN element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005628 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005629 }
5630
5631 if (!elems.fils_nonce) {
5632 wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005633 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005634 }
5635 os_memcpy(sm->fils_anonce, elems.fils_nonce, FILS_NONCE_LEN);
5636 wpa_hexdump(MSG_DEBUG, "FILS: ANonce", sm->fils_anonce, FILS_NONCE_LEN);
5637
Roshan Pius3a1667e2018-07-03 15:17:14 -07005638#ifdef CONFIG_IEEE80211R
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005639 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005640 if (!elems.mdie || !elems.ftie) {
5641 wpa_printf(MSG_DEBUG, "FILS+FT: No MDE or FTE");
5642 goto fail;
5643 }
5644
Roshan Pius3a1667e2018-07-03 15:17:14 -07005645 if (wpa_ft_parse_ies(pos, end - pos, &parse,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005646 sm->key_mgmt, false) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005647 wpa_printf(MSG_DEBUG, "FILS+FT: Failed to parse IEs");
5648 goto fail;
5649 }
5650
5651 if (!parse.r0kh_id) {
5652 wpa_printf(MSG_DEBUG,
5653 "FILS+FT: No R0KH-ID subelem in FTE");
5654 goto fail;
5655 }
5656 os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
5657 sm->r0kh_id_len = parse.r0kh_id_len;
5658 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
5659 sm->r0kh_id, sm->r0kh_id_len);
5660
5661 if (!parse.r1kh_id) {
5662 wpa_printf(MSG_DEBUG,
5663 "FILS+FT: No R1KH-ID subelem in FTE");
5664 goto fail;
5665 }
5666 os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
5667 wpa_hexdump(MSG_DEBUG, "FILS+FT: R1KH-ID",
5668 sm->r1kh_id, FT_R1KH_ID_LEN);
5669
5670 /* TODO: Check MDE and FTE payload */
5671
5672 wpabuf_free(sm->fils_ft_ies);
5673 sm->fils_ft_ies = wpabuf_alloc(2 + elems.mdie_len +
5674 2 + elems.ftie_len);
5675 if (!sm->fils_ft_ies)
5676 goto fail;
5677 wpabuf_put_data(sm->fils_ft_ies, elems.mdie - 2,
5678 2 + elems.mdie_len);
5679 wpabuf_put_data(sm->fils_ft_ies, elems.ftie - 2,
5680 2 + elems.ftie_len);
5681 } else {
5682 wpabuf_free(sm->fils_ft_ies);
5683 sm->fils_ft_ies = NULL;
5684 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07005685#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005686
5687 /* PMKID List */
5688 if (rsn.pmkid && rsn.num_pmkid > 0) {
5689 wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
5690 rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
5691
5692 if (rsn.num_pmkid != 1) {
5693 wpa_printf(MSG_DEBUG, "FILS: Invalid PMKID selection");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005694 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005695 }
5696 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", rsn.pmkid, PMKID_LEN);
5697 if (os_memcmp(sm->cur_pmksa->pmkid, rsn.pmkid, PMKID_LEN) != 0)
5698 {
5699 wpa_printf(MSG_DEBUG, "FILS: PMKID mismatch");
5700 wpa_hexdump(MSG_DEBUG, "FILS: Expected PMKID",
5701 sm->cur_pmksa->pmkid, PMKID_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005702 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005703 }
5704 wpa_printf(MSG_DEBUG,
5705 "FILS: Matching PMKID - continue using PMKSA caching");
5706 pmkid_match = 1;
5707 }
5708 if (!pmkid_match && sm->cur_pmksa) {
5709 wpa_printf(MSG_DEBUG,
5710 "FILS: No PMKID match - cannot use cached PMKSA entry");
5711 sm->cur_pmksa = NULL;
5712 }
5713
5714 /* FILS Session */
5715 if (!elems.fils_session) {
5716 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005717 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005718 }
5719 wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
5720 FILS_SESSION_LEN);
5721 if (os_memcmp(sm->fils_session, elems.fils_session, FILS_SESSION_LEN)
5722 != 0) {
5723 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
5724 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
5725 sm->fils_session, FILS_SESSION_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005726 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005727 }
5728
Hai Shalomfdcde762020-04-02 11:19:20 -07005729 /* Wrapped Data */
5730 if (!sm->cur_pmksa && elems.wrapped_data) {
Paul Stewart092955c2017-02-06 09:13:09 -08005731 u8 rmsk[ERP_MAX_KEY_LEN];
5732 size_t rmsk_len;
5733
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005734 wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
Hai Shalomfdcde762020-04-02 11:19:20 -07005735 elems.wrapped_data,
5736 elems.wrapped_data_len);
5737 eapol_sm_process_erp_finish(sm->eapol, elems.wrapped_data,
5738 elems.wrapped_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005739 if (eapol_sm_failed(sm->eapol))
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005740 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005741
Paul Stewart092955c2017-02-06 09:13:09 -08005742 rmsk_len = ERP_MAX_KEY_LEN;
5743 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
5744 if (res == PMK_LEN) {
5745 rmsk_len = PMK_LEN;
5746 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
5747 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005748 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005749 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005750
Paul Stewart092955c2017-02-06 09:13:09 -08005751 res = fils_rmsk_to_pmk(sm->key_mgmt, rmsk, rmsk_len,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005752 sm->fils_nonce, sm->fils_anonce,
5753 dh_ss ? wpabuf_head(dh_ss) : NULL,
5754 dh_ss ? wpabuf_len(dh_ss) : 0,
Paul Stewart092955c2017-02-06 09:13:09 -08005755 sm->pmk, &sm->pmk_len);
Hai Shalom81f62d82019-07-22 12:10:00 -07005756 forced_memzero(rmsk, sizeof(rmsk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005757
5758 /* Don't use DHss in PTK derivation if PMKSA caching is not
5759 * used. */
5760 wpabuf_clear_free(dh_ss);
5761 dh_ss = NULL;
5762
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005763 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005764 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08005765
5766 if (!sm->fils_erp_pmkid_set) {
5767 wpa_printf(MSG_DEBUG, "FILS: PMKID not available");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005768 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08005769 }
5770 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", sm->fils_erp_pmkid,
5771 PMKID_LEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005772 wpa_printf(MSG_DEBUG, "FILS: ERP processing succeeded - add PMKSA cache entry for the result");
Paul Stewart092955c2017-02-06 09:13:09 -08005773 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
5774 sm->fils_erp_pmkid, NULL, 0,
5775 sm->bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005776 sm->network_ctx, sm->key_mgmt,
5777 NULL);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005778 }
5779
5780 if (!sm->cur_pmksa) {
5781 wpa_printf(MSG_DEBUG,
5782 "FILS: No remaining options to continue FILS authentication");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005783 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005784 }
5785
Hai Shalom60840252021-02-19 19:02:11 -08005786 if (sm->force_kdk_derivation ||
Hai Shalomc1a21442022-02-04 13:43:00 -08005787 (sm->secure_ltf &&
5788 ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
Hai Shalom60840252021-02-19 19:02:11 -08005789 kdk_len = WPA_KDK_MAX_LEN;
5790 else
5791 kdk_len = 0;
5792
Sunil Ravi77d572f2023-01-17 23:58:31 +00005793 if (fils_pmk_to_ptk(sm->pmk, sm->pmk_len, sm->own_addr,
5794 wpa_sm_get_auth_addr(sm),
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005795 sm->fils_nonce, sm->fils_anonce,
5796 dh_ss ? wpabuf_head(dh_ss) : NULL,
5797 dh_ss ? wpabuf_len(dh_ss) : 0,
5798 &sm->ptk, ick, &ick_len,
5799 sm->key_mgmt, sm->pairwise_cipher,
Hai Shalom60840252021-02-19 19:02:11 -08005800 sm->fils_ft, &sm->fils_ft_len,
5801 kdk_len) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005802 wpa_printf(MSG_DEBUG, "FILS: Failed to derive PTK");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005803 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005804 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005805
Sunil Ravi89eba102022-09-13 21:04:37 -07005806#ifdef CONFIG_PASN
5807 if (sm->secure_ltf &&
5808 ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF) &&
5809 wpa_ltf_keyseed(&sm->ptk, sm->key_mgmt, sm->pairwise_cipher)) {
5810 wpa_printf(MSG_DEBUG, "FILS: Failed to derive LTF keyseed");
5811 goto fail;
5812 }
5813#endif /* CONFIG_PASN */
5814
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005815 wpabuf_clear_free(dh_ss);
5816 dh_ss = NULL;
5817
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005818 sm->ptk_set = 1;
5819 sm->tptk_set = 0;
5820 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
5821
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005822#ifdef CONFIG_FILS_SK_PFS
5823 if (sm->fils_dh_group) {
5824 if (!sm->fils_ecdh) {
5825 wpa_printf(MSG_INFO, "FILS: ECDH not initialized");
5826 goto fail;
5827 }
5828 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
5829 if (!pub)
5830 goto fail;
5831 wpa_hexdump_buf(MSG_DEBUG, "FILS: gSTA", pub);
5832 g_sta = wpabuf_head(pub);
5833 g_sta_len = wpabuf_len(pub);
5834 if (!g_ap) {
5835 wpa_printf(MSG_INFO, "FILS: gAP not available");
5836 goto fail;
5837 }
5838 wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len);
5839 }
5840#endif /* CONFIG_FILS_SK_PFS */
5841
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005842 res = fils_key_auth_sk(ick, ick_len, sm->fils_nonce,
5843 sm->fils_anonce, sm->own_addr, sm->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005844 g_sta, g_sta_len, g_ap, g_ap_len,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005845 sm->key_mgmt, sm->fils_key_auth_sta,
5846 sm->fils_key_auth_ap,
5847 &sm->fils_key_auth_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005848 wpabuf_free(pub);
Hai Shalom81f62d82019-07-22 12:10:00 -07005849 forced_memzero(ick, sizeof(ick));
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005850#ifdef CONFIG_IEEE80211R
5851 wpa_ft_parse_ies_free(&parse);
5852#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005853 return res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005854fail:
5855 wpabuf_free(pub);
5856 wpabuf_clear_free(dh_ss);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005857#ifdef CONFIG_IEEE80211R
5858 wpa_ft_parse_ies_free(&parse);
5859#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005860 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005861}
5862
5863
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005864#ifdef CONFIG_IEEE80211R
5865static int fils_ft_build_assoc_req_rsne(struct wpa_sm *sm, struct wpabuf *buf)
5866{
5867 struct rsn_ie_hdr *rsnie;
5868 u16 capab;
5869 u8 *pos;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005870 int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005871
5872 /* RSNIE[PMKR0Name/PMKR1Name] */
5873 rsnie = wpabuf_put(buf, sizeof(*rsnie));
5874 rsnie->elem_id = WLAN_EID_RSN;
5875 WPA_PUT_LE16(rsnie->version, RSN_VERSION);
5876
5877 /* Group Suite Selector */
5878 if (!wpa_cipher_valid_group(sm->group_cipher)) {
5879 wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
5880 sm->group_cipher);
5881 return -1;
5882 }
5883 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
5884 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
5885 sm->group_cipher));
5886
5887 /* Pairwise Suite Count */
5888 wpabuf_put_le16(buf, 1);
5889
5890 /* Pairwise Suite List */
5891 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
5892 wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
5893 sm->pairwise_cipher);
5894 return -1;
5895 }
5896 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
5897 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
5898 sm->pairwise_cipher));
5899
5900 /* Authenticated Key Management Suite Count */
5901 wpabuf_put_le16(buf, 1);
5902
5903 /* Authenticated Key Management Suite List */
5904 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
5905 if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256)
5906 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
5907 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384)
5908 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
5909 else {
5910 wpa_printf(MSG_WARNING,
5911 "FILS+FT: Invalid key management type (%d)",
5912 sm->key_mgmt);
5913 return -1;
5914 }
5915
5916 /* RSN Capabilities */
5917 capab = 0;
Hai Shalomc3565922019-10-28 11:58:20 -07005918 if (sm->mfp)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005919 capab |= WPA_CAPABILITY_MFPC;
Hai Shalomc3565922019-10-28 11:58:20 -07005920 if (sm->mfp == 2)
5921 capab |= WPA_CAPABILITY_MFPR;
Hai Shalom74f70d42019-02-11 14:42:39 -08005922 if (sm->ocv)
5923 capab |= WPA_CAPABILITY_OCVC;
Hai Shalomfdcde762020-04-02 11:19:20 -07005924 if (sm->ext_key_id)
5925 capab |= WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005926 wpabuf_put_le16(buf, capab);
5927
5928 /* PMKID Count */
5929 wpabuf_put_le16(buf, 1);
5930
5931 /* PMKID List [PMKR1Name] */
5932 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: XXKey (FILS-FT)",
5933 sm->fils_ft, sm->fils_ft_len);
5934 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: SSID", sm->ssid, sm->ssid_len);
5935 wpa_hexdump(MSG_DEBUG, "FILS+FT: MDID",
5936 sm->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
5937 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
5938 sm->r0kh_id, sm->r0kh_id_len);
5939 if (wpa_derive_pmk_r0(sm->fils_ft, sm->fils_ft_len, sm->ssid,
5940 sm->ssid_len, sm->mobility_domain,
5941 sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
Sunil Ravi77d572f2023-01-17 23:58:31 +00005942 sm->pmk_r0, sm->pmk_r0_name, sm->key_mgmt) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005943 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMK-R0");
5944 return -1;
5945 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00005946 if (wpa_key_mgmt_sae_ext_key(sm->key_mgmt))
5947 sm->pmk_r0_len = sm->fils_ft_len;
5948 else
5949 sm->pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005950 wpa_printf(MSG_DEBUG, "FILS+FT: R1KH-ID: " MACSTR,
5951 MAC2STR(sm->r1kh_id));
5952 pos = wpabuf_put(buf, WPA_PMK_NAME_LEN);
5953 if (wpa_derive_pmk_r1_name(sm->pmk_r0_name, sm->r1kh_id, sm->own_addr,
Sunil Ravi77d572f2023-01-17 23:58:31 +00005954 sm->pmk_r1_name, sm->fils_ft_len) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005955 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMKR1Name");
5956 return -1;
5957 }
Hai Shalom021b0b52019-04-10 11:17:58 -07005958 os_memcpy(pos, sm->pmk_r1_name, WPA_PMK_NAME_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005959
Sunil Ravi77d572f2023-01-17 23:58:31 +00005960 os_memcpy(sm->key_mobility_domain, sm->mobility_domain,
5961 MOBILITY_DOMAIN_ID_LEN);
5962
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005963 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
5964 /* Management Group Cipher Suite */
5965 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
5966 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
5967 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005968
5969 rsnie->len = ((u8 *) wpabuf_put(buf, 0) - (u8 *) rsnie) - 2;
5970 return 0;
5971}
5972#endif /* CONFIG_IEEE80211R */
5973
5974
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005975struct wpabuf * fils_build_assoc_req(struct wpa_sm *sm, const u8 **kek,
5976 size_t *kek_len, const u8 **snonce,
Paul Stewart092955c2017-02-06 09:13:09 -08005977 const u8 **anonce,
5978 const struct wpabuf **hlp,
5979 unsigned int num_hlp)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005980{
5981 struct wpabuf *buf;
Paul Stewart092955c2017-02-06 09:13:09 -08005982 size_t len;
5983 unsigned int i;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005984
Paul Stewart092955c2017-02-06 09:13:09 -08005985 len = 1000;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005986#ifdef CONFIG_IEEE80211R
5987 if (sm->fils_ft_ies)
5988 len += wpabuf_len(sm->fils_ft_ies);
5989 if (wpa_key_mgmt_ft(sm->key_mgmt))
5990 len += 256;
5991#endif /* CONFIG_IEEE80211R */
Paul Stewart092955c2017-02-06 09:13:09 -08005992 for (i = 0; hlp && i < num_hlp; i++)
5993 len += 10 + wpabuf_len(hlp[i]);
5994 buf = wpabuf_alloc(len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005995 if (!buf)
5996 return NULL;
5997
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005998#ifdef CONFIG_IEEE80211R
5999 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
6000 /* MDE and FTE when using FILS+FT */
6001 wpabuf_put_buf(buf, sm->fils_ft_ies);
6002 /* RSNE with PMKR1Name in PMKID field */
6003 if (fils_ft_build_assoc_req_rsne(sm, buf) < 0) {
6004 wpabuf_free(buf);
6005 return NULL;
6006 }
6007 }
6008#endif /* CONFIG_IEEE80211R */
6009
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006010 /* FILS Session */
6011 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
6012 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
6013 /* Element ID Extension */
6014 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
6015 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
6016
6017 /* Everything after FILS Session element gets encrypted in the driver
6018 * with KEK. The buffer returned from here is the plaintext version. */
6019
6020 /* TODO: FILS Public Key */
6021
6022 /* FILS Key Confirm */
6023 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
6024 wpabuf_put_u8(buf, 1 + sm->fils_key_auth_len); /* Length */
6025 /* Element ID Extension */
6026 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_KEY_CONFIRM);
6027 wpabuf_put_data(buf, sm->fils_key_auth_sta, sm->fils_key_auth_len);
6028
Paul Stewart092955c2017-02-06 09:13:09 -08006029 /* FILS HLP Container */
6030 for (i = 0; hlp && i < num_hlp; i++) {
6031 const u8 *pos = wpabuf_head(hlp[i]);
6032 size_t left = wpabuf_len(hlp[i]);
6033
6034 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
6035 if (left <= 254)
6036 len = 1 + left;
6037 else
6038 len = 255;
6039 wpabuf_put_u8(buf, len); /* Length */
6040 /* Element ID Extension */
6041 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_HLP_CONTAINER);
6042 /* Destination MAC Address, Source MAC Address, HLP Packet.
6043 * HLP Packet is in MSDU format (i.e., included the LLC/SNAP
6044 * header when LPD is used). */
6045 wpabuf_put_data(buf, pos, len - 1);
6046 pos += len - 1;
6047 left -= len - 1;
6048 while (left) {
6049 wpabuf_put_u8(buf, WLAN_EID_FRAGMENT);
6050 len = left > 255 ? 255 : left;
6051 wpabuf_put_u8(buf, len);
6052 wpabuf_put_data(buf, pos, len);
6053 pos += len;
6054 left -= len;
6055 }
6056 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006057
6058 /* TODO: FILS IP Address Assignment */
6059
Hai Shalom74f70d42019-02-11 14:42:39 -08006060#ifdef CONFIG_OCV
6061 if (wpa_sm_ocv_enabled(sm)) {
6062 struct wpa_channel_info ci;
6063 u8 *pos;
6064
6065 if (wpa_sm_channel_info(sm, &ci) != 0) {
6066 wpa_printf(MSG_WARNING,
6067 "FILS: Failed to get channel info for OCI element");
6068 wpabuf_free(buf);
6069 return NULL;
6070 }
Hai Shalom899fcc72020-10-19 14:38:18 -07006071#ifdef CONFIG_TESTING_OPTIONS
6072 if (sm->oci_freq_override_fils_assoc) {
6073 wpa_printf(MSG_INFO,
6074 "TEST: Override OCI KDE frequency %d -> %d MHz",
6075 ci.frequency,
6076 sm->oci_freq_override_fils_assoc);
6077 ci.frequency = sm->oci_freq_override_fils_assoc;
6078 }
6079#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -08006080
6081 pos = wpabuf_put(buf, OCV_OCI_EXTENDED_LEN);
6082 if (ocv_insert_extended_oci(&ci, pos) < 0) {
6083 wpabuf_free(buf);
6084 return NULL;
6085 }
6086 }
6087#endif /* CONFIG_OCV */
6088
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006089 wpa_hexdump_buf(MSG_DEBUG, "FILS: Association Request plaintext", buf);
6090
6091 *kek = sm->ptk.kek;
6092 *kek_len = sm->ptk.kek_len;
6093 wpa_hexdump_key(MSG_DEBUG, "FILS: KEK for AEAD", *kek, *kek_len);
6094 *snonce = sm->fils_nonce;
6095 wpa_hexdump(MSG_DEBUG, "FILS: SNonce for AEAD AAD",
6096 *snonce, FILS_NONCE_LEN);
6097 *anonce = sm->fils_anonce;
6098 wpa_hexdump(MSG_DEBUG, "FILS: ANonce for AEAD AAD",
6099 *anonce, FILS_NONCE_LEN);
6100
6101 return buf;
6102}
6103
6104
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08006105static void fils_process_hlp_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
6106{
6107 const u8 *pos, *end;
6108
6109 wpa_hexdump(MSG_MSGDUMP, "FILS: HLP response", resp, len);
6110 if (len < 2 * ETH_ALEN)
6111 return;
6112 pos = resp + 2 * ETH_ALEN;
6113 end = resp + len;
6114 if (end - pos >= 6 &&
6115 os_memcmp(pos, "\xaa\xaa\x03\x00\x00\x00", 6) == 0)
6116 pos += 6; /* Remove SNAP/LLC header */
6117 wpa_sm_fils_hlp_rx(sm, resp, resp + ETH_ALEN, pos, end - pos);
6118}
6119
6120
6121static void fils_process_hlp_container(struct wpa_sm *sm, const u8 *pos,
6122 size_t len)
6123{
6124 const u8 *end = pos + len;
6125 u8 *tmp, *tmp_pos;
6126
6127 /* Check if there are any FILS HLP Container elements */
6128 while (end - pos >= 2) {
6129 if (2 + pos[1] > end - pos)
6130 return;
6131 if (pos[0] == WLAN_EID_EXTENSION &&
6132 pos[1] >= 1 + 2 * ETH_ALEN &&
6133 pos[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER)
6134 break;
6135 pos += 2 + pos[1];
6136 }
6137 if (end - pos < 2)
6138 return; /* No FILS HLP Container elements */
6139
6140 tmp = os_malloc(end - pos);
6141 if (!tmp)
6142 return;
6143
6144 while (end - pos >= 2) {
6145 if (2 + pos[1] > end - pos ||
6146 pos[0] != WLAN_EID_EXTENSION ||
6147 pos[1] < 1 + 2 * ETH_ALEN ||
6148 pos[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER)
6149 break;
6150 tmp_pos = tmp;
6151 os_memcpy(tmp_pos, pos + 3, pos[1] - 1);
6152 tmp_pos += pos[1] - 1;
6153 pos += 2 + pos[1];
6154
6155 /* Add possible fragments */
6156 while (end - pos >= 2 && pos[0] == WLAN_EID_FRAGMENT &&
6157 2 + pos[1] <= end - pos) {
6158 os_memcpy(tmp_pos, pos + 2, pos[1]);
6159 tmp_pos += pos[1];
6160 pos += 2 + pos[1];
6161 }
6162
6163 fils_process_hlp_resp(sm, tmp, tmp_pos - tmp);
6164 }
6165
6166 os_free(tmp);
6167}
6168
6169
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006170int fils_process_assoc_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
6171{
6172 const struct ieee80211_mgmt *mgmt;
6173 const u8 *end, *ie_start;
6174 struct ieee802_11_elems elems;
6175 int keylen, rsclen;
6176 enum wpa_alg alg;
6177 struct wpa_gtk_data gd;
6178 int maxkeylen;
6179 struct wpa_eapol_ie_parse kde;
6180
6181 if (!sm || !sm->ptk_set) {
6182 wpa_printf(MSG_DEBUG, "FILS: No KEK available");
6183 return -1;
6184 }
6185
6186 if (!wpa_key_mgmt_fils(sm->key_mgmt)) {
6187 wpa_printf(MSG_DEBUG, "FILS: Not a FILS AKM");
6188 return -1;
6189 }
6190
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006191 if (sm->fils_completed) {
6192 wpa_printf(MSG_DEBUG,
6193 "FILS: Association has already been completed for this FILS authentication - ignore unexpected retransmission");
6194 return -1;
6195 }
6196
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006197 wpa_hexdump(MSG_DEBUG, "FILS: (Re)Association Response frame",
6198 resp, len);
6199
6200 mgmt = (const struct ieee80211_mgmt *) resp;
6201 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp))
6202 return -1;
6203
6204 end = resp + len;
6205 /* Same offset for Association Response and Reassociation Response */
6206 ie_start = mgmt->u.assoc_resp.variable;
6207
6208 if (ieee802_11_parse_elems(ie_start, end - ie_start, &elems, 1) ==
6209 ParseFailed) {
6210 wpa_printf(MSG_DEBUG,
6211 "FILS: Failed to parse decrypted elements");
6212 goto fail;
6213 }
6214
6215 if (!elems.fils_session) {
6216 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
6217 return -1;
6218 }
6219 if (os_memcmp(elems.fils_session, sm->fils_session,
6220 FILS_SESSION_LEN) != 0) {
6221 wpa_printf(MSG_DEBUG, "FILS: FILS Session mismatch");
6222 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
6223 elems.fils_session, FILS_SESSION_LEN);
6224 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
6225 sm->fils_session, FILS_SESSION_LEN);
6226 }
6227
Hai Shalom81f62d82019-07-22 12:10:00 -07006228 if (!elems.rsn_ie) {
6229 wpa_printf(MSG_DEBUG,
6230 "FILS: No RSNE in (Re)Association Response");
6231 /* As an interop workaround, allow this for now since IEEE Std
6232 * 802.11ai-2016 did not include all the needed changes to make
6233 * a FILS AP include RSNE in the frame. This workaround might
6234 * eventually be removed and replaced with rejection (goto fail)
6235 * to follow a strict interpretation of the standard. */
6236 } else if (wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
6237 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
6238 elems.rsn_ie - 2, elems.rsn_ie_len + 2)) {
6239 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
6240 "FILS: RSNE mismatch between Beacon/Probe Response and (Re)Association Response");
6241 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in Beacon/Probe Response",
6242 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
6243 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in (Re)Association Response",
6244 elems.rsn_ie, elems.rsn_ie_len);
6245 goto fail;
6246 }
6247
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006248 /* TODO: FILS Public Key */
6249
6250 if (!elems.fils_key_confirm) {
6251 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
6252 goto fail;
6253 }
6254 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
6255 wpa_printf(MSG_DEBUG,
6256 "FILS: Unexpected Key-Auth length %d (expected %d)",
6257 elems.fils_key_confirm_len,
6258 (int) sm->fils_key_auth_len);
6259 goto fail;
6260 }
6261 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_ap,
6262 sm->fils_key_auth_len) != 0) {
6263 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
6264 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
6265 elems.fils_key_confirm,
6266 elems.fils_key_confirm_len);
6267 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
6268 sm->fils_key_auth_ap, sm->fils_key_auth_len);
6269 goto fail;
6270 }
6271
Hai Shalom74f70d42019-02-11 14:42:39 -08006272#ifdef CONFIG_OCV
6273 if (wpa_sm_ocv_enabled(sm)) {
6274 struct wpa_channel_info ci;
6275
6276 if (wpa_sm_channel_info(sm, &ci) != 0) {
6277 wpa_printf(MSG_WARNING,
6278 "Failed to get channel info to validate received OCI in FILS (Re)Association Response frame");
6279 goto fail;
6280 }
6281
6282 if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci,
6283 channel_width_to_int(ci.chanwidth),
Hai Shalom899fcc72020-10-19 14:38:18 -07006284 ci.seg1_idx) != OCI_SUCCESS) {
6285 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
6286 "addr=" MACSTR " frame=fils-assoc error=%s",
6287 MAC2STR(sm->bssid), ocv_errorstr);
Hai Shalom74f70d42019-02-11 14:42:39 -08006288 goto fail;
6289 }
6290 }
6291#endif /* CONFIG_OCV */
6292
Hai Shalom021b0b52019-04-10 11:17:58 -07006293#ifdef CONFIG_IEEE80211R
6294 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
6295 struct wpa_ie_data rsn;
6296
6297 /* Check that PMKR1Name derived by the AP matches */
6298 if (!elems.rsn_ie ||
6299 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
6300 &rsn) < 0 ||
6301 !rsn.pmkid || rsn.num_pmkid != 1 ||
6302 os_memcmp(rsn.pmkid, sm->pmk_r1_name,
6303 WPA_PMK_NAME_LEN) != 0) {
6304 wpa_printf(MSG_DEBUG,
6305 "FILS+FT: No RSNE[PMKR1Name] match in AssocResp");
6306 goto fail;
6307 }
6308 }
6309#endif /* CONFIG_IEEE80211R */
6310
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006311 /* Key Delivery */
6312 if (!elems.key_delivery) {
6313 wpa_printf(MSG_DEBUG, "FILS: No Key Delivery element");
6314 goto fail;
6315 }
6316
6317 /* Parse GTK and set the key to the driver */
6318 os_memset(&gd, 0, sizeof(gd));
6319 if (wpa_supplicant_parse_ies(elems.key_delivery + WPA_KEY_RSC_LEN,
6320 elems.key_delivery_len - WPA_KEY_RSC_LEN,
6321 &kde) < 0) {
6322 wpa_printf(MSG_DEBUG, "FILS: Failed to parse KDEs");
6323 goto fail;
6324 }
6325 if (!kde.gtk) {
6326 wpa_printf(MSG_DEBUG, "FILS: No GTK KDE");
6327 goto fail;
6328 }
6329 maxkeylen = gd.gtk_len = kde.gtk_len - 2;
6330 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
6331 gd.gtk_len, maxkeylen,
6332 &gd.key_rsc_len, &gd.alg))
6333 goto fail;
6334
6335 wpa_hexdump_key(MSG_DEBUG, "FILS: Received GTK", kde.gtk, kde.gtk_len);
6336 gd.keyidx = kde.gtk[0] & 0x3;
6337 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
6338 !!(kde.gtk[0] & BIT(2)));
6339 if (kde.gtk_len - 2 > sizeof(gd.gtk)) {
6340 wpa_printf(MSG_DEBUG, "FILS: Too long GTK in GTK KDE (len=%lu)",
6341 (unsigned long) kde.gtk_len - 2);
6342 goto fail;
6343 }
6344 os_memcpy(gd.gtk, kde.gtk + 2, kde.gtk_len - 2);
6345
6346 wpa_printf(MSG_DEBUG, "FILS: Set GTK to driver");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006347 if (wpa_supplicant_install_gtk(sm, &gd, elems.key_delivery, 0) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006348 wpa_printf(MSG_DEBUG, "FILS: Failed to set GTK");
6349 goto fail;
6350 }
6351
6352 if (ieee80211w_set_keys(sm, &kde) < 0) {
6353 wpa_printf(MSG_DEBUG, "FILS: Failed to set IGTK");
6354 goto fail;
6355 }
6356
6357 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
6358 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006359 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
6360 wpa_printf(MSG_DEBUG, "FILS: TK length mismatch: %u != %lu",
6361 keylen, (long unsigned int) sm->ptk.tk_len);
6362 goto fail;
6363 }
Hai Shalomfdcde762020-04-02 11:19:20 -07006364
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006365 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
6366 wpa_hexdump_key(MSG_DEBUG, "FILS: Set TK to driver",
6367 sm->ptk.tk, keylen);
Sunil Ravi77d572f2023-01-17 23:58:31 +00006368 if (wpa_sm_set_key(sm, -1, alg, wpa_sm_get_auth_addr(sm), 0, 1,
6369 null_rsc, rsclen,
Hai Shalomfdcde762020-04-02 11:19:20 -07006370 sm->ptk.tk, keylen, KEY_FLAG_PAIRWISE_RX_TX) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006371 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Sunil Ravi77d572f2023-01-17 23:58:31 +00006372 "FILS: Failed to set PTK to the driver (alg=%d keylen=%d auth_addr="
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006373 MACSTR ")",
Sunil Ravi77d572f2023-01-17 23:58:31 +00006374 alg, keylen, MAC2STR(wpa_sm_get_auth_addr(sm)));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006375 goto fail;
6376 }
6377
Hai Shalom60840252021-02-19 19:02:11 -08006378 wpa_sm_store_ptk(sm, sm->bssid, sm->pairwise_cipher,
6379 sm->dot11RSNAConfigPMKLifetime, &sm->ptk);
6380
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006381 /* TODO: TK could be cleared after auth frame exchange now that driver
6382 * takes care of association frame encryption/decryption. */
6383 /* TK is not needed anymore in supplicant */
6384 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006385 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +02006386 sm->ptk.installed = 1;
Sunil8cd6f4d2022-06-28 18:40:46 +00006387 sm->tk_set = true;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006388
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08006389 /* FILS HLP Container */
6390 fils_process_hlp_container(sm, ie_start, end - ie_start);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006391
6392 /* TODO: FILS IP Address Assignment */
6393
6394 wpa_printf(MSG_DEBUG, "FILS: Auth+Assoc completed successfully");
6395 sm->fils_completed = 1;
Hai Shalom81f62d82019-07-22 12:10:00 -07006396 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006397
Hai Shalomfdcde762020-04-02 11:19:20 -07006398 if (kde.transition_disable)
6399 wpa_sm_transition_disable(sm, kde.transition_disable[0]);
6400
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006401 return 0;
6402fail:
Hai Shalom81f62d82019-07-22 12:10:00 -07006403 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006404 return -1;
6405}
6406
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006407
6408void wpa_sm_set_reset_fils_completed(struct wpa_sm *sm, int set)
6409{
6410 if (sm)
6411 sm->fils_completed = !!set;
6412}
6413
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006414#endif /* CONFIG_FILS */
6415
6416
6417int wpa_fils_is_completed(struct wpa_sm *sm)
6418{
6419#ifdef CONFIG_FILS
6420 return sm && sm->fils_completed;
6421#else /* CONFIG_FILS */
6422 return 0;
6423#endif /* CONFIG_FILS */
6424}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006425
6426
6427#ifdef CONFIG_OWE
6428
6429struct wpabuf * owe_build_assoc_req(struct wpa_sm *sm, u16 group)
6430{
6431 struct wpabuf *ie = NULL, *pub = NULL;
6432 size_t prime_len;
6433
6434 if (group == 19)
6435 prime_len = 32;
6436 else if (group == 20)
6437 prime_len = 48;
6438 else if (group == 21)
6439 prime_len = 66;
6440 else
6441 return NULL;
6442
6443 crypto_ecdh_deinit(sm->owe_ecdh);
6444 sm->owe_ecdh = crypto_ecdh_init(group);
6445 if (!sm->owe_ecdh)
6446 goto fail;
6447 sm->owe_group = group;
6448 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
6449 pub = wpabuf_zeropad(pub, prime_len);
6450 if (!pub)
6451 goto fail;
6452
6453 ie = wpabuf_alloc(5 + wpabuf_len(pub));
6454 if (!ie)
6455 goto fail;
6456 wpabuf_put_u8(ie, WLAN_EID_EXTENSION);
6457 wpabuf_put_u8(ie, 1 + 2 + wpabuf_len(pub));
6458 wpabuf_put_u8(ie, WLAN_EID_EXT_OWE_DH_PARAM);
6459 wpabuf_put_le16(ie, group);
6460 wpabuf_put_buf(ie, pub);
6461 wpabuf_free(pub);
6462 wpa_hexdump_buf(MSG_DEBUG, "OWE: Diffie-Hellman Parameter element",
6463 ie);
6464
6465 return ie;
6466fail:
6467 wpabuf_free(pub);
6468 crypto_ecdh_deinit(sm->owe_ecdh);
6469 sm->owe_ecdh = NULL;
6470 return NULL;
6471}
6472
6473
6474int owe_process_assoc_resp(struct wpa_sm *sm, const u8 *bssid,
6475 const u8 *resp_ies, size_t resp_ies_len)
6476{
6477 struct ieee802_11_elems elems;
6478 u16 group;
6479 struct wpabuf *secret, *pub, *hkey;
6480 int res;
6481 u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN];
6482 const char *info = "OWE Key Generation";
6483 const u8 *addr[2];
6484 size_t len[2];
6485 size_t hash_len, prime_len;
6486 struct wpa_ie_data data;
6487
6488 if (!resp_ies ||
6489 ieee802_11_parse_elems(resp_ies, resp_ies_len, &elems, 1) ==
6490 ParseFailed) {
6491 wpa_printf(MSG_INFO,
6492 "OWE: Could not parse Association Response frame elements");
6493 return -1;
6494 }
6495
6496 if (sm->cur_pmksa && elems.rsn_ie &&
6497 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, 2 + elems.rsn_ie_len,
6498 &data) == 0 &&
6499 data.num_pmkid == 1 && data.pmkid &&
6500 os_memcmp(sm->cur_pmksa->pmkid, data.pmkid, PMKID_LEN) == 0) {
6501 wpa_printf(MSG_DEBUG, "OWE: Use PMKSA caching");
6502 wpa_sm_set_pmk_from_pmksa(sm);
6503 return 0;
6504 }
6505
6506 if (!elems.owe_dh) {
6507 wpa_printf(MSG_INFO,
6508 "OWE: No Diffie-Hellman Parameter element found in Association Response frame");
6509 return -1;
6510 }
6511
6512 group = WPA_GET_LE16(elems.owe_dh);
6513 if (group != sm->owe_group) {
6514 wpa_printf(MSG_INFO,
6515 "OWE: Unexpected Diffie-Hellman group in response: %u",
6516 group);
6517 return -1;
6518 }
6519
6520 if (!sm->owe_ecdh) {
6521 wpa_printf(MSG_INFO, "OWE: No ECDH state available");
6522 return -1;
6523 }
6524
6525 if (group == 19)
6526 prime_len = 32;
6527 else if (group == 20)
6528 prime_len = 48;
6529 else if (group == 21)
6530 prime_len = 66;
6531 else
6532 return -1;
6533
6534 secret = crypto_ecdh_set_peerkey(sm->owe_ecdh, 0,
6535 elems.owe_dh + 2,
6536 elems.owe_dh_len - 2);
6537 secret = wpabuf_zeropad(secret, prime_len);
6538 if (!secret) {
6539 wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
6540 return -1;
6541 }
6542 wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret);
6543
6544 /* prk = HKDF-extract(C | A | group, z) */
6545
6546 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
6547 if (!pub) {
6548 wpabuf_clear_free(secret);
6549 return -1;
6550 }
6551
6552 /* PMKID = Truncate-128(Hash(C | A)) */
6553 addr[0] = wpabuf_head(pub);
6554 len[0] = wpabuf_len(pub);
6555 addr[1] = elems.owe_dh + 2;
6556 len[1] = elems.owe_dh_len - 2;
6557 if (group == 19) {
6558 res = sha256_vector(2, addr, len, pmkid);
6559 hash_len = SHA256_MAC_LEN;
6560 } else if (group == 20) {
6561 res = sha384_vector(2, addr, len, pmkid);
6562 hash_len = SHA384_MAC_LEN;
6563 } else if (group == 21) {
6564 res = sha512_vector(2, addr, len, pmkid);
6565 hash_len = SHA512_MAC_LEN;
6566 } else {
6567 res = -1;
6568 hash_len = 0;
6569 }
6570 pub = wpabuf_zeropad(pub, prime_len);
6571 if (res < 0 || !pub) {
6572 wpabuf_free(pub);
6573 wpabuf_clear_free(secret);
6574 return -1;
6575 }
6576
6577 hkey = wpabuf_alloc(wpabuf_len(pub) + elems.owe_dh_len - 2 + 2);
6578 if (!hkey) {
6579 wpabuf_free(pub);
6580 wpabuf_clear_free(secret);
6581 return -1;
6582 }
6583
6584 wpabuf_put_buf(hkey, pub); /* C */
6585 wpabuf_free(pub);
6586 wpabuf_put_data(hkey, elems.owe_dh + 2, elems.owe_dh_len - 2); /* A */
6587 wpabuf_put_le16(hkey, sm->owe_group); /* group */
6588 if (group == 19)
6589 res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
6590 wpabuf_head(secret), wpabuf_len(secret), prk);
6591 else if (group == 20)
6592 res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey),
6593 wpabuf_head(secret), wpabuf_len(secret), prk);
6594 else if (group == 21)
6595 res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey),
6596 wpabuf_head(secret), wpabuf_len(secret), prk);
6597 wpabuf_clear_free(hkey);
6598 wpabuf_clear_free(secret);
6599 if (res < 0)
6600 return -1;
6601
6602 wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
6603
6604 /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
6605
6606 if (group == 19)
6607 res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info,
6608 os_strlen(info), sm->pmk, hash_len);
6609 else if (group == 20)
6610 res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info,
6611 os_strlen(info), sm->pmk, hash_len);
6612 else if (group == 21)
6613 res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info,
6614 os_strlen(info), sm->pmk, hash_len);
Hai Shalom81f62d82019-07-22 12:10:00 -07006615 forced_memzero(prk, SHA512_MAC_LEN);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006616 if (res < 0) {
6617 sm->pmk_len = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006618 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006619 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006620 sm->pmk_len = hash_len;
6621
6622 wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sm->pmk, sm->pmk_len);
6623 wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
6624 pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, pmkid, NULL, 0,
6625 bssid, sm->own_addr, sm->network_ctx, sm->key_mgmt,
6626 NULL);
6627
6628 return 0;
6629}
6630
6631#endif /* CONFIG_OWE */
6632
6633
6634void wpa_sm_set_fils_cache_id(struct wpa_sm *sm, const u8 *fils_cache_id)
6635{
6636#ifdef CONFIG_FILS
6637 if (sm && fils_cache_id) {
6638 sm->fils_cache_id_set = 1;
6639 os_memcpy(sm->fils_cache_id, fils_cache_id, FILS_CACHE_ID_LEN);
6640 }
6641#endif /* CONFIG_FILS */
6642}
Hai Shalom021b0b52019-04-10 11:17:58 -07006643
6644
6645#ifdef CONFIG_DPP2
6646void wpa_sm_set_dpp_z(struct wpa_sm *sm, const struct wpabuf *z)
6647{
6648 if (sm) {
6649 wpabuf_clear_free(sm->dpp_z);
6650 sm->dpp_z = z ? wpabuf_dup(z) : NULL;
6651 }
6652}
6653#endif /* CONFIG_DPP2 */
Hai Shalom60840252021-02-19 19:02:11 -08006654
6655
6656#ifdef CONFIG_PASN
Sunil Ravi89eba102022-09-13 21:04:37 -07006657
Sunil Ravi89eba102022-09-13 21:04:37 -07006658void wpa_pasn_sm_set_caps(struct wpa_sm *sm, unsigned int flags2)
6659{
6660 if (flags2 & WPA_DRIVER_FLAGS2_SEC_LTF_STA)
6661 sm->secure_ltf = 1;
6662 if (flags2 & WPA_DRIVER_FLAGS2_SEC_RTT_STA)
6663 sm->secure_rtt = 1;
6664 if (flags2 & WPA_DRIVER_FLAGS2_PROT_RANGE_NEG_STA)
6665 sm->prot_range_neg = 1;
6666}
6667
Hai Shalom60840252021-02-19 19:02:11 -08006668#endif /* CONFIG_PASN */
Hai Shalomc1a21442022-02-04 13:43:00 -08006669
6670
6671void wpa_sm_pmksa_cache_reconfig(struct wpa_sm *sm)
6672{
6673 if (sm)
6674 pmksa_cache_reconfig(sm->pmksa);
6675}
Sunil Ravi77d572f2023-01-17 23:58:31 +00006676
6677
6678struct rsn_pmksa_cache * wpa_sm_get_pmksa_cache(struct wpa_sm *sm)
6679{
6680 return sm ? sm->pmksa : NULL;
6681}
6682
6683
6684void wpa_sm_set_cur_pmksa(struct wpa_sm *sm,
6685 struct rsn_pmksa_cache_entry *entry)
6686{
6687 if (sm)
6688 sm->cur_pmksa = entry;
6689}
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006690
6691
6692void wpa_sm_set_driver_bss_selection(struct wpa_sm *sm,
6693 bool driver_bss_selection)
6694{
6695 if (sm)
6696 sm->driver_bss_selection = driver_bss_selection;
6697}