blob: 9f49cf9c759761667d53402452cb4816430e9a7c [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
Roshan Pius3a1667e2018-07-03 15:17:14 -0700236 if (wpa_use_akm_defined(sm->key_mgmt))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800237 ver = WPA_KEY_INFO_TYPE_AKM_DEFINED;
238 else if (wpa_key_mgmt_ft(sm->key_mgmt) ||
239 wpa_key_mgmt_sha256(sm->key_mgmt))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700241 else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700242 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
243 else
244 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
245
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700246 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800247 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800249 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700250 if (rbuf == NULL)
251 return;
252
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800253 reply->type = (sm->proto == WPA_PROTO_RSN ||
254 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700255 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
256 key_info = WPA_KEY_INFO_REQUEST | ver;
Sunil Ravieb83e2a2024-06-28 17:34:56 +0000257 if (sm->ptk_set)
258 key_info |= WPA_KEY_INFO_SECURE;
259 if (sm->ptk_set && mic_len)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800260 key_info |= WPA_KEY_INFO_MIC;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700261 if (error)
262 key_info |= WPA_KEY_INFO_ERROR;
263 if (pairwise)
264 key_info |= WPA_KEY_INFO_KEY_TYPE;
265 WPA_PUT_BE16(reply->key_info, key_info);
266 WPA_PUT_BE16(reply->key_length, 0);
267 os_memcpy(reply->replay_counter, sm->request_counter,
268 WPA_REPLAY_COUNTER_LEN);
269 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
270
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800271 mic = (u8 *) (reply + 1);
272 WPA_PUT_BE16(mic + mic_len, 0);
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800273 if (!(key_info & WPA_KEY_INFO_MIC))
274 key_mic = NULL;
275 else
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800276 key_mic = mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700277
278 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
279 "WPA: Sending EAPOL-Key Request (error=%d "
280 "pairwise=%d ptk_set=%d len=%lu)",
281 error, pairwise, sm->ptk_set, (unsigned long) rlen);
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000282 wpa_eapol_key_send(sm, &sm->ptk, ver, wpa_sm_get_auth_addr(sm),
283 ETH_P_EAPOL, rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284}
285
286
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800287static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm *sm)
288{
289#ifdef CONFIG_IEEE80211R
290 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
291 if (wpa_sm_key_mgmt_set_pmk(sm, sm->xxkey, sm->xxkey_len))
292 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
293 "RSN: Cannot set low order 256 bits of MSK for key management offload");
294 } else {
295#endif /* CONFIG_IEEE80211R */
296 if (wpa_sm_key_mgmt_set_pmk(sm, sm->pmk, sm->pmk_len))
297 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
298 "RSN: Cannot set PMK for key management offload");
299#ifdef CONFIG_IEEE80211R
300 }
301#endif /* CONFIG_IEEE80211R */
302}
303
304
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700305static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
306 const unsigned char *src_addr,
307 const u8 *pmkid)
308{
309 int abort_cached = 0;
310
311 if (pmkid && !sm->cur_pmksa) {
312 /* When using drivers that generate RSN IE, wpa_supplicant may
313 * not have enough time to get the association information
314 * event before receiving this 1/4 message, so try to find a
315 * matching PMKSA cache entry here. */
Sunil Ravi77d572f2023-01-17 23:58:31 +0000316 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr,
317 sm->own_addr, pmkid,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700318 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700319 if (sm->cur_pmksa) {
320 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
321 "RSN: found matching PMKID from PMKSA cache");
322 } else {
323 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
324 "RSN: no matching PMKID found");
325 abort_cached = 1;
326 }
327 }
328
329 if (pmkid && sm->cur_pmksa &&
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700330 os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700331 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
332 wpa_sm_set_pmk_from_pmksa(sm);
333 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
334 sm->pmk, sm->pmk_len);
335 eapol_sm_notify_cached(sm->eapol);
336#ifdef CONFIG_IEEE80211R
337 sm->xxkey_len = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700338#ifdef CONFIG_SAE
Sunil Ravi89eba102022-09-13 21:04:37 -0700339 if ((sm->key_mgmt == WPA_KEY_MGMT_FT_SAE ||
340 sm->key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -0700341 sm->pmk_len == PMK_LEN) {
342 /* Need to allow FT key derivation to proceed with
343 * PMK from SAE being used as the XXKey in cases where
344 * the PMKID in msg 1/4 matches the PMKSA entry that was
345 * just added based on SAE authentication for the
346 * initial mobility domain association. */
347 os_memcpy(sm->xxkey, sm->pmk, sm->pmk_len);
348 sm->xxkey_len = sm->pmk_len;
349 }
350#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351#endif /* CONFIG_IEEE80211R */
352 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
353 int res, pmk_len;
Hai Shalom81f62d82019-07-22 12:10:00 -0700354#ifdef CONFIG_IEEE80211R
355 u8 buf[2 * PMK_LEN];
356#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800357
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800358 if (wpa_key_mgmt_sha384(sm->key_mgmt))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800359 pmk_len = PMK_LEN_SUITE_B_192;
360 else
361 pmk_len = PMK_LEN;
362 res = eapol_sm_get_key(sm->eapol, sm->pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363 if (res) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800364 if (pmk_len == PMK_LEN) {
365 /*
366 * EAP-LEAP is an exception from other EAP
367 * methods: it uses only 16-byte PMK.
368 */
369 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
370 pmk_len = 16;
371 }
Hai Shalomf1c97642019-07-19 23:42:07 +0000372 }
Hai Shalom81f62d82019-07-22 12:10:00 -0700373#ifdef CONFIG_IEEE80211R
374 if (res == 0 &&
375 eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0) {
376 if (wpa_key_mgmt_sha384(sm->key_mgmt)) {
377 os_memcpy(sm->xxkey, buf, SHA384_MAC_LEN);
378 sm->xxkey_len = SHA384_MAC_LEN;
379 } else {
380 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
381 sm->xxkey_len = PMK_LEN;
382 }
383 forced_memzero(buf, sizeof(buf));
384 if (sm->proto == WPA_PROTO_RSN &&
385 wpa_key_mgmt_ft(sm->key_mgmt)) {
386 struct rsn_pmksa_cache_entry *sa = NULL;
387 const u8 *fils_cache_id = NULL;
388
389#ifdef CONFIG_FILS
390 if (sm->fils_cache_id_set)
391 fils_cache_id = sm->fils_cache_id;
392#endif /* CONFIG_FILS */
393 wpa_hexdump_key(MSG_DEBUG,
394 "FT: Cache XXKey/MPMK",
395 sm->xxkey, sm->xxkey_len);
396 sa = pmksa_cache_add(sm->pmksa,
397 sm->xxkey, sm->xxkey_len,
398 NULL, NULL, 0,
399 src_addr, sm->own_addr,
400 sm->network_ctx,
401 sm->key_mgmt,
402 fils_cache_id);
403 if (!sm->cur_pmksa)
404 sm->cur_pmksa = sa;
405 }
406 }
407#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700408 if (res == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700409 struct rsn_pmksa_cache_entry *sa = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700410 const u8 *fils_cache_id = NULL;
411
412#ifdef CONFIG_FILS
413 if (sm->fils_cache_id_set)
414 fils_cache_id = sm->fils_cache_id;
415#endif /* CONFIG_FILS */
416
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700417 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
418 "machines", sm->pmk, pmk_len);
419 sm->pmk_len = pmk_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800420 wpa_supplicant_key_mgmt_set_pmk(sm);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700421 if (sm->proto == WPA_PROTO_RSN &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800422 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700423 !wpa_key_mgmt_ft(sm->key_mgmt)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700424 sa = pmksa_cache_add(sm->pmksa,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800425 sm->pmk, pmk_len, NULL,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800426 NULL, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700427 src_addr, sm->own_addr,
428 sm->network_ctx,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700429 sm->key_mgmt,
430 fils_cache_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431 }
432 if (!sm->cur_pmksa && pmkid &&
Sunil Ravi77d572f2023-01-17 23:58:31 +0000433 pmksa_cache_get(sm->pmksa, src_addr, sm->own_addr,
434 pmkid, NULL, 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700435 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
436 "RSN: the new PMK matches with the "
437 "PMKID");
438 abort_cached = 0;
Jouni Malinen6ec30382015-07-08 20:48:18 +0300439 } else if (sa && !sm->cur_pmksa && pmkid) {
440 /*
441 * It looks like the authentication server
442 * derived mismatching MSK. This should not
443 * really happen, but bugs happen.. There is not
444 * much we can do here without knowing what
445 * exactly caused the server to misbehave.
446 */
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800447 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Jouni Malinen6ec30382015-07-08 20:48:18 +0300448 "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
449 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700450 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700451
452 if (!sm->cur_pmksa)
453 sm->cur_pmksa = sa;
Hai Shalom81f62d82019-07-22 12:10:00 -0700454#ifdef CONFIG_IEEE80211R
455 } else if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->ft_protocol) {
456 wpa_printf(MSG_DEBUG,
457 "FT: Continue 4-way handshake without PMK/PMKID for association using FT protocol");
458#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700459 } else {
460 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
461 "WPA: Failed to get master session key from "
462 "EAPOL state machines - key handshake "
463 "aborted");
464 if (sm->cur_pmksa) {
465 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
466 "RSN: Cancelled PMKSA caching "
467 "attempt");
468 sm->cur_pmksa = NULL;
469 abort_cached = 1;
470 } else if (!abort_cached) {
471 return -1;
472 }
473 }
474 }
475
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700476 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800477 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800478 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
479 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700480 /* Send EAPOL-Start to trigger full EAP authentication. */
481 u8 *buf;
482 size_t buflen;
483
484 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
485 "RSN: no PMKSA entry found - trigger "
486 "full EAP authentication");
487 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
488 NULL, 0, &buflen, NULL);
489 if (buf) {
Hai Shalomc1a21442022-02-04 13:43:00 -0800490 /* Set and reset eapFail to allow EAP state machine to
491 * proceed with new authentication. */
492 eapol_sm_notify_eap_fail(sm->eapol, true);
493 eapol_sm_notify_eap_fail(sm->eapol, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700494 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
495 buf, buflen);
496 os_free(buf);
497 return -2;
498 }
499
500 return -1;
501 }
502
503 return 0;
504}
505
506
507/**
508 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
509 * @sm: Pointer to WPA state machine data from wpa_sm_init()
510 * @dst: Destination address for the frame
511 * @key: Pointer to the EAPOL-Key frame header
512 * @ver: Version bits from EAPOL-Key Key Info
513 * @nonce: Nonce value for the EAPOL-Key frame
514 * @wpa_ie: WPA/RSN IE
515 * @wpa_ie_len: Length of the WPA/RSN IE
516 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800517 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700518 */
519int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
520 const struct wpa_eapol_key *key,
521 int ver, const u8 *nonce,
522 const u8 *wpa_ie, size_t wpa_ie_len,
523 struct wpa_ptk *ptk)
524{
Sunil Ravieb83e2a2024-06-28 17:34:56 +0000525 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700526 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800527 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700528 u8 *rsn_ie_buf = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800529 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700530
531 if (wpa_ie == NULL) {
532 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
533 "cannot generate msg 2/4");
534 return -1;
535 }
536
537#ifdef CONFIG_IEEE80211R
538 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
539 int res;
540
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800541 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE before FT processing",
542 wpa_ie, wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700543 /*
544 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
545 * FTIE from (Re)Association Response.
546 */
547 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
548 sm->assoc_resp_ies_len);
549 if (rsn_ie_buf == NULL)
550 return -1;
551 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800552 res = wpa_insert_pmkid(rsn_ie_buf, &wpa_ie_len,
Sunil Ravieb83e2a2024-06-28 17:34:56 +0000553 sm->pmk_r1_name);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700554 if (res < 0) {
555 os_free(rsn_ie_buf);
556 return -1;
557 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800558 wpa_hexdump(MSG_DEBUG,
559 "WPA: WPA IE after PMKID[PMKR1Name] addition into RSNE",
560 rsn_ie_buf, wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700561
562 if (sm->assoc_resp_ies) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800563 wpa_hexdump(MSG_DEBUG, "WPA: Add assoc_resp_ies",
564 sm->assoc_resp_ies,
565 sm->assoc_resp_ies_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700566 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
567 sm->assoc_resp_ies_len);
568 wpa_ie_len += sm->assoc_resp_ies_len;
569 }
570
571 wpa_ie = rsn_ie_buf;
572 }
573#endif /* CONFIG_IEEE80211R */
574
575 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
576
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700577 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800578 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700579 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
Sunil Ravieb83e2a2024-06-28 17:34:56 +0000580 NULL, hdrlen + wpa_ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700581 &rlen, (void *) &reply);
582 if (rbuf == NULL) {
583 os_free(rsn_ie_buf);
584 return -1;
585 }
586
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800587 reply->type = (sm->proto == WPA_PROTO_RSN ||
588 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700589 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800590 key_info = ver | WPA_KEY_INFO_KEY_TYPE;
Sunil8cd6f4d2022-06-28 18:40:46 +0000591 if (sm->ptk_set && sm->proto != WPA_PROTO_WPA)
592 key_info |= WPA_KEY_INFO_SECURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800593 if (mic_len)
594 key_info |= WPA_KEY_INFO_MIC;
595 else
596 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
597 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800598 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700599 WPA_PUT_BE16(reply->key_length, 0);
600 else
601 os_memcpy(reply->key_length, key->key_length, 2);
602 os_memcpy(reply->replay_counter, key->replay_counter,
603 WPA_REPLAY_COUNTER_LEN);
604 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
605 WPA_REPLAY_COUNTER_LEN);
606
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800607 key_mic = (u8 *) (reply + 1);
Sunil Ravieb83e2a2024-06-28 17:34:56 +0000608 WPA_PUT_BE16(key_mic + mic_len, wpa_ie_len); /* Key Data Length */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800609 os_memcpy(key_mic + mic_len + 2, wpa_ie, wpa_ie_len); /* Key Data */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700610 os_free(rsn_ie_buf);
611
612 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
613
Roshan Pius5e7db942018-04-12 12:27:41 -0700614 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 2/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800615 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
616 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700617}
618
619
620static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800621 const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700622{
Sunil Ravi89eba102022-09-13 21:04:37 -0700623 int ret;
Hai Shalom021b0b52019-04-10 11:17:58 -0700624 const u8 *z = NULL;
Hai Shalom60840252021-02-19 19:02:11 -0800625 size_t z_len = 0, kdk_len;
Hai Shalomfdcde762020-04-02 11:19:20 -0700626 int akmp;
Hai Shalom021b0b52019-04-10 11:17:58 -0700627
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700628#ifdef CONFIG_IEEE80211R
629 if (wpa_key_mgmt_ft(sm->key_mgmt))
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800630 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700631#endif /* CONFIG_IEEE80211R */
632
Hai Shalom021b0b52019-04-10 11:17:58 -0700633#ifdef CONFIG_DPP2
634 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
635 z = wpabuf_head(sm->dpp_z);
636 z_len = wpabuf_len(sm->dpp_z);
637 }
638#endif /* CONFIG_DPP2 */
639
Hai Shalomfdcde762020-04-02 11:19:20 -0700640 akmp = sm->key_mgmt;
641#ifdef CONFIG_OWE
642 if (sm->owe_ptk_workaround && akmp == WPA_KEY_MGMT_OWE &&
643 sm->pmk_len > 32) {
644 wpa_printf(MSG_DEBUG,
645 "OWE: Force SHA256 for PTK derivation");
646 akmp |= WPA_KEY_MGMT_PSK_SHA256;
647 }
648#endif /* CONFIG_OWE */
Hai Shalom60840252021-02-19 19:02:11 -0800649
650 if (sm->force_kdk_derivation ||
Hai Shalomc1a21442022-02-04 13:43:00 -0800651 (sm->secure_ltf &&
652 ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
Hai Shalom60840252021-02-19 19:02:11 -0800653 kdk_len = WPA_KDK_MAX_LEN;
654 else
655 kdk_len = 0;
656
Sunil Ravi89eba102022-09-13 21:04:37 -0700657 ret = wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
Sunil Ravi77d572f2023-01-17 23:58:31 +0000658 sm->own_addr, wpa_sm_get_auth_addr(sm), sm->snonce,
Sunil Ravi89eba102022-09-13 21:04:37 -0700659 key->key_nonce, ptk, akmp,
660 sm->pairwise_cipher, z, z_len,
661 kdk_len);
662 if (ret) {
663 wpa_printf(MSG_ERROR, "WPA: PTK derivation failed");
664 return ret;
665 }
666
667#ifdef CONFIG_PASN
668 if (sm->secure_ltf &&
669 ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF))
670 ret = wpa_ltf_keyseed(ptk, akmp, sm->pairwise_cipher);
671#endif /* CONFIG_PASN */
672
673 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700674}
675
676
Hai Shalomfdcde762020-04-02 11:19:20 -0700677static int wpa_handle_ext_key_id(struct wpa_sm *sm,
678 struct wpa_eapol_ie_parse *kde)
679{
680 if (sm->ext_key_id) {
681 u16 key_id;
682
683 if (!kde->key_id) {
684 wpa_msg(sm->ctx->msg_ctx,
685 sm->use_ext_key_id ? MSG_INFO : MSG_DEBUG,
686 "RSN: No Key ID in Extended Key ID handshake");
687 sm->keyidx_active = 0;
688 return sm->use_ext_key_id ? -1 : 0;
689 }
690
691 key_id = kde->key_id[0] & 0x03;
692 if (key_id > 1) {
693 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
694 "RSN: Invalid Extended Key ID: %d", key_id);
695 return -1;
696 }
697 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
698 "RSN: Using Extended Key ID %d", key_id);
699 sm->keyidx_active = key_id;
700 sm->use_ext_key_id = 1;
701 } else {
702 if (kde->key_id && (kde->key_id[0] & 0x03)) {
703 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
704 "RSN: Non-zero Extended Key ID Key ID in PTK0 handshake");
705 return -1;
706 }
707
708 if (kde->key_id) {
709 /* This is not supposed to be included here, but ignore
710 * the case of matching Key ID 0 just in case. */
711 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
712 "RSN: Extended Key ID Key ID 0 in PTK0 handshake");
713 }
714 sm->keyidx_active = 0;
715 sm->use_ext_key_id = 0;
716 }
717
718 return 0;
719}
720
721
Sunil Ravi77d572f2023-01-17 23:58:31 +0000722static u8 * rsn_add_kde(u8 *pos, u32 kde, const u8 *data, size_t data_len)
723{
724 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
725 *pos++ = RSN_SELECTOR_LEN + data_len;
726 RSN_SELECTOR_PUT(pos, kde);
727 pos += RSN_SELECTOR_LEN;
728 os_memcpy(pos, data, data_len);
729 pos += data_len;
730
731 return pos;
732}
733
734
735static size_t wpa_mlo_link_kde_len(struct wpa_sm *sm)
736{
737 int i;
738 unsigned int num_links = 0;
739
Sunil Ravi88611412024-06-28 17:34:56 +0000740 for (i = 0; i < MAX_NUM_MLO_LINKS; i++) {
741 if (sm->mlo.assoc_link_id != i && (sm->mlo.req_links & BIT(i)))
Sunil Ravi77d572f2023-01-17 23:58:31 +0000742 num_links++;
743 }
744
745 return num_links * (RSN_SELECTOR_LEN + 1 + ETH_ALEN + 2);
746}
747
748
749static u8 * wpa_mlo_link_kde(struct wpa_sm *sm, u8 *pos)
750{
751 int i;
752 u8 hdr[1 + ETH_ALEN];
753
Sunil Ravi88611412024-06-28 17:34:56 +0000754 for (i = 0; i < MAX_NUM_MLO_LINKS; i++) {
755 if (sm->mlo.assoc_link_id == i || !(sm->mlo.req_links & BIT(i)))
Sunil Ravi77d572f2023-01-17 23:58:31 +0000756 continue;
757
758 wpa_printf(MSG_DEBUG,
759 "MLO: Add MLO Link %d KDE in EAPOL-Key 2/4", i);
760 hdr[0] = i & 0xF; /* LinkID; no RSNE or RSNXE */
761 os_memcpy(&hdr[1], sm->mlo.links[i].addr, ETH_ALEN);
762 pos = rsn_add_kde(pos, RSN_KEY_DATA_MLO_LINK, hdr, sizeof(hdr));
763 }
764
765 return pos;
766}
767
768
769static bool is_valid_ap_mld_mac_kde(struct wpa_sm *sm, const u8 *mac_kde)
770{
771 return mac_kde &&
Sunil Ravieb83e2a2024-06-28 17:34:56 +0000772 os_memcmp(mac_kde, sm->mlo.ap_mld_addr, ETH_ALEN) == 0;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000773}
774
775
776static void wpas_swap_tkip_mic_keys(struct wpa_ptk *ptk)
777{
778 u8 buf[8];
779
780 /* Supplicant: swap tx/rx Mic keys */
781 os_memcpy(buf, &ptk->tk[16], 8);
782 os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
783 os_memcpy(&ptk->tk[24], buf, 8);
784 forced_memzero(buf, sizeof(buf));
785}
786
787
788static void wpa_supplicant_process_1_of_4_wpa(struct wpa_sm *sm,
789 const unsigned char *src_addr,
790 const struct wpa_eapol_key *key,
791 u16 ver, const u8 *key_data,
792 size_t key_data_len,
793 enum frame_encryption encrypted)
794{
795 struct wpa_eapol_ie_parse ie;
796 struct wpa_ptk *ptk;
797 int res;
798
799 if (wpa_sm_get_network_ctx(sm) == NULL) {
800 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
801 "WPA: No SSID info found (msg 1 of 4)");
802 return;
803 }
804
805 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
806 "WPA: RX message 1 of 4-Way Handshake from " MACSTR
807 " (ver=%d)", MAC2STR(src_addr), ver);
808
809 os_memset(&ie, 0, sizeof(ie));
810
811 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
812 if (res == -2) {
813 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
814 "WPA: Do not reply to msg 1/4 - requesting full EAP authentication");
815 return;
816 }
817 if (res)
818 goto failed;
819
820 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
821
822 if (sm->renew_snonce) {
823 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
824 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
825 "WPA: Failed to get random data for SNonce");
826 goto failed;
827 }
828 sm->renew_snonce = 0;
829 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
830 sm->snonce, WPA_NONCE_LEN);
831 }
832
833 /* Calculate PTK which will be stored as a temporary PTK until it has
834 * been verified when processing message 3/4. */
835 ptk = &sm->tptk;
836 if (wpa_derive_ptk(sm, src_addr, key, ptk) < 0)
837 goto failed;
838 if (sm->pairwise_cipher == WPA_CIPHER_TKIP)
839 wpas_swap_tkip_mic_keys(ptk);
840 sm->tptk_set = 1;
841
842 if (wpa_supplicant_send_2_of_4(sm, wpa_sm_get_auth_addr(sm), key, ver,
843 sm->snonce, sm->assoc_wpa_ie,
844 sm->assoc_wpa_ie_len, ptk) < 0)
845 goto failed;
846
847 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
848 return;
849
850failed:
851 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
852}
853
854
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700855static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
856 const unsigned char *src_addr,
857 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700858 u16 ver, const u8 *key_data,
Sunil8cd6f4d2022-06-28 18:40:46 +0000859 size_t key_data_len,
860 enum frame_encryption encrypted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861{
862 struct wpa_eapol_ie_parse ie;
863 struct wpa_ptk *ptk;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700864 int res;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800865 u8 *kde, *kde_buf = NULL;
866 size_t kde_len;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000867 size_t mlo_kde_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868
Sunil8cd6f4d2022-06-28 18:40:46 +0000869 if (encrypted == FRAME_NOT_ENCRYPTED && sm->tk_set &&
870 wpa_sm_pmf_enabled(sm)) {
871 wpa_printf(MSG_DEBUG,
872 "RSN: Discard unencrypted EAPOL-Key msg 1/4 when TK is set and PMF is enabled");
873 return;
874 }
875
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700876 if (wpa_sm_get_network_ctx(sm) == NULL) {
877 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
878 "found (msg 1 of 4)");
879 return;
880 }
881
Hai Shalomfdcde762020-04-02 11:19:20 -0700882 if (sm->wpa_deny_ptk0_rekey && !sm->use_ext_key_id &&
883 wpa_sm_get_state(sm) == WPA_COMPLETED) {
884 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
885 "WPA: PTK0 rekey not allowed, reconnecting");
886 wpa_sm_reconnect(sm);
887 return;
888 }
889
Roshan Pius5e7db942018-04-12 12:27:41 -0700890 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: RX message 1 of 4-Way "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700891 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
892
893 os_memset(&ie, 0, sizeof(ie));
894
Sunil Ravi77d572f2023-01-17 23:58:31 +0000895 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
896 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data", key_data, key_data_len);
897 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0) {
898 wpa_printf(MSG_DEBUG,
899 "RSN: Discard EAPOL-Key msg 1/4 with invalid IEs/KDEs");
900 return;
901 }
902 if (ie.pmkid) {
903 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from Authenticator",
904 ie.pmkid, PMKID_LEN);
905 }
906
907 if (sm->mlo.valid_links && !is_valid_ap_mld_mac_kde(sm, ie.mac_addr)) {
908 wpa_printf(MSG_INFO,
909 "RSN: Discard EAPOL-Key msg 1/4 with invalid AP MLD MAC address KDE");
910 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700911 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912
913 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
914 if (res == -2) {
915 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
916 "msg 1/4 - requesting full EAP authentication");
917 return;
918 }
919 if (res)
920 goto failed;
921
Sunil8cd6f4d2022-06-28 18:40:46 +0000922 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
923
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700924 if (sm->renew_snonce) {
925 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
926 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
927 "WPA: Failed to get random data for SNonce");
928 goto failed;
929 }
930 sm->renew_snonce = 0;
931 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
932 sm->snonce, WPA_NONCE_LEN);
933 }
934
935 /* Calculate PTK which will be stored as a temporary PTK until it has
936 * been verified when processing message 3/4. */
937 ptk = &sm->tptk;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700938 if (wpa_derive_ptk(sm, src_addr, key, ptk) < 0)
939 goto failed;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000940 if (sm->pairwise_cipher == WPA_CIPHER_TKIP)
941 wpas_swap_tkip_mic_keys(ptk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700942 sm->tptk_set = 1;
943
Sunil Ravi77d572f2023-01-17 23:58:31 +0000944 /* Add MLO Link KDE and MAC KDE in M2 for ML connection */
945 if (sm->mlo.valid_links)
946 mlo_kde_len = wpa_mlo_link_kde_len(sm) +
947 RSN_SELECTOR_LEN + ETH_ALEN + 2;
948
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800949 kde = sm->assoc_wpa_ie;
950 kde_len = sm->assoc_wpa_ie_len;
Hai Shalomc3565922019-10-28 11:58:20 -0700951 kde_buf = os_malloc(kde_len +
952 2 + RSN_SELECTOR_LEN + 3 +
953 sm->assoc_rsnxe_len +
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700954 2 + RSN_SELECTOR_LEN + 1 +
Sunil Ravi77d572f2023-01-17 23:58:31 +0000955 2 + RSN_SELECTOR_LEN + 2 + mlo_kde_len);
956
Hai Shalomc3565922019-10-28 11:58:20 -0700957 if (!kde_buf)
958 goto failed;
959 os_memcpy(kde_buf, kde, kde_len);
960 kde = kde_buf;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800961
Hai Shalom74f70d42019-02-11 14:42:39 -0800962#ifdef CONFIG_OCV
963 if (wpa_sm_ocv_enabled(sm)) {
964 struct wpa_channel_info ci;
965 u8 *pos;
966
Hai Shalomc3565922019-10-28 11:58:20 -0700967 pos = kde + kde_len;
Hai Shalom74f70d42019-02-11 14:42:39 -0800968 if (wpa_sm_channel_info(sm, &ci) != 0) {
969 wpa_printf(MSG_WARNING,
970 "Failed to get channel info for OCI element in EAPOL-Key 2/4");
971 goto failed;
972 }
Hai Shalom899fcc72020-10-19 14:38:18 -0700973#ifdef CONFIG_TESTING_OPTIONS
974 if (sm->oci_freq_override_eapol) {
975 wpa_printf(MSG_INFO,
976 "TEST: Override OCI KDE frequency %d -> %d MHz",
977 ci.frequency, sm->oci_freq_override_eapol);
978 ci.frequency = sm->oci_freq_override_eapol;
979 }
980#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -0800981
Hai Shalom74f70d42019-02-11 14:42:39 -0800982 if (ocv_insert_oci_kde(&ci, &pos) < 0)
983 goto failed;
984 kde_len = pos - kde;
985 }
986#endif /* CONFIG_OCV */
987
Hai Shalomc3565922019-10-28 11:58:20 -0700988 if (sm->assoc_rsnxe && sm->assoc_rsnxe_len) {
989 os_memcpy(kde + kde_len, sm->assoc_rsnxe, sm->assoc_rsnxe_len);
990 kde_len += sm->assoc_rsnxe_len;
991 }
992
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800993#ifdef CONFIG_P2P
994 if (sm->p2p) {
Hai Shalomc3565922019-10-28 11:58:20 -0700995 u8 *pos;
996
997 wpa_printf(MSG_DEBUG,
998 "P2P: Add IP Address Request KDE into EAPOL-Key 2/4");
999 pos = kde + kde_len;
1000 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
1001 *pos++ = RSN_SELECTOR_LEN + 1;
1002 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
1003 pos += RSN_SELECTOR_LEN;
1004 *pos++ = 0x01;
1005 kde_len = pos - kde;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001006 }
1007#endif /* CONFIG_P2P */
1008
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001009#ifdef CONFIG_DPP2
1010 if (DPP_VERSION > 1 && sm->key_mgmt == WPA_KEY_MGMT_DPP) {
1011 u8 *pos;
1012
1013 wpa_printf(MSG_DEBUG, "DPP: Add DPP KDE into EAPOL-Key 2/4");
1014 pos = kde + kde_len;
1015 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
1016 *pos++ = RSN_SELECTOR_LEN + 2;
1017 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_DPP);
1018 pos += RSN_SELECTOR_LEN;
1019 *pos++ = DPP_VERSION; /* Protocol Version */
1020 *pos = 0; /* Flags */
1021 if (sm->dpp_pfs == 0)
1022 *pos |= DPP_KDE_PFS_ALLOWED;
1023 else if (sm->dpp_pfs == 1)
1024 *pos |= DPP_KDE_PFS_ALLOWED | DPP_KDE_PFS_REQUIRED;
1025 pos++;
1026 kde_len = pos - kde;
1027 }
1028#endif /* CONFIG_DPP2 */
1029
Sunil Ravi77d572f2023-01-17 23:58:31 +00001030 if (sm->mlo.valid_links) {
1031 u8 *pos;
1032
1033 /* Add MAC KDE */
1034 wpa_printf(MSG_DEBUG, "MLO: Add MAC KDE into EAPOL-Key 2/4");
1035 pos = kde + kde_len;
1036 pos = rsn_add_kde(pos, RSN_KEY_DATA_MAC_ADDR, sm->own_addr,
1037 ETH_ALEN);
1038
1039 /* Add MLO Link KDE */
1040 wpa_printf(MSG_DEBUG, "Add MLO Link KDE(s) into EAPOL-Key 2/4");
1041 pos = wpa_mlo_link_kde(sm, pos);
1042 kde_len = pos - kde;
1043 }
1044
1045 if (wpa_supplicant_send_2_of_4(sm, wpa_sm_get_auth_addr(sm), key, ver,
1046 sm->snonce, kde, kde_len, ptk) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001047 goto failed;
1048
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001049 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001050 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
1051 return;
1052
1053failed:
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001054 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001055 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1056}
1057
1058
1059static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
1060{
1061 struct wpa_sm *sm = eloop_ctx;
1062 rsn_preauth_candidate_process(sm);
1063}
1064
1065
1066static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
1067 const u8 *addr, int secure)
1068{
1069 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1070 "WPA: Key negotiation completed with "
1071 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
1072 wpa_cipher_txt(sm->pairwise_cipher),
1073 wpa_cipher_txt(sm->group_cipher));
1074 wpa_sm_cancel_auth_timeout(sm);
1075 wpa_sm_set_state(sm, WPA_COMPLETED);
1076
1077 if (secure) {
1078 wpa_sm_mlme_setprotection(
1079 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
1080 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
Hai Shalome21d4e82020-04-29 16:34:06 -07001081 eapol_sm_notify_portValid(sm->eapol, true);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001082 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
1083 sm->key_mgmt == WPA_KEY_MGMT_DPP ||
1084 sm->key_mgmt == WPA_KEY_MGMT_OWE)
Hai Shalome21d4e82020-04-29 16:34:06 -07001085 eapol_sm_notify_eap_success(sm->eapol, true);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001086 /*
1087 * Start preauthentication after a short wait to avoid a
1088 * possible race condition between the data receive and key
1089 * configuration after the 4-Way Handshake. This increases the
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001090 * likelihood of the first preauth EAPOL-Start frame getting to
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001091 * the target AP.
1092 */
Hai Shalom74f70d42019-02-11 14:42:39 -08001093 if (!dl_list_empty(&sm->pmksa_candidates))
1094 eloop_register_timeout(1, 0, wpa_sm_start_preauth,
1095 sm, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001096 }
1097
1098 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
1099 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1100 "RSN: Authenticator accepted "
1101 "opportunistic PMKSA entry - marking it valid");
1102 sm->cur_pmksa->opportunistic = 0;
1103 }
1104
1105#ifdef CONFIG_IEEE80211R
1106 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
1107 /* Prepare for the next transition */
1108 wpa_ft_prepare_auth_request(sm, NULL);
1109 }
1110#endif /* CONFIG_IEEE80211R */
1111}
1112
1113
1114static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
1115{
1116 struct wpa_sm *sm = eloop_ctx;
1117 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
1118 wpa_sm_key_request(sm, 0, 1);
1119}
1120
1121
1122static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
Hai Shalomfdcde762020-04-02 11:19:20 -07001123 const struct wpa_eapol_key *key,
1124 enum key_flag key_flag)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001125{
1126 int keylen, rsclen;
1127 enum wpa_alg alg;
1128 const u8 *key_rsc;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001129
Mathy Vanhoefc66556c2017-09-29 04:22:51 +02001130 if (sm->ptk.installed) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001131 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1132 "WPA: Do not re-install same PTK to the driver");
1133 return 0;
1134 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001135
1136 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1137 "WPA: Installing PTK to the driver");
1138
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001139 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001140 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
1141 "Suite: NONE - do not use pairwise keys");
1142 return 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001143 }
1144
1145 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001146 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1147 "WPA: Unsupported pairwise cipher %d",
1148 sm->pairwise_cipher);
1149 return -1;
1150 }
1151
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001152 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
1153 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001154 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
1155 wpa_printf(MSG_DEBUG, "WPA: TK length mismatch: %d != %lu",
1156 keylen, (long unsigned int) sm->ptk.tk_len);
1157 return -1;
1158 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001159 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
1160
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001161 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001162 key_rsc = null_rsc;
1163 } else {
1164 key_rsc = key->key_rsc;
1165 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
1166 }
1167
Sunil Ravi77d572f2023-01-17 23:58:31 +00001168 if (wpa_sm_set_key(sm, -1, alg, wpa_sm_get_auth_addr(sm),
1169 sm->keyidx_active, 1, key_rsc, rsclen, sm->ptk.tk,
1170 keylen, KEY_FLAG_PAIRWISE | key_flag) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001171 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Sunil Ravi77d572f2023-01-17 23:58:31 +00001172 "WPA: Failed to set PTK to the driver (alg=%d keylen=%d auth_addr="
Hai Shalomfdcde762020-04-02 11:19:20 -07001173 MACSTR " idx=%d key_flag=0x%x)",
Sunil Ravi77d572f2023-01-17 23:58:31 +00001174 alg, keylen, MAC2STR(wpa_sm_get_auth_addr(sm)),
Hai Shalomfdcde762020-04-02 11:19:20 -07001175 sm->keyidx_active, key_flag);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001176 return -1;
1177 }
1178
Sunil Ravi89eba102022-09-13 21:04:37 -07001179#ifdef CONFIG_PASN
1180 if (sm->secure_ltf &&
1181 ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF) &&
1182 wpa_sm_set_ltf_keyseed(sm, sm->own_addr, sm->bssid,
1183 sm->ptk.ltf_keyseed_len,
1184 sm->ptk.ltf_keyseed) < 0) {
1185 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1186 "WPA: Failed to set LTF keyseed to the driver (keylen=%zu bssid="
1187 MACSTR ")", sm->ptk.ltf_keyseed_len,
1188 MAC2STR(sm->bssid));
1189 return -1;
1190 }
1191#endif /* CONFIG_PASN */
1192
Hai Shalom60840252021-02-19 19:02:11 -08001193 wpa_sm_store_ptk(sm, sm->bssid, sm->pairwise_cipher,
1194 sm->dot11RSNAConfigPMKLifetime, &sm->ptk);
1195
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001196 /* TK is not needed anymore in supplicant */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001197 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001198 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +02001199 sm->ptk.installed = 1;
Sunil8cd6f4d2022-06-28 18:40:46 +00001200 sm->tk_set = true;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001201
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001202 if (sm->wpa_ptk_rekey) {
1203 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
1204 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
1205 sm, NULL);
1206 }
Hai Shalomfdcde762020-04-02 11:19:20 -07001207 return 0;
1208}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001209
Hai Shalomfdcde762020-04-02 11:19:20 -07001210
1211static int wpa_supplicant_activate_ptk(struct wpa_sm *sm)
1212{
1213 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Sunil Ravi77d572f2023-01-17 23:58:31 +00001214 "WPA: Activate PTK (idx=%d auth_addr=" MACSTR ")",
1215 sm->keyidx_active, MAC2STR(wpa_sm_get_auth_addr(sm)));
Hai Shalomfdcde762020-04-02 11:19:20 -07001216
Sunil Ravi77d572f2023-01-17 23:58:31 +00001217 if (wpa_sm_set_key(sm, -1, 0, wpa_sm_get_auth_addr(sm),
1218 sm->keyidx_active, 0, NULL, 0, NULL, 0,
1219 KEY_FLAG_PAIRWISE_RX_TX_MODIFY) < 0) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001220 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Sunil Ravi77d572f2023-01-17 23:58:31 +00001221 "WPA: Failed to activate PTK for TX (idx=%d auth_addr="
1222 MACSTR ")", sm->keyidx_active,
1223 MAC2STR(wpa_sm_get_auth_addr(sm)));
Hai Shalomfdcde762020-04-02 11:19:20 -07001224 return -1;
1225 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001226 return 0;
1227}
1228
1229
1230static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
1231 int group_cipher,
1232 int keylen, int maxkeylen,
1233 int *key_rsc_len,
1234 enum wpa_alg *alg)
1235{
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001236 int klen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001237
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001238 *alg = wpa_cipher_to_alg(group_cipher);
1239 if (*alg == WPA_ALG_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001240 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1241 "WPA: Unsupported Group Cipher %d",
1242 group_cipher);
1243 return -1;
1244 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001245 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001246
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001247 klen = wpa_cipher_key_len(group_cipher);
1248 if (keylen != klen || maxkeylen < klen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001249 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1250 "WPA: Unsupported %s Group Cipher key length %d (%d)",
1251 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001252 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001253 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001254 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001255}
1256
1257
1258struct wpa_gtk_data {
1259 enum wpa_alg alg;
1260 int tx, key_rsc_len, keyidx;
1261 u8 gtk[32];
1262 int gtk_len;
1263};
1264
1265
1266static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
1267 const struct wpa_gtk_data *gd,
Jouni Malinen58c0e962017-10-01 12:12:24 +03001268 const u8 *key_rsc, int wnm_sleep)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001269{
1270 const u8 *_gtk = gd->gtk;
1271 u8 gtk_buf[32];
1272
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001273 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +03001274 if ((sm->gtk.gtk_len == (size_t) gd->gtk_len &&
1275 os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) ||
1276 (sm->gtk_wnm_sleep.gtk_len == (size_t) gd->gtk_len &&
1277 os_memcmp(sm->gtk_wnm_sleep.gtk, gd->gtk,
1278 sm->gtk_wnm_sleep.gtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001279 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1280 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
1281 gd->keyidx, gd->tx, gd->gtk_len);
1282 return 0;
1283 }
1284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001285 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
1286 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1287 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
1288 gd->keyidx, gd->tx, gd->gtk_len);
1289 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
1290 if (sm->group_cipher == WPA_CIPHER_TKIP) {
1291 /* Swap Tx/Rx keys for Michael MIC */
1292 os_memcpy(gtk_buf, gd->gtk, 16);
1293 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
1294 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
1295 _gtk = gtk_buf;
1296 }
1297 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00001298 if (wpa_sm_set_key(sm, -1, gd->alg, NULL,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001299 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07001300 _gtk, gd->gtk_len,
1301 KEY_FLAG_GROUP_RX_TX_DEFAULT) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001302 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1303 "WPA: Failed to set GTK to the driver "
1304 "(Group only)");
Hai Shalom81f62d82019-07-22 12:10:00 -07001305 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001306 return -1;
1307 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00001308 } else if (wpa_sm_set_key(sm, -1, gd->alg, broadcast_ether_addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001309 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07001310 _gtk, gd->gtk_len, KEY_FLAG_GROUP_RX) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001311 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1312 "WPA: Failed to set GTK to "
1313 "the driver (alg=%d keylen=%d keyidx=%d)",
1314 gd->alg, gd->gtk_len, gd->keyidx);
Hai Shalom81f62d82019-07-22 12:10:00 -07001315 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001316 return -1;
1317 }
Hai Shalom81f62d82019-07-22 12:10:00 -07001318 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001319
Jouni Malinen58c0e962017-10-01 12:12:24 +03001320 if (wnm_sleep) {
1321 sm->gtk_wnm_sleep.gtk_len = gd->gtk_len;
1322 os_memcpy(sm->gtk_wnm_sleep.gtk, gd->gtk,
1323 sm->gtk_wnm_sleep.gtk_len);
1324 } else {
1325 sm->gtk.gtk_len = gd->gtk_len;
1326 os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
1327 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001328
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001329 return 0;
1330}
1331
1332
Sunil Ravi77d572f2023-01-17 23:58:31 +00001333static int wpa_supplicant_install_mlo_gtk(struct wpa_sm *sm, u8 link_id,
1334 const struct wpa_gtk_data *gd,
1335 const u8 *key_rsc, int wnm_sleep)
1336{
1337 const u8 *gtk = gd->gtk;
1338
1339 /* Detect possible key reinstallation */
1340 if ((sm->mlo.links[link_id].gtk.gtk_len == (size_t) gd->gtk_len &&
1341 os_memcmp(sm->mlo.links[link_id].gtk.gtk, gd->gtk,
1342 sm->mlo.links[link_id].gtk.gtk_len) == 0) ||
1343 (sm->mlo.links[link_id].gtk_wnm_sleep.gtk_len ==
1344 (size_t) gd->gtk_len &&
1345 os_memcmp(sm->mlo.links[link_id].gtk_wnm_sleep.gtk, gd->gtk,
1346 sm->mlo.links[link_id].gtk_wnm_sleep.gtk_len) == 0)) {
1347 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1348 "RSN: Not reinstalling already in-use GTK to the driver (link_id=%d keyidx=%d tx=%d len=%d)",
1349 link_id, gd->keyidx, gd->tx, gd->gtk_len);
1350 return 0;
1351 }
1352
1353 wpa_hexdump_link_key(MSG_DEBUG, link_id, "RSN: Group Key", gd->gtk,
1354 gd->gtk_len);
1355 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1356 "RSN: Installing GTK to the driver (link_id=%d keyidx=%d tx=%d len=%d)",
1357 link_id, gd->keyidx, gd->tx, gd->gtk_len);
1358 wpa_hexdump_link(MSG_DEBUG, link_id, "RSN: RSC",
1359 key_rsc, gd->key_rsc_len);
1360 if (wpa_sm_set_key(sm, link_id, gd->alg, broadcast_ether_addr,
1361 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len, gtk,
1362 gd->gtk_len, KEY_FLAG_GROUP_RX) < 0) {
1363 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1364 "RSN: Failed to set GTK to the driver (link_id=%d alg=%d keylen=%d keyidx=%d)",
1365 link_id, gd->alg, gd->gtk_len, gd->keyidx);
1366 return -1;
1367 }
1368
1369 if (wnm_sleep) {
1370 sm->mlo.links[link_id].gtk_wnm_sleep.gtk_len = gd->gtk_len;
1371 os_memcpy(sm->mlo.links[link_id].gtk_wnm_sleep.gtk, gd->gtk,
1372 sm->mlo.links[link_id].gtk_wnm_sleep.gtk_len);
1373 } else {
1374 sm->mlo.links[link_id].gtk.gtk_len = gd->gtk_len;
1375 os_memcpy(sm->mlo.links[link_id].gtk.gtk, gd->gtk,
1376 sm->mlo.links[link_id].gtk.gtk_len);
1377 }
1378
1379 return 0;
1380}
1381
1382
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001383static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
1384 int tx)
1385{
1386 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
1387 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
1388 * seemed to set this bit (incorrectly, since Tx is only when
1389 * doing Group Key only APs) and without this workaround, the
1390 * data connection does not work because wpa_supplicant
1391 * configured non-zero keyidx to be used for unicast. */
1392 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1393 "WPA: Tx bit set for GTK, but pairwise "
1394 "keys are used - ignore Tx bit");
1395 return 0;
1396 }
1397 return tx;
1398}
1399
1400
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001401static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
1402 const u8 *rsc)
1403{
1404 int rsclen;
1405
1406 if (!sm->wpa_rsc_relaxation)
1407 return 0;
1408
1409 rsclen = wpa_cipher_rsc_len(sm->group_cipher);
1410
1411 /*
1412 * Try to detect RSC (endian) corruption issue where the AP sends
1413 * the RSC bytes in EAPOL-Key message in the wrong order, both if
1414 * it's actually a 6-byte field (as it should be) and if it treats
1415 * it as an 8-byte field.
1416 * An AP model known to have this bug is the Sapido RB-1632.
1417 */
1418 if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
1419 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1420 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
1421 rsc[0], rsc[1], rsc[2], rsc[3],
1422 rsc[4], rsc[5], rsc[6], rsc[7]);
1423
1424 return 1;
1425 }
1426
1427 return 0;
1428}
1429
1430
Sunil Ravi77d572f2023-01-17 23:58:31 +00001431static int wpa_supplicant_mlo_gtk(struct wpa_sm *sm, u8 link_id, const u8 *gtk,
1432 size_t gtk_len, int key_info)
1433{
1434 struct wpa_gtk_data gd;
1435 const u8 *key_rsc;
1436 int ret;
1437
1438 /*
1439 * MLO GTK KDE format:
1440 * KeyID[bits 0-1], Tx [bit 2], Reserved [bit 3], link id [4-7]
1441 * PN
1442 * GTK
1443 */
1444 os_memset(&gd, 0, sizeof(gd));
1445 wpa_hexdump_link_key(MSG_DEBUG, link_id,
1446 "RSN: received GTK in pairwise handshake",
1447 gtk, gtk_len);
1448
1449 if (gtk_len < RSN_MLO_GTK_KDE_PREFIX_LENGTH ||
1450 gtk_len - RSN_MLO_GTK_KDE_PREFIX_LENGTH > sizeof(gd.gtk))
1451 return -1;
1452
1453 gd.keyidx = gtk[0] & 0x3;
1454 gtk += 1;
1455 gtk_len -= 1;
1456
1457 key_rsc = gtk;
1458
1459 gtk += 6;
1460 gtk_len -= 6;
1461
1462 os_memcpy(gd.gtk, gtk, gtk_len);
1463 gd.gtk_len = gtk_len;
1464
1465 ret = 0;
1466 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher, gtk_len,
1467 gtk_len, &gd.key_rsc_len,
1468 &gd.alg) ||
1469 wpa_supplicant_install_mlo_gtk(sm, link_id, &gd, key_rsc, 0)) {
1470 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1471 "RSN: Failed to install GTK for MLO Link ID %u",
1472 link_id);
1473 ret = -1;
1474 goto out;
1475 }
1476
1477out:
1478 forced_memzero(&gd, sizeof(gd));
1479 return ret;
1480}
1481
1482
1483static int wpa_supplicant_pairwise_mlo_gtk(struct wpa_sm *sm,
1484 const struct wpa_eapol_key *key,
1485 struct wpa_eapol_ie_parse *ie,
1486 int key_info)
1487{
1488 u8 i;
1489
Sunil Ravi88611412024-06-28 17:34:56 +00001490 for (i = 0; i < MAX_NUM_MLO_LINKS; i++) {
1491 if (!(sm->mlo.valid_links & BIT(i)))
1492 continue;
1493
Sunil Ravi77d572f2023-01-17 23:58:31 +00001494 if (!ie->mlo_gtk[i]) {
1495 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1496 "MLO RSN: GTK not found for link ID %u", i);
1497 return -1;
1498 }
1499
1500 if (wpa_supplicant_mlo_gtk(sm, i, ie->mlo_gtk[i],
1501 ie->mlo_gtk_len[i], key_info))
1502 return -1;
1503 }
1504
1505 return 0;
1506}
1507
1508
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001509static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
1510 const struct wpa_eapol_key *key,
1511 const u8 *gtk, size_t gtk_len,
1512 int key_info)
1513{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001514 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001515 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001516
1517 /*
1518 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
1519 * GTK KDE format:
1520 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
1521 * Reserved [bits 0-7]
1522 * GTK
1523 */
1524
1525 os_memset(&gd, 0, sizeof(gd));
1526 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
1527 gtk, gtk_len);
1528
1529 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
1530 return -1;
1531
1532 gd.keyidx = gtk[0] & 0x3;
1533 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1534 !!(gtk[0] & BIT(2)));
1535 gtk += 2;
1536 gtk_len -= 2;
1537
1538 os_memcpy(gd.gtk, gtk, gtk_len);
1539 gd.gtk_len = gtk_len;
1540
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001541 key_rsc = key->key_rsc;
1542 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1543 key_rsc = null_rsc;
1544
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001545 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
1546 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1547 gtk_len, gtk_len,
1548 &gd.key_rsc_len, &gd.alg) ||
Jouni Malinen58c0e962017-10-01 12:12:24 +03001549 wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001550 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1551 "RSN: Failed to install GTK");
Hai Shalom81f62d82019-07-22 12:10:00 -07001552 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001553 return -1;
1554 }
Hai Shalom81f62d82019-07-22 12:10:00 -07001555 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001556
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001557 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001558}
1559
1560
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001561static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
Jouni Malinen58c0e962017-10-01 12:12:24 +03001562 const struct wpa_igtk_kde *igtk,
1563 int wnm_sleep)
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001564{
1565 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1566 u16 keyidx = WPA_GET_LE16(igtk->keyid);
1567
1568 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +03001569 if ((sm->igtk.igtk_len == len &&
1570 os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) ||
1571 (sm->igtk_wnm_sleep.igtk_len == len &&
1572 os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1573 sm->igtk_wnm_sleep.igtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001574 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1575 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
1576 keyidx);
1577 return 0;
1578 }
1579
1580 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001581 "WPA: IGTK keyid %d pn " COMPACT_MACSTR,
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001582 keyidx, MAC2STR(igtk->pn));
1583 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
1584 if (keyidx > 4095) {
1585 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1586 "WPA: Invalid IGTK KeyID %d", keyidx);
1587 return -1;
1588 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00001589 if (wpa_sm_set_key(sm, -1, wpa_cipher_to_alg(sm->mgmt_group_cipher),
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001590 broadcast_ether_addr,
1591 keyidx, 0, igtk->pn, sizeof(igtk->pn),
Hai Shalomfdcde762020-04-02 11:19:20 -07001592 igtk->igtk, len, KEY_FLAG_GROUP_RX) < 0) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07001593 if (keyidx == 0x0400 || keyidx == 0x0500) {
1594 /* Assume the AP has broken PMF implementation since it
1595 * seems to have swapped the KeyID bytes. The AP cannot
1596 * be trusted to implement BIP correctly or provide a
1597 * valid IGTK, so do not try to configure this key with
1598 * swapped KeyID bytes. Instead, continue without
1599 * configuring the IGTK so that the driver can drop any
1600 * received group-addressed robust management frames due
1601 * to missing keys.
1602 *
1603 * Normally, this error behavior would result in us
1604 * disconnecting, but there are number of deployed APs
1605 * with this broken behavior, so as an interoperability
1606 * workaround, allow the connection to proceed. */
1607 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1608 "WPA: Ignore IGTK configuration error due to invalid IGTK KeyID byte order");
1609 } else {
1610 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1611 "WPA: Failed to configure IGTK to the driver");
1612 return -1;
1613 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001614 }
1615
Jouni Malinen58c0e962017-10-01 12:12:24 +03001616 if (wnm_sleep) {
1617 sm->igtk_wnm_sleep.igtk_len = len;
1618 os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1619 sm->igtk_wnm_sleep.igtk_len);
1620 } else {
1621 sm->igtk.igtk_len = len;
1622 os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
1623 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001624
1625 return 0;
1626}
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001627
1628
Hai Shalomfdcde762020-04-02 11:19:20 -07001629static int wpa_supplicant_install_bigtk(struct wpa_sm *sm,
1630 const struct wpa_bigtk_kde *bigtk,
1631 int wnm_sleep)
1632{
1633 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1634 u16 keyidx = WPA_GET_LE16(bigtk->keyid);
1635
1636 /* Detect possible key reinstallation */
1637 if ((sm->bigtk.bigtk_len == len &&
1638 os_memcmp(sm->bigtk.bigtk, bigtk->bigtk,
1639 sm->bigtk.bigtk_len) == 0) ||
1640 (sm->bigtk_wnm_sleep.bigtk_len == len &&
1641 os_memcmp(sm->bigtk_wnm_sleep.bigtk, bigtk->bigtk,
1642 sm->bigtk_wnm_sleep.bigtk_len) == 0)) {
1643 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1644 "WPA: Not reinstalling already in-use BIGTK to the driver (keyidx=%d)",
1645 keyidx);
1646 return 0;
1647 }
1648
1649 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1650 "WPA: BIGTK keyid %d pn " COMPACT_MACSTR,
1651 keyidx, MAC2STR(bigtk->pn));
1652 wpa_hexdump_key(MSG_DEBUG, "WPA: BIGTK", bigtk->bigtk, len);
1653 if (keyidx < 6 || keyidx > 7) {
1654 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1655 "WPA: Invalid BIGTK KeyID %d", keyidx);
1656 return -1;
1657 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00001658 if (wpa_sm_set_key(sm, -1, wpa_cipher_to_alg(sm->mgmt_group_cipher),
Hai Shalomfdcde762020-04-02 11:19:20 -07001659 broadcast_ether_addr,
1660 keyidx, 0, bigtk->pn, sizeof(bigtk->pn),
1661 bigtk->bigtk, len, KEY_FLAG_GROUP_RX) < 0) {
1662 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1663 "WPA: Failed to configure BIGTK to the driver");
1664 return -1;
1665 }
1666
1667 if (wnm_sleep) {
1668 sm->bigtk_wnm_sleep.bigtk_len = len;
1669 os_memcpy(sm->bigtk_wnm_sleep.bigtk, bigtk->bigtk,
1670 sm->bigtk_wnm_sleep.bigtk_len);
1671 } else {
1672 sm->bigtk.bigtk_len = len;
1673 os_memcpy(sm->bigtk.bigtk, bigtk->bigtk, sm->bigtk.bigtk_len);
1674 }
1675
1676 return 0;
1677}
1678
1679
Sunil Ravi77d572f2023-01-17 23:58:31 +00001680static int wpa_supplicant_install_mlo_igtk(struct wpa_sm *sm, u8 link_id,
1681 const struct rsn_mlo_igtk_kde *igtk,
1682 int wnm_sleep)
1683{
1684 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1685 u16 keyidx = WPA_GET_LE16(igtk->keyid);
1686
1687 /* Detect possible key reinstallation */
1688 if ((sm->mlo.links[link_id].igtk.igtk_len == len &&
1689 os_memcmp(sm->mlo.links[link_id].igtk.igtk, igtk->igtk,
1690 sm->mlo.links[link_id].igtk.igtk_len) == 0) ||
1691 (sm->mlo.links[link_id].igtk_wnm_sleep.igtk_len == len &&
1692 os_memcmp(sm->mlo.links[link_id].igtk_wnm_sleep.igtk, igtk->igtk,
1693 sm->mlo.links[link_id].igtk_wnm_sleep.igtk_len) == 0)) {
1694 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1695 "RSN: Not reinstalling already in-use IGTK to the driver (link_id=%d keyidx=%d)",
1696 link_id, keyidx);
1697 return 0;
1698 }
1699
1700 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1701 "RSN: MLO Link %u IGTK keyid %d pn " COMPACT_MACSTR,
1702 link_id, keyidx, MAC2STR(igtk->pn));
1703 wpa_hexdump_link_key(MSG_DEBUG, link_id, "RSN: IGTK", igtk->igtk, len);
1704 if (keyidx > 4095) {
1705 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1706 "RSN: Invalid MLO Link %d IGTK KeyID %d", link_id,
1707 keyidx);
1708 return -1;
1709 }
1710 if (wpa_sm_set_key(sm, link_id,
1711 wpa_cipher_to_alg(sm->mgmt_group_cipher),
1712 broadcast_ether_addr, keyidx, 0, igtk->pn,
1713 sizeof(igtk->pn), igtk->igtk, len,
1714 KEY_FLAG_GROUP_RX) < 0) {
1715 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1716 "RSN: Failed to configure MLO Link %d IGTK to the driver",
1717 link_id);
1718 return -1;
1719 }
1720
1721 if (wnm_sleep) {
1722 sm->mlo.links[link_id].igtk_wnm_sleep.igtk_len = len;
1723 os_memcpy(sm->mlo.links[link_id].igtk_wnm_sleep.igtk,
1724 igtk->igtk,
1725 sm->mlo.links[link_id].igtk_wnm_sleep.igtk_len);
1726 } else {
1727 sm->mlo.links[link_id].igtk.igtk_len = len;
1728 os_memcpy(sm->mlo.links[link_id].igtk.igtk, igtk->igtk,
1729 sm->mlo.links[link_id].igtk.igtk_len);
1730 }
1731
1732 return 0;
1733}
1734
1735
1736static int
1737wpa_supplicant_install_mlo_bigtk(struct wpa_sm *sm, u8 link_id,
1738 const struct rsn_mlo_bigtk_kde *bigtk,
1739 int wnm_sleep)
1740{
1741 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1742 u16 keyidx = WPA_GET_LE16(bigtk->keyid);
1743
1744 /* Detect possible key reinstallation */
1745 if ((sm->mlo.links[link_id].bigtk.bigtk_len == len &&
1746 os_memcmp(sm->mlo.links[link_id].bigtk.bigtk, bigtk->bigtk,
1747 sm->mlo.links[link_id].bigtk.bigtk_len) == 0) ||
1748 (sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk_len == len &&
1749 os_memcmp(sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk,
1750 bigtk->bigtk,
1751 sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk_len) ==
1752 0)) {
1753 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1754 "RSN: Not reinstalling already in-use BIGTK to the driver (link_id=%d keyidx=%d)",
1755 link_id, keyidx);
1756 return 0;
1757 }
1758
1759 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1760 "RSN: MLO Link %u BIGTK keyid %d pn " COMPACT_MACSTR,
1761 link_id, keyidx, MAC2STR(bigtk->pn));
1762 wpa_hexdump_link_key(MSG_DEBUG, link_id, "RSN: BIGTK", bigtk->bigtk,
1763 len);
1764 if (keyidx < 6 || keyidx > 7) {
1765 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1766 "RSN: Invalid MLO Link %d BIGTK KeyID %d", link_id,
1767 keyidx);
1768 return -1;
1769 }
1770 if (wpa_sm_set_key(sm, link_id,
1771 wpa_cipher_to_alg(sm->mgmt_group_cipher),
1772 broadcast_ether_addr, keyidx, 0, bigtk->pn,
1773 sizeof(bigtk->pn), bigtk->bigtk, len,
1774 KEY_FLAG_GROUP_RX) < 0) {
1775 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1776 "RSN: Failed to configure MLO Link %d BIGTK to the driver",
1777 link_id);
1778 return -1;
1779 }
1780
1781 if (wnm_sleep) {
1782 sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk_len = len;
1783 os_memcpy(sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk,
1784 bigtk->bigtk,
1785 sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk_len);
1786 } else {
1787 sm->mlo.links[link_id].bigtk.bigtk_len = len;
1788 os_memcpy(sm->mlo.links[link_id].bigtk.bigtk, bigtk->bigtk,
1789 sm->mlo.links[link_id].bigtk.bigtk_len);
1790 }
1791
1792 return 0;
1793}
1794
1795
1796static int _mlo_ieee80211w_set_keys(struct wpa_sm *sm, u8 link_id,
1797 struct wpa_eapol_ie_parse *ie)
1798{
1799 size_t len;
1800
1801 if (ie->mlo_igtk[link_id]) {
1802 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1803 if (ie->mlo_igtk_len[link_id] !=
1804 RSN_MLO_IGTK_KDE_PREFIX_LENGTH + len)
1805 return -1;
1806
1807 if (wpa_supplicant_install_mlo_igtk(
1808 sm, link_id,
1809 (const struct rsn_mlo_igtk_kde *)
1810 ie->mlo_igtk[link_id],
1811 0) < 0)
1812 return -1;
1813 }
1814
1815 if (ie->mlo_bigtk[link_id] && sm->beacon_prot) {
1816 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1817 if (ie->mlo_bigtk_len[link_id] !=
1818 RSN_MLO_BIGTK_KDE_PREFIX_LENGTH + len)
1819 return -1;
1820
1821 if (wpa_supplicant_install_mlo_bigtk(
1822 sm, link_id,
1823 (const struct rsn_mlo_bigtk_kde *)
1824 ie->mlo_bigtk[link_id],
1825 0) < 0)
1826 return -1;
1827 }
1828
1829 return 0;
1830}
1831
1832
1833static int mlo_ieee80211w_set_keys(struct wpa_sm *sm,
1834 struct wpa_eapol_ie_parse *ie)
1835{
1836 u8 i;
1837
1838 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) ||
1839 sm->mgmt_group_cipher == WPA_CIPHER_GTK_NOT_USED)
1840 return 0;
1841
Sunil Ravi88611412024-06-28 17:34:56 +00001842 for (i = 0; i < MAX_NUM_MLO_LINKS; i++) {
1843 if (!(sm->mlo.valid_links & BIT(i)))
1844 continue;
1845
Sunil Ravi77d572f2023-01-17 23:58:31 +00001846 if (_mlo_ieee80211w_set_keys(sm, i, ie))
1847 return -1;
1848 }
1849
1850 return 0;
1851}
1852
1853
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001854static int ieee80211w_set_keys(struct wpa_sm *sm,
1855 struct wpa_eapol_ie_parse *ie)
1856{
Hai Shalomfdcde762020-04-02 11:19:20 -07001857 size_t len;
1858
Hai Shalom60840252021-02-19 19:02:11 -08001859 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) ||
1860 sm->mgmt_group_cipher == WPA_CIPHER_GTK_NOT_USED)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001861 return 0;
1862
1863 if (ie->igtk) {
1864 const struct wpa_igtk_kde *igtk;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001865
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001866 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1867 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001868 return -1;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001869
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001870 igtk = (const struct wpa_igtk_kde *) ie->igtk;
Jouni Malinen58c0e962017-10-01 12:12:24 +03001871 if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001872 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001873 }
1874
Hai Shalomfdcde762020-04-02 11:19:20 -07001875 if (ie->bigtk && sm->beacon_prot) {
1876 const struct wpa_bigtk_kde *bigtk;
1877
1878 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1879 if (ie->bigtk_len != WPA_BIGTK_KDE_PREFIX_LEN + len)
1880 return -1;
1881
1882 bigtk = (const struct wpa_bigtk_kde *) ie->bigtk;
1883 if (wpa_supplicant_install_bigtk(sm, bigtk, 0) < 0)
1884 return -1;
1885 }
1886
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001887 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001888}
1889
1890
1891static void wpa_report_ie_mismatch(struct wpa_sm *sm,
1892 const char *reason, const u8 *src_addr,
1893 const u8 *wpa_ie, size_t wpa_ie_len,
1894 const u8 *rsn_ie, size_t rsn_ie_len)
1895{
1896 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
1897 reason, MAC2STR(src_addr));
1898
1899 if (sm->ap_wpa_ie) {
1900 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
1901 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
1902 }
1903 if (wpa_ie) {
1904 if (!sm->ap_wpa_ie) {
1905 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1906 "WPA: No WPA IE in Beacon/ProbeResp");
1907 }
1908 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
1909 wpa_ie, wpa_ie_len);
1910 }
1911
1912 if (sm->ap_rsn_ie) {
1913 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
1914 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
1915 }
1916 if (rsn_ie) {
1917 if (!sm->ap_rsn_ie) {
1918 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1919 "WPA: No RSN IE in Beacon/ProbeResp");
1920 }
1921 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
1922 rsn_ie, rsn_ie_len);
1923 }
1924
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001925 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001926}
1927
1928
1929#ifdef CONFIG_IEEE80211R
1930
1931static int ft_validate_mdie(struct wpa_sm *sm,
1932 const unsigned char *src_addr,
1933 struct wpa_eapol_ie_parse *ie,
1934 const u8 *assoc_resp_mdie)
1935{
1936 struct rsn_mdie *mdie;
1937
1938 mdie = (struct rsn_mdie *) (ie->mdie + 2);
1939 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
1940 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
1941 MOBILITY_DOMAIN_ID_LEN) != 0) {
1942 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
1943 "not match with the current mobility domain");
1944 return -1;
1945 }
1946
1947 if (assoc_resp_mdie &&
1948 (assoc_resp_mdie[1] != ie->mdie[1] ||
1949 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
1950 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
1951 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
1952 ie->mdie, 2 + ie->mdie[1]);
1953 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
1954 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
1955 return -1;
1956 }
1957
1958 return 0;
1959}
1960
1961
1962static int ft_validate_ftie(struct wpa_sm *sm,
1963 const unsigned char *src_addr,
1964 struct wpa_eapol_ie_parse *ie,
1965 const u8 *assoc_resp_ftie)
1966{
1967 if (ie->ftie == NULL) {
1968 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1969 "FT: No FTIE in EAPOL-Key msg 3/4");
1970 return -1;
1971 }
1972
1973 if (assoc_resp_ftie == NULL)
1974 return 0;
1975
1976 if (assoc_resp_ftie[1] != ie->ftie[1] ||
1977 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
1978 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
1979 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
1980 ie->ftie, 2 + ie->ftie[1]);
1981 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
1982 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
1983 return -1;
1984 }
1985
1986 return 0;
1987}
1988
1989
1990static int ft_validate_rsnie(struct wpa_sm *sm,
1991 const unsigned char *src_addr,
1992 struct wpa_eapol_ie_parse *ie)
1993{
1994 struct wpa_ie_data rsn;
1995
1996 if (!ie->rsn_ie)
1997 return 0;
1998
1999 /*
2000 * Verify that PMKR1Name from EAPOL-Key message 3/4
2001 * matches with the value we derived.
2002 */
2003 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
2004 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
2005 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
2006 "FT 4-way handshake message 3/4");
2007 return -1;
2008 }
2009
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002010 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
2011 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002012 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2013 "FT: PMKR1Name mismatch in "
2014 "FT 4-way handshake message 3/4");
2015 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
2016 rsn.pmkid, WPA_PMK_NAME_LEN);
2017 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
2018 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
2019 return -1;
2020 }
2021
2022 return 0;
2023}
2024
2025
2026static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
2027 const unsigned char *src_addr,
2028 struct wpa_eapol_ie_parse *ie)
2029{
2030 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
2031
2032 if (sm->assoc_resp_ies) {
2033 pos = sm->assoc_resp_ies;
2034 end = pos + sm->assoc_resp_ies_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002035 while (end - pos > 2) {
2036 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002037 break;
2038 switch (*pos) {
2039 case WLAN_EID_MOBILITY_DOMAIN:
2040 mdie = pos;
2041 break;
2042 case WLAN_EID_FAST_BSS_TRANSITION:
2043 ftie = pos;
2044 break;
2045 }
2046 pos += 2 + pos[1];
2047 }
2048 }
2049
2050 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
2051 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
2052 ft_validate_rsnie(sm, src_addr, ie) < 0)
2053 return -1;
2054
2055 return 0;
2056}
2057
2058#endif /* CONFIG_IEEE80211R */
2059
2060
2061static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
2062 const unsigned char *src_addr,
2063 struct wpa_eapol_ie_parse *ie)
2064{
2065 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
2066 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2067 "WPA: No WPA/RSN IE for this AP known. "
2068 "Trying to get from scan results");
2069 if (wpa_sm_get_beacon_ie(sm) < 0) {
2070 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2071 "WPA: Could not find AP from "
2072 "the scan results");
Hai Shalomfdcde762020-04-02 11:19:20 -07002073 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002074 }
Hai Shalomfdcde762020-04-02 11:19:20 -07002075 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
2076 "WPA: Found the current AP from updated scan results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002077 }
2078
2079 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
2080 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
2081 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
2082 "with IE in Beacon/ProbeResp (no IE?)",
2083 src_addr, ie->wpa_ie, ie->wpa_ie_len,
2084 ie->rsn_ie, ie->rsn_ie_len);
2085 return -1;
2086 }
2087
2088 if ((ie->wpa_ie && sm->ap_wpa_ie &&
2089 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
2090 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
2091 (ie->rsn_ie && sm->ap_rsn_ie &&
2092 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
2093 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
2094 ie->rsn_ie, ie->rsn_ie_len))) {
2095 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
2096 "with IE in Beacon/ProbeResp",
2097 src_addr, ie->wpa_ie, ie->wpa_ie_len,
2098 ie->rsn_ie, ie->rsn_ie_len);
2099 return -1;
2100 }
2101
2102 if (sm->proto == WPA_PROTO_WPA &&
2103 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
2104 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
2105 "detected - RSN was enabled and RSN IE "
2106 "was in msg 3/4, but not in "
2107 "Beacon/ProbeResp",
2108 src_addr, ie->wpa_ie, ie->wpa_ie_len,
2109 ie->rsn_ie, ie->rsn_ie_len);
2110 return -1;
2111 }
2112
Hai Shalom60840252021-02-19 19:02:11 -08002113 if (sm->proto == WPA_PROTO_RSN &&
2114 ((sm->ap_rsnxe && !ie->rsnxe) ||
2115 (!sm->ap_rsnxe && ie->rsnxe) ||
2116 (sm->ap_rsnxe && ie->rsnxe &&
2117 (sm->ap_rsnxe_len != ie->rsnxe_len ||
2118 os_memcmp(sm->ap_rsnxe, ie->rsnxe, sm->ap_rsnxe_len) != 0)))) {
Hai Shalomc3565922019-10-28 11:58:20 -07002119 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2120 "WPA: RSNXE mismatch between Beacon/ProbeResp and EAPOL-Key msg 3/4");
Hai Shalomfdcde762020-04-02 11:19:20 -07002121 wpa_hexdump(MSG_INFO, "RSNXE in Beacon/ProbeResp",
2122 sm->ap_rsnxe, sm->ap_rsnxe_len);
2123 wpa_hexdump(MSG_INFO, "RSNXE in EAPOL-Key msg 3/4",
2124 ie->rsnxe, ie->rsnxe_len);
2125 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Hai Shalomc3565922019-10-28 11:58:20 -07002126 return -1;
2127 }
2128
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002129#ifdef CONFIG_IEEE80211R
2130 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
2131 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
2132 return -1;
2133#endif /* CONFIG_IEEE80211R */
2134
2135 return 0;
2136}
2137
2138
2139/**
2140 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
2141 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2142 * @dst: Destination address for the frame
2143 * @key: Pointer to the EAPOL-Key frame header
2144 * @ver: Version bits from EAPOL-Key Key Info
2145 * @key_info: Key Info
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002146 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002147 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002148 */
2149int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
2150 const struct wpa_eapol_key *key,
2151 u16 ver, u16 key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002152 struct wpa_ptk *ptk)
2153{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002154 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002155 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002156 u8 *rbuf, *key_mic;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002157 u8 *kde = NULL;
Sunil Ravieb83e2a2024-06-28 17:34:56 +00002158 size_t kde_len = 0;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002159
2160 if (sm->mlo.valid_links) {
2161 u8 *pos;
2162
2163 kde = os_malloc(RSN_SELECTOR_LEN + ETH_ALEN + 2);
2164 if (!kde)
2165 return -1;
2166
2167 /* Add MAC KDE */
2168 wpa_printf(MSG_DEBUG, "MLO: Add MAC KDE into EAPOL-Key 4/4");
2169 pos = kde;
2170 pos = rsn_add_kde(pos, RSN_KEY_DATA_MAC_ADDR, sm->own_addr,
2171 ETH_ALEN);
2172 kde_len = pos - kde;
2173 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002174
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002175 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002176 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002177 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Sunil Ravieb83e2a2024-06-28 17:34:56 +00002178 hdrlen + kde_len, &rlen, (void *) &reply);
Sunil Ravi77d572f2023-01-17 23:58:31 +00002179 if (!rbuf) {
2180 os_free(kde);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002181 return -1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002182 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002183
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002184 reply->type = (sm->proto == WPA_PROTO_RSN ||
2185 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002186 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
2187 key_info &= WPA_KEY_INFO_SECURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002188 key_info |= ver | WPA_KEY_INFO_KEY_TYPE;
2189 if (mic_len)
2190 key_info |= WPA_KEY_INFO_MIC;
2191 else
2192 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002193 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002194 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002195 WPA_PUT_BE16(reply->key_length, 0);
2196 else
2197 os_memcpy(reply->key_length, key->key_length, 2);
2198 os_memcpy(reply->replay_counter, key->replay_counter,
2199 WPA_REPLAY_COUNTER_LEN);
2200
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002201 key_mic = (u8 *) (reply + 1);
Sunil Ravieb83e2a2024-06-28 17:34:56 +00002202 WPA_PUT_BE16(key_mic + mic_len, kde_len); /* Key Data length */
Sunil Ravi77d572f2023-01-17 23:58:31 +00002203 if (kde) {
2204 os_memcpy(key_mic + mic_len + 2, kde, kde_len); /* Key Data */
2205 os_free(kde);
2206 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002207
Roshan Pius5e7db942018-04-12 12:27:41 -07002208 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 4/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002209 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
2210 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002211}
2212
2213
Sunil Ravi77d572f2023-01-17 23:58:31 +00002214static int wpa_supplicant_validate_link_kde(struct wpa_sm *sm, u8 link_id,
2215 const u8 *link_kde,
2216 size_t link_kde_len)
2217{
2218 size_t rsne_len = 0, rsnxe_len = 0;
2219 const u8 *rsne = NULL, *rsnxe = NULL;
2220
2221 if (!link_kde ||
2222 link_kde_len < RSN_MLO_LINK_KDE_LINK_MAC_INDEX + ETH_ALEN) {
2223 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2224 "RSN: MLO Link KDE is not found for link ID %d",
2225 link_id);
2226 return -1;
2227 }
2228
Sunil Ravieb83e2a2024-06-28 17:34:56 +00002229 if (os_memcmp(sm->mlo.links[link_id].bssid,
2230 &link_kde[RSN_MLO_LINK_KDE_LINK_MAC_INDEX],
2231 ETH_ALEN) != 0) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00002232 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2233 "RSN: MLO Link %u MAC address (" MACSTR
2234 ") not matching association response (" MACSTR ")",
2235 link_id,
2236 MAC2STR(&link_kde[RSN_MLO_LINK_KDE_LINK_MAC_INDEX]),
2237 MAC2STR(sm->mlo.links[link_id].bssid));
2238 return -1;
2239 }
2240
2241 if (link_kde[0] & RSN_MLO_LINK_KDE_LI_RSNE_INFO) {
2242 rsne = link_kde + RSN_MLO_LINK_KDE_FIXED_LENGTH;
2243 if (link_kde_len < RSN_MLO_LINK_KDE_FIXED_LENGTH + 2 ||
2244 link_kde_len <
2245 (size_t) (RSN_MLO_LINK_KDE_FIXED_LENGTH + 2 + rsne[1])) {
2246 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2247 "RSN: No room for link %u RSNE in MLO Link KDE",
2248 link_id);
2249 return -1;
2250 }
2251
2252 rsne_len = rsne[1] + 2;
2253 }
2254
2255 if (!rsne) {
2256 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2257 "RSN: RSNE not present in MLO Link %u KDE", link_id);
2258 return -1;
2259 }
2260
2261 if (link_kde[0] & RSN_MLO_LINK_KDE_LI_RSNXE_INFO) {
2262 rsnxe = link_kde + RSN_MLO_LINK_KDE_FIXED_LENGTH + rsne_len;
2263 if (link_kde_len <
2264 (RSN_MLO_LINK_KDE_FIXED_LENGTH + rsne_len + 2) ||
2265 link_kde_len <
2266 (RSN_MLO_LINK_KDE_FIXED_LENGTH + rsne_len + 2 + rsnxe[1])) {
2267 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2268 "RSN: No room for link %u RSNXE in MLO Link KDE",
2269 link_id);
2270 return -1;
2271 }
2272
2273 rsnxe_len = rsnxe[1] + 2;
2274 }
2275
2276 if (wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
2277 sm->mlo.links[link_id].ap_rsne,
2278 sm->mlo.links[link_id].ap_rsne_len,
2279 rsne, rsne_len)) {
2280 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2281 "RSN MLO: IE in 3/4 msg does not match with IE in Beacon/ProbeResp for link ID %u",
2282 link_id);
2283 wpa_hexdump(MSG_INFO, "RSNE in Beacon/ProbeResp",
2284 sm->mlo.links[link_id].ap_rsne,
2285 sm->mlo.links[link_id].ap_rsne_len);
2286 wpa_hexdump(MSG_INFO, "RSNE in EAPOL-Key msg 3/4",
2287 rsne, rsne_len);
2288 return -1;
2289 }
2290
2291 if ((sm->mlo.links[link_id].ap_rsnxe && !rsnxe) ||
2292 (!sm->mlo.links[link_id].ap_rsnxe && rsnxe) ||
2293 (sm->mlo.links[link_id].ap_rsnxe && rsnxe &&
2294 (sm->mlo.links[link_id].ap_rsnxe_len != rsnxe_len ||
2295 os_memcmp(sm->mlo.links[link_id].ap_rsnxe, rsnxe,
2296 sm->mlo.links[link_id].ap_rsnxe_len) != 0))) {
2297 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2298 "RSN MLO: RSNXE mismatch between Beacon/ProbeResp and EAPOL-Key msg 3/4 for link ID %u",
2299 link_id);
2300 wpa_hexdump(MSG_INFO, "RSNXE in Beacon/ProbeResp",
2301 sm->mlo.links[link_id].ap_rsnxe,
2302 sm->mlo.links[link_id].ap_rsnxe_len);
2303 wpa_hexdump(MSG_INFO, "RSNXE in EAPOL-Key msg 3/4",
2304 rsnxe, rsnxe_len);
2305 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
2306 return -1;
2307 }
2308
2309 return 0;
2310}
2311
2312
2313static int wpa_validate_mlo_ieee80211w_kdes(struct wpa_sm *sm,
2314 u8 link_id,
2315 struct wpa_eapol_ie_parse *ie)
2316{
2317 if (ie->mlo_igtk[link_id] &&
2318 ie->mlo_igtk_len[link_id] != RSN_MLO_IGTK_KDE_PREFIX_LENGTH +
2319 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
2320 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2321 "RSN MLO: Invalid IGTK KDE length %lu for link ID %u",
Sunil Ravieb83e2a2024-06-28 17:34:56 +00002322 (unsigned long) ie->mlo_igtk_len, link_id);
Sunil Ravi77d572f2023-01-17 23:58:31 +00002323 return -1;
2324 }
2325
2326 if (!sm->beacon_prot)
2327 return 0;
2328
2329 if (ie->mlo_bigtk[link_id] &&
2330 ie->mlo_bigtk_len[link_id] != RSN_MLO_BIGTK_KDE_PREFIX_LENGTH +
2331 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
2332 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2333 "RSN MLO: Invalid BIGTK KDE length %lu for link ID %u",
Sunil Ravieb83e2a2024-06-28 17:34:56 +00002334 (unsigned long) ie->mlo_bigtk_len, link_id);
Sunil Ravi77d572f2023-01-17 23:58:31 +00002335 return -1;
2336 }
2337
2338 return 0;
2339}
2340
2341
2342static void wpa_supplicant_process_3_of_4_wpa(struct wpa_sm *sm,
2343 const struct wpa_eapol_key *key,
2344 u16 ver, const u8 *key_data,
2345 size_t key_data_len)
2346{
2347 u16 key_info, keylen;
2348 struct wpa_eapol_ie_parse ie;
2349
2350 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
2351 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2352 "WPA: RX message 3 of 4-Way Handshake from " MACSTR
2353 " (ver=%d)", MAC2STR(sm->bssid), ver);
2354
2355 key_info = WPA_GET_BE16(key->key_info);
2356
2357 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
2358 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
2359 goto failed;
2360
2361 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
2362 goto failed;
2363
2364 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
2365 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2366 "WPA: ANonce from message 1 of 4-Way Handshake differs from 3 of 4-Way Handshake - drop packet (src="
2367 MACSTR ")", MAC2STR(sm->bssid));
2368 goto failed;
2369 }
2370
2371 keylen = WPA_GET_BE16(key->key_length);
2372 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
2373 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2374 "WPA: Invalid %s key length %d (src=" MACSTR ")",
2375 wpa_cipher_txt(sm->pairwise_cipher), keylen,
2376 MAC2STR(sm->bssid));
2377 goto failed;
2378 }
2379
2380 if (wpa_supplicant_send_4_of_4(sm, wpa_sm_get_auth_addr(sm), key, ver,
2381 key_info, &sm->ptk) < 0)
2382 goto failed;
2383
2384 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
2385 * for the next 4-Way Handshake. If msg 3 is received again, the old
2386 * SNonce will still be used to avoid changing PTK. */
2387 sm->renew_snonce = 1;
2388
2389 if ((key_info & WPA_KEY_INFO_INSTALL) &&
2390 wpa_supplicant_install_ptk(sm, key, KEY_FLAG_RX_TX))
2391 goto failed;
2392
2393 if (key_info & WPA_KEY_INFO_SECURE) {
2394 wpa_sm_mlme_setprotection(
2395 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
2396 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
2397 eapol_sm_notify_portValid(sm->eapol, true);
2398 }
2399 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
2400
2401 sm->msg_3_of_4_ok = 1;
2402 return;
2403
2404failed:
2405 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2406}
2407
2408
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002409static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
2410 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002411 u16 ver, const u8 *key_data,
2412 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002413{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002414 u16 key_info, keylen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002415 struct wpa_eapol_ie_parse ie;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002416 bool mlo = sm->mlo.valid_links;
2417 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002418
2419 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
Sunil Ravi77d572f2023-01-17 23:58:31 +00002420 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO,
2421 "RSN: RX message 3 of 4-Way Handshake from " MACSTR
2422 " (ver=%d)%s", MAC2STR(sm->bssid), ver, mlo ? " (MLO)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002423
2424 key_info = WPA_GET_BE16(key->key_info);
2425
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002426 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
2427 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002428 goto failed;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002429
2430 if (mlo && !ie.valid_mlo_gtks) {
2431 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2432 "MLO RSN: No GTK KDE included in EAPOL-Key msg 3/4");
2433 goto failed;
2434 }
2435 if (mlo &&
2436 (key_info &
2437 (WPA_KEY_INFO_ENCR_KEY_DATA | WPA_KEY_INFO_INSTALL |
2438 WPA_KEY_INFO_SECURE)) !=
2439 (WPA_KEY_INFO_ENCR_KEY_DATA | WPA_KEY_INFO_INSTALL |
2440 WPA_KEY_INFO_SECURE)) {
2441 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2442 "RSN MLO: Invalid key info (0x%x) in EAPOL-Key msg 3/4",
2443 key_info);
2444 goto failed;
2445 }
2446
2447 if (mlo && !is_valid_ap_mld_mac_kde(sm, ie.mac_addr)) {
2448 wpa_printf(MSG_DEBUG, "RSN: Invalid AP MLD MAC address KDE");
2449 goto failed;
2450 }
2451
2452 for (i = 0; mlo && i < MAX_NUM_MLD_LINKS; i++) {
2453 if (!(sm->mlo.req_links & BIT(i)))
2454 continue;
2455
2456 if (wpa_supplicant_validate_link_kde(sm, i, ie.mlo_link[i],
2457 ie.mlo_link_len[i]) < 0)
2458 goto failed;
2459
2460 if (!(sm->mlo.valid_links & BIT(i)))
2461 continue;
2462
2463 if (!ie.mlo_gtk[i]) {
2464 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2465 "RSN: GTK not found for link ID %u", i);
2466 goto failed;
2467 }
2468
2469 if (sm->mgmt_group_cipher != WPA_CIPHER_GTK_NOT_USED &&
2470 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
2471 wpa_validate_mlo_ieee80211w_kdes(sm, i, &ie) < 0)
2472 goto failed;
2473 }
2474
2475#ifdef CONFIG_IEEE80211R
2476 if (mlo && wpa_key_mgmt_ft(sm->key_mgmt) &&
2477 wpa_supplicant_validate_ie_ft(sm, sm->bssid, &ie) < 0)
2478 goto failed;
2479#endif /* CONFIG_IEEE80211R */
2480
2481 if (!mlo && ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002482 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2483 "WPA: GTK IE in unencrypted key data");
2484 goto failed;
2485 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00002486 if (!mlo && ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002487 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2488 "WPA: IGTK KDE in unencrypted key data");
2489 goto failed;
2490 }
2491
Sunil Ravi77d572f2023-01-17 23:58:31 +00002492 if (!mlo && ie.igtk &&
Hai Shalom60840252021-02-19 19:02:11 -08002493 sm->mgmt_group_cipher != WPA_CIPHER_GTK_NOT_USED &&
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002494 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
2495 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
2496 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002497 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2498 "WPA: Invalid IGTK KDE length %lu",
2499 (unsigned long) ie.igtk_len);
2500 goto failed;
2501 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002502
Sunil Ravi77d572f2023-01-17 23:58:31 +00002503 if (!mlo && wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002504 goto failed;
2505
Hai Shalomfdcde762020-04-02 11:19:20 -07002506 if (wpa_handle_ext_key_id(sm, &ie))
2507 goto failed;
2508
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002509 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
2510 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2511 "WPA: ANonce from message 1 of 4-Way Handshake "
2512 "differs from 3 of 4-Way Handshake - drop packet (src="
2513 MACSTR ")", MAC2STR(sm->bssid));
2514 goto failed;
2515 }
2516
2517 keylen = WPA_GET_BE16(key->key_length);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002518 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
2519 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2520 "WPA: Invalid %s key length %d (src=" MACSTR
2521 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
2522 MAC2STR(sm->bssid));
2523 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002524 }
2525
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002526#ifdef CONFIG_P2P
2527 if (ie.ip_addr_alloc) {
2528 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
2529 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
2530 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
2531 }
2532#endif /* CONFIG_P2P */
2533
Hai Shalom74f70d42019-02-11 14:42:39 -08002534#ifdef CONFIG_OCV
2535 if (wpa_sm_ocv_enabled(sm)) {
2536 struct wpa_channel_info ci;
2537
2538 if (wpa_sm_channel_info(sm, &ci) != 0) {
2539 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2540 "Failed to get channel info to validate received OCI in EAPOL-Key 3/4");
2541 return;
2542 }
2543
2544 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
2545 channel_width_to_int(ci.chanwidth),
Hai Shalom899fcc72020-10-19 14:38:18 -07002546 ci.seg1_idx) != OCI_SUCCESS) {
2547 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
2548 "addr=" MACSTR " frame=eapol-key-m3 error=%s",
2549 MAC2STR(sm->bssid), ocv_errorstr);
Hai Shalom74f70d42019-02-11 14:42:39 -08002550 return;
2551 }
2552 }
2553#endif /* CONFIG_OCV */
2554
Hai Shalom4fbc08f2020-05-18 12:37:00 -07002555#ifdef CONFIG_DPP2
2556 if (DPP_VERSION > 1 && ie.dpp_kde) {
2557 wpa_printf(MSG_DEBUG,
2558 "DPP: peer Protocol Version %u Flags 0x%x",
2559 ie.dpp_kde[0], ie.dpp_kde[1]);
2560 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_pfs != 2 &&
2561 (ie.dpp_kde[1] & DPP_KDE_PFS_ALLOWED) && !sm->dpp_z) {
2562 wpa_printf(MSG_INFO,
2563 "DPP: Peer indicated it supports PFS and local configuration allows this, but PFS was not negotiated for the association");
2564 goto failed;
2565 }
2566 }
2567#endif /* CONFIG_DPP2 */
2568
Hai Shalomfdcde762020-04-02 11:19:20 -07002569 if (sm->use_ext_key_id &&
2570 wpa_supplicant_install_ptk(sm, key, KEY_FLAG_RX))
2571 goto failed;
2572
Sunil Ravi77d572f2023-01-17 23:58:31 +00002573 if (wpa_supplicant_send_4_of_4(sm, wpa_sm_get_auth_addr(sm), key, ver,
2574 key_info, &sm->ptk) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002575 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002576
2577 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
2578 * for the next 4-Way Handshake. If msg 3 is received again, the old
2579 * SNonce will still be used to avoid changing PTK. */
2580 sm->renew_snonce = 1;
2581
2582 if (key_info & WPA_KEY_INFO_INSTALL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002583 int res;
2584
2585 if (sm->use_ext_key_id)
2586 res = wpa_supplicant_activate_ptk(sm);
2587 else
2588 res = wpa_supplicant_install_ptk(sm, key,
2589 KEY_FLAG_RX_TX);
2590 if (res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002591 goto failed;
2592 }
2593
2594 if (key_info & WPA_KEY_INFO_SECURE) {
2595 wpa_sm_mlme_setprotection(
2596 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
2597 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
Hai Shalome21d4e82020-04-29 16:34:06 -07002598 eapol_sm_notify_portValid(sm->eapol, true);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002599 }
2600 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
2601
Sunil Ravi77d572f2023-01-17 23:58:31 +00002602 if (mlo) {
2603 if (wpa_supplicant_pairwise_mlo_gtk(sm, key, &ie,
2604 key_info) < 0) {
2605 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2606 "MLO RSN: Failed to configure MLO GTKs");
2607 goto failed;
2608 }
2609 } else if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07002610 /* No GTK to be set to the driver */
2611 } else if (!ie.gtk && sm->proto == WPA_PROTO_RSN) {
2612 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2613 "RSN: No GTK KDE included in EAPOL-Key msg 3/4");
2614 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002615 } else if (ie.gtk &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002616 wpa_supplicant_pairwise_gtk(sm, key,
2617 ie.gtk, ie.gtk_len, key_info) < 0) {
2618 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2619 "RSN: Failed to configure GTK");
2620 goto failed;
2621 }
2622
Sunil Ravi77d572f2023-01-17 23:58:31 +00002623 if ((mlo && mlo_ieee80211w_set_keys(sm, &ie) < 0) ||
2624 (!mlo && ieee80211w_set_keys(sm, &ie) < 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002625 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2626 "RSN: Failed to configure IGTK");
2627 goto failed;
2628 }
2629
Sunil Ravi77d572f2023-01-17 23:58:31 +00002630 if (mlo || sm->group_cipher == WPA_CIPHER_GTK_NOT_USED || ie.gtk)
Hai Shalom5f92bc92019-04-18 11:54:11 -07002631 wpa_supplicant_key_neg_complete(sm, sm->bssid,
2632 key_info & WPA_KEY_INFO_SECURE);
2633
Sunil Ravi77d572f2023-01-17 23:58:31 +00002634 if (mlo || ie.gtk)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002635 wpa_sm_set_rekey_offload(sm);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002636
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002637 /* Add PMKSA cache entry for Suite B AKMs here since PMKID can be
2638 * calculated only after KCK has been derived. Though, do not replace an
2639 * existing PMKSA entry after each 4-way handshake (i.e., new KCK/PMKID)
2640 * to avoid unnecessary changes of PMKID while continuing to use the
2641 * same PMK. */
2642 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt) &&
2643 !sm->cur_pmksa) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002644 struct rsn_pmksa_cache_entry *sa;
2645
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002646 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002647 sm->ptk.kck, sm->ptk.kck_len,
Sunil Ravi77d572f2023-01-17 23:58:31 +00002648 wpa_sm_get_auth_addr(sm), sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002649 sm->network_ctx, sm->key_mgmt, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002650 if (!sm->cur_pmksa)
2651 sm->cur_pmksa = sa;
2652 }
2653
Hai Shalomfdcde762020-04-02 11:19:20 -07002654 if (ie.transition_disable)
2655 wpa_sm_transition_disable(sm, ie.transition_disable[0]);
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002656 sm->msg_3_of_4_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002657 return;
2658
2659failed:
2660 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2661}
2662
2663
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002664static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
2665 const struct wpa_eapol_key *key,
2666 int ver, u16 key_info)
2667{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002668 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002669 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002670 u8 *rbuf, *key_mic;
Hai Shalom74f70d42019-02-11 14:42:39 -08002671 size_t kde_len = 0;
2672
Sunil Ravia04bd252022-05-02 22:54:18 -07002673#ifdef CONFIG_TESTING_OPTIONS
2674 if (sm->disable_eapol_g2_tx) {
2675 wpa_printf(MSG_INFO, "TEST: Disable sending EAPOL-Key 2/2");
2676 return 0;
2677 }
2678#endif /* CONFIG_TESTING_OPTIONS */
2679
Hai Shalom74f70d42019-02-11 14:42:39 -08002680#ifdef CONFIG_OCV
2681 if (wpa_sm_ocv_enabled(sm))
2682 kde_len = OCV_OCI_KDE_LEN;
2683#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002684
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002685 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002686 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002687 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Hai Shalom74f70d42019-02-11 14:42:39 -08002688 hdrlen + kde_len, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002689 if (rbuf == NULL)
2690 return -1;
2691
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002692 reply->type = (sm->proto == WPA_PROTO_RSN ||
2693 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002694 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
2695 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002696 key_info |= ver | WPA_KEY_INFO_SECURE;
2697 if (mic_len)
2698 key_info |= WPA_KEY_INFO_MIC;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002699 else
2700 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002701 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002702 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002703 WPA_PUT_BE16(reply->key_length, 0);
2704 else
2705 os_memcpy(reply->key_length, key->key_length, 2);
2706 os_memcpy(reply->replay_counter, key->replay_counter,
2707 WPA_REPLAY_COUNTER_LEN);
2708
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002709 key_mic = (u8 *) (reply + 1);
Hai Shalom74f70d42019-02-11 14:42:39 -08002710 WPA_PUT_BE16(key_mic + mic_len, kde_len); /* Key Data Length */
2711
2712#ifdef CONFIG_OCV
2713 if (wpa_sm_ocv_enabled(sm)) {
2714 struct wpa_channel_info ci;
2715 u8 *pos;
2716
2717 if (wpa_sm_channel_info(sm, &ci) != 0) {
2718 wpa_printf(MSG_WARNING,
2719 "Failed to get channel info for OCI element in EAPOL-Key 2/2");
2720 os_free(rbuf);
2721 return -1;
2722 }
Hai Shalom899fcc72020-10-19 14:38:18 -07002723#ifdef CONFIG_TESTING_OPTIONS
2724 if (sm->oci_freq_override_eapol_g2) {
2725 wpa_printf(MSG_INFO,
2726 "TEST: Override OCI KDE frequency %d -> %d MHz",
2727 ci.frequency,
2728 sm->oci_freq_override_eapol_g2);
2729 ci.frequency = sm->oci_freq_override_eapol_g2;
2730 }
2731#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -08002732
2733 pos = key_mic + mic_len + 2; /* Key Data */
2734 if (ocv_insert_oci_kde(&ci, &pos) < 0) {
2735 os_free(rbuf);
2736 return -1;
2737 }
2738 }
2739#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002740
2741 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002742 return wpa_eapol_key_send(sm, &sm->ptk, ver, wpa_sm_get_auth_addr(sm),
2743 ETH_P_EAPOL, rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002744}
2745
2746
Sunil Ravi77d572f2023-01-17 23:58:31 +00002747static void wpa_supplicant_process_mlo_1_of_2(struct wpa_sm *sm,
2748 const unsigned char *src_addr,
2749 const struct wpa_eapol_key *key,
2750 const u8 *key_data,
2751 size_t key_data_len, u16 ver)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002752{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002753 u16 key_info;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002754 u8 i;
2755 struct wpa_eapol_ie_parse ie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002756
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002757 if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) {
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002758 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00002759 "MLO RSN: Group Key Handshake started prior to completion of 4-way handshake");
2760 goto failed;
2761 }
2762
2763 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "MLO RSN: RX message 1 of Group "
2764 "Key Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr),
2765 ver);
2766
2767 key_info = WPA_GET_BE16(key->key_info);
2768
2769 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
2770
2771 wpa_hexdump_key(MSG_DEBUG, "MLO RSN: msg 1/2 key data", key_data,
2772 key_data_len);
2773 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
2774 goto failed;
2775
2776 if (!ie.valid_mlo_gtks) {
2777 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2778 "MLO RSN: No MLO GTK KDE in Group Key msg 1/2");
2779 goto failed;
2780 }
2781
2782 if (!(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
2783 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2784 "MLO RSN: MLO GTK KDE in unencrypted key data");
2785 goto failed;
2786 }
2787
2788#ifdef CONFIG_OCV
2789 if (wpa_sm_ocv_enabled(sm)) {
2790 struct wpa_channel_info ci;
2791
2792 if (wpa_sm_channel_info(sm, &ci) != 0) {
2793 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2794 "Failed to get channel info to validate received OCI in EAPOL-Key group msg 1/2");
2795 goto failed;
2796 }
2797
2798 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
2799 channel_width_to_int(ci.chanwidth),
2800 ci.seg1_idx) != OCI_SUCCESS) {
2801 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
2802 "addr=" MACSTR " frame=eapol-key-g1 error=%s",
2803 MAC2STR(sm->bssid), ocv_errorstr);
2804 goto failed;
2805 }
2806 }
2807#endif /* CONFIG_OCV */
2808
2809 if (mlo_ieee80211w_set_keys(sm, &ie) < 0)
2810 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2811 "MLO RSN: Failed to configure MLO IGTK");
2812
Sunil Ravi88611412024-06-28 17:34:56 +00002813 for (i = 0; i < MAX_NUM_MLO_LINKS; i++) {
2814 if (!(sm->mlo.valid_links & BIT(i)))
2815 continue;
2816
Sunil Ravi77d572f2023-01-17 23:58:31 +00002817 /*
2818 * AP may send group keys for subset of the all links during
2819 * rekey
2820 */
2821 if (!ie.mlo_gtk[i])
2822 continue;
2823
2824 if (wpa_supplicant_mlo_gtk(sm, i, ie.mlo_gtk[i],
2825 ie.mlo_gtk_len[i], key_info))
2826 goto failed;
2827 }
2828
2829 if (wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
2830 goto failed;
2831
2832 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "MLO RSN: Group rekeying completed "
2833 "with " MACSTR " [GTK=%s]", MAC2STR(sm->mlo.ap_mld_addr),
2834 wpa_cipher_txt(sm->group_cipher));
2835 wpa_sm_cancel_auth_timeout(sm);
2836 wpa_sm_set_state(sm, WPA_COMPLETED);
2837
2838 wpa_sm_set_rekey_offload(sm);
2839
2840 return;
2841
2842failed:
2843 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2844}
2845
2846
2847static void wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
2848 const unsigned char *src_addr,
2849 const struct wpa_eapol_key *key,
2850 const u8 *key_data,
2851 size_t key_data_len, u16 ver)
2852{
2853 u16 key_info;
2854 int rekey;
2855 struct wpa_gtk_data gd;
2856 const u8 *key_rsc;
2857 size_t maxkeylen;
2858 u16 gtk_len;
2859
2860 if (!sm->msg_3_of_4_ok) {
2861 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002862 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
2863 goto failed;
2864 }
2865
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002866 os_memset(&gd, 0, sizeof(gd));
2867
2868 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002869 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2870 "WPA: RX message 1 of Group Key Handshake from " MACSTR
2871 " (ver=%d)", MAC2STR(src_addr), ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002872
2873 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002874
Sunil Ravi77d572f2023-01-17 23:58:31 +00002875 gtk_len = WPA_GET_BE16(key->key_length);
2876 maxkeylen = key_data_len;
2877 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
2878 if (maxkeylen < 8) {
2879 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2880 "WPA: Too short maxkeylen (%lu)",
2881 (unsigned long) maxkeylen);
2882 goto failed;
2883 }
2884 maxkeylen -= 8;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002885 }
2886
Sunil Ravi77d572f2023-01-17 23:58:31 +00002887 if (gtk_len > maxkeylen ||
2888 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
2889 gtk_len, maxkeylen,
2890 &gd.key_rsc_len, &gd.alg))
2891 goto failed;
2892
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002893 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
2894
Sunil Ravi77d572f2023-01-17 23:58:31 +00002895 gd.gtk_len = gtk_len;
2896 gd.keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
2897 WPA_KEY_INFO_KEY_INDEX_SHIFT;
2898 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
2899#if defined(CONFIG_NO_RC4) || defined(CONFIG_FIPS)
2900 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2901 "WPA: RC4 not supported in the build");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002902 goto failed;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002903#else /* CONFIG_NO_RC4 || CONFIG_FIPS */
2904 u8 ek[32];
2905 if (key_data_len > sizeof(gd.gtk)) {
2906 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2907 "WPA: RC4 key data too long (%lu)",
2908 (unsigned long) key_data_len);
2909 goto failed;
2910 }
2911 os_memcpy(ek, key->key_iv, 16);
2912 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
2913 os_memcpy(gd.gtk, key_data, key_data_len);
2914 if (rc4_skip(ek, 32, 256, gd.gtk, key_data_len)) {
2915 forced_memzero(ek, sizeof(ek));
2916 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2917 "WPA: RC4 failed");
2918 goto failed;
2919 }
2920 forced_memzero(ek, sizeof(ek));
2921#endif /* CONFIG_NO_RC4 || CONFIG_FIPS */
2922 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
2923 if (maxkeylen % 8) {
2924 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2925 "WPA: Unsupported AES-WRAP len %lu",
2926 (unsigned long) maxkeylen);
2927 goto failed;
2928 }
2929 if (maxkeylen > sizeof(gd.gtk)) {
2930 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2931 "WPA: AES-WRAP key data "
2932 "too long (keydatalen=%lu maxkeylen=%lu)",
2933 (unsigned long) key_data_len,
2934 (unsigned long) maxkeylen);
2935 goto failed;
2936 }
2937 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
2938 key_data, gd.gtk)) {
2939 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2940 "WPA: AES unwrap failed - could not decrypt "
2941 "GTK");
2942 goto failed;
2943 }
2944 } else {
2945 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2946 "WPA: Unsupported key_info type %d", ver);
2947 goto failed;
2948 }
2949 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
2950 sm, !!(key_info & WPA_KEY_INFO_TXRX));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002951
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002952 key_rsc = key->key_rsc;
2953 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
2954 key_rsc = null_rsc;
2955
Jouni Malinen58c0e962017-10-01 12:12:24 +03002956 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002957 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002958 goto failed;
Hai Shalom81f62d82019-07-22 12:10:00 -07002959 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002960
2961 if (rekey) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00002962 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2963 "WPA: Group rekeying completed with " MACSTR
2964 " [GTK=%s]",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002965 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
2966 wpa_sm_cancel_auth_timeout(sm);
2967 wpa_sm_set_state(sm, WPA_COMPLETED);
2968 } else {
2969 wpa_supplicant_key_neg_complete(sm, sm->bssid,
Sunil Ravi77d572f2023-01-17 23:58:31 +00002970 key_info & WPA_KEY_INFO_SECURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002971 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002972
2973 wpa_sm_set_rekey_offload(sm);
2974
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002975 return;
2976
2977failed:
Hai Shalom81f62d82019-07-22 12:10:00 -07002978 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002979 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2980}
2981
2982
Sunil Ravi77d572f2023-01-17 23:58:31 +00002983static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
2984 const unsigned char *src_addr,
2985 const struct wpa_eapol_key *key,
2986 const u8 *key_data,
2987 size_t key_data_len, u16 ver)
2988{
2989 u16 key_info;
2990 struct wpa_gtk_data gd;
2991 const u8 *key_rsc;
2992 int maxkeylen;
2993 struct wpa_eapol_ie_parse ie;
2994 u16 gtk_len;
2995
2996 if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) {
2997 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2998 "RSN: Group Key Handshake started prior to completion of 4-way handshake");
2999 goto failed;
3000 }
3001
3002 os_memset(&gd, 0, sizeof(gd));
3003
3004 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3005 "RSN: RX message 1 of Group Key Handshake from " MACSTR
3006 " (ver=%d)", MAC2STR(src_addr), ver);
3007
3008 key_info = WPA_GET_BE16(key->key_info);
3009
3010 wpa_hexdump_key(MSG_DEBUG, "RSN: msg 1/2 key data",
3011 key_data, key_data_len);
3012 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
3013 goto failed;
3014
3015 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
3016
3017 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
3018 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3019 "RSN: GTK KDE in unencrypted key data");
3020 goto failed;
3021 }
3022 if (!ie.gtk) {
3023 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3024 "RSN: No GTK KDE in Group Key msg 1/2");
3025 goto failed;
3026 }
3027 gtk_len = ie.gtk_len;
3028 if (gtk_len < 2) {
3029 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3030 "RSN: Invalid GTK KDE length (%u) in Group Key msg 1/2",
3031 gtk_len);
3032 goto failed;
3033 }
3034 gtk_len -= 2;
3035 if (gtk_len > sizeof(gd.gtk)) {
3036 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3037 "RSN: Too long GTK in GTK KDE (len=%u)", gtk_len);
3038 goto failed;
3039 }
3040 maxkeylen = gd.gtk_len = gtk_len;
3041
3042#ifdef CONFIG_OCV
3043 if (wpa_sm_ocv_enabled(sm)) {
3044 struct wpa_channel_info ci;
3045
3046 if (wpa_sm_channel_info(sm, &ci) != 0) {
3047 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3048 "Failed to get channel info to validate received OCI in EAPOL-Key group msg 1/2");
3049 goto failed;
3050 }
3051
3052 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
3053 channel_width_to_int(ci.chanwidth),
3054 ci.seg1_idx) != OCI_SUCCESS) {
3055 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
3056 "addr=" MACSTR " frame=eapol-key-g1 error=%s",
3057 MAC2STR(sm->bssid), ocv_errorstr);
3058 goto failed;
3059 }
3060 }
3061#endif /* CONFIG_OCV */
3062
3063 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
3064 gtk_len, maxkeylen,
3065 &gd.key_rsc_len, &gd.alg))
3066 goto failed;
3067
3068 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
3069 ie.gtk, 2 + gtk_len);
3070 gd.keyidx = ie.gtk[0] & 0x3;
3071 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
3072 !!(ie.gtk[0] & BIT(2)));
3073 os_memcpy(gd.gtk, ie.gtk + 2, gtk_len);
3074
3075 if (ieee80211w_set_keys(sm, &ie) < 0)
3076 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3077 "RSN: Failed to configure IGTK");
3078
3079 key_rsc = key->key_rsc;
3080 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
3081 key_rsc = null_rsc;
3082
3083 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
3084 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
3085 goto failed;
3086 forced_memzero(&gd, sizeof(gd));
3087
3088 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3089 "RSN: Group rekeying completed with " MACSTR " [GTK=%s]",
3090 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
3091 wpa_sm_cancel_auth_timeout(sm);
3092 wpa_sm_set_state(sm, WPA_COMPLETED);
3093
3094 wpa_sm_set_rekey_offload(sm);
3095
3096 return;
3097
3098failed:
3099 forced_memzero(&gd, sizeof(gd));
3100 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
3101}
3102
3103
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003104static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003105 struct wpa_eapol_key *key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003106 u16 ver,
3107 const u8 *buf, size_t len)
3108{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003109 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003110 int ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003111 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003112
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003113 os_memcpy(mic, key + 1, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003114 if (sm->tptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003115 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003116 if (wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len,
3117 sm->key_mgmt,
3118 ver, buf, len, (u8 *) (key + 1)) < 0 ||
3119 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003120 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3121 "WPA: Invalid EAPOL-Key MIC "
3122 "when using TPTK - ignoring TPTK");
Hai Shalom74f70d42019-02-11 14:42:39 -08003123#ifdef TEST_FUZZ
3124 wpa_printf(MSG_INFO,
3125 "TEST: Ignore Key MIC failure for fuzz testing");
3126 goto continue_fuzz;
3127#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003128 } else {
Hai Shalom74f70d42019-02-11 14:42:39 -08003129#ifdef TEST_FUZZ
3130 continue_fuzz:
3131#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003132 ok = 1;
3133 sm->tptk_set = 0;
3134 sm->ptk_set = 1;
3135 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003136 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003137 /*
3138 * This assures the same TPTK in sm->tptk can never be
Roshan Pius3a1667e2018-07-03 15:17:14 -07003139 * copied twice to sm->ptk as the new PTK. In
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003140 * combination with the installed flag in the wpa_ptk
3141 * struct, this assures the same PTK is only installed
3142 * once.
3143 */
3144 sm->renew_snonce = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003145 }
3146 }
3147
3148 if (!ok && sm->ptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003149 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003150 if (wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len,
3151 sm->key_mgmt,
3152 ver, buf, len, (u8 *) (key + 1)) < 0 ||
3153 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003154 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3155 "WPA: Invalid EAPOL-Key MIC - "
3156 "dropping packet");
Hai Shalom74f70d42019-02-11 14:42:39 -08003157#ifdef TEST_FUZZ
3158 wpa_printf(MSG_INFO,
3159 "TEST: Ignore Key MIC failure for fuzz testing");
3160 goto continue_fuzz2;
3161#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003162 return -1;
3163 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003164#ifdef TEST_FUZZ
3165 continue_fuzz2:
3166#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003167 ok = 1;
3168 }
3169
3170 if (!ok) {
3171 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3172 "WPA: Could not verify EAPOL-Key MIC - "
3173 "dropping packet");
3174 return -1;
3175 }
3176
3177 os_memcpy(sm->rx_replay_counter, key->replay_counter,
3178 WPA_REPLAY_COUNTER_LEN);
3179 sm->rx_replay_counter_set = 1;
3180 return 0;
3181}
3182
3183
3184/* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
3185static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003186 struct wpa_eapol_key *key,
3187 size_t mic_len, u16 ver,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003188 u8 *key_data, size_t *key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003189{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003190 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003191 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003192 if (!sm->ptk_set) {
3193 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3194 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
3195 "Data");
3196 return -1;
3197 }
3198
3199 /* Decrypt key data here so that this operation does not need
3200 * to be implemented separately for each message type. */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003201 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00003202#if defined(CONFIG_NO_RC4) || defined(CONFIG_FIPS)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003203 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3204 "WPA: RC4 not supported in the build");
3205 return -1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003206#else /* CONFIG_NO_RC4 || CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003207 u8 ek[32];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003208
3209 wpa_printf(MSG_DEBUG, "WPA: Decrypt Key Data using RC4");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003210 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003211 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003212 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07003213 forced_memzero(ek, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003214 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
3215 "WPA: RC4 failed");
3216 return -1;
3217 }
Hai Shalom81f62d82019-07-22 12:10:00 -07003218 forced_memzero(ek, sizeof(ek));
Sunil Ravi77d572f2023-01-17 23:58:31 +00003219#endif /* CONFIG_NO_RC4 || CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003220 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003221 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07003222 wpa_use_aes_key_wrap(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003223 u8 *buf;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003224
3225 wpa_printf(MSG_DEBUG,
3226 "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)",
3227 (unsigned int) sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003228 if (*key_data_len < 8 || *key_data_len % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003229 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003230 "WPA: Unsupported AES-WRAP len %u",
3231 (unsigned int) *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003232 return -1;
3233 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003234 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
3235 buf = os_malloc(*key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003236 if (buf == NULL) {
3237 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3238 "WPA: No memory for AES-UNWRAP buffer");
3239 return -1;
3240 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003241#ifdef TEST_FUZZ
3242 os_memset(buf, 0x11, *key_data_len);
3243#endif /* TEST_FUZZ */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003244 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003245 key_data, buf)) {
Hai Shalom74f70d42019-02-11 14:42:39 -08003246#ifdef TEST_FUZZ
3247 wpa_printf(MSG_INFO,
3248 "TEST: Ignore AES unwrap failure for fuzz testing");
3249 goto continue_fuzz;
3250#endif /* TEST_FUZZ */
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08003251 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003252 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3253 "WPA: AES unwrap failed - "
3254 "could not decrypt EAPOL-Key key data");
3255 return -1;
3256 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003257#ifdef TEST_FUZZ
3258 continue_fuzz:
3259#endif /* TEST_FUZZ */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003260 os_memcpy(key_data, buf, *key_data_len);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08003261 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003262 WPA_PUT_BE16(((u8 *) (key + 1)) + mic_len, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003263 } else {
3264 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3265 "WPA: Unsupported key_info type %d", ver);
3266 return -1;
3267 }
3268 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003269 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003270 return 0;
3271}
3272
3273
3274/**
3275 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
3276 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3277 */
3278void wpa_sm_aborted_cached(struct wpa_sm *sm)
3279{
3280 if (sm && sm->cur_pmksa) {
3281 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3282 "RSN: Cancelling PMKSA caching attempt");
3283 sm->cur_pmksa = NULL;
3284 }
3285}
3286
3287
Hai Shalomc1a21442022-02-04 13:43:00 -08003288void wpa_sm_aborted_external_cached(struct wpa_sm *sm)
3289{
3290 if (sm && sm->cur_pmksa && sm->cur_pmksa->external) {
3291 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3292 "RSN: Cancelling external PMKSA caching attempt");
3293 sm->cur_pmksa = NULL;
3294 }
3295}
3296
3297
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003298static void wpa_eapol_key_dump(struct wpa_sm *sm,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003299 const struct wpa_eapol_key *key,
3300 unsigned int key_data_len,
3301 const u8 *mic, unsigned int mic_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003302{
3303#ifndef CONFIG_NO_STDOUT_DEBUG
3304 u16 key_info = WPA_GET_BE16(key->key_info);
3305
3306 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
3307 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3308 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
3309 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
3310 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
3311 WPA_KEY_INFO_KEY_INDEX_SHIFT,
3312 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
3313 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
3314 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
3315 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
3316 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
3317 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
3318 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
3319 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
3320 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
3321 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3322 " key_length=%u key_data_length=%u",
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003323 WPA_GET_BE16(key->key_length), key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003324 wpa_hexdump(MSG_DEBUG, " replay_counter",
3325 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
3326 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
3327 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
3328 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
3329 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003330 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003331#endif /* CONFIG_NO_STDOUT_DEBUG */
3332}
3333
3334
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003335#ifdef CONFIG_FILS
3336static int wpa_supp_aead_decrypt(struct wpa_sm *sm, u8 *buf, size_t buf_len,
3337 size_t *key_data_len)
3338{
3339 struct wpa_ptk *ptk;
3340 struct ieee802_1x_hdr *hdr;
3341 struct wpa_eapol_key *key;
3342 u8 *pos, *tmp;
3343 const u8 *aad[1];
3344 size_t aad_len[1];
3345
3346 if (*key_data_len < AES_BLOCK_SIZE) {
3347 wpa_printf(MSG_INFO, "No room for AES-SIV data in the frame");
3348 return -1;
3349 }
3350
3351 if (sm->tptk_set)
3352 ptk = &sm->tptk;
3353 else if (sm->ptk_set)
3354 ptk = &sm->ptk;
3355 else
3356 return -1;
3357
3358 hdr = (struct ieee802_1x_hdr *) buf;
3359 key = (struct wpa_eapol_key *) (hdr + 1);
3360 pos = (u8 *) (key + 1);
3361 pos += 2; /* Pointing at the Encrypted Key Data field */
3362
3363 tmp = os_malloc(*key_data_len);
3364 if (!tmp)
3365 return -1;
3366
3367 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
3368 * to Key Data (exclusive). */
3369 aad[0] = buf;
3370 aad_len[0] = pos - buf;
3371 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, *key_data_len,
3372 1, aad, aad_len, tmp) < 0) {
3373 wpa_printf(MSG_INFO, "Invalid AES-SIV data in the frame");
3374 bin_clear_free(tmp, *key_data_len);
3375 return -1;
3376 }
3377
3378 /* AEAD decryption and validation completed successfully */
3379 (*key_data_len) -= AES_BLOCK_SIZE;
3380 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
3381 tmp, *key_data_len);
3382
3383 /* Replace Key Data field with the decrypted version */
3384 os_memcpy(pos, tmp, *key_data_len);
3385 pos -= 2; /* Key Data Length field */
3386 WPA_PUT_BE16(pos, *key_data_len);
3387 bin_clear_free(tmp, *key_data_len);
3388
3389 if (sm->tptk_set) {
3390 sm->tptk_set = 0;
3391 sm->ptk_set = 1;
3392 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
3393 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
3394 }
3395
3396 os_memcpy(sm->rx_replay_counter, key->replay_counter,
3397 WPA_REPLAY_COUNTER_LEN);
3398 sm->rx_replay_counter_set = 1;
3399
3400 return 0;
3401}
3402#endif /* CONFIG_FILS */
3403
3404
Sunil Ravi77d572f2023-01-17 23:58:31 +00003405static int wpa_sm_rx_eapol_wpa(struct wpa_sm *sm, const u8 *src_addr,
3406 struct wpa_eapol_key *key,
3407 enum frame_encryption encrypted,
3408 const u8 *tmp, size_t data_len,
3409 u8 *key_data, size_t key_data_len)
3410{
3411 u16 key_info, ver;
3412
3413 key_info = WPA_GET_BE16(key->key_info);
3414
3415 if (key->type != EAPOL_KEY_TYPE_WPA) {
3416 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3417 "WPA: Unsupported EAPOL-Key type %d", key->type);
3418 return -1;
3419 }
3420
3421 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
3422 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
3423 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
3424 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3425 "WPA: Unsupported EAPOL-Key descriptor version %d",
3426 ver);
3427 return -1;
3428 }
3429
3430 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
3431 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
3432 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3433 "WPA: CCMP is used, but EAPOL-Key descriptor version (%d) is not 2",
3434 ver);
3435 if (sm->group_cipher != WPA_CIPHER_CCMP &&
3436 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
3437 /* Earlier versions of IEEE 802.11i did not explicitly
3438 * require version 2 descriptor for all EAPOL-Key
3439 * packets, so allow group keys to use version 1 if
3440 * CCMP is not used for them. */
3441 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3442 "WPA: Backwards compatibility: allow invalid version for non-CCMP group keys");
3443 } else
3444 return -1;
3445 }
3446
3447 if ((key_info & WPA_KEY_INFO_MIC) &&
3448 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
3449 return -1;
3450
3451 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
3452 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
3453 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3454 "WPA: Ignored EAPOL-Key (Pairwise) with non-zero key index");
3455 return -1;
3456 }
3457 if (key_info & (WPA_KEY_INFO_MIC |
3458 WPA_KEY_INFO_ENCR_KEY_DATA)) {
3459 /* 3/4 4-Way Handshake */
3460 wpa_supplicant_process_3_of_4_wpa(sm, key, ver,
3461 key_data,
3462 key_data_len);
3463 } else {
3464 /* 1/4 4-Way Handshake */
3465 wpa_supplicant_process_1_of_4_wpa(sm, src_addr, key,
3466 ver, key_data,
3467 key_data_len,
3468 encrypted);
3469 }
3470 } else {
3471 if (key_info & WPA_KEY_INFO_MIC) {
3472 /* 1/2 Group Key Handshake */
3473 wpa_supplicant_process_1_of_2_wpa(sm, src_addr, key,
3474 key_data,
3475 key_data_len,
3476 ver);
3477 } else {
3478 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3479 "WPA: EAPOL-Key (Group) without Mic/Encr bit - dropped");
3480 }
3481 }
3482
3483 return 1;
3484}
3485
3486
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003487/**
3488 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
3489 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3490 * @src_addr: Source MAC address of the EAPOL packet
3491 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
3492 * @len: Length of the EAPOL frame
Sunil8cd6f4d2022-06-28 18:40:46 +00003493 * @encrypted: Whether the frame was encrypted
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003494 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
3495 *
3496 * This function is called for each received EAPOL frame. Other than EAPOL-Key
3497 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
3498 * only processing WPA and WPA2 EAPOL-Key frames.
3499 *
3500 * The received EAPOL-Key packets are validated and valid packets are replied
3501 * to. In addition, key material (PTK, GTK) is configured at the end of a
3502 * successful key handshake.
3503 */
3504int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
Sunil8cd6f4d2022-06-28 18:40:46 +00003505 const u8 *buf, size_t len, enum frame_encryption encrypted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003506{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003507 size_t plen, data_len, key_data_len;
3508 const struct ieee802_1x_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003509 struct wpa_eapol_key *key;
3510 u16 key_info, ver;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003511 u8 *tmp = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003512 int ret = -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003513 u8 *mic, *key_data;
Hai Shalom899fcc72020-10-19 14:38:18 -07003514 size_t mic_len, keyhdrlen, pmk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003515
3516#ifdef CONFIG_IEEE80211R
3517 sm->ft_completed = 0;
3518#endif /* CONFIG_IEEE80211R */
3519
Hai Shalom899fcc72020-10-19 14:38:18 -07003520 pmk_len = sm->pmk_len;
3521 if (!pmk_len && sm->cur_pmksa)
3522 pmk_len = sm->cur_pmksa->pmk_len;
3523 mic_len = wpa_mic_len(sm->key_mgmt, pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003524 keyhdrlen = sizeof(*key) + mic_len + 2;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003525
3526 if (len < sizeof(*hdr) + keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003527 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3528 "WPA: EAPOL frame too short to be a WPA "
3529 "EAPOL-Key (len %lu, expecting at least %lu)",
3530 (unsigned long) len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003531 (unsigned long) sizeof(*hdr) + keyhdrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003532 return 0;
3533 }
3534
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003535 hdr = (const struct ieee802_1x_hdr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003536 plen = be_to_host16(hdr->length);
3537 data_len = plen + sizeof(*hdr);
3538 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3539 "IEEE 802.1X RX: version=%d type=%d length=%lu",
3540 hdr->version, hdr->type, (unsigned long) plen);
3541
3542 if (hdr->version < EAPOL_VERSION) {
3543 /* TODO: backwards compatibility */
3544 }
3545 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
3546 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3547 "WPA: EAPOL frame (type %u) discarded, "
3548 "not a Key frame", hdr->type);
3549 ret = 0;
3550 goto out;
3551 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003552 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003553 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003554 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3555 "WPA: EAPOL frame payload size %lu "
3556 "invalid (frame size %lu)",
3557 (unsigned long) plen, (unsigned long) len);
3558 ret = 0;
3559 goto out;
3560 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003561 if (data_len < len) {
3562 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3563 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
3564 (unsigned long) len - data_len);
3565 }
3566
3567 /*
3568 * Make a copy of the frame since we need to modify the buffer during
3569 * MAC validation and Key Data decryption.
3570 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003571 tmp = os_memdup(buf, data_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003572 if (tmp == NULL)
3573 goto out;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003574 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003575 mic = (u8 *) (key + 1);
3576 key_data = mic + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003577
3578 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
3579 {
3580 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3581 "WPA: EAPOL-Key type (%d) unknown, discarded",
3582 key->type);
3583 ret = 0;
3584 goto out;
3585 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003586
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003587 key_data_len = WPA_GET_BE16(mic + mic_len);
3588 wpa_eapol_key_dump(sm, key, key_data_len, mic, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003589
3590 if (key_data_len > plen - keyhdrlen) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003591 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
3592 "frame - key_data overflow (%u > %u)",
3593 (unsigned int) key_data_len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003594 (unsigned int) (plen - keyhdrlen));
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003595 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003596 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003597
Sunil Ravi77d572f2023-01-17 23:58:31 +00003598 if (sm->rx_replay_counter_set &&
3599 os_memcmp(key->replay_counter, sm->rx_replay_counter,
3600 WPA_REPLAY_COUNTER_LEN) <= 0) {
3601 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3602 "WPA: EAPOL-Key Replay Counter did not increase - dropping packet");
3603 goto out;
3604 }
3605
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003606 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
Sunil Ravi77d572f2023-01-17 23:58:31 +00003607
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003608 key_info = WPA_GET_BE16(key->key_info);
Sunil Ravi77d572f2023-01-17 23:58:31 +00003609
3610 if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
3611 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3612 "WPA: Unsupported SMK bit in key_info");
3613 goto out;
3614 }
3615
3616 if (!(key_info & WPA_KEY_INFO_ACK)) {
3617 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3618 "WPA: No Ack bit in key_info");
3619 goto out;
3620 }
3621
3622 if (key_info & WPA_KEY_INFO_REQUEST) {
3623 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3624 "WPA: EAPOL-Key with Request bit - dropped");
3625 goto out;
3626 }
3627
3628 if (sm->proto == WPA_PROTO_WPA) {
3629 ret = wpa_sm_rx_eapol_wpa(sm, src_addr, key, encrypted,
3630 tmp, data_len,
3631 key_data, key_data_len);
3632 goto out;
3633 }
3634
3635 if (key->type != EAPOL_KEY_TYPE_RSN) {
3636 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3637 "RSN: Unsupported EAPOL-Key type %d", key->type);
3638 goto out;
3639 }
3640
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003641 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
3642 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003643 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003644 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07003645 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003646 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003647 "RSN: Unsupported EAPOL-Key descriptor version %d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003648 ver);
3649 goto out;
3650 }
3651
Sunil Ravi77d572f2023-01-17 23:58:31 +00003652 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
3653 sm->pairwise_cipher != WPA_CIPHER_TKIP) {
3654 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3655 "RSN: EAPOL-Key descriptor version %d not allowed without TKIP as the pairwise cipher",
3656 ver);
3657 goto out;
3658 }
3659
3660 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
3661 (sm->key_mgmt != WPA_KEY_MGMT_IEEE8021X &&
3662 sm->key_mgmt != WPA_KEY_MGMT_PSK)) {
3663 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3664 "RSN: EAPOL-Key descriptor version %d not allowed due to negotiated AKM (0x%x)",
3665 ver, sm->key_mgmt);
3666 goto out;
3667 }
3668
Roshan Pius3a1667e2018-07-03 15:17:14 -07003669 if (wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003670 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
3671 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3672 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
3673 ver);
3674 goto out;
3675 }
3676
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003677#ifdef CONFIG_IEEE80211R
3678 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
3679 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
Roshan Pius3a1667e2018-07-03 15:17:14 -07003680 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
3681 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003682 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3683 "FT: AP did not use AES-128-CMAC");
3684 goto out;
3685 }
3686 } else
3687#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003688 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003689 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07003690 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003691 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003692 "RSN: AP did not use the negotiated AES-128-CMAC");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003693 goto out;
3694 }
Hai Shalomc3565922019-10-28 11:58:20 -07003695 } else if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
3696 !wpa_use_akm_defined(sm->key_mgmt) &&
3697 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003698 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003699 "RSN: CCMP is used, but EAPOL-Key descriptor version (%d) is not 2", ver);
3700 if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003701 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003702 "RSN: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
3703 } else {
Jouni Malinen658fb4a2014-11-14 20:57:05 +02003704 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003705 "RSN: Unexpected descriptor version %u", ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003706 goto out;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003707 }
Dmitry Shmidt71757432014-06-02 13:50:35 -07003708 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07003709 !wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt71757432014-06-02 13:50:35 -07003710 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003711 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003712 "RSN: GCMP is used, but EAPOL-Key descriptor version (%d) is not 2",
3713 ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003714 goto out;
3715 }
3716
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003717 if ((key_info & WPA_KEY_INFO_MIC) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003718 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003719 goto out;
3720
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003721#ifdef CONFIG_FILS
3722 if (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
3723 if (wpa_supp_aead_decrypt(sm, tmp, data_len, &key_data_len))
3724 goto out;
3725 }
3726#endif /* CONFIG_FILS */
3727
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003728 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003729 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && mic_len) {
Hai Shalomce48b4a2018-09-05 11:41:35 -07003730 /*
3731 * Only decrypt the Key Data field if the frame's authenticity
3732 * was verified. When using AES-SIV (FILS), the MIC flag is not
3733 * set, so this check should only be performed if mic_len != 0
3734 * which is the case in this code branch.
3735 */
3736 if (!(key_info & WPA_KEY_INFO_MIC)) {
3737 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3738 "WPA: Ignore EAPOL-Key with encrypted but unauthenticated data");
3739 goto out;
3740 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003741 if (wpa_supplicant_decrypt_key_data(sm, key, mic_len,
3742 ver, key_data,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003743 &key_data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003744 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003745 }
3746
3747 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
3748 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
3749 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003750 "RSN: Ignored EAPOL-Key (Pairwise) with non-zero key index");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003751 goto out;
3752 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003753 if (key_info & (WPA_KEY_INFO_MIC |
3754 WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003755 /* 3/4 4-Way Handshake */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003756 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
3757 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003758 } else {
3759 /* 1/4 4-Way Handshake */
3760 wpa_supplicant_process_1_of_4(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003761 ver, key_data,
Sunil8cd6f4d2022-06-28 18:40:46 +00003762 key_data_len,
3763 encrypted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003764 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003765 } else {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003766 if ((mic_len && (key_info & WPA_KEY_INFO_MIC)) ||
3767 (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003768 /* 1/2 Group Key Handshake */
Sunil Ravi77d572f2023-01-17 23:58:31 +00003769 if (sm->mlo.valid_links)
3770 wpa_supplicant_process_mlo_1_of_2(sm, src_addr,
3771 key, key_data,
3772 key_data_len,
3773 ver);
3774 else
3775 wpa_supplicant_process_1_of_2(sm, src_addr, key,
3776 key_data,
3777 key_data_len,
3778 ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003779 } else {
3780 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003781 "RSN: EAPOL-Key (Group) without Mic/Encr bit - dropped");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003782 }
3783 }
3784
3785 ret = 1;
3786
3787out:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003788 bin_clear_free(tmp, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003789 return ret;
3790}
3791
3792
3793#ifdef CONFIG_CTRL_IFACE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003794static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
3795{
3796 switch (sm->key_mgmt) {
3797 case WPA_KEY_MGMT_IEEE8021X:
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003798 return ((sm->proto == WPA_PROTO_RSN ||
3799 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003800 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
3801 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
3802 case WPA_KEY_MGMT_PSK:
3803 return (sm->proto == WPA_PROTO_RSN ?
3804 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
3805 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
3806#ifdef CONFIG_IEEE80211R
3807 case WPA_KEY_MGMT_FT_IEEE8021X:
3808 return RSN_AUTH_KEY_MGMT_FT_802_1X;
3809 case WPA_KEY_MGMT_FT_PSK:
3810 return RSN_AUTH_KEY_MGMT_FT_PSK;
3811#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003812 case WPA_KEY_MGMT_IEEE8021X_SHA256:
3813 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
3814 case WPA_KEY_MGMT_PSK_SHA256:
3815 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003816 case WPA_KEY_MGMT_CCKM:
3817 return (sm->proto == WPA_PROTO_RSN ?
3818 RSN_AUTH_KEY_MGMT_CCKM:
3819 WPA_AUTH_KEY_MGMT_CCKM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003820 case WPA_KEY_MGMT_WPA_NONE:
3821 return WPA_AUTH_KEY_MGMT_NONE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003822 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
3823 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003824 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
3825 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003826 case WPA_KEY_MGMT_IEEE8021X_SHA384:
3827 return RSN_AUTH_KEY_MGMT_802_1X_SHA384;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003828 default:
3829 return 0;
3830 }
3831}
3832
3833
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003834#define RSN_SUITE "%02x-%02x-%02x-%d"
3835#define RSN_SUITE_ARG(s) \
3836((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
3837
3838/**
3839 * wpa_sm_get_mib - Dump text list of MIB entries
3840 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3841 * @buf: Buffer for the list
3842 * @buflen: Length of the buffer
3843 * Returns: Number of bytes written to buffer
3844 *
3845 * This function is used fetch dot11 MIB variables.
3846 */
3847int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
3848{
3849 char pmkid_txt[PMKID_LEN * 2 + 1];
Hai Shalome21d4e82020-04-29 16:34:06 -07003850 bool rsna;
3851 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003852 size_t len;
3853
3854 if (sm->cur_pmksa) {
3855 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
3856 sm->cur_pmksa->pmkid, PMKID_LEN);
3857 } else
3858 pmkid_txt[0] = '\0';
3859
Hai Shalome21d4e82020-04-29 16:34:06 -07003860 rsna = (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
3861 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
3862 sm->proto == WPA_PROTO_RSN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003863
3864 ret = os_snprintf(buf, buflen,
3865 "dot11RSNAOptionImplemented=TRUE\n"
3866 "dot11RSNAPreauthenticationImplemented=TRUE\n"
3867 "dot11RSNAEnabled=%s\n"
3868 "dot11RSNAPreauthenticationEnabled=%s\n"
3869 "dot11RSNAConfigVersion=%d\n"
3870 "dot11RSNAConfigPairwiseKeysSupported=5\n"
3871 "dot11RSNAConfigGroupCipherSize=%d\n"
3872 "dot11RSNAConfigPMKLifetime=%d\n"
3873 "dot11RSNAConfigPMKReauthThreshold=%d\n"
3874 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
3875 "dot11RSNAConfigSATimeout=%d\n",
3876 rsna ? "TRUE" : "FALSE",
3877 rsna ? "TRUE" : "FALSE",
3878 RSN_VERSION,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003879 wpa_cipher_key_len(sm->group_cipher) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003880 sm->dot11RSNAConfigPMKLifetime,
3881 sm->dot11RSNAConfigPMKReauthThreshold,
3882 sm->dot11RSNAConfigSATimeout);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003883 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003884 return 0;
3885 len = ret;
3886
3887 ret = os_snprintf(
3888 buf + len, buflen - len,
3889 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
3890 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
3891 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
3892 "dot11RSNAPMKIDUsed=%s\n"
3893 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
3894 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
3895 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
3896 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
3897 "dot11RSNA4WayHandshakeFailures=%u\n",
3898 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003899 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
3900 sm->pairwise_cipher)),
3901 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
3902 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003903 pmkid_txt,
3904 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003905 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
3906 sm->pairwise_cipher)),
3907 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
3908 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003909 sm->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003910 if (!os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003911 len += ret;
3912
3913 return (int) len;
3914}
3915#endif /* CONFIG_CTRL_IFACE */
3916
3917
3918static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003919 void *ctx, enum pmksa_free_reason reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003920{
3921 struct wpa_sm *sm = ctx;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003922 int deauth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003923
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003924 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
3925 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
3926
3927 if (sm->cur_pmksa == entry) {
3928 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3929 "RSN: %s current PMKSA entry",
3930 reason == PMKSA_REPLACE ? "replaced" : "removed");
3931 pmksa_cache_clear_current(sm);
3932
3933 /*
3934 * If an entry is simply being replaced, there's no need to
3935 * deauthenticate because it will be immediately re-added.
3936 * This happens when EAP authentication is completed again
3937 * (reauth or failed PMKSA caching attempt).
3938 */
3939 if (reason != PMKSA_REPLACE)
3940 deauth = 1;
3941 }
3942
3943 if (reason == PMKSA_EXPIRE &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003944 (sm->pmk_len == entry->pmk_len &&
3945 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
3946 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003947 "RSN: deauthenticating due to expired PMK");
3948 pmksa_cache_clear_current(sm);
3949 deauth = 1;
3950 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003951
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003952 if (deauth) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003953 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003954 os_memset(sm->pmk, 0, sizeof(sm->pmk));
3955 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
3956 }
3957}
3958
3959
Hai Shalomc1a21442022-02-04 13:43:00 -08003960static bool wpa_sm_pmksa_is_current_cb(struct rsn_pmksa_cache_entry *entry,
3961 void *ctx)
3962{
3963 struct wpa_sm *sm = ctx;
3964
3965 return sm->cur_pmksa == entry;
3966}
3967
3968
Sunil Ravi77d572f2023-01-17 23:58:31 +00003969static void wpa_sm_pmksa_notify_cb(struct rsn_pmksa_cache_entry *entry,
3970 void *ctx)
3971{
3972 struct wpa_sm *sm = ctx;
3973
3974 wpa_sm_notify_pmksa_cache_entry(sm, entry);
3975}
3976
3977
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003978/**
3979 * wpa_sm_init - Initialize WPA state machine
3980 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
3981 * Returns: Pointer to the allocated WPA state machine data
3982 *
3983 * This function is used to allocate a new WPA state machine and the returned
3984 * value is passed to all WPA state machine calls.
3985 */
3986struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
3987{
3988 struct wpa_sm *sm;
3989
3990 sm = os_zalloc(sizeof(*sm));
3991 if (sm == NULL)
3992 return NULL;
3993 dl_list_init(&sm->pmksa_candidates);
3994 sm->renew_snonce = 1;
3995 sm->ctx = ctx;
3996
3997 sm->dot11RSNAConfigPMKLifetime = 43200;
3998 sm->dot11RSNAConfigPMKReauthThreshold = 70;
3999 sm->dot11RSNAConfigSATimeout = 60;
4000
Hai Shalomc1a21442022-02-04 13:43:00 -08004001 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb,
Sunil Ravi77d572f2023-01-17 23:58:31 +00004002 wpa_sm_pmksa_is_current_cb,
4003 wpa_sm_pmksa_notify_cb, sm, sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004004 if (sm->pmksa == NULL) {
4005 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
4006 "RSN: PMKSA cache initialization failed");
4007 os_free(sm);
4008 return NULL;
4009 }
4010
4011 return sm;
4012}
4013
4014
4015/**
4016 * wpa_sm_deinit - Deinitialize WPA state machine
4017 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4018 */
4019void wpa_sm_deinit(struct wpa_sm *sm)
4020{
Sunil Ravi77d572f2023-01-17 23:58:31 +00004021 int i;
4022
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004023 if (sm == NULL)
4024 return;
4025 pmksa_cache_deinit(sm->pmksa);
4026 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
4027 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
4028 os_free(sm->assoc_wpa_ie);
Hai Shalomc3565922019-10-28 11:58:20 -07004029 os_free(sm->assoc_rsnxe);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004030 os_free(sm->ap_wpa_ie);
4031 os_free(sm->ap_rsn_ie);
Hai Shalomc3565922019-10-28 11:58:20 -07004032 os_free(sm->ap_rsnxe);
Sunil Ravi77d572f2023-01-17 23:58:31 +00004033 for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
4034 os_free(sm->mlo.links[i].ap_rsne);
4035 os_free(sm->mlo.links[i].ap_rsnxe);
4036 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004037 wpa_sm_drop_sa(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004038 os_free(sm->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004039#ifdef CONFIG_IEEE80211R
4040 os_free(sm->assoc_resp_ies);
4041#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08004042#ifdef CONFIG_TESTING_OPTIONS
4043 wpabuf_free(sm->test_assoc_ie);
4044#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004045#ifdef CONFIG_FILS_SK_PFS
4046 crypto_ecdh_deinit(sm->fils_ecdh);
4047#endif /* CONFIG_FILS_SK_PFS */
4048#ifdef CONFIG_FILS
4049 wpabuf_free(sm->fils_ft_ies);
4050#endif /* CONFIG_FILS */
4051#ifdef CONFIG_OWE
4052 crypto_ecdh_deinit(sm->owe_ecdh);
4053#endif /* CONFIG_OWE */
Hai Shalom021b0b52019-04-10 11:17:58 -07004054#ifdef CONFIG_DPP2
4055 wpabuf_clear_free(sm->dpp_z);
4056#endif /* CONFIG_DPP2 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004057 os_free(sm);
4058}
4059
4060
Sunil Ravi77d572f2023-01-17 23:58:31 +00004061static void wpa_sm_clear_ptk(struct wpa_sm *sm)
4062{
4063 int i;
4064
4065 sm->ptk_set = 0;
4066 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
4067 sm->tptk_set = 0;
4068 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
4069 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
4070 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
4071 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
4072 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004073 os_memset(&sm->bigtk, 0, sizeof(sm->bigtk));
4074 os_memset(&sm->bigtk_wnm_sleep, 0, sizeof(sm->bigtk_wnm_sleep));
Sunil Ravi77d572f2023-01-17 23:58:31 +00004075 sm->tk_set = false;
4076 for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
4077 os_memset(&sm->mlo.links[i].gtk, 0,
4078 sizeof(sm->mlo.links[i].gtk));
4079 os_memset(&sm->mlo.links[i].gtk_wnm_sleep, 0,
4080 sizeof(sm->mlo.links[i].gtk_wnm_sleep));
4081 os_memset(&sm->mlo.links[i].igtk, 0,
4082 sizeof(sm->mlo.links[i].igtk));
4083 os_memset(&sm->mlo.links[i].igtk_wnm_sleep, 0,
4084 sizeof(sm->mlo.links[i].igtk_wnm_sleep));
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004085 os_memset(&sm->mlo.links[i].bigtk, 0,
4086 sizeof(sm->mlo.links[i].bigtk));
4087 os_memset(&sm->mlo.links[i].bigtk_wnm_sleep, 0,
4088 sizeof(sm->mlo.links[i].bigtk_wnm_sleep));
Sunil Ravi77d572f2023-01-17 23:58:31 +00004089 }
4090}
4091
4092
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004093/**
4094 * wpa_sm_notify_assoc - Notify WPA state machine about association
4095 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4096 * @bssid: The BSSID of the new association
4097 *
4098 * This function is called to let WPA state machine know that the connection
4099 * was established.
4100 */
4101void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
4102{
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02004103 int clear_keys = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004104
4105 if (sm == NULL)
4106 return;
4107
4108 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4109 "WPA: Association event - clear replay counter");
4110 os_memcpy(sm->bssid, bssid, ETH_ALEN);
4111 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
4112 sm->rx_replay_counter_set = 0;
4113 sm->renew_snonce = 1;
Sunil Ravieb83e2a2024-06-28 17:34:56 +00004114 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004115 rsn_preauth_deinit(sm);
4116
4117#ifdef CONFIG_IEEE80211R
4118 if (wpa_ft_is_completed(sm)) {
4119 /*
4120 * Clear portValid to kick EAPOL state machine to re-enter
4121 * AUTHENTICATED state to get the EAPOL port Authorized.
4122 */
Hai Shalome21d4e82020-04-29 16:34:06 -07004123 eapol_sm_notify_portValid(sm->eapol, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004124 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
4125
4126 /* Prepare for the next transition */
4127 wpa_ft_prepare_auth_request(sm, NULL);
4128
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02004129 clear_keys = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07004130 sm->ft_protocol = 1;
4131 } else {
4132 sm->ft_protocol = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004133 }
4134#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004135#ifdef CONFIG_FILS
4136 if (sm->fils_completed) {
4137 /*
4138 * Clear portValid to kick EAPOL state machine to re-enter
4139 * AUTHENTICATED state to get the EAPOL port Authorized.
4140 */
4141 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02004142 clear_keys = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004143 }
4144#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004145
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02004146 if (clear_keys) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004147 /*
4148 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
4149 * this is not part of a Fast BSS Transition.
4150 */
4151 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
Sunil Ravi77d572f2023-01-17 23:58:31 +00004152 wpa_sm_clear_ptk(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004153 }
4154
4155#ifdef CONFIG_TDLS
4156 wpa_tdls_assoc(sm);
4157#endif /* CONFIG_TDLS */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004158
4159#ifdef CONFIG_P2P
4160 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
4161#endif /* CONFIG_P2P */
Hai Shalomfdcde762020-04-02 11:19:20 -07004162
4163 sm->keyidx_active = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004164}
4165
4166
4167/**
4168 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
4169 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4170 *
4171 * This function is called to let WPA state machine know that the connection
4172 * was lost. This will abort any existing pre-authentication session.
4173 */
4174void wpa_sm_notify_disassoc(struct wpa_sm *sm)
4175{
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07004176 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
4177 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004178 rsn_preauth_deinit(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004179 pmksa_cache_clear_current(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004180 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
4181 sm->dot11RSNA4WayHandshakeFailures++;
4182#ifdef CONFIG_TDLS
4183 wpa_tdls_disassoc(sm);
4184#endif /* CONFIG_TDLS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004185#ifdef CONFIG_FILS
4186 sm->fils_completed = 0;
4187#endif /* CONFIG_FILS */
Jouni Malinen4283f9e2017-09-22 12:06:37 +03004188#ifdef CONFIG_IEEE80211R
4189 sm->ft_reassoc_completed = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07004190 sm->ft_protocol = 0;
Jouni Malinen4283f9e2017-09-22 12:06:37 +03004191#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004192
4193 /* Keys are not needed in the WPA state machine anymore */
4194 wpa_sm_drop_sa(sm);
Hai Shalomfdcde762020-04-02 11:19:20 -07004195 sm->keyidx_active = 0;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07004196
4197 sm->msg_3_of_4_ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004198 os_memset(sm->bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004199}
4200
4201
4202/**
4203 * wpa_sm_set_pmk - Set PMK
4204 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4205 * @pmk: The new PMK
4206 * @pmk_len: The length of the new PMK in bytes
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004207 * @pmkid: Calculated PMKID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004208 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004209 *
4210 * Configure the PMK for WPA state machine.
4211 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004212void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004213 const u8 *pmkid, const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004214{
4215 if (sm == NULL)
4216 return;
4217
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004218 wpa_hexdump_key(MSG_DEBUG, "WPA: Set PMK based on external data",
4219 pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004220 sm->pmk_len = pmk_len;
4221 os_memcpy(sm->pmk, pmk, pmk_len);
4222
4223#ifdef CONFIG_IEEE80211R
4224 /* Set XXKey to be PSK for FT key derivation */
4225 sm->xxkey_len = pmk_len;
4226 os_memcpy(sm->xxkey, pmk, pmk_len);
4227#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004228
4229 if (bssid) {
Hai Shalomc1a21442022-02-04 13:43:00 -08004230 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len,
4231 pmkid, NULL, 0, bssid,
4232 sm->own_addr,
4233 sm->network_ctx, sm->key_mgmt,
4234 NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004235 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004236}
4237
4238
4239/**
4240 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
4241 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4242 *
4243 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
4244 * will be cleared.
4245 */
4246void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
4247{
4248 if (sm == NULL)
4249 return;
4250
4251 if (sm->cur_pmksa) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004252 wpa_hexdump_key(MSG_DEBUG,
4253 "WPA: Set PMK based on current PMKSA",
4254 sm->cur_pmksa->pmk, sm->cur_pmksa->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004255 sm->pmk_len = sm->cur_pmksa->pmk_len;
4256 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
4257 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004258 wpa_printf(MSG_DEBUG, "WPA: No current PMKSA - clear PMK");
4259 sm->pmk_len = 0;
4260 os_memset(sm->pmk, 0, PMK_LEN_MAX);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004261 }
4262}
4263
4264
4265/**
4266 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
4267 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4268 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
4269 */
4270void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
4271{
4272 if (sm)
4273 sm->fast_reauth = fast_reauth;
4274}
4275
4276
4277/**
4278 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
4279 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4280 * @scard_ctx: Context pointer for smartcard related callback functions
4281 */
4282void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
4283{
4284 if (sm == NULL)
4285 return;
4286 sm->scard_ctx = scard_ctx;
4287 if (sm->preauth_eapol)
4288 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
4289}
4290
4291
4292/**
Hai Shalomfdcde762020-04-02 11:19:20 -07004293 * wpa_sm_set_config - Notification of current configuration change
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004294 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4295 * @config: Pointer to current network configuration
4296 *
4297 * Notify WPA state machine that configuration has changed. config will be
4298 * stored as a backpointer to network configuration. This can be %NULL to clear
4299 * the stored pointed.
4300 */
4301void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
4302{
4303 if (!sm)
4304 return;
4305
4306 if (config) {
4307 sm->network_ctx = config->network_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004308 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
4309 sm->proactive_key_caching = config->proactive_key_caching;
4310 sm->eap_workaround = config->eap_workaround;
4311 sm->eap_conf_ctx = config->eap_conf_ctx;
4312 if (config->ssid) {
4313 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
4314 sm->ssid_len = config->ssid_len;
4315 } else
4316 sm->ssid_len = 0;
4317 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004318 sm->p2p = config->p2p;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004319 sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
Hai Shalomfdcde762020-04-02 11:19:20 -07004320 sm->owe_ptk_workaround = config->owe_ptk_workaround;
Hai Shalom60840252021-02-19 19:02:11 -08004321 sm->force_kdk_derivation = config->force_kdk_derivation;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004322#ifdef CONFIG_FILS
4323 if (config->fils_cache_id) {
4324 sm->fils_cache_id_set = 1;
4325 os_memcpy(sm->fils_cache_id, config->fils_cache_id,
4326 FILS_CACHE_ID_LEN);
4327 } else {
4328 sm->fils_cache_id_set = 0;
4329 }
4330#endif /* CONFIG_FILS */
Hai Shalomfdcde762020-04-02 11:19:20 -07004331 sm->beacon_prot = config->beacon_prot;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004332 } else {
4333 sm->network_ctx = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004334 sm->allowed_pairwise_cipher = 0;
4335 sm->proactive_key_caching = 0;
4336 sm->eap_workaround = 0;
4337 sm->eap_conf_ctx = NULL;
4338 sm->ssid_len = 0;
4339 sm->wpa_ptk_rekey = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004340 sm->p2p = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004341 sm->wpa_rsc_relaxation = 0;
Hai Shalomfdcde762020-04-02 11:19:20 -07004342 sm->owe_ptk_workaround = 0;
4343 sm->beacon_prot = 0;
Hai Shalom60840252021-02-19 19:02:11 -08004344 sm->force_kdk_derivation = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004345 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004346}
4347
4348
Sunil Ravi77d572f2023-01-17 23:58:31 +00004349int wpa_sm_set_mlo_params(struct wpa_sm *sm, const struct wpa_sm_mlo *mlo)
4350{
4351 int i;
4352
4353 if (!sm)
4354 return -1;
4355
4356 os_memcpy(sm->mlo.ap_mld_addr, mlo->ap_mld_addr, ETH_ALEN);
4357 sm->mlo.assoc_link_id = mlo->assoc_link_id;
4358 sm->mlo.valid_links = mlo->valid_links;
4359 sm->mlo.req_links = mlo->req_links;
4360
4361 for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
4362 const u8 *ie;
4363 size_t len;
4364
4365 if (sm->mlo.req_links & BIT(i)) {
4366 if (!mlo->links[i].ap_rsne ||
4367 mlo->links[i].ap_rsne_len == 0) {
4368 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO,
4369 "RSN: No RSNE for AP MLO link %d with BSSID "
4370 MACSTR,
4371 i, MAC2STR(mlo->links[i].bssid));
4372 return -1;
4373
4374 }
4375 os_memcpy(sm->mlo.links[i].addr, mlo->links[i].addr,
4376 ETH_ALEN);
4377 os_memcpy(sm->mlo.links[i].bssid, mlo->links[i].bssid,
4378 ETH_ALEN);
4379 }
4380
4381 ie = mlo->links[i].ap_rsne;
4382 len = mlo->links[i].ap_rsne_len;
4383 os_free(sm->mlo.links[i].ap_rsne);
4384 if (!ie || len == 0) {
4385 if (sm->mlo.links[i].ap_rsne)
4386 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4387 "RSN: Clearing MLO link[%u] AP RSNE",
4388 i);
4389 sm->mlo.links[i].ap_rsne = NULL;
4390 sm->mlo.links[i].ap_rsne_len = 0;
4391 } else {
4392 wpa_hexdump_link(MSG_DEBUG, i, "RSN: Set AP RSNE",
4393 ie, len);
4394 sm->mlo.links[i].ap_rsne = os_memdup(ie, len);
4395 if (!sm->mlo.links[i].ap_rsne) {
4396 sm->mlo.links[i].ap_rsne_len = 0;
4397 return -1;
4398 }
4399 sm->mlo.links[i].ap_rsne_len = len;
4400 }
4401
4402 ie = mlo->links[i].ap_rsnxe;
4403 len = mlo->links[i].ap_rsnxe_len;
4404 os_free(sm->mlo.links[i].ap_rsnxe);
4405 if (!ie || len == 0) {
4406 if (sm->mlo.links[i].ap_rsnxe)
4407 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4408 "RSN: Clearing MLO link[%u] AP RSNXE",
4409 i);
4410 sm->mlo.links[i].ap_rsnxe = NULL;
4411 sm->mlo.links[i].ap_rsnxe_len = 0;
4412 } else {
4413 wpa_hexdump_link(MSG_DEBUG, i, "RSN: Set AP RSNXE", ie,
4414 len);
4415 sm->mlo.links[i].ap_rsnxe = os_memdup(ie, len);
4416 if (!sm->mlo.links[i].ap_rsnxe) {
4417 sm->mlo.links[i].ap_rsnxe_len = 0;
4418 return -1;
4419 }
4420 sm->mlo.links[i].ap_rsnxe_len = len;
4421 }
4422 }
4423
4424 return 0;
4425}
4426
4427
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004428/**
4429 * wpa_sm_set_own_addr - Set own MAC address
4430 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4431 * @addr: Own MAC address
4432 */
4433void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
4434{
4435 if (sm)
4436 os_memcpy(sm->own_addr, addr, ETH_ALEN);
4437}
4438
4439
4440/**
4441 * wpa_sm_set_ifname - Set network interface name
4442 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4443 * @ifname: Interface name
4444 * @bridge_ifname: Optional bridge interface name (for pre-auth)
4445 */
4446void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
4447 const char *bridge_ifname)
4448{
4449 if (sm) {
4450 sm->ifname = ifname;
4451 sm->bridge_ifname = bridge_ifname;
4452 }
4453}
4454
4455
4456/**
4457 * wpa_sm_set_eapol - Set EAPOL state machine pointer
4458 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4459 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
4460 */
4461void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
4462{
4463 if (sm)
4464 sm->eapol = eapol;
4465}
4466
4467
4468/**
4469 * wpa_sm_set_param - Set WPA state machine parameters
4470 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4471 * @param: Parameter field
4472 * @value: Parameter value
4473 * Returns: 0 on success, -1 on failure
4474 */
4475int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
4476 unsigned int value)
4477{
4478 int ret = 0;
4479
4480 if (sm == NULL)
4481 return -1;
4482
4483 switch (param) {
4484 case RSNA_PMK_LIFETIME:
4485 if (value > 0)
4486 sm->dot11RSNAConfigPMKLifetime = value;
4487 else
4488 ret = -1;
4489 break;
4490 case RSNA_PMK_REAUTH_THRESHOLD:
4491 if (value > 0 && value <= 100)
4492 sm->dot11RSNAConfigPMKReauthThreshold = value;
4493 else
4494 ret = -1;
4495 break;
4496 case RSNA_SA_TIMEOUT:
4497 if (value > 0)
4498 sm->dot11RSNAConfigSATimeout = value;
4499 else
4500 ret = -1;
4501 break;
4502 case WPA_PARAM_PROTO:
4503 sm->proto = value;
4504 break;
4505 case WPA_PARAM_PAIRWISE:
4506 sm->pairwise_cipher = value;
4507 break;
4508 case WPA_PARAM_GROUP:
4509 sm->group_cipher = value;
4510 break;
4511 case WPA_PARAM_KEY_MGMT:
4512 sm->key_mgmt = value;
4513 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004514 case WPA_PARAM_MGMT_GROUP:
4515 sm->mgmt_group_cipher = value;
4516 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004517 case WPA_PARAM_RSN_ENABLED:
4518 sm->rsn_enabled = value;
4519 break;
4520 case WPA_PARAM_MFP:
4521 sm->mfp = value;
4522 break;
Hai Shalom74f70d42019-02-11 14:42:39 -08004523 case WPA_PARAM_OCV:
4524 sm->ocv = value;
4525 break;
Hai Shalomc3565922019-10-28 11:58:20 -07004526 case WPA_PARAM_SAE_PWE:
4527 sm->sae_pwe = value;
4528 break;
Hai Shalom899fcc72020-10-19 14:38:18 -07004529 case WPA_PARAM_SAE_PK:
4530 sm->sae_pk = value;
4531 break;
Hai Shalomfdcde762020-04-02 11:19:20 -07004532 case WPA_PARAM_DENY_PTK0_REKEY:
4533 sm->wpa_deny_ptk0_rekey = value;
4534 break;
4535 case WPA_PARAM_EXT_KEY_ID:
4536 sm->ext_key_id = value;
4537 break;
4538 case WPA_PARAM_USE_EXT_KEY_ID:
4539 sm->use_ext_key_id = value;
4540 break;
Hai Shalomb755a2a2020-04-23 21:49:02 -07004541#ifdef CONFIG_TESTING_OPTIONS
4542 case WPA_PARAM_FT_RSNXE_USED:
4543 sm->ft_rsnxe_used = value;
4544 break;
Hai Shalom899fcc72020-10-19 14:38:18 -07004545 case WPA_PARAM_OCI_FREQ_EAPOL:
4546 sm->oci_freq_override_eapol = value;
4547 break;
4548 case WPA_PARAM_OCI_FREQ_EAPOL_G2:
4549 sm->oci_freq_override_eapol_g2 = value;
4550 break;
4551 case WPA_PARAM_OCI_FREQ_FT_ASSOC:
4552 sm->oci_freq_override_ft_assoc = value;
4553 break;
4554 case WPA_PARAM_OCI_FREQ_FILS_ASSOC:
4555 sm->oci_freq_override_fils_assoc = value;
4556 break;
Sunil Ravia04bd252022-05-02 22:54:18 -07004557 case WPA_PARAM_DISABLE_EAPOL_G2_TX:
4558 sm->disable_eapol_g2_tx = value;
4559 break;
Hai Shalomb755a2a2020-04-23 21:49:02 -07004560#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004561#ifdef CONFIG_DPP2
4562 case WPA_PARAM_DPP_PFS:
4563 sm->dpp_pfs = value;
4564 break;
4565#endif /* CONFIG_DPP2 */
Sunil Ravi640215c2023-06-28 23:08:09 +00004566 case WPA_PARAM_WMM_ENABLED:
4567 sm->wmm_enabled = value;
4568 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004569 default:
4570 break;
4571 }
4572
4573 return ret;
4574}
4575
4576
4577/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004578 * wpa_sm_get_status - Get WPA state machine
4579 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4580 * @buf: Buffer for status information
4581 * @buflen: Maximum buffer length
4582 * @verbose: Whether to include verbose status information
4583 * Returns: Number of bytes written to buf.
4584 *
4585 * Query WPA state machine for status information. This function fills in
4586 * a text area with current status information. If the buffer (buf) is not
4587 * large enough, status information will be truncated to fit the buffer.
4588 */
4589int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
4590 int verbose)
4591{
4592 char *pos = buf, *end = buf + buflen;
4593 int ret;
4594
4595 ret = os_snprintf(pos, end - pos,
4596 "pairwise_cipher=%s\n"
4597 "group_cipher=%s\n"
4598 "key_mgmt=%s\n",
4599 wpa_cipher_txt(sm->pairwise_cipher),
4600 wpa_cipher_txt(sm->group_cipher),
4601 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004602 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004603 return pos - buf;
4604 pos += ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004605
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004606#ifdef CONFIG_DPP2
4607 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
4608 ret = os_snprintf(pos, end - pos, "dpp_pfs=1\n");
4609 if (os_snprintf_error(end - pos, ret))
4610 return pos - buf;
4611 pos += ret;
4612 }
4613#endif /* CONFIG_DPP2 */
4614
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004615 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
4616 struct wpa_ie_data rsn;
4617 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
4618 >= 0 &&
4619 rsn.capabilities & (WPA_CAPABILITY_MFPR |
4620 WPA_CAPABILITY_MFPC)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004621 ret = os_snprintf(pos, end - pos, "pmf=%d\n"
4622 "mgmt_group_cipher=%s\n",
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004623 (rsn.capabilities &
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004624 WPA_CAPABILITY_MFPR) ? 2 : 1,
4625 wpa_cipher_txt(
4626 sm->mgmt_group_cipher));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004627 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004628 return pos - buf;
4629 pos += ret;
4630 }
4631 }
4632
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004633 return pos - buf;
4634}
4635
4636
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004637int wpa_sm_pmf_enabled(struct wpa_sm *sm)
4638{
4639 struct wpa_ie_data rsn;
4640
4641 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
4642 return 0;
4643
4644 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
4645 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
4646 return 1;
4647
4648 return 0;
4649}
4650
4651
Hai Shalomfdcde762020-04-02 11:19:20 -07004652int wpa_sm_ext_key_id(struct wpa_sm *sm)
4653{
4654 return sm ? sm->ext_key_id : 0;
4655}
4656
4657
4658int wpa_sm_ext_key_id_active(struct wpa_sm *sm)
4659{
4660 return sm ? sm->use_ext_key_id : 0;
4661}
4662
4663
Hai Shalom74f70d42019-02-11 14:42:39 -08004664int wpa_sm_ocv_enabled(struct wpa_sm *sm)
4665{
4666 struct wpa_ie_data rsn;
4667
4668 if (!sm->ocv || !sm->ap_rsn_ie)
4669 return 0;
4670
4671 return wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len,
4672 &rsn) >= 0 &&
4673 (rsn.capabilities & WPA_CAPABILITY_OCVC);
4674}
4675
4676
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004677/**
4678 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
4679 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4680 * @wpa_ie: Pointer to buffer for WPA/RSN IE
4681 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
4682 * Returns: 0 on success, -1 on failure
4683 */
4684int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
4685 size_t *wpa_ie_len)
4686{
4687 int res;
4688
4689 if (sm == NULL)
4690 return -1;
4691
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08004692#ifdef CONFIG_TESTING_OPTIONS
4693 if (sm->test_assoc_ie) {
4694 wpa_printf(MSG_DEBUG,
4695 "TESTING: Replace association WPA/RSN IE");
4696 if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
4697 return -1;
4698 os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
4699 wpabuf_len(sm->test_assoc_ie));
4700 res = wpabuf_len(sm->test_assoc_ie);
4701 } else
4702#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004703 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
4704 if (res < 0)
4705 return -1;
4706 *wpa_ie_len = res;
4707
4708 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
4709 wpa_ie, *wpa_ie_len);
4710
4711 if (sm->assoc_wpa_ie == NULL) {
4712 /*
4713 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
4714 * the correct version of the IE even if PMKSA caching is
4715 * aborted (which would remove PMKID from IE generation).
4716 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004717 sm->assoc_wpa_ie = os_memdup(wpa_ie, *wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004718 if (sm->assoc_wpa_ie == NULL)
4719 return -1;
4720
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004721 sm->assoc_wpa_ie_len = *wpa_ie_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004722 } else {
4723 wpa_hexdump(MSG_DEBUG,
4724 "WPA: Leave previously set WPA IE default",
4725 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004726 }
4727
4728 return 0;
4729}
4730
4731
4732/**
4733 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
4734 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4735 * @ie: Pointer to IE data (starting from id)
4736 * @len: IE length
4737 * Returns: 0 on success, -1 on failure
4738 *
4739 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
4740 * Request frame. The IE will be used to override the default value generated
4741 * with wpa_sm_set_assoc_wpa_ie_default().
4742 */
4743int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
4744{
4745 if (sm == NULL)
4746 return -1;
4747
4748 os_free(sm->assoc_wpa_ie);
4749 if (ie == NULL || len == 0) {
4750 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4751 "WPA: clearing own WPA/RSN IE");
4752 sm->assoc_wpa_ie = NULL;
4753 sm->assoc_wpa_ie_len = 0;
4754 } else {
4755 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004756 sm->assoc_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004757 if (sm->assoc_wpa_ie == NULL)
4758 return -1;
4759
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004760 sm->assoc_wpa_ie_len = len;
4761 }
4762
4763 return 0;
4764}
4765
4766
4767/**
Hai Shalomc3565922019-10-28 11:58:20 -07004768 * wpa_sm_set_assoc_rsnxe_default - Generate own RSNXE from configuration
4769 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4770 * @rsnxe: Pointer to buffer for RSNXE
4771 * @rsnxe_len: Pointer to the length of the rsne buffer
4772 * Returns: 0 on success, -1 on failure
4773 */
4774int wpa_sm_set_assoc_rsnxe_default(struct wpa_sm *sm, u8 *rsnxe,
4775 size_t *rsnxe_len)
4776{
4777 int res;
4778
4779 if (!sm)
4780 return -1;
4781
4782 res = wpa_gen_rsnxe(sm, rsnxe, *rsnxe_len);
4783 if (res < 0)
4784 return -1;
4785 *rsnxe_len = res;
4786
4787 wpa_hexdump(MSG_DEBUG, "RSN: Set own RSNXE default", rsnxe, *rsnxe_len);
4788
4789 if (sm->assoc_rsnxe) {
4790 wpa_hexdump(MSG_DEBUG,
4791 "RSN: Leave previously set RSNXE default",
4792 sm->assoc_rsnxe, sm->assoc_rsnxe_len);
4793 } else if (*rsnxe_len > 0) {
4794 /*
4795 * Make a copy of the RSNXE so that 4-Way Handshake gets the
4796 * correct version of the IE even if it gets changed.
4797 */
4798 sm->assoc_rsnxe = os_memdup(rsnxe, *rsnxe_len);
4799 if (!sm->assoc_rsnxe)
4800 return -1;
4801
4802 sm->assoc_rsnxe_len = *rsnxe_len;
4803 }
4804
4805 return 0;
4806}
4807
4808
4809/**
4810 * wpa_sm_set_assoc_rsnxe - Set own RSNXE from (Re)AssocReq
4811 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4812 * @ie: Pointer to IE data (starting from id)
4813 * @len: IE length
4814 * Returns: 0 on success, -1 on failure
4815 *
4816 * Inform WPA state machine about the RSNXE used in (Re)Association Request
4817 * frame. The IE will be used to override the default value generated
4818 * with wpa_sm_set_assoc_rsnxe_default().
4819 */
4820int wpa_sm_set_assoc_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len)
4821{
4822 if (!sm)
4823 return -1;
4824
4825 os_free(sm->assoc_rsnxe);
4826 if (!ie || len == 0) {
4827 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4828 "RSN: clearing own RSNXE");
4829 sm->assoc_rsnxe = NULL;
4830 sm->assoc_rsnxe_len = 0;
4831 } else {
4832 wpa_hexdump(MSG_DEBUG, "RSN: set own RSNXE", ie, len);
4833 sm->assoc_rsnxe = os_memdup(ie, len);
4834 if (!sm->assoc_rsnxe)
4835 return -1;
4836
4837 sm->assoc_rsnxe_len = len;
4838 }
4839
4840 return 0;
4841}
4842
4843
4844/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004845 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
4846 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4847 * @ie: Pointer to IE data (starting from id)
4848 * @len: IE length
4849 * Returns: 0 on success, -1 on failure
4850 *
4851 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
4852 * frame.
4853 */
4854int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
4855{
4856 if (sm == NULL)
4857 return -1;
4858
4859 os_free(sm->ap_wpa_ie);
4860 if (ie == NULL || len == 0) {
4861 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4862 "WPA: clearing AP WPA IE");
4863 sm->ap_wpa_ie = NULL;
4864 sm->ap_wpa_ie_len = 0;
4865 } else {
4866 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004867 sm->ap_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004868 if (sm->ap_wpa_ie == NULL)
4869 return -1;
4870
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004871 sm->ap_wpa_ie_len = len;
4872 }
4873
4874 return 0;
4875}
4876
4877
4878/**
4879 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
4880 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4881 * @ie: Pointer to IE data (starting from id)
4882 * @len: IE length
4883 * Returns: 0 on success, -1 on failure
4884 *
4885 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
4886 * frame.
4887 */
4888int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
4889{
4890 if (sm == NULL)
4891 return -1;
4892
4893 os_free(sm->ap_rsn_ie);
4894 if (ie == NULL || len == 0) {
4895 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4896 "WPA: clearing AP RSN IE");
4897 sm->ap_rsn_ie = NULL;
4898 sm->ap_rsn_ie_len = 0;
4899 } else {
4900 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004901 sm->ap_rsn_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004902 if (sm->ap_rsn_ie == NULL)
4903 return -1;
4904
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004905 sm->ap_rsn_ie_len = len;
4906 }
4907
4908 return 0;
4909}
4910
4911
4912/**
Hai Shalomc3565922019-10-28 11:58:20 -07004913 * wpa_sm_set_ap_rsnxe - Set AP RSNXE from Beacon/ProbeResp
4914 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4915 * @ie: Pointer to IE data (starting from id)
4916 * @len: IE length
4917 * Returns: 0 on success, -1 on failure
4918 *
4919 * Inform WPA state machine about the RSNXE used in Beacon / Probe Response
4920 * frame.
4921 */
4922int wpa_sm_set_ap_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len)
4923{
4924 if (!sm)
4925 return -1;
4926
4927 os_free(sm->ap_rsnxe);
4928 if (!ie || len == 0) {
4929 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: clearing AP RSNXE");
4930 sm->ap_rsnxe = NULL;
4931 sm->ap_rsnxe_len = 0;
4932 } else {
4933 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSNXE", ie, len);
4934 sm->ap_rsnxe = os_memdup(ie, len);
4935 if (!sm->ap_rsnxe)
4936 return -1;
4937
4938 sm->ap_rsnxe_len = len;
4939 }
4940
4941 return 0;
4942}
4943
4944
4945/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004946 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
4947 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4948 * @data: Pointer to data area for parsing results
4949 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
4950 *
4951 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
4952 * parsed data into data.
4953 */
4954int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
4955{
4956 if (sm == NULL)
4957 return -1;
4958
4959 if (sm->assoc_wpa_ie == NULL) {
4960 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4961 "WPA: No WPA/RSN IE available from association info");
4962 return -1;
4963 }
4964 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
4965 return -2;
4966 return 0;
4967}
4968
4969
4970int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
4971{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004972 return pmksa_cache_list(sm->pmksa, buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004973}
4974
4975
Dmitry Shmidt29333592017-01-09 12:27:11 -08004976struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_head(struct wpa_sm *sm)
4977{
4978 return pmksa_cache_head(sm->pmksa);
4979}
4980
4981
4982struct rsn_pmksa_cache_entry *
4983wpa_sm_pmksa_cache_add_entry(struct wpa_sm *sm,
4984 struct rsn_pmksa_cache_entry * entry)
4985{
4986 return pmksa_cache_add_entry(sm->pmksa, entry);
4987}
4988
4989
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004990void wpa_sm_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
4991 const u8 *pmkid, const u8 *bssid,
4992 const u8 *fils_cache_id)
4993{
4994 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
4995 bssid, sm->own_addr, sm->network_ctx,
4996 sm->key_mgmt, fils_cache_id);
4997}
4998
4999
Sunil Ravi77d572f2023-01-17 23:58:31 +00005000int wpa_sm_pmksa_exists(struct wpa_sm *sm, const u8 *bssid, const u8 *own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005001 const void *network_ctx)
5002{
Sunil Ravi77d572f2023-01-17 23:58:31 +00005003 return pmksa_cache_get(sm->pmksa, bssid, own_addr, NULL, network_ctx,
5004 0) != NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005005}
5006
5007
Hai Shalom60840252021-02-19 19:02:11 -08005008struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_get(struct wpa_sm *sm,
5009 const u8 *aa,
5010 const u8 *pmkid,
5011 const void *network_ctx,
5012 int akmp)
5013{
Sunil Ravi77d572f2023-01-17 23:58:31 +00005014 return pmksa_cache_get(sm->pmksa, aa, sm->own_addr, pmkid, network_ctx,
5015 akmp);
5016}
5017
5018
5019void wpa_sm_pmksa_cache_remove(struct wpa_sm *sm,
5020 struct rsn_pmksa_cache_entry *entry)
5021{
5022 if (sm && sm->pmksa)
5023 pmksa_cache_remove(sm->pmksa, entry);
Hai Shalom60840252021-02-19 19:02:11 -08005024}
5025
5026
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005027void wpa_sm_drop_sa(struct wpa_sm *sm)
5028{
5029 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
Sunil Ravi77d572f2023-01-17 23:58:31 +00005030 wpa_sm_clear_ptk(sm);
Roshan Pius3a1667e2018-07-03 15:17:14 -07005031 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005032 os_memset(sm->pmk, 0, sizeof(sm->pmk));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005033#ifdef CONFIG_IEEE80211R
5034 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
Roshan Pius3a1667e2018-07-03 15:17:14 -07005035 sm->xxkey_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005036 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
Roshan Pius3a1667e2018-07-03 15:17:14 -07005037 sm->pmk_r0_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005038 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
Roshan Pius3a1667e2018-07-03 15:17:14 -07005039 sm->pmk_r1_len = 0;
Hai Shalom60840252021-02-19 19:02:11 -08005040#ifdef CONFIG_PASN
5041 os_free(sm->pasn_r1kh);
5042 sm->pasn_r1kh = NULL;
5043 sm->n_pasn_r1kh = 0;
5044#endif /* CONFIG_PASN */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005045#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005046}
5047
5048
Sunil Ravi77d572f2023-01-17 23:58:31 +00005049#ifdef CONFIG_IEEE80211R
5050bool wpa_sm_has_ft_keys(struct wpa_sm *sm, const u8 *md)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005051{
Sunil Ravi77d572f2023-01-17 23:58:31 +00005052 if (!sm)
5053 return false;
5054 if (!wpa_key_mgmt_ft(sm->key_mgmt) ||
5055 os_memcmp(md, sm->key_mobility_domain,
5056 MOBILITY_DOMAIN_ID_LEN) != 0) {
5057 /* Do not allow FT protocol to be used even if we were to have
5058 * an PTK since the mobility domain has changed. */
5059 return false;
5060 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005061 return sm->ptk_set;
5062}
Sunil Ravi77d572f2023-01-17 23:58:31 +00005063#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005064
5065
Hai Shalomfdcde762020-04-02 11:19:20 -07005066int wpa_sm_has_ptk_installed(struct wpa_sm *sm)
5067{
5068 if (!sm)
5069 return 0;
Sunil8cd6f4d2022-06-28 18:40:46 +00005070 return sm->tk_set || sm->ptk.installed;
Hai Shalomfdcde762020-04-02 11:19:20 -07005071}
5072
5073
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005074void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
5075{
5076 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
5077}
5078
5079
5080void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
5081{
Hai Shalomc1a21442022-02-04 13:43:00 -08005082 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0, false);
5083}
5084
5085
5086void wpa_sm_external_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
5087{
5088 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0, true);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005089}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005090
Andy Kuoaba17c12022-04-14 16:05:31 +08005091#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Mir Ali677e7482020-11-12 19:49:02 +05305092void wpa_sm_install_pmk(struct wpa_sm *sm)
5093{
5094 /* In case the driver wants to handle re-assocs, pass it down the PMK. */
Sunil Ravi77d572f2023-01-17 23:58:31 +00005095 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 +05305096 (u8*)sm->pmk, sm->pmk_len, KEY_FLAG_PMK) < 0) {
5097 wpa_hexdump(MSG_DEBUG, "PSK: Install PMK to the driver for driver reassociations",
5098 (u8*)sm->pmk, sm->pmk_len);
5099 /* No harm if the driver doesn't support. */
5100 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
5101 "WPA: Failed to set PMK to the driver");
5102 }
5103}
Mir Alieaaf04e2021-06-07 12:17:29 +05305104
5105void wpa_sm_notify_brcm_ft_reassoc(struct wpa_sm *sm, const u8 *bssid)
5106{
5107 u8 buf[256];
5108 struct wpa_supplicant *wpa_s = sm->ctx->ctx;
5109
5110 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
5111 "WPA: BRCM FT Reassociation event - clear replay counter");
5112 os_memcpy(sm->bssid, bssid, ETH_ALEN);
5113 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
5114 sm->rx_replay_counter_set = 0;
5115
5116 if (wpa_drv_driver_cmd(wpa_s, "GET_FTKEY", (char *)buf, sizeof(buf)) < 0) {
5117 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
5118 "WPA: Failed to get FT KEY information");
5119 wpa_supplicant_deauthenticate(
5120 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
5121
5122 } else {
5123 /* update kck and kek */
5124 os_memcpy(sm->ptk.kck, buf, 16);
5125 os_memcpy(sm->ptk.kek, buf + 16, 16);
5126 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
5127 "WPA: Updated KCK and KEK after FT reassoc");
5128 }
5129}
Andy Kuoaba17c12022-04-14 16:05:31 +08005130#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
5131
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005132
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005133#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005134int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
5135{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005136 u16 keyinfo;
5137 u8 keylen; /* plaintext key len */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005138 u8 *key_rsc;
5139
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005140 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07005141 struct wpa_gtk_data gd;
5142
5143 os_memset(&gd, 0, sizeof(gd));
5144 keylen = wpa_cipher_key_len(sm->group_cipher);
5145 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
5146 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
5147 if (gd.alg == WPA_ALG_NONE) {
5148 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
5149 return -1;
5150 }
5151
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005152 key_rsc = buf + 5;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005153 keyinfo = WPA_GET_LE16(buf + 2);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005154 gd.gtk_len = keylen;
5155 if (gd.gtk_len != buf[4]) {
5156 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
5157 gd.gtk_len, buf[4]);
5158 return -1;
5159 }
5160 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
5161 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
5162 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
5163
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005164 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005165
5166 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
5167 gd.gtk, gd.gtk_len);
Jouni Malinen58c0e962017-10-01 12:12:24 +03005168 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07005169 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005170 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
5171 "WNM mode");
5172 return -1;
5173 }
Hai Shalom81f62d82019-07-22 12:10:00 -07005174 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005175 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02005176 const struct wpa_igtk_kde *igtk;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07005177
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02005178 igtk = (const struct wpa_igtk_kde *) (buf + 2);
Jouni Malinen58c0e962017-10-01 12:12:24 +03005179 if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005180 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07005181 } else if (subelem_id == WNM_SLEEP_SUBELEM_BIGTK) {
5182 const struct wpa_bigtk_kde *bigtk;
5183
5184 bigtk = (const struct wpa_bigtk_kde *) (buf + 2);
5185 if (sm->beacon_prot &&
5186 wpa_supplicant_install_bigtk(sm, bigtk, 1) < 0)
5187 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005188 } else {
5189 wpa_printf(MSG_DEBUG, "Unknown element id");
5190 return -1;
5191 }
5192
5193 return 0;
5194}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005195#endif /* CONFIG_WNM */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005196
5197
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005198#ifdef CONFIG_P2P
5199
5200int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
5201{
5202 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
5203 return -1;
5204 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
5205 return 0;
5206}
5207
5208#endif /* CONFIG_P2P */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005209
5210
5211void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
5212{
5213 if (rx_replay_counter == NULL)
5214 return;
5215
5216 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
5217 WPA_REPLAY_COUNTER_LEN);
5218 sm->rx_replay_counter_set = 1;
5219 wpa_printf(MSG_DEBUG, "Updated key replay counter");
5220}
5221
5222
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005223void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
5224 const u8 *ptk_kck, size_t ptk_kck_len,
5225 const u8 *ptk_kek, size_t ptk_kek_len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005226{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005227 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
5228 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
5229 sm->ptk.kck_len = ptk_kck_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005230 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
5231 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005232 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
5233 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
5234 sm->ptk.kek_len = ptk_kek_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005235 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
5236 }
5237 sm->ptk_set = 1;
5238}
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08005239
5240
5241#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005242
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08005243void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
5244{
5245 wpabuf_free(sm->test_assoc_ie);
5246 sm->test_assoc_ie = buf;
5247}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005248
5249
5250const u8 * wpa_sm_get_anonce(struct wpa_sm *sm)
5251{
5252 return sm->anonce;
5253}
5254
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08005255#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005256
5257
Roshan Pius3a1667e2018-07-03 15:17:14 -07005258unsigned int wpa_sm_get_key_mgmt(struct wpa_sm *sm)
5259{
5260 return sm->key_mgmt;
5261}
5262
5263
Sunil Ravi77d572f2023-01-17 23:58:31 +00005264const u8 * wpa_sm_get_auth_addr(struct wpa_sm *sm)
5265{
5266 return sm->mlo.valid_links ? sm->mlo.ap_mld_addr : sm->bssid;
5267}
5268
5269
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005270#ifdef CONFIG_FILS
5271
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005272struct wpabuf * fils_build_auth(struct wpa_sm *sm, int dh_group, const u8 *md)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005273{
5274 struct wpabuf *buf = NULL;
5275 struct wpabuf *erp_msg;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005276 struct wpabuf *pub = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005277
5278 erp_msg = eapol_sm_build_erp_reauth_start(sm->eapol);
5279 if (!erp_msg && !sm->cur_pmksa) {
5280 wpa_printf(MSG_DEBUG,
5281 "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS");
5282 goto fail;
5283 }
5284
5285 wpa_printf(MSG_DEBUG, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)",
5286 erp_msg != NULL, sm->cur_pmksa != NULL);
5287
5288 sm->fils_completed = 0;
5289
5290 if (!sm->assoc_wpa_ie) {
5291 wpa_printf(MSG_INFO, "FILS: No own RSN IE set for FILS");
5292 goto fail;
5293 }
5294
5295 if (random_get_bytes(sm->fils_nonce, FILS_NONCE_LEN) < 0 ||
5296 random_get_bytes(sm->fils_session, FILS_SESSION_LEN) < 0)
5297 goto fail;
5298
5299 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Nonce",
5300 sm->fils_nonce, FILS_NONCE_LEN);
5301 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Session",
5302 sm->fils_session, FILS_SESSION_LEN);
5303
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005304#ifdef CONFIG_FILS_SK_PFS
5305 sm->fils_dh_group = dh_group;
5306 if (dh_group) {
5307 crypto_ecdh_deinit(sm->fils_ecdh);
5308 sm->fils_ecdh = crypto_ecdh_init(dh_group);
5309 if (!sm->fils_ecdh) {
5310 wpa_printf(MSG_INFO,
5311 "FILS: Could not initialize ECDH with group %d",
5312 dh_group);
5313 goto fail;
5314 }
5315 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
5316 if (!pub)
5317 goto fail;
5318 wpa_hexdump_buf(MSG_DEBUG, "FILS: Element (DH public key)",
5319 pub);
5320 sm->fils_dh_elem_len = wpabuf_len(pub);
5321 }
5322#endif /* CONFIG_FILS_SK_PFS */
5323
5324 buf = wpabuf_alloc(1000 + sm->assoc_wpa_ie_len +
5325 (pub ? wpabuf_len(pub) : 0));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005326 if (!buf)
5327 goto fail;
5328
5329 /* Fields following the Authentication algorithm number field */
5330
5331 /* Authentication Transaction seq# */
5332 wpabuf_put_le16(buf, 1);
5333
5334 /* Status Code */
5335 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
5336
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005337 /* TODO: FILS PK */
5338#ifdef CONFIG_FILS_SK_PFS
5339 if (dh_group) {
5340 /* Finite Cyclic Group */
5341 wpabuf_put_le16(buf, dh_group);
5342 /* Element */
5343 wpabuf_put_buf(buf, pub);
5344 }
5345#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005346
5347 /* RSNE */
5348 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in FILS Authentication frame",
5349 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
5350 wpabuf_put_data(buf, sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
5351
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005352 if (md) {
5353 /* MDE when using FILS for FT initial association */
5354 struct rsn_mdie *mdie;
5355
5356 wpabuf_put_u8(buf, WLAN_EID_MOBILITY_DOMAIN);
5357 wpabuf_put_u8(buf, sizeof(*mdie));
5358 mdie = wpabuf_put(buf, sizeof(*mdie));
5359 os_memcpy(mdie->mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
5360 mdie->ft_capab = 0;
5361 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005362
5363 /* FILS Nonce */
5364 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
5365 wpabuf_put_u8(buf, 1 + FILS_NONCE_LEN); /* Length */
5366 /* Element ID Extension */
5367 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_NONCE);
5368 wpabuf_put_data(buf, sm->fils_nonce, FILS_NONCE_LEN);
5369
5370 /* FILS Session */
5371 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
5372 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
5373 /* Element ID Extension */
5374 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
5375 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
5376
Hai Shalomfdcde762020-04-02 11:19:20 -07005377 /* Wrapped Data */
Paul Stewart092955c2017-02-06 09:13:09 -08005378 sm->fils_erp_pmkid_set = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005379 if (erp_msg) {
5380 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
5381 wpabuf_put_u8(buf, 1 + wpabuf_len(erp_msg)); /* Length */
5382 /* Element ID Extension */
Hai Shalomfdcde762020-04-02 11:19:20 -07005383 wpabuf_put_u8(buf, WLAN_EID_EXT_WRAPPED_DATA);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005384 wpabuf_put_buf(buf, erp_msg);
Paul Stewart092955c2017-02-06 09:13:09 -08005385 /* Calculate pending PMKID here so that we do not need to
5386 * maintain a copy of the EAP-Initiate/Reauth message. */
5387 if (fils_pmkid_erp(sm->key_mgmt, wpabuf_head(erp_msg),
5388 wpabuf_len(erp_msg),
5389 sm->fils_erp_pmkid) == 0)
5390 sm->fils_erp_pmkid_set = 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005391 }
5392
5393 wpa_hexdump_buf(MSG_DEBUG, "RSN: FILS fields for Authentication frame",
5394 buf);
5395
5396fail:
5397 wpabuf_free(erp_msg);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005398 wpabuf_free(pub);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005399 return buf;
5400}
5401
5402
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005403int fils_process_auth(struct wpa_sm *sm, const u8 *bssid, const u8 *data,
5404 size_t len)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005405{
5406 const u8 *pos, *end;
5407 struct ieee802_11_elems elems;
5408 struct wpa_ie_data rsn;
5409 int pmkid_match = 0;
5410 u8 ick[FILS_ICK_MAX_LEN];
5411 size_t ick_len;
5412 int res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005413 struct wpabuf *dh_ss = NULL;
5414 const u8 *g_sta = NULL;
5415 size_t g_sta_len = 0;
5416 const u8 *g_ap = NULL;
Hai Shalom60840252021-02-19 19:02:11 -08005417 size_t g_ap_len = 0, kdk_len;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005418 struct wpabuf *pub = NULL;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005419#ifdef CONFIG_IEEE80211R
5420 struct wpa_ft_ies parse;
5421
5422 os_memset(&parse, 0, sizeof(parse));
5423#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005424
5425 os_memcpy(sm->bssid, bssid, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005426
5427 wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
5428 data, len);
5429 pos = data;
5430 end = data + len;
5431
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005432 /* TODO: FILS PK */
5433#ifdef CONFIG_FILS_SK_PFS
5434 if (sm->fils_dh_group) {
5435 u16 group;
5436
5437 /* Using FILS PFS */
5438
5439 /* Finite Cyclic Group */
5440 if (end - pos < 2) {
5441 wpa_printf(MSG_DEBUG,
5442 "FILS: No room for Finite Cyclic Group");
5443 goto fail;
5444 }
5445 group = WPA_GET_LE16(pos);
5446 pos += 2;
5447 if (group != sm->fils_dh_group) {
5448 wpa_printf(MSG_DEBUG,
5449 "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)",
5450 group, sm->fils_dh_group);
5451 goto fail;
5452 }
5453
5454 /* Element */
5455 if ((size_t) (end - pos) < sm->fils_dh_elem_len) {
5456 wpa_printf(MSG_DEBUG, "FILS: No room for Element");
5457 goto fail;
5458 }
5459
5460 if (!sm->fils_ecdh) {
5461 wpa_printf(MSG_DEBUG, "FILS: No ECDH state available");
5462 goto fail;
5463 }
5464 dh_ss = crypto_ecdh_set_peerkey(sm->fils_ecdh, 1, pos,
5465 sm->fils_dh_elem_len);
5466 if (!dh_ss) {
5467 wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
5468 goto fail;
5469 }
5470 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", dh_ss);
5471 g_ap = pos;
5472 g_ap_len = sm->fils_dh_elem_len;
5473 pos += sm->fils_dh_elem_len;
5474 }
5475#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005476
5477 wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
5478 if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
5479 wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005480 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005481 }
5482
5483 /* RSNE */
5484 wpa_hexdump(MSG_DEBUG, "FILS: RSN element", elems.rsn_ie,
5485 elems.rsn_ie_len);
5486 if (!elems.rsn_ie ||
5487 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
5488 &rsn) < 0) {
5489 wpa_printf(MSG_DEBUG, "FILS: No RSN element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005490 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005491 }
5492
5493 if (!elems.fils_nonce) {
5494 wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005495 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005496 }
5497 os_memcpy(sm->fils_anonce, elems.fils_nonce, FILS_NONCE_LEN);
5498 wpa_hexdump(MSG_DEBUG, "FILS: ANonce", sm->fils_anonce, FILS_NONCE_LEN);
5499
Roshan Pius3a1667e2018-07-03 15:17:14 -07005500#ifdef CONFIG_IEEE80211R
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005501 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005502 if (!elems.mdie || !elems.ftie) {
5503 wpa_printf(MSG_DEBUG, "FILS+FT: No MDE or FTE");
5504 goto fail;
5505 }
5506
Roshan Pius3a1667e2018-07-03 15:17:14 -07005507 if (wpa_ft_parse_ies(pos, end - pos, &parse,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005508 sm->key_mgmt, false) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005509 wpa_printf(MSG_DEBUG, "FILS+FT: Failed to parse IEs");
5510 goto fail;
5511 }
5512
5513 if (!parse.r0kh_id) {
5514 wpa_printf(MSG_DEBUG,
5515 "FILS+FT: No R0KH-ID subelem in FTE");
5516 goto fail;
5517 }
5518 os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
5519 sm->r0kh_id_len = parse.r0kh_id_len;
5520 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
5521 sm->r0kh_id, sm->r0kh_id_len);
5522
5523 if (!parse.r1kh_id) {
5524 wpa_printf(MSG_DEBUG,
5525 "FILS+FT: No R1KH-ID subelem in FTE");
5526 goto fail;
5527 }
5528 os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
5529 wpa_hexdump(MSG_DEBUG, "FILS+FT: R1KH-ID",
5530 sm->r1kh_id, FT_R1KH_ID_LEN);
5531
5532 /* TODO: Check MDE and FTE payload */
5533
5534 wpabuf_free(sm->fils_ft_ies);
5535 sm->fils_ft_ies = wpabuf_alloc(2 + elems.mdie_len +
5536 2 + elems.ftie_len);
5537 if (!sm->fils_ft_ies)
5538 goto fail;
5539 wpabuf_put_data(sm->fils_ft_ies, elems.mdie - 2,
5540 2 + elems.mdie_len);
5541 wpabuf_put_data(sm->fils_ft_ies, elems.ftie - 2,
5542 2 + elems.ftie_len);
5543 } else {
5544 wpabuf_free(sm->fils_ft_ies);
5545 sm->fils_ft_ies = NULL;
5546 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07005547#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005548
5549 /* PMKID List */
5550 if (rsn.pmkid && rsn.num_pmkid > 0) {
5551 wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
5552 rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
5553
5554 if (rsn.num_pmkid != 1) {
5555 wpa_printf(MSG_DEBUG, "FILS: Invalid PMKID selection");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005556 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005557 }
5558 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", rsn.pmkid, PMKID_LEN);
5559 if (os_memcmp(sm->cur_pmksa->pmkid, rsn.pmkid, PMKID_LEN) != 0)
5560 {
5561 wpa_printf(MSG_DEBUG, "FILS: PMKID mismatch");
5562 wpa_hexdump(MSG_DEBUG, "FILS: Expected PMKID",
5563 sm->cur_pmksa->pmkid, PMKID_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005564 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005565 }
5566 wpa_printf(MSG_DEBUG,
5567 "FILS: Matching PMKID - continue using PMKSA caching");
5568 pmkid_match = 1;
5569 }
5570 if (!pmkid_match && sm->cur_pmksa) {
5571 wpa_printf(MSG_DEBUG,
5572 "FILS: No PMKID match - cannot use cached PMKSA entry");
5573 sm->cur_pmksa = NULL;
5574 }
5575
5576 /* FILS Session */
5577 if (!elems.fils_session) {
5578 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005579 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005580 }
5581 wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
5582 FILS_SESSION_LEN);
5583 if (os_memcmp(sm->fils_session, elems.fils_session, FILS_SESSION_LEN)
5584 != 0) {
5585 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
5586 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
5587 sm->fils_session, FILS_SESSION_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005588 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005589 }
5590
Hai Shalomfdcde762020-04-02 11:19:20 -07005591 /* Wrapped Data */
5592 if (!sm->cur_pmksa && elems.wrapped_data) {
Paul Stewart092955c2017-02-06 09:13:09 -08005593 u8 rmsk[ERP_MAX_KEY_LEN];
5594 size_t rmsk_len;
5595
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005596 wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
Hai Shalomfdcde762020-04-02 11:19:20 -07005597 elems.wrapped_data,
5598 elems.wrapped_data_len);
5599 eapol_sm_process_erp_finish(sm->eapol, elems.wrapped_data,
5600 elems.wrapped_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005601 if (eapol_sm_failed(sm->eapol))
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005602 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005603
Paul Stewart092955c2017-02-06 09:13:09 -08005604 rmsk_len = ERP_MAX_KEY_LEN;
5605 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
5606 if (res == PMK_LEN) {
5607 rmsk_len = PMK_LEN;
5608 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
5609 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005610 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005611 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005612
Paul Stewart092955c2017-02-06 09:13:09 -08005613 res = fils_rmsk_to_pmk(sm->key_mgmt, rmsk, rmsk_len,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005614 sm->fils_nonce, sm->fils_anonce,
5615 dh_ss ? wpabuf_head(dh_ss) : NULL,
5616 dh_ss ? wpabuf_len(dh_ss) : 0,
Paul Stewart092955c2017-02-06 09:13:09 -08005617 sm->pmk, &sm->pmk_len);
Hai Shalom81f62d82019-07-22 12:10:00 -07005618 forced_memzero(rmsk, sizeof(rmsk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005619
5620 /* Don't use DHss in PTK derivation if PMKSA caching is not
5621 * used. */
5622 wpabuf_clear_free(dh_ss);
5623 dh_ss = NULL;
5624
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005625 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005626 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08005627
5628 if (!sm->fils_erp_pmkid_set) {
5629 wpa_printf(MSG_DEBUG, "FILS: PMKID not available");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005630 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08005631 }
5632 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", sm->fils_erp_pmkid,
5633 PMKID_LEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005634 wpa_printf(MSG_DEBUG, "FILS: ERP processing succeeded - add PMKSA cache entry for the result");
Paul Stewart092955c2017-02-06 09:13:09 -08005635 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
5636 sm->fils_erp_pmkid, NULL, 0,
5637 sm->bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005638 sm->network_ctx, sm->key_mgmt,
5639 NULL);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005640 }
5641
5642 if (!sm->cur_pmksa) {
5643 wpa_printf(MSG_DEBUG,
5644 "FILS: No remaining options to continue FILS authentication");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005645 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005646 }
5647
Hai Shalom60840252021-02-19 19:02:11 -08005648 if (sm->force_kdk_derivation ||
Hai Shalomc1a21442022-02-04 13:43:00 -08005649 (sm->secure_ltf &&
5650 ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
Hai Shalom60840252021-02-19 19:02:11 -08005651 kdk_len = WPA_KDK_MAX_LEN;
5652 else
5653 kdk_len = 0;
5654
Sunil Ravi77d572f2023-01-17 23:58:31 +00005655 if (fils_pmk_to_ptk(sm->pmk, sm->pmk_len, sm->own_addr,
5656 wpa_sm_get_auth_addr(sm),
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005657 sm->fils_nonce, sm->fils_anonce,
5658 dh_ss ? wpabuf_head(dh_ss) : NULL,
5659 dh_ss ? wpabuf_len(dh_ss) : 0,
5660 &sm->ptk, ick, &ick_len,
5661 sm->key_mgmt, sm->pairwise_cipher,
Hai Shalom60840252021-02-19 19:02:11 -08005662 sm->fils_ft, &sm->fils_ft_len,
5663 kdk_len) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005664 wpa_printf(MSG_DEBUG, "FILS: Failed to derive PTK");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005665 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005666 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005667
Sunil Ravi89eba102022-09-13 21:04:37 -07005668#ifdef CONFIG_PASN
5669 if (sm->secure_ltf &&
5670 ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF) &&
5671 wpa_ltf_keyseed(&sm->ptk, sm->key_mgmt, sm->pairwise_cipher)) {
5672 wpa_printf(MSG_DEBUG, "FILS: Failed to derive LTF keyseed");
5673 goto fail;
5674 }
5675#endif /* CONFIG_PASN */
5676
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005677 wpabuf_clear_free(dh_ss);
5678 dh_ss = NULL;
5679
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005680 sm->ptk_set = 1;
5681 sm->tptk_set = 0;
5682 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
5683
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005684#ifdef CONFIG_FILS_SK_PFS
5685 if (sm->fils_dh_group) {
5686 if (!sm->fils_ecdh) {
5687 wpa_printf(MSG_INFO, "FILS: ECDH not initialized");
5688 goto fail;
5689 }
5690 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
5691 if (!pub)
5692 goto fail;
5693 wpa_hexdump_buf(MSG_DEBUG, "FILS: gSTA", pub);
5694 g_sta = wpabuf_head(pub);
5695 g_sta_len = wpabuf_len(pub);
5696 if (!g_ap) {
5697 wpa_printf(MSG_INFO, "FILS: gAP not available");
5698 goto fail;
5699 }
5700 wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len);
5701 }
5702#endif /* CONFIG_FILS_SK_PFS */
5703
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005704 res = fils_key_auth_sk(ick, ick_len, sm->fils_nonce,
5705 sm->fils_anonce, sm->own_addr, sm->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005706 g_sta, g_sta_len, g_ap, g_ap_len,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005707 sm->key_mgmt, sm->fils_key_auth_sta,
5708 sm->fils_key_auth_ap,
5709 &sm->fils_key_auth_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005710 wpabuf_free(pub);
Hai Shalom81f62d82019-07-22 12:10:00 -07005711 forced_memzero(ick, sizeof(ick));
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005712#ifdef CONFIG_IEEE80211R
5713 wpa_ft_parse_ies_free(&parse);
5714#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005715 return res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005716fail:
5717 wpabuf_free(pub);
5718 wpabuf_clear_free(dh_ss);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005719#ifdef CONFIG_IEEE80211R
5720 wpa_ft_parse_ies_free(&parse);
5721#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005722 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005723}
5724
5725
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005726#ifdef CONFIG_IEEE80211R
5727static int fils_ft_build_assoc_req_rsne(struct wpa_sm *sm, struct wpabuf *buf)
5728{
5729 struct rsn_ie_hdr *rsnie;
5730 u16 capab;
5731 u8 *pos;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005732 int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005733
5734 /* RSNIE[PMKR0Name/PMKR1Name] */
5735 rsnie = wpabuf_put(buf, sizeof(*rsnie));
5736 rsnie->elem_id = WLAN_EID_RSN;
5737 WPA_PUT_LE16(rsnie->version, RSN_VERSION);
5738
5739 /* Group Suite Selector */
5740 if (!wpa_cipher_valid_group(sm->group_cipher)) {
5741 wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
5742 sm->group_cipher);
5743 return -1;
5744 }
5745 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
5746 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
5747 sm->group_cipher));
5748
5749 /* Pairwise Suite Count */
5750 wpabuf_put_le16(buf, 1);
5751
5752 /* Pairwise Suite List */
5753 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
5754 wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
5755 sm->pairwise_cipher);
5756 return -1;
5757 }
5758 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
5759 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
5760 sm->pairwise_cipher));
5761
5762 /* Authenticated Key Management Suite Count */
5763 wpabuf_put_le16(buf, 1);
5764
5765 /* Authenticated Key Management Suite List */
5766 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
5767 if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256)
5768 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
5769 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384)
5770 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
5771 else {
5772 wpa_printf(MSG_WARNING,
5773 "FILS+FT: Invalid key management type (%d)",
5774 sm->key_mgmt);
5775 return -1;
5776 }
5777
5778 /* RSN Capabilities */
5779 capab = 0;
Hai Shalomc3565922019-10-28 11:58:20 -07005780 if (sm->mfp)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005781 capab |= WPA_CAPABILITY_MFPC;
Hai Shalomc3565922019-10-28 11:58:20 -07005782 if (sm->mfp == 2)
5783 capab |= WPA_CAPABILITY_MFPR;
Hai Shalom74f70d42019-02-11 14:42:39 -08005784 if (sm->ocv)
5785 capab |= WPA_CAPABILITY_OCVC;
Hai Shalomfdcde762020-04-02 11:19:20 -07005786 if (sm->ext_key_id)
5787 capab |= WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005788 wpabuf_put_le16(buf, capab);
5789
5790 /* PMKID Count */
5791 wpabuf_put_le16(buf, 1);
5792
5793 /* PMKID List [PMKR1Name] */
5794 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: XXKey (FILS-FT)",
5795 sm->fils_ft, sm->fils_ft_len);
5796 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: SSID", sm->ssid, sm->ssid_len);
5797 wpa_hexdump(MSG_DEBUG, "FILS+FT: MDID",
5798 sm->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
5799 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
5800 sm->r0kh_id, sm->r0kh_id_len);
5801 if (wpa_derive_pmk_r0(sm->fils_ft, sm->fils_ft_len, sm->ssid,
5802 sm->ssid_len, sm->mobility_domain,
5803 sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
Sunil Ravi77d572f2023-01-17 23:58:31 +00005804 sm->pmk_r0, sm->pmk_r0_name, sm->key_mgmt) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005805 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMK-R0");
5806 return -1;
5807 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00005808 if (wpa_key_mgmt_sae_ext_key(sm->key_mgmt))
5809 sm->pmk_r0_len = sm->fils_ft_len;
5810 else
5811 sm->pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005812 wpa_printf(MSG_DEBUG, "FILS+FT: R1KH-ID: " MACSTR,
5813 MAC2STR(sm->r1kh_id));
5814 pos = wpabuf_put(buf, WPA_PMK_NAME_LEN);
5815 if (wpa_derive_pmk_r1_name(sm->pmk_r0_name, sm->r1kh_id, sm->own_addr,
Sunil Ravi77d572f2023-01-17 23:58:31 +00005816 sm->pmk_r1_name, sm->fils_ft_len) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005817 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMKR1Name");
5818 return -1;
5819 }
Hai Shalom021b0b52019-04-10 11:17:58 -07005820 os_memcpy(pos, sm->pmk_r1_name, WPA_PMK_NAME_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005821
Sunil Ravi77d572f2023-01-17 23:58:31 +00005822 os_memcpy(sm->key_mobility_domain, sm->mobility_domain,
5823 MOBILITY_DOMAIN_ID_LEN);
5824
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005825 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
5826 /* Management Group Cipher Suite */
5827 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
5828 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
5829 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005830
5831 rsnie->len = ((u8 *) wpabuf_put(buf, 0) - (u8 *) rsnie) - 2;
5832 return 0;
5833}
5834#endif /* CONFIG_IEEE80211R */
5835
5836
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005837struct wpabuf * fils_build_assoc_req(struct wpa_sm *sm, const u8 **kek,
5838 size_t *kek_len, const u8 **snonce,
Paul Stewart092955c2017-02-06 09:13:09 -08005839 const u8 **anonce,
5840 const struct wpabuf **hlp,
5841 unsigned int num_hlp)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005842{
5843 struct wpabuf *buf;
Paul Stewart092955c2017-02-06 09:13:09 -08005844 size_t len;
5845 unsigned int i;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005846
Paul Stewart092955c2017-02-06 09:13:09 -08005847 len = 1000;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005848#ifdef CONFIG_IEEE80211R
5849 if (sm->fils_ft_ies)
5850 len += wpabuf_len(sm->fils_ft_ies);
5851 if (wpa_key_mgmt_ft(sm->key_mgmt))
5852 len += 256;
5853#endif /* CONFIG_IEEE80211R */
Paul Stewart092955c2017-02-06 09:13:09 -08005854 for (i = 0; hlp && i < num_hlp; i++)
5855 len += 10 + wpabuf_len(hlp[i]);
5856 buf = wpabuf_alloc(len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005857 if (!buf)
5858 return NULL;
5859
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005860#ifdef CONFIG_IEEE80211R
5861 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
5862 /* MDE and FTE when using FILS+FT */
5863 wpabuf_put_buf(buf, sm->fils_ft_ies);
5864 /* RSNE with PMKR1Name in PMKID field */
5865 if (fils_ft_build_assoc_req_rsne(sm, buf) < 0) {
5866 wpabuf_free(buf);
5867 return NULL;
5868 }
5869 }
5870#endif /* CONFIG_IEEE80211R */
5871
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005872 /* FILS Session */
5873 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
5874 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
5875 /* Element ID Extension */
5876 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
5877 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
5878
5879 /* Everything after FILS Session element gets encrypted in the driver
5880 * with KEK. The buffer returned from here is the plaintext version. */
5881
5882 /* TODO: FILS Public Key */
5883
5884 /* FILS Key Confirm */
5885 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
5886 wpabuf_put_u8(buf, 1 + sm->fils_key_auth_len); /* Length */
5887 /* Element ID Extension */
5888 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_KEY_CONFIRM);
5889 wpabuf_put_data(buf, sm->fils_key_auth_sta, sm->fils_key_auth_len);
5890
Paul Stewart092955c2017-02-06 09:13:09 -08005891 /* FILS HLP Container */
5892 for (i = 0; hlp && i < num_hlp; i++) {
5893 const u8 *pos = wpabuf_head(hlp[i]);
5894 size_t left = wpabuf_len(hlp[i]);
5895
5896 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
5897 if (left <= 254)
5898 len = 1 + left;
5899 else
5900 len = 255;
5901 wpabuf_put_u8(buf, len); /* Length */
5902 /* Element ID Extension */
5903 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_HLP_CONTAINER);
5904 /* Destination MAC Address, Source MAC Address, HLP Packet.
5905 * HLP Packet is in MSDU format (i.e., included the LLC/SNAP
5906 * header when LPD is used). */
5907 wpabuf_put_data(buf, pos, len - 1);
5908 pos += len - 1;
5909 left -= len - 1;
5910 while (left) {
5911 wpabuf_put_u8(buf, WLAN_EID_FRAGMENT);
5912 len = left > 255 ? 255 : left;
5913 wpabuf_put_u8(buf, len);
5914 wpabuf_put_data(buf, pos, len);
5915 pos += len;
5916 left -= len;
5917 }
5918 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005919
5920 /* TODO: FILS IP Address Assignment */
5921
Hai Shalom74f70d42019-02-11 14:42:39 -08005922#ifdef CONFIG_OCV
5923 if (wpa_sm_ocv_enabled(sm)) {
5924 struct wpa_channel_info ci;
5925 u8 *pos;
5926
5927 if (wpa_sm_channel_info(sm, &ci) != 0) {
5928 wpa_printf(MSG_WARNING,
5929 "FILS: Failed to get channel info for OCI element");
5930 wpabuf_free(buf);
5931 return NULL;
5932 }
Hai Shalom899fcc72020-10-19 14:38:18 -07005933#ifdef CONFIG_TESTING_OPTIONS
5934 if (sm->oci_freq_override_fils_assoc) {
5935 wpa_printf(MSG_INFO,
5936 "TEST: Override OCI KDE frequency %d -> %d MHz",
5937 ci.frequency,
5938 sm->oci_freq_override_fils_assoc);
5939 ci.frequency = sm->oci_freq_override_fils_assoc;
5940 }
5941#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -08005942
5943 pos = wpabuf_put(buf, OCV_OCI_EXTENDED_LEN);
5944 if (ocv_insert_extended_oci(&ci, pos) < 0) {
5945 wpabuf_free(buf);
5946 return NULL;
5947 }
5948 }
5949#endif /* CONFIG_OCV */
5950
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005951 wpa_hexdump_buf(MSG_DEBUG, "FILS: Association Request plaintext", buf);
5952
5953 *kek = sm->ptk.kek;
5954 *kek_len = sm->ptk.kek_len;
5955 wpa_hexdump_key(MSG_DEBUG, "FILS: KEK for AEAD", *kek, *kek_len);
5956 *snonce = sm->fils_nonce;
5957 wpa_hexdump(MSG_DEBUG, "FILS: SNonce for AEAD AAD",
5958 *snonce, FILS_NONCE_LEN);
5959 *anonce = sm->fils_anonce;
5960 wpa_hexdump(MSG_DEBUG, "FILS: ANonce for AEAD AAD",
5961 *anonce, FILS_NONCE_LEN);
5962
5963 return buf;
5964}
5965
5966
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005967static void fils_process_hlp_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
5968{
5969 const u8 *pos, *end;
5970
5971 wpa_hexdump(MSG_MSGDUMP, "FILS: HLP response", resp, len);
5972 if (len < 2 * ETH_ALEN)
5973 return;
5974 pos = resp + 2 * ETH_ALEN;
5975 end = resp + len;
5976 if (end - pos >= 6 &&
5977 os_memcmp(pos, "\xaa\xaa\x03\x00\x00\x00", 6) == 0)
5978 pos += 6; /* Remove SNAP/LLC header */
5979 wpa_sm_fils_hlp_rx(sm, resp, resp + ETH_ALEN, pos, end - pos);
5980}
5981
5982
5983static void fils_process_hlp_container(struct wpa_sm *sm, const u8 *pos,
5984 size_t len)
5985{
5986 const u8 *end = pos + len;
5987 u8 *tmp, *tmp_pos;
5988
5989 /* Check if there are any FILS HLP Container elements */
5990 while (end - pos >= 2) {
5991 if (2 + pos[1] > end - pos)
5992 return;
5993 if (pos[0] == WLAN_EID_EXTENSION &&
5994 pos[1] >= 1 + 2 * ETH_ALEN &&
5995 pos[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER)
5996 break;
5997 pos += 2 + pos[1];
5998 }
5999 if (end - pos < 2)
6000 return; /* No FILS HLP Container elements */
6001
6002 tmp = os_malloc(end - pos);
6003 if (!tmp)
6004 return;
6005
6006 while (end - pos >= 2) {
6007 if (2 + pos[1] > end - pos ||
6008 pos[0] != WLAN_EID_EXTENSION ||
6009 pos[1] < 1 + 2 * ETH_ALEN ||
6010 pos[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER)
6011 break;
6012 tmp_pos = tmp;
6013 os_memcpy(tmp_pos, pos + 3, pos[1] - 1);
6014 tmp_pos += pos[1] - 1;
6015 pos += 2 + pos[1];
6016
6017 /* Add possible fragments */
6018 while (end - pos >= 2 && pos[0] == WLAN_EID_FRAGMENT &&
6019 2 + pos[1] <= end - pos) {
6020 os_memcpy(tmp_pos, pos + 2, pos[1]);
6021 tmp_pos += pos[1];
6022 pos += 2 + pos[1];
6023 }
6024
6025 fils_process_hlp_resp(sm, tmp, tmp_pos - tmp);
6026 }
6027
6028 os_free(tmp);
6029}
6030
6031
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006032int fils_process_assoc_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
6033{
6034 const struct ieee80211_mgmt *mgmt;
6035 const u8 *end, *ie_start;
6036 struct ieee802_11_elems elems;
6037 int keylen, rsclen;
6038 enum wpa_alg alg;
6039 struct wpa_gtk_data gd;
6040 int maxkeylen;
6041 struct wpa_eapol_ie_parse kde;
6042
6043 if (!sm || !sm->ptk_set) {
6044 wpa_printf(MSG_DEBUG, "FILS: No KEK available");
6045 return -1;
6046 }
6047
6048 if (!wpa_key_mgmt_fils(sm->key_mgmt)) {
6049 wpa_printf(MSG_DEBUG, "FILS: Not a FILS AKM");
6050 return -1;
6051 }
6052
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006053 if (sm->fils_completed) {
6054 wpa_printf(MSG_DEBUG,
6055 "FILS: Association has already been completed for this FILS authentication - ignore unexpected retransmission");
6056 return -1;
6057 }
6058
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006059 wpa_hexdump(MSG_DEBUG, "FILS: (Re)Association Response frame",
6060 resp, len);
6061
6062 mgmt = (const struct ieee80211_mgmt *) resp;
6063 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp))
6064 return -1;
6065
6066 end = resp + len;
6067 /* Same offset for Association Response and Reassociation Response */
6068 ie_start = mgmt->u.assoc_resp.variable;
6069
6070 if (ieee802_11_parse_elems(ie_start, end - ie_start, &elems, 1) ==
6071 ParseFailed) {
6072 wpa_printf(MSG_DEBUG,
6073 "FILS: Failed to parse decrypted elements");
6074 goto fail;
6075 }
6076
6077 if (!elems.fils_session) {
6078 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
6079 return -1;
6080 }
6081 if (os_memcmp(elems.fils_session, sm->fils_session,
6082 FILS_SESSION_LEN) != 0) {
6083 wpa_printf(MSG_DEBUG, "FILS: FILS Session mismatch");
6084 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
6085 elems.fils_session, FILS_SESSION_LEN);
6086 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
6087 sm->fils_session, FILS_SESSION_LEN);
6088 }
6089
Hai Shalom81f62d82019-07-22 12:10:00 -07006090 if (!elems.rsn_ie) {
6091 wpa_printf(MSG_DEBUG,
6092 "FILS: No RSNE in (Re)Association Response");
6093 /* As an interop workaround, allow this for now since IEEE Std
6094 * 802.11ai-2016 did not include all the needed changes to make
6095 * a FILS AP include RSNE in the frame. This workaround might
6096 * eventually be removed and replaced with rejection (goto fail)
6097 * to follow a strict interpretation of the standard. */
6098 } else if (wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
6099 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
6100 elems.rsn_ie - 2, elems.rsn_ie_len + 2)) {
6101 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
6102 "FILS: RSNE mismatch between Beacon/Probe Response and (Re)Association Response");
6103 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in Beacon/Probe Response",
6104 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
6105 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in (Re)Association Response",
6106 elems.rsn_ie, elems.rsn_ie_len);
6107 goto fail;
6108 }
6109
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006110 /* TODO: FILS Public Key */
6111
6112 if (!elems.fils_key_confirm) {
6113 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
6114 goto fail;
6115 }
6116 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
6117 wpa_printf(MSG_DEBUG,
6118 "FILS: Unexpected Key-Auth length %d (expected %d)",
6119 elems.fils_key_confirm_len,
6120 (int) sm->fils_key_auth_len);
6121 goto fail;
6122 }
6123 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_ap,
6124 sm->fils_key_auth_len) != 0) {
6125 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
6126 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
6127 elems.fils_key_confirm,
6128 elems.fils_key_confirm_len);
6129 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
6130 sm->fils_key_auth_ap, sm->fils_key_auth_len);
6131 goto fail;
6132 }
6133
Hai Shalom74f70d42019-02-11 14:42:39 -08006134#ifdef CONFIG_OCV
6135 if (wpa_sm_ocv_enabled(sm)) {
6136 struct wpa_channel_info ci;
6137
6138 if (wpa_sm_channel_info(sm, &ci) != 0) {
6139 wpa_printf(MSG_WARNING,
6140 "Failed to get channel info to validate received OCI in FILS (Re)Association Response frame");
6141 goto fail;
6142 }
6143
6144 if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci,
6145 channel_width_to_int(ci.chanwidth),
Hai Shalom899fcc72020-10-19 14:38:18 -07006146 ci.seg1_idx) != OCI_SUCCESS) {
6147 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
6148 "addr=" MACSTR " frame=fils-assoc error=%s",
6149 MAC2STR(sm->bssid), ocv_errorstr);
Hai Shalom74f70d42019-02-11 14:42:39 -08006150 goto fail;
6151 }
6152 }
6153#endif /* CONFIG_OCV */
6154
Hai Shalom021b0b52019-04-10 11:17:58 -07006155#ifdef CONFIG_IEEE80211R
6156 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
6157 struct wpa_ie_data rsn;
6158
6159 /* Check that PMKR1Name derived by the AP matches */
6160 if (!elems.rsn_ie ||
6161 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
6162 &rsn) < 0 ||
6163 !rsn.pmkid || rsn.num_pmkid != 1 ||
6164 os_memcmp(rsn.pmkid, sm->pmk_r1_name,
6165 WPA_PMK_NAME_LEN) != 0) {
6166 wpa_printf(MSG_DEBUG,
6167 "FILS+FT: No RSNE[PMKR1Name] match in AssocResp");
6168 goto fail;
6169 }
6170 }
6171#endif /* CONFIG_IEEE80211R */
6172
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006173 /* Key Delivery */
6174 if (!elems.key_delivery) {
6175 wpa_printf(MSG_DEBUG, "FILS: No Key Delivery element");
6176 goto fail;
6177 }
6178
6179 /* Parse GTK and set the key to the driver */
6180 os_memset(&gd, 0, sizeof(gd));
6181 if (wpa_supplicant_parse_ies(elems.key_delivery + WPA_KEY_RSC_LEN,
6182 elems.key_delivery_len - WPA_KEY_RSC_LEN,
6183 &kde) < 0) {
6184 wpa_printf(MSG_DEBUG, "FILS: Failed to parse KDEs");
6185 goto fail;
6186 }
6187 if (!kde.gtk) {
6188 wpa_printf(MSG_DEBUG, "FILS: No GTK KDE");
6189 goto fail;
6190 }
6191 maxkeylen = gd.gtk_len = kde.gtk_len - 2;
6192 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
6193 gd.gtk_len, maxkeylen,
6194 &gd.key_rsc_len, &gd.alg))
6195 goto fail;
6196
6197 wpa_hexdump_key(MSG_DEBUG, "FILS: Received GTK", kde.gtk, kde.gtk_len);
6198 gd.keyidx = kde.gtk[0] & 0x3;
6199 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
6200 !!(kde.gtk[0] & BIT(2)));
6201 if (kde.gtk_len - 2 > sizeof(gd.gtk)) {
6202 wpa_printf(MSG_DEBUG, "FILS: Too long GTK in GTK KDE (len=%lu)",
6203 (unsigned long) kde.gtk_len - 2);
6204 goto fail;
6205 }
6206 os_memcpy(gd.gtk, kde.gtk + 2, kde.gtk_len - 2);
6207
6208 wpa_printf(MSG_DEBUG, "FILS: Set GTK to driver");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006209 if (wpa_supplicant_install_gtk(sm, &gd, elems.key_delivery, 0) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006210 wpa_printf(MSG_DEBUG, "FILS: Failed to set GTK");
6211 goto fail;
6212 }
6213
6214 if (ieee80211w_set_keys(sm, &kde) < 0) {
6215 wpa_printf(MSG_DEBUG, "FILS: Failed to set IGTK");
6216 goto fail;
6217 }
6218
6219 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
6220 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006221 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
6222 wpa_printf(MSG_DEBUG, "FILS: TK length mismatch: %u != %lu",
6223 keylen, (long unsigned int) sm->ptk.tk_len);
6224 goto fail;
6225 }
Hai Shalomfdcde762020-04-02 11:19:20 -07006226
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006227 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
6228 wpa_hexdump_key(MSG_DEBUG, "FILS: Set TK to driver",
6229 sm->ptk.tk, keylen);
Sunil Ravi77d572f2023-01-17 23:58:31 +00006230 if (wpa_sm_set_key(sm, -1, alg, wpa_sm_get_auth_addr(sm), 0, 1,
6231 null_rsc, rsclen,
Hai Shalomfdcde762020-04-02 11:19:20 -07006232 sm->ptk.tk, keylen, KEY_FLAG_PAIRWISE_RX_TX) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006233 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Sunil Ravi77d572f2023-01-17 23:58:31 +00006234 "FILS: Failed to set PTK to the driver (alg=%d keylen=%d auth_addr="
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006235 MACSTR ")",
Sunil Ravi77d572f2023-01-17 23:58:31 +00006236 alg, keylen, MAC2STR(wpa_sm_get_auth_addr(sm)));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006237 goto fail;
6238 }
6239
Hai Shalom60840252021-02-19 19:02:11 -08006240 wpa_sm_store_ptk(sm, sm->bssid, sm->pairwise_cipher,
6241 sm->dot11RSNAConfigPMKLifetime, &sm->ptk);
6242
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006243 /* TODO: TK could be cleared after auth frame exchange now that driver
6244 * takes care of association frame encryption/decryption. */
6245 /* TK is not needed anymore in supplicant */
6246 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006247 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +02006248 sm->ptk.installed = 1;
Sunil8cd6f4d2022-06-28 18:40:46 +00006249 sm->tk_set = true;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006250
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08006251 /* FILS HLP Container */
6252 fils_process_hlp_container(sm, ie_start, end - ie_start);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006253
6254 /* TODO: FILS IP Address Assignment */
6255
6256 wpa_printf(MSG_DEBUG, "FILS: Auth+Assoc completed successfully");
6257 sm->fils_completed = 1;
Hai Shalom81f62d82019-07-22 12:10:00 -07006258 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006259
Hai Shalomfdcde762020-04-02 11:19:20 -07006260 if (kde.transition_disable)
6261 wpa_sm_transition_disable(sm, kde.transition_disable[0]);
6262
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006263 return 0;
6264fail:
Hai Shalom81f62d82019-07-22 12:10:00 -07006265 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006266 return -1;
6267}
6268
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006269
6270void wpa_sm_set_reset_fils_completed(struct wpa_sm *sm, int set)
6271{
6272 if (sm)
6273 sm->fils_completed = !!set;
6274}
6275
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006276#endif /* CONFIG_FILS */
6277
6278
6279int wpa_fils_is_completed(struct wpa_sm *sm)
6280{
6281#ifdef CONFIG_FILS
6282 return sm && sm->fils_completed;
6283#else /* CONFIG_FILS */
6284 return 0;
6285#endif /* CONFIG_FILS */
6286}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006287
6288
6289#ifdef CONFIG_OWE
6290
6291struct wpabuf * owe_build_assoc_req(struct wpa_sm *sm, u16 group)
6292{
6293 struct wpabuf *ie = NULL, *pub = NULL;
6294 size_t prime_len;
6295
6296 if (group == 19)
6297 prime_len = 32;
6298 else if (group == 20)
6299 prime_len = 48;
6300 else if (group == 21)
6301 prime_len = 66;
6302 else
6303 return NULL;
6304
6305 crypto_ecdh_deinit(sm->owe_ecdh);
6306 sm->owe_ecdh = crypto_ecdh_init(group);
6307 if (!sm->owe_ecdh)
6308 goto fail;
6309 sm->owe_group = group;
6310 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
6311 pub = wpabuf_zeropad(pub, prime_len);
6312 if (!pub)
6313 goto fail;
6314
6315 ie = wpabuf_alloc(5 + wpabuf_len(pub));
6316 if (!ie)
6317 goto fail;
6318 wpabuf_put_u8(ie, WLAN_EID_EXTENSION);
6319 wpabuf_put_u8(ie, 1 + 2 + wpabuf_len(pub));
6320 wpabuf_put_u8(ie, WLAN_EID_EXT_OWE_DH_PARAM);
6321 wpabuf_put_le16(ie, group);
6322 wpabuf_put_buf(ie, pub);
6323 wpabuf_free(pub);
6324 wpa_hexdump_buf(MSG_DEBUG, "OWE: Diffie-Hellman Parameter element",
6325 ie);
6326
6327 return ie;
6328fail:
6329 wpabuf_free(pub);
6330 crypto_ecdh_deinit(sm->owe_ecdh);
6331 sm->owe_ecdh = NULL;
6332 return NULL;
6333}
6334
6335
6336int owe_process_assoc_resp(struct wpa_sm *sm, const u8 *bssid,
6337 const u8 *resp_ies, size_t resp_ies_len)
6338{
6339 struct ieee802_11_elems elems;
6340 u16 group;
6341 struct wpabuf *secret, *pub, *hkey;
6342 int res;
6343 u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN];
6344 const char *info = "OWE Key Generation";
6345 const u8 *addr[2];
6346 size_t len[2];
6347 size_t hash_len, prime_len;
6348 struct wpa_ie_data data;
6349
6350 if (!resp_ies ||
6351 ieee802_11_parse_elems(resp_ies, resp_ies_len, &elems, 1) ==
6352 ParseFailed) {
6353 wpa_printf(MSG_INFO,
6354 "OWE: Could not parse Association Response frame elements");
6355 return -1;
6356 }
6357
6358 if (sm->cur_pmksa && elems.rsn_ie &&
6359 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, 2 + elems.rsn_ie_len,
6360 &data) == 0 &&
6361 data.num_pmkid == 1 && data.pmkid &&
6362 os_memcmp(sm->cur_pmksa->pmkid, data.pmkid, PMKID_LEN) == 0) {
6363 wpa_printf(MSG_DEBUG, "OWE: Use PMKSA caching");
6364 wpa_sm_set_pmk_from_pmksa(sm);
6365 return 0;
6366 }
6367
6368 if (!elems.owe_dh) {
6369 wpa_printf(MSG_INFO,
6370 "OWE: No Diffie-Hellman Parameter element found in Association Response frame");
6371 return -1;
6372 }
6373
6374 group = WPA_GET_LE16(elems.owe_dh);
6375 if (group != sm->owe_group) {
6376 wpa_printf(MSG_INFO,
6377 "OWE: Unexpected Diffie-Hellman group in response: %u",
6378 group);
6379 return -1;
6380 }
6381
6382 if (!sm->owe_ecdh) {
6383 wpa_printf(MSG_INFO, "OWE: No ECDH state available");
6384 return -1;
6385 }
6386
6387 if (group == 19)
6388 prime_len = 32;
6389 else if (group == 20)
6390 prime_len = 48;
6391 else if (group == 21)
6392 prime_len = 66;
6393 else
6394 return -1;
6395
6396 secret = crypto_ecdh_set_peerkey(sm->owe_ecdh, 0,
6397 elems.owe_dh + 2,
6398 elems.owe_dh_len - 2);
6399 secret = wpabuf_zeropad(secret, prime_len);
6400 if (!secret) {
6401 wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
6402 return -1;
6403 }
6404 wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret);
6405
6406 /* prk = HKDF-extract(C | A | group, z) */
6407
6408 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
6409 if (!pub) {
6410 wpabuf_clear_free(secret);
6411 return -1;
6412 }
6413
6414 /* PMKID = Truncate-128(Hash(C | A)) */
6415 addr[0] = wpabuf_head(pub);
6416 len[0] = wpabuf_len(pub);
6417 addr[1] = elems.owe_dh + 2;
6418 len[1] = elems.owe_dh_len - 2;
6419 if (group == 19) {
6420 res = sha256_vector(2, addr, len, pmkid);
6421 hash_len = SHA256_MAC_LEN;
6422 } else if (group == 20) {
6423 res = sha384_vector(2, addr, len, pmkid);
6424 hash_len = SHA384_MAC_LEN;
6425 } else if (group == 21) {
6426 res = sha512_vector(2, addr, len, pmkid);
6427 hash_len = SHA512_MAC_LEN;
6428 } else {
6429 res = -1;
6430 hash_len = 0;
6431 }
6432 pub = wpabuf_zeropad(pub, prime_len);
6433 if (res < 0 || !pub) {
6434 wpabuf_free(pub);
6435 wpabuf_clear_free(secret);
6436 return -1;
6437 }
6438
6439 hkey = wpabuf_alloc(wpabuf_len(pub) + elems.owe_dh_len - 2 + 2);
6440 if (!hkey) {
6441 wpabuf_free(pub);
6442 wpabuf_clear_free(secret);
6443 return -1;
6444 }
6445
6446 wpabuf_put_buf(hkey, pub); /* C */
6447 wpabuf_free(pub);
6448 wpabuf_put_data(hkey, elems.owe_dh + 2, elems.owe_dh_len - 2); /* A */
6449 wpabuf_put_le16(hkey, sm->owe_group); /* group */
6450 if (group == 19)
6451 res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
6452 wpabuf_head(secret), wpabuf_len(secret), prk);
6453 else if (group == 20)
6454 res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey),
6455 wpabuf_head(secret), wpabuf_len(secret), prk);
6456 else if (group == 21)
6457 res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey),
6458 wpabuf_head(secret), wpabuf_len(secret), prk);
6459 wpabuf_clear_free(hkey);
6460 wpabuf_clear_free(secret);
6461 if (res < 0)
6462 return -1;
6463
6464 wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
6465
6466 /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
6467
6468 if (group == 19)
6469 res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info,
6470 os_strlen(info), sm->pmk, hash_len);
6471 else if (group == 20)
6472 res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info,
6473 os_strlen(info), sm->pmk, hash_len);
6474 else if (group == 21)
6475 res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info,
6476 os_strlen(info), sm->pmk, hash_len);
Hai Shalom81f62d82019-07-22 12:10:00 -07006477 forced_memzero(prk, SHA512_MAC_LEN);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006478 if (res < 0) {
6479 sm->pmk_len = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006480 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006481 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006482 sm->pmk_len = hash_len;
6483
6484 wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sm->pmk, sm->pmk_len);
6485 wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
6486 pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, pmkid, NULL, 0,
6487 bssid, sm->own_addr, sm->network_ctx, sm->key_mgmt,
6488 NULL);
6489
6490 return 0;
6491}
6492
6493#endif /* CONFIG_OWE */
6494
6495
6496void wpa_sm_set_fils_cache_id(struct wpa_sm *sm, const u8 *fils_cache_id)
6497{
6498#ifdef CONFIG_FILS
6499 if (sm && fils_cache_id) {
6500 sm->fils_cache_id_set = 1;
6501 os_memcpy(sm->fils_cache_id, fils_cache_id, FILS_CACHE_ID_LEN);
6502 }
6503#endif /* CONFIG_FILS */
6504}
Hai Shalom021b0b52019-04-10 11:17:58 -07006505
6506
6507#ifdef CONFIG_DPP2
6508void wpa_sm_set_dpp_z(struct wpa_sm *sm, const struct wpabuf *z)
6509{
6510 if (sm) {
6511 wpabuf_clear_free(sm->dpp_z);
6512 sm->dpp_z = z ? wpabuf_dup(z) : NULL;
6513 }
6514}
6515#endif /* CONFIG_DPP2 */
Hai Shalom60840252021-02-19 19:02:11 -08006516
6517
6518#ifdef CONFIG_PASN
Sunil Ravi89eba102022-09-13 21:04:37 -07006519
Sunil Ravi89eba102022-09-13 21:04:37 -07006520void wpa_pasn_sm_set_caps(struct wpa_sm *sm, unsigned int flags2)
6521{
6522 if (flags2 & WPA_DRIVER_FLAGS2_SEC_LTF_STA)
6523 sm->secure_ltf = 1;
6524 if (flags2 & WPA_DRIVER_FLAGS2_SEC_RTT_STA)
6525 sm->secure_rtt = 1;
6526 if (flags2 & WPA_DRIVER_FLAGS2_PROT_RANGE_NEG_STA)
6527 sm->prot_range_neg = 1;
6528}
6529
Hai Shalom60840252021-02-19 19:02:11 -08006530#endif /* CONFIG_PASN */
Hai Shalomc1a21442022-02-04 13:43:00 -08006531
6532
6533void wpa_sm_pmksa_cache_reconfig(struct wpa_sm *sm)
6534{
6535 if (sm)
6536 pmksa_cache_reconfig(sm->pmksa);
6537}
Sunil Ravi77d572f2023-01-17 23:58:31 +00006538
6539
6540struct rsn_pmksa_cache * wpa_sm_get_pmksa_cache(struct wpa_sm *sm)
6541{
6542 return sm ? sm->pmksa : NULL;
6543}
6544
6545
6546void wpa_sm_set_cur_pmksa(struct wpa_sm *sm,
6547 struct rsn_pmksa_cache_entry *entry)
6548{
6549 if (sm)
6550 sm->cur_pmksa = entry;
6551}
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006552
6553
6554void wpa_sm_set_driver_bss_selection(struct wpa_sm *sm,
6555 bool driver_bss_selection)
6556{
6557 if (sm)
6558 sm->driver_bss_selection = driver_bss_selection;
6559}